Skip to content

Commit 5e92990

Browse files
committed
fix(modem): Minor cleanup of pppos example
* Use CONFIG_EXAMPLE_DETECT_MODE_BEFORE_CONNECT to demonstrate mode detect * Use disconnection flag to indicate conneciton issue and gracefully degrade to command mode * Remove IDF-verion < v5.0 code
1 parent c989c6a commit 5e92990

File tree

6 files changed

+48
-17
lines changed

6 files changed

+48
-17
lines changed

components/esp_modem/examples/pppos_client/main/Kconfig.projbuild

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,13 @@ menu "Example Configuration"
211211
publishes another MQTT message.
212212
Connection to the MQTT broker should be kept.
213213

214+
config EXAMPLE_DETECT_MODE_BEFORE_CONNECT
215+
bool "Detect mode before connect"
216+
default n
217+
help
218+
Set this to true to demonstrate mode auto-detection.
219+
If enabled, the example tries to recognize the actual mode.
220+
If mode is detected correctly and it is not a command mode,
221+
then the example switches to command mode.
222+
214223
endmenu

components/esp_modem/examples/pppos_client/main/pppos_client_main.c

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
static const char *TAG = "pppos_example";
3535
static EventGroupHandle_t event_group = NULL;
3636
static const int CONNECT_BIT = BIT0;
37+
static const int DISCONNECT_BIT = BIT1;
3738
static const int GOT_DATA_BIT = BIT2;
3839
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
3940

