Send file data demo.

Dependencies:   modem_ref_helper CRC DebouncedInterrupt

Files at this revision

API Documentation at this revision

Comitter:
marin_wizzi
Date:
Fri Oct 29 13:11:38 2021 +0000
Parent:
19:68f3a84dbc8f
Commit message:
Compatible with 6.2 modem version

Changed in this revision

CRC.lib Show annotated file Show diff for this revision Revisions of this file
files.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
modem_callbacks.cpp Show annotated file Show diff for this revision Revisions of this file
modem_callbacks.h Show annotated file Show diff for this revision Revisions of this file
modem_d7a.lib Show annotated file Show diff for this revision Revisions of this file
modem_ref_helper.lib Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CRC.lib	Fri Oct 29 13:11:38 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/WizziLab/code/CRC/#88116ae677af
--- a/files.h	Thu May 28 09:13:44 2020 +0000
+++ b/files.h	Fri Oct 29 13:11:38 2021 +0000
@@ -7,6 +7,13 @@
 #include "alp_helpers.h"
 #include "modem_ref.h"
 
+TYPEDEF_STRUCT_PACKED {
+    uint8_t fid;
+    uint32_t offset;
+    uint32_t length;
+} touch_t;
+
+extern Queue<touch_t, 8> modem_resp;
 
 #define FID_HOST_REV            65
 #define SIZE_HOST_REV           sizeof(revision_t)
--- a/main.cpp	Thu May 28 09:13:44 2020 +0000
+++ b/main.cpp	Fri Oct 29 13:11:38 2021 +0000
@@ -2,15 +2,16 @@
 // @date: 2017-05-02
 
 #include "DebouncedInterrupt.h"
-#include "modem_ref_helper.h"
+#include "modem_d7a.h"
 #include "modem_callbacks.h"
 #include "files.h"
+#include "crc.h"
 
 
 Semaphore button_user(0);
 Semaphore modem_ready(0);
 Semaphore user_ready(0);
-Queue<void, 8> modem_resp;
+Queue<touch_t, 8> modem_resp;
 
 uint8_t id, g_report_id;
 
