43 uint8_t *bytes = (uint8_t *) data;
44 uint16_t crc = 0xFFFF, i;
45 for (i = 0; i < length; i++)
47 crc = (uint16_t)((crc << 8) ^
crc16_table[(crc >> 8) ^ bytes[i]]);
57 return ((data[0] + data[1] + data[2] + data[3]) ^ 0xFF) + 1;
66 if (an_packet != NULL)
69 an_packet->
length = length;
98 uint16_t decode_iterator = 0;
100 uint8_t header_lrc, id, length;
103 while (decode_iterator + AN_PACKET_HEADER_SIZE <= an_decoder->buffer_length)
105 header_lrc = an_decoder->
buffer[decode_iterator++];
108 id = an_decoder->
buffer[decode_iterator++];
109 length = an_decoder->
buffer[decode_iterator++];
110 crc = an_decoder->
buffer[decode_iterator++];
111 crc |= an_decoder->
buffer[decode_iterator++] << 8;
122 if (an_packet != NULL)
125 memcpy(an_packet->
data, &an_decoder->
buffer[decode_iterator], length *
sizeof(uint8_t));
127 decode_iterator += length;
137 if (decode_iterator < an_decoder->buffer_length)
139 if (decode_iterator > 0)
141 memmove(&an_decoder->
buffer[0], &an_decoder->
buffer[decode_iterator], (an_decoder->
buffer_length - decode_iterator) *
sizeof(uint8_t));
156 an_packet->
header[1] = an_packet->
id;
159 memcpy(&an_packet->
header[3], &crc,
sizeof(uint16_t));
static const uint16_t crc16_table[256]
an_packet_t * an_packet_decode(an_decoder_t *an_decoder)
void an_packet_encode(an_packet_t *an_packet)
uint16_t calculate_crc16(const void *data, uint16_t length)
an_packet_t * an_packet_allocate(uint8_t length, uint8_t id)
void an_decoder_initialise(an_decoder_t *an_decoder)
uint8_t buffer[AN_DECODE_BUFFER_SIZE]
void an_packet_free(an_packet_t **an_packet)
uint8_t calculate_header_lrc(uint8_t *data)
#define AN_PACKET_HEADER_SIZE
uint8_t header[AN_PACKET_HEADER_SIZE]