feat(modem): Add support for pausing netif

Closes https://github.com/espressif/esp-protocols/issues/699
This commit is contained in:
David Cermak
2024-11-21 08:11:15 +01:00
parent 32387f7e39
commit 247f1681e8
7 changed files with 80 additions and 9 deletions

View File

@ -85,4 +85,11 @@ menu "esp-modem"
mode more robust for some devices (e.g. Quectel), but might cause mode more robust for some devices (e.g. Quectel), but might cause
trouble for other devices (e.g. SIMCOM). trouble for other devices (e.g. SIMCOM).
config ESP_MODEM_ADD_DEBUG_LOGS
bool "Add UART Tx/Rx logs"
default n
help
If enabled, the library dumps all transmitted and received data.
This option is only used for debugging.
endmenu endmenu

View File

@ -385,6 +385,17 @@ extern "C" void app_main(void)
return 0; return 0;
}); });
#endif #endif
const ConsoleCommand PauseNetwork("pause_net", "toggle network pause", no_args, [&](ConsoleCommand * c) {
static int cnt = 0;
if (++cnt % 2) {
ESP_LOGI(TAG, "Pausing netif");
dce->pause_netif(true);
} else {
ESP_LOGI(TAG, "Unpausing netif");
dce->pause_netif(false);
}
return 0;
});
const struct SetApn { const struct SetApn {
SetApn(): apn(STR1, nullptr, nullptr, "<apn>", "APN (Access Point Name)") {} SetApn(): apn(STR1, nullptr, nullptr, "<apn>", "APN (Access Point Name)") {}

View File

@ -103,6 +103,28 @@ public:
} }
#endif #endif
/**
* @brief Pauses/Unpauses network temporarily
* @param do_pause true to pause, false to unpause
* @param force true to ignore command failures and continue
* @return command_result of the underlying commands
*/
command_result pause_netif(bool do_pause, bool force = false)
{
command_result result;
if (do_pause) {
netif.pause();
dte->set_command_callbacks();
result = device->set_command_mode();
} else {
result = device->resume_data_mode();
if (result == command_result::OK || force) {
netif.resume();
}
}
return result;
}
protected: protected:
std::shared_ptr<DTE> dte; std::shared_ptr<DTE> dte;
std::shared_ptr<SpecificModule> device; std::shared_ptr<SpecificModule> device;

View File

@ -145,6 +145,12 @@ public:
*/ */
bool recover(); bool recover();
/**
* @brief Set internal command callbacks to the underlying terminal.
* Here we capture command replies to be processed by supplied command callbacks in struct command_cb.
*/
void set_command_callbacks();
protected: protected:
/** /**
* @brief Allows for locking the DTE * @brief Allows for locking the DTE
@ -204,12 +210,6 @@ private:
} inflatable; } inflatable;
#endif // CONFIG_ESP_MODEM_USE_INFLATABLE_BUFFER_IF_NEEDED #endif // CONFIG_ESP_MODEM_USE_INFLATABLE_BUFFER_IF_NEEDED
/**
* @brief Set internal command callbacks to the underlying terminal.
* Here we capture command replies to be processed by supplied command callbacks in struct command_cb.
*/
void set_command_callbacks();
/** /**
* @brief This abstracts command callback processing and implements its locking, signaling of completion and timeouts. * @brief This abstracts command callback processing and implements its locking, signaling of completion and timeouts.
*/ */

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -54,6 +54,16 @@ public:
*/ */
void stop(); void stop();
/**
* @brief Pause the network interface
*/
void pause();
/**
* @brief Resume the network interface
*/
void resume();
void receive(uint8_t *data, size_t len); void receive(uint8_t *data, size_t len);
private: private:

View File

@ -99,6 +99,20 @@ void Netif::stop()
signal.clear(PPP_STARTED); signal.clear(PPP_STARTED);
} }
void Netif::resume()
{
ppp_dte->set_read_cb([this](uint8_t *data, size_t len) -> bool {
receive(data, len);
return true;
});
signal.set(PPP_STARTED);
}
void Netif::pause()
{
signal.clear(PPP_STARTED);
}
Netif::~Netif() Netif::~Netif()
{ {
if (signal.is_any(PPP_STARTED)) { if (signal.is_any(PPP_STARTED)) {

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -176,13 +176,20 @@ int UartTerminal::read(uint8_t *data, size_t len)
uart_get_buffered_data_len(uart.port, &length); uart_get_buffered_data_len(uart.port, &length);
length = std::min(len, length); length = std::min(len, length);
if (length > 0) { if (length > 0) {
return uart_read_bytes(uart.port, data, length, portMAX_DELAY); int read_len = uart_read_bytes(uart.port, data, length, portMAX_DELAY);
#if CONFIG_ESP_MODEM_ADD_DEBUG_LOGS
ESP_LOG_BUFFER_HEXDUMP("uart-rx", data, read_len, ESP_LOG_DEBUG);
#endif
return read_len;
} }
return 0; return 0;
} }
int UartTerminal::write(uint8_t *data, size_t len) int UartTerminal::write(uint8_t *data, size_t len)
{ {
#if CONFIG_ESP_MODEM_ADD_DEBUG_LOGS
ESP_LOG_BUFFER_HEXDUMP("uart-tx", data, len, ESP_LOG_DEBUG);
#endif
return uart_write_bytes_compat(uart.port, data, len); return uart_write_bytes_compat(uart.port, data, len);
} }