Trond Enger / d7a_1x

Fork of d7a_1x by WizziLab

Revision:
77:8c792719a1fc
Parent:
76:fda2e34ff19d
Child:
78:f1c0affd99e7
--- a/src/d7a_alp.cpp	Tue Dec 20 09:30:14 2016 +0000
+++ b/src/d7a_alp.cpp	Wed Dec 21 10:47:29 2016 +0000
@@ -10,17 +10,16 @@
 #include "d7a_alp.h"
 #include "sha.h"
 
-typedef struct {
-    uint8_t tag;
-    uint8_t buffer[256];
-    Queue<d7a_com_rx_msg_t, 16> pkt_queue;
-    Queue<d7a_com_rx_msg_t, 8> pl_queue;
-    
-    UnsolicitedMsgFunction uns_msg;
-    Thread* thread;
-} d7a_alp_ctx_t;
+#define ALP_CMD_MAX_LENGHT          (256)
 
-static d7a_alp_ctx_t g_alp_ctx;
+static uint8_t                      g_alp_tag;
+static uint8_t                      g_alp_buffer[ALP_CMD_MAX_LENGHT];
+static Queue<d7a_com_rx_msg_t, 8>   g_alp_pkt_queue;
+static Queue<d7a_com_rx_msg_t, 8>   g_alp_pl_queue;
+static UnsolicitedMsgFunction       g_alp_uns_msg;
+static Thread                       g_alp_thread(osPriorityHigh, DEFAULT_STACK_SIZE, NULL);
+
+static const uint32_t g_alp_stack_size = 1 + ALP_CMD_MAX_LENGHT + sizeof(g_alp_pkt_queue) + sizeof(g_alp_pl_queue) + 4 + 256;
 
 void d7a_alp_thread();
 
@@ -28,10 +27,9 @@
 {
     FPRINT("\r\n");
 
-    g_alp_ctx.uns_msg = uns_msg;
-    g_alp_ctx.thread = new Thread(osPriorityHigh, DEFAULT_STACK_SIZE*4, NULL);
+    g_alp_uns_msg = uns_msg;    
     
-    osStatus err = g_alp_ctx.thread->start(d7a_alp_thread);
+    osStatus err = g_alp_thread.start(d7a_alp_thread);
     ASSERT(err == osOK, "Failed to start d7a_alp_thread (err: %d)\r\n", err);
     
     return D7A_ERR_NONE;
@@ -41,7 +39,7 @@
 {
     FPRINT("\r\n");
 
-    g_alp_ctx.thread->terminate();
+    g_alp_thread.terminate();
     
     return D7A_ERR_NONE;
 }
@@ -49,26 +47,26 @@
 void d7a_alp_new_pkt(d7a_com_rx_msg_t* pkt)
 {
     FPRINT("\r\n");
-    ASSERT(g_alp_ctx.pkt_queue.put(pkt) == osOK, "ALP queue full!\r\n");
+    ASSERT(g_alp_pkt_queue.put(pkt) == osOK, "ALP queue full!\r\n");
 }
 
 static void d7a_alp_new_pl(d7a_com_rx_msg_t* pl)
 {
     FPRINT("\r\n");
-    ASSERT(g_alp_ctx.pl_queue.put(pl) == osOK, "ALP PL queue full!\r\n");
+    ASSERT(g_alp_pl_queue.put(pl) == osOK, "ALP PL queue full!\r\n");
 }
 
 static d7a_com_rx_msg_t* d7a_alp_wait_pkt(uint32_t millisec)
 {
     FPRINT("\r\n");
-    osEvent evt = g_alp_ctx.pkt_queue.get(millisec);
+    osEvent evt = g_alp_pkt_queue.get(millisec);
     return (evt.status == osEventMessage)? (d7a_com_rx_msg_t*)evt.value.p : NULL;
 }
 
 static d7a_com_rx_msg_t* d7a_alp_wait_pl(uint32_t millisec)
 {
     FPRINT("\r\n");
-    osEvent evt = g_alp_ctx.pl_queue.get(millisec);
+    osEvent evt = g_alp_pl_queue.get(millisec);
     return (evt.status == osEventMessage)? (d7a_com_rx_msg_t*)evt.value.p : NULL;
 }
 
@@ -268,7 +266,7 @@
     uint8_t* t = p;
     
     *p++ = ALP_OPCODE_TAG + ((eop)? ALP_CTRL_EOP : 0);
-    *p++ = ++g_alp_ctx.tag;
+    *p++ = ++g_alp_tag;
     
     return (uint32_t)(p - t);
 }
