Skip to content

Commit

Permalink
blocks: Better reporting for large transfers
Browse files Browse the repository at this point in the history
Update stateless token to track 20 bits - the largest no of blocks
as per RFC7959.

Make output of data detail configurable for coap_show_pdu().

Add in new function coap_enable_pdu_data_output() to do this.

Add -x option to coap-client and coap-server to enable this dropping
of data detail.
  • Loading branch information
mrdeep1 committed Sep 26, 2024
1 parent ac83b32 commit 79a6630
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 8 deletions.
8 changes: 6 additions & 2 deletions examples/coap-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ usage(const char *program, const char *version) {
fprintf(stderr, "\n"
"Usage: %s [-a addr] [-b [num,]size] [-e text] [-f file] [-l loss]\n"
"\t\t[-m method] [-o file] [-p port] [-q tls_engine_conf_file] [-r]\n"
"\t\t[-s duration] [-t type] [-v num] [-w] [-A type] [-B seconds]\n"
"\t\t[-s duration] [-t type] [-v num] [-w] [-x] [-A type] [-B seconds]\n"
"\t\t[-E oscore_conf_file[,seq_file]] [-G count] [-H hoplimit]\n"
"\t\t[-K interval] [-N] [-O num,text] [-P scheme://address[:port]\n"
"\t\t[-T token] [-U] [-V num] [-X size]\n"
Expand Down Expand Up @@ -543,6 +543,7 @@ usage(const char *program, const char *version) {
"\t-v num \t\tVerbosity level (default 4, maximum is 8) for general\n"
"\t \t\tCoAP logging\n"
"\t-w \t\tAppend a newline to received data\n"
"\t-x \t\tDisable output of PDU data when displaying PDUs\n"
"\t-A type\t\tAccepted media type\n"
"\t-B seconds\tBreak operation after waiting given seconds\n"
"\t \t\t(default is %d)\n"
Expand Down Expand Up @@ -1678,7 +1679,7 @@ main(int argc, char **argv) {
coap_startup();

while ((opt = getopt(argc, argv,
"a:b:c:d:e:f:h:j:k:l:m:no:p:q:rs:t:u:v:wA:B:C:E:G:H:J:K:L:M:NO:P:R:T:UV:X:2")) != -1) {
"a:b:c:d:e:f:h:j:k:l:m:no:p:q:rs:t:u:v:wx:A:B:C:E:G:H:J:K:L:M:NO:P:R:T:UV:X:2")) != -1) {
switch (opt) {
case 'a':
strncpy(node_str, optarg, NI_MAXHOST - 1);
Expand Down Expand Up @@ -1738,6 +1739,9 @@ main(int argc, char **argv) {
case 'w':
add_nl = 1;
break;
case 'x':
coap_enable_pdu_data_output(0);
break;
case 'N':
msgtype = COAP_MESSAGE_NON;
break;
Expand Down
6 changes: 5 additions & 1 deletion examples/coap-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1562,7 +1562,7 @@ usage(const char *program, const char *version) {
"Usage: %s [-a priority] [-b max_block_size] [-d max] [-e]\n"
"\t\t[-f scheme://address[:port] [-g group] -l loss] [-p port]\n"
"\t\t[-q tls_engine_conf_file] [-r] [-v num] [-w [port][,secure_port]]\n"
"\t\t[-A address] [-E oscore_conf_file[,seq_file]] [-G group_if]\n"
"\t\t[-x] [-A address] [-E oscore_conf_file[,seq_file]] [-G group_if]\n"
"\t\t[-L value] [-N] [-P scheme://address[:port],[name1[,name2..]]]\n"
"\t\t[-T max_token_size] [-U type] [-V num] [-X size]\n"
"\t\t[[-h hint] [-i match_identity_file] [-k key]\n"
Expand Down Expand Up @@ -1612,6 +1612,7 @@ usage(const char *program, const char *version) {
"\t-w [port][,secure_port]\n"
"\t \t\tEnable WebSockets support on port (WS) and/or secure_port\n"
"\t \t\t(WSS), comma separated\n"
"\t-x \t\tDisable output of PDU data when displaying PDUs\n"
"\t-A address\tInterface address to bind to\n"
"\t-E oscore_conf_file[,seq_file]\n"
"\t \t\toscore_conf_file contains OSCORE configuration. See\n"
Expand Down Expand Up @@ -2510,6 +2511,9 @@ main(int argc, char **argv) {
}
enable_ws = 1;
break;
case 'x':
coap_enable_pdu_data_output(0);
break;
case 'X':
csm_max_message_size = strtol(optarg, NULL, 10);
break;
Expand Down
8 changes: 5 additions & 3 deletions include/coap3/coap_block_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
* @{
*/

#define STATE_TOKEN_BASE(t) ((t) & 0xffffffffffffULL)
#define STATE_TOKEN_RETRY(t) ((uint64_t)(t) >> 48)
#define STATE_TOKEN_FULL(t,r) (STATE_TOKEN_BASE(t) + ((uint64_t)(r) << 48))
/* Use the top 20 bits of a 64 bit token for tracking current block in large transfer */
#define STATE_MAX_BLK_CNT_BITS 20
#define STATE_TOKEN_BASE(t) ((t) & (0xffffffffffffffffULL >> STATE_MAX_BLK_CNT_BITS))
#define STATE_TOKEN_RETRY(t) ((uint64_t)(t) >> (64 - STATE_MAX_BLK_CNT_BITS))
#define STATE_TOKEN_FULL(t,r) (STATE_TOKEN_BASE(t) + ((uint64_t)(r) << (64 - STATE_MAX_BLK_CNT_BITS)))

#if COAP_Q_BLOCK_SUPPORT
#define COAP_BLOCK_SET_MASK (COAP_BLOCK_USE_LIBCOAP | \
Expand Down
11 changes: 11 additions & 0 deletions include/coap3/coap_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,21 @@ void coap_print_contiki_prefix(coap_log_t level);
*/
void coap_set_show_pdu_output(int use_fprintf);

/**
* Defines whether the data is to be output or not for the
* coap_show_pdu() function.
*
* @param enable_data @p 1 if the data is to be output (the default)
* @p 0 if the data detail is not to be output.
*/
void coap_enable_pdu_data_output(int enable_data);

/**
* Display the contents of the specified @p pdu.
* Note: The output method of coap_show_pdu() is dependent on the setting of
* coap_set_show_pdu_output().
* Note: Data may, or may not be output depending on the setting of
* coap_enable_pdu_data_output().
*
* @param level The required minimum logging level.
* @param pdu The PDU to decode.
Expand Down
1 change: 1 addition & 0 deletions libcoap-3.map
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ global:
coap_dtls_rpk_is_supported;
coap_dtls_set_log_level;
coap_dump_memory_type_counts;
coap_enable_pdu_data_output;
coap_encode_var_safe8;
coap_encode_var_safe;
coap_endpoint_set_default_mtu;
Expand Down
1 change: 1 addition & 0 deletions libcoap-3.sym
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ coap_dtls_psk_is_supported
coap_dtls_rpk_is_supported
coap_dtls_set_log_level
coap_dump_memory_type_counts
coap_enable_pdu_data_output
coap_encode_var_safe
coap_encode_var_safe8
coap_endpoint_set_default_mtu
Expand Down
1 change: 1 addition & 0 deletions man/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ install-man: install-man3 install-man5 install-man7
@echo ".so man3/coap_logging.3" > coap_package_version.3
@echo ".so man3/coap_logging.3" > coap_package_build.3
@echo ".so man3/coap_logging.3" > coap_set_show_pdu_output.3
@echo ".so man3/coap_logging.3" > coap_enable_pdu_data_output.3
@echo ".so man3/coap_logging.3" > coap_show_pdu.3
@echo ".so man3/coap_logging.3" > coap_endpoint_str.3
@echo ".so man3/coap_logging.3" > coap_session_str.3
Expand Down
5 changes: 4 additions & 1 deletion man/coap-client.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SYNOPSIS
*coap-client* [*-a* addr] [*-b* [num,]size] [*-e* text] [*-f* file] [*-l* loss]
[*-m* method] [*-o* file] [*-p* port] [*-q* tls_engine_conf_file]
[*-r*] [*-s duration*]
[*-t* type] [*-v* num] [*-w*] [*-A* type] [*-B* seconds]
[*-t* type] [*-v* num] [*-w*] [*-x*] [*-A* type] [*-B* seconds]
[*-E* oscore_conf_file[,seq_file]] [*-G* count] [*-H* hoplimit]
[*-K* interval] [*-L* value] [*-N*] [*-O* num,text]
[*-P* scheme://addr[:port]] [*-T* token] [*-U*] [*-V* num]
Expand Down Expand Up @@ -130,6 +130,9 @@ OPTIONS - General
*-w*::
Append a newline to received data.

*-x*::
Disable output of PDU data when displaying PDUs.

*-A* type::
Accepted media type. 'type' must be either a numeric value reflecting a
valid CoAP content format or a string that specifies a registered format as
Expand Down
5 changes: 4 additions & 1 deletion man/coap-server.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SYNOPSIS
*coap-server* [*-a* priority] [*-b* max_block_size] [*-d* max] [*-e*]
[*-f* scheme://addr[:port]
[*-g* group] [*-l* loss] [*-p* port] [*-q* tls_engine_conf_file]
[*-r*] [*-t*] [*-v* num] [*-w* [port][,secure_port]]
[*-r*] [*-t*] [*-v* num] [*-w* [port][,secure_port]] [*-x*]
[*-A* address] [*-E* oscore_conf_file[,seq_file]]
[*-G* group_if] [*-L* value] [*-N*]
[*-P* scheme://addr[:port],[name1[,name2..]]]
Expand Down Expand Up @@ -109,6 +109,9 @@ OPTIONS - General
Enable WebSockets support support on port (WS) and/or secure_port (WSS),
comma separated.

*-x*::
Disable output of PDU data when displaying PDUs.

*-A* address::
The local address of the interface which the server has to listen on.

Expand Down
10 changes: 10 additions & 0 deletions man/coap_logging.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ coap_package_name,
coap_package_version,
coap_package_build,
coap_set_show_pdu_output,
coap_enable_pdu_data_output,
coap_show_pdu,
coap_endpoint_str,
coap_session_str,
Expand Down Expand Up @@ -85,6 +86,8 @@ SYNOPSIS

*void coap_set_show_pdu_output(int _use_fprintf_);*

*void coap_enable_pdu_data_output(int _enable_data_);*

*void coap_show_pdu(coap_log_t _level_, const coap_pdu_t *_pdu_);*

*const char *coap_endpoint_str(const coap_endpoint_t *_endpoint_);*
Expand Down Expand Up @@ -278,6 +281,13 @@ The *coap_set_show_pdu_output*() function defines whether the output from
*coap_log*(). _use_fprintf_ is set to 1 for stdout/stderr (the default), and
_use_fprintf_ is set to 0 for *coap_log*().

*Function: coap_enable_pdu_data_output()*

The *coap_enable_pdu_data_output*() function defines whether the output from
*coap_show_pdu*() is to include data detail or not. _enable_data_ is set to
1 for data to be output (the default) or set to 0 if the data detail is
suppressed.

*Function: coap_show_pdu()*

The *coap_show_pdu*() function is used to decode the _pdu_, outputting as
Expand Down
1 change: 1 addition & 0 deletions src/coap_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -4099,6 +4099,7 @@ coap_handle_response_get_block(coap_context_t *context,
if (!coap_binary_equal(&rcvd->actual_token, lg_crcv->app_token)) {
/* need to put back original token into rcvd */
coap_update_token(rcvd, lg_crcv->app_token->length, lg_crcv->app_token->s);
coap_remove_option(rcvd, COAP_OPTION_BLOCK1);
coap_log_debug("PDU presented to app.\n");
coap_show_pdu(COAP_LOG_DEBUG, rcvd);
}
Expand Down
16 changes: 16 additions & 0 deletions src/coap_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static coap_log_t maxlog = COAP_LOG_WARN; /* default maximum CoAP log level */
#endif /* RIOT_VERSION */

static int use_fprintf_for_show_pdu = 1; /* non zero to output with fprintf */
static int enable_data_for_show_pdu = 1; /* By default show PDU data for coap_show_pdu() */

const char *
coap_package_name(void) {
Expand All @@ -91,6 +92,11 @@ coap_set_show_pdu_output(int use_fprintf) {
use_fprintf_for_show_pdu = use_fprintf;
}

void
coap_enable_pdu_data_output(int enable_data) {
enable_data_for_show_pdu = enable_data;
}

coap_log_t
coap_get_log_level(void) {
return maxlog;
Expand Down Expand Up @@ -1041,6 +1047,16 @@ coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu) {
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, " ]");

if (coap_get_data(pdu, &data_len, &data)) {
if (!enable_data_for_show_pdu) {
/* Only output data if wanted */
outbuflen = strlen(outbuf);
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, " :: ");
outbuflen = strlen(outbuf);
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen,
"data length %zu (data suppressed)\n", data_len);
COAP_DO_SHOW_OUTPUT_LINE;
return;
}

outbuflen = strlen(outbuf);
snprintf(&outbuf[outbuflen], sizeof(outbuf)-outbuflen, " :: ");
Expand Down

0 comments on commit 79a6630

Please sign in to comment.