Configurable application log level
This commit is contained in:
parent
b30701bb3c
commit
59707f3775
@ -25,4 +25,24 @@ menu "EspTunnel Configuration"
|
|||||||
help
|
help
|
||||||
The UART interface to use for RX and TX.
|
The UART interface to use for RX and TX.
|
||||||
|
|
||||||
|
choice ESPTNL_LOG_LEVEL
|
||||||
|
prompt "Log level"
|
||||||
|
default ESPTNL_LOG_DEBUG
|
||||||
|
help
|
||||||
|
The application's own log level.
|
||||||
|
|
||||||
|
config ESPTNL_LOG_NONE
|
||||||
|
bool "None"
|
||||||
|
config ESPTNL_LOG_ERROR
|
||||||
|
bool "Error"
|
||||||
|
config ESPTNL_LOG_WARN
|
||||||
|
bool "Warning"
|
||||||
|
config ESPTNL_LOG_INFO
|
||||||
|
bool "Information"
|
||||||
|
config ESPTNL_LOG_DEBUG
|
||||||
|
bool "Debug"
|
||||||
|
config ESPTNL_LOG_VERBOSE
|
||||||
|
bool "Verbose"
|
||||||
|
endchoice
|
||||||
|
|
||||||
endmenu
|
endmenu
|
||||||
@ -2,6 +2,8 @@
|
|||||||
// Created by erki on 11/06/23.
|
// Created by erki on 11/06/23.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include "app_config.hpp"
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
#include "esp_now.h"
|
#include "esp_now.h"
|
||||||
|
|||||||
31
main/app_config.hpp
Normal file
31
main/app_config.hpp
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
//
|
||||||
|
// Created by erki on 12/06/23.
|
||||||
|
//
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "esp_log.h"
|
||||||
|
|
||||||
|
namespace Config
|
||||||
|
{
|
||||||
|
|
||||||
|
constexpr esp_log_level_t appLogLevel()
|
||||||
|
{
|
||||||
|
#ifdef CONFIG_ESPTNL_LOG_NONE
|
||||||
|
return ESP_LOG_NONE;
|
||||||
|
#elifdef CONFIG_ESPTNL_LOG_ERROR
|
||||||
|
return ESP_LOG_ERROR;
|
||||||
|
#elifdef CONFIG_ESPTNL_LOG_WARN
|
||||||
|
return ESP_LOG_WARN;
|
||||||
|
#elifdef CONFIG_ESPTNL_LOG_INFO
|
||||||
|
return ESP_LOG_INFO;
|
||||||
|
#elifdef CONFIG_ESPTNL_LOG_DEBUG
|
||||||
|
return ESP_LOG_DEBUG;
|
||||||
|
#elifdef CONFIG_ESPTNL_LOG_VERBOSE
|
||||||
|
return ESP_LOG_VERBOSE;
|
||||||
|
#else
|
||||||
|
# error "Invalid log setting set."
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -17,8 +17,6 @@ namespace
|
|||||||
|
|
||||||
using namespace Networking;
|
using namespace Networking;
|
||||||
|
|
||||||
static const char* TAG = "Networking";
|
|
||||||
|
|
||||||
static_assert(std::is_standard_layout_v<EspNowEvent> && std::is_trivial_v<EspNowEvent>,
|
static_assert(std::is_standard_layout_v<EspNowEvent> && std::is_trivial_v<EspNowEvent>,
|
||||||
"EspNowEvent is not compatible with a FreeRTOS queue.");
|
"EspNowEvent is not compatible with a FreeRTOS queue.");
|
||||||
|
|
||||||
@ -55,7 +53,7 @@ void s_cbEspNowSendComplete(const std::uint8_t* peer_mac, esp_now_send_status_t
|
|||||||
{
|
{
|
||||||
EspNowEvent event = { EspNowEvent::MSG_SEND_COMPLETE, 0 };
|
EspNowEvent event = { EspNowEvent::MSG_SEND_COMPLETE, 0 };
|
||||||
xQueueSend(s_esp_now_queue, &event, 0);
|
xQueueSend(s_esp_now_queue, &event, 0);
|
||||||
ESP_LOGD(TAG, "Send complete.");
|
ESP_LOGD(LOG_TAG, "Send complete.");
|
||||||
}
|
}
|
||||||
|
|
||||||
void s_cbEspNowReceiveComplete(const esp_now_recv_info_t* recv_info, const std::uint8_t* data, int len)
|
void s_cbEspNowReceiveComplete(const esp_now_recv_info_t* recv_info, const std::uint8_t* data, int len)
|
||||||
@ -65,7 +63,7 @@ void s_cbEspNowReceiveComplete(const esp_now_recv_info_t* recv_info, const std::
|
|||||||
|
|
||||||
EspNowEvent event = { EspNowEvent::MSG_RECEIVED, rx_len };
|
EspNowEvent event = { EspNowEvent::MSG_RECEIVED, rx_len };
|
||||||
xQueueSend(s_esp_now_queue, &event, 0);
|
xQueueSend(s_esp_now_queue, &event, 0);
|
||||||
ESP_LOGD(TAG, "Received message. Length: %d.", len);
|
ESP_LOGD(LOG_TAG, "Received message. Length: %d.", len);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -118,11 +116,11 @@ void sendData(const std::array<std::uint8_t, 128>& buffer, const std::size_t len
|
|||||||
{
|
{
|
||||||
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_now_send(peer_mac->data(), buffer.data(), length));
|
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_now_send(peer_mac->data(), buffer.data(), length));
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Message send started.");
|
ESP_LOGD(LOG_TAG, "Message send started. Length: %u", length);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ESP_LOGW(TAG, "Dropped message.");
|
ESP_LOGW(LOG_TAG, "Dropped message.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -9,6 +9,8 @@
|
|||||||
namespace Networking
|
namespace Networking
|
||||||
{
|
{
|
||||||
|
|
||||||
|
static const char* LOG_TAG = "Networking";
|
||||||
|
|
||||||
struct EspNowEvent
|
struct EspNowEvent
|
||||||
{
|
{
|
||||||
enum : std::uint8_t
|
enum : std::uint8_t
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
#include "app_networking.hpp"
|
#include "app_networking.hpp"
|
||||||
#include "app_serial.hpp"
|
#include "app_serial.hpp"
|
||||||
|
#include "app_config.hpp"
|
||||||
|
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/queue.h"
|
#include "freertos/queue.h"
|
||||||
@ -15,7 +16,7 @@
|
|||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
|
||||||
static const char* TAG = "App";
|
static const char* LOG_TAG = "App";
|
||||||
|
|
||||||
TaskHandle_t s_main_task = nullptr;
|
TaskHandle_t s_main_task = nullptr;
|
||||||
|
|
||||||
@ -27,7 +28,7 @@ QueueHandle_t s_uart_queue = nullptr;
|
|||||||
|
|
||||||
void s_sendUartData(const std::size_t len)
|
void s_sendUartData(const std::size_t len)
|
||||||
{
|
{
|
||||||
ESP_LOGD(TAG, "Sending %u bytes via UART.", len);
|
ESP_LOGD(LOG_TAG, "Sending %u bytes via UART.", len);
|
||||||
|
|
||||||
std::array<std::uint8_t, 128> tx_buffer;
|
std::array<std::uint8_t, 128> tx_buffer;
|
||||||
uart_read_bytes(CONFIG_ESPTNL_UART, tx_buffer.data(), len, 0);
|
uart_read_bytes(CONFIG_ESPTNL_UART, tx_buffer.data(), len, 0);
|
||||||
@ -48,14 +49,14 @@ std::size_t s_uartBytesWaiting()
|
|||||||
{
|
{
|
||||||
QueueSetMemberHandle_t queue_select = xQueueSelectFromSet(s_queue_set, portMAX_DELAY);
|
QueueSetMemberHandle_t queue_select = xQueueSelectFromSet(s_queue_set, portMAX_DELAY);
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Event: %s.", queue_select == s_esp_now_queue ? "ESP-NOW event" : "UART event");
|
ESP_LOGI(LOG_TAG, "Event: %s.", queue_select == s_esp_now_queue ? "ESP-NOW event" : "UART event");
|
||||||
|
|
||||||
if (queue_select == s_esp_now_queue)
|
if (queue_select == s_esp_now_queue)
|
||||||
{
|
{
|
||||||
Networking::EspNowEvent event;
|
Networking::EspNowEvent event;
|
||||||
xQueueReceive(queue_select, &event, 0);
|
xQueueReceive(queue_select, &event, 0);
|
||||||
|
|
||||||
ESP_LOGD(TAG, "ESP-NOW event: %u, rx-length: %ull.", event.type, event.rx_length);
|
ESP_LOGD(LOG_TAG, "ESP-NOW event: %u, rx-length: %ull.", event.type, event.rx_length);
|
||||||
|
|
||||||
if (event.type == Networking::EspNowEvent::MSG_RECEIVED)
|
if (event.type == Networking::EspNowEvent::MSG_RECEIVED)
|
||||||
{
|
{
|
||||||
@ -63,18 +64,18 @@ std::size_t s_uartBytesWaiting()
|
|||||||
uart_get_tx_buffer_free_size(CONFIG_ESPTNL_UART, &uart_free);
|
uart_get_tx_buffer_free_size(CONFIG_ESPTNL_UART, &uart_free);
|
||||||
if (uart_free >= event.rx_length)
|
if (uart_free >= event.rx_length)
|
||||||
{
|
{
|
||||||
ESP_LOGD(TAG, "ESP-NOW event: wrote %ull bytes to UART RX.", uart_free);
|
ESP_LOGD(LOG_TAG, "ESP-NOW event: wrote %ull bytes to UART RX.", uart_free);
|
||||||
const auto& esp_rx_buffer = Networking::getRxBuffer();
|
const auto& esp_rx_buffer = Networking::getRxBuffer();
|
||||||
uart_write_bytes(CONFIG_ESPTNL_UART, esp_rx_buffer.data(), event.rx_length);
|
uart_write_bytes(CONFIG_ESPTNL_UART, esp_rx_buffer.data(), event.rx_length);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ESP_LOGW(TAG, "ESP-NOW event: dropped RX data due to full UART RX buffer.");
|
ESP_LOGW(LOG_TAG, "ESP-NOW event: dropped RX data due to full UART RX buffer.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (event.type == Networking::EspNowEvent::MSG_SEND_COMPLETE)
|
else if (event.type == Networking::EspNowEvent::MSG_SEND_COMPLETE)
|
||||||
{
|
{
|
||||||
ESP_LOGD(TAG, "ESP-NOW event: TX completed. Checking to send data.");
|
ESP_LOGD(LOG_TAG, "ESP-NOW event: TX completed. Checking to send data.");
|
||||||
|
|
||||||
const std::size_t bytes_waiting = s_uartBytesWaiting();
|
const std::size_t bytes_waiting = s_uartBytesWaiting();
|
||||||
if (s_uartBytesWaiting() >= 128)
|
if (s_uartBytesWaiting() >= 128)
|
||||||
@ -89,13 +90,12 @@ std::size_t s_uartBytesWaiting()
|
|||||||
uart_event_t event;
|
uart_event_t event;
|
||||||
xQueueReceive(s_uart_queue, &event, 0);
|
xQueueReceive(s_uart_queue, &event, 0);
|
||||||
|
|
||||||
ESP_LOGD(TAG, "UART event: %d", event.type);
|
ESP_LOGD(LOG_TAG, "UART event: %d, size: %u", event.type, event.size);
|
||||||
|
|
||||||
if (event.type == UART_DATA)
|
if (event.type == UART_DATA)
|
||||||
{
|
{
|
||||||
const std::size_t bytes_waiting = s_uartBytesWaiting();
|
if (event.size >= 128)
|
||||||
if (s_uartBytesWaiting() >= 128)
|
s_sendUartData(event.size);
|
||||||
s_sendUartData(bytes_waiting);
|
|
||||||
else
|
else
|
||||||
xTimerReset(s_timer, 0);
|
xTimerReset(s_timer, 0);
|
||||||
}
|
}
|
||||||
@ -105,6 +105,8 @@ std::size_t s_uartBytesWaiting()
|
|||||||
|
|
||||||
void s_cbListenTimeout(TimerHandle_t)
|
void s_cbListenTimeout(TimerHandle_t)
|
||||||
{
|
{
|
||||||
|
ESP_LOGD(LOG_TAG, "s_cbListenTimeout invoked from timer.");
|
||||||
|
|
||||||
const std::size_t bytes_waiting = s_uartBytesWaiting();
|
const std::size_t bytes_waiting = s_uartBytesWaiting();
|
||||||
if (bytes_waiting > 0)
|
if (bytes_waiting > 0)
|
||||||
s_sendUartData(bytes_waiting);
|
s_sendUartData(bytes_waiting);
|
||||||
@ -116,11 +118,11 @@ extern "C" void app_main(void)
|
|||||||
{
|
{
|
||||||
ESP_ERROR_CHECK(nvs_flash_init());
|
ESP_ERROR_CHECK(nvs_flash_init());
|
||||||
|
|
||||||
esp_log_level_set("*", ESP_LOG_INFO);
|
esp_log_level_set("*", esp_log_level_t(CONFIG_LOG_DEFAULT_LEVEL));
|
||||||
esp_log_level_set(TAG, ESP_LOG_DEBUG);
|
esp_log_level_set(LOG_TAG, Config::appLogLevel());
|
||||||
esp_log_level_set("Networking", ESP_LOG_DEBUG);
|
esp_log_level_set(Networking::LOG_TAG, Config::appLogLevel());
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Starting main.");
|
ESP_LOGD(LOG_TAG, "Starting main.");
|
||||||
|
|
||||||
Networking::setupWifi();
|
Networking::setupWifi();
|
||||||
s_esp_now_queue = Networking::setupEspNow();
|
s_esp_now_queue = Networking::setupEspNow();
|
||||||
@ -132,6 +134,6 @@ extern "C" void app_main(void)
|
|||||||
xQueueAddToSet(s_esp_now_queue, s_queue_set);
|
xQueueAddToSet(s_esp_now_queue, s_queue_set);
|
||||||
xQueueAddToSet(s_uart_queue, s_queue_set);
|
xQueueAddToSet(s_uart_queue, s_queue_set);
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Setup completed.");
|
ESP_LOGI(LOG_TAG, "Setup completed.");
|
||||||
xTaskCreate(s_mainTask, "main_task", 2048, nullptr, 4, &s_main_task);
|
xTaskCreate(s_mainTask, "main_task", 2048, nullptr, 4, &s_main_task);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user