WizziLab / Mbed OS D7A_1x_demo_send_file_data_and_forget Featured

Dependencies:   modem_ref_helper

Files at this revision

API Documentation at this revision

Comitter:
Jeej
Date:
Thu Sep 20 12:37:37 2018 +0000
Parent:
9:ef574b88e854
Child:
11:a3308870afac
Commit message:
Updated libraries and APP for modem v5.2+

Changed in this revision

files.cpp 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_ref_helper.lib Show annotated file Show diff for this revision Revisions of this file
--- a/files.cpp	Fri Mar 09 16:19:31 2018 +0000
+++ b/files.cpp	Thu Sep 20 12:37:37 2018 +0000
@@ -28,7 +28,7 @@
     .fw_version.id       = 0,
     .fw_version.major    = 1,
     .fw_version.minor    = 0,
-    .fw_version.patch    = 0,
+    .fw_version.patch    = 1,
     .fw_version.hash     = 0x00000000,
     /// Not used
     .cup_max_size        = 0x00000000
--- a/main.cpp	Fri Mar 09 16:19:31 2018 +0000
+++ b/main.cpp	Thu Sep 20 12:37:37 2018 +0000
@@ -12,7 +12,29 @@
 sensor_config_t g_light_config;
 Queue<void, 8> g_file_modified;
 
-uint8_t g_main_id;
+void print_status(uint8_t id, int status)
+{
+    switch (status)
+    {
+        case ALP_ERR_NONE:
+            PRINT("Status[%d]: OK\n", id);
+            break;
+        default:
+            PRINT("Status[%d]: error %d\n", id, status);
+            break;
+    }
+}
+
+// Callback for id User
+void my_main_callback(uint8_t terminal, int8_t err, uint8_t id)
+{
+    print_status(id, err);
+    
+    if (terminal)
+    {    
+        modem_ready.release();
+    }
+}
 
 static bool report_ok(uint32_t last_report_time)
 {
@@ -27,19 +49,19 @@
 }
 
 // Check parameters to see if data should be send
-static bool report_needed(sensor_config_t* config, int32_t value, int32_t last_value, uint32_t last_report_time, uint8_t user_id)
+static bool report_needed(sensor_config_t* config, int32_t value, int32_t last_value, uint32_t last_report_time, uint8_t id)
 {
     switch (config->report_type)
     {
         case REPORT_ALWAYS:
             // Send a report at each measure
-            PRINT("Report[%d] always\r\n", user_id);
+            PRINT("Report[%d] always\r\n", id);
             return report_ok(last_report_time);
         case REPORT_ON_DIFFERENCE:
             // Send a report when the difference between the last reported measure and the current mesure is greater than max_diff
             if (abs(last_value - value) >= config->max_diff && config->max_diff)
             {
-                PRINT("Report[%d] on difference (last:%d new:%d max_diff:%d)\r\n", user_id, last_value, value, config->max_diff);
+                PRINT("Report[%d] on difference (last:%d new:%d max_diff:%d)\r\n", id, last_value, value, config->max_diff);
                 return report_ok(last_report_time);
             }
             break;
@@ -50,7 +72,7 @@
                 || (value < config->threshold_high  && last_value >= config->threshold_high)
                 || (value > config->threshold_low   && last_value <= config->threshold_low))
             {
-                PRINT("Report[%d] on threshold (last:%d new:%d th:%d tl:%d)\r\n", user_id, last_value, value, config->threshold_high, config->threshold_low);
+                PRINT("Report[%d] on threshold (last:%d new:%d th:%d tl:%d)\r\n", id, last_value, value, config->threshold_high, config->threshold_low);
                 return report_ok(last_report_time);
             }
             break;
@@ -61,7 +83,7 @@
     // Send a report if it's been more than max_period since the last report
     if (((last_report_time/1000) >= config->max_period) && config->max_period)
     {
-        PRINT("Report[%d] on period (max_period:%d time:%d)\r\n", user_id, config->max_period, last_report_time);
+        PRINT("Report[%d] on period (max_period:%d time:%d)\r\n", id, config->max_period, last_report_time);
         return report_ok(last_report_time);
     }
 
@@ -75,6 +97,7 @@
     
     // To force a first report
     uint32_t last_report_time = 0xFFFFFFFF;
+    uint8_t id = modem_get_id(my_main_callback);
     
     FPRINT("(id:0x%08x)\r\n", osThreadGetId());
     
@@ -87,12 +110,12 @@
         
         PRINT("Light %d\r\n", light_level);
                 
-        if (report_needed(&g_light_config, light_level, light_level_old, last_report_time, g_main_id))
+        if (report_needed(&g_light_config, light_level, light_level_old, last_report_time, id))
         {
             PRINT("Light report %d\r\n", light_level);
         
             // Send notification
-            modem_write_file(FID_SENSOR_LIGHT, &light_level, 0, SIZE_SENSOR_LIGHT, g_main_id);
+            modem_write_file(FID_SENSOR_LIGHT, &light_level, 0, SIZE_SENSOR_LIGHT, id);
             modem_ready.wait();
         
             // Update 
@@ -138,38 +161,6 @@
     memcpy(hdr, ram_fs_get_header(fid), sizeof(alp_file_header_t));
 }
 
-void print_status(int status)
-{
-    switch (status)
-    {
-        case ALP_ERR_NONE:
-            PRINT("Status: OK\n");
-            break;
-        case ALP_ERR_FILE_EXIST:
-            PRINT("Status: Already registered\n");
-            break;
-        default:
-            PRINT("Status: error %d\n", status);
-            break;
-    }
-}
-
-void print_resp(int status)
-{
-    switch (status)
-    {
-        case ALP_ERR_NONE:
-            PRINT("Resp: OK\n");
-            break;
-        case ALP_ERR_FILE_EXIST:
-            PRINT("Resp: Already registered\n");
-            break;
-        default:
-            PRINT("Resp: error %d\n", status);
-            break;
-    }
-}
-
 modem_callbacks_t callbacks = {
     .read       = my_read,
     .write      = my_write,
@@ -180,25 +171,10 @@
     .lqual      = my_lqual,
     .ldown      = my_ldown,
     .reset      = my_reset,
-    .boot       = my_boot
+    .boot       = my_boot,
+    .busy       = my_busy,
 };
 
-// Callback for g_main_id User
-void my_main_callback(uint8_t terminal, int8_t err, uint8_t id)
-{
-    (void)id;
-    
-    if (terminal)
-    {    
-        print_status(err);
-        modem_ready.release();
-    }
-    else
-    {
-        print_resp(err);
-    }
-}
-
 /*** Main function ------------------------------------------------------------- ***/
 int main()
 {
@@ -215,7 +191,7 @@
     
     modem_helper_open(&callbacks);
     
-    g_main_id = modem_get_id(my_main_callback);
+    uint8_t id = modem_get_id(my_main_callback);
     
     DPRINT("Register Files\n");
     
@@ -230,21 +206,23 @@
 
     // 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, g_main_id);
+    modem_enable_urc(ALP_URC_TYPE_LQUAL, IFID_REPORT, 10, true, id);
     modem_ready.wait();
     
     PRINT("Start D7A Stack\n");
-    modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP , true, g_main_id);
+    modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP , true, id);
     modem_ready.wait();
     
     PRINT("Notify Modem Version\n");
