-
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
nanocoap: define and use coap_request_ctx_t for request handlers #17957
Conversation
The idea to present the resource path in a handler of a resource would only occur to me if there existed generic handlers, but usually a handler is written for a dedicated resource, so the path cast in stone. const coap_resource_t coap_resources[] = {
COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER,
{ "/riot/board", COAP_GET, _riot_board_handler, NULL },
/* this line adds the whole "/suit"-subtree */
SUIT_COAP_SUBTREE,
}; Within Edit: Ok, but redefining a I would keep it more general. typedef struct {
const coap_resource_t *resource;
} coap_request_ctx_t; typedef ssize_t (*coap_handler_t)(coap_pkt_t *pkt, uint8_t *buf, size_t len,
coap_request_ctx_t *context); The name makes it possible to extend this struct with resource unrelated members, like endpoint addresses, as you have written in "Issues/PRs references". |
The use case was the CoAP fileserver. The fileserver is configured to serve a certain directory, e.g. You can re-assemble the request path from the packet, but without knowing the resource there is no way to tell if you should now try to serve
I thought that might generate a bit too much noise, but if there is a use case for the other fields of |
sys/include/net/nanocoap.h
Outdated
* | ||
* @return Resource path of the request | ||
*/ | ||
static inline const char *coap_request_get_path(const coap_resource_ctx_t *ctx) |
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.
I am not a big fan of get_member()
functions. But they allow us to hide the details of coap_resource_ctx_t
from the user, using just a forward declaration. Which is good to not cause API changes. Just having coap_request_get_resource()
sufficient instead of ... get_path()
, ...get_methods()
and so on.
You forgot to update some functions: Lines 78 to 82 in 5643b52
RIOT/examples/cord_epsim/main.c Lines 60 to 64 in 5643b52
RIOT/examples/nanocoap_server/coap_handler.c Lines 166 to 173 in 5643b52
RIOT/sys/net/application_layer/gcoap/forward_proxy.c Lines 34 to 35 in 5643b52
RIOT/tests/pkg_edhoc_c/responder.c Lines 102 to 105 in 5643b52
RIOT/tests/riotboot_flashwrite/coap_handler.c Lines 76 to 79 in 5643b52
And if we decide to not expose the definition of |
You may also squash if you like. |
sys/include/net/nanocoap.h
Outdated
/** | ||
* @brief CoAP resource request handler context | ||
*/ | ||
struct _coap_request_ctx { |
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 we have the order of the members swapped? Otherwise this would break applications that cast the user context, right?
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.
Applications cannot cast coap_request_ctx_t *
to whatever they expect. But as of now, they can cast the void *context
member of that struct to whatever they expect. But hiding the definition of coap_request_ctx_t
and providing coap_request_get_context()
would be better IMO.
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.
Applications cannot cast
coap_request_ctx_t *
to whatever they expect
Yes, true. My bad. But then I'm worried this is a big API change for applications out there.
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.
I think it is reasonable and beneficial. Users who are registering a function prototype with a void *context
in their static const coap_resource_t _resources[]
resource array are going to face a compilation error. And then they should look at the documentation and should be able to update the prototype.
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.
But then I'm worried this is a big API change for applications out there.
There are pending use cases for an API change on this (like knowing which client requested this, esp. w/rt security). This change (that adds an extensible pointer) should be the one we need to compatibly add the others later.
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.
Yup, this is good from my side. You may squash and on green Murdock I will give the first ACK.
ee2f574
to
542916f
Compare
@chrysn can this still make it into the next release? |
On 11 July 2022 12:57:36 CEST, benpicco ***@***.***> wrote:
***@***.*** can this still make it into the next release?
Yes. Let's not drag this out needlessly, it's early enough to still spot and fix any fallout.
|
@chrysn so want to throw in a 2nd ACK? 😉 |
542916f
to
3806f7d
Compare
rebased to include #16705 |
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.
The API change is warranted, and allows future evolution of the data provided to the handler without further API breaks.
Thanks, seconded.
Contribution description
Currently a resource handler has no information about the resource it handles.
This leads to awkward work-around such as specifying the resource path in the user context again.
To avoid this, introduce a
coap_resource_ctx_t
struct that contains the user context and the resource path, that can be easily extended if more context is needed in the future.Testing procedure
All CoAP applications should still work.
Since the user context was usually NULL, most will not even notice a change.
Issues/PRs references
#14397 (comment)
Can provide an alternative to #16827 (and also move
tl_type
out ofcoap_pkt_t
intocoap_resource_ctx_t
)depends on !11