Zephyr API Documentation 4.3.0-rc1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
coap_client.h
Go to the documentation of this file.
1
6
7/*
8 * Copyright (c) 2023 Nordic Semiconductor ASA
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
12#ifndef ZEPHYR_INCLUDE_NET_COAP_CLIENT_H_
13#define ZEPHYR_INCLUDE_NET_COAP_CLIENT_H_
14
23
24#include <zephyr/net/coap.h>
25#include <zephyr/kernel.h>
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
32#define MAX_COAP_MSG_LEN (CONFIG_COAP_CLIENT_MESSAGE_HEADER_SIZE + \
33 CONFIG_COAP_CLIENT_MESSAGE_SIZE)
34
55
68typedef void (*coap_client_response_cb_t)(const struct coap_client_response_data *data,
69 void *user_data);
70
98typedef int (*coap_client_payload_cb_t)(size_t offset, const uint8_t **payload,
99 size_t *len, bool *last_block,
100 void *user_data);
101
108#if defined(CONFIG_COAP_EXTENDED_OPTIONS_LEN)
112 uint8_t value[CONFIG_COAP_EXTENDED_OPTIONS_LEN_VALUE];
113#else
118#endif
119};
120
122#define MAX_PATH_SIZE (CONFIG_COAP_CLIENT_MAX_PATH_LENGTH + 1)
123#define MAX_EXTRA_OPTIONS CONFIG_COAP_CLIENT_MAX_EXTRA_OPTIONS
125
143
145struct coap_client_internal_request {
146 uint8_t request_token[COAP_TOKEN_MAX_LEN];
147 uint32_t offset;
148 uint16_t last_id;
149 uint8_t request_tkl;
150 bool request_ongoing;
151 atomic_t in_callback;
152 struct coap_block_context recv_blk_ctx;
153 struct coap_block_context send_blk_ctx;
154 struct coap_pending pending;
155 struct coap_client_request coap_request;
156 struct coap_packet request;
157 uint8_t request_tag[COAP_TOKEN_MAX_LEN];
158 uint8_t send_buf[MAX_COAP_MSG_LEN];
159
160 /* For GETs with observe option set */
161 bool is_observe;
162 int last_response_id;
163};
164
165struct coap_client {
166 int fd;
167 struct sockaddr address;
168 socklen_t socklen;
169 struct k_mutex lock;
170 uint8_t recv_buf[MAX_COAP_MSG_LEN];
171 struct coap_client_internal_request requests[CONFIG_COAP_CLIENT_MAX_REQUESTS];
172 struct coap_option echo_option;
173 bool send_echo;
174};
176
186int coap_client_init(struct coap_client *client, const char *info);
187
208
209int coap_client_req(struct coap_client *client, int sock, const struct sockaddr *addr,
210 struct coap_client_request *req, struct coap_transmission_parameters *params);
211
222void coap_client_cancel_requests(struct coap_client *client);
223
237void coap_client_cancel_request(struct coap_client *client, struct coap_client_request *req);
238
251
267bool coap_client_has_ongoing_exchange(struct coap_client *client);
268
269#ifdef __cplusplus
270}
271#endif
272
276
277#endif /* ZEPHYR_INCLUDE_NET_COAP_CLIENT_H_ */
long atomic_t
Definition atomic_types.h:15
CoAP implementation for Zephyr.
int coap_client_req(struct coap_client *client, int sock, const struct sockaddr *addr, struct coap_client_request *req, struct coap_transmission_parameters *params)
Send CoAP request.
void(* coap_client_response_cb_t)(const struct coap_client_response_data *data, void *user_data)
Callback for CoAP request.
Definition coap_client.h:68
int(* coap_client_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 request.
Definition coap_client.h:98
void coap_client_cancel_requests(struct coap_client *client)
Cancel all current requests.
struct coap_client_option coap_client_option_initial_block2(void)
Initialise a Block2 option to be added to a request.
#define MAX_COAP_MSG_LEN
Maximum size of a CoAP message.
Definition coap_client.h:32
void coap_client_cancel_request(struct coap_client *client, struct coap_client_request *req)
Cancel matching requests.
bool coap_client_has_ongoing_exchange(struct coap_client *client)
Check if client has ongoing exchange.
int coap_client_init(struct coap_client *client, const char *info)
Initialize the CoAP client.
coap_method
Available request methods.
Definition coap.h:76
coap_content_format
Set of Content-Format option values for CoAP.
Definition coap.h:215
uint32_t socklen_t
Length of a socket address.
Definition net_ip.h:172
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
__INT16_TYPE__ int16_t
Definition stdint.h:73
Represents the current state of a block-wise transaction.
Definition coap.h:790
Representation of extra options for the CoAP client request.
Definition coap_client.h:105
uint16_t code
Option code.
Definition coap_client.h:107
uint8_t value[12]
Buffer for the length.
Definition coap_client.h:117
uint8_t len
Option len.
Definition coap_client.h:115
Representation of a CoAP client request.
Definition coap_client.h:129
uint8_t num_options
Number of extra options.
Definition coap_client.h:140
size_t len
Length of the payload.
Definition coap_client.h:135
coap_client_payload_cb_t payload_cb
Optional payload callback.
Definition coap_client.h:136
enum coap_method method
Method of the request.
Definition coap_client.h:130
struct coap_client_option options[MAX_EXTRA_OPTIONS]
Extra options to be added to request.
Definition coap_client.h:138
bool confirmable
CoAP Confirmable/Non-confirmable message.
Definition coap_client.h:131
char path[MAX_PATH_SIZE]
Path of the requested resource.
Definition coap_client.h:132
const uint8_t * payload
User allocated buffer for send request.
Definition coap_client.h:134
void * user_data
User provided context.
Definition coap_client.h:141
enum coap_content_format fmt
Content format to be used.
Definition coap_client.h:133
coap_client_response_cb_t cb
Callback when response received.
Definition coap_client.h:137
Representation for CoAP client response data.
Definition coap_client.h:38
const uint8_t * payload
Buffer containing the payload from the response.
Definition coap_client.h:49
int16_t result_code
Result code of the response.
Definition coap_client.h:43
const struct coap_packet * packet
A pointer to the response CoAP packet.
Definition coap_client.h:45
bool last_block
Indicates the last block of the response.
Definition coap_client.h:53
size_t payload_len
Size of the payload.
Definition coap_client.h:51
size_t offset
Payload offset from the beginning of a blockwise transfer.
Definition coap_client.h:47
Representation of a CoAP Packet.
Definition coap.h:313
Represents a request awaiting for an acknowledgment (ACK).
Definition coap.h:377
CoAP transmission parameters.
Definition coap.h:358
Generic sockaddr struct.
Definition net_ip.h:410