Data reception demo.

Dependencies:   modem_ref_helper CRC

Files at this revision

API Documentation at this revision

Comitter:
marin_wizzi
Date:
Fri Oct 29 13:12:47 2021 +0000
Parent:
14:44cbb58de405
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.cpp 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
diff -r 44cbb58de405 -r e90cc8e37c4d CRC.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/CRC.lib	Fri Oct 29 13:12:47 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/WizziLab/code/CRC/#88116ae677af
diff -r 44cbb58de405 -r e90cc8e37c4d files.cpp
--- a/files.cpp	Thu May 28 09:13:19 2020 +0000
+++ b/files.cpp	Fri Oct 29 13:12:47 2021 +0000
@@ -26,9 +26,9 @@
     ///  FW_ID | MAJOR | MINOR | PATCH | HASH |
     //     1B  |  1B   |  1B   |   2B  |  4B  |
     .fw_version.id       = 0,
-    .fw_version.major    = 1,
+    .fw_version.major    = 2,
     .fw_version.minor    = 0,
-    .fw_version.patch    = 5,
+    .fw_version.patch    = 0,
     .fw_version.hash     = 0x20200528,
     /// Not used
     .cup_max_size        = 0x00000000
@@ -53,3 +53,5 @@
     .size = HAL_U32_BYTE_SWAP((uint32_t)SIZE_STATUS_FILE),
     .alloc= HAL_U32_BYTE_SWAP((uint32_t)SIZE_STATUS_FILE)
 };
+
+uint8_t f_status_file = 0;
diff -r 44cbb58de405 -r e90cc8e37c4d files.h
--- a/files.h	Thu May 28 09:13:19 2020 +0000
+++ b/files.h	Fri Oct 29 13:12:47 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> g_file_modified;
 
 #define FID_HOST_REV                65
 #define SIZE_HOST_REV               sizeof(revision_t)
@@ -21,6 +28,7 @@
 #define FID_STATUS_FILE             133
 #define SIZE_STATUS_FILE            sizeof(uint8_t)
 extern const alp_file_header_t      h_status_file;
+extern uint8_t                      f_status_file;
 
 // No local data since the file will be created on the modem
 
diff -r 44cbb58de405 -r e90cc8e37c4d main.cpp
--- a/main.cpp	Thu May 28 09:13:19 2020 +0000
+++ b/main.cpp	Fri Oct 29 13:12:47 2021 +0000
@@ -1,17 +1,33 @@
 // @autor: jeremie@wizzilab.com
 // @date: 2017-05-02
 
-#include "modem_ref_helper.h"
+#include "modem_d7a.h"
 #include "modem_callbacks.h"
 #include "files.h"
+#include "crc.h"
 #include "CriusOLED.h"
 
 #define RESTORE_TIME    30
 #define KEEP_ALIVE      3600
 #define MIN_REPORT_PERIOD   (10) // Seconds
+#define CHUNK_SIZE              128
 
-Semaphore modem_ready(0);
-Queue<void, 8> g_file_modified;
+Queue<touch_t, 8> g_file_modified;
+
+// This describe the upload interface
+// Do not modify uncommented parameters
+alp_itf_d7a_cfg_t report_itf = {
+    .type                           = ALP_ITF_TYPE_D7A,
+    .cfg.to.byte                    = D7A_CTF_ENCODE(0),
+    .cfg.te.byte                    = D7A_CTF_ENCODE(0),
+    .cfg.qos.bf.resp                = D7A_RESP_PREFERRED,
+    .cfg.qos.bf.retry               = ALP_RPOL_ONESHOT,
+    .cfg.addressee.ctrl.bf.nls      = D7A_NLS_AES_CCM_64, // Security level
+    .cfg.addressee.ctrl.bf.idf      = D7A_ID_NBID,
+    .cfg.addressee.xcl.bf           = {.s = 2, .m = 0x1}, // Gateway access class
+    .cfg.addressee.id[0]            = D7A_CTF_ENCODE(4),
+};
+
 
 uint8_t id;
 uint32_t g_restore_time = RESTORE_TIME + 1;
