@@ -73,6 +73,7 @@ static LoRaMacEventInfoStatus_t last_mcps_indication_status;
7373static LoRaMacEventInfoStatus_t last_mlme_indication_status ;
7474
7575static uint8_t (* getBatteryLevelUser )(void );
76+ static void (* dr_change_cb )(enum lorawan_datarate dr );
7677
7778static 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
195199static 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
223227static 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
309313out :
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+
519538int lorawan_start (void )
520539{
521540 LoRaMacStatus_t status ;
0 commit comments