Skip to content

Commit 7505c99

Browse files
committed
Merge remote-tracking branch 'arduino/wifi_r4_debug_refactor' into HEAD
2 parents ec21701 + 2a6f0d4 commit 7505c99

File tree

2 files changed

+79
-48
lines changed

2 files changed

+79
-48
lines changed

libraries/WiFiS3/src/Modem.cpp

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,19 @@ bool ModemClass::passthrough(const uint8_t *data, size_t size) {
6969
break;
7070
}
7171
}
72-
}
73-
#ifdef MODEM_DEBUG_PASSTHROUGH
74-
Serial.print(" passthrough, rx |>>");
75-
Serial.print(data_res.c_str());
76-
Serial.println("<<|");
72+
}
73+
74+
if(_serial_debug && _debug_level >= 2) {
75+
_serial_debug->print(" ANSWER (passthrough): ");
76+
_serial_debug->println(data_res.c_str());
7777
if(res) {
78-
Serial.println(" Result: OK");
78+
_serial_debug->println(" Result: OK");
7979
}
8080
else {
81-
Serial.println(" Result: FAILED");
82-
}
83-
#endif
81+
_serial_debug->println(" Result: FAILED");
82+
}
83+
}
84+
8485
return res;
8586
}
8687

@@ -92,11 +93,13 @@ void ModemClass::write_nowait(const string &cmd, string &str, char * fmt, ...) {
9293
va_start (va, fmt);
9394
vsprintf ((char *)tx_buff, fmt, va);
9495
va_end (va);
95-
#ifdef MODEM_DEBUG
96-
Serial.print(" Write Call no wait, command sent: ");
97-
Serial.write(tx_buff,strlen((char *)tx_buff));
98-
Serial.println();
99-
#endif
96+
97+
if(_serial_debug && _debug_level >= 2) {
98+
_serial_debug->print("REQUEST (passthrough): ");
99+
_serial_debug->write(tx_buff,strlen((char *)tx_buff));
100+
_serial_debug->println();
101+
}
102+
100103
_serial->write(tx_buff,strlen((char *)tx_buff));
101104
return;
102105
}
@@ -105,25 +108,22 @@ void ModemClass::write_nowait(const string &cmd, string &str, char * fmt, ...) {
105108
/* -------------------------------------------------------------------------- */
106109
bool ModemClass::write(const string &prompt, string &data_res, char * fmt, ...){
107110
/* -------------------------------------------------------------------------- */
108-
data_res.clear();
109-
memset(tx_buff,0x00,MAX_BUFF_SIZE);
110-
va_list va;
111-
va_start (va, fmt);
112-
vsprintf ((char *)tx_buff, fmt, va);
113-
va_end (va);
114-
#ifdef MODEM_DEBUG
115-
Serial.println();
116-
Serial.println("###>");
117-
Serial.print("READ BY SIZE: ");
118-
Serial.println((int)read_by_size);
119-
Serial.print(" Write Call, command sent: ");
120-
Serial.write(tx_buff,strlen((char *)tx_buff));
121-
Serial.println();
111+
data_res.clear();
112+
memset(tx_buff,0x00,MAX_BUFF_SIZE);
113+
va_list va;
114+
va_start (va, fmt);
115+
vsprintf ((char *)tx_buff, fmt, va);
116+
va_end (va);
117+
118+
if(_serial_debug) {
119+
_serial_debug->println();
120+
_serial_debug->print("REQUEST: ");
121+
_serial_debug->write(tx_buff,strlen((char *)tx_buff));
122+
_serial_debug->println();
123+
}
122124

123-
Serial.println("<###");
124-
#endif
125-
_serial->write(tx_buff,strlen((char *)tx_buff));
126-
return buf_read(prompt,data_res);;
125+
_serial->write(tx_buff,strlen((char *)tx_buff));
126+
return buf_read(prompt,data_res);;
127127
}
128128

129129

@@ -200,16 +200,20 @@ bool ModemClass::buf_read(const string &prompt, string &data_res) {
200200
bool res = false;
201201
bool found = false;
202202

203+
if(_serial_debug && _debug_level >= 1) {
204+
_serial_debug->print("RAW: ");
205+
}
206+
203207
unsigned long start_time = millis();
204208
while((millis() - start_time < _timeout) && !found){
205209
while( _serial->available() ){
206210
char c = _serial->read();
207211
data_res += c;
208-
#ifdef SELECTABLE_MODEM_DEBUG
209-
if(enable_dbg) {
210-
Serial.print(c);
212+
213+
if(_serial_debug && _debug_level >= 1) {
214+
_serial_debug->print(c);
211215
}
212-
#endif
216+
213217

214218
if(read_by_size) {
215219
if(read_by_size_finished(data_res)) {
@@ -264,18 +268,24 @@ bool ModemClass::buf_read(const string &prompt, string &data_res) {
264268
}
265269
trim_results = true;
266270
read_by_size = false;
267-
#ifdef MODEM_DEBUG
268-
Serial.print(" Write Call, response rx |>>");
269-
Serial.print(data_res.c_str());
270-
Serial.println("<<|");
271+
272+
if(_serial_debug && _debug_level >= 1) {
273+
_serial_debug->print("<-RAW END");
274+
_serial_debug->println();
275+
}
276+
277+
if(_serial_debug) {
278+
_serial_debug->print(" ANSWER: ");
279+
_serial_debug->println(data_res.c_str());
271280
if(res) {
272-
Serial.println(" Result: OK");
281+
_serial_debug->println(" Result: OK");
273282
}
274283
else {
275-
Serial.println(" Result: FAILED");
276-
}
277-
#endif
278-
284+
_serial_debug->println(" Result: FAILED");
285+
}
286+
}
287+
288+
279289
return res;
280290
}
281291

libraries/WiFiS3/src/Modem.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
#include "StringHelpers.h"
77

88

9-
//#define MODEM_DEBUG
10-
//#define MODEM_DEBUG_PASSTHROUGH
9+
/* uncomment this will allow debug for passthrough "binary" commands */
10+
#define MODEM_DEBUG_PASSTHROUGH
11+
1112
#define MODEM_TIMEOUT 10000
12-
//#define SELECTABLE_MODEM_DEBUG
1313
#define MAX_BUFF_SIZE 64
1414

1515
#define DO_NOT_CHECK_CMD "NO_CMD_CHECK"
@@ -38,6 +38,25 @@ class ModemClass {
3838
}
3939
bool beginned;
4040

41+
/* calling this function with no argument will enable debug message to be printed
42+
on Serial
43+
use first parameter UART *u to redirect debug output to a different serial
44+
45+
level from 0 defaul to 2 (maximum) */
46+
47+
void debug(Stream &u, uint8_t level = 0) {
48+
_serial_debug = &u;
49+
50+
if(level > 2) {
51+
level = 2;
52+
}
53+
_debug_level = level;
54+
}
55+
56+
void noDebug() {
57+
_serial_debug = nullptr;
58+
}
59+
4160
#ifdef SELECTABLE_MODEM_DEBUG
4261
bool enable_dbg = false;
4362
void debug(bool e) {enable_dbg = e;}
@@ -52,6 +71,8 @@ class ModemClass {
5271
bool trim_results;
5372
bool read_by_size;
5473
bool read_by_size_finished(std::string &rx);
74+
Stream * _serial_debug;
75+
uint8_t _debug_level = 0;
5576
};
5677

5778
extern ModemClass modem;

0 commit comments

Comments
 (0)