Trond Enger / d7a_1x

Fork of d7a_1x by WizziLab

Revision:
46:665391110051
Parent:
45:b85384e7d825
Child:
47:19f22e1a448a
--- a/src/d7a_alp.cpp	Thu Sep 01 09:35:27 2016 +0000
+++ b/src/d7a_alp.cpp	Thu Sep 01 18:13:46 2016 +0000
@@ -138,7 +138,12 @@
 }
 
 d7a_alp_rsp_t* d7a_alp_parse_pl(d7a_com_rx_msg_t* pkt)
-{
+{    
+    if (pkt == NULL)
+    {
+        return NULL;
+    }
+    
     uint8_t* p = pkt->buffer;
     uint8_t* t = p;
     uint8_t len = pkt->blen;
@@ -149,11 +154,6 @@
     rsp->length = 0;
     rsp->data = NULL;
     
-    if (pkt == NULL)
-    {
-        return rsp;
-    }
-    
     while ((p - t) < len)
     {
         
@@ -161,15 +161,26 @@
         switch (ctrl & 0x3F)
         {
             case ALP_OPCODE_RSP_STATUS:
-                rsp->status[rsp->nb_status].aid = *p++; // Action ID
-                rsp->status[rsp->nb_status].status = *p++; // Status
-                DPRINT("ALP RSP STATUS[%d] aid:%d Status:%d\r\n", rsp->nb_status, rsp->status[rsp->nb_status].aid, rsp->status[rsp->nb_status].status);
-                rsp->nb_status++;
+                if (ctrl & ALP_OPCODE_INDIRECT)
+                {
+                    uint8_t type = *p++; // ITF Type
+                    uint32_t length;
+                    p += alp_decode_length(p, &length); // Length
+                    p += length; // Data
+                    PRINT("ALP RSP ISTATUS %02X\r\n", type);
+                }
+                else
+                {
+                    rsp->status[rsp->nb_status].aid = *p++; // Action ID
+                    rsp->status[rsp->nb_status].status = *p++; // Status
+                    PRINT("ALP RSP STATUS[%d] aid:%d Status:%d\r\n", rsp->nb_status, rsp->status[rsp->nb_status].aid, rsp->status[rsp->nb_status].status);
+                    rsp->nb_status++;
+                }
                 break;
             case ALP_OPCODE_RSP_TAG:
                 rsp->tag_status = (ctrl & ~0x3F) | 0x01;
                 rsp->tag = *p++; // TAG
-                DPRINT("ALP RSP TAG %d %02x\r\n", rsp->tag, rsp->tag_status);
+                PRINT("ALP RSP TAG %d %02x\r\n", rsp->tag, rsp->tag_status);
                 break;
             case ALP_OPCODE_RSP_F_DATA:
                 uint8_t fid = *p++; // File ID
@@ -178,7 +189,7 @@
                 p += alp_decode_length(p, &(rsp->length)); // length
                 rsp->data = (uint8_t*)MALLOC(rsp->length);
                 p += d7a_alp_add(rsp->data, p, rsp->length);
-                DPRINT("ALP RSP F_DATA f:%d o:%d s:%d\r\n", fid, offset, rsp->length);
+                PRINT("ALP RSP F_DATA f:%d o:%d s:%d\r\n", fid, offset, rsp->length);
                 //dbg_print_data("DATA: ", "%02X ", (uint8_t*)rsp->data, rsp->length, "\r\n");
                 break;
             default:
@@ -205,16 +216,14 @@
     return (uint32_t)(p - t);
 }
 
-uint32_t d7a_alp_forward(uint8_t* p, d7a_addressee_t* addressee, uint8_t retry, bool ack, bool resp)
+
+
+uint32_t d7a_alp_forward_action(uint8_t* p, alp_d7a_itf_t* itf, bool resp)
 {
     uint8_t* t = p;
     
     *p++ = ALP_OPCODE_FORWARD + ((resp)? ALP_CTRL_RESP : 0);
-    *p++ = (retry << 3) | ((ack)? 2 : 0);
-    *p++ = 0; // dorm_to
-    *p++ = addressee->ctrl.bf.nls | (D7A_ID_UID<<4);
-    *p++ = addressee->xcl.byte;
-    p += d7a_alp_add(p, addressee->id, D7A_UID_LEN);
+    p += d7a_alp_add(p, (uint8_t*)itf, sizeof(alp_d7a_itf_t));
     
     return (uint32_t)(p - t);
 }
@@ -254,7 +263,7 @@
 }
 
 
-int8_t d7a_alp_write_file(const uint8_t file_id, const uint32_t offset, const uint32_t size, const uint8_t* const buf, d7a_addressee_t* addressee, uint8_t retry, bool resp)
+d7a_errors_t d7a_alp_write_file(const uint8_t file_id, const uint32_t offset, const uint32_t size, const uint8_t* const buf, d7a_addressee_t* addressee, uint8_t retry, bool resp)
 {
     uint8_t* p = &g_alp_ctx.buffer[0];
     uint8_t* t = p;
@@ -264,8 +273,20 @@
         
     if (addressee)
     {
+        // Construct interface
+        alp_d7a_itf_t itf = {
+            .type = 0xD7, // Dash7 interface
+            .cfg.qos.bf.resp = D7A_RESP_ANY,
+            .cfg.qos.bf.retry = retry,
+            .cfg.qos.bf.record = 0,
+            .cfg.qos.bf.stop_on_err = 0,
+            .cfg.dorm_to.byte = 0,
+        };
+        memcpy(&itf.cfg.addressee, addressee, sizeof(d7a_addressee_t));
+        itf.cfg.addressee.ctrl.bf.idf = D7A_ID_UID;
+            
         // Forward
-        p += d7a_alp_forward(p, addressee, retry, true, true);
+        p += d7a_alp_forward_action(p, &itf, true);
     }
     
     // Write action
@@ -274,76 +295,59 @@
     d7a_com_dump(&g_alp_ctx.buffer[0], (uint8_t)(p - t), KAL_COM_FLOW_AT_CMD);
     
     d7a_alp_rsp_t* pl;
-    int8_t status;
+    d7a_errors_t status;
     
-    if (addressee)
+    do
     {
-        pl = d7a_alp_parse_pl(d7a_alp_wait_pl(3000));
-        if (pl->nb_status == 1
-         && pl->status[0].status == 0
-         && pl->tag_status == 0x01
-         && pl->tag == g_alp_ctx.tag)
+        pl = d7a_alp_parse_pl(d7a_alp_wait_pl(60000));
+        
+        if (pl == NULL)
         {
-            // OK
-        }
-        else
-        {
-            status = pl->status[0].status;
+            status = D7A_ERR_CMD_TO;
+            free_pl(pl);
+            break;
         }
         
-        free_pl(pl);
+        ASSERT(pl->tag_status & 0x01, "No tag in payload expected %d\r\n", g_alp_ctx.tag);
+        ASSERT(pl->tag == g_alp_ctx.tag, "Wrong tag %d expected %d\r\n", pl->tag, g_alp_ctx.tag);
         
-        if (status)
+        if (pl->tag_status & ALP_CTRL_ERR)
         {
-            return status;
-        }
-    }
-    
-    pl = d7a_alp_parse_pl(d7a_alp_wait_pl(3000));
-    if (resp)
-    {
-        if (pl->nb_status == 1
-         && pl->status[0].status == 0
-         && pl->tag_status == 0x01 | ALP_CTRL_EOP
-         && pl->tag == g_alp_ctx.tag
-         )
-        {
-            // OK
-        }
-        else
-        {
-            
             if (pl->nb_status)
             {
-                status = pl->status[0].status;
+                status = (d7a_errors_t)pl->status[0].status;
             }
             else
             {
                 status = D7A_ERR_TX_FAILED;
             }
         }
-    }
-    else
-    {
-        if (pl->nb_status == 0
-         && pl->tag_status == 0x01 | ALP_CTRL_EOP
-         && pl->tag == g_alp_ctx.tag
-         )
+    
+        if (pl->tag_status & ALP_CTRL_EOP)
         {
-            // OK
+            if (!addressee)
+            {
+                if (pl->nb_status)
+                {
+                    status = (d7a_errors_t)pl->status[0].status;
+                }
+                else
+                {
+                    status = D7A_ERR_TX_FAILED;
+                }
+            }
+
+            free_pl(pl);
+            break;
         }
-        else
-        {
-            status = D7A_ERR_TX_FAILED;
-        }
-    }
-    
-    free_pl(pl);
+        
+        free_pl(pl);
+    } while (1);
     
     return status;
 }
 
-int8_t d7a_alp_read_file(const uint8_t file_id, const uint32_t offset, const uint32_t size, const uint8_t* const buf, d7a_addressee_t* addressee, uint8_t retry)
+d7a_errors_t d7a_alp_read_file(const uint8_t file_id, const uint32_t offset, const uint32_t size, const uint8_t* const buf, d7a_addressee_t* addressee, uint8_t retry)
 {
     uint8_t* p = &g_alp_ctx.buffer[0];
     uint8_t* t = p;
@@ -353,8 +357,20 @@
     
     if (addressee)
     {
+        // Construct interface
+        alp_d7a_itf_t itf = {
+            .type = 0xD7, // Dash7 interface
+            .cfg.qos.bf.resp = D7A_RESP_ANY,
+            .cfg.qos.bf.retry = retry,
+            .cfg.qos.bf.record = 0,
+            .cfg.qos.bf.stop_on_err = 0,
+            .cfg.dorm_to.byte = 0,
+        };
+        memcpy(&itf.cfg.addressee, addressee, sizeof(d7a_addressee_t));
+        itf.cfg.addressee.ctrl.bf.idf = D7A_ID_UID;
+            
         // Forward
-        p += d7a_alp_forward(p, addressee, retry, true, true);
+        p += d7a_alp_forward_action(p, &itf, true);
     }
     
     // Read action
@@ -363,55 +379,47 @@
     d7a_com_dump(&g_alp_ctx.buffer[0], (uint8_t)(p - t), KAL_COM_FLOW_AT_CMD);
     
     d7a_alp_rsp_t* pl;
-    int8_t status;
+    d7a_errors_t status;
     
-    if (addressee)
+    do
     {
-        pl = d7a_alp_parse_pl(d7a_alp_wait_pl(3000));
-        if (pl->nb_status == 1
-         && pl->status[0].status == 0
-         && pl->tag_status == 0x01
-         && pl->tag == g_alp_ctx.tag)
+        pl = d7a_alp_parse_pl(d7a_alp_wait_pl(60000));
+        
+        if (pl == NULL)
+        {
+            status = D7A_ERR_CMD_TO;
+            free_pl(pl);
+            break;
+        }
+        
+        ASSERT(pl->tag_status & 0x01, "No tag in payload expected %d\r\n", g_alp_ctx.tag);
+        ASSERT(pl->tag == g_alp_ctx.tag, "Wrong tag %d expected %d\r\n", pl->tag, g_alp_ctx.tag);
+        
+        if (pl->tag_status & ALP_CTRL_ERR)
         {
-            // OK
+            if (pl->nb_status)
+            {
+                status = (d7a_errors_t)pl->status[0].status;
+            }
+            else
+            {
+                status = D7A_ERR_TX_FAILED;
+            }
         }
-        else
+        
+        if (pl->data != NULL)
         {
-            status = pl->status[0].status;
+            memcpy((void*)buf, (void*)pl->data, pl->length);
+        }
+    
+        if (pl->tag_status & ALP_CTRL_EOP)
+        {
+            free_pl(pl);
+            break;
         }
         
         free_pl(pl);
-        
-        if (status)
-        {
-            return status;
-        }
-    }
-    
-    pl = d7a_alp_parse_pl(d7a_alp_wait_pl(3000));
-
-    if (pl->nb_status == 0
-     && pl->data != NULL
-     && pl->tag_status == 0x01 | ALP_CTRL_EOP
-     && pl->tag == g_alp_ctx.tag
-     )
-    {
-        // OK
-        memcpy((void*)buf, (void*)pl->data, pl->length);
-    }
-    else
-    {
-        if (pl->nb_status)
-        {
-            status = pl->status[0].status;
-        }
-        else
-        {
-            status = D7A_ERR_TX_FAILED;
-        }
-    }
-    
-    free_pl(pl);
+    } while (1);
     
     return status;
 }
@@ -429,7 +437,7 @@
         switch(pkt->id)
         {
             case KAL_COM_FLOW_AT_RESP:
-                DPRINT("KAL_COM_FLOW_AT_RESP\r\n");
+                PRINT("KAL_COM_FLOW_AT_RESP\r\n");
 
                 d7a_alp_new_pl(pkt);