libcoap  4.3.0beta
resource.c File Reference
#include "coap_internal.h"
#include <stdio.h>
#include <errno.h>
+ Include dependency graph for resource.c:

Go to the source code of this file.

Macros

#define COAP_MALLOC_TYPE(Type)    ((coap_##Type##_t *)coap_malloc(sizeof(coap_##Type##_t)))
 
#define COAP_FREE_TYPE(Type, Object)   coap_free(Object)
 
#define COAP_PRINT_STATUS_MAX   (~COAP_PRINT_STATUS_MASK)
 
#define min(a, b)   ((a) < (b) ? (a) : (b))
 
#define PRINT_WITH_OFFSET(Buf, Offset, Char)
 Adds Char to Buf if Offset is zero. More...
 
#define PRINT_COND_WITH_OFFSET(Buf, Bufend, Offset, Char, Result)
 Adds Char to Buf if Offset is zero and Buf is less than Bufend. More...
 
#define COPY_COND_WITH_OFFSET(Buf, Bufend, Offset, Str, Length, Result)
 Copies at most Length characters of Str to Buf. More...
 
#define MATCH_URI   0x01
 
#define MATCH_PREFIX   0x02
 
#define MATCH_SUBSTRING   0x04
 
#define INET6_ADDRSTRLEN   40
 

Typedefs

typedef enum coap_deleting_resource_t coap_deleting_resource_t
 

Enumerations

enum  coap_deleting_resource_t { COAP_DELETING_RESOURCE , COAP_NOT_DELETING_RESOURCE }
 

Functions

static int match (const coap_str_const_t *text, const coap_str_const_t *pattern, int match_prefix, int match_substring)
 
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. More...
 
coap_resource_tcoap_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. More...
 
coap_resource_tcoap_resource_unknown_init (coap_method_handler_t put_handler)
 Creates a new resource object for the unknown resource handler with support for PUT. More...
 
coap_resource_tcoap_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. More...
 
coap_attr_tcoap_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. More...
 
coap_attr_tcoap_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. More...
 
void coap_delete_attr (coap_attr_t *attr)
 Deletes an attribute. More...
 
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)
 
void coap_add_resource (coap_context_t *context, coap_resource_t *resource)
 Registers the given resource for context. More...
 
int coap_delete_resource (coap_context_t *context, coap_resource_t *resource)
 Deletes a resource identified by resource. More...
 
void coap_delete_all_resources (coap_context_t *context)
 Deletes all resources from given context and frees their storage. More...
 
coap_resource_tcoap_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. More...
 
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. More...
 
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. More...
 
coap_subscription_tcoap_find_observer (coap_resource_t *resource, coap_session_t *session, const coap_binary_t *token)
 Returns a subscription object for given peer. More...
 
static coap_subscription_tcoap_find_observer_query (coap_resource_t *resource, coap_session_t *session, const coap_string_t *query)
 
coap_subscription_tcoap_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, uint8_t code)
 Adds the specified peer as observer for resource. More...
 
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. More...
 
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. More...
 
void coap_delete_observers (coap_context_t *context, coap_session_t *session)
 Removes any subscription for session and releases the allocated storage. More...
 
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)
 Initiate the sending of an Observe packet for all observers of resource, optionally matching query if not NULL. More...
 
void coap_check_notify (coap_context_t *context)
 Checks for all known resources, if they are dirty and notifies subscribed observers. More...
 
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 given resource when COAP_OBS_MAX_FAIL is reached. More...
 
void coap_handle_failed_notify (coap_context_t *context, coap_session_t *session, const coap_binary_t *token)
 

Variables

static coap_str_const_t null_path_value = {0, (const uint8_t*)""}
 
static coap_str_const_tnull_path = &null_path_value
 
static const uint8_t coap_unknown_resource_uri []
 
static const uint8_t coap_proxy_resource_uri []
 

Macro Definition Documentation

◆ COAP_FREE_TYPE

#define COAP_FREE_TYPE (   Type,
  Object 
)    coap_free(Object)

Definition at line 58 of file resource.c.

◆ COAP_MALLOC_TYPE

#define COAP_MALLOC_TYPE (   Type)     ((coap_##Type##_t *)coap_malloc(sizeof(coap_##Type##_t)))

Definition at line 56 of file resource.c.

◆ COAP_PRINT_STATUS_MAX

#define COAP_PRINT_STATUS_MAX   (~COAP_PRINT_STATUS_MASK)

Definition at line 61 of file resource.c.

◆ COPY_COND_WITH_OFFSET

#define COPY_COND_WITH_OFFSET (   Buf,
  Bufend,
  Offset,
  Str,
  Length,
  Result 
)
Value:
{ \
size_t i; \
for (i = 0; i < (Length); i++) { \
PRINT_COND_WITH_OFFSET((Buf), (Bufend), (Offset), (Str)[i], (Result)); \
} \
}

Copies at most Length characters of Str to Buf.

The first Offset characters are skipped. Output may be truncated to Bufend - Buf characters.

Definition at line 97 of file resource.c.

◆ INET6_ADDRSTRLEN

#define INET6_ADDRSTRLEN   40

◆ MATCH_PREFIX

#define MATCH_PREFIX   0x02

