mirror of
https://github.com/manuelbl/ttn-esp32.git
synced 2025-07-27 03:42:51 +02:00
Receive downlink messages
This commit is contained in:
@ -112,12 +112,16 @@ bool TheThingsNetwork::join(const char *devEui, const char *appEui, const char *
|
||||
|
||||
bool TheThingsNetwork::join()
|
||||
{
|
||||
int result = 0;
|
||||
// empty queue
|
||||
while (xQueueReceive(result_queue, &result, 0) == pdTRUE)
|
||||
;
|
||||
|
||||
hal_enterCriticalSection();
|
||||
LMIC_startJoining();
|
||||
hal_wakeUp();
|
||||
hal_leaveCriticalSection();
|
||||
|
||||
int result = 0;
|
||||
while (true)
|
||||
{
|
||||
xQueueReceive(result_queue, &result, portMAX_DELAY);
|
||||
@ -130,6 +134,11 @@ bool TheThingsNetwork::join()
|
||||
|
||||
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();
|
||||
if (LMIC.opmode & OP_TXRXPEND)
|
||||
{
|
||||
@ -141,9 +150,28 @@ ttn_response_t TheThingsNetwork::sendBytes(const uint8_t *payload, size_t length
|
||||
hal_wakeUp();
|
||||
hal_leaveCriticalSection();
|
||||
|
||||
int result = 0;
|
||||
xQueueReceive(result_queue, &result, portMAX_DELAY);
|
||||
return result == EV_TXCOMPLETE ? TTN_SUCCESSFUL_TRANSMISSION : TTN_ERROR_SEND_COMMAND_FAILED;
|
||||
|
||||
if (result == EV_TXCOMPLETE)
|
||||
{
|
||||
bool hasRecevied = (LMIC.txrxFlags & (TXRX_DNW1 | TXRX_DNW2)) != 0;
|
||||
if (hasRecevied && messageCallback != NULL)
|
||||
{
|
||||
port_t port = 0;
|
||||
if ((LMIC.txrxFlags & TXRX_PORT))
|
||||
port = LMIC.frame[LMIC.dataBeg - 1];
|
||||
messageCallback(LMIC.frame + LMIC.dataBeg, LMIC.dataLen, port);
|
||||
}
|
||||
|
||||
return hasRecevied ? TTN_SUCCESSFUL_RECEIVE : TTN_SUCCESSFUL_TRANSMISSION;
|
||||
}
|
||||
|
||||
return TTN_ERROR_SEND_COMMAND_FAILED;
|
||||
}
|
||||
|
||||
void TheThingsNetwork::onMessage(message_cb_t callback)
|
||||
{
|
||||
messageCallback = callback;
|
||||
}
|
||||
|
||||
|
||||
@ -190,9 +218,7 @@ void onEvent (ev_t ev) {
|
||||
|
||||
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);
|
||||
ESP_LOGI(TAG, "ACK received\n");
|
||||
}
|
||||
|
||||
int result = ev;
|
||||
|
@ -25,6 +25,7 @@
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "rom/ets_sys.h"
|
||||
#include "lmic.h"
|
||||
|
||||
// ----------------------------------------
|
||||
@ -500,13 +501,14 @@ static void txlora () {
|
||||
// select LoRa modem (from sleep mode)
|
||||
//writeReg(RegOpMode, OPMODE_LORA);
|
||||
opmodeLora();
|
||||
// can take a moment to change; so try five times
|
||||
// can take a moment to change; so try ten times
|
||||
u1_t reg;
|
||||
for (int i = 0; i < 5; i++)
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
reg = readReg(RegOpMode);
|
||||
if ((reg & OPMODE_LORA) != 0)
|
||||
break;
|
||||
ets_delay_us(100);
|
||||
}
|
||||
ASSERT((reg & OPMODE_LORA) != 0);
|
||||
|
||||
|
Reference in New Issue
Block a user