Skip to content

Commit 1cf0041

Browse files
committed
fix(modem): Use link-id from responder class
1 parent d2feaf3 commit 1cf0041

File tree

3 files changed

+62
-45
lines changed

3 files changed

+62
-45
lines changed

components/esp_modem/examples/modem_tcp_client/main/generate/sock_dce.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@ namespace sock_dce {
1515

1616
constexpr auto const *TAG = "sock_dce";
1717

18+
// Definition of the static member variables
19+
std::vector<DCE*> DCE::dce_list{};
20+
bool DCE::network_init = false;
21+
int Responder::s_link_id = 0;
22+
23+
// Constructor - add this DCE instance to the static list
24+
DCE::DCE(std::shared_ptr<esp_modem::DTE> dte_arg, const esp_modem_dce_config *config)
25+
: Module(std::move(dte_arg), config)
26+
{
27+
dce_list.push_back(this);
28+
}
29+
30+
// Destructor - remove this DCE instance from the static list
31+
DCE::~DCE()
32+
{
33+
auto it = std::find(dce_list.begin(), dce_list.end(), this);
34+
if (it != dce_list.end()) {
35+
dce_list.erase(it);
36+
}
37+
}
38+
1839

1940
bool DCE::perform_sock()
2041
{
@@ -224,10 +245,12 @@ void DCE::start_listening(int port)
224245

225246
bool DCE::connect(std::string host, int port)
226247
{
248+
data_ready_fd = eventfd(0, EFD_SUPPORT_ISR);
249+
assert(data_ready_fd > 0);
227250
dte->on_read(nullptr);
228251
tcp_close();
229252
dte->on_read([this](uint8_t *data, size_t len) {
230-
this->perform_at(data, len);
253+
read_callback(data, len);
231254
return esp_modem::command_result::TIMEOUT;
232255
});
233256
if (!at.start_connecting(host, port)) {
@@ -241,12 +264,13 @@ bool DCE::connect(std::string host, int port)
241264

242265
bool DCE::init()
243266
{
267+
if (network_init) {
268+
return true;
269+
}
270+
network_init = true;
244271
esp_vfs_eventfd_config_t config = ESP_VFS_EVENTD_CONFIG_DEFAULT();
245272
esp_vfs_eventfd_register(&config);
246273

247-
data_ready_fd = eventfd(0, EFD_SUPPORT_ISR);
248-
assert(data_ready_fd > 0);
249-
250274
dte->on_read(nullptr);
251275
const int retries = 5;
252276
int i = 0;

components/esp_modem/examples/modem_tcp_client/main/generate/sock_dce.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ class Responder {
6363
return total_len;
6464
}
6565

66+
int link_id{s_link_id++};
6667
private:
68+
static int s_link_id;
6769
static constexpr size_t buffer_size = 512;
6870

6971
bool on_read(char *data, size_t len)
@@ -99,8 +101,9 @@ class Responder {
99101
};
100102

101103
class DCE : public Module {
102-
using Module::Module;
103104
public:
105+
DCE(std::shared_ptr<esp_modem::DTE> dte_arg, const esp_modem_dce_config *config);
106+
~DCE();
104107

105108
// --- ESP-MODEM command module starts here ---
106109
#include "esp_modem_command_declare_helper.inc"
@@ -204,6 +207,14 @@ esp_modem::return_type name(ESP_MODEM_COMMAND_PARAMS(__VA_ARGS__));
204207
}
205208
return -1;
206209
}
210+
static std::vector<DCE*> dce_list;
211+
static bool network_init;
212+
static void read_callback(uint8_t *data, size_t len)
213+
{
214+
for (auto dce : dce_list) {
215+
dce->perform_at(data, len);
216+
}
217+
}
207218

208219
private:
209220
esp_modem::SignalGroup signal;

components/esp_modem/examples/modem_tcp_client/main/sock_commands_espat.cpp

Lines changed: 22 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,28 @@ command_result net_close(CommandableIf *t)
8686
return command_result::OK;
8787
}
8888

89-
command_result tcp_open(CommandableIf *t, const std::string &host, int port, int timeout)
90-
{
91-
ESP_LOGV(TAG, "%s", __func__);
92-
93-
// Use link ID 0 for now (can be extended to support multiple concurrent connections)
94-
const int link_id = 0;
95-
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);
103-
if (ret != command_result::OK) {
104-
ESP_LOGE(TAG, "Failed to establish TCP connection to %s:%d on link %d", host.c_str(), port, link_id);
105-
return ret;
106-
}
107-
108-
ESP_LOGI(TAG, "TCP connection established to %s:%d on link %d", host.c_str(), port, link_id);
109-
return command_result::OK;
110-
}
89+
// command_result tcp_open(CommandableIf *t, const std::string &host, int port, int timeout)
90+
// {
91+
// ESP_LOGV(TAG, "%s", __func__);
92+
//
93+
// // Use link ID 0 for now (can be extended to support multiple concurrent connections)
94+
// const int link_id = 0;
95+
//
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);
103+
// if (ret != command_result::OK) {
104+
// ESP_LOGE(TAG, "Failed to establish TCP connection to %s:%d on link %d", host.c_str(), port, link_id);
105+
// return ret;
106+
// }
107+
//
108+
// ESP_LOGI(TAG, "TCP connection established to %s:%d on link %d", host.c_str(), port, link_id);
109+
// return command_result::OK;
110+
// }
111111

112112
command_result tcp_close(CommandableIf *t)
113113
{
@@ -122,21 +122,6 @@ command_result tcp_close(CommandableIf *t)
122122
return dce_commands::generic_command(t, close_cmd, expected_response, "ERROR", 5000);
123123
}
124124

125-
command_result tcp_send(CommandableIf *t, uint8_t *data, size_t len)
126-
{
127-
ESP_LOGV(TAG, "%s", __func__);
128-
// This function is not used in the current implementation
129-
// Data sending is handled by the DCE responder
130-
return command_result::FAIL;
131-
}
132-
133-
command_result tcp_recv(CommandableIf *t, uint8_t *data, size_t len, size_t &out_len)
134-
{
135-
ESP_LOGV(TAG, "%s", __func__);
136-
// This function is not used in the current implementation
137-
// Data receiving is handled by the DCE responder
138-
return command_result::FAIL;
139-
}
140125

141126
command_result get_ip(CommandableIf *t, std::string &ip)
142127
{
@@ -183,21 +168,18 @@ void Responder::start_sending(size_t len)
183168
data_to_send = len;
184169
send_stat = 0;
185170
// For multiple connections mode, include link ID
186-
const int link_id = 0;
187171
send_cmd("AT+CIPSEND=" + std::to_string(link_id) + "," + std::to_string(len) + "\r\n");
188172
}
189173

190174
void Responder::start_receiving(size_t len)
191175
{
192176
// For multiple connections mode, include link ID
193-
const int link_id = 0;
194177
send_cmd("AT+CIPRECVDATA=" + std::to_string(link_id) + "," + std::to_string(len) + "\r\n");
195178
}
196179

197180
bool Responder::start_connecting(std::string host, int port)
198181
{
199182
// For multiple connections mode, include link ID
200-
const int link_id = 0;
201183
std::string cmd = "AT+CIPSTART=" + std::to_string(link_id) + ",\"TCP\",\"" + host + "\"," + std::to_string(port) + "\r\n";
202184
send_cmd(cmd);
203185
return true;

0 commit comments

Comments
 (0)