Sending and reception of big data file (1kB example)
Dependencies: modem_ref_helper CRC DebouncedInterrupt
Diff: main.cpp
- Revision:
- 1:76a8a3cc5f2e
- Parent:
- 0:a2bbc478f812
- Child:
- 6:33dfecc85ebf
--- a/main.cpp Thu Dec 14 14:22:40 2017 +0000 +++ b/main.cpp Thu Dec 14 18:25:58 2017 +0000 @@ -10,7 +10,6 @@ #define MIN(a,b) ((a<b)?a:b) #define MY_POLICY_IDX 0 -#define FID_OUTPUT_FILE 200 #define CHUNK_SIZE 128 Semaphore button_user(0); @@ -25,34 +24,22 @@ MODEM_RESP_ACK, MODEM_RESP_TIMEOUT, }; - -const input_file_t output_file = { - .data = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec id pulvinar est. Mauris pretium arcu at facilisis rutrum. Nulla facilisi. " - "Quisque auctor, massa et gravida semper, libero dui varius est, quis ornare dolor odio a turpis. Suspendisse in turpis vel risus porta " - "aliquet et a nibh. Mauris vehicula purus ut risus convallis, id luctus erat tincidunt. Aenean vitae gravida nisi. Nulla eleifend lorem " - "purus, non sagittis neque accumsan molestie.\n" - "\n" - "Praesent interdum semper viverra. Cras ut sodales quam. Etiam vitae orci sit amet lorem semper commodo. Phasellus elementum nec dui eget " - "pellentesque. Proin eget dignissim nibh. In iaculis imperdiet enim, consequat gravida mi varius sed. Suspendisse eu elementum justo. " - "Donec non mauris odio. Sed aliquet vestibulum risus non ultricies. Integer mollis neque id magna gravida auctor. Cras at auctor velit. " - "Vivamus tempor dui purus, id commodo arcu iaculis a. Curabitur hendrerit, nibh sit amet gravida porttitor, leo magna gravida ex, non " - "porta ligula quam ac risus. Maur", - // Used only to know size and offset - .crc = 0, -} - - +// This discribes the retry policy of the stack for each packet +// Do not modify uncommented parameters +// After changing this, reboot the modem to apply it alp_retry_policy_t my_policy = { .meta.procedure = 0, .meta.respond = true, .meta.persistant = false, .meta.bulk = false, .depth = 1, - .retries = 0, - .slot_time = 0 + .retries = 2, // The stack will retry 2 times (each packet will be sent a maximum of 3 times) + .slot_time = 1, // Interval between retries in seconds }; +// This describe the upload interface +// Do not modify uncommented parameters alp_d7a_itf_t my_itf = { .type = ALP_ITF_TYPE_D7A, .cfg.to = 0, @@ -61,9 +48,9 @@ .cfg.qos.bf.retry = MY_POLICY_IDX, .cfg.qos.bf.record = 0, .cfg.qos.bf.stop_on_err = 0, - .cfg.addressee.ctrl.bf.nls = D7A_NLS_AES_CCM_64, + .cfg.addressee.ctrl.bf.nls = D7A_NLS_AES_CCM_64, // Security level .cfg.addressee.ctrl.bf.idf = D7A_ID_NBID, - .cfg.addressee.xcl.bf = {.s = 0x0, .m = 0x1},// XXX D7A_XCL_GW, + .cfg.addressee.xcl.bf = {.s = 0x2, .m = 0x1}, // Gateway access class .cfg.addressee.id[0] = 4, }; @@ -83,7 +70,7 @@ } } -// Callback for broadcast read +// Response Callback void my_response_callback(uint8_t terminal, int8_t err, uint8_t id) { (void)id; @@ -113,64 +100,68 @@ button_user.release(); } +// Upload thread void button_user_thread() { - FPRINT("(id:0x%08x)\r\n", osThreadGetId()); - osEvent evt; uint32_t resp; d7a_sp_res_t istat; + output_file_t* output; uint32_t sent = 0; - uint8_t chunk[CHUNK_SIZE]; uint8_t id = modem_get_id(my_response_callback); memset(&istat, 0, sizeof(d7a_sp_res_t)); + // Directly get data pointer to avoid reading file + output = (output_file_t*)ram_fs_get_data(FID_OUTPUT_FILE); + + // Update file CRC (Calculate CRC on string without end of string) + output->crc = crc32((char*)output->data, strlen((char*)output->data)); + while (true) { Timer tim; bool is_ok; - - // Calculate CRC - uint32_t crc = calculate_crc32(output_file, sizeof(output_file)); + uint32_t output_length = strlen((char*)output->data) + 1; // +1 as data is a string, send end of string // Wait for button press - PRINT("Press user button to send file.\n"); + PRINT("Press user button to send file. (%d bytes)\n", output_length); button_user.wait(); + // Update file CRC (Calculate CRC on string without end of string) + output->crc = crc32((char*)output->data, output_length - 1); + sent = 0; tim.start(); // Send chunks - while (sent < sizeof(output_file)) + while (sent < output_length) { is_ok = false; - uint32_t chunk_size = MIN(sizeof(chunk), sizeof(output_file) - sent); + uint32_t chunk_size = MIN(CHUNK_SIZE, output_length - sent); uint32_t chunk_offset = sent; - memcpy(chunk, &(output_file[chunk_offset]), chunk_size); - - PRINT("Sending chunk %4d/%4d (%3d bytes)... ", chunk_offset, sizeof(output_file), chunk_size); + PRINT("Sending chunk %4d/%4d (%3d bytes)... ", chunk_offset, output_length, chunk_size); FLUSH(); - modem_send_file_content((uint8_t*)&my_itf, D7_ITF_SIZE(&my_itf), (void*)&istat, FID_OUTPUT_FILE, chunk, chunk_offset, chunk_size, id); + 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); do { - evt = modem_resp.get(3000); + evt = modem_resp[id].get(3000); resp = (evt.status == osEventMessage)? (uint32_t)evt.value.p : MODEM_RESP_TIMEOUT; if (MODEM_RESP_ACK == resp) { //PRINT_DATA("ACK UID:", "%02X", istat.addressee.id, 8, " "); - //PRINT("SNR:%d dB RXLEV:%d dBm LB:%d dB\n", istat.snr, -istat.rxlev, istat.lb); + //PRINT("SNR: %ddB RXLEV: %ddBm LB: %ddB\n", istat.snr, -istat.rxlev, istat.lb); //PRINT("ACK.\n"); is_ok = true; } else if (MODEM_RESP_TIMEOUT == resp) { - PRINT("TIMEOUT.\n"); + PRINT("WAITING...\n"); } else if (MODEM_RESP_ERROR == resp) { @@ -206,8 +197,8 @@ if (is_ok) { // Send CRC - PRINT("Sendind CRC 0x%08X\n", crc); - modem_send_file_content((uint8_t*)&my_itf, D7_ITF_SIZE(&my_itf), (void*)&istat, FID_OUTPUT_FILE, &crc, offsetof(input_file_t, crc), sizeof_field(input_file_t, crc), id); + PRINT("Sendind CRC 0x%08X\n", output->crc); + 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); do { @@ -217,11 +208,11 @@ if (MODEM_RESP_ACK == resp) { PRINT_DATA("ACK UID:", "%02X", istat.addressee.id, 8, " "); - PRINT("SNR:%d dB RXLEV:%d dBm LB:%d dB\n", istat.snr, -istat.rxlev, istat.lb); + PRINT("SNR: %ddB RXLEV: %ddBm LB: %ddB\n", istat.snr, -istat.rxlev, istat.lb); } else if (MODEM_RESP_TIMEOUT == resp) { - PRINT("TIMEOUT.\n"); + PRINT("WAITING...\n"); } else if (MODEM_RESP_ERROR == resp) { @@ -243,6 +234,8 @@ { touch_t* touch; osEvent evt; + uint32_t resp; + d7a_sp_res_t istat; uint8_t chunk[CHUNK_SIZE]; uint8_t id = modem_get_id(my_response_callback); @@ -255,33 +248,49 @@ switch (touch->fid) { + case FID_OUTPUT_FILE: + output_file_t* output; + uint32_t output_length; + + // Directly get data pointer to avoid reading file + output = (output_file_t*)ram_fs_get_data(FID_OUTPUT_FILE); + + output_length = strlen((char*)output->data); + + // Update file CRC (Calculate CRC on string without end of string) + output->crc = crc32((char*)output->data, output_length); + + PRINT("NEW OUTPUT CRC 0x%08X (on %d bytes)\n", output->crc, output_length); + break; case FID_INPUT_FILE: - PRINT("Got chunk %4d/%4d (%3d bytes)\n", touch->offset, sizeof_field(input_file_t, data), touch->length); - ram_fs_read(touch->fid, touch->offset, touch->length, chunk); - PRINT("%s\n", chunk); + input_file_t* input; + uint32_t input_length; + uint32_t crc; + + // Directly get data pointer to avoid reading file + input = (input_file_t*)ram_fs_get_data(FID_INPUT_FILE); + + input_length = strlen((char*)input->data); + + // Calculate CRC + crc = crc32((char*)input->data, input_length); // Check if CRC has been updated - if ((touch->offset == offsetof(input_file_t, crc)) && (touch->length == sizeof_field(input_file_t, crc))) + if ((offsetof(input_file_t, crc) == touch->offset) && (sizeof_field(input_file_t, crc) == touch->length)) { - input_file_t input; - - // Read file - ram_fs_read(touch->fid, 0, sizeof(input_file_t), input); + - // Calculate and compare CRC - uint32_t crc = calculate_crc32(input.data, strlen(input.data)); + PRINT("INPUT CRC 0x%08X CRC 0x%08X (on %d bytes)\n", input->crc, crc, input_length); - PRINT("INPUT CRC 0x%08X CRC 0x%08X\n", input.crc, crc); - - if (input.crc != crc) + if (input->crc != crc) { // delete CRC - input.crc = 0; + input->crc = 0; } // Send CRC as confirmation - PRINT("COMFIRM CRC 0x%08X\n", input.crc); - 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); + PRINT("COMFIRM CRC 0x%08X\n", input->crc); + 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); do { @@ -291,11 +300,11 @@ if (MODEM_RESP_ACK == resp) { PRINT_DATA("ACK UID:", "%02X", istat.addressee.id, 8, " "); - PRINT("SNR:%d dB RXLEV:%d dBm LB:%d dB\n", istat.snr, -istat.rxlev, istat.lb); + PRINT("SNR: %ddB RXLEV: %ddBm LB: %ddB\n", istat.snr, -istat.rxlev, istat.lb); } else if (MODEM_RESP_TIMEOUT == resp) { - PRINT("TIMEOUT.\n"); + PRINT("WAITING...\n"); } else if (MODEM_RESP_ERROR == resp) { @@ -310,6 +319,15 @@ memset(&istat, 0, sizeof(d7a_sp_res_t)); } while (MODEM_RESP_TERMINAL != resp); } + else + { + PRINT("Got chunk %4d/%4d (%3d bytes)\n", touch->offset, sizeof_field(input_file_t, data), touch->length); + ram_fs_read(touch->fid, touch->offset, touch->length, chunk); + //PRINT("%s\n", chunk); + //PRINT_DATA("chunk end:", "%02X", &(chunk[touch->length-4]), 4, "\n"); + + PRINT("NEW INPUT CRC 0x%08X (on %d bytes)\n", crc, input_length); + } break; default: PRINT("TOUCH FID %d OFF %d LEN %d\n", touch->fid, touch->offset, touch->length); @@ -338,15 +356,12 @@ { (void)id; + print_resp(id, err); + if (terminal) { - print_status(err); modem_ready.release(); } - else - { - print_resp(err); - } } /*** Main function ------------------------------------------------------------- ***/ @@ -361,16 +376,13 @@ "-----------------------------------------\n" "------------- Demo Big File -------------\n" "-----------------------------------------\n"); - - FPRINT("(id:0x%08x)\r\n", osThreadGetId()); - + modem_helper_open(&callbacks); uint8_t id = modem_get_id(my_main_callback); // Set custom retry policy // XXX Won't work the first time as we need to reboot the modem for the changes to be applied - modem_write_file(WM_FID_ALP_CFG, &my_policy, MY_POLICY_IDX * sizeof(alp_retry_policy_t), sizeof(alp_retry_policy_t), id); modem_ready.wait(); modem_flush_file(WM_FID_ALP_CFG, id); @@ -379,6 +391,7 @@ PRINT("Register Files\n"); // HOST Revision is a local file. Uses D7AActP Notification. modem_update_file(FID_HOST_REV, (alp_file_header_t*)&h_rev, (uint8_t*)&f_rev); + modem_update_file(FID_OUTPUT_FILE, (alp_file_header_t*)&h_output_file, (uint8_t*)&f_output_file); modem_update_file(FID_INPUT_FILE, (alp_file_header_t*)&h_input_file, (uint8_t*)&f_input_file); PRINT("Start D7A Stack\n");