Send file data demo.

Dependencies:   modem_ref_helper CRC DebouncedInterrupt

Revision:
20:5856f9bfd8e3
Parent:
19:68f3a84dbc8f
--- a/main.cpp	Thu May 28 09:13:44 2020 +0000
+++ b/main.cpp	Fri Oct 29 13:11:38 2021 +0000
@@ -2,15 +2,16 @@
 // @date: 2017-05-02
 
 #include "DebouncedInterrupt.h"
-#include "modem_ref_helper.h"
+#include "modem_d7a.h"
 #include "modem_callbacks.h"
 #include "files.h"
+#include "crc.h"
 
 
 Semaphore button_user(0);
 Semaphore modem_ready(0);
 Semaphore user_ready(0);
-Queue<void, 8> modem_resp;
+Queue<touch_t, 8> modem_resp;
 
 uint8_t id, g_report_id;
 
@@ -66,17 +67,17 @@
     
     if (terminal)
     {
-        modem_resp.put((void*)MODEM_RESP_TERMINAL);
+        modem_resp.put((touch_t*)MODEM_RESP_TERMINAL);
     }
     else
     {
         if (ALP_ERR_NONE == err)
         {
-            modem_resp.put((void*)MODEM_RESP_ACK);
+            modem_resp.put((touch_t*)MODEM_RESP_ACK);
         }
         else if (ALP_ERR_NONE > err)
         {
-            modem_resp.put((void*)MODEM_RESP_ERROR);
+            modem_resp.put((touch_t*)MODEM_RESP_ERROR);
         }
         else
         {
@@ -90,96 +91,93 @@
 
 void button_user_thread()
 {
+    touch_t* touch;
     FPRINT("(id:0x%08x)\r\n", osThreadGetId());
     
     osEvent evt;
     uint32_t resp;
     uint8_t alarm;
-    uint8_t id = modem_get_id(my_main_callback);
+    uint8_t id = modem_ref_get_id(my_main_callback);
     d7a_sp_res_t istat;
+    alp_payload_t* alp;
+    alp_payload_t* alp_rsp;
     uint8_t nb = 0;
+    int err;
     
     memset(&istat, 0, sizeof(d7a_sp_res_t));
     
     // Load alarm value
-    ram_fs_read(FID_ALARM, 0, 1, &alarm);
+    ram_fs_read(FID_ALARM, &alarm, 0, 1);
     
-    // Send initial value
-    modem_send_file_content((uint8_t*)&alarm_itf, D7_ITF_SIZE(&alarm_itf), (void*)&istat, FID_ALARM, &alarm, 0, 1, id);
-    modem_ready.acquire();
-    
-    // Change callback
-    modem_free_id(id);
-    id = modem_get_id(my_response_callback);
     
     while (true)
     {
+        
         // Wait for button press
         PRINT("PRESS BUTTON TO SEND ALARM...\r\n");
         button_user.acquire();
         
+        
+        
         nb = 0;
         
         // load/save value to keep choerency in case of remote access...
-        ram_fs_read(FID_ALARM, 0, 1, &alarm);
+        ram_fs_read(FID_ALARM, &alarm, 0, 1);
 
         // 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);
         
-        // Send data
-        modem_send_file_content((uint8_t*)&alarm_itf, D7_ITF_SIZE(&alarm_itf), (void*)&istat, FID_ALARM, &alarm, 0, 1, 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
+        if (err < ALP_ERR_NONE)
+        {
+            PRINT("Timeout.\n");
+        }
+        else
         {
-            // Wait for callback
-            evt = modem_resp.get(3000);
-            resp = (evt.status == osEventMessage)? (uint32_t)evt.value.p : MODEM_RESP_TIMEOUT;
-                        
-            if (MODEM_RESP_ACK == resp)
-            {
-                // Print metadata
-                PRINT("ACK %d: ", ++nb);
-                PRINT_DATA("UID:", "%02X", istat.addressee.id, 8, " ");
-                PRINT("SNR: %ddB RXLEV: %ddBm LB: %ddB\n", istat.snr, -istat.rxlev, istat.lb);
+            err = alp_payload_get_err(alp_rsp);
+            PRINT("err %d\n", err);
+            modem_print_error(alarm_itf.type, err);
+        }
                 
-                // Clear istatus buffer
-                memset(&istat, 0, sizeof(d7a_sp_res_t));
-                
-                // Resume processing
-                user_ready.release();
-            }
-            else if (MODEM_RESP_TIMEOUT == resp)
+        do {
+            nb++;
+                    
+            alp = alp_payload_extract(&alp_rsp, ALP_OPCODE_RSP_ISTATUS);
+    
+            if (alp)
             {
-                // Could be because of the RF duty cycle,
-                // especialy after boot where the duty credit is near 0.
-                // The only thing to do is wait.
-                // The transmission will resume once some duty credit is available.
-                PRINT("WAITING...\n");
-            }
-            else if (MODEM_RESP_ERROR == resp)
-            {
-                PRINT("ERROR.\n");
-                
-                // Resume processing
-                user_ready.release();
+                alp_parsed_chunk_t r;
+                u8* p = alp->d;
+                    
+                alp_parse_chunk(&p, &r);
+                memcpy(&istat, r.data, r.meta.itf.length);
+                        
+                // Print metadata
+                PRINT("ACK %d: ", nb);
+                PRINT_DATA("UID:", "%02X", istat.addressee.id, 8, " ");
+                PRINT("snr:%d rxlev:%d lb:%d \n", istat.snr, -istat.rxlev, istat.lb);
+                        
+                alp_payload_free(alp);
             }
-            else if (MODEM_RESP_TERMINAL == resp)
+            else
             {
-                PRINT("DONE.\n");
-                
-                // Resume processing
-                user_ready.release();
+                break;
             }
+                    
+                FLUSH();
+            } while (1);
+            PRINT("Done.\n");
+        }
+}
 
-        } while (MODEM_RESP_TERMINAL != resp);
-        
-        PRINT("BUTTON ALARM DONE\r\n");
-    }
-}
 
 // Misc
 // ============================================================{{{
