iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / lib / libcoap-4.1.1 / resource.h
1 /* resource.h -- generic resource handling
2  *
3  * Copyright (C) 2010,2011,2014 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use. 
7  */
8
9 /** 
10  * @file resource.h
11  * @brief generic resource handling
12  */
13
14 #ifndef _COAP_RESOURCE_H_
15 #define _COAP_RESOURCE_H_
16
17 #include "config.h"
18 #include "t_list.h"
19
20 #if defined(HAVE_ASSERT_H) && !defined(assert)
21 # include <assert.h>
22 #endif
23
24 #ifndef COAP_RESOURCE_CHECK_TIME
25 /** The interval in seconds to check if resources have changed. */
26 #define COAP_RESOURCE_CHECK_TIME 2
27 #endif /* COAP_RESOURCE_CHECK_TIME */
28
29 #ifndef WITH_CONTIKI
30 #  ifdef COAP_RESOURCES_NOHASH
31 #    include "utlist.h"
32 #  else
33 #    include "uthash.h"
34 #  endif
35 #else /* WITH_CONTIKI */
36 #endif /* WITH_CONTIKI */
37 #include "hashkey.h"
38 #include "async.h"
39 #include "str.h"
40 #include "pdu.h"
41 #include "net.h"
42 #include "subscribe.h"
43
44 /** Definition of message handler function (@sa coap_resource_t). */
45 typedef void (*coap_method_handler_t)(coap_context_t *, struct coap_resource_t *, coap_address_t *,
46         coap_pdu_t *, str * /* token */, coap_pdu_t * /* response */);
47
48 #define COAP_ATTR_FLAGS_RELEASE_NAME  0x1
49 #define COAP_ATTR_FLAGS_RELEASE_VALUE 0x2
50
51 typedef struct coap_attr_t
52 {
53     struct coap_attr_t *next;
54     str name;
55     str value;
56     int flags;
57 } coap_attr_t;
58
59 #define COAP_RESOURCE_FLAGS_RELEASE_URI 0x1
60
61 typedef struct coap_resource_t
62 {
63     unsigned int dirty :1; /**< set to 1 if resource has changed */
64     unsigned int partiallydirty :1; /**< set to 1 if some subscribers have not yet been notified of the last change */
65     unsigned int observable :1; /**< can be observed */
66     unsigned int cacheable :1; /**< can be cached */
67
68     /** 
69      * Used to store handlers for the four coap methods @c GET, @c POST,
70      * @c PUT, and @c DELETE. coap_dispatch() will pass incoming
71      * requests to the handler that corresponds to its request method or
72      * generate a 4.05 response if no handler is available.
73      */
74     coap_method_handler_t handler[4];
75
76     coap_key_t key; /**< the actual key bytes for this resource */
77
78 #ifndef WITH_CONTIKI
79 #ifdef COAP_RESOURCES_NOHASH
80     struct coap_resource_t *next;
81 #else
82     UT_hash_handle hh;
83 #endif
84 #endif /* WITH_CONTIKI */
85
86 #ifndef WITH_CONTIKI
87     coap_attr_t *link_attr; /**< attributes to be included with the link format */
88 #else /* WITH_CONTIKI */
89     LIST_STRUCT(link_attr); /**< attributes to be included with the link format */
90 #endif /* WITH_CONTIKI */
91     LIST_STRUCT(subscribers); /**< list of observers for this resource */
92
93     /**
94      * Request URI for this resource. This field will point into the
95      * static memory. */
96     str uri;
97     int flags;
98
99 } coap_resource_t;
100
101 /** 
102  * Creates a new resource object and initializes the link field to the
103  * string of length @p len.  This function returns the
104  * new coap_resource_t object.
105  * 
106  * @param uri    The URI path of the new resource.
107  * @param len    The length of @p uri.
108  * @param flags  Flags for memory management (in particular release of memory)
109  * 
110  * @return A pointer to the new object or @c NULL on error.
111  */
112 coap_resource_t *coap_resource_init(const unsigned char *uri, size_t len, int flags);
113
114 /**
115  * Registers the given @p resource for @p context. The resource must
116  * have been created by coap_resource_init(), the storage allocated
117  * for the resource will be released by coap_delete_resource().
118  * 
119  * @param context  The context to use.
120  * @param resource The resource to store.
121  */
122 void coap_add_resource(coap_context_t *context, coap_resource_t *resource);
123
124 /** 
125  * Deletes a resource identified by @p key. The storage allocated for
126  * that resource is freed.
127  * 
128  * @param context  The context where the resources are stored.
129  * @param key      The unique key for the resource to delete.
130  * 
131  * @return @c 1 if the resource was found (and destroyed), @c 0 otherwise.
132  */
133 int coap_delete_resource(coap_context_t *context, coap_key_t key);
134
135 /** 
136  * Registers a new attribute with the given @p resource. As the
137  * attributes str fields will point to @p name and @p val the 
138  * caller must ensure that these pointers are valid during the
139  * attribute's lifetime.
140  * 
141  * @param resource  The resource to register the attribute with.
142  * @param name      The attribute's name.
143  * @param nlen      Length of @p name.
144  * @param val       The attribute's value or @c NULL if none.
145  * @param vlen      Length of @p val if specified.
146  * @param flags     Flags for memory management (in particular release of memory)
147  *
148  * @return A pointer to the new attribute or @c NULL on error.
149  */
150 coap_attr_t *coap_add_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen,
151         const unsigned char *val, size_t vlen, int flags);
152
153 /**
154  * Returns @p resource's coap_attr_t object with given @p name if
155  * found, @c NULL otherwise.
156  *
157  * @param resource  The resource to search for attribute @p name.
158  * @param name      Name of the requested attribute.
159  * @param nlen      Actual length of @p name.
160  * @return The first attribute with specified @p name or @c NULL if
161  *         none was found.
162  */
163 coap_attr_t *coap_find_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen);
164
165 /** 
166  * Deletes an attribute
167  * 
168  * @param attr  Pointer to a previously created attribute
169  * 
170  */
171 void coap_delete_attr(coap_attr_t *attr);
172
173 /**
174  * Status word to encode the result of conditional print or copy
175  * operations such as coap_print_link(). The lower 28 bits of
176  * coap_print_status_t are used to encode the number of characters
177  * that has actually been printed, bits 28 to 31 encode the status.
178  * When COAP_PRINT_STATUS_ERROR is set, an error occurred during
179  * output. In this case, the other bits are undefined.
180  * COAP_PRINT_STATUS_TRUNC indicates that the output is truncated,
181  * i.e. the printing would have exceeded the current buffer.
182  */
183 typedef unsigned int coap_print_status_t;
184
185 #define COAP_PRINT_STATUS_MASK  0xF0000000u
186 #define COAP_PRINT_OUTPUT_LENGTH(v) ((v) & ~COAP_PRINT_STATUS_MASK)
187 #define COAP_PRINT_STATUS_ERROR 0x80000000u
188 #define COAP_PRINT_STATUS_TRUNC 0x40000000u
189
190 /** 
191  * Writes a description of this resource in link-format to given text
192  * buffer. @p len must be initialized to the maximum length of @p buf
193  * and will be set to the number of characters actually written if
194  * successful.  This function returns @c 1 on success or @c 0 on
195  * error.
196  * 
197  * @param resource The resource to describe.
198  * @param buf      The output buffer to write the description to.
199  * @param len      Must be initialized to the length of @p buf and 
200  *                 will be set to the length of the printed link description.
201  * @param offset   The offset within the resource description where to
202  *                 start writing into @p buf. This is useful for dealing
203  *                 with the Block2 option. @p offset is updated during
204  *                 output as it is consumed.
205  * 
206  * @return If COAP_PRINT_STATUS_ERROR is set, an error occured. Otherwise,
207  *         the lower 28 bits will indicate the number of characters that
208  *         have actually been output into @p buffer. The flag
209  *         COAP_PRINT_STATUS_TRUNC indicates that the output has been
210  *         truncated. 
211  */
212 coap_print_status_t coap_print_link(const coap_resource_t *resource, unsigned char *buf,
213         size_t *len, size_t *offset);
214
215 /** 
216  * Registers the specified @p handler as message handler for the request type
217  * @p method 
218  * 
219  * @param resource The resource for which the handler shall be registered.
220  * @param method   The CoAP request method to handle. 
221  * @param handler  The handler to register with @p resource.
222  */
223 static inline void coap_register_handler(coap_resource_t *resource, unsigned char method,
224         coap_method_handler_t handler)
225 {
226     assert(resource);
227     assert(
228             method > 0
229                     && (size_t)(method - 1)
230                             < sizeof(resource->handler) / sizeof(coap_method_handler_t));
231     resource->handler[method - 1] = handler;
232 }
233
234 /** 
235  * Returns the resource identified by the unique string @p key. If no
236  * resource was found, this function returns @c NULL.
237  * 
238  * @param context  The context to look for this resource.
239  * @param key      The unique key of the resource.
240  * 
241  * @return A pointer to the resource or @c NULL if not found.
242  */
243 coap_resource_t *coap_get_resource_from_key(coap_context_t *context, coap_key_t key);
244
245 /** 
246  * Calculates the hash key for the resource requested by the
247  * Uri-Options of @p request.  This function calls coap_hash() for
248  * every path segment. 
249  * 
250  * @param request The requesting pdu.
251  * @param key     The resulting hash is stored in @p key
252  */
253 void coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key);
254
255 /** 
256  * @addtogroup observe 
257  */
258
259 /**
260  * Adds the specified peer as observer for @p resource. The
261  * subscription is identified by the given @p token. This function
262  * returns the registered subscription information if the @p observer
263  * has been added, or @c NULL on error.
264  *
265  * @param resource The observed resource.
266  * @param observer The remote peer that wants to received status updates.
267  * @param token The token that identifies this subscription.
268  * @param token_length The actual length of @p token. Must be @c 0 when
269  *        @p token is @c NULL.
270  * @return A pointer to the added/updated subscription information or 
271  *        @c NULL on error.
272  */
273 coap_subscription_t *coap_add_observer(coap_resource_t *resource, const coap_address_t *observer,
274         const str *token);
275
276 /**
277  * Returns a subscription object for given @p peer.
278  *
279  * @param resource The observed resource.
280  * @param peer The address to search for.
281  * @param token The token that identifies this subscription or @c NULL for any
282  *              token.
283  * @return A valid subscription if exists or @c NULL otherwise.
284  */
285 coap_subscription_t *coap_find_observer(coap_resource_t *resource, const coap_address_t *peer,
286         const str *token);
287
288 /**
289  * Marks an observer as alive.
290  *
291  * @param context  The CoAP context to use
292  * @param observer The transport address of the observer
293  * @param token    The corresponding token that has been used for 
294  *   the subscription
295  */
296 void coap_touch_observer(coap_context_t *context, const coap_address_t *observer, const str *token);
297
298 /**
299  * Removes any subscription for @p observer from @p resource and releases
300  * the allocated storage.
301  *
302  * @param resource The observed resource.
303  * @param observer The observer's address.
304  * @param token    The token that identifies this subscription or @c NULL for any
305  *                 token.
306  */
307 void coap_delete_observer(coap_resource_t *resource, const coap_address_t *observer,
308         const str *token);
309
310 /** 
311  * Checks for all known resources, if they are dirty and notifies
312  * subscribed observers.
313  */
314 void coap_check_notify(coap_context_t *context);
315
316 /** @} */
317
318 #endif /* _COAP_RESOURCE_H_ */