From 6471396ae6794765542eeceabffaf21f7b193a77 Mon Sep 17 00:00:00 2001 From: Liu Han Date: Mon, 25 Jan 2021 21:02:32 +0800 Subject: [PATCH 1/2] esp_modem: Fixed send data and wait for prompt fail from DCE Closes https://github.com/espressif/esp-idf/issues/6428 Closes https://github.com/espressif/esp-idf/issues/6651 --- .../components/modem/include/esp_modem_dce.h | 1 + .../components/modem/src/esp_modem.c | 38 +++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/examples/protocols/pppos_client/components/modem/include/esp_modem_dce.h b/examples/protocols/pppos_client/components/modem/include/esp_modem_dce.h index 3da9d0aea1..e17f8bbb29 100644 --- a/examples/protocols/pppos_client/components/modem/include/esp_modem_dce.h +++ b/examples/protocols/pppos_client/components/modem/include/esp_modem_dce.h @@ -77,6 +77,7 @@ struct modem_dce { char name[MODEM_MAX_NAME_LENGTH]; /*!< Module name */ char oper[MODEM_MAX_OPERATOR_LENGTH]; /*!< Operator name */ uint8_t act; /*!< Access technology */ + const char *prompt; /*!< Modem prompt string */ modem_state_t state; /*!< Modem working state */ modem_mode_t mode; /*!< Working mode */ modem_dte_t *dte; /*!< DTE which connect to DCE */ diff --git a/examples/protocols/pppos_client/components/modem/src/esp_modem.c b/examples/protocols/pppos_client/components/modem/src/esp_modem.c index fa7a5f2ed3..a3470be76b 100644 --- a/examples/protocols/pppos_client/components/modem/src/esp_modem.c +++ b/examples/protocols/pppos_client/components/modem/src/esp_modem.c @@ -329,6 +329,22 @@ err: return -1; } +/** + * @brief Handle response from send data and wait from prompt. + */ +static esp_err_t esp_modem_dte_send_wait_default_handler(modem_dce_t *dce, const char *line) +{ + esp_err_t err = ESP_FAIL; + if (!strncmp(line, dce->prompt, strlen(dce->prompt))) { + dce->state = MODEM_STATE_SUCCESS; + err = dce->dte->process_cmd_done(dce->dte); + } else { + dce->state = MODEM_STATE_FAIL; + err = dce->dte->process_cmd_done(dce->dte); + } + return err; +} + /** * @brief Send data and wait for prompt from DCE * @@ -346,22 +362,20 @@ static esp_err_t esp_modem_dte_send_wait(modem_dte_t *dte, const char *data, uin { MODEM_CHECK(data, "data is NULL", err_param); MODEM_CHECK(prompt, "prompt is NULL", err_param); + modem_dce_t *dce = dte->dce; + MODEM_CHECK(dce, "DTE has not yet bind with DCE", err_param); esp_modem_dte_t *esp_dte = __containerof(dte, esp_modem_dte_t, parent); - // We'd better disable pattern detection here for a moment in case prompt string contains the pattern character - uart_disable_pattern_det_intr(esp_dte->uart_port); - // uart_disable_rx_intr(esp_dte->uart_port); - MODEM_CHECK(uart_write_bytes(esp_dte->uart_port, data, length) >= 0, "uart write bytes failed", err_write); - uint32_t len = strlen(prompt); - uint8_t *buffer = calloc(len + 1, sizeof(uint8_t)); - int res = uart_read_bytes(esp_dte->uart_port, buffer, len, pdMS_TO_TICKS(timeout)); - MODEM_CHECK(res >= len, "wait prompt [%s] timeout", err, prompt); - MODEM_CHECK(!strncmp(prompt, (const char *)buffer, len), "get wrong prompt: %s", err, buffer); - free(buffer); + // We'd better change pattern detection here for a moment in case prompt string contains the pattern character + uart_enable_pattern_det_baud_intr(esp_dte->uart_port, ' ', 1, MIN_PATTERN_INTERVAL, MIN_POST_IDLE, MIN_PRE_IDLE); + dce->prompt = prompt; + dce->handle_line = esp_modem_dte_send_wait_default_handler; + MODEM_CHECK(dte->send_cmd(dte, data, timeout) == ESP_OK, "wait for prompt timeout", err); + MODEM_CHECK(dce->state == MODEM_STATE_SUCCESS, "wait for prompt failed", err); + dce->prompt = NULL; uart_enable_pattern_det_baud_intr(esp_dte->uart_port, '\n', 1, MIN_PATTERN_INTERVAL, MIN_POST_IDLE, MIN_PRE_IDLE); return ESP_OK; err: - free(buffer); -err_write: + dce->prompt = NULL; uart_enable_pattern_det_baud_intr(esp_dte->uart_port, '\n', 1, MIN_PATTERN_INTERVAL, MIN_POST_IDLE, MIN_PRE_IDLE); err_param: return ESP_FAIL; From fd47dd4beac1fe69fa044630e764012a0195183d Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 30 Mar 2021 16:00:46 +0200 Subject: [PATCH 2/2] Examples/PPPoS: Fix pppos SMS send potential timeout in prompt waiting --- examples/protocols/pppos_client/main/pppos_client_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/protocols/pppos_client/main/pppos_client_main.c b/examples/protocols/pppos_client/main/pppos_client_main.c index 2602299e25..700bc77b34 100644 --- a/examples/protocols/pppos_client/main/pppos_client_main.c +++ b/examples/protocols/pppos_client/main/pppos_client_main.c @@ -59,7 +59,7 @@ static esp_err_t example_handle_cmgs(modem_dce_t *dce, const char *line) #define MODEM_SMS_MAX_LENGTH (128) #define MODEM_COMMAND_TIMEOUT_SMS_MS (120000) -#define MODEM_PROMPT_TIMEOUT_MS (10) +#define MODEM_PROMPT_TIMEOUT_MS (100) static esp_err_t example_send_message_text(modem_dce_t *dce, const char *phone_num, const char *text) {