Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
CoAP TCP client API

CoAP TCP client API. More...

Data Structures

union  coap_client_tcp_event_data
 Event data for CoAP TCP signaling events. More...
struct  coap_client_tcp_option
 Representation of extra options for the CoAP TCP client request. More...
struct  coap_client_tcp_request
 Representation of a CoAP TCP client request. More...

Macros

#define MAX_COAP_TCP_MSG_LEN
 Maximum size of a CoAP TCP message.

Typedefs

typedef void(* coap_client_tcp_response_cb_t) (const struct coap_client_response_data *data, void *user_data)
 Callback for CoAP TCP request.
typedef int(* coap_client_tcp_payload_cb_t) (size_t offset, const uint8_t **payload, size_t *len, bool *last_block, void *user_data)
 Callback for providing a payload for the CoAP TCP request.
typedef int(* coap_client_tcp_socket_config_cb_t) (int fd, void *user_data)
 Callback to configure the socket before connecting.
typedef void(* coap_client_tcp_event_cb_t) (struct coap_client_tcp *client, enum coap_client_tcp_event event, const union coap_client_tcp_event_data *data, void *user_data)
 Callback for CoAP TCP signaling events (Release/Abort).

Enumerations

enum  coap_client_tcp_event { COAP_CLIENT_TCP_EVENT_CSM_UPDATED , COAP_CLIENT_TCP_EVENT_PONG_RECEIVED , COAP_CLIENT_TCP_EVENT_RELEASE , COAP_CLIENT_TCP_EVENT_ABORT }
 CoAP TCP client event types for signaling. More...

Functions

int coap_client_tcp_init (struct coap_client_tcp *client, const char *info)
 Initialize the TCP CoAP client.
int coap_client_tcp_connect (struct coap_client_tcp *client, const struct net_sockaddr *addr, net_socklen_t addrlen, int proto)
 Connect the CoAP TCP client to a server.
int coap_client_tcp_close (struct coap_client_tcp *client)
 Close the CoAP TCP client connection.
int coap_client_tcp_req (struct coap_client_tcp *client, struct coap_client_tcp_request *req)
 Send CoAP request over TCP.
int coap_client_tcp_csm_req (struct coap_client_tcp *client, uint32_t max_block_size, coap_client_tcp_response_cb_t cb, void *user_data)
 Send CSM (Capabilities and Settings Message) over TCP.
void coap_client_tcp_cancel_requests (struct coap_client_tcp *client)
 Cancel all current TCP requests.
void coap_client_tcp_cancel_and_reset_all (struct coap_client_tcp *client)
 Cancel and fully reset all requests when changing socket.
struct coap_client_tcp_option coap_client_tcp_option_initial_block2 (struct coap_client_tcp *client)
 Get initial Block2 option for TCP (BERT-aware).
bool coap_client_tcp_has_ongoing_exchange (struct coap_client_tcp *client)
 Check if TCP client has ongoing exchange.
int coap_client_tcp_ping (struct coap_client_tcp *client)
 Send a Ping signal to the server (RFC 8323).
int coap_client_tcp_release (struct coap_client_tcp *client, const char *alt_addr, uint32_t hold_off_sec)
 Send a Release signal to the server (RFC 8323).
void coap_client_tcp_set_event_cb (struct coap_client_tcp *client, coap_client_tcp_event_cb_t cb, void *user_data)
 Set callback for signaling events (Release/Abort).

Detailed Description

CoAP TCP client API.

Macro Definition Documentation

◆ MAX_COAP_TCP_MSG_LEN

#define MAX_COAP_TCP_MSG_LEN

#include <zephyr/net/coap_client_tcp.h>

Value:
(CONFIG_COAP_CLIENT_MESSAGE_HEADER_SIZE + \
CONFIG_COAP_CLIENT_MESSAGE_SIZE)

Maximum size of a CoAP TCP message.

Typedef Documentation

◆ coap_client_tcp_event_cb_t

