Dash7Board Code Upgrade Protocol demonstration code.
Dependencies: modem_ref_helper CRC
modem_callbacks.cpp@9:d110f2b86831, 2021-02-19 (annotated)
- Committer:
- Jeej
- Date:
- Fri Feb 19 11:00:25 2021 +0000
- Revision:
- 9:d110f2b86831
- Parent:
- 8:6b7d38139b43
v2.0.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jeej | 9:d110f2b86831 | 1 | #include "modem_d7a.h" |
Jeej | 0:5589104abba0 | 2 | #include "files.h" |
Jeej | 0:5589104abba0 | 3 | #include "fast_crc32.h" |
Jeej | 0:5589104abba0 | 4 | |
Jeej | 0:5589104abba0 | 5 | #define SERIAL_MAX_PACKET_SIZE (255) |
Jeej | 0:5589104abba0 | 6 | |
Jeej | 0:5589104abba0 | 7 | uint32_t stream_crc = 0; |
mikl_andre | 3:cfe26c2bb78b | 8 | uint16_t last_end=0; |
Jeej | 0:5589104abba0 | 9 | |
Jeej | 0:5589104abba0 | 10 | // ============================================================}}} |
Jeej | 0:5589104abba0 | 11 | |
Jeej | 0:5589104abba0 | 12 | // Callbacks to MODEM's ALP requests |
Jeej | 0:5589104abba0 | 13 | // ============================================================{{{ |
Jeej | 9:d110f2b86831 | 14 | void my_read(u8 action, u8 fid, u32 offset, u32 length, int id) |
Jeej | 0:5589104abba0 | 15 | { |
Jeej | 0:5589104abba0 | 16 | u8 data[SERIAL_MAX_PACKET_SIZE]; |
Jeej | 0:5589104abba0 | 17 | |
Jeej | 0:5589104abba0 | 18 | ASSERT((ALP_ACTION_RSP_TAG_SIZE + ALP_ACTION_RSP_F_DATA_SIZE(offset, length)) <= SERIAL_MAX_PACKET_SIZE, |
Jeej | 0:5589104abba0 | 19 | "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)); |
Jeej | 0:5589104abba0 | 20 | |
Jeej | 9:d110f2b86831 | 21 | if (ram_fs_read(fid, data, offset, length)) |
Jeej | 0:5589104abba0 | 22 | { |
Jeej | 9:d110f2b86831 | 23 | modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id); |
Jeej | 0:5589104abba0 | 24 | } |
Jeej | 0:5589104abba0 | 25 | else |
Jeej | 0:5589104abba0 | 26 | { |
Jeej | 9:d110f2b86831 | 27 | modem_ref_respond_read(fid, data, offset, length, id); |
Jeej | 0:5589104abba0 | 28 | } |
Jeej | 0:5589104abba0 | 29 | } |
Jeej | 0:5589104abba0 | 30 | |
Jeej | 9:d110f2b86831 | 31 | void my_write(u8 action, u8 fid, void *data, u32 offset, u32 length, int id) |
Jeej | 0:5589104abba0 | 32 | { |
Jeej | 9:d110f2b86831 | 33 | if (!ram_fs_write(fid, (uint8_t*)data, offset, length) || FID_APP_CUP_CODE == fid) |
Jeej | 0:5589104abba0 | 34 | { |
Jeej | 0:5589104abba0 | 35 | touch_t* touch = (touch_t*)MALLOC(sizeof(touch_t)); |
Jeej | 0:5589104abba0 | 36 | |
Jeej | 0:5589104abba0 | 37 | if (FID_APP_CUP_CODE == fid) |
Jeej | 0:5589104abba0 | 38 | { |
Jeej | 0:5589104abba0 | 39 | // Calculate crc on chunk |
mikl_andre | 3:cfe26c2bb78b | 40 | if (offset+length>last_end) |
mikl_andre | 3:cfe26c2bb78b | 41 | { |
mikl_andre | 3:cfe26c2bb78b | 42 | // Only calculate on new chunks, ignore repeated chunks |
mikl_andre | 3:cfe26c2bb78b | 43 | // If we missed some chunks the CRC will be bad anyway |
mikl_andre | 3:cfe26c2bb78b | 44 | stream_crc = crc32_fast(data, length, stream_crc); |
mikl_andre | 3:cfe26c2bb78b | 45 | } |
mikl_andre | 3:cfe26c2bb78b | 46 | last_end=offset+length; |
mikl_andre | 3:cfe26c2bb78b | 47 | |
Jeej | 0:5589104abba0 | 48 | /************************/ |
Jeej | 0:5589104abba0 | 49 | /* Save the chunk data. */ |
Jeej | 0:5589104abba0 | 50 | /************************/ |
Jeej | 8:6b7d38139b43 | 51 | } |
Jeej | 8:6b7d38139b43 | 52 | else if (FID_APP_CUP_CFG == fid) |
mikl_andre | 3:cfe26c2bb78b | 53 | { |
mikl_andre | 3:cfe26c2bb78b | 54 | // Reset last offset when entering CUP mode (and also exiting) |
Jeej | 8:6b7d38139b43 | 55 | last_end = 0; |
Jeej | 0:5589104abba0 | 56 | } |
Jeej | 0:5589104abba0 | 57 | |
Jeej | 0:5589104abba0 | 58 | touch->fid = fid; |
Jeej | 0:5589104abba0 | 59 | touch->offset = offset; |
Jeej | 0:5589104abba0 | 60 | touch->length = length; |
Jeej | 0:5589104abba0 | 61 | |
Jeej | 0:5589104abba0 | 62 | g_file_modified.put(touch); |
Jeej | 0:5589104abba0 | 63 | |
Jeej | 9:d110f2b86831 | 64 | modem_ref_respond(action, ALP_ERR_NONE, id); |
Jeej | 0:5589104abba0 | 65 | } |
Jeej | 0:5589104abba0 | 66 | else |
Jeej | 0:5589104abba0 | 67 | { |
Jeej | 9:d110f2b86831 | 68 | modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id); |
Jeej | 0:5589104abba0 | 69 | } |
Jeej | 0:5589104abba0 | 70 | } |
Jeej | 0:5589104abba0 | 71 | |
Jeej | 9:d110f2b86831 | 72 | void my_read_fprop(u8 action, u8 fid, int id) |
Jeej | 0:5589104abba0 | 73 | { |
Jeej | 9:d110f2b86831 | 74 | alp_file_header_t* hdr = (alp_file_header_t*)ram_fs_get_header(fid); |
Jeej | 9:d110f2b86831 | 75 | |
Jeej | 9:d110f2b86831 | 76 | if (hdr != NULL) |
Jeej | 9:d110f2b86831 | 77 | { |
Jeej | 9:d110f2b86831 | 78 | modem_ref_respond_fprop(fid, hdr, id); |
Jeej | 9:d110f2b86831 | 79 | } |
Jeej | 9:d110f2b86831 | 80 | else |
Jeej | 9:d110f2b86831 | 81 | { |
Jeej | 9:d110f2b86831 | 82 | modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id); |
Jeej | 9:d110f2b86831 | 83 | } |
Jeej | 0:5589104abba0 | 84 | } |
Jeej | 0:5589104abba0 | 85 | |
Jeej | 9:d110f2b86831 | 86 | void my_flush(u8 action, u8 fid, int id) |
Jeej | 8:6b7d38139b43 | 87 | { |
Jeej | 9:d110f2b86831 | 88 | // No flush in this file system |
Jeej | 9:d110f2b86831 | 89 | modem_ref_respond(action, ALP_ERR_NONE, id); |
Jeej | 9:d110f2b86831 | 90 | } |
Jeej | 9:d110f2b86831 | 91 | |
Jeej | 9:d110f2b86831 | 92 | void my_delete(u8 action, u8 fid, int id) |
Jeej | 9:d110f2b86831 | 93 | { |
Jeej | 9:d110f2b86831 | 94 | modem_ref_respond(action, ALP_ERR_FILE_NOT_FOUND, id); |
Jeej | 9:d110f2b86831 | 95 | } |
Jeej | 9:d110f2b86831 | 96 | |
Jeej | 9:d110f2b86831 | 97 | void my_udata(alp_payload_t* alp) |
Jeej | 9:d110f2b86831 | 98 | { |
Jeej | 9:d110f2b86831 | 99 | alp_payload_print(alp); |
Jeej | 0:5589104abba0 | 100 | } |
Jeej | 0:5589104abba0 | 101 | |
Jeej | 0:5589104abba0 | 102 | void my_lqual(u8 ifid, int per) |
Jeej | 0:5589104abba0 | 103 | { |
Jeej | 0:5589104abba0 | 104 | PRINT("Interface File [%3d] LQUAL : %d%% PER\r\n", ifid, per); |
Jeej | 0:5589104abba0 | 105 | } |
Jeej | 0:5589104abba0 | 106 | |
Jeej | 0:5589104abba0 | 107 | void my_ldown(u8 ifid) |
Jeej | 0:5589104abba0 | 108 | { |
Jeej | 0:5589104abba0 | 109 | PRINT("Interface File [%3d] LDOWN\r\n", ifid); |
Jeej | 0:5589104abba0 | 110 | } |
Jeej | 0:5589104abba0 | 111 | |
Jeej | 0:5589104abba0 | 112 | void my_reset(void) |
Jeej | 0:5589104abba0 | 113 | { |
Jeej | 0:5589104abba0 | 114 | PRINT("Restarting application...\r\n"); |
Jeej | 0:5589104abba0 | 115 | FLUSH(); |
Jeej | 0:5589104abba0 | 116 | NVIC_SystemReset(); |
Jeej | 0:5589104abba0 | 117 | } |
Jeej | 0:5589104abba0 | 118 | |
Jeej | 0:5589104abba0 | 119 | void my_boot(u8 cause, u16 number) |
Jeej | 0:5589104abba0 | 120 | { |
Jeej | 0:5589104abba0 | 121 | PRINT("Modem BOOT[%c] #%d\r\n", cause, number); |
Jeej | 0:5589104abba0 | 122 | |
Jeej | 0:5589104abba0 | 123 | // Modem re-booted, restart APP |
Jeej | 0:5589104abba0 | 124 | my_reset(); |
Jeej | 1:dd4e18b267a1 | 125 | } |
Jeej | 1:dd4e18b267a1 | 126 | |
Jeej | 1:dd4e18b267a1 | 127 | void my_busy(u8 busy) |
Jeej | 1:dd4e18b267a1 | 128 | { |
Jeej | 1:dd4e18b267a1 | 129 | if (busy) |
Jeej | 1:dd4e18b267a1 | 130 | { |
Jeej | 1:dd4e18b267a1 | 131 | PRINT("Modem Busy\r\n"); |
Jeej | 1:dd4e18b267a1 | 132 | |
Jeej | 2:49acddddec25 | 133 | /* Stop report, do not use modem */ |
Jeej | 1:dd4e18b267a1 | 134 | /* Wait for modem reboot or modem not busy */ |
Jeej | 1:dd4e18b267a1 | 135 | } |
Jeej | 1:dd4e18b267a1 | 136 | else |
Jeej | 1:dd4e18b267a1 | 137 | { |
Jeej | 1:dd4e18b267a1 | 138 | PRINT("Modem not Busy\r\n"); |
Jeej | 1:dd4e18b267a1 | 139 | |
Jeej | 2:49acddddec25 | 140 | /* Resume reports */ |
Jeej | 1:dd4e18b267a1 | 141 | } |
Jeej | 0:5589104abba0 | 142 | } |