@@ -58,6 +58,14 @@ command_result net_open(CommandableIf *t)
5858 }
5959 ESP_LOGI (TAG, " WiFi connected successfully" );
6060
61+ // Enable multiple connections mode
62+ ret = dce_commands::generic_command (t, " AT+CIPMUX=1\r\n " , " OK" , " ERROR" , 1000 );
63+ if (ret != command_result::OK) {
64+ ESP_LOGE (TAG, " Failed to enable multiple connections mode" );
65+ return ret;
66+ }
67+ ESP_LOGI (TAG, " Multiple connections mode enabled" );
68+
6169 // Set passive receive mode (1) for better control
6270 ret = set_rx_mode (t, 1 );
6371 if (ret != command_result::OK) {
@@ -82,28 +90,36 @@ command_result tcp_open(CommandableIf *t, const std::string &host, int port, int
8290{
8391 ESP_LOGV (TAG, " %s" , __func__);
8492
85- // Set single connection mode (just in case)
86- auto ret = dce_commands::generic_command (t, " AT+CIPMUX=0\r\n " , " OK" , " ERROR" , 1000 );
87- if (ret != command_result::OK) {
88- ESP_LOGW (TAG, " Failed to set single connection mode" );
89- }
93+ // Use link ID 0 for now (can be extended to support multiple concurrent connections)
94+ const int link_id = 0 ;
9095
91- // Establish TCP connection
92- std::string tcp_cmd = " AT+CIPSTART=\" TCP\" ,\" " + host + " \" ," + std::to_string (port) + " \r\n " ;
93- ret = dce_commands::generic_command (t, tcp_cmd, " CONNECT" , " ERROR" , timeout);
96+ // Establish TCP connection with link ID for multiple connections mode
97+ std::string tcp_cmd = " AT+CIPSTART=" + std::to_string (link_id) + " ,\" TCP\" ,\" " + host + " \" ," + std::to_string (port) + " \r\n " ;
98+
99+ // In multiple connections mode, response format is: <link ID>,CONNECT
100+ std::string expected_response = std::to_string (link_id) + " ,CONNECT" ;
101+
102+ auto ret = dce_commands::generic_command (t, tcp_cmd, expected_response, " ERROR" , timeout);
94103 if (ret != command_result::OK) {
95- ESP_LOGE (TAG, " Failed to establish TCP connection to %s:%d" , host.c_str (), port);
104+ ESP_LOGE (TAG, " Failed to establish TCP connection to %s:%d on link %d " , host.c_str (), port, link_id );
96105 return ret;
97106 }
98107
99- ESP_LOGI (TAG, " TCP connection established to %s:%d" , host.c_str (), port);
108+ ESP_LOGI (TAG, " TCP connection established to %s:%d on link %d " , host.c_str (), port, link_id );
100109 return command_result::OK;
101110}
102111
103112command_result tcp_close (CommandableIf *t)
104113{
105114 ESP_LOGV (TAG, " %s" , __func__);
106- return dce_commands::generic_command (t, " AT+CIPCLOSE\r\n " , " CLOSED" , " ERROR" , 5000 );
115+ // Use link ID 0 for closing connection
116+ const int link_id = 0 ;
117+ std::string close_cmd = " AT+CIPCLOSE=" + std::to_string (link_id) + " \r\n " ;
118+
119+ // In multiple connections mode, response format is: <link ID>,CLOSED
120+ std::string expected_response = std::to_string (link_id) + " ,CLOSED" ;
121+
122+ return dce_commands::generic_command (t, close_cmd, expected_response, " ERROR" , 5000 );
107123}
108124
109125command_result tcp_send (CommandableIf *t, uint8_t *data, size_t len)
@@ -150,9 +166,11 @@ command_result get_ip(CommandableIf *t, std::string &ip)
150166
151167command_result set_rx_mode (CommandableIf *t, int mode)
152168{
153- ESP_LOGE (TAG, " %s" , __func__);
169+ ESP_LOGV (TAG, " %s" , __func__);
170+ // For multiple connections mode, set receive mode for link ID 0
171+ const int link_id = 0 ;
154172 // Active mode (0) sends data automatically, Passive mode (1) notifies about data for reading
155- std::string cmd = " AT+CIPRECVTYPE=" + std::to_string (mode) + " \r\n " ;
173+ std::string cmd = " AT+CIPRECVTYPE=" + std::to_string (link_id) + " , " + std::to_string ( mode) + " \r\n " ;
156174 return dce_commands::generic_command (t, cmd, " OK" , " ERROR" , 1000 );
157175}
158176
@@ -164,17 +182,23 @@ void Responder::start_sending(size_t len)
164182{
165183 data_to_send = len;
166184 send_stat = 0 ;
167- send_cmd (" AT+CIPSEND=" + std::to_string (len) + " \r\n " );
185+ // For multiple connections mode, include link ID
186+ const int link_id = 0 ;
187+ send_cmd (" AT+CIPSEND=" + std::to_string (link_id) + " ," + std::to_string (len) + " \r\n " );
168188}
169189
170190void Responder::start_receiving (size_t len)
171191{
172- send_cmd (" AT+CIPRECVDATA=" + std::to_string (len) + " \r\n " );
192+ // For multiple connections mode, include link ID
193+ const int link_id = 0 ;
194+ send_cmd (" AT+CIPRECVDATA=" + std::to_string (link_id) + " ," + std::to_string (len) + " \r\n " );
173195}
174196
175197bool Responder::start_connecting (std::string host, int port)
176198{
177- std::string cmd = " AT+CIPSTART=\" TCP\" ,\" " + host + " \" ," + std::to_string (port) + " \r\n " ;
199+ // For multiple connections mode, include link ID
200+ const int link_id = 0 ;
201+ std::string cmd = " AT+CIPSTART=" + std::to_string (link_id) + " ,\" TCP\" ,\" " + host + " \" ," + std::to_string (port) + " \r\n " ;
178202 send_cmd (cmd);
179203 return true ;
180204}
@@ -299,7 +323,8 @@ Responder::ret Responder::send(std::string_view response)
299323
300324Responder::ret Responder::connect (std::string_view response)
301325{
302- if (response.find (" CONNECT" ) != std::string::npos) {
326+ // In multiple connections mode, response format is: <link ID>,CONNECT
327+ if (response.find (" ,CONNECT" ) != std::string::npos || response.find (" CONNECT" ) != std::string::npos) {
303328 ESP_LOGI (TAG, " TCP connected!" );
304329 return ret::OK;
305330 }
@@ -321,15 +346,15 @@ Responder::ret Responder::check_async_replies(status state, std::string_view &re
321346 ESP_LOGW (TAG, " WiFi disconnected" );
322347 }
323348
324- // Handle TCP status messages
349+ // Handle TCP status messages (multiple connections format: <link ID>,CONNECT or <link ID>,CLOSED)
325350 if (response.find (" CONNECT" ) != std::string::npos && state == status::CONNECTING) {
326351 return connect (response);
327352 } else if (response.find (" CLOSED" ) != std::string::npos) {
328353 ESP_LOGW (TAG, " TCP connection closed" );
329354 return ret::FAIL;
330355 }
331356
332- // Handle data notifications in active mode (if we switch to it later)
357+ // Handle data notifications - in multiple connections mode, format is +IPD,<link ID>,<len>
333358 if (response.find (" +IPD," ) != std::string::npos) {
334359 uint64_t data_ready = 1 ;
335360 write (data_ready_fd, &data_ready, sizeof (data_ready));
0 commit comments