libcoap
4.3.0
|
API functions 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... | |
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_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... | |
API functions for handling PDUs using CoAP Contexts.
#define coap_join_mcast_group | ( | ctx, | |
groupname | |||
) | (coap_join_mcast_group_intf(ctx, groupname, NULL)) |
#define coap_send_large | ( | session, | |
pdu | |||
) | coap_send(session, pdu) |
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. |
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. |
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. |
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. typedef enum coap_response_t coap_response_t |
enum coap_response_t |
int coap_can_exit | ( | coap_context_t * | context | ) |
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. unsigned int coap_context_get_csm_timeout | ( | const coap_context_t * | context | ) |
Get the CSM timeout value.
context | The coap_context_t object. |
unsigned int coap_context_get_max_handshake_sessions | ( | const coap_context_t * | context | ) |
Get the session timeout value.
context | The coap_context_t object. |
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. |
unsigned int coap_context_get_session_timeout | ( | const coap_context_t * | context | ) |
Get the session timeout value.
context | The coap_context_t object. |
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. |
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. |
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. |
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. |
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
. Definition at line 422 of file net.c.
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 437 of file 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
. Definition at line 388 of file net.c.
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
. Definition at line 408 of file net.c.
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. |
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 631 of file 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. 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 3163 of file 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. |
Definition at line 3280 of file net.c.
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 contexsion. |
hops | The number of hops (ttl) to use before the multicast packet expires. |
Definition at line 3503 of file net.c.
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 505 of file 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 2057 of file 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 1158 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. |
void coap_register_option | ( | coap_context_t * | ctx, |
uint16_t | type | ||
) |
void coap_register_ping_handler | ( | coap_context_t * | context, |
coap_ping_handler_t | handler | ||
) |
void coap_register_pong_handler | ( | coap_context_t * | context, |
coap_pong_handler_t | handler | ||
) |
void coap_register_response_handler | ( | coap_context_t * | context, |
coap_response_handler_t | handler | ||
) |
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 the pdu after calling coap_send().
session | The CoAP session. |
pdu | The CoAP PDU to send. |
COAP_INVALID_MID
on error. Definition at line 1057 of file 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 752 of file 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 902 of file 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 920 of file 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 452 of file 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 . |
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.