Compare commits

...

4 Commits

Author SHA1 Message Date
erki
59707f3775 Configurable application log level 2023-06-21 19:01:34 +03:00
erki
b30701bb3c Make UART configurable, move config validation 2023-06-11 23:11:20 +03:00
erki
fd0c853f36 Add silence timeout 2023-06-11 21:51:45 +03:00
erki
0993f4df1a Fix peer adding 2023-06-11 21:51:32 +03:00
9 changed files with 151 additions and 53 deletions

View File

@ -1,4 +1,5 @@
idf_component_register(SRCS "esp_tunnel.cpp"
"app_networking.cpp"
"app_serial.cpp"
"app_config.cpp"
INCLUDE_DIRS ".")

View File

@ -12,7 +12,6 @@ menu "EspTunnel Configuration"
help
ESPNOW local master key. Must be 16 bytes.
config ESPTNL_CHANNEL
int "Channel"
default 1
@ -20,4 +19,30 @@ menu "EspTunnel Configuration"
help
The channel for sending and receiving ESPNOW data.
config ESPTNL_UART
int "UART interface"
default 0
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

26
main/app_config.cpp Normal file
View File

@ -0,0 +1,26 @@
//
// Created by erki on 11/06/23.
//
#include "app_config.hpp"
#include <cstdint>
#include "esp_now.h"
#include "driver/uart.h"
namespace
{
template<std::size_t N>
constexpr auto STR_LEN(const char (&s)[N])
{
return N;
}
static_assert(STR_LEN(CONFIG_ESPTNL_PMK) == ESP_NOW_KEY_LEN + 1, "CONFIG_ESPTNL_PMK must be of length 16 bytes + 1 null terminator.");
static_assert(STR_LEN(CONFIG_ESPTNL_LMK) == ESP_NOW_KEY_LEN + 1, "CONFIG_ESPTNL_LMK must be of length 16 bytes + 1 null terminator.");
static_assert(CONFIG_ESPTNL_UART < UART_NUM_MAX && CONFIG_ESPTNL_UART >= 0, "CONFIG_ESPTNL_UART must be within range [0, UART_NUM_MAX[.");
}

31
main/app_config.hpp Normal file
View 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
}
}

View File

@ -17,20 +17,9 @@ namespace
using namespace Networking;
static const char* TAG = "Networking";
static_assert(std::is_standard_layout_v<EspNowEvent> && std::is_trivial_v<EspNowEvent>,
"EspNowEvent is not compatible with a FreeRTOS queue.");
template<std::size_t N>
constexpr auto STR_LEN(const char (&s)[N])
{
return N;
}
static_assert(STR_LEN(CONFIG_ESPTNL_PMK) == ESP_NOW_KEY_LEN + 1, "CONFIG_ESPTNL_PMK must be of length 16 bytes + 1 null terminator.");
static_assert(STR_LEN(CONFIG_ESPTNL_LMK) == ESP_NOW_KEY_LEN + 1, "CONFIG_ESPTNL_LMK must be of length 16 bytes + 1 null terminator.");
QueueHandle_t s_esp_now_queue = nullptr;
std::array<std::uint8_t, 128> s_rx_buffer;
std::variant<MacAddress, std::monostate> s_peer;
@ -46,7 +35,7 @@ bool s_isBroadcastAddress(const esp_now_recv_info_t* sender)
return s_isBroadcastAddress(sender->src_addr);
}
bool s_addPeer(const MacAddress& peer_address)
esp_err_t s_addPeer(const MacAddress& peer_address)
{
esp_now_peer_info_t broadcast_peer;
std::memset(&broadcast_peer, 0, sizeof(broadcast_peer));
@ -57,17 +46,14 @@ bool s_addPeer(const MacAddress& peer_address)
std::memcpy(broadcast_peer.lmk, CONFIG_ESPTNL_LMK, ESP_NOW_KEY_LEN);
std::memcpy(broadcast_peer.peer_addr, peer_address.data(), peer_address.size());
auto successful = esp_now_add_peer(&broadcast_peer);
ESP_ERROR_CHECK_WITHOUT_ABORT(successful);
return successful == ESP_OK;
return esp_now_add_peer(&broadcast_peer);
}
void s_cbEspNowSendComplete(const std::uint8_t* peer_mac, esp_now_send_status_t status)
{
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)
@ -77,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);
}
}
@ -130,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_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.");
}
}

