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_internal.h"
#include <stdio.h>
#if defined(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)
#elif defined(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);
}
COAP_STATIC_INLINE coap_subscription_t *
coap_malloc_subscription() {
return memb_alloc(&subscription_storage);
}
COAP_STATIC_INLINE void
coap_free_subscription(coap_subscription_t *subscription) {
memb_free(&subscription_storage, subscription);
}
#else
#define COAP_MALLOC_TYPE(Type) \
((coap_##Type##_t *)coap_malloc(sizeof(coap_##Type##_t)))
#define COAP_FREE_TYPE(Type, Object) coap_free(Object)
#endif
#define COAP_PRINT_STATUS_MAX (~COAP_PRINT_STATUS_MASK)
#ifndef min
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif
/* 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 coap_str_const_t *text, const coap_str_const_t *pattern, int match_prefix,
int match_substring
) {
assert(text); assert(pattern);
if (text->length < pattern->length)
return 0;
if (match_substring) {
const uint8_t *next_token = text->s;
size_t remaining_length = text->length;
while (remaining_length) {
size_t token_length;
const uint8_t *token = next_token;
next_token = (unsigned char *)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 */
size_t output_length = 0;
unsigned char *p = buf;
const uint8_t *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
coap_str_const_t 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 coap_str_const_t _rt_attributes[] = {
{2, (const uint8_t *)"rt"},
{2, (const uint8_t *)"if"},
{3, (const uint8_t *)"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 coap_str_const_t *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_path, &query_pattern, (flags & MATCH_PREFIX) != 0,
(flags & MATCH_SUBSTRING) != 0))
continue;
} else { /* match attribute */
coap_attr_t *attr;
coap_str_const_t unquoted_val;
attr = coap_find_attr(r, &resource_param);
if (!attr || !attr->value) continue;
unquoted_val = *attr->value;
if (attr->value->s[0] == '"') { /* if attribute has a quoted value, remove double quotes */
unquoted_val.length -= 2;
unquoted_val.s += 1;
}
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;
output_length = p - buf;
if (output_length > COAP_PRINT_STATUS_MAX) {
return COAP_PRINT_STATUS_ERROR;
}
result = (coap_print_status_t)output_length;
if (result + old_offset - offset < *buflen) {
result |= COAP_PRINT_STATUS_TRUNC;
}
return result;
}
static coap_str_const_t null_path_value = {0, (const uint8_t*)""};
static coap_str_const_t *null_path = &null_path_value;
coap_resource_t *
coap_resource_init(coap_str_const_t *uri_path, int flags) {
coap_resource_t *r;
r = (coap_resource_t *)coap_malloc_type(COAP_RESOURCE, sizeof(coap_resource_t));
if (r) {
memset(r, 0, sizeof(coap_resource_t));
if (!(flags & COAP_RESOURCE_FLAGS_RELEASE_URI)) {
/* Need to take a copy if caller is not providing a release request */
if (uri_path)
uri_path = coap_new_str_const(uri_path->s, uri_path->length);
else
uri_path = coap_new_str_const(null_path->s, null_path->length);
}
else if (!uri_path) {
/* Do not expecte this, but ... */
uri_path = coap_new_str_const(null_path->s, null_path->length);
}
if (uri_path)
r->uri_path = uri_path;
r->flags = flags;
} else {
coap_log(LOG_DEBUG, "coap_resource_init: no memory left\n");
}
return r;
}
static const uint8_t coap_unknown_resource_uri[] =
"- Unknown -";
coap_resource_t *
coap_resource_unknown_init(coap_method_handler_t put_handler) {
coap_resource_t *r;
r = (coap_resource_t *)coap_malloc_type(COAP_RESOURCE, sizeof(coap_resource_t));
if (r) {
memset(r, 0, sizeof(coap_resource_t));
r->is_unknown = 1;
/* Something unlikely to be used, but it shows up in the logs */
r->uri_path = coap_new_str_const(coap_unknown_resource_uri, sizeof(coap_unknown_resource_uri)-1);
coap_register_handler(r, COAP_REQUEST_PUT, put_handler);
} else {
coap_log(LOG_DEBUG, "coap_resource_unknown_init: no memory left\n");
}
return r;
}
coap_attr_t *
coap_add_attr(coap_resource_t *resource,
coap_str_const_t *name,
coap_str_const_t *val,
int flags) {
coap_attr_t *attr;
if (!resource || !name)
return NULL;
attr = (coap_attr_t *)coap_malloc_type(COAP_RESOURCEATTR, sizeof(coap_attr_t));
if (attr) {
if (!(flags & COAP_ATTR_FLAGS_RELEASE_NAME)) {
/* Need to take a copy if caller is not providing a release request */
name = coap_new_str_const(name->s, name->length);
}
attr->name = name;
if (val) {
if (!(flags & COAP_ATTR_FLAGS_RELEASE_VALUE)) {
/* Need to take a copy if caller is not providing a release request */
val = coap_new_str_const(val->s, val->length);
}
}
attr->value = val;
attr->flags = flags;
/* add attribute to resource list */
LL_PREPEND(resource->link_attr, attr);
} else {
coap_log(LOG_DEBUG, "coap_add_attr: no memory left\n");
}
return attr;
}
coap_attr_t *
coap_find_attr(coap_resource_t *resource,
coap_str_const_t *name) {
coap_attr_t *attr;
if (!resource || !name)
return NULL;
LL_FOREACH(resource->link_attr, attr) {
if (attr->name->length == name->length &&
memcmp(attr->name->s, name->s, name->length) == 0)
return attr;
}
return NULL;
}
void
coap_delete_attr(coap_attr_t *attr) {
if (!attr)
return;
coap_delete_str_const(attr->name);
if (attr->value) {
coap_delete_str_const(attr->value);
}
#ifdef WITH_LWIP
memp_free(MEMP_COAP_RESOURCEATTR, attr);
#endif
#ifndef WITH_LWIP
coap_free_type(COAP_RESOURCEATTR, attr);
#endif
}
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);
/* Either the application provided or libcoap copied - need to delete it */
coap_delete_str_const(resource->uri_path);
/* free all elements from resource->subscribers */
LL_FOREACH_SAFE( resource->subscribers, obs, otmp ) {
coap_session_release( obs->session );
if (obs->query)
coap_delete_string(obs->query);
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 */
}
void
coap_add_resource(coap_context_t *context, coap_resource_t *resource) {
if (resource->is_unknown) {
if (context->unknown_resource)
coap_free_resource(context->unknown_resource);
context->unknown_resource = resource;
}
else {
coap_resource_t *r = coap_get_resource_from_uri_path(context,
resource->uri_path);
if (r) {
coap_log(LOG_WARNING,
"coap_add_resource: Duplicate uri_path '%*.*s', old resource deleted\n",
(int)resource->uri_path->length, (int)resource->uri_path->length,
resource->uri_path->s);
coap_delete_resource(context, r);
}
RESOURCES_ADD(context->resources, resource);
}
}
int
coap_delete_resource(coap_context_t *context, coap_resource_t *resource) {
if (!context || !resource)
return 0;
if (resource->is_unknown && (context->unknown_resource == resource)) {
coap_free_resource(context->unknown_resource);
context->unknown_resource = NULL;
return 1;
}
/* 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. */
HASH_ITER(hh, context->resources, res, rtmp) {
HASH_DELETE(hh, context->resources, res);
coap_free_resource(res);
}
context->resources = NULL;
if (context->unknown_resource) {
coap_free_resource(context->unknown_resource);
context->unknown_resource = NULL;
}
}
coap_resource_t *
coap_get_resource_from_uri_path(coap_context_t *context, coap_str_const_t *uri_path) {
coap_resource_t *result;
RESOURCES_FIND(context->resources, uri_path, 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 uint8_t *bufend = buf + *len;
coap_attr_t *attr;
coap_print_status_t result = 0;
size_t output_length = 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_path->s, resource->uri_path->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 && 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);
}
output_length = p - buf;
if (output_length > COAP_PRINT_STATUS_MAX) {
return COAP_PRINT_STATUS_ERROR;
}
result = (coap_print_status_t)output_length;
if (result + old_offset - *offset < *len) {
result |= COAP_PRINT_STATUS_TRUNC;
}
return result;
}
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;
}
#ifndef WITHOUT_OBSERVE
coap_subscription_t *
coap_find_observer(coap_resource_t *resource, coap_session_t *session,
const coap_binary_t *token) {
coap_subscription_t *s;
assert(resource);
assert(session);
LL_FOREACH(resource->subscribers, s) {
if (s->session == session
&& (!token || (token->length == s->token_length
&& memcmp(token->s, s->token, token->length) == 0)))
return s;
}
return NULL;
}
static coap_subscription_t *
coap_find_observer_query(coap_resource_t *resource, coap_session_t *session,
const coap_string_t *query) {
coap_subscription_t *s;
assert(resource);
assert(session);
LL_FOREACH(resource->subscribers, s) {
if (s->session == session
&& ((!query && !s->query)
|| (query && s->query && coap_string_equal(query, s->query))))
return s;
}
return NULL;
}
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 *s;
assert( session );
/* Check if there is already a subscription for this peer. */
s = coap_find_observer(resource, session, token);
if (!s) {
/*
* Cannot allow a duplicate to be created for the same query as application
* may not be cleaning up duplicates. If duplicate found, then original
* observer is deleted and a new one created with the new token
*/
s = coap_find_observer_query(resource, session, query);
if (s) {
/* Delete old entry with old token */
coap_binary_t tmp_token = { s->token_length, s->token };
coap_delete_observer(resource, session, &tmp_token);
s = NULL;
}
}
/* We are done if subscription was found. */
if (s) {
if (s->query)
coap_delete_string(s->query);
s->query = query;
return s;
}
/* s points to a different subscription, so we have to create
* another one. */
s = COAP_MALLOC_TYPE(subscription);
if (!s) {
if (query)
coap_delete_string(query);
return NULL;
}
coap_subscription_init(s);
s->session = coap_session_reference( session );
if (token && token->length) {
s->token_length = token->length;
memcpy(s->token, token->s, min(s->token_length, 8));
}
s->query = query;
s->has_block2 = has_block2;
s->block2 = block2;
/* add subscriber to resource */
LL_PREPEND(resource->subscribers, s);
coap_log(LOG_DEBUG, "create new subscription\n");
return s;
}
void
coap_touch_observer(coap_context_t *context, coap_session_t *session,
const coap_binary_t *token) {
coap_subscription_t *s;
RESOURCES_ITER(context->resources, r) {
s = coap_find_observer(r, session, token);
if (s) {
s->fail_cnt = 0;
}
}
}
int
coap_delete_observer(coap_resource_t *resource, coap_session_t *session,
const coap_binary_t *token) {
coap_subscription_t *s;
s = coap_find_observer(resource, session, token);
if ( s && coap_get_log_level() >= LOG_DEBUG ) {
char outbuf[2 * 8 + 1] = "";
unsigned int i;
for ( i = 0; i < s->token_length; i++ )
snprintf( &outbuf[2 * i], 3, "%02x", s->token[i] );
coap_log(LOG_DEBUG, "removed observer tid %s\n", outbuf);
}
if (resource->subscribers && s) {
LL_DELETE(resource->subscribers, s);
coap_session_release( session );
if (s->query)
coap_delete_string(s->query);
COAP_FREE_TYPE(subscription,s);
}
return s != NULL;
}
void
coap_delete_observers(coap_context_t *context, coap_session_t *session) {
RESOURCES_ITER(context->resources, resource) {
coap_subscription_t *s, *tmp;
LL_FOREACH_SAFE(resource->subscribers, s, tmp) {
if (s->session == session) {
LL_DELETE(resource->subscribers, s);
coap_session_release(session);
if (s->query)
coap_delete_string(s->query);
COAP_FREE_TYPE(subscription, s);
}
}
}
}
static void
coap_notify_observers(coap_context_t *context, coap_resource_t *r) {
coap_method_handler_t h;
coap_subscription_t *obs;
coap_binary_t 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;
if (obs->session->con_active >= COAP_DEFAULT_NSTART &&
((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) ||
(obs->non_cnt >= COAP_OBS_MAX_NON)))
continue;
coap_tid_t tid = COAP_INVALID_TID;
obs->dirty = 0;
/* initialize response */
response = coap_pdu_init(COAP_MESSAGE_CON, 0, 0, coap_session_max_pdu_size(obs->session));
if (!response) {
obs->dirty = 1;
r->partiallydirty = 1;
coap_log(LOG_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;
coap_log(LOG_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;
obs->tid = response->tid = coap_new_message_id(obs->session);
if ((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) == 0
&& obs->non_cnt < COAP_OBS_MAX_NON) {
response->type = COAP_MESSAGE_NON;
} else {
response->type = COAP_MESSAGE_CON;
}
/* fill with observer-specific data */
h(context, r, obs->session, NULL, &token, obs->query, response);
/* TODO: do not send response and remove observer when
* COAP_RESPONSE_CLASS(response->hdr->code) > 2
*/
if (response->type == COAP_MESSAGE_CON) {
obs->non_cnt = 0;
} else {
obs->non_cnt++;
}
tid = coap_send( obs->session, response );
if (COAP_INVALID_TID == tid) {
coap_log(LOG_DEBUG,
"coap_check_notify: sending failed, resource stays "
"partially dirty\n");
obs->dirty = 1;
r->partiallydirty = 1;
}
}
}
r->dirty = 0;
}
int
coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query) {
return coap_resource_notify_observers(r, query);
}
int
coap_resource_notify_observers(coap_resource_t *r, const coap_string_t *query) {
if (!r->observable)
return 0;
if (query) {
coap_subscription_t *obs;
int found = 0;
LL_FOREACH(r->subscribers, obs) {
if (obs->query
&& obs->query->length==query->length
&& memcmp(obs->query->s, query->s, query->length)==0 ) {
found = 1;
if (!r->dirty && !obs->dirty) {
obs->dirty = 1;
r->partiallydirty = 1;
}
}
}
if (!found)
return 0;
} else {
if ( !r->subscribers )
return 0;
r->dirty = 1;
}
/* Increment value for next Observe use. Observe value must be < 2^24 */
r->observe = (r->observe + 1) & 0xFFFFFF;
return 1;
}
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,
coap_session_t *session,
const coap_binary_t *token) {
coap_subscription_t *obs, *otmp;
LL_FOREACH_SAFE(resource->subscribers, obs, otmp) {
if ( obs->session == session &&
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;
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->session->addr_info.remote,
addr, INET6_ADDRSTRLEN+8))
coap_log(LOG_DEBUG, "** removed observer %s\n", addr);
}
coap_cancel_all_messages(context, obs->session,
obs->token, obs->token_length);
coap_session_release( obs->session );
if (obs->query)
coap_delete_string(obs->query);
COAP_FREE_TYPE(subscription, obs);
}
break; /* break loop if observer was found */
}
}
}
void
coap_handle_failed_notify(coap_context_t *context,
coap_session_t *session,
const coap_binary_t *token) {
RESOURCES_ITER(context->resources, r) {
coap_remove_failed_observers(context, r, session, token);
}
}
#endif /* WITHOUT_NOTIFY */
coap_internal.h
Pulls together all the internal only header files.
coap_session_max_pdu_size
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
Definition: coap_session.c:187
coap_session_reference
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition: coap_session.c:68
coap_session_release
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition: coap_session.c:74
COAP_DEFAULT_NSTART
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server.
Definition: coap_session.h:395
coap_get_log_level
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition: coap_debug.c:61
coap_print_addr
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
Definition: coap_debug.c:161
coap_log
#define coap_log(level,...)
Logging function.
Definition: coap_debug.h:129
LOG_WARNING
@ LOG_WARNING
Warning.
Definition: coap_debug.h:52
LOG_DEBUG
@ LOG_DEBUG
Debug.
Definition: coap_debug.h:55
coap_opt_length
uint16_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:238
coap_opt_value
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: option.c:275
coap_delete_str_const
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
Definition: str.c:40
coap_string_equal
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
Definition: str.h:109
coap_new_str_const
coap_str_const_t * coap_new_str_const(const uint8_t *data, size_t size)
Returns a new const string object with at least size+1 bytes storage allocated, and the provided data...
Definition: str.c:31
coap_delete_string
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition: str.c:27
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: coap_subscribe_internal.h:31
coap_subscription_init
void coap_subscription_init(coap_subscription_t *s)
Definition: subscribe.c:13
COAP_OBS_MAX_FAIL
#define COAP_OBS_MAX_FAIL
Number of confirmable notifications that may fail (i.e.
Definition: coap_subscribe_internal.h:40
COAP_STATIC_INLINE
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
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.
COAP_RESOURCE
@ COAP_RESOURCE
Definition: mem.h:39
COAP_RESOURCEATTR
@ COAP_RESOURCEATTR
Definition: mem.h:40
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_send
coap_tid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition: net.c:966
coap_cancel_all_messages
void coap_cancel_all_messages(coap_context_t *context, coap_session_t *session, const uint8_t *token, size_t token_length)
Cancels all outstanding messages for session session that have the specified token.
Definition: net.c:1672
coap_new_message_id
COAP_STATIC_INLINE uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
Definition: net.h:390
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
coap_delete_pdu
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition: pdu.c:127
coap_pdu_init
coap_pdu_t * coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition: pdu.c:79
coap_add_token
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
Definition: pdu.c:191
COAP_REQUEST_PUT
#define COAP_REQUEST_PUT
Definition: pdu.h:81
COAP_MESSAGE_NON
#define COAP_MESSAGE_NON
Definition: pdu.h:73
COAP_MESSAGE_CON
#define COAP_MESSAGE_CON
Definition: pdu.h:72
coap_tid_t
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:238
COAP_REQUEST_GET
#define COAP_REQUEST_GET
Definition: pdu.h:79
COAP_INVALID_TID
#define COAP_INVALID_TID
Indicates an invalid transaction id.
Definition: pdu.h:241
MATCH_URI
#define MATCH_URI
coap_unknown_resource_uri
static const uint8_t coap_unknown_resource_uri[]
Definition: resource.c:325
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_free_resource
static void coap_free_resource(coap_resource_t *resource)
Definition: resource.c:418
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:79
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
null_path_value
static coap_str_const_t null_path_value
Definition: resource.c:291
coap_delete_attr
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
Definition: resource.c:401
coap_resource_notify_observers
int coap_resource_notify_observers(coap_resource_t *r, 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_resource_set_dirty
int coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query)
Definition: resource.c:827
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
match
static int match(const coap_str_const_t *text, const coap_str_const_t *pattern, int match_prefix, int match_substring)
Definition: resource.c:99
coap_handle_failed_notify
void coap_handle_failed_notify(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Definition: resource.c:924
MATCH_SUBSTRING
#define MATCH_SUBSTRING
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_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:163
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_remove_failed_observers
static void coap_remove_failed_observers(coap_context_t *context, coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token)
Checks the failure counter for (peer, token) and removes peer from the list of observers for the give...
Definition: resource.c:882
COAP_PRINT_STATUS_MAX
#define COAP_PRINT_STATUS_MAX
Definition: resource.c:55
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_MALLOC_TYPE
#define COAP_MALLOC_TYPE(Type)
Definition: resource.c:50
coap_find_observer_query
static coap_subscription_t * coap_find_observer_query(coap_resource_t *resource, coap_session_t *session, const coap_string_t *query)
Definition: resource.c:600
null_path
static coap_str_const_t * null_path
Definition: resource.c:292
COAP_FREE_TYPE
#define COAP_FREE_TYPE(Type, Object)
Definition: resource.c:52
min
#define min(a, b)
Definition: resource.c:58
coap_add_attr
coap_attr_t * coap_add_attr(coap_resource_t *resource, coap_str_const_t *name, coap_str_const_t *val, 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_notify_observers
static void coap_notify_observers(coap_context_t *context, coap_resource_t *r)
Definition: resource.c:740
MATCH_PREFIX
#define MATCH_PREFIX
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:91
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
INET6_ADDRSTRLEN
#define INET6_ADDRSTRLEN
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
RESOURCES_ADD
#define RESOURCES_ADD(r, obj)
Definition: resource.h:448
COAP_ATTR_FLAGS_RELEASE_VALUE
#define COAP_ATTR_FLAGS_RELEASE_VALUE
Definition: resource.h:44
COAP_ATTR_FLAGS_RELEASE_NAME
#define COAP_ATTR_FLAGS_RELEASE_NAME
Definition: resource.h:43
COAP_PRINT_STATUS_TRUNC
#define COAP_PRINT_STATUS_TRUNC
Definition: resource.h:315
COAP_RESOURCE_FLAGS_NOTIFY_CON
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Notifications will be sent confirmable by default.
Definition: resource.h:66
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
RESOURCES_FIND
#define RESOURCES_FIND(r, k, res)
Definition: resource.h:458
COAP_PRINT_STATUS_ERROR
#define COAP_PRINT_STATUS_ERROR
Definition: resource.h:314
COAP_PRINT_OUTPUT_LENGTH
#define COAP_PRINT_OUTPUT_LENGTH(v)
Definition: resource.h:313
COAP_RESOURCE_FLAGS_RELEASE_URI
#define COAP_RESOURCE_FLAGS_RELEASE_URI
The URI passed to coap_resource_init() is free'd by coap_delete_resource().
Definition: resource.h:54
RESOURCES_DELETE
#define RESOURCES_DELETE(r, obj)
Definition: resource.h:451
RESOURCES_ITER
#define RESOURCES_ITER(r, tmp)
Definition: resource.h:454
coap_addr_tuple_t::remote
coap_address_t remote
remote address and port
Definition: coap_io.h:50
coap_attr_t
Definition: resource.h:46
coap_attr_t::value
coap_str_const_t * value
Definition: resource.h:49
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_binary_t::length
size_t length
length of binary data
Definition: str.h:44
coap_binary_t::s
uint8_t * s
binary data
Definition: str.h:45
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_context_t::unknown_resource
struct coap_resource_t * unknown_resource
can be used for handling unknown resources
Definition: net.h:151
coap_context_t::resources
struct coap_resource_t * resources
hash table or list of known resources
Definition: net.h:149
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_pdu_t::type
uint8_t type
message type
Definition: pdu.h:286
coap_pdu_t::tid
uint16_t tid
transaction id, if any, in regular host byte order
Definition: pdu.h:291
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::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::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_session_t
Definition: coap_session.h:59
coap_session_t::addr_info
coap_addr_tuple_t addr_info
key: remote/local address info
Definition: coap_session.h:68
coap_session_t::con_active
uint8_t con_active
Active CON request sent.
Definition: coap_session.h:75
coap_str_const_t
Coap string data definition with const data.
Definition: str.h:33
coap_str_const_t::s
const uint8_t * s
string data
Definition: str.h:35
coap_str_const_t::length
size_t length
length of string
Definition: str.h:34
coap_string_t
Coap string data definition.
Definition: str.h:25
coap_string_t::s
uint8_t * s
string data
Definition: str.h:27
coap_string_t::length
size_t length
length of string
Definition: str.h:26
coap_subscription_t
Subscriber information.
Definition: coap_subscribe_internal.h:44
coap_subscription_t::block2
coap_block_t block2
GET request Block2 definition.
Definition: coap_subscribe_internal.h:55
coap_subscription_t::token
unsigned char token[8]
token used for subscription
Definition: coap_subscribe_internal.h:57
coap_subscription_t::has_block2
unsigned int has_block2
GET request had Block2 definition.
Definition: coap_subscribe_internal.h:53
coap_subscription_t::fail_cnt
unsigned int fail_cnt
up to 3 confirmable notifies can fail
Definition: coap_subscribe_internal.h:49
coap_subscription_t::non_cnt
unsigned int non_cnt
up to 15 non-confirmable notifies allowed
Definition: coap_subscribe_internal.h:48
coap_subscription_t::token_length
size_t token_length
actual length of token
Definition: coap_subscribe_internal.h:56
coap_subscription_t::session
struct coap_session_t * session
subscriber session
Definition: coap_subscribe_internal.h:46
coap_subscription_t::dirty
unsigned int dirty
set if the notification temporarily could not be sent (in that case, the resource's partially dirty f...
Definition: coap_subscribe_internal.h:50
coap_subscription_t::tid
uint16_t tid
transaction id, if any, in regular host byte order
Definition: coap_subscribe_internal.h:54
coap_subscription_t::query
struct coap_string_t * query
query string used for subscription, if any
Definition: coap_subscribe_internal.h:58
HASH_DELETE
#define HASH_DELETE(hh, head, delptr)
Definition: uthash.h:368
HASH_ITER
#define HASH_ITER(hh, head, el, tmp)
Definition: uthash.h:1031
uint8_t
unsigned char uint8_t
Definition: uthash.h:79
LL_DELETE
#define LL_DELETE(head, del)
Definition: utlist.h:385
LL_FOREACH
#define LL_FOREACH(head, el)
Definition: utlist.h:413
LL_PREPEND
#define LL_PREPEND(head, add)
Definition: utlist.h:314
LL_FOREACH_SAFE
#define LL_FOREACH_SAFE(head, el, tmp)
Definition: utlist.h:419