diff --git a/src/TTNLogging.cpp b/src/TTNLogging.cpp index 550002d..d03912a 100644 --- a/src/TTNLogging.cpp +++ b/src/TTNLogging.cpp @@ -43,10 +43,21 @@ struct TTNLogMessage { u1_t saveIrqFlags; }; +static const char* const SF_NAMES[] = { "FSK", "SF7", "SF8", "SF9", "SF10", "SF11", "SF12", "SFrfu" }; +static const char* const BW_NAMES[] = { "BW125", "BW250", "BW500", "BWrfu" }; +static const char* const CR_NAMES[] = { "CR 4/5", "CR 4/6", "CR 4/7", "CR 4/8" }; +static const char* const CRC_NAMES[] = { "NoCrc", "Crc" }; -void TTNLogging::initInstance() +static void printMessage(TTNLogMessage* log); +static void printEvtJoined(TTNLogMessage* log); +static void printEvtJoinFailed(TTNLogMessage* log); +static void bin2hex(const uint8_t* bin, unsigned len, char* buf, char sep = 0); + + +TTNLogging* TTNLogging::initInstance() { ttnLog.init(); + return &ttnLog; } void TTNLogging::init() @@ -122,25 +133,99 @@ void TTNLogging::loggingTask(void* param) if (log == nullptr) continue; - if (log->event == -1) - { - ESP_LOGI(TAG, "%s: opmode=0x%x", log->message, log->opmode); - } - else if (log->event == -2) - { - ESP_LOGI(TAG, "%s: datum=0x%x, opmode=0x%x)", log->message, log->datum, log->opmode); - } - else if (log->event == -3) - { - ESP_LOGE(TAG, "%s, %d: freq=%d.%d", - log->message, log->datum, - log->freq / 1000000, (log->freq % 1000000) / 100000 - ); - } + printMessage(log); vRingbufferReturnItem(ringBuffer, log); } } +void printMessage(TTNLogMessage* log) +{ + switch((int)log->event) + { + case -1: + ESP_LOGI(TAG, "%s: opmode=0x%x", log->message, log->opmode); + break; + + case -2: + ESP_LOGI(TAG, "%s: datum=0x%x, opmode=0x%x)", log->message, log->datum, log->opmode); + break; + + case -3: + ESP_LOGE(TAG, "%s, %d: freq=%d.%d", + log->message, log->datum, + log->freq / 1000000, (log->freq % 1000000) / 100000 + ); + break; + + case EV_JOINED: + printEvtJoined(log); + break; + + case EV_JOIN_FAILED: + printEvtJoinFailed(log); + break; + + default: + break; + } +} + + +void printEvtJoined(TTNLogMessage* log) +{ + ESP_LOGI(TAG, "%s: ch=%d", log->message, (unsigned)log->txChnl); + + u4_t netid = 0; + devaddr_t devaddr = 0; + u1_t nwkKey[16]; + u1_t artKey[16]; + LMIC_getSessionKeys(&netid, &devaddr, nwkKey, artKey); + + ESP_LOGI(TAG, "netid: %d", netid); + + ESP_LOGI(TAG, "devaddr: %08x", devaddr); + + char hexBuf[48]; + bin2hex((uint8_t*)&artKey, sizeof(artKey), hexBuf, '-'); + ESP_LOGI(TAG, "artKey: %s", hexBuf); + + bin2hex((uint8_t*)&nwkKey, sizeof(nwkKey), hexBuf, '-'); + ESP_LOGI(TAG, "nwkKey: %s", hexBuf); +} + + +void printEvtJoinFailed(TTNLogMessage* log) +{ + rps_t rps = log->rps; + ESP_LOGE(TAG, "%s: freq=%d.%d, opmode=0x%x, rps=0x%02x (%s, %s, %s, %s, IH=%d)", + log->message, + log->freq / 1000000, (log->freq % 1000000) / 100000, + log->opmode, + rps, + SF_NAMES[getSf(rps)], + BW_NAMES[getBw(rps)], + CR_NAMES[getCr(rps)], + CRC_NAMES[getNocrc(rps)], + getIh(rps) + ); +} + + +static const char* HEX_DIGITS = "0123456789ABCDEF"; + +void bin2hex(const uint8_t* bin, unsigned len, char* buf, char sep) +{ + int tgt = 0; + for (int i = 0; i < len; i++) { + if (sep != 0 && i != 0) + buf[tgt++] = sep; + buf[tgt++] = HEX_DIGITS[bin[i] >> 4]; + buf[tgt++] = HEX_DIGITS[bin[i] & 0xf]; + } + buf[tgt] = 0; +} + + #endif diff --git a/src/TTNLogging.h b/src/TTNLogging.h index 003cac8..b5c267a 100644 --- a/src/TTNLogging.h +++ b/src/TTNLogging.h @@ -22,7 +22,7 @@ class TTNLogging { public: - static void initInstance(); + static TTNLogging* initInstance(); void init(); void logEvent(int event, const char* message, uint32_t datum); diff --git a/src/TheThingsNetwork.cpp b/src/TheThingsNetwork.cpp index 6ffea96..86c902c 100644 --- a/src/TheThingsNetwork.cpp +++ b/src/TheThingsNetwork.cpp @@ -51,6 +51,9 @@ static TheThingsNetwork* ttnInstance; static QueueHandle_t resultQueue; static TTNClientAction clientAction = eActionUnrelated; static TTNProvisioning provisioning; +#if LMIC_ENABLE_event_logging +static TTNLogging* logging; +#endif static void eventCallback(void* userData, ev_t event); static void messageReceivedCallback(void *userData, uint8_t port, const uint8_t *message, size_t messageSize); @@ -81,7 +84,7 @@ void TheThingsNetwork::configurePins(spi_host_device_t spi_host, uint8_t nss, ui ttn_hal.configurePins(spi_host, nss, rxtx, rst, dio0, dio1); #if LMIC_ENABLE_event_logging - TTNLogging::initInstance(); + logging = TTNLogging::initInstance(); #endif LMIC_registerEventCb(eventCallback, nullptr); @@ -252,17 +255,19 @@ void TheThingsNetwork::setRSSICal(int8_t rssiCal) // --- Callbacks --- -#if CONFIG_LOG_DEFAULT_LEVEL >= 3 -static const char *eventNames[] = { LMIC_EVENT_NAME_TABLE__INIT }; +#if CONFIG_LOG_DEFAULT_LEVEL >= 3 || LMIC_ENABLE_event_logging +const char *eventNames[] = { LMIC_EVENT_NAME_TABLE__INIT }; #endif void eventCallback(void* userData, ev_t event) { - #if CONFIG_LOG_DEFAULT_LEVEL >= 3 - ESP_LOGI(TAG, "event %s", eventNames[event]); - #endif +#if LMIC_ENABLE_event_logging + logging->logEvent(event, eventNames[event], 0); +#elif CONFIG_LOG_DEFAULT_LEVEL >= 3 + ESP_LOGI(TAG, "event %s", eventNames[event]); +#endif if (event == EV_TXCOMPLETE) { if (LMIC.txrxFlags & TXRX_ACK)