diff --git a/src/TTNLogging.cpp b/src/TTNLogging.cpp new file mode 100644 index 0000000..3232b3f --- /dev/null +++ b/src/TTNLogging.cpp @@ -0,0 +1,96 @@ +/******************************************************************************* + * + * ttn-esp32 - The Things Network device library for ESP-IDF / SX127x + * + * Copyright (c) 2018-2019 Manuel Bleichenbacher + * + * Licensed under MIT License + * https://opensource.org/licenses/MIT + * + * Circular buffer for detailed logging without affecting LMIC timing. + *******************************************************************************/ + + +#if LMIC_ENABLE_event_logging + +#include +#include +#include "lmic/lmic.h" +#include +#include +#include "TTNLogging.h" + + +#define NO_DATUM 0x7cabcde3 + +static const char* const TAG = "lmic"; +static TTNLogging ttnLog; + +struct TTNLogMessage { + const char* message; + uint32_t datum; +}; + + +void TTNLogging::initInstance() +{ + ttnLog.init(); +} + +void TTNLogging::init() +{ + ringBuffer = xRingbufferCreate(50 * sizeof(TTNLogMessage), RINGBUF_TYPE_NOSPLIT); + if (ringBuffer == NULL) { + ESP_LOGE(TAG, "Failed to create ring buffer"); + ASSERT(0); + } + + xTaskCreate(loggingTask, "ttn_log", 1024 * 4, ringBuffer, 4, NULL); +} + +void TTNLogging::logEvent(const char* message, uint32_t datum) +{ + if (ringBuffer == NULL) + return; + + TTNLogMessage log; + + log.message = message; + log.datum = datum; + + xRingbufferSend(ringBuffer, &log, sizeof(log), 0); +} + + +void TTNLogging::loggingTask(void* param) +{ + RingbufHandle_t ringBuffer = (RingbufHandle_t)param; + + while (true) { + size_t size; + TTNLogMessage* log = (TTNLogMessage*) xRingbufferReceive(ringBuffer, &size, portMAX_DELAY); + if (log == NULL) + continue; + + if (log->datum == NO_DATUM) + ESP_LOGI(TAG, "%s", log->message); + else + ESP_LOGI(TAG, "%s (0x%x)", log->message, log->datum); + + vRingbufferReturnItem(ringBuffer, log); + } +} + + +extern "C" void LMICOS_logEvent(const char *pMessage) +{ + ttnLog.logEvent(pMessage, NO_DATUM); + +} + +extern "C" void LMICOS_logEventUint32(const char *pMessage, uint32_t datum) +{ + ttnLog.logEvent(pMessage, datum); +} + +#endif diff --git a/src/TTNLogging.h b/src/TTNLogging.h new file mode 100644 index 0000000..ff5b2a4 --- /dev/null +++ b/src/TTNLogging.h @@ -0,0 +1,38 @@ +/******************************************************************************* + * + * ttn-esp32 - The Things Network device library for ESP-IDF / SX127x + * + * Copyright (c) 2018-2019 Manuel Bleichenbacher + * + * Licensed under MIT License + * https://opensource.org/licenses/MIT + * + * Circular buffer for detailed logging without affecting LMIC timing. + *******************************************************************************/ + +#ifndef _ttnlogging_h_ +#define _ttnlogging_h_ + + +#if LMIC_ENABLE_event_logging + +#include +#include + + +class TTNLogging { +public: + static void initInstance(); + + void init(); + void logEvent(const char* message, uint32_t datum); + +private: + static void loggingTask(void* param); + + RingbufHandle_t ringBuffer; +}; + +#endif + +#endif diff --git a/src/TTNProvisioning.cpp b/src/TTNProvisioning.cpp index d4e5453..ccadbd6 100644 --- a/src/TTNProvisioning.cpp +++ b/src/TTNProvisioning.cpp @@ -2,7 +2,7 @@ * * ttn-esp32 - The Things Network device library for ESP-IDF / SX127x * - * Copyright (c) 2018 Manuel Bleichenbacher + * Copyright (c) 2018-2019 Manuel Bleichenbacher * * Licensed under MIT License * https://opensource.org/licenses/MIT diff --git a/src/TTNProvisioning.h b/src/TTNProvisioning.h index 30fd9f9..dcd537a 100644 --- a/src/TTNProvisioning.h +++ b/src/TTNProvisioning.h @@ -2,7 +2,7 @@ * * ttn-esp32 - The Things Network device library for ESP-IDF / SX127x * - * Copyright (c) 2018 Manuel Bleichenbacher + * Copyright (c) 2018-2019 Manuel Bleichenbacher * * Licensed under MIT License * https://opensource.org/licenses/MIT diff --git a/src/TheThingsNetwork.cpp b/src/TheThingsNetwork.cpp index b95fc4f..0bba0b9 100644 --- a/src/TheThingsNetwork.cpp +++ b/src/TheThingsNetwork.cpp @@ -2,7 +2,7 @@ * * ttn-esp32 - The Things Network device library for ESP-IDF / SX127x * - * Copyright (c) 2018 Manuel Bleichenbacher + * Copyright (c) 2018-2019 Manuel Bleichenbacher * * Licensed under MIT License * https://opensource.org/licenses/MIT @@ -17,6 +17,7 @@ #include "hal/hal_esp32.h" #include "lmic/lmic.h" #include "TTNProvisioning.h" +#include "TTNLogging.h" enum ClientAction @@ -57,6 +58,10 @@ 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(); +#endif + os_init_ex(NULL); reset(); diff --git a/src/hal/hal_esp32.cpp b/src/hal/hal_esp32.cpp index 7fe31b1..af0d32f 100755 --- a/src/hal/hal_esp32.cpp +++ b/src/hal/hal_esp32.cpp @@ -2,12 +2,12 @@ * * ttn-esp32 - The Things Network device library for ESP-IDF / SX127x * - * Copyright (c) 2018 Manuel Bleichenbacher + * Copyright (c) 2018-2019 Manuel Bleichenbacher * * Licensed under MIT License * https://opensource.org/licenses/MIT * - * Hardware abstraction layer to run LMIC on a ESP32 using ESP-iDF. + * Hardware abstraction layer to run LMIC on a ESP32 using ESP-IDF. *******************************************************************************/ #include "../lmic/lmic.h" diff --git a/src/hal/hal_esp32.h b/src/hal/hal_esp32.h index da5f3c3..86ec185 100644 --- a/src/hal/hal_esp32.h +++ b/src/hal/hal_esp32.h @@ -2,12 +2,12 @@ * * ttn-esp32 - The Things Network device library for ESP-IDF / SX127x * - * Copyright (c) 2018 Manuel Bleichenbacher + * Copyright (c) 2018-2019 Manuel Bleichenbacher * * Licensed under MIT License * https://opensource.org/licenses/MIT * - * Hardware abstraction layer to run LMIC on a ESP32 using ESP-iDF. + * Hardware abstraction layer to run LMIC on a ESP32 using ESP-IDF. *******************************************************************************/ #ifndef _hal_esp32_h_