Skip to content

Commit 247f168

Browse files
committed
feat(modem): Add support for pausing netif
Closes #699
1 parent 32387f7 commit 247f168

File tree

7 files changed

+80
-9
lines changed

7 files changed

+80
-9
lines changed

components/esp_modem/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,11 @@ menu "esp-modem"
8585
mode more robust for some devices (e.g. Quectel), but might cause
8686
trouble for other devices (e.g. SIMCOM).
8787

88+
config ESP_MODEM_ADD_DEBUG_LOGS
89+
bool "Add UART Tx/Rx logs"
90+
default n
91+
help
92+
If enabled, the library dumps all transmitted and received data.
93+
This option is only used for debugging.
94+
8895
endmenu

components/esp_modem/examples/modem_console/main/modem_console_main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,17 @@ extern "C" void app_main(void)
385385
return 0;
386386
});
387387
#endif
388+
const ConsoleCommand PauseNetwork("pause_net", "toggle network pause", no_args, [&](ConsoleCommand * c) {
389+
static int cnt = 0;
390+
if (++cnt % 2) {
391+
ESP_LOGI(TAG, "Pausing netif");
392+
dce->pause_netif(true);
393+
} else {
394+
ESP_LOGI(TAG, "Unpausing netif");
395+
dce->pause_netif(false);
396+
}
397+
return 0;
398+
});
388399

389400
const struct SetApn {
390401
SetApn(): apn(STR1, nullptr, nullptr, "<apn>", "APN (Access Point Name)") {}

components/esp_modem/include/cxx_include/esp_modem_dce.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,28 @@ class DCE_T {
103103
}
104104
#endif
105105

106+
/**
107+
* @brief Pauses/Unpauses network temporarily
108+
* @param do_pause true to pause, false to unpause
109+
* @param force true to ignore command failures and continue
110+
* @return command_result of the underlying commands
111+
*/
112+
command_result pause_netif(bool do_pause, bool force = false)
113+
{
114+
command_result result;
115+
if (do_pause) {
116+
netif.pause();
117+
dte->set_command_callbacks();
118+
result = device->set_command_mode();
119+
} else {
120+
result = device->resume_data_mode();
121+
if (result == command_result::OK || force) {
122+
netif.resume();
123+
}
124+
}
125+
return result;
126+
}
127+
106128
protected:
107129
std::shared_ptr<DTE> dte;
108130
std::shared_ptr<SpecificModule> device;

components/esp_modem/include/cxx_include/esp_modem_dte.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ class DTE : public CommandableIf {
145145
*/
146146
bool recover();
147147

148+
/**
149+
* @brief Set internal command callbacks to the underlying terminal.
150+
* Here we capture command replies to be processed by supplied command callbacks in struct command_cb.
151+
*/
152+
void set_command_callbacks();
153+
148154
protected:
149155
/**
150156
* @brief Allows for locking the DTE
@@ -204,12 +210,6 @@ class DTE : public CommandableIf {
204210
} inflatable;
205211
#endif // CONFIG_ESP_MODEM_USE_INFLATABLE_BUFFER_IF_NEEDED
206212

207-
/**
208-
* @brief Set internal command callbacks to the underlying terminal.
209-
* Here we capture command replies to be processed by supplied command callbacks in struct command_cb.
210-
*/
211-
void set_command_callbacks();
212-
213213
/**
214214
* @brief This abstracts command callback processing and implements its locking, signaling of completion and timeouts.
215215
*/

components/esp_modem/include/cxx_include/esp_modem_netif.hpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -54,6 +54,16 @@ class Netif {
5454
*/
5555
void stop();
5656

57+
/**
58+
* @brief Pause the network interface
59+
*/
60+
void pause();
61+
62+
/**
63+
* @brief Resume the network interface
64+
*/
65+
void resume();
66+
5767
void receive(uint8_t *data, size_t len);
5868

5969
private:

components/esp_modem/src/esp_modem_netif.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ void Netif::stop()
9999
signal.clear(PPP_STARTED);
100100
}
101101

102+
void Netif::resume()
103+
{
104+
ppp_dte->set_read_cb([this](uint8_t *data, size_t len) -> bool {
105+
receive(data, len);
106+
return true;
107+
});
108+
signal.set(PPP_STARTED);
109+
}
110+
111+
void Netif::pause()
112+
{
113+
signal.clear(PPP_STARTED);
114+
}
115+
102116
Netif::~Netif()
103117
{
104118
if (signal.is_any(PPP_STARTED)) {

components/esp_modem/src/esp_modem_uart.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -176,13 +176,20 @@ int UartTerminal::read(uint8_t *data, size_t len)
176176
uart_get_buffered_data_len(uart.port, &length);
177177
length = std::min(len, length);
178178
if (length > 0) {
179-
return uart_read_bytes(uart.port, data, length, portMAX_DELAY);
179+
int read_len = uart_read_bytes(uart.port, data, length, portMAX_DELAY);
180+
#if CONFIG_ESP_MODEM_ADD_DEBUG_LOGS
181+
ESP_LOG_BUFFER_HEXDUMP("uart-rx", data, read_len, ESP_LOG_DEBUG);
182+
#endif
183+
return read_len;
180184
}
181185
return 0;
182186
}
183187

184188
int UartTerminal::write(uint8_t *data, size_t len)
185189
{
190+
#if CONFIG_ESP_MODEM_ADD_DEBUG_LOGS
191+
ESP_LOG_BUFFER_HEXDUMP("uart-tx", data, len, ESP_LOG_DEBUG);
192+
#endif
186193
return uart_write_bytes_compat(uart.port, data, len);
187194
}
188195

0 commit comments

Comments
 (0)