@@ -58,6 +58,14 @@ command_result net_open(CommandableIf *t)
58
58
}
59
59
ESP_LOGI (TAG, " WiFi connected successfully" );
60
60
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
+
61
69
// Set passive receive mode (1) for better control
62
70
ret = set_rx_mode (t, 1 );
63
71
if (ret != command_result::OK) {
@@ -82,28 +90,36 @@ command_result tcp_open(CommandableIf *t, const std::string &host, int port, int
82
90
{
83
91
ESP_LOGV (TAG, " %s" , __func__);
84
92
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 ;
90
95
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);
94
103
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 );
96
105
return ret;
97
106
}
98
107
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 );
100
109
return command_result::OK;
101
110
}
102
111
103
112
command_result tcp_close (CommandableIf *t)
104
113
{
105
114
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 );
107
123
}
108
124
109
125
command_result tcp_send (CommandableIf *t, uint8_t *data, size_t len)
@@ -150,9 +166,11 @@ command_result get_ip(CommandableIf *t, std::string &ip)
150
166
151
167
command_result set_rx_mode (CommandableIf *t, int mode)
152
168
{
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 ;
154
172
// 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 " ;
156
174
return dce_commands::generic_command (t, cmd, " OK" , " ERROR" , 1000 );
157
175
}
158
176
@@ -164,17 +182,23 @@ void Responder::start_sending(size_t len)
164
182
{
165
183
data_to_send = len;
166
184
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 " );
168
188
}
169
189
170
190
void Responder::start_receiving (size_t len)
171
191
{
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 " );
173
195
}
174
196
175
197
bool Responder::start_connecting (std::string host, int port)
176
198
{
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 " ;
178
202
send_cmd (cmd);
179
203
return true ;
180
204
}
@@ -299,7 +323,8 @@ Responder::ret Responder::send(std::string_view response)
299
323
300
324
Responder::ret Responder::connect (std::string_view response)
301
325
{
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) {
303
328
ESP_LOGI (TAG, " TCP connected!" );
304
329
return ret::OK;
305
330
}
@@ -321,15 +346,15 @@ Responder::ret Responder::check_async_replies(status state, std::string_view &re
321
346
ESP_LOGW (TAG, " WiFi disconnected" );
322
347
}
323
348
324
- // Handle TCP status messages
349
+ // Handle TCP status messages (multiple connections format: <link ID>,CONNECT or <link ID>,CLOSED)
325
350
if (response.find (" CONNECT" ) != std::string::npos && state == status::CONNECTING) {
326
351
return connect (response);
327
352
} else if (response.find (" CLOSED" ) != std::string::npos) {
328
353
ESP_LOGW (TAG, " TCP connection closed" );
329
354
return ret::FAIL;
330
355
}
331
356
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>
333
358
if (response.find (" +IPD," ) != std::string::npos) {
334
359
uint64_t data_ready = 1 ;
335
360
write (data_ready_fd, &data_ready, sizeof (data_ready));
0 commit comments