From f421db44d70ac13381dbaa6d0168753102e9309d Mon Sep 17 00:00:00 2001 From: Manuel Bleichenbacher Date: Sun, 25 Jul 2021 20:49:02 +0200 Subject: [PATCH] Convert logging to C --- .vscode/settings.json | 6 +- src/TheThingsNetwork.cpp | 9 +-- src/{TTNLogging.cpp => ttn_logging.c} | 104 +++++++++++++------------- src/{TTNLogging.h => ttn_logging.h} | 31 ++++---- 4 files changed, 74 insertions(+), 76 deletions(-) rename src/{TTNLogging.cpp => ttn_logging.c} (85%) rename src/{TTNLogging.h => ttn_logging.h} (69%) diff --git a/.vscode/settings.json b/.vscode/settings.json index 532e202..85be09c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,6 +10,10 @@ "__config": "c", "__nullptr": "c", "stdint.h": "c", - "*.ipp": "c" + "*.ipp": "c", + "algorithm": "c", + "random": "c", + "complex": "c", + "valarray": "c" } } \ No newline at end of file diff --git a/src/TheThingsNetwork.cpp b/src/TheThingsNetwork.cpp index a2d8356..3222c66 100644 --- a/src/TheThingsNetwork.cpp +++ b/src/TheThingsNetwork.cpp @@ -17,7 +17,7 @@ #include "lmic/lmic.h" #include "TheThingsNetwork.h" #include "ttn_provisioning.h" -#include "TTNLogging.h" +#include "ttn_logging.h" /** @@ -59,9 +59,6 @@ static const char *TAG = "ttn"; static TheThingsNetwork* ttnInstance; static QueueHandle_t lmicEventQueue = nullptr; static TTNWaitingReason waitingReason = eWaitingNone; -#if LMIC_ENABLE_event_logging -static TTNLogging* logging; -#endif static TTNRFSettings lastRfSettings[4]; static TTNRxTxWindow currentWindow; @@ -95,7 +92,7 @@ void TheThingsNetwork::configurePins(spi_host_device_t spi_host, uint8_t nss, ui hal_esp32_configure_pins(spi_host, nss, rxtx, rst, dio0, dio1); #if LMIC_ENABLE_event_logging - logging = TTNLogging::initInstance(); + ttn_log_init(); #endif LMIC_registerEventCb(eventCallback, nullptr); @@ -364,7 +361,7 @@ void eventCallback(void* userData, ev_t event) }; #if LMIC_ENABLE_event_logging - logging->logEvent(event, eventNames[event], 0); + ttn_log_event(event, eventNames[event], 0); #elif CONFIG_LOG_DEFAULT_LEVEL >= 3 ESP_LOGI(TAG, "event %s", eventNames[event]); #endif diff --git a/src/TTNLogging.cpp b/src/ttn_logging.c similarity index 85% rename from src/TTNLogging.cpp rename to src/ttn_logging.c index 2107bc1..0810bf0 100644 --- a/src/TTNLogging.cpp +++ b/src/ttn_logging.c @@ -2,7 +2,7 @@ * * ttn-esp32 - The Things Network device library for ESP-IDF / SX127x * - * Copyright (c) 2018-2019 Manuel Bleichenbacher + * Copyright (c) 2018-2021 Manuel Bleichenbacher * * Licensed under MIT License * https://opensource.org/licenses/MIT @@ -13,24 +13,24 @@ #if LMIC_ENABLE_event_logging -#include -#include -#include -#include +#include "ttn_logging.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_log.h" #include "lmic/lmic.h" -#include "TTNLogging.h" +#include #define NUM_RINGBUF_MSG 50 -static const char* const TAG = "lmic"; -static TTNLogging ttnLog; +#define TAG "lmic" + /** * @brief Message structure used in ring buffer * * The structure is sent from the LMIC task to the logging task. */ -struct TTNLogMessage { +typedef struct { const char* message; uint32_t datum; ev_t event; @@ -47,13 +47,10 @@ struct TTNLogMessage { u1_t datarate; u1_t txrxFlags; u1_t saveIrqFlags; -}; +} TTNLogMessage; -// Constants for formatting LORA values -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" }; +static void loggingTask(void* param); +static void logFatal(const char* file, uint16_t line); static void printMessage(TTNLogMessage* log); static void printFatalError(TTNLogMessage* log); @@ -64,77 +61,78 @@ static void printEvtTxComplete(TTNLogMessage* log); static void printEvtTxStart(TTNLogMessage* log); static void printEvtRxStart(TTNLogMessage* log); static void printEvtJoinTxComplete(TTNLogMessage* log); -static void bin2hex(const uint8_t* bin, unsigned len, char* buf, char sep = 0); +static void bin2hex(const uint8_t* bin, unsigned len, char* buf, char sep); +// Constants for formatting LORA values +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" }; + +static RingbufHandle_t ringBuffer; -// Create singleton instance -TTNLogging* TTNLogging::initInstance() -{ - ttnLog.init(); - return &ttnLog; -} // Initialize logging -void TTNLogging::init() +void ttn_log_init(void) { ringBuffer = xRingbufferCreate(NUM_RINGBUF_MSG * sizeof(TTNLogMessage), RINGBUF_TYPE_NOSPLIT); - if (ringBuffer == nullptr) { + if (ringBuffer == NULL) { ESP_LOGE(TAG, "Failed to create ring buffer"); ASSERT(0); } - xTaskCreate(loggingTask, "ttn_log", 1024 * 4, ringBuffer, 4, nullptr); + xTaskCreate(loggingTask, "ttn_log", 1024 * 4, ringBuffer, 4, NULL); hal_set_failure_handler(logFatal); } // Record a logging event for later output -void TTNLogging::logEvent(int event, const char* message, uint32_t datum) +void ttn_log_event(int event, const char* message, uint32_t datum) { - if (ringBuffer == nullptr) + if (ringBuffer == NULL) return; - TTNLogMessage log; - log.message = message; - log.datum = datum; - // capture state - log.time = os_getTime(); - log.txend = LMIC.txend; - log.globalDutyAvail = LMIC.globalDutyAvail; - log.event = (ev_t)event; - log.freq = LMIC.freq; - log.opmode = LMIC.opmode; - log.fcntDn = (u2_t) LMIC.seqnoDn; - log.fcntUp = (u2_t) LMIC.seqnoUp; - log.rxsyms = LMIC.rxsyms; - log.rps = LMIC.rps; - log.txChnl = LMIC.txChnl; - log.datarate = LMIC.datarate; - log.txrxFlags = LMIC.txrxFlags; - log.saveIrqFlags = LMIC.saveIrqFlags; + TTNLogMessage log = { + .message = message, + .datum = datum, + .time = os_getTime(), + .txend = LMIC.txend, + .globalDutyAvail = LMIC.globalDutyAvail, + .event = (ev_t)event, + .freq = LMIC.freq, + .opmode = LMIC.opmode, + .fcntDn = (u2_t) LMIC.seqnoDn, + .fcntUp = (u2_t) LMIC.seqnoUp, + .rxsyms = LMIC.rxsyms, + .rps = LMIC.rps, + .txChnl = LMIC.txChnl, + .datarate = LMIC.datarate, + .txrxFlags = LMIC.txrxFlags, + .saveIrqFlags = LMIC.saveIrqFlags, + }; xRingbufferSend(ringBuffer, &log, sizeof(log), 0); } // record a fatal event (failed assert) for later output -void TTNLogging::logFatal(const char* file, uint16_t line) +void logFatal(const char* const file, const uint16_t line) { - ttnLog.logEvent(-3, file, line); + ttn_log_event(-3, file, line); } // Record an informational message for later output // The message must not be freed. -extern "C" void LMICOS_logEvent(const char *pMessage) +void LMICOS_logEvent(const char *pMessage) { - ttnLog.logEvent(-1, pMessage, 0); + ttn_log_event(-1, pMessage, 0); } // Record an information message with an integer value for later output // The message must not be freed. -extern "C" void LMICOS_logEventUint32(const char *pMessage, uint32_t datum) +void LMICOS_logEventUint32(const char *pMessage, uint32_t datum) { - ttnLog.logEvent(-2, pMessage, datum); + ttn_log_event(-2, pMessage, datum); } @@ -142,14 +140,14 @@ extern "C" void LMICOS_logEventUint32(const char *pMessage, uint32_t datum) // Log output // Tasks that receiveds the recorded messages, formats and outputs them. -void TTNLogging::loggingTask(void* param) +void loggingTask(void* param) { RingbufHandle_t ringBuffer = (RingbufHandle_t)param; while (true) { size_t size; TTNLogMessage* log = (TTNLogMessage*) xRingbufferReceive(ringBuffer, &size, portMAX_DELAY); - if (log == nullptr) + if (log == NULL) continue; printMessage(log); diff --git a/src/TTNLogging.h b/src/ttn_logging.h similarity index 69% rename from src/TTNLogging.h rename to src/ttn_logging.h index 2295603..681323a 100644 --- a/src/TTNLogging.h +++ b/src/ttn_logging.h @@ -2,7 +2,7 @@ * * ttn-esp32 - The Things Network device library for ESP-IDF / SX127x * - * Copyright (c) 2018-2019 Manuel Bleichenbacher + * Copyright (c) 2018-2021 Manuel Bleichenbacher * * Licensed under MIT License * https://opensource.org/licenses/MIT @@ -10,8 +10,8 @@ * Circular buffer for detailed logging without affecting LMIC timing. *******************************************************************************/ -#ifndef _ttnlogging_h_ -#define _ttnlogging_h_ +#ifndef TTN_LOGGING_H +#define TTN_LOGGING_H #if LMIC_ENABLE_event_logging @@ -20,8 +20,13 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + + /** - * @brief Logging class. + * @brief Logging functions. * * Logs internal information from LMIC in an asynchrnous fashion in order * not to distrub the sensitive LORA timing. @@ -33,22 +38,16 @@ * * In order to activate the detailed logging, set the macro * `LMIC_ENABLE_event_logging` to 1. - * - * This class is not to be used directly. */ -class TTNLogging { -public: - static TTNLogging* initInstance(); - void init(); - void logEvent(int event, const char* message, uint32_t datum); +void ttn_log_init(void); +void ttn_log_event(int event, const char* message, uint32_t datum); -private: - static void loggingTask(void* param); - static void logFatal(const char* file, uint16_t line); - RingbufHandle_t ringBuffer; -}; +#ifdef __cplusplus +} +#endif + #endif