61 #include "utils/crc/PJON_CRC8.h"
62 #include "utils/crc/PJON_CRC32.h"
70 #ifndef PJON_BROADCAST
71 #define PJON_BROADCAST 0
75 #ifndef PJON_NOT_ASSIGNED
76 #define PJON_NOT_ASSIGNED 255
80 #define PJON_FAIL 65535
81 #define PJON_TO_BE_SENT 74
84 #define PJON_SIMPLEX false
85 #define PJON_HALF_DUPLEX true
90 #define PJON_NO_HEADER 0B01001000
93 #define PJON_MODE_BIT 0B00000001
97 #define PJON_TX_INFO_BIT 0B00000010
100 #define PJON_ACK_REQ_BIT 0B00000100
103 #define PJON_MAC_BIT 0B00001000
106 #define PJON_PORT_BIT 0B00010000
109 #define PJON_CRC_BIT 0B00100000
112 #define PJON_EXT_LEN_BIT 0B01000000
115 #define PJON_PACKET_ID_BIT 0B10000000
119 #define PJON_CONNECTION_LOST 101
120 #define PJON_PACKETS_BUFFER_FULL 102
121 #define PJON_CONTENT_TOO_LONG 104
126 #ifndef PJON_MAX_HOPS
127 #define PJON_MAX_HOPS 15
133 #ifndef PJON_MAX_PACKETS
134 #define PJON_MAX_PACKETS 5
140 #ifndef PJON_PACKET_MAX_LENGTH
141 #define PJON_PACKET_MAX_LENGTH 50
145 #ifndef PJON_MAX_RECENT_PACKET_IDS
146 #define PJON_MAX_RECENT_PACKET_IDS 10
152 #ifdef PJON_INCLUDE_PACKET_ID
153 #undef PJON_INCLUDE_PACKET_ID
154 #define PJON_INCLUDE_PACKET_ID true
156 #define PJON_INCLUDE_PACKET_ID false
160 #ifdef PJON_INCLUDE_PORT
161 #undef PJON_INCLUDE_PORT
162 #define PJON_INCLUDE_PORT true
164 #define PJON_INCLUDE_PORT false
168 #ifdef PJON_INCLUDE_MAC
169 #undef PJON_INCLUDE_MAC
170 #define PJON_INCLUDE_MAC true
172 #define PJON_INCLUDE_MAC false
178 uint8_t attempts = 0;
179 uint8_t content[PJON_PACKET_MAX_LENGTH];
181 uint32_t registration;
190 uint8_t sender_bus_id[4];
192 #if(PJON_INCLUDE_PACKET_ID)
198 uint8_t
id = PJON_NOT_ASSIGNED;
200 uint8_t bus_id[4] = {0, 0, 0, 0};
202 #if(PJON_INCLUDE_MAC)
203 uint8_t mac[6] = {0, 0, 0, 0, 0, 0};
210 uint8_t header = PJON_NO_HEADER;
212 void *custom_pointer;
215 #if(PJON_INCLUDE_PACKET_ID)
218 #if(PJON_INCLUDE_PORT)
219 uint16_t port = PJON_BROADCAST;
223 typedef void (* PJON_Receiver)(
229 typedef void (* PJON_Error)(
240 static const uint8_t* localhost()
242 static const uint8_t lh[4] = {0, 0, 0, 0};
248 static const uint8_t* no_mac()
250 static const uint8_t lh[6] = {0, 0, 0, 0, 0, 0};
256 static uint8_t packet_overhead(uint8_t header)
260 (header & PJON_MODE_BIT) ?
261 (header & PJON_TX_INFO_BIT ? 11 : 6) :
262 (header & PJON_TX_INFO_BIT ? 2 : 1)
263 ) + (header & PJON_EXT_LEN_BIT ? 2 : 1)
264 + (header & PJON_CRC_BIT ? 4 : 1)
265 + (header & PJON_PORT_BIT ? 2 : 0)
266 + (header & PJON_PACKET_ID_BIT ? 2 : 0)
267 + (header & PJON_MAC_BIT ? 12 : 0)
274 static uint8_t crc_overhead(uint8_t header)
276 return (header & PJON_CRC_BIT) ? 4 : 1;
281 static uint16_t new_packet_id(uint16_t seed)
291 static void copy_id(uint8_t dest[],
const uint8_t src[], uint8_t length)
293 memcpy(dest, src, length);
298 static bool id_equality(
299 const uint8_t *n_one,
300 const uint8_t *n_two,
304 for(uint8_t i = 0; i < length; i++)
305 if(n_one[i] != n_two[i]) {
313 static uint16_t compose_packet(
322 info.header |= PJON_EXT_LEN_BIT;
324 #if(PJON_INCLUDE_PORT)
325 if(info.port != PJON_BROADCAST) {
326 info.header |= PJON_PORT_BIT;
328 if((info.header & PJON_PORT_BIT) && (info.port == PJON_BROADCAST)) {
329 info.header &= ~PJON_PORT_BIT;
332 if(info.rx.id == PJON_BROADCAST) {
333 info.header &= ~(PJON_ACK_REQ_BIT);
335 uint16_t new_length = length + packet_overhead(info.header);
336 bool extended_length = info.header & PJON_EXT_LEN_BIT;
337 if(new_length > 15 && !(info.header & PJON_CRC_BIT)) {
338 info.header |= PJON_CRC_BIT;
339 new_length = (uint16_t)(length + packet_overhead(info.header));
341 if(new_length > 255 && !extended_length) {
342 info.header |= PJON_EXT_LEN_BIT;
343 new_length = (uint16_t)(length + packet_overhead(info.header));
345 if(new_length >= PJON_PACKET_MAX_LENGTH) {
350 if(extended_length) {
359 if(info.header & PJON_MODE_BIT) {
360 copy_id((uint8_t*) &
destination[index], info.rx.bus_id, 4);
362 if(info.header & PJON_TX_INFO_BIT) {
363 copy_id((uint8_t*) &
destination[index], info.tx.bus_id, 4);
369 if(info.header & PJON_TX_INFO_BIT) {
372 #if(PJON_INCLUDE_PACKET_ID)
373 if(info.header & PJON_PACKET_ID_BIT) {
378 #if(PJON_INCLUDE_PORT)
379 if(info.header & PJON_PORT_BIT) {
380 if(info.port != PJON_BROADCAST) {
386 #if(PJON_INCLUDE_MAC)
387 if(info.header & PJON_MAC_BIT) {
395 if(info.header & PJON_CRC_BIT) {
396 uint32_t computed_crc =
397 PJON_crc32::compute((uint8_t *)
destination, new_length - 4);
399 (uint8_t)((uint32_t)(computed_crc) >> 24);
401 (uint8_t)((uint32_t)(computed_crc) >> 16);
403 (uint8_t)((uint32_t)(computed_crc) >> 8);
405 (uint8_t)((uint32_t)computed_crc);
407 PJON_crc8::compute((uint8_t *)
destination, new_length - 1);
418 info.rx.id = packet[index++];
419 bool extended_length = packet[index] & PJON_EXT_LEN_BIT;
420 info.header = packet[index++];
421 index += extended_length + 2;
423 if(info.header & PJON_MODE_BIT) {
424 copy_id(info.rx.bus_id, packet + index, 4);
426 if(info.header & PJON_TX_INFO_BIT) {
427 copy_id(info.tx.bus_id, packet + index, 4);
430 info.hops = packet[index++];
433 if(info.header & PJON_TX_INFO_BIT) {
434 info.tx.id = packet[index++];
436 #if(PJON_INCLUDE_PACKET_ID)
437 if(info.header & PJON_PACKET_ID_BIT) {
438 info.id = (packet[index] << 8) | (packet[index + 1] & 0xFF);
442 #if(PJON_INCLUDE_PORT)
443 if(info.header & PJON_PORT_BIT) {
444 info.port = (packet[index] << 8) | (packet[index + 1] & 0xFF);
448 #if(PJON_INCLUDE_MAC)
449 copy_id(info.rx.mac, packet + index, 6);
451 copy_id(info.tx.mac, packet + index, 6);