Go to the documentation of this file.
13 #if defined(WITH_LWIP)
19 #include <lwip/memp.h>
21 #define COAP_MALLOC_TYPE(Type) \
22 ((coap_##Type##_t *)memp_malloc(MEMP_COAP_##Type))
23 #define COAP_FREE_TYPE(Type, Object) memp_free(MEMP_COAP_##Type, Object)
25 #elif defined(WITH_CONTIKI)
28 #define COAP_MALLOC_TYPE(Type) \
29 ((coap_##Type##_t *)memb_alloc(&(Type##_storage)))
30 #define COAP_FREE_TYPE(Type, Object) memb_free(&(Type##_storage), (Object))
35 coap_resources_init() {
36 memb_init(&subscription_storage);
40 coap_malloc_subscription() {
41 return memb_alloc(&subscription_storage);
46 memb_free(&subscription_storage, subscription);
50 #define COAP_MALLOC_TYPE(Type) \
51 ((coap_##Type##_t *)coap_malloc(sizeof(coap_##Type##_t)))
52 #define COAP_FREE_TYPE(Type, Object) coap_free(Object)
55 #define COAP_PRINT_STATUS_MAX (~COAP_PRINT_STATUS_MASK)
58 #define min(a,b) ((a) < (b) ? (a) : (b))
69 #define PRINT_WITH_OFFSET(Buf,Offset,Char) \
70 if ((Offset) == 0) { \
71 (*(Buf)++) = (Char); \
79 #define PRINT_COND_WITH_OFFSET(Buf,Bufend,Offset,Char,Result) { \
80 if ((Buf) < (Bufend)) { \
81 PRINT_WITH_OFFSET(Buf,Offset,Char); \
91 #define COPY_COND_WITH_OFFSET(Buf,Bufend,Offset,Str,Length,Result) { \
93 for (i = 0; i < (Length); i++) { \
94 PRINT_COND_WITH_OFFSET((Buf), (Bufend), (Offset), (Str)[i], (Result)); \
102 assert(text); assert(pattern);
107 if (match_substring) {
108 const uint8_t *next_token = text->
s;
109 size_t remaining_length = text->
length;
110 while (remaining_length) {
112 const uint8_t *token = next_token;
113 next_token = (
unsigned char *)memchr(token,
' ', remaining_length);
116 token_length = next_token - token;
117 remaining_length -= (token_length + 1);
120 token_length = remaining_length;
121 remaining_length = 0;
124 if ((match_prefix || pattern->
length == token_length) &&
125 memcmp(token, pattern->
s, pattern->
length) == 0)
131 return (match_prefix || pattern->
length == text->
length) &&
132 memcmp(text->
s, pattern->
s, pattern->
length) == 0;
156 #if defined(__GNUC__) && defined(WITHOUT_QUERY_FILTER)
160 coap_opt_t *query_filter __attribute__ ((unused))) {
166 size_t output_length = 0;
167 unsigned char *p = buf;
168 const uint8_t *bufend = buf + *buflen;
169 size_t left, written = 0;
171 const size_t old_offset = offset;
172 int subsequent_resource = 0;
173 #ifndef WITHOUT_QUERY_FILTER
176 #define MATCH_URI 0x01
177 #define MATCH_PREFIX 0x02
178 #define MATCH_SUBSTRING 0x04
186 #ifndef WITHOUT_QUERY_FILTER
191 && resource_param.
s[resource_param.
length] !=
'=')
196 if (resource_param.
length == 4 &&
197 memcmp(resource_param.
s,
"href", 4) == 0)
200 for (rt_attributes = _rt_attributes; rt_attributes->
s; rt_attributes++) {
202 memcmp(resource_param.
s, rt_attributes->
s, rt_attributes->
length) == 0) {
213 query_pattern.length =
218 query_pattern.length--;
221 if (query_pattern.length &&
222 query_pattern.s[query_pattern.length-1] ==
'*') {
223 query_pattern.length--;
232 #ifndef WITHOUT_QUERY_FILTER
233 if (resource_param.
length) {
243 if (!attr || !attr->
value)
continue;
244 unquoted_val = *attr->
value;
245 if (attr->
value->
s[0] ==
'"') {
249 if (!(
match(&unquoted_val, &query_pattern,
257 if (!subsequent_resource) {
258 subsequent_resource = 1;
277 output_length = p - buf;
285 if (result + old_offset - offset < *buflen) {
309 else if (!uri_path) {
353 if (!resource || !name)
388 if (!resource || !name)
410 memp_free(MEMP_COAP_RESOURCEATTR, attr);
439 memp_free(MEMP_COAP_RESOURCE, resource);
459 "coap_add_resource: Duplicate uri_path '%*.*s', old resource deleted\n",
470 if (!context || !resource)
520 unsigned char *buf,
size_t *len,
size_t *offset) {
521 unsigned char *p = buf;
522 const uint8_t *bufend = buf + *len;
525 size_t output_length = 0;
526 const size_t old_offset = *offset;
556 output_length = p - buf;
564 if (result + old_offset - *offset < *len) {
573 unsigned char method,
577 resource->
handler[method-1] = handler;
580 #ifndef WITHOUT_OBSERVE
609 && ((!query && !s->
query)
666 if (token && token->
length) {
705 char outbuf[2 * 8 + 1] =
"";
708 snprintf( &outbuf[2 * i], 3,
"%02x", s->
token[i] );
774 "coap_check_notify: pdu init failed, resource stays "
775 "partially dirty\n");
783 "coap_check_notify: cannot add token, resource stays "
784 "partially dirty\n");
800 h(context, r, obs->
session, NULL, &token, obs->
query, response);
815 "coap_check_notify: sending failed, resource stays "
816 "partially dirty\n");
889 if ( obs->
session == session &&
902 #ifndef INET6_ADDRSTRLEN
903 #define INET6_ADDRSTRLEN 40
Coap string data definition with const data.
coap_address_t remote
remote address and port
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
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 (.
unsigned int observe
The next value for the Observe option.
#define COAP_OBS_MAX_NON
Number of notifications that may be sent non-confirmable before a confirmable message is sent to dete...
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.
unsigned int has_block2
GET request had Block2 definition.
unsigned int coap_print_status_t
Status word to encode the result of conditional print or copy operations such as coap_print_link().
struct coap_string_t * query
query string used for subscription, if any
void coap_delete_observers(coap_context_t *context, coap_session_t *session)
Removes any subscription for session and releases the allocated storage.
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.
unsigned int is_unknown
resource created for unknown handler
#define RESOURCES_DELETE(r, obj)
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
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_FREE_TYPE(Type, Object)
unsigned char token[8]
token used for subscription
#define HASH_ITER(hh, head, el, tmp)
uint8_t con_active
Active CON request sent.
uint16_t tid
transaction id, if any, in regular host byte order
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
coap_session_t * coap_session_reference(coap_session_t *session)
Increment reference counter on a session.
The CoAP stack's global state is stored in a coap_context_t object.
coap_tid_t coap_send(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
unsigned int non_cnt
up to 15 non-confirmable notifies allowed
void coap_handle_failed_notify(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
size_t length
length of string
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.
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
const uint8_t * s
string data
static const uint8_t coap_unknown_resource_uri[]
static int match(const coap_str_const_t *text, const coap_str_const_t *pattern, int match_prefix, int match_substring)
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.
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
#define COAP_RESOURCE_FLAGS_RELEASE_URI
The URI passed to coap_resource_init() is free'd by coap_delete_resource().
coap_attr_t * link_attr
attributes to be included with the link format
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_string_equal(string1, string2)
Compares the two strings for equality.
#define HASH_DELETE(hh, head, delptr)
coap_addr_tuple_t addr_info
key: remote/local address info
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...
struct coap_session_t * session
subscriber session
#define COAP_RESOURCE_FLAGS_NOTIFY_CON
Notifications will be sent confirmable by default.
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.
#define COAP_INVALID_TID
Indicates an invalid transaction id.
#define COAP_PRINT_STATUS_ERROR
unsigned int fail_cnt
up to 3 confirmable notifies can fail
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.
static coap_str_const_t null_path_value
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.
static void coap_notify_observers(coap_context_t *context, coap_resource_t *r)
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...
coap_method_handler_t handler[7]
Used to store handlers for the seven coap methods GET, POST, PUT, DELETE, FETCH, PATCH and IPATCH.
#define RESOURCES_ITER(r, tmp)
COAP_STATIC_INLINE uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
struct coap_resource_t * unknown_resource
can be used for handling unknown resources
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.
unsigned int partiallydirty
set to 1 if some subscribers have not yet been notified of the last change
#define COAP_MALLOC_TYPE(Type)
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 string data definition.
void coap_delete_attr(coap_attr_t *attr)
Deletes an attribute.
void coap_add_resource(coap_context_t *context, coap_resource_t *resource)
Registers the given resource for context.
static coap_subscription_t * coap_find_observer_query(coap_resource_t *resource, coap_session_t *session, const coap_string_t *query)
uint16_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
#define COAP_DEFAULT_NSTART
The number of simultaneous outstanding interactions that a client maintains to a given server.
coap_str_const_t * uri_path
Request URI Path for this resource.
#define COAP_PRINT_STATUS_TRUNC
#define LL_FOREACH_SAFE(head, el, tmp)
size_t length
length of string
size_t token_length
actual length of token
Coap binary data definition.
#define PRINT_COND_WITH_OFFSET(Buf, Bufend, Offset, Char, Result)
Adds Char to Buf if Offset is zero and Buf is less than Bufend.
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...
int coap_delete_resource(coap_context_t *context, coap_resource_t *resource)
Deletes a resource identified by resource.
#define COAP_PRINT_STATUS_MAX
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
#define LL_PREPEND(head, add)
coap_subscription_t * subscribers
list of observers for this resource
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
int coap_resource_set_dirty(coap_resource_t *r, const coap_string_t *query)
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
#define RESOURCES_FIND(r, k, res)
void coap_session_release(coap_session_t *session)
Decrement reference counter on a session.
unsigned int observable
can be observed
#define RESOURCES_ADD(r, obj)
#define COAP_OBS_MAX_FAIL
Number of confirmable notifications that may fail (i.e.
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.
#define COAP_ATTR_FLAGS_RELEASE_VALUE
#define coap_log(level,...)
Logging function.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
#define COAP_STATIC_INLINE
#define LL_FOREACH(head, el)
#define COAP_ATTR_FLAGS_RELEASE_NAME
Structure of Block options.
static void coap_free_resource(coap_resource_t *resource)
#define COPY_COND_WITH_OFFSET(Buf, Bufend, Offset, Str, Length, Result)
Copies at most Length characters of Str to Buf.
struct coap_resource_t * resources
hash table or list of known resources
#define LL_DELETE(head, del)
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_block_t block2
GET request Block2 definition.
void coap_subscription_init(coap_subscription_t *s)
void coap_delete_all_resources(coap_context_t *context)
Deletes all resources from given context and frees their storage.
coap_log_t coap_get_log_level(void)
Get the current logging level.
#define COAP_PRINT_OUTPUT_LENGTH(v)
static coap_str_const_t * null_path
size_t length
length of binary data
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.
uint16_t tid
transaction id, if any, in regular host byte order
unsigned int dirty
set if the notification temporarily could not be sent (in that case, the resource's partially dirty f...
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.
void coap_touch_observer(coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
Marks an observer as alive.
Pulls together all the internal only header files.
unsigned int dirty
set to 1 if resource has changed