main.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. #include <Arduino.h> // needed for PlatformIO
  2. #include <Mesh.h>
  3. #if defined(NRF52_PLATFORM)
  4. #include <InternalFileSystem.h>
  5. #elif defined(RP2040_PLATFORM)
  6. #include <LittleFS.h>
  7. #elif defined(ESP32)
  8. #include <SPIFFS.h>
  9. #endif
  10. #include <helpers/ArduinoHelpers.h>
  11. #include <helpers/StaticPoolPacketManager.h>
  12. #include <helpers/SimpleMeshTables.h>
  13. #include <helpers/IdentityStore.h>
  14. #include <helpers/AdvertDataHelpers.h>
  15. #include <helpers/TxtDataHelpers.h>
  16. #include <helpers/CommonCLI.h>
  17. #include <RTClib.h>
  18. #include <target.h>
  19. /* ------------------------------ Config -------------------------------- */
  20. #ifndef FIRMWARE_BUILD_DATE
  21. #define FIRMWARE_BUILD_DATE "24 Jul 2025"
  22. #endif
  23. #ifndef FIRMWARE_VERSION
  24. #define FIRMWARE_VERSION "v1.7.4"
  25. #endif
  26. #ifndef LORA_FREQ
  27. #define LORA_FREQ 915.0
  28. #endif
  29. #ifndef LORA_BW
  30. #define LORA_BW 250
  31. #endif
  32. #ifndef LORA_SF
  33. #define LORA_SF 10
  34. #endif
  35. #ifndef LORA_CR
  36. #define LORA_CR 5
  37. #endif
  38. #ifndef LORA_TX_POWER
  39. #define LORA_TX_POWER 20
  40. #endif
  41. #ifndef ADVERT_NAME
  42. #define ADVERT_NAME "Test BBS"
  43. #endif
  44. #ifndef ADVERT_LAT
  45. #define ADVERT_LAT 0.0
  46. #endif
  47. #ifndef ADVERT_LON
  48. #define ADVERT_LON 0.0
  49. #endif
  50. #ifndef ADMIN_PASSWORD
  51. #define ADMIN_PASSWORD "password"
  52. #endif
  53. #ifndef MAX_CLIENTS
  54. #define MAX_CLIENTS 32
  55. #endif
  56. #ifndef MAX_UNSYNCED_POSTS
  57. #define MAX_UNSYNCED_POSTS 32
  58. #endif
  59. #ifndef SERVER_RESPONSE_DELAY
  60. #define SERVER_RESPONSE_DELAY 300
  61. #endif
  62. #ifndef TXT_ACK_DELAY
  63. #define TXT_ACK_DELAY 200
  64. #endif
  65. #ifdef DISPLAY_CLASS
  66. #include "UITask.h"
  67. static UITask ui_task(display);
  68. #endif
  69. #define FIRMWARE_ROLE "room_server"
  70. #define PACKET_LOG_FILE "/packet_log"
  71. /* ------------------------------ Code -------------------------------- */
  72. enum RoomPermission {
  73. ADMIN,
  74. GUEST,
  75. READ_ONLY
  76. };
  77. struct ClientInfo {
  78. mesh::Identity id;
  79. uint32_t last_timestamp; // by THEIR clock
  80. uint32_t last_activity; // by OUR clock
  81. uint32_t sync_since; // sync messages SINCE this timestamp (by OUR clock)
  82. uint32_t pending_ack;
  83. uint32_t push_post_timestamp;
  84. unsigned long ack_timeout;
  85. RoomPermission permission;
  86. uint8_t push_failures;
  87. uint8_t secret[PUB_KEY_SIZE];
  88. int out_path_len;
  89. uint8_t out_path[MAX_PATH_SIZE];
  90. };
  91. #define MAX_POST_TEXT_LEN (160-9)
  92. struct PostInfo {
  93. mesh::Identity author;
  94. uint32_t post_timestamp; // by OUR clock
  95. char text[MAX_POST_TEXT_LEN+1];
  96. };
  97. #define REPLY_DELAY_MILLIS 1500
  98. #define PUSH_NOTIFY_DELAY_MILLIS 2000
  99. #define SYNC_PUSH_INTERVAL 1200
  100. #define PUSH_ACK_TIMEOUT_FLOOD 12000
  101. #define PUSH_TIMEOUT_BASE 4000
  102. #define PUSH_ACK_TIMEOUT_FACTOR 2000
  103. #define POST_SYNC_DELAY_SECS 6
  104. #define CLIENT_KEEP_ALIVE_SECS 0 // Now Disabled (was 128)
  105. #define REQ_TYPE_GET_STATUS 0x01 // same as _GET_STATS
  106. #define REQ_TYPE_KEEP_ALIVE 0x02
  107. #define REQ_TYPE_GET_TELEMETRY_DATA 0x03
  108. #define RESP_SERVER_LOGIN_OK 0 // response to ANON_REQ
  109. struct ServerStats {
  110. uint16_t batt_milli_volts;
  111. uint16_t curr_tx_queue_len;
  112. int16_t noise_floor;
  113. int16_t last_rssi;
  114. uint32_t n_packets_recv;
  115. uint32_t n_packets_sent;
  116. uint32_t total_air_time_secs;
  117. uint32_t total_up_time_secs;
  118. uint32_t n_sent_flood, n_sent_direct;
  119. uint32_t n_recv_flood, n_recv_direct;
  120. uint16_t err_events; // was 'n_full_events'
  121. int16_t last_snr; // x 4
  122. uint16_t n_direct_dups, n_flood_dups;
  123. uint16_t n_posted, n_post_push;
  124. };
  125. class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
  126. FILESYSTEM* _fs;
  127. unsigned long next_local_advert, next_flood_advert;
  128. bool _logging;
  129. NodePrefs _prefs;
  130. CommonCLI _cli;
  131. uint8_t reply_data[MAX_PACKET_PAYLOAD];
  132. int num_clients;
  133. ClientInfo known_clients[MAX_CLIENTS];
  134. unsigned long next_push;
  135. uint16_t _num_posted, _num_post_pushes;
  136. int next_client_idx; // for round-robin polling
  137. int next_post_idx;
  138. PostInfo posts[MAX_UNSYNCED_POSTS]; // cyclic queue
  139. CayenneLPP telemetry;
  140. unsigned long set_radio_at, revert_radio_at;
  141. float pending_freq;
  142. float pending_bw;
  143. uint8_t pending_sf;
  144. uint8_t pending_cr;
  145. ClientInfo* putClient(const mesh::Identity& id) {
  146. for (int i = 0; i < num_clients; i++) {
  147. if (id.matches(known_clients[i].id)) return &known_clients[i]; // already known
  148. }
  149. ClientInfo* newClient;
  150. if (num_clients < MAX_CLIENTS) {
  151. newClient = &known_clients[num_clients++];
  152. } else { // table is currently full
  153. // evict least active client
  154. uint32_t oldest_timestamp = 0xFFFFFFFF;
  155. newClient = &known_clients[0];
  156. for (int i = 0; i < num_clients; i++) {
  157. auto c = &known_clients[i];
  158. if (c->last_activity < oldest_timestamp) {
  159. oldest_timestamp = c->last_activity;
  160. newClient = c;
  161. }
  162. }
  163. }
  164. newClient->id = id;
  165. newClient->out_path_len = -1; // initially out_path is unknown
  166. newClient->last_timestamp = 0;
  167. return newClient;
  168. }
  169. void evict(ClientInfo* client) {
  170. client->last_activity = 0; // this slot will now be re-used (will be oldest)
  171. memset(client->id.pub_key, 0, sizeof(client->id.pub_key));
  172. memset(client->secret, 0, sizeof(client->secret));
  173. client->pending_ack = 0;
  174. }
  175. void addPost(ClientInfo* client, const char* postData) {
  176. // TODO: suggested postData format: <title>/<descrption>
  177. posts[next_post_idx].author = client->id; // add to cyclic queue
  178. StrHelper::strncpy(posts[next_post_idx].text, postData, MAX_POST_TEXT_LEN);
  179. posts[next_post_idx].post_timestamp = getRTCClock()->getCurrentTimeUnique();
  180. next_post_idx = (next_post_idx + 1) % MAX_UNSYNCED_POSTS;
  181. next_push = futureMillis(PUSH_NOTIFY_DELAY_MILLIS);
  182. _num_posted++; // stats
  183. }
  184. void pushPostToClient(ClientInfo* client, PostInfo& post) {
  185. int len = 0;
  186. memcpy(&reply_data[len], &post.post_timestamp, 4); len += 4; // this is a PAST timestamp... but should be accepted by client
  187. uint8_t attempt;
  188. getRNG()->random(&attempt, 1); // need this for re-tries, so packet hash (and ACK) will be different
  189. reply_data[len++] = (TXT_TYPE_SIGNED_PLAIN << 2) | (attempt & 3); // 'signed' plain text
  190. // encode prefix of post.author.pub_key
  191. memcpy(&reply_data[len], post.author.pub_key, 4); len += 4; // just first 4 bytes
  192. int text_len = strlen(post.text);
  193. memcpy(&reply_data[len], post.text, text_len); len += text_len;
  194. // calc expected ACK reply
  195. mesh::Utils::sha256((uint8_t *)&client->pending_ack, 4, reply_data, len, client->id.pub_key, PUB_KEY_SIZE);
  196. client->push_post_timestamp = post.post_timestamp;
  197. auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, client->id, client->secret, reply_data, len);
  198. if (reply) {
  199. if (client->out_path_len < 0) {
  200. sendFlood(reply);
  201. client->ack_timeout = futureMillis(PUSH_ACK_TIMEOUT_FLOOD);
  202. } else {
  203. sendDirect(reply, client->out_path, client->out_path_len);
  204. client->ack_timeout = futureMillis(PUSH_TIMEOUT_BASE + PUSH_ACK_TIMEOUT_FACTOR * (client->out_path_len + 1));
  205. }
  206. _num_post_pushes++; // stats
  207. } else {
  208. client->pending_ack = 0;
  209. MESH_DEBUG_PRINTLN("Unable to push post to client");
  210. }
  211. }
  212. uint8_t getUnsyncedCount(ClientInfo* client) {
  213. uint8_t count = 0;
  214. for (int k = 0; k < MAX_UNSYNCED_POSTS; k++) {
  215. if (posts[k].post_timestamp > client->sync_since // is new post for this Client?
  216. && !posts[k].author.matches(client->id)) { // don't push posts to the author
  217. count++;
  218. }
  219. }
  220. return count;
  221. }
  222. bool processAck(const uint8_t *data) {
  223. for (int i = 0; i < num_clients; i++) {
  224. auto client = &known_clients[i];
  225. if (client->pending_ack && memcmp(data, &client->pending_ack, 4) == 0) { // got an ACK from Client!
  226. client->pending_ack = 0; // clear this, so next push can happen
  227. client->push_failures = 0;
  228. client->sync_since = client->push_post_timestamp; // advance Client's SINCE timestamp, to sync next post
  229. return true;
  230. }
  231. }
  232. return false;
  233. }
  234. mesh::Packet* createSelfAdvert() {
  235. uint8_t app_data[MAX_ADVERT_DATA_SIZE];
  236. uint8_t app_data_len;
  237. {
  238. AdvertDataBuilder builder(ADV_TYPE_ROOM, _prefs.node_name, _prefs.node_lat, _prefs.node_lon);
  239. app_data_len = builder.encodeTo(app_data);
  240. }
  241. return createAdvert(self_id, app_data, app_data_len);
  242. }
  243. File openAppend(const char* fname) {
  244. #if defined(NRF52_PLATFORM)
  245. return _fs->open(fname, FILE_O_WRITE);
  246. #elif defined(RP2040_PLATFORM)
  247. return _fs->open(fname, "a");
  248. #else
  249. return _fs->open(fname, "a", true);
  250. #endif
  251. }
  252. int handleRequest(ClientInfo* sender, uint32_t sender_timestamp, uint8_t* payload, size_t payload_len) {
  253. // uint32_t now = getRTCClock()->getCurrentTimeUnique();
  254. // memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
  255. memcpy(reply_data, &sender_timestamp, 4); // reflect sender_timestamp back in response packet (kind of like a 'tag')
  256. switch (payload[0]) {
  257. case REQ_TYPE_GET_STATUS: {
  258. ServerStats stats;
  259. stats.batt_milli_volts = board.getBattMilliVolts();
  260. stats.curr_tx_queue_len = _mgr->getOutboundCount(0xFFFFFFFF);
  261. stats.noise_floor = (int16_t)_radio->getNoiseFloor();
  262. stats.last_rssi = (int16_t) radio_driver.getLastRSSI();
  263. stats.n_packets_recv = radio_driver.getPacketsRecv();
  264. stats.n_packets_sent = radio_driver.getPacketsSent();
  265. stats.total_air_time_secs = getTotalAirTime() / 1000;
  266. stats.total_up_time_secs = _ms->getMillis() / 1000;
  267. stats.n_sent_flood = getNumSentFlood();
  268. stats.n_sent_direct = getNumSentDirect();
  269. stats.n_recv_flood = getNumRecvFlood();
  270. stats.n_recv_direct = getNumRecvDirect();
  271. stats.err_events = _err_flags;
  272. stats.last_snr = (int16_t)(radio_driver.getLastSNR() * 4);
  273. stats.n_direct_dups = ((SimpleMeshTables *)getTables())->getNumDirectDups();
  274. stats.n_flood_dups = ((SimpleMeshTables *)getTables())->getNumFloodDups();
  275. stats.n_posted = _num_posted;
  276. stats.n_post_push = _num_post_pushes;
  277. memcpy(&reply_data[4], &stats, sizeof(stats));
  278. return 4 + sizeof(stats);
  279. }
  280. case REQ_TYPE_GET_TELEMETRY_DATA: {
  281. uint8_t perm_mask = ~(payload[1]); // NEW: first reserved byte (of 4), is now inverse mask to apply to permissions
  282. telemetry.reset();
  283. telemetry.addVoltage(TELEM_CHANNEL_SELF, (float)board.getBattMilliVolts() / 1000.0f);
  284. // query other sensors -- target specific
  285. sensors.querySensors((sender->permission == RoomPermission::ADMIN ? 0xFF : 0x00) & perm_mask, telemetry);
  286. uint8_t tlen = telemetry.getSize();
  287. memcpy(&reply_data[4], telemetry.getBuffer(), tlen);
  288. return 4 + tlen; // reply_len
  289. }
  290. }
  291. return 0; // unknown command
  292. }
  293. protected:
  294. float getAirtimeBudgetFactor() const override {
  295. return _prefs.airtime_factor;
  296. }
  297. void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override {
  298. #if MESH_PACKET_LOGGING
  299. Serial.print(getLogDateTime());
  300. Serial.print(" RAW: ");
  301. mesh::Utils::printHex(Serial, raw, len);
  302. Serial.println();
  303. #endif
  304. }
  305. void logRx(mesh::Packet* pkt, int len, float score) override {
  306. if (_logging) {
  307. File f = openAppend(PACKET_LOG_FILE);
  308. if (f) {
  309. f.print(getLogDateTime());
  310. f.printf(": RX, len=%d (type=%d, route=%s, payload_len=%d) SNR=%d RSSI=%d score=%d",
  311. len, pkt->getPayloadType(), pkt->isRouteDirect() ? "D" : "F", pkt->payload_len,
  312. (int)_radio->getLastSNR(), (int)_radio->getLastRSSI(), (int)(score*1000));
  313. if (pkt->getPayloadType() == PAYLOAD_TYPE_PATH || pkt->getPayloadType() == PAYLOAD_TYPE_REQ
  314. || pkt->getPayloadType() == PAYLOAD_TYPE_RESPONSE || pkt->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) {
  315. f.printf(" [%02X -> %02X]\n", (uint32_t)pkt->payload[1], (uint32_t)pkt->payload[0]);
  316. } else {
  317. f.printf("\n");
  318. }
  319. f.close();
  320. }
  321. }
  322. }
  323. void logTx(mesh::Packet* pkt, int len) override {
  324. if (_logging) {
  325. File f = openAppend(PACKET_LOG_FILE);
  326. if (f) {
  327. f.print(getLogDateTime());
  328. f.printf(": TX, len=%d (type=%d, route=%s, payload_len=%d)",
  329. len, pkt->getPayloadType(), pkt->isRouteDirect() ? "D" : "F", pkt->payload_len);
  330. if (pkt->getPayloadType() == PAYLOAD_TYPE_PATH || pkt->getPayloadType() == PAYLOAD_TYPE_REQ
  331. || pkt->getPayloadType() == PAYLOAD_TYPE_RESPONSE || pkt->getPayloadType() == PAYLOAD_TYPE_TXT_MSG) {
  332. f.printf(" [%02X -> %02X]\n", (uint32_t)pkt->payload[1], (uint32_t)pkt->payload[0]);
  333. } else {
  334. f.printf("\n");
  335. }
  336. f.close();
  337. }
  338. }
  339. }
  340. void logTxFail(mesh::Packet* pkt, int len) override {
  341. if (_logging) {
  342. File f = openAppend(PACKET_LOG_FILE);
  343. if (f) {
  344. f.print(getLogDateTime());
  345. f.printf(": TX FAIL!, len=%d (type=%d, route=%s, payload_len=%d)\n",
  346. len, pkt->getPayloadType(), pkt->isRouteDirect() ? "D" : "F", pkt->payload_len);
  347. f.close();
  348. }
  349. }
  350. }
  351. int calcRxDelay(float score, uint32_t air_time) const override {
  352. if (_prefs.rx_delay_base <= 0.0f) return 0;
  353. return (int) ((pow(_prefs.rx_delay_base, 0.85f - score) - 1.0) * air_time);
  354. }
  355. const char* getLogDateTime() override {
  356. static char tmp[32];
  357. uint32_t now = getRTCClock()->getCurrentTime();
  358. DateTime dt = DateTime(now);
  359. sprintf(tmp, "%02d:%02d:%02d - %d/%d/%d U", dt.hour(), dt.minute(), dt.second(), dt.day(), dt.month(), dt.year());
  360. return tmp;
  361. }
  362. uint32_t getRetransmitDelay(const mesh::Packet* packet) override {
  363. uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * _prefs.tx_delay_factor);
  364. return getRNG()->nextInt(0, 6)*t;
  365. }
  366. uint32_t getDirectRetransmitDelay(const mesh::Packet* packet) override {
  367. uint32_t t = (_radio->getEstAirtimeFor(packet->path_len + packet->payload_len + 2) * _prefs.direct_tx_delay_factor);
  368. return getRNG()->nextInt(0, 6)*t;
  369. }
  370. int getInterferenceThreshold() const override {
  371. return _prefs.interference_threshold;
  372. }
  373. int getAGCResetInterval() const override {
  374. return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds
  375. }
  376. uint8_t getExtraAckTransmitCount() const override {
  377. return _prefs.multi_acks;
  378. }
  379. bool allowPacketForward(const mesh::Packet* packet) override {
  380. if (_prefs.disable_fwd) return false;
  381. if (packet->isRouteFlood() && packet->path_len >= _prefs.flood_max) return false;
  382. return true;
  383. }
  384. void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override {
  385. if (packet->getPayloadType() == PAYLOAD_TYPE_ANON_REQ) { // received an initial request by a possible admin client (unknown at this stage)
  386. uint32_t sender_timestamp, sender_sync_since;
  387. memcpy(&sender_timestamp, data, 4);
  388. memcpy(&sender_sync_since, &data[4], 4); // sender's "sync messags SINCE x" timestamp
  389. RoomPermission perm;
  390. data[len] = 0; // ensure null terminator
  391. if (strcmp((char *) &data[8], _prefs.password) == 0) { // check for valid admin password
  392. perm = RoomPermission::ADMIN;
  393. } else {
  394. if (strcmp((char *) &data[8], _prefs.guest_password) == 0) { // check the room/public password
  395. perm = RoomPermission::GUEST;
  396. } else if (_prefs.allow_read_only) {
  397. perm = RoomPermission::READ_ONLY;
  398. } else {
  399. MESH_DEBUG_PRINTLN("Incorrect room password");
  400. return; // no response. Client will timeout
  401. }
  402. }
  403. auto client = putClient(sender); // add to known clients (if not already known)
  404. if (sender_timestamp <= client->last_timestamp) {
  405. MESH_DEBUG_PRINTLN("possible replay attack!");
  406. return;
  407. }
  408. MESH_DEBUG_PRINTLN("Login success!");
  409. client->permission = perm;
  410. client->last_timestamp = sender_timestamp;
  411. client->sync_since = sender_sync_since;
  412. client->pending_ack = 0;
  413. client->push_failures = 0;
  414. memcpy(client->secret, secret, PUB_KEY_SIZE);
  415. uint32_t now = getRTCClock()->getCurrentTime();
  416. client->last_activity = now;
  417. now = getRTCClock()->getCurrentTimeUnique();
  418. memcpy(reply_data, &now, 4); // response packets always prefixed with timestamp
  419. // TODO: maybe reply with count of messages waiting to be synced for THIS client?
  420. reply_data[4] = RESP_SERVER_LOGIN_OK;
  421. reply_data[5] = (CLIENT_KEEP_ALIVE_SECS >> 4); // NEW: recommended keep-alive interval (secs / 16)
  422. reply_data[6] = (perm == RoomPermission::ADMIN ? 1 : (perm == RoomPermission::GUEST ? 0 : 2));
  423. reply_data[7] = getUnsyncedCount(client); // NEW
  424. memcpy(&reply_data[8], "OK", 2); // REVISIT: not really needed
  425. next_push = futureMillis(PUSH_NOTIFY_DELAY_MILLIS); // delay next push, give RESPONSE packet time to arrive first
  426. if (packet->isRouteFlood()) {
  427. // let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
  428. mesh::Packet* path = createPathReturn(sender, client->secret, packet->path, packet->path_len,
  429. PAYLOAD_TYPE_RESPONSE, reply_data, 8 + 2);
  430. if (path) sendFlood(path, SERVER_RESPONSE_DELAY);
  431. } else {
  432. mesh::Packet* reply = createDatagram(PAYLOAD_TYPE_RESPONSE, sender, client->secret, reply_data, 8 + 2);
  433. if (reply) {
  434. if (client->out_path_len >= 0) { // we have an out_path, so send DIRECT
  435. sendDirect(reply, client->out_path, client->out_path_len, SERVER_RESPONSE_DELAY);
  436. } else {
  437. sendFlood(reply, SERVER_RESPONSE_DELAY);
  438. }
  439. }
  440. }
  441. }
  442. }
  443. int matching_peer_indexes[MAX_CLIENTS];
  444. int searchPeersByHash(const uint8_t* hash) override {
  445. int n = 0;
  446. for (int i = 0; i < num_clients; i++) {
  447. if (known_clients[i].id.isHashMatch(hash)) {
  448. matching_peer_indexes[n++] = i; // store the INDEXES of matching contacts (for subsequent 'peer' methods)
  449. }
  450. }
  451. return n;
  452. }
  453. void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override {
  454. int i = matching_peer_indexes[peer_idx];
  455. if (i >= 0 && i < num_clients) {
  456. // lookup pre-calculated shared_secret
  457. memcpy(dest_secret, known_clients[i].secret, PUB_KEY_SIZE);
  458. } else {
  459. MESH_DEBUG_PRINTLN("getPeerSharedSecret: Invalid peer idx: %d", i);
  460. }
  461. }
  462. void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override {
  463. int i = matching_peer_indexes[sender_idx];
  464. if (i < 0 || i >= num_clients) { // get from our known_clients table (sender SHOULD already be known in this context)
  465. MESH_DEBUG_PRINTLN("onPeerDataRecv: invalid peer idx: %d", i);
  466. return;
  467. }
  468. auto client = &known_clients[i];
  469. if (type == PAYLOAD_TYPE_TXT_MSG && len > 5) { // a CLI command or new Post
  470. uint32_t sender_timestamp;
  471. memcpy(&sender_timestamp, data, 4); // timestamp (by sender's RTC clock - which could be wrong)
  472. uint flags = (data[4] >> 2); // message attempt number, and other flags
  473. if (!(flags == TXT_TYPE_PLAIN || flags == TXT_TYPE_CLI_DATA)) {
  474. MESH_DEBUG_PRINTLN("onPeerDataRecv: unsupported command flags received: flags=%02x", (uint32_t)flags);
  475. } else if (sender_timestamp >= client->last_timestamp) { // prevent replay attacks, but send Acks for retries
  476. bool is_retry = (sender_timestamp == client->last_timestamp);
  477. client->last_timestamp = sender_timestamp;
  478. uint32_t now = getRTCClock()->getCurrentTimeUnique();
  479. client->last_activity = now;
  480. client->push_failures = 0; // reset so push can resume (if prev failed)
  481. // len can be > original length, but 'text' will be padded with zeroes
  482. data[len] = 0; // need to make a C string again, with null terminator
  483. uint32_t ack_hash; // calc truncated hash of the message timestamp + text + sender pub_key, to prove to sender that we got it
  484. mesh::Utils::sha256((uint8_t *) &ack_hash, 4, data, 5 + strlen((char *)&data[5]), client->id.pub_key, PUB_KEY_SIZE);
  485. uint8_t temp[166];
  486. bool send_ack;
  487. if (flags == TXT_TYPE_CLI_DATA) {
  488. if (client->permission == RoomPermission::ADMIN) {
  489. if (is_retry) {
  490. temp[5] = 0; // no reply
  491. } else {
  492. handleCommand(sender_timestamp, (char *) &data[5], (char *) &temp[5]);
  493. temp[4] = (TXT_TYPE_CLI_DATA << 2); // attempt and flags, (NOTE: legacy was: TXT_TYPE_PLAIN)
  494. }
  495. send_ack = false;
  496. } else {
  497. temp[5] = 0; // no reply
  498. send_ack = false; // and no ACK... user shoudn't be sending these
  499. }
  500. } else { // TXT_TYPE_PLAIN
  501. if (client->permission == RoomPermission::READ_ONLY) {
  502. temp[5] = 0; // no reply
  503. send_ack = false; // no ACK
  504. } else {
  505. if (!is_retry) {
  506. addPost(client, (const char *) &data[5]);
  507. }
  508. temp[5] = 0; // no reply (ACK is enough)
  509. send_ack = true;
  510. }
  511. }
  512. uint32_t delay_millis;
  513. if (send_ack) {
  514. if (client->out_path_len < 0) {
  515. mesh::Packet* ack = createAck(ack_hash);
  516. if (ack) sendFlood(ack, TXT_ACK_DELAY);
  517. delay_millis = TXT_ACK_DELAY + REPLY_DELAY_MILLIS;
  518. } else {
  519. uint32_t d = TXT_ACK_DELAY;
  520. if (getExtraAckTransmitCount() > 0) {
  521. mesh::Packet* a1 = createMultiAck(ack_hash, 1);
  522. if (a1) sendDirect(a1, client->out_path, client->out_path_len, d);
  523. d += 300;
  524. }
  525. mesh::Packet* a2 = createAck(ack_hash);
  526. if (a2) sendDirect(a2, client->out_path, client->out_path_len, d);
  527. delay_millis = d + REPLY_DELAY_MILLIS;
  528. }
  529. } else {
  530. delay_millis = 0;
  531. }
  532. int text_len = strlen((char *) &temp[5]);
  533. if (text_len > 0) {
  534. if (now == sender_timestamp) {
  535. // WORKAROUND: the two timestamps need to be different, in the CLI view
  536. now++;
  537. }
  538. memcpy(temp, &now, 4); // mostly an extra blob to help make packet_hash unique
  539. // calc expected ACK reply
  540. //mesh::Utils::sha256((uint8_t *)&expected_ack_crc, 4, temp, 5 + text_len, self_id.pub_key, PUB_KEY_SIZE);
  541. auto reply = createDatagram(PAYLOAD_TYPE_TXT_MSG, client->id, secret, temp, 5 + text_len);
  542. if (reply) {
  543. if (client->out_path_len < 0) {
  544. sendFlood(reply, delay_millis + SERVER_RESPONSE_DELAY);
  545. } else {
  546. sendDirect(reply, client->out_path, client->out_path_len, delay_millis + SERVER_RESPONSE_DELAY);
  547. }
  548. }
  549. }
  550. } else {
  551. MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected");
  552. }
  553. } else if (type == PAYLOAD_TYPE_REQ && len >= 5) {
  554. uint32_t sender_timestamp;
  555. memcpy(&sender_timestamp, data, 4); // timestamp (by sender's RTC clock - which could be wrong)
  556. if (sender_timestamp < client->last_timestamp) { // prevent replay attacks
  557. MESH_DEBUG_PRINTLN("onPeerDataRecv: possible replay attack detected");
  558. } else {
  559. client->last_timestamp = sender_timestamp;
  560. uint32_t now = getRTCClock()->getCurrentTime();
  561. client->last_activity = now; // <-- THIS will keep client connection alive
  562. client->push_failures = 0; // reset so push can resume (if prev failed)
  563. if (data[4] == REQ_TYPE_KEEP_ALIVE && packet->isRouteDirect()) { // request type
  564. uint32_t forceSince = 0;
  565. if (len >= 9) { // optional - last post_timestamp client received
  566. memcpy(&forceSince, &data[5], 4); // NOTE: this may be 0, if part of decrypted PADDING!
  567. } else {
  568. memcpy(&data[5], &forceSince, 4); // make sure there are zeroes in payload (for ack_hash calc below)
  569. }
  570. if (forceSince > 0) {
  571. client->sync_since = forceSince; // force-update the 'sync since'
  572. }
  573. client->pending_ack = 0;
  574. // TODO: Throttle KEEP_ALIVE requests!
  575. // if client sends too quickly, evict()
  576. // RULE: only send keep_alive response DIRECT!
  577. if (client->out_path_len >= 0) {
  578. uint32_t ack_hash; // calc ACK to prove to sender that we got request
  579. mesh::Utils::sha256((uint8_t *) &ack_hash, 4, data, 9, client->id.pub_key, PUB_KEY_SIZE);
  580. auto reply = createAck(ack_hash);
  581. if (reply) {
  582. reply->payload[reply->payload_len++] = getUnsyncedCount(client); // NEW: add unsynced counter to end of ACK packet
  583. sendDirect(reply, client->out_path, client->out_path_len, SERVER_RESPONSE_DELAY);
  584. }
  585. }
  586. } else {
  587. int reply_len = handleRequest(client, sender_timestamp, &data[4], len - 4);
  588. if (reply_len > 0) { // valid command
  589. if (packet->isRouteFlood()) {
  590. // let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
  591. mesh::Packet* path = createPathReturn(client->id, secret, packet->path, packet->path_len,
  592. PAYLOAD_TYPE_RESPONSE, reply_data, reply_len);
  593. if (path) sendFlood(path, SERVER_RESPONSE_DELAY);
  594. } else {
  595. mesh::Packet* reply = createDatagram(PAYLOAD_TYPE_RESPONSE, client->id, secret, reply_data, reply_len);
  596. if (reply) {
  597. if (client->out_path_len >= 0) { // we have an out_path, so send DIRECT
  598. sendDirect(reply, client->out_path, client->out_path_len, SERVER_RESPONSE_DELAY);
  599. } else {
  600. sendFlood(reply, SERVER_RESPONSE_DELAY);
  601. }
  602. }
  603. }
  604. }
  605. }
  606. }
  607. }
  608. }
  609. bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override {
  610. // TODO: prevent replay attacks
  611. int i = matching_peer_indexes[sender_idx];
  612. if (i >= 0 && i < num_clients) { // get from our known_clients table (sender SHOULD already be known in this context)
  613. MESH_DEBUG_PRINTLN("PATH to client, path_len=%d", (uint32_t) path_len);
  614. auto client = &known_clients[i];
  615. memcpy(client->out_path, path, client->out_path_len = path_len); // store a copy of path, for sendDirect()
  616. } else {
  617. MESH_DEBUG_PRINTLN("onPeerPathRecv: invalid peer idx: %d", i);
  618. }
  619. if (extra_type == PAYLOAD_TYPE_ACK && extra_len >= 4) {
  620. // also got an encoded ACK!
  621. processAck(extra);
  622. }
  623. // NOTE: no reciprocal path send!!
  624. return false;
  625. }
  626. void onAckRecv(mesh::Packet* packet, uint32_t ack_crc) override {
  627. if (processAck((uint8_t *)&ack_crc)) {
  628. packet->markDoNotRetransmit(); // ACK was for this node, so don't retransmit
  629. }
  630. }
  631. public:
  632. MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables)
  633. : mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables),
  634. _cli(board, rtc, &_prefs, this), telemetry(MAX_PACKET_PAYLOAD - 4)
  635. {
  636. next_local_advert = next_flood_advert = 0;
  637. _logging = false;
  638. set_radio_at = revert_radio_at = 0;
  639. // defaults
  640. memset(&_prefs, 0, sizeof(_prefs));
  641. _prefs.airtime_factor = 1.0; // one half
  642. _prefs.rx_delay_base = 0.0f; // off by default, was 10.0
  643. _prefs.tx_delay_factor = 0.5f; // was 0.25f;
  644. StrHelper::strncpy(_prefs.node_name, ADVERT_NAME, sizeof(_prefs.node_name));
  645. _prefs.node_lat = ADVERT_LAT;
  646. _prefs.node_lon = ADVERT_LON;
  647. StrHelper::strncpy(_prefs.password, ADMIN_PASSWORD, sizeof(_prefs.password));
  648. _prefs.freq = LORA_FREQ;
  649. _prefs.sf = LORA_SF;
  650. _prefs.bw = LORA_BW;
  651. _prefs.cr = LORA_CR;
  652. _prefs.tx_power_dbm = LORA_TX_POWER;
  653. _prefs.disable_fwd = 1;
  654. _prefs.advert_interval = 1; // default to 2 minutes for NEW installs
  655. _prefs.flood_advert_interval = 12; // 12 hours
  656. _prefs.flood_max = 64;
  657. _prefs.interference_threshold = 0; // disabled
  658. #ifdef ROOM_PASSWORD
  659. StrHelper::strncpy(_prefs.guest_password, ROOM_PASSWORD, sizeof(_prefs.guest_password));
  660. #endif
  661. num_clients = 0;
  662. next_post_idx = 0;
  663. next_client_idx = 0;
  664. next_push = 0;
  665. memset(posts, 0, sizeof(posts));
  666. _num_posted = _num_post_pushes = 0;
  667. }
  668. void begin(FILESYSTEM* fs) {
  669. mesh::Mesh::begin();
  670. _fs = fs;
  671. // load persisted prefs
  672. _cli.loadPrefs(_fs);
  673. radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
  674. radio_set_tx_power(_prefs.tx_power_dbm);
  675. updateAdvertTimer();
  676. updateFloodAdvertTimer();
  677. }
  678. const char* getFirmwareVer() override { return FIRMWARE_VERSION; }
  679. const char* getBuildDate() override { return FIRMWARE_BUILD_DATE; }
  680. const char* getRole() override { return FIRMWARE_ROLE; }
  681. const char* getNodeName() { return _prefs.node_name; }
  682. NodePrefs* getNodePrefs() {
  683. return &_prefs;
  684. }
  685. void savePrefs() override {
  686. _cli.savePrefs(_fs);
  687. }
  688. void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override {
  689. set_radio_at = futureMillis(2000); // give CLI reply some time to be sent back, before applying temp radio params
  690. pending_freq = freq;
  691. pending_bw = bw;
  692. pending_sf = sf;
  693. pending_cr = cr;
  694. revert_radio_at = futureMillis(2000 + timeout_mins*60*1000); // schedule when to revert radio params
  695. }
  696. bool formatFileSystem() override {
  697. #if defined(NRF52_PLATFORM)
  698. return InternalFS.format();
  699. #elif defined(RP2040_PLATFORM)
  700. return LittleFS.format();
  701. #elif defined(ESP32)
  702. return SPIFFS.format();
  703. #else
  704. #error "need to implement file system erase"
  705. return false;
  706. #endif
  707. }
  708. void sendSelfAdvertisement(int delay_millis) override {
  709. mesh::Packet* pkt = createSelfAdvert();
  710. if (pkt) {
  711. sendFlood(pkt, delay_millis);
  712. } else {
  713. MESH_DEBUG_PRINTLN("ERROR: unable to create advertisement packet!");
  714. }
  715. }
  716. void updateAdvertTimer() override {
  717. if (_prefs.advert_interval > 0) { // schedule local advert timer
  718. next_local_advert = futureMillis((uint32_t)_prefs.advert_interval * 2 * 60 * 1000);
  719. } else {
  720. next_local_advert = 0; // stop the timer
  721. }
  722. }
  723. void updateFloodAdvertTimer() override {
  724. if (_prefs.flood_advert_interval > 0) { // schedule flood advert timer
  725. next_flood_advert = futureMillis( ((uint32_t)_prefs.flood_advert_interval) * 60 * 60 * 1000);
  726. } else {
  727. next_flood_advert = 0; // stop the timer
  728. }
  729. }
  730. void setLoggingOn(bool enable) override { _logging = enable; }
  731. void eraseLogFile() override {
  732. _fs->remove(PACKET_LOG_FILE);
  733. }
  734. void dumpLogFile() override {
  735. #if defined(RP2040_PLATFORM)
  736. File f = _fs->open(PACKET_LOG_FILE, "r");
  737. #else
  738. File f = _fs->open(PACKET_LOG_FILE);
  739. #endif
  740. if (f) {
  741. while (f.available()) {
  742. int c = f.read();
  743. if (c < 0) break;
  744. Serial.print((char)c);
  745. }
  746. f.close();
  747. }
  748. }
  749. void setTxPower(uint8_t power_dbm) override {
  750. radio_set_tx_power(power_dbm);
  751. }
  752. void formatNeighborsReply(char *reply) override {
  753. strcpy(reply, "not supported");
  754. }
  755. mesh::LocalIdentity& getSelfId() override { return self_id; }
  756. void clearStats() override {
  757. radio_driver.resetStats();
  758. resetStats();
  759. ((SimpleMeshTables *)getTables())->resetStats();
  760. }
  761. void handleCommand(uint32_t sender_timestamp, char* command, char* reply) {
  762. while (*command == ' ') command++; // skip leading spaces
  763. if (strlen(command) > 4 && command[2] == '|') { // optional prefix (for companion radio CLI)
  764. memcpy(reply, command, 3); // reflect the prefix back
  765. reply += 3;
  766. command += 3;
  767. }
  768. _cli.handleCommand(sender_timestamp, command, reply); // common CLI commands
  769. }
  770. void loop() {
  771. mesh::Mesh::loop();
  772. if (millisHasNowPassed(next_push) && num_clients > 0) {
  773. // check for ACK timeouts
  774. for (int i = 0; i < num_clients; i++) {
  775. auto c = &known_clients[i];
  776. if (c->pending_ack && millisHasNowPassed(c->ack_timeout)) {
  777. c->push_failures++;
  778. c->pending_ack = 0; // reset (TODO: keep prev expected_ack's in a list, incase they arrive LATER, after we retry)
  779. MESH_DEBUG_PRINTLN("pending ACK timed out: push_failures: %d", (uint32_t)c->push_failures);
  780. }
  781. }
  782. // check next Round-Robin client, and sync next new post
  783. auto client = &known_clients[next_client_idx];
  784. bool did_push = false;
  785. if (client->pending_ack == 0 && client->last_activity != 0 && client->push_failures < 3) { // not already waiting for ACK, AND not evicted, AND retries not max
  786. MESH_DEBUG_PRINTLN("loop - checking for client %02X", (uint32_t) client->id.pub_key[0]);
  787. uint32_t now = getRTCClock()->getCurrentTime();
  788. for (int k = 0, idx = next_post_idx; k < MAX_UNSYNCED_POSTS; k++) {
  789. auto p = &posts[idx];
  790. if (now >= p->post_timestamp + POST_SYNC_DELAY_SECS && p->post_timestamp > client->sync_since // is new post for this Client?
  791. && !p->author.matches(client->id)) { // don't push posts to the author
  792. // push this post to Client, then wait for ACK
  793. pushPostToClient(client, *p);
  794. did_push = true;
  795. MESH_DEBUG_PRINTLN("loop - pushed to client %02X: %s", (uint32_t) client->id.pub_key[0], p->text);
  796. break;
  797. }
  798. idx = (idx + 1) % MAX_UNSYNCED_POSTS; // wrap to start of cyclic queue
  799. }
  800. } else {
  801. MESH_DEBUG_PRINTLN("loop - skipping busy (or evicted) client %02X", (uint32_t) client->id.pub_key[0]);
  802. }
  803. next_client_idx = (next_client_idx + 1) % num_clients; // round robin polling for each client
  804. if (did_push) {
  805. next_push = futureMillis(SYNC_PUSH_INTERVAL);
  806. } else {
  807. // were no unsynced posts for curr client, so proccess next client much quicker! (in next loop())
  808. next_push = futureMillis(SYNC_PUSH_INTERVAL / 8);
  809. }
  810. }
  811. if (next_flood_advert && millisHasNowPassed(next_flood_advert)) {
  812. mesh::Packet* pkt = createSelfAdvert();
  813. if (pkt) sendFlood(pkt);
  814. updateFloodAdvertTimer(); // schedule next flood advert
  815. updateAdvertTimer(); // also schedule local advert (so they don't overlap)
  816. } else if (next_local_advert && millisHasNowPassed(next_local_advert)) {
  817. mesh::Packet* pkt = createSelfAdvert();
  818. if (pkt) sendZeroHop(pkt);
  819. updateAdvertTimer(); // schedule next local advert
  820. }
  821. if (set_radio_at && millisHasNowPassed(set_radio_at)) { // apply pending (temporary) radio params
  822. set_radio_at = 0; // clear timer
  823. radio_set_params(pending_freq, pending_bw, pending_sf, pending_cr);
  824. MESH_DEBUG_PRINTLN("Temp radio params");
  825. }
  826. if (revert_radio_at && millisHasNowPassed(revert_radio_at)) { // revert radio params to orig
  827. revert_radio_at = 0; // clear timer
  828. radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);
  829. MESH_DEBUG_PRINTLN("Radio params restored");
  830. }
  831. #ifdef DISPLAY_CLASS
  832. ui_task.loop();
  833. #endif
  834. // TODO: periodically check for OLD/inactive entries in known_clients[], and evict
  835. }
  836. };
  837. StdRNG fast_rng;
  838. SimpleMeshTables tables;
  839. MyMesh the_mesh(board, radio_driver, *new ArduinoMillis(), fast_rng, rtc_clock, tables);
  840. void halt() {
  841. while (1) ;
  842. }
  843. static char command[MAX_POST_TEXT_LEN+1];
  844. void setup() {
  845. Serial.begin(115200);
  846. delay(1000);
  847. board.begin();
  848. #ifdef DISPLAY_CLASS
  849. if (display.begin()) {
  850. display.startFrame();
  851. display.print("Please wait...");
  852. display.endFrame();
  853. }
  854. #endif
  855. if (!radio_init()) { halt(); }
  856. fast_rng.begin(radio_get_rng_seed());
  857. FILESYSTEM* fs;
  858. #if defined(NRF52_PLATFORM)
  859. InternalFS.begin();
  860. fs = &InternalFS;
  861. IdentityStore store(InternalFS, "");
  862. #elif defined(RP2040_PLATFORM)
  863. LittleFS.begin();
  864. fs = &LittleFS;
  865. IdentityStore store(LittleFS, "/identity");
  866. store.begin();
  867. #elif defined(ESP32)
  868. SPIFFS.begin(true);
  869. fs = &SPIFFS;
  870. IdentityStore store(SPIFFS, "/identity");
  871. #else
  872. #error "need to define filesystem"
  873. #endif
  874. if (!store.load("_main", the_mesh.self_id)) {
  875. the_mesh.self_id = radio_new_identity(); // create new random identity
  876. int count = 0;
  877. while (count < 10 && (the_mesh.self_id.pub_key[0] == 0x00 || the_mesh.self_id.pub_key[0] == 0xFF)) { // reserved id hashes
  878. the_mesh.self_id = radio_new_identity(); count++;
  879. }
  880. store.save("_main", the_mesh.self_id);
  881. }
  882. Serial.print("Room ID: ");
  883. mesh::Utils::printHex(Serial, the_mesh.self_id.pub_key, PUB_KEY_SIZE); Serial.println();
  884. command[0] = 0;
  885. sensors.begin();
  886. the_mesh.begin(fs);
  887. #ifdef DISPLAY_CLASS
  888. ui_task.begin(the_mesh.getNodePrefs(), FIRMWARE_BUILD_DATE, FIRMWARE_VERSION);
  889. #endif
  890. // send out initial Advertisement to the mesh
  891. the_mesh.sendSelfAdvertisement(16000);
  892. }
  893. void loop() {
  894. int len = strlen(command);
  895. while (Serial.available() && len < sizeof(command)-1) {
  896. char c = Serial.read();
  897. if (c != '\n') {
  898. command[len++] = c;
  899. command[len] = 0;
  900. }
  901. Serial.print(c);
  902. }
  903. if (len == sizeof(command)-1) { // command buffer full
  904. command[sizeof(command)-1] = '\r';
  905. }
  906. if (len > 0 && command[len - 1] == '\r') { // received complete line
  907. command[len - 1] = 0; // replace newline with C string null terminator
  908. char reply[160];
  909. the_mesh.handleCommand(0, command, reply); // NOTE: there is no sender_timestamp via serial!
  910. if (reply[0]) {
  911. Serial.print(" -> "); Serial.println(reply);
  912. }
  913. command[0] = 0; // reset command buffer
  914. }
  915. the_mesh.loop();
  916. sensors.loop();
  917. }