Skip to content

Commit fd421dd

Browse files
committed
debug.c: Make coap_show_pdu() use coap_log() or fprintf()
coap_show_pdu()'s API is now coap_show_pdu(coap_log_t, coap_pdu_t*) New function void coap_set_show_pdu_output(int use_fprintf) Default is use fprintf() instead of coap_log() The output buffer size can be configuring -DCOAP_DEBUG_BUF_SIZE Output COAP_OPTION_SIZE2 option as well For binary data, output ascii readable equivalent immediately under the hex output for ease of debugging. include/coap/debug.h src/debug.c Update coap_show_pdu() API New function coap_set_show_pdu_output() Update coap_show_pdu() code include/coap/pdu.h Define the coap debug output debug buffer size COAP_DEBUG_BUF_SIZE examples/client.c examples/contiki/coap-observer.c src/net.c tests/test_wellknown.c tests/test_pdu.c Update coap_show_pdu() calls libcoap-2.map libcoap-2.sym New function coap_set_show_pdu_output()
1 parent bf4323e commit fd421dd

File tree

10 files changed

+129
-37
lines changed

10 files changed

+129
-37
lines changed

examples/client.c

+7-12
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,7 @@ clear_obs(coap_context_t *ctx, coap_session_t *session) {
211211
}
212212
}
213213

214-
if (LOG_INFO <= coap_get_log_level())
215-
coap_show_pdu(pdu);
214+
coap_show_pdu(LOG_INFO, pdu);
216215

217216
tid = coap_send(session, pdu);
218217

@@ -297,11 +296,9 @@ message_handler(struct coap_context_t *ctx,
297296
coap_tid_t tid;
298297

299298
#ifndef NDEBUG
300-
if (LOG_INFO <= coap_get_log_level()) {
301-
coap_log(LOG_DEBUG, "** process incoming %d.%02d response:\n",
302-
(received->code >> 5), received->code & 0x1F);
303-
coap_show_pdu(received);
304-
}
299+
coap_log(LOG_DEBUG, "** process incoming %d.%02d response:\n",
300+
(received->code >> 5), received->code & 0x1F);
301+
coap_show_pdu(LOG_INFO, received);
305302
#endif
306303

307304
/* check if this is a response to our original request */
@@ -456,7 +453,7 @@ message_handler(struct coap_context_t *ctx,
456453
payload.s,
457454
block.num,
458455
block.szx);
459-
coap_show_pdu(pdu);
456+
coap_show_pdu(LOG_WARNING, pdu);
460457

461458
tid = coap_send(session, pdu);
462459

@@ -1278,10 +1275,8 @@ main(int argc, char **argv) {
12781275
}
12791276

12801277
#ifndef NDEBUG
1281-
if (LOG_INFO <= coap_get_log_level()) {
1282-
coap_log(LOG_DEBUG, "sending CoAP request:\n");
1283-
coap_show_pdu(pdu);
1284-
}
1278+
coap_log(LOG_DEBUG, "sending CoAP request:\n");
1279+
coap_show_pdu(LOG_INFO, pdu);
12851280
#endif
12861281

12871282
coap_send(session, pdu);

examples/contiki/coap-observer.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ message_handler(struct coap_context_t *ctx,
103103

104104
debug("** process incoming %d.%02d response:\n",
105105
(received->hdr->code >> 5), received->hdr->code & 0x1F);
106-
coap_show_pdu(received);
106+
coap_show_pdu(LOG_WARNING, received);
107107

108108
coap_ticks(&last_seen);
109109
}

include/coap/debug.h

+16-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,22 @@ void coap_log_impl(coap_log_t level, const char *format, ...);
7474
#define debug(...) coap_log(LOG_DEBUG, __VA_ARGS__)
7575

7676
#include "pdu.h"
77-
void coap_show_pdu(const coap_pdu_t *);
77+
78+
/**
79+
* Defines the output mode for the coap_show_pdu() function.
80+
*
81+
* @param use_printf 1 if the output is to use fprintf() (the default)
82+
* 0 if the output is to use coap_log()
83+
*/
84+
void coap_set_show_pdu_output(int use_fprintf);
85+
86+
/**
87+
* Display the contents of the specified @p pdu.
88+
*
89+
* @param level The required minimum logging level
90+
* @param pdu The PDU to decode
91+
*/
92+
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu);
7893

7994
struct coap_address_t;
8095
size_t coap_print_addr(const struct coap_address_t *, unsigned char *, size_t);

include/coap/pdu.h

+9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ struct coap_session_t;
5252
#endif
5353
#endif /* COAP_DEFAULT_MAX_PDU_RX_SIZE */
5454

55+
#ifndef COAP_DEBUG_BUF_SIZE
56+
#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
57+
#define COAP_DEBUG_BUF_SIZE 128
58+
#else /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
59+
/* 1024 derived from RFC7252 4.6. Message Size max payload */
60+
#define COAP_DEBUG_BUF_SIZE (8 + 1024 * 2)
61+
#endif /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
62+
#endif /* COAP_DEBUG_BUF_SIZE */
63+
5564
#define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */
5665
#define COAP_DEFAULT_SCHEME "coap" /* the default scheme for CoAP URIs */
5766

