libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
Macros | Functions
resource.c File Reference
#include "config.h"
#include "net.h"
#include "debug.h"
#include "resource.h"
#include "subscribe.h"

Go to the source code of this file.

Macros

#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
 

Functions

int match (const str *text, const str *pattern, int match_prefix, int match_substring)
 
coap_print_status_t 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 (const unsigned char *uri, size_t len, int flags)
 Creates a new resource object and initializes the link field to the string of length len. More...
 
coap_attr_tcoap_add_attr (coap_resource_t *resource, const unsigned char *name, size_t nlen, const unsigned char *val, size_t vlen, int flags)
 Registers a new attribute with the given resource. More...
 
coap_attr_tcoap_find_attr (coap_resource_t *resource, const unsigned char *name, size_t nlen)
 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...
 
void coap_hash_request_uri (const coap_pdu_t *request, coap_key_t key)
 Calculates the hash key for the resource requested by the Uri-Options of request. More...
 
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_key_t key)
 Deletes a resource identified by key. More...
 
coap_resource_tcoap_get_resource_from_key (coap_context_t *context, coap_key_t key)
 Returns the resource identified by the unique string key. 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...
 
coap_subscription_tcoap_find_observer (coap_resource_t *resource, const coap_address_t *peer, const str *token)
 Returns a subscription object for given peer. More...
 
coap_subscription_tcoap_add_observer (coap_resource_t *resource, const coap_address_t *observer, const str *token)
 Adds the specified peer as observer for resource. More...
 
void coap_touch_observer (coap_context_t *context, const coap_address_t *observer, const str *token)
 Marks an observer as alive. More...
 
void coap_delete_observer (coap_resource_t *resource, const coap_address_t *observer, const str *token)
 Removes any subscription for observer from resource and releases the allocated storage. More...
 
static void coap_notify_observers (coap_context_t *context, coap_resource_t *r)
 
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, const coap_address_t *peer, const str *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, const coap_address_t *peer, const str *token)
 

Macro Definition Documentation

#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)); \
} \
}
#define PRINT_COND_WITH_OFFSET(Buf, Bufend, Offset, Char, Result)
Adds Char to Buf if Offset is zero and Buf is less than Bufend.
Definition: resource.c:84

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 96 of file resource.c.

#define INET6_ADDRSTRLEN   40
#define MATCH_PREFIX   0x02
#define MATCH_SUBSTRING   0x04
#define MATCH_URI   0x01
#define min (   a,
 
)    ((a) < (b) ? (a) : (b))

Definition at line 64 of file resource.c.

#define PRINT_COND_WITH_OFFSET (   Buf,
  Bufend,
  Offset,
  Char,
  Result 
)
Value:
{ \
if ((Buf) < (Bufend)) { \
PRINT_WITH_OFFSET(Buf,Offset,Char); \
} \
(Result)++; \
}
#define PRINT_WITH_OFFSET(Buf, Offset, Char)
Adds Char to Buf if Offset is zero.
Definition: resource.c:74

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

Definition at line 84 of file resource.c.

#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 74 of file resource.c.

Function Documentation

coap_attr_t* coap_add_attr ( coap_resource_t resource,
const unsigned char *  name,
size_t  nlen,
const unsigned char *  val,
size_t  vlen,
int  flags 
)

Registers a new attribute with the given resource.

As the attributes str fields will point to name and val the caller must ensure that these pointers are valid during the attribute's lifetime.

Parameters
resourceThe resource to register the attribute with.
nameThe attribute's name.
nlenLength of name.
valThe attribute's value or NULL if none.
vlenLength of val if specified.
flagsFlags for memory management (in particular release of memory)
Returns
A pointer to the new attribute or NULL on error.

Definition at line 341 of file resource.c.

coap_subscription_t* coap_add_observer ( coap_resource_t resource,
const coap_address_t *  observer,
const str token 
)

Adds the specified peer as observer for resource.

The subscription is identified by the given token. This function returns the registered subscription information if the observer has been added, or NULL on error.

Parameters
resourceThe observed resource.
observerThe remote peer that wants to received status updates.
tokenThe token that identifies this subscription.
token_lengthThe actual length of token. Must be 0 when token is NULL.
Returns
A pointer to the added/updated subscription information or NULL on error.

Definition at line 607 of file resource.c.

void coap_add_resource ( coap_context_t context,
coap_resource_t resource 
)

Registers the given resource for context.

The resource must have been created by coap_resource_init(), the storage allocated for the resource will be released by coap_delete_resource().

