Skip to content

refactoring debug for uno R4 wifi #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 77 additions & 52 deletions libraries/WiFiS3/src/Modem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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);;
}


Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

Expand Down
27 changes: 24 additions & 3 deletions libraries/WiFiS3/src/Modem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -38,6 +38,25 @@ 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(Stream &u, uint8_t level = 0) {
_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;}
Expand All @@ -52,6 +71,8 @@ class ModemClass {
bool trim_results;
bool read_by_size;
bool read_by_size_finished(std::string &rx);
Stream * _serial_debug;
uint8_t _debug_level = 0;
};

extern ModemClass modem;
Expand Down