Skip to content

AT new CLIENTCONNECT commands with parameter for timeout #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions UNOR4USBBridge/cmds_wifi_SSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
/* ....................................................................... */
Expand Down
60 changes: 60 additions & 0 deletions UNOR4USBBridge/cmds_wifi_netif.h
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
/* -------------------------------------------------------------------------- */
Expand Down Expand Up @@ -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) {
/* ....................................................................... */
Expand Down
2 changes: 2 additions & 0 deletions UNOR4USBBridge/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down