typedef void(* coap_client_tcp_event_cb_t) (struct coap_client_tcp *client, enum coap_client_tcp_event event, const union coap_client_tcp_event_data *data, void *user_data)

#include <zephyr/net/coap_client_tcp.h>

Callback for CoAP TCP signaling events (Release/Abort).

Parameters
clientThe client instance that received the event.
eventThe signaling event type.
dataEvent-specific data (may be NULL for some events).
user_dataUser provided context.

◆ coap_client_tcp_payload_cb_t

typedef int(* coap_client_tcp_payload_cb_t) (size_t offset, const uint8_t **payload, size_t *len, bool *last_block, void *user_data)

#include <zephyr/net/coap_client_tcp.h>

Callback for providing a payload for the CoAP TCP request.

Parameters
offsetPayload offset from the beginning of a blockwise transfer.
payloadA pointer for the buffer containing the payload block.
lenRequested (maximum) block size on input. The actual payload length on output.
last_blockA pointer to the flag indicating whether more payload blocks are expected.
user_dataUser provided context.
Returns
Zero on success, a negative error code to abort upload.

◆ coap_client_tcp_response_cb_t

typedef void(* coap_client_tcp_response_cb_t) (const struct coap_client_response_data *data, void *user_data)

#include <zephyr/net/coap_client_tcp.h>

Callback for CoAP TCP request.

Parameters
dataThe CoAP response data.
user_dataUser provided context.

◆ coap_client_tcp_socket_config_cb_t

typedef int(* coap_client_tcp_socket_config_cb_t) (int fd, void *user_data)

#include <zephyr/net/coap_client_tcp.h>

Callback to configure the socket before connecting.

Called after socket creation but before connect, allowing the application to set socket options (e.g. TLS credentials).

Parameters
fdThe socket file descriptor.
user_dataUser provided context.
Returns
0 on success, negative error code otherwise.

Enumeration Type Documentation

◆ coap_client_tcp_event

#include <zephyr/net/coap_client_tcp.h>

CoAP TCP client event types for signaling.

Enumerator
COAP_CLIENT_TCP_EVENT_CSM_UPDATED 

CSM capabilities have been updated from server.

COAP_CLIENT_TCP_EVENT_PONG_RECEIVED 

Pong received in response to Ping.

COAP_CLIENT_TCP_EVENT_RELEASE 

Server initiated connection release (RFC 8323).

COAP_CLIENT_TCP_EVENT_ABORT 

Server aborted connection (RFC 8323).

Function Documentation

◆ coap_client_tcp_cancel_and_reset_all()

void coap_client_tcp_cancel_and_reset_all ( struct coap_client_tcp * client)

#include <zephyr/net/coap_client_tcp.h>

Cancel and fully reset all requests when changing socket.

Use when tearing down a transport to ensure clean state for a new socket.

Parameters
clientClient instance.

◆ coap_client_tcp_cancel_requests()

void coap_client_tcp_cancel_requests ( struct coap_client_tcp * client)

#include <zephyr/net/coap_client_tcp.h>

Cancel all current TCP requests.

Parameters
clientClient instance.

◆ coap_client_tcp_close()

int coap_client_tcp_close ( struct coap_client_tcp * client)

#include <zephyr/net/coap_client_tcp.h>

Close the CoAP TCP client connection.

Cancels any ongoing requests and closes the underlying socket. For graceful shutdown, send a Release signal with coap_client_tcp_release before calling this.

Parameters
clientClient instance.
Returns
0 on success, negative error code otherwise.

◆ coap_client_tcp_connect()

int coap_client_tcp_connect ( struct coap_client_tcp * client,
const struct net_sockaddr * addr,
net_socklen_t addrlen,
int proto )

#include <zephyr/net/coap_client_tcp.h>

Connect the CoAP TCP client to a server.

Unlike the UDP client, the TCP client manages the socket internally. This function creates a TCP socket, connects to the server, and automatically sends a CSM (Capabilities and Settings Message) to negotiate capabilities per RFC 8323.

