Reformat project using clang-format

This commit is contained in:
Lorow
2026-01-06 22:51:24 +01:00
parent 567d3ebd75
commit 555e290d71
70 changed files with 3282 additions and 3428 deletions

View File

@@ -2,34 +2,34 @@
#include "esp_log.h"
#include "main_globals.hpp"
SerialManager::SerialManager(std::shared_ptr<CommandManager> commandManager, esp_timer_handle_t *timerHandle)
SerialManager::SerialManager(std::shared_ptr<CommandManager> commandManager, esp_timer_handle_t* timerHandle)
: commandManager(commandManager), timerHandle(timerHandle)
{
this->data = static_cast<uint8_t *>(malloc(BUF_SIZE));
this->temp_data = static_cast<uint8_t *>(malloc(256));
this->data = static_cast<uint8_t*>(malloc(BUF_SIZE));
this->temp_data = static_cast<uint8_t*>(malloc(256));
}
// Function to notify that a command was received during startup
void SerialManager::notify_startup_command_received()
{
setStartupCommandReceived(true);
setStartupCommandReceived(true);
// Cancel the startup timer if it's still running
if (timerHandle != nullptr && *timerHandle != nullptr)
{
esp_timer_stop(*timerHandle);
esp_timer_delete(*timerHandle);
*timerHandle = nullptr;
ESP_LOGI("[MAIN]", "Startup timer cancelled, staying in heartbeat mode");
}
// Cancel the startup timer if it's still running
if (timerHandle != nullptr && *timerHandle != nullptr)
{
esp_timer_stop(*timerHandle);
esp_timer_delete(*timerHandle);
*timerHandle = nullptr;
ESP_LOGI("[MAIN]", "Startup timer cancelled, staying in heartbeat mode");
}
}
// we can cancel this task once we're in cdc
void HandleSerialManagerTask(void *pvParameters)
void HandleSerialManagerTask(void* pvParameters)
{
auto const serialManager = static_cast<SerialManager *>(pvParameters);
while (true)
{
serialManager->try_receive();
}
auto const serialManager = static_cast<SerialManager*>(pvParameters);
while (true)
{
serialManager->try_receive();
}
}

View File

@@ -3,18 +3,18 @@
#define SERIALMANAGER_HPP
#include <stdio.h>
#include <string>
#include <memory>
#include <CommandManager.hpp>
#include <ProjectConfig.hpp>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "sdkconfig.h"
#include "esp_log.h"
#include <memory>
#include <string>
#include "driver/gpio.h"
#include "esp_vfs_dev.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "esp_vfs_dev.h"
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "sdkconfig.h"
#ifndef BUF_SIZE
#define BUF_SIZE (1024)
@@ -27,26 +27,26 @@ extern QueueHandle_t cdcMessageQueue;
struct cdc_command_packet_t
{
uint8_t len;
uint8_t data[64];
uint8_t len;
uint8_t data[64];
};
class SerialManager
{
public:
explicit SerialManager(std::shared_ptr<CommandManager> commandManager, esp_timer_handle_t *timerHandle);
void setup();
void try_receive();
void notify_startup_command_received();
void shutdown();
public:
explicit SerialManager(std::shared_ptr<CommandManager> commandManager, esp_timer_handle_t* timerHandle);
void setup();
void try_receive();
void notify_startup_command_received();
void shutdown();
private:
std::shared_ptr<CommandManager> commandManager;
esp_timer_handle_t *timerHandle;
uint8_t *data;
uint8_t *temp_data;
private:
std::shared_ptr<CommandManager> commandManager;
esp_timer_handle_t* timerHandle;
uint8_t* data;
uint8_t* temp_data;
};
void HandleSerialManagerTask(void *pvParameters);
void HandleCDCSerialManagerTask(void *pvParameters);
void HandleSerialManagerTask(void* pvParameters);
void HandleCDCSerialManagerTask(void* pvParameters);
#endif

View File