@@ -55,6 +56,7 @@ static void usb_terminal_error_handler(esp_modem_terminal_error_t err)
5556
}
5657
#define CHECK_USB_DISCONNECTION(event_group) \
5758
if ((xEventGroupGetBits(event_group) & USB_DISCONNECTED_BIT) == USB_DISCONNECTED_BIT) { \
59+
ESP_LOGE(TAG, "USB_DISCONNECTED_BIT destroying modem dce"); \
5860
esp_modem_destroy(dce); \
5961
continue; \
6062
}
@@ -140,6 +142,7 @@ static void on_ip_event(void *arg, esp_event_base_t event_base,
140142
ESP_LOGI(TAG, "GOT ip event!!!");
141143
} else if (event_id == IP_EVENT_PPP_LOST_IP) {
142144
ESP_LOGI(TAG, "Modem Disconnect from PPP Server");
145+
xEventGroupSetBits(event_group, DISCONNECT_BIT);
143146
} else if (event_id == IP_EVENT_GOT_IP6) {
144147
ESP_LOGI(TAG, "GOT IPv6 event!");
145148

@@ -158,6 +161,7 @@ void app_main(void)
158161
ESP_ERROR_CHECK(esp_event_handler_register(NETIF_PPP_STATUS, ESP_EVENT_ANY_ID, &on_ppp_changed, NULL));
159162

160163
/* Configure the PPP netif */
164+
esp_err_t err;
161165
esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(CONFIG_EXAMPLE_MODEM_PPP_APN);
162166
esp_netif_config_t netif_ppp_config = ESP_NETIF_DEFAULT_PPP();
163167
esp_netif_t *esp_netif = esp_netif_new(&netif_ppp_config);
@@ -177,7 +181,7 @@ void app_main(void)
177181
dte_config.uart_config.rx_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE;
178182
dte_config.uart_config.tx_buffer_size = CONFIG_EXAMPLE_MODEM_UART_TX_BUFFER_SIZE;
179183
dte_config.uart_config.event_queue_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_QUEUE_SIZE;
180-
dte_config.task_stack_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_STACK_SIZE * 2;
184+
dte_config.task_stack_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_STACK_SIZE;
181185
dte_config.task_priority = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_PRIORITY;
182186
dte_config.dte_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE / 2;
183187

@@ -205,7 +209,7 @@ void app_main(void)
205209
#endif
206210
assert(dce);
207211
if (dte_config.uart_config.flow_control == ESP_MODEM_FLOW_CONTROL_HW) {
208-
esp_err_t err = esp_modem_set_flow_control(dce, 2, 2); //2/2 means HW Flow Control.
212+
err = esp_modem_set_flow_control(dce, 2, 2); //2/2 means HW Flow Control.
209213
if (err != ESP_OK) {
210214
ESP_LOGE(TAG, "Failed to set the set_flow_control mode");
211215
return;
@@ -246,23 +250,27 @@ void app_main(void)
246250
#error Invalid serial connection to modem.
247251
#endif
248252

249-
xEventGroupClearBits(event_group, CONNECT_BIT | GOT_DATA_BIT | USB_DISCONNECTED_BIT);
253+
#if CONFIG_EXAMPLE_DETECT_MODE_BEFORE_CONNECT
254+
xEventGroupClearBits(event_group, CONNECT_BIT | GOT_DATA_BIT | USB_DISCONNECTED_BIT | DISCONNECT_BIT);
250255

251-
esp_err_t err = esp_modem_set_mode(dce, ESP_MODEM_MODE_DETECT);
256+
err = esp_modem_set_mode(dce, ESP_MODEM_MODE_DETECT);
252257
if (err != ESP_OK) {
253258
ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_DETECT) failed with %d", err);
254259
return;
255260
}
256-
ESP_LOGI(TAG, "Mode detected!");
257261
esp_modem_dce_mode_t mode = esp_modem_get_mode(dce);
258-
ESP_LOGI(TAG, "Current mode is : %d", mode);
259-
if (mode != ESP_MODEM_MODE_COMMAND) {
262+
ESP_LOGI(TAG, "Mode detection completed: current mode is: %d", mode);
263+
if (mode == ESP_MODEM_MODE_DATA) { // set back to command mode
260264
err = esp_modem_set_mode(dce, ESP_MODEM_MODE_COMMAND);
261265
if (err != ESP_OK) {
262266
ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_COMMAND) failed with %d", err);
263267
return;
264268
}
269+
ESP_LOGI(TAG, "Command mode restored");
265270
}
271+
#endif // CONFIG_EXAMPLE_DETECT_MODE_BEFORE_CONNECT
272+
273+
xEventGroupClearBits(event_group, CONNECT_BIT | GOT_DATA_BIT | USB_DISCONNECTED_BIT | DISCONNECT_BIT);
266274

267275
/* Run the modem demo app */
268276
#if CONFIG_EXAMPLE_NEED_SIM_PIN == 1
@@ -317,19 +325,24 @@ void app_main(void)
317325
}
318326
/* Wait for IP address */
319327
ESP_LOGI(TAG, "Waiting for IP address");
320-
xEventGroupWaitBits(event_group, CONNECT_BIT | USB_DISCONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
328+
xEventGroupWaitBits(event_group, CONNECT_BIT | USB_DISCONNECTED_BIT | DISCONNECT_BIT, pdFALSE, pdFALSE,
329+
pdMS_TO_TICKS(60000));
321330
CHECK_USB_DISCONNECTION(event_group);
331+
if ((xEventGroupGetBits(event_group) & CONNECT_BIT) != CONNECT_BIT) {
332+
ESP_LOGW(TAG, "Modem not connected, switching back to the command mode");
333+
err = esp_modem_set_mode(dce, ESP_MODEM_MODE_COMMAND);
334+
if (err != ESP_OK) {
335+
ESP_LOGE(TAG, "esp_modem_set_mode(ESP_MODEM_MODE_COMMAND) failed with %d", err);
336+
return;
337+
}
338+
ESP_LOGI(TAG, "Command mode restored");
339+
return;
340+
}
322341

323342
/* Config MQTT */
324-
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
325343
esp_mqtt_client_config_t mqtt_config = {
326344
.broker.address.uri = CONFIG_EXAMPLE_MQTT_BROKER_URI,
327345
};
328-
#else
329-
esp_mqtt_client_config_t mqtt_config = {
330-
.uri = CONFIG_EXAMPLE_MQTT_BROKER_URI,
331-
};
332-
#endif
333346
esp_mqtt_client_handle_t mqtt_client = esp_mqtt_client_init(&mqtt_config);
334347
esp_mqtt_client_register_event(mqtt_client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL);
335348
esp_mqtt_client_start(mqtt_client);
@@ -345,7 +358,7 @@ void app_main(void)
345358
ESP_LOGI(TAG, "Signal quality: rssi=%d, ber=%d", rssi, ber);
346359
esp_modem_pause_net(dce, false);
347360
esp_mqtt_client_publish(mqtt_client, CONFIG_EXAMPLE_MQTT_TEST_TOPIC, CONFIG_EXAMPLE_MQTT_TEST_DATA, 0, 0, 0);
348-
#endif
361+
#endif // CONFIG_EXAMPLE_PAUSE_NETIF_TO_CHECK_SIGNAL
349362

350363
ESP_LOGI(TAG, "Waiting for MQTT data");
351364
xEventGroupWaitBits(event_group, GOT_DATA_BIT | USB_DISCONNECTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);

components/esp_modem/examples/pppos_client/pytest_pppos_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def test_pppos_connect(dut):
1212
4. checks that the client cleanly disconnects
1313
"""
1414
# Check the sequence of connecting, publishing, disconnecting
15-
dut.expect('Modem Connect to PPP Server')
15+
dut.expect('Modem Connect to PPP Server', timeout=90)
1616
# Check for MQTT connection and the data event
1717
dut.expect('MQTT_EVENT_CONNECTED')
1818
dut.expect('MQTT_EVENT_DATA')

components/esp_modem/examples/pppos_client/sdkconfig.ci.sim800_c3

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ CONFIG_EXAMPLE_MQTT_TEST_TOPIC="/ci/esp-modem/pppos-client"
1414
CONFIG_EXAMPLE_PAUSE_NETIF_TO_CHECK_SIGNAL=y
1515
CONFIG_ESP_SYSTEM_PANIC_PRINT_HALT=y
1616
CONFIG_ESP32_PANIC_PRINT_HALT=y
17-
CONFIG_LOG_DEFAULT_LEVEL_DEBUG=y
17+
CONFIG_EXAMPLE_DETECT_MODE_BEFORE_CONNECT=y

components/esp_modem/examples/simple_cmux_client/sdkconfig.ci.sim800_cmux

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ CONFIG_COMPILER_CXX_EXCEPTIONS=y
1616
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8192
1717
CONFIG_EXAMPLE_CLOSE_CMUX_AT_END=y
1818
CONFIG_EXAMPLE_MQTT_TEST_TOPIC="/ci/esp-modem/pppos-client"
19+
CONFIG_BROKER_URI="mqtt://mqtt.eclipseprojects.io"

components/esp_modem/src/esp_modem_c_api.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,11 @@ extern "C" esp_err_t esp_modem_pause_net(esp_modem_dce_t *dce_wrap, bool pause)
516516
}
517517
return command_response_to_esp_err(dce_wrap->dce->pause_netif(pause));
518518
}
519+
520+
extern "C" esp_err_t esp_modem_hang_up(esp_modem_dce_t *dce_wrap)
521+
{
522+
if (dce_wrap == nullptr || dce_wrap->dce == nullptr) {
523+
return ESP_ERR_INVALID_ARG;
524+
}
525+
return command_response_to_esp_err(dce_wrap->dce->hang_up());
526+
}

0 commit comments

Comments
 (0)