16 #ifdef COAP_EPOLL_SUPPORT
17 #include <sys/epoll.h>
18 #include <sys/timerfd.h>
21 #if defined(WITH_LWIP)
27 #include <lwip/memp.h>
29 #define COAP_MALLOC_TYPE(Type) \
30 ((coap_##Type##_t *)memp_malloc(MEMP_COAP_##Type))
31 #define COAP_FREE_TYPE(Type, Object) memp_free(MEMP_COAP_##Type, Object)
33 #elif defined(WITH_CONTIKI)
36 #define COAP_MALLOC_TYPE(Type) \
37 ((coap_##Type##_t *)memb_alloc(&(Type##_storage)))
38 #define COAP_FREE_TYPE(Type, Object) memb_free(&(Type##_storage), (Object))
43 coap_resources_init() {
44 memb_init(&subscription_storage);
48 coap_malloc_subscription() {
49 return memb_alloc(&subscription_storage);
54 memb_free(&subscription_storage, subscription);
58 #define COAP_MALLOC_TYPE(Type) \
59 ((coap_##Type##_t *)coap_malloc(sizeof(coap_##Type##_t)))
60 #define COAP_FREE_TYPE(Type, Object) coap_free(Object)
63 #define COAP_PRINT_STATUS_MAX (~COAP_PRINT_STATUS_MASK)
66 #define min(a,b) ((a) < (b) ? (a) : (b))
77 #define PRINT_WITH_OFFSET(Buf,Offset,Char) \
78 if ((Offset) == 0) { \
79 (*(Buf)++) = (Char); \
87 #define PRINT_COND_WITH_OFFSET(Buf,Bufend,Offset,Char,Result) { \
88 if ((Buf) < (Bufend)) { \
89 PRINT_WITH_OFFSET(Buf,Offset,Char); \
99 #define COPY_COND_WITH_OFFSET(Buf,Bufend,Offset,Str,Length,Result) { \
101 for (i = 0; i < (Length); i++) { \
102 PRINT_COND_WITH_OFFSET((Buf), (Bufend), (Offset), (Str)[i], (Result)); \
110 assert(text); assert(pattern);
115 if (match_substring) {
116 const uint8_t *next_token = text->
s;
117 size_t remaining_length = text->
length;
118 while (remaining_length) {
120 const uint8_t *token = next_token;
121 next_token = (
unsigned char *)memchr(token,
' ', remaining_length);
124 token_length = next_token - token;
125 remaining_length -= (token_length + 1);
128 token_length = remaining_length;
129 remaining_length = 0;
132 if ((match_prefix || pattern->
length == token_length) &&
133 memcmp(token, pattern->
s, pattern->
length) == 0)
139 return (match_prefix || pattern->
length == text->
length) &&
140 memcmp(text->
s, pattern->
s, pattern->
length) == 0;
164 #if defined(__GNUC__) && defined(WITHOUT_QUERY_FILTER)
174 size_t output_length = 0;
175 unsigned char *p = buf;
176 const uint8_t *bufend = buf + *buflen;
177 size_t left, written = 0;
179 const size_t old_offset = offset;
180 int subsequent_resource = 0;
181 #ifndef WITHOUT_QUERY_FILTER
184 #define MATCH_URI 0x01
185 #define MATCH_PREFIX 0x02
186 #define MATCH_SUBSTRING 0x04
188 {2, (
const uint8_t *)
"rt"},
189 {2, (
const uint8_t *)
"if"},
190 {3, (
const uint8_t *)
"rel"},
194 #ifndef WITHOUT_QUERY_FILTER
199 && resource_param.
s[resource_param.
length] !=
'=')
204 if (resource_param.
length == 4 &&
205 memcmp(resource_param.
s,
"href", 4) == 0)
208 for (rt_attributes = _rt_attributes; rt_attributes->
s; rt_attributes++) {
210 memcmp(resource_param.
s, rt_attributes->
s, rt_attributes->
length) == 0) {
221 query_pattern.length =
226 query_pattern.length--;
229 if (query_pattern.length &&
230 query_pattern.s[query_pattern.length-1] ==
'*') {
231 query_pattern.length--;
240 #ifndef WITHOUT_QUERY_FILTER
241 if (resource_param.
length) {
251 if (!attr || !attr->
value)
continue;
252 unquoted_val = *attr->
value;
253 if (attr->
value->
s[0] ==
'"') {
257 if (!(
match(&unquoted_val, &query_pattern,
265 if (!subsequent_resource) {
266 subsequent_resource = 1;
285 output_length = p - buf;
293 if (result + old_offset - offset < *buflen) {
317 else if (!uri_path) {
359 size_t host_name_count,
const char *host_name_list[]) {
362 if (host_name_count == 0) {
364 "coap_resource_proxy_uri_init: Must have one or more host names defined\n");
375 for (i = 0; i < (
sizeof(r->
handler) /
sizeof(r->
handler[0])); i++) {
378 if (host_name_count) {
382 for (i = 0; i < host_name_count; i++) {
385 strlen(host_name_list[i]));
388 "coap_resource_proxy_uri_init: unable to add host name\n");
413 if (!resource || !name)
448 if (!resource || !name)
477 memp_free(MEMP_COAP_RESOURCEATTR, attr);
512 LL_FOREACH_SAFE( resource->
subscribers, obs, otmp ) {
528 memp_free(MEMP_COAP_RESOURCE, resource);
553 "coap_add_resource: Duplicate uri_path '%*.*s', old resource deleted\n",
560 assert(resource->
context == NULL);
566 if (!context || !resource)
597 HASH_ITER(hh, context->
resources, res, rtmp) {
598 HASH_DELETE(hh, context->
resources, res);
625 unsigned char *buf,
size_t *len,
size_t *offset) {
626 unsigned char *p = buf;
627 const uint8_t *bufend = buf + *len;
630 size_t output_length = 0;
631 const size_t old_offset = *offset;
661 output_length = p - buf;
669 if (result + old_offset - *offset < *len) {
682 resource->
handler[method-1] = handler;
744 cache_ignore_options,
745 sizeof(cache_ignore_options)/
sizeof(cache_ignore_options[0]));
770 request->
token, NULL);
771 if (s->
pdu == NULL) {
779 if (cache_key == NULL) {
782 cache_ignore_options,
783 sizeof(cache_ignore_options)/
sizeof(cache_ignore_options[0]));
784 if (cache_key == NULL) {
823 char outbuf[2 * 8 + 1] =
"";
826 snprintf( &outbuf[2 * i], 3,
"%02x", s->
pdu->
token[i] );
845 LL_FOREACH_SAFE(resource->subscribers, s, tmp) {
847 LL_DELETE(resource->subscribers, s);
898 "coap_check_notify: pdu init failed, resource stays "
899 "partially dirty\n");
908 "coap_check_notify: cannot add token, resource stays "
909 "partially dirty\n");
946 h(r, obs->
session, obs->
pdu, query, response);
975 "coap_check_notify: sending failed, resource stays "
976 "partially dirty\n");
1006 #ifdef COAP_EPOLL_SUPPORT
1007 if (r->
context->eptimerfd != -1) {
1009 struct itimerspec new_value;
1012 memset(&new_value, 0,
sizeof(new_value));
1013 new_value.it_value.tv_nsec = 1;
1014 ret = timerfd_settime(r->
context->eptimerfd, 0, &new_value, NULL);
1017 "%s: timerfd_settime failed: %s (%d)\n",
1018 "coap_resource_notify_observers",
1089 LL_FOREACH_SAFE(resource->
subscribers, obs, otmp) {
1090 if ( obs->
session == session &&
1103 #ifndef INET6_ADDRSTRLEN
1104 #define INET6_ADDRSTRLEN 40
Pulls together all the internal only header files.
const char * coap_socket_strerror(void)
void coap_check_code_lg_xmit(coap_session_t *session, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *query)
The function checks that the code in a newly formed lg_xmit created by coap_add_data_large_response()...
int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block)
Initializes block from pdu.
void coap_delete_cache_key(coap_cache_key_t *cache_key)
Delete the cache-key.
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 *cache_ignore_options, size_t cache_ignore_count)
Calculates a cache-key for the given CoAP PDU.
@ COAP_CACHE_IS_SESSION_BASED
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server.
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.
#define RESOURCES_ADD(r, obj)
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
#define RESOURCES_FIND(r, k, res)
#define RESOURCES_DELETE(r, obj)
#define RESOURCES_ITER(r, tmp)
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.
#define COAP_RESOURCE_FLAGS_NOTIFY_NON
Notifications will be sent non-confirmable by default.
#define COAP_ATTR_FLAGS_RELEASE_VALUE
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.
#define COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS
Notifications will always be sent non-confirmable.
#define COAP_ATTR_FLAGS_RELEASE_NAME
void coap_resource_set_mode(coap_resource_t *resource, int mode)
Sets the notification message type of resource resource to given mode.
coap_str_const_t * coap_resource_get_uri_path(coap_resource_t *resource)
Get the uri_path from a resource.
coap_str_const_t * coap_attr_get_value(coap_attr_t *attr)
Returns attribute's value.
#define COAP_PRINT_STATUS_TRUNC
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_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.
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Notifications will be sent confirmable.
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_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_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.
unsigned int coap_print_status_t
Status word to encode the result of conditional print or copy operations such as coap_print_link().
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.
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.
void(* coap_resource_release_userdata_handler_t)(void *user_data)
Definition of release resource user_data callback function.
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
void coap_resource_set_userdata(coap_resource_t *resource, void *data)
Sets the user_data.
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.
#define COAP_PRINT_STATUS_ERROR
#define COAP_PRINT_OUTPUT_LENGTH(v)
int coap_delete_resource(coap_context_t *context, coap_resource_t *resource)
Deletes a resource identified by resource.
void * coap_resource_get_userdata(coap_resource_t *resource)
Gets the user_data.
#define COAP_RESOURCE_FLAGS_RELEASE_URI
The URI passed to coap_resource_init() is free'd by coap_delete_resource().
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
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.
uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
coap_log_t coap_get_log_level(void)
Get the current logging level.
size_t coap_print_addr(const coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
#define coap_log(level,...)
Logging function.
void coap_resource_set_get_observable(coap_resource_t *resource, int mode)
Set whether a resource is observable.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
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.
#define COAP_OPTION_BLOCK2
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
coap_request_t
CoAP PDU Request methods.
#define COAP_RESPONSE_CODE(N)
#define COAP_RESPONSE_CLASS(C)
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
size_t coap_add_option(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.
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.
#define COAP_INVALID_MID
Indicates an invalid message id.
#define COAP_OPTION_OBSERVE
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.
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.
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
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...
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
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.
coap_subscription_t * coap_add_observer(coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token, const coap_pdu_t *request)
Adds the specified peer as observer for resource.
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.
void coap_delete_observers(coap_context_t *context, coap_session_t *session)
Removes any subscription for session and releases the allocated storage.
void coap_check_notify(coap_context_t *context)
Checks all known resources to see if they are dirty and then notifies subscribed observers.
void coap_handle_failed_notify(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Handles a failed observe notify.
#define COAP_OBS_MAX_NON
Number of notifications that may be sent non-confirmable before a confirmable message is sent to dete...
void coap_subscription_init(coap_subscription_t *s)
#define COAP_OBS_MAX_FAIL
Number of confirmable notifications that may fail (i.e.
void coap_touch_observer(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Flags that data is ready to be sent to observers.
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.
#define COAP_STATIC_INLINE
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_STATIC_INLINE void * coap_malloc(size_t size)
Wrapper function to coap_malloc_type() for backwards compatibility.
COAP_STATIC_INLINE void coap_free(void *object)
Wrapper function to coap_free_type() for backwards compatibility.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
static const uint8_t coap_unknown_resource_uri[]
static const uint8_t coap_proxy_resource_uri[]
static void coap_free_resource(coap_resource_t *resource)
#define PRINT_COND_WITH_OFFSET(Buf, Bufend, Offset, Char, Result)
Adds Char to Buf if Offset is zero and Buf is less than Bufend.
static coap_str_const_t null_path_value
static coap_subscription_t * coap_find_observer_cache_key(coap_resource_t *resource, coap_session_t *session, const coap_cache_key_t *cache_key)
int coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query)
int coap_resource_notify_observers(coap_resource_t *r, const coap_string_t *query COAP_UNUSED)
static int match(const coap_str_const_t *text, const coap_str_const_t *pattern, int match_prefix, int match_substring)
@ COAP_NOT_DELETING_RESOURCE
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...
#define COAP_PRINT_STATUS_MAX
#define COAP_MALLOC_TYPE(Type)
static coap_str_const_t * null_path
#define COAP_FREE_TYPE(Type, Object)
static void coap_notify_observers(coap_context_t *context, coap_resource_t *r, coap_deleting_resource_t deleting)
#define COPY_COND_WITH_OFFSET(Buf, Bufend, Offset, Str, Length, Result)
Copies at most Length characters of Str to Buf.
coap_address_t remote
remote address and port
Abstraction of attribute associated with a resource.
coap_str_const_t * value
Value of the attribute (can be NULL)
coap_str_const_t * name
Name of the attribute.
CoAP binary data definition.
size_t length
length of binary data
Structure of Block options.
unsigned int szx
block size
The CoAP stack's global state is stored in a coap_context_t object.
coap_resource_t * resources
hash table or list of known resources
coap_resource_release_userdata_handler_t release_userdata
function to release user_data when resource is deleted
uint8_t observe_pending
Observe response pending.
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
coap_resource_t * unknown_resource
can be used for handling unknown resources
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
uint8_t * token
first byte of token, if any, or options
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
uint8_t token_length
length of Token
coap_mid_t mid
message id, if any, in regular host byte order
coap_pdu_type_t type
message type
Abstraction of resource that can be attached to coap_context_t.
unsigned int dirty
set to 1 if resource has changed
unsigned int partiallydirty
set to 1 if some subscribers have not yet been notified of the last change
coap_subscription_t * subscribers
list of observers for this resource
void * user_data
This pointer is under user control.
coap_str_const_t ** proxy_name_list
Array valid names this host is known by (proxy support)
coap_str_const_t * uri_path
Request URI Path for this resource.
unsigned int observe
The next value for the Observe option.
coap_context_t * context
Pointer back to the context that 'owns' this resource.
coap_method_handler_t handler[7]
Used to store handlers for the seven coap methods GET, POST, PUT, DELETE, FETCH, PATCH and IPATCH.
unsigned int is_proxy_uri
resource created for proxy URI handler
unsigned int is_unknown
resource created for unknown handler
coap_attr_t * link_attr
attributes to be included with the link format
unsigned int observable
can be observed
size_t proxy_name_count
Count of valid names this host is known by (proxy support)
int flags
zero or more COAP_RESOURCE_FLAGS_* or'd together
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_addr_tuple_t addr_info
key: remote/local address info
uint8_t con_active
Active CON request sent.
CoAP string data definition with const data.
const uint8_t * s
read-only string data
size_t length
length of string
CoAP string data definition.
unsigned int fail_cnt
up to 3 confirmable notifies can fail
unsigned int non_cnt
up to 15 non-confirmable notifies allowed
coap_cache_key_t * cache_key
struct coap_session_t * session
subscriber session
unsigned int dirty
set if the notification temporarily could not be sent (in that case, the resource's partially dirty f...
coap_pdu_t * pdu
cache_key to identify requester