Trond Enger / d7a_1x

Fork of d7a_1x by WizziLab

Revision:
76:fda2e34ff19d
Parent:
74:30180585b09e
Child:
77:8c792719a1fc
--- a/src/d7a_alp.cpp	Fri Dec 16 16:04:27 2016 +0000
+++ b/src/d7a_alp.cpp	Tue Dec 20 09:30:14 2016 +0000
@@ -22,14 +22,17 @@
 
 static d7a_alp_ctx_t g_alp_ctx;
 
-void d7a_alp_thread(const void *p);
+void d7a_alp_thread();
 
 d7a_errors_t d7a_alp_open(UnsolicitedMsgFunction uns_msg)
 {
     FPRINT("\r\n");
 
     g_alp_ctx.uns_msg = uns_msg;
-    g_alp_ctx.thread = new Thread(d7a_alp_thread, NULL, osPriorityHigh, DEFAULT_STACK_SIZE*8);
+    g_alp_ctx.thread = new Thread(osPriorityHigh, DEFAULT_STACK_SIZE*4, NULL);
+    
+    osStatus err = g_alp_ctx.thread->start(d7a_alp_thread);
+    ASSERT(err == osOK, "Failed to start d7a_alp_thread (err: %d)\r\n", err);
     
     return D7A_ERR_NONE;
 }
@@ -436,20 +439,10 @@
     } while (1);
 }
 
-d7a_msg_t** d7a_alp_write_file(const uint8_t file_id, const uint32_t offset, const uint32_t size, const uint8_t* const buf, const uint8_t* root_key, d7a_addressee_t* addressee, uint8_t retry, bool resp)
+uint32_t d7a_alp_construct_itf(uint8_t* p, const uint8_t* root_key, d7a_addressee_t* addressee, uint8_t retry, bool resp)
 {
-    FPRINT("\r\n");
-    
-    // Get command buffer
-    uint8_t* p = &g_alp_ctx.buffer[0];
-    // Save initial position of the command buffer
-    uint8_t* t = p;
-    
     bool broadcast = false;
-    uint8_t current_tag;
-    d7a_msg_t** ret = NULL;
     uint32_t max_responses = 2;
-    uint8_t i;
     
     if (addressee)
     {
@@ -465,25 +458,7 @@
             broadcast = true;
             max_responses = 33;
         }
-    }
-    
-    // malloc and init pointer array
-    ret = (d7a_msg_t**)MALLOC(sizeof(d7a_msg_t*) * (max_responses + 1));
-    for (i = 0; i < (max_responses + 1); i++)
-    {
-        ret[i] = NULL;
-    }
-    
-    // Construct command
-    
-    // Tag action
-    p += d7a_alp_tag(p, true);
-    
-    // get tag
-    current_tag = g_alp_ctx.tag;
-    
-    if (addressee)
-    {
+
         // Construct interface
          d7a_itf_t itf = {
             // Dash7 interface
@@ -493,7 +468,7 @@
             .cfg.qos.bf.retry = retry,
             .cfg.qos.bf.record = 0,
             .cfg.qos.bf.stop_on_err = 0,
-            .cfg.dorm_to.byte = 0,
+            .cfg.to.byte = 0,
             .cfg.te.byte = 0,
         };
         memcpy(&itf.cfg.addressee, addressee, sizeof(d7a_addressee_t));
@@ -502,6 +477,45 @@
         p += d7a_alp_forward_action(p, &itf, true);
     }
     
