diff --git a/.gitmodules b/.gitmodules index 6bf3930..8322415 100644 --- a/.gitmodules +++ b/.gitmodules @@ -19,3 +19,6 @@ url = https://github.com/arduino/arduino-esp32.git branch = unor4wifi-2.0.9 shallow = true +[submodule "libraries/ArduinoHttpClient"] + path = libraries/ArduinoHttpClient + url = https://github.com/arduino-libraries/ArduinoHttpClient.git diff --git a/UNOR4USBBridge/OTA.cpp b/UNOR4USBBridge/OTA.cpp index 93b28ac..296e3d9 100644 --- a/UNOR4USBBridge/OTA.cpp +++ b/UNOR4USBBridge/OTA.cpp @@ -22,19 +22,27 @@ #include #include #include "OTA.h" +#include "BossaUnoR4WiFi.h" + +#include "FS.h" +#include "SPIFFS.h" +#include "at_handler.h" /****************************************************************************** PUBLIC MEMBER FUNCTIONS ******************************************************************************/ -Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::begin(const char* file_path, uint32_t magic) -{ - /* initialize private variables */ - otaInit(); +Arduino_UNOWIFIR4_OTA::Arduino_UNOWIFIR4_OTA() +: _updating_renesas(true) { - /* ... initialize CRC ... */ - crc32Init(); +} +Arduino_UNOWIFIR4_OTA::~Arduino_UNOWIFIR4_OTA() { + closeStorage(); +} + +Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::begin(const char* file_path, uint32_t magic) +{ /* ... configure board Magic number */ setMagic(magic); @@ -47,7 +55,7 @@ Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::begin(const char* file_path, uin SPIFFS.remove(file_path); } - _spiffs = true; + _updating_renesas = true; SPIFFS.end(); return Error::None; @@ -55,15 +63,14 @@ Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::begin(const char* file_path, uin void Arduino_UNOWIFIR4_OTA::write_byte_to_flash(uint8_t data) { - if(_spiffs) { + if(_updating_renesas) { int ret = fwrite(&data, sizeof(data), 1, _file); } else { Arduino_ESP32_OTA::write_byte_to_flash(data); } } -int Arduino_UNOWIFIR4_OTA::download(const char * ota_url, const char* file_path) -{ +int Arduino_UNOWIFIR4_OTA::initStorage(const char* file_path) { if(!SPIFFS.begin()) { DEBUG_ERROR("%s: failed to initialize SPIFFS", __FUNCTION__); return static_cast(Error::OtaStorageInit); @@ -76,33 +83,66 @@ int Arduino_UNOWIFIR4_OTA::download(const char * ota_url, const char* file_path) DEBUG_ERROR("%s: failed to write SPIFFS", __FUNCTION__); return static_cast(Error::OtaStorageInit); } + return static_cast(Error::None); +} - /* Download and decode OTA file */ - size_t size = download(ota_url); +int Arduino_UNOWIFIR4_OTA::closeStorage() { + int res = 0; + if(_file != nullptr) { + res = fclose(_file); + _file = nullptr; + } - fclose(_file); - _file = nullptr; SPIFFS.end(); - return size; + return res; } -Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::verify() +int Arduino_UNOWIFIR4_OTA::download(const char * ota_url, const char* file_path) { - /* ... then finalize ... */ - crc32Finalize(); + int res = initStorage(file_path); - if(!crc32Verify()) { - DEBUG_ERROR("%s: CRC32 mismatch", __FUNCTION__); - return Error::OtaHeaderCrc; + if(res < 0) { + return res; } - return Error::None; + + /* Download and decode OTA file */ + res = download(ota_url); + + closeStorage(); + + return res; } -Arduino_ESP32_OTA::Error Arduino_UNOWIFIR4_OTA::update() +int Arduino_UNOWIFIR4_OTA::startDownload(const char * ota_url, const char* file_path) { - if (!Update.end(true)) { - DEBUG_ERROR("%s: Failure to apply OTA update. Error: %s", __FUNCTION__, Update.errorString()); - return Error::OtaStorageEnd; + int res = initStorage(file_path); + + if(res < 0) { + return res; } - return Error::None; + + /* Download and decode OTA file */ + res = startDownload(ota_url); + + if(res < 0) { + closeStorage(); + } + + return res; +} + +int Arduino_UNOWIFIR4_OTA::downloadPoll() +{ + auto res = Arduino_ESP32_OTA::downloadPoll(); + + if(_updating_renesas && res != 0) { + closeStorage(); + } + + return res; +} + +int Arduino_UNOWIFIR4_OTA::update(const char* file_path) +{ + return BOSSA.program(file_path, Serial, GPIO_BOOT, GPIO_RST); } diff --git a/UNOR4USBBridge/OTA.h b/UNOR4USBBridge/OTA.h index e14e72e..17b54be 100644 --- a/UNOR4USBBridge/OTA.h +++ b/UNOR4USBBridge/OTA.h @@ -35,9 +35,11 @@ class Arduino_UNOWIFIR4_OTA : public Arduino_ESP32_OTA { - public: + Arduino_UNOWIFIR4_OTA(); + ~Arduino_UNOWIFIR4_OTA(); + enum class UNO_WiFi_R4_Error : int { StorageConfig = -20, @@ -45,16 +47,29 @@ class Arduino_UNOWIFIR4_OTA : public Arduino_ESP32_OTA using Arduino_ESP32_OTA::begin; Arduino_ESP32_OTA::Error begin(const char* file_path, uint32_t magic = ARDUINO_RA4M1_OTA_MAGIC); + using Arduino_ESP32_OTA::download; int download(const char * ota_url, const char* file_path); + + using Arduino_ESP32_OTA::startDownload; + int startDownload(const char * ota_url, const char* file_path); + + int downloadPoll() override; + using Arduino_ESP32_OTA::downloadProgress; + void write_byte_to_flash(uint8_t data); - Arduino_ESP32_OTA::Error verify(); - Arduino_ESP32_OTA::Error update(); -private: + using Arduino_ESP32_OTA::verify; + + using Arduino_ESP32_OTA::update; + int update(const char* file_path); + int initStorage(const char* file_path); + int closeStorage(); + +private: FILE* _file; - bool _spiffs; + bool _updating_renesas; }; #endif /* ARDUINO_UNOWIFIR4_OTA_H_ */ diff --git a/UNOR4USBBridge/UNOR4USBBridge.ino b/UNOR4USBBridge/UNOR4USBBridge.ino index b024dfc..d9c4d6b 100644 --- a/UNOR4USBBridge/UNOR4USBBridge.ino +++ b/UNOR4USBBridge/UNOR4USBBridge.ino @@ -61,8 +61,8 @@ void ets_putc_handler(char c) /* -------------------------------------------------------------------------- */ void CAtHandler::onWiFiEvent(WiFiEvent_t event) { -/* -------------------------------------------------------------------------- */ - switch (event) { +/* -------------------------------------------------------------------------- */ + switch (event) { case ARDUINO_EVENT_WIFI_READY: wifi_status = WIFI_ST_IDLE_STATUS; break; @@ -81,7 +81,7 @@ void CAtHandler::onWiFiEvent(WiFiEvent_t event) { case ARDUINO_EVENT_WIFI_STA_DISCONNECTED: wifi_status = WIFI_ST_IDLE_STATUS; break; - case ARDUINO_EVENT_WIFI_AP_START: + case ARDUINO_EVENT_WIFI_AP_START: wifi_status = WIFI_ST_AP_LISTENING; break; case ARDUINO_EVENT_WIFI_AP_STOP: @@ -91,7 +91,7 @@ void CAtHandler::onWiFiEvent(WiFiEvent_t event) { wifi_status = WIFI_ST_AP_CONNECTED; break; case ARDUINO_EVENT_WIFI_AP_STADISCONNECTED: - + break; case ARDUINO_EVENT_WIFI_AP_STAIPASSIGNED: @@ -120,7 +120,7 @@ void atLoop(void* param) { /* -------------------------------------------------------------------------- */ void setup() { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ /* redirect stdout */ stdout = funopen(NULL, NULL, &write_fn, NULL, NULL); @@ -184,7 +184,7 @@ static uint8_t buf[2048]; /* -------------------------------------------------------------------------- */ void loop() { -/* -------------------------------------------------------------------------- */ +/* -------------------------------------------------------------------------- */ if (SERIAL_USER.baudRate() != _baud) { _baud = SERIAL_USER.baudRate(); diff --git a/UNOR4USBBridge/at_handler.cpp b/UNOR4USBBridge/at_handler.cpp index fc08c75..8894ee3 100644 --- a/UNOR4USBBridge/at_handler.cpp +++ b/UNOR4USBBridge/at_handler.cpp @@ -22,62 +22,76 @@ 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; - } + CClientWrapper rv; + + bool is_server = false; + bool is_sslclienet = false; + + int internal_sock = -1; - if (is_sslclienet) { + 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) { + } + else if(is_server) { rv.client = &serverClients[internal_sock].client; rv.can_delete = -1; - } - else { + } + else { rv.client = clients[internal_sock]; rv.can_delete = internal_sock; - } - return rv; + } + return rv; } /* -------------------------------------------------------------------------- */ void CAtHandler::run() { -/* -------------------------------------------------------------------------- */ - at_srv.run(); - vTaskDelay(1); +/* -------------------------------------------------------------------------- */ + at_srv.run(); + + // execute all the pending tasks + + // it is intended for the tasks to add other tasks in queue after being executed, + // saving the current size of the queue + auto size = tasks.size(); + for(int i=0; isecond(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..fa6be19 100644 --- a/UNOR4USBBridge/at_handler.h +++ b/UNOR4USBBridge/at_handler.h @@ -4,6 +4,7 @@ #include "chAT.hpp" #include "WiFi.h" #include "Server.h" +#include #include "WiFiClient.h" #include @@ -54,10 +55,10 @@ class CServerClient { class CAtHandler { private: - static uint8_t wifi_status; + 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]; @@ -66,6 +67,10 @@ class CAtHandler { std::vector clients_ca[MAX_CLIENT_AVAILABLE]; std::vector clients_cert_pem[MAX_CLIENT_AVAILABLE]; std::vector clients_key_pem[MAX_CLIENT_AVAILABLE]; + + // this vector is a list of function to be called in the run function + std::queue> tasks; + int udps_num = 0; int servers_num = 0; int clientsToServer_num = 0; @@ -80,8 +85,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(); @@ -90,6 +95,10 @@ class CAtHandler { void add_cmds_preferences(); void add_cmds_se(); public: + inline void addTask(std::function task) { + tasks.push(task); + } + /* Used by cmds_se */ std::vector se_buf; diff --git a/UNOR4USBBridge/cmds_ota.h b/UNOR4USBBridge/cmds_ota.h index de1f7f7..0e23c75 100644 --- a/UNOR4USBBridge/cmds_ota.h +++ b/UNOR4USBBridge/cmds_ota.h @@ -3,14 +3,14 @@ #include "at_handler.h" #include "OTA.h" -#include "BossaUnoR4WiFi.h" Arduino_UNOWIFIR4_OTA OTA; +void otaDownloadProgress(CAtHandler* at_handler); void CAtHandler::add_cmds_ota() { /* ....................................................................... */ command_table[_OTA_SETCAROOT] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { case chAT::CommandMode::Write: { if (parser.args.size() != 1) { @@ -26,7 +26,7 @@ void CAtHandler::add_cmds_ota() { if(!ca_root_size) { return chAT::CommandStatus::ERROR; } - + ota_cert_buf = srv.inhibit_read(ca_root_size); size_t offset = ota_cert_buf.size(); if(offset < ca_root_size) { @@ -40,7 +40,7 @@ void CAtHandler::add_cmds_ota() { return chAT::CommandStatus::OK; } default: - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } }; @@ -50,7 +50,7 @@ void CAtHandler::add_cmds_ota() { switch (parser.cmd_mode) { case chAT::CommandMode::Run: { Arduino_ESP32_OTA::Error ota_error = OTA.begin(); - String error = String((int)ota_error) + "\r\n"; + String error = String((int)ota_error) + _ENDL; srv.write_response_prompt(); srv.write_str((const char *)(error.c_str())); srv.write_line_end(); @@ -60,14 +60,14 @@ void CAtHandler::add_cmds_ota() { if (parser.args.size() != 1) { return chAT::CommandStatus::ERROR; } - + auto &path = parser.args[0]; if (path.empty()) { return chAT::CommandStatus::ERROR; } Arduino_ESP32_OTA::Error ota_error = OTA.begin(path.c_str()); - String error = String((int)ota_error) + "\r\n"; + String error = String((int)ota_error) + _ENDL; srv.write_response_prompt(); srv.write_str((const char *)(error.c_str())); srv.write_line_end(); @@ -89,9 +89,9 @@ void CAtHandler::add_cmds_ota() { if (url.empty()) { return chAT::CommandStatus::ERROR; } - + int ota_error = OTA.download(url.c_str()); - String error = String((int)ota_error) + "\r\n"; + String error = String((int)ota_error) + _ENDL; srv.write_response_prompt(); srv.write_str((const char *)(error.c_str())); srv.write_line_end(); @@ -108,7 +108,58 @@ void CAtHandler::add_cmds_ota() { } int ota_error = OTA.download(url.c_str(), path.c_str()); - String error = String((int)ota_error) + "\r\n"; + String error = String((int)ota_error) + _ENDL; + srv.write_response_prompt(); + srv.write_str((const char *)(error.c_str())); + srv.write_line_end(); + return chAT::CommandStatus::OK; + } else { + return chAT::CommandStatus::ERROR; + } + + } + default: + return chAT::CommandStatus::ERROR; + } + }; + + /* ....................................................................... */ + command_table[_OTA_DOWNLOAD_START] = [this](auto & srv, auto & parser) { + /* ....................................................................... */ + + switch (parser.cmd_mode) { + case chAT::CommandMode::Write: { + if (parser.args.size() == 1) { + auto &url = parser.args[0]; + if (url.empty()) { + return chAT::CommandStatus::ERROR; + } + + int ota_error = OTA.startDownload(url.c_str()); + String error = String((int)ota_error) + _ENDL; + srv.write_response_prompt(); + srv.write_str((const char *)(error.c_str())); + srv.write_line_end(); + + this->addTask(std::bind(&otaDownloadProgress, this)); + + return chAT::CommandStatus::OK; + } else if(parser.args.size() == 2) { + auto &url = parser.args[0]; + if (url.empty()) { + return chAT::CommandStatus::ERROR; + } + + auto &path = parser.args[1]; + if (path.empty()) { + return chAT::CommandStatus::ERROR; + } + + int ota_error = OTA.startDownload(url.c_str(), path.c_str()); + + this->addTask(std::bind(&otaDownloadProgress, this)); + + String error = String((int)ota_error) + _ENDL; srv.write_response_prompt(); srv.write_str((const char *)(error.c_str())); srv.write_line_end(); @@ -123,13 +174,31 @@ void CAtHandler::add_cmds_ota() { } }; + /* ....................................................................... */ + command_table[_OTA_DOWNLOAD_PROGRESS] = [this](auto & srv, auto & parser) { + /* ....................................................................... */ + switch (parser.cmd_mode) { + case chAT::CommandMode::Run: { + int progress = OTA.downloadProgress(); + String pogress_str = String((int)progress) + _ENDL; + srv.write_response_prompt(); + srv.write_str((const char *)(pogress_str.c_str())); + srv.write_line_end(); + + return chAT::CommandStatus::OK; + } + default: + return chAT::CommandStatus::ERROR; + } + }; + /* ....................................................................... */ command_table[_OTA_VERIFY] = [this](auto & srv, auto & parser) { /* ....................................................................... */ switch (parser.cmd_mode) { case chAT::CommandMode::Run: { Arduino_ESP32_OTA::Error ota_error = OTA.verify(); - String error = String((int)ota_error) + "\r\n"; + String error = String((int)ota_error) + _ENDL; srv.write_response_prompt(); srv.write_str((const char *)(error.c_str())); srv.write_line_end(); @@ -146,7 +215,7 @@ void CAtHandler::add_cmds_ota() { switch (parser.cmd_mode) { case chAT::CommandMode::Run: { Arduino_ESP32_OTA::Error ota_error = OTA.update(); - String error = String((int)ota_error) + "\r\n"; + String error = String((int)ota_error) + _ENDL; srv.write_response_prompt(); srv.write_str((const char *)(error.c_str())); srv.write_line_end(); @@ -156,14 +225,14 @@ void CAtHandler::add_cmds_ota() { if (parser.args.size() != 1) { return chAT::CommandStatus::ERROR; } - + auto &path = parser.args[0]; if (path.empty()) { return chAT::CommandStatus::ERROR; } - int flash_error = BOSSA.program(path.c_str(), Serial, GPIO_BOOT, GPIO_RST); - String error = String(flash_error) + "\r\n"; + int flash_error = OTA.update(path.c_str()); + String error = String(flash_error) + _ENDL; srv.write_response_prompt(); srv.write_str((const char *)(error.c_str())); srv.write_line_end(); @@ -191,4 +260,15 @@ void CAtHandler::add_cmds_ota() { }; } +// in order to make the download progress downloadPoll must be called periodically +// until the end, this function readds itself to the task list until completed +void otaDownloadProgress(CAtHandler* at_handler) { + auto res = OTA.downloadPoll(); + + // continue to progress the download if the state is not completed or not an error + if(res == 0) { + at_handler->addTask(std::bind(&otaDownloadProgress, at_handler)); + } +}; + #endif diff --git a/UNOR4USBBridge/cmds_wifi_SSL.h b/UNOR4USBBridge/cmds_wifi_SSL.h index cdc78e1..cd4a85d 100644 --- a/UNOR4USBBridge/cmds_wifi_SSL.h +++ b/UNOR4USBBridge/cmds_wifi_SSL.h @@ -21,7 +21,7 @@ INCBIN(x509_crt_bundle, PATH_CERT_BUNDLE); void CAtHandler::add_cmds_wifi_SSL() { /* ....................................................................... */ command_table[_SSLBEGINCLIENT] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { case chAT::CommandMode::Run: { if (sslclients_num < MAX_CLIENT_AVAILABLE) { @@ -51,7 +51,7 @@ void CAtHandler::add_cmds_wifi_SSL() { /* ....................................................................... */ command_table[_SETCAROOT] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { case chAT::CommandMode::Write: { if (parser.args.size() < 1) { @@ -90,7 +90,7 @@ void CAtHandler::add_cmds_wifi_SSL() { if(ca_root_custom) { clients_ca[internal_sock] = srv.inhibit_read(ca_root_size); size_t offset = clients_ca[internal_sock].size(); - + if(offset < ca_root_size) { clients_ca[internal_sock].resize(ca_root_size); @@ -240,7 +240,7 @@ void CAtHandler::add_cmds_wifi_SSL() { /* ....................................................................... */ command_table[_SSLCLIENTSTATE] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { case chAT::CommandMode::Write: { if (parser.args.size() != 1) { @@ -275,7 +275,7 @@ void CAtHandler::add_cmds_wifi_SSL() { /* ....................................................................... */ command_table[_SSLCLIENTCONNECTNAME] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { case chAT::CommandMode::Write: { if (parser.args.size() != 3) { @@ -335,10 +335,10 @@ void CAtHandler::add_cmds_wifi_SSL() { return chAT::CommandStatus::ERROR; } }; - + /* ....................................................................... */ command_table[_SSLCLIENTCONNECTIP] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { case chAT::CommandMode::Write: { if (parser.args.size() != 3) { @@ -443,14 +443,14 @@ void CAtHandler::add_cmds_wifi_SSL() { int timeout = WIFI_CLIENT_DEF_CONN_TIMEOUT_MS; if (parser.args.size() > 3) { - auto &tmp = parser.args[3]; - if (tmp.empty()) { - return chAT::CommandStatus::ERROR; - } - int t = atoi(tmp.c_str()); - if (t > 0) { - timeout = t; - } + auto &tmp = parser.args[3]; + if (tmp.empty()) { + return chAT::CommandStatus::ERROR; + } + int t = atoi(tmp.c_str()); + if (t > 0) { + timeout = t; + } } /* Set custom root ca */ @@ -482,21 +482,21 @@ void CAtHandler::add_cmds_wifi_SSL() { /* ....................................................................... */ command_table[_SSLCLIENTSEND] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { case chAT::CommandMode::Write: { - /* the command receive 2 data: the socket and the length of data + /* the command receive 2 data: the socket and the length of data to be transmitted */ if (parser.args.size() != 2) { - + return chAT::CommandStatus::ERROR; } /* socket */ auto &sock_num = parser.args[0]; if (sock_num.empty()) { - + return chAT::CommandStatus::ERROR; } @@ -510,32 +510,32 @@ void CAtHandler::add_cmds_wifi_SSL() { /* data len */ auto &size_p = parser.args[1]; if (size_p.empty()) { - + return chAT::CommandStatus::ERROR; } int data_size = atoi(size_p.c_str()); if(data_size <= 0) { - + return chAT::CommandStatus::ERROR; } - /* socket and data received - answer back _CLIENTDATA: DATA\r\n + /* socket and data received + answer back _CLIENTDATA: DATA\r\n so that data transmission can begin */ //srv.write_response_prompt(); //srv.write_str(" DATA"); //srv.write_line_end(); //return chAT::CommandStatus::OK; - + /* ----------------------------------- * BEGIN TRANSPARENT DATA TRANSMISSION * ----------------------------------- */ std::vector data_received; data_received = srv.inhibit_read(data_size); size_t offset = data_received.size(); - + if(offset < data_size) { data_received.resize(data_size); @@ -549,11 +549,11 @@ void CAtHandler::add_cmds_wifi_SSL() { int sent = 0; sent += the_client.sslclient->write(data_received.data() + sent, data_received.size() - sent); - + if (sent < data_received.size()) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } - + return chAT::CommandStatus::OK; } default: @@ -563,15 +563,15 @@ void CAtHandler::add_cmds_wifi_SSL() { /* ....................................................................... */ command_table[_SSLCLIENTCLOSE] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { case chAT::CommandMode::Write: { if (parser.args.size() != 1) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } auto &sock_num = parser.args[0]; if (sock_num.empty()) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } int sock = atoi(sock_num.c_str()); @@ -581,7 +581,7 @@ void CAtHandler::add_cmds_wifi_SSL() { } else { the_client.sslclient->stop(); - + if(the_client.can_delete >= 0) { delete sslclients[the_client.can_delete]; sslclients[the_client.can_delete] = nullptr; @@ -603,15 +603,15 @@ void CAtHandler::add_cmds_wifi_SSL() { /* ....................................................................... */ command_table[_SSLIPCLIENT] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { case chAT::CommandMode::Write: { if (parser.args.size() != 1) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } auto &sock_num = parser.args[0]; if (sock_num.empty()) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } int sock = atoi(sock_num.c_str()); CClientWrapper the_client = getClient(sock); @@ -629,22 +629,22 @@ void CAtHandler::add_cmds_wifi_SSL() { return chAT::CommandStatus::OK; } default: - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } }; /* ....................................................................... */ command_table[_SSLCLIENTCONNECTED] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { case chAT::CommandMode::Write: {// write to do the read of a specific list if (parser.args.size() != 1) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } auto &socket_num = parser.args[0]; if (socket_num.empty()) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } int sock = atoi(socket_num.c_str()); @@ -662,21 +662,21 @@ void CAtHandler::add_cmds_wifi_SSL() { } default: - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } }; /* ....................................................................... */ command_table[_SSLCLIENTRECEIVE] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { case chAT::CommandMode::Write: { if (parser.args.size() != 2) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } auto &socket_num = parser.args[0]; if (socket_num.empty()) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } int sock = atoi(socket_num.c_str()); CClientWrapper the_client = getClient(sock); @@ -687,12 +687,12 @@ void CAtHandler::add_cmds_wifi_SSL() { auto &size = parser.args[1]; if (size.empty()) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } int data_wanted = atoi(size.c_str()); if(data_wanted <= 0) { return chAT::CommandStatus::ERROR; - } + } int data_available = the_client.sslclient->available(); data_wanted = (data_wanted < data_available) ? data_wanted : data_available; @@ -702,7 +702,7 @@ void CAtHandler::add_cmds_wifi_SSL() { int res = the_client.sslclient->read(data_received.data(), data_wanted); String results = String(data_received.size()) + "|"; - + srv.write_response_prompt(); srv.write_str((const char *)(results.c_str())); srv.write_vec8(data_received); @@ -716,16 +716,16 @@ void CAtHandler::add_cmds_wifi_SSL() { /* ....................................................................... */ command_table[_SSLAVAILABLE] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { - + case chAT::CommandMode::Write: { if (parser.args.size() != 1) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } auto &socket_num = parser.args[0]; if (socket_num.empty()) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } int sock = atoi(socket_num.c_str()); CClientWrapper the_client = getClient(sock); @@ -747,16 +747,16 @@ void CAtHandler::add_cmds_wifi_SSL() { /* ....................................................................... */ command_table[_SSLCLIENTSTATUS] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { - + case chAT::CommandMode::Write: { if (parser.args.size() != 1) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } auto &socket_num = parser.args[0]; if (socket_num.empty()) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } int sock = atoi(socket_num.c_str()); CClientWrapper the_client = getClient(sock); @@ -764,7 +764,7 @@ void CAtHandler::add_cmds_wifi_SSL() { if (the_client.sslclient == nullptr) { return chAT::CommandStatus::ERROR; } - + srv.write_response_prompt(); //String st(sslclients[sock]->status()); //srv.write_str((const char *)(st.c_str())); @@ -778,16 +778,16 @@ void CAtHandler::add_cmds_wifi_SSL() { /* ....................................................................... */ command_table[_SSLCLIENTFLUSH] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { - + case chAT::CommandMode::Write: { if (parser.args.size() != 1) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } auto &socket_num = parser.args[0]; if (socket_num.empty()) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } int sock = atoi(socket_num.c_str()); CClientWrapper the_client = getClient(sock); @@ -808,16 +808,16 @@ void CAtHandler::add_cmds_wifi_SSL() { /* ....................................................................... */ command_table[_SSLREMOTEIP] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { - + case chAT::CommandMode::Write: { if (parser.args.size() != 1) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } auto &socket_num = parser.args[0]; if (socket_num.empty()) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } int sock = atoi(socket_num.c_str()); CClientWrapper the_client = getClient(sock); @@ -840,16 +840,16 @@ void CAtHandler::add_cmds_wifi_SSL() { /* ....................................................................... */ command_table[_SSLREMOTEPORT] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ + /* ....................................................................... */ switch (parser.cmd_mode) { - + case chAT::CommandMode::Write: { if (parser.args.size() != 1) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } auto &socket_num = parser.args[0]; if (socket_num.empty()) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } int sock = atoi(socket_num.c_str()); CClientWrapper the_client = getClient(sock); @@ -872,16 +872,16 @@ void CAtHandler::add_cmds_wifi_SSL() { /* ....................................................................... */ command_table[_SSLPEEK] = [this](auto & srv, auto & parser) { - /* ....................................................................... */ - + /* ....................................................................... */ + switch (parser.cmd_mode) { case chAT::CommandMode::Write: { if (parser.args.size() != 1) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } auto &socket_num = parser.args[0]; if (socket_num.empty()) { - return chAT::CommandStatus::ERROR; + return chAT::CommandStatus::ERROR; } int sock = atoi(socket_num.c_str()); CClientWrapper the_client = getClient(sock); diff --git a/UNOR4USBBridge/commands.h b/UNOR4USBBridge/commands.h index ffa9555..bade7dc 100644 --- a/UNOR4USBBridge/commands.h +++ b/UNOR4USBBridge/commands.h @@ -17,7 +17,7 @@ enum file_op { }; #define _AT "AT" -#define _ENDL "\r\n" +#define _ENDL "\r\n" #define _WIFISCAN "+WIFISCAN" #define _RESET "+RESET" @@ -34,7 +34,7 @@ enum file_op { #define _DISCONNECT "+DISCONNECT" #define _BEGINSOFTAP "+BEGINSOFTAP" #define _MACSTA "+MACSTA" -#define _MACSOFTAP "+MACSOFTAP" +#define _MACSOFTAP "+MACSOFTAP" #define _DISCONNECTSOFTAP "+DISCONNECTSOFTAP" #define _AUTOCONNECT "+AUTOCONNECT" #define _IPSTA "+IPSTA" @@ -87,63 +87,65 @@ enum file_op { #define _SERVERACCEPT "+SERVERACCEPT" #define _SERVEREND "+SERVEREND" -#define _UDPBEGIN "+UDPBEGIN" -#define _UDPBEGINMULTI "+UDPBEGINMULTI" -#define _UDPBEGINPACKET "+UDPBEGINPACKET" -#define _UDPBEGINPACKETMULTI "+BEGINPACKETMULTI" -#define _UDPBEGINPACKETNAME "+UDPBEGINPACKETADD" -#define _UDPBEGINPACKETIP "+UDPBEGINPACKETIP" -#define _UDPENDPACKET "+UDPENDPACKET" -#define _UDPWRITE "+UDPWRITE" -#define _UDPPARSE "+UDPPARSE" -#define _UDPAVAILABLE "+UDPAVAILABLE" -#define _UDPREAD "+UDPREAD" -#define _UDPPEEK "+UDPPEEK" -#define _UDPFLUSH "+UDPFLUSH" -#define _UDPREMOTEIP "+UDPREMOTEIP" -#define _UDPREMOTEPORT "+UDPREMOTEPORT" -#define _UDPSTOP "+UDPSTOP" - -#define _FWVERSION "+FWVERSION" - -#define _SOFTAPCONFIG "+SOFTAPCONFIG" -#define _SERVERWRITE "+SERVERWRITE" - -#define _HCI_BEGIN "+HCIBEGIN" -#define _HCI_END "+HCIEND" -#define _HCI_WAIT "+HCIWAIT" -#define _HCI_READ "+HCIREAD" -#define _HCI_WRITE "+HCIWRITE" -#define _HCI_AVAILABLE "+HCIAVAILABLE" - -#define _OTA_SETCAROOT "+OTASETCAROOT" -#define _OTA_BEGIN "+OTABEGIN" -#define _OTA_DOWNLOAD "+OTADOWNLOAD" -#define _OTA_VERIFY "+OTAVERIFY" -#define _OTA_UPDATE "+OTAUPDATE" -#define _OTA_RESET "+OTARESET" - -#define _PREF_BEGIN "+PREFBEGIN" -#define _PREF_END "+PREFEND" -#define _PREF_CLEAR "+PREFCLEAR" -#define _PREF_REMOVE "+PREFREMOVE" -#define _PREF_PUT "+PREFPUT" -#define _PREF_GET "+PREFGET" -#define _PREF_LEN "+PREFLEN" -#define _PREF_STAT "+PREFSTAT" - -#define _SOFTSE_BEGIN "+SOFTSEBEGIN" -#define _SOFTSE_END "+SOFTSEEND" -#define _SOFTSE_SERIAL "+SOFTSE_SERIAL" -#define _SOFTSE_RND "+SOFTSE_RND" -#define _SOFTSE_PRI_KEY "+SOFTSE_PRI_KEY" -#define _SOFTSE_PUB_KEY "+SOFTSE_PUB_KEY" -#define _SOFTSE_WRITE_SLOT "+SOFTSE_WRITE_SLOT" -#define _SOFTSE_READ_SLOT "+SOFTSE_READ_SLOT" -#define _SOFTSE_S_V_BUF_SET "+SOFTSE_S_V_BUF_SET" -#define _SOFTSE_SIGN_GET "+SOFTSE_SIGN_GET" -#define _SOFTSE_VERIFY_GET "+SOFTSE_VERIFY_GET" -#define _SOFTSE_SHA256_GET "+SOFTSE_SHA256_GET" +#define _UDPBEGIN "+UDPBEGIN" +#define _UDPBEGINMULTI "+UDPBEGINMULTI" +#define _UDPBEGINPACKET "+UDPBEGINPACKET" +#define _UDPBEGINPACKETMULTI "+BEGINPACKETMULTI" +#define _UDPBEGINPACKETNAME "+UDPBEGINPACKETADD" +#define _UDPBEGINPACKETIP "+UDPBEGINPACKETIP" +#define _UDPENDPACKET "+UDPENDPACKET" +#define _UDPWRITE "+UDPWRITE" +#define _UDPPARSE "+UDPPARSE" +#define _UDPAVAILABLE "+UDPAVAILABLE" +#define _UDPREAD "+UDPREAD" +#define _UDPPEEK "+UDPPEEK" +#define _UDPFLUSH "+UDPFLUSH" +#define _UDPREMOTEIP "+UDPREMOTEIP" +#define _UDPREMOTEPORT "+UDPREMOTEPORT" +#define _UDPSTOP "+UDPSTOP" + +#define _FWVERSION "+FWVERSION" + +#define _SOFTAPCONFIG "+SOFTAPCONFIG" +#define _SERVERWRITE "+SERVERWRITE" + +#define _HCI_BEGIN "+HCIBEGIN" +#define _HCI_END "+HCIEND" +#define _HCI_WAIT "+HCIWAIT" +#define _HCI_READ "+HCIREAD" +#define _HCI_WRITE "+HCIWRITE" +#define _HCI_AVAILABLE "+HCIAVAILABLE" + +#define _OTA_SETCAROOT "+OTASETCAROOT" +#define _OTA_BEGIN "+OTABEGIN" +#define _OTA_DOWNLOAD "+OTADOWNLOAD" +#define _OTA_DOWNLOAD_START "+OTADOWNLOADSTART" +#define _OTA_DOWNLOAD_PROGRESS "+OTADOWNLOADPROGRESS" +#define _OTA_VERIFY "+OTAVERIFY" +#define _OTA_UPDATE "+OTAUPDATE" +#define _OTA_RESET "+OTARESET" + +#define _PREF_BEGIN "+PREFBEGIN" +#define _PREF_END "+PREFEND" +#define _PREF_CLEAR "+PREFCLEAR" +#define _PREF_REMOVE "+PREFREMOVE" +#define _PREF_PUT "+PREFPUT" +#define _PREF_GET "+PREFGET" +#define _PREF_LEN "+PREFLEN" +#define _PREF_STAT "+PREFSTAT" + +#define _SOFTSE_BEGIN "+SOFTSEBEGIN" +#define _SOFTSE_END "+SOFTSEEND" +#define _SOFTSE_SERIAL "+SOFTSE_SERIAL" +#define _SOFTSE_RND "+SOFTSE_RND" +#define _SOFTSE_PRI_KEY "+SOFTSE_PRI_KEY" +#define _SOFTSE_PUB_KEY "+SOFTSE_PUB_KEY" +#define _SOFTSE_WRITE_SLOT "+SOFTSE_WRITE_SLOT" +#define _SOFTSE_READ_SLOT "+SOFTSE_READ_SLOT" +#define _SOFTSE_S_V_BUF_SET "+SOFTSE_S_V_BUF_SET" +#define _SOFTSE_SIGN_GET "+SOFTSE_SIGN_GET" +#define _SOFTSE_VERIFY_GET "+SOFTSE_VERIFY_GET" +#define _SOFTSE_SHA256_GET "+SOFTSE_SHA256_GET" diff --git a/libraries/ArduinoHttpClient b/libraries/ArduinoHttpClient new file mode 160000 index 0000000..6f2659d --- /dev/null +++ b/libraries/ArduinoHttpClient @@ -0,0 +1 @@ +Subproject commit 6f2659de4c0523f0a5fa4bc1154d1e5dc1b01546 diff --git a/libraries/Arduino_ESP32_OTA b/libraries/Arduino_ESP32_OTA index fc755e7..5ed0df5 160000 --- a/libraries/Arduino_ESP32_OTA +++ b/libraries/Arduino_ESP32_OTA @@ -1 +1 @@ -Subproject commit fc755e7d1d3946232107e2590662ee08d6ccdec4 +Subproject commit 5ed0df5a21d655e7ec28d7fcee04da95657d24be