Parameters
contextThe context to use.
resourceThe resource to store.

Definition at line 440 of file resource.c.

void coap_check_notify ( coap_context_t context)

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

Definition at line 764 of file resource.c.

void coap_delete_attr ( coap_attr_t attr)

Deletes an attribute.

Parameters
attrPointer to a previously created attribute

Definition at line 405 of file resource.c.

void coap_delete_observer ( coap_resource_t resource,
const coap_address_t *  observer,
const str token 
)

Removes any subscription for observer from resource and releases the allocated storage.

Parameters
resourceThe observed resource.
observerThe observer's address.
tokenThe token that identifies this subscription or NULL for any token.

Definition at line 674 of file resource.c.

int coap_delete_resource ( coap_context_t context,
coap_key_t  key 
)

Deletes a resource identified by key.

The storage allocated for that resource is freed.

Parameters
contextThe context where the resources are stored.
keyThe unique key for the resource to delete.
Returns
1 if the resource was found (and destroyed), 0 otherwise.

Definition at line 451 of file resource.c.

coap_attr_t* coap_find_attr ( coap_resource_t resource,
const unsigned char *  name,
size_t  nlen 
)

Returns resource's coap_attr_t object with given name if found, NULL otherwise.

Parameters
resourceThe resource to search for attribute name.
nameName of the requested attribute.
nlenActual length of name.
Returns
The first attribute with specified name or NULL if none was found.

Definition at line 383 of file resource.c.

coap_subscription_t* coap_find_observer ( coap_resource_t resource,
const coap_address_t *  peer,
const str token 
)

Returns a subscription object for given peer.

Parameters
resourceThe observed resource.
peerThe address to search for.
tokenThe token that identifies this subscription or NULL for any token.
Returns
A valid subscription if exists or NULL otherwise.

Definition at line 589 of file resource.c.

coap_resource_t* coap_get_resource_from_key ( coap_context_t context,
coap_key_t  key 
)

Returns the resource identified by the unique string key.

If no resource was found, this function returns NULL.

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

Definition at line 503 of file resource.c.

void coap_handle_failed_notify ( coap_context_t context,
const coap_address_t *  peer,
const str token 
)

Definition at line 841 of file resource.c.

void coap_hash_request_uri ( const coap_pdu_t request,
coap_key_t  key 
)

Calculates the hash key for the resource requested by the Uri-Options of request.

This function calls coap_hash() for every path segment.

Parameters
requestThe requesting pdu.
keyThe resulting hash is stored in key

Definition at line 424 of file resource.c.

static void coap_notify_observers ( coap_context_t context,
coap_resource_t r 
)
static

Definition at line 688 of file resource.c.

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.

len must be initialized to the maximum length of buf and will be set to the number of characters actually written if successful. This function returns 1 on success or 0 on error.

Parameters
resourceThe resource to describe.
bufThe output buffer to write the description to.
lenMust be initialized to the length of buf and will be set to the length of the printed link description.
offsetThe offset within the resource description where to start writing into buf. This is useful for dealing with the Block2 option. offset is updated during output as it is consumed.
Returns
If COAP_PRINT_STATUS_ERROR is set, an error occured. Otherwise, the lower 28 bits will indicate the number of characters that have actually been output into buffer. The flag COAP_PRINT_STATUS_TRUNC indicates that the output has been truncated.

Definition at line 538 of file resource.c.

static void coap_remove_failed_observers ( coap_context_t context,
coap_resource_t resource,
const coap_address_t *  peer,
const str 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)
peerThe observer's address
tokenThe token that has been used for subscription.

Definition at line 799 of file resource.c.

coap_resource_t* coap_resource_init ( const unsigned char *  uri,
size_t  len,
int  flags 
)

Creates a new resource object and initializes the link field to the string of length len.

This function returns the new coap_resource_t object.

Parameters
uriThe URI path of the new resource.
lenThe length of uri.
flagsFlags for memory management (in particular release of memory)
Returns
A pointer to the new object or NULL on error.

Definition at line 307 of file resource.c.

void coap_touch_observer ( coap_context_t context,
const coap_address_t *  observer,
const str token 
)

Marks an observer as alive.

Parameters
contextThe CoAP context to use
observerThe transport address of the observer
tokenThe corresponding token that has been used for the subscription

Definition at line 643 of file resource.c.

int match ( const str text,
const str pattern,
int  match_prefix,
int  match_substring 
)

Definition at line 104 of file resource.c.

coap_print_status_t 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 166 of file resource.c.