Skip to content

Conversation

cgundogan
Copy link
Member

Contribution description

The current nanocoap API allows to add a path using the convenient coap_opt_add_string() function. The path is then split into multiple CoAP options. However, this function requires the path to be zero-terminated, which is quite inconvenient when dealing with buffers.

This PR adds coap_opt_add_chars(), which takes a character array and the length of the desired string as input. coap_opt_add_string() is modified to call coap_opt_add_chars() with strlen() as input.

Testing procedure

The unittests should run for the existing coap_opt_add_string() test cases, and for the newly introduced coap_opt_add_chars() test case (at the very end of the unittest file).

Issues/PRs references

This is a cutout of #13790

@cgundogan cgundogan added Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation Area: CoAP Area: Constrained Application Protocol implementations labels Apr 5, 2020
@cgundogan cgundogan requested a review from kb2ma April 5, 2020 11:15
@cgundogan cgundogan changed the title nanocoap: allow coap_opt_add_string() for zero-terminated strings nanocoap: allow coap_opt_add_string() for non-zero-terminated strings Apr 5, 2020
Copy link
Contributor

@benpicco benpicco left a comment

Choose a reason for hiding this comment

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

Looks pretty straightforward - would make sense to also add coap_opt_add_uri_path_chars() so applications can make use of this too.

Copy link
Member

@kb2ma kb2ma left a comment

Choose a reason for hiding this comment

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

Straightforward addition. Good idea to add a unit test.

@kb2ma kb2ma self-requested a review April 6, 2020 12:02
kb2ma
kb2ma previously requested changes Apr 6, 2020
Copy link
Member

@kb2ma kb2ma left a comment

Choose a reason for hiding this comment

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

Meant to mark as requesting changes.

@chrysn
Copy link
Member

chrysn commented Apr 7, 2020 via email

@benpicco
Copy link
Contributor

benpicco commented Apr 8, 2020

Does CoAP require it to be an (ASCII) string or could we also add binary data here?

@chrysn
Copy link
Member

chrysn commented Apr 8, 2020

The function works for every option, and some options take binary data (typically used with coap_opt_add_opaque)

In the concrete place where it's being introduced for (path components and query parameters of a CoAP URI), it's arbitrary UTF-8 that may contain any code points. (Using a function like coap_opt_add_chars rules out one of them unless the delimiter is 0xff or some other byte invalid in UTF-8).

@kb2ma
Copy link
Member

kb2ma commented Apr 8, 2020

Let me summarize the conversation to this point. To be clear, when I mention a String option or Opaque option, I mean them as described in sec. 3.2 of 7252 on option value formats.

Originally, this PR intended to extend coap_opt_add_string() for multiple String options to allow passing in a length-specified char array (without the terminating null byte). @chrysn has suggested that the function also could be used to write multiple Opaque options (like If-Match or ETag) as well as String options. However, the use of a delimiter character parameter to separate the embedded String/Opaque options means that an Opaque option cannot include that character in its contents.

So, the question then is do we want to extend/publish this function to allow writing multiple Opaque options as well as multiple String options? Or perhaps it would be more useful to create a separate function to add multiple Opaque options more tailored to that use case. For example, it might be better to use a size_t array to identify the starting offset for each embedded Opaque value within the buffer so we don't eliminate use of a particular character. At the same time, someone could still use coap_opt_add_chars() for Opaque options given the limitation with delimiters.

@cgundogan
Copy link
Member Author

@kb2ma thanks for the feedback! I added two new fixup commits to address your comments. @benpicco I think we should keep this PR small and clean. We could add coap_opt_add_uri_path_chars() in a follow-up?

So, the question then is do we want to extend/publish this function to allow writing multiple Opaque options as well as multiple String options? Or perhaps it would be more useful to create a separate function to add multiple Opaque options more tailored to that use case.

Hm, I am a little bit indifferent about the naming here. I do not see a use case for a generic function that adds opaque values and at the same time includes the feature to split the buffer at a specific character (e.g., '/'). I had a quick look at the available CoAP options and I could not find a scenario, where such a function would be beneficial (although, this doesn't mean that such an option wouldn't be defined in future specs).

@kb2ma kb2ma dismissed their stale review April 18, 2020 14:51

Comments addressed

@kb2ma
Copy link
Member

kb2ma commented Apr 18, 2020

Fixups look good. I think this PR is ready to merge, but I'll wait a couple of days for any other feedback before approving.

If someone wants to use coap_opt_add_chars() to generate Opaque options, they certainly can do that. I think it's OK to leave the function doc as is, without commenting on its use for a particular option value format.

@kb2ma
Copy link
Member

kb2ma commented Apr 18, 2020

@cgundogan, I just noticed Travis found an unused variable.

@cgundogan cgundogan force-pushed the pr/nanocoap/add-string-non-zero branch from 66f24ea to 4533c1b Compare April 22, 2020 10:29
@cgundogan cgundogan added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Apr 22, 2020
@cgundogan cgundogan force-pushed the pr/nanocoap/add-string-non-zero branch from 4533c1b to ecc4932 Compare April 22, 2020 10:50
@cgundogan
Copy link
Member Author

@kb2ma I fixed the unused variable warning, squashed all commits and triggered murdock. Let's wait for the outcome.

@cgundogan
Copy link
Member Author

Murdock and Travis agree (:

@kb2ma
Copy link
Member

kb2ma commented Apr 22, 2020

Excellent, I'll take one more look to be sure we're good.

@kb2ma kb2ma merged commit f034f64 into RIOT-OS:master Apr 22, 2020
@cgundogan
Copy link
Member Author

thanks for the review @kb2ma ! (:

@cgundogan cgundogan deleted the pr/nanocoap/add-string-non-zero branch April 23, 2020 06:58
@kb2ma kb2ma added this to the Release 2020.07 milestone Apr 27, 2020
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 CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR Type: enhancement The issue suggests enhanceable parts / The PR enhances parts of the codebase / documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants