From 6bea2b8110727fbe470520153a6ff14898c518c4 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 6 Dec 2024 10:56:06 +0100 Subject: [PATCH] fix(modem): Use the same public broker for examples --- .../pppos_client/main/Kconfig.projbuild | 9 ++++ .../pppos_client/main/pppos_client_main.c | 44 +++++++++++++++++-- .../pppos_client/pytest_pppos_client.py | 2 +- .../pppos_client/sdkconfig.ci.sim800_c3 | 2 +- .../pppos_client/sdkconfig.ci.usb_a7670_s2 | 1 + .../sdkconfig.ci.sim800_cmux | 1 + components/esp_modem/src/esp_modem_c_api.cpp | 8 ++++ 7 files changed, 61 insertions(+), 6 deletions(-) diff --git a/components/esp_modem/examples/pppos_client/main/Kconfig.projbuild b/components/esp_modem/examples/pppos_client/main/Kconfig.projbuild index d5d6381b08..2bb3e4c551 100644 --- a/components/esp_modem/examples/pppos_client/main/Kconfig.projbuild +++ b/components/esp_modem/examples/pppos_client/main/Kconfig.projbuild @@ -211,4 +211,13 @@ menu "Example Configuration" publishes another MQTT message. Connection to the MQTT broker should be kept. + config EXAMPLE_DETECT_MODE_BEFORE_CONNECT + bool "Detect mode before connect" + default n + help + Set this to true to demonstrate mode auto-detection. + If enabled, the example tries to recognize the actual mode. + If mode is detected correctly and it is not a command mode, + then the example switches to command mode. + 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 7c0212eefb..f200e7717a 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 @@ -34,6 +34,7 @@ static const char *TAG = "pppos_example"; static EventGroupHandle_t event_group = NULL; static const int CONNECT_BIT = BIT0; +static const int DISCONNECT_BIT = BIT1; static const int GOT_DATA_BIT = BIT2; static const int USB_DISCONNECTED_BIT = BIT3; // Used only with USB DTE but we define it unconditionally, to avoid too many #ifdefs in the code @@ -55,6 +56,7 @@ static void usb_terminal_error_handler(esp_modem_terminal_error_t err) } #define CHECK_USB_DISCONNECTION(event_group) \ if ((xEventGroupGetBits(event_group) & USB_DISCONNECTED_BIT) == USB_DISCONNECTED_BIT) { \ + ESP_LOGE(TAG, "USB_DISCONNECTED_BIT destroying modem dce"); \ esp_modem_destroy(dce); \ continue; \ } @@ -140,6 +142,7 @@ static void on_ip_event(void *arg, esp_event_base_t event_base, ESP_LOGI(TAG, "GOT ip event!!!"); } else if (event_id == IP_EVENT_PPP_LOST_IP) { ESP_LOGI(TAG, "Modem Disconnect from PPP Server"); + xEventGroupSetBits(event_group, DISCONNECT_BIT); } else if (event_id == IP_EVENT_GOT_IP6) { ESP_LOGI(TAG, "GOT IPv6 event!"); @@ -158,6 +161,7 @@ void app_main(void) ESP_ERROR_CHECK(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, NULL)); /* Configure the PPP netif */ + esp_err_t err; esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(CONFIG_EXAMPLE_MODEM_PPP_APN); esp_netif_config_t netif_ppp_config = ESP_NETIF_DEFAULT_PPP(); esp_netif_t *esp_netif = esp_netif_new(&netif_ppp_config); @@ -246,9 +250,10 @@ void app_main(void) #error Invalid serial connection to modem. #endif - xEventGroupClearBits(event_group, CONNECT_BIT | GOT_DATA_BIT | USB_DISCONNECTED_BIT); +#if CONFIG_EXAMPLE_DETECT_MODE_BEFORE_CONNECT + xEventGroupClearBits(event_group, CONNECT_BIT | GOT_DATA_BIT | USB_DISCONNECTED_BIT | DISCONNECT_BIT); - esp_err_t err = esp_modem_set_mode(dce, ESP_MODEM_MODE_DETECT); + err = esp_modem_set_mode(dce, ESP_MODEM_MODE_DETECT); if (err != ESP_OK) { ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_DETECT) failed with %d", err); return; @@ -256,13 +261,26 @@ void app_main(void) ESP_LOGI(TAG, "Mode detected!"); esp_modem_dce_mode_t mode = esp_modem_get_mode(dce); ESP_LOGI(TAG, "Current mode is : %d", mode); - if (mode != ESP_MODEM_MODE_COMMAND) { + if (mode == ESP_MODEM_MODE_DATA) { + ESP_LOGI(TAG, "Waiting for IP address"); + xEventGroupWaitBits(event_group, CONNECT_BIT | USB_DISCONNECTED_BIT | DISCONNECT_BIT, pdFALSE, pdFALSE, pdMS_TO_TICKS(5000)); + ESP_LOGW(TAG, "Waiting for IP address DONE 1"); err = esp_modem_set_mode(dce, ESP_MODEM_MODE_COMMAND); if (err != ESP_OK) { ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_COMMAND) failed with %d", err); return; } + ESP_LOGW(TAG, "Command mode done"); + err = esp_modem_hang_up(dce); + if (err != ESP_OK) { + ESP_LOGE(TAG, "esp_modem_hang_up failed with %d", err); + return; + } + ESP_LOGW(TAG, "Hangup completed..."); } +#endif + + xEventGroupClearBits(event_group, CONNECT_BIT | GOT_DATA_BIT | USB_DISCONNECTED_BIT | DISCONNECT_BIT); /* Run the modem demo app */ #if CONFIG_EXAMPLE_NEED_SIM_PIN == 1 @@ -317,8 +335,26 @@ void app_main(void) } /* Wait for IP address */ ESP_LOGI(TAG, "Waiting for IP address"); - xEventGroupWaitBits(event_group, CONNECT_BIT | USB_DISCONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY); + xEventGroupWaitBits(event_group, CONNECT_BIT | USB_DISCONNECTED_BIT | DISCONNECT_BIT, pdFALSE, pdFALSE, + pdMS_TO_TICKS(30000)); CHECK_USB_DISCONNECTION(event_group); + ESP_LOGW(TAG, "Waiting for IP address DONE!"); + if ((xEventGroupGetBits(event_group) & CONNECT_BIT) != CONNECT_BIT) { + ESP_LOGW(TAG, "Modem not connected!"); + err = esp_modem_set_mode(dce, ESP_MODEM_MODE_COMMAND); + if (err != ESP_OK) { + ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_COMMAND) failed with %d", err); + return; + } + ESP_LOGW(TAG, "Command mode done"); + err = esp_modem_reset(dce); + if (err != ESP_OK) { + ESP_LOGE(TAG, "esp_modem_hang_up failed with %d", err); + return; + } + ESP_LOGW(TAG, "Reset completed..."); + return; + } /* Config MQTT */ #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) diff --git a/components/esp_modem/examples/pppos_client/pytest_pppos_client.py b/components/esp_modem/examples/pppos_client/pytest_pppos_client.py index 2ffb42cabc..370fa75908 100644 --- a/components/esp_modem/examples/pppos_client/pytest_pppos_client.py +++ b/components/esp_modem/examples/pppos_client/pytest_pppos_client.py @@ -12,7 +12,7 @@ def test_pppos_connect(dut): 4. checks that the client cleanly disconnects """ # Check the sequence of connecting, publishing, disconnecting - dut.expect('Modem Connect to PPP Server') + dut.expect('Modem Connect to PPP Server', timeout=180) # Check for MQTT connection and the data event dut.expect('MQTT_EVENT_CONNECTED') dut.expect('MQTT_EVENT_DATA') 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 a537afeb22..23b0fc200f 100644 --- a/components/esp_modem/examples/pppos_client/sdkconfig.ci.sim800_c3 +++ b/components/esp_modem/examples/pppos_client/sdkconfig.ci.sim800_c3 @@ -14,4 +14,4 @@ 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 -CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y +CONFIG_EXAMPLE_DETECT_MODE_BEFORE_CONNECT=y diff --git a/components/esp_modem/examples/pppos_client/sdkconfig.ci.usb_a7670_s2 b/components/esp_modem/examples/pppos_client/sdkconfig.ci.usb_a7670_s2 index 665f78517c..5f555f9ba5 100644 --- a/components/esp_modem/examples/pppos_client/sdkconfig.ci.usb_a7670_s2 +++ b/components/esp_modem/examples/pppos_client/sdkconfig.ci.usb_a7670_s2 @@ -12,3 +12,4 @@ CONFIG_EXAMPLE_MODEM_PPP_AUTH_NONE=y CONFIG_EXAMPLE_MQTT_TEST_TOPIC="/ci/esp-modem/pppos-client" CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT=y CONFIG_ESP32_PANIC_PRINT_HALT=y +CONFIG_EXAMPLE_DETECT_MODE_BEFORE_CONNECT=y diff --git a/components/esp_modem/examples/simple_cmux_client/sdkconfig.ci.sim800_cmux b/components/esp_modem/examples/simple_cmux_client/sdkconfig.ci.sim800_cmux index 6bf623113c..6a22a89f12 100644 --- a/components/esp_modem/examples/simple_cmux_client/sdkconfig.ci.sim800_cmux +++ b/components/esp_modem/examples/simple_cmux_client/sdkconfig.ci.sim800_cmux @@ -16,3 +16,4 @@ CONFIG_COMPILER_CXX_EXCEPTIONS=y CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192 CONFIG_EXAMPLE_CLOSE_CMUX_AT_END=y CONFIG_EXAMPLE_MQTT_TEST_TOPIC="/ci/esp-modem/pppos-client" +CONFIG_BROKER_URI="mqtt://mqtt.eclipseprojects.io" diff --git a/components/esp_modem/src/esp_modem_c_api.cpp b/components/esp_modem/src/esp_modem_c_api.cpp index 036e013630..e9698995e1 100644 --- a/components/esp_modem/src/esp_modem_c_api.cpp +++ b/components/esp_modem/src/esp_modem_c_api.cpp @@ -516,3 +516,11 @@ extern "C" esp_err_t esp_modem_pause_net(esp_modem_dce_t *dce_wrap, bool pause) } return command_response_to_esp_err(dce_wrap->dce->pause_netif(pause)); } + +extern "C" esp_err_t esp_modem_hang_up(esp_modem_dce_t *dce_wrap) +{ + if (dce_wrap == nullptr || dce_wrap->dce == nullptr) { + return ESP_ERR_INVALID_ARG; + } + return command_response_to_esp_err(dce_wrap->dce->hang_up()); +}