@@ -496,7 +494,7 @@
     FPRINT("\r\n");
     
     // Get command buffer
-    uint8_t* p = &g_alp_ctx.buffer[0];
+    uint8_t* p = &g_alp_buffer[0];
     // Save initial position of the command buffer
     uint8_t* t = p;
     
@@ -514,7 +512,7 @@
     p += d7a_alp_tag(p, true);
     
     // get tag
-    current_tag = g_alp_ctx.tag;
+    current_tag = g_alp_tag;
        
     // Ask for root permissions
     if (root_key)
@@ -528,7 +526,7 @@
     p += d7a_alp_write_action(p, file_id, offset, size, buf, resp);
     
     // Send command
-    d7a_com_dump(&g_alp_ctx.buffer[0], (uint8_t)(p - t), KAL_COM_FLOW_AT_CMD);
+    d7a_com_dump(&g_alp_buffer[0], (uint8_t)(p - t), KAL_COM_FLOW_AT_CMD);
     
     // Parse responses
     d7a_alp_construct_resp(ret, current_tag, max_responses);
@@ -541,7 +539,7 @@
     FPRINT("\r\n");
     
     // Get command buffer
-    uint8_t* p = &g_alp_ctx.buffer[0];
+    uint8_t* p = &g_alp_buffer[0];
     // Save initial position of the command buffer
     uint8_t* t = p;
     
@@ -558,7 +556,7 @@
     p += d7a_alp_tag(p, true);
     
     // get tag
-    current_tag = g_alp_ctx.tag;
+    current_tag = g_alp_tag;
            
     // Ask for root permissions
     if (root_key)
@@ -572,7 +570,7 @@
     p += d7a_alp_read_action(p, file_id, offset, size, true);
     
     // Send command
-    d7a_com_dump(&g_alp_ctx.buffer[0], (uint8_t)(p - t), KAL_COM_FLOW_AT_CMD);
+    d7a_com_dump(&g_alp_buffer[0], (uint8_t)(p - t), KAL_COM_FLOW_AT_CMD);
     
     // Parse responses
     d7a_alp_construct_resp(ret, current_tag, max_responses);
@@ -586,7 +584,7 @@
     FPRINT("\r\n");
     
     // Get command buffer
-    uint8_t* p = &g_alp_ctx.buffer[0];
+    uint8_t* p = &g_alp_buffer[0];
     // Save initial position of the command buffer
     uint8_t* t = p;
     
@@ -603,7 +601,7 @@
     p += d7a_alp_tag(p, true);
     
     // get tag
-    current_tag = g_alp_ctx.tag;
+    current_tag = g_alp_tag;
                
     // Ask for root permissions
     if (root_key)
@@ -617,7 +615,7 @@
     p += d7a_alp_flush_action(p, file_id, resp);
     
     // Send command
-    d7a_com_dump(&g_alp_ctx.buffer[0], (uint8_t)(p - t), KAL_COM_FLOW_AT_CMD);
+    d7a_com_dump(&g_alp_buffer[0], (uint8_t)(p - t), KAL_COM_FLOW_AT_CMD);
     
     // Parse responses
     d7a_alp_construct_resp(ret, current_tag, max_responses);
@@ -646,7 +644,7 @@
                 break;
             case KAL_COM_FLOW_AT_UNS:
                 DPRINT("KAL_COM_FLOW_AT_UNS\r\n");
-                if (g_alp_ctx.uns_msg)
+                if (g_alp_uns_msg)
                 {
                     d7a_msg_t** uns = (d7a_msg_t**)MALLOC(sizeof(d7a_msg_t*) * 2);
                     
@@ -658,7 +656,7 @@
                     FREE(pl);
                     
                     // Callback
-                    g_alp_ctx.uns_msg(uns);
+                    g_alp_uns_msg(uns);
                 }
 
                 FREE(pkt);