@@ -368,7 +368,7 @@ bool ATHandler::fill_buffer(bool wait_for_timeout)
368
368
// Reset buffer when full
369
369
if (sizeof (_recv_buff) == _recv_len) {
370
370
tr_error (" AT overflow" );
371
- debug_print (_recv_buff, _recv_len);
371
+ debug_print (_recv_buff, _recv_len, AT_ERR );
372
372
reset_buffer ();
373
373
}
374
374
@@ -379,7 +379,7 @@ bool ATHandler::fill_buffer(bool wait_for_timeout)
379
379
if (count > 0 && (fhs.revents & POLLIN)) {
380
380
ssize_t len = _fileHandle->read (_recv_buff + _recv_len, sizeof (_recv_buff) - _recv_len);
381
381
if (len > 0 ) {
382
- debug_print (_recv_buff + _recv_len, len);
382
+ debug_print (_recv_buff + _recv_len, len, AT_RX );
383
383
_recv_len += len;
384
384
return true ;
385
385
}
@@ -481,7 +481,6 @@ ssize_t ATHandler::read_bytes(uint8_t *buf, size_t len)
481
481
}
482
482
buf[read_len] = c;
483
483
if (_debug_on && read_len >= DEBUG_MAXLEN) {
484
- debug_print (DEBUG_END_MARK, sizeof (DEBUG_END_MARK) - 1 );
485
484
_debug_on = false ;
486
485
}
487
486
}
@@ -586,7 +585,6 @@ ssize_t ATHandler::read_hex_string(char *buf, size_t size)
586
585
int c = get_char ();
587
586
588
587
if (_debug_on && read_idx >= DEBUG_MAXLEN) {
589
- debug_print (DEBUG_END_MARK, sizeof (DEBUG_END_MARK) - 1 );
590
588
_debug_on = false ;
591
589
}
592
590
@@ -1231,9 +1229,8 @@ size_t ATHandler::write(const void *data, size_t len)
1231
1229
}
1232
1230
if (_debug_on && write_len < DEBUG_MAXLEN) {
1233
1231
if (write_len + ret < DEBUG_MAXLEN) {
1234
- debug_print ((char *)data + write_len, ret);
1232
+ debug_print ((char *)data + write_len, ret, AT_TX );
1235
1233
} else {
1236
- debug_print (DEBUG_END_MARK, sizeof (DEBUG_END_MARK) - 1 );
1237
1234
_debug_on = false ;
1238
1235
}
1239
1236
}
@@ -1287,30 +1284,45 @@ void ATHandler::flush()
1287
1284
}
1288
1285
}
1289
1286
1290
- void ATHandler::debug_print (const char *p, int len)
1287
+ void ATHandler::debug_print (const char *p, int len, ATType type )
1291
1288
{
1292
1289
#if MBED_CONF_CELLULAR_DEBUG_AT
1293
1290
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 ;
1303
1304
} else if (c == ' \n ' ) {
1305
+ sprintf (pbuf, " <ln>" );
1306
+ pbuf += 4 ;
1304
1307
} else {
1305
- debug (" #%02x" , c);
1308
+ sprintf (pbuf, " <%02X>" , c);
1309
+ pbuf += 4 ;
1306
1310
}
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);
1307
1318
} else {
1308
- debug ( " %c " , c );
1319
+ tr_info ( " AT ERR (%2d): %s " , len, buffer );
1309
1320
}
1321
+
1322
+ free (buffer);
1323
+ } else {
1324
+ tr_error (" AT trace unable to allocate buffer!" );
1310
1325
}
1311
- #if MBED_CONF_MBED_TRACE_ENABLE
1312
- mbed_cellular_trace::mutex_release ();
1313
- #endif
1314
1326
}
1315
1327
#endif // MBED_CONF_CELLULAR_DEBUG_AT
1316
1328
}
0 commit comments