Complete sensor demo.

Dependencies:   modem_ref_helper CRC X_NUCLEO_IKS01A1 DebouncedInterrupt

Revision:
18:51b15d8bf2fe
Parent:
17:3e6083d76bc6
--- a/main.cpp	Thu May 28 09:22:46 2020 +0000
+++ b/main.cpp	Fri Oct 29 12:55:52 2021 +0000
@@ -6,9 +6,10 @@
 #include "sensors.h"
 #include "sensors_cfg.h"
 #include "simul.h"
-#include "modem_ref_helper.h"
+#include "modem_d7a.h"
 #include "modem_callbacks.h"
 #include "files.h"
+#include "crc.h"
 
 #define MIN_REPORT_PERIOD   (10) // Seconds
 
@@ -19,10 +20,10 @@
 };
 
 Semaphore button_user(1);
-Semaphore modem_ready[MAX_USER_NB];
 Semaphore thread_ready(0);
 sensor_config_t g_light_config;
-Queue<void, 8> g_file_modified;
+int sensor_id = 0;
+Queue<touch_t, 8> g_file_modified;
 
 static bool report_ok(uint32_t last_report_time)
 {
@@ -37,20 +38,32 @@
 }
 
 // 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 thread_id)
 {
-
+    char thread_name[10];        
+    switch (thread_id)
+    {
+        case 0: strcpy(thread_name,"Magnetometer"); break;
+        case 1: strcpy(thread_name,"Accelerometer"); break;
+        case 2: strcpy(thread_name,"Pressure"); break;
+        case 3: strcpy(thread_name,"Humidity"); break;
+        case 4: strcpy(thread_name,"Temperature sensor 1"); break;
+        case 5: strcpy(thread_name,"Temperature sensor 2"); break;
+        case 6: strcpy(thread_name,"Light sensor"); break;
+        default: strcpy(thread_name,"Unknown"); break;          
+    }
+        
     switch (config->report_type)
     {
         case REPORT_ALWAYS:
             // Send a report at each measure
-            PRINT("Report[%d] always\r\n", user_id);
+            PRINT("Report[%d] : %s always\r\n", thread_id, thread_name);
             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] : %s on difference (last:%d new:%d max_diff:%d)\r\n", thread_id, thread_name, last_value, value, config->max_diff);
                 return report_ok(last_report_time);
             }
             break;
@@ -61,7 +74,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] : %s on threshold (last:%d new:%d th:%d tl:%d)\r\n", thread_id, thread_name, last_value, value, config->threshold_high, config->threshold_low);
                 return report_ok(last_report_time);
             }
             break;
@@ -72,10 +85,9 @@
     // 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] : %s on period (max_period:%d time:%d)\r\n", thread_id, thread_name, config->max_period, last_report_time);
         return report_ok(last_report_time);
     }
-
     return false;
 }
 
@@ -85,7 +97,7 @@
     button_user.release();
 }
 
-modem_callbacks_t callbacks = {
+modem_ref_callbacks_t callbacks = {
     .read       = my_read,
     .write      = my_write,
     .read_fprop = my_read_fprop,
@@ -99,20 +111,6 @@
     .busy       = my_busy,
 };
 
-// Callback for Users
-void my_main_callback(uint8_t terminal, int8_t err, uint8_t id)
-{
-    if (ALP_ERR_NONE > err)
-    {
-        PRINT("Status[%d]: ", id);
-        modem_print_error(ALP_ITF_TYPE_D7A, err);
-    }
-    
-    if (terminal)
-    {
-        modem_ready[id].release();
-    }
-}
 
 // -----------------------------------------------
 // Sensor Threads
@@ -155,7 +153,6 @@
         .config_file_id = FID_SENSOR_CONFIG_##NAME\
     }
 
-
 SENSOR_THREAD_CTX(mag, MAG, 3);
 SENSOR_THREAD_CTX(acc, ACC, 3);
 SENSOR_THREAD_CTX(gyr, GYR, 3);
