Skip to content

Commit

Permalink
feat(modem): Add support for pausing netif
Browse files Browse the repository at this point in the history
Closes #699
  • Loading branch information
david-cermak committed Nov 29, 2024
1 parent 849fe7b commit 5db7692
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 8 deletions.
7 changes: 7 additions & 0 deletions components/esp_modem/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,11 @@ menu "esp-modem"
help
If enabled, APIs to add URC handler are available

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
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,17 @@ extern "C" void app_main(void)
return 0;
});
#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 {
SetApn(): apn(STR1, nullptr, nullptr, "<apn>", "APN (Access Point Name)") {}
Expand Down
22 changes: 22 additions & 0 deletions components/esp_modem/include/cxx_include/esp_modem_dce.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,28 @@ class DCE_T {
}
#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(true);
dte->set_command_callbacks();
result = device->set_command_mode();
} else {
result = device->resume_data_mode();
if (result == command_result::OK || force) {
netif.pause(false);
}
}
return result;
}

protected:
std::shared_ptr<DTE> dte;
std::shared_ptr<SpecificModule> device;
Expand Down
12 changes: 6 additions & 6 deletions components/esp_modem/include/cxx_include/esp_modem_dte.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ class DTE : public CommandableIf {
*/
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:
/**
* @brief Allows for locking the DTE
Expand Down Expand Up @@ -204,12 +210,6 @@ class DTE : public CommandableIf {
} inflatable;
#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.
*/
Expand Down
5 changes: 5 additions & 0 deletions components/esp_modem/include/cxx_include/esp_modem_netif.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class Netif {
*/
void stop();

/**
* @brief Pause or unpause the network interface
*/
void pause(bool do_pause);

void receive(uint8_t *data, size_t len);

private:
Expand Down
13 changes: 13 additions & 0 deletions components/esp_modem/src/esp_modem_netif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,19 @@ void Netif::stop()
signal.clear(PPP_STARTED);
}

void Netif::pause(bool do_pause)
{
if (do_pause == false) {
ppp_dte->set_read_cb([this](uint8_t *data, size_t len) -> bool {
receive(data, len);
return true;
});
signal.set(PPP_STARTED);
} else {
signal.clear(PPP_STARTED);
}
}

Netif::~Netif()
{
if (signal.is_any(PPP_STARTED)) {
Expand Down
11 changes: 9 additions & 2 deletions components/esp_modem/src/esp_modem_uart.cpp
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -176,13 +176,20 @@ int UartTerminal::read(uint8_t *data, size_t len)
uart_get_buffered_data_len(uart.port, &length);
length = std::min(len, length);
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;
}

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);
}

Expand Down

0 comments on commit 5db7692

Please sign in to comment.