@@ -66,17 +67,17 @@
     
     if (terminal)
     {
-        modem_resp.put((void*)MODEM_RESP_TERMINAL);
+        modem_resp.put((touch_t*)MODEM_RESP_TERMINAL);
     }
     else
     {
         if (ALP_ERR_NONE == err)
         {
-            modem_resp.put((void*)MODEM_RESP_ACK);
+            modem_resp.put((touch_t*)MODEM_RESP_ACK);
         }
         else if (ALP_ERR_NONE > err)
         {
-            modem_resp.put((void*)MODEM_RESP_ERROR);
+            modem_resp.put((touch_t*)MODEM_RESP_ERROR);
         }
         else
         {
@@ -90,96 +91,93 @@
 
 void button_user_thread()
 {
+    touch_t* touch;
     FPRINT("(id:0x%08x)\r\n", osThreadGetId());
     
     osEvent evt;
     uint32_t resp;
     uint8_t alarm;
-    uint8_t id = modem_get_id(my_main_callback);
+    uint8_t id = modem_ref_get_id(my_main_callback);
     d7a_sp_res_t istat;
+    alp_payload_t* alp;
+    alp_payload_t* alp_rsp;
     uint8_t nb = 0;
+    int err;
     
     memset(&istat, 0, sizeof(d7a_sp_res_t));
     
     // Load alarm value
-    ram_fs_read(FID_ALARM, 0, 1, &alarm);
+    ram_fs_read(FID_ALARM, &alarm, 0, 1);
     
-    // Send initial value
-    modem_send_file_content((uint8_t*)&alarm_itf, D7_ITF_SIZE(&alarm_itf), (void*)&istat, FID_ALARM, &alarm, 0, 1, id);
-    modem_ready.acquire();
-    
-    // Change callback
-    modem_free_id(id);
-    id = modem_get_id(my_response_callback);
     
     while (true)
     {
+        
         // Wait for button press
         PRINT("PRESS BUTTON TO SEND ALARM...\r\n");
         button_user.acquire();
         
+        
+        
         nb = 0;
         
         // load/save value to keep choerency in case of remote access...
-        ram_fs_read(FID_ALARM, 0, 1, &alarm);
+        ram_fs_read(FID_ALARM, &alarm, 0, 1);
 
         // Toggle alarm state
         alarm = !alarm;
         
-        ram_fs_write(FID_ALARM, 0, 1, &alarm);
+        ram_fs_write(FID_ALARM, &alarm, 0, 1);
         
         PRINT("BUTTON ALARM %d\r\n", alarm);
         
-        // Send data
-        modem_send_file_content((uint8_t*)&alarm_itf, D7_ITF_SIZE(&alarm_itf), (void*)&istat, FID_ALARM, &alarm, 0, 1, id);
+        alp = NULL;
+        alp = alp_payload_rsp_f_data(alp, FID_ALARM, &alarm, 0, 1);
+                
+        err = modem_remote_raw_alp((void*)&alarm_itf, alp, &alp_rsp, (uint32_t)15000);
         
-        do
+        if (err < ALP_ERR_NONE)
+        {
+            PRINT("Timeout.\n");
+        }
+        else
         {
-            // Wait for callback
-            evt = modem_resp.get(3000);
-            resp = (evt.status == osEventMessage)? (uint32_t)evt.value.p : MODEM_RESP_TIMEOUT;
-                        
-            if (MODEM_RESP_ACK == resp)
-            {
-                // Print metadata
-                PRINT("ACK %d: ", ++nb);
-                PRINT_DATA("UID:", "%02X", istat.addressee.id, 8, " ");
-                PRINT("SNR: %ddB RXLEV: %ddBm LB: %ddB\n", istat.snr, -istat.rxlev, istat.lb);
+            err = alp_payload_get_err(alp_rsp);
+            PRINT("err %d\n", err);
+            modem_print_error(alarm_itf.type, err);
+        }
                 
-                // Clear istatus buffer
-                memset(&istat, 0, sizeof(d7a_sp_res_t));
-                
-                // Resume processing
-                user_ready.release();
-            }
-            else if (MODEM_RESP_TIMEOUT == resp)
+        do {
+            nb++;
+                    
+            alp = alp_payload_extract(&alp_rsp, ALP_OPCODE_RSP_ISTATUS);
+    
+            if (alp)
             {
-                // Could be because of the RF duty cycle,
-                // especialy after boot where the duty credit is near 0.
-                // The only thing to do is wait.
-                // The transmission will resume once some duty credit is available.
-                PRINT("WAITING...\n");
-            }
-            else if (MODEM_RESP_ERROR == resp)
-            {
-                PRINT("ERROR.\n");
-                
-                // Resume processing
-                user_ready.release();
+                alp_parsed_chunk_t r;
+                u8* p = alp->d;
+                    
+                alp_parse_chunk(&p, &r);
+                memcpy(&istat, r.data, r.meta.itf.length);
+                        
+                // Print metadata
+                PRINT("ACK %d: ", nb);
+                PRINT_DATA("UID:", "%02X", istat.addressee.id, 8, " ");
+                PRINT("snr:%d rxlev:%d lb:%d \n", istat.snr, -istat.rxlev, istat.lb);
+                        
+                alp_payload_free(alp);
             }
-            else if (MODEM_RESP_TERMINAL == resp)
+            else
             {
-                PRINT("DONE.\n");
-                
-                // Resume processing
-                user_ready.release();
+                break;
             }
+                    
+                FLUSH();
+            } while (1);
+            PRINT("Done.\n");
+        }
+}
 
-        } while (MODEM_RESP_TERMINAL != resp);
-        
-        PRINT("BUTTON ALARM DONE\r\n");
-    }
-}
 
 // Misc
 // ============================================================{{{
@@ -189,7 +187,7 @@
     memcpy(hdr, ram_fs_get_header(fid), sizeof(alp_file_header_t));
 }
 