@@ -167,17 +164,16 @@
 
 void thread_sensor()
 {
+    int thread_id = sensor_id++;
+    
     FPRINT("(id:0x%08x)\r\n", osThreadGetId());
 
     // To force a first report
     uint32_t last_report_time = 0xFFFFFFFF;
     sensor_thread_ctx_t* ctx = g_thread_ctx;
-    uint8_t user_id = modem_get_id(my_main_callback);
 
     // Get the sensor configuration
-    ram_fs_read(ctx->config_file_id, 0, sizeof(sensor_config_t), (uint8_t*)&(ctx->config));
-    
-    PRINT("Start sensor thread %d\n", user_id);
+    ram_fs_read(ctx->config_file_id, (uint8_t*)&(ctx->config), 0, sizeof(sensor_config_t));
     
     thread_ready.release();
     
@@ -196,11 +192,10 @@
         
         for (uint8_t i = 0; i < ctx->nb_values; i++)
         {
-            if (report_needed(&(ctx->config), ctx->current_value[i], ctx->last_report_value[i], last_report_time, user_id))
+            if (report_needed(&(ctx->config), ctx->current_value[i], ctx->last_report_value[i], last_report_time, thread_id))
             {
                 // Send notification
-                modem_write_file(ctx->value_file_id, ctx->current_value, 0, ctx->data_size, user_id);
-                modem_ready[user_id].acquire();
+                modem_write_file(ctx->value_file_id, ctx->current_value, 0, ctx->data_size);
                 
                 // Update last report value
                 memcpy(ctx->last_report_value, ctx->current_value, ctx->data_size);
@@ -230,28 +225,28 @@
         {
             // If a configuration file has been modified, update the context
             case FID_SENSOR_CONFIG_MAG:
-                ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(mag_thread_ctx.config));
+                ram_fs_read(fid, (uint8_t*)&(mag_thread_ctx.config), 0, sizeof(sensor_config_t));
                 break;
             case FID_SENSOR_CONFIG_ACC:
-                ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(acc_thread_ctx.config));
+                ram_fs_read(fid, (uint8_t*)&(mag_thread_ctx.config), 0, sizeof(sensor_config_t));
                 break;
             case FID_SENSOR_CONFIG_GYR:
-                ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(gyr_thread_ctx.config));
+                ram_fs_read(fid, (uint8_t*)&(gyr_thread_ctx.config), 0, sizeof(sensor_config_t));
                 break;
             case FID_SENSOR_CONFIG_PRE:
-                ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(pre_thread_ctx.config));
+                ram_fs_read(fid, (uint8_t*)&(gyr_thread_ctx.config), 0, sizeof(sensor_config_t));
                 break;
             case FID_SENSOR_CONFIG_HUM:
-                ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(hum_thread_ctx.config));
+                ram_fs_read(fid, (uint8_t*)&(gyr_thread_ctx.config), 0, sizeof(sensor_config_t));
                 break;
             case FID_SENSOR_CONFIG_TEM1:
-                ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(tem1_thread_ctx.config));
+                ram_fs_read(fid, (uint8_t*)&(gyr_thread_ctx.config), 0, sizeof(sensor_config_t));
                 break;
             case FID_SENSOR_CONFIG_TEM2:
-                ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(tem2_thread_ctx.config));
+                ram_fs_read(fid, (uint8_t*)&(gyr_thread_ctx.config), 0, sizeof(sensor_config_t));
                 break;
             case FID_SENSOR_CONFIG_LIGHT:
-                ram_fs_read(fid, 0, sizeof(sensor_config_t), (uint8_t*)&(light_thread_ctx.config));
+                ram_fs_read(fid, (uint8_t*)&(gyr_thread_ctx.config), 0, sizeof(sensor_config_t));
                 break;                
             default:
                 break;
@@ -259,8 +254,6 @@
     }
 }
 
-Queue<void, 8> modem_resp;
-
 // Callback for button
 void my_response_callback(uint8_t terminal, int8_t err, uint8_t id)
 {
@@ -273,13 +266,13 @@
     
     if (terminal)
     {    
-        modem_resp.put((void*)MODEM_RESP_TERMINAL);
+        g_file_modified.put((touch_t*)MODEM_RESP_TERMINAL);
     }
     else
     {
         if (ALP_ERR_NONE == err)
         {
-            modem_resp.put((void*)MODEM_RESP_DONE);
+            g_file_modified.put((touch_t*)MODEM_RESP_DONE);
         }
     }
 }
@@ -292,8 +285,10 @@
     uint32_t resp;
     uint8_t alarm;
     d7a_sp_res_t istat;
-    uint8_t user_id = modem_get_id(my_response_callback);
+    alp_payload_t* alp;
+    alp_payload_t* alp_rsp;
     uint8_t nb = 0;
+    int err;
     
     alp_d7a_itf_t alarm_itf = {
         .type                           = ALP_ITF_TYPE_D7A,
@@ -308,7 +303,7 @@
     };
     
     // Load alarm value
