diff --git a/README.md b/README.md index 9626dcd..e98b979 100644 --- a/README.md +++ b/README.md @@ -216,9 +216,14 @@ To run the job the `doWorkCallback()` function is executed by the LMIC scheduler `doWorkCallback()` calls the `processWork()` function where the actual work is performed. The first `doWork` run is started in `setup()`. On completion `doWork` reschedules itself for the next run. +When the node has joined and the `EV_JOINED` event is handled by the event handler, the next scheduled doWork job is cancelled and is re-scheduled for immediate execution. This is done to prevent that any uplink will have to wait until the current doWork interval ends. `processWork()` skips doing any work while the node is still joining. As a result sending the first uplink message may have to wait until the current doWork interval ends (max `DO_WORK_INTERVAL_SECONDS` seconds). Directly running the doWork job after a join prevents the in this case unnecessary and unwanted delay. + ### 3.3 processWork() function -The `processWork()` function contains user code that performs the actual work like reading sensor data and scheduling uplink messages. +The `processWork()` function contains user code that performs the actual work like reading sensor data and scheduling uplink messages. In LMIC-node `processWork()` will skip doing any work if the node is still joining for two reasons: + +1. To prevent unnecessary incrementing of the counter. +2. Uplink messages cannot yet be sent. ### 3.4 processDownlink() function diff --git a/src/LMIC-node.cpp b/src/LMIC-node.cpp index a273aa8..85e216d 100644 --- a/src/LMIC-node.cpp +++ b/src/LMIC-node.cpp @@ -571,6 +571,14 @@ void onEvent(ev_t ev) // during join, but because slow data rates change // max TX size, it is not used in this example. LMIC_setLinkCheckMode(0); + + // The doWork job has probably run already (while + // the node was still joining) and have rescheduled itself. + // Cancel the next scheduled doWork job and re-schedule + // for immediate execution to prevent that any uplink will + // have to wait until the current doWork interval ends. + os_clearCallback(&doWorkJob); + os_setCallback(&doWorkJob, doWorkCallback); break; case EV_TXCOMPLETE: diff --git a/src/LMIC-node.h b/src/LMIC-node.h index 95ac194..3537da5 100644 --- a/src/LMIC-node.h +++ b/src/LMIC-node.h @@ -44,6 +44,7 @@ enum class InitType { Hardware, PostInitSerial }; enum class PrintTarget { All, Serial, Display }; // Forward declarations +static void doWorkCallback(osjob_t* job); void processWork(ostime_t timestamp); void processDownlink(ostime_t eventTimestamp, uint8_t fPort, uint8_t* data, uint8_t dataLength); void onLmicEvent(void *pUserData, ev_t ev);