libcoap-2.map

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ global:
167167
coap_set_event_handler;
168168
coap_set_log_handler;
169169
coap_set_log_level;
170+
coap_set_show_pdu_output;
170171
coap_show_pdu;
171172
coap_socket_strerror;
172173
coap_split_path;

libcoap-2.sym

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ coap_set_app_data
165165
coap_set_event_handler
166166
coap_set_log_handler
167167
coap_set_log_level
168+
coap_set_show_pdu_output
168169
coap_show_pdu
169170
coap_socket_strerror
170171
coap_split_path

src/debug.c

+89-17
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252

5353
static coap_log_t maxlog = LOG_WARNING; /* default maximum log level */
5454

55+
static int use_fprintf_for_show_pdu = 1; /* non zero to output with fprintf */
56+
5557
const char *coap_package_name(void) {
5658
return PACKAGE_NAME;
5759
}
@@ -60,6 +62,11 @@ const char *coap_package_version(void) {
6062
return PACKAGE_STRING;
6163
}
6264

65+
void
66+
coap_set_show_pdu_output(int use_fprintf) {
67+
use_fprintf_for_show_pdu = use_fprintf;
68+
}
69+
6370
coap_log_t
6471
coap_get_log_level(void) {
6572
return maxlog;
@@ -316,6 +323,7 @@ msg_option_string(uint8_t code, uint16_t option_type) {
316323
{ COAP_OPTION_PROXY_URI, "Proxy-Uri" },
317324
{ COAP_OPTION_PROXY_SCHEME, "Proxy-Scheme" },
318325
{ COAP_OPTION_SIZE1, "Size1" },
326+
{ COAP_OPTION_SIZE2, "Size2" },
319327
{ COAP_OPTION_NORESPONSE, "No-Response" }
320328
};
321329

@@ -431,8 +439,18 @@ is_binary(int content_format) {
431439
content_format == COAP_MEDIATYPE_APPLICATION_JSON);
432440
}
433441

442+
#define COAP_DO_SHOW_OUTPUT_LINE \
443+
do { \
444+
if (use_fprintf_for_show_pdu) { \
445+
fprintf(COAP_DEBUG_FD, "%s", outbuf); \
446+
} \
447+
else { \
448+
coap_log(level, "%s", outbuf); \
449+
} \
450+
} while (0)
451+
434452
void
435-
coap_show_pdu(const coap_pdu_t *pdu) {
453+
coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) {
436454
unsigned char buf[1024]; /* need some space for output creation */
437455
size_t buf_len = 0; /* takes the number of bytes written to buf */
438456
int encode = 0, have_options = 0, i;
@@ -441,25 +459,36 @@ coap_show_pdu(const coap_pdu_t *pdu) {
441459
int content_format = -1;
442460
size_t data_len;
443461
unsigned char *data;
462+
char outbuf[COAP_DEBUG_BUF_SIZE];
463+
int outbuflen = 0;
464+
465+
/* Save time if not needed */
466+
if (level > coap_get_log_level())
467+
return;
444468

445-
fprintf(COAP_DEBUG_FD, "v:%d t:%s c:%s i:%04x {",
469+
snprintf(outbuf, sizeof(outbuf), "v:%d t:%s c:%s i:%04x {",
446470
COAP_DEFAULT_VERSION, msg_type_string(pdu->type),
447471
msg_code_string(pdu->code), pdu->tid);
448472

449473
for (i = 0; i < pdu->token_length; i++) {
450-
fprintf(COAP_DEBUG_FD, "%02x", pdu->token[i]);
474+
outbuflen = strlen(outbuf);
475+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
476+
"%02x", pdu->token[i]);
451477
}
452-
fprintf(COAP_DEBUG_FD, "}");
478+
outbuflen = strlen(outbuf);
479+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, "}");
453480

454481
/* show options, if any */
455482
coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL);
456483

457-
fprintf(COAP_DEBUG_FD, " [");
484+
outbuflen = strlen(outbuf);
485+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, " [");
458486
while ((option = coap_option_next(&opt_iter))) {
459487
if (!have_options) {
460488
have_options = 1;
461489
} else {
462-
fprintf(COAP_DEBUG_FD, ",");
490+
outbuflen = strlen(outbuf);
491+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, ",");
463492
}
464493

465494
if (pdu->code == COAP_SIGNALING_CSM) switch(opt_iter.type) {
@@ -520,6 +549,7 @@ coap_show_pdu(const coap_pdu_t *pdu) {
520549
case COAP_OPTION_MAXAGE:
521550
case COAP_OPTION_OBSERVE:
522551
case COAP_OPTION_SIZE1:
552+
case COAP_OPTION_SIZE2:
523553
/* show values as unsigned decimal value */
524554
buf_len = snprintf((char *)buf, sizeof(buf), "%u",
525555
coap_decode_var_bytes(coap_opt_value(option),
@@ -544,32 +574,70 @@ coap_show_pdu(const coap_pdu_t *pdu) {
544574
buf, sizeof(buf), encode);
545575
}
546576

547-
fprintf(COAP_DEBUG_FD, " %s:%.*s",
548-
msg_option_string(pdu->code, opt_iter.type),
549-
(int)buf_len, buf);
577+
outbuflen = strlen(outbuf);
578+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
579+
" %s:%.*s", msg_option_string(pdu->code, opt_iter.type),
580+
(int)buf_len, buf);
550581
}
551582

552-
fprintf(COAP_DEBUG_FD, " ]");
583+
outbuflen = strlen(outbuf);
584+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, " ]");
553585

