From 2be5023bff58ecb04f7e3105e389e0e52ef3e5df Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Mon, 29 Jul 2024 14:10:13 +0200 Subject: [PATCH 1/2] codestyle unification --- UNOR4USBBridge/UNOR4USBBridge.ino | 3 +- UNOR4USBBridge/at_handler.cpp | 82 +++++++++++++++---------------- UNOR4USBBridge/at_handler.h | 6 +-- 3 files changed, 45 insertions(+), 46 deletions(-) diff --git a/UNOR4USBBridge/UNOR4USBBridge.ino b/UNOR4USBBridge/UNOR4USBBridge.ino index b024dfc..461bc2a 100644 --- a/UNOR4USBBridge/UNOR4USBBridge.ino +++ b/UNOR4USBBridge/UNOR4USBBridge.ino @@ -173,7 +173,6 @@ void setup() { 1, /* Priority of the task */ &atTask, /* Task handle. */ 0); /* Core where the task should run */ - } /* @@ -250,4 +249,4 @@ void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t event_id, extern "C" void mylogchar(char c) { SERIAL_USER.print(c); -} \ No newline at end of file +} diff --git a/UNOR4USBBridge/at_handler.cpp b/UNOR4USBBridge/at_handler.cpp index fc08c75..f80410d 100644 --- a/UNOR4USBBridge/at_handler.cpp +++ b/UNOR4USBBridge/at_handler.cpp @@ -22,45 +22,45 @@ uint8_t CAtHandler::wifi_status = WIFI_ST_IDLE_STATUS; /* -------------------------------------------------------------------------- */ CClientWrapper CAtHandler::getClient(int sock) { /* -------------------------------------------------------------------------- */ - CClientWrapper rv; - - bool is_server = false; - bool is_sslclienet = false; - - int internal_sock = -1; - - if(sock >= START_SSL_CLIENT_SOCK) { - internal_sock = sock - START_SSL_CLIENT_SOCK; - is_sslclienet = true; - } else - if(sock >= START_CLIENT_SERVER_SOCK) { - internal_sock = sock - START_CLIENT_SERVER_SOCK; - is_server = true; - } - else { - internal_sock = sock; - } - - if(internal_sock < 0 || internal_sock >= MAX_CLIENT_AVAILABLE) { - rv.client = nullptr; - rv.sslclient = nullptr; - rv.can_delete = -1; - return rv; - } - - if (is_sslclienet) { - rv.sslclient = sslclients[internal_sock]; + CClientWrapper rv; + + bool is_server = false; + bool is_sslclienet = false; + + int internal_sock = -1; + + if(sock >= START_SSL_CLIENT_SOCK) { + internal_sock = sock - START_SSL_CLIENT_SOCK; + is_sslclienet = true; + } else + if(sock >= START_CLIENT_SERVER_SOCK) { + internal_sock = sock - START_CLIENT_SERVER_SOCK; + is_server = true; + } + else { + internal_sock = sock; + } + + if(internal_sock < 0 || internal_sock >= MAX_CLIENT_AVAILABLE) { + rv.client = nullptr; + rv.sslclient = nullptr; + rv.can_delete = -1; + return rv; + } + + if (is_sslclienet) { + rv.sslclient = sslclients[internal_sock]; + rv.can_delete = internal_sock; + } + else if(is_server) { + rv.client = &serverClients[internal_sock].client; + rv.can_delete = -1; + } + else { + rv.client = clients[internal_sock]; rv.can_delete = internal_sock; - } - else if(is_server) { - rv.client = &serverClients[internal_sock].client; - rv.can_delete = -1; - } - else { - rv.client = clients[internal_sock]; - rv.can_delete = internal_sock; - } - return rv; + } + return rv; } @@ -119,16 +119,16 @@ CAtHandler::CAtHandler(HardwareSerial *s) : last_server_client_sock(0) { if (it == command_table.end()) { return chAT::CommandStatus::ERROR; - } + } else { return it->second(srv, srv.parser()); } }); - /* SET UP COMMAND TABLE */ + /* SET UP COMMAND TABLE */ add_cmds_esp_generic(); add_cmds_wifi_station(); - add_cmds_wifi_softAP(); + add_cmds_wifi_softAP(); add_cmds_wifi_SSL(); add_cmds_wifi_netif(); add_cmds_wifi_udp(); diff --git a/UNOR4USBBridge/at_handler.h b/UNOR4USBBridge/at_handler.h index f4a987b..86af234 100644 --- a/UNOR4USBBridge/at_handler.h +++ b/UNOR4USBBridge/at_handler.h @@ -57,7 +57,7 @@ class CAtHandler { static uint8_t wifi_status; int last_server_client_sock; - + WiFiUDP * udps[MAX_UDP_AVAILABLE]; WiFiServer * serverWiFi[MAX_SERVER_AVAILABLE]; WiFiClient * clients[MAX_CLIENT_AVAILABLE]; @@ -80,8 +80,8 @@ class CAtHandler { CClientWrapper getClient(int sock); void add_cmds_esp_generic(); - void add_cmds_wifi_station(); - void add_cmds_wifi_softAP(); + void add_cmds_wifi_station(); + void add_cmds_wifi_softAP(); void add_cmds_wifi_SSL(); void add_cmds_wifi_netif(); void add_cmds_wifi_udp(); From 416c0a5352b0141df26ecf91bca7c1e3ac5ecd23 Mon Sep 17 00:00:00 2001 From: Andrea Gilardoni Date: Mon, 29 Jul 2024 14:17:55 +0200 Subject: [PATCH 2/2] [WIP] autodetection of baudrate --- UNOR4USBBridge/at_handler.cpp | 51 ++++++++++++++++++++++++++++++----- UNOR4USBBridge/at_handler.h | 5 +++- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/UNOR4USBBridge/at_handler.cpp b/UNOR4USBBridge/at_handler.cpp index f80410d..0f85938 100644 --- a/UNOR4USBBridge/at_handler.cpp +++ b/UNOR4USBBridge/at_handler.cpp @@ -63,21 +63,58 @@ CClientWrapper CAtHandler::getClient(int sock) { return rv; } - +static int baudrates[] = { + 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, + 1000000, 1500000, 2000000 +}; +int i = 0; /* -------------------------------------------------------------------------- */ void CAtHandler::run() { -/* -------------------------------------------------------------------------- */ - at_srv.run(); - vTaskDelay(1); +/* -------------------------------------------------------------------------- */ + switch(state) { + case Running: + at_srv.run(); + break; + case BaudRateDetection: + if(serial->available()) { + log_e("1available %d", serial->available()); + uint8_t a = serial->read(); + log_e("read 0x%X", a); + + serial->end(); + + while(serial->available() > 0) { + log_i("%02X", serial->read()); + } + + if (a == 0x55) { + state = Running; + + log_e("Baudrate detection finished %d", baudrates[i]); + serial->flush(); + serial->begin(baudrates[i], SERIAL_8N1, 6, 5); + serial->write(0x55); // send the confirmation the baudrate was detected + break; + } + + i = (i+1) % (sizeof(baudrates)/sizeof(baudrates[0])); + log_e("trying %d %d", i, baudrates[i]); + + serial->begin(baudrates[i], SERIAL_8N1, 6, 5); + serial->updateBaudRate(baudrates[i]); + } + break; + } + vTaskDelay(1); } /* -------------------------------------------------------------------------- */ -CAtHandler::CAtHandler(HardwareSerial *s) : last_server_client_sock(0) { -/* -------------------------------------------------------------------------- */ - +CAtHandler::CAtHandler(HardwareSerial *s) : last_server_client_sock(0), state(BaudRateDetection) { +/* -------------------------------------------------------------------------- */ + for(int i = 0; i < MAX_CLIENT_AVAILABLE; i++) { clients[i] = nullptr; } diff --git a/UNOR4USBBridge/at_handler.h b/UNOR4USBBridge/at_handler.h index 86af234..378b77c 100644 --- a/UNOR4USBBridge/at_handler.h +++ b/UNOR4USBBridge/at_handler.h @@ -54,7 +54,10 @@ class CServerClient { class CAtHandler { private: - static uint8_t wifi_status; + enum state_t { + Running, BaudRateDetection, + } state; + static uint8_t wifi_status; int last_server_client_sock;