Ping pong app demo.
Dependencies: modem_ref_helper DebouncedInterrupt
main.cpp
- Committer:
- Jeej
- Date:
- 2019-08-02
- Revision:
- 10:b69fe2a98432
- Parent:
- 9:63592df2faad
- Child:
- 11:aa9c3fdf2303
File content as of revision 10:b69fe2a98432:
// @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 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 // 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 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 = 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] = 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] = D7A_CTF_ENCODE(4), // Estimation of expected responses (1 to 32) }; // Callback for User void my_main_callback(uint8_t terminal, int8_t err, uint8_t id) { (void)id; if (ALP_ERR_NONE != err) { modem_print_error(ALP_ITF_TYPE_D7A, err); } if (terminal) { modem_ready[id].release(); } } void my_void_callback(uint8_t terminal, int8_t err, uint8_t id) { (void)terminal; (void)id; if (ALP_ERR_NONE != err) { modem_print_error(ALP_ITF_TYPE_D7A, 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(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 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; #endif ThisThread::sleep_for(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 = 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; FPRINT("(id:0x%08x)\r\n", osThreadGetId()); while (true) { // Wait for button press PRINT("PRESS BUTTON TO INITIATE PING...\n"); button_user.acquire(); // Initiate ping PRINT("Initiate PING\n"); modem_send_file_content((uint8_t*)&my_itf, D7_ITF_SIZE(&my_itf), NULL, FID_PING_PONG, &ping, 0, PING_COUNTER_SIZE, g_main_id); modem_ready[g_main_id].acquire(); } } 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, .busy = my_busy, }; /*** 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); 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].acquire(); PRINT("Start D7A Stack\n"); modem_activate_itf(ALP_ITF_TYPE_D7A, 24, 0, ALP_D7A_ISTAT_RESP | ALP_D7A_ISTAT_UNS | ALP_D7A_ISTAT_EOP, true, g_main_id); modem_ready[g_main_id].acquire(); #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(), osPriorityLow); while(true) { ThisThread::sleep_for(500); } }