diff --git a/UNOR4USBBridge/cmds_wifi_SSL.h b/UNOR4USBBridge/cmds_wifi_SSL.h index 81d18fe..eff4912 100644 --- a/UNOR4USBBridge/cmds_wifi_SSL.h +++ b/UNOR4USBBridge/cmds_wifi_SSL.h @@ -12,6 +12,10 @@ INCBIN(x509_crt_bundle, PATH_CERT_BUNDLE); #include "at_handler.h" +#ifndef WIFI_CLIENT_DEF_CONN_TIMEOUT_MS +#define WIFI_CLIENT_DEF_CONN_TIMEOUT_MS (3000) +#endif + void CAtHandler::add_cmds_wifi_SSL() { /* ....................................................................... */ command_table[_SSLBEGINCLIENT] = [this](auto & srv, auto & parser) { @@ -233,6 +237,61 @@ void CAtHandler::add_cmds_wifi_SSL() { } }; + /* ....................................................................... */ + command_table[_SSLCLIENTCONNECT] = [this](auto & srv, auto & parser) { + /* ....................................................................... */ + switch (parser.cmd_mode) { + case chAT::CommandMode::Write: { + if (parser.args.size() < 3) { + return chAT::CommandStatus::ERROR; + } + + auto &sock_num = parser.args[0]; + if (sock_num.empty()) { + return chAT::CommandStatus::ERROR; + } + + int sock = atoi(sock_num.c_str()); + CClientWrapper the_client = getClient(sock); + + if (the_client.sslclient == nullptr) { + return chAT::CommandStatus::ERROR; + } + + auto &host = parser.args[1]; + if (host.empty()) { + return chAT::CommandStatus::ERROR; + } + + auto &port = parser.args[2]; + if (port.empty()) { + return chAT::CommandStatus::ERROR; + } + + 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; + } + } + + if (!the_client.sslclient->connect(host.c_str(), atoi(port.c_str()), timeout)) { + return chAT::CommandStatus::ERROR; + } + srv.write_response_prompt(); + srv.write_line_end(); + return chAT::CommandStatus::OK; + } + default: + return chAT::CommandStatus::ERROR; + } + }; + /* ....................................................................... */ command_table[_SSLCLIENTSEND] = [this](auto & srv, auto & parser) { /* ....................................................................... */ diff --git a/UNOR4USBBridge/cmds_wifi_netif.h b/UNOR4USBBridge/cmds_wifi_netif.h index 658e4dd..236d49f 100644 --- a/UNOR4USBBridge/cmds_wifi_netif.h +++ b/UNOR4USBBridge/cmds_wifi_netif.h @@ -5,6 +5,10 @@ #define INCREMENT_MOD(x,MOD) x = (++x) % MOD +#ifndef WIFI_CLIENT_DEF_CONN_TIMEOUT_MS +#define WIFI_CLIENT_DEF_CONN_TIMEOUT_MS (3000) +#endif + /* -------------------------------------------------------------------------- */ void CAtHandler::add_cmds_wifi_netif() { /* -------------------------------------------------------------------------- */ @@ -197,6 +201,62 @@ void CAtHandler::add_cmds_wifi_netif() { } }; + /* ....................................................................... */ + command_table[_CLIENTCONNECT] = [this](auto & srv, auto & parser) { + /* ....................................................................... */ + switch (parser.cmd_mode) { + case chAT::CommandMode::Write: { + if (parser.args.size() < 3) { + return chAT::CommandStatus::ERROR; + } + + auto &sock_num = parser.args[0]; + if (sock_num.empty()) { + return chAT::CommandStatus::ERROR; + } + + int sock = atoi(sock_num.c_str()); + + CClientWrapper the_client = getClient(sock); + + if (the_client.client == nullptr) { + return chAT::CommandStatus::ERROR; + } + + auto &hostname = parser.args[1]; + if (hostname.empty()) { + return chAT::CommandStatus::ERROR; + } + + auto &hostport = parser.args[2]; + if (hostport.empty()) { + return chAT::CommandStatus::ERROR; + } + + 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; + } + } + + if (!the_client.client->connect(hostname.c_str(), atoi(hostport.c_str()), timeout)) { + return chAT::CommandStatus::ERROR; + } + srv.write_response_prompt(); + srv.write_line_end(); + return chAT::CommandStatus::OK; + } + default: + return chAT::CommandStatus::ERROR; + } + }; + /* ....................................................................... */ command_table[_CLIENTSEND] = [this](auto & srv, auto & parser) { /* ....................................................................... */ diff --git a/UNOR4USBBridge/commands.h b/UNOR4USBBridge/commands.h index a2cdf2f..c68f426 100644 --- a/UNOR4USBBridge/commands.h +++ b/UNOR4USBBridge/commands.h @@ -49,6 +49,7 @@ enum { #define _CLIENTSTATE "+CLIENTSTATE" #define _CLIENTCONNECTIP "+CLIENTCONNECTIP" #define _CLIENTCONNECTNAME "+CLIENTCONNECTNAME" +#define _CLIENTCONNECT "+CLIENTCONNECT" #define _CLIENTSEND "+CLIENTSEND" #define _CLIENTRECEIVE "+CLIENTRECEIVE" #define _CLIENTCLOSE "+CLIENTCLOSE" @@ -59,6 +60,7 @@ enum { #define _SETCAROOT "+SETCAROOT" #define _SSLCLIENTSTATE "+SSLCLIENTSTATE" #define _SSLCLIENTCONNECTNAME "+SSLCLIENTCONNECTNAME" +#define _SSLCLIENTCONNECT "+SSLCLIENTCONNECT" #define _SETIP "+SETIP" #define _GETHOSTBYNAME "+HOSTBYNAME" #define _AVAILABLE "+AVAILABLE"