Send file data through D7A Action Protocol demo.

Dependencies:   modem_ref_helper

Revision:
10:daa4b52991b7
Parent:
9:ef574b88e854
Child:
11:a3308870afac
--- 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);