◆ MATCH_SUBSTRING

#define MATCH_SUBSTRING   0x04

◆ MATCH_URI

#define MATCH_URI   0x01

◆ min

#define min (   a,
 
)    ((a) < (b) ? (a) : (b))

Definition at line 64 of file resource.c.

◆ PRINT_COND_WITH_OFFSET

#define PRINT_COND_WITH_OFFSET (   Buf,
  Bufend,
  Offset,
  Char,
  Result 
)
Value:
{ \
if ((Buf) < (Bufend)) { \
PRINT_WITH_OFFSET(Buf,Offset,Char); \
} \
(Result)++; \
}

Adds Char to Buf if Offset is zero and Buf is less than Bufend.

Definition at line 85 of file resource.c.

◆ PRINT_WITH_OFFSET

#define PRINT_WITH_OFFSET (   Buf,
  Offset,
  Char 
)
Value:
if ((Offset) == 0) { \
(*(Buf)++) = (Char); \
} else { \
(Offset)--; \
} \

Adds Char to Buf if Offset is zero.

Otherwise, Char is not written and Offset is decremented.

Definition at line 75 of file resource.c.

Typedef Documentation

◆ coap_deleting_resource_t

Enumeration Type Documentation

◆ coap_deleting_resource_t

Enumerator
COAP_DELETING_RESOURCE 
COAP_NOT_DELETING_RESOURCE 

Definition at line 475 of file resource.c.

Function Documentation

◆ coap_check_notify()

void coap_check_notify ( coap_context_t context)

Checks for all known resources, if they are dirty and notifies subscribed observers.

Definition at line 999 of file resource.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_find_observer_query()

static coap_subscription_t* coap_find_observer_query ( coap_resource_t resource,
coap_session_t session,
const coap_string_t query 
)
static

Definition at line 693 of file resource.c.

+ Here is the caller graph for this function:

◆ coap_free_resource()

static void coap_free_resource ( coap_resource_t resource)
static

Definition at line 484 of file resource.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ 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.

If no resource was found, this function returns NULL.

Parameters
contextThe context to look for this resource.
uri_pathThe unique string uri of the resource.
Returns
A pointer to the resource or NULL if not found.

Definition at line 603 of file resource.c.

+ Here is the caller graph for this function:

◆ coap_handle_failed_notify()

void coap_handle_failed_notify ( coap_context_t context,
coap_session_t session,
const coap_binary_t token 
)

Definition at line 1062 of file resource.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_notify_observers()

static void coap_notify_observers ( coap_context_t context,
coap_resource_t r,
coap_deleting_resource_t  deleting 
)
static

Definition at line 837 of file resource.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_print_wellknown()

coap_print_status_t coap_print_wellknown ( coap_context_t context,
unsigned char *  buf,
size_t *  buflen,
size_t  offset,
coap_opt_t query_filter 
)

Prints the names of all known resources to buf.

This function sets buflen to the number of bytes actually written and returns 1 on succes. On error, the value in buflen is undefined and the return value will be 0.

Parameters
contextThe context with the resource map.
bufThe buffer to write the result.
buflenMust be initialized to the maximum length of buf and will be set to the length of the well-known response on return.
offsetThe offset in bytes where the output shall start and is shifted accordingly with the characters that have been processed. This parameter is used to support the block option.
query_filterA filter query according to Link Format
Returns
COAP_PRINT_STATUS_ERROR on error. Otherwise, the lower 28 bits are set to the number of bytes that have actually been written to buf. COAP_PRINT_STATUS_TRUNC is set when the output has been truncated.

Definition at line 169 of file resource.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_remove_failed_observers()

static void coap_remove_failed_observers ( coap_context_t context,
coap_resource_t resource,
coap_session_t session,
const coap_binary_t token 
)
static

Checks the failure counter for (peer, token) and removes peer from the list of observers for the given resource when COAP_OBS_MAX_FAIL is reached.

Parameters
contextThe CoAP context to use
resourceThe resource to check for (peer, token)
sessionThe observer's session
tokenThe token that has been used for subscription.

Definition at line 1020 of file resource.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ coap_resource_set_dirty()

int coap_resource_set_dirty ( coap_resource_t r,
const coap_string_t query 
)
Deprecated:
use coap_resource_notify_observers() instead.

Definition at line 943 of file resource.c.

+ Here is the call graph for this function:

◆ match()

static int match ( const coap_str_const_t text,
const coap_str_const_t pattern,
int  match_prefix,
int  match_substring 
)
static

Definition at line 105 of file resource.c.

+ Here is the caller graph for this function:

Variable Documentation

◆ coap_proxy_resource_uri

const uint8_t coap_proxy_resource_uri[]
static
Initial value:
=
"- Proxy URI -"

Definition at line 352 of file resource.c.

◆ coap_unknown_resource_uri

const uint8_t coap_unknown_resource_uri[]
static
Initial value:
=
"- Unknown -"

Definition at line 331 of file resource.c.

◆ null_path

coap_str_const_t* null_path = &null_path_value
static

Definition at line 298 of file resource.c.

◆ null_path_value

coap_str_const_t null_path_value = {0, (const uint8_t*)""}
static

Definition at line 297 of file resource.c.