Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <stdlib.h>
00031 #include <stdint.h>
00032 #include <string.h>
00033 #include "an_packet_protocol.h"
00034
00035
00036
00037
00038
00039
00040
00041 uint16_t calculate_crc16(const void *data, uint16_t length)
00042 {
00043 uint8_t *bytes = (uint8_t *) data;
00044 uint16_t crc = 0xFFFF, i;
00045 for (i = 0; i < length; i++)
00046 {
00047 crc = (uint16_t)((crc << 8) ^ crc16_table[(crc >> 8) ^ bytes[i]]);
00048 }
00049 return crc;
00050 }
00051
00052
00053
00054
00055 uint8_t calculate_header_lrc(uint8_t *data)
00056 {
00057 return ((data[0] + data[1] + data[2] + data[3]) ^ 0xFF) + 1;
00058 }
00059
00060
00061
00062
00063 an_packet_t *an_packet_allocate(uint8_t length, uint8_t id)
00064 {
00065 an_packet_t *an_packet = (an_packet_t *)malloc(sizeof(an_packet_t) + length * sizeof(uint8_t));
00066 if (an_packet != NULL)
00067 {
00068 an_packet->id = id;
00069 an_packet->length = length;
00070 }
00071 return an_packet;
00072 }
00073
00074
00075
00076
00077 void an_packet_free(an_packet_t **an_packet)
00078 {
00079 free(*an_packet);
00080 *an_packet = NULL;
00081 }
00082
00083
00084
00085
00086 void an_decoder_initialise(an_decoder_t *an_decoder)
00087 {
00088 an_decoder->buffer_length = 0;
00089 an_decoder->crc_errors = 0;
00090 }
00091
00092
00093
00094
00095
00096 an_packet_t *an_packet_decode(an_decoder_t *an_decoder)
00097 {
00098 uint16_t decode_iterator = 0;
00099 an_packet_t *an_packet = NULL;
00100 uint8_t header_lrc, id, length;
00101 uint16_t crc;
00102
00103 while (decode_iterator + AN_PACKET_HEADER_SIZE <= an_decoder->buffer_length)
00104 {
00105 header_lrc = an_decoder->buffer[decode_iterator++];
00106 if (header_lrc == calculate_header_lrc(&an_decoder->buffer[decode_iterator]))
00107 {
00108 id = an_decoder->buffer[decode_iterator++];
00109 length = an_decoder->buffer[decode_iterator++];
00110 crc = an_decoder->buffer[decode_iterator++];
00111 crc |= an_decoder->buffer[decode_iterator++] << 8;
00112
00113 if (decode_iterator + length > an_decoder->buffer_length)
00114 {
00115 decode_iterator -= AN_PACKET_HEADER_SIZE;
00116 break;
00117 }
00118
00119 if (crc == calculate_crc16(&an_decoder->buffer[decode_iterator], length))
00120 {
00121 an_packet = an_packet_allocate(length, id);
00122 if (an_packet != NULL)
00123 {
00124 memcpy(an_packet->header, &an_decoder->buffer[decode_iterator - AN_PACKET_HEADER_SIZE], AN_PACKET_HEADER_SIZE * sizeof(uint8_t));
00125 memcpy(an_packet->data, &an_decoder->buffer[decode_iterator], length * sizeof(uint8_t));
00126 }
00127 decode_iterator += length;
00128 break;
00129 }
00130 else
00131 {
00132 decode_iterator -= (AN_PACKET_HEADER_SIZE - 1);
00133 an_decoder->crc_errors++;
00134 }
00135 }
00136 }
00137 if (decode_iterator < an_decoder->buffer_length)
00138 {
00139 if (decode_iterator > 0)
00140 {
00141 memmove(&an_decoder->buffer[0], &an_decoder->buffer[decode_iterator], (an_decoder->buffer_length - decode_iterator) * sizeof(uint8_t));
00142 an_decoder->buffer_length -= decode_iterator;
00143 }
00144 }
00145 else an_decoder->buffer_length = 0;
00146
00147 return an_packet;
00148 }
00149
00150
00151
00152
00153 void an_packet_encode(an_packet_t *an_packet)
00154 {
00155 uint16_t crc;
00156 an_packet->header[1] = an_packet->id;
00157 an_packet->header[2] = an_packet->length;
00158 crc = calculate_crc16(an_packet->data, an_packet->length);
00159 memcpy(&an_packet->header[3], &crc, sizeof(uint16_t));
00160 an_packet->header[0] = calculate_header_lrc(&an_packet->header[1]);
00161 }
00162