Skip to content

Commit 54d13ba

Browse files
Jordan Yatesnashif
authored andcommitted
lorawan: add callback for datarate changes
Add a callback to notify applications when the datarate has changed. This allows applications to know when payload sizes have changed without needing to call `lorawan_get_payload_sizes` before each transmission. This also enables: * Monitoring of network conditions on the device * Determining if a network connection has been lost Signed-off-by: Jordan Yates <[email protected]>
1 parent bdf13bc commit 54d13ba

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

include/lorawan/lorawan.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ struct lorawan_join_config {
121121
*/
122122
int lorawan_set_battery_level_callback(uint8_t (*battery_lvl_cb)(void));
123123

124+
/**
125+
* @brief Register a callback to be called when the datarate changes
126+
*
127+
* The callback is called once upon successfully joining a network and again
128+
* each time the datarate changes due to ADR.
129+
*
130+
* The callback function takes one parameter:
131+
* - dr - updated datarate
132+
*
133+
* @param dr_cb Pointer to datarate update callback
134+
*/
135+
void lorawan_register_dr_changed_callback(void (*dr_cb)(enum lorawan_datarate));
136+
124137
/**
125138
* @brief Join the LoRaWAN network
126139
*

subsys/lorawan/lorawan.c

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static LoRaMacEventInfoStatus_t last_mcps_indication_status;
7373
static LoRaMacEventInfoStatus_t last_mlme_indication_status;
7474

7575
static uint8_t (*getBatteryLevelUser)(void);
76+
static void (*dr_change_cb)(enum lorawan_datarate dr);
7677

7778
static uint8_t getBatteryLevelLocal(void)
7879
{
@@ -98,6 +99,9 @@ static void datarate_observe(bool force_notification)
9899
if ((mibGet.Param.ChannelsDatarate != current_datarate) ||
99100
(force_notification)) {
100101
current_datarate = mibGet.Param.ChannelsDatarate;
102+
if (dr_change_cb) {
103+
dr_change_cb(current_datarate);
104+
}
101105
LOG_INF("Datarate changed: DR_%d", current_datarate);
102106
}
103107
}
@@ -193,7 +197,7 @@ static void MlmeIndication(MlmeIndication_t *mlmeIndication)
193197
}
194198

195199
static LoRaMacStatus_t lorawan_join_otaa(
196-
const struct lorawan_join_config *join_cfg)
200+
const struct lorawan_join_config *join_cfg)
197201
{
198202
MlmeReq_t mlme_req;
199203
MibRequestConfirm_t mib_req;
@@ -221,7 +225,7 @@ static LoRaMacStatus_t lorawan_join_otaa(
221225
}
222226

223227
static LoRaMacStatus_t lorawan_join_abp(
224-
const struct lorawan_join_config *join_cfg)
228+
const struct lorawan_join_config *join_cfg)
225229
{
226230
MibRequestConfirm_t mib_req;
227231

@@ -307,20 +311,30 @@ int lorawan_join(const struct lorawan_join_config *join_cfg)
307311
}
308312

309313
out:
310-
/*
311-
* Several regions (AS923, AU915, US915) overwrite the datarate as part
312-
* of the join process. Reset the datarate to the value requested
313-
* (and validated) in lorawan_set_datarate so that the MAC layer is
314-
* aware of the set datarate for LoRaMacQueryTxPossible. This is only
315-
* performed when ADR is disabled as it the network servers
316-
* responsibility to increase datarates when ADR is enabled.
317-
*/
318-
if ((ret == 0) && (!lorawan_adr_enable)) {
319-
MibRequestConfirm_t mib_req;
314+
/* If the join succeeded */
315+
if (ret == 0) {
316+
/*
317+
* Several regions (AS923, AU915, US915) overwrite the
318+
* datarate as part of the join process. Reset the datarate
319+
* to the value requested (and validated) in
320+
* lorawan_set_datarate so that the MAC layer is aware of the
321+
* set datarate for LoRaMacQueryTxPossible. This is only
322+
* performed when ADR is disabled as it the network servers
323+
* responsibility to increase datarates when ADR is enabled.
324+
*/
325+
if (!lorawan_adr_enable) {
326+
MibRequestConfirm_t mib_req;
320327

321-
mib_req.Type = MIB_CHANNELS_DATARATE;
322-
mib_req.Param.ChannelsDatarate = default_datarate;
323-
LoRaMacMibSetRequestConfirm(&mib_req);
328+
mib_req.Type = MIB_CHANNELS_DATARATE;
329+
mib_req.Param.ChannelsDatarate = default_datarate;
330+
LoRaMacMibSetRequestConfirm(&mib_req);
331+
}
332+
333+
/*
334+
* Force a notification of the datarate on network join as the
335+
* user may not have explicitly set a datarate to use.
336+
*/
337+
datarate_observe(true);
324338
}
325339

326340
k_mutex_unlock(&lorawan_join_mutex);
@@ -344,7 +358,7 @@ int lorawan_set_class(enum lorawan_class dev_class)
344358
return -ENOTSUP;
345359
default:
346360
return -EINVAL;
347-
};
361+
}
348362

349363
status = LoRaMacMibSetRequestConfirm(&mib_req);
350364
if (status != LORAMAC_STATUS_OK) {
@@ -516,6 +530,11 @@ int lorawan_set_battery_level_callback(uint8_t (*battery_lvl_cb)(void))
516530
return 0;
517531
}
518532

533+
void lorawan_register_dr_changed_callback(void (*cb)(enum lorawan_datarate))
534+
{
535+
dr_change_cb = cb;
536+
}
537+
519538
int lorawan_start(void)
520539
{
521540
LoRaMacStatus_t status;

0 commit comments

Comments
 (0)