From 1db83cd1ca03c64bbe2e939c2214e03e86c3c348 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 5 Dec 2024 10:30:22 +0100 Subject: [PATCH] feat(modem): Support for pausing network in C-API also adds a demo of this feature to pppos client example --- .../examples/pppos_client/main/Kconfig.projbuild | 10 ++++++++++ .../pppos_client/main/pppos_client_main.c | 16 +++++++++++++++- .../examples/pppos_client/sdkconfig.ci.sim800_c3 | 1 + .../esp_modem/include/esp_modem_c_api_types.h | 11 +++++++++++ components/esp_modem/src/esp_modem_c_api.cpp | 8 ++++++++ 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/components/esp_modem/examples/pppos_client/main/Kconfig.projbuild b/components/esp_modem/examples/pppos_client/main/Kconfig.projbuild index e4ccc451db..d5d6381b08 100644 --- a/components/esp_modem/examples/pppos_client/main/Kconfig.projbuild +++ b/components/esp_modem/examples/pppos_client/main/Kconfig.projbuild @@ -201,4 +201,14 @@ menu "Example Configuration" help MQTT data message, which we publish and expect to receive. + config EXAMPLE_PAUSE_NETIF_TO_CHECK_SIGNAL + bool "Demonstrate netif pause" + default n + help + Set this to true to demonstrate network pausing. + If enabled, the example waits for an MQTT data, then temporarily + drops network to check signal quality, resumes networking and + publishes another MQTT message. + Connection to the MQTT broker should be kept. + endmenu diff --git a/components/esp_modem/examples/pppos_client/main/pppos_client_main.c b/components/esp_modem/examples/pppos_client/main/pppos_client_main.c index 3529965406..b128844f6b 100644 --- a/components/esp_modem/examples/pppos_client/main/pppos_client_main.c +++ b/components/esp_modem/examples/pppos_client/main/pppos_client_main.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -317,6 +317,20 @@ void app_main(void) esp_mqtt_client_handle_t mqtt_client = esp_mqtt_client_init(&mqtt_config); esp_mqtt_client_register_event(mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); esp_mqtt_client_start(mqtt_client); + +#if CONFIG_EXAMPLE_PAUSE_NETIF_TO_CHECK_SIGNAL + xEventGroupWaitBits(event_group, GOT_DATA_BIT, pdTRUE, pdFALSE, portMAX_DELAY); + esp_modem_pause_net(dce, true); + err = esp_modem_get_signal_quality(dce, &rssi, &ber); + if (err != ESP_OK) { + ESP_LOGE(TAG, "esp_modem_get_signal_quality failed with %d", err); + return; + } + ESP_LOGI(TAG, "Signal quality: rssi=%d, ber=%d", rssi, ber); + esp_modem_pause_net(dce, false); + esp_mqtt_client_publish(mqtt_client, CONFIG_EXAMPLE_MQTT_TEST_TOPIC, CONFIG_EXAMPLE_MQTT_TEST_DATA, 0, 0, 0); +#endif + ESP_LOGI(TAG, "Waiting for MQTT data"); xEventGroupWaitBits(event_group, GOT_DATA_BIT | USB_DISCONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY); CHECK_USB_DISCONNECTION(event_group); diff --git a/components/esp_modem/examples/pppos_client/sdkconfig.ci.sim800_c3 b/components/esp_modem/examples/pppos_client/sdkconfig.ci.sim800_c3 index c82ef823fc..a7832bc0ad 100644 --- a/components/esp_modem/examples/pppos_client/sdkconfig.ci.sim800_c3 +++ b/components/esp_modem/examples/pppos_client/sdkconfig.ci.sim800_c3 @@ -11,5 +11,6 @@ CONFIG_EXAMPLE_MODEM_DEVICE_SIM800=y CONFIG_EXAMPLE_MODEM_DEVICE_BG96=n CONFIG_EXAMPLE_MODEM_PPP_APN="lpwa.vodafone.com" CONFIG_EXAMPLE_MQTT_TEST_TOPIC="/ci/esp-modem/pppos-client" +CONFIG_EXAMPLE_PAUSE_NETIF_TO_CHECK_SIGNAL=y CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT=y CONFIG_ESP32_PANIC_PRINT_HALT=y diff --git a/components/esp_modem/include/esp_modem_c_api_types.h b/components/esp_modem/include/esp_modem_c_api_types.h index d061045e9a..d3d9718d69 100644 --- a/components/esp_modem/include/esp_modem_c_api_types.h +++ b/components/esp_modem/include/esp_modem_c_api_types.h @@ -160,6 +160,17 @@ esp_err_t esp_modem_set_apn(esp_modem_dce_t *dce, const char *apn); esp_err_t esp_modem_set_urc(esp_modem_dce_t *dce, esp_err_t(*got_line_cb)(uint8_t *data, size_t len)); #endif +/** + * @brief This API provides support for temporarily pausing networking in order + * to send/receive AT commands and resume networking afterwards. + * @note This function does not switch modes, the modem is still in data mode. + * + * @param dce Modem DCE handle + * @param pause true to pause the network interface, false to resume networking + * @return ESP_OK on success + */ +esp_err_t esp_modem_pause_net(esp_modem_dce_t *dce, bool pause); + /** * @} */ diff --git a/components/esp_modem/src/esp_modem_c_api.cpp b/components/esp_modem/src/esp_modem_c_api.cpp index 4d1fa62919..edd8ff379f 100644 --- a/components/esp_modem/src/esp_modem_c_api.cpp +++ b/components/esp_modem/src/esp_modem_c_api.cpp @@ -475,3 +475,11 @@ extern "C" esp_err_t esp_modem_set_urc(esp_modem_dce_t *dce_wrap, esp_err_t(*got return ESP_OK; } #endif + +extern "C" esp_err_t esp_modem_pause_net(esp_modem_dce_t *dce_wrap, bool pause) +{ + if (dce_wrap == nullptr || dce_wrap->dce == nullptr) { + return ESP_ERR_INVALID_ARG; + } + return command_response_to_esp_err(dce_wrap->dce->pause_netif(pause)); +}