Skip to content

Commit cf7ca4f

Browse files
committed
fix(modem): Make tcp-client example support multiple connections
1 parent 4e901eb commit cf7ca4f

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <charconv>
88
#include <sys/socket.h>
9+
#include <algorithm> // for std::find
910
#include "esp_vfs.h"
1011
#include "esp_vfs_eventfd.h"
1112

@@ -15,6 +16,25 @@ namespace sock_dce {
1516

1617
constexpr auto const *TAG = "sock_dce";
1718

19+
// Definition of the static member variables
20+
std::vector<DCE*> DCE::dce_list{};
21+
bool DCE::network_init = false;
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+
}
1838

1939
bool DCE::perform_sock()
2040
{
@@ -104,6 +124,7 @@ void DCE::close_sock()
104124
close(sock);
105125
sock = -1;
106126
}
127+
close(data_ready_fd);
107128
dte->on_read(nullptr);
108129
const int retries = 5;
109130
int i = 0;
@@ -224,15 +245,18 @@ 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)) {
234257
ESP_LOGE(TAG, "Unable to start connecting");
235258
dte->on_read(nullptr);
259+
close(data_ready_fd);
236260
return false;
237261
}
238262
state = status::CONNECTING;
@@ -241,12 +265,13 @@ bool DCE::connect(std::string host, int port)
241265

242266
bool DCE::init()
243267
{
268+
if (network_init) {
269+
return true;
270+
}
271+
network_init = true;
244272
esp_vfs_eventfd_config_t config = ESP_VFS_EVENTD_CONFIG_DEFAULT();
245273
esp_vfs_eventfd_register(&config);
246274

247-
data_ready_fd = eventfd(0, EFD_SUPPORT_ISR);
248-
assert(data_ready_fd > 0);
249-
250275
dte->on_read(nullptr);
251276
const int retries = 5;
252277
int i = 0;

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,14 @@ class Responder {
9999
};
100100

101101
class DCE : public Module {
102-
using Module::Module;
103102
public:
103+
// Constructor and destructor for managing dce_list
104+
// explicit GenericModule(std::shared_ptr<DTE> dte, std::unique_ptr<PdpContext> pdp):
105+
// dte(std::move(dte)), pdp(std::move(pdp)) {}
106+
// explicit GenericModule(std::shared_ptr<DTE> dte, );
107+
//
108+
DCE(std::shared_ptr<esp_modem::DTE> dte_arg, const esp_modem_dce_config *config);
109+
~DCE();
104110

105111
/**
106112
* @brief Opens network in AT command mode
@@ -237,6 +243,15 @@ class DCE : public Module {
237243
int sock {-1};
238244
int listen_sock {-1};
239245
int data_ready_fd {-1};
246+
static std::vector<DCE*> dce_list;
247+
static bool network_init;
248+
static void read_callback(uint8_t *data, size_t len)
249+
{
250+
for (auto dce : dce_list) {
251+
dce->perform_at(data, len);
252+
}
253+
}
254+
240255
};
241256
std::unique_ptr<DCE> create(const esp_modem::dce_config *config, std::shared_ptr<esp_modem::DTE> dte);
242257
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_
7373
}
7474
}
7575

76+
// sock_dce::DCE::dce_list{};
7677

7778
extern "C" void app_main(void)
7879
{
@@ -104,7 +105,7 @@ extern "C" void app_main(void)
104105
esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(CONFIG_EXAMPLE_MODEM_APN);
105106

106107
/* create the DCE and initialize network manually (using AT commands) */
107-
auto dce = sock_dce::create(&dce_config, std::move(dte));
108+
auto dce = sock_dce::create(&dce_config, dte);
108109
if (!dce->init()) {
109110
ESP_LOGE(TAG, "Failed to setup network");
110111
return;

0 commit comments

Comments
 (0)