Zephyr API Documentation 3.7.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
coap_client.h
Go to the documentation of this file.
1
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
24#include <zephyr/net/coap.h>
25#include <zephyr/kernel.h>
26
28#define MAX_COAP_MSG_LEN (CONFIG_COAP_CLIENT_MESSAGE_HEADER_SIZE + \
29 CONFIG_COAP_CLIENT_MESSAGE_SIZE)
30
48typedef void (*coap_client_response_cb_t)(int16_t result_code,
49 size_t offset, const uint8_t *payload, size_t len,
50 bool last_block, void *user_data);
51
67
74#if defined(CONFIG_COAP_EXTENDED_OPTIONS_LEN)
78 uint8_t value[CONFIG_COAP_EXTENDED_OPTIONS_LEN_VALUE];
79#else
84#endif
85};
86
88struct coap_client_internal_request {
89 uint8_t request_token[COAP_TOKEN_MAX_LEN];
90 uint32_t offset;
91 uint32_t last_id;
92 uint8_t request_tkl;
93 bool request_ongoing;
94 atomic_t in_callback;
95 struct coap_block_context recv_blk_ctx;
96 struct coap_block_context send_blk_ctx;
97 struct coap_pending pending;
98 struct coap_client_request coap_request;
99 struct coap_packet request;
100 uint8_t request_tag[COAP_TOKEN_MAX_LEN];
101
102 /* For GETs with observe option set */
103 bool is_observe;
104 int last_response_id;
105};
106
107struct coap_client {
108 int fd;
109 struct sockaddr address;
110 socklen_t socklen;
111 bool response_ready;
112 struct k_mutex lock;
113 uint8_t send_buf[MAX_COAP_MSG_LEN];
114 uint8_t recv_buf[MAX_COAP_MSG_LEN];
115 struct coap_client_internal_request requests[CONFIG_COAP_CLIENT_MAX_REQUESTS];
116 struct coap_option echo_option;
117 bool send_echo;
118};
130int coap_client_init(struct coap_client *client, const char *info);
131
149int coap_client_req(struct coap_client *client, int sock, const struct sockaddr *addr,
150 struct coap_client_request *req, struct coap_transmission_parameters *params);
151
160void coap_client_cancel_requests(struct coap_client *client);
161
174{
175 struct coap_client_option block2 = {
176 .code = COAP_OPTION_BLOCK2,
177 .len = 1,
178 .value[0] = coap_bytes_to_block_size(CONFIG_COAP_CLIENT_BLOCK_SIZE),
179 };
180
181 return block2;
182}
183
188#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)(int16_t result_code, size_t offset, const uint8_t *payload, size_t len, bool last_block, void *user_data)
Callback for CoAP request.
Definition coap_client.h:48
void coap_client_cancel_requests(struct coap_client *client)
Cancel all current requests.
#define MAX_COAP_MSG_LEN
Maximum size of a CoAP message.
Definition coap_client.h:28
static struct coap_client_option coap_client_option_initial_block2(void)
Initialise a Block2 option to be added to a request.
Definition coap_client.h:173
int coap_client_init(struct coap_client *client, const char *info)
Initialize the CoAP client.
static enum coap_block_size coap_bytes_to_block_size(uint16_t bytes)
Helper for converting block size in bytes to enumeration.
Definition coap.h:734
coap_method
Available request methods.
Definition coap.h:75
coap_content_format
Set of Content-Format option values for CoAP.
Definition coap.h:213
@ COAP_OPTION_BLOCK2
Block2 (RFC 7959)
Definition coap.h:60
size_t socklen_t
Length of a socket address.
Definition net_ip.h:170
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:750
Representation of extra options for the CoAP client request.
Definition coap_client.h:71
uint16_t code
Option code.
Definition coap_client.h:73
uint8_t value[12]
Buffer for the length.
Definition coap_client.h:83
uint8_t len
Option len.
Definition coap_client.h:81
Representation of a CoAP client request.
Definition coap_client.h:55
uint8_t num_options
Number of extra options.
Definition coap_client.h:64
size_t len
Length of the payload.
Definition coap_client.h:61
enum coap_method method
Method of the request.
Definition coap_client.h:56
bool confirmable
CoAP Confirmable/Non-confirmable message.
Definition coap_client.h:57
const char * path
Path of the requested resource.
Definition coap_client.h:58
struct coap_client_option * options
Extra options to be added to request.
Definition coap_client.h:63
void * user_data
User provided context.
Definition coap_client.h:65
enum coap_content_format fmt
Content format to be used.
Definition coap_client.h:59
uint8_t * payload
User allocated buffer for send request.
Definition coap_client.h:60
coap_client_response_cb_t cb
Callback when response received.
Definition coap_client.h:62
Representation of a CoAP option.
Definition coap.h:314
Representation of a CoAP Packet.
Definition coap.h:295
Represents a request awaiting for an acknowledgment (ACK).
Definition coap.h:352
CoAP transmission parameters.
Definition coap.h:340
Mutex Structure.
Definition kernel.h:2994
Generic sockaddr struct.
Definition net_ip.h:387