+    return max_responses;
+}
+
+d7a_msg_t** d7a_alp_init_ret(uint32_t max_responses)
+{
+    d7a_msg_t** ret = (d7a_msg_t**)MALLOC(sizeof(d7a_msg_t*) * (max_responses + 1));
+    for (uint32_t i = 0; i < (max_responses + 1); i++)
+    {
+        ret[i] = NULL;
+    }
+    
+    return ret;
+}
+
+d7a_msg_t** d7a_alp_write_file(const uint8_t file_id, const uint32_t offset, const uint32_t size, const uint8_t* const buf, const uint8_t* root_key, d7a_addressee_t* addressee, uint8_t retry, bool resp)
+{
+    FPRINT("\r\n");
+    
+    // Get command buffer
+    uint8_t* p = &g_alp_ctx.buffer[0];
+    // Save initial position of the command buffer
+    uint8_t* t = p;
+    
+    uint8_t current_tag;
+    d7a_msg_t** ret = NULL;
+    uint32_t max_responses;
+    
+    max_responses = d7a_alp_construct_itf(p, root_key, addressee, retry, resp);
+    
+    // malloc and init pointer array
+    ret = d7a_alp_init_ret(max_responses);
+    
+    
+    // Tag action
+    p += d7a_alp_tag(p, true);
+    
+    // get tag
+    current_tag = g_alp_ctx.tag;
+       
     // Ask for root permissions
     if (root_key)
     {
@@ -509,7 +523,7 @@
         uint8_t req_size = d7a_alp_write_action(req, file_id, offset, size, buf, resp);
         p += d7a_alp_perm_request_action(p, req, req_size, root_key, false);
     }
-    
+   
     // Write action
     p += d7a_alp_write_action(p, file_id, offset, size, buf, resp);
     
@@ -531,69 +545,26 @@
     // Save initial position of the command buffer
     uint8_t* t = p;
     
-    bool broadcast = false;
     uint8_t current_tag;
     d7a_msg_t** ret = NULL;
-    uint32_t max_responses = 2;
-    uint8_t i;
+    uint32_t max_responses;
     
-    if (addressee)
-    {
-        if (addressee->ctrl.bf.idf == D7A_ID_NBID)
-        {
-            broadcast = true;
-            d7a_ctf_t ctf;
-            ctf.byte = addressee->id[0];
-            max_responses = ((1 << (2*ctf.bf.exp)) * ctf.bf.mant) + 1;
-        }
-        else if (addressee->ctrl.bf.idf == D7A_ID_NOID)
-        {
-            broadcast = true;
-            max_responses = 33;
-        }
-    }
+    max_responses = d7a_alp_construct_itf(p, root_key, addressee, retry, true);
     
     // malloc and init pointer array
-    ret = (d7a_msg_t**)MALLOC(sizeof(d7a_msg_t*) * (max_responses + 1));
-    for (i = 0; i < (max_responses + 1); i++)
-    {
-        ret[i] = NULL;
-    }
-    
-    // Construct command
-    
+    ret = d7a_alp_init_ret(max_responses);
+        
     // Tag action
     p += d7a_alp_tag(p, true);
     
     // get tag
     current_tag = g_alp_ctx.tag;
-    
-    if (addressee)
-    {
-        // Construct interface
-        d7a_itf_t itf = {
-            // Dash7 interface
-            .type = 0xD7,
-            // Switch response type if broadcast
-            //.cfg.qos.bf.resp = (broadcast)? D7A_RESP_ALL : D7A_RESP_ANY,
-            .cfg.qos.bf.resp = D7A_RESP_ALL,
-            .cfg.qos.bf.retry = retry,
-            .cfg.qos.bf.record = 0,
-            .cfg.qos.bf.stop_on_err = 0,
-            .cfg.dorm_to.byte = 0,
-            .cfg.te.byte = 0,
-        };
-        memcpy(&itf.cfg.addressee, addressee, sizeof(d7a_addressee_t));
-        
-        // Forward action
-        p += d7a_alp_forward_action(p, &itf, true);
-    }
-    
+           
     // Ask for root permissions
     if (root_key)
     {
         uint8_t req[100];
-        uint8_t req_size = d7a_alp_read_action(req, file_id, offset, size, true);
+        uint8_t req_size = d7a_alp_read_action(p, file_id, offset, size, true);
         p += d7a_alp_perm_request_action(p, req, req_size, root_key, false);
     }
     
@@ -619,72 +590,30 @@
     // Save initial position of the command buffer
     uint8_t* t = p;
     
-    bool broadcast = false;
     uint8_t current_tag;
     d7a_msg_t** ret = NULL;
-    uint32_t max_responses = 2;
-    uint8_t i;
+    uint32_t max_responses;
     
-    if (addressee)
-    {
-        if (addressee->ctrl.bf.idf == D7A_ID_NBID)
-        {
-            broadcast = true;
-            d7a_ctf_t ctf;
-            ctf.byte = addressee->id[0];
-            max_responses = ((1 << (2*ctf.bf.exp)) * ctf.bf.mant) + 1;
-        }
-        else if (addressee->ctrl.bf.idf == D7A_ID_NOID)
-        {
-            broadcast = true;
-            max_responses = 33;
-        }
-    }
+    max_responses = d7a_alp_construct_itf(p, root_key, addressee, retry, resp);
     
     // malloc and init pointer array
-    ret = (d7a_msg_t**)MALLOC(sizeof(d7a_msg_t*) * (max_responses + 1));
-    for (i = 0; i < (max_responses + 1); i++)
-    {
-        ret[i] = NULL;
-    }
-    
-    // Construct command
+    ret = d7a_alp_init_ret(max_responses);
     
     // Tag action
     p += d7a_alp_tag(p, true);
     
     // get tag
     current_tag = g_alp_ctx.tag;
-    
-    if (addressee)
-    {
-        // Construct interface
-        d7a_itf_t itf = {
-            // Dash7 interface
-            .type = 0xD7,
-            // Switch response type if broadcast
-            .cfg.qos.bf.resp = (broadcast)? D7A_RESP_ALL : 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,
-            .cfg.te.byte = 0,
-        };
-        memcpy(&itf.cfg.addressee, addressee, sizeof(d7a_addressee_t));
-        
-        // Forward action
-        p += d7a_alp_forward_action(p, &itf, true);
-    }
-    
+               
     // Ask for root permissions
     if (root_key)
     {
         uint8_t req[100];
-        uint8_t req_size = d7a_alp_flush_action(req, file_id, resp);
+        uint8_t req_size = d7a_alp_flush_action(p, file_id, resp);
         p += d7a_alp_perm_request_action(p, req, req_size, root_key, false);
     }
     
-    // Write action
+    // Flush action
     p += d7a_alp_flush_action(p, file_id, resp);
     
     // Send command
@@ -697,7 +626,7 @@
 }
 
 
-void d7a_alp_thread(const void *p)
+void d7a_alp_thread()
 {
     FPRINT("(id:0x%08x)\r\n", osThreadGetId());
     d7a_com_rx_msg_t* pkt;