libcoap  4.3.0beta
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 #ifndef COAP_RESOURCE_CHECK_TIME
20 #define COAP_RESOURCE_CHECK_TIME 2
21 #endif /* COAP_RESOURCE_CHECK_TIME */
22 
23 #include "uthash.h"
24 #include "async.h"
25 #include "block.h"
26 #include "str.h"
27 #include "pdu.h"
28 #include "net.h"
29 #include "subscribe.h"
30 
40 typedef void (*coap_method_handler_t)
41  (coap_context_t *,
42  struct coap_resource_t *,
44  coap_pdu_t *,
45  coap_binary_t * /* token */,
46  coap_string_t * /* query string */,
47  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;
56  int flags;
58 
60 #define COAP_RESOURCE_FLAGS_RELEASE_URI 0x1
61 
67 #define COAP_RESOURCE_FLAGS_NOTIFY_NON 0x0
68 
73 #define COAP_RESOURCE_FLAGS_NOTIFY_CON 0x2
74 
83 #define COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS 0x4
84 
85 typedef struct coap_resource_t {
86  unsigned int dirty:1;
87  unsigned int partiallydirty:1;
89  unsigned int observable:1;
90  unsigned int cacheable:1;
91  unsigned int is_unknown:1;
92  unsigned int is_proxy_uri:1;
102 
104 
114  int flags;
115 
120  unsigned int observe;
121 
126 
131 
136 
141  void *user_data;
142 
144 
177  int flags);
178 
179 
207 
224  size_t host_name_count, const char *host_name_list[]);
225 
236  resource->flags = (resource->flags &
239 }
240 
251  resource->user_data = data;
252 }
253 
262 COAP_STATIC_INLINE void *
264  return resource->user_data;
265 }
266 
275 void coap_add_resource(coap_context_t *context, coap_resource_t *resource);
276 
287 int coap_delete_resource(coap_context_t *context, coap_resource_t *resource);
288 
297 
329  coap_str_const_t *name,
330  coap_str_const_t *value,
331  int flags);
332 
343  coap_str_const_t *name);
344 
352 void coap_delete_attr(coap_attr_t *attr);
353 
363 typedef unsigned int coap_print_status_t;
364 
365 #define COAP_PRINT_STATUS_MASK 0xF0000000u
366 #define COAP_PRINT_OUTPUT_LENGTH(v) ((v) & ~COAP_PRINT_STATUS_MASK)
367 #define COAP_PRINT_STATUS_ERROR 0x80000000u
368 #define COAP_PRINT_STATUS_TRUNC 0x40000000u
369 
392  unsigned char *buf,
393  size_t *len,
394  size_t *offset);
395 
405  coap_request_t method,
406  coap_method_handler_t handler);
407 
420  coap_str_const_t *uri_path);
421 
422 #define RESOURCES_ADD(r, obj) \
423  HASH_ADD(hh, (r), uri_path->s[0], (obj)->uri_path->length, (obj))
424 
425 #define RESOURCES_DELETE(r, obj) \
426  HASH_DELETE(hh, (r), (obj))
427 
428 #define RESOURCES_ITER(r,tmp) \
429  coap_resource_t *tmp, *rtmp; \
430  HASH_ITER(hh, (r), tmp, rtmp)
431 
432 #define RESOURCES_FIND(r, k, res) { \
433  HASH_FIND(hh, (r), (k)->s, (k)->length, (res)); \
434  }
435 
461  coap_session_t *session,
462  const coap_binary_t *token,
463  coap_string_t *query,
464  int has_block2,
465  coap_block_t block2,
466  uint8_t code);
467 
478  coap_session_t *session,
479  const coap_binary_t *token);
480 
489 void coap_touch_observer(coap_context_t *context,
490  coap_session_t *session,
491  const coap_binary_t *token);
492 
505  coap_session_t *session,
506  const coap_binary_t *token);
507 
514 void coap_delete_observers(coap_context_t *context, coap_session_t *session);
515 
528  resource->observable = mode ? 1 : 0;
529 }
530 
540 int
542  const coap_string_t *query);
543 
553  if (resource)
554  return resource->uri_path;
555  return NULL;
556 }
557 
564 void coap_check_notify(coap_context_t *context);
565 
567  unsigned char *,
568  size_t *, size_t,
569  coap_opt_t *);
570 
571 void
573  coap_session_t *,
574  const coap_binary_t *);
575 
579 COAP_DEPRECATED int
581  const coap_string_t *query);
582 
583 #endif /* COAP_RESOURCE_H_ */
State management for asynchronous messages.
coap_resource_t * coap_resource_proxy_uri_init(coap_method_handler_t handler, size_t host_name_count, const char *host_name_list[])
Creates a new resource object for handling proxy URIs.
Definition: resource.c:356
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:41
#define COAP_RESOURCE_FLAGS_NOTIFY_NON
Notifications will be sent non-confirmable by default.
Definition: resource.h:67
COAP_STATIC_INLINE void coap_resource_set_userdata(coap_resource_t *resource, void *data)
Sets the user_data.
Definition: resource.h:250
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:612
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
Definition: resource.c:578
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:235
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
Definition: resource.c:459
COAP_STATIC_INLINE void * coap_resource_get_userdata(coap_resource_t *resource)
Gets the user_data.
Definition: resource.h:263
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Notifications will be sent confirmable.
Definition: resource.h:73
void coap_register_handler(coap_resource_t *resource, coap_request_t method, coap_method_handler_t handler)
Registers the specified handler as message handler for the request type method.
Definition: resource.c:665
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:335
struct coap_attr_t coap_attr_t
coap_attr_t * coap_find_attr(coap_resource_t *resource, coap_str_const_t *name)
Returns resource's coap_attr_t object with given name if found, NULL otherwise.
Definition: resource.c:442
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:363
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:301
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
Definition: resource.c:524
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:405
int coap_delete_resource(coap_context_t *context, coap_resource_t *resource)
Deletes a resource identified by resource.
Definition: resource.c:553
struct coap_resource_t coap_resource_t
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:948
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:795
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, uint8_t code)
Adds the specified peer as observer for resource.
Definition: resource.c:711
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:675
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:821
COAP_STATIC_INLINE void coap_resource_set_get_observable(coap_resource_t *resource, int mode)
Set whether a resource is observable.
Definition: resource.h:527
void coap_touch_observer(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Flags that data is ready to be sent to observers.
Definition: resource.c:782
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:552
#define COAP_DEPRECATED
Definition: libcoap.h:46
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
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
Pre-defined constants that reflect defaults for CoAP.
coap_request_t
Definition: pdu.h:81
COAP_DEPRECATED int coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query)
Definition: resource.c:943
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
void coap_handle_failed_notify(coap_context_t *, coap_session_t *, const coap_binary_t *)
Definition: resource.c:1062
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:603
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
Definition: resource.c:999
coap_str_const_t * value
Definition: resource.h:55
struct coap_attr_t * next
Definition: resource.h:53
coap_str_const_t * name
Definition: resource.h:54
int flags
Definition: resource.h:56
CoAP binary data definition.
Definition: str.h:48
Structure of Block options.
Definition: block.h:33
The CoAP stack's global state is stored in a coap_context_t object.
Definition: net.h:141
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:86
unsigned int partiallydirty
set to 1 if some subscribers have not yet been notified of the last change
Definition: resource.h:87
coap_subscription_t * subscribers
list of observers for this resource
Definition: resource.h:106
void * user_data
This pointer is under user control.
Definition: resource.h:141
coap_str_const_t ** proxy_name_list
Array valid names this host is known by (proxy support)
Definition: resource.h:135
coap_str_const_t * uri_path
Request URI Path for this resource.
Definition: resource.h:113
unsigned int observe
The next value for the Observe option.
Definition: resource.h:120
unsigned int cacheable
can be cached
Definition: resource.h:90
coap_context_t * context
Pointer back to the context that 'owns' this resource.
Definition: resource.h:125
coap_method_handler_t handler[7]
Used to store handlers for the seven coap methods GET, POST, PUT, DELETE, FETCH, PATCH and IPATCH.
Definition: resource.h:101
unsigned int is_proxy_uri
resource created for proxy URI handler
Definition: resource.h:92
unsigned int is_unknown
resource created for unknown handler
Definition: resource.h:91
coap_attr_t * link_attr
attributes to be included with the link format
Definition: resource.h:105
unsigned int observable
can be observed
Definition: resource.h:89
size_t proxy_name_count
Count of valid names this host is known by (proxy support)
Definition: resource.h:130
UT_hash_handle hh
Definition: resource.h:103
CoAP string data definition with const data.
Definition: str.h:38
CoAP string data definition.
Definition: str.h:30
Subscriber information.
Defines the application visible subscribe information.
unsigned char uint8_t
Definition: uthash.h:79