554586
if (coap_get_data(pdu, &data_len, &data)) {
555587

556-
fprintf(COAP_DEBUG_FD, " :: ");
588+
outbuflen = strlen(outbuf);
589+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, " :: ");
557590

558591
if (is_binary(content_format)) {
559-
fprintf(COAP_DEBUG_FD, "<<");
592+
int keep_data_len = data_len;
593+
uint8_t *keep_data = data;
594+
595+
outbuflen = strlen(outbuf);
596+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
597+
"binary data length %ld\n", data_len);
598+
COAP_DO_SHOW_OUTPUT_LINE;
599+
/*
600+
* Output hex dump of binary data as a continuous entry
601+
*/
602+
outbuf[0] = '\000';
603+
snprintf(outbuf, sizeof(outbuf), "<<");
604+
while (data_len--) {
605+
outbuflen = strlen(outbuf);
606+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
607+
"%02x", *data++);
608+
}
609+
outbuflen = strlen(outbuf);
610+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, ">>");
611+
data_len = keep_data_len;
612+
data = keep_data;
613+
outbuflen = strlen(outbuf);
614+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, "\n");
615+
COAP_DO_SHOW_OUTPUT_LINE;
616+
/*
617+
* Output ascii readable (if possible), immediately under the
618+
* hex value of the character output above to help binary debugging
619+
*/
620+
outbuf[0] = '\000';
621+
snprintf(outbuf, sizeof(outbuf), "<<");
560622
while (data_len--) {
561-
fprintf(COAP_DEBUG_FD, "%02x", *data++);
623+
outbuflen = strlen(outbuf);
624+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
625+
"%c ", isprint (*data) ? *data : '.');
626+
data++;
562627
}
563-
fprintf(COAP_DEBUG_FD, ">>");
628+
outbuflen = strlen(outbuf);
629+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, ">>");
564630
} else {
565631
if (print_readable(data, data_len, buf, sizeof(buf), 0)) {
566-
fprintf(COAP_DEBUG_FD, "'%s'", buf);
632+
outbuflen = strlen(outbuf);
633+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, "'%s'", buf);
567634
}
568635
}
569636
}
570637

571-
fprintf(COAP_DEBUG_FD, "\n");
572-
fflush(COAP_DEBUG_FD);
638+
outbuflen = strlen(outbuf);
639+
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, "\n");
640+
COAP_DO_SHOW_OUTPUT_LINE;
573641
}
574642

575643
static coap_log_handler_t log_handler = NULL;
@@ -585,7 +653,11 @@ coap_log_impl(coap_log_t level, const char *format, ...) {
585653
return;
586654

587655
if (log_handler) {
656+
#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
588657
char message[128];
658+
#else
659+
char message[8 + 1024 * 2]; /* O/H + Max packet payload size * 2 */
660+
#endif
589661
va_list ap;
590662
va_start(ap, format);
591663
vsnprintf( message, sizeof(message), format, ap);

src/net.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -617,9 +617,7 @@ coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu) {
617617
default:
618618
break;
619619
}
620-
if (LOG_DEBUG <= coap_get_log_level()) {
621-
coap_show_pdu(pdu);
622-
}
620+
coap_show_pdu(LOG_DEBUG, pdu);
623621
return bytes_written;
624622
}
625623

@@ -2075,7 +2073,7 @@ coap_dispatch(coap_context_t *context, coap_session_t *session,
20752073
(int)msg_len, addr, localaddr);
20762074
20772075
*/
2078-
coap_show_pdu(pdu);
2076+
coap_show_pdu(LOG_DEBUG, pdu);
20792077
}
20802078
#endif
20812079

tests/test_pdu.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,9 @@ t_parse_pdu16(void) {
300300
result = coap_pdu_parse(COAP_PROTO_UDP, teststr, sizeof(teststr), testpdu);
301301
CU_ASSERT(result == 0);
302302

303+
coap_set_show_pdu_output(0);
303304
coap_set_log_handler(log_handler);
304-
coap_show_pdu(testpdu); /* display PDU */
305+
coap_show_pdu(LOG_ERR, testpdu); /* display PDU */
305306
coap_set_log_handler(NULL);
306307

307308
coap_delete_pdu(testpdu);

tests/test_wellknown.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ t_wellknown6(void) {
241241

242242
CU_ASSERT_PTR_NOT_NULL(response);
243243

244-
/* coap_show_pdu(response); */
244+
/* coap_show_pdu(LOG_INFO, response); */
245245

246246
CU_ASSERT(coap_get_block(response, COAP_OPTION_BLOCK2, &block) != 0);
247247

0 commit comments

Comments
 (0)