Ping pong app demo.
Dependencies: modem_ref_helper DebouncedInterrupt
Diff: main.cpp
- Revision:
- 8:1b7101152a76
- Parent:
- 7:7524fab147aa
- Child:
- 9:63592df2faad
--- a/main.cpp Fri Mar 09 16:17:19 2018 +0000 +++ b/main.cpp Thu Sep 20 11:03:19 2018 +0000 @@ -23,8 +23,6 @@ d7a_addressee_t addressee; } ping_t; - -#define MY_POLICY_IDX 0 #define FID_PING_PONG 128 #define PING_DELAY 1000 #define PING_COUNTER_SIZE sizeof(uint32_t) @@ -35,22 +33,10 @@ // { .bf.s = 13, .bf.m = 1 }; --> Normal Rate // { .bf.s = 14, .bf.m = 1 }; --> Slow Rate -// We use these access class in this test, because in a normal access class, -// the transmission will be stopped by the duty cycle limit after several PINGs. -d7a_xcl_t ping_pong_xcl = { .bf.s = 13, .bf.m = 1 }; - -// This discribes the retry policy of the stack for each packet -// Do not modify uncommented parameters -alp_retry_policy_t my_policy = { - .meta.procedure = 0, - .meta.respond = true, - .meta.persistant = false, - .meta.bulk = false, - .depth = 1, - .retries = 0, // The stack will retry x times (each packet will be sent a maximum of x+1 times) - .slot_time = 0 // Interval between retries in seconds -}; - +// Use Gateway access profile and its 2 subprofiles at the same time +// Subprofile 0: 3 channels +// Subprofile 1: 4 channels +d7a_xcl_t ping_pong_xcl = { .bf.s = 2, .bf.m = 0x1 }; // This describe the interface used for communication // Do not modify uncommented parameters @@ -59,17 +45,17 @@ .cfg.to = 0, .cfg.te = 0, .cfg.qos.bf.resp = D7A_RESP_NO, // Communication protocol - .cfg.qos.bf.retry = MY_POLICY_IDX, // Retry policy + .cfg.qos.bf.retry = ALP_RPOL_ONESHOT, // Retry policy .cfg.addressee.ctrl.bf.nls = D7A_NLS_AES_CCM_64, // Security level .cfg.addressee.xcl = ping_pong_xcl, // Used Access Class // One of the followings: - .cfg.addressee.ctrl.bf.idf = D7A_ID_NOID, // in Broadcast (same as D7A_ID_NBID with .cfg.addressee.id[0] = 32) + .cfg.addressee.ctrl.bf.idf = D7A_ID_NOID, // in Broadcast (same as D7A_ID_NBID with .cfg.addressee.id[0] = D7A_CTF_VAL(2,2) (32)) //.cfg.addressee.ctrl.bf.idf = D7A_ID_UID, // in Unicast //.cfg.addressee.id = { 0x00, 0x1B, 0xC5, 0x0C, 0x70, 0x00, 0x00, 0x00 }, // Destination UID //.cfg.addressee.ctrl.bf.idf = D7A_ID_NBID, // in Broadcast - //.cfg.addressee.id[0] = 4, // Estimation of expected responses (1 to 32) + //.cfg.addressee.id[0] = D7A_CTF_VAL(4,0), // Estimation of expected responses (1 to 32) }; void print_status(int status) @@ -138,59 +124,87 @@ DigitalOut my_led(DEBUG_LED); #endif -void my_udata(u8 fid, void *data, u32 offset, u32 length, u8 i_type, u8 i_length, u8* i_data) -{ - (void)data; - (void)i_length; - - if (i_type == ALP_ITF_TYPE_D7A) - { - d7a_sp_res_t istat; +void my_udata(void *data, u32 length) +{ + uint8_t* p = (uint8_t*)data; + int32_t rem = length; + alp_parsed_chunk_t r; + d7a_sp_res_t istat; + + do { + uint32_t parsed = alp_parse_chunk(&p, &r); + if (!parsed) + { + // Discard the payload in case of parsing error. + PRINT("Parsing error!\r\n"); + break; + } + rem -= parsed; + + switch (r.type) + { + // Interface status + case ALP_OPCODE_RSP_ISTATUS: + // D7A Interface + if (ALP_ITF_TYPE_D7A == r.meta.itf.type) + { + // Save interface status + memcpy(&istat, r.data, sizeof(d7a_sp_res_t)); + } + else + { + PRINT("Got accessed by unknown Interface 0x%02X\n", r.meta.itf.type); + } + break; + // Data return + case ALP_OPCODE_RSP_F_DATA: + if(FID_PING_PONG == r.meta.f_data.fid && 0 == r.meta.f_data.offset && PING_COUNTER_SIZE == r.meta.f_data.length) + { + uint32_t count; - // Get interface status - memcpy(&istat, i_data, i_length); - - if(FID_PING_PONG == fid && 0 == offset && PING_COUNTER_SIZE == length) - { - uint32_t count; - - // Get data - memcpy(&count, data, length); - - PRINT("Got PING %d", count); - PRINT_DATA(" from ", "%02X", istat.addressee.id, 8, ""); - PRINT(" (SNR:%d dB RXLEV:%d dBm LB:%d dB)\n", istat.snr, -istat.rxlev, istat.lb); - + // Get data + memcpy(&count, r.data, r.meta.f_data.length); + + PRINT("Got PING %d", count); + PRINT_DATA(" from ", "%02X", istat.addressee.id, 8, ""); + PRINT(" (SNR:%d dB RXLEV:%d dBm LB:%d dB)\n", istat.snr, -istat.rxlev, istat.lb); + #ifdef DEBUG_LED - my_led = 1; + my_led = 1; #endif - - Thread::wait(PING_DELAY); - + + Thread::wait(PING_DELAY); + #ifdef DEBUG_LED - my_led = 0; + my_led = 0; #endif - - alp_d7a_itf_t resp_itf = { - .type = ALP_ITF_TYPE_D7A, - .cfg.to = 0, - .cfg.te = 0, - .cfg.qos.bf.resp = D7A_RESP_NO, - .cfg.qos.bf.retry = MY_POLICY_IDX, - .cfg.addressee = istat.addressee, - }; - - count++; - - // Send ping - PRINT("Send PING %d", count); - PRINT_DATA(" to ", "%02X", resp_itf.cfg.addressee.id, 8, "\n"); - - modem_send_file_content((uint8_t*)&resp_itf, D7_ITF_SIZE(&resp_itf), NULL, FID_PING_PONG, &count, 0, PING_COUNTER_SIZE, g_void_id); + + alp_d7a_itf_t resp_itf = { + .type = ALP_ITF_TYPE_D7A, + .cfg.to = 0, + .cfg.te = 0, + .cfg.qos.bf.resp = D7A_RESP_NO, + .cfg.qos.bf.retry = ALP_RPOL_ONESHOT, + .cfg.addressee = istat.addressee, + }; + + count++; + + // Send ping + PRINT("Send PING %d", count); + PRINT_DATA(" to ", "%02X", resp_itf.cfg.addressee.id, 8, "\n"); + + modem_send_file_content((uint8_t*)&resp_itf, D7_ITF_SIZE(&resp_itf), NULL, FID_PING_PONG, &count, 0, PING_COUNTER_SIZE, g_void_id); + } + break; + default: + PRINT("Untreated OPCODE %d\n", r.type); + break; } - } + } while (rem > 0); } + void button_user_thread() { uint32_t ping = 0; @@ -220,7 +234,8 @@ .lqual = my_lqual, .ldown = my_ldown, .reset = my_reset, - .boot = my_boot + .boot = my_boot, + .busy = my_busy, }; /*** Main function ------------------------------------------------------------- ***/ @@ -242,30 +257,12 @@ modem_helper_open(&callbacks); g_main_id = modem_get_id(my_main_callback); - g_void_id = modem_get_id(NULL); + g_void_id = modem_get_id(my_void_callback); // Put modem to listen to this access class modem_write_file(D7A_FID_DLL_CFG, &ping_pong_xcl, 0, sizeof(d7a_xcl_t), g_main_id); modem_ready[g_main_id].wait(); - alp_retry_policy_t old_policy; - - // Get old retry policy - modem_read_file(WM_FID_ALP_CFG, &old_policy, MY_POLICY_IDX * sizeof(alp_retry_policy_t), sizeof(alp_retry_policy_t), g_main_id); - modem_ready[g_main_id].wait(); - - if (!memcmp(&old_policy, &my_policy, sizeof(alp_retry_policy_t))) - { - // Set custom retry policy - modem_write_file(WM_FID_ALP_CFG, &my_policy, MY_POLICY_IDX * sizeof(alp_retry_policy_t), sizeof(alp_retry_policy_t), g_main_id); - modem_ready[g_main_id].wait(); - modem_flush_file(WM_FID_ALP_CFG, g_main_id); - modem_ready[g_main_id].wait(); - - // XXX We need to reboot the modem for the changes to be applied - my_reset(); - } - PRINT("Start D7A Stack\n"); modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP | ALP_D7A_ISTAT_UNS, true, g_main_id); modem_ready[g_main_id].wait();