@@ -1,102 +1,98 @@
#include "SerialManager.hpp"
#include "driver/uart.h"
#include "esp_log.h"
#include "main_globals.hpp"
#include "driver/uart.h"
void SerialManager::setup()
{
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
};
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
};
const auto uart_num = static_cast<uart_port_t>(CONFIG_UART_PORT_NUMBER);
const auto uart_num = static_cast<uart_port_t>(CONFIG_UART_PORT_NUMBER);
uart_driver_install(uart_num, BUF_SIZE, BUF_SIZE, 0, NULL, 0);
uart_param_config(uart_num, &uart_config);
uart_driver_install(uart_num, BUF_SIZE, BUF_SIZE, 0, NULL, 0);
uart_param_config(uart_num, &uart_config);
uart_set_pin(uart_num,
CONFIG_UART_TX_PIN,
CONFIG_UART_RX_PIN,
UART_PIN_NO_CHANGE,
UART_PIN_NO_CHANGE);
uart_set_pin(uart_num, CONFIG_UART_TX_PIN, CONFIG_UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
gpio_set_pull_mode(static_cast<gpio_num_t>(CONFIG_UART_RX_PIN), GPIO_PULLDOWN_ONLY);
gpio_set_pull_mode(static_cast<gpio_num_t>(CONFIG_UART_RX_PIN), GPIO_PULLDOWN_ONLY);
// ----- Startup Flush -----
uart_flush(uart_num);
// ----- Startup Flush -----
uart_flush(uart_num);
uint8_t dump_buf[256];
// clean up initial onslaught of logs
while (uart_read_bytes(uart_num, dump_buf, sizeof(dump_buf), 10 / portTICK_PERIOD_MS) > 0)
{
}
uint8_t dump_buf[256];
// clean up initial onslaught of logs
while (uart_read_bytes(uart_num, dump_buf, sizeof(dump_buf), 10 / portTICK_PERIOD_MS) > 0)
{
}
}
void uart_write_bytes_chunked(uart_port_t uart_num, const void *src, size_t size)
void uart_write_bytes_chunked(uart_port_t uart_num, const void* src, size_t size)
{
while (size > 0)
{
auto to_write = size > BUF_SIZE ? BUF_SIZE : size;
auto written = uart_write_bytes(uart_num, src, to_write);
src += written;
size -= written;
}
while (size > 0)
{
auto to_write = size > BUF_SIZE ? BUF_SIZE : size;
auto written = uart_write_bytes(uart_num, src, to_write);
src += written;
size -= written;
}
}
void SerialManager::try_receive()
{
static auto current_position = 0;
const auto uart_num = static_cast<uart_port_t>(CONFIG_UART_PORT_NUMBER);
int len = uart_read_bytes(uart_num, this->temp_data, BUF_SIZE, 1000 / 20);
static auto current_position = 0;
const auto uart_num = static_cast<uart_port_t>(CONFIG_UART_PORT_NUMBER);
int len = uart_read_bytes(uart_num, this->temp_data, BUF_SIZE, 1000 / 20);
// If driver is uninstalled or an error occurs, abort read gracefully
if (len <= 0)
{
return;
}
if (len > 0)
{
notify_startup_command_received();
}
// since we've got something on the serial port
// we gotta keep reading until we've got the whole message
// we will submit the command once we get a newline, a return or the buffer is full
for (auto i = 0; i < len; i++)
{
this->data[current_position++] = this->temp_data[i];
// if we're at the end of the buffer, try to process the command anyway
// if we've got a new line, we've finished sending the commands, process them
if (current_position >= BUF_SIZE || this->data[current_position - 1] == '\n' || this->data[current_position - 1] == '\r')
// If driver is uninstalled or an error occurs, abort read gracefully
if (len <= 0)
{
data[current_position] = '\0';
current_position = 0;
const nlohmann::json result = this->commandManager->executeFromJson(std::string_view(reinterpret_cast<const char *>(this->data)));
const auto resultMessage = result.dump();
// todo check if this works
// uart_write_bytes_chunked(uart_num, resultMessage.c_str(), resultMessage.length())s
uart_write_bytes(uart_num, resultMessage.c_str(), resultMessage.length());
return;
}
if (len > 0)
{
notify_startup_command_received();
}
// since we've got something on the serial port
// we gotta keep reading until we've got the whole message
// we will submit the command once we get a newline, a return or the buffer is full
for (auto i = 0; i < len; i++)
{
this->data[current_position++] = this->temp_data[i];
// if we're at the end of the buffer, try to process the command anyway
// if we've got a new line, we've finished sending the commands, process them
if (current_position >= BUF_SIZE || this->data[current_position - 1] == '\n' || this->data[current_position - 1] == '\r')
{
data[current_position] = '\0';
current_position = 0;
const nlohmann::json result = this->commandManager->executeFromJson(std::string_view(reinterpret_cast<const char*>(this->data)));
const auto resultMessage = result.dump();
// todo check if this works
// uart_write_bytes_chunked(uart_num, resultMessage.c_str(), resultMessage.length())s
uart_write_bytes(uart_num, resultMessage.c_str(), resultMessage.length());
}
}
}
}
void SerialManager::shutdown()
{
// Uninstall the UART driver to free the internal to keep compatibility with JTAG implementation.
const auto uart_num = static_cast<uart_port_t>(CONFIG_UART_PORT_NUMBER);
esp_err_t err = uart_driver_delete(uart_num);
if (err == ESP_OK)
{
ESP_LOGI("[SERIAL]", "usb_serial_jtag driver uninstalled");
}
else if (err != ESP_ERR_INVALID_STATE)
{
ESP_LOGW("[SERIAL]", "usb_serial_jtag_driver_uninstall returned %s", esp_err_to_name(err));
}
// Uninstall the UART driver to free the internal to keep compatibility with JTAG implementation.
const auto uart_num = static_cast<uart_port_t>(CONFIG_UART_PORT_NUMBER);
esp_err_t err = uart_driver_delete(uart_num);
if (err == ESP_OK)
{
ESP_LOGI("[SERIAL]", "usb_serial_jtag driver uninstalled");
}
else if (err != ESP_ERR_INVALID_STATE)
{
ESP_LOGW("[SERIAL]", "usb_serial_jtag_driver_uninstall returned %s", esp_err_to_name(err));
}
}

