Skip to content

Commit 22cc4f6

Browse files
matthijskooijmanfpistm
authored andcommitted
STM32LoRaWAN::setMaintainNeededCallback: Add method
This method allows asynchronous applications to get notified when there is work to be done, which allows them to be much more efficient and also allows implementing sleeping without race conditions.
1 parent 2317171 commit 22cc4f6

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/STM32LoRaWAN.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ void STM32LoRaWAN::MacProcessNotify()
105105
{
106106
// Called by the stack from an ISR when there is work to do
107107
instance->mac_process_pending = true;
108+
if (instance->maintain_needed_callback)
109+
instance->maintain_needed_callback();
108110
}
109111

110112
void STM32LoRaWAN::maintain()

src/STM32LoRaWAN.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,27 @@ class STM32LoRaWAN : public Stream {
758758
*/
759759
void maintainUntilIdle();
760760

761+
/**
762+
* Registers a callback that is called whenever there is some work
763+
* to do and the maintain() function must be called soon.
764+
*
765+
* \warning This callback is called from an interrupt handler, so it
766+
* should not do any work and definitely *not* call `maintain()`,
767+
* but just set a flag, enable a task, or something similarly short,
768+
* and make sure to do that in a interrupt-safe manner.
769+
*
770+
* When using this callback, it is not needed to call the
771+
* `maintain()` method unless this callback is called, allowing
772+
* applications to be more efficient, or use sleeping (but care
773+
* should be taken to prevent race conditions).
774+
*
775+
* Only one callback can be active at the same time, so any
776+
* previously configured callback is replaced by the new one passed.
777+
*
778+
* \NotInMKRWAN
779+
*/
780+
void setMaintainNeededCallback(std::function<void(void)> callback);
781+
761782
/// @}
762783

763784

@@ -999,6 +1020,8 @@ class STM32LoRaWAN : public Stream {
9991020
uint32_t fcnt_up = 0;
10001021
uint32_t fcnt_down = 0;
10011022

1023+
std::function<void(void)> maintain_needed_callback;
1024+
10021025
bool mac_process_pending = false;
10031026

10041027
static constexpr uint32_t DEFAULT_JOIN_TIMEOUT = 60000;

0 commit comments

Comments
 (0)