From 5a4fab25066e77dec48bb8cac11799edce8be67f Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 1 May 2020 08:51:53 -0700 Subject: [PATCH] start msg sequence numbers with a random number each boot --- docs/software/nrf52-TODO.md | 3 ++- src/esp32/main-esp32.cpp | 2 ++ src/mesh/MeshService.cpp | 10 ++++++++-- src/nrf52/main-nrf52.cpp | 1 + src/rf95/RadioLibInterface.h | 2 +- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/docs/software/nrf52-TODO.md b/docs/software/nrf52-TODO.md index 323aa1e0e..17cf83652 100644 --- a/docs/software/nrf52-TODO.md +++ b/docs/software/nrf52-TODO.md @@ -5,7 +5,6 @@ Minimum items needed to make sure hardware is good. - add a hard fault handler -- at boot we are starting our message IDs at 1, rather we should start them at a random number. also, seed random based on timer. this could be the cause of our first message not seen bug. - use "variants" to get all gpio bindings - plug in correct variants for the real board - Use the PMU driver on real hardware @@ -57,6 +56,7 @@ Needed to be fully functional at least at the same level of the ESP32 boards. At - currently using soft device SD140, is that ideal? - turn on the watchdog timer, require servicing from key application threads - install a hardfault handler for null ptrs (if one isn't already installed) +- nrf52setup should call randomSeed(tbd) ## Things to do 'someday' @@ -97,6 +97,7 @@ Nice ideas worth considering someday... - DONE track rxbad, rxgood, txgood - DONE neg 7 error code from receive - DONE remove unused sx1262 lib from github +- at boot we are starting our message IDs at 1, rather we should start them at a random number. also, seed random based on timer. this could be the cause of our first message not seen bug. ``` diff --git a/src/esp32/main-esp32.cpp b/src/esp32/main-esp32.cpp index b15d60732..48af9b998 100644 --- a/src/esp32/main-esp32.cpp +++ b/src/esp32/main-esp32.cpp @@ -164,6 +164,8 @@ void axp192Init() void esp32Setup() { + randomSeed(esp_random()); // ESP docs say this is fairly random + #ifdef AXP192_SLAVE_ADDRESS axp192Init(); #endif diff --git a/src/mesh/MeshService.cpp b/src/mesh/MeshService.cpp index d0e693ada..873d17eca 100644 --- a/src/mesh/MeshService.cpp +++ b/src/mesh/MeshService.cpp @@ -61,8 +61,14 @@ static Periodic sendOwnerPeriod(sendOwnerCb); // FIXME, move this someplace better PacketId generatePacketId() { - static uint32_t i __attribute__(( - section(".noinit"))); // We try to keep this in noinit so that hopefully it keeps increasing even across reboots + static uint32_t i; // Note: trying to keep this in noinit didn't help for working across reboots + static bool didInit = false; + + if (!didInit) { + didInit = true; + i = random(0, NUM_PACKET_ID + + 1); // pick a random initial sequence number at boot (to prevent repeated reboots always starting at 0) + } i++; return (i % NUM_PACKET_ID) + 1; // return number between 1 and 255 diff --git a/src/nrf52/main-nrf52.cpp b/src/nrf52/main-nrf52.cpp index 255b80101..0fe2c8d96 100644 --- a/src/nrf52/main-nrf52.cpp +++ b/src/nrf52/main-nrf52.cpp @@ -67,4 +67,5 @@ void nrf52Setup() { // Not yet on board // pmu.init(); + DEBUG_MSG("FIXME, need to call randomSeed on nrf52!\n"); } \ No newline at end of file diff --git a/src/rf95/RadioLibInterface.h b/src/rf95/RadioLibInterface.h index bf5e20f7c..1e42aedb0 100644 --- a/src/rf95/RadioLibInterface.h +++ b/src/rf95/RadioLibInterface.h @@ -101,7 +101,7 @@ class RadioLibInterface : public RadioInterface void applyModemConfig(); /** Could we send right now (i.e. either not actively receiving or transmitting)? */ - virtual bool canSendImmediately(); + bool canSendImmediately(); /** are we actively receiving a packet (only called during receiving state) */ virtual bool isActivelyReceiving() = 0;