From ce175df376222b42a658769e90ca4752ccb90403 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 16 Sep 2022 16:17:01 +0200 Subject: [PATCH 1/3] fix(esp_modem): CMUX to ignore MSC frames Otherwise it gets confused with DISC frame and causes trouble when entering CMUX mode (if device sends MSC requests) Closes https://github.com/espressif/esp-protocols/issues/140 --- components/esp_modem/src/esp_modem_cmux.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/components/esp_modem/src/esp_modem_cmux.cpp b/components/esp_modem/src/esp_modem_cmux.cpp index 0243a4991..743371a99 100644 --- a/components/esp_modem/src/esp_modem_cmux.cpp +++ b/components/esp_modem/src/esp_modem_cmux.cpp @@ -143,6 +143,10 @@ void CMux::data_available(uint8_t *data, size_t len) #endif } } else if ((type&FT_UIH) == FT_UIH && dlci == 0) { // notify the internal DISC command + if (len > 0 && (data[0] & 0xE1) == 0xE1) { + // Not a DISC, ignore (MSC frame) + return; + } Scoped l(lock); sabm_ack = dlci; } @@ -385,6 +389,9 @@ bool CMux::init() return false; } } + if (i > 1) { // wait for each virtual terminal to settle MSC (no need for control term, DLCI=0) + usleep(100'000); + } } return true; } From 25ac2d98c66f3a45c359ea99a39ea112ecff61f5 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Fri, 30 Sep 2022 12:01:50 +0200 Subject: [PATCH 2/3] feat(esp_modem): Make some CMUX params compile-time configurable * bool flag to defragment CMUX payload (useful for devices that send longer messages) * int flag to force a delay between creating virtual terminals (useful for chatty devices that send some requests) --- components/esp_modem/Kconfig | 28 +++++++++++++++++++++ components/esp_modem/docs/README.md | 16 +++++++++++- components/esp_modem/src/esp_modem_cmux.cpp | 7 ++++-- 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 components/esp_modem/Kconfig diff --git a/components/esp_modem/Kconfig b/components/esp_modem/Kconfig new file mode 100644 index 000000000..242b58b6f --- /dev/null +++ b/components/esp_modem/Kconfig @@ -0,0 +1,28 @@ +menu "esp-modem" + + config ESP_MODEM_CMUX_DEFRAGMENT_PAYLOAD + bool "Defragment CMUX messages internally" + default y + help + If enabled (default), the esp-modem automatically defragments CMUX messages + to only pass the completed CMUX message to higher layers. + This is useful for messages in command mode (if they're received fragmented). + It's not a problem for messages in data mode as the upper layer (PPP protocol) + defines message boundaries. + Keep the default to true for most cases (as most devices use simply 1 byte CMUX + length, as the internal Rx buffer of size >= 256 bytes won't overflow) + Set to false if your devices uses 2 byte CMUX payload (e.g. A7672S). + The operation would work without an issue in data mode, but some replies + in command mode might come fragmented in rare cases so might need to retry + AT commands. + + config ESP_MODEM_CMUX_DELAY_AFTER_DLCI_SETUP + int "Delay in ms to wait before creating another virtual terminal" + default 0 + help + Some devices might need a pause before sending SABM command that creates + virtual terminal. This delay applies only to establishing a CMUX mode. + The typical reason for failing SABM request without a delay is that + some devices (SIM800) send MSC requests just after opening a new DLCI. + +endmenu diff --git a/components/esp_modem/docs/README.md b/components/esp_modem/docs/README.md index 918af0b4f..dc1e0639c 100644 --- a/components/esp_modem/docs/README.md +++ b/components/esp_modem/docs/README.md @@ -69,10 +69,24 @@ after creating multiple virtual terminals, designating some of them solely to da ### DTE's -Currently we support only UART, but modern modules support other communication interfaces, such as USB, SPI. +Currently, we support only UART (and USB as a preview feature), but modern modules support other communication interfaces, such as USB, SPI. ### Other devices Adding a new device is a must-have requirement for the esp-modem component. Different modules support different commands, or some commands might have a different implementation. Adding a new device means to provide a new implementation as a class derived from `GenericModule`, where we could add new commands or modify the existing ones. + +## Configuration + +Modem abstraction is configurable both compile-time and run-time. + +### Component Kconfig + +Compile-time configuration is provided using menuconfig. Please check the description for the CMUX mode configuration options. + +### Runtime configuration + +Is defined using standard configuration structures for `DTE` and `DCE` objects separately. Please find documentation of +* :cpp:class:`esp_modem_dte_config_t` +* :cpp:class:`esp_modem_dce_config_t` diff --git a/components/esp_modem/src/esp_modem_cmux.cpp b/components/esp_modem/src/esp_modem_cmux.cpp index 743371a99..8cb7e566f 100644 --- a/components/esp_modem/src/esp_modem_cmux.cpp +++ b/components/esp_modem/src/esp_modem_cmux.cpp @@ -17,15 +17,18 @@ #include #include "cxx_include/esp_modem_dte.hpp" #include "esp_log.h" +#include "sdkconfig.h" using namespace esp_modem; +#ifdef CONFIG_ESP_MODEM_CMUX_DEFRAGMENT_PAYLOAD /** * @brief Define this to defragment partially received data of CMUX payload * This is useful if upper layers expect the entire payload available * for parsing. */ #define DEFRAGMENT_CMUX_PAYLOAD +#endif #define EA 0x01 /* Extension bit */ #define CR 0x02 /* Command / Response */ @@ -290,7 +293,7 @@ bool CMux::on_cmux_data(uint8_t *data, size_t actual_len) actual_len = term->read(data, data_to_read); #else data = buffer.get(); - actual_len = term->read(data, buffer_size); + actual_len = term->read(data, buffer.size); #endif } ESP_LOG_BUFFER_HEXDUMP("CMUX Received", data, actual_len, ESP_LOG_VERBOSE); @@ -390,7 +393,7 @@ bool CMux::init() } } if (i > 1) { // wait for each virtual terminal to settle MSC (no need for control term, DLCI=0) - usleep(100'000); + usleep(CONFIG_ESP_MODEM_CMUX_DELAY_AFTER_DLCI_SETUP * 1'000); } } return true; From 9b48b0a5e0fa775c5834af649722895926a1ec7e Mon Sep 17 00:00:00 2001 From: David Cermak Date: Tue, 20 Sep 2022 08:00:47 +0200 Subject: [PATCH 3/3] update(esp_modem): Bump component version --- components/esp_modem/idf_component.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/esp_modem/idf_component.yml b/components/esp_modem/idf_component.yml index bdff44e37..ae17e474f 100644 --- a/components/esp_modem/idf_component.yml +++ b/components/esp_modem/idf_component.yml @@ -1,4 +1,4 @@ -version: "0.1.23" +version: "0.1.24" description: esp modem url: https://github.com/espressif/esp-protocols/tree/master/components/esp_modem dependencies: