Skip to content
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

NanoCoAP: Add helper functions for NanoCBOR payloads #19997

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

bergzand
Copy link
Member

@bergzand bergzand commented Oct 19, 2023

Contribution description

This adds a module to easily generate CBOR payloads in block-wise requests and responses. It wraps NanoCoAP and gcoap around the NanoCBOR stream-like interface to generate sliced block-wise payloads automatically from nanocbor encoder serialization calls. The content is serialized for every individual block-wise request. This module supports both block1 and block2 payloads and will automatically generate and update the etag option in the packet.

This module assumes that the serialized content does not change between successive serializations. In case this happens it should be detected via the etag that can be included in the packet.

Testing procedure

I've extended the example gcoap_block_server with a CBOR encoded response leveraging this module.

Issues/PRs references

Needs #19980 and #19981

@bergzand bergzand added Type: new feature The issue requests / The PR implemements a new feature for RIOT State: waiting for other PR State: The PR requires another PR to be merged first labels Oct 19, 2023
@github-actions github-actions bot added Area: network Area: Networking Area: tests Area: tests and testing framework Area: pkg Area: External package ports Area: CoAP Area: Constrained Application Protocol implementations Area: sys Area: System Area: examples Area: Example Applications labels Oct 19, 2023
@bergzand bergzand changed the title Pr/nanocbor/coap helper NanoCoAP: Add helper functions for NanoCBOR payloads Oct 19, 2023
@bergzand bergzand force-pushed the pr/nanocbor/coap_helper branch from e3abe4d to 3d422d7 Compare October 19, 2023 14:45
@github-actions github-actions bot removed the Area: pkg Area: External package ports label Oct 19, 2023
@bergzand
Copy link
Member Author

This now also supports non-block-wise packets.

This adds 3 functions for adding entity-tags to coap packets. The first
simply adds the option to the coap packet with the supplied etag value.
The second functionallow for adding a dummy etag value which is to be
replaced later by the last function. This allows for allocating space
for the option up front when the etag value itself is generated based on
the payload
This adds an internal function to find options in a CoAP packet without
modifying the coap_pkt_t struct
This adds a module to easily generate CBOR payloads in block-wise
requests and responses.
@bergzand bergzand force-pushed the pr/nanocbor/coap_helper branch from f62eb9f to a74eb64 Compare October 20, 2023 09:14
Copy link
Contributor

@Teufelchen1 Teufelchen1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! 🐪

I gave it a quick look. Do you still work on it or can I go ahead and test & review it properly?

@@ -339,7 +339,7 @@ static int _gcoap_forward_proxy_copy_options(coap_pkt_t *pkt,
static const uint8_t tmp[COAP_ETAG_LENGTH_MAX] = { 0 };
/* add slack to maybe add an ETag on stale cache hit later, as is done in
* gcoap_req_send() (which we circumvented in _gcoap_forward_proxy_via_coap()) */
if (coap_opt_add_opaque(pkt, COAP_OPT_ETAG, tmp, sizeof(tmp))) {
if (coap_opt_add_etag(pkt, COAP_OPT_ETAG, tmp, sizeof(tmp))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mh?

Suggested change
if (coap_opt_add_etag(pkt, COAP_OPT_ETAG, tmp, sizeof(tmp))) {
if (coap_opt_add_etag(pkt, tmp, sizeof(tmp))) {

@@ -1117,6 +1117,35 @@ static void test_nanocoap__token_length_ext_269(void)
TEST_ASSERT_EQUAL_INT(14, hdr->ver_t_tkl & 0xf);
}

static void test_nanocoap__replace_etag(void)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function name does not explain your intend well enough. Remember to unit test behavior so ideally something along test_that_nanocoap_replaces_an_etag_within_a_prebuild_pkt_without_segfault 😈

}
}

static ssize_t _etag_finish(coap_pkt_t *pdu, coap_nanocbor_slicer_helper_t *helper)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are using a lot of 'private' functions in this PR with names I find hard to relate. I would appreciate if you could make them a bit more descriptive or add small comments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: CoAP Area: Constrained Application Protocol implementations Area: examples Area: Example Applications Area: network Area: Networking Area: sys Area: System Area: tests Area: tests and testing framework State: waiting for other PR State: The PR requires another PR to be merged first Type: new feature The issue requests / The PR implemements a new feature for RIOT
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants