Skip to content

Commit f1c1f02

Browse files
authored
Merge pull request #22 from JAndrassy/at_server_accept
AT command SERVERACCEPT for library's WiFiServer::accept()
2 parents aefbe0f + 7adbfd8 commit f1c1f02

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

UNOR4USBBridge/at_handler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class CServerClient {
4949
public:
5050
WiFiClient client;
5151
int server = -1;
52+
bool accepted = false;
5253
};
5354

5455
class CAtHandler {

UNOR4USBBridge/cmds_wifi_netif.h

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ void CAtHandler::add_cmds_wifi_netif() {
403403

404404
for(int i = 0;i<MAX_CLIENT_AVAILABLE;i++) {
405405
if(serverClients[i].server == sock) {
406-
if(serverClients[i].client) {
406+
if(serverClients[i].client && !serverClients[i].accepted) {
407407
serverClients[i].client.write(data_received.data(), data_received.size());
408408
}
409409
}
@@ -491,6 +491,7 @@ void CAtHandler::add_cmds_wifi_netif() {
491491
if(!serverClients[i].client)
492492
break;
493493
serverClients[i].server = sock;
494+
serverClients[i].accepted = false;
494495
}
495496
}
496497

@@ -501,6 +502,7 @@ void CAtHandler::add_cmds_wifi_netif() {
501502
do {
502503
if(serverClients[last_server_client_sock].client
503504
&& serverClients[last_server_client_sock].server == sock
505+
&& !serverClients[last_server_client_sock].accepted
504506
&& serverClients[last_server_client_sock].client.available() > 0) {
505507
client_sock = last_server_client_sock;
506508
break;
@@ -532,6 +534,62 @@ void CAtHandler::add_cmds_wifi_netif() {
532534
}
533535
};
534536

537+
/* ....................................................................... */
538+
command_table[_SERVERACCEPT] = [this](auto & srv, auto & parser) {
539+
/* ....................................................................... */
540+
switch (parser.cmd_mode) {
541+
case chAT::CommandMode::Write: {
542+
if (parser.args.size() != 1) {
543+
return chAT::CommandStatus::ERROR;
544+
}
545+
546+
auto &socket = parser.args[0];
547+
if (socket.empty()) {
548+
return chAT::CommandStatus::ERROR;
549+
}
550+
int sock = atoi(socket.c_str());
551+
552+
if(sock < 0 || sock >= MAX_SERVER_AVAILABLE) {
553+
return chAT::CommandStatus::ERROR;
554+
}
555+
556+
if(serverWiFi[sock] == nullptr) {
557+
return chAT::CommandStatus::ERROR;
558+
}
559+
560+
int client_sock = -1;
561+
562+
/* accept */
563+
for(int i = 0;i<MAX_CLIENT_AVAILABLE;i++) {
564+
if(!serverClients[i].client) {
565+
serverClients[i].client = serverWiFi[sock]->available();
566+
if (serverClients[i].client) {
567+
serverClients[i].server = sock;
568+
serverClients[i].accepted = true;
569+
client_sock = i;
570+
}
571+
break;
572+
}
573+
}
574+
575+
int sock_to_send = -1;
576+
if(client_sock != -1) {
577+
sock_to_send = START_CLIENT_SERVER_SOCK + client_sock;
578+
}
579+
580+
srv.write_response_prompt();
581+
srv.write_str((const char *) String(sock_to_send).c_str());
582+
srv.write_line_end();
583+
return chAT::CommandStatus::OK;
584+
}
585+
case chAT::CommandMode::Run: {
586+
return chAT::CommandStatus::ERROR;
587+
}
588+
default:
589+
return chAT::CommandStatus::ERROR;
590+
}
591+
};
592+
535593
/* ....................................................................... */
536594
command_table[_SERVEREND] = [this](auto & srv, auto & parser) {
537595
/* ....................................................................... */
@@ -870,4 +928,4 @@ void CAtHandler::add_cmds_wifi_netif() {
870928
};
871929
}
872930

873-
#endif
931+
#endif

UNOR4USBBridge/commands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ enum {
8181
#define _SSLREMOTEPORT "+SSLREMOTEPORT"
8282
#define _SSLPEEK "+SSLPEEK"
8383
#define _SERVERAVAILABLE "+SERVERAVAILABLE"
84+
#define _SERVERACCEPT "+SERVERACCEPT"
8485
#define _SERVEREND "+SERVEREND"
8586

8687
#define _UDPBEGIN "+UDPBEGIN"

0 commit comments

Comments
 (0)