libcoap  4.2.1
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
19 
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 
34 typedef void (*coap_method_handler_t)
36  struct coap_resource_t *,
38  coap_pdu_t *,
39  coap_binary_t * /* token */,
40  coap_string_t * /* query string */,
41  coap_pdu_t * /* response */);
42 
43 #define COAP_ATTR_FLAGS_RELEASE_NAME 0x1
44 #define COAP_ATTR_FLAGS_RELEASE_VALUE 0x2
45 
46 typedef struct coap_attr_t {
47  struct coap_attr_t *next;
50  int flags;
51 } coap_attr_t;
52 
54 #define COAP_RESOURCE_FLAGS_RELEASE_URI 0x1
55 
60 #define COAP_RESOURCE_FLAGS_NOTIFY_NON 0x0
61 
66 #define COAP_RESOURCE_FLAGS_NOTIFY_CON 0x2
67 
68 typedef struct coap_resource_t {
69  unsigned int dirty:1;
70  unsigned int partiallydirty:1;
72  unsigned int observable:1;
73  unsigned int cacheable:1;
74  unsigned int is_unknown:1;
84 
86 
96  int flags;
97 
102  unsigned int observe;
103 
108  void *user_data;
109 
111 
144  int flags);
145 
146 
174 
185  resource->flags = (resource->flags &
188 }
189 
200  r->user_data = data;
201 }
202 
211 COAP_STATIC_INLINE void *
213  return r->user_data;
214 }
215 
224 void coap_add_resource(coap_context_t *context, coap_resource_t *resource);
225 
236 int coap_delete_resource(coap_context_t *context, coap_resource_t *resource);
237 
244 
278  int flags);
279 
290  coap_str_const_t *name);
291 
299 void coap_delete_attr(coap_attr_t *attr);
300 
310 typedef unsigned int coap_print_status_t;
311 
312 #define COAP_PRINT_STATUS_MASK 0xF0000000u
313 #define COAP_PRINT_OUTPUT_LENGTH(v) ((v) & ~COAP_PRINT_STATUS_MASK)
314 #define COAP_PRINT_STATUS_ERROR 0x80000000u
315 #define COAP_PRINT_STATUS_TRUNC 0x40000000u
316 
339  unsigned char *buf,
340  size_t *len,
341  size_t *offset);
342 
352  unsigned char method,
353  coap_method_handler_t handler);
354 
365  coap_str_const_t *uri_path);
366 
388  coap_session_t *session,
389  const coap_binary_t *token,
390  coap_string_t *query,
391  int has_block2,
392  coap_block_t block2);
393 
404  coap_session_t *session,
405  const coap_binary_t *token);
406 
415 void coap_touch_observer(coap_context_t *context,
416  coap_session_t *session,
417  const coap_binary_t *token);
418 
431  coap_session_t *session,
432  const coap_binary_t *token);
433 
440 void coap_delete_observers(coap_context_t *context, coap_session_t *session);
441 
446 void coap_check_notify(coap_context_t *context);
447 
448 #define RESOURCES_ADD(r, obj) \
449  HASH_ADD(hh, (r), uri_path->s[0], (obj)->uri_path->length, (obj))
450 
451 #define RESOURCES_DELETE(r, obj) \
452  HASH_DELETE(hh, (r), (obj))
453 
454 #define RESOURCES_ITER(r,tmp) \
455  coap_resource_t *tmp, *rtmp; \
456  HASH_ITER(hh, (r), tmp, rtmp)
457 
458 #define RESOURCES_FIND(r, k, res) { \
459  HASH_FIND(hh, (r), (k)->s, (k)->length, (res)); \
460  }
461 
465  unsigned char *,
466  size_t *, size_t,
467  coap_opt_t *);
468 
469 void
471  coap_session_t *,
472  const coap_binary_t *);
473 
486  resource->observable = mode ? 1 : 0;
487 }
488 
498 int
500  const coap_string_t *query);
501 
511  if (resource)
512  return resource->uri_path;
513  return NULL;
514 }
515 
519 COAP_DEPRECATED int
521  const coap_string_t *query);
522 
523 #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:384
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:73
int flags
Definition: resource.h:50
COAP_DEPRECATED int coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query)
Definition: resource.c:827
State management for asynchronous messages.
UT_hash_handle hh
Definition: resource.h:85
coap_attr_t * link_attr
attributes to be included with the link format
Definition: resource.h:87
unsigned int partiallydirty
set to 1 if some subscribers have not yet been notified of the last change
Definition: resource.h:70
unsigned int is_unknown
resource created for unknown handler
Definition: resource.h:74
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:184
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
Definition: resource.c:864
COAP_STATIC_INLINE void coap_resource_set_get_observable(coap_resource_t *resource, int mode)
Set whether a resource is observable.
Definition: resource.h:485
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
Definition: resource.c:489
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:724
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:469
Coap string data definition with const data.
Definition: str.h:33
coap_str_const_t * name
Definition: resource.h:48
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:295
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:69
unsigned int observable
can be observed
Definition: resource.h:72
COAP_STATIC_INLINE void * coap_resource_get_userdata(coap_resource_t *r)
Gets the user_data.
Definition: resource.h:212
void coap_handle_failed_notify(coap_context_t *, coap_session_t *, const coap_binary_t *)
Definition: resource.c:924
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:618
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:519
#define COAP_DEPRECATED
Definition: libcoap.h:46
#define COAP_RESOURCE_FLAGS_NOTIFY_NON
Notifications will be sent non-confirmable by default.
Definition: resource.h:60
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:582
struct coap_attr_t coap_attr_t
coap_str_const_t * value
Definition: resource.h:49
coap_subscription_t * subscribers
list of observers for this resource
Definition: resource.h:88
#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:698
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:163
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:347
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Notifications will be sent confirmable by default.
Definition: resource.h:66
Subscriber information.
Structure of Block options.
Definition: block.h:36
void * user_data
This pointer is under user control.
Definition: resource.h:108
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
Definition: resource.c:447
struct coap_attr_t * next
Definition: resource.h:47
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:310
COAP_STATIC_INLINE void coap_resource_set_userdata(coap_resource_t *r, void *data)
Sets the user_data.
Definition: resource.h:199
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:510
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:572
coap_str_const_t * uri_path
Request URI Path for this resource.
Definition: resource.h:95
Defines the application visible subscribe information.
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:510
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:35
unsigned int observe
The next value for the Observe option.
Definition: resource.h:102
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:685
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:329
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:147
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:832
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
Definition: resource.c:401