Skip to content

Commit 8cbcc55

Browse files
authored
Merge pull request #10121 from kivaisan/improve_at_trace
Cellular: Improve ATHandler AT debug traces
2 parents 1d26dbb + aff3815 commit 8cbcc55

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

features/cellular/framework/AT/ATHandler.cpp

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ bool ATHandler::fill_buffer(bool wait_for_timeout)
368368
// Reset buffer when full
369369
if (sizeof(_recv_buff) == _recv_len) {
370370
tr_error("AT overflow");
371-
debug_print(_recv_buff, _recv_len);
371+
debug_print(_recv_buff, _recv_len, AT_ERR);
372372
reset_buffer();
373373
}
374374

@@ -379,7 +379,7 @@ bool ATHandler::fill_buffer(bool wait_for_timeout)
379379
if (count > 0 && (fhs.revents & POLLIN)) {
380380
ssize_t len = _fileHandle->read(_recv_buff + _recv_len, sizeof(_recv_buff) - _recv_len);
381381
if (len > 0) {
382-
debug_print(_recv_buff + _recv_len, len);
382+
debug_print(_recv_buff + _recv_len, len, AT_RX);
383383
_recv_len += len;
384384
return true;
385385
}
@@ -481,7 +481,6 @@ ssize_t ATHandler::read_bytes(uint8_t *buf, size_t len)
481481
}
482482
buf[read_len] = c;
483483
if (_debug_on && read_len >= DEBUG_MAXLEN) {
484-
debug_print(DEBUG_END_MARK, sizeof(DEBUG_END_MARK) - 1);
485484
_debug_on = false;
486485
}
487486
}
@@ -586,7 +585,6 @@ ssize_t ATHandler::read_hex_string(char *buf, size_t size)
586585
int c = get_char();
587586

588587
if (_debug_on && read_idx >= DEBUG_MAXLEN) {
589-
debug_print(DEBUG_END_MARK, sizeof(DEBUG_END_MARK) - 1);
590588
_debug_on = false;
591589
}
592590

@@ -1231,9 +1229,8 @@ size_t ATHandler::write(const void *data, size_t len)
12311229
}
12321230
if (_debug_on && write_len < DEBUG_MAXLEN) {
12331231
if (write_len + ret < DEBUG_MAXLEN) {
1234-
debug_print((char *)data + write_len, ret);
1232+
debug_print((char *)data + write_len, ret, AT_TX);
12351233
} else {
1236-
debug_print(DEBUG_END_MARK, sizeof(DEBUG_END_MARK) - 1);
12371234
_debug_on = false;
12381235
}
12391236
}
@@ -1287,30 +1284,45 @@ void ATHandler::flush()
12871284
}
12881285
}
12891286

1290-
void ATHandler::debug_print(const char *p, int len)
1287+
void ATHandler::debug_print(const char *p, int len, ATType type)
12911288
{
12921289
#if MBED_CONF_CELLULAR_DEBUG_AT
12931290
if (_debug_on) {
1294-
#if MBED_CONF_MBED_TRACE_ENABLE
1295-
mbed_cellular_trace::mutex_wait();
1296-
#endif
1297-
char c;
1298-
for (ssize_t i = 0; i < len; i++) {
1299-
c = *p++;
1300-
if (!isprint(c)) {
1301-
if (c == '\r') {
1302-
debug("\n");
1291+
const int buf_size = len * 4 + 1; // x4 -> reserve space for extra characters, +1 -> terminating null
1292+
char *buffer = (char *)malloc(buf_size);
1293+
if (buffer) {
1294+
memset(buffer, 0, buf_size);
1295+
1296+
char *pbuf = buffer;
1297+
for (ssize_t i = 0; i < len; i++) {
1298+
const char c = *p++;
1299+
if (isprint(c)) {
1300+
*pbuf++ = c;
1301+
} else if (c == '\r') {
1302+
sprintf(pbuf, "<cr>");
1303+
pbuf += 4;
13031304
} else if (c == '\n') {
1305+
sprintf(pbuf, "<ln>");
1306+
pbuf += 4;
13041307
} else {
1305-
debug("#%02x", c);
1308+
sprintf(pbuf, "<%02X>", c);
1309+
pbuf += 4;
13061310
}
1311+
}
1312+
MBED_ASSERT((int)(pbuf - buffer) <= buf_size); // Check for buffer overflow
1313+
1314+
if (type == AT_RX) {
1315+
tr_info("AT RX (%2d): %s", len, buffer);
1316+
} else if (type == AT_TX) {
1317+
tr_info("AT TX (%2d): %s", len, buffer);
13071318
} else {
1308-
debug("%c", c);
1319+
tr_info("AT ERR (%2d): %s", len, buffer);
13091320
}
1321+
1322+
free(buffer);
1323+
} else {
1324+
tr_error("AT trace unable to allocate buffer!");
13101325
}
1311-
#if MBED_CONF_MBED_TRACE_ENABLE
1312-
mbed_cellular_trace::mutex_release();
1313-
#endif
13141326
}
13151327
#endif // MBED_CONF_CELLULAR_DEBUG_AT
13161328
}

features/cellular/framework/AT/ATHandler.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,12 @@ class ATHandler {
571571
bool find_urc_handler(const char *prefix);
572572

573573
// print contents of a buffer to trace log
574-
void debug_print(const char *p, int len);
574+
enum ATType {
575+
AT_ERR,
576+
AT_RX,
577+
AT_TX
578+
};
579+
void debug_print(const char *p, int len, ATType type);
575580
};
576581

577582
} // namespace mbed

0 commit comments

Comments
 (0)