From 87090072cfb0e2f3a7373c7dcbb7ede0c353c77e Mon Sep 17 00:00:00 2001
From: Alexandre Abadie <alexandre.abadie@inria.fr>
Date: Fri, 6 Apr 2018 15:24:15 +0200
Subject: [PATCH] pkg/semtech-loramac: handle all mcps confirm cases

---
 pkg/semtech-loramac/contrib/semtech_loramac.c | 29 ++++++++++++++++---
 pkg/semtech-loramac/include/semtech_loramac.h |  2 ++
 2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/pkg/semtech-loramac/contrib/semtech_loramac.c b/pkg/semtech-loramac/contrib/semtech_loramac.c
index 4e6d0195c2..0114d65576 100644
--- a/pkg/semtech-loramac/contrib/semtech_loramac.c
+++ b/pkg/semtech-loramac/contrib/semtech_loramac.c
@@ -171,6 +171,11 @@ static void mcps_confirm(McpsConfirm_t *confirm)
                 break;
         }
     }
+    else {
+        msg_t msg;
+        msg.type = MSG_TYPE_LORAMAC_TX_CNF_FAILED;
+        msg_send(&msg, semtech_loramac_pid);
+    }
 }
 
 /* MCPS-Indication event function */
@@ -555,6 +560,14 @@ void *_semtech_loramac_event_loop(void *arg)
                     mac->state = SEMTECH_LORAMAC_STATE_IDLE;
                     break;
                 }
+                case MSG_TYPE_LORAMAC_TX_CNF_FAILED:
+                    DEBUG("[semtech-loramac] loramac TX failed\n");
+                    msg_t msg_ret;
+                    msg_ret.type = MSG_TYPE_LORAMAC_TX_CNF_FAILED;
+                    msg_send(&msg_ret, mac->caller_pid);
+                    /* switch back to idle state now*/
+                    mac->state = SEMTECH_LORAMAC_STATE_IDLE;
+                    break;
                 case MSG_TYPE_LORAMAC_RX:
                 {
                     msg_t msg_ret;
@@ -678,10 +691,18 @@ uint8_t semtech_loramac_recv(semtech_loramac_t *mac)
     /* Wait until the mac receive some information */
     msg_t msg;
     msg_receive(&msg);
-    uint8_t ret = SEMTECH_LORAMAC_TX_DONE;
-    if (msg.type == MSG_TYPE_LORAMAC_RX) {
-        ret = SEMTECH_LORAMAC_DATA_RECEIVED;
-    }
+    uint8_t ret;
+    switch (msg.type) {
+        case MSG_TYPE_LORAMAC_RX:
+            ret = SEMTECH_LORAMAC_DATA_RECEIVED;
+            break;
+        case MSG_TYPE_LORAMAC_TX_CNF_FAILED:
+            ret = SEMTECH_LORAMAC_TX_CNF_FAILED;
+            break;
+        default:
+            ret = SEMTECH_LORAMAC_TX_DONE;
+            break;
+     }
 
     DEBUG("[semtech-loramac] MAC reply received: %d\n", ret);
 
diff --git a/pkg/semtech-loramac/include/semtech_loramac.h b/pkg/semtech-loramac/include/semtech_loramac.h
index 065ddd0a2c..c0e07750cf 100644
--- a/pkg/semtech-loramac/include/semtech_loramac.h
+++ b/pkg/semtech-loramac/include/semtech_loramac.h
@@ -45,6 +45,7 @@ extern "C" {
 #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 */
+#define MSG_TYPE_LORAMAC_TX_CNF_FAILED (0x3465)  /**< MAC TX confirmed failed */
 /** @} */
 
 /**
@@ -61,6 +62,7 @@ enum {
     SEMTECH_LORAMAC_NOT_JOINED,                  /**< MAC is not joined */
     SEMTECH_LORAMAC_TX_SCHEDULED,                /**< TX data scheduled */
     SEMTECH_LORAMAC_TX_DONE,                     /**< Transmission completed */
+    SEMTECH_LORAMAC_TX_CNF_FAILED,               /**< Confirmable transmission failed */
     SEMTECH_LORAMAC_DATA_RECEIVED,               /**< Data received */
     SEMTECH_LORAMAC_BUSY                         /**< Internal MAC is busy */
 };
-- 
GitLab