introduced LMIC user events

This commit is contained in:
Verkehrsrot 2019-08-31 15:19:49 +02:00
parent 9475c5e633
commit 80a24f10ab
8 changed files with 62 additions and 44 deletions

View File

@ -37,6 +37,9 @@ void switch_lora(uint8_t sf, uint8_t tx);
void lora_send(void *pvParameters);
void lora_enqueuedata(MessageBuffer_t *message);
void lora_queuereset(void);
void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg,
size_t nMsg);
void myTxCallback(void *pUserData, int fSuccess);
#if (TIME_SYNC_LORAWAN)
void user_request_network_time_callback(void *pVoidUserUTCTime,
int flagSuccess);

View File

@ -23,7 +23,7 @@ typedef struct {
const bool store;
} cmd_t;
void rcommand(uint8_t cmd[], uint8_t cmdlength);
void rcommand(const uint8_t cmd[], const uint8_t cmdlength);
void do_reset();
#endif

View File

@ -12,7 +12,7 @@
void timesync_init(void);
void send_timesync_req(void);
int recv_timesync_ans(uint8_t seq_no, uint8_t buf[], uint8_t buf_len);
int recv_timesync_ans(const uint8_t seq_no, const uint8_t buf[], const uint8_t buf_len);
void process_timesync_req(void *taskparameter);
void store_time_sync_req(uint32_t t_millisec);

View File

@ -19,9 +19,14 @@
// LMIC LORAWAN STACK SETTINGS
// --> adapt to your device only if necessary
// use interrupts only if LORA_IRQ and LORA_DIO are connected to interrupt
// capable GPIO pins on your board, if not disable interrupts
//#define LMIC_USE_INTERRUPTS 1
//time sync via LoRaWAN network, is not yet supported by TTN (LoRaWAN spec v1.0.3)
// needed for paxcounter code
#define LMIC_ENABLE_user_events 1
// time sync via LoRaWAN network, note: not supported by TTNv2
// #define LMIC_ENABLE_DeviceTimeReq 1
// 16 μs per tick

View File

@ -244,38 +244,6 @@ void onEvent(ev_t ev) {
strcpy_P(buff, (LMIC.txrxFlags & TXRX_ACK) ? PSTR("RECEIVED ACK")
: PSTR("TX COMPLETE"));
sprintf(display_line6, " "); // clear previous lmic status
if (LMIC.dataLen) { // did we receive payload data -> display info
ESP_LOGI(TAG, "Received %d bytes of payload, RSSI %d SNR %d",
LMIC.dataLen, LMIC.rssi, LMIC.snr / 4);
sprintf(display_line6, "RSSI %d SNR %d", LMIC.rssi, LMIC.snr / 4);
if (LMIC.txrxFlags & TXRX_PORT) { // FPort -> use to switch
switch (LMIC.frame[LMIC.dataBeg - 1]) {
case RCMDPORT: // opcode -> call rcommand interpreter
rcommand(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
break;
default:
#if (TIME_SYNC_LORASERVER)
// timesync answer -> call timesync processor
if ((LMIC.frame[LMIC.dataBeg - 1] >= TIMEANSWERPORT_MIN) &&
(LMIC.frame[LMIC.dataBeg - 1] <= TIMEANSWERPORT_MAX)) {
recv_timesync_ans(LMIC.frame[LMIC.dataBeg - 1],
LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
break;
}
#endif
// unknown port -> display info
ESP_LOGI(TAG, "Received data on unsupported port #%d",
LMIC.frame[LMIC.dataBeg - 1]);
break;
}
}
}
break;
case EV_LOST_TSYNC:
@ -402,9 +370,10 @@ void lora_send(void *pvParameters) {
// attempt to transmit payload
else {
switch (LMIC_setTxData2(SendBuffer.MessagePort, SendBuffer.Message,
SendBuffer.MessageSize,
(cfg.countermode & 0x02))) {
switch (LMIC_sendWithCallback(
SendBuffer.MessagePort, SendBuffer.Message, SendBuffer.MessageSize,
(cfg.countermode & 0x02), myTxCallback, NULL)) {
case 0:
ESP_LOGI(TAG, "%d byte(s) delivered to LMIC", SendBuffer.MessageSize);
break;
@ -554,6 +523,9 @@ void lmictask(void *pvParameters) {
// rate for 125 kHz channels, and it minimizes air time and battery power.
// Set the transmission power to 14 dBi (25 mW).
LMIC_setDrTxpow(DR_SF7, 14);
// register a callback for downlink messages. We aren't trying to write
// reentrant code, so pUserData is NULL.
LMIC_registerRxMessageCb(myRxCallback, NULL);
#if defined(CFG_US915) || defined(CFG_au921)
// in the US, with TTN, it saves join time if we start on subband 1
@ -569,4 +541,41 @@ void lmictask(void *pvParameters) {
}
} // lmictask
// receive message handler
void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg,
size_t nMsg) {
// did we receive payload data -> display info
if (nMsg)
ESP_LOGI(TAG, "Received %d bytes of payload on port %d", nMsg, port);
switch (port) {
// ignore mac messages
case MACPORT:
break;
// rcommand received -> call interpreter
case RCMDPORT:
rcommand(pMsg, nMsg);
break;
default:
#if (TIME_SYNC_LORASERVER)
// valid timesync answer -> call timesync processor
if ((port >= TIMEANSWERPORT_MIN) && (port <= TIMEANSWERPORT_MAX))
recv_timesync_ans(port, pMsg, nMsg);
break;
#endif
// unknown port -> display info
ESP_LOGI(TAG, "Received data on unsupported port %d", port);
break;
} // switch
}
// transmit complete message handler
void myTxCallback(void *pUserData, int fSuccess) { /* currently unused */ }
#endif // HAS_LORA

View File

@ -85,6 +85,7 @@
// Ports on which the device sends and listenes on LoRaWAN and SPI
#define COUNTERPORT 1 // counts
#define MACPORT 0 // network commands
#define RCMDPORT 2 // remote commands
#define STATUSPORT 2 // remote command results
#define CONFIGPORT 3 // config query results

View File

@ -353,7 +353,7 @@ const uint8_t cmdtablesize =
sizeof(table) / sizeof(table[0]); // number of commands in command table
// check and execute remote command
void rcommand(uint8_t cmd[], uint8_t cmdlength) {
void rcommand(const uint8_t cmd[], const uint8_t cmdlength) {
if (cmdlength == 0)
return;

View File

@ -154,7 +154,7 @@ void store_time_sync_req(uint32_t timestamp) {
}
// process timeserver timestamp answer, called from lorawan.cpp
int recv_timesync_ans(uint8_t seq_no, uint8_t buf[], uint8_t buf_len) {
int recv_timesync_ans(const uint8_t seq_no, const uint8_t buf[], const uint8_t buf_len) {
// if no timesync handshake is pending then exit
if (!timeSyncPending)