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 
60 #define COAP_RESOURCE_FLAGS_RELEASE_URI 0x1
61 #define COAP_RESOURCE_FLAGS_NOTIFY_NON 0x0
62 #define COAP_RESOURCE_FLAGS_NOTIFY_CON 0x2
63 
64 typedef struct coap_resource_t {
65  unsigned int dirty:1;
66  unsigned int partiallydirty:1;
68  unsigned int observable:1;
69  unsigned int cacheable:1;
78 
81 #ifdef COAP_RESOURCES_NOHASH
82  struct coap_resource_t *next;
83 #else
85 #endif
86 
94  str uri;
95  int flags;
96 
98 
109 coap_resource_t *coap_resource_init(const unsigned char *uri,
110  size_t len, int flags);
111 
112 
118 static inline void
120  r->flags = (r->flags & !COAP_RESOURCE_FLAGS_NOTIFY_CON) | mode;
121 }
122 
131 void coap_add_resource(coap_context_t *context, coap_resource_t *resource);
132 
144 
151 
169  const unsigned char *name,
170  size_t nlen,
171  const unsigned char *val,
172  size_t vlen,
173  int flags);
174 
186  const unsigned char *name,
187  size_t nlen);
188 
195 void coap_delete_attr(coap_attr_t *attr);
196 
206 typedef unsigned int coap_print_status_t;
207 
208 #define COAP_PRINT_STATUS_MASK 0xF0000000u
209 #define COAP_PRINT_OUTPUT_LENGTH(v) ((v) & ~COAP_PRINT_STATUS_MASK)
210 #define COAP_PRINT_STATUS_ERROR 0x80000000u
211 #define COAP_PRINT_STATUS_TRUNC 0x40000000u
212 
235  unsigned char *buf,
236  size_t *len,
237  size_t *offset);
238 
247 static inline void
249  unsigned char method,
251  assert(resource);
252  assert(method > 0 && (size_t)(method-1) < sizeof(resource->handler)/sizeof(coap_method_handler_t));
253  resource->handler[method-1] = handler;
254 }
255 
266  coap_key_t key);
267 
275 void coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key);
276 
296  const coap_endpoint_t *local_interface,
297  const coap_address_t *observer,
298  const str *token);
299 
310  const coap_address_t *peer,
311  const str *token);
312 
321 void coap_touch_observer(coap_context_t *context,
322  const coap_address_t *observer,
323  const str *token);
324 
337  const coap_address_t *observer,
338  const str *token);
339 
344 void coap_check_notify(coap_context_t *context);
345 
346 #ifdef COAP_RESOURCES_NOHASH
347 
348 #define RESOURCES_ADD(r, obj) \
349  LL_PREPEND((r), (obj))
350 
351 #define RESOURCES_DELETE(r, obj) \
352  LL_DELETE((r), (obj))
353 
354 #define RESOURCES_ITER(r,tmp) \
355  coap_resource_t *tmp; \
356  LL_FOREACH((r), tmp)
357 
358 #define RESOURCES_FIND(r, k, res) { \
359  coap_resource_t *tmp; \
360  (res) = tmp = NULL; \
361  LL_FOREACH((r), tmp) { \
362  if (memcmp((k), tmp->key, sizeof(coap_key_t)) == 0) { \
363  (res) = tmp; \
364  break; \
365  } \
366  } \
367  }
368 #else /* COAP_RESOURCES_NOHASH */
369 
370 #define RESOURCES_ADD(r, obj) \
371  HASH_ADD(hh, (r), key, sizeof(coap_key_t), (obj))
372 
373 #define RESOURCES_DELETE(r, obj) \
374  HASH_DELETE(hh, (r), (obj))
375 
376 #define RESOURCES_ITER(r,tmp) \
377  coap_resource_t *tmp, *rtmp; \
378  HASH_ITER(hh, (r), tmp, rtmp)
379 
380 #define RESOURCES_FIND(r, k, res) { \
381  HASH_FIND(hh, (r), (k), sizeof(coap_key_t), (res)); \
382  }
383 
384 #endif /* COAP_RESOURCES_NOHASH */
385 
389  unsigned char *,
390  size_t *, size_t,
391  coap_opt_t *);
392 
394  const coap_address_t *,
395  const str *);
396 
397 #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:57
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:119
UT_hash_handle hh
Definition: resource.h:84
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:87
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:39
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:94
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:79
#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:248
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:88
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
Definition: resource.h:62
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:206
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:77
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:77