Send file data through D7A Action Protocol demo.

Dependencies:   modem_ref_helper

Revision:
0:3058da317f01
Child:
3:e905bf88e8a9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu May 11 14:49:14 2017 +0000
@@ -0,0 +1,341 @@
+// @autor: jeremie@wizzilab.com
+// @date: 2017-05-02
+
+#include "mbed.h"
+#include "rtos.h"
+#include "WizziDebug.h"
+#include "WizziCom.h"
+
+#include "hwcfg.h"
+#include "files.h"
+#include "ram_fs.h"
+#include "modem_callbacks.h"
+
+#include "revision.h"
+#include "alp_spec.h"
+#include "alp_helpers.h"
+#include "modem_ref.h"
+#include "kal_fs.h"
+#include "d7a_1x.h"
+#include "alp.h"
+
+#include "sensor.h"
+
+WizziCom* g_modem_com;
+Semaphore modem_ready(0);
+sensor_config_t g_light_config;
+Queue<void, 8> g_file_modified;
+
+uint8_t g_main_id, g_report_id;
+
+// Check parameters to see if data should be send
+static bool report_needed(sensor_config_t* cfg, int32_t value, int32_t last_value, uint32_t last_report_time)
+{
+    switch (cfg->report_type)
+    {
+        case REPORT_ALWAYS:
+            // Send a report at each measure
+            IPRINT("Report always\r\n");
+            return true;
+        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) >= cfg->max_diff && cfg->max_diff)
+            {
+                IPRINT("Report on difference (last:%d new:%d max_diff:%d)\r\n", last_value, value, cfg->max_diff);
+                return true;
+            }
+            break;
+        case REPORT_ON_THRESHOLD:
+            // Send a report when crossing a threshold
+            if (   (value >= cfg->threshold_high && last_value < cfg->threshold_high)
+                || (value <= cfg->threshold_low  && last_value > cfg->threshold_low)
+                || (value < cfg->threshold_high  && last_value >= cfg->threshold_high)
+                || (value > cfg->threshold_low   && last_value <= cfg->threshold_low))
+            {
+                IPRINT("Rerport on threshold (last:%d new:%d th:%d tl:%d)\r\n", last_value, value, cfg->threshold_high, cfg->threshold_low);
+                return true;
+            }
+            break;
+        default:
+            break;
+    }
+    
+    // Send a report if it's been more than max_period since the last report
+    if (((last_report_time/1000) >= cfg->max_period) && cfg->max_period)
+    {
+        IPRINT("Report on period (max_period:%d time:%d)\r\n", cfg->max_period, last_report_time);
+        return true;
+    }
+
+    return false;
+}
+
+void thread_sensor_light()
+{
+    uint8_t light_level;
+    uint8_t light_level_old = 0;
+    
+    // To force a first report
+    uint32_t last_report_time = 0xFFFFFFFF;
+    
+    FPRINT("(id:0x%08x)\r\n", osThreadGetId());
+    
+    // Get the sensor configuration
+    ram_fs_read(FID_SENSOR_CONFIG, 0, SIZE_SENSOR_CONFIG, (uint8_t*)&g_light_config);
+    
+    while (true)
+    {
+        light_level = sensor_get_light();
+        
+        PRINT("Light %d\r\n", light_level);
+                
+        if (report_needed(&g_light_config, light_level, light_level_old, last_report_time))
+        {
+            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_ready.wait();
+        
+            // Update 
+            light_level_old = light_level;
+            last_report_time = 0;
+        }
+        
+        // Update last report time
+        last_report_time += g_light_config.read_period;
+        
+        Thread::wait(g_light_config.read_period);
+    }
+}
+
+void thread_file_modified()
+{
+    uint8_t fid;
+    osEvent evt;
+    
+    while (true)
+    {
+        evt = g_file_modified.get();
+        fid = (evt.status == osEventMessage)? (uint8_t)(uint32_t)evt.value.p : NULL;
+        
+        switch (fid)
+        {
+            case FID_SENSOR_CONFIG:
+                // Update sensor configuration
+                ram_fs_read(FID_SENSOR_CONFIG, 0, SIZE_SENSOR_CONFIG, (uint8_t*)&g_light_config);
+                PRINT("Sensor configuration updated\r\n");
+                break;
+            default:
+            break;
+        }
+    }
+}
+
+// Misc
+// ============================================================{{{
+
+void my_get_alp_file_props(uint8_t fid, alp_file_header_t* hdr)
+{
+    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;
+    }
+}
+
+// ============================================================}}}
+
+// Serial adapters to WizziLab's own architecture
+// ============================================================{{{
+
+void my_serial_input(WizziCom* com, WizziComPacket_t* pkt)
+{
+    modem_input(wizzicom_type_to_flow(pkt->type), pkt->data, pkt->length);
+    FREE(pkt);
+}
+
+int my_serial_send(uint8_t* data1, uint8_t size1, uint8_t* data2, uint8_t size2)
+{
+    (void)size1;
+    
+    // Retrieve Flow ID from header and send packet 
+    g_modem_com->send((WizziComPacketType)wizzicom_flow_to_type(data1[4]), size2, data2);
+
+    return (size1 + size2);
+}
+
+modem_callbacks_t callbacks = {
+    .read       = my_read,
+    .write      = my_write,
+    .read_fprop = my_read_fprop,
+    .flush      = my_flush,
+    .remove     = my_delete,
+    .lqual      = my_lqual,
+    .ldown      = my_ldown,
+    .reset      = my_reset,
+    .boot       = my_boot
+};
+
+// 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()
+{
+    // Start & initialize
+    DBG_OPEN(DEBUG_LED);
+    PRINT("\r\n--- Starting new run ---\r\n");
+    FPRINT("(id:0x%08x)\r\n", osThreadGetId());
+    
+    alp_file_header_t hdr;
+    static union {
+        uint8_t      b[8];
+        uint32_t     w[2];
+    } uid;
+    revision_t rev;
+        
+    // Hardware reset
+    DigitalOut reset_low(MODEM_PIN_RESET, 0);
+    Thread::wait(100);
+        
+    // Release reset
+    DigitalIn reset_release(MODEM_PIN_RESET);
+    Thread::wait(1000);
+    
+    // Open modem Com port
+    g_modem_com = new WizziCom(MODEM_PIN_TX, MODEM_PIN_RX, MODEM_PIN_IRQ_OUT, MODEM_PIN_IRQ_IN);
+    
+    // Redirect All Port traffic to my_serial_input
+    g_modem_com->attach(my_serial_input, WizziComPacketOther);
+
+    // Open driver
+    modem_open(my_serial_send, &callbacks);
+    
+    g_main_id = modem_get_id(my_main_callback);
+
+    DPRINT("Start Modem Process (id=%d)\n", g_main_id);
+    Thread::wait(1000);
+    
+    modem_read_file(0, (uint8_t*)&uid.b[0], 0, 8, g_main_id);
+    modem_ready.wait();
+    
+    modem_read_file(2, (uint8_t*)&rev, 0, sizeof(revision_t), g_main_id);
+    modem_ready.wait();
+    
+    PRINT("------------ D7A Modem infos ------------\r\n");
+    PRINT_DATA(" - UID:              ", "%02X", uid.b, 8, "\r\n");
+    PRINT(" - Manufacturer ID:  %08X\r\n", rev.manufacturer_id);
+    PRINT(" - Device ID:        %08X\r\n", rev.device_id);
+    PRINT(" - Hardware version: %08X\r\n", rev.hw_version);
+    PRINT(" - Firmware version: v%d.%d.%d\r\n", rev.fw_version.major, rev.fw_version.minor, rev.fw_version.patch);
+    PRINT(" - File system CRC:  0x%08x\r\n", rev.fs_crc);
+    PRINT("-----------------------------------------\r\n");
+    
+    //DPRINT("UID: %08X%08X\n", HAL_U32_BYTE_SWAP(uid.w[0]), HAL_U32_BYTE_SWAP(uid.w[1]));
+
+    DPRINT("Register Files\n");
+    // HOST Revision is a local file. Uses D7AActP Notification.
+    ram_fs_new(FID_HOST_REV, (uint8_t*)&h_rev, (uint8_t*)&f_rev);
+    my_get_alp_file_props(FID_HOST_REV, &hdr);
+    modem_declare_file(FID_HOST_REV, &hdr, g_main_id);
+    modem_ready.wait();
+
+    // Create report file on modem.
+    modem_delete_file(FID_SENSOR_LIGHT, g_main_id);
+    modem_ready.wait();
+    ram_fs_new(FID_SENSOR_LIGHT, (uint8_t*)&h_sensor_light, NULL);
+    my_get_alp_file_props(FID_SENSOR_LIGHT, &hdr);
+    modem_create_file(FID_SENSOR_LIGHT, &hdr, g_main_id);
+    modem_ready.wait();
+    
+    // Allow remote access.
+    modem_delete_file(FID_SENSOR_CONFIG, g_main_id);
+    modem_ready.wait();
+    ram_fs_new(FID_SENSOR_CONFIG, (uint8_t*)&h_sensor_config, (uint8_t*)&f_sensor_config);
+    my_get_alp_file_props(FID_SENSOR_CONFIG, &hdr);
+    modem_declare_file(FID_SENSOR_CONFIG, &hdr, g_main_id);
+    modem_ready.wait();
+
+    // 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_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_ready.wait();
+    
+    PRINT("Notify Modem Version\n");
+    modem_notify_file(FID_MODEM_REV, 0, SIZE_HOST_REV, g_main_id);
+    modem_ready.wait();
+    
+    PRINT("Notify FW Version\n");
+    modem_notify_file(FID_HOST_REV, 0, SIZE_HOST_REV, g_main_id);
+    modem_ready.wait();
+    
+    // Start file modified thread
+    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);
+    
+    // Start light measure thread
+    Thread th_sensor_light(osPriorityNormal, 1024, NULL);
+    status = th_sensor_light.start(thread_sensor_light);
+    ASSERT(status == osOK, "Failed to start thread_sensor_light (err: %d)\r\n", status);
+
+#ifdef DEBUG_LED
+    DigitalOut my_led(DEBUG_LED);
+#endif
+    
+    // Set main task to lowest priority
+    osThreadSetPriority(osThreadGetId(), osPriorityIdle);
+    while(true)
+    {
+        Thread::wait(500);
+#ifdef DEBUG_LED
+        my_led = !my_led;
+#endif
+    }
+}
\ No newline at end of file