nanocoap: add context pointer to resources#8513
Conversation
|
I understand this is useful, but could you describe in the PR description what the intended purpose is? 1-2 sentences is probably enough |
Yes, sorry! |
|
I need this e.g., for the javascript coap bindings. In javascript is is possible to dynamically register a coap handler callback. For that, the coap_register_t is allocated, with a generic javascript handler and a context struct containing a reference to the actual javascript handler object and some housekeeping. |
kb2ma
left a comment
There was a problem hiding this comment.
Overall, this addition is simple and straightforward. Tested on native for the nanocoap and gcoap examples. I also tried actually reading a context value, and that worked too. Unit tests OK.
Change request: You redefine CoAP resource arrays in several places to add the context pointer. In a few places, you use designated initializers and in a few places you use generic initializers. Please use one form or the other for consistency.
Also see the inline comment about the name of the attribute.
| const char *path; /**< URI path of resource */ | ||
| unsigned methods; /**< OR'ed methods this resource allows */ | ||
| coap_handler_t handler; /**< ptr to resource handler */ | ||
| void *context; /**< ptr to user defined context data */ |
There was a problem hiding this comment.
I prefer naming this attribute 'data' rather than 'context'. To me, 'context' includes a connotation of an enveloping entity. This pointer is just user-defined data for the resource. I don't feel strongly about this though -- it's just my preference and I'm OK with 'context'.
There was a problem hiding this comment.
I chose context to be consistent with netdev...
Hm, do you mind a lot? I used designated initializers when they fit into 80 characters, as they're IMO much more readable, and if they would've been used before, would have saved me touching them. I hoped to have chosen the most readable option. |
Contribution description
This PR adds an opaque
contextpointer to coap resources.It allows e.g., parameterizing coap resource handlers.
One use case would be a generic "saul coap handler" which would get it's driver information through the newly introduced context parameter. This handler could then easily be re-used.
Example (pseudocode):