Dash7Board Code Upgrade Protocol demonstration code.

Dependencies:   modem_ref_helper CRC

Revision:
8:6b7d38139b43
Parent:
7:bfe920ee44f2
Child:
9:d110f2b86831
--- a/main.cpp	Tue Aug 20 13:55:45 2019 +0000
+++ b/main.cpp	Thu May 28 09:11:58 2020 +0000
@@ -5,6 +5,7 @@
 #include "modem_callbacks.h"
 #include "files.h"
 #include "crc.h"
+#include "cup_app.h"
 
 #define CHUNK_SIZE (128)
 
@@ -36,7 +37,7 @@
 {
     (void)id;
     
-    if (ALP_ERR_NONE != err)
+    if (ALP_ERR_NONE > err)
     {
         modem_print_error(ALP_ITF_TYPE_D7A, err);
     }
@@ -69,54 +70,56 @@
         switch (touch->fid)
         {
             case FID_APP_CUP_CFG_BCAST:
+            {
                 cup_cfg_bcast_header_t cup_cfg;
                 
                 ram_fs_read(FID_APP_CUP_CFG_BCAST, 0, sizeof(cup_cfg_bcast_header_t), (uint8_t*)&cup_cfg);
                         
                 if (CUP_CMD_UPGRADE_FILE_START == cup_cfg.cmd)
                 {
-                    PRINT("Enter upload mode. Watchdog %ds\n", cup_cfg.to);
-                    
-                    tim.stop();
-                    tim.reset();
-                    total       = 0;
-                    next_chunk  = 0;
-                    stream_crc  = 0;
-                    chunks      = 0;
-                    
-                    // Write to the modem CUP config file (as root)
-                    // to enter upload mode
-                    cup_cfg_t cup_start = {
-                        .cmd = CUP_CMD_UPGRADE_UPLOAD,
-                        .arch_nb = cup_cfg.to,
-                        };
-                    modem_write_file_root(FID_CUP_CFG, (uint8_t*)&cup_start, 0, 4, (uint8_t*)default_root_key, id);
-                    modem_ready.acquire();
-                }
-                else if (CUP_CMD_UPGRADE_FILE_END == cup_cfg.cmd)
-                {
-                    float time_s = tim.read();
-                    uint32_t expected_size = cup_cfg.len;
-                    uint32_t expected_crc = cup_cfg.sig_new;
-                
-                    PRINT("\nDone. %d/%d bytes received in %d chunks in %.2f sec (%.2f kb/s)\r\n"
-                          "Expected CRC 0x%08X. Stream CRC 0x%08X.\r\n",
-                            total, expected_size, chunks, time_s, (double)(total/time_s)/1024.0,
-                            expected_crc, stream_crc);
-                    
-                    tim.stop();
-                    
-                    // Compare expected crc with received data crc
-                    if (stream_crc == expected_crc)
+                    if (cup_cfg_bcast_init())
                     {
-                        PRINT("Received complete binary.\n");
+                        PRINT("Enter upload mode. Watchdog %ds\n", cup_cfg.to);
+                        
+                        tim.stop();
+                        tim.reset();
+                        total       = 0;
+                        next_chunk  = 0;
+                        stream_crc  = 0;
+                        chunks      = 0;
+                        
+                        // Write to the modem CUP config file (as root)
+                        // to enter upload mode
+                        cup_cfg_t cup_start = {
+                            .cmd = CUP_CMD_UPGRADE_UPLOAD,
+                            .arch_nb = cup_cfg.to,
+                            };
+                        modem_write_file_root(FID_CUP_CFG, (uint8_t*)&cup_start, 0, 4, (uint8_t*)default_root_key, id);
+                        modem_ready.acquire();
                     }
                     else
                     {
-                        PRINT("Missed or corrupted data in this session.\n");
+                        PRINT("Failed bcast init\n");
                     }
-                    
-                    
+                }
+                else
+                {
+                    PRINT("CUP command 0x%04X\n", cup_cfg.cmd);
+                }
+               
+                break;
+            }
+            case FID_APP_CUP_CFG:
+            {
+                cup_cfg_t cup_cfg = { 0 };
+                uint8_t* data = (uint8_t*)((uint8_t*)&cup_cfg + touch->offset);
+                
+                ram_fs_read(touch->fid, touch->offset, touch->length, data);
+                
+                if (CUP_CMD_UPGRADE_FILE_END == cup_cfg.cmd)
+                {
+                    PRINT("Done.\n");
+
                     // Actual check is done here since we could have received some of the missed data in a previous session.
                     
                     /******************************/
@@ -133,36 +136,14 @@
                 {
                     PRINT("CUP command 0x%04X\n", cup_cfg.cmd);
                 }
-               
+                
                 break;
+            }
             case FID_APP_CUP_CODE:
+            {
+                cup_cfg_bcast_update(touch->offset, touch->length);
+
                 // Chunk data should be saved in my_write callback
-                //PRINT("Got CUP code chunk length %d offset %d\n", touch->length, touch->offset);
-                chunks++;
-                
-                if (!next_chunk)
-                {
-                    // First chunk, start timer
-                    tim.start();
-                    PRINT("Receiving CUP code chunks-> ");
-                }
-                
-                if (next_chunk < touch->offset)
-                {
-                    PRINT("\nMissed %d chunk(s) at offset %d (got offset %d)\n", (touch->offset - next_chunk) / CHUNK_SIZE, next_chunk, touch->offset);
-                    next_chunk = touch->offset + touch->length;
-                    total += touch->length;
-                }
-                else if (next_chunk == touch->offset)
-                {
-                    next_chunk = touch->offset + touch->length;
-                    total += touch->length;
-                    PRINT(".");
-                }
-                else
-                {
-                     //PRINT("Repeat chunk(s) at offset %d\n", touch->offset);
-                }
                 
                 // Notify modem code file to reset upload watchdog
                 // else the device will exit upload mode after the watchdog timeout
@@ -170,6 +151,7 @@
                 modem_ready.acquire();
                 
                 break;
+            }
             default:
                 PRINT("TOUCH FID %d OFF %d LEN %d\n", touch->fid, touch->offset, touch->length);
                 break;
@@ -206,6 +188,7 @@
 
     PRINT("Register Files\n");
     modem_update_file(FID_HOST_REV, (alp_file_header_t*)&h_rev, (uint8_t*)&f_rev);
+    modem_update_file(FID_APP_CUP_CFG, (alp_file_header_t*)&h_cup_cfg, (uint8_t*)&f_cup_cfg);
     modem_update_file(FID_APP_CUP_CFG_BCAST, (alp_file_header_t*)&h_cup_cfg_bcast, (uint8_t*)&f_cup_cfg_bcast);
     
     // Declare the cup code file
@@ -214,17 +197,12 @@
     modem_declare_file(FID_APP_CUP_CODE, (alp_file_header_t*)&h_cup_code, true, id);
     modem_ready.acquire();
     
-    PRINT("Setup URCs\n");
-    // Busy URC to know if modem enters CUP mode
-    modem_enable_urc(ALP_URC_TYPE_BUSY, 0, 0, true, id);
-    modem_ready.acquire();
-    
     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("Notify Modem Version\n");
-    modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, SIZE_HOST_REV, id);
+    modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, sizeof(revision_t), id);
     modem_ready.acquire();
     
     PRINT("Notify Host Version\n");