-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
net/coap: Block optimizations #11057
Changes from all commits
233dd31
dae9c4a
efb75c5
5eaa51e
2abcbc0
e22d472
2dc4209
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -717,6 +717,26 @@ size_t coap_blockwise_put_bytes(coap_block_slicer_t *slicer, uint8_t *bufpos, | |
*/ | ||
size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, char c); | ||
|
||
/** | ||
* @brief Block option getter | ||
* | ||
* This function gets a CoAP packet's block option and parses it into a helper | ||
* structure. | ||
* | ||
* If no block option is present in @p pkt, the values in @p block will be | ||
* initialized with zero. That implies both block->offset and block->more are | ||
* also valid in that case, as packet with offset==0 and more==0 means it contains | ||
* all the payload for the corresponding request. | ||
* | ||
* @param[in] pkt pkt to work on | ||
* @param[out] block ptr to preallocated coap_block1_t structure | ||
* @param[in] option block1 or block2 | ||
* | ||
* @returns 0 if block option not present | ||
* @returns 1 if structure has been filled | ||
*/ | ||
int coap_get_block(coap_pkt_t *pkt, coap_block1_t *block, uint16_t option); | ||
|
||
/** | ||
* @brief Block1 option getter | ||
* | ||
|
@@ -729,23 +749,29 @@ size_t coap_blockwise_put_char(coap_block_slicer_t *slicer, uint8_t *bufpos, cha | |
* all the payload for the corresponding request. | ||
* | ||
* @param[in] pkt pkt to work on | ||
* @param[out] block1 ptr to preallocated coap_block1_t structure | ||
* @param[out] block ptr to preallocated coap_block1_t structure | ||
* | ||
* @returns 0 if block1 option not present | ||
* @returns 1 if structure has been filled | ||
*/ | ||
int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block1); | ||
static inline int coap_get_block1(coap_pkt_t *pkt, coap_block1_t *block) | ||
{ | ||
return coap_get_block(pkt, block, COAP_OPT_BLOCK1); | ||
} | ||
|
||
/** | ||
* @brief Block2 option getter | ||
* | ||
* @param[in] pkt pkt to work on | ||
* @param[out] block2 ptr to preallocated coap_block1_t structure | ||
* @param[out] block ptr to preallocated coap_block1_t structure | ||
* | ||
* @returns 0 if block2 option not present | ||
* @returns 1 if structure has been filled | ||
*/ | ||
int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block2); | ||
static inline int coap_get_block2(coap_pkt_t *pkt, coap_block1_t *block) | ||
{ | ||
return coap_get_block(pkt, block, COAP_OPT_BLOCK2); | ||
} | ||
|
||
/** | ||
* @brief Generic block option getter | ||
|
@@ -1039,17 +1065,18 @@ static inline size_t coap_opt_put_block2(uint8_t *buf, uint16_t lastonum, | |
} | ||
|
||
/** | ||
* @brief Insert block option into buffer from block struct | ||
* @brief Encode the given uint option into buffer | ||
* | ||
* @param[in] buf buffer to write to | ||
* @param[in] lastonum last option number (must be < @p option) | ||
* @param[in] block block option attribute struct | ||
* @param[in] option option number (block1 or block2) | ||
* @param[out] buf buffer to write to | ||
* @param[in] lastonum number of previous option (for delta calculation), | ||
* or 0 for first option | ||
* @param[in] onum number of option | ||
* @param[in] value value to encode | ||
* | ||
* @returns amount of bytes written to @p buf | ||
*/ | ||
size_t coap_opt_put_block_object(uint8_t *buf, uint16_t lastonum, | ||
coap_block1_t *block, uint16_t option); | ||
size_t coap_opt_put_uint(uint8_t *buf, uint16_t lastonum, uint16_t onum, | ||
uint32_t value); | ||
|
||
/** | ||
* @brief Insert block1 option into buffer in control usage | ||
|
@@ -1063,7 +1090,8 @@ size_t coap_opt_put_block_object(uint8_t *buf, uint16_t lastonum, | |
static inline size_t coap_opt_put_block1_control(uint8_t *buf, uint16_t lastonum, | ||
coap_block1_t *block) | ||
{ | ||
return coap_opt_put_block_object(buf, lastonum, block, COAP_OPT_BLOCK1); | ||
return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1, | ||
(block->blknum << 4) | block->szx | (block->more ? 0x8 : 0)); | ||
} | ||
|
||
/** | ||
|
@@ -1080,8 +1108,9 @@ static inline size_t coap_opt_put_block1_control(uint8_t *buf, uint16_t lastonum | |
static inline size_t coap_opt_put_block2_control(uint8_t *buf, uint16_t lastonum, | ||
coap_block1_t *block) | ||
{ | ||
block->more = 0; | ||
return coap_opt_put_block_object(buf, lastonum, block, COAP_OPT_BLOCK2); | ||
/* block.more must be zero, so no need to 'or' it in */ | ||
return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK2, | ||
(block->blknum << 4) | block->szx); | ||
} | ||
|
||
/** | ||
|
@@ -1216,7 +1245,12 @@ size_t coap_put_option(uint8_t *buf, uint16_t lastonum, uint16_t onum, const uin | |
* | ||
* @returns amount of bytes written to @p buf | ||
*/ | ||
size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum, unsigned blknum, unsigned szx, int more); | ||
static inline size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we change this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about actually deprecating this function in favor of using coap_opt_put_block1_control(), which already is used in the nanocoap_server example? One of my goals with this sequence of PRs was consistent names for block functions. Either way, I would like to put the deprecation in a separate PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
unsigned blknum, unsigned szx, int more) | ||
{ | ||
return coap_opt_put_uint(buf, lastonum, COAP_OPT_BLOCK1, | ||
(blknum << 4) | szx | (more ? 0x8 : 0)); | ||
} | ||
|
||
/** | ||
* @brief Insert content type option into buffer | ||
|
@@ -1228,7 +1262,11 @@ size_t coap_put_option_block1(uint8_t *buf, uint16_t lastonum, unsigned blknum, | |
* | ||
* @returns amount of bytes written to @p buf | ||
*/ | ||
size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum, uint16_t content_type); | ||
static inline size_t coap_put_option_ct(uint8_t *buf, uint16_t lastonum, | ||
uint16_t content_type) | ||
{ | ||
return coap_opt_put_uint(buf, lastonum, COAP_OPT_CONTENT_FORMAT, content_type); | ||
} | ||
/**@}*/ | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this also pass the remaining space available in
buf
so we don't silently overflow?