Skip to content

Commit c72b9f5

Browse files
martinjaegercarlescufi
authored andcommitted
lorawan: use callback function signature typedefs
This avoids duplication of the function signature in several places and makes the API documentation more clean. Signed-off-by: Martin Jäger <[email protected]>
1 parent 8b7c8b0 commit c72b9f5

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

include/zephyr/lorawan/lorawan.h

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,32 @@ struct lorawan_downlink_cb {
175175
};
176176

177177
/**
178-
* @brief Add battery level callback function.
178+
* @brief Defines the battery level callback handler function signature.
179+
*
180+
* @retval 0 if the node is connected to an external power source
181+
* @retval 1..254 battery level, where 1 is the minimum and 254 is the maximum value
182+
* @retval 255 if the node was not able to measure the battery level
183+
*/
184+
typedef uint8_t (*lorawan_battery_level_cb_t)(void);
185+
186+
/**
187+
* @brief Defines the datarate changed callback handler function signature.
188+
*
189+
* @param dr Updated datarate.
190+
*/
191+
typedef void (*lorawan_dr_changed_cb_t)(enum lorawan_datarate dr);
192+
193+
/**
194+
* @brief Register a battery level callback function.
179195
*
180196
* Provide the LoRaWAN stack with a function to be called whenever a battery
181-
* level needs to be read. As per LoRaWAN specification the callback needs to
182-
* return "0: node is connected to an external power source,
183-
* 1..254: battery level, where 1 is the minimum and 254 is the maximum
184-
* value,
185-
* 255: the node was not able to measure the battery level"
197+
* level needs to be read.
186198
*
187199
* Should no callback be provided the lorawan backend will report 255.
188200
*
189-
* @param battery_lvl_cb Pointer to the battery level function
201+
* @param cb Pointer to the battery level function
190202
*/
191-
void lorawan_register_battery_level_callback(uint8_t (*battery_lvl_cb)(void));
203+
void lorawan_register_battery_level_callback(lorawan_battery_level_cb_t cb);
192204

193205
/**
194206
* @brief Register a callback to be run on downlink packets
@@ -203,12 +215,9 @@ void lorawan_register_downlink_callback(struct lorawan_downlink_cb *cb);
203215
* The callback is called once upon successfully joining a network and again
204216
* each time the datarate changes due to ADR.
205217
*
206-
* The callback function takes one parameter:
207-
* - dr - updated datarate
208-
*
209-
* @param dr_cb Pointer to datarate update callback
218+
* @param cb Pointer to datarate update callback
210219
*/
211-
void lorawan_register_dr_changed_callback(void (*dr_cb)(enum lorawan_datarate));
220+
void lorawan_register_dr_changed_callback(lorawan_dr_changed_cb_t cb);
212221

213222
/**
214223
* @brief Join the LoRaWAN network

subsys/lorawan/lorawan.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ static LoRaMacEventInfoStatus_t last_mlme_indication_status;
7272

7373
static LoRaMacRegion_t selected_region = DEFAULT_LORAWAN_REGION;
7474

75-
static uint8_t (*get_battery_level_user)(void);
76-
static void (*dr_change_cb)(enum lorawan_datarate dr);
75+
static lorawan_battery_level_cb_t battery_level_cb;
76+
static lorawan_dr_changed_cb_t dr_changed_cb;
7777

7878
/* implementation required by the soft-se (software secure element) */
7979
void BoardGetUniqueId(uint8_t *id)
@@ -83,11 +83,11 @@ void BoardGetUniqueId(uint8_t *id)
8383

8484
static uint8_t get_battery_level(void)
8585
{
86-
if (get_battery_level_user != NULL) {
87-
return get_battery_level_user();
86+
if (battery_level_cb != NULL) {
87+
return battery_level_cb();
88+
} else {
89+
return 255;
8890
}
89-
90-
return 255;
9191
}
9292

9393
static void mac_process_notify(void)
@@ -105,8 +105,8 @@ static void datarate_observe(bool force_notification)
105105
if ((mib_req.Param.ChannelsDatarate != current_datarate) ||
106106
(force_notification)) {
107107
current_datarate = mib_req.Param.ChannelsDatarate;
108-
if (dr_change_cb) {
109-
dr_change_cb(current_datarate);
108+
if (dr_changed_cb != NULL) {
109+
dr_changed_cb(current_datarate);
110110
}
111111
LOG_INF("Datarate changed: DR_%d", current_datarate);
112112
}
@@ -621,19 +621,19 @@ int lorawan_send(uint8_t port, uint8_t *data, uint8_t len,
621621
return ret;
622622
}
623623

624-
void lorawan_register_battery_level_callback(uint8_t (*battery_lvl_cb)(void))
624+
void lorawan_register_battery_level_callback(lorawan_battery_level_cb_t cb)
625625
{
626-
get_battery_level_user = battery_lvl_cb;
626+
battery_level_cb = cb;
627627
}
628628

629629
void lorawan_register_downlink_callback(struct lorawan_downlink_cb *cb)
630630
{
631631
sys_slist_append(&dl_callbacks, &cb->node);
632632
}
633633

634-
void lorawan_register_dr_changed_callback(void (*cb)(enum lorawan_datarate))
634+
void lorawan_register_dr_changed_callback(lorawan_dr_changed_cb_t cb)
635635
{
636-
dr_change_cb = cb;
636+
dr_changed_cb = cb;
637637
}
638638

639639
int lorawan_start(void)

0 commit comments

Comments
 (0)