Convert logging to C

This commit is contained in:
Manuel Bleichenbacher 2021-07-25 20:49:02 +02:00
parent 8e2886db27
commit f421db44d7
4 changed files with 74 additions and 76 deletions

View File

@ -10,6 +10,10 @@
"__config": "c", "__config": "c",
"__nullptr": "c", "__nullptr": "c",
"stdint.h": "c", "stdint.h": "c",
"*.ipp": "c" "*.ipp": "c",
"algorithm": "c",
"random": "c",
"complex": "c",
"valarray": "c"
} }
} }

View File

@ -17,7 +17,7 @@
#include "lmic/lmic.h" #include "lmic/lmic.h"
#include "TheThingsNetwork.h" #include "TheThingsNetwork.h"
#include "ttn_provisioning.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 TheThingsNetwork* ttnInstance;
static QueueHandle_t lmicEventQueue = nullptr; static QueueHandle_t lmicEventQueue = nullptr;
static TTNWaitingReason waitingReason = eWaitingNone; static TTNWaitingReason waitingReason = eWaitingNone;
#if LMIC_ENABLE_event_logging
static TTNLogging* logging;
#endif
static TTNRFSettings lastRfSettings[4]; static TTNRFSettings lastRfSettings[4];
static TTNRxTxWindow currentWindow; 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); hal_esp32_configure_pins(spi_host, nss, rxtx, rst, dio0, dio1);
#if LMIC_ENABLE_event_logging #if LMIC_ENABLE_event_logging
logging = TTNLogging::initInstance(); ttn_log_init();
#endif #endif
LMIC_registerEventCb(eventCallback, nullptr); LMIC_registerEventCb(eventCallback, nullptr);
@ -364,7 +361,7 @@ void eventCallback(void* userData, ev_t event)
}; };
#if LMIC_ENABLE_event_logging #if LMIC_ENABLE_event_logging
logging->logEvent(event, eventNames[event], 0); ttn_log_event(event, eventNames[event], 0);
#elif CONFIG_LOG_DEFAULT_LEVEL >= 3 #elif CONFIG_LOG_DEFAULT_LEVEL >= 3
ESP_LOGI(TAG, "event %s", eventNames[event]); ESP_LOGI(TAG, "event %s", eventNames[event]);
#endif #endif

View File