-modem_callbacks_t callbacks = {
+modem_ref_callbacks_t callbacks = {
     .read       = my_read,
     .write      = my_write,
     .read_fprop = my_read_fprop,
@@ -220,29 +218,32 @@
               
     FPRINT("(id:0x%08x)\r\n", osThreadGetId());
     
-    modem_helper_open(&callbacks);
+    modem_open(&callbacks);
     
-    uint8_t id = modem_get_id(my_main_callback);
+    uint8_t id = modem_ref_get_id(my_main_callback);
     
     PRINT("Register Files\n");
     // Button/Alarm is a local file. As we want to check the outcome of sending
     // this, don't use D7AActP Notification but rather plain ALP ITF Forwarding.
     // Declaration just allows remote access.
-    modem_update_file(FID_ALARM, (alp_file_header_t*)&h_alarm, (uint8_t*)&f_alarm);
+    ram_fs_new(FID_ALARM, (uint8_t*)&h_alarm, (uint8_t*)&f_alarm);
+    modem_declare_file(FID_ALARM, (alp_file_header_t*)&h_alarm);
     
-    PRINT("Start D7A Stack\n");
-    modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP | ALP_D7A_ISTAT_UNS | ALP_D7A_ISTAT_EOP, true, id);
-    modem_ready.acquire();
+    
+    PRINT("Enable D7A interface\n");
+    modem_d7a_enable_itf();
     
-    PRINT("Notify Modem Version\n");
-    modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, SIZE_HOST_REV, id);
-    modem_ready.acquire();
+    // Host revision file is in the modem. Update it.
+    PRINT("Update host revision\n");
+    modem_write_file(FID_HOST_REV, (void*)&f_rev, 0, sizeof(revision_t));
+    
     
-    PRINT("Notify FW Version\n");
-    uint8_t default_root_key[16] = DEFAULT_ROOT_KEY;
-    modem_notify_host_rev(&f_rev, &h_rev, default_root_key);
+    // Retrieve modem revision
+    PRINT("Send revision\n");
     
-    modem_free_id(id);
+    revision_t rev;
+    
+    modem_read_file(FID_WM_REV, &rev, 0, sizeof(revision_t));
 
 #ifdef DEBUG_BUTTON
     DebouncedInterrupt user_interrupt(DEBUG_BUTTON);
--- a/modem_callbacks.cpp	Thu May 28 09:13:44 2020 +0000
+++ b/modem_callbacks.cpp	Fri Oct 29 13:11:38 2021 +0000
@@ -1,107 +1,82 @@
-#include "modem_ref_helper.h"
+#include "modem_d7a.h"
+#include "files.h"
 
+#define SERIAL_MAX_PACKET_SIZE  (255)
 
 // ============================================================}}}
 
 // Callbacks to MODEM's ALP requests
 // ============================================================{{{
-void my_read(u8 fid, u32 offset, u32 length, int id)
+void my_read(u8 action, u8 fid, u32 offset, u32 length, int id)
 {
-    u8 data[256];
+    u8 data[SERIAL_MAX_PACKET_SIZE];
     
-    if (ram_fs_read(fid, offset, length, data))
+    ASSERT((ALP_ACTION_RSP_TAG_SIZE + ALP_ACTION_RSP_F_DATA_SIZE(offset, length)) <= SERIAL_MAX_PACKET_SIZE,
+    "Read response too big for serial protocol (%d/%dmax)", length, ALP_ACTION_RSP_TAG_SIZE + ALP_ACTION_RSP_F_DATA_SIZE(offset,SERIAL_MAX_PACKET_SIZE));
+    
+    if (ram_fs_read(fid, data, offset, length))
     {
-        modem_respond(ALP_ERR_FILE_NOT_FOUND, id);
+        modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id);
     }
     else
     {
-        modem_respond_read(fid, data, offset, length, id);
+        modem_ref_respond_read(fid, data, offset, length, id);
     }
 }
 
-void my_write(u8 fid, void *data, u32 offset, u32 length, int id)
+
+void my_write(u8 action, u8 fid, void *data, u32 offset, u32 length, int id)
 {
-    modem_respond((ram_fs_write(fid, offset, length, (uint8_t*)data))? ALP_ERR_FILE_NOT_FOUND : ALP_ERR_NONE, id);
+    alp_errors_t err;
+    
+    if (ram_fs_write(fid, (uint8_t*)data, offset, length))
+    {
+        err = ALP_ERR_FILE_NOT_FOUND;
+    }
+    else
+    {
+        err = ALP_ERR_NONE;
+        
+        touch_t* touch = (touch_t*)MALLOC(sizeof(touch_t));
+        
+        touch->fid = fid;
+        touch->offset = offset;
+        touch->length = length;
+        
+        modem_resp.put(touch);
+    }
+    
+    modem_ref_respond(action, err, id);
 }
 
-void my_read_fprop(u8 fid, int id)
+void my_read_fprop(u8 action, u8 fid, int id)
 {
     u8* hdr = (u8*)ram_fs_get_header(fid);
     
     if (hdr != NULL)
     {
-        modem_respond_fprop(fid, hdr, id);
+        modem_ref_respond_fprop(fid, (alp_file_header_t*)hdr, id);
     }
     else
     {
-        modem_respond(ALP_ERR_FILE_NOT_FOUND, id);
+        modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id);
     }
 }
 
