
Dash7Board Code Upgrade Protocol demonstration code.
Dependencies: modem_ref_helper CRC
Revision 9:d110f2b86831, committed 2021-02-19
- Comitter:
- Jeej
- Date:
- Fri Feb 19 11:00:25 2021 +0000
- Parent:
- 8:6b7d38139b43
- Commit message:
- v2.0.0
Changed in this revision
diff -r 6b7d38139b43 -r d110f2b86831 cup_app.cpp --- a/cup_app.cpp Thu May 28 09:11:58 2020 +0000 +++ b/cup_app.cpp Fri Feb 19 11:00:25 2021 +0000 @@ -1,4 +1,4 @@ -#include "modem_ref_helper.h" +#include "modem_d7a.h" #include "cup_app.h" #include "cup.h" #include "ram_fs.h" @@ -15,7 +15,7 @@ uint8_t cup_cfg_bcast_init(void) { cup_cfg_bcast_header_t h; - ram_fs_read(FID_APP_CUP_CFG_BCAST, offsetof(cup_cfg_bcast_t, header), sizeofof(cup_cfg_bcast_t, header), (uint8_t*)&h); + ram_fs_read(FID_APP_CUP_CFG_BCAST, (uint8_t*)&h, offsetof(cup_cfg_bcast_t, header), sizeofof(cup_cfg_bcast_t, header)); PRINT("CUP BCAST START 0x%08x CHUNK %d LEN %d CMD 0x%04X TO %d SIG 0x%08x\r\n", h.start, h.chunk, h.len, h.cmd, h.to, h.sig_new); @@ -32,7 +32,7 @@ h.sig_curr = h.sig_new; h.missed = KAL_DIV_CEILING(h.len, h.chunk); h.crc_ok = 0; - ram_fs_write(FID_APP_CUP_CFG_BCAST, 0, sizeof(cup_cfg_bcast_header_t), (uint8_t*)&h); + ram_fs_write(FID_APP_CUP_CFG_BCAST, (uint8_t*)&h, 0, sizeof(cup_cfg_bcast_header_t)); PRINT("CUP BCAST RESET, MISSED %d\r\n", h.missed); } else @@ -59,7 +59,7 @@ { // update header cup_cfg_bcast_header_t h; - ram_fs_read(FID_APP_CUP_CFG_BCAST, offsetof(cup_cfg_bcast_t, header), sizeofof(cup_cfg_bcast_t, header), (uint8_t*)&h); + ram_fs_read(FID_APP_CUP_CFG_BCAST, (uint8_t*)&h, offsetof(cup_cfg_bcast_t, header), sizeofof(cup_cfg_bcast_t, header)); // Update the status only when a valid command is present if (((h.cmd == CUP_CMD_UPGRADE_UPLOAD) || (h.cmd == CUP_CMD_UPGRADE_UPLOAD_ALT) || (h.cmd == CUP_CMD_UPGRADE_FILE_START)) @@ -76,12 +76,12 @@ { // read-modify-write one bit u8 bmp, bit = (1 << (idx & 7)); - ram_fs_read(FID_APP_CUP_CFG_BCAST, fof, 1, &bmp); + ram_fs_read(FID_APP_CUP_CFG_BCAST, &bmp, fof, 1); if (!(bmp & bit)) { h.missed--; bmp |= bit; - ram_fs_write(FID_APP_CUP_CFG_BCAST, fof, 1, &bmp); + ram_fs_write(FID_APP_CUP_CFG_BCAST, &bmp, fof, 1); } // CRC check when done or on the last chunk @@ -94,7 +94,7 @@ PRINT("CUP BCAST CRC=%08x / Expect %08x\r\n", stream_crc, h.sig_curr); } - ram_fs_write(FID_APP_CUP_CFG_BCAST, 0, sizeof(cup_cfg_bcast_header_t), (uint8_t*)&h); + ram_fs_write(FID_APP_CUP_CFG_BCAST, (uint8_t*)&h, 0, sizeof(cup_cfg_bcast_header_t)); } } }
diff -r 6b7d38139b43 -r d110f2b86831 files.cpp --- a/files.cpp Thu May 28 09:11:58 2020 +0000 +++ b/files.cpp Fri Feb 19 11:00:25 2021 +0000 @@ -26,9 +26,9 @@ /// FW_ID | MAJOR | MINOR | PATCH | HASH | // 1B | 1B | 1B | 2B | 4B | .fw_version.id = 0, - .fw_version.major = 1, + .fw_version.major = 2, .fw_version.minor = 0, - .fw_version.patch = 5, + .fw_version.patch = 0, .fw_version.hash = 0x20200528, /// Maximum size for CUP code .cup_max_size = CUP_FILE_MAX_SIZE
diff -r 6b7d38139b43 -r d110f2b86831 main.cpp --- a/main.cpp Thu May 28 09:11:58 2020 +0000 +++ b/main.cpp Fri Feb 19 11:00:25 2021 +0000 @@ -1,7 +1,7 @@ // @autor: jeremie@wizzilab.com // @date: 2017-12-14 -#include "modem_ref_helper.h" +#include "modem_d7a.h" #include "modem_callbacks.h" #include "files.h" #include "crc.h" @@ -9,16 +9,13 @@ #define CHUNK_SIZE (128) -Semaphore modem_ready(0); Queue<touch_t, 8> g_file_modified; // CRC calculated on the incoming data stream extern uint32_t stream_crc; extern int32_t last_end; -const uint8_t default_root_key[] = DEFAULT_ROOT_KEY; - -modem_callbacks_t callbacks = { +modem_ref_callbacks_t callbacks = { .read = my_read, .write = my_write, .read_fprop = my_read_fprop, @@ -32,22 +29,6 @@ .busy = my_busy }; -// Callback -void my_main_callback(uint8_t terminal, int8_t err, uint8_t id) -{ - (void)id; - - if (ALP_ERR_NONE > err) - { - modem_print_error(ALP_ITF_TYPE_D7A, err); - } - - if (terminal) - { - modem_ready.release(); - } -} - void thread_file_modified() { touch_t* touch; @@ -57,8 +38,6 @@ uint32_t next_chunk = 0; uint32_t chunks=0; - uint8_t id = modem_get_id(my_main_callback); - PRINT("Ready\n"); while (true) @@ -73,7 +52,7 @@ { 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); + ram_fs_read(FID_APP_CUP_CFG_BCAST, (uint8_t*)&cup_cfg, 0, sizeof(cup_cfg_bcast_header_t)); if (CUP_CMD_UPGRADE_FILE_START == cup_cfg.cmd) { @@ -94,8 +73,7 @@ .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(); + modem_write_file(FID_CUP_CFG, (uint8_t*)&cup_start, 0, 4); } else { @@ -114,7 +92,7 @@ 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); + ram_fs_read(touch->fid, data, touch->offset, touch->length); if (CUP_CMD_UPGRADE_FILE_END == cup_cfg.cmd) { @@ -147,8 +125,7 @@ // Notify modem code file to reset upload watchdog // else the device will exit upload mode after the watchdog timeout - modem_notify_file(FID_CUP_CODE, 0, 1, id); - modem_ready.acquire(); + modem_notify_file(FID_CUP_CODE, 0, 1); break; } @@ -174,7 +151,7 @@ "---------------- Demo CUP ---------------\n" "-----------------------------------------\n"); - modem_helper_open(&callbacks); + modem_open(&callbacks); PRINT("--------------- APP infos ---------------\r\n"); PRINT(" - Manufacturer ID: %08X\r\n", f_rev.manufacturer_id); @@ -184,32 +161,39 @@ PRINT(" - CUP max size: %d\r\n", f_rev.cup_max_size); PRINT("-----------------------------------------\r\n"); - uint8_t id = modem_get_id(my_main_callback); - 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); + ram_fs_new(FID_APP_CUP_CFG, (uint8_t*)&h_cup_cfg, (uint8_t*)&f_cup_cfg); + ram_fs_new(FID_APP_CUP_CFG_BCAST, (uint8_t*)&h_cup_cfg_bcast, (uint8_t*)&f_cup_cfg_bcast); + modem_declare_file(FID_APP_CUP_CFG, (alp_file_header_t*)&h_cup_cfg); + modem_declare_file(FID_APP_CUP_CFG_BCAST, (alp_file_header_t*)&h_cup_cfg_bcast); // Declare the cup code file // It needs a special handling of its data in my_write callback since we want to keep them. // (can't be done with the current RAM file system) - modem_declare_file(FID_APP_CUP_CODE, (alp_file_header_t*)&h_cup_code, true, id); - modem_ready.acquire(); + modem_declare_file(FID_APP_CUP_CODE, (alp_file_header_t*)&h_cup_code); - 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(); + + // Host revision file is in the modem. Update it. + PRINT("Update host revision\n"); + modem_write_file(FID_HOST_REV, &f_rev, 0, sizeof(revision_t)); - PRINT("Notify Modem Version\n"); - modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, sizeof(revision_t), id); - modem_ready.acquire(); + // Retrieve modem revision + PRINT("Send revision\n"); + revision_t rev; + modem_read_file(FID_WM_REV, &rev, 0, sizeof(revision_t)); - PRINT("Notify Host Version\n"); - modem_notify_host_rev(&f_rev, &h_rev, (uint8_t*)default_root_key); + // Send both to the server + // Build payload + alp_payload_t* alp = NULL; + alp = alp_payload_rsp_f_data(alp, FID_WM_REV, &rev, 0, sizeof(revision_t)); + alp = alp_payload_rsp_f_data(alp, FID_HOST_REV, &f_rev, 0, sizeof(revision_t)); - // id no longer needed - modem_free_id(id); + // Send + alp_itf_d7a_cfg_t report_itf; + modem_read_file(IFID_REPORT, &report_itf, 0, sizeof(alp_itf_d7a_cfg_t)); + modem_remote_raw_alp((void*)&report_itf, alp, NULL, 10000); // Start file modified thread Thread th_file_modified(osPriorityNormal, 1024, NULL);
diff -r 6b7d38139b43 -r d110f2b86831 modem_callbacks.cpp --- a/modem_callbacks.cpp Thu May 28 09:11:58 2020 +0000 +++ b/modem_callbacks.cpp Fri Feb 19 11:00:25 2021 +0000 @@ -1,4 +1,4 @@ -#include "modem_ref_helper.h" +#include "modem_d7a.h" #include "files.h" #include "fast_crc32.h" @@ -11,26 +11,26 @@ // Callbacks to MODEM's ALP requests // ============================================================{{{ -void my_read(u8 fid, u32 offset, u32 length, int id) +void my_read(u8 action, u8 fid, u32 offset, u32 length, int id) { u8 data[SERIAL_MAX_PACKET_SIZE]; ASSERT((ALP_ACTION_RSP_TAG_SIZE + ALP_ACTION_RSP_F_DATA_SIZE(offset, length)) <= SERIAL_MAX_PACKET_SIZE, "Read response too big for serial protocol (%d/%dmax)\r\n", length, ALP_ACTION_RSP_TAG_SIZE + ALP_ACTION_RSP_F_DATA_SIZE(offset,SERIAL_MAX_PACKET_SIZE)); - if (ram_fs_read(fid, offset, length, data)) + if (ram_fs_read(fid, data, offset, length)) { - modem_respond(ALP_ERR_FILE_NOT_FOUND, id); + modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id); } else { - modem_respond_read(fid, data, offset, length, id); + modem_ref_respond_read(fid, data, offset, length, id); } } -void my_write(u8 fid, void *data, u32 offset, u32 length, int id) +void my_write(u8 action, u8 fid, void *data, u32 offset, u32 length, int id) { - if (!ram_fs_write(fid, offset, length, (uint8_t*)data) || FID_APP_CUP_CODE == fid) + if (!ram_fs_write(fid, (uint8_t*)data, offset, length) || FID_APP_CUP_CODE == fid) { touch_t* touch = (touch_t*)MALLOC(sizeof(touch_t)); @@ -61,92 +61,42 @@ g_file_modified.put(touch); - modem_respond(ALP_ERR_NONE, id); + modem_ref_respond(action, ALP_ERR_NONE, id); } else { - modem_respond(ALP_ERR_FILE_NOT_FOUND, id); - } -} - -void my_read_fprop(u8 fid, int id) -{ - u8* hdr = (u8*)ram_fs_get_header(fid); - - if (hdr != NULL) - { - modem_respond_fprop(fid, hdr, id); - } - else - { - modem_respond(ALP_ERR_FILE_NOT_FOUND, id); + modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id); } } -void my_flush(u8 fid, int id) +void my_read_fprop(u8 action, u8 fid, int id) { - // No flush in this file system - modem_respond(ALP_ERR_NONE, id); -} - -void my_delete(u8 fid, int id) -{ - modem_respond(ALP_ERR_FILE_NOT_FOUND, id); + alp_file_header_t* hdr = (alp_file_header_t*)ram_fs_get_header(fid); + + if (hdr != NULL) + { + modem_ref_respond_fprop(fid, hdr, id); + } + else + { + modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id); + } } -void my_udata(void *data, u32 length) +void my_flush(u8 action, u8 fid, int id) { - uint8_t* p = (uint8_t*)data; - int32_t rem = length; - alp_parsed_chunk_t r; - d7a_sp_res_t* istat; - - do { - uint32_t parsed = alp_parse_chunk(&p, &r); - if (!parsed) - { - // Discard the payload in case of parsing error. - PRINT("Parsing error!\r\n"); - break; - } - rem -= parsed; - - switch (r.type) - { - // Interface status - case ALP_OPCODE_RSP_ISTATUS: - // D7A Interface - if (ALP_ITF_TYPE_D7A == r.meta.itf.type) - { - union { - u8 b[8]; - u32 w[2]; - } uid; - - // ISTATUS can come either alone or together with ALP_OPCODE_RSP_F_DATA - // but there should be only one per payload, moreover it will come first - istat = (d7a_sp_res_t*)r.data; - memcpy(uid.b,istat->addressee.id,8); - - PRINT("Got accessed by UID:%08X%08X SNR: %3ddB RXLEV: -%-3ddBm LB: %3ddB\n", - HAL_U32_BYTE_SWAP(uid.w[0]), HAL_U32_BYTE_SWAP(uid.w[1]), - istat->snr, istat->rxlev, istat->lb); - } - else - { - PRINT("Got accessed by unknown Interface 0x%02X\n", r.meta.itf.type); - } - break; - // Data return - case ALP_OPCODE_RSP_F_DATA: - // RSP_F_DATA can come either alone or together with ISTATUS - PRINT("Got UNS File[%3d]@%d %d Bytes\n", r.meta.f_data.fid, r.meta.f_data.offset, r.meta.f_data.length); - break; - default: - PRINT("Untreated OPCODE %d\n", r.type); - break; - } - } while (rem > 0); + // No flush in this file system + modem_ref_respond(action, ALP_ERR_NONE, id); +} + +void my_delete(u8 action, u8 fid, int id) +{ + modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id); +} + +void my_udata(alp_payload_t* alp) +{ + alp_payload_print(alp); } void my_lqual(u8 ifid, int per)
diff -r 6b7d38139b43 -r d110f2b86831 modem_callbacks.h --- a/modem_callbacks.h Thu May 28 09:11:58 2020 +0000 +++ b/modem_callbacks.h Fri Feb 19 11:00:25 2021 +0000 @@ -1,11 +1,11 @@ #include "hal_types.h" -void my_read(u8 fid, u32 offset, u32 length, int id); -void my_write(u8 fid, void *data, u32 offset, u32 length, int id); -void my_read_fprop(u8 fid, int id); -void my_flush(u8 fid, int id); -void my_delete(u8 fid, int id); -void my_udata(void *data, u32 length); +void my_read(u8 action, u8 fid, u32 offset, u32 length, int id); +void my_write(u8 action, u8 fid, void *data, u32 offset, u32 length, int id); +void my_read_fprop(u8 action, u8 fid, int id); +void my_flush(u8 action, u8 fid, int id); +void my_delete(u8 action, u8 fid, int id); +void my_udata(alp_payload_t* alp); void my_lqual(u8 ifid, int per); void my_ldown(u8 ifid); void my_reset(void);
diff -r 6b7d38139b43 -r d110f2b86831 modem_d7a.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/modem_d7a.lib Fri Feb 19 11:00:25 2021 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/WizziLab/code/modem_ref_helper/#15bf99fcfed9
diff -r 6b7d38139b43 -r d110f2b86831 modem_ref_helper.lib --- a/modem_ref_helper.lib Thu May 28 09:11:58 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://developer.mbed.org/teams/WizziLab/code/modem_ref_helper/#243e0ef07efb