Trond Enger / d7a_1x

Fork of d7a_1x by WizziLab

Revision:
28:0376b97b4b55
Parent:
27:934ab7455115
Child:
29:8e7c5c1e9aab
diff -r 934ab7455115 -r 0376b97b4b55 src/d7a_modem.cpp
--- a/src/d7a_modem.cpp	Fri Apr 01 10:55:30 2016 +0000
+++ b/src/d7a_modem.cpp	Mon Apr 04 14:00:28 2016 +0000
@@ -8,11 +8,14 @@
 
 typedef struct {
     Thread* thread;
+    Semaphore* ressource;
     Queue<void, 8> ready;
     Queue<void, 2> boot;
     Queue<d7a_com_rx_msg_t, 16> pkt_queue;
     int8_t status;
     DigitalInOut* reset;
+    uint8_t waiting_count;
+    uint8_t ready_count;
 } d7a_modem_ctx_t;
 
 d7a_modem_ctx_t g_modem_ctx;
@@ -22,8 +25,12 @@
 void d7a_modem_open( PinName reset_pin )
 {
     FPRINT("\r\n");
+    
+    g_modem_ctx.ressource = new Semaphore(1);
     g_modem_ctx.thread = new Thread(d7a_modem_thread, NULL, osPriorityBelowNormal, DEFAULT_STACK_SIZE*2);
     g_modem_ctx.reset = new DigitalInOut(reset_pin, PIN_OUTPUT, OpenDrain, 0);
+    g_modem_ctx.waiting_count = 0;
+    g_modem_ctx.ready_count = 0;
     
     Thread::wait(10);
     
@@ -38,7 +45,7 @@
 
 int32_t d7a_modem_wait_ready( uint32_t millisec )
 {
-    FPRINT("\r\n");
+    //FPRINT("(%d)\r\n", ++g_modem_ctx.waiting_count);
     int32_t ret;
     osEvent evt = g_modem_ctx.ready.get(millisec);
     if (evt.status == osEventMessage)
@@ -81,6 +88,8 @@
 
 void d7a_modem_register_file(uint8_t fid, uint8_t fifo_size, uint8_t flush_policy)
 {
+    g_modem_ctx.ressource->wait();
+    
     FPRINT("(fid:%d, fifo_size:%d, flush_policy:0x%02X)\r\n", fid, fifo_size, flush_policy);
     register_file_param_t rparams = {
         .bf.fid = fid ,
@@ -102,16 +111,21 @@
         d7a_fs_distant_create(rparams.bf.fid, &props);
         g_modem_ctx.status = (int32_t)d7a_fs_wait_done(TO_FS);
     }
+    
     if(g_modem_ctx.status == 0)
     {
         // Distant file is ready, lets register a D7A Interface for it
         d7a_modem_msg(WM_CMD_REGISTER_FILE,(uint8_t*)&rparams,sizeof(register_file_param_t));
         d7a_modem_wait_ready();
     }
+    
+    g_modem_ctx.ressource->release();
 }
 
 void d7a_modem_notify_file(uint8_t fid, uint32_t offset, uint32_t length)
 {
+    g_modem_ctx.ressource->wait();
+    
     FPRINT("(fid:%d, offset:%d, length:%d)\r\n", fid, offset, length);
     notify_file_param_t nfp = {
         .bf.fid = fid,
@@ -121,6 +135,10 @@
 
     d7a_modem_msg(WM_CMD_NOTIFY_FILE,(uint8_t*)&nfp,sizeof(notify_file_param_t));
     d7a_modem_wait_ready();
+    
+    Thread::wait(100); // XXX: To avoid concurrent file access
+    
+    g_modem_ctx.ressource->release();
 }
 
 void d7a_modem_infos( void )
@@ -161,7 +179,7 @@
                 uint8_t cmd = pkt->buffer[0];
                 if (cmd == WM_CMD_READY)
                 {
-                    DPRINT("Modem ready\r\n");
+                    //FPRINT("(Modem ready (%d))\r\n", --g_modem_ctx.waiting_count);
                     g_modem_ctx.ready.put(NULL);
                 }
                 else if (cmd == WM_CMD_BOOT)