7 #define SMQ_HOSTNAME "Generic"
11 #error ReelTwoSMQ32.h must be included before ReelTwo.h
19 #define SMQ_DEBUG_PRINTLN(s) DEBUG_PRINTLN(s)
20 #define SMQ_DEBUG_PRINT(s) DEBUG_PRINT(s)
21 #define SMQ_DEBUG_PRINTF(...) DEBUG_PRINTF(__VA_ARGS__)
22 #define SMQ_DEBUG_PRINTLN_HEX(s) DEBUG_PRINTLN_HEX(s)
23 #define SMQ_DEBUG_PRINT_HEX(s) DEBUG_PRINT_HEX(s)
24 #define SMQ_DEBUG_FLUSH(s) DEBUG_FLUSH()
26 #define SMQ_DEBUG_PRINTLN(s)
27 #define SMQ_DEBUG_PRINT(s)
28 #define SMQ_DEBUG_PRINTF(...)
29 #define SMQ_DEBUG_PRINTLN_HEX(s)
30 #define SMQ_DEBUG_PRINT_HEX(s)
31 #define SMQ_DEBUG_FLUSH(s)
34 #define SMQ_ADDR_HEX_STR "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx"
35 #define SMQ_ADDR_HEX_ARR(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
36 #define SMQ_ADDR_HEX_PTR(addr) &addr[0], &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]
38 #define SMQ_MAX_KEYLEN ESP_NOW_KEY_LEN
40 #define SMQ_BEACON_BROADCAST_INTERVAL 1000
41 #define SMQ_HOST_LOST_TIMEOUT 10000
43 #define SMQ_MAX_PAIRED_HOSTS 20
44 #define SMQ_MINIMUM_KEY_LEN 8
47 #define SMQ_PAIRING_TIMEOUT 2*60*1000
54 #define WIFI_CHANNEL 1
56 #define MAX_MSG_SIZE 250
57 #define SMQ_MAX_HOST_NAME 13
65 return (memcmp(addr,
fData,
sizeof(
fData)) == 0);
76 snprintf(macaddr,
sizeof(macaddr),
"%02X:%02X:%02X:%02X:%02X:%02X",
88 return (memcmp(addr,
fData,
sizeof(
fData)) == 0);
100 snprintf(str,
sizeof(str),
"%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
101 p[0], p[1], p[2], p[3], p[4], p[5],
102 p[6], p[7], p[8], p[9], p[10], p[11],
103 p[12], p[13], p[14], p[15]);
135 snprintf(macaddr,
sizeof(macaddr),
"%02X:%02X:%02X:%02X:%02X:%02X",
136 p[0], p[1], p[2], p[3], p[4], p[5]);
147 memcpy(addr, &
fAddr,
sizeof(*addr));
153 for (
unsigned i = 0; i <
fCount; i++)
173 static bool sSMQInited =
false;
175 static unsigned sSMQPairedHostsCount;
177 static char sSMQPairingMode =
false;
178 static uint32_t sSMQPairingTimeOut = 0;
179 static bool sClearToSend =
false;
181 static uint8_t* sSendPtr = sSendBuffer;
183 static uint8_t sBroadcastMAC[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
184 static QueueHandle_t sRecvQueue;
185 static uint8_t* sReadPtr =
nullptr;
186 static int sReadLen = 0;
190 static uint32_t sKeyHash;
191 static uint8_t* sSendAddr =
nullptr;
192 static void (*sPairingEvent)(
SMQHost* host);
193 static void (*sDiscoverEvent)(
SMQHost* host);
194 static void (*sLostEvent)(
SMQHost* host);
198 #define REELTWO_READY() _REELTWO_READY_
217 sSMQPairingMode =
true;
224 return (sSMQPairingMode ==
true);
232 sSMQPairingTimeOut = millis() + 2000;
238 static SMQLMK zeroAddress;
241 if (lmk.
equals(zeroAddress) == remoteKey->
equals(zeroAddress))
243 if (lmk.
equals(zeroAddress))
246 printf(
"No master key. One device must have a master key to pair.\n");
249 else if (!lmk.
equals(*remoteKey))
252 printf(
"Two different master keys. Reset one device to factory defaults and try again.\n");
260 if (!lmk.
equals(zeroAddress))
262 printf(
"WE HAVE A MASTER KEY\n");
268 printf(
"REMOTE HAS MASTER KEY\n");
277 sSMQPairedHostsCount = numHosts;
278 memcpy(sSMQPairedHosts, hosts, numHosts *
sizeof(sSMQPairedHosts[0]));
284 for (
int i = 0; i < sSMQPairedHostsCount; i++)
295 for (
unsigned i = 0; i < sSMQPairedHostsCount; i++)
297 if (memcmp(&sSMQPairedHosts[i].fAddr, addr,
sizeof(*addr)) == 0)
305 sSMQPairedHosts[sSMQPairedHostsCount++] = host;
313 return sSMQPairedHostsCount;
318 maxCount = min(maxCount, sSMQPairedHostsCount);
319 memcpy(hosts, sSMQPairedHosts, maxCount *
sizeof(sSMQPairedHosts[0]));
325 sPairingEvent = callback;
330 sDiscoverEvent = callback;
335 sLostEvent = callback;
345 return WiFi.macAddress();
348 static bool init(String hostName, String key)
350 return init(hostName.c_str(), key.c_str());
353 static bool init(
const char* hostName =
nullptr,
const char* key =
nullptr)
361 if (WiFi.getMode() != WIFI_STA)
363 printf(
"CHANGING TO WIFI_STA MODE\n");
367 if (WiFi.getMode() != WIFI_STA)
372 if (esp_now_init() != ESP_OK)
377 memset(sHostName,
'\0',
sizeof(sHostName));
378 snprintf(sHostName,
sizeof(sHostName)-1,
"%s", (hostName !=
nullptr) ? hostName :
SMQ_HOSTNAME);
379 sKeyHash = (key !=
nullptr && *key !=
'\0') ?
WSID32(key) : 0;
386 master.crc[0] =
WSID32(key);
387 master.crc[1] =
WSID32(key+1);
388 master.crc[2] =
WSID32(key+2);
389 master.crc[3] =
WSID32(key+3);
390 printf(
"MASTER: %s\n", master.key.toString().c_str());
391 esp_now_set_pmk(master.key.fData);
394 esp_err_t status = esp_now_register_recv_cb(msg_recv_cb);
395 if (ESP_OK != status)
402 status = esp_now_register_send_cb(msg_send_cb);
403 if (ESP_OK != status)
417 if (!esp_now_is_peer_exist(sBroadcastMAC))
419 esp_now_peer_info_t peer_info;
421 memcpy(peer_info.peer_addr, sBroadcastMAC,
sizeof(sBroadcastMAC));
422 peer_info.ifidx = WIFI_IF_STA;
423 peer_info.encrypt =
false;
424 esp_err_t status = esp_now_add_peer(&peer_info);
425 if (ESP_OK != status)
437 esp_fill_random(key->
fData,
sizeof(key->
fData));
453 esp_now_del_peer(sBroadcastMAC);
458 static uint32_t sLastBeacon;
462 while (xQueueReceive(sRecvQueue, &msg, 0))
464 sReadPtr = msg.
fData;
465 sReadLen = msg.
fSize;
467 if (sReadLen > 1 && *sReadPtr++ == 0x0E)
473 Message::process(&msg);
478 if (sSMQPairingMode && sSMQPairingTimeOut < millis())
481 sSMQPairingMode =
false;
483 if (sSMQPairingMode ==
true && sPairingEvent)
485 sPairingEvent(
nullptr);
494 else if (sSMQPairedHostsCount != 0)
496 Message::sendBeacon();
498 sLastBeacon = millis();
511 uint8_t delim = 0x00;
512 send_raw_bytes(&delim,
sizeof(delim));
514 uint16_t len = strlen(str);
515 send_data(&len,
sizeof(len));
521 uint8_t delim = 0x00;
522 send_raw_bytes(&delim,
sizeof(delim));
524 size_t len = strlen_P((
const char*)str);
525 send_data(&len,
sizeof(len));
531 uint8_t delim = 0x01;
532 send_raw_bytes(&delim,
sizeof(delim));
533 send_raw_bytes(&
id,
sizeof(
id));
538 uint8_t delim = 0x0E;
539 send_raw_bytes(&delim,
sizeof(delim));
540 send_raw_bytes(&
id,
sizeof(
id));
550 uint8_t delim = 0x01;
551 send_raw_bytes(&delim,
sizeof(delim));
553 uint16_t crc = 0xFFFF;
554 uint16_t len = strlen(str);
555 const uint8_t* b = (uint8_t*)str;
556 const uint8_t* buf_end = (uint8_t*)str + len;
559 crc = update_crc(crc, *b++);
562 send_raw_bytes(&crc,
sizeof(crc));
567 uint8_t delim = 0x01;
568 send_raw_bytes(&delim,
sizeof(delim));
570 uint16_t crc = 0xFFFF;
571 uint16_t len = strlen_P((
const char*)str);
572 const uint8_t* b = (uint8_t*)str;
573 const uint8_t* buf_end = (uint8_t*)str + len;
576 crc = update_crc(crc, pgm_read_byte(b++));
578 send_raw_bytes(&crc,
sizeof(crc));
584 send_raw_bytes(&sKeyHash,
sizeof(sKeyHash));
590 send_raw_bytes(&sKeyHash,
sizeof(sKeyHash));
596 send_raw_bytes(&sKeyHash,
sizeof(sKeyHash));
645 uint8_t delim = 0x02;
647 send_raw_bytes(&delim,
sizeof(delim));
648 send_data(&val,
sizeof(val));
653 uint8_t delim = 0x02;
655 send_raw_bytes(&delim,
sizeof(delim));
656 send_data(&val,
sizeof(val));
661 uint8_t delim = 0x02;
663 send_raw_bytes(&delim,
sizeof(delim));
664 send_data(&val,
sizeof(val));
669 uint8_t delim = 0x03;
671 send_raw_bytes(&delim,
sizeof(delim));
672 send_data(&val,
sizeof(val));
677 uint8_t delim = 0x03;
679 send_raw_bytes(&delim,
sizeof(delim));
680 send_data(&val,
sizeof(val));
685 uint8_t delim = 0x03;
687 send_raw_bytes(&delim,
sizeof(delim));
688 send_data(&val,
sizeof(val));
693 uint8_t delim = 0x04;
695 send_raw_bytes(&delim,
sizeof(delim));
696 send_data(&val,
sizeof(val));
701 uint8_t delim = 0x04;
703 send_raw_bytes(&delim,
sizeof(delim));
704 send_data(&val,
sizeof(val));
709 uint8_t delim = 0x04;
711 send_raw_bytes(&delim,
sizeof(delim));
712 send_data(&val,
sizeof(val));
727 uint8_t delim = 0x05;
729 send_raw_bytes(&delim,
sizeof(delim));
730 send_data(&val,
sizeof(val));
735 uint8_t delim = 0x05;
737 send_raw_bytes(&delim,
sizeof(delim));
738 send_data(&val,
sizeof(val));
743 uint8_t delim = 0x05;
745 send_raw_bytes(&delim,
sizeof(delim));
746 send_data(&val,
sizeof(val));
751 uint8_t delim = 0x06;
753 send_raw_bytes(&delim,
sizeof(delim));
754 send_data(&val,
sizeof(val));
759 uint8_t delim = 0x06;
761 send_raw_bytes(&delim,
sizeof(delim));
762 send_data(&val,
sizeof(val));
767 uint8_t delim = 0x06;
769 send_raw_bytes(&delim,
sizeof(delim));
770 send_data(&val,
sizeof(val));
775 uint8_t delim = 0x07;
777 send_raw_bytes(&delim,
sizeof(delim));
778 send_data(&val,
sizeof(val));
783 uint8_t delim = 0x07;
785 send_raw_bytes(&delim,
sizeof(delim));
786 send_data(&val,
sizeof(val));
791 uint8_t delim = 0x07;
793 send_raw_bytes(&delim,
sizeof(delim));
794 send_data(&val,
sizeof(val));
799 uint8_t delim = 0x08;
801 send_raw_bytes(&delim,
sizeof(delim));
802 send_data(&val,
sizeof(val));
807 uint8_t delim = 0x08;
809 send_raw_bytes(&delim,
sizeof(delim));
810 send_data(&val,
sizeof(val));
815 uint8_t delim = 0x08;
817 send_raw_bytes(&delim,
sizeof(delim));
818 send_data(&val,
sizeof(val));
823 uint8_t delim = 0x09;
825 send_raw_bytes(&delim,
sizeof(delim));
826 send_data(&val,
sizeof(val));
831 uint8_t delim = 0x09;
833 send_raw_bytes(&delim,
sizeof(delim));
834 send_data(&val,
sizeof(val));
839 uint8_t delim = 0x09;
841 send_raw_bytes(&delim,
sizeof(delim));
842 send_data(&val,
sizeof(val));
847 uint8_t delim = (val) ? 0x0A : 0x0B;
849 send_raw_bytes(&delim,
sizeof(delim));
854 uint8_t delim = (val) ? 0x0A : 0x0B;
856 send_raw_bytes(&delim,
sizeof(delim));
861 uint8_t delim = (val) ? 0x0A : 0x0B;
863 send_raw_bytes(&delim,
sizeof(delim));
868 uint8_t delim = 0x0C;
870 send_raw_bytes(&delim,
sizeof(delim));
875 uint8_t delim = 0x0C;
877 send_raw_bytes(&delim,
sizeof(delim));
882 uint8_t delim = 0x0C;
884 send_raw_bytes(&delim,
sizeof(delim));
898 esp_now_peer_info_t peer;
899 esp_err_t e = esp_now_fetch_peer(
true, &peer);
903 memcpy(addr, peer.peer_addr,
sizeof(addr));
904 e = esp_now_fetch_peer(
false, &peer);
905 esp_now_del_peer(addr);
912 return sendTopic(topic.c_str(), (host.length() != 0) ? host.c_str() :
nullptr);
921 static bool sendTopic(
const char* topic,
const char* hostNameAddr =
nullptr)
932 bool searchHostMac =
false;
934 if (hostNameAddr !=
nullptr)
938 searchHostMac =
true;
944 for (
SMQHost* host = sHostHead; host !=
nullptr; host = host->
fNext)
947 if (hostNameAddr !=
nullptr)
951 if (strcmp(hostNameAddr, host->fName) != 0)
954 else if (memcmp(addr, &host->fAddr,
sizeof(addr)) != 0)
959 for (
unsigned i = 0; i < host->fCount; i++)
961 if (host->fTopics[i] == topicID)
963 esp_now_peer_info_t peer_info;
964 memset(&peer_info,
'\0',
sizeof(peer_info));
966 memcpy(peer_info.peer_addr, &host->fAddr,
sizeof(host->fAddr));
967 peer_info.ifidx = WIFI_IF_STA;
968 peer_info.encrypt =
true;
969 memcpy(peer_info.lmk, host->fLMK.fData,
sizeof(host->fLMK.fData));
970 SMQ_DEBUG_PRINTF(
"ADD %s PEER: %s [LMK:%s]\n", host->getHostName().c_str(), host->getHostAddress().c_str(), host->getHostKey().c_str());
971 if (esp_now_add_peer(&peer_info) == ESP_OK)
975 sSendAddr = host->fAddr.fData;
1000 return sSMQFromAddr;
1005 sSendAddr = sBroadcastMAC;
1020 sSendAddr =
nullptr;
1036 for (
unsigned i = 0; i < sSMQPairedHostsCount; i++)
1039 esp_now_peer_info_t peer_info;
1040 memset(&peer_info,
'\0',
sizeof(peer_info));
1047 memcpy(peer_info.peer_addr, &host->
fAddr,
sizeof(host->
fAddr));
1048 peer_info.ifidx = WIFI_IF_STA;
1049 peer_info.encrypt =
true;
1052 esp_err_t status = esp_now_add_peer(&peer_info);
1053 if (ESP_OK != status)
1066 uint8_t delim = 0xFF;
1067 send_raw_bytes(&delim,
sizeof(delim));
1070 sClearToSend =
false;
1071 esp_err_t status = esp_now_send(sSendAddr, sSendBuffer, sSendPtr - sSendBuffer);
1074 case ESP_ERR_ESPNOW_NOT_INIT:
1077 case ESP_ERR_ESPNOW_ARG:
1080 case ESP_ERR_ESPNOW_INTERNAL:
1083 case ESP_ERR_ESPNOW_NO_MEM:
1086 case ESP_ERR_ESPNOW_NOT_FOUND:
1089 case ESP_ERR_ESPNOW_IF:
1095 if (status != ESP_OK)
1098 sClearToSend =
true;
1100 sSendPtr = sSendBuffer;
1209 long get_integer(
const msg_id keyID)
1211 if (find_key(keyID))
1213 return get_integer_worker();
1219 long get_integer(
const char* key)
1221 if (key == NULL || find_key(
WSID16(key)))
1223 return get_integer_worker();
1231 if (key == NULL || find_key(key))
1233 return get_integer_worker();
1239 double get_double(
const msg_id keyID)
1241 if (find_key(keyID))
1243 return get_double_worker();
1249 double get_double(
const char* key)
1251 if (key == NULL || find_key(key))
1253 return get_double_worker();
1261 if (key == NULL || find_key(key))
1263 return get_double_worker();
1269 String getString(
const msg_id keyID)
1272 return get_string(keyID, buf,
sizeof(buf));
1276 String getString(
const char* key)
1279 return get_string(key, buf,
sizeof(buf));
1282 String getString(String key)
1284 return getString(key.c_str());
1287 const char* get_string(
const msg_id keyID,
char* buffer,
size_t maxlen)
1289 if (find_key(keyID))
1291 return get_string_worker(buffer, maxlen);
1299 const char* get_string(
const char* key,
char* buffer,
size_t maxlen)
1301 if (find_key(
WSID16(key)))
1303 return get_string_worker(buffer, maxlen);
1310 const char* get_string(
PROGMEMString key,
char* buffer,
size_t maxlen)
1312 if (key == NULL || find_key(key))
1314 return get_string_worker(buffer, maxlen);
1321 bool get_boolean(
const msg_id key)
1323 return (
bool)get_integer(key);
1325 bool get_boolean(
const char* key)
1327 return (
bool)get_integer(key);
1331 return (
bool)get_integer(key);
1333 int8_t get_int8(
const msg_id key)
1335 return (int8_t)get_integer(key);
1337 int8_t get_int8(
const char* key)
1339 return (int8_t)get_integer(key);
1343 return (int8_t)get_integer(key);
1345 int16_t get_int16(
const msg_id key)
1347 return (int16_t)get_integer(key);
1349 int16_t get_int16(
const char* key)
1351 return (int16_t)get_integer(key);
1355 return (int16_t)get_integer(key);
1357 int32_t get_int32(
const msg_id key)
1359 return (int16_t)get_integer(key);
1361 int32_t get_int32(
const char* key)
1363 return (int16_t)get_integer(key);
1367 return (int16_t)get_integer(key);
1369 uint8_t get_uint8(
const msg_id key)
1371 return (uint8_t)get_integer(key);
1373 uint8_t get_uint8(
const char* key)
1375 return (uint8_t)get_integer(key);
1379 return (uint8_t)get_integer(key);
1381 uint16_t get_uint16(
const uint16_t key)
1383 return (uint16_t)get_integer(key);
1385 uint16_t get_uint16(
const char* key)
1387 return (uint16_t)get_integer(key);
1391 return (uint16_t)get_integer(key);
1393 uint32_t get_uint32(
const msg_id key)
1395 return (uint32_t)get_integer(key);
1397 uint32_t get_uint32(
const char* key)
1399 return (uint32_t)get_integer(key);
1403 return (uint32_t)get_integer(key);
1405 float get_float(
const msg_id key)
1407 return (
float)get_double(key);
1409 float get_float(
const char* key)
1411 return (
float)get_double(key);
1415 return (
float)get_double(key);
1422 find_key(
static_cast<const char*
>(NULL));
1427 bool find_key(
const msg_id keyID)
1432 uint8_t t = read_uint8();
1439 uint16_t lencrc = read_uint16();
1440 uint16_t len = read_uint16();
1444 uint16_t crc = read_uint16();
1448 int8_t ch = read_int8();
1449 recrc = update_crc(recrc, ch);
1453 match = (crc == keyID);
1470 uint16_t crc = read_uint16();
1471 match = (keyID == crc);
1482 fMType = read_uint8();
1487 get_integer_worker();
1492 bool find_key(
const char* key)
1494 unsigned keylen = (key != NULL) ? strlen(key) : 0;
1498 uint8_t t = read_uint8();
1505 uint16_t lencrc = read_uint16();
1506 uint16_t len = read_uint16();
1510 uint16_t crc = read_uint16();
1512 const char* k = key;
1513 match = (keylen == len);
1516 int8_t ch = read_int8();
1523 recrc = update_crc(recrc, ch);
1524 if (match && *k++ != ch)
1544 uint16_t crc = read_uint16();
1545 uint16_t keycrc = 0xFFFF;
1546 const uint8_t* b = (uint8_t*)key;
1547 const uint8_t* buf_end = (uint8_t*)key + keylen;
1550 keycrc = update_crc(keycrc, *b++);
1552 match = (keylen != 0 && keycrc == crc);
1563 fMType = read_uint8();
1568 get_integer_worker();
1575 const char* key = (
const char*)keyP;
1576 unsigned keylen = (key != NULL) ? strlen_P(key) : 0;
1580 uint8_t t = read_uint8();
1587 uint16_t lencrc = read_uint16();
1588 uint16_t len = read_uint16();
1592 uint16_t crc = read_uint16();
1594 const char* k = key;
1595 match = (keylen == len);
1598 int8_t ch = read_int8();
1605 recrc = update_crc(recrc, ch);
1606 if (match && pgm_read_byte(k++) != ch)
1626 uint16_t crc = read_uint16();
1627 uint16_t keycrc = 0xFFFF;
1628 const uint8_t* b = (uint8_t*)key;
1629 const uint8_t* buf_end = (uint8_t*)key + keylen;
1632 keycrc = update_crc(keycrc, pgm_read_byte(b++));
1634 match = (keylen != 0 && keycrc == crc);
1645 fMType = read_uint8();
1650 get_integer_worker();
1655 long get_integer_worker()
1663 uint16_t lencrc = read_uint16();
1664 uint16_t len = read_uint16();
1668 uint16_t crc = read_uint16();
1672 uint8_t ch = read_uint8();
1673 recrc = update_crc(recrc, ch);
1684 uint16_t crc = read_uint16();
1691 uint16_t crc = read_uint16();
1692 int8_t val = read_int8();
1694 for (
unsigned i = 0; i <
sizeof(val); i++)
1696 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1705 uint16_t crc = read_uint16();
1706 int16_t val = read_int16();
1708 for (
unsigned i = 0; i <
sizeof(val); i++)
1710 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1719 uint16_t crc = read_uint16();
1720 int32_t val = read_int32();
1722 for (
unsigned i = 0; i <
sizeof(val); i++)
1724 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1733 uint16_t crc = read_uint16();
1734 uint8_t val = read_uint8();
1736 for (
unsigned i = 0; i <
sizeof(val); i++)
1738 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1747 uint16_t crc = read_uint16();
1748 uint16_t val = read_uint16();
1750 for (
unsigned i = 0; i <
sizeof(val); i++)
1752 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1761 uint16_t crc = read_uint16();
1762 uint32_t val = read_uint32();
1764 for (
unsigned i = 0; i <
sizeof(val); i++)
1766 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1775 uint16_t crc = read_uint16();
1776 float val = read_float();
1778 for (
unsigned i = 0; i <
sizeof(val); i++)
1780 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1789 uint16_t crc = read_uint16();
1790 double val = read_double();
1792 for (
unsigned i = 0; i <
sizeof(val); i++)
1794 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1823 double get_double_worker()
1831 uint16_t lencrc = read_uint16();
1832 uint16_t len = read_uint16();
1836 uint16_t crc = read_uint16();
1840 uint8_t ch = read_uint8();
1841 recrc = update_crc(recrc, ch);
1852 uint16_t crc = read_uint16();
1859 uint16_t crc = read_uint16();
1860 int8_t val = read_int8();
1862 for (
unsigned i = 0; i <
sizeof(val); i++)
1864 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1873 uint16_t crc = read_uint16();
1874 int16_t val = read_int16();
1876 for (
unsigned i = 0; i <
sizeof(val); i++)
1878 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1887 uint16_t crc = read_uint16();
1888 int32_t val = read_int32();
1890 for (
unsigned i = 0; i <
sizeof(val); i++)
1892 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1901 uint16_t crc = read_uint16();
1902 uint8_t val = read_uint8();
1904 for (
unsigned i = 0; i <
sizeof(val); i++)
1906 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1915 uint16_t crc = read_uint16();
1916 uint16_t val = read_uint16();
1918 for (
unsigned i = 0; i <
sizeof(val); i++)
1920 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1929 uint16_t crc = read_uint16();
1930 uint32_t val = read_uint32();
1932 for (
unsigned i = 0; i <
sizeof(val); i++)
1934 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1943 uint16_t crc = read_uint16();
1944 float val = read_float();
1946 for (
unsigned i = 0; i <
sizeof(val); i++)
1948 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1957 uint16_t crc = read_uint16();
1958 double val = read_double();
1960 for (
unsigned i = 0; i <
sizeof(val); i++)
1962 recrc = update_crc(recrc, ((uint8_t*)&val)[i]);
1991 const char* get_string_worker(
char* buffer,
size_t maxlen)
1993 const char* ret = NULL;
1999 uint16_t lencrc = read_uint16();
2000 uint16_t len = read_uint16();
2004 uint16_t crc = read_uint16();
2012 uint8_t ch = read_uint8();
2022 recrc = update_crc(recrc, ch);
2039 get_integer_worker();
2048 smq_id recvTopicID = read_uint32();
2050 uint32_t keyHash = read_uint32();
2051 if (keyHash == sKeyHash)
2054 if (recvTopicID == sSMQPAIRING_ID && !sSMQPairingMode)
2058 else if (recvTopicID == sSMQBEACON_ID || recvTopicID == sSMQPAIRING_ID)
2061 read_buffer((uint8_t*)name,
sizeof(name),
sizeof(name));
2062 name[
sizeof(name)-1] =
'\0';
2065 uint8_t count = read_uint8();
2067 if (recvTopicID == sSMQPAIRING_ID)
2069 read_buffer(key.fData,
sizeof(key.fData),
sizeof(key.fData));
2071 if (host !=
nullptr)
2073 memcpy(host->
fLMK.
fData, &key,
sizeof(key));
2084 if (host !=
nullptr)
2088 if (strcmp(host->
fName, name) != 0 ||
2089 (recvTopicID == sSMQBEACON_ID && host->
fPaired) ||
2090 (recvTopicID == sSMQPAIRING_ID && !host->
fPaired))
2093 strcpy(host->
fName, name);
2097 for (
unsigned i = 0; i < count; i++)
2100 read_buffer((uint8_t*)&topic,
sizeof(topic),
sizeof(topic));
2104 if (recvTopicID == sSMQPAIRING_ID && sPairingEvent !=
nullptr)
2106 if (sSMQPairingMode)
2109 Message::sendPair();
2110 sPairingEvent(host);
2115 else if (recvTopicID == sSMQBEACON_ID && sDiscoverEvent !=
nullptr)
2117 sDiscoverEvent(host);
2124 printf(
"HOST NOT FOUND: %s\n", name);
2131 for (Message* msg = *tail(); msg != NULL; msg = msg->fNext)
2135 smq_id topicID = msg->fTopic;
2136 if (recvTopicID == topicID)
2144 msg->fHandler(*msg);
2153 static void sendPair()
2156 for (Message* msg = *tail(); msg != NULL; msg = msg->fNext)
2160 send_raw_bytes(&sHostName,
sizeof(sHostName));
2161 send_raw_bytes(&count,
sizeof(count));
2162 send_raw_bytes(&sSMQLMK,
sizeof(sSMQLMK));
2163 for (Message* msg = *tail(); msg != NULL; msg = msg->fNext)
2165 smq_id topicID = msg->fTopic;
2166 send_raw_bytes(&topicID,
sizeof(topicID));
2176 static void sendBeacon()
2179 for (Message* msg = *tail(); msg != NULL; msg = msg->fNext)
2183 send_raw_bytes(&sHostName,
sizeof(sHostName));
2184 send_raw_bytes(&count,
sizeof(count));
2185 for (Message* msg = *tail(); msg != NULL; msg = msg->fNext)
2187 smq_id topicID = msg->fTopic;
2188 send_raw_bytes(&topicID,
sizeof(topicID));
2200 static Message** tail()
2202 static Message* sTail;
2209 static SMQHost* getHost(
const uint8_t* mac_addr, uint8_t topicCount = 0)
2212 for (host = sHostHead; host !=
nullptr; host = host->
fNext)
2214 if (memcmp(mac_addr, &host->
fAddr,
sizeof(host->
fAddr)) == 0)
2221 size_t siz =
sizeof(
SMQHost)+
sizeof(host->
fTopics[0])*topicCount;
2223 if (host !=
nullptr)
2225 memset(host,
'\0', siz);
2226 memcpy(&host->
fAddr, mac_addr,
sizeof(host->
fAddr));
2227 if (sHostHead ==
nullptr)
2233 sHostTail->
fNext = host;
2235 host->
fPrev = sHostTail;
2239 for (
unsigned i = 0; i < sSMQPairedHostsCount; i++)
2250 static void pruneHost(
SMQHost* host,
bool notify =
true)
2255 if (host->
fPrev !=
nullptr)
2259 else if (sHostHead == host)
2261 sHostHead = host->
fNext;
2263 if (host->
fNext !=
nullptr)
2267 else if (host == sHostTail)
2269 sHostTail = host->
fPrev;
2271 if (host !=
nullptr)
2278 static void pruneHostList()
2280 uint32_t now = millis();
2282 while (host !=
nullptr)
2288 pruneHost(lostHost);
2294 static void msg_recv_cb(
const uint8_t *mac_addr,
const uint8_t *data,
int len)
2297 if (len <
sizeof(msg.
fData))
2299 memcpy(msg.
fAddr, mac_addr,
sizeof(msg.
fAddr));
2300 memcpy(msg.
fData, data, len);
2303 if (xQueueSend(sRecvQueue, &msg, 0) != pdPASS)
2310 static void msg_send_cb(
const uint8_t* mac, esp_now_send_status_t sendStatus)
2314 case ESP_NOW_SEND_SUCCESS:
2318 case ESP_NOW_SEND_FAIL:
2325 sClearToSend =
true;
2329 static inline uint16_t update_crc(uint16_t crc,
const uint8_t data)
2332 static const PROGMEM uint16_t crc16_table[256] =
2334 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
2335 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
2336 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
2337 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
2338 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
2339 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
2340 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
2341 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
2342 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
2343 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
2344 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
2345 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
2346 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
2347 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
2348 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
2349 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
2351 return (crc >> 8) ^ pgm_read_word_near(&crc16_table[(crc ^ data) & 0xFF]);
2354 static uint16_t calc_crc(
const void* buf,
size_t len, uint16_t crc)
2356 const uint8_t* b = (uint8_t*)buf;
2357 const uint8_t* buf_end = (uint8_t*)buf + len;
2360 crc = update_crc(crc, *b++);
2365 static void send_raw_bytes(
const void* buf,
size_t len)
2368 if (sSendPtr + len >= &sSendBuffer[
sizeof(sSendBuffer)])
2370 printf(
"SMQ TRUNCATE\n");
2377 memcpy(sSendPtr, buf, len);
2381 static void send_data(
const void* buf, uint16_t len)
2384 const uint8_t* b = (uint8_t*)buf;
2385 const uint8_t* buf_end = (uint8_t*)buf + len;
2388 crc = update_crc(crc, *b++);
2390 send_raw_bytes(&crc,
sizeof(crc));
2391 send_raw_bytes(buf, len);
2397 const uint8_t* pb = (uint8_t*)pbuf;
2398 const uint8_t* pbuf_end = (uint8_t*)pbuf + len;
2399 while (pb < pbuf_end)
2401 crc = update_crc(crc, pgm_read_byte(pb++));
2403 send_raw_bytes(&crc,
sizeof(crc));
2404 pb = (uint8_t*)pbuf;
2405 pbuf_end = (uint8_t*)pbuf + len;
2406 while (pb < pbuf_end)
2408 byte b = pgm_read_byte(pb++);
2409 send_raw_bytes(&b,
sizeof(b));
2413 static int read(
void* buf,
size_t len)
2415 char* b = (
char*)buf;
2416 char* b_end = b + len;
2424 int cnt = b - (
char*)buf;
2439 static int8_t read_int8()
2442 read(&val,
sizeof(val));
2446 static int16_t read_int16()
2449 read(&val,
sizeof(val));
2453 static int32_t read_int32()
2456 read(&val,
sizeof(val));
2460 static uint8_t read_uint8()
2463 read(&val,
sizeof(val));
2467 static uint16_t read_uint16()
2470 read(&val,
sizeof(val));
2474 static uint32_t read_uint32()
2477 read(&val,
sizeof(val));
2481 static float read_float()
2484 read(&val,
sizeof(val));
2488 static double read_double()
2491 read(&val,
sizeof(val));
2495 static uint8_t* read_buffer(uint8_t* buf, uint16_t len, uint16_t bufsize)
2498 uint8_t* pend = p + min(len, bufsize);
2508 len = len - bufsize;
2521 #define SMQMSG_FUNC_DECL(topic) \
2522 static void SMQHandler_##topic(SMQ::Message& msg)
2523 #define SMQMESSAGE(topic, handler) \
2524 SMQMSG_FUNC_DECL(topic); \
2525 SMQ::Message SMQMSG_##topic(STRID(#topic), SMQHandler_##topic); \
2526 SMQMSG_FUNC_DECL(topic) { UNUSED_ARG(msg) handler }