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