From 4db49189f572d7441776b976f781ee23168d6b4b Mon Sep 17 00:00:00 2001 From: maidnl Date: Wed, 5 Jul 2023 17:11:10 +0200 Subject: [PATCH 1/2] refactoring debug for uno R4 wifi --- libraries/WiFiS3/src/Modem.cpp | 129 ++++++++++++++++++++------------- libraries/WiFiS3/src/Modem.h | 31 +++++++- 2 files changed, 105 insertions(+), 55 deletions(-) diff --git a/libraries/WiFiS3/src/Modem.cpp b/libraries/WiFiS3/src/Modem.cpp index b1905e503..d4050adb8 100644 --- a/libraries/WiFiS3/src/Modem.cpp +++ b/libraries/WiFiS3/src/Modem.cpp @@ -69,18 +69,19 @@ bool ModemClass::passthrough(const uint8_t *data, size_t size) { break; } } - } - #ifdef MODEM_DEBUG_PASSTHROUGH - Serial.print(" passthrough, rx |>>"); - Serial.print(data_res.c_str()); - Serial.println("<<|"); + } + + if(_serial_debug && _debug_level >= 2) { + _serial_debug->print(" ANSWER (passthrough): "); + _serial_debug->println(data_res.c_str()); if(res) { - Serial.println(" Result: OK"); + _serial_debug->println(" Result: OK"); } else { - Serial.println(" Result: FAILED"); - } - #endif + _serial_debug->println(" Result: FAILED"); + } + } + return res; } @@ -92,11 +93,13 @@ void ModemClass::write_nowait(const string &cmd, string &str, char * fmt, ...) { va_start (va, fmt); vsprintf ((char *)tx_buff, fmt, va); va_end (va); - #ifdef MODEM_DEBUG - Serial.print(" Write Call no wait, command sent: "); - Serial.write(tx_buff,strlen((char *)tx_buff)); - Serial.println(); - #endif + + if(_serial_debug && _debug_level >= 2) { + _serial_debug->print("REQUEST (passthrough): "); + _serial_debug->write(tx_buff,strlen((char *)tx_buff)); + _serial_debug->println(); + } + _serial->write(tx_buff,strlen((char *)tx_buff)); return; } @@ -105,25 +108,22 @@ void ModemClass::write_nowait(const string &cmd, string &str, char * fmt, ...) { /* -------------------------------------------------------------------------- */ bool ModemClass::write(const string &prompt, string &data_res, char * fmt, ...){ /* -------------------------------------------------------------------------- */ - data_res.clear(); - memset(tx_buff,0x00,MAX_BUFF_SIZE); - va_list va; - va_start (va, fmt); - vsprintf ((char *)tx_buff, fmt, va); - va_end (va); - #ifdef MODEM_DEBUG - Serial.println(); - Serial.println("###>"); - Serial.print("READ BY SIZE: "); - Serial.println((int)read_by_size); - Serial.print(" Write Call, command sent: "); - Serial.write(tx_buff,strlen((char *)tx_buff)); - Serial.println(); + data_res.clear(); + memset(tx_buff,0x00,MAX_BUFF_SIZE); + va_list va; + va_start (va, fmt); + vsprintf ((char *)tx_buff, fmt, va); + va_end (va); + + if(_serial_debug) { + _serial_debug->println(); + _serial_debug->print("REQUEST: "); + _serial_debug->write(tx_buff,strlen((char *)tx_buff)); + _serial_debug->println(); + } - Serial.println("<###"); - #endif - _serial->write(tx_buff,strlen((char *)tx_buff)); - return buf_read(prompt,data_res);; + _serial->write(tx_buff,strlen((char *)tx_buff)); + return buf_read(prompt,data_res);; } @@ -156,12 +156,22 @@ bool ModemClass::read_by_size_finished(string &rx) { int pos_space = rx.find(" "); if(pos != string::npos && pos_space != string::npos) { string n = rx.substr(pos_space,pos); - /* add 4 because OK\r\n is always added at the end of data */ - data_to_be_received = atoi(n.c_str()) + 4; - rx.clear(); - data_received = 0; - st = WAIT_FOR_DATA; - + int to_be_rx = atoi(n.c_str()); + if(to_be_rx <= 0) { + while( _serial->available() ){ + _serial->read(); + } + rv = true; + first_call = true; + st = IDLE; + } + else { + /* add 4 because OK\r\n is always added at the end of data */ + data_to_be_received = to_be_rx + 4; + data_received = 0; + st = WAIT_FOR_DATA; + } + rx.clear(); } } break; @@ -190,23 +200,32 @@ bool ModemClass::buf_read(const string &prompt, string &data_res) { bool res = false; bool found = false; + if(_serial_debug && _debug_level >= 1) { + _serial_debug->print("RAW: "); + } + unsigned long start_time = millis(); while((millis() - start_time < _timeout) && !found){ while( _serial->available() ){ char c = _serial->read(); data_res += c; - #ifdef SELECTABLE_MODEM_DEBUG - if(enable_dbg) { - Serial.print(c); + + if(_serial_debug && _debug_level >= 1) { + _serial_debug->print(c); } - #endif + if(read_by_size) { if(read_by_size_finished(data_res)) { found = true; read_by_size = false; res = true; - data_res = data_res.substr(0, data_res.length() - (sizeof(RESULT_OK) - 1)); + if(data_res.size() > 0) { + data_res = data_res.substr(0, data_res.length() - (sizeof(RESULT_OK) - 1)); + } + else { + break; + } } } else { @@ -249,18 +268,24 @@ bool ModemClass::buf_read(const string &prompt, string &data_res) { } trim_results = true; read_by_size = false; - #ifdef MODEM_DEBUG - Serial.print(" Write Call, response rx |>>"); - Serial.print(data_res.c_str()); - Serial.println("<<|"); + + if(_serial_debug && _debug_level >= 1) { + _serial_debug->print("<-RAW END"); + _serial_debug->println(); + } + + if(_serial_debug) { + _serial_debug->print(" ANSWER: "); + _serial_debug->println(data_res.c_str()); if(res) { - Serial.println(" Result: OK"); + _serial_debug->println(" Result: OK"); } else { - Serial.println(" Result: FAILED"); - } - #endif - + _serial_debug->println(" Result: FAILED"); + } + } + + return res; } diff --git a/libraries/WiFiS3/src/Modem.h b/libraries/WiFiS3/src/Modem.h index 666756afa..52850dc7d 100644 --- a/libraries/WiFiS3/src/Modem.h +++ b/libraries/WiFiS3/src/Modem.h @@ -6,10 +6,10 @@ #include "StringHelpers.h" -//#define MODEM_DEBUG -//#define MODEM_DEBUG_PASSTHROUGH +/* uncomment this will allow debug for passthrough "binary" commands */ +#define MODEM_DEBUG_PASSTHROUGH + #define MODEM_TIMEOUT 10000 -//#define SELECTABLE_MODEM_DEBUG #define MAX_BUFF_SIZE 64 #define DO_NOT_CHECK_CMD "NO_CMD_CHECK" @@ -38,6 +38,29 @@ class ModemClass { } bool beginned; + /* calling this function with no argument will enable debug message to be printed + on Serial + use first parameter UART *u to redirect debug output to a different serial + + level from 0 defaul to 2 (maximum) */ + + void debug(UART *u = nullptr, uint8_t level = 0) { + if(u == nullptr) { + _serial_debug = &Serial; + } + else { + _serial_debug = u; + } + if(level > 2) { + level = 2; + } + _debug_level = level; + } + + void noDebug() { + _serial_debug = nullptr; + } + #ifdef SELECTABLE_MODEM_DEBUG bool enable_dbg = false; void debug(bool e) {enable_dbg = e;} @@ -52,6 +75,8 @@ class ModemClass { bool trim_results; bool read_by_size; bool read_by_size_finished(std::string &rx); + UART * _serial_debug; + uint8_t _debug_level = 0; }; extern ModemClass modem; From 2a6f0d47416ac732e69c43c779440a607d2f2755 Mon Sep 17 00:00:00 2001 From: maidnl Date: Wed, 5 Jul 2023 17:46:25 +0200 Subject: [PATCH 2/2] change debug from Serial to Stream --- libraries/WiFiS3/src/Modem.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/libraries/WiFiS3/src/Modem.h b/libraries/WiFiS3/src/Modem.h index 52850dc7d..39bfd8150 100644 --- a/libraries/WiFiS3/src/Modem.h +++ b/libraries/WiFiS3/src/Modem.h @@ -44,13 +44,9 @@ class ModemClass { level from 0 defaul to 2 (maximum) */ - void debug(UART *u = nullptr, uint8_t level = 0) { - if(u == nullptr) { - _serial_debug = &Serial; - } - else { - _serial_debug = u; - } + void debug(Stream &u, uint8_t level = 0) { + _serial_debug = &u; + if(level > 2) { level = 2; } @@ -75,7 +71,7 @@ class ModemClass { bool trim_results; bool read_by_size; bool read_by_size_finished(std::string &rx); - UART * _serial_debug; + Stream * _serial_debug; uint8_t _debug_level = 0; };