-
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/nanocoap: add generic handling for string-based options #8920
net/nanocoap: add generic handling for string-based options #8920
Conversation
sys/include/net/nanocoap.h
Outdated
} | ||
|
||
/** | ||
* @brief Get the packet's request URI |
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.
this is of course copy/paste bla bla, will adjust once the code is approved...
ACK for the idea! |
b8e8ca7
to
c58df24
Compare
Cool. |
alright, updated the doxygen accordingly. |
should be ready for review |
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.
Minors, feel free to squash after.
sys/include/net/nanocoap.h
Outdated
* @return -ENOSPC if the complete option does fit into @p target | ||
* @return nr of bytes written to @p target (including '\0') | ||
*/ | ||
int coap_get_opt_string(const coap_pkt_t *pkt, uint16_t optnum, |
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.
probably change to size_t return type for consistency
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.
ssize_t
since error codes are negative?
@@ -504,6 +512,8 @@ size_t coap_put_option_uri(uint8_t *buf, uint16_t lastonum, const char *uri, uin | |||
return bufpos - buf; | |||
} | |||
|
|||
|
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.
...
@kb2ma I'm about to ACK, would you like to take a quick look? |
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.
Same as @kaspar030, nothing big, feel free to squash after these too.
sys/include/net/nanocoap.h
Outdated
@@ -434,7 +494,32 @@ unsigned coap_get_content_type(coap_pkt_t *pkt); | |||
* @returns -ENOSPC if URI option is larger than NANOCOAP_URI_MAX | |||
* @returns nr of bytes written to @p target (including '\0') | |||
*/ | |||
int coap_get_uri(coap_pkt_t *pkt, uint8_t *target); | |||
static inline int coap_get_uri(const coap_pkt_t *pkt, uint8_t *target) |
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.
If @kaspar030 comment is applied, the return type also propagates to this function.
sys/include/net/nanocoap.h
Outdated
* @returns -ENOSPC if URI option is larger than @p max_len | ||
* @returns nr of bytes written to @p target (including '\0') | ||
*/ | ||
static inline int coap_get_location_path(const coap_pkt_t *pkt, |
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.
And to this one
{ | ||
coap_optpos_t *optpos = pkt->options; | ||
coap_optpos_t *optpos = (coap_optpos_t *)pkt->options; |
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.
If you mark *optpos
as const you don't need the cast here.
Just a remark before I forget it again: somebody would need to go through all of the Comments look all valid, will address them ASAP |
There is a typo in the commit message of the second commit "LOCACTION_PATH" -> "LOCATION_PATH". |
Yes, thanks. I'll post my feedback later today. |
sys/include/net/nanocoap.h
Outdated
* @param[in] max_len size of @p target | ||
* @param[in] separator character used for separating the option parts | ||
* | ||
* @return -ENOSPC if the complete option does fit into @p target |
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.
Should be "does not fit".
5768a81
to
5ff4745
Compare
rebased, squashed, and fixed comments:
|
and fixed the typo in the commit message :-) |
I'm going to put my review here to keep it all together, rather than put some of it in the code. coap_put_option_location_path() -- shouldn't this be coap_put_option_location() to include Location-Path and Location-Query? This approach would be consistent with coap_put_option_uri(), which includes Uri-Path and Uri-Query. I have a couple of higher level concerns. Why do coap_[get|put]_option_string() require the separator as an argument? Isn't this implicit in the option number argument? For example, Uri-Path always uses '/', and Uri-Query uses '&' except for the initial '?'. What about options with an opaque data type, like ETag? WIll this require a coap_[get|put]_option_buf()? Why not roll up string and opaque options into a single function? Once again, the option number defines the behavior. I think the fundamental API is:
Any other option-specific functions likely are inline on top of those. gcoap then simplifies the put options by removing the requirement to specify them in order, as shown in #8831. |
Isn't the difficulty with string functions that one e.g, path is split into multiple occurences of the option? |
I have been experimenting with the adaptation of gcoap with the API I described above. The problem with this approach is that it means nanocoap would need to maintain lookup tables for the configuration of an option. Examples -- is it repeatable, what is the separator character, how to match the option number to a position in the table. This approach would allow nanocoap to validate options more carefully. However, I think this work is two steps away from where we are today. The next step is just to get options working based on #8772. So, I agree that the API needs functions to get/set string values. If/when we get to option definitions, the string functions can be deprecated. So how about this for an updated API. I do think the use of 'coap_opt' for the prefix improves usability, as @kaspar030 suggested.
|
5ff4745
to
c6b9051
Compare
yes, that's better indeed. Will fix.
As you stated below, making this explicit depending on the option number implies that we need to keep some kind of a lookup table to map the behavior. This is IMHO overkill (especially considering nanocaop's scope to be as lightweight as possible). Further, the separator character in the string representation is something specific chosen by the nanocoap user, and is not really for making the string nice to read for humans, so there is IMHO no need for having e.g. a query string start with
I like it. I will adapt the functions touched by this PR and propose that we open an issue for adapting the rest of the option handling functions in separate PR(s). How does that sound? |
Thinking about this:
... the get functions get a packet, the put functions a buffer. I propose using |
Thanks @kb2ma! Now I start to remember/understand what the option handling concept was all about :-) I agree with your proposed changes and merged them in, will do one more verification round on my side now and then I hope this PR should be ready to go. |
Excellent! Since you're working with Uri-Query options, remember that coap_parse() currently does not set the coap_pkt_t.qs attribute for gcoap. I'll watch for your OK for final review. |
cb5cc7d
to
31273af
Compare
alright, fixed one more alignment issue and did some (successful) testing. Also cleaned up the commits and squashed. On my part this PR should be ready for final review! |
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.
Also, please take a look at sys/net/application_layer/nanocoap/sock.c. It uses the obsolete coap_put_option_uri(). One more minor issue, the commit comment includes the word 'genering' :-)
sys/include/net/nanocoap.h
Outdated
} | ||
|
||
/** | ||
* @brief Convenience function for getting the packet's LOCATION_PATH option |
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.
Should be LOCATION_QUERY
@@ -296,7 +304,7 @@ ssize_t coap_handle_req(coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_le | |||
uint8_t *uri = pkt->url; | |||
#else | |||
uint8_t uri[NANOCOAP_URI_MAX]; | |||
if (coap_get_uri(pkt, uri) <= 0) { | |||
if (coap_get_uri_query(pkt, uri) <= 0) { |
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.
Should be coap_get_uri_path()
Tested OK on native and samr21-xpro, with the code updates in my review. Test included use of rdcli-simple example to register with libcoap coap-rd server. Good stuff! |
90fab04
to
2cc095d
Compare
addressed comments and squashed. Thanks for the testing! |
@kaspar030, @bergzand: are your change requests satisfies as well? |
Still don't see the fix to sock.c and Murdock is unhappy with documentation. |
you are right, I missed that one |
- add generic string put and get functions - add location path and location query options - add dedicated functions for getting and setting URI query, URI path, location query, and location path options
2cc095d
to
04bc6ee
Compare
ok, should be fixed now. |
Looks good to me! Since you've got an open question out there, I'll leave it to you to merge when ready. |
Lets wait for the other two to approve or dismiss their review -> @kaspar030, @bergzand |
I'll try to take a look at this PR today to see if I can spot any remaining issues. |
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.
LGTM
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.
ACK
awesome, thanks guys! -> let's go! |
dammit, 10 seconds too late :-) |
Contribution description
Many string-based options (e.g.
COAP_OPT_URI
,COAP_OPT_LOCATION_PATH
) share a similar structure: they consist of string 'snippets', that are split using some kind of separation character, typically/
. This PR adds a more generic function for setting and reading these kind of options tonanocoap
.Please let me know what you think of this approach - once consensus is reached I will spend the time to add the missing doxygen...
Issues/PRs references
Follow-up on #8772