libcoap  4.2.0
resource.h
Go to the documentation of this file.
1 /*
2  * resource.h -- generic resource handling
3  *
4  * Copyright (C) 2010,2011,2014,2015 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * This file is part of the CoAP library libcoap. Please see README for terms
7  * of use.
8  */
9 
15 #ifndef COAP_RESOURCE_H_
16 #define COAP_RESOURCE_H_
17 
18 # include <assert.h>
19 
20 #ifndef COAP_RESOURCE_CHECK_TIME
21 
22 #define COAP_RESOURCE_CHECK_TIME 2
23 #endif /* COAP_RESOURCE_CHECK_TIME */
24 
25 #include "uthash.h"
26 #include "async.h"
27 #include "str.h"
28 #include "pdu.h"
29 #include "net.h"
30 #include "subscribe.h"
31 
35 typedef void (*coap_method_handler_t)
37  struct coap_resource_t *,
39  coap_pdu_t *,
40  coap_binary_t * /* token */,
41  coap_string_t * /* query string */,
42  coap_pdu_t * /* response */);
43 
44 #define COAP_ATTR_FLAGS_RELEASE_NAME 0x1
45 #define COAP_ATTR_FLAGS_RELEASE_VALUE 0x2
46 
47 typedef struct coap_attr_t {
48  struct coap_attr_t *next;
51  int flags;
52 } coap_attr_t;
53 
55 #define COAP_RESOURCE_FLAGS_RELEASE_URI 0x1
56 
61 #define COAP_RESOURCE_FLAGS_NOTIFY_NON 0x0
62 
67 #define COAP_RESOURCE_FLAGS_NOTIFY_CON 0x2
68 
69 typedef struct coap_resource_t {
70  unsigned int dirty:1;
71  unsigned int partiallydirty:1;
73  unsigned int observable:1;
74  unsigned int cacheable:1;
75  unsigned int is_unknown:1;
85 
87 
97  int flags;
98 
103  unsigned int observe;
104 
109  void *user_data;
110 
112 
145  int flags);
146 
147 
175 
186  resource->flags = (resource->flags &
189 }
190 
201  r->user_data = data;
202 }
203 
212 COAP_STATIC_INLINE void *
214  return r->user_data;
215 }
216 
225 void coap_add_resource(coap_context_t *context, coap_resource_t *resource);
226 
237 int coap_delete_resource(coap_context_t *context, coap_resource_t *resource);
238 
245 
279  int flags);
280 
291  coap_str_const_t *name);
292 
300 void coap_delete_attr(coap_attr_t *attr);
301 
311 typedef unsigned int coap_print_status_t;
312 
313 #define COAP_PRINT_STATUS_MASK 0xF0000000u
314 #define COAP_PRINT_OUTPUT_LENGTH(v) ((v) & ~COAP_PRINT_STATUS_MASK)
315 #define COAP_PRINT_STATUS_ERROR 0x80000000u
316 #define COAP_PRINT_STATUS_TRUNC 0x40000000u
317 
340  unsigned char *buf,
341  size_t *len,
342  size_t *offset);
343 
353  unsigned char method,
354  coap_method_handler_t handler);
355 
366  coap_str_const_t *uri_path);
367 
389  coap_session_t *session,
390  const coap_binary_t *token,
391  coap_string_t *query,
392  int has_block2,
393  coap_block_t block2);
394 
405  coap_session_t *session,
406  const coap_binary_t *token);
407 
416 void coap_touch_observer(coap_context_t *context,
417  coap_session_t *session,
418  const coap_binary_t *token);
419 
432  coap_session_t *session,
433  const coap_binary_t *token);
434 
441 void coap_delete_observers(coap_context_t *context, coap_session_t *session);
442 
447 void coap_check_notify(coap_context_t *context);
448 
449 #define RESOURCES_ADD(r, obj) \
450  HASH_ADD(hh, (r), uri_path->s[0], (obj)->uri_path->length, (obj))
451 
452 #define RESOURCES_DELETE(r, obj) \
453  HASH_DELETE(hh, (r), (obj))
454 
455 #define RESOURCES_ITER(r,tmp) \
456  coap_resource_t *tmp, *rtmp; \
457  HASH_ITER(hh, (r), tmp, rtmp)
458 
459 #define RESOURCES_FIND(r, k, res) { \
460  HASH_FIND(hh, (r), (k)->s, (k)->length, (res)); \
461  }
462 
466  unsigned char *,
467  size_t *, size_t,
468  coap_opt_t *);
469 
470 void
472  coap_session_t *,
473  const coap_binary_t *);
474 
487  resource->observable = mode ? 1 : 0;
488 }
489 
499 int
501  const coap_string_t *query);
502 
512  if (resource)
513  return resource->uri_path;
514  return NULL;
515 }
516 
520 COAP_DEPRECATED int
522  const coap_string_t *query);
523 
524 #endif /* COAP_RESOURCE_H_ */
coap_attr_t * coap_find_attr(coap_resource_t *resource, coap_str_const_t *name)
Returns resource&#39;s coap_attr_t object with given name if found, NULL otherwise.
Definition: resource.c:389
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition: option.h:25
unsigned int cacheable
can be cached
Definition: resource.h:74
int flags
Definition: resource.h:51
COAP_DEPRECATED int coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query)
Definition: resource.c:832
State management for asynchronous messages.
UT_hash_handle hh
Definition: resource.h:86
coap_attr_t * link_attr
attributes to be included with the link format
Definition: resource.h:88
unsigned int partiallydirty
set to 1 if some subscribers have not yet been notified of the last change
Definition: resource.h:71
unsigned int is_unknown
resource created for unknown handler
Definition: resource.h:75
COAP_STATIC_INLINE void coap_resource_set_mode(coap_resource_t *resource, int mode)
Sets the notification message type of resource resource to given mode.
Definition: resource.h:185
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
Definition: resource.c:869
COAP_STATIC_INLINE void coap_resource_set_get_observable(coap_resource_t *resource, int mode)
Set whether a resource is observable.
Definition: resource.h:486
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
Definition: resource.c:494
void coap_delete_observers(coap_context_t *context, coap_session_t *session)
Removes any subscription for session and releases the allocated storage.
Definition: resource.c:729
Coap string data definition.
Definition: str.h:25
int coap_delete_resource(coap_context_t *context, coap_resource_t *resource)
Deletes a resource identified by resource.
Definition: resource.c:474
Coap string data definition with const data.
Definition: str.h:33
coap_str_const_t * name
Definition: resource.h:49
coap_resource_t * coap_resource_init(coap_str_const_t *uri_path, int flags)
Creates a new resource object and initializes the link field to the string uri_path.
Definition: resource.c:300
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
unsigned int dirty
set to 1 if resource has changed
Definition: resource.h:70
unsigned int observable
can be observed
Definition: resource.h:73
COAP_STATIC_INLINE void * coap_resource_get_userdata(coap_resource_t *r)
Gets the user_data.
Definition: resource.h:213
void coap_handle_failed_notify(coap_context_t *, coap_session_t *, const coap_binary_t *)
Definition: resource.c:930
coap_subscription_t * coap_add_observer(coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token, coap_string_t *query, int has_block2, coap_block_t block2)
Adds the specified peer as observer for resource.
Definition: resource.c:623
coap_print_status_t coap_print_link(const coap_resource_t *resource, unsigned char *buf, size_t *len, size_t *offset)
Writes a description of this resource in link-format to given text buffer.
Definition: resource.c:524
#define COAP_DEPRECATED
Definition: libcoap.h:49
#define COAP_RESOURCE_FLAGS_NOTIFY_NON
Notifications will be sent non-confirmable by default.
Definition: resource.h:61
struct coap_resource_t coap_resource_t
Coap binary data definition.
Definition: str.h:43
coap_subscription_t * coap_find_observer(coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token)
Returns a subscription object for given peer.
Definition: resource.c:587
struct coap_attr_t coap_attr_t
coap_str_const_t * value
Definition: resource.h:50
coap_subscription_t * subscribers
list of observers for this resource
Definition: resource.h:89
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
int coap_delete_observer(coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token)
Removes any subscription for observer from resource and releases the allocated storage.
Definition: resource.c:703
Pre-defined constants that reflect defaults for CoAP.
coap_print_status_t coap_print_wellknown(coap_context_t *, unsigned char *, size_t *, size_t, coap_opt_t *)
Prints the names of all known resources to buf.
Definition: resource.c:169
coap_attr_t * coap_add_attr(coap_resource_t *resource, coap_str_const_t *name, coap_str_const_t *value, int flags)
Registers a new attribute with the given resource.
Definition: resource.c:352
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Notifications will be sent confirmable by default.
Definition: resource.h:67
Subscriber information.
Definition: subscribe.h:56
Structure of Block options.
Definition: block.h:36
void * user_data
This pointer is under user control.
Definition: resource.h:109
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
Definition: resource.c:452
struct coap_attr_t * next
Definition: resource.h:48
unsigned int coap_print_status_t
Status word to encode the result of conditional print or copy operations such as coap_print_link().
Definition: resource.h:311
COAP_STATIC_INLINE void coap_resource_set_userdata(coap_resource_t *r, void *data)
Sets the user_data.
Definition: resource.h:200
COAP_STATIC_INLINE coap_str_const_t * coap_resource_get_uri_path(coap_resource_t *resource)
Get the UriPath from a resource.
Definition: resource.h:511
void coap_register_handler(coap_resource_t *resource, unsigned char method, coap_method_handler_t handler)
Registers the specified handler as message handler for the request type method.
Definition: resource.c:577
coap_str_const_t * uri_path
Request URI Path for this resource.
Definition: resource.h:96
coap_resource_t * coap_get_resource_from_uri_path(coap_context_t *context, coap_str_const_t *uri_path)
Returns the resource identified by the unique string uri_path.
Definition: resource.c:515
void(* coap_method_handler_t)(coap_context_t *, struct coap_resource_t *, coap_session_t *, coap_pdu_t *, coap_binary_t *, coap_string_t *, coap_pdu_t *)
Definition of message handler function (.
Definition: resource.h:36
unsigned int observe
The next value for the Observe option.
Definition: resource.h:103
void coap_touch_observer(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Marks an observer as alive.
Definition: resource.c:690
coap_resource_t * coap_resource_unknown_init(coap_method_handler_t put_handler)
Creates a new resource object for the unknown resource handler with support for PUT.
Definition: resource.c:334
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:148
int coap_resource_notify_observers(coap_resource_t *resource, const coap_string_t *query)
Initiate the sending of an Observe packet for all observers of resource, optionally matching query if...
Definition: resource.c:837
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
Definition: resource.c:406