View File

@@ -1,114 +1,114 @@
#include "SerialManager.hpp"
#include "esp_log.h"
#include "main_globals.hpp"
#include "driver/usb_serial_jtag.h"
#include "esp_log.h"
#include "esp_vfs_usb_serial_jtag.h"
#include "main_globals.hpp"
#include "tusb.h"
void SerialManager::setup()
{
#ifndef CONFIG_USE_UART_FOR_COMMUNICATION
usb_serial_jtag_driver_config_t usb_serial_jtag_config;
usb_serial_jtag_config.rx_buffer_size = BUF_SIZE;
usb_serial_jtag_config.tx_buffer_size = BUF_SIZE;
usb_serial_jtag_driver_install(&usb_serial_jtag_config);
usb_serial_jtag_driver_config_t usb_serial_jtag_config;
usb_serial_jtag_config.rx_buffer_size = BUF_SIZE;
usb_serial_jtag_config.tx_buffer_size = BUF_SIZE;
usb_serial_jtag_driver_install(&usb_serial_jtag_config);
#endif
}
void usb_serial_jtag_write_bytes_chunked(const char *data, size_t len, size_t timeout)
void usb_serial_jtag_write_bytes_chunked(const char* data, size_t len, size_t timeout)
{
#ifndef CONFIG_USE_UART_FOR_COMMUNICATION
while (len > 0)
{
auto to_write = len > BUF_SIZE ? BUF_SIZE : len;
auto written = usb_serial_jtag_write_bytes(data, to_write, timeout);
data += written;
len -= written;
}
while (len > 0)
{
auto to_write = len > BUF_SIZE ? BUF_SIZE : len;
auto written = usb_serial_jtag_write_bytes(data, to_write, timeout);
data += written;
len -= written;
}
#endif
}
void SerialManager::try_receive()
{
static auto current_position = 0;
int len = usb_serial_jtag_read_bytes(this->temp_data, 256, 1000 / 20);
static auto current_position = 0;
int len = usb_serial_jtag_read_bytes(this->temp_data, 256, 1000 / 20);
// If driver is uninstalled or an error occurs, abort read gracefully
if (len < 0)
{
return;
}
if (len > 0)
{
// Notify main that a command was received during startup
notify_startup_command_received();
}
// since we've got something on the serial port
// we gotta keep reading until we've got the whole message
// we will submit the command once we get a newline, a return or the buffer is full
for (auto i = 0; i < len; i++)
{
this->data[current_position++] = this->temp_data[i];
// if we're at the end of the buffer, try to process the command anyway
// if we've got a new line, we've finished sending the commands, process them
if (current_position >= BUF_SIZE || this->data[current_position - 1] == '\n' || this->data[current_position - 1] == '\r')
// If driver is uninstalled or an error occurs, abort read gracefully
if (len < 0)
{
data[current_position] = '\0';
current_position = 0;
const nlohmann::json result = this->commandManager->executeFromJson(std::string_view(reinterpret_cast<const char *>(this->data)));
const auto resultMessage = result.dump();
usb_serial_jtag_write_bytes_chunked(resultMessage.c_str(), resultMessage.length(), 1000 / 20);
return;
}
if (len > 0)
{
// Notify main that a command was received during startup
notify_startup_command_received();
}
// since we've got something on the serial port
// we gotta keep reading until we've got the whole message
// we will submit the command once we get a newline, a return or the buffer is full
for (auto i = 0; i < len; i++)
{
this->data[current_position++] = this->temp_data[i];
// if we're at the end of the buffer, try to process the command anyway
// if we've got a new line, we've finished sending the commands, process them
if (current_position >= BUF_SIZE || this->data[current_position - 1] == '\n' || this->data[current_position - 1] == '\r')
{
data[current_position] = '\0';
current_position = 0;
const nlohmann::json result = this->commandManager->executeFromJson(std::string_view(reinterpret_cast<const char*>(this->data)));
const auto resultMessage = result.dump();
usb_serial_jtag_write_bytes_chunked(resultMessage.c_str(), resultMessage.length(), 1000 / 20);
}
}
}
}
void SerialManager::shutdown()
{
// Uninstall the USB Serial JTAG driver to free the internal USB for TinyUSB.
esp_err_t err = usb_serial_jtag_driver_uninstall();
if (err == ESP_OK)
{
ESP_LOGI("[SERIAL]", "usb_serial_jtag driver uninstalled");
}
else if (err != ESP_ERR_INVALID_STATE)
{
ESP_LOGW("[SERIAL]", "usb_serial_jtag_driver_uninstall returned %s", esp_err_to_name(err));
}
// Uninstall the USB Serial JTAG driver to free the internal USB for TinyUSB.
esp_err_t err = usb_serial_jtag_driver_uninstall();
if (err == ESP_OK)
{
ESP_LOGI("[SERIAL]", "usb_serial_jtag driver uninstalled");
}
else if (err != ESP_ERR_INVALID_STATE)
{
ESP_LOGW("[SERIAL]", "usb_serial_jtag_driver_uninstall returned %s", esp_err_to_name(err));
}
}
void HandleCDCSerialManagerTask(void *pvParameters)
void HandleCDCSerialManagerTask(void* pvParameters)
{
#ifndef CONFIG_USE_UART_FOR_COMMUNICATION
auto const commandManager = static_cast<CommandManager *>(pvParameters);
static char buffer[BUF_SIZE];
auto idx = 0;
auto const commandManager = static_cast<CommandManager*>(pvParameters);
static char buffer[BUF_SIZE];
auto idx = 0;
cdc_command_packet_t packet;
while (true)
{
if (xQueueReceive(cdcMessageQueue, &packet, portMAX_DELAY) == pdTRUE)
cdc_command_packet_t packet;
while (true)
{
for (auto i = 0; i < packet.len; i++)
{
buffer[idx++] = packet.data[i];
// if we're at the end of the buffer, try to process the command anyway
// if we've got a new line, we've finished sending the commands, process them
if (idx >= BUF_SIZE || buffer[idx - 1] == '\n' || buffer[idx - 1] == '\r')
if (xQueueReceive(cdcMessageQueue, &packet, portMAX_DELAY) == pdTRUE)
{
buffer[idx - 1] = '\0';
const nlohmann::json result = commandManager->executeFromJson(std::string_view(reinterpret_cast<const char *>(buffer)));
const auto resultMessage = result.dump();
tud_cdc_write(resultMessage.c_str(), resultMessage.length());
tud_cdc_write_flush();
idx = 0;
for (auto i = 0; i < packet.len; i++)
{
buffer[idx++] = packet.data[i];
// if we're at the end of the buffer, try to process the command anyway
// if we've got a new line, we've finished sending the commands, process them
if (idx >= BUF_SIZE || buffer[idx - 1] == '\n' || buffer[idx - 1] == '\r')
{
buffer[idx - 1] = '\0';
const nlohmann::json result = commandManager->executeFromJson(std::string_view(reinterpret_cast<const char*>(buffer)));
const auto resultMessage = result.dump();
tud_cdc_write(resultMessage.c_str(), resultMessage.length());
tud_cdc_write_flush();
idx = 0;
}
}
}
}
}
}
#endif
}
@@ -117,36 +117,35 @@ void HandleCDCSerialManagerTask(void *pvParameters)
// grab the data and send it to a queue, a special task will process it and handle with the command manager
extern "C" void tud_cdc_rx_cb(uint8_t itf)
{
// we can void the interface number
(void)itf;
cdc_command_packet_t packet;
auto len = tud_cdc_available();
// we can void the interface number
(void)itf;
cdc_command_packet_t packet;
auto len = tud_cdc_available();
if (len > 0)
{
auto read = tud_cdc_read(packet.data, sizeof(packet.data));
if (read > 0)
if (len > 0)
{
// we should be safe here, given that the max buffer size is 64
packet.len = static_cast<uint8_t>(read);
xQueueSend(cdcMessageQueue, &packet, 1);
auto read = tud_cdc_read(packet.data, sizeof(packet.data));
if (read > 0)
{
// we should be safe here, given that the max buffer size is 64
packet.len = static_cast<uint8_t>(read);
xQueueSend(cdcMessageQueue, &packet, 1);
}
}
}
}
extern "C" void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
{
(void)itf;
(void)dtr;
(void)rts;
(void)itf;
(void)dtr;
(void)rts;
ESP_LOGI("[SERIAL]", "CDC line state changed: DTR=%d, RTS=%d", dtr, rts);
ESP_LOGI("[SERIAL]", "CDC line state changed: DTR=%d, RTS=%d", dtr, rts);
}
void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const *p_line_coding)
void tud_cdc_line_coding_cb(uint8_t itf, cdc_line_coding_t const* p_line_coding)
{
(void)itf;
ESP_LOGI("[SERIAL]", "CDC line coding: %" PRIu32 " bps, %d stop bits, %d parity, %d data bits",
p_line_coding->bit_rate, p_line_coding->stop_bits,
p_line_coding->parity, p_line_coding->data_bits);
(void)itf;
ESP_LOGI("[SERIAL]", "CDC line coding: %" PRIu32 " bps, %d stop bits, %d parity, %d data bits", p_line_coding->bit_rate, p_line_coding->stop_bits,
p_line_coding->parity, p_line_coding->data_bits);
}