diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index a3e708d..c310a28 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -25,4 +25,24 @@ menu "EspTunnel Configuration" help 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 \ No newline at end of file diff --git a/main/app_config.cpp b/main/app_config.cpp index 046240d..9946920 100644 --- a/main/app_config.cpp +++ b/main/app_config.cpp @@ -2,6 +2,8 @@ // Created by erki on 11/06/23. // +#include "app_config.hpp" + #include #include "esp_now.h" diff --git a/main/app_config.hpp b/main/app_config.hpp new file mode 100644 index 0000000..d265afa --- /dev/null +++ b/main/app_config.hpp @@ -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 +} + +} diff --git a/main/app_networking.cpp b/main/app_networking.cpp index 6d96c4d..73bee9b 100644 --- a/main/app_networking.cpp +++ b/main/app_networking.cpp @@ -17,8 +17,6 @@ namespace using namespace Networking; -static const char* TAG = "Networking"; - static_assert(std::is_standard_layout_v && std::is_trivial_v, "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 }; 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) @@ -65,7 +63,7 @@ void s_cbEspNowReceiveComplete(const esp_now_recv_info_t* recv_info, const std:: EspNowEvent event = { EspNowEvent::MSG_RECEIVED, rx_len }; 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& buffer, const std::size_t len { 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 { - ESP_LOGW(TAG, "Dropped message."); + ESP_LOGW(LOG_TAG, "Dropped message."); } } diff --git a/main/app_networking.hpp b/main/app_networking.hpp index f2d3e41..3c68824 100644 --- a/main/app_networking.hpp +++ b/main/app_networking.hpp @@ -9,6 +9,8 @@ namespace Networking { +static const char* LOG_TAG = "Networking"; + struct EspNowEvent { enum : std::uint8_t diff --git a/main/esp_tunnel.cpp b/main/esp_tunnel.cpp index 1a0721d..ee47c11 100644 --- a/main/esp_tunnel.cpp +++ b/main/esp_tunnel.cpp @@ -4,6 +4,7 @@ #include "app_networking.hpp" #include "app_serial.hpp" +#include "app_config.hpp" #include "freertos/FreeRTOS.h" #include "freertos/queue.h" @@ -15,7 +16,7 @@ namespace { -static const char* TAG = "App"; +static const char* LOG_TAG = "App"; TaskHandle_t s_main_task = nullptr; @@ -27,7 +28,7 @@ QueueHandle_t s_uart_queue = nullptr; 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 tx_buffer; 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); - 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) { Networking::EspNowEvent event; 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) { @@ -63,18 +64,18 @@ std::size_t s_uartBytesWaiting() uart_get_tx_buffer_free_size(CONFIG_ESPTNL_UART, &uart_free); 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(); uart_write_bytes(CONFIG_ESPTNL_UART, esp_rx_buffer.data(), event.rx_length); } 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) { - 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(); if (s_uartBytesWaiting() >= 128) @@ -89,13 +90,12 @@ std::size_t s_uartBytesWaiting() uart_event_t event; 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) { - const std::size_t bytes_waiting = s_uartBytesWaiting(); - if (s_uartBytesWaiting() >= 128) - s_sendUartData(bytes_waiting); + if (event.size >= 128) + s_sendUartData(event.size); else xTimerReset(s_timer, 0); } @@ -105,6 +105,8 @@ std::size_t s_uartBytesWaiting() void s_cbListenTimeout(TimerHandle_t) { + ESP_LOGD(LOG_TAG, "s_cbListenTimeout invoked from timer."); + const std::size_t bytes_waiting = s_uartBytesWaiting(); if (bytes_waiting > 0) s_sendUartData(bytes_waiting); @@ -116,11 +118,11 @@ extern "C" void app_main(void) { ESP_ERROR_CHECK(nvs_flash_init()); - esp_log_level_set("*", ESP_LOG_INFO); - esp_log_level_set(TAG, ESP_LOG_DEBUG); - esp_log_level_set("Networking", ESP_LOG_DEBUG); + esp_log_level_set("*", esp_log_level_t(CONFIG_LOG_DEFAULT_LEVEL)); + esp_log_level_set(LOG_TAG, Config::appLogLevel()); + esp_log_level_set(Networking::LOG_TAG, Config::appLogLevel()); - ESP_LOGD(TAG, "Starting main."); + ESP_LOGD(LOG_TAG, "Starting main."); Networking::setupWifi(); 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_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); }