View File

@ -9,6 +9,8 @@
namespace Networking
{
static const char* LOG_TAG = "Networking";
struct EspNowEvent
{
enum : std::uint8_t

View File

@ -16,7 +16,7 @@ QueueHandle_t s_uart_queue = nullptr;
namespace Serial
{
QueueHandle_t setupSerial()
QueueHandle_t setupSerial(const uart_port_t uart)
{
uart_config_t config = {
.baud_rate = 115200,
@ -27,9 +27,9 @@ QueueHandle_t setupSerial()
.source_clk = UART_SCLK_DEFAULT,
};
ESP_ERROR_CHECK(uart_driver_install(UART_NUM_0, 132, 128 * 2, 4, &s_uart_queue, 0));
ESP_ERROR_CHECK(uart_param_config(UART_NUM_0, &config));
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_0, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
ESP_ERROR_CHECK(uart_driver_install(uart, 132, 128 * 2, 4, &s_uart_queue, 0));
ESP_ERROR_CHECK(uart_param_config(uart, &config));
ESP_ERROR_CHECK(uart_set_pin(uart, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
return s_uart_queue;
}

View File

@ -6,10 +6,11 @@
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "driver/uart.h"
namespace Serial
{
[[nodiscard]] QueueHandle_t setupSerial();
[[nodiscard]] QueueHandle_t setupSerial(const uart_port_t uart);
}

View File

@ -4,9 +4,11 @@
#include "app_networking.hpp"
#include "app_serial.hpp"
#include "app_config.hpp"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/timers.h"
#include "nvs_flash.h"
#include "driver/uart.h"
#include "esp_log.h"
@ -14,27 +16,31 @@
namespace
{
static const char* TAG = "App";
static const char* LOG_TAG = "App";
TaskHandle_t s_main_task = nullptr;
TimerHandle_t s_timer = nullptr;
QueueSetHandle_t s_queue_set = nullptr;
QueueHandle_t s_esp_now_queue = nullptr;
QueueHandle_t s_uart_queue = nullptr;
void s_sendUartData()
void s_sendUartData(const std::size_t len)
{
std::size_t uart_rx_size = 0;
uart_get_buffered_data_len(UART_NUM_0, &uart_rx_size);
if (uart_rx_size > 128 / 2)
{
ESP_LOGD(TAG, "Sending %ull bytes via UART.", uart_rx_size);
ESP_LOGD(LOG_TAG, "Sending %u bytes via UART.", len);
std::array<std::uint8_t, 128> tx_buffer;
uart_read_bytes(UART_NUM_0, tx_buffer.data(), uart_rx_size, 0);
std::array<std::uint8_t, 128> tx_buffer;
uart_read_bytes(CONFIG_ESPTNL_UART, tx_buffer.data(), len, 0);
Networking::sendData(tx_buffer, uart_rx_size);
}
Networking::sendData(tx_buffer, len);
}
std::size_t s_uartBytesWaiting()
{
std::size_t len = 0;
uart_get_buffered_data_len(CONFIG_ESPTNL_UART, &len);
return std::min<std::size_t>(len, 128);
}
[[noreturn]] void s_mainTask(void*)
@ -43,34 +49,40 @@ void s_sendUartData()
{
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)
{
std::size_t uart_free = 0;
uart_get_tx_buffer_free_size(UART_NUM_0, &uart_free);
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(UART_NUM_0, esp_rx_buffer.data(), event.rx_length);
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.");
s_sendUartData();
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)
{
s_sendUartData(bytes_waiting);
xTimerStop(s_timer, 0);
}
}
}
else if (queue_select == s_uart_queue)
@ -78,36 +90,50 @@ void s_sendUartData()
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)
{
s_sendUartData();
if (event.size >= 128)
s_sendUartData(event.size);
else
xTimerReset(s_timer, 0);
}
}
}
}
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);
}
}// namespace
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();
s_uart_queue = Serial::setupSerial();
s_uart_queue = Serial::setupSerial(CONFIG_ESPTNL_UART);
s_timer = xTimerCreate("autosend_timer", pdMS_TO_TICKS(100), false, nullptr, s_cbListenTimeout);
s_queue_set = xQueueCreateSet(4 + 4);
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);
}