lorawan.cpp: MAC decoder (experimental)

This commit is contained in:
Verkehrsrot 2019-09-01 00:57:19 +02:00
parent cc2cfd3c13
commit b97361ca8c

View File

@ -21,6 +21,23 @@ static const char TAG[] = "lora";
QueueHandle_t LoraSendQueue;
TaskHandle_t lmicTask = NULL, lorasendTask = NULL;
// table of LORAWAN MAC messages sent by the network to the device
// format: opcode, cmdname (max 19 chars), #bytes params
// source: LoRaWAN 1.1 Specification (October 11, 2017)
static mac_t table[] = {
{0x01, "ResetConf", 1}, {0x02, "LinkCheckAns", 2},
{0x03, "LinkADRReq", 4}, {0x04, "DutyCycleReq", 1},
{0x05, "RXParamSetupReq", 4}, {0x06, "DevStatusReq", 0},
{0x07, "NewChannelReq", 5}, {0x08, "RxTimingSetupReq", 1},
{0x09, "TxParamSetupReq", 1}, {0x0A, "DlChannelReq", 4},
{0x0B, "RekeyConf", 1}, {0x0C, "ADRParamSetupReq", 1},
{0x0D, "DeviceTimeAns", 5}, {0x0E, "ForceRejoinReq", 2},
{0x0F, "RejoinParamSetupReq", 1}};
static const uint8_t cmdtablesize =
sizeof(table) / sizeof(table[0]); // number of commands in MAC table
class MyHalConfig_t : public Arduino_LMIC::HalConfiguration_t {
public:
@ -545,10 +562,6 @@ void lmictask(void *pvParameters) {
void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg,
size_t nMsg) {
// tell the compiler that pUserData is required by the API, but we don't
// happen to use it.
LMIC_API_PARAMETER(pUserData);
// display type of received data
if (nMsg)
ESP_LOGI(TAG, "Received %u bytes of payload on port %u", nMsg, port);
@ -560,7 +573,7 @@ void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg,
if (port != MACPORT)
--nMac;
if (nMac) {
ESP_LOGI(TAG, "Received %u MAC messages:", nMac);
ESP_LOGI(TAG, "Received %u byte MAC message", nMac);
// NOT WORKING YET
// whe need to strip some protocol overhead from LMIC.frame to unwrap the
// MAC command
@ -582,9 +595,10 @@ void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg,
#if (TIME_SYNC_LORASERVER)
// valid timesync answer -> call timesync processor
if ((port >= TIMEANSWERPORT_MIN) && (port <= TIMEANSWERPORT_MAX))
if ((port >= TIMEANSWERPORT_MIN) && (port <= TIMEANSWERPORT_MAX)) {
recv_timesync_ans(port, pMsg, nMsg);
break;
}
#endif
// unknown port -> display info
@ -595,34 +609,10 @@ void myRxCallback(void *pUserData, uint8_t port, const uint8_t *pMsg,
// transmit complete message handler
void myTxCallback(void *pUserData, int fSuccess) {
/* currently no code here */
// tell the compiler that pUserData is required by the API, but we don't
// happen to use it.
LMIC_API_PARAMETER(pUserData);
}
// LORAWAN MAC interpreter
// table of LORAWAN MAC messages sent by the network to the device
// format: opcode, cmdname (max 19 chars), #bytes params
// source: LoRaWAN 1.1 Specification (October 11, 2017)
static mac_t table[] = {
{0x01, "ResetConf", 1}, {0x02, "LinkCheckAns", 2},
{0x03, "LinkADRReq", 4}, {0x04, "DutyCycleReq", 1},
{0x05, "RXParamSetupReq", 4}, {0x06, "DevStatusReq", 0},
{0x07, "NewChannelReq", 5}, {0x08, "RxTimingSetupReq", 1},
{0x09, "TxParamSetupReq", 1}, {0x0A, "DlChannelReq", 4},
{0x0B, "RekeyConf", 1}, {0x0C, "ADRParamSetupReq", 1},
{0x0D, "DeviceTimeAns", 5}, {0x0E, "ForceRejoinReq", 2},
{0x0F, "RejoinParamSetupReq", 1}};
static const uint8_t cmdtablesize =
sizeof(table) / sizeof(table[0]); // number of commands in command table
// decode mac message
// decode LORAWAN MAC message
void mac_decode(const uint8_t cmd[], const uint8_t cmdlength) {
if (!cmdlength)
@ -640,15 +630,15 @@ void mac_decode(const uint8_t cmd[], const uint8_t cmdlength) {
memmove(foundcmd, cmd + cursor,
table[i].params); // strip opcode from cmd array
cursor += table[i].params;
ESP_LOGI(TAG, "Network command %s", table[i].cmdname);
ESP_LOGD(TAG, "Network command %s", table[i].cmdname);
} else
ESP_LOGI(TAG, "MAC message 0x%02X with missing parameter(s)",
ESP_LOGD(TAG, "MAC message 0x%02X with missing parameter(s)",
table[i].opcode);
break; // command found -> exit table lookup loop
} // end of command validation
} // end of command table lookup loop
if (i < 0) { // command not found -> skip it
ESP_LOGI(TAG, "Unknown MAC message 0x%02X", cmd[cursor]);
ESP_LOGD(TAG, "Unknown MAC message 0x%02X", cmd[cursor]);
cursor++;
}
} // command parsing loop