From 599efa83c8aef9dbbc8724d4dea425c1ef614adb Mon Sep 17 00:00:00 2001 From: alkonosst Date: Sun, 23 Nov 2025 23:47:15 -0300 Subject: [PATCH] feat(Modem): Add support for verbose error codes in logging --- README.md | 11 +++++++++++ platformio.ini | 3 ++- src/SIM7600Log.h | 5 +++++ src/SIM7600Modem.cpp | 39 +++++++++++++++++++++++++++++---------- src/SIM7600Modem.h | 1 + 5 files changed, 48 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 7ba1de4..8ae0e88 100644 --- a/README.md +++ b/README.md @@ -287,6 +287,16 @@ If you are using an `ESP32` platform, you can enable the native `ESP_LOGX()` log #include "SIM7600Modem.h" ``` +If you need verbose error codes (`+CME ERROR:` and `+CMS ERROR:`), you can define the +`SIM7600_LOG_VERBOSE_ERROR_CODES` macro. You need to set the log level to `1` (ERROR) or higher: + +```cpp +// Enable verbose error codes +#define SIM7600_LOG_VERBOSE_ERROR_CODES +#define SIM7600_LOG_LEVEL 3 +#include "SIM7600Modem.h" +``` + If you need a custom log function, you can define the `SIM7600_LOG_CUSTOM` to a custom function with the following signature: @@ -317,6 +327,7 @@ build_flags = -DSIM7600_LOG_BUFFER_SIZE_B=512 -DSIM7600_USE_ESP32_LOGS -DSIM7600_LOG_CUSTOM=logFunction + -DSIM7600_LOG_VERBOSE_ERROR_CODES ``` ## Modem features diff --git a/platformio.ini b/platformio.ini index 768e74d..565859f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -52,4 +52,5 @@ build_flags = -DARDUINO_USB_CDC_ON_BOOT=1 ; Enable library debug (for ESP32) - -DSIM7600_LOG_LEVEL=5 \ No newline at end of file + -DSIM7600_LOG_LEVEL=5 + -DSIM7600_LOG_VERBOSE_ERROR_CODES \ No newline at end of file diff --git a/src/SIM7600Log.h b/src/SIM7600Log.h index 92eee63..d657640 100644 --- a/src/SIM7600Log.h +++ b/src/SIM7600Log.h @@ -24,6 +24,11 @@ #define SIM7600_LOG_STREAM Serial #endif +// Enable verbose error codes, only printed when log level is ERROR or higher +#ifndef SIM7600_LOG_VERBOSE_ERROR_CODES +#define SIM7600_LOG_VERBOSE_ERROR_CODES 0 +#endif + // Log buffer size #ifndef SIM7600_LOG_BUFFER_SIZE_B #define SIM7600_LOG_BUFFER_SIZE_B 256 diff --git a/src/SIM7600Modem.cpp b/src/SIM7600Modem.cpp index 077c5a0..05d5c49 100644 --- a/src/SIM7600Modem.cpp +++ b/src/SIM7600Modem.cpp @@ -77,10 +77,17 @@ Status Modem::init(const char* pin, const uint32_t timeout_ms) { status = sendATCmdAndWaitResp("ATE0", AT_OK); if (status != Status::Success) return status; +#if SIM7600_LOG_VERBOSE_ERROR_CODES + // Enable verbose errors + SIM7600_LOGD(tag, "Enabling verbose errors"); + status = sendATCmdAndWaitResp("AT+CMEE=2", AT_OK); + if (status != Status::Success) return status; +#else // Disable verbose errors SIM7600_LOGD(tag, "Disabling verbose errors"); status = sendATCmdAndWaitResp("AT+CMEE=0", AT_OK); if (status != Status::Success) return status; +#endif // Enable CGREG URCs SIM7600_LOGD(tag, "Enabling CGREG URCs"); @@ -201,11 +208,8 @@ Status Modem::waitForResponse(const char* expected_response, const uint32_t time return Status::Success; } - // Error response - if (strcmp(_rx_buf, AT_ERROR) == 0) { - SIM7600_LOGW(tag, "Received AT error response"); - return Status::Error; - } + // Check for error response + if (_receivedErrorResponse()) return Status::Error; } SIM7600_LOGE(tag, "Timed out expecting response"); @@ -235,11 +239,8 @@ Status Modem::waitForResponses(const char** expected_responses, const uint8_t re } } - // Error response - if (strcmp(_rx_buf, AT_ERROR) == 0) { - SIM7600_LOGW(tag, "Received AT error response"); - return Status::Error; - } + // Check for error response + if (_receivedErrorResponse()) return Status::Error; } SIM7600_LOGE(tag, "Timed out expecting %u responses", response_count); @@ -1197,6 +1198,24 @@ Status Modem::_waitAsyncMQTTResponse(const char* mqtt_response_prefix, uint8_t& return Status::Success; } +bool Modem::_receivedErrorResponse() { +#if SIM7600_LOG_VERBOSE_ERROR_CODES + // Verbose error response + if ((strncmp(_rx_buf, "+CME ERROR:", 11) == 0) || (strncmp(_rx_buf, "+CMS ERROR:", 11) == 0)) { + SIM7600_LOGE(tag, "<< [Verbose Error]: %s", _rx_buf); + return true; + } +#endif + + // Error response + if (strcmp(_rx_buf, AT_ERROR) == 0) { + SIM7600_LOGW(tag, "Received AT error response"); + return true; + } + + return false; +} + bool Modem::_handleURCs() { // First, check registered handlers (TCPClient, MQTTClient) for (uint8_t i = 0; i < _urc_handler_count; i++) { diff --git a/src/SIM7600Modem.h b/src/SIM7600Modem.h index 22165d0..eed65b9 100644 --- a/src/SIM7600Modem.h +++ b/src/SIM7600Modem.h @@ -542,6 +542,7 @@ class Modem { Status _waitPromptAndSendData(const uint8_t* data, const size_t data_length, size_t& bytes_sent, const uint32_t timeout_ms = SIM7600_MODEM_DEFAULT_TIMEOUT_MS); Status _waitAsyncMQTTResponse(const char* mqtt_response_prefix, uint8_t& err_code); + bool _receivedErrorResponse(); bool _handleURCs();