Skip to content

Commit 6db9a8b

Browse files
author
Cruz Monrreal
authored
Merge pull request #6875 from kivaisan/fix_batterylevel_callback_v2
Lora: Fix battery_level callback
2 parents 9923899 + 9915478 commit 6db9a8b

File tree

6 files changed

+39
-5
lines changed

6 files changed

+39
-5
lines changed

features/lorawan/LoRaWANStack.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ lorawan_status_t LoRaWANStack::set_lora_callbacks(const lorawan_app_callbacks_t
134134

135135
if (callbacks->battery_level) {
136136
_callbacks.battery_level = callbacks->battery_level;
137+
_loramac.set_batterylevel_callback(callbacks->battery_level);
137138
}
138139

139140
return LORAWAN_STATUS_OK;

features/lorawan/lorastack/mac/LoRaMac.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,11 @@ void LoRaMac::handle_data_frame(const uint8_t* const payload,
661661
_params.ack_timeout_retry_counter, _params.max_ack_timeout_retries );
662662
}
663663

664+
void LoRaMac::set_batterylevel_callback(mbed::Callback<uint8_t(void)> battery_level)
665+
{
666+
_mac_commands.set_batterylevel_callback(battery_level);
667+
}
668+
664669
void LoRaMac::on_radio_rx_done(const uint8_t* const payload, uint16_t size,
665670
int16_t rssi, int8_t snr)
666671
{

features/lorawan/lorastack/mac/LoRaMac.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,11 @@ class LoRaMac {
437437
void post_process_mlme_request(void);
438438
void post_process_mlme_ind(void);
439439

440+
/**
441+
* Set battery level query callback
442+
*/
443+
void set_batterylevel_callback(mbed::Callback<uint8_t(void)> battery_level);
444+
440445
/**
441446
* These locks trample through to the upper layers and make
442447
* the stack thread safe.

features/lorawan/lorastack/mac/LoRaMacCommand.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,11 @@ lorawan_status_t LoRaMacCommand::process_mac_commands(const uint8_t *payload, ui
233233
}
234234
break;
235235
case SRV_MAC_DEV_STATUS_REQ: {
236-
uint8_t batteryLevel = BAT_LEVEL_NO_MEASURE;
237-
// we don't have a mechanism at the moment to measure
238-
// battery levels
239-
ret_value = add_dev_status_ans(batteryLevel, snr & 0x3F);
236+
uint8_t battery_level = BAT_LEVEL_NO_MEASURE;
237+
if (_battery_level_cb) {
238+
battery_level = _battery_level_cb();
239+
}
240+
ret_value = add_dev_status_ans(battery_level, snr & 0x3F);
240241
break;
241242
}
242243
case SRV_MAC_NEW_CHANNEL_REQ: {
@@ -329,6 +330,11 @@ int32_t LoRaMacCommand::cmd_buffer_remaining() const
329330
return sizeof(mac_cmd_buffer) - mac_cmd_buf_idx_to_repeat - mac_cmd_buf_idx;
330331
}
331332

333+
void LoRaMacCommand::set_batterylevel_callback(mbed::Callback<uint8_t(void)> battery_level)
334+
{
335+
_battery_level_cb = battery_level;
336+
}
337+
332338
lorawan_status_t LoRaMacCommand::add_link_check_req()
333339
{
334340
lorawan_status_t ret = LORAWAN_STATUS_LENGTH_ERROR;

features/lorawan/lorastack/mac/LoRaMacCommand.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ class LoRaMacCommand {
147147
*/
148148
lorawan_status_t add_link_check_req();
149149

150+
/**
151+
* @brief Set battery level query callback method
152+
* If callback is not set, BAT_LEVEL_NO_MEASURE is returned.
153+
*/
154+
void set_batterylevel_callback(mbed::Callback<uint8_t(void)> battery_level);
155+
150156
private:
151157
/**
152158
* @brief Get the remaining size of the MAC command buffer
@@ -261,6 +267,8 @@ class LoRaMacCommand {
261267
* Buffer containing the MAC layer commands which must be repeated
262268
*/
263269
uint8_t mac_cmd_buffer_to_repeat[LORA_MAC_COMMAND_MAX_LENGTH];
270+
271+
mbed::Callback<uint8_t(void)> _battery_level_cb;
264272
};
265273

266274
#endif //__LORAMACCOMMAND_H__

features/lorawan/lorawan_types.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,17 @@ typedef struct {
274274
* Optional
275275
*/
276276
mbed::Callback<void(uint8_t, uint8_t)> link_check_resp;
277+
278+
/**
279+
* Battery level return value must follow the specification
280+
* for DevStatusAns MAC command:
281+
*
282+
* 0 The end-device is connected to an external power source
283+
* 1 - 254 The battery level, 1 being at minimum and 254 being at maximum
284+
* 255 The end-device was not able to measure the battery level.
285+
*/
277286
mbed::Callback<uint8_t(void)> battery_level;
278-
} lorawan_app_callbacks_t;
287+
} lorawan_app_callbacks_t;
279288

280289
/**
281290
* DO NOT MODIFY, WILL BREAK THE API!

0 commit comments

Comments
 (0)