Skip to content

Commit

Permalink
feat(modem): Add mode detection to the example
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Dec 6, 2024
1 parent 1db83cd commit 18f196f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void app_main(void)
dte_config.uart_config.rx_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE;
dte_config.uart_config.tx_buffer_size = CONFIG_EXAMPLE_MODEM_UART_TX_BUFFER_SIZE;
dte_config.uart_config.event_queue_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_QUEUE_SIZE;
dte_config.task_stack_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_STACK_SIZE;
dte_config.task_stack_size = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_STACK_SIZE * 2;
dte_config.task_priority = CONFIG_EXAMPLE_MODEM_UART_EVENT_TASK_PRIORITY;
dte_config.dte_buffer_size = CONFIG_EXAMPLE_MODEM_UART_RX_BUFFER_SIZE / 2;

Expand Down Expand Up @@ -248,6 +248,22 @@ void app_main(void)

xEventGroupClearBits(event_group, CONNECT_BIT | GOT_DATA_BIT | USB_DISCONNECTED_BIT);

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

/* Run the modem demo app */
#if CONFIG_EXAMPLE_NEED_SIM_PIN == 1
// check if PIN needed
Expand All @@ -262,7 +278,7 @@ void app_main(void)
#endif

int rssi, ber;
esp_err_t err = esp_modem_get_signal_quality(dce, &rssi, &ber);
err = esp_modem_get_signal_quality(dce, &rssi, &ber);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_modem_get_signal_quality failed with %d %s", err, esp_err_to_name(err));
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +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
8 changes: 7 additions & 1 deletion components/esp_modem/include/cxx_include/esp_modem_dce.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ class DCE_T {
return mode.set(dte.get(), device.get(), netif, m);
}

modem_mode get_mode()
{
return mode.get();
}

bool recover()
{
return dte->recover();
Expand All @@ -109,11 +114,12 @@ class DCE_T {
* @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 pause_netif(bool do_pause, bool force = false, int delay = 1000)
{
command_result result;
if (do_pause) {
netif.pause();
Task::Delay(delay); // Mandatory 1s pause before
dte->set_command_callbacks();
result = device->set_command_mode();
} else {
Expand Down
3 changes: 3 additions & 0 deletions components/esp_modem/include/esp_modem_c_api_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ typedef enum esp_modem_dce_mode {
ESP_MODEM_MODE_CMUX_MANUAL_SWAP, /**< Swap terminals in CMUX manual mode */
ESP_MODEM_MODE_CMUX_MANUAL_DATA, /**< Set DATA mode in CMUX manual mode */
ESP_MODEM_MODE_CMUX_MANUAL_COMMAND, /**< Set COMMAND mode in CMUX manual mode */
ESP_MODEM_MODE_DETECT, /**< Detect the mode and resume it (if sucessfully detected) */
ESP_MODEM_MODE_UNDEF,
} esp_modem_dce_mode_t;

/**
Expand Down Expand Up @@ -171,6 +173,7 @@ esp_err_t esp_modem_set_urc(esp_modem_dce_t *dce, esp_err_t(*got_line_cb)(uint8_
*/
esp_err_t esp_modem_pause_net(esp_modem_dce_t *dce, bool pause);

esp_modem_dce_mode_t esp_modem_get_mode(esp_modem_dce_t *dce);
/**
* @}
*/
Expand Down
33 changes: 33 additions & 0 deletions components/esp_modem/src/esp_modem_c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,43 @@ extern "C" esp_err_t esp_modem_sync(esp_modem_dce_t *dce_wrap)
return command_response_to_esp_err(dce_wrap->dce->sync());
}

extern "C" esp_modem_dce_mode_t esp_modem_get_mode(esp_modem_dce_t *dce_wrap)
{
if (dce_wrap == nullptr || dce_wrap->dce == nullptr) {
return ESP_MODEM_MODE_UNDEF;
}
auto mode = dce_wrap->dce->get_mode();
switch (mode) {
default:
case modem_mode::UNDEF:
return ESP_MODEM_MODE_UNDEF;
case modem_mode::COMMAND_MODE:
return ESP_MODEM_MODE_COMMAND;
case modem_mode::DATA_MODE:
return ESP_MODEM_MODE_DATA;
case modem_mode::CMUX_MODE:
return ESP_MODEM_MODE_CMUX;
case modem_mode::CMUX_MANUAL_MODE:
return ESP_MODEM_MODE_CMUX_MANUAL;
case modem_mode::CMUX_MANUAL_EXIT:
return ESP_MODEM_MODE_CMUX_MANUAL_EXIT;
case modem_mode::CMUX_MANUAL_DATA:
return ESP_MODEM_MODE_CMUX_MANUAL_DATA;
case modem_mode::CMUX_MANUAL_COMMAND:
return ESP_MODEM_MODE_CMUX_MANUAL_COMMAND;
case modem_mode::CMUX_MANUAL_SWAP:
return ESP_MODEM_MODE_CMUX_MANUAL_SWAP;
}
}

extern "C" esp_err_t esp_modem_set_mode(esp_modem_dce_t *dce_wrap, esp_modem_dce_mode_t mode)
{
if (dce_wrap == nullptr || dce_wrap->dce == nullptr) {
return ESP_ERR_INVALID_ARG;
}
switch (mode) {
case ESP_MODEM_MODE_UNDEF:
return dce_wrap->dce->set_mode(modem_mode::UNDEF) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_DATA:
return dce_wrap->dce->set_mode(modem_mode::DATA_MODE) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_COMMAND:
Expand All @@ -117,6 +148,8 @@ extern "C" esp_err_t esp_modem_set_mode(esp_modem_dce_t *dce_wrap, esp_modem_dce
return dce_wrap->dce->set_mode(modem_mode::CMUX_MANUAL_DATA) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_CMUX_MANUAL_COMMAND:
return dce_wrap->dce->set_mode(modem_mode::CMUX_MANUAL_COMMAND) ? ESP_OK : ESP_FAIL;
case ESP_MODEM_MODE_DETECT:
return dce_wrap->dce->set_mode(modem_mode::AUTODETECT) ? ESP_OK : ESP_FAIL;
}
return ESP_ERR_NOT_SUPPORTED;
}
Expand Down

0 comments on commit 18f196f

Please sign in to comment.