-    modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, SIZE_HOST_REV, g_main_id);
+    modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, SIZE_HOST_REV, id);
     modem_ready.wait();
     
     PRINT("Notify FW Version\n");
-    modem_notify_file(FID_HOST_REV, 0, SIZE_HOST_REV, g_main_id);
+    modem_notify_file(FID_HOST_REV, 0, SIZE_HOST_REV, id);
     modem_ready.wait();
     
+    modem_free_id(id);
+    
     // Start file modified thread
     Thread th_file_modified(osPriorityNormal, 1024, NULL);
     osStatus status = th_file_modified.start(thread_file_modified);
--- a/modem_callbacks.cpp	Fri Mar 09 16:19:31 2018 +0000
+++ b/modem_callbacks.cpp	Thu Sep 20 12:37:37 2018 +0000
@@ -62,23 +62,59 @@
     modem_respond(0, (ram_fs_delete(fid))? ALP_ERR_FILE_NOT_FOUND : ALP_ERR_NONE, id);
 }
 
-void my_udata(u8 fid,void *data,u32 offset,u32 length, u8 i_type, u8 i_length, u8* i_data)
-{
-    (void)data;
-    (void)i_length;
-    PRINT("Got UNS File[%3d]@%d %d Bytes\n",fid,offset,length);
-    if (i_type == ALP_ITF_TYPE_D7A)
-    {
-        static union {
-            u8      b[8];
-            u32     w[2];
-        } uid;
-        d7a_sp_res_t* istat = (d7a_sp_res_t*) i_data;
-        memcpy(uid.b,istat->addressee.id,8);
-        PRINT("From UID: %08X%08X (rxlev:%d lb:%d)\n",
-                HAL_U32_BYTE_SWAP(uid.w[0]), HAL_U32_BYTE_SWAP(uid.w[1]),
-                istat->rxlev, istat->lb);
-    }
+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_lqual(u8 ifid, int per)
@@ -93,13 +129,32 @@
 
 void my_reset(void)
 {
+    PRINT("Restarting application...\r\n");
+    FLUSH();
     NVIC_SystemReset();
 }
 
 void my_boot(u8 cause, u16 number)
 {
-    PRINT("Modem BOOT[%c] #%d\r\nRe-Init\r\n", cause, number);
+    PRINT("Modem BOOT[%c] #%d\r\n", cause, number);
     
     // Modem re-booted, restart APP
     my_reset();
+}
+
+void my_busy(u8 busy)
+{
+    if (busy)
+    {
+        PRINT("Modem Busy\r\n");
+        
+        /* Stop report, do not use modem */
+        /* Wait for modem reboot or modem not busy */
+    }
+    else
+    {
+        PRINT("Modem not Busy\r\n");
+        
+        /* Resume reports */
+    }
 }
\ No newline at end of file
--- a/modem_callbacks.h	Fri Mar 09 16:19:31 2018 +0000
+++ b/modem_callbacks.h	Thu Sep 20 12:37:37 2018 +0000
@@ -5,8 +5,9 @@
 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(u8 fid,void *data,u32 offset,u32 length, u8 i_type, u8 i_length, u8* i_data);
+void my_udata(void *data, u32 length);
 void my_lqual(u8 ifid, int per);
 void my_ldown(u8 ifid);
 void my_reset(void);
-void my_boot(u8 cause, u16 number);
\ No newline at end of file
+void my_boot(u8 cause, u16 number);
+void my_busy(u8 busy);
\ No newline at end of file
--- a/modem_ref_helper.lib	Fri Mar 09 16:19:31 2018 +0000
+++ b/modem_ref_helper.lib	Thu Sep 20 12:37:37 2018 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/teams/WizziLab/code/modem_ref_helper/#d3b2889f9fab
+https://developer.mbed.org/teams/WizziLab/code/modem_ref_helper/#7386c5c5bb9f