resource.h
Go to the documentation of this file.
/*
* resource.h -- generic resource handling
*
* Copyright (C) 2010,2011,2014,2015 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_RESOURCE_H_
#define COAP_RESOURCE_H_
#ifndef COAP_RESOURCE_CHECK_TIME
#define COAP_RESOURCE_CHECK_TIME 2
#endif /* COAP_RESOURCE_CHECK_TIME */
#include "uthash.h"
#include "async.h"
#include "block.h"
#include "str.h"
#include "pdu.h"
#include "net.h"
#include "subscribe.h"
typedef void (*coap_method_handler_t)
(coap_context_t *,
struct coap_resource_t *,
coap_session_t *,
coap_pdu_t *,
coap_binary_t * /* token */,
coap_string_t * /* query string */,
coap_pdu_t * /* response */);
#define COAP_ATTR_FLAGS_RELEASE_NAME 0x1
#define COAP_ATTR_FLAGS_RELEASE_VALUE 0x2
typedef struct coap_attr_t {
struct coap_attr_t *next;
coap_str_const_t *name;
coap_str_const_t *value;
int flags;
} coap_attr_t;
#define COAP_RESOURCE_FLAGS_RELEASE_URI 0x1
#define COAP_RESOURCE_FLAGS_NOTIFY_NON 0x0
#define COAP_RESOURCE_FLAGS_NOTIFY_CON 0x2
typedef struct coap_resource_t {
unsigned int dirty:1;
unsigned int partiallydirty:1;
unsigned int observable:1;
unsigned int cacheable:1;
unsigned int is_unknown:1;
coap_method_handler_t handler[7];
UT_hash_handle hh;
coap_attr_t *link_attr;
coap_subscription_t *subscribers;
coap_str_const_t *uri_path;
int flags;
unsigned int observe;
void *user_data;
} coap_resource_t;
coap_resource_t *coap_resource_init(coap_str_const_t *uri_path,
int flags);
coap_resource_t *coap_resource_unknown_init(coap_method_handler_t put_handler);
COAP_STATIC_INLINE void
coap_resource_set_mode(coap_resource_t *resource, int mode) {
resource->flags = (resource->flags &
~(COAP_RESOURCE_FLAGS_NOTIFY_CON|COAP_RESOURCE_FLAGS_NOTIFY_NON)) |
(mode & (COAP_RESOURCE_FLAGS_NOTIFY_CON|COAP_RESOURCE_FLAGS_NOTIFY_NON));
}
COAP_STATIC_INLINE void
coap_resource_set_userdata(coap_resource_t *r, void *data) {
r->user_data = data;
}
COAP_STATIC_INLINE void *
coap_resource_get_userdata(coap_resource_t *r) {
return r->user_data;
}
void coap_add_resource(coap_context_t *context, coap_resource_t *resource);
int coap_delete_resource(coap_context_t *context, coap_resource_t *resource);
void coap_delete_all_resources(coap_context_t *context);
coap_attr_t *coap_add_attr(coap_resource_t *resource,
coap_str_const_t *name,
coap_str_const_t *value,
int flags);
coap_attr_t *coap_find_attr(coap_resource_t *resource,
coap_str_const_t *name);
void coap_delete_attr(coap_attr_t *attr);
typedef unsigned int coap_print_status_t;
#define COAP_PRINT_STATUS_MASK 0xF0000000u
#define COAP_PRINT_OUTPUT_LENGTH(v) ((v) & ~COAP_PRINT_STATUS_MASK)
#define COAP_PRINT_STATUS_ERROR 0x80000000u
#define COAP_PRINT_STATUS_TRUNC 0x40000000u
coap_print_status_t coap_print_link(const coap_resource_t *resource,
unsigned char *buf,
size_t *len,
size_t *offset);
void coap_register_handler(coap_resource_t *resource,
unsigned char method,
coap_method_handler_t handler);
coap_resource_t *coap_get_resource_from_uri_path(coap_context_t *context,
coap_str_const_t *uri_path);
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);
coap_subscription_t *coap_find_observer(coap_resource_t *resource,
coap_session_t *session,
const coap_binary_t *token);
void coap_touch_observer(coap_context_t *context,
coap_session_t *session,
const coap_binary_t *token);
int coap_delete_observer(coap_resource_t *resource,
coap_session_t *session,
const coap_binary_t *token);
void coap_delete_observers(coap_context_t *context, coap_session_t *session);
void coap_check_notify(coap_context_t *context);
#define RESOURCES_ADD(r, obj) \
HASH_ADD(hh, (r), uri_path->s[0], (obj)->uri_path->length, (obj))
#define RESOURCES_DELETE(r, obj) \
HASH_DELETE(hh, (r), (obj))
#define RESOURCES_ITER(r,tmp) \
coap_resource_t *tmp, *rtmp; \
HASH_ITER(hh, (r), tmp, rtmp)
#define RESOURCES_FIND(r, k, res) { \
HASH_FIND(hh, (r), (k)->s, (k)->length, (res)); \
}
coap_print_status_t coap_print_wellknown(coap_context_t *,
unsigned char *,
size_t *, size_t,
coap_opt_t *);
void
coap_handle_failed_notify(coap_context_t *,
coap_session_t *,
const coap_binary_t *);
COAP_STATIC_INLINE void
coap_resource_set_get_observable(coap_resource_t *resource, int mode) {
resource->observable = mode ? 1 : 0;
}
int
coap_resource_notify_observers(coap_resource_t *resource,
const coap_string_t *query);
COAP_STATIC_INLINE coap_str_const_t*
coap_resource_get_uri_path(coap_resource_t *resource) {
if (resource)
return resource->uri_path;
return NULL;
}
COAP_DEPRECATED int
coap_resource_set_dirty(coap_resource_t *r,
const coap_string_t *query);
#endif /* COAP_RESOURCE_H_ */
async.h
State management for asynchronous messages.
block.h
COAP_DEPRECATED
#define COAP_DEPRECATED
Definition: libcoap.h:46
COAP_STATIC_INLINE
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
net.h
coap_opt_t
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
pdu.h
Pre-defined constants that reflect defaults for CoAP.
coap_resource_set_dirty
COAP_DEPRECATED int coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query)
Definition: resource.c:827
coap_resource_notify_observers
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
coap_print_wellknown
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_method_handler_t
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
COAP_RESOURCE_FLAGS_NOTIFY_NON
#define COAP_RESOURCE_FLAGS_NOTIFY_NON
Notifications will be sent non-confirmable by default.
Definition: resource.h:60
coap_delete_observer
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
coap_handle_failed_notify
void coap_handle_failed_notify(coap_context_t *, coap_session_t *, const coap_binary_t *)
Definition: resource.c:924
coap_print_link
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
coap_delete_all_resources
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
Definition: resource.c:489
coap_find_observer
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
coap_resource_set_mode
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
coap_delete_attr
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
Definition: resource.c:401
coap_resource_set_userdata
COAP_STATIC_INLINE void coap_resource_set_userdata(coap_resource_t *r, void *data)
Sets the user_data.
Definition: resource.h:199
coap_get_resource_from_uri_path
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
coap_delete_observers
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_check_notify
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_RESOURCE_FLAGS_NOTIFY_CON
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Notifications will be sent confirmable by default.
Definition: resource.h:66
coap_resource_set_get_observable
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
coap_resource_unknown_init
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
coap_attr_t
struct coap_attr_t coap_attr_t
coap_find_attr
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:384
coap_print_status_t
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_resource_init
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
coap_add_resource
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
Definition: resource.c:447
coap_add_observer
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_add_attr
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
coap_delete_resource
int coap_delete_resource(coap_context_t *context, coap_resource_t *resource)
Deletes a resource identified by resource.
Definition: resource.c:469
coap_touch_observer
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
struct coap_resource_t coap_resource_t
coap_resource_get_userdata
COAP_STATIC_INLINE void * coap_resource_get_userdata(coap_resource_t *r)
Gets the user_data.
Definition: resource.h:212
coap_register_handler
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_resource_get_uri_path
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
str.h
UT_hash_handle
Definition: uthash.h:1097
coap_attr_t
Definition: resource.h:46
coap_attr_t::value
coap_str_const_t * value
Definition: resource.h:49
coap_attr_t::next
struct coap_attr_t * next
Definition: resource.h:47
coap_attr_t::name
coap_str_const_t * name
Definition: resource.h:48
coap_attr_t::flags
int flags
Definition: resource.h:50
coap_binary_t
Coap binary data definition.
Definition: str.h:43
coap_block_t
Structure of Block options.
Definition: block.h:36
coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
Definition: net.h:147
coap_pdu_t
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:285
coap_resource_t
Definition: resource.h:68
coap_resource_t::dirty
unsigned int dirty
set to 1 if resource has changed
Definition: resource.h:69
coap_resource_t::partiallydirty
unsigned int partiallydirty
set to 1 if some subscribers have not yet been notified of the last change
Definition: resource.h:70
coap_resource_t::subscribers
coap_subscription_t * subscribers
list of observers for this resource
Definition: resource.h:88
coap_resource_t::user_data
void * user_data
This pointer is under user control.
Definition: resource.h:108
coap_resource_t::uri_path
coap_str_const_t * uri_path
Request URI Path for this resource.
Definition: resource.h:95
coap_resource_t::observe
unsigned int observe
The next value for the Observe option.
Definition: resource.h:102
coap_resource_t::cacheable
unsigned int cacheable
can be cached
Definition: resource.h:73
coap_resource_t::handler
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:83
coap_resource_t::is_unknown
unsigned int is_unknown
resource created for unknown handler
Definition: resource.h:74
coap_resource_t::link_attr
coap_attr_t * link_attr
attributes to be included with the link format
Definition: resource.h:87
coap_resource_t::observable
unsigned int observable
can be observed
Definition: resource.h:72
coap_resource_t::flags
int flags
Definition: resource.h:96
coap_resource_t::hh
UT_hash_handle hh
Definition: resource.h:85
coap_session_t
Definition: coap_session.h:59
coap_str_const_t
Coap string data definition with const data.
Definition: str.h:33
coap_string_t
Coap string data definition.
Definition: str.h:25
coap_subscription_t
Subscriber information.
Definition: coap_subscribe_internal.h:44
subscribe.h
Defines the application visible subscribe information.
uthash.h