resource.h: Introduce new field user_data

The library API lacks a way to store context for the handlers. A
LED-handler for example needs to now on which pin the specific LED
is connected in order to control it. Or a higher level API would like to save the
object pointer to the object responsible for the resource.

This change will introduce a void pointer for the user to save context.
He can then retrieve the context in the handler if needed.
This commit is contained in:
Serafin Leschke committed 2018-03-13 09:29:53 +01:00
1 parent 3870bfc809
commit e394ca2953
1 file changed
+32
+32
View File
@@ -112,6 +112,12 @@ typedef struct coap_resource_t {
*/
unsigned int observe;
/**
* This pointer is under user control. It can be used to store context for
* the coap handler.
*/
void *user_data;
} coap_resource_t;
/**
@@ -138,6 +144,32 @@ coap_resource_set_mode(coap_resource_t *r, int mode) {
r->flags = (r->flags & !COAP_RESOURCE_FLAGS_NOTIFY_CON) | mode;
}
/**
* Sets the user_data. The user_data is exclusively used by the library-user
* and can be used as context in the handler functions.
*
* @param r Resource to attach the data to
* @param data Data to attach to the user_data field. This pointer is only used for
* storage, the data remains under user control
*/
COAP_STATIC_INLINE void
coap_resource_set_userdata(coap_resource_t *r, void *data) {
r->user_data = data;
}
/**
* Gets the user_data. The user_data is exclusively used by the library-user
* and can be used as context in the handler functions.
*
* @param r Resource to retrieve the user_darta from
*
* @return The user_data pointer
*/
COAP_STATIC_INLINE void *
coap_resource_get_userdata(coap_resource_t *r) {
return r->user_data;
}
/**
* Registers the given @p resource for @p context. The resource must have been
* created by coap_resource_init(), the storage allocated for the resource will