@ -2,7 +2,7 @@
* *
* ttn-esp32 - The Things Network device library for ESP-IDF / SX127x * 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 * Licensed under MIT License
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
@ -13,24 +13,24 @@
#if LMIC_ENABLE_event_logging #if LMIC_ENABLE_event_logging
#include <string.h> #include "ttn_logging.h"
#include <esp_log.h> #include "freertos/FreeRTOS.h"
#include <freertos/FreeRTOS.h> #include "freertos/task.h"
#include <freertos/task.h> #include "esp_log.h"
#include "lmic/lmic.h" #include "lmic/lmic.h"
#include "TTNLogging.h" #include <string.h>
#define NUM_RINGBUF_MSG 50 #define NUM_RINGBUF_MSG 50
static const char* const TAG = "lmic"; #define TAG "lmic"
static TTNLogging ttnLog;
/** /**
* @brief Message structure used in ring buffer * @brief Message structure used in ring buffer
* *
* The structure is sent from the LMIC task to the logging task. * The structure is sent from the LMIC task to the logging task.
*/ */
struct TTNLogMessage { typedef struct {
const char* message; const char* message;
uint32_t datum; uint32_t datum;
ev_t event; ev_t event;
@ -47,13 +47,10 @@ struct TTNLogMessage {
u1_t datarate; u1_t datarate;
u1_t txrxFlags; u1_t txrxFlags;
u1_t saveIrqFlags; u1_t saveIrqFlags;
}; } TTNLogMessage;
// Constants for formatting LORA values static void loggingTask(void* param);
static const char* const SF_NAMES[] = { "FSK", "SF7", "SF8", "SF9", "SF10", "SF11", "SF12", "SFrfu" }; static void logFatal(const char* file, uint16_t line);
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 printMessage(TTNLogMessage* log); static void printMessage(TTNLogMessage* log);
static void printFatalError(TTNLogMessage* log); static void printFatalError(TTNLogMessage* log);
@ -64,77 +61,78 @@ static void printEvtTxComplete(TTNLogMessage* log);
static void printEvtTxStart(TTNLogMessage* log); static void printEvtTxStart(TTNLogMessage* log);
static void printEvtRxStart(TTNLogMessage* log); static void printEvtRxStart(TTNLogMessage* log);
static void printEvtJoinTxComplete(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 // Initialize logging
void TTNLogging::init() void ttn_log_init(void)
{ {
ringBuffer = xRingbufferCreate(NUM_RINGBUF_MSG * sizeof(TTNLogMessage), RINGBUF_TYPE_NOSPLIT); ringBuffer = xRingbufferCreate(NUM_RINGBUF_MSG * sizeof(TTNLogMessage), RINGBUF_TYPE_NOSPLIT);
if (ringBuffer == nullptr) { if (ringBuffer == NULL) {
ESP_LOGE(TAG, "Failed to create ring buffer"); ESP_LOGE(TAG, "Failed to create ring buffer");
ASSERT(0); 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); hal_set_failure_handler(logFatal);
} }
// Record a logging event for later output // 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; return;
TTNLogMessage log;
log.message = message;
log.datum = datum;
// capture state // capture state
log.time = os_getTime(); TTNLogMessage log = {
log.txend = LMIC.txend; .message = message,
log.globalDutyAvail = LMIC.globalDutyAvail; .datum = datum,
log.event = (ev_t)event; .time = os_getTime(),
log.freq = LMIC.freq; .txend = LMIC.txend,
log.opmode = LMIC.opmode; .globalDutyAvail = LMIC.globalDutyAvail,
log.fcntDn = (u2_t) LMIC.seqnoDn; .event = (ev_t)event,
log.fcntUp = (u2_t) LMIC.seqnoUp; .freq = LMIC.freq,
log.rxsyms = LMIC.rxsyms; .opmode = LMIC.opmode,
log.rps = LMIC.rps; .fcntDn = (u2_t) LMIC.seqnoDn,
log.txChnl = LMIC.txChnl; .fcntUp = (u2_t) LMIC.seqnoUp,
log.datarate = LMIC.datarate; .rxsyms = LMIC.rxsyms,
log.txrxFlags = LMIC.txrxFlags; .rps = LMIC.rps,
log.saveIrqFlags = LMIC.saveIrqFlags; .txChnl = LMIC.txChnl,
.datarate = LMIC.datarate,
.txrxFlags = LMIC.txrxFlags,
.saveIrqFlags = LMIC.saveIrqFlags,
};
xRingbufferSend(ringBuffer, &log, sizeof(log), 0); xRingbufferSend(ringBuffer, &log, sizeof(log), 0);
} }
// record a fatal event (failed assert) for later output // 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 // Record an informational message for later output
// The message must not be freed. // 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 // Record an information message with an integer value for later output
// The message must not be freed. // 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 // Log output
// Tasks that receiveds the recorded messages, formats and outputs them. // 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; RingbufHandle_t ringBuffer = (RingbufHandle_t)param;
while (true) { while (true) {
size_t size; size_t size;
TTNLogMessage* log = (TTNLogMessage*) xRingbufferReceive(ringBuffer, &size, portMAX_DELAY); TTNLogMessage* log = (TTNLogMessage*) xRingbufferReceive(ringBuffer, &size, portMAX_DELAY);
if (log == nullptr) if (log == NULL)
continue; continue;
printMessage(log); printMessage(log);

View File

@ -2,7 +2,7 @@
* *
* ttn-esp32 - The Things Network device library for ESP-IDF / SX127x * 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 * Licensed under MIT License
* https://opensource.org/licenses/MIT * https://opensource.org/licenses/MIT
@ -10,8 +10,8 @@
* Circular buffer for detailed logging without affecting LMIC timing. * Circular buffer for detailed logging without affecting LMIC timing.
*******************************************************************************/ *******************************************************************************/
#ifndef _ttnlogging_h_ #ifndef TTN_LOGGING_H
#define _ttnlogging_h_ #define TTN_LOGGING_H
#if LMIC_ENABLE_event_logging #if LMIC_ENABLE_event_logging
@ -20,8 +20,13 @@
#include <freertos/ringbuf.h> #include <freertos/ringbuf.h>
#ifdef __cplusplus
extern "C" {
#endif
/** /**
* @brief Logging class. * @brief Logging functions.
* *
* Logs internal information from LMIC in an asynchrnous fashion in order * Logs internal information from LMIC in an asynchrnous fashion in order
* not to distrub the sensitive LORA timing. * not to distrub the sensitive LORA timing.
@ -33,22 +38,16 @@
* *
* In order to activate the detailed logging, set the macro * In order to activate the detailed logging, set the macro
* `LMIC_ENABLE_event_logging` to 1. * `LMIC_ENABLE_event_logging` to 1.
*
* This class is not to be used directly.
*/ */
class TTNLogging {
public:
static TTNLogging* initInstance();
void init(); void ttn_log_init(void);
void logEvent(int event, const char* message, uint32_t datum); 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 #endif