Loading...
Searching...
No Matches
coap_resource.c
Go to the documentation of this file.
/* coap_resource.c -- generic resource handling
*
* Copyright (C) 2010--2023 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include "coap3/coap_internal.h"
#if COAP_SERVER_SUPPORT
#include <stdio.h>
#ifdef COAP_EPOLL_SUPPORT
#include <sys/epoll.h>
#include <sys/timerfd.h>
#endif /* COAP_EPOLL_SUPPORT */
#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,
const coap_string_t *query_filter COAP_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, const coap_string_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 = query_filter->s;
while (resource_param.length < query_filter->length &&
resource_param.s[resource_param.length] != '=')
resource_param.length++;
if (resource_param.length < query_filter->length) {
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 =
query_filter->s + resource_param.length + 1;
assert((resource_param.length + 1) <= query_filter->length);
query_pattern.length =
query_filter->length - (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 expect 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;
r->observe = 2;
} else {
coap_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_init2(coap_method_handler_t put_handler, 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));
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);
r->flags = flags & COAP_RESOURCE_FLAGS_MCAST_LIST;
coap_register_handler(r, COAP_REQUEST_PUT, put_handler);
} else {
coap_log_debug("coap_resource_unknown_init: no memory left\n");
}
return r;
}
coap_resource_t *
coap_resource_unknown_init(coap_method_handler_t put_handler) {
return coap_resource_unknown_init2(put_handler, 0);
}
static const uint8_t coap_proxy_resource_uri[] =
"- Proxy URI -";
coap_resource_t *
coap_resource_proxy_uri_init2(coap_method_handler_t handler,
size_t host_name_count,
const char *host_name_list[], int flags) {
coap_resource_t *r;
if (host_name_count == 0) {
coap_log_err("coap_resource_proxy_uri_init: Must have one or more host names defined\n");
return NULL;
}
r = (coap_resource_t *)coap_malloc_type(COAP_RESOURCE, sizeof(coap_resource_t));
if (r) {
size_t i;
memset(r, 0, sizeof(coap_resource_t));
r->is_proxy_uri = 1;
/* Something unlikely to be used, but it shows up in the logs */
r->uri_path = coap_new_str_const(coap_proxy_resource_uri, sizeof(coap_proxy_resource_uri)-1);
/* Preset all the handlers */
for (i = 0; i < (sizeof(r->handler) / sizeof(r->handler[0])); i++) {
r->handler[i] = handler;
}
if (host_name_count) {
r->proxy_name_list = coap_malloc_type(COAP_STRING, host_name_count *
sizeof(coap_str_const_t *));
if (r->proxy_name_list) {
for (i = 0; i < host_name_count; i++) {
r->proxy_name_list[i] =
coap_new_str_const((const uint8_t *)host_name_list[i],
strlen(host_name_list[i]));
if (!r->proxy_name_list[i]) {
coap_log_err("coap_resource_proxy_uri_init: unable to add host name\n");
if (i == 0) {
coap_free_type(COAP_STRING, r->proxy_name_list);
r->proxy_name_list = NULL;
}
break;
}
}
r->proxy_name_count = i;
}
}
r->flags = flags & COAP_RESOURCE_FLAGS_MCAST_LIST;
} else {
coap_log_debug("coap_resource_proxy_uri_init2: no memory left\n");
}
return r;
}
coap_resource_t *
coap_resource_proxy_uri_init(coap_method_handler_t handler,
size_t host_name_count, const char *host_name_list[]) {
return coap_resource_proxy_uri_init2(handler, host_name_count,
host_name_list, 0);
}
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_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;
}
coap_str_const_t *
coap_attr_get_value(coap_attr_t *attr) {
if (attr)
return attr->value;
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);
}
coap_free_type(COAP_RESOURCEATTR, attr);
}
typedef enum coap_deleting_resource_t {
COAP_DELETING_RESOURCE,
COAP_NOT_DELETING_RESOURCE
} coap_deleting_resource_t;
static void coap_notify_observers(coap_context_t *context, coap_resource_t *r,
coap_deleting_resource_t deleting);
static void
coap_free_resource(coap_resource_t *resource) {
coap_attr_t *attr, *tmp;
coap_subscription_t *obs, *otmp;
assert(resource);
if (!resource->context->observe_no_clear) {
coap_resource_notify_observers(resource, NULL);
coap_notify_observers(resource->context, resource, COAP_DELETING_RESOURCE);
}
if (resource->context->resource_deleted)
resource->context->resource_deleted(resource->context, resource->uri_path,
resource->context->observe_user_data);
if (resource->context->release_userdata && resource->user_data)
resource->context->release_userdata(resource->user_data);
/* 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) {
if (resource->context->observe_deleted)
resource->context->observe_deleted(obs->session, obs,
resource->context->observe_user_data);
coap_session_release(obs->session);
coap_delete_pdu(obs->pdu);
coap_delete_cache_key(obs->cache_key);
coap_free_type(COAP_SUBSCRIPTION, obs);
}
if (resource->proxy_name_count && resource->proxy_name_list) {
size_t i;
for (i = 0; i < resource->proxy_name_count; i++) {
coap_delete_str_const(resource->proxy_name_list[i]);
}
coap_free_type(COAP_STRING, resource->proxy_name_list);
}
coap_free_type(COAP_RESOURCE, resource);
}
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 if (resource->is_proxy_uri) {
if (context->proxy_uri_resource)
coap_free_resource(context->proxy_uri_resource);
context->proxy_uri_resource = resource;
} else {
coap_resource_t *r = coap_get_resource_from_uri_path(context,
resource->uri_path);
if (r) {
coap_log_warn("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);
#if COAP_WITH_OBSERVE_PERSIST
if (context->unknown_pdu && context->dyn_resource_save_file &&
context->dyn_resource_added && resource->observable) {
coap_bin_const_t raw_packet;
raw_packet.s = context->unknown_pdu->token -
context->unknown_pdu->hdr_size;
raw_packet.length = context->unknown_pdu->used_size +
context->unknown_pdu->hdr_size;
context->dyn_resource_added(context->unknown_session, resource->uri_path,
&raw_packet, context->observe_user_data);
}
#endif /* COAP_WITH_OBSERVE_PERSIST */
}
assert(resource->context == NULL);
resource->context = context;
}
/*
* Input context is ignored, but param left there to keep API consistent
*/
int
coap_delete_resource(coap_context_t *context, coap_resource_t *resource) {
if (!resource)
return 0;
context = resource->context;
if (resource->is_unknown) {
if (context && context->unknown_resource == resource) {
context->unknown_resource = NULL;
}
} else if (resource->is_proxy_uri) {
if (context && context->proxy_uri_resource == resource) {
context->proxy_uri_resource = NULL;
}
} else if (context) {
/* 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;
}
if (context->proxy_uri_resource) {
coap_free_resource(context->proxy_uri_resource);
context->proxy_uri_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);
}
#if COAP_OSCORE_SUPPORT
/* If oscore is enabled */
if (resource->flags & COAP_RESOURCE_FLAGS_OSCORE_ONLY)
COPY_COND_WITH_OFFSET(p, bufend, *offset, ";osc", 4, *len);
#endif /* COAP_OSCORE_SUPPORT */
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,
coap_request_t method,
coap_method_handler_t handler) {
coap_register_request_handler(resource, method, handler);
}
void
coap_register_request_handler(coap_resource_t *resource,
coap_request_t 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_subscription_t *
coap_find_observer(coap_resource_t *resource, coap_session_t *session,
const coap_bin_const_t *token) {
coap_subscription_t *s;
assert(resource);
assert(session);
LL_FOREACH(resource->subscribers, s) {
if (s->session == session &&
(!token || coap_binary_equal(token, &s->pdu->actual_token)))
return s;
}
return NULL;
}
static coap_subscription_t *
coap_find_observer_cache_key(coap_resource_t *resource, coap_session_t *session,
const coap_cache_key_t *cache_key) {
coap_subscription_t *s;
assert(resource);
assert(session);
LL_FOREACH(resource->subscribers, s) {
if (s->session == session
&& (memcmp(cache_key, s->cache_key, sizeof(coap_cache_key_t)) == 0))
return s;
}
return NULL;
}
coap_subscription_t *
coap_add_observer(coap_resource_t *resource,
coap_session_t *session,
const coap_bin_const_t *token,
const coap_pdu_t *request) {
coap_subscription_t *s;
coap_cache_key_t *cache_key = NULL;
size_t len;
const uint8_t *data;
/* https://rfc-editor.org/rfc/rfc7641#section-3.6 */
static const uint16_t cache_ignore_options[] = { COAP_OPTION_ETAG,
COAP_OPTION_OSCORE
};
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
*/
cache_key = coap_cache_derive_key_w_ignore(session, request,
COAP_CACHE_IS_SESSION_BASED,
cache_ignore_options,
sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0]));
if (cache_key) {
s = coap_find_observer_cache_key(resource, session, cache_key);
if (s) {
/* Delete old entry with old token */
coap_delete_observer(resource, session, &s->pdu->actual_token);
s = NULL;
}
}
}
/* We are done if subscription was found. */
if (s) {
return s;
}
/* Check if there is already maximum number of subscribers present */
#if (COAP_RESOURCE_MAX_SUBSCRIBER > 0)
uint32_t subscriber_count = 0;
LL_COUNT(resource->subscribers, s, subscriber_count);
if (subscriber_count >= COAP_RESOURCE_MAX_SUBSCRIBER) {
return NULL; /* Signal error */
}
#endif /* COAP_RESOURCE_MAX_SUBSCRIBER */
/* Create a new subscription */
s = coap_malloc_type(COAP_SUBSCRIPTION, sizeof(coap_subscription_t));
if (!s) {
coap_delete_cache_key(cache_key);
return NULL;
}
coap_subscription_init(s);
s->pdu = coap_pdu_duplicate(request, session, token->length,
token->s, NULL);
if (s->pdu == NULL) {
coap_delete_cache_key(cache_key);
coap_free_type(COAP_SUBSCRIPTION, s);
return NULL;
}
if (coap_get_data(request, &len, &data)) {
/* This could be a large bodied FETCH */
s->pdu->max_size = 0;
coap_add_data(s->pdu, len, data);
}
if (cache_key == NULL) {
cache_key = coap_cache_derive_key_w_ignore(session, request,
COAP_CACHE_IS_SESSION_BASED,
cache_ignore_options,
sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0]));
if (cache_key == NULL) {
coap_delete_pdu(s->pdu);
coap_delete_cache_key(cache_key);
coap_free_type(COAP_SUBSCRIPTION, s);
return NULL;
}
}
s->cache_key = cache_key;
s->session = coap_session_reference(session);
/* add subscriber to resource */
LL_PREPEND(resource->subscribers, s);
coap_log_debug("create new subscription %p key 0x%02x%02x%02x%02x\n",
(void *)s, s->cache_key->key[0], s->cache_key->key[1],
s->cache_key->key[2], s->cache_key->key[3]);
if (session->context->observe_added && session->proto == COAP_PROTO_UDP) {
coap_bin_const_t raw_packet;
coap_bin_const_t *oscore_info = NULL;
#if COAP_OSCORE_SUPPORT
oscore_association_t *association;
if (session->recipient_ctx && session->recipient_ctx->recipient_id) {
/*
* Need to track the association used for tracking this observe, done as
* a CBOR array. Read in coap_persist_observe_add().
*
* If an entry is null, then use nil, else a set of bytes
*
* Currently tracking 5 items
* recipient_id
* id_context
* aad (from oscore_association_t)
* partial_iv (from oscore_association_t)
* nonce (from oscore_association_t)
*/
uint8_t info_buffer[60];
uint8_t *info_buf = info_buffer;
size_t info_len = sizeof(info_buffer);
size_t ret = 0;
coap_bin_const_t ctoken = { token->length, token->s };
ret += oscore_cbor_put_array(&info_buf, &info_len, 5);
ret += oscore_cbor_put_bytes(&info_buf,
&info_len,
session->recipient_ctx->recipient_id->s,
session->recipient_ctx->recipient_id->length);
if (session->recipient_ctx->osc_ctx &&
session->recipient_ctx->osc_ctx->id_context) {
ret += oscore_cbor_put_bytes(&info_buf,
&info_len,
session->recipient_ctx->osc_ctx->id_context->s,
session->recipient_ctx->osc_ctx->id_context->length);
} else {
ret += oscore_cbor_put_nil(&info_buf, &info_len);
}
association = oscore_find_association(session, &ctoken);
if (association) {
if (association->aad) {
ret += oscore_cbor_put_bytes(&info_buf,
&info_len,
association->aad->s,
association->aad->length);
} else {
ret += oscore_cbor_put_nil(&info_buf, &info_len);
}
if (association->partial_iv) {
ret += oscore_cbor_put_bytes(&info_buf,
&info_len,
association->partial_iv->s,
association->partial_iv->length);
} else {
ret += oscore_cbor_put_nil(&info_buf, &info_len);
}
if (association->nonce) {
ret += oscore_cbor_put_bytes(&info_buf,
&info_len,
association->nonce->s,
association->nonce->length);
} else {
ret += oscore_cbor_put_nil(&info_buf, &info_len);
}
} else {
ret += oscore_cbor_put_nil(&info_buf, &info_len);
ret += oscore_cbor_put_nil(&info_buf, &info_len);
}
oscore_info = coap_new_bin_const(info_buffer, ret);
}
#endif /* COAP_OSCORE_SUPPORT */
/* s->pdu header is not currently encoded */
memcpy(s->pdu->token - request->hdr_size,
request->token - request->hdr_size, request->hdr_size);
raw_packet.s = s->pdu->token - request->hdr_size;
raw_packet.length = s->pdu->used_size + request->hdr_size;
session->context->observe_added(session, s, session->proto,
&session->endpoint->bind_addr,
&session->addr_info,
&raw_packet,
oscore_info,
session->context->observe_user_data);
#if COAP_OSCORE_SUPPORT
coap_delete_bin_const(oscore_info);
#endif /* COAP_OSCORE_SUPPORT */
}
if (resource->context->track_observe_value) {
/* Track last used observe value (as app handler is called) */
resource->context->track_observe_value(resource->context,resource->uri_path,
resource->observe,
resource->context->observe_user_data);
}
return s;
}
void
coap_touch_observer(coap_context_t *context, coap_session_t *session,
const coap_bin_const_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_bin_const_t *token) {
coap_subscription_t *s;
s = coap_find_observer(resource, session, token);
if (s && coap_get_log_level() >= COAP_LOG_DEBUG) {
char outbuf[2 * 8 + 1] = "";
unsigned int i;
for (i = 0; i < s->pdu->actual_token.length; i++) {
size_t size = strlen(outbuf);
snprintf(&outbuf[size], sizeof(outbuf)-size, "%02x",
s->pdu->actual_token.s[i]);
}
coap_log_debug("removed subscription %p with token '%s' key 0x%02x%02x%02x%02x\n",
(void *)s, outbuf, s->cache_key->key[0], s->cache_key->key[1],
s->cache_key->key[2], s-> cache_key->key[3]);
}
if (s && session->context->observe_deleted)
session->context->observe_deleted(session, s,
session->context->observe_user_data);
if (resource->subscribers && s) {
LL_DELETE(resource->subscribers, s);
coap_session_release(session);
coap_delete_pdu(s->pdu);
coap_delete_cache_key(s->cache_key);
coap_free_type(COAP_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) {
if (context->observe_deleted)
context->observe_deleted(session, s, context->observe_user_data);
LL_DELETE(resource->subscribers, s);
coap_session_release(session);
coap_delete_pdu(s->pdu);
coap_delete_cache_key(s->cache_key);
coap_free_type(COAP_SUBSCRIPTION, s);
}
}
}
}
static void
coap_notify_observers(coap_context_t *context, coap_resource_t *r,
coap_deleting_resource_t deleting) {
coap_method_handler_t h;
coap_subscription_t *obs, *otmp;
coap_pdu_t *response;
uint8_t buf[4];
coap_string_t *query;
coap_block_b_t block;
coap_tick_t now;
coap_session_t *obs_session;
if (r->observable && (r->dirty || r->partiallydirty)) {
r->partiallydirty = 0;
LL_FOREACH_SAFE(r->subscribers, obs, otmp) {
obs_session = obs->session;
if (r->dirty == 0 && obs->dirty == 0) {
/*
* running this resource due to partiallydirty, but this observation's
* notification was already enqueued
*/
context->observe_pending = 1;
continue;
}
if (obs->session->con_active >= COAP_NSTART(obs->session) &&
((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) ||
(obs->non_cnt >= COAP_OBS_MAX_NON))) {
/* Waiting for the previous unsolicited response to finish */
r->partiallydirty = 1;
obs->dirty = 1;
context->observe_pending = 1;
continue;
}
coap_ticks(&now);
if (obs->session->lg_xmit && obs->session->lg_xmit->last_all_sent == 0 &&
obs->session->lg_xmit->last_obs &&
(obs->session->lg_xmit->last_obs + 2*COAP_TICKS_PER_SECOND) > now) {
/* Waiting for the previous blocked unsolicited response to finish */
r->partiallydirty = 1;
obs->dirty = 1;
context->observe_pending = 1;
continue;
}
coap_mid_t mid = COAP_INVALID_MID;
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;
context->observe_pending = 1;
coap_log_debug("coap_check_notify: pdu init failed, resource stays "
"partially dirty\n");
continue;
}
if (!coap_add_token(response, obs->pdu->actual_token.length,
obs->pdu->actual_token.s)) {
obs->dirty = 1;
r->partiallydirty = 1;
context->observe_pending = 1;
coap_log_debug("coap_check_notify: cannot add token, resource stays "
"partially dirty\n");
coap_delete_pdu(response);
continue;
}
obs->pdu->mid = response->mid = coap_new_message_id(obs->session);
/* A lot of the reliable code assumes type is CON */
if (COAP_PROTO_NOT_RELIABLE(obs->session->proto) &&
(r->flags & COAP_RESOURCE_FLAGS_NOTIFY_CON) == 0 &&
((r->flags & COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS) ||
obs->non_cnt < COAP_OBS_MAX_NON)) {
response->type = COAP_MESSAGE_NON;
} else {
response->type = COAP_MESSAGE_CON;
}
switch (deleting) {
case COAP_NOT_DELETING_RESOURCE:
/* fill with observer-specific data */
coap_add_option_internal(response, COAP_OPTION_OBSERVE,
coap_encode_var_safe(buf, sizeof(buf),
r->observe),
buf);
if (coap_get_block_b(obs->session, obs->pdu, COAP_OPTION_BLOCK2,
&block)) {
/* Will get updated later (e.g. M bit) if appropriate */
coap_add_option_internal(response, COAP_OPTION_BLOCK2,
coap_encode_var_safe(buf, sizeof(buf),
((0 << 4) |
(0 << 3) |
block.aszx)),
buf);
}
#if COAP_Q_BLOCK_SUPPORT
else if (coap_get_block_b(obs->session, obs->pdu, COAP_OPTION_Q_BLOCK2,
&block)) {
/* Will get updated later (e.g. M bit) if appropriate */
coap_add_option_internal(response, COAP_OPTION_Q_BLOCK2,
coap_encode_var_safe(buf, sizeof(buf),
((0 << 4) |
(0 << 3) |
block.szx)),
buf);
}
#endif /* COAP_Q_BLOCK_SUPPORT */
h = r->handler[obs->pdu->code - 1];
assert(h); /* we do not allow subscriptions if no
* GET/FETCH handler is defined */
query = coap_get_query(obs->pdu);
coap_log_debug("Observe PDU presented to app.\n");
coap_show_pdu(COAP_LOG_DEBUG, obs->pdu);
coap_log_debug("call custom handler for resource '%*.*s' (4)\n",
(int)r->uri_path->length, (int)r->uri_path->length,
r->uri_path->s);
h(r, obs->session, obs->pdu, query, response);
/* Check if lg_xmit generated and update PDU code if so */
coap_check_code_lg_xmit(obs->session, obs->pdu, response, r, query);
coap_delete_string(query);
if (COAP_RESPONSE_CLASS(response->code) != 2) {
coap_remove_option(response, COAP_OPTION_OBSERVE);
}
if (COAP_RESPONSE_CLASS(response->code) > 2) {
coap_delete_observer(r, obs->session, &obs->pdu->actual_token);
obs = NULL;
}
break;
case COAP_DELETING_RESOURCE:
default:
/* Don't worry if it does not get there */
response->type = COAP_MESSAGE_NON;
response->code = COAP_RESPONSE_CODE(404);
break;
}
if (obs) {
if (response->type == COAP_MESSAGE_CON ||
(r->flags & COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS)) {
obs->non_cnt = 0;
} else {
obs->non_cnt++;
}
#if COAP_Q_BLOCK_SUPPORT
if (response->code == COAP_RESPONSE_CODE(205) &&
coap_get_block_b(obs->session, response, COAP_OPTION_Q_BLOCK2,
&block) &&
block.m) {
query = coap_get_query(obs->pdu);
mid = coap_send_q_block2(obs->session, r, query, obs->pdu->code,
block, response, 1);
coap_delete_string(query);
goto finish;
}
#endif /* COAP_Q_BLOCK_SUPPORT */
}
mid = coap_send_internal(obs_session, response);
#if COAP_Q_BLOCK_SUPPORT
finish:
#endif /* COAP_Q_BLOCK_SUPPORT */
if (COAP_INVALID_MID == mid && obs) {
coap_subscription_t *s;
coap_log_debug("coap_check_notify: sending failed, resource stays "
"partially dirty\n");
LL_FOREACH(r->subscribers, s) {
if (s == obs) {
/* obs not deleted during coap_send_internal() */
obs->dirty = 1;
break;
}
}
r->partiallydirty = 1;
context->observe_pending = 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 COAP_UNUSED) {
if (!r->observable)
return 0;
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;
assert(r->context);
if (r->context->track_observe_value) {
/* Track last used observe value */
if ((r->observe % r->context->observe_save_freq) == 0)
r->context->track_observe_value(r->context, r->uri_path,
r->observe,
r->context->observe_user_data);
}
r->context->observe_pending = 1;
#ifdef COAP_EPOLL_SUPPORT
coap_update_epoll_timer(r->context, 0);
#endif /* COAP_EPOLL_SUPPORT */
return 1;
}
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));
}
void
coap_resource_set_userdata(coap_resource_t *resource, void *data) {
resource->user_data = data;
}
void *
coap_resource_get_userdata(coap_resource_t *resource) {
return resource->user_data;
}
void
coap_resource_release_userdata_handler(coap_context_t *context,
coap_resource_release_userdata_handler_t callback) {
context->release_userdata = callback;
}
void
coap_resource_set_get_observable(coap_resource_t *resource, int mode) {
resource->observable = mode ? 1 : 0;
}
coap_str_const_t *
coap_resource_get_uri_path(coap_resource_t *resource) {
if (resource)
return resource->uri_path;
return NULL;
}
void
coap_check_notify(coap_context_t *context) {
if (context->observe_pending) {
context->observe_pending = 0;
RESOURCES_ITER(context->resources, r) {
coap_notify_observers(context, r, COAP_NOT_DELETING_RESOURCE);
}
}
}
void
coap_persist_set_observe_num(coap_resource_t *resource,
uint32_t start_observe_no) {
if (!resource)
return;
resource->observe = start_observe_no & 0xffffff;
}
static void
coap_remove_failed_observers(coap_context_t *context,
coap_resource_t *resource,
coap_session_t *session,
const coap_bin_const_t *token) {
coap_subscription_t *obs, *otmp;
LL_FOREACH_SAFE(resource->subscribers, obs, otmp) {
if (obs->session == session &&
coap_binary_equal(token, &obs->pdu->actual_token)) {
/* count failed notifies and remove when
* COAP_OBS_MAX_FAIL is reached */
obs->fail_cnt++;
if (obs->fail_cnt >= COAP_OBS_MAX_FAIL) {
coap_cancel_all_messages(context, obs->session,
&obs->pdu->actual_token);
coap_delete_observer(resource, session, token);
}
break; /* break loop if observer was found */
}
}
}
void
coap_handle_failed_notify(coap_context_t *context,
coap_session_t *session,
const coap_bin_const_t *token) {
RESOURCES_ITER(context->resources, r) {
coap_remove_failed_observers(context, r, session, token);
}
}
#endif /* ! COAP_SERVER_SUPPORT */
coap_internal.h
Pulls together all the internal only header files.
coap_update_epoll_timer
void coap_update_epoll_timer(coap_context_t *context, coap_tick_t delay)
Update the epoll timer fd as to when it is to trigger.
COAP_RESOURCE
@ COAP_RESOURCE
Definition coap_mem.h:47
COAP_RESOURCEATTR
@ COAP_RESOURCEATTR
Definition coap_mem.h:48
COAP_SUBSCRIPTION
@ COAP_SUBSCRIPTION
Definition coap_mem.h:58
COAP_STRING
@ COAP_STRING
Definition coap_mem.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_free_type
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
coap_resource_set_dirty
COAP_DEPRECATED int coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query)
coap_check_code_lg_xmit
void coap_check_code_lg_xmit(const coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, const coap_resource_t *resource, const coap_string_t *query)
The function checks that the code in a newly formed lg_xmit created by coap_add_data_large_response()...
coap_get_block_b
int coap_get_block_b(const coap_session_t *session, const coap_pdu_t *pdu, coap_option_num_t number, coap_block_b_t *block)
Initializes block from pdu.
Definition coap_block.c:58
coap_delete_cache_key
void coap_delete_cache_key(coap_cache_key_t *cache_key)
Delete the cache-key.
coap_cache_derive_key_w_ignore
coap_cache_key_t * coap_cache_derive_key_w_ignore(const coap_session_t *session, const coap_pdu_t *pdu, coap_cache_session_based_t session_based, const uint16_t *ignore_options, size_t ignore_count)
Calculates a cache-key for the given CoAP PDU.
COAP_CACHE_IS_SESSION_BASED
@ COAP_CACHE_IS_SESSION_BASED
Definition coap_cache.h:39
coap_tick_t
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:144
COAP_TICKS_PER_SECOND
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
Definition coap_time.h:159
RESOURCES_ADD
#define RESOURCES_ADD(r, obj)
Definition coap_resource_internal.h:121
coap_print_wellknown
coap_print_status_t coap_print_wellknown(coap_context_t *, unsigned char *, size_t *, size_t, const coap_string_t *)
coap_delete_all_resources
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
coap_delete_attr
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
RESOURCES_FIND
#define RESOURCES_FIND(r, k, res)
Definition coap_resource_internal.h:131
RESOURCES_DELETE
#define RESOURCES_DELETE(r, obj)
Definition coap_resource_internal.h:124
RESOURCES_ITER
#define RESOURCES_ITER(r, tmp)
Definition coap_resource_internal.h:127
COAP_RESOURCE_FLAGS_NOTIFY_NON
#define COAP_RESOURCE_FLAGS_NOTIFY_NON
Observe Notifications will be sent non-confirmable by default.
Definition coap_resource.h:54
coap_resource_proxy_uri_init
coap_resource_t * coap_resource_proxy_uri_init(coap_method_handler_t handler, size_t host_name_count, const char *host_name_list[])
Creates a new resource object for handling proxy URIs.
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.
COAP_ATTR_FLAGS_RELEASE_VALUE
#define COAP_ATTR_FLAGS_RELEASE_VALUE
Definition coap_resource.h:43
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.
coap_resource_proxy_uri_init2
coap_resource_t * coap_resource_proxy_uri_init2(coap_method_handler_t handler, size_t host_name_count, const char *host_name_list[], int flags)
Creates a new resource object for handling proxy URIs with configurable control over multicast reques...
COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS
#define COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS
Observe Notifications will always be sent non-confirmable.
Definition coap_resource.h:70
COAP_ATTR_FLAGS_RELEASE_NAME
#define COAP_ATTR_FLAGS_RELEASE_NAME
Definition coap_resource.h:42
coap_resource_set_mode
void coap_resource_set_mode(coap_resource_t *resource, int mode)
Sets the notification message type of resource resource to given mode.
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.
COAP_PRINT_STATUS_TRUNC
#define COAP_PRINT_STATUS_TRUNC
Definition coap_resource.h:463
coap_resource_release_userdata_handler
void coap_resource_release_userdata_handler(coap_context_t *context, coap_resource_release_userdata_handler_t callback)
Defines the context wide callback to use to when the resource is deleted to release the data held in ...
COAP_RESOURCE_FLAGS_OSCORE_ONLY
#define COAP_RESOURCE_FLAGS_OSCORE_ONLY
Define this resource as an OSCORE enabled access only.
Definition coap_resource.h:146
COAP_RESOURCE_FLAGS_MCAST_LIST
#define COAP_RESOURCE_FLAGS_MCAST_LIST
Definition coap_resource.h:129
COAP_RESOURCE_FLAGS_NOTIFY_CON
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Observe Notifications will be sent confirmable.
Definition coap_resource.h:60
coap_register_handler
void coap_register_handler(coap_resource_t *resource, coap_request_t method, coap_method_handler_t handler)
Registers the specified handler as message handler for the request type method.
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 coap_resource.h:458
coap_method_handler_t
void(* coap_method_handler_t)(coap_resource_t *, coap_session_t *, const coap_pdu_t *, const coap_string_t *, coap_pdu_t *)
Definition of message handler function.
Definition coap_resource.h:36
coap_resource_release_userdata_handler_t
void(* coap_resource_release_userdata_handler_t)(void *user_data)
Definition of release resource user_data callback function.
Definition coap_resource.h:331
coap_add_resource
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
coap_register_request_handler
void coap_register_request_handler(coap_resource_t *resource, coap_request_t method, coap_method_handler_t handler)
Registers the specified handler as message handler for the request type method.
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.
coap_resource_set_userdata
void coap_resource_set_userdata(coap_resource_t *resource, void *data)
Sets the user_data.
COAP_PRINT_STATUS_ERROR
#define COAP_PRINT_STATUS_ERROR
Definition coap_resource.h:462
coap_resource_get_uri_path
coap_str_const_t * coap_resource_get_uri_path(coap_resource_t *resource)
Get the uri_path from a resource.
coap_resource_unknown_init2
coap_resource_t * coap_resource_unknown_init2(coap_method_handler_t put_handler, int flags)
Creates a new resource object for the unknown resource handler with support for PUT and configurable ...
coap_attr_get_value
coap_str_const_t * coap_attr_get_value(coap_attr_t *attribute)
Returns attribute's value.
COAP_PRINT_OUTPUT_LENGTH
#define COAP_PRINT_OUTPUT_LENGTH(v)
Definition coap_resource.h:461
coap_delete_resource
int coap_delete_resource(coap_context_t *context, coap_resource_t *resource)
Deletes a resource identified by resource.
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.
coap_resource_get_userdata
void * coap_resource_get_userdata(coap_resource_t *resource)
Gets the user_data.
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 coap_resource.h:46
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.
coap_send_internal
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
Definition coap_net.c:1381
coap_cancel_all_messages
void coap_cancel_all_messages(coap_context_t *context, coap_session_t *session, coap_bin_const_t *token)
Cancels all outstanding messages for session session that have the specified token.
Definition coap_net.c:2323
coap_new_message_id
uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
Definition coap_session.c:1590
coap_ticks
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
coap_encode_var_safe
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
Definition coap_encode.c:47
coap_log_debug
#define coap_log_debug(...)
Definition coap_debug.h:120
coap_get_log_level
coap_log_t coap_get_log_level(void)
Get the current logging level.
Definition coap_debug.c:91
coap_show_pdu
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
Definition coap_debug.c:703
coap_log_warn
#define coap_log_warn(...)
Definition coap_debug.h:102
coap_log_err
#define coap_log_err(...)
Definition coap_debug.h:96
COAP_LOG_DEBUG
@ COAP_LOG_DEBUG
Definition coap_debug.h:58
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...
coap_persist_set_observe_num
void coap_persist_set_observe_num(coap_resource_t *resource, uint32_t observe_num)
Sets the current observe number value.
coap_resource_set_get_observable
void coap_resource_set_get_observable(coap_resource_t *resource, int mode)
Set whether a resource is observable.
oscore_cbor_put_nil
size_t oscore_cbor_put_nil(uint8_t **buffer, size_t *buf_size)
Definition oscore_cbor.c:59
oscore_cbor_put_bytes
size_t oscore_cbor_put_bytes(uint8_t **buffer, size_t *buf_size, const uint8_t *bytes, size_t bytes_len)
Definition oscore_cbor.c:100
oscore_cbor_put_array
size_t oscore_cbor_put_array(uint8_t **buffer, size_t *buf_size, size_t elements)
Definition oscore_cbor.c:92
oscore_find_association
oscore_association_t * oscore_find_association(coap_session_t *session, coap_bin_const_t *token)
Definition oscore_context.c:748
coap_remove_option
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
Definition coap_pdu.c:426
coap_add_option_internal
size_t coap_add_option_internal(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
Definition coap_pdu.c:701
COAP_OPTION_BLOCK2
#define COAP_OPTION_BLOCK2
Definition coap_pdu.h:133
coap_delete_pdu
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Definition coap_pdu.c:163
coap_mid_t
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:255
coap_request_t
coap_request_t
CoAP PDU Request methods.
Definition coap_pdu.h:74
COAP_RESPONSE_CODE
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:152
COAP_RESPONSE_CLASS
#define COAP_RESPONSE_CLASS(C)
Definition coap_pdu.h:155
COAP_OPTION_OSCORE
#define COAP_OPTION_OSCORE
Definition coap_pdu.h:122
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 coap_pdu.c:304
COAP_OPTION_Q_BLOCK2
#define COAP_OPTION_Q_BLOCK2
Definition coap_pdu.h:136
coap_get_data
int coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data)
Retrieves the length and data pointer of specified PDU.
Definition coap_pdu.c:797
coap_pdu_duplicate
coap_pdu_t * coap_pdu_duplicate(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options)
Duplicate an existing PDU.
Definition coap_pdu.c:179
coap_pdu_init
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition coap_pdu.c:97
COAP_INVALID_MID
#define COAP_INVALID_MID
Indicates an invalid message id.
Definition coap_pdu.h:258
COAP_OPTION_ETAG
#define COAP_OPTION_ETAG
Definition coap_pdu.h:117
COAP_OPTION_OBSERVE
#define COAP_OPTION_OBSERVE
Definition coap_pdu.h:119
coap_add_data
int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds given data to the pdu that is passed as first parameter.
Definition coap_pdu.c:766
COAP_REQUEST_PUT
@ COAP_REQUEST_PUT
Definition coap_pdu.h:77
COAP_PROTO_UDP
@ COAP_PROTO_UDP
Definition coap_pdu.h:306
COAP_MESSAGE_NON
@ COAP_MESSAGE_NON
Definition coap_pdu.h:66
COAP_MESSAGE_CON
@ COAP_MESSAGE_CON
Definition coap_pdu.h:65
COAP_NSTART
#define COAP_NSTART(s)
Definition coap_session_internal.h:392
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:608
COAP_PROTO_NOT_RELIABLE
#define COAP_PROTO_NOT_RELIABLE(p)
Definition coap_session.h:36
coap_session_release
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
Definition coap_session.c:354
coap_session_reference
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
Definition coap_session.c:348
coap_delete_bin_const
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
Definition coap_str.c:120
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 coap_str.c:61
coap_new_bin_const
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
Definition coap_str.c:110
coap_binary_equal
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
Definition coap_str.h:203
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 coap_str.c:51
coap_delete_string
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
Definition coap_str.c:46
coap_delete_observer
int coap_delete_observer(coap_resource_t *resource, coap_session_t *session, const coap_bin_const_t *token)
Removes any subscription for observer from resource and releases the allocated storage.
coap_find_observer
coap_subscription_t * coap_find_observer(coap_resource_t *resource, coap_session_t *session, const coap_bin_const_t *token)
Returns a subscription object for given peer.
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.
coap_check_notify
void coap_check_notify(coap_context_t *context)
Checks all known resources to see if they are dirty and then notifies subscribed observers.
coap_handle_failed_notify
void coap_handle_failed_notify(coap_context_t *context, coap_session_t *session, const coap_bin_const_t *token)
Handles a failed observe notify.
coap_subscription_init
void coap_subscription_init(coap_subscription_t *)
coap_add_observer
coap_subscription_t * coap_add_observer(coap_resource_t *resource, coap_session_t *session, const coap_bin_const_t *token, const coap_pdu_t *pdu)
Adds the specified peer as observer for resource.
coap_touch_observer
void coap_touch_observer(coap_context_t *context, coap_session_t *session, const coap_bin_const_t *token)
Flags that data is ready to be sent to observers.
coap_get_query
coap_string_t * coap_get_query(const coap_pdu_t *request)
Extract query string from request PDU according to escape rules in 6.5.8.
Definition coap_uri.c:718
COAP_UNUSED
#define COAP_UNUSED
Definition libcoap.h:68
coap_attr_t
Limits the number of subscribers for each resource that this server support.
Definition coap_resource_internal.h:42
coap_attr_t::value
coap_str_const_t * value
Value of the attribute (can be NULL)
Definition coap_resource_internal.h:45
coap_attr_t::name
coap_str_const_t * name
Name of the attribute.
Definition coap_resource_internal.h:44
coap_attr_t::flags
int flags
Definition coap_resource_internal.h:46
coap_bin_const_t
CoAP binary data definition with const data.
Definition coap_str.h:64
coap_bin_const_t::length
size_t length
length of binary data
Definition coap_str.h:65
coap_bin_const_t::s
const uint8_t * s
read-only binary data
Definition coap_str.h:66
coap_block_b_t
Structure of Block options with BERT support.
Definition coap_block.h:51
coap_block_b_t::aszx
unsigned int aszx
block size (0-7 including BERT
Definition coap_block.h:55
coap_block_b_t::m
unsigned int m
1 if more blocks follow, 0 otherwise
Definition coap_block.h:53
coap_block_b_t::szx
unsigned int szx
block size (0-6)
Definition coap_block.h:54
coap_cache_key_t
Definition coap_cache_internal.h:37
coap_cache_key_t::key
uint8_t key[32]
Definition coap_cache_internal.h:38
coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
Definition coap_net_internal.h:49
coap_context_t::observe_added
coap_observe_added_t observe_added
Called when there is a new observe subscription request.
Definition coap_net_internal.h:109
coap_context_t::resources
coap_resource_t * resources
hash table or list of known resources
Definition coap_net_internal.h:52
coap_context_t::dyn_resource_added
coap_dyn_resource_added_t dyn_resource_added
Callback to save dynamic resource when created.
Definition coap_net_internal.h:118
coap_context_t::release_userdata
coap_resource_release_userdata_handler_t release_userdata
function to release user_data when resource is deleted
Definition coap_net_internal.h:58
coap_context_t::observe_user_data
void * observe_user_data
App provided data for use in observe_added or observe_deleted.
Definition coap_net_internal.h:113
coap_context_t::track_observe_value
coap_track_observe_value_t track_observe_value
Callback to save observe value when updated.
Definition coap_net_internal.h:116
coap_context_t::observe_pending
uint8_t observe_pending
Observe response pending.
Definition coap_net_internal.h:185
coap_context_t::observe_save_freq
uint32_t observe_save_freq
How frequently to update observe value.
Definition coap_net_internal.h:115
coap_context_t::observe_no_clear
uint8_t observe_no_clear
Observe 4.04 not to be sent on deleting resource.
Definition coap_net_internal.h:186
coap_context_t::proxy_uri_resource
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
Definition coap_net_internal.h:56
coap_context_t::observe_deleted
coap_observe_deleted_t observe_deleted
Called when there is a observe subscription de-register request.
Definition coap_net_internal.h:111
coap_context_t::unknown_resource
coap_resource_t * unknown_resource
can be used for handling unknown resources
Definition coap_net_internal.h:54
coap_context_t::resource_deleted
coap_resource_deleted_t resource_deleted
Invoked when resource is deleted.
Definition coap_net_internal.h:120
coap_endpoint_t::bind_addr
coap_address_t bind_addr
local interface address
Definition coap_session_internal.h:226
coap_lg_xmit_t::last_all_sent
coap_tick_t last_all_sent
Last time all data sent or 0.
Definition coap_block_internal.h:133
coap_lg_xmit_t::last_obs
coap_tick_t last_obs
Last time used (Observe tracking) or 0.
Definition coap_block_internal.h:134
coap_pdu_t
structure for CoAP PDUs
Definition coap_pdu_internal.h:119
coap_pdu_t::token
uint8_t * token
first byte of token (or extended length bytes prefix), if any, or options
Definition coap_pdu_internal.h:139
coap_pdu_t::max_size
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
Definition coap_pdu_internal.h:137
coap_pdu_t::code
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
Definition coap_pdu_internal.h:121
coap_pdu_t::hdr_size
uint8_t hdr_size
actual size used for protocol-specific header (0 until header is encoded)
Definition coap_pdu_internal.h:126
coap_pdu_t::actual_token
coap_bin_const_t actual_token
Actual token in pdu.
Definition coap_pdu_internal.h:132
coap_pdu_t::mid
coap_mid_t mid
message id, if any, in regular host byte order
Definition coap_pdu_internal.h:123
coap_pdu_t::used_size
size_t used_size
used bytes of storage for token, options and payload
Definition coap_pdu_internal.h:135
coap_pdu_t::type
coap_pdu_type_t type
message type
Definition coap_pdu_internal.h:120
coap_resource_t
Abstraction of resource that can be attached to coap_context_t.
Definition coap_resource_internal.h:53
coap_resource_t::dirty
unsigned int dirty
set to 1 if resource has changed
Definition coap_resource_internal.h:54
coap_resource_t::partiallydirty
unsigned int partiallydirty
set to 1 if some subscribers have not yet been notified of the last change
Definition coap_resource_internal.h:55
coap_resource_t::subscribers
coap_subscription_t * subscribers
list of observers for this resource
Definition coap_resource_internal.h:74
coap_resource_t::user_data
void * user_data
This pointer is under user control.
Definition coap_resource_internal.h:110
coap_resource_t::proxy_name_list
coap_str_const_t ** proxy_name_list
Array valid names this host is known by (proxy support)
Definition coap_resource_internal.h:104
coap_resource_t::uri_path
coap_str_const_t * uri_path
Request URI Path for this resource.
Definition coap_resource_internal.h:81
coap_resource_t::observe
unsigned int observe
The next value for the Observe option.
Definition coap_resource_internal.h:89
coap_resource_t::context
coap_context_t * context
Pointer back to the context that 'owns' this resource.
Definition coap_resource_internal.h:94
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 coap_resource_internal.h:69
coap_resource_t::is_proxy_uri
unsigned int is_proxy_uri
resource created for proxy URI handler
Definition coap_resource_internal.h:60
coap_resource_t::is_unknown
unsigned int is_unknown
resource created for unknown handler
Definition coap_resource_internal.h:59
coap_resource_t::link_attr
coap_attr_t * link_attr
attributes to be included with the link format
Definition coap_resource_internal.h:73
coap_resource_t::observable
unsigned int observable
can be observed
Definition coap_resource_internal.h:57
coap_resource_t::proxy_name_count
size_t proxy_name_count
Count of valid names this host is known by (proxy support)
Definition coap_resource_internal.h:99
coap_resource_t::flags
int flags
zero or more COAP_RESOURCE_FLAGS_* or'd together
Definition coap_resource_internal.h:83
coap_session_t
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
Definition coap_session_internal.h:68
coap_session_t::lg_xmit
coap_lg_xmit_t * lg_xmit
list of large transmissions
Definition coap_session_internal.h:96
coap_session_t::endpoint
coap_endpoint_t * endpoint
session's endpoint
Definition coap_session_internal.h:84
coap_session_t::addr_info
coap_addr_tuple_t addr_info
remote/local address info
Definition coap_session_internal.h:79
coap_session_t::proto
coap_proto_t proto
protocol used
Definition coap_session_internal.h:69
coap_session_t::con_active
uint8_t con_active
Active CON request sent.
Definition coap_session_internal.h:90
coap_session_t::context
coap_context_t * context
session's context
Definition coap_session_internal.h:86
coap_str_const_t
CoAP string data definition with const data.
Definition coap_str.h:46
coap_str_const_t::s
const uint8_t * s
read-only string data
Definition coap_str.h:48
coap_str_const_t::length
size_t length
length of string
Definition coap_str.h:47
coap_string_t
CoAP string data definition.
Definition coap_str.h:38
coap_string_t::s
uint8_t * s
string data
Definition coap_str.h:40
coap_string_t::length
size_t length
length of string
Definition coap_str.h:39
coap_subscription_t
Number of notifications that may be sent non-confirmable before a confirmable message is sent to dete...
Definition coap_subscribe_internal.h:57
coap_subscription_t::dirty
uint8_t dirty
set if the notification temporarily could not be sent (in that case, the resource's partially dirty f...
Definition coap_subscribe_internal.h:63
coap_subscription_t::non_cnt
uint8_t non_cnt
up to 255 non-confirmable notifies allowed
Definition coap_subscribe_internal.h:61
coap_subscription_t::cache_key
coap_cache_key_t * cache_key
Definition coap_subscribe_internal.h:66
coap_subscription_t::session
struct coap_session_t * session
subscriber session
Definition coap_subscribe_internal.h:59
coap_subscription_t::fail_cnt
uint8_t fail_cnt
up to 255 confirmable notifies can fail
Definition coap_subscribe_internal.h:62
coap_subscription_t::pdu
coap_pdu_t * pdu
cache_key to identify requester
Definition coap_subscribe_internal.h:67
oscore_association_t
Definition oscore_context.h:142
oscore_association_t::partial_iv
coap_bin_const_t * partial_iv
Definition oscore_context.h:149
oscore_association_t::aad
coap_bin_const_t * aad
Definition oscore_context.h:147
oscore_association_t::nonce
coap_bin_const_t * nonce
Definition oscore_context.h:148