@@ -189,7 +187,7 @@
     memcpy(hdr, ram_fs_get_header(fid), sizeof(alp_file_header_t));
 }
 
-modem_callbacks_t callbacks = {
+modem_ref_callbacks_t callbacks = {
     .read       = my_read,
     .write      = my_write,
     .read_fprop = my_read_fprop,
@@ -220,29 +218,32 @@
               
     FPRINT("(id:0x%08x)\r\n", osThreadGetId());
     
-    modem_helper_open(&callbacks);
+    modem_open(&callbacks);
     
-    uint8_t id = modem_get_id(my_main_callback);
+    uint8_t id = modem_ref_get_id(my_main_callback);
     
     PRINT("Register Files\n");
     // 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.
-    modem_update_file(FID_ALARM, (alp_file_header_t*)&h_alarm, (uint8_t*)&f_alarm);
+    ram_fs_new(FID_ALARM, (uint8_t*)&h_alarm, (uint8_t*)&f_alarm);
+    modem_declare_file(FID_ALARM, (alp_file_header_t*)&h_alarm);
     
-    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, id);
-    modem_ready.acquire();
+    
+    PRINT("Enable D7A interface\n");
+    modem_d7a_enable_itf();
     
-    PRINT("Notify Modem Version\n");
-    modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, SIZE_HOST_REV, id);
-    modem_ready.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("Notify FW Version\n");
-    uint8_t default_root_key[16] = DEFAULT_ROOT_KEY;
-    modem_notify_host_rev(&f_rev, &h_rev, default_root_key);
+    // Retrieve modem revision
+    PRINT("Send revision\n");
     
-    modem_free_id(id);
+    revision_t rev;
+    
+    modem_read_file(FID_WM_REV, &rev, 0, sizeof(revision_t));
 
 #ifdef DEBUG_BUTTON
     DebouncedInterrupt user_interrupt(DEBUG_BUTTON);