-    ram_fs_read(FID_ALARM, 0, 1, &alarm);
+    ram_fs_read(FID_ALARM, &alarm, 0, 1);
     
     while (true)
     {
@@ -316,24 +311,27 @@
         button_user.acquire();
         
         // load/save value to keep coherence in case of remote access...
-        ram_fs_read(FID_ALARM, 0, 1, &alarm);
+        ram_fs_read(FID_ALARM, &alarm, 0, 1);
 
         // Initial value
         if (alarm != 255)
         {
             // 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);
         
         nb = 0;
-        modem_send_file_content((uint8_t*)&alarm_itf, D7_ITF_SIZE(&alarm_itf), (void*)&istat, FID_ALARM, &alarm, 0, 1, user_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
         {
-            evt = modem_resp.get();
+            evt = g_file_modified.get();
             resp = (evt.status == osEventMessage)? (uint32_t)evt.value.p : MODEM_RESP_NO;
             
             if (MODEM_RESP_DONE == resp)
@@ -353,14 +351,15 @@
         {
             // Toggle alarm state
             alarm = !!alarm;
-            ram_fs_write(FID_ALARM, 0, 1, &alarm);
+            ram_fs_write(FID_ALARM, &alarm, 0, 1);
         }
     }
 }
 
 // Todo for each sensor
-#define SENSOR_SETUP(NAME,name) modem_update_file(FID_SENSOR_VALUE_##NAME, (alp_file_header_t*)&h_sensor_value_##name, NULL);\
-            modem_update_file(FID_SENSOR_CONFIG_##NAME, (alp_file_header_t*)&h_sensor_config_##name, (uint8_t*)&f_sensor_config_##name);\
+#define SENSOR_SETUP(NAME,name) ram_fs_new(FID_SENSOR_VALUE_##NAME, (uint8_t*)&h_sensor_value_##name, (uint8_t*)f_sensor_value_##name); modem_declare_file(FID_SENSOR_VALUE_##NAME, (alp_file_header_t*)&h_sensor_value_##name);\
+            ram_fs_new(FID_SENSOR_CONFIG_##NAME, (uint8_t*)&h_sensor_config_##name, (uint8_t*)&f_sensor_config_##name);\
+            modem_declare_file(FID_SENSOR_CONFIG_##NAME, (alp_file_header_t*)&h_sensor_config_##name);\
             g_thread_ctx = &name##_thread_ctx;\
             Thread th_##name(osPriorityNormal, 1024, NULL);\
             status = th_##name.start(thread_sensor);\
@@ -384,37 +383,26 @@
           
     FPRINT("(id:0x%08x)\r\n", osThreadGetId());
     
-    modem_helper_open(&callbacks);
-    
-    uint8_t main_id = modem_get_id(my_main_callback);
+    modem_open(&callbacks);
     
     PRINT("Register Files\n");
-    modem_update_file(FID_ALARM, &h_alarm, (uint8_t*)&f_alarm);
-
-    // 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, main_id);
-    modem_ready[main_id].acquire();
+    ram_fs_new(FID_ALARM, (uint8_t*)&h_alarm, (uint8_t*)&f_alarm);
+    modem_declare_file(FID_ALARM, (alp_file_header_t*)&h_alarm);
     
-    // Put modem to listen to downlink access class
-    d7a_xcl_t xcl = { .bf.s = 0, .bf.m = 0x1 };
-    modem_write_file(D7A_FID_DLL_CFG, (void*)&xcl, offsetof(d7a_dll_cfg_t, xcl), sizeof(d7a_xcl_t), main_id);
-    modem_ready[main_id].acquire();
+    PRINT("Enable D7A interface\n");
+    modem_d7a_enable_itf();
     
-    modem_flush_file(D7A_FID_DLL_CFG, main_id);
-    modem_ready[main_id].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("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, main_id);
-    modem_ready[main_id].acquire();
     
-    PRINT("Notify Modem Version\n");
-    modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, SIZE_HOST_REV, main_id);
-    modem_ready[main_id].acquire();
+    // Retrieve modem revision
+    PRINT("Send revision\n");
     
-    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);
+    revision_t rev;
+    
+    modem_read_file(FID_WM_REV, &rev, 0, sizeof(revision_t));
     
     // Start file modified thread
     Thread th_file_modified(osPriorityNormal, 1024, NULL);
@@ -483,7 +471,6 @@
     SENSOR_SETUP(LIGHT,light);
 #endif
     
-    modem_free_id(main_id);
     
     // For button
 #ifdef DEBUG_BUTTON