mirror of
https://github.com/BotChain-Robots/firmware.git
synced 2026-07-08 17:47:21 +02:00
Initial TCP server
This commit is contained in:
16
components/constants/include/constants/tcp.h
Normal file
16
components/constants/include/constants/tcp.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
//
|
||||||
|
// Created by Johnathon Slightham on 2025-06-01.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef TCP_H
|
||||||
|
#define TCP_H
|
||||||
|
|
||||||
|
#define TCP_DEFAULT_LISTEN_BACKLOG 1
|
||||||
|
|
||||||
|
#define KEEPALIVE_IDLE 7200
|
||||||
|
#define KEEPALIVE_INTERVAL 75
|
||||||
|
#define KEEPALIVE_COUNT 9
|
||||||
|
|
||||||
|
#define SLEEP_AFTER_FAIL_MS 5000
|
||||||
|
|
||||||
|
#endif //TCP_H
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
#ifndef WIFI_H
|
#ifndef WIFI_H
|
||||||
#define WIFI_H
|
#define WIFI_H
|
||||||
|
|
||||||
|
// SoftAP Configuration
|
||||||
#define WIFI_SSID "botchain"
|
#define WIFI_SSID "botchain"
|
||||||
#define SOFTAP_CHANNEL 1
|
#define SOFTAP_CHANNEL 1
|
||||||
#define SOFTAP_MAX_CONNECTIONS 10
|
#define SOFTAP_MAX_CONNECTIONS 10
|
||||||
|
|||||||
@@ -1,3 +1,3 @@
|
|||||||
idf_component_register(SRCS "rpc.cpp" "WifiManager.cpp" "mDNSDiscoveryService.cpp"
|
idf_component_register(SRCS "rpc.cpp" "WifiManager.cpp" "mDNSDiscoveryService.cpp" "TCPServer"
|
||||||
PRIV_REQUIRES driver esp_event nvs_flash esp_netif esp_wifi espressif__mdns constants
|
PRIV_REQUIRES driver esp_event nvs_flash esp_netif esp_wifi espressif__mdns constants
|
||||||
INCLUDE_DIRS "include")
|
INCLUDE_DIRS "include")
|
||||||
|
|||||||
@@ -1,54 +1,201 @@
|
|||||||
#include <sys/socket.h>
|
#include <string.h>
|
||||||
|
#include <sys/param.h>
|
||||||
#include "esp_system.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "esp_mac.h"
|
#include "freertos/task.h"
|
||||||
|
#include "esp_event.h"
|
||||||
#include "esp_netif.h"
|
#include "esp_netif.h"
|
||||||
#include "esp_log.h"
|
|
||||||
|
|
||||||
#define HOST_IP_ADDR "192.168.0.196"
|
#include "lwip/err.h"
|
||||||
#define PORT 3001
|
#include "lwip/sockets.h"
|
||||||
#define TAG "SOCKET"
|
#include "lwip/sys.h"
|
||||||
|
#include <lwip/netdb.h>
|
||||||
|
|
||||||
static const char *payload = "Message from board";
|
#include "TCPServer.h"
|
||||||
|
|
||||||
int tcp_client() {
|
#include <bits/shared_ptr_base.h>
|
||||||
char rx_buffer[128];
|
|
||||||
char host_ip[] = HOST_IP_ADDR;
|
|
||||||
int addr_family = AF_INET;
|
|
||||||
int ip_protocol = IPPROTO_IP;
|
|
||||||
|
|
||||||
struct sockaddr_in dest_addr;
|
#include "constants/tcp.h"
|
||||||
inet_pton(AF_INET, host_ip, &dest_addr.sin_addr); // Convert ipv4 address to binary
|
|
||||||
dest_addr.sin_family = AF_INET;
|
|
||||||
dest_addr.sin_port = htons(PORT);
|
|
||||||
|
|
||||||
int sock = socket(addr_family, SOCK_STREAM, ip_protocol);
|
// todo: - add message routing to correct client
|
||||||
if (sock < 0) {
|
// - authenticate (don't just return true from the auth function)
|
||||||
ESP_LOGE(TAG, "Failed to create socket: %d", errno);
|
|
||||||
return sock;
|
TCPServer::TCPServer(const int port) {
|
||||||
|
this->m_port = port;
|
||||||
|
this->m_mutex = xSemaphoreCreateMutex();
|
||||||
|
this->m_clients = std::unordered_set<int>();
|
||||||
|
|
||||||
|
|
||||||
|
this->m_task = nullptr;
|
||||||
|
this->m_rx_task = nullptr;
|
||||||
|
this->m_tx_task = nullptr;
|
||||||
|
|
||||||
|
xTaskCreate(tcp_server_task, "tcp_accept_server", 4096, this, 5, &this->m_task);
|
||||||
|
xTaskCreate(socket_monitor_thread, "tcp_rx", 4096, this, 5, &this->m_rx_task);
|
||||||
|
}
|
||||||
|
|
||||||
|
TCPServer::~TCPServer() {
|
||||||
|
vTaskDelete(this->m_task);
|
||||||
|
vTaskDelete(this->m_rx_task);
|
||||||
|
vTaskDelete(this->m_tx_task);
|
||||||
|
vSemaphoreDelete(this->m_mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[noreturn]] void TCPServer::tcp_server_task(void *args) {
|
||||||
|
constexpr int keepAlive = 1;
|
||||||
|
constexpr int keepIdle = KEEPALIVE_IDLE;
|
||||||
|
constexpr int keepInterval = KEEPALIVE_INTERVAL;
|
||||||
|
constexpr int keepCount = KEEPALIVE_COUNT;
|
||||||
|
|
||||||
|
auto that = static_cast<TCPServer*>(args);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
printf("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);
|
||||||
|
vTaskDelay(SLEEP_AFTER_FAIL_MS / portTICK_PERIOD_MS);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (0 != connect(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr))) {
|
constexpr int opt = 1;
|
||||||
ESP_LOGE(TAG, "Failed to connect: %d", errno);
|
setsockopt(that->m_server_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
||||||
|
printf("Socket created\n");
|
||||||
|
|
||||||
|
sockaddr_in server_addr = {
|
||||||
|
.sin_family = AF_INET,
|
||||||
|
.sin_port = htons(that->m_port),
|
||||||
|
.sin_addr = {
|
||||||
|
.s_addr = htonl(INADDR_ANY),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
close(that->m_server_sock);
|
||||||
|
that->m_server_sock = -1;
|
||||||
|
vTaskDelay(SLEEP_AFTER_FAIL_MS / portTICK_PERIOD_MS);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Connected");
|
printf("Socket bound to port %d\n", that->m_port);
|
||||||
|
|
||||||
while(1) {
|
err = listen(that->m_server_sock, TCP_DEFAULT_LISTEN_BACKLOG);
|
||||||
int err = send(sock, payload, strlen(payload), 0);
|
if (0 != err) {
|
||||||
if (err < 0) {
|
printf("Error occurred during TCP listen: errno %d\n", errno);
|
||||||
ESP_LOGE(TAG, "Error occurred during sending: %d", errno);
|
close(that->m_server_sock);
|
||||||
|
that->m_server_sock = -1;
|
||||||
|
vTaskDelay(SLEEP_AFTER_FAIL_MS / portTICK_PERIOD_MS);
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
int len = recv(sock, rx_buffer, sizeof(rx_buffer) -1, 0); // blocking call
|
while (is_network_connected()) {
|
||||||
if (len < 0) {
|
sockaddr_in client_addr{};
|
||||||
ESP_LOGE(TAG, "Failed to receive: %d", errno);
|
socklen_t addr_len = sizeof(client_addr);
|
||||||
} else {
|
int client_sock = accept(that->m_server_sock, reinterpret_cast<struct sockaddr *>(&client_addr), &addr_len);
|
||||||
rx_buffer[len] = 0; // temp: Null terminate to treat as a string
|
if (client_sock < 0) {
|
||||||
ESP_LOGI(TAG, "Received %d bytes", len);
|
printf("Unable to accept TCP connection: errno %d\n", errno);
|
||||||
ESP_LOGI(TAG, "%s", rx_buffer);
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
err = that->authenticate_client(client_sock);
|
||||||
|
if (0 != err) {
|
||||||
|
printf("Client failed authentication\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
setsockopt(client_sock, SOL_SOCKET, SO_KEEPALIVE, &keepAlive, sizeof(int));
|
||||||
|
setsockopt(client_sock, IPPROTO_TCP, TCP_KEEPIDLE, &keepIdle, sizeof(int));
|
||||||
|
setsockopt(client_sock, IPPROTO_TCP, TCP_KEEPINTVL, &keepInterval, sizeof(int));
|
||||||
|
setsockopt(client_sock, IPPROTO_TCP, TCP_KEEPCNT, &keepCount, sizeof(int));
|
||||||
|
|
||||||
|
xSemaphoreTake(that->m_mutex, portMAX_DELAY);
|
||||||
|
that->m_clients.emplace(client_sock);
|
||||||
|
xSemaphoreGive(that->m_mutex);
|
||||||
|
}
|
||||||
|
|
||||||
|
close(that->m_server_sock);
|
||||||
|
vTaskDelay(SLEEP_AFTER_FAIL_MS / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[noreturn]] void TCPServer::socket_monitor_thread(void *args) {
|
||||||
|
auto that = static_cast<TCPServer *>(args);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
printf("top of loop\n");
|
||||||
|
fd_set readfds;
|
||||||
|
FD_ZERO(&readfds);
|
||||||
|
int max_fd = -1;
|
||||||
|
|
||||||
|
xSemaphoreTake(that->m_mutex, portMAX_DELAY);
|
||||||
|
for (const auto sock : that->m_clients) {
|
||||||
|
FD_SET(sock, &readfds);
|
||||||
|
if (sock > max_fd) max_fd = sock;
|
||||||
|
}
|
||||||
|
xSemaphoreGive(that->m_mutex);
|
||||||
|
|
||||||
|
timeval timeout = {1, 0}; // 1 second timeout
|
||||||
|
int ret = select(max_fd + 1, &readfds, nullptr, nullptr, &timeout);
|
||||||
|
|
||||||
|
if (ret > 0) {
|
||||||
|
xSemaphoreTake(that->m_mutex, portMAX_DELAY);
|
||||||
|
std::vector<int> to_remove;
|
||||||
|
for (int sock : that->m_clients) {
|
||||||
|
if (FD_ISSET(sock, &readfds)) {
|
||||||
|
// Handle socket
|
||||||
|
char buffer[512];
|
||||||
|
int len = recv(sock, buffer, sizeof(buffer) - 1, 0); // temp: for the null terminator
|
||||||
|
|
||||||
|
if (len < 0) {
|
||||||
|
printf("Error occurred during receiving: errno %d\n", errno);
|
||||||
|
} else if (0 == len) {
|
||||||
|
printf("Connection closed\n");
|
||||||
|
close(sock);
|
||||||
|
to_remove.emplace_back(sock);
|
||||||
|
} else {
|
||||||
|
// todo: send to rx queue
|
||||||
|
buffer[len] = 0; // temp: Null-terminate whatever is received and treat it like a string
|
||||||
|
printf("TCP Server Received %d bytes: %s\n", len, buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const auto r : to_remove) {
|
||||||
|
that->m_clients.erase(r);
|
||||||
|
}
|
||||||
|
|
||||||
|
xSemaphoreGive(that->m_mutex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TCPServer::is_network_connected() {
|
||||||
|
esp_netif_ip_info_t ip_info;
|
||||||
|
esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
|
||||||
|
|
||||||
|
if (netif != nullptr && esp_netif_get_ip_info(netif, &ip_info) == ESP_OK) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (0 != ip_info.ip.addr) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
netif = esp_netif_get_handle_from_ifkey("WIFI_AP_DEF");
|
||||||
|
|
||||||
|
if (netif != nullptr && esp_netif_get_ip_info(netif, &ip_info) == ESP_OK) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (0 != ip_info.ip.addr) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TCPServer::authenticate_client(int sock) {
|
||||||
|
// todo: authentication (wait for a passphrase from the client)
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ int WifiManager::init_connection() {
|
|||||||
wifi_config_t wifi_configuration;
|
wifi_config_t wifi_configuration;
|
||||||
wifi_configuration = {
|
wifi_configuration = {
|
||||||
.sta = {
|
.sta = {
|
||||||
.ssid = "dlink-C32D",
|
.ssid = "",
|
||||||
.password = "",
|
.password = "",
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -7,6 +7,6 @@
|
|||||||
|
|
||||||
class IRPCServer {
|
class IRPCServer {
|
||||||
|
|
||||||
}
|
};
|
||||||
|
|
||||||
#endif //IRPCSERVER_H
|
#endif //IRPCSERVER_H
|
||||||
|
|||||||
@@ -5,15 +5,34 @@
|
|||||||
#ifndef TCPSERVER_H
|
#ifndef TCPSERVER_H
|
||||||
#define TCPSERVER_H
|
#define TCPSERVER_H
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
|
||||||
#include "IRPCServer.h"
|
#include "IRPCServer.h"
|
||||||
|
|
||||||
class TCPServer : IRPCServer {
|
class TCPServer : IRPCServer {
|
||||||
public:
|
public:
|
||||||
TCPServer();
|
explicit TCPServer(int port);
|
||||||
~TCPServer();
|
~TCPServer();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
bool authenticate_client(int client_sock);
|
||||||
|
|
||||||
}
|
static bool is_network_connected();
|
||||||
|
[[noreturn]] static void tcp_server_task(void *args);
|
||||||
|
[[noreturn]] static void socket_monitor_thread(void *args);
|
||||||
|
|
||||||
|
int m_port;
|
||||||
|
int m_server_sock;
|
||||||
|
|
||||||
|
TaskHandle_t m_task;
|
||||||
|
TaskHandle_t m_rx_task;
|
||||||
|
TaskHandle_t m_tx_task;
|
||||||
|
|
||||||
|
SemaphoreHandle_t m_mutex;
|
||||||
|
std::unordered_set<int> m_clients;
|
||||||
|
};
|
||||||
|
|
||||||
#endif //TCPSERVER_H
|
#endif //TCPSERVER_H
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include "WifiManager.h"
|
#include "WifiManager.h"
|
||||||
#include "mDNSDiscoveryService.h"
|
#include "mDNSDiscoveryService.h"
|
||||||
|
#include "TCPServer.h"
|
||||||
|
|
||||||
extern "C" [[noreturn]] void app_main(void) {
|
extern "C" [[noreturn]] void app_main(void) {
|
||||||
nvs_flash_init();
|
nvs_flash_init();
|
||||||
@@ -18,6 +19,10 @@ extern "C" [[noreturn]] void app_main(void) {
|
|||||||
|
|
||||||
const auto discovery = std::make_unique<mDNSDiscoveryService>();
|
const auto discovery = std::make_unique<mDNSDiscoveryService>();
|
||||||
|
|
||||||
|
vTaskDelay(20000 / portTICK_PERIOD_MS);
|
||||||
|
|
||||||
|
const auto tcp_server = std::make_unique<TCPServer>(3001);
|
||||||
|
|
||||||
printf("Hello world!\n");
|
printf("Hello world!\n");
|
||||||
|
|
||||||
for (int i = 0; ; i++) {
|
for (int i = 0; ; i++) {
|
||||||
|
|||||||
Reference in New Issue
Block a user