52
52
53
53
static coap_log_t maxlog = LOG_WARNING ; /* default maximum log level */
54
54
55
+ static int use_fprintf_for_show_pdu = 1 ; /* non zero to output with fprintf */
56
+
55
57
const char * coap_package_name (void ) {
56
58
return PACKAGE_NAME ;
57
59
}
@@ -60,6 +62,11 @@ const char *coap_package_version(void) {
60
62
return PACKAGE_STRING ;
61
63
}
62
64
65
+ void
66
+ coap_set_show_pdu_output (int use_fprintf ) {
67
+ use_fprintf_for_show_pdu = use_fprintf ;
68
+ }
69
+
63
70
coap_log_t
64
71
coap_get_log_level (void ) {
65
72
return maxlog ;
@@ -316,6 +323,7 @@ msg_option_string(uint8_t code, uint16_t option_type) {
316
323
{ COAP_OPTION_PROXY_URI , "Proxy-Uri" },
317
324
{ COAP_OPTION_PROXY_SCHEME , "Proxy-Scheme" },
318
325
{ COAP_OPTION_SIZE1 , "Size1" },
326
+ { COAP_OPTION_SIZE2 , "Size2" },
319
327
{ COAP_OPTION_NORESPONSE , "No-Response" }
320
328
};
321
329
@@ -431,8 +439,18 @@ is_binary(int content_format) {
431
439
content_format == COAP_MEDIATYPE_APPLICATION_JSON );
432
440
}
433
441
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
+
434
452
void
435
- coap_show_pdu (const coap_pdu_t * pdu ) {
453
+ coap_show_pdu (coap_log_t level , const coap_pdu_t * pdu ) {
436
454
unsigned char buf [1024 ]; /* need some space for output creation */
437
455
size_t buf_len = 0 ; /* takes the number of bytes written to buf */
438
456
int encode = 0 , have_options = 0 , i ;
@@ -441,25 +459,36 @@ coap_show_pdu(const coap_pdu_t *pdu) {
441
459
int content_format = -1 ;
442
460
size_t data_len ;
443
461
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 ;
444
468
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 {" ,
446
470
COAP_DEFAULT_VERSION , msg_type_string (pdu -> type ),
447
471
msg_code_string (pdu -> code ), pdu -> tid );
448
472
449
473
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 ]);
451
477
}
452
- fprintf (COAP_DEBUG_FD , "}" );
478
+ outbuflen = strlen (outbuf );
479
+ snprintf (& outbuf [outbuflen ], sizeof (outbuf )- outbuflen , "}" );
453
480
454
481
/* show options, if any */
455
482
coap_option_iterator_init (pdu , & opt_iter , COAP_OPT_ALL );
456
483
457
- fprintf (COAP_DEBUG_FD , " [" );
484
+ outbuflen = strlen (outbuf );
485
+ snprintf (& outbuf [outbuflen ], sizeof (outbuf )- outbuflen , " [" );
458
486
while ((option = coap_option_next (& opt_iter ))) {
459
487
if (!have_options ) {
460
488
have_options = 1 ;
461
489
} else {
462
- fprintf (COAP_DEBUG_FD , "," );
490
+ outbuflen = strlen (outbuf );
491
+ snprintf (& outbuf [outbuflen ], sizeof (outbuf )- outbuflen , "," );
463
492
}
464
493
465
494
if (pdu -> code == COAP_SIGNALING_CSM ) switch (opt_iter .type ) {
@@ -520,6 +549,7 @@ coap_show_pdu(const coap_pdu_t *pdu) {
520
549
case COAP_OPTION_MAXAGE :
521
550
case COAP_OPTION_OBSERVE :
522
551
case COAP_OPTION_SIZE1 :
552
+ case COAP_OPTION_SIZE2 :
523
553
/* show values as unsigned decimal value */
524
554
buf_len = snprintf ((char * )buf , sizeof (buf ), "%u" ,
525
555
coap_decode_var_bytes (coap_opt_value (option ),
@@ -544,32 +574,70 @@ coap_show_pdu(const coap_pdu_t *pdu) {
544
574
buf , sizeof (buf ), encode );
545
575
}
546
576
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 );
550
581
}
551
582
552
- fprintf (COAP_DEBUG_FD , " ]" );
583
+ outbuflen = strlen (outbuf );
584
+ snprintf (& outbuf [outbuflen ], sizeof (outbuf )- outbuflen , " ]" );
553
585
554
586
if (coap_get_data (pdu , & data_len , & data )) {
555
587
556
- fprintf (COAP_DEBUG_FD , " :: " );
588
+ outbuflen = strlen (outbuf );
589
+ snprintf (& outbuf [outbuflen ], sizeof (outbuf )- outbuflen , " :: " );
557
590
558
591
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 ), "<<" );
560
622
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 ++ ;
562
627
}
563
- fprintf (COAP_DEBUG_FD , ">>" );
628
+ outbuflen = strlen (outbuf );
629
+ snprintf (& outbuf [outbuflen ], sizeof (outbuf )- outbuflen , ">>" );
564
630
} else {
565
631
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 );
567
634
}
568
635
}
569
636
}
570
637
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 ;
573
641
}
574
642
575
643
static coap_log_handler_t log_handler = NULL ;
@@ -585,7 +653,11 @@ coap_log_impl(coap_log_t level, const char *format, ...) {
585
653
return ;
586
654
587
655
if (log_handler ) {
656
+ #if defined(WITH_CONTIKI ) || defined(WITH_LWIP )
588
657
char message [128 ];
658
+ #else
659
+ char message [8 + 1024 * 2 ]; /* O/H + Max packet payload size * 2 */
660
+ #endif
589
661
va_list ap ;
590
662
va_start (ap , format );
591
663
vsnprintf ( message , sizeof (message ), format , ap );
0 commit comments