mirror of
https://github.com/manuelbl/ttn-esp32.git
synced 2025-06-15 20:24:28 +02:00
Improved state handling
This commit is contained in:
parent
b8221a614e
commit
8a37c67e11
@ -18,13 +18,21 @@
|
|||||||
#include "hal_esp32.h"
|
#include "hal_esp32.h"
|
||||||
#include "lmic.h"
|
#include "lmic.h"
|
||||||
|
|
||||||
|
enum ClientAction
|
||||||
|
{
|
||||||
|
eActionUnrelated,
|
||||||
|
eActionJoining,
|
||||||
|
eActionSending
|
||||||
|
};
|
||||||
|
|
||||||
static const char *TAG = "ttn";
|
static const char *TAG = "ttn";
|
||||||
|
|
||||||
static TheThingsNetwork* ttnInstance;
|
static TheThingsNetwork* ttnInstance;
|
||||||
static uint8_t devEui[8];
|
static uint8_t devEui[8];
|
||||||
static uint8_t appEui[8];
|
static uint8_t appEui[8];
|
||||||
static uint8_t appKey[16];
|
static uint8_t appKey[16];
|
||||||
static QueueHandle_t result_queue;
|
static QueueHandle_t resultQueue;
|
||||||
|
static ClientAction clientAction = eActionUnrelated;
|
||||||
|
|
||||||
|
|
||||||
static bool hexStringToBin(const char *hex, uint8_t *buf, int len);
|
static bool hexStringToBin(const char *hex, uint8_t *buf, int len);
|
||||||
@ -58,8 +66,8 @@ void TheThingsNetwork::configurePins(spi_host_device_t spi_host, uint8_t nss, ui
|
|||||||
os_init();
|
os_init();
|
||||||
reset();
|
reset();
|
||||||
|
|
||||||
result_queue = xQueueCreate(12, sizeof(int));
|
resultQueue = xQueueCreate(12, sizeof(int));
|
||||||
assert(result_queue != NULL);
|
assert(resultQueue != NULL);
|
||||||
hal_startBgTask();
|
hal_startBgTask();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,33 +120,19 @@ bool TheThingsNetwork::join(const char *devEui, const char *appEui, const char *
|
|||||||
|
|
||||||
bool TheThingsNetwork::join()
|
bool TheThingsNetwork::join()
|
||||||
{
|
{
|
||||||
int result = 0;
|
|
||||||
// empty queue
|
|
||||||
while (xQueueReceive(result_queue, &result, 0) == pdTRUE)
|
|
||||||
;
|
|
||||||
|
|
||||||
hal_enterCriticalSection();
|
hal_enterCriticalSection();
|
||||||
|
clientAction = eActionJoining;
|
||||||
LMIC_startJoining();
|
LMIC_startJoining();
|
||||||
hal_wakeUp();
|
hal_wakeUp();
|
||||||
hal_leaveCriticalSection();
|
hal_leaveCriticalSection();
|
||||||
|
|
||||||
while (true)
|
int result = 0;
|
||||||
{
|
xQueueReceive(resultQueue, &result, portMAX_DELAY);
|
||||||
xQueueReceive(result_queue, &result, portMAX_DELAY);
|
|
||||||
if (result != EV_JOINING)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result == EV_JOINED;
|
return result == EV_JOINED;
|
||||||
}
|
}
|
||||||
|
|
||||||
ttn_response_t TheThingsNetwork::sendBytes(const uint8_t *payload, size_t length, port_t port, bool confirm)
|
ttn_response_t TheThingsNetwork::sendBytes(const uint8_t *payload, size_t length, port_t port, bool confirm)
|
||||||
{
|
{
|
||||||
int result = 0;
|
|
||||||
// empty queue
|
|
||||||
while (xQueueReceive(result_queue, &result, 0) == pdTRUE)
|
|
||||||
;
|
|
||||||
|
|
||||||
hal_enterCriticalSection();
|
hal_enterCriticalSection();
|
||||||
if (LMIC.opmode & OP_TXRXPEND)
|
if (LMIC.opmode & OP_TXRXPEND)
|
||||||
{
|
{
|
||||||
@ -146,11 +140,13 @@ ttn_response_t TheThingsNetwork::sendBytes(const uint8_t *payload, size_t length
|
|||||||
return TTN_ERROR_SEND_COMMAND_FAILED;
|
return TTN_ERROR_SEND_COMMAND_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clientAction = eActionSending;
|
||||||
LMIC_setTxData2(port, (xref2u1_t)payload, length, confirm);
|
LMIC_setTxData2(port, (xref2u1_t)payload, length, confirm);
|
||||||
hal_wakeUp();
|
hal_wakeUp();
|
||||||
hal_leaveCriticalSection();
|
hal_leaveCriticalSection();
|
||||||
|
|
||||||
xQueueReceive(result_queue, &result, portMAX_DELAY);
|
int result = 0;
|
||||||
|
xQueueReceive(resultQueue, &result, portMAX_DELAY);
|
||||||
|
|
||||||
if (result == EV_TXCOMPLETE)
|
if (result == EV_TXCOMPLETE)
|
||||||
{
|
{
|
||||||
@ -221,8 +217,24 @@ void onEvent (ev_t ev) {
|
|||||||
ESP_LOGI(TAG, "ACK received\n");
|
ESP_LOGI(TAG, "ACK received\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (clientAction == eActionUnrelated)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if (clientAction == eActionJoining)
|
||||||
|
{
|
||||||
|
if (ev != EV_JOINED && EV_REJOIN_FAILED)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (ev != EV_TXCOMPLETE && ev != EV_LINK_DEAD)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
int result = ev;
|
int result = ev;
|
||||||
xQueueSend(result_queue, &result, 100 / portTICK_PERIOD_MS);
|
clientAction = eActionUnrelated;
|
||||||
|
xQueueSend(resultQueue, &result, 100 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user