10 #include "coap_config.h" 19 #if defined(WITH_LWIP) 25 #include <lwip/memp.h> 27 #define COAP_MALLOC_TYPE(Type) \ 28 ((coap_##Type##_t *)memp_malloc(MEMP_COAP_##Type)) 29 #define COAP_FREE_TYPE(Type, Object) memp_free(MEMP_COAP_##Type, Object) 31 #elif defined(WITH_CONTIKI) 34 #define COAP_MALLOC_TYPE(Type) \ 35 ((coap_##Type##_t *)memb_alloc(&(Type##_storage))) 36 #define COAP_FREE_TYPE(Type, Object) memb_free(&(Type##_storage), (Object)) 41 coap_resources_init() {
42 memb_init(&subscription_storage);
46 coap_malloc_subscription() {
47 return memb_alloc(&subscription_storage);
52 memb_free(&subscription_storage, subscription);
56 #define COAP_MALLOC_TYPE(Type) \ 57 ((coap_##Type##_t *)coap_malloc(sizeof(coap_##Type##_t))) 58 #define COAP_FREE_TYPE(Type, Object) coap_free(Object) 61 #define COAP_PRINT_STATUS_MAX (~COAP_PRINT_STATUS_MASK) 64 #define min(a,b) ((a) < (b) ? (a) : (b)) 75 #define PRINT_WITH_OFFSET(Buf,Offset,Char) \ 76 if ((Offset) == 0) { \ 77 (*(Buf)++) = (Char); \ 85 #define PRINT_COND_WITH_OFFSET(Buf,Bufend,Offset,Char,Result) { \ 86 if ((Buf) < (Bufend)) { \ 87 PRINT_WITH_OFFSET(Buf,Offset,Char); \ 97 #define COPY_COND_WITH_OFFSET(Buf,Bufend,Offset,Str,Length,Result) { \ 99 for (i = 0; i < (Length); i++) { \ 100 PRINT_COND_WITH_OFFSET((Buf), (Bufend), (Offset), (Str)[i], (Result)); \ 113 if (match_substring) {
114 const uint8_t *next_token = text->
s;
115 size_t remaining_length = text->
length;
116 while (remaining_length) {
118 const uint8_t *token = next_token;
119 next_token = (
unsigned char *)memchr(token,
' ', remaining_length);
122 token_length = next_token - token;
123 remaining_length -= (token_length + 1);
126 token_length = remaining_length;
127 remaining_length = 0;
130 if ((match_prefix || pattern->
length == token_length) &&
131 memcmp(token, pattern->
s, pattern->
length) == 0)
137 return (match_prefix || pattern->
length == text->
length) &&
138 memcmp(text->
s, pattern->
s, pattern->
length) == 0;
162 #if defined(__GNUC__) && defined(WITHOUT_QUERY_FILTER) 166 coap_opt_t *query_filter __attribute__ ((unused))) {
172 size_t output_length = 0;
173 unsigned char *p = buf;
174 const uint8_t *bufend = buf + *buflen;
175 size_t left, written = 0;
177 const size_t old_offset = offset;
178 int subsequent_resource = 0;
179 #ifndef WITHOUT_QUERY_FILTER 182 #define MATCH_URI 0x01 183 #define MATCH_PREFIX 0x02 184 #define MATCH_SUBSTRING 0x04 192 #ifndef WITHOUT_QUERY_FILTER 197 && resource_param.
s[resource_param.
length] !=
'=')
202 if (resource_param.
length == 4 &&
203 memcmp(resource_param.
s,
"href", 4) == 0)
206 for (rt_attributes = _rt_attributes; rt_attributes->
s; rt_attributes++) {
208 memcmp(resource_param.
s, rt_attributes->
s, rt_attributes->
length) == 0) {
219 query_pattern.length =
224 query_pattern.length--;
227 if (query_pattern.length &&
228 query_pattern.s[query_pattern.length-1] ==
'*') {
229 query_pattern.length--;
238 #ifndef WITHOUT_QUERY_FILTER 239 if (resource_param.
length) {
249 if (!attr || !attr->
value)
continue;
250 unquoted_val = *attr->
value;
251 if (attr->
value->
s[0] ==
'"') {
255 if (!(
match(&unquoted_val, &query_pattern,
263 if (!subsequent_resource) {
264 subsequent_resource = 1;
283 output_length = p - buf;
291 if (result + old_offset - offset < *buflen) {
314 else if (!uri_path) {
358 if (!resource || !name)
393 if (!resource || !name)
415 memp_free(MEMP_COAP_RESOURCEATTR, attr);
444 memp_free(MEMP_COAP_RESOURCE, resource);
464 "coap_add_resource: Duplicate uri_path '%*.*s', old resource deleted\n",
475 if (!context || !resource)
525 unsigned char *buf,
size_t *len,
size_t *offset) {
526 unsigned char *p = buf;
527 const uint8_t *bufend = buf + *len;
530 size_t output_length = 0;
531 const size_t old_offset = *offset;
561 output_length = p - buf;
569 if (result + old_offset - *offset < *len) {
578 unsigned char method,
582 resource->
handler[method-1] = handler;
585 #ifndef WITHOUT_OBSERVE 614 && ((!query && !s->
query)
671 if (token && token->
length) {
710 char outbuf[2 * 8 + 1] =
"";
713 snprintf( &outbuf[2 * i], 3,
"%02x", s->
token[i] );
779 "coap_check_notify: pdu init failed, resource stays " 780 "partially dirty\n");
788 "coap_check_notify: cannot add token, resource stays " 789 "partially dirty\n");
805 h(context, r, obs->
session, NULL, &token, obs->
query, response);
820 "coap_check_notify: sending failed, resource stays " 821 "partially dirty\n");
894 if ( obs->
session == session &&
908 #ifndef INET6_ADDRSTRLEN 909 #define INET6_ADDRSTRLEN 40
#define LL_FOREACH(head, el)
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
uint8_t con_active
Active CON request sent.
#define RESOURCES_DELETE(r, obj)
static coap_subscription_t * coap_find_observer_query(coap_resource_t *resource, coap_session_t *session, const coap_string_t *query)
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
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_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_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_OUTPUT_LENGTH(v)
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
#define LL_PREPEND(head, add)
#define HASH_ITER(hh, head, el, tmp)
void coap_touch_observer(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Marks an observer as alive.
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
Print the address into the defined buffer.
coap_tid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
coap_attr_t * link_attr
attributes to be included with the link format
void coap_delete_observers(coap_context_t *context, coap_session_t *session)
Removes any subscription for session and releases the allocated storage.
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...
#define COAP_ATTR_FLAGS_RELEASE_NAME
unsigned int partiallydirty
set to 1 if some subscribers have not yet been notified of the last change
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.
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
unsigned int is_unknown
resource created for unknown handler
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
size_t length
length of string
#define COAP_OBS_MAX_FAIL
Number of confirmable notifications that may fail (i.e.
COAP_STATIC_INLINE uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
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.
Coap string data definition.
int coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query)
Coap string data definition with const data.
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
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 HASH_DELETE(hh, head, delptr)
#define COAP_INVALID_TID
Indicates an invalid transaction id.
size_t token_length
actual length of token
coap_address_t remote_addr
remote address and port
#define RESOURCES_ITER(r, tmp)
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
uint16_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
unsigned int dirty
set to 1 if resource has changed
#define COAP_MALLOC_TYPE(Type)
#define COAP_ATTR_FLAGS_RELEASE_VALUE
unsigned int observable
can be observed
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
unsigned int non_cnt
up to 15 non-confirmable notifies allowed
#define COAP_PRINT_STATUS_MAX
#define RESOURCES_ADD(r, obj)
Coap binary data definition.
Generic resource handling.
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server...
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
static void coap_notify_observers(coap_context_t *context, coap_resource_t *r)
coap_block_t block2
GET request Block2 definition.
#define coap_make_str_const(string)
Take the specified byte array (text) and create a coap_str_const_t *.
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
coap_subscription_t * subscribers
list of observers for this resource
static coap_str_const_t * null_path
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.
#define COAP_STATIC_INLINE
size_t length
length of string
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Notifications will be sent confirmable by default.
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
Structure of Block options.
const uint8_t * s
string data
unsigned int dirty
set if the notification temporarily could not be sent (in that case, the resource's partially dirty f...
#define RESOURCES_FIND(r, k, res)
coap_session_t * session
subscriber session
unsigned int has_block2
GET request had Block2 definition.
struct coap_resource_t * unknown_resource
can be used for handling unknown resources
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
#define LL_FOREACH_SAFE(head, el, tmp)
#define PRINT_COND_WITH_OFFSET(Buf, Bufend, Offset, Char, Result)
Adds Char to Buf if Offset is zero and Buf is less than Bufend.
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.
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
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.
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.
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_string_t * query
query string used for subscription, if any
#define LL_DELETE(head, del)
#define COAP_PRINT_STATUS_ERROR
coap_log_t coap_get_log_level(void)
Get the current logging level.
unsigned int coap_print_status_t
Status word to encode the result of conditional print or copy operations such as coap_print_link().
static int match(const coap_str_const_t *text, const coap_str_const_t *pattern, int match_prefix, int match_substring)
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.
#define COPY_COND_WITH_OFFSET(Buf, Bufend, Offset, Str, Length, Result)
Copies at most Length characters of Str to Buf.
unsigned int fail_cnt
up to 3 confirmable notifies can fail
#define COAP_FREE_TYPE(Type, Object)
#define coap_log(level,...)
Logging function.
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.
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...
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...
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_handle_failed_notify(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
unsigned char token[8]
token used for subscription
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
coap_str_const_t * uri_path
Request URI Path for this resource.
size_t length
length of binary data
#define COAP_PRINT_STATUS_TRUNC
void coap_subscription_init(coap_subscription_t *s)
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 (.
#define COAP_RESOURCE_FLAGS_RELEASE_URI
The URI passed to coap_resource_init() is free'd by coap_delete_resource().
unsigned int observe
The next value for the Observe option.
static const uint8_t coap_unknown_resource_uri[]
coap_method_handler_t handler[7]
Used to store handlers for the seven coap methods GET, POST, PUT, DELETE, FETCH, PATCH and IPATCH...
int coap_delete_resource(coap_context_t *context, coap_resource_t *resource)
Deletes a resource identified by resource.
The CoAP stack's global state is stored in a coap_context_t object.
uint16_t tid
transaction id, if any, in regular host byte order
static void coap_free_resource(coap_resource_t *resource)
#define COAP_OBS_MAX_NON
Number of notifications that may be sent non-confirmable before a confirmable message is sent to dete...
struct coap_resource_t * resources
hash table or list of known resources