libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
resource.h
Go to the documentation of this file.
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 
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 
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 
45 typedef void (*coap_method_handler_t)
46  (coap_context_t *, struct coap_resource_t *, coap_address_t *, coap_pdu_t *,
47  str * /* token */, coap_pdu_t * /* response */);
48 
49 #define COAP_ATTR_FLAGS_RELEASE_NAME 0x1
50 #define COAP_ATTR_FLAGS_RELEASE_VALUE 0x2
51 
52 typedef struct coap_attr_t {
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  unsigned int dirty:1;
63  unsigned int partiallydirty:1;
64  unsigned int observable:1;
65  unsigned int cacheable:1;
74 
77 #ifndef WITH_CONTIKI
78 #ifdef COAP_RESOURCES_NOHASH
79  struct coap_resource_t *next;
80 #else
82 #endif
83 #endif /* WITH_CONTIKI */
84 
85 #ifndef WITH_CONTIKI
87 #else /* WITH_CONTIKI */
89 #endif /* WITH_CONTIKI */
90  LIST_STRUCT(subscribers);
96  str uri;
97  int flags;
98 
100 
112 coap_resource_t *coap_resource_init(const unsigned char *uri, size_t len, int flags);
113 
122 void coap_add_resource(coap_context_t *context, coap_resource_t *resource);
123 
134 
151  const unsigned char *name, size_t nlen,
152  const unsigned char *val, size_t vlen,
153  int flags);
154 
166  const unsigned char *name, size_t nlen);
167 
174 void coap_delete_attr(coap_attr_t *attr);
175 
186 typedef unsigned int coap_print_status_t;
187 
188 #define COAP_PRINT_STATUS_MASK 0xF0000000u
189 #define COAP_PRINT_OUTPUT_LENGTH(v) ((v) & ~COAP_PRINT_STATUS_MASK)
190 #define COAP_PRINT_STATUS_ERROR 0x80000000u
191 #define COAP_PRINT_STATUS_TRUNC 0x40000000u
192 
216  unsigned char *buf, size_t *len, size_t *offset);
217 
226 static inline void
228  unsigned char method, coap_method_handler_t handler) {
229  assert(resource);
230  assert(method > 0 && (size_t)(method-1) < sizeof(resource->handler)/sizeof(coap_method_handler_t));
231  resource->handler[method-1] = handler;
232 }
233 
244  coap_key_t key);
245 
254 void coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key);
255 
275  const coap_address_t *observer,
276  const str *token);
277 
288  const coap_address_t *peer,
289  const str *token);
290 
299 void coap_touch_observer(coap_context_t *context,
300  const coap_address_t *observer,
301  const str *token);
302 
312 void coap_delete_observer(coap_resource_t *resource,
313  const coap_address_t *observer,
314  const str *token);
315 
320 void coap_check_notify(coap_context_t *context);
321 
324 #endif /* _COAP_RESOURCE_H_ */
unsigned int cacheable
can be cached
Definition: resource.h:65
unsigned char coap_key_t[4]
Definition: hashkey.h:19
int flags
Definition: resource.h:56
Wrappers for list structures and functions.
state management for asynchronous messages
UT_hash_handle hh
Definition: resource.h:81
coap_attr_t * link_attr
attributes to be included with the link format
Definition: resource.h:86
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:503
unsigned int partiallydirty
set to 1 if some subscribers have not yet been notified of the last change
Definition: resource.h:63
int coap_delete_resource(coap_context_t *context, coap_key_t key)
Deletes a resource identified by key.
Definition: resource.c:451
static coap_uri_t uri
Definition: client.c:36
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
Definition: resource.c:764
str name
Definition: resource.h:54
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:424
LIST_STRUCT(subscribers)
list of observers for this resource
str uri
Request URI for this resource.
Definition: resource.h:96
Header structure for CoAP PDUs.
Definition: pdu.h:206
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:589
coap_key_t key
the actual key bytes for this resource
Definition: resource.h:75
unsigned int dirty
set to 1 if resource has changed
Definition: resource.h:62
unsigned int observable
can be observed
Definition: resource.h:64
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:227
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:538
coap_attr_t * coap_find_attr(coap_resource_t *resource, const unsigned char *name, size_t nlen)
Returns resource's coap_attr_t object with given name if found, NULL otherwise.
Definition: resource.c:383
struct coap_resource_t coap_resource_t
str value
Definition: resource.h:55
struct coap_attr_t coap_attr_t
method_t method
Definition: client.c:51
coap_subscription_t * coap_add_observer(coap_resource_t *resource, const coap_address_t *observer, const str *token)
Adds the specified peer as observer for resource.
Definition: resource.c:607
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:341
int flags
Definition: client.c:26
Subscriber information.
Definition: subscribe.h:41
Definition: str.h:14
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
Definition: resource.c:440
definition of hash key type and helper functions
struct coap_attr_t * next
Definition: resource.h:53
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:307
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:186
void(* coap_method_handler_t)(coap_context_t *, struct coap_resource_t *, coap_address_t *, coap_pdu_t *, str *, coap_pdu_t *)
Definition of message handler function (.
Definition: resource.h:46
The CoAP stack's global state is stored in a coap_context_t object.
Definition: net.h:97
void coap_touch_observer(coap_context_t *context, const coap_address_t *observer, const str *token)
Marks an observer as alive.
Definition: resource.c:643
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
Definition: resource.c:405
void 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:674
coap_method_handler_t handler[4]
Used to store handlers for the four coap methods GET, POST, PUT, and DELETE.
Definition: resource.h:73