resource.c
Go to the documentation of this file.
/* resource.c -- generic resource handling
*
* Copyright (C) 2010--2015 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include "coap_config.h"
#include "coap.h"
#include "debug.h"
#include "mem.h"
#include "net.h"
#include "resource.h"
#include "subscribe.h"
#include "utlist.h"
#ifdef WITH_LWIP
/* mem.h is only needed for the string free calls for
* COAP_ATTR_FLAGS_RELEASE_NAME / COAP_ATTR_FLAGS_RELEASE_VALUE /
* COAP_RESOURCE_FLAGS_RELEASE_URI. not sure what those lines should actually
* do on lwip. */
#include <lwip/memp.h>
#define COAP_MALLOC_TYPE(Type) \
((coap_##Type##_t *)memp_malloc(MEMP_COAP_##Type))
#define COAP_FREE_TYPE(Type, Object) memp_free(MEMP_COAP_##Type, Object)
#endif
#ifdef WITH_POSIX
#define COAP_MALLOC_TYPE(Type) \
((coap_##Type##_t *)coap_malloc(sizeof(coap_##Type##_t)))
#define COAP_FREE_TYPE(Type, Object) coap_free(Object)
#endif /* WITH_POSIX */
#ifdef WITH_CONTIKI
#include "memb.h"
#define COAP_MALLOC_TYPE(Type) \
((coap_##Type##_t *)memb_alloc(&(Type##_storage)))
#define COAP_FREE_TYPE(Type, Object) memb_free(&(Type##_storage), (Object))
MEMB(subscription_storage, coap_subscription_t, COAP_MAX_SUBSCRIBERS);
void
coap_resources_init() {
memb_init(&subscription_storage);
}
static inline coap_subscription_t *
coap_malloc_subscription() {
return memb_alloc(&subscription_storage);
}
static inline void
coap_free_subscription(coap_subscription_t *subscription) {
memb_free(&subscription_storage, subscription);
}
#endif /* WITH_CONTIKI */
#define min(a,b) ((a) < (b) ? (a) : (b))
/* Helper functions for conditional output of character sequences into
* a given buffer. The first Offset characters are skipped.
*/
#define PRINT_WITH_OFFSET(Buf,Offset,Char) \
if ((Offset) == 0) { \
(*(Buf)++) = (Char); \
} else { \
(Offset)--; \
} \
#define PRINT_COND_WITH_OFFSET(Buf,Bufend,Offset,Char,Result) { \
if ((Buf) < (Bufend)) { \
PRINT_WITH_OFFSET(Buf,Offset,Char); \
} \
(Result)++; \
}
#define COPY_COND_WITH_OFFSET(Buf,Bufend,Offset,Str,Length,Result) { \
size_t i; \
for (i = 0; i < (Length); i++) { \
PRINT_COND_WITH_OFFSET((Buf), (Bufend), (Offset), (Str)[i], (Result)); \
} \
}
static int
match(const str *text, const str *pattern, int match_prefix, int match_substring) {
assert(text); assert(pattern);
if (text->length < pattern->length)
return 0;
if (match_substring) {
unsigned char *next_token = text->s;
size_t remaining_length = text->length;
while (remaining_length) {
size_t token_length;
unsigned char *token = next_token;
next_token = memchr(token, ' ', remaining_length);
if (next_token) {
token_length = next_token - token;
remaining_length -= (token_length + 1);
next_token++;
} else {
token_length = remaining_length;
remaining_length = 0;
}
if ((match_prefix || pattern->length == token_length) &&
memcmp(token, pattern->s, pattern->length) == 0)
return 1;
}
return 0;
}
return (match_prefix || pattern->length == text->length) &&
memcmp(text->s, pattern->s, pattern->length) == 0;
}
#if defined(__GNUC__) && defined(WITHOUT_QUERY_FILTER)
coap_print_status_t
coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
size_t offset,
coap_opt_t *query_filter __attribute__ ((unused))) {
#else /* not a GCC */
coap_print_status_t
coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen,
size_t offset, coap_opt_t *query_filter) {
#endif /* GCC */
unsigned char *p = buf;
const unsigned char *bufend = buf + *buflen;
size_t left, written = 0;
coap_print_status_t result;
const size_t old_offset = offset;
int subsequent_resource = 0;
#ifndef WITHOUT_QUERY_FILTER
str resource_param = { 0, NULL }, query_pattern = { 0, NULL };
int flags = 0; /* MATCH_SUBSTRING, MATCH_PREFIX, MATCH_URI */
#define MATCH_URI 0x01
#define MATCH_PREFIX 0x02
#define MATCH_SUBSTRING 0x04
static const str _rt_attributes[] = {
{2, (unsigned char *)"rt"},
{2, (unsigned char *)"if"},
{3, (unsigned char *)"rel"},
{0, NULL}};
#endif /* WITHOUT_QUERY_FILTER */
#ifndef WITHOUT_QUERY_FILTER
/* split query filter, if any */
if (query_filter) {
resource_param.s = COAP_OPT_VALUE(query_filter);
while (resource_param.length < COAP_OPT_LENGTH(query_filter)
&& resource_param.s[resource_param.length] != '=')
resource_param.length++;
if (resource_param.length < COAP_OPT_LENGTH(query_filter)) {
const str *rt_attributes;
if (resource_param.length == 4 &&
memcmp(resource_param.s, "href", 4) == 0)
flags |= MATCH_URI;
for (rt_attributes = _rt_attributes; rt_attributes->s; rt_attributes++) {
if (resource_param.length == rt_attributes->length &&
memcmp(resource_param.s, rt_attributes->s, rt_attributes->length) == 0) {
flags |= MATCH_SUBSTRING;
break;
}
}
/* rest is query-pattern */
query_pattern.s =
COAP_OPT_VALUE(query_filter) + resource_param.length + 1;
assert((resource_param.length + 1) <= COAP_OPT_LENGTH(query_filter));
query_pattern.length =
COAP_OPT_LENGTH(query_filter) - (resource_param.length + 1);
if ((query_pattern.s[0] == '/') && ((flags & MATCH_URI) == MATCH_URI)) {
query_pattern.s++;
query_pattern.length--;
}
if (query_pattern.length &&
query_pattern.s[query_pattern.length-1] == '*') {
query_pattern.length--;
flags |= MATCH_PREFIX;
}
}
}
#endif /* WITHOUT_QUERY_FILTER */
RESOURCES_ITER(context->resources, r) {
#ifndef WITHOUT_QUERY_FILTER
if (resource_param.length) { /* there is a query filter */
if (flags & MATCH_URI) { /* match resource URI */
if (!match(&r->uri, &query_pattern, (flags & MATCH_PREFIX) != 0, (flags & MATCH_SUBSTRING) != 0))
continue;
} else { /* match attribute */
coap_attr_t *attr;
str unquoted_val;
attr = coap_find_attr(r, resource_param.s, resource_param.length);
if (!attr) continue;
if (attr->value.s[0] == '"') { /* if attribute has a quoted value, remove double quotes */
unquoted_val.length = attr->value.length - 2;
unquoted_val.s = attr->value.s + 1;
} else {
unquoted_val = attr->value;
}
if (!(match(&unquoted_val, &query_pattern,
(flags & MATCH_PREFIX) != 0,
(flags & MATCH_SUBSTRING) != 0)))
continue;
}
}
#endif /* WITHOUT_QUERY_FILTER */
if (!subsequent_resource) { /* this is the first resource */
subsequent_resource = 1;
} else {
PRINT_COND_WITH_OFFSET(p, bufend, offset, ',', written);
}
left = bufend - p; /* calculate available space */
result = coap_print_link(r, p, &left, &offset);
if (result & COAP_PRINT_STATUS_ERROR) {
break;
}
/* coap_print_link() returns the number of characters that
* where actually written to p. Now advance to its end. */
p += COAP_PRINT_OUTPUT_LENGTH(result);
written += left;
}
*buflen = written;
result = p - buf;
if (result + old_offset - offset < *buflen) {
result |= COAP_PRINT_STATUS_TRUNC;
}
return result;
}
coap_resource_t *
coap_resource_init(const unsigned char *uri, size_t len, int flags) {
coap_resource_t *r;
#ifdef WITH_LWIP
r = (coap_resource_t *)memp_malloc(MEMP_COAP_RESOURCE);
#endif
#ifndef WITH_LWIP
r = (coap_resource_t *)coap_malloc_type(COAP_RESOURCE, sizeof(coap_resource_t));
#endif
if (r) {
memset(r, 0, sizeof(coap_resource_t));
r->uri.s = (unsigned char *)uri;
r->uri.length = len;
coap_hash_path(r->uri.s, r->uri.length, r->key);
r->flags = flags;
} else {
debug("coap_resource_init: no memory left\n");
}
return r;
}
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 *attr;
if (!resource || !name)
return NULL;
#ifdef WITH_LWIP
attr = (coap_attr_t *)memp_malloc(MEMP_COAP_RESOURCEATTR);
#endif
#ifndef WITH_LWIP
attr = (coap_attr_t *)coap_malloc_type(COAP_RESOURCEATTR, sizeof(coap_attr_t));
#endif
if (attr) {
attr->name.length = nlen;
attr->value.length = val ? vlen : 0;
attr->name.s = (unsigned char *)name;
attr->value.s = (unsigned char *)val;
attr->flags = flags;
/* add attribute to resource list */
LL_PREPEND(resource->link_attr, attr);
} else {
debug("coap_add_attr: no memory left\n");
}
return attr;
}
coap_attr_t *
coap_find_attr(coap_resource_t *resource,
const unsigned char *name, size_t nlen) {
coap_attr_t *attr;
if (!resource || !name)
return NULL;
LL_FOREACH(resource->link_attr, attr) {
if (attr->name.length == nlen &&
memcmp(attr->name.s, name, nlen) == 0)
return attr;
}
return NULL;
}
void
coap_delete_attr(coap_attr_t *attr) {
if (!attr)
return;
if (attr->flags & COAP_ATTR_FLAGS_RELEASE_NAME)
coap_free(attr->name.s);
if (attr->flags & COAP_ATTR_FLAGS_RELEASE_VALUE)
coap_free(attr->value.s);
#ifdef WITH_LWIP
memp_free(MEMP_COAP_RESOURCEATTR, attr);
#endif
#ifndef WITH_LWIP
coap_free_type(COAP_RESOURCEATTR, attr);
#endif
}
void
coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key) {
coap_opt_iterator_t opt_iter;
coap_opt_filter_t filter;
coap_opt_t *option;
memset(key, 0, sizeof(coap_key_t));
coap_option_filter_clear(filter);
coap_option_setb(filter, COAP_OPTION_URI_PATH);
coap_option_iterator_init((coap_pdu_t *)request, &opt_iter, filter);
while ((option = coap_option_next(&opt_iter)))
coap_hash(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option), key);
}
void
coap_add_resource(coap_context_t *context, coap_resource_t *resource) {
RESOURCES_ADD(context->resources, resource);
}
static void
coap_free_resource(coap_resource_t *resource) {
coap_attr_t *attr, *tmp;
coap_subscription_t *obs, *otmp;
assert(resource);
/* delete registered attributes */
LL_FOREACH_SAFE(resource->link_attr, attr, tmp) coap_delete_attr(attr);
if (resource->flags & COAP_RESOURCE_FLAGS_RELEASE_URI)
coap_free(resource->uri.s);
/* free all elements from resource->subscribers */
LL_FOREACH_SAFE(resource->subscribers, obs, otmp) COAP_FREE_TYPE(subscription, obs);
#ifdef WITH_LWIP
memp_free(MEMP_COAP_RESOURCE, resource);
#endif
#ifndef WITH_LWIP
coap_free_type(COAP_RESOURCE, resource);
#endif /* WITH_CONTIKI */
}
int
coap_delete_resource(coap_context_t *context, coap_key_t key) {
coap_resource_t *resource;
if (!context)
return 0;
resource = coap_get_resource_from_key(context, key);
if (!resource)
return 0;
/* remove resource from list */
RESOURCES_DELETE(context->resources, resource);
/* and free its allocated memory */
coap_free_resource(resource);
return 1;
}
void
coap_delete_all_resources(coap_context_t *context) {
coap_resource_t *res;
coap_resource_t *rtmp;
/* Cannot call RESOURCES_ITER because coap_free_resource() releases
* the allocated storage. */
#ifdef COAP_RESOURCES_NOHASH
LL_FOREACH_SAFE(context->resources, res, rtmp) {
#else
HASH_ITER(hh, context->resources, res, rtmp) {
HASH_DELETE(hh, context->resources, res);
#endif
coap_free_resource(res);
}
context->resources = NULL;
}
coap_resource_t *
coap_get_resource_from_key(coap_context_t *context, coap_key_t key) {
coap_resource_t *result;
RESOURCES_FIND(context->resources, key, result);
return result;
}
coap_print_status_t
coap_print_link(const coap_resource_t *resource,
unsigned char *buf, size_t *len, size_t *offset) {
unsigned char *p = buf;
const unsigned char *bufend = buf + *len;
coap_attr_t *attr;
coap_print_status_t result = 0;
const size_t old_offset = *offset;
*len = 0;
PRINT_COND_WITH_OFFSET(p, bufend, *offset, '<', *len);
PRINT_COND_WITH_OFFSET(p, bufend, *offset, '/', *len);
COPY_COND_WITH_OFFSET(p, bufend, *offset,
resource->uri.s, resource->uri.length, *len);
PRINT_COND_WITH_OFFSET(p, bufend, *offset, '>', *len);
LL_FOREACH(resource->link_attr, attr) {
PRINT_COND_WITH_OFFSET(p, bufend, *offset, ';', *len);
COPY_COND_WITH_OFFSET(p, bufend, *offset,
attr->name.s, attr->name.length, *len);
if (attr->value.s) {
PRINT_COND_WITH_OFFSET(p, bufend, *offset, '=', *len);
COPY_COND_WITH_OFFSET(p, bufend, *offset,
attr->value.s, attr->value.length, *len);
}
}
if (resource->observable) {
COPY_COND_WITH_OFFSET(p, bufend, *offset, ";obs", 4, *len);
}
result = p - buf;
if (result + old_offset - *offset < *len) {
result |= COAP_PRINT_STATUS_TRUNC;
}
return result;
}
#ifndef WITHOUT_OBSERVE
coap_subscription_t *
coap_find_observer(coap_resource_t *resource, const coap_address_t *peer,
const str *token) {
coap_subscription_t *s;
assert(resource);
assert(peer);
LL_FOREACH(resource->subscribers, s) {
if (coap_address_equals(&s->subscriber, peer)
&& (!token || (token->length == s->token_length
&& memcmp(token->s, s->token, token->length) == 0)))
return s;
}
return NULL;
}
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 *s;
assert(observer);
/* Check if there is already a subscription for this peer. */
s = coap_find_observer(resource, observer, token);
/* We are done if subscription was found. */
if (s)
return s;
/* s points to a different subscription, so we have to create
* another one. */
s = COAP_MALLOC_TYPE(subscription);
if (!s)
return NULL;
coap_subscription_init(s);
s->local_if = *local_interface;
memcpy(&s->subscriber, observer, sizeof(coap_address_t));
if (token && token->length) {
s->token_length = token->length;
memcpy(s->token, token->s, min(s->token_length, 8));
}
/* add subscriber to resource */
LL_PREPEND(resource->subscribers, s);
return s;
}
void
coap_touch_observer(coap_context_t *context, const coap_address_t *observer,
const str *token) {
coap_subscription_t *s;
RESOURCES_ITER(context->resources, r) {
s = coap_find_observer(r, observer, token);
if (s) {
s->fail_cnt = 0;
}
}
}
int
coap_delete_observer(coap_resource_t *resource, const coap_address_t *observer,
const str *token) {
coap_subscription_t *s;
s = coap_find_observer(resource, observer, token);
if (resource->subscribers && s) {
LL_DELETE(resource->subscribers, s);
COAP_FREE_TYPE(subscription,s);
}
return s != NULL;
}
static void
coap_notify_observers(coap_context_t *context, coap_resource_t *r) {
coap_method_handler_t h;
coap_subscription_t *obs;
str token;
coap_pdu_t *response;
if (r->observable && (r->dirty || r->partiallydirty)) {
r->partiallydirty = 0;
/* retrieve GET handler, prepare response */
h = r->handler[COAP_REQUEST_GET - 1];
assert(h); /* we do not allow subscriptions if no
* GET handler is defined */
LL_FOREACH(r->subscribers, obs) {
if (r->dirty == 0 && obs->dirty == 0)
/* running this resource due to partiallydirty, but this observation's notification was already enqueued */
continue;
coap_tid_t tid = COAP_INVALID_TID;
obs->dirty = 0;
/* initialize response */
response = coap_pdu_init(COAP_MESSAGE_CON, 0, 0, COAP_MAX_PDU_SIZE);
if (!response) {
obs->dirty = 1;
r->partiallydirty = 1;
debug("coap_check_notify: pdu init failed, resource stays partially dirty\n");
continue;
}
if (!coap_add_token(response, obs->token_length, obs->token)) {
obs->dirty = 1;
r->partiallydirty = 1;
debug("coap_check_notify: cannot add token, resource stays partially dirty\n");
coap_delete_pdu(response);
continue;
}
token.length = obs->token_length;
token.s = obs->token;
response->hdr->id = coap_new_message_id(context);
if ((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) == 0
&& obs->non_cnt < COAP_OBS_MAX_NON) {
response->hdr->type = COAP_MESSAGE_NON;
} else {
response->hdr->type = COAP_MESSAGE_CON;
}
/* fill with observer-specific data */
h(context, r, &obs->local_if, &obs->subscriber, NULL, &token, response);
/* TODO: do not send response and remove observer when
* COAP_RESPONSE_CLASS(response->hdr->code) > 2
*/
if (response->hdr->type == COAP_MESSAGE_CON) {
tid = coap_send_confirmed(context, &obs->local_if, &obs->subscriber, response);
obs->non_cnt = 0;
} else {
tid = coap_send(context, &obs->local_if, &obs->subscriber, response);
obs->non_cnt++;
}
if (COAP_INVALID_TID == tid || response->hdr->type != COAP_MESSAGE_CON)
coap_delete_pdu(response);
if (COAP_INVALID_TID == tid)
{
debug("coap_check_notify: sending failed, resource stays partially dirty\n");
obs->dirty = 1;
r->partiallydirty = 1;
}
}
/* Increment value for next Observe use. */
context->observe++;
}
r->dirty = 0;
}
void
coap_check_notify(coap_context_t *context) {
RESOURCES_ITER(context->resources, r) {
coap_notify_observers(context, r);
}
}
static void
coap_remove_failed_observers(coap_context_t *context,
coap_resource_t *resource,
const coap_address_t *peer,
const str *token) {
coap_subscription_t *obs, *otmp;
LL_FOREACH_SAFE(resource->subscribers, obs, otmp) {
if (coap_address_equals(peer, &obs->subscriber) &&
token->length == obs->token_length &&
memcmp(token->s, obs->token, token->length) == 0) {
/* count failed notifies and remove when
* COAP_MAX_FAILED_NOTIFY is reached */
if (obs->fail_cnt < COAP_OBS_MAX_FAIL)
obs->fail_cnt++;
else {
LL_DELETE(resource->subscribers, obs);
obs->fail_cnt = 0;
#ifndef NDEBUG
if (LOG_DEBUG <= coap_get_log_level()) {
#ifndef INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN 40
#endif
unsigned char addr[INET6_ADDRSTRLEN+8];
if (coap_print_addr(&obs->subscriber, addr, INET6_ADDRSTRLEN+8))
debug("** removed observer %s\n", addr);
}
#endif
coap_cancel_all_messages(context, &obs->subscriber,
obs->token, obs->token_length);
COAP_FREE_TYPE(subscription, obs);
}
}
break; /* break loop if observer was found */
}
}
void
coap_handle_failed_notify(coap_context_t *context,
const coap_address_t *peer,
const str *token) {
RESOURCES_ITER(context->resources, r) {
coap_remove_failed_observers(context, r, peer, token);
}
}
#endif /* WITHOUT_NOTIFY */
LL_FOREACH
#define LL_FOREACH(head, el)
Definition: utlist.h:428
coap_context_t::observe
unsigned int observe
The next value to be used for Observe.
Definition: net.h:120
coap_send_confirmed
coap_tid_t coap_send_confirmed(coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a confirmed CoAP message to given destination.
Definition: net.c:699
RESOURCES_DELETE
#define RESOURCES_DELETE(r, obj)
Definition: resource.h:384
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_key_t
unsigned char coap_key_t[4]
Definition: hashkey.h:20
coap_attr_t::flags
int flags
Definition: resource.h:57
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
mem.h
net.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
debug.h
INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN
coap_address_t
multi-purpose address abstraction
Definition: address.h:59
COAP_PRINT_OUTPUT_LENGTH
#define COAP_PRINT_OUTPUT_LENGTH(v)
Definition: resource.h:220
coap_hdr_t::id
unsigned short id
Definition: pdu.h:191
coap_tid_t
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:163
LL_PREPEND
#define LL_PREPEND(head, add)
Definition: utlist.h:309
COAP_REQUEST_GET
#define COAP_REQUEST_GET
Definition: pdu.h:50
HASH_ITER
#define HASH_ITER(hh, head, el, tmp)
Definition: uthash.h:886
coap_add_token
int coap_add_token(coap_pdu_t *pdu, size_t len, const unsigned char *data)
Adds token of length len to pdu.
Definition: pdu.c:153
COAP_MESSAGE_NON
#define COAP_MESSAGE_NON
Definition: pdu.h:44
coap_resource_t::link_attr
coap_attr_t * link_attr
attributes to be included with the link format
Definition: resource.h:98
COAP_ATTR_FLAGS_RELEASE_NAME
#define COAP_ATTR_FLAGS_RELEASE_NAME
Definition: resource.h:50
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
COAP_RESOURCEATTR
Definition: mem.h:40
str::length
size_t length
Definition: str.h:16
coap_remove_failed_observers
static void coap_remove_failed_observers(coap_context_t *context, coap_resource_t *resource, const coap_address_t *peer, const str *token)
Checks the failure counter for (peer, token) and removes peer from the list of observers for the give...
Definition: resource.c:707
MATCH_URI
#define MATCH_URI
coap_option_next
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
Definition: option.c:159
coap_option_setb
static int coap_option_setb(coap_opt_filter_t filter, unsigned short type)
Sets the corresponding bit for type in filter.
Definition: option.h:199
coap_pdu_t::hdr
coap_hdr_t * hdr
Address of the first byte of the CoAP message.
Definition: pdu.h:229
LOG_DEBUG
Definition: debug.h:34
match
static int match(const str *text, const str *pattern, int match_prefix, int match_substring)
Definition: resource.c:105
COAP_OBS_MAX_FAIL
#define COAP_OBS_MAX_FAIL
Number of confirmable notifications that may fail (i.e.
Definition: subscribe.h:50
coap_endpoint_t
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_io.h:35
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
MATCH_PREFIX
#define MATCH_PREFIX
MATCH_SUBSTRING
#define MATCH_SUBSTRING
coap_resource_t
Definition: resource.h:75
coap_print_addr
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
Definition: debug.c:162
debug
#define debug(...)
Definition: debug.h:66
coap_attr_t::name
str name
Definition: resource.h:55
HASH_DELETE
#define HASH_DELETE(hh, head, delptr)
Definition: uthash.h:224
COAP_INVALID_TID
#define COAP_INVALID_TID
Indicates an invalid transaction id.
Definition: pdu.h:166
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
coap_subscription_t::token_length
size_t token_length
actual length of token
Definition: subscribe.h:64
coap_cancel_all_messages
void coap_cancel_all_messages(coap_context_t *context, const coap_address_t *dst, const unsigned char *token, size_t token_length)
Cancels all outstanding messages for peer dst that have the specified token.
Definition: net.c:980
coap_resource_t::uri
str uri
Request URI for this resource.
Definition: resource.h:105
RESOURCES_ITER
#define RESOURCES_ITER(r, tmp)
Definition: resource.h:387
coap_pdu_t
Header structure for CoAP PDUs.
Definition: pdu.h:227
coap_option_iterator_init
coap_opt_iterator_t * coap_option_iterator_init(coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t filter)
Initializes the given option iterator oi to point to the beginning of the pdu&#39;s option list...
Definition: option.c:120
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_MALLOC_TYPE
#define COAP_MALLOC_TYPE(Type)
Definition: resource.c:34
coap_send
coap_tid_t coap_send(coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a non-confirmed CoAP message to given destination.
Definition: net.c:613
COAP_ATTR_FLAGS_RELEASE_VALUE
#define COAP_ATTR_FLAGS_RELEASE_VALUE
Definition: resource.h:51
coap_resource_t::observable
unsigned int observable
can be observed
Definition: resource.h:79
utlist.h
assert
#define assert(...)
Definition: mem.c:17
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
coap_subscription_t::non_cnt
unsigned int non_cnt
up to 15 non-confirmable notifies allowed
Definition: subscribe.h:59
coap_opt_iterator_t
Iterator to run through PDU options.
Definition: option.h:253
COAP_MESSAGE_CON
#define COAP_MESSAGE_CON
Definition: pdu.h:43
coap_attr_t::value
str value
Definition: resource.h:56
RESOURCES_ADD
#define RESOURCES_ADD(r, obj)
Definition: resource.h:381
resource.h
Generic resource handling.
COAP_MAX_PDU_SIZE
#define COAP_MAX_PDU_SIZE
Definition: pdu.h:27
coap_attr_t
Definition: resource.h:53
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_hash
#define coap_hash(String, Length, Result)
Definition: hashkey.h:34
coap_notify_observers
static void coap_notify_observers(coap_context_t *context, coap_resource_t *r)
Definition: resource.c:609
coap_resource_t::subscribers
coap_subscription_t * subscribers
list of observers for this resource
Definition: resource.h:99
coap_opt_filter_t
uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE]
Fixed-size vector we use for option filtering.
Definition: option.h:135
coap_address_equals
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
Definition: address.c:18
COAP_RESOURCE_FLAGS_NOTIFY_CON
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Notifications will be sent confirmable by default.
Definition: resource.h:73
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_subscription_t
Subscriber information.
Definition: subscribe.h:54
coap_delete_pdu
void coap_delete_pdu(coap_pdu_t *pdu)
Definition: pdu.c:136
coap_pdu_init
coap_pdu_t * coap_pdu_init(unsigned char type, unsigned char code, unsigned short id, size_t size)
Creates a new CoAP PDU of given size (must be large enough to hold the basic CoAP message header (coa...
Definition: pdu.c:75
coap_subscription_t::dirty
unsigned int dirty
set if the notification temporarily could not be sent (in that case, the resource&#39;s partially dirty f...
Definition: subscribe.h:61
RESOURCES_FIND
#define RESOURCES_FIND(r, k, res)
Definition: resource.h:391
coap_option_filter_clear
static void coap_option_filter_clear(coap_opt_filter_t f)
Clears filter f.
Definition: option.h:146
coap_hash_path
int coap_hash_path(const unsigned char *path, size_t len, coap_key_t key)
Calculates a hash over the given path and stores the result in key.
Definition: uri.c:483
COAP_RESOURCE
Definition: mem.h:39
str
Definition: str.h:15
LL_FOREACH_SAFE
#define LL_FOREACH_SAFE(head, el, tmp)
Definition: utlist.h:434
PRINT_COND_WITH_OFFSET
#define PRINT_COND_WITH_OFFSET(Buf, Bufend, Offset, Char, Result)
Adds Char to Buf if Offset is zero and Buf is less than Bufend.
Definition: resource.c:85
COAP_OPT_LENGTH
#define COAP_OPT_LENGTH(opt)
Definition: option.h:392
coap_new_message_id
static unsigned short coap_new_message_id(coap_context_t *context)
Returns a new message id and updates context->message_id accordingly.
Definition: net.h:189
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_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_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_OPTION_URI_PATH
#define COAP_OPTION_URI_PATH
Definition: pdu.h:63
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_free
static void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
Definition: mem.h:77
coap_malloc_type
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
LL_DELETE
#define LL_DELETE(head, del)
Definition: utlist.h:349
COAP_PRINT_STATUS_ERROR
#define COAP_PRINT_STATUS_ERROR
Definition: resource.h:221
coap_resource_t::flags
int flags
Definition: resource.h:106
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_hdr_t::type
unsigned int type
Definition: pdu.h:187
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
COPY_COND_WITH_OFFSET
#define COPY_COND_WITH_OFFSET(Buf, Bufend, Offset, Str, Length, Result)
Copies at most Length characters of Str to Buf.
Definition: resource.c:97
coap_subscription_t::fail_cnt
unsigned int fail_cnt
up to 3 confirmable notifies can fail
Definition: subscribe.h:60
COAP_FREE_TYPE
#define COAP_FREE_TYPE(Type, Object)
Definition: resource.c:36
min
#define min(a, b)
Definition: resource.c:65
coap_print_wellknown
coap_print_status_t coap_print_wellknown(coap_context_t *context, unsigned char *buf, size_t *buflen, size_t offset, coap_opt_t *query_filter)
Prints the names of all known resources to buf.
Definition: resource.c:167
coap_free_type
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
coap_subscription_t::token
unsigned char token[8]
token used for subscription
Definition: subscribe.h:65
coap_delete_attr
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
Definition: resource.c:366
subscribe.h
coap.h
str::s
unsigned char * s
Definition: str.h:17
coap_subscription_t::local_if
coap_endpoint_t local_if
local communication interface
Definition: subscribe.h:56
COAP_PRINT_STATUS_TRUNC
#define COAP_PRINT_STATUS_TRUNC
Definition: resource.h:222
coap_subscription_init
void coap_subscription_init(coap_subscription_t *s)
Definition: subscribe.c:20
COAP_RESOURCE_FLAGS_RELEASE_URI
#define COAP_RESOURCE_FLAGS_RELEASE_URI
The URI passed to coap_resource_init() is free&#39;d by coap_delete_resource().
Definition: resource.h:61
coap_get_log_level
coap_log_t coap_get_log_level(void)
Returns the current log level.
Definition: debug.c:60
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_handle_failed_notify
void coap_handle_failed_notify(coap_context_t *context, const coap_address_t *peer, const str *token)
Definition: resource.c:748
coap_subscription_t::subscriber
coap_address_t subscriber
address and port of subscriber
Definition: subscribe.h:57
COAP_OPT_VALUE
#define COAP_OPT_VALUE(opt)
Definition: option.h:406
coap_context_t
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:76
coap_free_resource
static void coap_free_resource(coap_resource_t *resource)
Definition: resource.c:404
COAP_OBS_MAX_NON
#define COAP_OBS_MAX_NON
Number of notifications that may be sent non-confirmable before a confirmable message is sent to dete...
Definition: subscribe.h:41
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_context_t::resources
struct coap_resource_t * resources
hash table or list of known resources
Definition: net.h:78
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