Skip to content

Commit 4e901eb

Browse files
committed
fix(modem): Use nicer way to override modules
1 parent 26b75f9 commit 4e901eb

File tree

10 files changed

+84
-30
lines changed

10 files changed

+84
-30
lines changed

components/esp_modem/examples/modem_tcp_client/main/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
set(module_dir "generic_module")
12
if (CONFIG_EXAMPLE_MODEM_DEVICE_BG96)
23
set(device_srcs sock_commands_bg96.cpp)
34
elseif(CONFIG_EXAMPLE_MODEM_DEVICE_SIM7600)
45
set(device_srcs sock_commands_sim7600.cpp)
56
elseif(CONFIG_EXAMPLE_MODEM_DEVICE_ESPAT)
67
set(device_srcs sock_commands_espat.cpp)
8+
set(module_dir "espat_module")
79
endif()
810

911
if(CONFIG_ESP_MODEM_ENABLE_DEVELOPMENT_MODE)
@@ -16,4 +18,4 @@ idf_component_register(SRCS "modem_client.cpp"
1618
"${command_dir}/sock_dce.cpp"
1719
"tcp_transport_at.cpp"
1820
"${device_srcs}"
19-
INCLUDE_DIRS "." "${command_dir}")
21+
INCLUDE_DIRS "." "${command_dir}" "${module_dir}")

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#include <sys/socket.h>
99
#include "esp_vfs.h"
1010
#include "esp_vfs_eventfd.h"
11+
1112
#include "sock_dce.hpp"
12-
#include "cxx_include/esp_modem_command_library_utils.hpp"
1313

