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