6 #error ReelTwoSMQ.h must be included before ReelTwo.h
12 #define SMQ_DEBUG_PRINTLN(s) DEBUG_PRINTLN(s)
13 #define SMQ_DEBUG_PRINT(s) DEBUG_PRINT(s)
14 #define SMQ_DEBUG_PRINTLN_HEX(s) DEBUG_PRINTLN_HEX(s)
15 #define SMQ_DEBUG_PRINT_HEX(s) DEBUG_PRINT_HEX(s)
16 #define SMQ_DEBUG_FLUSH(s) DEBUG_FLUSH()
18 #define SMQ_DEBUG_PRINTLN(s)
19 #define SMQ_DEBUG_PRINT(s)
20 #define SMQ_DEBUG_PRINTLN_HEX(s)
21 #define SMQ_DEBUG_PRINT_HEX(s)
22 #define SMQ_DEBUG_FLUSH(s)
25 #define REELTWO_READY() \
30 SMQ_SERIAL.begin(DEFAULT_BAUD_RATE); \
32 SMQ_DEBUG_PRINTLN("Waiting for agent"); \
38 static bool sSMQREADY;
59 while (!
sSMQ->available())
63 int val = read_int8();
71 Message::sendSubscriberList();
76 while (
sSMQ->available())
78 int op =
sSMQ->read();
82 sSMQ->write(&ack,
sizeof(ack));
108 uint8_t delim = 0x00;
109 send_raw_bytes(&delim,
sizeof(delim));
111 size_t len = strlen(str);
112 send_data(&len,
sizeof(len));
118 uint8_t delim = 0x00;
119 send_raw_bytes(&delim,
sizeof(delim));
121 size_t len = strlen_P((
const char*)str);
122 send_data(&len,
sizeof(len));
128 uint8_t delim = 0x01;
129 send_raw_bytes(&delim,
sizeof(delim));
130 send_raw_bytes(&
id,
sizeof(
id));
135 uint8_t delim = 0x01;
136 send_raw_bytes(&delim,
sizeof(delim));
138 uint16_t crc = 0xFFFF;
139 size_t len = strlen(str);
140 const uint8_t* b = (uint8_t*)str;
141 const uint8_t* buf_end = (uint8_t*)str + len;
144 crc = update_crc(crc, *b++);
147 send_raw_bytes(&crc,
sizeof(crc));
222 uint8_t delim = 0x02;
224 send_raw_bytes(&delim,
sizeof(delim));
225 send_data(&val,
sizeof(val));
230 uint8_t delim = 0x02;
232 send_raw_bytes(&delim,
sizeof(delim));
233 send_data(&val,
sizeof(val));
238 uint8_t delim = 0x02;
240 send_raw_bytes(&delim,
sizeof(delim));
241 send_data(&val,
sizeof(val));
246 uint8_t delim = 0x03;
248 send_raw_bytes(&delim,
sizeof(delim));
249 send_data(&val,
sizeof(val));
254 uint8_t delim = 0x03;
256 send_raw_bytes(&delim,
sizeof(delim));
257 send_data(&val,
sizeof(val));
262 uint8_t delim = 0x03;
264 send_raw_bytes(&delim,
sizeof(delim));
265 send_data(&val,
sizeof(val));
270 uint8_t delim = 0x04;
272 send_raw_bytes(&delim,
sizeof(delim));
273 send_data(&val,
sizeof(val));
278 uint8_t delim = 0x04;
280 send_raw_bytes(&delim,
sizeof(delim));
281 send_data(&val,
sizeof(val));
286 uint8_t delim = 0x04;
288 send_raw_bytes(&delim,
sizeof(delim));
289 send_data(&val,
sizeof(val));
294 uint8_t delim = 0x05;
296 send_raw_bytes(&delim,
sizeof(delim));
297 send_data(&val,
sizeof(val));
302 uint8_t delim = 0x05;
304 send_raw_bytes(&delim,
sizeof(delim));
305 send_data(&val,
sizeof(val));
310 uint8_t delim = 0x05;
312 send_raw_bytes(&delim,
sizeof(delim));
313 send_data(&val,
sizeof(val));
318 uint8_t delim = 0x06;
320 send_raw_bytes(&delim,
sizeof(delim));
321 send_data(&val,
sizeof(val));
326 uint8_t delim = 0x06;
328 send_raw_bytes(&delim,
sizeof(delim));
329 send_data(&val,
sizeof(val));
334 uint8_t delim = 0x06;
336 send_raw_bytes(&delim,
sizeof(delim));
337 send_data(&val,
sizeof(val));
342 uint8_t delim = 0x07;
344 send_raw_bytes(&delim,
sizeof(delim));
345 send_data(&val,
sizeof(val));
350 uint8_t delim = 0x07;
352 send_raw_bytes(&delim,
sizeof(delim));
353 send_data(&val,
sizeof(val));
358 uint8_t delim = 0x07;
360 send_raw_bytes(&delim,
sizeof(delim));
361 send_data(&val,
sizeof(val));
366 uint8_t delim = 0x08;
368 send_raw_bytes(&delim,
sizeof(delim));
369 send_data(&val,
sizeof(val));
374 uint8_t delim = 0x08;
376 send_raw_bytes(&delim,
sizeof(delim));
377 send_data(&val,
sizeof(val));
382 uint8_t delim = 0x08;
384 send_raw_bytes(&delim,
sizeof(delim));
385 send_data(&val,
sizeof(val));
390 uint8_t delim = 0x09;
392 send_raw_bytes(&delim,
sizeof(delim));
393 send_data(&val,
sizeof(val));
398 uint8_t delim = 0x09;
400 send_raw_bytes(&delim,
sizeof(delim));
401 send_data(&val,
sizeof(val));
406 uint8_t delim = 0x09;
408 send_raw_bytes(&delim,
sizeof(delim));
409 send_data(&val,
sizeof(val));
414 uint8_t delim = (val) ? 0x0A : 0x0B;
416 send_raw_bytes(&delim,
sizeof(delim));
421 uint8_t delim = (val) ? 0x0A : 0x0B;
423 send_raw_bytes(&delim,
sizeof(delim));
428 uint8_t delim = (val) ? 0x0A : 0x0B;
430 send_raw_bytes(&delim,
sizeof(delim));
435 uint8_t delim = 0x0C;
437 send_raw_bytes(&delim,
sizeof(delim));
442 uint8_t delim = 0x0C;
444 send_raw_bytes(&delim,
sizeof(delim));
449 uint8_t delim = 0x0C;
451 send_raw_bytes(&delim,
sizeof(delim));
465 uint8_t delim = 0xFF;
466 send_raw_bytes(&delim,
sizeof(delim));
570 long get_integer(
const msg_id keyID)
574 return get_integer_worker();
580 long get_integer(
const char* key)
582 if (key == NULL || find_key(key))
584 return get_integer_worker();
592 if (key == NULL || find_key(key))
594 return get_integer_worker();
600 double get_double(
const msg_id keyID)
604 return get_double_worker();
610 double get_double(
const char* key)
612 if (key == NULL || find_key(key))
614 return get_double_worker();
622 if (key == NULL || find_key(key))
624 return get_double_worker();
630 const char* get_string(
const msg_id keyID,
char* buffer,
size_t maxlen)
634 return get_string_worker(buffer, maxlen);
641 const char* get_string(
const char* key,
char* buffer,
size_t maxlen)
643 if (key == NULL || find_key(key))
645 return get_string_worker(buffer, maxlen);
653 const char* get_string(
PROGMEMString key,
char* buffer,
size_t maxlen)
655 if (key == NULL || find_key(key))
657 return get_string_worker(buffer, maxlen);
664 bool get_boolean(
const msg_id key)
666 return (
bool)get_integer(key);
668 bool get_boolean(
const char* key)
670 return (
bool)get_integer(key);
674 return (
bool)get_integer(key);
676 int8_t get_int8(
const msg_id key)
678 return (int8_t)get_integer(key);
680 int8_t get_int8(
const char* key)
682 return (int8_t)get_integer(key);
686 return (int8_t)get_integer(key);
688 int16_t get_int16(
const msg_id key)
690 return (int16_t)get_integer(key);
692 int16_t get_int16(
const char* key)
694 return (int16_t)get_integer(key);
698 return (int16_t)get_integer(key);
700 int32_t get_int32(
const msg_id key)
702 return (int16_t)get_integer(key);
704 int32_t get_int32(
const char* key)
706 return (int16_t)get_integer(key);
710 return (int16_t)get_integer(key);
712 uint8_t get_uint8(
const msg_id key)
714 return (uint8_t)get_integer(key);
716 uint8_t get_uint8(
const char* key)
718 return (uint8_t)get_integer(key);
722 return (uint8_t)get_integer(key);
724 uint16_t get_uint16(
const uint16_t key)
726 return (uint16_t)get_integer(key);
728 uint16_t get_uint16(
const char* key)
730 return (uint16_t)get_integer(key);
734 return (uint16_t)get_integer(key);
736 uint32_t get_uint32(
const msg_id key)
738 return (uint32_t)get_integer(key);
740 uint32_t get_uint32(
const char* key)
742 return (uint32_t)get_integer(key);
746 return (uint32_t)get_integer(key);
748 float get_float(
const msg_id key)
750 return (
float)get_double(key);
752 float get_float(
const char* key)
754 return (
float)get_double(key);
758 return (
float)get_double(key);
765 find_key(
static_cast<const char*
>(NULL));
770 bool find_key(
const msg_id keyID)
775 uint8_t t = read_uint8();
782 uint16_t lencrc = read_uint16();
783 uint16_t len = read_uint16();
787 uint16_t crc = read_uint16();
791 int8_t ch = read_int8();
792 recrc = update_crc(recrc, ch);
796 match = (crc == keyID);
813 uint16_t crc = read_uint16();
814 match = (keyID == crc);
825 fMType = read_uint8();
830 get_integer_worker();
835 bool find_key(
const char* key)
837 unsigned keylen = (key != NULL) ? strlen(key) : 0;
841 uint8_t t = read_uint8();
848 uint16_t lencrc = read_uint16();
849 uint16_t len = read_uint16();
853 uint16_t crc = read_uint16();
856 match = (keylen == len);
859 int8_t ch = read_int8();
866 recrc = update_crc(recrc, ch);
867 if (match && *k++ != ch)
887 uint16_t crc = read_uint16();
888 uint16_t keycrc = 0xFFFF;
889 const uint8_t* b = (uint8_t*)key;
890 const uint8_t* buf_end = (uint8_t*)key + keylen;
893 keycrc = update_crc(keycrc, *b++);
895 match = (keylen != 0 && keycrc == crc);
906 fMType = read_uint8();
911 get_integer_worker();
918 const char* key = (
const char*)keyP;
919 unsigned keylen = (key != NULL) ? strlen_P(key) : 0;
923 uint8_t t = read_uint8();
930 uint16_t lencrc = read_uint16();
931 uint16_t len = read_uint16();
935 uint16_t crc = read_uint16();
938 match = (keylen == len);
941 int8_t ch = read_int8();
948 recrc = update_crc(recrc, ch);
949 if (match && pgm_read_byte(k++) != ch)
969 uint16_t crc = read_uint16();
970 uint16_t keycrc = 0xFFFF;
971 const uint8_t* b = (uint8_t*)key;
972 const uint8_t* buf_end = (uint8_t*)key + keylen;
975 keycrc = update_crc(keycrc, pgm_read_byte(b++));
977 match = (keylen != 0 && keycrc == crc);
988 fMType = read_uint8();
993 get_integer_worker();
998 long get_integer_worker()
1006 uint16_t lencrc = read_uint16();
1007 uint16_t len = read_uint16();
1011 uint16_t crc = read_uint16();
1015 uint8_t ch = read_uint8();
1016 recrc = update_crc(recrc, ch);
1027 uint16_t crc = read_uint16();
1034 uint16_t crc = read_uint16();
1035 int8_t val = read_int8();
1037 for (
unsigned i = 0; i <
sizeof(val); i++)
1039 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1048 uint16_t crc = read_uint16();
1049 int16_t val = read_int16();
1051 for (
unsigned i = 0; i <
sizeof(val); i++)
1053 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1062 uint16_t crc = read_uint16();
1063 int32_t val = read_int32();
1065 for (
unsigned i = 0; i <
sizeof(val); i++)
1067 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1076 uint16_t crc = read_uint16();
1077 uint8_t val = read_uint8();
1079 for (
unsigned i = 0; i <
sizeof(val); i++)
1081 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1090 uint16_t crc = read_uint16();
1091 uint16_t val = read_uint16();
1093 for (
unsigned i = 0; i <
sizeof(val); i++)
1095 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1104 uint16_t crc = read_uint16();
1105 uint32_t val = read_uint32();
1107 for (
unsigned i = 0; i <
sizeof(val); i++)
1109 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1118 uint16_t crc = read_uint16();
1119 float val = read_float();
1121 for (
unsigned i = 0; i <
sizeof(val); i++)
1123 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1132 uint16_t crc = read_uint16();
1133 double val = read_double();
1135 for (
unsigned i = 0; i <
sizeof(val); i++)
1137 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1166 double get_double_worker()
1174 uint16_t lencrc = read_uint16();
1175 uint16_t len = read_uint16();
1179 uint16_t crc = read_uint16();
1183 uint8_t ch = read_uint8();
1184 recrc = update_crc(recrc, ch);
1195 uint16_t crc = read_uint16();
1202 uint16_t crc = read_uint16();
1203 int8_t val = read_int8();
1205 for (
unsigned i = 0; i <
sizeof(val); i++)
1207 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1216 uint16_t crc = read_uint16();
1217 int16_t val = read_int16();
1219 for (
unsigned i = 0; i <
sizeof(val); i++)
1221 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1230 uint16_t crc = read_uint16();
1231 int32_t val = read_int32();
1233 for (
unsigned i = 0; i <
sizeof(val); i++)
1235 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1244 uint16_t crc = read_uint16();
1245 uint8_t val = read_uint8();
1247 for (
unsigned i = 0; i <
sizeof(val); i++)
1249 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1258 uint16_t crc = read_uint16();
1259 uint16_t val = read_uint16();
1261 for (
unsigned i = 0; i <
sizeof(val); i++)
1263 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1272 uint16_t crc = read_uint16();
1273 uint32_t val = read_uint32();
1275 for (
unsigned i = 0; i <
sizeof(val); i++)
1277 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1286 uint16_t crc = read_uint16();
1287 float val = read_float();
1289 for (
unsigned i = 0; i <
sizeof(val); i++)
1291 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1300 uint16_t crc = read_uint16();
1301 double val = read_double();
1303 for (
unsigned i = 0; i <
sizeof(val); i++)
1305 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1334 const char* get_string_worker(
char* buffer,
size_t maxlen)
1336 const char* ret = NULL;
1342 uint16_t lencrc = read_uint16();
1343 uint16_t len = read_uint16();
1347 uint16_t crc = read_uint16();
1355 uint8_t ch = read_uint8();
1365 recrc = update_crc(recrc, ch);
1382 get_integer_worker();
1391 smq_id recvTopicID = (
sizeof(
smq_id) ==
sizeof(uint32_t)) ? read_uint32() : read_uint16();
1393 for (Message* msg = *tail(); msg != NULL; msg = msg->fNext)
1397 smq_id topicID = msg->fTopic;
1398 if (recvTopicID == topicID)
1405 msg->fHandler(*msg);
1412 static void sendSubscriberList()
1415 for (Message* msg = *tail(); msg != NULL; msg = msg->fNext)
1421 send_raw_bytes(&count,
sizeof(count));
1422 for (Message* msg = *tail(); msg != NULL; msg = msg->fNext)
1426 smq_id topicID = msg->fTopic;
1428 send_raw_bytes(&topicID,
sizeof(topicID));
1441 static Message** tail()
1443 static Message* sTail;
1450 static inline uint16_t update_crc(uint16_t crc,
const uint8_t data)
1453 static const PROGMEM uint16_t crc16_table[256] =
1455 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
1456 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
1457 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
1458 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
1459 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
1460 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
1461 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
1462 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
1463 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
1464 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
1465 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
1466 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
1467 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
1468 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
1469 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
1470 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
1472 return (crc >> 8) ^ pgm_read_word_near(&crc16_table[(crc ^ data) & 0xFF]);
1475 static uint16_t calc_crc(
const void* buf,
size_t len, uint16_t crc)
1477 const uint8_t* b = (uint8_t*)buf;
1478 const uint8_t* buf_end = (uint8_t*)buf + len;
1481 crc = update_crc(crc, *b++);
1486 static void send_raw_bytes(
const void* buf,
size_t len)
1488 sSMQ->write((uint8_t*)buf, len);
1492 static void send_data(
const void* buf, uint16_t len)
1495 const uint8_t* b = (uint8_t*)buf;
1496 const uint8_t* buf_end = (uint8_t*)buf + len;
1499 crc = update_crc(crc, *b++);
1501 send_raw_bytes(&crc,
sizeof(crc));
1502 send_raw_bytes(buf, len);
1508 const uint8_t* pb = (uint8_t*)pbuf;
1509 const uint8_t* pbuf_end = (uint8_t*)pbuf + len;
1510 while (pb < pbuf_end)
1512 crc = update_crc(crc, pgm_read_byte(pb++));
1514 send_raw_bytes(&crc,
sizeof(crc));
1515 pb = (uint8_t*)pbuf;
1516 pbuf_end = (uint8_t*)pbuf + len;
1517 while (pb < pbuf_end)
1519 byte b = pgm_read_byte(pb++);
1520 sSMQ->write((uint8_t*)&b, 1);
1525 static int read(
void* buf,
size_t len)
1527 char* b = (
char*)buf;
1528 char* b_end = b + len;
1531 while (!
sSMQ->available())
1533 int v =
sSMQ->read();
1537 int cnt = b - (
char*)buf;
1541 for (
int i = 0; i < cnt; i++)
1552 static int8_t read_int8()
1555 read(&val,
sizeof(val));
1559 static int16_t read_int16()
1562 read(&val,
sizeof(val));
1566 static int32_t read_int32()
1569 read(&val,
sizeof(val));
1573 static uint8_t read_uint8()
1576 read(&val,
sizeof(val));
1580 static uint16_t read_uint16()
1583 read(&val,
sizeof(val));
1587 static uint32_t read_uint32()
1590 read(&val,
sizeof(val));
1594 static float read_float()
1597 read(&val,
sizeof(val));
1601 static double read_double()
1604 read(&val,
sizeof(val));
1608 static uint8_t* read_buffer(uint8_t* buf, uint16_t len, uint16_t bufsize)
1611 uint8_t* pend = p + min(len, bufsize);
1614 while (!
sSMQ->available())
1616 *p++ =
sSMQ->read();
1621 len = len - bufsize;
1633 #define SMQMSG_FUNC_DECL(topic) \
1634 static void SMQHandler_##topic(SMQ::Message& msg)
1635 #define SMQMESSAGE(topic, handler) \
1636 SMQMSG_FUNC_DECL(topic); \
1637 SMQ::Message SMQMSG_##topic(SMQID(#topic), SMQHandler_##topic); \
1638 SMQMSG_FUNC_DECL(topic) { UNUSED_ARG(msg) handler }