libcoap
4.1.2
|
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include "coap_io.h"
#include "coap_time.h"
#include "option.h"
#include "pdu.h"
#include "prng.h"
Go to the source code of this file.
Data Structures | |
struct | coap_queue_t |
struct | coap_mid_cache_t |
struct | coap_context_t |
The CoAP stack's global state is stored in a coap_context_t object. More... | |
Macros | |
#define | COAP_MID_CACHE_SIZE 3 |
Typedefs | |
typedef struct coap_queue_t | coap_queue_t |
typedef void(* | coap_response_handler_t) (struct coap_context_t *, const coap_endpoint_t *local_interface, const coap_address_t *remote, coap_pdu_t *sent, coap_pdu_t *received, const coap_tid_t id) |
Message handler that is used as call-back in coap_context_t. More... | |
typedef struct coap_context_t | coap_context_t |
The CoAP stack's global state is stored in a coap_context_t object. More... | |
Functions | |
int | coap_insert_node (coap_queue_t **queue, coap_queue_t *node) |
Adds node to given queue, ordered by node->t. More... | |
int | coap_delete_node (coap_queue_t *node) |
Destroys specified node. More... | |
void | coap_delete_all (coap_queue_t *queue) |
Removes all items from given queue and frees the allocated storage. More... | |
coap_queue_t * | coap_new_node (void) |
Creates a new node suitable for adding to the CoAP sendqueue. More... | |
static void | coap_register_response_handler (coap_context_t *context, coap_response_handler_t handler) |
Registers a new message handler that is called whenever a response was received that matches an ongoing transaction. More... | |
static void | coap_register_option (coap_context_t *ctx, unsigned char type) |
Registers the option type type with the given context object ctx . More... | |
unsigned int | coap_adjust_basetime (coap_context_t *ctx, coap_tick_t now) |
Set sendqueue_basetime in the given context object ctx to now . More... | |
coap_queue_t * | coap_peek_next (coap_context_t *context) |
Returns the next pdu to send without removing from sendqeue. More... | |
coap_queue_t * | coap_pop_next (coap_context_t *context) |
Returns the next pdu to send and removes it from the sendqeue. More... | |
coap_context_t * | coap_new_context (const coap_address_t *listen_addr) |
Creates a new coap_context_t object that will hold the CoAP stack status. More... | |
static unsigned short | coap_new_message_id (coap_context_t *context) |
Returns a new message id and updates context->message_id accordingly. More... | |
void | coap_free_context (coap_context_t *context) |
CoAP stack context must be released with coap_free_context(). More... | |
coap_tid_t | coap_send_confirmed (coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *pdu) |
Sends a confirmed CoAP message to given destination. More... | |
coap_pdu_t * | coap_new_error_response (coap_pdu_t *request, unsigned char code, coap_opt_filter_t opts) |
Creates a new ACK PDU with specified error code . More... | |
coap_tid_t | coap_send (coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *pdu) |
Sends a non-confirmed CoAP message to given destination. More... | |
coap_tid_t | coap_send_error (coap_context_t *context, coap_pdu_t *request, const coap_endpoint_t *local_interface, const coap_address_t *dst, unsigned char code, coap_opt_filter_t opts) |
Sends an error response with code code for request request to dst . More... | |
coap_tid_t | coap_send_message_type (coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *request, unsigned char type) |
Helper funktion to create and send a message with type (usually ACK or RST). More... | |
coap_tid_t | coap_send_ack (coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *request) |
Sends an ACK message with code 0 for the specified request to dst . More... | |
static coap_tid_t | coap_send_rst (coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *request) |
Sends an RST message with code 0 for the specified request to dst . More... | |
coap_tid_t | coap_retransmit (coap_context_t *context, coap_queue_t *node) |
Handles retransmissions of confirmable messages. More... | |
int | coap_read (coap_context_t *context) |
Reads data from the network and tries to parse as CoAP PDU. More... | |
int | coap_handle_message (coap_context_t *ctx, coap_packet_t *packet) |
Parses and interprets a CoAP message with context ctx . More... | |
void | coap_transaction_id (const coap_address_t *peer, const coap_pdu_t *pdu, coap_tid_t *id) |
Calculates a unique transaction id from given arguments peer and pdu . More... | |
int | coap_remove_from_queue (coap_queue_t **queue, coap_tid_t id, coap_queue_t **node) |
This function removes the element with given id from the list given list. More... | |
static int | coap_remove_transaction (coap_queue_t **queue, coap_tid_t id) |
Removes the transaction identified by id from given queue . More... | |
coap_queue_t * | coap_find_transaction (coap_queue_t *queue, coap_tid_t id) |
Retrieves transaction from the queue. More... | |
void | coap_cancel_all_messages (coap_context_t *context, const coap_address_t *dst, const unsigned char *token, size_t token_length) |
Cancels all outstanding messages for peer dst that have the specified token. More... | |
void | coap_dispatch (coap_context_t *context, coap_queue_t *rcvd) |
Dispatches the PDUs from the receive queue in given context. More... | |
int | coap_can_exit (coap_context_t *context) |
Returns 1 if there are no messages to send or to dispatch in the context's queues. More... | |
void | coap_ticks (coap_tick_t *) |
Returns the current value of an internal tick counter. More... | |
int | coap_option_check_critical (coap_context_t *ctx, coap_pdu_t *pdu, coap_opt_filter_t unknown) |
Verifies that pdu contains no unknown critical options. More... | |
coap_pdu_t * | coap_wellknown_response (coap_context_t *context, coap_pdu_t *request) |
Creates a new response for given request with the contents of .well-known/core. More... | |
typedef struct coap_context_t coap_context_t |
The CoAP stack's global state is stored in a coap_context_t object.
typedef struct coap_queue_t coap_queue_t |
typedef void(* coap_response_handler_t) (struct coap_context_t *, const coap_endpoint_t *local_interface, const coap_address_t *remote, coap_pdu_t *sent, coap_pdu_t *received, const coap_tid_t id) |
Message handler that is used as call-back in coap_context_t.
unsigned int coap_adjust_basetime | ( | coap_context_t * | ctx, |
coap_tick_t | now | ||
) |
int coap_can_exit | ( | coap_context_t * | context | ) |
void coap_cancel_all_messages | ( | coap_context_t * | context, |
const coap_address_t * | dst, | ||
const unsigned char * | token, | ||
size_t | token_length | ||
) |
Cancels all outstanding messages for peer dst
that have the specified token.
context | The context in use. |
dst | Destination address of the messages to remove. |
token | Message token. |
token_length | Actual length of token . |
Definition at line 980 of file net.c.
void coap_delete_all | ( | coap_queue_t * | queue | ) |
int coap_delete_node | ( | coap_queue_t * | node | ) |
void coap_dispatch | ( | coap_context_t * | context, |
coap_queue_t * | rcvd | ||
) |
coap_queue_t* coap_find_transaction | ( | coap_queue_t * | queue, |
coap_tid_t | id | ||
) |
void coap_free_context | ( | coap_context_t * | context | ) |
CoAP stack context must be released with coap_free_context().
This function clears all entries from the receive queue and send queue and deletes the resources that have been registered with context
, and frees the attached endpoints.
Definition at line 418 of file net.c.
int coap_handle_message | ( | coap_context_t * | ctx, |
coap_packet_t * | packet | ||
) |
Parses and interprets a CoAP message with context ctx
.
This function returns 0
if the message was handled, or a value less than zero on error.
ctx | The current CoAP context. |
packet | The received packet. |
0
if message was handled successfully, or less than zero on error. get debug to work again ** unsigned char addr[INET6_ADDRSTRLEN+8], localaddr[INET6_ADDRSTRLEN+8]; if (coap_print_addr(remote, addr, INET6_ADDRSTRLEN+8) && coap_print_addr(&packet->dst, localaddr, INET6_ADDRSTRLEN+8) ) debug("** received %d bytes from %s on interface %s:\n", (int)msg_len, addr, localaddr);
Definition at line 845 of file net.c.
int coap_insert_node | ( | coap_queue_t ** | queue, |
coap_queue_t * | node | ||
) |
coap_context_t* coap_new_context | ( | const coap_address_t * | listen_addr | ) |
Creates a new coap_context_t object that will hold the CoAP stack status.
Definition at line 333 of file net.c.
coap_pdu_t* coap_new_error_response | ( | coap_pdu_t * | request, |
unsigned char | code, | ||
coap_opt_filter_t | opts | ||
) |
Creates a new ACK PDU with specified error code
.
The options specified by the filter expression opts
will be copied from the original request contained in request
. Unless SHORT_ERROR_RESPONSE
was defined at build time, the textual reason phrase for code
will be added as payload, with Content-Type 0
. This function returns a pointer to the new response message, or NULL
on error. The storage allocated for the new message must be relased with coap_free().
request | Specification of the received (confirmable) request. |
code | The error code to set. |
opts | An option filter that specifies which options to copy from the original request in node . |
NULL
on error. Definition at line 1028 of file net.c.
|
inlinestatic |
Returns a new message id and updates context->message_id
accordingly.
The message id is returned in network byte order to make it easier to read in tracing tools.
context | The current coap_context_t object. |
Definition at line 189 of file net.h.
coap_queue_t* coap_new_node | ( | void | ) |
int coap_option_check_critical | ( | coap_context_t * | ctx, |
coap_pdu_t * | pdu, | ||
coap_opt_filter_t | unknown | ||
) |
Verifies that pdu
contains no unknown critical options.
Options must be registered at ctx
, using the function coap_register_option(). A basic set of options is registered automatically by coap_new_context(). This function returns 1
if pdu
is ok, 0
otherwise. The given filter object unknown
will be updated with the unknown options. As only COAP_MAX_OPT
options can be signalled this way, remaining options must be examined manually.
ctx | The context where all known options are registered. |
pdu | The PDU to check. |
unknown | The output filter that will be updated to indicate the unknown critical options found in pdu . |
1
if everything was ok, 0
otherwise. Definition at line 443 of file net.c.
coap_queue_t* coap_peek_next | ( | coap_context_t * | context | ) |
coap_queue_t* coap_pop_next | ( | coap_context_t * | context | ) |
int coap_read | ( | coap_context_t * | context | ) |
Reads data from the network and tries to parse as CoAP PDU.
On success, 0 is returned and a new node with the parsed PDU is added to the receive queue in the specified context object.
Definition at line 822 of file net.c.
|
inlinestatic |
|
inlinestatic |
int coap_remove_from_queue | ( | coap_queue_t ** | queue, |
coap_tid_t | id, | ||
coap_queue_t ** | node | ||
) |
This function removes the element with given id
from the list given list.
If id
was found, node
is updated to point to the removed element. Note that the storage allocated by node
is not released. The caller must do this manually using coap_delete_node(). This function returns 1
if the element with id id
was found, 0
otherwise. For a return value of 0
, the contents of node
is undefined.
queue | The queue to search for id . |
id | The node id to look for. |
node | If found, node is updated to point to the removed node. You must release the storage pointed to by node manually. |
1
if id
was found, 0
otherwise. Definition at line 930 of file net.c.
|
inlinestatic |
Removes the transaction identified by id
from given queue
.
This is a convenience function for coap_remove_from_queue() with automatic deletion of the removed node.
queue | The queue to search for id . |
id | The transaction id. |
1
if node was found, removed and destroyed, 0
otherwise. Definition at line 424 of file net.h.
coap_tid_t coap_retransmit | ( | coap_context_t * | context, |
coap_queue_t * | node | ||
) |
coap_tid_t coap_send | ( | coap_context_t * | context, |
const coap_endpoint_t * | local_interface, | ||
const coap_address_t * | dst, | ||
coap_pdu_t * | pdu | ||
) |
Sends a non-confirmed CoAP message to given destination.
The memory that is allocated by pdu will not be released by coap_send(). The caller must release the memory.
context | The CoAP context to use. |
local_interface | The local network interface where the outbound packet is sent. |
dst | The address to send to. |
pdu | The CoAP PDU to send. |
COAP_INVALID_TID
on error. Definition at line 613 of file net.c.
coap_tid_t coap_send_ack | ( | coap_context_t * | context, |
const coap_endpoint_t * | local_interface, | ||
const coap_address_t * | dst, | ||
coap_pdu_t * | request | ||
) |
Sends an ACK message with code 0
for the specified request
to dst
.
This function returns the corresponding transaction id if the message was sent or COAP_INVALID_TID
on error.
context | The context to use. |
local_interface | The local network interface where the outbound packet is sent. |
dst | The destination address. |
request | The request to be acknowledged. |
COAP_INVALID_TID
on error. Definition at line 533 of file net.c.
coap_tid_t coap_send_confirmed | ( | coap_context_t * | context, |
const coap_endpoint_t * | local_interface, | ||
const coap_address_t * | dst, | ||
coap_pdu_t * | pdu | ||
) |
Sends a confirmed CoAP message to given destination.
The memory that is allocated by pdu will not be released by coap_send_confirmed(). The caller must release the memory.
context | The CoAP context to use. |
local_interface | The local network interface where the outbound packet is sent. |
dst | The address to send to. |
pdu | The CoAP PDU to send. |
COAP_INVALID_TID
on error. Definition at line 699 of file net.c.
coap_tid_t coap_send_error | ( | coap_context_t * | context, |
coap_pdu_t * | request, | ||
const coap_endpoint_t * | local_interface, | ||
const coap_address_t * | dst, | ||
unsigned char | code, | ||
coap_opt_filter_t | opts | ||
) |
Sends an error response with code code
for request request
to dst
.
opts
will be passed to coap_new_error_response() to copy marked options from the request. This function returns the transaction id if the message was sent, or COAP_INVALID_TID
otherwise.
context | The context to use. |
request | The original request to respond to. |
local_interface | The local network interface where the outbound packet is sent. |
dst | The remote peer that sent the request. |
code | The response code. |
opts | A filter that specifies the options to copy from the request . |
COAP_INVALID_TID
otherwise. Definition at line 621 of file net.c.
coap_tid_t coap_send_message_type | ( | coap_context_t * | context, |
const coap_endpoint_t * | local_interface, | ||
const coap_address_t * | dst, | ||
coap_pdu_t * | request, | ||
unsigned char | type | ||
) |
Helper funktion to create and send a message with type
(usually ACK or RST).
This function returns COAP_INVALID_TID
when the message was not sent, a valid transaction id otherwise.
context | The CoAP context. |
local_interface | The local network interface where the outbound packet is sent. |
dst | Where to send the context. |
request | The request that should be responded to. |
type | Which type to set. |
COAP_INVALID_TID
otherwise. Definition at line 643 of file net.c.
|
inlinestatic |
Sends an RST message with code 0
for the specified request
to dst
.
This function returns the corresponding transaction id if the message was sent or COAP_INVALID_TID
on error.
context | The context to use. |
local_interface | The local network interface where the outbound packet is sent. |
dst | The destination address. |
request | The request to be reset. |
COAP_INVALID_TID
on error. Definition at line 346 of file net.h.
void coap_transaction_id | ( | const coap_address_t * | peer, |
const coap_pdu_t * | pdu, | ||
coap_tid_t * | id | ||
) |
coap_pdu_t* coap_wellknown_response | ( | coap_context_t * | context, |
coap_pdu_t * | request | ||
) |
Creates a new response for given request
with the contents of .well-known/core.
The result is NULL on error or a newly allocated PDU that must be released by coap_delete_pdu().
context | The current coap context to use. |
request | The request for .well-known/core . |
.well-known/core or NULL on error. Definition at line 1143 of file net.c.