libcoap 4.3.2
|
API for handling PDUs using CoAP Contexts. More...
Macros | |
#define | coap_send_large(session, pdu) coap_send(session, pdu) |
#define | coap_join_mcast_group(ctx, groupname) (coap_join_mcast_group_intf(ctx, groupname, NULL)) |
Typedefs | |
typedef enum coap_response_t | coap_response_t |
typedef coap_response_t(* | coap_response_handler_t) (coap_session_t *session, const coap_pdu_t *sent, const coap_pdu_t *received, const coap_mid_t mid) |
Response handler that is used as callback in coap_context_t. More... | |
typedef void(* | coap_nack_handler_t) (coap_session_t *session, const coap_pdu_t *sent, const coap_nack_reason_t reason, const coap_mid_t mid) |
Negative Acknowedge handler that is used as callback in coap_context_t. More... | |
typedef void(* | coap_ping_handler_t) (coap_session_t *session, const coap_pdu_t *received, const coap_mid_t mid) |
Received Ping handler that is used as callback in coap_context_t. More... | |
typedef void(* | coap_pong_handler_t) (coap_session_t *session, const coap_pdu_t *received, const coap_mid_t mid) |
Received Pong handler that is used as callback in coap_context_t. More... | |
Enumerations | |
enum | coap_response_t { COAP_RESPONSE_FAIL , COAP_RESPONSE_OK } |
Functions | |
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 is received. More... | |
void | coap_register_nack_handler (coap_context_t *context, coap_nack_handler_t handler) |
Registers a new message handler that is called whenever a confirmable message (request or response) is dropped after all retries have been exhausted, or a rst message was received, or a network or TLS level event was received that indicates delivering the message is not possible. More... | |
void | coap_register_ping_handler (coap_context_t *context, coap_ping_handler_t handler) |
Registers a new message handler that is called whenever a CoAP Ping message is received. More... | |
void | coap_register_pong_handler (coap_context_t *context, coap_pong_handler_t handler) |
Registers a new message handler that is called whenever a CoAP Pong message is received. More... | |
void | coap_register_option (coap_context_t *ctx, uint16_t type) |
Registers the option type type with the given context object ctx . 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... | |
int | coap_context_set_psk (coap_context_t *context, const char *hint, const uint8_t *key, size_t key_len) |
Set the context's default PSK hint and/or key for a server. More... | |
int | coap_context_set_psk2 (coap_context_t *context, coap_dtls_spsk_t *setup_data) |
Set the context's default PSK hint and/or key for a server. More... | |
int | coap_context_set_pki (coap_context_t *context, const coap_dtls_pki_t *setup_data) |
Set the context's default PKI information for a server. More... | |
int | coap_context_set_pki_root_cas (coap_context_t *context, const char *ca_file, const char *ca_dir) |
Set the context's default Root CA information for a client or server. More... | |
void | coap_context_set_keepalive (coap_context_t *context, unsigned int seconds) |
Set the context keepalive timer for sessions. More... | |
void | coap_context_set_max_token_size (coap_context_t *context, size_t max_token_size) |
Set the maximum token size (RFC8974). More... | |
int | coap_context_get_coap_fd (const coap_context_t *context) |
Get the libcoap internal file descriptor for using in an application's select() or returned as an event in an application's epoll_wait() call. More... | |
void | coap_context_set_max_idle_sessions (coap_context_t *context, unsigned int max_idle_sessions) |
Set the maximum idle sessions count. More... | |
unsigned int | coap_context_get_max_idle_sessions (const coap_context_t *context) |
Get the maximum idle sessions count. More... | |
void | coap_context_set_session_timeout (coap_context_t *context, unsigned int session_timeout) |
Set the session timeout value. More... | |
unsigned int | coap_context_get_session_timeout (const coap_context_t *context) |
Get the session timeout value. More... | |
void | coap_context_set_csm_timeout (coap_context_t *context, unsigned int csm_tmeout) |
Set the CSM timeout value. More... | |
unsigned int | coap_context_get_csm_timeout (const coap_context_t *context) |
Get the CSM timeout value. More... | |
void | coap_context_set_csm_max_message_size (coap_context_t *context, uint32_t csm_max_message_size) |
Set the CSM max session size value. More... | |
uint32_t | coap_context_get_csm_max_message_size (const coap_context_t *context) |
Get the CSM max session size value. More... | |
void | coap_context_set_max_handshake_sessions (coap_context_t *context, unsigned int max_handshake_sessions) |
Set the maximum number of sessions in (D)TLS handshake value. More... | |
unsigned int | coap_context_get_max_handshake_sessions (const coap_context_t *context) |
Get the session timeout value. More... | |
uint16_t | coap_new_message_id (coap_session_t *session) |
Returns a new message id and updates session->tx_mid accordingly. More... | |
void | coap_free_context (coap_context_t *context) |
CoAP stack context must be released with coap_free_context(). More... | |
void | coap_set_app_data (coap_context_t *context, void *data) |
Stores data with the given CoAP context. More... | |
void * | coap_get_app_data (const coap_context_t *context) |
Returns any application-specific data that has been stored with context using the function coap_set_app_data(). More... | |
coap_pdu_t * | coap_new_error_response (const coap_pdu_t *request, coap_pdu_code_t code, coap_opt_filter_t *opts) |
Creates a new ACK PDU with specified error code . More... | |
coap_mid_t | coap_send_error (coap_session_t *session, const coap_pdu_t *request, coap_pdu_code_t code, coap_opt_filter_t *opts) |
Sends an error response with code code for request request to dst . More... | |
coap_mid_t | coap_send_message_type (coap_session_t *session, const coap_pdu_t *request, coap_pdu_type_t type) |
Helper function to create and send a message with type (usually ACK or RST). More... | |
coap_mid_t | coap_send_ack (coap_session_t *session, const coap_pdu_t *request) |
Sends an ACK message with code 0 for the specified request to dst . More... | |
COAP_STATIC_INLINE coap_mid_t | coap_send_rst (coap_session_t *session, const coap_pdu_t *request) |
Sends an RST message with code 0 for the specified request to dst . More... | |
coap_mid_t | coap_send (coap_session_t *session, coap_pdu_t *pdu) |
Sends a CoAP message to given peer. More... | |
int | coap_handle_event (coap_context_t *context, coap_event_t event, coap_session_t *session) |
Invokes the event handler of context for the given event and data . 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_join_mcast_group_intf (coap_context_t *ctx, const char *groupname, const char *ifname) |
Function interface for joining a multicast group for listening for the currently defined endpoints that are UDP. More... | |
int | coap_mcast_set_hops (coap_session_t *session, size_t hops) |
Function interface for defining the hop count (ttl) for sending multicast traffic. More... | |
void | coap_mcast_per_resource (coap_context_t *context) |
Function interface to enable processing mcast requests on a per resource basis. More... | |
API for handling PDUs using CoAP Contexts.
#define coap_join_mcast_group | ( | ctx, | |
groupname | |||
) | (coap_join_mcast_group_intf(ctx, groupname, NULL)) |
Definition at line 538 of file coap_net.h.
#define coap_send_large | ( | session, | |
pdu | |||
) | coap_send(session, pdu) |
Definition at line 491 of file coap_net.h.
typedef void(* coap_nack_handler_t) (coap_session_t *session, const coap_pdu_t *sent, const coap_nack_reason_t reason, const coap_mid_t mid) |
Negative Acknowedge handler that is used as callback in coap_context_t.
session | CoAP session. |
sent | The PDU that was transmitted. |
reason | The reason for the NACK. |
mid | CoAP message ID. |
Definition at line 75 of file coap_net.h.
typedef void(* coap_ping_handler_t) (coap_session_t *session, const coap_pdu_t *received, const coap_mid_t mid) |
Received Ping handler that is used as callback in coap_context_t.
session | CoAP session. |
received | The PDU that was received. |
mid | CoAP message ID. |
Definition at line 87 of file coap_net.h.
typedef void(* coap_pong_handler_t) (coap_session_t *session, const coap_pdu_t *received, const coap_mid_t mid) |
Received Pong handler that is used as callback in coap_context_t.
session | CoAP session. |
received | The PDU that was received. |
mid | CoAP message ID. |
Definition at line 98 of file coap_net.h.
typedef coap_response_t(* coap_response_handler_t) (coap_session_t *session, const coap_pdu_t *sent, const coap_pdu_t *received, const coap_mid_t mid) |
Response handler that is used as callback in coap_context_t.
session | CoAP session. |
sent | The PDU that was transmitted. |
received | The PDU that was received. |
mid | CoAP transaction ID. |
COAP_RESPONSE_OK
if successful, else COAP_RESPONSE_FAIL
which triggers sending a RST packet. Definition at line 62 of file coap_net.h.
typedef enum coap_response_t coap_response_t |
enum coap_response_t |
Enumerator | |
---|---|
COAP_RESPONSE_FAIL | Response not liked - send CoAP RST packet. |
COAP_RESPONSE_OK | Response is fine. |
Definition at line 46 of file coap_net.h.
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.
context | The CoAP context to check. |
0
if there are still pending transmits else 1
if nothing queued for transmission. Note that 0
does not mean there has been a response to a transmitted request. Definition at line 3928 of file coap_net.c.
int coap_context_get_coap_fd | ( | const coap_context_t * | context | ) |
Get the libcoap internal file descriptor for using in an application's select() or returned as an event in an application's epoll_wait() call.
context | The coap_context_t object. |
-1
if epoll is not available. Definition at line 454 of file coap_net.c.
uint32_t coap_context_get_csm_max_message_size | ( | const coap_context_t * | context | ) |
Get the CSM max session size value.
context | The coap_context_t object. |
Definition at line 438 of file coap_net.c.
unsigned int coap_context_get_csm_timeout | ( | const coap_context_t * | context | ) |
Get the CSM timeout value.
context | The coap_context_t object. |
Definition at line 426 of file coap_net.c.
unsigned int coap_context_get_max_handshake_sessions | ( | const coap_context_t * | context | ) |
Get the session timeout value.
context | The coap_context_t object. |
Definition at line 415 of file coap_net.c.
unsigned int coap_context_get_max_idle_sessions | ( | const coap_context_t * | context | ) |
Get the maximum idle sessions count.
context | The coap_context_t object. |
Definition at line 404 of file coap_net.c.
unsigned int coap_context_get_session_timeout | ( | const coap_context_t * | context | ) |
Get the session timeout value.
context | The coap_context_t object. |
Definition at line 449 of file coap_net.c.
void coap_context_set_csm_max_message_size | ( | coap_context_t * | context, |
uint32_t | csm_max_message_size | ||
) |
Set the CSM max session size value.
The largest PDU that can be received.
context | The coap_context_t object. |
csm_max_message_size | The CSM max message size value. |
Definition at line 431 of file coap_net.c.
void coap_context_set_csm_timeout | ( | coap_context_t * | context, |
unsigned int | csm_tmeout | ||
) |
Set the CSM timeout value.
The number of seconds to wait for a (TCP) CSM negotiation response from the peer. 0 (the default) means use wait forever.
context | The coap_context_t object. |
csm_tmeout | The CSM timeout value. |
Definition at line 420 of file coap_net.c.
void coap_context_set_keepalive | ( | coap_context_t * | context, |
unsigned int | seconds | ||
) |
Set the context keepalive timer for sessions.
A keepalive message will be sent after if a session has been inactive, i.e. no packet sent or received, for the given number of seconds. For unreliable protocols, a CoAP Empty message will be sent. If a CoAP RST is not received, the CoAP Empty messages will get resent based on the Confirmable retry parameters until there is a failure timeout, at which point the session will be considered as disconnected. For reliable protocols, a CoAP PING message will be sent. If a CoAP PONG has not been received before the next PING is due to be sent, the session will be considered as disconnected.
context | The coap_context_t object. |
seconds | Number of seconds for the inactivity timer, or zero to disable CoAP-level keepalive messages. |
Definition at line 385 of file coap_net.c.
void coap_context_set_max_handshake_sessions | ( | coap_context_t * | context, |
unsigned int | max_handshake_sessions | ||
) |
Set the maximum number of sessions in (D)TLS handshake value.
If this number is exceeded, the least recently used server session in handshake is completely removed. 0 (the default) means that the number is not monitored.
context | The coap_context_t object. |
max_handshake_sessions | The maximum number of sessions in handshake. |
Definition at line 409 of file coap_net.c.
void coap_context_set_max_idle_sessions | ( | coap_context_t * | context, |
unsigned int | max_idle_sessions | ||
) |
Set the maximum idle sessions count.
The number of server sessions that are currently not in use. If this number is exceeded, the least recently used server session is completely removed. 0 (the default) means that the number is not monitored.
context | The coap_context_t object. |
max_idle_sessions | The maximum idle session count. |
Definition at line 398 of file coap_net.c.
void coap_context_set_max_token_size | ( | coap_context_t * | context, |
size_t | max_token_size | ||
) |
Set the maximum token size (RFC8974).
context | The coap_context_t object. |
max_token_size | The maximum token size. A value between 8 and 65804 inclusive. |
Definition at line 390 of file coap_net.c.
int coap_context_set_pki | ( | coap_context_t * | context, |
const coap_dtls_pki_t * | setup_data | ||
) |
Set the context's default PKI information for a server.
context | The current coap_context_t object. |
setup_data | If NULL, PKI authentication will fail. Certificate information required. |
1
if successful, else 0
. int coap_context_set_pki_root_cas | ( | coap_context_t * | context, |
const char * | ca_file, | ||
const char * | ca_dir | ||
) |
Set the context's default Root CA information for a client or server.
context | The current coap_context_t object. |
ca_file | If not NULL, is the full path name of a PEM encoded file containing all the Root CAs to be used. |
ca_dir | If not NULL, points to a directory containing PEM encoded files containing all the Root CAs to be used. |
1
if successful, else 0
. Definition at line 375 of file coap_net.c.
int coap_context_set_psk | ( | coap_context_t * | context, |
const char * | hint, | ||
const uint8_t * | key, | ||
size_t | key_len | ||
) |
Set the context's default PSK hint and/or key for a server.
context | The current coap_context_t object. |
hint | The default PSK server hint sent to a client. If NULL, PSK authentication is disabled. Empty string is a valid hint. |
key | The default PSK key. If NULL, PSK authentication will fail. |
key_len | The default PSK key's length. If 0 , PSK authentication will fail. |
1
if successful, else 0
. int coap_context_set_psk2 | ( | coap_context_t * | context, |
coap_dtls_spsk_t * | setup_data | ||
) |
Set the context's default PSK hint and/or key for a server.
context | The current coap_context_t object. |
setup_data | If NULL, PSK authentication will fail. PSK information required. |
1
if successful, else 0
. void coap_context_set_session_timeout | ( | coap_context_t * | context, |
unsigned int | session_timeout | ||
) |
Set the session timeout value.
The number of seconds of inactivity after which an unused server session will be closed. 0 means use default (300 secs).
context | The coap_context_t object. |
session_timeout | The session timeout value. |
Definition at line 443 of file coap_net.c.
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.
context | The current coap_context_t object to free off. |
Definition at line 566 of file coap_net.c.
void * coap_get_app_data | ( | const coap_context_t * | context | ) |
Returns any application-specific data that has been stored with context
using the function coap_set_app_data().
This function will return NULL
if no data has been stored.
context | The CoAP context. |
NULL
if not data stored. Definition at line 560 of file coap_net.c.
int coap_handle_event | ( | coap_context_t * | context, |
coap_event_t | event, | ||
coap_session_t * | session | ||
) |
Invokes the event handler of context
for the given event
and data
.
context | The CoAP context whose event handler is to be called. |
event | The event to deliver. |
session | The session related to event . |
Definition at line 3917 of file coap_net.c.
int coap_join_mcast_group_intf | ( | coap_context_t * | ctx, |
const char * | groupname, | ||
const char * | ifname | ||
) |
Function interface for joining a multicast group for listening for the currently defined endpoints that are UDP.
ctx | The current context. |
groupname | The name of the group that is to be joined for listening. |
ifname | Network interface to join the group on, or NULL if first appropriate interface is to be chosen by the O/S. |
void coap_mcast_per_resource | ( | coap_context_t * | context | ) |
Function interface to enable processing mcast requests on a per resource basis.
This then enables a set of configuration flags set up when configuring the resources (coap_resource_init()).
context | The current context. |
int coap_mcast_set_hops | ( | coap_session_t * | session, |
size_t | hops | ||
) |
Function interface for defining the hop count (ttl) for sending multicast traffic.
session | The current session. |
hops | The number of hops (ttl) to use before the multicast packet expires. |
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 464 of file coap_net.c.
coap_pdu_t * coap_new_error_response | ( | const coap_pdu_t * | request, |
coap_pdu_code_t | 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 released 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 2356 of file coap_net.c.
uint16_t coap_new_message_id | ( | coap_session_t * | session | ) |
Returns a new message id and updates session->tx_mid
accordingly.
The message id is returned in network byte order to make it easier to read in tracing tools.
session | The current coap_session_t object. |
Definition at line 1590 of file coap_session.c.
void coap_register_nack_handler | ( | coap_context_t * | context, |
coap_nack_handler_t | handler | ||
) |
Registers a new message handler that is called whenever a confirmable message (request or response) is dropped after all retries have been exhausted, or a rst message was received, or a network or TLS level event was received that indicates delivering the message is not possible.
context | The context to register the handler for. |
handler | The nack handler to register. |
Definition at line 4069 of file coap_net.c.
void coap_register_option | ( | coap_context_t * | ctx, |
uint16_t | type | ||
) |
Registers the option type type
with the given context object ctx
.
ctx | The context to use. |
type | The option type to register. |
Definition at line 4087 of file coap_net.c.
void coap_register_ping_handler | ( | coap_context_t * | context, |
coap_ping_handler_t | handler | ||
) |
Registers a new message handler that is called whenever a CoAP Ping message is received.
context | The context to register the handler for. |
handler | The ping handler to register. |
Definition at line 4075 of file coap_net.c.
void coap_register_pong_handler | ( | coap_context_t * | context, |
coap_pong_handler_t | handler | ||
) |
Registers a new message handler that is called whenever a CoAP Pong message is received.
context | The context to register the handler for. |
handler | The pong handler to register. |
Definition at line 4081 of file coap_net.c.
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 is received.
context | The context to register the handler for. |
handler | The response handler to register. |
Definition at line 4058 of file coap_net.c.
coap_mid_t coap_send | ( | coap_session_t * | session, |
coap_pdu_t * | pdu | ||
) |
Sends a CoAP message to given peer.
The memory that is allocated for the pdu will be released by coap_send(). The caller must not use or delete the pdu after calling coap_send().
session | The CoAP session. |
pdu | The CoAP PDU to send. |
COAP_INVALID_MID
on error. Definition at line 1032 of file coap_net.c.
coap_mid_t coap_send_ack | ( | coap_session_t * | session, |
const coap_pdu_t * | request | ||
) |
Sends an ACK message with code 0
for the specified request
to dst
.
This function returns the corresponding message id if the message was sent or COAP_INVALID_MID
on error.
session | The CoAP session. |
request | The request to be acknowledged. |
COAP_INVALID_MID
on error. Definition at line 770 of file coap_net.c.
coap_mid_t coap_send_error | ( | coap_session_t * | session, |
const coap_pdu_t * | request, | ||
coap_pdu_code_t | 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 message id if the message was sent, or COAP_INVALID_MID
otherwise.
session | The CoAP session. |
request | The original request to respond to. |
code | The response code. |
opts | A filter that specifies the options to copy from the request . |
COAP_INVALID_MID
otherwise. Definition at line 836 of file coap_net.c.
coap_mid_t coap_send_message_type | ( | coap_session_t * | session, |
const coap_pdu_t * | request, | ||
coap_pdu_type_t | type | ||
) |
Helper function to create and send a message with type
(usually ACK or RST).
This function returns COAP_INVALID_MID
when the message was not sent, a valid transaction id otherwise.
session | The CoAP session. |
request | The request that should be responded to. |
type | Which type to set. |
COAP_INVALID_MID
otherwise. Definition at line 854 of file coap_net.c.
COAP_STATIC_INLINE coap_mid_t coap_send_rst | ( | coap_session_t * | session, |
const coap_pdu_t * | request | ||
) |
Sends an RST message with code 0
for the specified request
to dst
.
This function returns the corresponding message id if the message was sent or COAP_INVALID_MID
on error.
session | The CoAP session. |
request | The request to be reset. |
COAP_INVALID_MID
on error. Definition at line 474 of file coap_net.h.
void coap_set_app_data | ( | coap_context_t * | context, |
void * | data | ||
) |
Stores data
with the given CoAP context.
This function overwrites any value that has previously been stored with context
.
context | The CoAP context. |
data | The data to store with wih the context. Note that this data must be valid during the lifetime of context . |
Definition at line 554 of file coap_net.c.
void coap_ticks | ( | coap_tick_t * | ) |
Returns the current value of an internal tick counter.
The counter counts COAP_TICKS_PER_SECOND
ticks every second.