WizziLab / Mbed OS D7A_1x_demo_send_file_data Featured

Dependencies:   modem_ref_helper CRC DebouncedInterrupt

Revision:
0:065f2318fcb9
Child:
1:f4a7a48a5f2b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed May 03 11:31:11 2017 +0000
@@ -0,0 +1,256 @@
+// @autor: jeremie@wizzilab.com
+// @date: 2017-05-02
+
+#include "mbed.h"
+#include "rtos.h"
+#include "DebouncedInterrupt.h"
+#include "WizziDebug.h"
+#include "WizziCom.h"
+
+#include "hwcfg.h"
+#include "files.h"
+#include "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"
+
+
+WizziCom* g_modem_com;
+Semaphore button_user(0);
+Semaphore modem_ready(0);
+
+uint8_t g_main_id, g_report_id;
+
+TYPEDEF_STRUCT_PACKED {
+    uint8_t type;
+    d7a_sp_cfg_t cfg;
+} alp_d7a_itf_t;
+
+#define D7A_CTF_VAL(mant,exp)   ((uint8_t)(mant|(exp<<5)))
+#define ALP_ITF_TYPE_D7A        0xD7
+alp_d7a_itf_t alarm_itf = {
+    .type                           = ALP_ITF_TYPE_D7A,
+    .cfg.to                         = 0,
+    .cfg.te                         = 0,
+    .cfg.qos.bf.record              = 0,
+    .cfg.qos.bf.stop_on_err         = 0,
+    .cfg.qos.bf.resp                = D7A_RESP_PREFERRED,
+    .cfg.addressee.ctrl.bf.nls      = D7A_NLS_AES_CCM_64,
+    .cfg.addressee.ctrl.bf.idf      = D7A_ID_NBID,
+    .cfg.addressee.xcl.bf           = {.s = 0x2, .m = 0x1},// XXX D7A_XCL_GW,
+    .cfg.addressee.id[0]            = D7A_CTF_VAL(1,1),
+    .cfg.qos.bf.retry               = 1 // XXX WM_RPOL_RARE_SINGLE_CHECK,
+};
+
+#define MY_D7_ITF_SIZE(_itf) (1+my_alp_itf_d7a_cfg_size(&(_itf)->cfg))
+int my_alp_itf_d7a_cfg_size(d7a_sp_cfg_t* cfg)
+{
+    int size = sizeof(d7a_sp_cfg_t) - sizeof(d7a_addressee_t);
+    size += D7A_ADDR_LEN(cfg->addressee.ctrl);
+    return size;
+}
+
+// Interrupt Service Routine on button press.
+void button_push_isr( void )
+{
+    button_user.release();
+}
+
+void button_user_thread()
+{
+    uint8_t alarm;
+    
+    FPRINT("(id:0x%08x)\r\n", osThreadGetId());
+    
+    // Load alarm value
+    fs_read(FID_ALARM, 0, 1, &alarm);
+    
+    // Send initial value
+    modem_send_file_content((uint8_t*)&alarm_itf, MY_D7_ITF_SIZE(&alarm_itf), FID_ALARM, &alarm, 0, 1, g_main_id);
+    modem_ready.wait();
+    
+    while (true)
+    {
+        // Wait for button press
+        button_user.wait();
+        
+        // load/save value to keep choerency in case of remote access...
+        fs_read(FID_ALARM, 0, 1, &alarm);
+
+        // Toggle alarm state
+        alarm = !alarm;
+        
+        fs_write(FID_ALARM, 0, 1, &alarm);
+        
+        PRINT("BUTTON ALARM %d\r\n", alarm);
+                    
+        modem_send_file_content((uint8_t*)&alarm_itf, MY_D7_ITF_SIZE(&alarm_itf), FID_ALARM, &alarm, 0, 1, g_main_id);
+        modem_ready.wait();
+    }
+}
+
+// Misc
+// ============================================================{{{
+
+void my_get_alp_file_props(uint8_t fid, alp_file_header_t* hdr)
+{
+    memcpy(hdr, fs_get_header(fid), sizeof(alp_file_header_t));
+}
+
+void print_status(int status)
+{
+    switch (status)
+    {
+        case ALP_ERR_NONE:
+            DPRINT("Status: OK\n");
+            break;
+        case ALP_ERR_FILE_EXIST:
+            DPRINT("Status: Already registered\n");
+            break;
+        default:
+            DPRINT("Status: error %d\n", status);
+            break;
+    }
+}
+
+// ============================================================}}}
+
+// Serial adapters to WizziLab's own architecture
+// ============================================================{{{
+
+void 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;
+    
+    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(int8_t err, uint8_t id)
+{
+    (void)id;
+    
+    print_status(err);
+    
+    modem_ready.release();
+}
+
+/*** 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;
+    
+    // Add files to the local file system
+    fs_new(FID_HOST_REV, (uint8_t*)&h_rev, (uint8_t*)&f_rev);
+    fs_new(FID_ALARM, (uint8_t*)&h_alarm, (uint8_t*)&f_alarm);
+    
+    // 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 HST/MODEM Port traffic to serial_input
+    g_modem_com->attach(serial_input, WizziComPacketAlpCmd);
+    g_modem_com->attach(serial_input, WizziComPacketAlpResp);
+    g_modem_com->attach(serial_input, WizziComPacketAlpUns);
+    g_modem_com->attach(serial_input, WizziComPacketAlpErr);
+    g_modem_com->attach(serial_input, WizziComPacketCmd);
+    g_modem_com->attach(serial_input, WizziComPacketSysReset);
+    g_modem_com->attach(serial_input, WizziComPacketSysButton);
+    g_modem_com->attach(serial_input, WizziComPacketSysInfo);
+    g_modem_com->attach(serial_input, WizziComPacketSysCup);
+    g_modem_com->attach(serial_input, WizziComPacketSysPing);
+    g_modem_com->attach(serial_input, WizziComPacketSysPong);
+    g_modem_com->attach(serial_input, WizziComPacketSysConfig);
+    g_modem_com->attach(serial_input, WizziComPacketSysTlev);
+    g_modem_com->attach(serial_input, WizziComPacketSysRedir);
+
+    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();
+    
+    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.
+    my_get_alp_file_props(FID_HOST_REV, &hdr);
+    modem_declare_file(FID_HOST_REV, &hdr, g_main_id);
+    modem_ready.wait();
+
+    // Button/Alarm is a local file. As we want to check the outcome of sending
+    // this, don't use D7AActP Notification but rather plain ALP ITF Forwarding.
+    // Declaration just allows remote access.
+    my_get_alp_file_props(FID_ALARM, &hdr);
+    modem_declare_file(FID_ALARM, &hdr, g_main_id);
+    modem_ready.wait();
+
+    // Configure URC: LQUAL on report file notification every 10 reports
+    DPRINT("Setup URCs\n");
+    modem_enable_urc(ALP_URC_TYPE_LQUAL, IFID_REPORT, 10, true, g_main_id);
+    modem_ready.wait();
+    
+    DPRINT("Notify FW Version\n");
+    modem_notify_file(FID_HOST_REV, 0, SIZE_HOST_REV, g_main_id);
+    modem_ready.wait();
+
+#ifdef DEBUG_BUTTON
+    DebouncedInterrupt user_interrupt(DEBUG_BUTTON);
+    user_interrupt.attach(button_push_isr, IRQ_FALL, 500, true);
+    
+    Thread but_th(osPriorityNormal, 2048, NULL);
+    osStatus status = but_th.start(button_user_thread);
+    ASSERT(status == osOK, "Failed to start but thread (err: %d)\r\n", status);
+#endif
+
+#ifdef DEBUG_LED
+    DigitalOut my_led(DEBUG_LED);
+#endif
+    
+    // Set main task to lowest priority
+    osThreadSetPriority(osThreadGetId(), osPriorityIdle);
+    while(true)
+    {
+        Thread::wait(5000);
+#ifdef DEBUG_LED
+        my_led = !my_led;
+#endif
+    }
+}
\ No newline at end of file