-
Notifications
You must be signed in to change notification settings - Fork 2.1k
gcoap: add a handler for request matching #14029
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
Conversation
This would make OSCORE integration a lot easier, I'm taking this up for review. |
I had to look at how this is used in the proxy PR to understand how this is used. I'm not convinced this is the right place to put this. It does appear to work, but AFAICT only for some limited patterns of application:
This works, but the latter mode is not immediately obvious from the documentation. I'd think that the better point to hook into would be to replace the whole _find_resource loop as a whole rather than the function evaluated inside that loop. At that point, So this is an improvement because it enables some more cases, but we should at least consider going the next step right away. |
while working on the feedback by @chrysn I was wondering whether we should keep two callback functions:
|
c1027f6
to
6079c68
Compare
@chrysn I moved the inner for loop into the callback function of the gcoap listener. |
This does look better to me. What I still find odd is that a coap_resource_t is set (rather than a handler only), and why the listener is passed back. I see, though, that the former is is due to observation needing a resource entry (meaning that for the time being it'll be hard to do observation on anything matched with a different method). The latter looks like a relic, do you see any purpose in it? (The listener variable in handle_req is unused). |
I did have a deeper look into the observation part, and the fact that this is a resource is only ever used in the debug statements of registration and deregistration. (Otherwise, identity to an arbitrary const pointer is good enough). I'd prefer to get the generalization away from matching-by-path complete right away (by making gcoap_resource_t merely a thing of the default handler), but do understand if you want to get it done here, as long as that's not entrenching gcoap_resource_t as the way all handlers need to be (which it is not, it's just not pushing it out either). |
hmm, that's true. In addition, it is also used to match against existing observe registrations and to hold the context for the response handler. Generalizing the resource seems possible and is probably also not that complicated. I'd like to focus on that in a separate PR, though. |
Conceptually, I'm happy with this (and will go over non-conceptual things like testing, code style and impact next). I'd still like to indicate in the type documentationof gcoap_listener how it'd be used by other implementations. Would something like typedef struct gcoap_listener {
union {
const coap_resource_t *resources; /**< First element in the array of
* resources; must order alphabetically */
void *data1; /** User data field when request_matcher is not the default handler */
}
union {
size_t resources_len; /**< Length of array */
void *data2; /** Additional user data field when request matcher is not the default handler */
}
gcoap_link_encoder_t link_encoder; /**< Writes a link for a resource */
struct gcoap_listener *next; /**< Next listener in list */
/**
* @brief ptr to a request matcher strategy implementation
*/
gcoap_request_matcher_t request_matcher;
} gcoap_listener_t; work for you? I'd need a more experienced reviewer's help to judge the practical impact of the added field. Can we rely on users to only allocate such structs in ways that guarantee that fields they don't set themselves are zeroed out? (Otherwise a user upgrading without careful reading of the changelog might find themselves call to a garbage address in the request_matcher, possibly even only in production and not in debug builds). |
For impact assessment, I've merged in master and built the gcoap example with
The data impact is about what's to be expected (new callbacks for user and default handler), and the text overhead is minimal. Neither IMO warrants the additional code complexity incurred by making this feature optional. There is some run-time overhead in that |
Follow-up not on the union aliasing: Same goes for the |
Code review:
Other than the above, the new code does right what the old did. Testing: This adds no new behavior but only an extension point for which there are no users in the PR, so I consider the existing CoAP testing sufficient. |
Suggestion for request_matcher documentation:
|
which constification do you mean here? The return value is an int.
👍 since
not sure I fully understand this remark. I constified the
good catch. I restored the old behavior in the latest commit |
The argument pdu to coap_get_code_detail is cast from
I was merely referring to the |
1762356
to
d7b37a8
Compare
|
@chrysn there are two new commits:
|
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: Works in tests, and all else that could be done can just as well be done later.
@@ -340,7 +341,7 @@ static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len, | |||
gcoap_observe_memo_t *memo = NULL; | |||
gcoap_observe_memo_t *resource_memo = NULL; | |||
|
|||
switch (_find_resource(pdu, &resource, &listener)) { | |||
switch (_find_resource((const coap_pkt_t *)pdu, &resource, &listener)) { |
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 constness could also be propagated up.
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.
Not sure about this. Propagating the constness might require to change the signature of many other functions. Let's do the assessment and a potential change in another PR.
359ff57
to
7d820f7
Compare
murdock finally agree |
@chrysn thanks for this great review! |
if (res > 0) { | ||
continue; | ||
} | ||
/* resources expected in alphabetical order */ |
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.
Why?
Contribution description
This PR moves the (currently) hardcoded request matching logic into a handler function. The idea is to have the matching logic configurable, which is needed for a seamless proxy integration (see #13790 (review))
Testing procedure
It should still be possible to send and receive various coap packets with two gcoap examples.
Issues/PRs references
#13790