Skip to content

Commit da4f8b0

Browse files
authored
Merge pull request #23 from JAndrassy/at_client_connect_timeout
AT new CLIENTCONNECT commands with parameter for timeout
2 parents f1c1f02 + 8c45e18 commit da4f8b0

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

UNOR4USBBridge/cmds_wifi_SSL.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ INCBIN(x509_crt_bundle, PATH_CERT_BUNDLE);
1212

1313
#include "at_handler.h"
1414

15+
#ifndef WIFI_CLIENT_DEF_CONN_TIMEOUT_MS
16+
#define WIFI_CLIENT_DEF_CONN_TIMEOUT_MS (3000)
17+
#endif
18+
1519
void CAtHandler::add_cmds_wifi_SSL() {
1620
/* ....................................................................... */
1721
command_table[_SSLBEGINCLIENT] = [this](auto & srv, auto & parser) {
@@ -233,6 +237,61 @@ void CAtHandler::add_cmds_wifi_SSL() {
233237
}
234238
};
235239

240+
/* ....................................................................... */
241+
command_table[_SSLCLIENTCONNECT] = [this](auto & srv, auto & parser) {
242+
/* ....................................................................... */
243+
switch (parser.cmd_mode) {
244+
case chAT::CommandMode::Write: {
245+
if (parser.args.size() < 3) {
246+
return chAT::CommandStatus::ERROR;
247+
}
248+
249+
auto &sock_num = parser.args[0];
250+
if (sock_num.empty()) {
251+
return chAT::CommandStatus::ERROR;
252+
}
253+
254+
int sock = atoi(sock_num.c_str());
255+
CClientWrapper the_client = getClient(sock);
256+
257+
if (the_client.sslclient == nullptr) {
258+
return chAT::CommandStatus::ERROR;
259+
}
260+
261+
auto &host = parser.args[1];
262+
if (host.empty()) {
263+
return chAT::CommandStatus::ERROR;
264+
}
265+
266+
auto &port = parser.args[2];
267+
if (port.empty()) {
268+
return chAT::CommandStatus::ERROR;
269+
}
270+
271+
int timeout = WIFI_CLIENT_DEF_CONN_TIMEOUT_MS;
272+
if (parser.args.size() > 3) {
273+
auto &tmp = parser.args[3];
274+
if (tmp.empty()) {
275+
return chAT::CommandStatus::ERROR;
276+
}
277+
int t = atoi(tmp.c_str());
278+
if (t > 0) {
279+
timeout = t;
280+
}
281+
}
282+
283+
if (!the_client.sslclient->connect(host.c_str(), atoi(port.c_str()), timeout)) {
284+
return chAT::CommandStatus::ERROR;
285+
}
286+
srv.write_response_prompt();
287+
srv.write_line_end();
288+
return chAT::CommandStatus::OK;
289+
}
290+
default:
291+
return chAT::CommandStatus::ERROR;
292+
}
293+
};
294+
236295
/* ....................................................................... */
237296
command_table[_SSLCLIENTSEND] = [this](auto & srv, auto & parser) {
238297
/* ....................................................................... */

UNOR4USBBridge/cmds_wifi_netif.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
#define INCREMENT_MOD(x,MOD) x = (++x) % MOD
77

8+
#ifndef WIFI_CLIENT_DEF_CONN_TIMEOUT_MS
9+
#define WIFI_CLIENT_DEF_CONN_TIMEOUT_MS (3000)
10+
#endif
11+
812
/* -------------------------------------------------------------------------- */
913
void CAtHandler::add_cmds_wifi_netif() {
1014
/* -------------------------------------------------------------------------- */
@@ -197,6 +201,62 @@ void CAtHandler::add_cmds_wifi_netif() {
197201
}
198202
};
199203

204+
/* ....................................................................... */
205+
command_table[_CLIENTCONNECT] = [this](auto & srv, auto & parser) {
206+
/* ....................................................................... */
207+
switch (parser.cmd_mode) {
208+
case chAT::CommandMode::Write: {
209+
if (parser.args.size() < 3) {
210+
return chAT::CommandStatus::ERROR;
211+
}
212+
213+
auto &sock_num = parser.args[0];
214+
if (sock_num.empty()) {
215+
return chAT::CommandStatus::ERROR;
216+
}
217+
218+
int sock = atoi(sock_num.c_str());
219+
220+
CClientWrapper the_client = getClient(sock);
221+
222+
if (the_client.client == nullptr) {
223+
return chAT::CommandStatus::ERROR;
224+
}
225+
226+
auto &hostname = parser.args[1];
227+
if (hostname.empty()) {
228+
return chAT::CommandStatus::ERROR;
229+
}
230+
231+
auto &hostport = parser.args[2];
232+
if (hostport.empty()) {
233+
return chAT::CommandStatus::ERROR;
234+
}
235+
236+
int timeout = WIFI_CLIENT_DEF_CONN_TIMEOUT_MS;
237+
if (parser.args.size() > 3) {
238+
auto &tmp = parser.args[3];
239+
if (tmp.empty()) {
240+
return chAT::CommandStatus::ERROR;
241+
}
242+
int t = atoi(tmp.c_str());
243+
if (t > 0) {
244+
timeout = t;
245+
}
246+
}
247+
248+
if (!the_client.client->connect(hostname.c_str(), atoi(hostport.c_str()), timeout)) {
249+
return chAT::CommandStatus::ERROR;
250+
}
251+
srv.write_response_prompt();
252+
srv.write_line_end();
253+
return chAT::CommandStatus::OK;
254+
}
255+
default:
256+
return chAT::CommandStatus::ERROR;
257+
}
258+
};
259+
200260
/* ....................................................................... */
201261
command_table[_CLIENTSEND] = [this](auto & srv, auto & parser) {
202262
/* ....................................................................... */

UNOR4USBBridge/commands.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ enum {
4949
#define _CLIENTSTATE "+CLIENTSTATE"
5050
#define _CLIENTCONNECTIP "+CLIENTCONNECTIP"
5151
#define _CLIENTCONNECTNAME "+CLIENTCONNECTNAME"
52+
#define _CLIENTCONNECT "+CLIENTCONNECT"
5253
#define _CLIENTSEND "+CLIENTSEND"
5354
#define _CLIENTRECEIVE "+CLIENTRECEIVE"
5455
#define _CLIENTCLOSE "+CLIENTCLOSE"
@@ -59,6 +60,7 @@ enum {
5960
#define _SETCAROOT "+SETCAROOT"
6061
#define _SSLCLIENTSTATE "+SSLCLIENTSTATE"
6162
#define _SSLCLIENTCONNECTNAME "+SSLCLIENTCONNECTNAME"
63+
#define _SSLCLIENTCONNECT "+SSLCLIENTCONNECT"
6264
#define _SETIP "+SETIP"
6365
#define _GETHOSTBYNAME "+HOSTBYNAME"
6466
#define _AVAILABLE "+AVAILABLE"

0 commit comments

Comments
 (0)