libcoap  4.1.2
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 #ifdef COAP_RESOURCES_NOHASH
26 # include "utlist.h"
27 #else
28 # include "uthash.h"
29 #endif
30 
31 #include "hashkey.h"
32 #include "async.h"
33 #include "str.h"
34 #include "pdu.h"
35 #include "net.h"
36 #include "subscribe.h"
37 
41 typedef void (*coap_method_handler_t)
43  struct coap_resource_t *,
44  const coap_endpoint_t *,
46  coap_pdu_t *,
47  str * /* token */,
48  coap_pdu_t * /* response */);
49 
50 #define COAP_ATTR_FLAGS_RELEASE_NAME 0x1
51 #define COAP_ATTR_FLAGS_RELEASE_VALUE 0x2
52 
53 typedef struct coap_attr_t {
54  struct coap_attr_t *next;
55  str name;
56  str value;
57  int flags;
58 } coap_attr_t;
59 
61 #define COAP_RESOURCE_FLAGS_RELEASE_URI 0x1
62 
67 #define COAP_RESOURCE_FLAGS_NOTIFY_NON 0x0
68 
73 #define COAP_RESOURCE_FLAGS_NOTIFY_CON 0x2
74 
75 typedef struct coap_resource_t {
76  unsigned int dirty:1;
77  unsigned int partiallydirty:1;
79  unsigned int observable:1;
80  unsigned int cacheable:1;
89 
92 #ifdef COAP_RESOURCES_NOHASH
93  struct coap_resource_t *next;
94 #else
96 #endif
97 
105  str uri;
106  int flags;
107 
109 
120 coap_resource_t *coap_resource_init(const unsigned char *uri,
121  size_t len, int flags);
122 
123 
129 static inline void
131  r->flags = (r->flags & !COAP_RESOURCE_FLAGS_NOTIFY_CON) | mode;
132 }
133 
142 void coap_add_resource(coap_context_t *context, coap_resource_t *resource);
143 
155 
162 
180  const unsigned char *name,
181  size_t nlen,
182  const unsigned char *val,
183  size_t vlen,
184  int flags);
185 
197  const unsigned char *name,
198  size_t nlen);
199 
206 void coap_delete_attr(coap_attr_t *attr);
207 
217 typedef unsigned int coap_print_status_t;
218 
219 #define COAP_PRINT_STATUS_MASK 0xF0000000u
220 #define COAP_PRINT_OUTPUT_LENGTH(v) ((v) & ~COAP_PRINT_STATUS_MASK)
221 #define COAP_PRINT_STATUS_ERROR 0x80000000u
222 #define COAP_PRINT_STATUS_TRUNC 0x40000000u
223 
246  unsigned char *buf,
247  size_t *len,
248  size_t *offset);
249 
258 static inline void
260  unsigned char method,
262  assert(resource);
263  assert(method > 0 && (size_t)(method-1) < sizeof(resource->handler)/sizeof(coap_method_handler_t));
264  resource->handler[method-1] = handler;
265 }
266 
277  coap_key_t key);
278 
286 void coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key);
287 
307  const coap_endpoint_t *local_interface,
308  const coap_address_t *observer,
309  const str *token);
310 
321  const coap_address_t *peer,
322  const str *token);
323 
332 void coap_touch_observer(coap_context_t *context,
333  const coap_address_t *observer,
334  const str *token);
335 
348  const coap_address_t *observer,
349  const str *token);
350 
355 void coap_check_notify(coap_context_t *context);
356 
357 #ifdef COAP_RESOURCES_NOHASH
358 
359 #define RESOURCES_ADD(r, obj) \
360  LL_PREPEND((r), (obj))
361 
362 #define RESOURCES_DELETE(r, obj) \
363  LL_DELETE((r), (obj))
364 
365 #define RESOURCES_ITER(r,tmp) \
366  coap_resource_t *tmp; \
367  LL_FOREACH((r), tmp)
368 
369 #define RESOURCES_FIND(r, k, res) { \
370  coap_resource_t *tmp; \
371  (res) = tmp = NULL; \
372  LL_FOREACH((r), tmp) { \
373  if (memcmp((k), tmp->key, sizeof(coap_key_t)) == 0) { \
374  (res) = tmp; \
375  break; \
376  } \
377  } \
378  }
379 #else /* COAP_RESOURCES_NOHASH */
380 
381 #define RESOURCES_ADD(r, obj) \
382  HASH_ADD(hh, (r), key, sizeof(coap_key_t), (obj))
383 
384 #define RESOURCES_DELETE(r, obj) \
385  HASH_DELETE(hh, (r), (obj))
386 
387 #define RESOURCES_ITER(r,tmp) \
388  coap_resource_t *tmp, *rtmp; \
389  HASH_ITER(hh, (r), tmp, rtmp)
390 
391 #define RESOURCES_FIND(r, k, res) { \
392  HASH_FIND(hh, (r), (k), sizeof(coap_key_t), (res)); \
393  }
394 
395 #endif /* COAP_RESOURCES_NOHASH */
396 
400  unsigned char *,
401  size_t *, size_t,
402  coap_opt_t *);
403 
405  const coap_address_t *,
406  const str *);
407 
408 #endif /* _COAP_RESOURCE_H_ */
unsigned char coap_key_t[4]
Definition: hashkey.h:20
int flags
Definition: resource.h:57
multi-purpose address abstraction
Definition: address.h:59
State management for asynchronous messages.
static void coap_resource_set_mode(coap_resource_t *r, int mode)
Sets the notification message type of resource r to given mode which must be one of COAP_RESOURCE_FLA...
Definition: resource.h:130
UT_hash_handle hh
Definition: resource.h:95
int coap_delete_observer(coap_resource_t *resource, const coap_address_t *observer, const str *token)
Removes any subscription for observer from resource and releases the allocated storage.
Definition: resource.c:593
coap_attr_t * link_attr
attributes to be included with the link format
Definition: resource.h:98
coap_resource_t * coap_get_resource_from_key(coap_context_t *context, coap_key_t key)
Returns the resource identified by the unique string key.
Definition: resource.c:469
int coap_delete_resource(coap_context_t *context, coap_key_t key)
Deletes a resource identified by key.
Definition: resource.c:428
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
Definition: resource.c:689
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_io.h:35
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
Definition: resource.c:449
str name
Definition: resource.h:55
void coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key)
Calculates the hash key for the resource requested by the Uri-Options of request. ...
Definition: resource.c:383
str uri
Request URI for this resource.
Definition: resource.h:105
Header structure for CoAP PDUs.
Definition: pdu.h:227
coap_subscription_t * coap_find_observer(coap_resource_t *resource, const coap_address_t *peer, const str *token)
Returns a subscription object for given peer.
Definition: resource.c:524
coap_key_t key
the actual key bytes for this resource
Definition: resource.h:90
#define assert(...)
Definition: mem.c:17
static 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.h:259
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:478
coap_attr_t * coap_find_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen)
Returns resource&#39;s coap_attr_t object with given name if found, NULL otherwise.
Definition: resource.c:349
struct coap_resource_t coap_resource_t
coap_subscription_t * coap_add_observer(coap_resource_t *resource, const coap_endpoint_t *local_interface, const coap_address_t *observer, const str *token)
Adds the specified peer as observer for resource.
Definition: resource.c:542
str value
Definition: resource.h:56
struct coap_attr_t coap_attr_t
coap_subscription_t * subscribers
list of observers for this resource
Definition: resource.h:99
coap_attr_t * coap_add_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen, const unsigned char *val, size_t vlen, int flags)
Registers a new attribute with the given resource.
Definition: resource.c:314
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:167
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Notifications will be sent confirmable by default.
Definition: resource.h:73
Subscriber information.
Definition: subscribe.h:54
Definition: str.h:15
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
Definition: resource.c:399
definition of hash key type and helper functions
struct coap_attr_t * next
Definition: resource.h:54
coap_resource_t * coap_resource_init(const unsigned char *uri, size_t len, int flags)
Creates a new resource object and initializes the link field to the string of length len...
Definition: resource.c:288
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:217
unsigned char 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
void coap_handle_failed_notify(coap_context_t *, const coap_address_t *, const str *)
Definition: resource.c:748
void(* coap_method_handler_t)(coap_context_t *, struct coap_resource_t *, const coap_endpoint_t *, coap_address_t *, coap_pdu_t *, str *, coap_pdu_t *)
Definition of message handler function (.
Definition: resource.h:42
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:76
void coap_touch_observer(coap_context_t *context, const coap_address_t *observer, const str *token)
Marks an observer as alive.
Definition: resource.c:580
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
Definition: resource.c:366
coap_method_handler_t handler[4]
Used to store handlers for the four coap methods GET, POST, PUT, and DELETE.
Definition: resource.h:88