Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
coap_client_tcp.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Nordic Semiconductor ASA
3 * Copyright (c) 2026 Ellenby Technologies Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7#ifndef ZEPHYR_INCLUDE_NET_COAP_CLIENT_TCP_H_
8#define ZEPHYR_INCLUDE_NET_COAP_CLIENT_TCP_H_
9
17
24
25#include <zephyr/net/coap.h>
27#include <zephyr/kernel.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
34#define MAX_COAP_TCP_MSG_LEN (CONFIG_COAP_CLIENT_MESSAGE_HEADER_SIZE + \
35 CONFIG_COAP_CLIENT_MESSAGE_SIZE)
36
45 void *user_data);
46
59typedef int (*coap_client_tcp_payload_cb_t)(size_t offset, const uint8_t **payload,
60 size_t *len, bool *last_block,
61 void *user_data);
62
75typedef int (*coap_client_tcp_socket_config_cb_t)(int fd, void *user_data);
76
77/* Forward declaration for event callback */
78struct coap_client_tcp;
79
93
111
122 struct coap_client_tcp *client,
123 enum coap_client_tcp_event event,
124 const union coap_client_tcp_event_data *data,
125 void *user_data);
126
133#if defined(CONFIG_COAP_EXTENDED_OPTIONS_LEN)
137 uint8_t value[CONFIG_COAP_EXTENDED_OPTIONS_LEN_VALUE];
138#else
143#endif
144};
145
147#define MAX_TCP_PATH_SIZE (CONFIG_COAP_CLIENT_MAX_PATH_LENGTH + 1)
148#define MAX_TCP_EXTRA_OPTIONS CONFIG_COAP_CLIENT_MAX_EXTRA_OPTIONS
150
167
169struct coap_client_tcp_internal_request {
170 uint8_t request_token[COAP_TOKEN_MAX_LEN];
171 uint32_t offset;
172 uint8_t request_tkl;
173 bool request_ongoing;
174 atomic_t in_callback;
175 struct coap_block_context recv_blk_ctx;
176 struct coap_block_context send_blk_ctx;
177 struct coap_client_tcp_request coap_request;
178 struct coap_packet request;
179 uint8_t request_tag[COAP_TOKEN_MAX_LEN];
181
182 /* TCP-specific timeout handling (no retransmission) */
183 int64_t tcp_t0;
184 uint32_t tcp_timeout_ms;
185
186 /* For blockwise transfers */
187 uint32_t last_payload_len;
188
189 /* For GETs with observe option set */
190 bool is_observe;
191};
192
193struct coap_client_tcp {
194 int fd;
195 struct k_mutex lock;
197 size_t recv_offset; /* Bytes received so far into recv_buf */
198 struct coap_client_tcp_internal_request requests[CONFIG_COAP_CLIENT_MAX_REQUESTS];
199 struct coap_option echo_option;
200 bool send_echo;
201
202 /* Max block/message size from CSM negotiation */
203 uint32_t max_block_size;
204
205 /* Blockwise transfer capability (negotiated via CSM BWT option) */
206 bool blockwise_enabled;
207
208 /* Ping/Pong tracking */
209 bool ping_pending;
210 int64_t ping_t0;
211
212 /* Event callback for Release/Abort signals */
214 void *event_cb_user_data;
215
216 coap_client_tcp_socket_config_cb_t socket_config_cb;
217 void *socket_config_cb_user_data;
218};
220
229int coap_client_tcp_init(struct coap_client_tcp *client, const char *info);
230
246int coap_client_tcp_connect(struct coap_client_tcp *client,
247 const struct net_sockaddr *addr,
248 net_socklen_t addrlen, int proto);
249
261int coap_client_tcp_close(struct coap_client_tcp *client);
262
270int coap_client_tcp_req(struct coap_client_tcp *client,
271 struct coap_client_tcp_request *req);
272
285int coap_client_tcp_csm_req(struct coap_client_tcp *client,
286 uint32_t max_block_size,
288 void *user_data);
289
295void coap_client_tcp_cancel_requests(struct coap_client_tcp *client);
296
304void coap_client_tcp_cancel_and_reset_all(struct coap_client_tcp *client);
305
313
320bool coap_client_tcp_has_ongoing_exchange(struct coap_client_tcp *client);
321
331int coap_client_tcp_ping(struct coap_client_tcp *client);
332
344int coap_client_tcp_release(struct coap_client_tcp *client,
345 const char *alt_addr, uint32_t hold_off_sec);
346
354void coap_client_tcp_set_event_cb(struct coap_client_tcp *client,
356 void *user_data);
357
358#ifdef __cplusplus
359}
360#endif
361
365
366#endif /* ZEPHYR_INCLUDE_NET_COAP_CLIENT_TCP_H_ */
long atomic_t
Definition atomic_types.h:15
CoAP implementation for Zephyr.
CoAP client API.
void coap_client_tcp_cancel_and_reset_all(struct coap_client_tcp *client)
Cancel and fully reset all requests when changing socket.
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).
Definition coap_client_tcp.h:121
struct coap_client_tcp_option coap_client_tcp_option_initial_block2(struct coap_client_tcp *client)
Get initial Block2 option for TCP (BERT-aware).
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).
bool coap_client_tcp_has_ongoing_exchange(struct coap_client_tcp *client)
Check if TCP client has ongoing exchange.
void coap_client_tcp_cancel_requests(struct coap_client_tcp *client)
Cancel all current TCP requests.
int coap_client_tcp_close(struct coap_client_tcp *client)
Close the CoAP TCP client connection.
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.
int(* coap_client_tcp_socket_config_cb_t)(int fd, void *user_data)
Callback to configure the socket before connecting.
Definition coap_client_tcp.h:75
int coap_client_tcp_req(struct coap_client_tcp *client, struct coap_client_tcp_request *req)
Send CoAP request over TCP.
void(* coap_client_tcp_response_cb_t)(const struct coap_client_response_data *data, void *user_data)
Callback for CoAP TCP request.
Definition coap_client_tcp.h:44
coap_client_tcp_event
CoAP TCP client event types for signaling.
Definition coap_client_tcp.h:83
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.
#define MAX_COAP_TCP_MSG_LEN
Maximum size of a CoAP TCP message.
Definition coap_client_tcp.h:34
int coap_client_tcp_ping(struct coap_client_tcp *client)
Send a Ping 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).
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.
Definition coap_client_tcp.h:59
int coap_client_tcp_init(struct coap_client_tcp *client, const char *info)
Initialize the TCP CoAP client.
@ COAP_CLIENT_TCP_EVENT_RELEASE
Server initiated connection release (RFC 8323).
Definition coap_client_tcp.h:89
@ COAP_CLIENT_TCP_EVENT_CSM_UPDATED
CSM capabilities have been updated from server.
Definition coap_client_tcp.h:85
@ COAP_CLIENT_TCP_EVENT_ABORT
Server aborted connection (RFC 8323).
Definition coap_client_tcp.h:91
@ COAP_CLIENT_TCP_EVENT_PONG_RECEIVED
Pong received in response to Ping.
Definition coap_client_tcp.h:87
#define MAX_COAP_CLIENT_OPTION_LEN
Maximum length in bytes for options specified in coap_client_option.
Definition coap_client.h:35
coap_method
Available request methods.
Definition coap.h:81
coap_content_format
Set of Content-Format option values for CoAP.
Definition coap.h:242
uint32_t net_socklen_t
Length of a socket address.
Definition net_ip.h:171
Public kernel APIs.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
__INT64_TYPE__ int64_t
Definition stdint.h:75
FUNC_NORETURN void abort(void)
Represents the current state of a block-wise transaction.
Definition coap.h:829
Representation for CoAP client response data.
Definition coap_client.h:40
const uint8_t * payload
Buffer containing the payload from the response.
Definition coap_client.h:51
bool last_block
Indicates the last block of the response.
Definition coap_client.h:55
size_t offset
Payload offset from the beginning of a blockwise transfer.
Definition coap_client.h:49
Representation of extra options for the CoAP TCP client request.
Definition coap_client_tcp.h:130
uint8_t value[(12)]
Buffer for the length.
Definition coap_client_tcp.h:142
uint16_t code
Option code.
Definition coap_client_tcp.h:132
uint8_t len
Option len.
Definition coap_client_tcp.h:140
Representation of a CoAP TCP client request.
Definition coap_client_tcp.h:154
enum coap_method method
Method of the request.
Definition coap_client_tcp.h:155
char path[MAX_TCP_PATH_SIZE]
Path of the requested resource.
Definition coap_client_tcp.h:156
coap_client_tcp_response_cb_t cb
Callback when response received.
Definition coap_client_tcp.h:161
size_t len
Length of the payload.
Definition coap_client_tcp.h:159
struct coap_client_tcp_option options[MAX_TCP_EXTRA_OPTIONS]
Extra options to be added to request.
Definition coap_client_tcp.h:162
uint8_t num_options
Number of extra options.
Definition coap_client_tcp.h:164
const uint8_t * payload
User allocated buffer for send request.
Definition coap_client_tcp.h:158
void * user_data
User provided context.
Definition coap_client_tcp.h:165
coap_client_tcp_payload_cb_t payload_cb
Optional payload callback.
Definition coap_client_tcp.h:160
enum coap_content_format fmt
Content format to be used.
Definition coap_client_tcp.h:157
Representation of a CoAP Packet.
Definition coap.h:342
Generic sockaddr struct.
Definition net_ip.h:448
Event data for CoAP TCP signaling events.
Definition coap_client_tcp.h:97
uint32_t hold_off_sec
Hold-off time in seconds before reconnecting.
Definition coap_client_tcp.h:103
struct coap_client_tcp_event_data::@247245245366330053270216334112005332353215175024 release
Data for RELEASE event.
uint16_t bad_csm_option
Bad CSM option code that caused abort (0 if not provided).
Definition coap_client_tcp.h:108
const char * alt_addr
Alternative address to reconnect (NULL if not provided).
Definition coap_client_tcp.h:101