From 8329bc2c688224917d33de950bb56fe9cda46591 Mon Sep 17 00:00:00 2001
From: Alexandre Abadie <alexandre.abadie@inria.fr>
Date: Sat, 24 Feb 2018 15:16:12 +0100
Subject: [PATCH] pkg/semtech-loramac: add link check request support

---
 pkg/semtech-loramac/contrib/semtech_loramac.c | 34 +++++++++++++++++++
 pkg/semtech-loramac/include/semtech_loramac.h | 18 ++++++++++
 2 files changed, 52 insertions(+)

diff --git a/pkg/semtech-loramac/contrib/semtech_loramac.c b/pkg/semtech-loramac/contrib/semtech_loramac.c
index 5212b30af7..4e6d0195c2 100644
--- a/pkg/semtech-loramac/contrib/semtech_loramac.c
+++ b/pkg/semtech-loramac/contrib/semtech_loramac.c
@@ -241,6 +241,15 @@ static void mlme_confirm(MlmeConfirm_t *confirm)
             }
             break;
 
+        case MLME_LINK_CHECK:
+            if (confirm->Status == LORAMAC_EVENT_INFO_STATUS_OK) {
+                DEBUG("[semtech-loramac] link check received\n");
+                msg_t msg;
+                msg.type = MSG_TYPE_LORAMAC_LINK_CHECK;
+                msg.content.ptr = confirm;
+                msg_send(&msg, semtech_loramac_pid);
+            }
+
         default:
             break;
     }
@@ -313,6 +322,7 @@ void _init_loramac(semtech_loramac_t *mac,
     semtech_loramac_set_class(mac, LORAMAC_DEFAULT_DEVICE_CLASS);
     semtech_loramac_set_tx_port(mac, LORAMAC_DEFAULT_TX_PORT);
     semtech_loramac_set_tx_mode(mac, LORAMAC_DEFAULT_TX_MODE);
+    mac->link_chk.available = false;
 }
 
 static void _join_otaa(semtech_loramac_t *mac)
@@ -522,6 +532,19 @@ void *_semtech_loramac_event_loop(void *arg)
                     mac->state = SEMTECH_LORAMAC_STATE_IDLE;
                     break;
                 }
+                case MSG_TYPE_LORAMAC_LINK_CHECK:
+                {
+                    MlmeConfirm_t *confirm = (MlmeConfirm_t *)msg.content.ptr;
+                    mac->link_chk.demod_margin = confirm->DemodMargin;
+                    mac->link_chk.nb_gateways = confirm->NbGateways;
+                    mac->link_chk.available = true;
+                    DEBUG("[semtech-loramac] link check info received:\n"
+                          "  - Demodulation marging: %d\n"
+                          "  - Number of gateways: %d\n",
+                          mac->link_chk.demod_margin,
+                          mac->link_chk.nb_gateways);
+                    break;
+                }
                 case MSG_TYPE_LORAMAC_TX_DONE:
                 {
                     DEBUG("[semtech-loramac] loramac TX done\n");
@@ -609,6 +632,16 @@ uint8_t semtech_loramac_join(semtech_loramac_t *mac, uint8_t type)
     return SEMTECH_LORAMAC_JOIN_SUCCEEDED;
 }
 
+void semtech_loramac_request_link_check(semtech_loramac_t *mac)
+{
+    mutex_lock(&mac->lock);
+    mac->link_chk.available = false;
+    MlmeReq_t mlmeReq;
+    mlmeReq.Type = MLME_LINK_CHECK;
+    LoRaMacMlmeRequest(&mlmeReq);
+    mutex_unlock(&mac->lock);
+}
+
 uint8_t semtech_loramac_send(semtech_loramac_t *mac, uint8_t *data, uint8_t len)
 {
     mutex_lock(&mac->lock);
@@ -616,6 +649,7 @@ uint8_t semtech_loramac_send(semtech_loramac_t *mac, uint8_t *data, uint8_t len)
     mibReq.Type = MIB_NETWORK_JOINED;
     LoRaMacMibGetRequestConfirm(&mibReq);
     bool is_joined = mibReq.Param.IsNetworkJoined;
+    mac->link_chk.available = false;
     mutex_unlock(&mac->lock);
 
     if (!is_joined) {
diff --git a/pkg/semtech-loramac/include/semtech_loramac.h b/pkg/semtech-loramac/include/semtech_loramac.h
index 321d626d32..065ddd0a2c 100644
--- a/pkg/semtech-loramac/include/semtech_loramac.h
+++ b/pkg/semtech-loramac/include/semtech_loramac.h
@@ -44,6 +44,7 @@ extern "C" {
 #define MSG_TYPE_LORAMAC_JOIN          (0x3461)  /**< MAC join event */
 #define MSG_TYPE_LORAMAC_TX_DONE       (0x3462)  /**< MAC TX completes */
 #define MSG_TYPE_LORAMAC_RX            (0x3463)  /**< Some data received */
+#define MSG_TYPE_LORAMAC_LINK_CHECK    (0x3464)  /**< Link check info received */
 /** @} */
 
 /**
@@ -89,6 +90,15 @@ typedef struct {
     uint8_t port;                                /**< RX port */
 } semtech_loramac_rx_data_t;
 
+/**
+ * @brief   LoRaMAC link check information
+ */
+typedef struct {
+    uint8_t demod_margin;                        /**< Demodulation margin */
+    uint8_t nb_gateways;                         /**< number of LoRa gateways found */
+    bool available;                              /**< new link check information avalable */
+} semtech_loramac_link_check_info_t;
+
 /**
  * @brief   Semtech LoRaMAC descriptor
  */
@@ -105,6 +115,7 @@ typedef struct {
     uint8_t nwkskey[LORAMAC_NWKSKEY_LEN];        /**< network session key */
     uint8_t devaddr[LORAMAC_DEVADDR_LEN];        /**< device address */
     semtech_loramac_rx_data_t rx_data;           /**< struct handling the RX data */
+    semtech_loramac_link_check_info_t link_chk;  /**< link check information */
 } semtech_loramac_t;
 
 /**
@@ -166,6 +177,13 @@ uint8_t semtech_loramac_send(semtech_loramac_t *mac, uint8_t *data, uint8_t len)
  */
 uint8_t semtech_loramac_recv(semtech_loramac_t *mac);
 
+/**
+ * @brief   Requests a LoRaWAN link check
+ *
+ * @param[in] mac          Pointer to the mac
+ */
+void semtech_loramac_request_link_check(semtech_loramac_t *mac);
+
 /**
  * @brief   Sets the device EUI
  *
-- 
GitLab