1414
namespace sock_dce {
1515

@@ -305,21 +305,6 @@ std::unique_ptr<DCE> create(const esp_modem::dce_config *config, std::shared_ptr
305305
}
306306

307307

308-
esp_modem::command_result DCE::sync()
309-
{
310-
return esp_modem::dce_commands::generic_command_common(dte.get(), "AT\r\n");
311-
}
312-
313-
esp_modem::command_result DCE::set_echo(bool on)
314-
{
315-
return esp_modem::dce_commands::generic_command_common(dte.get(), "ATE0\r\n");
316-
}
317-
318-
esp_modem::command_result DCE::set_pdp_context(esp_modem::PdpContext &pdp)
319-
{
320-
return esp_modem::command_result::OK;
321-
}
322-
323308
/**
324309
* @brief Opens network in AT command mode
325310
* @return OK, FAIL or TIMEOUT

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "cxx_include/esp_modem_api.hpp"
99
#include <cxx_include/esp_modem_dce_factory.hpp>
1010
#include "sock_commands.hpp"
11+
#include "sock_module.hpp"
1112
#include <sys/socket.h>
1213

1314
#pragma once
@@ -97,13 +98,10 @@ class Responder {
9798
std::shared_ptr<esp_modem::DTE> &dte;
9899
};
99100

100-
class DCE : public ::esp_modem::GenericModule {
101-
using esp_modem::GenericModule::GenericModule;
101+
class DCE : public Module {
102+
using Module::Module;
102103
public:
103104

104-
esp_modem::command_result sync() override;
105-
esp_modem::command_result set_echo(bool on) override;
106-
esp_modem::command_result set_pdp_context(esp_modem::PdpContext &pdp) override;
107105
/**
108106
* @brief Opens network in AT command mode
109107
* @return OK, FAIL or TIMEOUT
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "esp_modem_config.h"
8+
#include "cxx_include/esp_modem_api.hpp"
9+
10+
#pragma once
11+
12+
namespace sock_dce {
13+
14+
class Module: public esp_modem::GenericModule {
15+
using esp_modem::GenericModule::GenericModule;
16+
public:
17+
18+
esp_modem::command_result sync() override;
19+
esp_modem::command_result set_echo(bool on) override;
20+
esp_modem::command_result set_pdp_context(esp_modem::PdpContext &pdp) override;
21+
22+
};
23+
24+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "cxx_include/esp_modem_api.hpp"
99
#include <cxx_include/esp_modem_dce_factory.hpp>
1010
#include "sock_commands.hpp"
11+
#include "sock_module.hpp"
1112
#include <sys/socket.h>
1213

1314
#pragma once
@@ -97,8 +98,8 @@ class Responder {
9798
std::shared_ptr<esp_modem::DTE> &dte;
9899
};
99100

100-
class DCE : public ::esp_modem::GenericModule {
101-
using esp_modem::GenericModule::GenericModule;
101+
class DCE : public Module {
102+
using Module::Module;
102103
public:
103104

104105
// --- ESP-MODEM command module starts here ---
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "esp_modem_config.h"
8+
#include "cxx_include/esp_modem_api.hpp"
9+
10+
#pragma once
11+
12+
namespace sock_dce {
13+
14+
using Module = esp_modem::GenericModule;
15+
16+
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,14 @@ extern "C" void app_main(void)
124124
mqtt_config.network.transport = ssl;
125125
#endif
126126
esp_mqtt_client_handle_t mqtt_client = esp_mqtt_client_init(&mqtt_config);
127-
esp_mqtt_client_register_event(mqtt_client, static_cast<esp_mqtt_event_id_t>(ESP_EVENT_ANY_ID), mqtt_event_handler, NULL);
127+
esp_mqtt_client_register_event(mqtt_client, static_cast<esp_mqtt_event_id_t>(ESP_EVENT_ANY_ID), mqtt_event_handler, nullptr);
128128
esp_mqtt_client_start(mqtt_client);
129129
#ifndef CONFIG_EXAMPLE_CUSTOM_TCP_TRANSPORT
130-
dce->set_rx_mode(1);
131130
if (!dce->connect(BROKER_URL, BROKER_PORT)) {
132131
ESP_LOGE(TAG, "Failed to start DCE");
133132
return;
134133
}
135-
while (1) {
134+
while (true) {
136135
while (dce->perform_sock()) {
137136
ESP_LOGV(TAG, "...performing");
138137
}

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,31 @@
99
#include "sock_commands.hpp"
1010
#include "cxx_include/esp_modem_command_library_utils.hpp"
1111
#include "sock_dce.hpp"
12+
#include "sock_module.hpp"
1213

1314
static const char *TAG = "sock_commands_espat";
1415

16+
namespace sock_dce {
17+
18+
using namespace esp_modem;
19+
20+
command_result Module::sync()
21+
{
22+
return dce_commands::generic_command_common(dte.get(), "AT\r\n");
23+
}
24+
25+
command_result Module::set_echo(bool on)
26+
{
27+
return dce_commands::generic_command_common(dte.get(), "ATE0\r\n");
28+
}
29+
30+
command_result Module::set_pdp_context(PdpContext &pdp)
31+
{
32+
return command_result::OK;
33+
}
34+
35+
}
36+
1537
namespace sock_commands {
1638

1739
using namespace esp_modem;
@@ -34,8 +56,14 @@ command_result net_open(CommandableIf *t)
3456
ESP_LOGE(TAG, "Failed to connect to WiFi");
3557
return ret;
3658
}
37-
3859
ESP_LOGI(TAG, "WiFi connected successfully");
60+
61+
// Set passive receive mode (1) for better control
62+
ret = set_rx_mode(t, 1);
63+
if (ret != command_result::OK) {
64+
ESP_LOGE(TAG, "Failed to set preferred Rx mode");
65+
return ret;
66+
}
3967
return command_result::OK;
4068
}
4169

@@ -123,8 +151,7 @@ command_result get_ip(CommandableIf *t, std::string &ip)
123151
command_result set_rx_mode(CommandableIf *t, int mode)
124152
{
125153
ESP_LOGE(TAG, "%s", __func__);
126-
// Set passive receive mode (1) for better control
127-
// Active mode (0) would send +IPD automatically
154+
// Active mode (0) sends data automatically, Passive mode (1) notifies about data for reading
128155
std::string cmd = "AT+CIPRECVTYPE=" + std::to_string(mode) + "\r\n";
129156
return dce_commands::generic_command(t, cmd, "OK", "ERROR", 1000);
130157
}

components/esp_modem/examples/modem_tcp_client/sdkconfig.ci.default

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CONFIG_IDF_TARGET="esp32"
2+
CONFIG_EXAMPLE_MODEM_DEVICE_ESPAT=y

0 commit comments

Comments
 (0)