1
0
mirror of https://github.com/manuelbl/ttn-esp32.git synced 2025-06-21 14:34:28 +02:00

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",
"__nullptr": "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 "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

View File

@ -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 <string.h>
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "ttn_logging.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "lmic/lmic.h"
#include "TTNLogging.h"
#include <string.h>
#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);

View File

@ -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 <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
* 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