Ping pong app demo.
Dependencies: modem_ref_helper DebouncedInterrupt
main.cpp
- Committer:
- Jeej
- Date:
- 2018-03-05
- Revision:
- 6:287a9759d70a
- Parent:
- 3:0979d8cba5ec
- Child:
- 7:7524fab147aa
File content as of revision 6:287a9759d70a:
// @autor: jeremie@wizzilab.com // @date: 2017-05-02 #include "DebouncedInterrupt.h" #include "modem_ref_helper.h" #include "modem_callbacks.h" Semaphore button_user(0); Semaphore modem_ready[MAX_USER_NB]; enum { MODEM_RESP_NO, MODEM_RESP_TERMINAL, MODEM_RESP_DONE, }; uint8_t g_main_id; uint8_t g_void_id; typedef struct { Thread* thread; uint32_t count; 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) #define MAX_PING_NB MAX_USER_NB-2 // Special access classes for tests: no duty cycle limit, continuous scan // { .bf.s = 12, .bf.m = 1 }; --> High Rate // { .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 }; // This describe the interface used for communication // Do not modify uncommented parameters alp_d7a_itf_t my_itf = { .type = ALP_ITF_TYPE_D7A, .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.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, // No response //.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 reachable devices (2 to 32) }; void print_status(int status) { switch (status) { case ALP_ERR_NONE: //PRINT("Status: OK\n"); break; case ALP_ERR_FILE_EXIST: PRINT("Status: Already registered\n"); break; default: PRINT("Status: error %d\n", status); break; } } void print_resp(int status) { switch (status) { case ALP_ERR_NONE: //PRINT("Resp: OK\n"); break; case ALP_ERR_FILE_EXIST: PRINT("Resp: Already registered\n"); break; default: PRINT("Resp: error %d\n", status); break; } } // Callback for User void my_main_callback(uint8_t terminal, int8_t err, uint8_t id) { (void)id; if (terminal) { print_status(err); modem_ready[id].release(); } else { print_resp(err); } } void my_void_callback(uint8_t terminal, int8_t err, uint8_t id) { (void)terminal; (void)id; print_status(err); } // Interrupt Service Routine on button press. void button_push_isr( void ) { button_user.release(); } #ifdef DEBUG_LED 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; // 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); #ifdef DEBUG_LED my_led = 1; #endif Thread::wait(PING_DELAY); #ifdef DEBUG_LED 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); } } } void button_user_thread() { d7a_sp_res_t istat; uint32_t ping = 0; FPRINT("(id:0x%08x)\r\n", osThreadGetId()); while (true) { // Wait for button press PRINT("PRESS BUTTON TO INITIATE PING...\n"); button_user.wait(); // Initiate ping PRINT("Initiate PING\n"); modem_send_file_content((uint8_t*)&my_itf, D7_ITF_SIZE(&my_itf), (void*)&istat, FID_PING_PONG, &ping, 0, PING_COUNTER_SIZE, g_main_id); modem_ready[g_main_id].wait(); } } modem_callbacks_t callbacks = { .read = my_read, .write = my_write, .read_fprop = my_read_fprop, .flush = my_flush, .remove = my_delete, .udata = my_udata, .lqual = my_lqual, .ldown = my_ldown, .reset = my_reset, .boot = my_boot }; /*** Main function ------------------------------------------------------------- ***/ int main() { // Start & initialize #ifdef DEBUG_LED DBG_OPEN(DEBUG_LED); #else DBG_OPEN(NC); #endif PRINT("\n" "-----------------------------------------\n" "------------ Demo Ping Pong -------------\n" "-----------------------------------------\n"); FPRINT("(id:0x%08x)\r\n", osThreadGetId()); modem_helper_open(&callbacks); g_main_id = modem_get_id(my_main_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(); #ifdef DEBUG_BUTTON DebouncedInterrupt user_interrupt(DEBUG_BUTTON); user_interrupt.attach(button_push_isr, IRQ_FALL, 500, true); Thread but_th(osPriorityNormal, 1024, NULL); osStatus status = but_th.start(button_user_thread); ASSERT(status == osOK, "Failed to start but thread (err: %d)\r\n", status); #endif // Set main task to lowest priority osThreadSetPriority(osThreadGetId(), osPriorityIdle); while(true) { Thread::wait(500); } }