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_
# include <assert.h>
#ifndef COAP_RESOURCE_CHECK_TIME
#define COAP_RESOURCE_CHECK_TIME 2
#endif /* COAP_RESOURCE_CHECK_TIME */
#ifdef COAP_RESOURCES_NOHASH
# include "utlist.h"
#else
# include "uthash.h"
#endif
#include "hashkey.h"
#include "async.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 *,
const coap_endpoint_t *,
coap_address_t *,
coap_pdu_t *,
str * /* token */,
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;
str name;
str 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;
coap_method_handler_t handler[4];
coap_key_t key;
#ifdef COAP_RESOURCES_NOHASH
struct coap_resource_t *next;
#else
UT_hash_handle hh;
#endif
coap_attr_t *link_attr;
coap_subscription_t *subscribers;
str uri;
int flags;
} coap_resource_t;
coap_resource_t *coap_resource_init(const unsigned char *uri,
size_t len, int flags);
static inline void
coap_resource_set_mode(coap_resource_t *r, int mode) {
r->flags = (r->flags & !COAP_RESOURCE_FLAGS_NOTIFY_CON) | mode;
}
void coap_add_resource(coap_context_t *context, coap_resource_t *resource);
int coap_delete_resource(coap_context_t *context, coap_key_t key);
void coap_delete_all_resources(coap_context_t *context);
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);
coap_attr_t *coap_find_attr(coap_resource_t *resource,
const unsigned char *name,
size_t nlen);
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);
static inline void
coap_register_handler(coap_resource_t *resource,
unsigned char method,
coap_method_handler_t handler) {
assert(resource);
assert(method > 0 && (size_t)(method-1) < sizeof(resource->handler)/sizeof(coap_method_handler_t));
resource->handler[method-1] = handler;
}
coap_resource_t *coap_get_resource_from_key(coap_context_t *context,
coap_key_t key);
void coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key);
coap_subscription_t *coap_add_observer(coap_resource_t *resource,
const coap_endpoint_t *local_interface,
const coap_address_t *observer,
const str *token);
coap_subscription_t *coap_find_observer(coap_resource_t *resource,
const coap_address_t *peer,
const str *token);
void coap_touch_observer(coap_context_t *context,
const coap_address_t *observer,
const str *token);
int coap_delete_observer(coap_resource_t *resource,
const coap_address_t *observer,
const str *token);
void coap_check_notify(coap_context_t *context);
#ifdef COAP_RESOURCES_NOHASH
#define RESOURCES_ADD(r, obj) \
LL_PREPEND((r), (obj))
#define RESOURCES_DELETE(r, obj) \
LL_DELETE((r), (obj))
#define RESOURCES_ITER(r,tmp) \
coap_resource_t *tmp; \
LL_FOREACH((r), tmp)
#define RESOURCES_FIND(r, k, res) { \
coap_resource_t *tmp; \
(res) = tmp = NULL; \
LL_FOREACH((r), tmp) { \
if (memcmp((k), tmp->key, sizeof(coap_key_t)) == 0) { \
(res) = tmp; \
break; \
} \
} \
}
#else /* COAP_RESOURCES_NOHASH */
#define RESOURCES_ADD(r, obj) \
HASH_ADD(hh, (r), key, sizeof(coap_key_t), (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), sizeof(coap_key_t), (res)); \
}
#endif /* COAP_RESOURCES_NOHASH */
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 *,
const coap_address_t *,
const str *);
#endif /* _COAP_RESOURCE_H_ */
coap_resource_t::cacheable
unsigned int cacheable
can be cached
Definition: resource.h:80
coap_key_t
unsigned char coap_key_t[4]
Definition: hashkey.h:20
coap_attr_t::flags
int flags
Definition: resource.h:57
net.h
coap_address_t
multi-purpose address abstraction
Definition: address.h:59
async.h
State management for asynchronous messages.
coap_resource_set_mode
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:130
coap_resource_t::hh
UT_hash_handle hh
Definition: resource.h:95
coap_delete_observer
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_resource_t::link_attr
coap_attr_t * link_attr
attributes to be included with the link format
Definition: resource.h:98
coap_get_resource_from_key
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
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:77
str.h
coap_delete_resource
int coap_delete_resource(coap_context_t *context, coap_key_t key)
Deletes a resource identified by key.
Definition: resource.c:428
UT_hash_handle
Definition: uthash.h:952
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:689
coap_endpoint_t
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_io.h:35
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:449
coap_resource_t
Definition: resource.h:75
coap_attr_t::name
str name
Definition: resource.h:55
coap_hash_request_uri
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
coap_resource_t::uri
str uri
Request URI for this resource.
Definition: resource.h:105
coap_pdu_t
Header structure for CoAP PDUs.
Definition: pdu.h:227
coap_find_observer
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_resource_t::key
coap_key_t key
the actual key bytes for this resource
Definition: resource.h:90
coap_resource_t::dirty
unsigned int dirty
set to 1 if resource has changed
Definition: resource.h:76
coap_resource_t::observable
unsigned int observable
can be observed
Definition: resource.h:79
utlist.h
assert
#define assert(...)
Definition: mem.c:17
coap_register_handler
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:259
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:478
coap_find_attr
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
coap_resource_t
struct coap_resource_t coap_resource_t
coap_add_observer
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
coap_attr_t::value
str value
Definition: resource.h:56
coap_attr_t
Definition: resource.h:53
coap_attr_t
struct coap_attr_t coap_attr_t
coap_resource_t::subscribers
coap_subscription_t * subscribers
list of observers for this resource
Definition: resource.h:99
coap_add_attr
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
pdu.h
Pre-defined constants that reflect defaults for CoAP.
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:167
COAP_RESOURCE_FLAGS_NOTIFY_CON
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Notifications will be sent confirmable by default.
Definition: resource.h:73
coap_subscription_t
Subscriber information.
Definition: subscribe.h:54
str
Definition: str.h:15
coap_add_resource
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
Definition: resource.c:399
hashkey.h
definition of hash key type and helper functions
coap_attr_t::next
struct coap_attr_t * next
Definition: resource.h:54
coap_resource_t::flags
int flags
Definition: resource.h:106
coap_resource_init
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
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:217
coap_opt_t
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
coap_handle_failed_notify
void coap_handle_failed_notify(coap_context_t *, const coap_address_t *, const str *)
Definition: resource.c:748
subscribe.h
coap_method_handler_t
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
coap_context_t
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:76
coap_touch_observer
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
coap_delete_attr
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
Definition: resource.c:366
coap_resource_t::handler
coap_method_handler_t handler[4]
Used to store handlers for the four coap methods GET, POST, PUT, and DELETE.
Definition: resource.h:88
uthash.h