@@ -22,20 +38,6 @@
     sendImage();
 }
 
-static void my_main_callback(uint8_t terminal, int8_t err, uint8_t id)
-{
-    (void)id;
-    
-    if (ALP_ERR_NONE > err)
-    {
-        modem_print_error(ALP_ITF_TYPE_D7A, err);
-    }
-    
-    if (terminal)
-    {
-        modem_ready.release();
-    }
-}
 
 void thread_reset_display()
 {
@@ -43,7 +45,6 @@
     
     uint32_t time = 0;
     uint8_t status = 0;
-    uint8_t id = modem_get_id(my_main_callback);
     
     while (true)
     {
@@ -55,8 +56,7 @@
         if ((time++ % KEEP_ALIVE) == 0)
         {
             // Keep alive: Report status
-            modem_write_file(FID_STATUS_FILE, &status, 0, SIZE_STATUS_FILE, id);
-            modem_ready.acquire();
+            modem_notify_file(FID_STATUS_FILE, 0, SIZE_STATUS_FILE);
         }
         
         Thread::wait(1000);
@@ -65,22 +65,61 @@
 
 void thread_file_modified()
 {
+    touch_t* touch;
+    
     FPRINT("(id:0x%08x)\r\n", osThreadGetId());
     
     uint8_t fid;
     osEvent evt;
     
+    int err;
+    alp_payload_t* alp;
+    alp_payload_t* rsp;
+    
+    PRINT("Register Files\n");
+    ram_fs_new(FID_STRING_FILE, (uint8_t*)&h_string_file, (uint8_t*)&f_string_file);
+    ram_fs_new(FID_STATUS_FILE, (uint8_t*)&h_status_file, (uint8_t*)&f_status_file);
+    
+    modem_declare_file(FID_STRING_FILE, (alp_file_header_t*)&h_string_file);
+    modem_declare_file(FID_STATUS_FILE, (alp_file_header_t*)&h_status_file);
+    
+    PRINT("Enable D7A interface\n");
+    modem_d7a_enable_itf();
+    
+    // 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));
+    
+    
+    // Retrieve modem revision
+    PRINT("Send revision\n");
+    
+    revision_t rev;
+    
+    modem_read_file(FID_WM_REV, &rev, 0, sizeof(revision_t));
+    
+    // Send both to the server
+    // Build payload
+    alp = NULL;
+    alp = alp_payload_rsp_f_data(alp, FID_WM_REV, &rev, 0, sizeof(revision_t));
+    alp = alp_payload_rsp_f_data(alp, FID_HOST_REV, (void*)&f_rev, 0, sizeof(revision_t));
+    // Send
+    modem_remote_raw_alp((void*)&report_itf, alp, NULL, 10000);
+    
+    
     while (true)
     {
         evt = g_file_modified.get();
-        fid = (evt.status == osEventMessage)? (uint8_t)(uint32_t)evt.value.p : NULL;
         
-        switch (fid)
+        touch = (evt.status == osEventMessage)? (touch_t*)evt.value.p : NULL;
+        ASSERT(touch != NULL, "NULL touch pointer!\n");
+        
+        switch (touch->fid)
         {
             case FID_STRING_FILE:
                 // Print string
                 uint8_t str[SIZE_STRING_FILE];
-                ram_fs_read(FID_STRING_FILE, 0, SIZE_STRING_FILE, (uint8_t*)&str);
+                ram_fs_read(FID_STRING_FILE, (uint8_t*)&str, 0, SIZE_STRING_FILE);
                 PRINT("STRING: ");
                 PRINT((char*)str);
                 PRINT("\r\n");
@@ -91,13 +130,14 @@
                 g_restore_time = 0;
                 break;
             default:
-                PRINT("Unknown file %d\n", fid);
+                PRINT("Unknown file %d\n", touch->fid);
                 break;
         }
+        FREE(touch);
     }
 }
 
-modem_callbacks_t callbacks = {
+modem_ref_callbacks_t callbacks = {
     .read       = my_read,
     .write      = my_write,
     .read_fprop = my_read_fprop,
@@ -127,43 +167,11 @@
           
     FPRINT("(id:0x%08x)\r\n", osThreadGetId());
     
-    modem_helper_open(&callbacks);
+    modem_open(&callbacks);
     
-    uint8_t id = modem_get_id(my_main_callback);
-        
-    PRINT("Register Files\n");
-    // Allow remote access.
-    // The string file data is on this host, specify the data buffer.
-    modem_update_file(FID_STRING_FILE, (alp_file_header_t*)&h_string_file, (uint8_t*)&f_string_file);
-    // The status file data is in the modem, do not specify a data buffer.
-    modem_update_file(FID_STATUS_FILE, (alp_file_header_t*)&h_status_file, NULL);
-
-    // Put modem to listen to this access class
-    d7a_xcl_t my_xcl = { .bf.s = 0, .bf.m = 2 };
-    modem_write_file(D7A_FID_DLL_CFG, &my_xcl, 0, sizeof(d7a_xcl_t), id);
-    modem_ready.acquire();
-
-    // Configure URC: LQUAL on report file notification every 10 reports
-    PRINT("Setup URCs\n");
-    modem_enable_urc(ALP_URC_TYPE_LQUAL, IFID_REPORT, 10, true, id);
-    modem_ready.acquire();
-    
-    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("Notify Modem Version\n");
-    modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, SIZE_HOST_REV, id);
-    modem_ready.acquire();
-    
-    PRINT("Notify FW Version\n");
-    uint8_t default_root_key[16] = DEFAULT_ROOT_KEY;
-    modem_notify_host_rev((revision_t*)&f_rev, (alp_file_header_t*)&h_rev, default_root_key);
-    
-    modem_free_id(id);
     
     // Start file modified thread
-    Thread th_file_modified(osPriorityNormal, 512, NULL);
+    Thread th_file_modified(osPriorityNormal, 1024, NULL);
     osStatus status = th_file_modified.start(thread_file_modified);
     ASSERT(status == osOK, "Failed to start thread_file_modified (err: %d)\r\n", status);
     
diff -r 44cbb58de405 -r e90cc8e37c4d modem_callbacks.cpp
--- a/modem_callbacks.cpp	Thu May 28 09:13:19 2020 +0000
+++ b/modem_callbacks.cpp	Fri Oct 29 13:12:47 2021 +0000
@@ -1,116 +1,82 @@
-#include "modem_ref_helper.h"
+#include "modem_d7a.h"
+#include "files.h"
 
-extern Queue<void, 8>   g_file_modified;
+#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_read(fid, data, offset, length, id);
+        modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id);
     }
     else
     {
-        modem_respond(ALP_ERR_FILE_NOT_FOUND, 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)
 {
-    if (!ram_fs_write(fid, offset, length, (uint8_t*)data))
+    alp_errors_t err;
+    
+    if (ram_fs_write(fid, (uint8_t*)data, offset, length))
     {
-        modem_respond(ALP_ERR_NONE, id);
-        g_file_modified.put((void*)fid);
+        err = ALP_ERR_FILE_NOT_FOUND;
     }
     else
     {
-        modem_respond(ALP_ERR_FILE_NOT_FOUND, id);
-    }   
+        err = ALP_ERR_NONE;
+        
+        touch_t* touch = (touch_t*)MALLOC(sizeof(touch_t));
+        
+        touch->fid = fid;
+        touch->offset = offset;
+        touch->length = length;
+        
+        g_file_modified.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)
diff -r 44cbb58de405 -r e90cc8e37c4d modem_callbacks.h
--- a/modem_callbacks.h	Thu May 28 09:13:19 2020 +0000
+++ b/modem_callbacks.h	Fri Oct 29 13:12:47 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);
diff -r 44cbb58de405 -r e90cc8e37c4d modem_d7a.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modem_d7a.lib	Fri Oct 29 13:12:47 2021 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/WizziLab/code/modem_ref_helper/#99c556ae5b35
diff -r 44cbb58de405 -r e90cc8e37c4d modem_ref_helper.lib
--- a/modem_ref_helper.lib	Thu May 28 09:13:19 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