From c1760fed4132e855bbe4145200665a48fcd4f309 Mon Sep 17 00:00:00 2001 From: Manuel Bleichenbacher Date: Tue, 17 Jul 2018 19:46:29 +0200 Subject: [PATCH] Block member functions until operation is complete --- examples/send_msg/main/main.cpp | 6 ++--- src/TheThingsNetwork.cpp | 45 +++++++++++++++++++++++---------- src/radio.c | 10 +++++++- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/examples/send_msg/main/main.cpp b/examples/send_msg/main/main.cpp index 546be72..8e120ad 100644 --- a/examples/send_msg/main/main.cpp +++ b/examples/send_msg/main/main.cpp @@ -34,9 +34,7 @@ static uint8_t msgData[] = "Hello, world"; void send_messages(void* pvParameter) { - vTaskDelay(TX_INTERVAL * 1000 / portTICK_PERIOD_MS); - - while(1) { + while (1) { printf("Sending message...\n"); ttn_response_t res = ttn.sendBytes(msgData, sizeof(msgData) - 1); if (res == TTN_SUCCESSFUL_TRANSMISSION) @@ -68,7 +66,9 @@ extern "C" void app_main(void) ttn.provision(appEui, appKey, devEui); + printf("Joining...\n"); ttn.join(); + printf("Joined.\n"); xTaskCreate(send_messages, "send_messages", 1024 * 4, (void* )0, 3, NULL); } diff --git a/src/TheThingsNetwork.cpp b/src/TheThingsNetwork.cpp index 1a6e2b6..3835d56 100644 --- a/src/TheThingsNetwork.cpp +++ b/src/TheThingsNetwork.cpp @@ -9,6 +9,8 @@ * This the hardware abstraction layer to run LMIC in on ESP32 using ESP-iDF. *******************************************************************************/ +#include "freertos/FreeRTOS.h" +#include "esp_event.h" #include "TheThingsNetwork.h" #include "esp_log.h" #include "oslmic.h" @@ -22,6 +24,8 @@ static TheThingsNetwork* ttnInstance; static uint8_t appEui[8]; static uint8_t appKey[16]; static uint8_t devEui[8]; +static QueueHandle_t result_queue; + static bool hexStringToBin(const char *hex, uint8_t *buf, int len); static int hexTupleToByte(const char *hex); @@ -31,10 +35,12 @@ static void swapByteOrder(uint8_t* buf, int len); TheThingsNetwork::TheThingsNetwork(uint8_t sf, uint8_t fsb) { + ASSERT(ttnInstance == NULL); ttnInstance = this; spreadingFactor = sf; frequencySubband = fsb; hal_initCriticalSection(); + } TheThingsNetwork::~TheThingsNetwork() @@ -54,6 +60,8 @@ void TheThingsNetwork::configurePins(spi_host_device_t spi_host, uint8_t nss, ui os_init(); reset(); + result_queue = xQueueCreate(12, sizeof(int)); + assert(result_queue != NULL); hal_startBgTask(); } @@ -110,7 +118,16 @@ bool TheThingsNetwork::join(int8_t retries, uint32_t retryDelay) LMIC_startJoining(); hal_wakeUp(); hal_leaveCriticalSection(); - return true; + + int result = 0; + while (true) + { + xQueueReceive(result_queue, &result, portMAX_DELAY); + if (result != EV_JOINING) + break; + } + + return result == EV_JOINED; } ttn_response_t TheThingsNetwork::sendBytes(const uint8_t *payload, size_t length, port_t port, bool confirm, uint8_t sf) @@ -125,7 +142,10 @@ ttn_response_t TheThingsNetwork::sendBytes(const uint8_t *payload, size_t length LMIC_setTxData2(port, (xref2u1_t)payload, length, confirm); hal_wakeUp(); hal_leaveCriticalSection(); - return TTN_SUCCESSFUL_TRANSMISSION; + + int result = 0; + xQueueReceive(result_queue, &result, portMAX_DELAY); + return result == EV_TXCOMPLETE ? TTN_SUCCESSFUL_TRANSMISSION : TTN_ERROR_SEND_COMMAND_FAILED; } @@ -167,21 +187,18 @@ void os_getDevKey (u1_t* buf) void onEvent (ev_t ev) { #if CONFIG_LOG_DEFAULT_LEVEL >= 3 - ESP_LOGI(TAG, "%ld: event %s", os_getTime(), eventNames[ev]); + ESP_LOGI(TAG, "event %s", eventNames[ev]); #endif - if (ev == EV_JOINED) - { - // Disable link check validation (automatically enabled - // during join, but not supported by TTN at this time). - LMIC_setLinkCheckMode(0); - } else if (ev == EV_TXCOMPLETE) { - if (LMIC.txrxFlags & TXRX_ACK) - printf("Received ack\n"); - if (LMIC.dataLen) { - printf("Received %d bytes of payload\n", LMIC.dataLen); - } + if (ev == EV_TXCOMPLETE) { + if (LMIC.txrxFlags & TXRX_ACK) + ESP_LOGI(TAG, "Received ack\n"); + if (LMIC.dataLen) + ESP_LOGI(TAG, "Received %d bytes of payload\n", LMIC.dataLen); } + + int result = ev; + xQueueSend(result_queue, &result, 100 / portTICK_PERIOD_MS); } diff --git a/src/radio.c b/src/radio.c index a76967a..a66a2ea 100755 --- a/src/radio.c +++ b/src/radio.c @@ -500,7 +500,15 @@ static void txlora () { // select LoRa modem (from sleep mode) //writeReg(RegOpMode, OPMODE_LORA); opmodeLora(); - ASSERT((readReg(RegOpMode) & OPMODE_LORA) != 0); + // can take a moment to change; so try five times + u1_t reg; + for (int i = 0; i < 5; i++) + { + reg = readReg(RegOpMode); + if ((reg & OPMODE_LORA) != 0) + break; + } + ASSERT((reg & OPMODE_LORA) != 0); // enter standby mode (required for FIFO loading)) opmode(OPMODE_STANDBY);