Parameters
clientClient instance.
addrDestination address of the server.
addrlenLength of the address structure.
protoProtocol to use (IPPROTO_TCP or IPPROTO_TLS_1_2).
Returns
0 on success, negative error code otherwise.

◆ coap_client_tcp_csm_req()

int coap_client_tcp_csm_req ( struct coap_client_tcp * client,
uint32_t max_block_size,
coap_client_tcp_response_cb_t cb,
void * user_data )

#include <zephyr/net/coap_client_tcp.h>

Send CSM (Capabilities and Settings Message) over TCP.

Per RFC 8323, CSM should be exchanged when a CoAP-over-TCP connection is established. This negotiates capabilities like max message size and BERT.

Parameters
clientClient instance (must be connected via coap_client_tcp_connect).
max_block_sizeMaximum block size to advertise.
cbResponse callback (can be NULL since server may not respond).
user_dataUser data for callback.
Returns
0 on success, -ENOTCONN if not connected, negative error code otherwise.

◆ coap_client_tcp_has_ongoing_exchange()

bool coap_client_tcp_has_ongoing_exchange ( struct coap_client_tcp * client)

#include <zephyr/net/coap_client_tcp.h>

Check if TCP client has ongoing exchange.

Parameters
clientPointer to the CoAP TCP client instance.
Returns
true if there is an ongoing exchange, false otherwise.

◆ coap_client_tcp_init()

int coap_client_tcp_init ( struct coap_client_tcp * client,
const char * info )

#include <zephyr/net/coap_client_tcp.h>

Initialize the TCP CoAP client.

Parameters
[in]clientClient instance.
[in]infoName for the receiving thread (NULL for default).
Returns
0 on success, negative error code otherwise.

◆ coap_client_tcp_option_initial_block2()

struct coap_client_tcp_option coap_client_tcp_option_initial_block2 ( struct coap_client_tcp * client)

#include <zephyr/net/coap_client_tcp.h>

Get initial Block2 option for TCP (BERT-aware).

Parameters
clientClient instance.
Returns
CoAP client initial Block2 option structure.

◆ coap_client_tcp_ping()

int coap_client_tcp_ping ( struct coap_client_tcp * client)

#include <zephyr/net/coap_client_tcp.h>

Send a Ping signal to the server (RFC 8323).

This sends a Ping signal and waits for Pong response. Can be used for connection keep-alive or RTT measurement.

Parameters
clientClient instance (must be connected).
Returns
0 on success, negative error code otherwise.

◆ coap_client_tcp_release()

int coap_client_tcp_release ( struct coap_client_tcp * client,
const char * alt_addr,
uint32_t hold_off_sec )

#include <zephyr/net/coap_client_tcp.h>

Send a Release signal to the server (RFC 8323).

This signals graceful connection termination. The server should complete pending requests before closing.

Parameters
clientClient instance (must be connected).
alt_addrAlternative address suggestion (NULL if none).
hold_off_secHold-off time in seconds (0 if none).
Returns
0 on success, negative error code otherwise.

◆ coap_client_tcp_req()

int coap_client_tcp_req ( struct coap_client_tcp * client,
struct coap_client_tcp_request * req )

#include <zephyr/net/coap_client_tcp.h>

Send CoAP request over TCP.

Parameters
clientClient instance (must be connected via coap_client_tcp_connect).
reqCoAP request structure.
Returns
0 on success, -ENOTCONN if not connected, negative error code otherwise.

◆ coap_client_tcp_set_event_cb()

void coap_client_tcp_set_event_cb ( struct coap_client_tcp * client,
coap_client_tcp_event_cb_t cb,
void * user_data )

#include <zephyr/net/coap_client_tcp.h>

Set callback for signaling events (Release/Abort).

Parameters
clientClient instance.
cbCallback function to invoke on events.
user_dataUser data passed to callback.