mirror of
https://github.com/BotChain-Robots/firmware.git
synced 2026-07-08 17:47:21 +02:00
Cleanup logging
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
#include "CommunicationRouter.h"
|
||||
|
||||
#include <AngleControlMessageBuilder.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "CommunicationRouter.h"
|
||||
#include "AngleControlMessageBuilder.h"
|
||||
#include "mDNSDiscoveryService.h"
|
||||
#include "MPIMessageBuilder.h"
|
||||
#include "WifiManager.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "Tables.h"
|
||||
#include "PtrQueue.h"
|
||||
#include "OrientationDetection.h"
|
||||
|
||||
#define TAG "CommunicationRouter"
|
||||
|
||||
CommunicationRouter::~CommunicationRouter() {
|
||||
vTaskDelete(m_router_thread);
|
||||
}
|
||||
@@ -24,7 +25,7 @@ CommunicationRouter::~CommunicationRouter() {
|
||||
|
||||
while (true) {
|
||||
const auto buffer = that->m_tcp_rx_queue->dequeue();
|
||||
std::cout << "routing from tcp" << std::endl;
|
||||
ESP_LOGD(TAG, "Got message from TCP");
|
||||
that->route(buffer->data(), buffer->size());
|
||||
}
|
||||
}
|
||||
@@ -35,10 +36,10 @@ CommunicationRouter::~CommunicationRouter() {
|
||||
const auto channel = params->channel;
|
||||
delete params;
|
||||
|
||||
char buffer[512];
|
||||
size_t bytes_received = 0;
|
||||
that->m_data_link_manager->start_receive_frames(channel);
|
||||
while (true) {
|
||||
char buffer[512];
|
||||
// todo: very c style function calls
|
||||
const auto err = that->m_data_link_manager->receive(reinterpret_cast<uint8_t *>(buffer), 512, &bytes_received, channel);
|
||||
that->m_data_link_manager->start_receive_frames(channel);
|
||||
@@ -46,7 +47,7 @@ CommunicationRouter::~CommunicationRouter() {
|
||||
// todo: do we only want one thread ever doing this?
|
||||
if (std::chrono::system_clock::now() - that->m_last_leader_updated > std::chrono::seconds(15)) {
|
||||
that->m_last_leader_updated = std::chrono::system_clock::now();
|
||||
std::cout << "Updating leader" << std::endl;
|
||||
ESP_LOGI(TAG, "Updating leader");
|
||||
that->update_leader();
|
||||
}
|
||||
|
||||
@@ -54,12 +55,13 @@ CommunicationRouter::~CommunicationRouter() {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::cout << "routing message from rmt" << std::endl;
|
||||
ESP_LOGD(TAG, "Got message from RMT");
|
||||
that->route(reinterpret_cast<uint8_t *>(buffer), bytes_received);
|
||||
}
|
||||
}
|
||||
|
||||
int CommunicationRouter::send_msg(char* buffer, const size_t length) const {
|
||||
ESP_LOGD(TAG, "Got message from application");
|
||||
route(reinterpret_cast<uint8_t *>(buffer), length);
|
||||
return 0;
|
||||
}
|
||||
@@ -101,17 +103,16 @@ void CommunicationRouter::route(uint8_t* buffer, const size_t length) const {
|
||||
const auto& mpi_message = Flatbuffers::MPIMessageBuilder::parse_mpi_message(buffer);
|
||||
|
||||
if (mpi_message->destination() == m_module_id) {
|
||||
std::cout << "Routing to this module [dest:" << static_cast<int>(mpi_message->destination()) << ", length: " << length << "]" << std::endl;
|
||||
|
||||
ESP_LOGD(TAG, "Routing to this module [dest: %d, length: %d]", static_cast<int>(mpi_message->destination()), length);
|
||||
this->m_rx_callback(reinterpret_cast<char *>(buffer), 512);
|
||||
} else if (mpi_message->destination() == PC_ADDR && this->m_leader == m_module_id) {
|
||||
std::cout << "Routing to wifi [dest:" << static_cast<int>(mpi_message->destination()) << ", length: " << length << "]" << std::endl;
|
||||
ESP_LOGD(TAG, "Routing to wifi [dest: %d, length: %d]", static_cast<int>(mpi_message->destination()), length);
|
||||
this->m_tcp_server->send_msg(reinterpret_cast<char *>(buffer), 512);
|
||||
} else if (mpi_message->destination() == PC_ADDR) {
|
||||
std::cout << "Routing to wireline for wifi [dest:" << static_cast<int>(mpi_message->destination()) << ", length: " << length << "]" << std::endl;
|
||||
ESP_LOGD(TAG, "Routing to wireline for wifi [dest: %d, length: %d]", static_cast<int>(mpi_message->destination()), length);
|
||||
this->m_data_link_manager->send(this->m_leader, buffer, length, FrameType::MOTOR_TYPE, 0);
|
||||
}else {
|
||||
std::cout << "Routing to wireline [dest:" << static_cast<int>(mpi_message->destination()) << ", length: " << length << "]" << std::endl;
|
||||
ESP_LOGD(TAG, "Routing to wireline [dest: %d, length: %d]", static_cast<int>(mpi_message->destination()), length);
|
||||
this->m_data_link_manager->send(mpi_message->destination(), buffer, length, FrameType::MOTOR_TYPE, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,12 @@
|
||||
// Created by Johnathon Slightham on 2025-07-26.
|
||||
//
|
||||
|
||||
#include <constants/module.h>
|
||||
#include "constants/module.h"
|
||||
#include "OrientationDetection.h"
|
||||
#include "ConfigManager.h"
|
||||
#include "iostream"
|
||||
|
||||
#include <ConfigManager.h>
|
||||
#include <iostream>
|
||||
#include <bits/ostream.tcc>
|
||||
#define TAG "OrientationDetection"
|
||||
|
||||
void OrientationDetection::init() {
|
||||
const auto& config_manager = ConfigManager::get_instance();
|
||||
@@ -21,16 +21,16 @@ void OrientationDetection::init() {
|
||||
|
||||
Orientation OrientationDetection::get_orientation(const uint8_t channel) {
|
||||
if (gpio_get_level(static_cast<gpio_num_t>(CHANNEL_TO_90_DEG_MAP[channel]))) {
|
||||
std::cout << "90deg" << std::endl;
|
||||
ESP_LOGD(TAG, "90deg");
|
||||
return Orientation_Deg90;
|
||||
} else if (gpio_get_level(static_cast<gpio_num_t>(CHANNEL_TO_180_DEG_MAP[channel]))) {
|
||||
std::cout << "180deg" << std::endl;
|
||||
ESP_LOGD(TAG, "180deg");
|
||||
return Orientation_Deg180;
|
||||
} else if (gpio_get_level(static_cast<gpio_num_t>(CHANNEL_TO_270_DEG_MAP[channel]))) {
|
||||
std::cout << "270deg" << std::endl;
|
||||
ESP_LOGD(TAG, "270deg");
|
||||
return Orientation_Deg270;
|
||||
} else {
|
||||
std::cout << "No orientation detected" << std::endl;
|
||||
ESP_LOGD(TAG, "No orientation detected");
|
||||
return Orientation_Deg0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,23 @@
|
||||
#include <string.h>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <esp_log.h>
|
||||
#include <sys/param.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_netif.h"
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/sys.h"
|
||||
#include <lwip/netdb.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "lwip/netdb.h"
|
||||
#include "TCPServer.h"
|
||||
|
||||
#include <memory>
|
||||
#include <bits/shared_ptr_base.h>
|
||||
#include <constants/app_comms.h>
|
||||
|
||||
#include "bits/shared_ptr_base.h"
|
||||
#include "constants/app_comms.h"
|
||||
#include "constants/tcp.h"
|
||||
|
||||
#define TAG "TCPServer"
|
||||
|
||||
// todo: - add message routing to correct client
|
||||
// - authenticate (don't just return true from the auth function)
|
||||
// - tx from board
|
||||
@@ -51,18 +50,18 @@ TCPServer::~TCPServer() {
|
||||
const auto that = static_cast<TCPServer*>(args);
|
||||
|
||||
while (true) {
|
||||
printf("Attempting to start TCP Server on port %d", that->m_port);
|
||||
ESP_LOGI(TAG, "Attempting to start TCP Server on port %d", that->m_port);
|
||||
|
||||
that->m_server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
|
||||
if (that->m_server_sock < 0) {
|
||||
printf("Unable to create TCP socket: errno %d\n", errno);
|
||||
ESP_LOGE(TAG, "Unable to create TCP socket: errno %d\n", errno);
|
||||
vTaskDelay(SLEEP_AFTER_FAIL_MS / portTICK_PERIOD_MS);
|
||||
continue;
|
||||
}
|
||||
|
||||
constexpr int opt = 1;
|
||||
setsockopt(that->m_server_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
||||
printf("Socket created\n");
|
||||
ESP_LOGI(TAG, "Socket created\n");
|
||||
|
||||
sockaddr_in server_addr = {
|
||||
.sin_family = AF_INET,
|
||||
@@ -74,18 +73,18 @@ TCPServer::~TCPServer() {
|
||||
|
||||
int err = bind(that->m_server_sock, reinterpret_cast<struct sockaddr *>(&server_addr), sizeof(server_addr));
|
||||
if (0 != err) {
|
||||
printf("Socket unable to bind: errno %d\n", errno);
|
||||
ESP_LOGE(TAG, "Socket unable to bind: errno %d\n", errno);
|
||||
close(that->m_server_sock);
|
||||
that->m_server_sock = -1;
|
||||
vTaskDelay(SLEEP_AFTER_FAIL_MS / portTICK_PERIOD_MS);
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("Socket bound to port %d\n", that->m_port);
|
||||
ESP_LOGI(TAG, "Socket bound to port %d\n", that->m_port);
|
||||
|
||||
err = listen(that->m_server_sock, TCP_DEFAULT_LISTEN_BACKLOG);
|
||||
if (0 != err) {
|
||||
printf("Error occurred during TCP listen: errno %d\n", errno);
|
||||
ESP_LOGE(TAG, "Error occurred during TCP listen: errno %d\n", errno);
|
||||
close(that->m_server_sock);
|
||||
that->m_server_sock = -1;
|
||||
vTaskDelay(SLEEP_AFTER_FAIL_MS / portTICK_PERIOD_MS);
|
||||
@@ -97,13 +96,13 @@ TCPServer::~TCPServer() {
|
||||
socklen_t addr_len = sizeof(client_addr);
|
||||
int client_sock = accept(that->m_server_sock, reinterpret_cast<struct sockaddr *>(&client_addr), &addr_len);
|
||||
if (client_sock < 0) {
|
||||
printf("Unable to accept TCP connection: errno %d\n", errno);
|
||||
ESP_LOGE(TAG, "Unable to accept TCP connection: errno %d\n", errno);
|
||||
continue;
|
||||
}
|
||||
|
||||
err = that->authenticate_client(client_sock);
|
||||
if (0 != err) {
|
||||
printf("Client failed authentication\n");
|
||||
ESP_LOGE(TAG, "Client failed authentication\n");
|
||||
}
|
||||
|
||||
setsockopt(client_sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive, sizeof(int));
|
||||
@@ -154,18 +153,17 @@ TCPServer::~TCPServer() {
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("Message size: %ld\n", msg_size);
|
||||
ESP_LOGD(TAG, "Message size: %ld\n", msg_size);
|
||||
|
||||
int len = recv(sock, buffer->data(), msg_size, MSG_WAITALL);
|
||||
if (len < 0) {
|
||||
printf("Error occurred during receiving: errno %d\n", errno);
|
||||
if (int len = recv(sock, buffer->data(), msg_size, MSG_WAITALL); len < 0) {
|
||||
ESP_LOGE(TAG, "Error occurred during receiving: errno %d\n", errno);
|
||||
to_remove.emplace_back(sock);
|
||||
} else if (0 == len) {
|
||||
printf("Connection closed\n");
|
||||
ESP_LOGW(TAG, "Connection closed\n");
|
||||
close(sock);
|
||||
to_remove.emplace_back(sock);
|
||||
} else {
|
||||
printf("TCP Server Received %d bytes\n", len);
|
||||
ESP_LOGD(TAG, "TCP Server Received %d bytes\n", len);
|
||||
buffer->resize(len);
|
||||
that->m_rx_queue->enqueue(std::move(buffer));
|
||||
}
|
||||
@@ -219,7 +217,6 @@ int TCPServer::send_msg(char *buffer, uint32_t length) const {
|
||||
}
|
||||
|
||||
for (const auto client_sock : m_clients) {
|
||||
std::cout << "sending tcp" << std::endl;
|
||||
send(client_sock, &length, 4, 0);
|
||||
send(client_sock, buffer, length, 0);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include "constants/wifi.h"
|
||||
#include "mDNSDiscoveryService.h"
|
||||
|
||||
#define TAG "WifiManager"
|
||||
|
||||
WifiManager::~WifiManager() {
|
||||
this->handle_disconnect();
|
||||
vTaskDelete(this->m_task);
|
||||
@@ -32,21 +34,21 @@ int WifiManager::disconnect() {
|
||||
|
||||
switch (state) {
|
||||
case wifi_state::connect:
|
||||
printf("Attempting to connect to wifi in station mode\n");
|
||||
ESP_LOGI(TAG, "Attempting to connect to wifi in station mode\n");
|
||||
init_connection();
|
||||
update_state(wifi_state::connecting);
|
||||
break;
|
||||
case wifi_state::connecting:
|
||||
printf("connecting...\n");
|
||||
ESP_LOGI(TAG, "connecting...\n");
|
||||
handle_connecting();
|
||||
break;
|
||||
case wifi_state::broadcast:
|
||||
printf("Attempting to broadcast in softap mode\n");
|
||||
ESP_LOGI(TAG, "Attempting to broadcast in softap mode\n");
|
||||
init_softap();
|
||||
update_state(wifi_state::broadcasting);
|
||||
break;
|
||||
case wifi_state::disconnect:
|
||||
printf("Shutting down wifi\n");
|
||||
ESP_LOGI(TAG, "Shutting down wifi\n");
|
||||
handle_disconnect();
|
||||
update_state(wifi_state::disconnected);
|
||||
break;
|
||||
@@ -169,13 +171,13 @@ void WifiManager::wifi_event_handler(void *event_handler_arg, esp_event_base_t e
|
||||
const auto that = static_cast<WifiManager *>(event_handler_arg);
|
||||
|
||||
if (WIFI_EVENT_STA_START == event_id) {
|
||||
printf("Station mode started\n");
|
||||
ESP_LOGI(TAG, "Station mode started\n");
|
||||
} else if (WIFI_EVENT_STA_CONNECTED == event_id) {
|
||||
printf("Connected to wifi in station mode\n");
|
||||
ESP_LOGI(TAG, "Connected to wifi in station mode\n");
|
||||
that->update_state(wifi_state::connected);
|
||||
mDNSDiscoveryService::setup();
|
||||
} else if (WIFI_EVENT_STA_DISCONNECTED == event_id) {
|
||||
printf("Station mode shutdown\n");
|
||||
ESP_LOGI(TAG, "Station mode shutdown\n");
|
||||
xSemaphoreTake(that->m_mutex, portMAX_DELAY);
|
||||
if (that->m_state == wifi_state::connected) {
|
||||
xSemaphoreGive(that->m_mutex);
|
||||
@@ -185,13 +187,13 @@ void WifiManager::wifi_event_handler(void *event_handler_arg, esp_event_base_t e
|
||||
xSemaphoreGive(that->m_mutex);
|
||||
}
|
||||
} else if (IP_EVENT_STA_GOT_IP == event_id) {
|
||||
printf("Got IP as station\n");
|
||||
ESP_LOGI(TAG, "Got IP as station\n");
|
||||
} else if (WIFI_EVENT_AP_STACONNECTED == event_id) {
|
||||
printf("User connected to AP\n");
|
||||
ESP_LOGI(TAG, "User connected to AP\n");
|
||||
} else if (WIFI_EVENT_AP_STADISCONNECTED == event_id) {
|
||||
printf("User disconnected from AP\n");
|
||||
ESP_LOGI(TAG, "User disconnected from AP\n");
|
||||
} else if (WIFI_EVENT_AP_START == event_id) {
|
||||
mDNSDiscoveryService::setup();
|
||||
printf("AP started\n");
|
||||
ESP_LOGI(TAG, "AP started\n");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user