Sending and reception of big data file (1kB example)
Dependencies: modem_ref_helper CRC DebouncedInterrupt
main.cpp@1:76a8a3cc5f2e, 2017-12-14 (annotated)
- Committer:
- Jeej
- Date:
- Thu Dec 14 18:25:58 2017 +0000
- Revision:
- 1:76a8a3cc5f2e
- Parent:
- 0:a2bbc478f812
- Child:
- 6:33dfecc85ebf
First version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jeej | 0:a2bbc478f812 | 1 | // @autor: jeremie@wizzilab.com |
Jeej | 0:a2bbc478f812 | 2 | // @date: 2017-12-14 |
Jeej | 0:a2bbc478f812 | 3 | |
Jeej | 0:a2bbc478f812 | 4 | #include "DebouncedInterrupt.h" |
Jeej | 0:a2bbc478f812 | 5 | #include "modem_ref_helper.h" |
Jeej | 0:a2bbc478f812 | 6 | #include "modem_callbacks.h" |
Jeej | 0:a2bbc478f812 | 7 | #include "files.h" |
Jeej | 0:a2bbc478f812 | 8 | #include "crc.h" |
Jeej | 0:a2bbc478f812 | 9 | |
Jeej | 0:a2bbc478f812 | 10 | #define MIN(a,b) ((a<b)?a:b) |
Jeej | 0:a2bbc478f812 | 11 | |
Jeej | 0:a2bbc478f812 | 12 | #define MY_POLICY_IDX 0 |
Jeej | 0:a2bbc478f812 | 13 | #define CHUNK_SIZE 128 |
Jeej | 0:a2bbc478f812 | 14 | |
Jeej | 0:a2bbc478f812 | 15 | Semaphore button_user(0); |
Jeej | 0:a2bbc478f812 | 16 | Semaphore modem_ready(0); |
Jeej | 0:a2bbc478f812 | 17 | Queue<void, 8> modem_resp[MAX_USER_NB]; |
Jeej | 0:a2bbc478f812 | 18 | Queue<touch_t, 8> g_file_modified; |
Jeej | 0:a2bbc478f812 | 19 | |
Jeej | 0:a2bbc478f812 | 20 | enum { |
Jeej | 0:a2bbc478f812 | 21 | MODEM_RESP_NO, |
Jeej | 0:a2bbc478f812 | 22 | MODEM_RESP_TERMINAL, |
Jeej | 0:a2bbc478f812 | 23 | MODEM_RESP_ERROR, |
Jeej | 0:a2bbc478f812 | 24 | MODEM_RESP_ACK, |
Jeej | 0:a2bbc478f812 | 25 | MODEM_RESP_TIMEOUT, |
Jeej | 0:a2bbc478f812 | 26 | }; |
Jeej | 0:a2bbc478f812 | 27 | |
Jeej | 1:76a8a3cc5f2e | 28 | // This discribes the retry policy of the stack for each packet |
Jeej | 1:76a8a3cc5f2e | 29 | // Do not modify uncommented parameters |
Jeej | 1:76a8a3cc5f2e | 30 | // After changing this, reboot the modem to apply it |
Jeej | 0:a2bbc478f812 | 31 | alp_retry_policy_t my_policy = { |
Jeej | 0:a2bbc478f812 | 32 | .meta.procedure = 0, |
Jeej | 0:a2bbc478f812 | 33 | .meta.respond = true, |
Jeej | 0:a2bbc478f812 | 34 | .meta.persistant = false, |
Jeej | 0:a2bbc478f812 | 35 | .meta.bulk = false, |
Jeej | 0:a2bbc478f812 | 36 | .depth = 1, |
Jeej | 1:76a8a3cc5f2e | 37 | .retries = 2, // The stack will retry 2 times (each packet will be sent a maximum of 3 times) |
Jeej | 1:76a8a3cc5f2e | 38 | .slot_time = 1, // Interval between retries in seconds |
Jeej | 0:a2bbc478f812 | 39 | }; |
Jeej | 0:a2bbc478f812 | 40 | |
Jeej | 1:76a8a3cc5f2e | 41 | // This describe the upload interface |
Jeej | 1:76a8a3cc5f2e | 42 | // Do not modify uncommented parameters |
Jeej | 0:a2bbc478f812 | 43 | alp_d7a_itf_t my_itf = { |
Jeej | 0:a2bbc478f812 | 44 | .type = ALP_ITF_TYPE_D7A, |
Jeej | 0:a2bbc478f812 | 45 | .cfg.to = 0, |
Jeej | 0:a2bbc478f812 | 46 | .cfg.te = 0, |
Jeej | 0:a2bbc478f812 | 47 | .cfg.qos.bf.resp = D7A_RESP_PREFERRED, |
Jeej | 0:a2bbc478f812 | 48 | .cfg.qos.bf.retry = MY_POLICY_IDX, |
Jeej | 0:a2bbc478f812 | 49 | .cfg.qos.bf.record = 0, |
Jeej | 0:a2bbc478f812 | 50 | .cfg.qos.bf.stop_on_err = 0, |
Jeej | 1:76a8a3cc5f2e | 51 | .cfg.addressee.ctrl.bf.nls = D7A_NLS_AES_CCM_64, // Security level |
Jeej | 0:a2bbc478f812 | 52 | .cfg.addressee.ctrl.bf.idf = D7A_ID_NBID, |
Jeej | 1:76a8a3cc5f2e | 53 | .cfg.addressee.xcl.bf = {.s = 0x2, .m = 0x1}, // Gateway access class |
Jeej | 0:a2bbc478f812 | 54 | .cfg.addressee.id[0] = 4, |
Jeej | 0:a2bbc478f812 | 55 | }; |
Jeej | 0:a2bbc478f812 | 56 | |
Jeej | 0:a2bbc478f812 | 57 | void print_resp(uint8_t id, int status) |
Jeej | 0:a2bbc478f812 | 58 | { |
Jeej | 0:a2bbc478f812 | 59 | switch (status) |
Jeej | 0:a2bbc478f812 | 60 | { |
Jeej | 0:a2bbc478f812 | 61 | case ALP_ERR_NONE: |
Jeej | 0:a2bbc478f812 | 62 | //PRINT("Resp[%d]: OK\n", id); |
Jeej | 0:a2bbc478f812 | 63 | break; |
Jeej | 0:a2bbc478f812 | 64 | case ALP_ERR_FILE_EXIST: |
Jeej | 0:a2bbc478f812 | 65 | PRINT("Resp[%d]: Already registered\n", id); |
Jeej | 0:a2bbc478f812 | 66 | break; |
Jeej | 0:a2bbc478f812 | 67 | default: |
Jeej | 0:a2bbc478f812 | 68 | PRINT("Resp[%d]: error %d\n", id, status); |
Jeej | 0:a2bbc478f812 | 69 | break; |
Jeej | 0:a2bbc478f812 | 70 | } |
Jeej | 0:a2bbc478f812 | 71 | } |
Jeej | 0:a2bbc478f812 | 72 | |
Jeej | 1:76a8a3cc5f2e | 73 | // Response Callback |
Jeej | 0:a2bbc478f812 | 74 | void my_response_callback(uint8_t terminal, int8_t err, uint8_t id) |
Jeej | 0:a2bbc478f812 | 75 | { |
Jeej | 0:a2bbc478f812 | 76 | (void)id; |
Jeej | 0:a2bbc478f812 | 77 | |
Jeej | 0:a2bbc478f812 | 78 | print_resp(id, err); |
Jeej | 0:a2bbc478f812 | 79 | |
Jeej | 0:a2bbc478f812 | 80 | if (terminal) |
Jeej | 0:a2bbc478f812 | 81 | { |
Jeej | 0:a2bbc478f812 | 82 | modem_resp[id].put((void*)MODEM_RESP_TERMINAL); |
Jeej | 0:a2bbc478f812 | 83 | } |
Jeej | 0:a2bbc478f812 | 84 | else |
Jeej | 0:a2bbc478f812 | 85 | { |
Jeej | 0:a2bbc478f812 | 86 | if (ALP_ERR_NONE == err) |
Jeej | 0:a2bbc478f812 | 87 | { |
Jeej | 0:a2bbc478f812 | 88 | modem_resp[id].put((void*)MODEM_RESP_ACK); |
Jeej | 0:a2bbc478f812 | 89 | } |
Jeej | 0:a2bbc478f812 | 90 | else |
Jeej | 0:a2bbc478f812 | 91 | { |
Jeej | 0:a2bbc478f812 | 92 | modem_resp[id].put((void*)MODEM_RESP_ERROR); |
Jeej | 0:a2bbc478f812 | 93 | } |
Jeej | 0:a2bbc478f812 | 94 | } |
Jeej | 0:a2bbc478f812 | 95 | } |
Jeej | 0:a2bbc478f812 | 96 | |
Jeej | 0:a2bbc478f812 | 97 | // Interrupt Service Routine on button press. |
Jeej | 0:a2bbc478f812 | 98 | void button_push_isr( void ) |
Jeej | 0:a2bbc478f812 | 99 | { |
Jeej | 0:a2bbc478f812 | 100 | button_user.release(); |
Jeej | 0:a2bbc478f812 | 101 | } |
Jeej | 0:a2bbc478f812 | 102 | |
Jeej | 1:76a8a3cc5f2e | 103 | // Upload thread |
Jeej | 0:a2bbc478f812 | 104 | void button_user_thread() |
Jeej | 0:a2bbc478f812 | 105 | { |
Jeej | 0:a2bbc478f812 | 106 | osEvent evt; |
Jeej | 0:a2bbc478f812 | 107 | uint32_t resp; |
Jeej | 0:a2bbc478f812 | 108 | d7a_sp_res_t istat; |
Jeej | 1:76a8a3cc5f2e | 109 | output_file_t* output; |
Jeej | 0:a2bbc478f812 | 110 | uint32_t sent = 0; |
Jeej | 0:a2bbc478f812 | 111 | |
Jeej | 0:a2bbc478f812 | 112 | uint8_t id = modem_get_id(my_response_callback); |
Jeej | 0:a2bbc478f812 | 113 | |
Jeej | 0:a2bbc478f812 | 114 | memset(&istat, 0, sizeof(d7a_sp_res_t)); |
Jeej | 0:a2bbc478f812 | 115 | |
Jeej | 1:76a8a3cc5f2e | 116 | // Directly get data pointer to avoid reading file |
Jeej | 1:76a8a3cc5f2e | 117 | output = (output_file_t*)ram_fs_get_data(FID_OUTPUT_FILE); |
Jeej | 1:76a8a3cc5f2e | 118 | |
Jeej | 1:76a8a3cc5f2e | 119 | // Update file CRC (Calculate CRC on string without end of string) |
Jeej | 1:76a8a3cc5f2e | 120 | output->crc = crc32((char*)output->data, strlen((char*)output->data)); |
Jeej | 1:76a8a3cc5f2e | 121 | |
Jeej | 0:a2bbc478f812 | 122 | while (true) |
Jeej | 0:a2bbc478f812 | 123 | { |
Jeej | 0:a2bbc478f812 | 124 | Timer tim; |
Jeej | 0:a2bbc478f812 | 125 | bool is_ok; |
Jeej | 1:76a8a3cc5f2e | 126 | uint32_t output_length = strlen((char*)output->data) + 1; // +1 as data is a string, send end of string |
Jeej | 0:a2bbc478f812 | 127 | |
Jeej | 0:a2bbc478f812 | 128 | // Wait for button press |
Jeej | 1:76a8a3cc5f2e | 129 | PRINT("Press user button to send file. (%d bytes)\n", output_length); |
Jeej | 0:a2bbc478f812 | 130 | button_user.wait(); |
Jeej | 0:a2bbc478f812 | 131 | |
Jeej | 1:76a8a3cc5f2e | 132 | // Update file CRC (Calculate CRC on string without end of string) |
Jeej | 1:76a8a3cc5f2e | 133 | output->crc = crc32((char*)output->data, output_length - 1); |
Jeej | 1:76a8a3cc5f2e | 134 | |
Jeej | 0:a2bbc478f812 | 135 | sent = 0; |
Jeej | 0:a2bbc478f812 | 136 | tim.start(); |
Jeej | 0:a2bbc478f812 | 137 | |
Jeej | 0:a2bbc478f812 | 138 | // Send chunks |
Jeej | 1:76a8a3cc5f2e | 139 | while (sent < output_length) |
Jeej | 0:a2bbc478f812 | 140 | { |
Jeej | 0:a2bbc478f812 | 141 | is_ok = false; |
Jeej | 1:76a8a3cc5f2e | 142 | uint32_t chunk_size = MIN(CHUNK_SIZE, output_length - sent); |
Jeej | 0:a2bbc478f812 | 143 | uint32_t chunk_offset = sent; |
Jeej | 0:a2bbc478f812 | 144 | |
Jeej | 1:76a8a3cc5f2e | 145 | PRINT("Sending chunk %4d/%4d (%3d bytes)... ", chunk_offset, output_length, chunk_size); |
Jeej | 0:a2bbc478f812 | 146 | FLUSH(); |
Jeej | 1:76a8a3cc5f2e | 147 | modem_send_file_content((uint8_t*)&my_itf, D7_ITF_SIZE(&my_itf), (void*)&istat, FID_OUTPUT_FILE, &(output->data[chunk_offset]), chunk_offset, chunk_size, id); |
Jeej | 0:a2bbc478f812 | 148 | |
Jeej | 0:a2bbc478f812 | 149 | do |
Jeej | 0:a2bbc478f812 | 150 | { |
Jeej | 1:76a8a3cc5f2e | 151 | evt = modem_resp[id].get(3000); |
Jeej | 0:a2bbc478f812 | 152 | resp = (evt.status == osEventMessage)? (uint32_t)evt.value.p : MODEM_RESP_TIMEOUT; |
Jeej | 0:a2bbc478f812 | 153 | |
Jeej | 0:a2bbc478f812 | 154 | if (MODEM_RESP_ACK == resp) |
Jeej | 0:a2bbc478f812 | 155 | { |
Jeej | 0:a2bbc478f812 | 156 | //PRINT_DATA("ACK UID:", "%02X", istat.addressee.id, 8, " "); |
Jeej | 1:76a8a3cc5f2e | 157 | //PRINT("SNR: %ddB RXLEV: %ddBm LB: %ddB\n", istat.snr, -istat.rxlev, istat.lb); |
Jeej | 0:a2bbc478f812 | 158 | |
Jeej | 0:a2bbc478f812 | 159 | //PRINT("ACK.\n"); |
Jeej | 0:a2bbc478f812 | 160 | is_ok = true; |
Jeej | 0:a2bbc478f812 | 161 | } |
Jeej | 0:a2bbc478f812 | 162 | else if (MODEM_RESP_TIMEOUT == resp) |
Jeej | 0:a2bbc478f812 | 163 | { |
Jeej | 1:76a8a3cc5f2e | 164 | PRINT("WAITING...\n"); |
Jeej | 0:a2bbc478f812 | 165 | } |
Jeej | 0:a2bbc478f812 | 166 | else if (MODEM_RESP_ERROR == resp) |
Jeej | 0:a2bbc478f812 | 167 | { |
Jeej | 0:a2bbc478f812 | 168 | //PRINT("ERROR.\n"); |
Jeej | 0:a2bbc478f812 | 169 | break; |
Jeej | 0:a2bbc478f812 | 170 | } |
Jeej | 0:a2bbc478f812 | 171 | else if (MODEM_RESP_TERMINAL == resp) |
Jeej | 0:a2bbc478f812 | 172 | { |
Jeej | 0:a2bbc478f812 | 173 | //PRINT("DONE.\n"); |
Jeej | 0:a2bbc478f812 | 174 | } |
Jeej | 0:a2bbc478f812 | 175 | |
Jeej | 0:a2bbc478f812 | 176 | memset(&istat, 0, sizeof(d7a_sp_res_t)); |
Jeej | 0:a2bbc478f812 | 177 | } while (MODEM_RESP_TERMINAL != resp); |
Jeej | 0:a2bbc478f812 | 178 | |
Jeej | 0:a2bbc478f812 | 179 | if (is_ok) |
Jeej | 0:a2bbc478f812 | 180 | { |
Jeej | 0:a2bbc478f812 | 181 | sent += chunk_size; |
Jeej | 0:a2bbc478f812 | 182 | PRINT("OK.\n"); |
Jeej | 0:a2bbc478f812 | 183 | } |
Jeej | 0:a2bbc478f812 | 184 | else |
Jeej | 0:a2bbc478f812 | 185 | { |
Jeej | 0:a2bbc478f812 | 186 | PRINT("FAILED.\n"); |
Jeej | 0:a2bbc478f812 | 187 | break; |
Jeej | 0:a2bbc478f812 | 188 | } |
Jeej | 0:a2bbc478f812 | 189 | } |
Jeej | 0:a2bbc478f812 | 190 | |
Jeej | 0:a2bbc478f812 | 191 | double time_s = tim.read(); |
Jeej | 0:a2bbc478f812 | 192 | if (sent) |
Jeej | 0:a2bbc478f812 | 193 | { |
Jeej | 0:a2bbc478f812 | 194 | PRINT("%d bytes sent in %.3fs (%d B/s)\n", sent, time_s, (int)((double)sent/time_s)); |
Jeej | 0:a2bbc478f812 | 195 | } |
Jeej | 0:a2bbc478f812 | 196 | |
Jeej | 0:a2bbc478f812 | 197 | if (is_ok) |
Jeej | 0:a2bbc478f812 | 198 | { |
Jeej | 0:a2bbc478f812 | 199 | // Send CRC |
Jeej | 1:76a8a3cc5f2e | 200 | PRINT("Sendind CRC 0x%08X\n", output->crc); |
Jeej | 1:76a8a3cc5f2e | 201 | modem_send_file_content((uint8_t*)&my_itf, D7_ITF_SIZE(&my_itf), (void*)&istat, FID_OUTPUT_FILE, &(output->crc), offsetof(output_file_t, crc), sizeof_field(output_file_t, crc), id); |
Jeej | 0:a2bbc478f812 | 202 | |
Jeej | 0:a2bbc478f812 | 203 | do |
Jeej | 0:a2bbc478f812 | 204 | { |
Jeej | 0:a2bbc478f812 | 205 | evt = modem_resp[id].get(3000); |
Jeej | 0:a2bbc478f812 | 206 | resp = (evt.status == osEventMessage)? (uint32_t)evt.value.p : MODEM_RESP_TIMEOUT; |
Jeej | 0:a2bbc478f812 | 207 | |
Jeej | 0:a2bbc478f812 | 208 | if (MODEM_RESP_ACK == resp) |
Jeej | 0:a2bbc478f812 | 209 | { |
Jeej | 0:a2bbc478f812 | 210 | PRINT_DATA("ACK UID:", "%02X", istat.addressee.id, 8, " "); |
Jeej | 1:76a8a3cc5f2e | 211 | PRINT("SNR: %ddB RXLEV: %ddBm LB: %ddB\n", istat.snr, -istat.rxlev, istat.lb); |
Jeej | 0:a2bbc478f812 | 212 | } |
Jeej | 0:a2bbc478f812 | 213 | else if (MODEM_RESP_TIMEOUT == resp) |
Jeej | 0:a2bbc478f812 | 214 | { |
Jeej | 1:76a8a3cc5f2e | 215 | PRINT("WAITING...\n"); |
Jeej | 0:a2bbc478f812 | 216 | } |
Jeej | 0:a2bbc478f812 | 217 | else if (MODEM_RESP_ERROR == resp) |
Jeej | 0:a2bbc478f812 | 218 | { |
Jeej | 0:a2bbc478f812 | 219 | PRINT("ERROR.\n"); |
Jeej | 0:a2bbc478f812 | 220 | break; |
Jeej | 0:a2bbc478f812 | 221 | } |
Jeej | 0:a2bbc478f812 | 222 | else if (MODEM_RESP_TERMINAL == resp) |
Jeej | 0:a2bbc478f812 | 223 | { |
Jeej | 0:a2bbc478f812 | 224 | PRINT("DONE.\n"); |
Jeej | 0:a2bbc478f812 | 225 | } |
Jeej | 0:a2bbc478f812 | 226 | |
Jeej | 0:a2bbc478f812 | 227 | memset(&istat, 0, sizeof(d7a_sp_res_t)); |
Jeej | 0:a2bbc478f812 | 228 | } while (MODEM_RESP_TERMINAL != resp); |
Jeej | 0:a2bbc478f812 | 229 | } |
Jeej | 0:a2bbc478f812 | 230 | } |
Jeej | 0:a2bbc478f812 | 231 | } |
Jeej | 0:a2bbc478f812 | 232 | |
Jeej | 0:a2bbc478f812 | 233 | void thread_file_modified() |
Jeej | 0:a2bbc478f812 | 234 | { |
Jeej | 0:a2bbc478f812 | 235 | touch_t* touch; |
Jeej | 0:a2bbc478f812 | 236 | osEvent evt; |
Jeej | 1:76a8a3cc5f2e | 237 | uint32_t resp; |
Jeej | 1:76a8a3cc5f2e | 238 | d7a_sp_res_t istat; |
Jeej | 0:a2bbc478f812 | 239 | uint8_t chunk[CHUNK_SIZE]; |
Jeej | 0:a2bbc478f812 | 240 | |
Jeej | 0:a2bbc478f812 | 241 | uint8_t id = modem_get_id(my_response_callback); |
Jeej | 0:a2bbc478f812 | 242 | |
Jeej | 0:a2bbc478f812 | 243 | while (true) |
Jeej | 0:a2bbc478f812 | 244 | { |
Jeej | 0:a2bbc478f812 | 245 | evt = g_file_modified.get(); |
Jeej | 0:a2bbc478f812 | 246 | touch = (evt.status == osEventMessage)? (touch_t*)evt.value.p : NULL; |
Jeej | 0:a2bbc478f812 | 247 | ASSERT(touch != NULL, "NULL touch pointer!\n"); |
Jeej | 0:a2bbc478f812 | 248 | |
Jeej | 0:a2bbc478f812 | 249 | switch (touch->fid) |
Jeej | 0:a2bbc478f812 | 250 | { |
Jeej | 1:76a8a3cc5f2e | 251 | case FID_OUTPUT_FILE: |
Jeej | 1:76a8a3cc5f2e | 252 | output_file_t* output; |
Jeej | 1:76a8a3cc5f2e | 253 | uint32_t output_length; |
Jeej | 1:76a8a3cc5f2e | 254 | |
Jeej | 1:76a8a3cc5f2e | 255 | // Directly get data pointer to avoid reading file |
Jeej | 1:76a8a3cc5f2e | 256 | output = (output_file_t*)ram_fs_get_data(FID_OUTPUT_FILE); |
Jeej | 1:76a8a3cc5f2e | 257 | |
Jeej | 1:76a8a3cc5f2e | 258 | output_length = strlen((char*)output->data); |
Jeej | 1:76a8a3cc5f2e | 259 | |
Jeej | 1:76a8a3cc5f2e | 260 | // Update file CRC (Calculate CRC on string without end of string) |
Jeej | 1:76a8a3cc5f2e | 261 | output->crc = crc32((char*)output->data, output_length); |
Jeej | 1:76a8a3cc5f2e | 262 | |
Jeej | 1:76a8a3cc5f2e | 263 | PRINT("NEW OUTPUT CRC 0x%08X (on %d bytes)\n", output->crc, output_length); |
Jeej | 1:76a8a3cc5f2e | 264 | break; |
Jeej | 0:a2bbc478f812 | 265 | case FID_INPUT_FILE: |
Jeej | 1:76a8a3cc5f2e | 266 | input_file_t* input; |
Jeej | 1:76a8a3cc5f2e | 267 | uint32_t input_length; |
Jeej | 1:76a8a3cc5f2e | 268 | uint32_t crc; |
Jeej | 1:76a8a3cc5f2e | 269 | |
Jeej | 1:76a8a3cc5f2e | 270 | // Directly get data pointer to avoid reading file |
Jeej | 1:76a8a3cc5f2e | 271 | input = (input_file_t*)ram_fs_get_data(FID_INPUT_FILE); |
Jeej | 1:76a8a3cc5f2e | 272 | |
Jeej | 1:76a8a3cc5f2e | 273 | input_length = strlen((char*)input->data); |
Jeej | 1:76a8a3cc5f2e | 274 | |
Jeej | 1:76a8a3cc5f2e | 275 | // Calculate CRC |
Jeej | 1:76a8a3cc5f2e | 276 | crc = crc32((char*)input->data, input_length); |
Jeej | 0:a2bbc478f812 | 277 | |
Jeej | 0:a2bbc478f812 | 278 | // Check if CRC has been updated |
Jeej | 1:76a8a3cc5f2e | 279 | if ((offsetof(input_file_t, crc) == touch->offset) && (sizeof_field(input_file_t, crc) == touch->length)) |
Jeej | 0:a2bbc478f812 | 280 | { |
Jeej | 1:76a8a3cc5f2e | 281 | |
Jeej | 0:a2bbc478f812 | 282 | |
Jeej | 1:76a8a3cc5f2e | 283 | PRINT("INPUT CRC 0x%08X CRC 0x%08X (on %d bytes)\n", input->crc, crc, input_length); |
Jeej | 0:a2bbc478f812 | 284 | |
Jeej | 1:76a8a3cc5f2e | 285 | if (input->crc != crc) |
Jeej | 0:a2bbc478f812 | 286 | { |
Jeej | 0:a2bbc478f812 | 287 | // delete CRC |
Jeej | 1:76a8a3cc5f2e | 288 | input->crc = 0; |
Jeej | 0:a2bbc478f812 | 289 | } |
Jeej | 0:a2bbc478f812 | 290 | |
Jeej | 0:a2bbc478f812 | 291 | // Send CRC as confirmation |
Jeej | 1:76a8a3cc5f2e | 292 | PRINT("COMFIRM CRC 0x%08X\n", input->crc); |
Jeej | 1:76a8a3cc5f2e | 293 | modem_send_file_content((uint8_t*)&my_itf, D7_ITF_SIZE(&my_itf), (void*)&istat, FID_INPUT_FILE, &(input->crc), offsetof(input_file_t, crc), sizeof_field(input_file_t, crc), id); |
Jeej | 0:a2bbc478f812 | 294 | |
Jeej | 0:a2bbc478f812 | 295 | do |
Jeej | 0:a2bbc478f812 | 296 | { |
Jeej | 0:a2bbc478f812 | 297 | evt = modem_resp[id].get(3000); |
Jeej | 0:a2bbc478f812 | 298 | resp = (evt.status == osEventMessage)? (uint32_t)evt.value.p : MODEM_RESP_TIMEOUT; |
Jeej | 0:a2bbc478f812 | 299 | |
Jeej | 0:a2bbc478f812 | 300 | if (MODEM_RESP_ACK == resp) |
Jeej | 0:a2bbc478f812 | 301 | { |
Jeej | 0:a2bbc478f812 | 302 | PRINT_DATA("ACK UID:", "%02X", istat.addressee.id, 8, " "); |
Jeej | 1:76a8a3cc5f2e | 303 | PRINT("SNR: %ddB RXLEV: %ddBm LB: %ddB\n", istat.snr, -istat.rxlev, istat.lb); |
Jeej | 0:a2bbc478f812 | 304 | } |
Jeej | 0:a2bbc478f812 | 305 | else if (MODEM_RESP_TIMEOUT == resp) |
Jeej | 0:a2bbc478f812 | 306 | { |
Jeej | 1:76a8a3cc5f2e | 307 | PRINT("WAITING...\n"); |
Jeej | 0:a2bbc478f812 | 308 | } |
Jeej | 0:a2bbc478f812 | 309 | else if (MODEM_RESP_ERROR == resp) |
Jeej | 0:a2bbc478f812 | 310 | { |
Jeej | 0:a2bbc478f812 | 311 | PRINT("ERROR.\n"); |
Jeej | 0:a2bbc478f812 | 312 | break; |
Jeej | 0:a2bbc478f812 | 313 | } |
Jeej | 0:a2bbc478f812 | 314 | else if (MODEM_RESP_TERMINAL == resp) |
Jeej | 0:a2bbc478f812 | 315 | { |
Jeej | 0:a2bbc478f812 | 316 | PRINT("DONE.\n"); |
Jeej | 0:a2bbc478f812 | 317 | } |
Jeej | 0:a2bbc478f812 | 318 | |
Jeej | 0:a2bbc478f812 | 319 | memset(&istat, 0, sizeof(d7a_sp_res_t)); |
Jeej | 0:a2bbc478f812 | 320 | } while (MODEM_RESP_TERMINAL != resp); |
Jeej | 0:a2bbc478f812 | 321 | } |
Jeej | 1:76a8a3cc5f2e | 322 | else |
Jeej | 1:76a8a3cc5f2e | 323 | { |
Jeej | 1:76a8a3cc5f2e | 324 | PRINT("Got chunk %4d/%4d (%3d bytes)\n", touch->offset, sizeof_field(input_file_t, data), touch->length); |
Jeej | 1:76a8a3cc5f2e | 325 | ram_fs_read(touch->fid, touch->offset, touch->length, chunk); |
Jeej | 1:76a8a3cc5f2e | 326 | //PRINT("%s\n", chunk); |
Jeej | 1:76a8a3cc5f2e | 327 | //PRINT_DATA("chunk end:", "%02X", &(chunk[touch->length-4]), 4, "\n"); |
Jeej | 1:76a8a3cc5f2e | 328 | |
Jeej | 1:76a8a3cc5f2e | 329 | PRINT("NEW INPUT CRC 0x%08X (on %d bytes)\n", crc, input_length); |
Jeej | 1:76a8a3cc5f2e | 330 | } |
Jeej | 0:a2bbc478f812 | 331 | break; |
Jeej | 0:a2bbc478f812 | 332 | default: |
Jeej | 0:a2bbc478f812 | 333 | PRINT("TOUCH FID %d OFF %d LEN %d\n", touch->fid, touch->offset, touch->length); |
Jeej | 0:a2bbc478f812 | 334 | break; |
Jeej | 0:a2bbc478f812 | 335 | } |
Jeej | 0:a2bbc478f812 | 336 | |
Jeej | 0:a2bbc478f812 | 337 | FREE(touch); |
Jeej | 0:a2bbc478f812 | 338 | } |
Jeej | 0:a2bbc478f812 | 339 | } |
Jeej | 0:a2bbc478f812 | 340 | |
Jeej | 0:a2bbc478f812 | 341 | modem_callbacks_t callbacks = { |
Jeej | 0:a2bbc478f812 | 342 | .read = my_read, |
Jeej | 0:a2bbc478f812 | 343 | .write = my_write, |
Jeej | 0:a2bbc478f812 | 344 | .read_fprop = my_read_fprop, |
Jeej | 0:a2bbc478f812 | 345 | .flush = my_flush, |
Jeej | 0:a2bbc478f812 | 346 | .remove = my_delete, |
Jeej | 0:a2bbc478f812 | 347 | .udata = my_udata, |
Jeej | 0:a2bbc478f812 | 348 | .lqual = my_lqual, |
Jeej | 0:a2bbc478f812 | 349 | .ldown = my_ldown, |
Jeej | 0:a2bbc478f812 | 350 | .reset = my_reset, |
Jeej | 0:a2bbc478f812 | 351 | .boot = my_boot |
Jeej | 0:a2bbc478f812 | 352 | }; |
Jeej | 0:a2bbc478f812 | 353 | |
Jeej | 0:a2bbc478f812 | 354 | // Callback |
Jeej | 0:a2bbc478f812 | 355 | void my_main_callback(uint8_t terminal, int8_t err, uint8_t id) |
Jeej | 0:a2bbc478f812 | 356 | { |
Jeej | 0:a2bbc478f812 | 357 | (void)id; |
Jeej | 0:a2bbc478f812 | 358 | |
Jeej | 1:76a8a3cc5f2e | 359 | print_resp(id, err); |
Jeej | 1:76a8a3cc5f2e | 360 | |
Jeej | 0:a2bbc478f812 | 361 | if (terminal) |
Jeej | 0:a2bbc478f812 | 362 | { |
Jeej | 0:a2bbc478f812 | 363 | modem_ready.release(); |
Jeej | 0:a2bbc478f812 | 364 | } |
Jeej | 0:a2bbc478f812 | 365 | } |
Jeej | 0:a2bbc478f812 | 366 | |
Jeej | 0:a2bbc478f812 | 367 | /*** Main function ------------------------------------------------------------- ***/ |
Jeej | 0:a2bbc478f812 | 368 | int main() { |
Jeej | 0:a2bbc478f812 | 369 | // Start & initialize |
Jeej | 0:a2bbc478f812 | 370 | #ifdef DEBUG_LED |
Jeej | 0:a2bbc478f812 | 371 | DBG_OPEN(DEBUG_LED); |
Jeej | 0:a2bbc478f812 | 372 | #else |
Jeej | 0:a2bbc478f812 | 373 | DBG_OPEN(NC); |
Jeej | 0:a2bbc478f812 | 374 | #endif |
Jeej | 0:a2bbc478f812 | 375 | PRINT("\n" |
Jeej | 0:a2bbc478f812 | 376 | "-----------------------------------------\n" |
Jeej | 0:a2bbc478f812 | 377 | "------------- Demo Big File -------------\n" |
Jeej | 0:a2bbc478f812 | 378 | "-----------------------------------------\n"); |
Jeej | 1:76a8a3cc5f2e | 379 | |
Jeej | 0:a2bbc478f812 | 380 | modem_helper_open(&callbacks); |
Jeej | 0:a2bbc478f812 | 381 | |
Jeej | 0:a2bbc478f812 | 382 | uint8_t id = modem_get_id(my_main_callback); |
Jeej | 0:a2bbc478f812 | 383 | |
Jeej | 0:a2bbc478f812 | 384 | // Set custom retry policy |
Jeej | 0:a2bbc478f812 | 385 | // XXX Won't work the first time as we need to reboot the modem for the changes to be applied |
Jeej | 0:a2bbc478f812 | 386 | modem_write_file(WM_FID_ALP_CFG, &my_policy, MY_POLICY_IDX * sizeof(alp_retry_policy_t), sizeof(alp_retry_policy_t), id); |
Jeej | 0:a2bbc478f812 | 387 | modem_ready.wait(); |
Jeej | 0:a2bbc478f812 | 388 | modem_flush_file(WM_FID_ALP_CFG, id); |
Jeej | 0:a2bbc478f812 | 389 | modem_ready.wait(); |
Jeej | 0:a2bbc478f812 | 390 | |
Jeej | 0:a2bbc478f812 | 391 | PRINT("Register Files\n"); |
Jeej | 0:a2bbc478f812 | 392 | // HOST Revision is a local file. Uses D7AActP Notification. |
Jeej | 0:a2bbc478f812 | 393 | modem_update_file(FID_HOST_REV, (alp_file_header_t*)&h_rev, (uint8_t*)&f_rev); |
Jeej | 1:76a8a3cc5f2e | 394 | modem_update_file(FID_OUTPUT_FILE, (alp_file_header_t*)&h_output_file, (uint8_t*)&f_output_file); |
Jeej | 0:a2bbc478f812 | 395 | modem_update_file(FID_INPUT_FILE, (alp_file_header_t*)&h_input_file, (uint8_t*)&f_input_file); |
Jeej | 0:a2bbc478f812 | 396 | |
Jeej | 0:a2bbc478f812 | 397 | PRINT("Start D7A Stack\n"); |
Jeej | 0:a2bbc478f812 | 398 | modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP | ALP_D7A_ISTAT_UNS, true, id); |
Jeej | 0:a2bbc478f812 | 399 | modem_ready.wait(); |
Jeej | 0:a2bbc478f812 | 400 | |
Jeej | 0:a2bbc478f812 | 401 | PRINT("Notify Modem Version\n"); |
Jeej | 0:a2bbc478f812 | 402 | modem_notify_file(D7A_FID_FIRMWARE_VERSION, 0, SIZE_HOST_REV, id); |
Jeej | 0:a2bbc478f812 | 403 | modem_ready.wait(); |
Jeej | 0:a2bbc478f812 | 404 | |
Jeej | 0:a2bbc478f812 | 405 | PRINT("Notify Host Version\n"); |
Jeej | 0:a2bbc478f812 | 406 | modem_notify_file(FID_HOST_REV, 0, SIZE_HOST_REV, id); |
Jeej | 0:a2bbc478f812 | 407 | modem_ready.wait(); |
Jeej | 0:a2bbc478f812 | 408 | |
Jeej | 0:a2bbc478f812 | 409 | // id no longer needed |
Jeej | 0:a2bbc478f812 | 410 | modem_free_id(id); |
Jeej | 0:a2bbc478f812 | 411 | |
Jeej | 0:a2bbc478f812 | 412 | // Start file modified thread |
Jeej | 0:a2bbc478f812 | 413 | Thread th_file_modified(osPriorityNormal, 1024, NULL); |
Jeej | 0:a2bbc478f812 | 414 | osStatus status = th_file_modified.start(thread_file_modified); |
Jeej | 0:a2bbc478f812 | 415 | ASSERT(status == osOK, "Failed to start thread_file_modified (err: %d)\r\n", status); |
Jeej | 0:a2bbc478f812 | 416 | |
Jeej | 0:a2bbc478f812 | 417 | #ifdef DEBUG_BUTTON |
Jeej | 0:a2bbc478f812 | 418 | DebouncedInterrupt user_interrupt(DEBUG_BUTTON); |
Jeej | 0:a2bbc478f812 | 419 | user_interrupt.attach(button_push_isr, IRQ_FALL, 500, true); |
Jeej | 0:a2bbc478f812 | 420 | |
Jeej | 0:a2bbc478f812 | 421 | Thread but_th(osPriorityNormal, 4096, NULL); |
Jeej | 0:a2bbc478f812 | 422 | status = but_th.start(button_user_thread); |
Jeej | 0:a2bbc478f812 | 423 | ASSERT(status == osOK, "Failed to start but thread (err: %d)\r\n", status); |
Jeej | 0:a2bbc478f812 | 424 | #else |
Jeej | 0:a2bbc478f812 | 425 | #error You need a button to use this APP as is |
Jeej | 0:a2bbc478f812 | 426 | #endif |
Jeej | 0:a2bbc478f812 | 427 | |
Jeej | 0:a2bbc478f812 | 428 | #ifdef DEBUG_LED |
Jeej | 0:a2bbc478f812 | 429 | DigitalOut my_led(DEBUG_LED); |
Jeej | 0:a2bbc478f812 | 430 | #endif |
Jeej | 0:a2bbc478f812 | 431 | |
Jeej | 0:a2bbc478f812 | 432 | // Set main task to lowest priority |
Jeej | 0:a2bbc478f812 | 433 | osThreadSetPriority(osThreadGetId(), osPriorityIdle); |
Jeej | 0:a2bbc478f812 | 434 | while(true) |
Jeej | 0:a2bbc478f812 | 435 | { |
Jeej | 0:a2bbc478f812 | 436 | Thread::wait(500); |
Jeej | 0:a2bbc478f812 | 437 | #ifdef DEBUG_LED |
Jeej | 0:a2bbc478f812 | 438 | my_led = !my_led; |
Jeej | 0:a2bbc478f812 | 439 | #endif |
Jeej | 0:a2bbc478f812 | 440 | } |
Jeej | 0:a2bbc478f812 | 441 | } |