Scan example on D7-LoRa

Dependencies:   modem_ref_helper DebouncedInterrupt

Revision:
0:6703784ff93e
Child:
1:27716ee59ca4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri May 25 16:10:45 2018 +0000
@@ -0,0 +1,280 @@
+// @autor: jeremie@wizzilab.com
+// @date: 2017-05-02
+
+#include "DebouncedInterrupt.h"
+#include "modem_ref_helper.h"
+#include "modem_callbacks.h"
+
+#define MY_POLICY_IDX           0
+
+Semaphore button_user(0);
+Semaphore modem_ready(0);
+Queue<void, 8> modem_resp;
+
+enum {
+    MODEM_RESP_NO,
+    MODEM_RESP_TERMINAL,
+    MODEM_RESP_DONE,
+};
+
+uint8_t g_main_id;
+    
+d7a_xcl_t d7lora_dl_xcl = { .bf.s = 8, .bf.m = 1 };
+d7a_xcl_t d7lora_ul_xcl = { .bf.s = 9, .bf.m = 1 };
+
+alp_retry_policy_t my_policy = {
+    .meta.procedure     = 0,
+    .meta.respond       = true,
+    .meta.persistant    = false,
+    .meta.bulk          = false,
+    .depth              = 1,
+    .retries            = 0,
+    .slot_time          = 0
+};
+
+alp_d7a_itf_t my_itf = {
+    .type                           = ALP_ITF_TYPE_D7A,
+    .cfg.to                         = 0,
+    .cfg.te                         = 0,
+    .cfg.qos.bf.resp                = D7A_RESP_ALL,
+    .cfg.qos.bf.retry               = MY_POLICY_IDX,
+    .cfg.addressee.ctrl.bf.nls      = D7A_NLS_AES_CCM_64,
+    .cfg.addressee.ctrl.bf.idf      = D7A_ID_NBID,
+    .cfg.addressee.xcl              = d7lora_ul_xcl,
+    .cfg.addressee.id[0]            = D7A_CTF_VAL(4,0),
+};
+
+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;
+    }
+}
+
+// Callback for broadcast read
+void my_read_response_callback(uint8_t terminal, int8_t err, uint8_t id)
+{
+    (void)id;
+    
+    if (terminal)
+    {    
+        print_status(err);
+        modem_resp.put((void*)MODEM_RESP_TERMINAL);
+    }
+    else
+    {
+        print_resp(err);
+        if (ALP_ERR_NONE == err)
+        {
+            modem_resp.put((void*)MODEM_RESP_DONE);
+        }
+    }
+}
+
+// Interrupt Service Routine on button press.
+void button_push_isr( void )
+{
+    button_user.release();
+}
+
+void button_user_thread()
+{
+    FPRINT("(id:0x%08x)\r\n", osThreadGetId());
+
+    osEvent evt;
+    uint32_t resp;
+    d7a_sp_res_t istat;
+    fw_version_t fw_ver;
+    uint8_t nb = 0;
+
+    uint8_t my_user_id = modem_get_id(my_read_response_callback);
+        
+    uint8_t scan_xcl[] = { d7lora_dl_xcl.byte, d7lora_ul_xcl.byte };
+    
+    memset(&istat, 0, sizeof(d7a_sp_res_t));
+    memset(&fw_ver, 0, sizeof(fw_version_t));
+    
+    while (true)
+    {
+        // Wait for button press
+        PRINT("Press button to scan...\r\n");
+        button_user.wait();
+        
+        for (uint8_t i = 0; i < sizeof(scan_xcl); i++)
+        {
+            nb = 0;
+            my_itf.cfg.addressee.xcl.byte = scan_xcl[i];
+            
+            PRINT("Scanning XCL 0x%02X...\n", my_itf.cfg.addressee.xcl.byte);
+            
+            modem_remote_read_file((uint8_t*)&my_itf, D7_ITF_SIZE(&my_itf), (void*)&istat, D7A_FID_FIRMWARE_VERSION, (void*)&fw_ver, 12, sizeof(fw_version_t), my_user_id);
+            
+            do
+            {
+                evt = modem_resp.get();
+                resp = (evt.status == osEventMessage)? (uint32_t)evt.value.p : MODEM_RESP_NO;
+                
+                if (MODEM_RESP_DONE == resp)
+                {
+                    nb++;
+                    PRINT("%d: XCL:%02X ", nb, istat.addressee.xcl.byte);
+                    PRINT_DATA("UID:", "%02X", istat.addressee.id, 8, " ");
+                    PRINT("snr:%d rxlev:%d lb:%d ", istat.snr, istat.rxlev, istat.lb);
+                    PRINT("v%d.%d.%d\n", fw_ver.major, fw_ver.minor, fw_ver.patch);
+                    
+                    // Reset variables
+                    memset(&istat, 0, sizeof(d7a_sp_res_t));
+                    memset(&fw_ver, 0, sizeof(fw_version_t));
+                }
+
+            } while (MODEM_RESP_TERMINAL != resp);
+        }
+        
+        PRINT("Done.\n");
+    }
+}
+
+modem_callbacks_t callbacks = {
+    .read       = my_read,
+    .write      = my_write,
+    .read_fprop = my_read_fprop,
+    .flush      = my_flush,
+    .remove     = my_delete,
+    .udata      = my_udata,
+    .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
+#ifdef DEBUG_LED
+    DBG_OPEN(DEBUG_LED);
+#else
+    DBG_OPEN(NC);
+#endif
+    PRINT("\n"
+          "-----------------------------------------\n"
+          "--------- Demo ActiveRFIDReader ---------\n"
+          "-----------------------------------------\n");
+          
+    FPRINT("(id:0x%08x)\r\n", osThreadGetId());
+    
+    modem_helper_open(&callbacks);
+    
+    g_main_id = modem_get_id(my_main_callback);
+    
+    // Put modem to listen to downlink access class
+    modem_write_file(D7A_FID_DLL_CFG, &d7lora_dl_xcl, offsetof(d7a_dll_cfg_t, xcl), sizeof(d7a_xcl_t), g_main_id);
+    modem_ready.wait();
+    
+    // Configure interfaces to use uplink access class
+    modem_write_file(FID_D7A_ITF0, &d7lora_ul_xcl, offsetof(alp_d7a_itf_t, cfg.addressee.xcl), sizeof(d7a_xcl_t), g_main_id);
+    modem_ready.wait();
+    
+    modem_write_file(FID_D7A_ITF1, &d7lora_ul_xcl, offsetof(alp_d7a_itf_t, cfg.addressee.xcl), sizeof(d7a_xcl_t), g_main_id);
+    modem_ready.wait();
+    
+    modem_write_file(FID_D7A_ITF2, &d7lora_ul_xcl, offsetof(alp_d7a_itf_t, cfg.addressee.xcl), sizeof(d7a_xcl_t), g_main_id);
+    modem_ready.wait();
+    
+    modem_write_file(FID_D7A_ITF3, &d7lora_ul_xcl, offsetof(alp_d7a_itf_t, cfg.addressee.xcl), sizeof(d7a_xcl_t), g_main_id);
+    modem_ready.wait();
+    
+    // Set custom retry policy
+    // XXX Won't work the first time as we need to reboot the modem for the changes to be applied
+    // Check current retry policy
+    alp_retry_policy_t old;
+    modem_read_file(WM_FID_ALP_CFG, &old, MY_POLICY_IDX * sizeof(alp_retry_policy_t), sizeof(alp_retry_policy_t), g_main_id);
+    modem_ready.wait();
+    if (memcmp(&old, &my_policy, sizeof(alp_retry_policy_t)))
+    {
+        // Update retry policy
+        modem_write_file(WM_FID_ALP_CFG, &my_policy, MY_POLICY_IDX * sizeof(alp_retry_policy_t), sizeof(alp_retry_policy_t), g_main_id);
+        modem_ready.wait();
+        modem_flush_file(WM_FID_ALP_CFG, g_main_id);
+        modem_ready.wait();
+        
+        // Reboot
+        PRINT("Rebooting to apply new retry policy\n");
+        my_reset();
+    }
+    
+    // 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 | ALP_D7A_ISTAT_UNS, true, 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);
+#else
+    #error You need a button to use this APP as is
+#endif
+
+#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