Skip to content

Commit 3197be9

Browse files
Martdurkartben
authored andcommitted
lorawan: add link check support.
Add link check support, by adding: - link check callback in response of LinkCheckAns. - link check request function using LinkCheckReq. Signed-off-by: Martin Durietz <[email protected]>
1 parent 7f7965e commit 3197be9

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

doc/releases/release-notes-4.2.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ New APIs and options
172172
* :c:func:`util_eq`
173173
* :c:func:`util_memeq`
174174

175+
* LoRaWAN
176+
* :c:func:`lorawan_request_link_check`
177+
175178
New Boards
176179
**********
177180

include/zephyr/lorawan/lorawan.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@ typedef uint8_t (*lorawan_battery_level_cb_t)(void);
217217
*/
218218
typedef void (*lorawan_dr_changed_cb_t)(enum lorawan_datarate dr);
219219

220+
/**
221+
* @brief Defines the link check answer handler function signature.
222+
*
223+
* @param demod_margin The demodulation margin in db.
224+
* @param nb_gateways The number of gateways the device is connected to.
225+
*/
226+
typedef void (*lorawan_link_check_ans_cb_t)(uint8_t demod_margin, uint8_t nb_gateways);
227+
220228
/**
221229
* @brief Defines the user's descriptor callback handler function signature.
222230
*
@@ -267,6 +275,14 @@ void lorawan_register_downlink_callback(struct lorawan_downlink_cb *cb);
267275
*/
268276
void lorawan_register_dr_changed_callback(lorawan_dr_changed_cb_t cb);
269277

278+
/**
279+
* @brief Register a callback to be called when getting answer
280+
* a to check link request.
281+
*
282+
* @param cb Pointer to link check ans callback
283+
*/
284+
void lorawan_register_link_check_ans_callback(lorawan_link_check_ans_cb_t cb);
285+
270286
/**
271287
* @brief Join the LoRaWAN network
272288
*
@@ -426,6 +442,18 @@ int lorawan_request_device_time(bool force_request);
426442
*/
427443
int lorawan_device_time_get(uint32_t *gps_time);
428444

445+
/**
446+
* @brief Request for link check according to LinkCheckReq MAC cmd
447+
*
448+
* Append MAC LinkCheckReq command. It will be processed on next send
449+
* message or force sending empty message to request time immediately.
450+
*
451+
* @param force_request Immediately send an empty message to execute the request
452+
*
453+
* @return 0 if successful, negative errno otherwise
454+
*/
455+
int lorawan_request_link_check(bool force_request);
456+
429457
#ifdef CONFIG_LORAWAN_APP_CLOCK_SYNC
430458

431459
/**

subsys/lorawan/lorawan.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static enum lorawan_channels_mask_size region_channels_mask_size =
9393

9494
static lorawan_battery_level_cb_t battery_level_cb;
9595
static lorawan_dr_changed_cb_t dr_changed_cb;
96+
static lorawan_link_check_ans_cb_t link_check_cb;
9697

9798
/* implementation required by the soft-se (software secure element) */
9899
void BoardGetUniqueId(uint8_t *id)
@@ -213,8 +214,10 @@ static void mlme_confirm_handler(MlmeConfirm_t *mlme_confirm)
213214
LOG_INF("Joined network! DevAddr: %08x", mib_req.Param.DevAddr);
214215
break;
215216
case MLME_LINK_CHECK:
216-
/* Not implemented */
217-
LOG_INF("Link check not implemented yet!");
217+
if (link_check_cb != NULL) {
218+
link_check_cb(mlme_confirm->DemodMargin, mlme_confirm->NbGateways);
219+
}
220+
LOG_INF("Link check done");
218221
break;
219222
case MLME_DEVICE_TIME:
220223
LOG_INF("DevTimeReq done");
@@ -399,6 +402,27 @@ int lorawan_set_region(enum lorawan_region region)
399402
return 0;
400403
}
401404

405+
int lorawan_request_link_check(bool force_request)
406+
{
407+
int ret = 0;
408+
LoRaMacStatus_t status;
409+
MlmeReq_t mlme_req;
410+
411+
mlme_req.Type = MLME_LINK_CHECK;
412+
status = LoRaMacMlmeRequest(&mlme_req);
413+
if (status != LORAMAC_STATUS_OK) {
414+
LOG_ERR("LinkCheckReq failed: %s", lorawan_status2str(status));
415+
ret = lorawan_status2errno(status);
416+
return ret;
417+
}
418+
419+
if (force_request) {
420+
ret = lorawan_send(0U, "", 0U, LORAWAN_MSG_UNCONFIRMED);
421+
}
422+
423+
return ret;
424+
}
425+
402426
int lorawan_request_device_time(bool force_request)
403427
{
404428
int ret = 0;
@@ -732,6 +756,11 @@ void lorawan_register_dr_changed_callback(lorawan_dr_changed_cb_t cb)
732756
dr_changed_cb = cb;
733757
}
734758

759+
void lorawan_register_link_check_ans_callback(lorawan_link_check_ans_cb_t cb)
760+
{
761+
link_check_cb = cb;
762+
}
763+
735764
int lorawan_start(void)
736765
{
737766
LoRaMacStatus_t status;

0 commit comments

Comments
 (0)