// -------------------------------------------- // -------------------------------------------- void onRadioRecv(void) { static bool flRxProcessing=false; if(flRxProcessing){ #ifdef PRINT_PACKETS PRINTF("RX Locked\n"); #endif return; } flRxProcessing=true; // There is a chance for a small race condition #ifdef PRINT_PACKETS uint32_t rxTime = getTimeMs(); #endif int16_t rxLen; rssi_t rssi; lqi_t lqi; rxIdx++; if( rxIdx < 0 ) rxIdx=0; led1Toggle(); // rxLen = radioRecv( &(DB_REC(db)), DB_REC_SIZE(db)); // DB_REC(db).recLen = rxLen; rxLen = radioRecv(&radioBuffer, sizeof(radioBuffer)); rssi = radioGetLastRSSI(); lqi = radioGetLastLQI(); #ifdef PRINT_PACKETS PRINTF("%d\t%d\t%d\t%d\t%ld\t", (int)rxIdx, (int)rxLen, (int)rssi, (int)lqi, (long)rxTime); #endif #ifdef PRINT_PACKETS if (rxLen < 0) { PRINTF("RX failed\n"); } else if (rxLen > 0 ) { debugHexdump((uint8_t *) &radioBuffer, rxLen); // debugHexdump((uint8_t *) &(DB_REC(db)), rxLen); } #endif if (rxLen < 0) { led2Toggle(); flRxProcessing=false; return; } if( ! MSG_SIGNATURE_OK(radioBuffer) ) { flRxProcessing = false; return; } // Anticipated payload types. MSG_NEW_PAYLOAD_PTR(radioBuffer, phaser_ping_t, test_data_p); MSG_NEW_PAYLOAD_PTR(radioBuffer, phaser_control_t, ctrl_data_p); MSG_NEW_PAYLOAD_PTR(radioBuffer, msg_text_data_t, msg_text_p); MSG_NEW_PAYLOAD_PTR(radioBuffer, test_config_t, test_config_p); int act = MSG_ACT_CLEAR; bool flOK=true; switch( radioBuffer.id ){ case PH_MSG_Test: MSG_CHECK_FOR_PAYLOAD(radioBuffer, phaser_ping_t, flOK=false ); if( !flOK ){ PRINTF("BadChk\n"); break; } // Check if new experiment iteration started. if(lastExpIdx != test_data_p->expIdx && curExp){ sendTestResults(); } processTestMsg(test_data_p, rssi, lqi); break; case PH_MSG_Angle: if(curExp) sendTestResults(); if( flRestart ){ // Best time to resend the restart message after the angle change send_ctrl_msg(MSG_ACT_RESTART); flRestart = false; } break; case PH_MSG_Control: MSG_CHECK_FOR_PAYLOAD(radioBuffer, phaser_control_t, break); if(curExp) sendTestResults(); act = ctrl_data_p->action; if(act == MSG_ACT_START ){ flRestart = false; // Clear restart command attempt } printAction(act); break; case PH_MSG_Text: MSG_CHECK_FOR_PAYLOAD(radioBuffer, msg_text_data_t, break ); PRINTF(msg_text_p->text); PRINTF("\n"); break; case PH_MSG_Config: MSG_CHECK_FOR_PAYLOAD(radioBuffer, test_config_t, break ); PRINTF("Config received:\n"); // TODO: parse the config and print print_test_config(test_config_p); } flRxProcessing=false; }
/*---------------------------------------------------------------------------*/ static int xio_server_main(void *data) { struct xio_server *server; struct xio_context_params ctx_params; char url[256]; atomic_add(2, &module_state); print_test_config(&test_config); g_test_params.finite_run = test_config.finite_run; g_test_params.disconnect_nr = PRINT_COUNTER * DISCONNECT_FACTOR; memset(&ctx_params, 0, sizeof(ctx_params)); ctx_params.flags = XIO_LOOP_GIVEN_THREAD; ctx_params.worker = current; g_test_params.ctx = xio_context_create(&ctx_params, 0, g_test_params.cpu); if (!g_test_params.ctx) { int error = xio_errno(); pr_err("context creation failed. reason %d - (%s)\n", error, xio_strerror(error)); goto cleanup; } sprintf(url, "%s://%s:%d", test_config.transport, test_config.server_addr, test_config.server_port); server = xio_bind(g_test_params.ctx, &server_ops, url, NULL, 0, &g_test_params); if (server) { pr_info("listen to %s\n", url); if (atomic_add_unless(&module_state, 4, 0x83)) xio_context_run_loop(g_test_params.ctx); atomic_sub(4, &module_state); /* normal exit phase */ pr_info("exit signaled\n"); /* free the server */ xio_unbind(server); } else { pr_err("**** Error - xio_bind failed - %s. " \ "Did you load a transport module?\n", xio_strerror(xio_errno())); /*xio_assert(0);*/ } xio_context_destroy(g_test_params.ctx); kfree(g_test_params.xbuf); g_test_params.xbuf = NULL; cleanup: complete_and_exit(&cleanup_complete, 0); return 0; }
/*---------------------------------------------------------------------------*/ static int xio_client_main(void *data) { char url[256]; struct xio_session_params params; struct xio_context_params ctx_params; struct xio_connection_params cparams; int error; int retval = 0; atomic_add(2, &module_state); print_counter = PRINT_COUNTER; print_test_config(&test_config); memset(¶ms, 0, sizeof(params)); memset(&cparams, 0, sizeof(cparams)); /* prepare buffers for this test */ if (msg_api_init(&msg_params, test_config.hdr_len, test_config.data_len, 0) != 0) { pr_err("msg_api_init failed\n"); return -1; } pool = msg_pool_alloc(MAX_POOL_SIZE, 1, 1); if (!pool) { pr_err("msg_pool_alloc failed\n"); goto cleanup; } /* create thread context for the server */ memset(&ctx_params, 0, sizeof(ctx_params)); ctx_params.flags = XIO_LOOP_GIVEN_THREAD; ctx_params.worker = current; ctx = xio_context_create(&ctx_params, POLLING_TIMEOUT, cpu); if (!ctx) { pr_err("context open failed\n"); goto cleanup; } sprintf(url, "%s://%s:%d", test_config.transport, test_config.server_addr, test_config.server_port); params.type = XIO_SESSION_CLIENT; params.ses_ops = &ses_ops; params.uri = url; g_session = xio_session_create(¶ms); if (!g_session) pr_err("session creation failed\n"); cparams.session = g_session; cparams.ctx = ctx; cparams.conn_idx = test_config.conn_idx; /* connect the session */ g_connection = xio_connect(&cparams); /* the default xio supplied main loop */ if (atomic_add_unless(&module_state, 4, 0x83)) retval = xio_context_run_loop(ctx); atomic_sub(4, &module_state); if (retval != 0) { error = xio_errno(); pr_err("running event loop failed. reason %d - (%s)\n", error, xio_strerror(error)); xio_assert(retval == 0); } /* normal exit phase */ pr_info("exit signaled\n"); xio_context_destroy(ctx); msg_pool_free(pool); cleanup: msg_api_free(&msg_params); pr_info("exit complete\n"); complete_and_exit(&cleanup_complete, 0); return 0; }