-void my_flush(u8 fid, int id)
+void my_flush(u8 action, u8 fid, int id)
 {
     // No flush in this file system
-    modem_respond(ALP_ERR_NONE, id);
-}
-
-void my_delete(u8 fid, int id)
-{
-    modem_respond((ram_fs_delete(fid))? ALP_ERR_FILE_NOT_FOUND : ALP_ERR_NONE, id);
+    modem_ref_respond(action, ALP_ERR_NONE, id);
 }
 
-void my_udata(void *data, u32 length)
-{    
-    uint8_t* p = (uint8_t*)data;
-    int32_t rem = length;
-    alp_parsed_chunk_t r;
-    d7a_sp_res_t* istat;
-    
-    do {
-        uint32_t parsed = alp_parse_chunk(&p, &r);
-        if (!parsed)
-        {
-            // Discard the payload in case of parsing error.
-            PRINT("Parsing error!\r\n");
-            break;
-        }
-        rem -= parsed;
-        
-        switch (r.type)
-        {
-            // Interface status
-            case ALP_OPCODE_RSP_ISTATUS:
-                // D7A Interface
-                if (ALP_ITF_TYPE_D7A == r.meta.itf.type)
-                {
-                    union {
-                        u8      b[8];
-                        u32     w[2];
-                    } uid;
-                    
-                    // ISTATUS can come either alone or together with ALP_OPCODE_RSP_F_DATA
-                    // but there should be only one per payload, moreover it will come first
-                    istat = (d7a_sp_res_t*)r.data;
-                    memcpy(uid.b,istat->addressee.id,8);
-                        
-                    PRINT("Got accessed by UID:%08X%08X SNR: %3ddB RXLEV: -%-3ddBm LB: %3ddB\n",
-                    HAL_U32_BYTE_SWAP(uid.w[0]), HAL_U32_BYTE_SWAP(uid.w[1]),
-                    istat->snr, istat->rxlev, istat->lb);
-                }
-                else
-                {
-                    PRINT("Got accessed by unknown Interface 0x%02X\n", r.meta.itf.type);
-                }
-                break;
-            // Data return
-            case ALP_OPCODE_RSP_F_DATA:
-                // RSP_F_DATA can come either alone or together with ISTATUS
-                PRINT("Got UNS File[%3d]@%d %d Bytes\n", r.meta.f_data.fid, r.meta.f_data.offset, r.meta.f_data.length);
-                break;
-            default:
-                PRINT("Untreated OPCODE %d\n", r.type);
-                break;
-        }
-    } while (rem > 0);
+void my_delete(u8 action, u8 fid, int id)
+{
+    modem_ref_respond(action, (ram_fs_delete(fid))? ALP_ERR_FILE_NOT_FOUND : ALP_ERR_NONE, id);
+}
+
+void my_udata(alp_payload_t* alp)
+{
+    alp_payload_print(alp);
 }
 
 void my_lqual(u8 ifid, int per)
--- a/modem_callbacks.h	Thu May 28 09:13:44 2020 +0000
+++ b/modem_callbacks.h	Fri Oct 29 13:11:38 2021 +0000
@@ -1,11 +1,11 @@
 #include "hal_types.h"
 
-void my_read(u8 fid, u32 offset, u32 length, int id);
-void my_write(u8 fid, void *data, u32 offset, u32 length, int id);
-void my_read_fprop(u8 fid, int id);
-void my_flush(u8 fid, int id);
-void my_delete(u8 fid, int id);
-void my_udata(void *data, u32 length);
+void my_read(u8 action, u8 fid, u32 offset, u32 length, int id);
+void my_write(u8 action, u8 fid, void *data, u32 offset, u32 length, int id);
+void my_read_fprop(u8 action, u8 fid, int id);
+void my_flush(u8 action, u8 fid, int id);
+void my_delete(u8 action, u8 fid, int id);
+void my_udata(alp_payload_t*);
 void my_lqual(u8 ifid, int per);
 void my_ldown(u8 ifid);
 void my_reset(void);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modem_d7a.lib	Fri Oct 29 13:11:38 2021 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/WizziLab/code/modem_ref_helper/#99c556ae5b35
--- a/modem_ref_helper.lib	Thu May 28 09:13:44 2020 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/teams/WizziLab/code/modem_ref_helper/#d624707636f9