Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
coap.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Intel Corporation
3 * Copyright (c) 2021 Nordic Semiconductor
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
14#ifndef ZEPHYR_INCLUDE_NET_COAP_H_
15#define ZEPHYR_INCLUDE_NET_COAP_H_
16
24#include <zephyr/types.h>
25#include <stddef.h>
26#include <stdbool.h>
27#include <net/net_ip.h>
28
29#include <sys/slist.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
64};
65
76};
77
78#define COAP_REQUEST_MASK 0x07
79
80#define COAP_VERSION_1 1U
81
114
115#define coap_make_response_code(class, det) ((class << 5) | (det))
116
150
151#define COAP_CODE_EMPTY (0)
152
153#define COAP_TOKEN_MAX_LEN 8UL
154
161 COAP_CONTENT_FORMAT_TEXT_PLAIN = 0, /* charset=urf-8 */
168};
169
170/* block option helper */
171#define GET_BLOCK_NUM(v) ((v) >> 4)
172#define GET_BLOCK_SIZE(v) (((v) & 0x7))
173#define GET_MORE(v) (!!((v) & 0x08))
174
175struct coap_observer;
176struct coap_packet;
177struct coap_pending;
178struct coap_reply;
179struct coap_resource;
180
186typedef int (*coap_method_t)(struct coap_resource *resource,
187 struct coap_packet *request,
188 struct sockaddr *addr, socklen_t addr_len);
189
195typedef void (*coap_notify_t)(struct coap_resource *resource,
196 struct coap_observer *observer);
197
208 const char * const *path;
211 int age;
212};
213
222};
223
228 uint8_t *data; /* User allocated buffer */
229 uint16_t offset; /* CoAP lib maintains offset while adding data */
230 uint16_t max_len; /* Max CoAP packet data length */
231 uint8_t hdr_len; /* CoAP header length */
232 uint16_t opt_len; /* Total options length (delta + len + value) */
233 uint16_t delta; /* Used for delta calculation in CoAP packet */
234#if defined(CONFIG_COAP_KEEP_USER_DATA)
235 void *user_data; /* Application specific user data */
236#endif
237};
238
241#if defined(CONFIG_COAP_EXTENDED_OPTIONS_LEN)
243 uint8_t value[CONFIG_COAP_EXTENDED_OPTIONS_LEN_VALUE];
244#else
247#endif
248};
249
255typedef int (*coap_reply_t)(const struct coap_packet *response,
256 struct coap_reply *reply,
257 const struct sockaddr *from);
258
259#define COAP_DEFAULT_MAX_RETRANSMIT 4
260#define COAP_DEFAULT_ACK_RANDOM_FACTOR 1.5
261
273};
274
282 int age;
286};
287
296
305
316
325
334
345 uint16_t *len);
346
361 struct coap_option *options, uint8_t opt_num);
362
379 uint8_t ver, uint8_t type, uint8_t token_len,
380 const uint8_t *token, uint8_t code, uint16_t id);
381
397int coap_ack_init(struct coap_packet *cpkt, const struct coap_packet *req,
398 uint8_t *data, uint16_t max_len, uint8_t code);
399
407
414
428int coap_find_options(const struct coap_packet *cpkt, uint16_t code,
429 struct coap_option *options, uint16_t veclen);
430
447 const uint8_t *value, uint16_t len);
448
460unsigned int coap_option_value_to_int(const struct coap_option *option);
461
475 unsigned int val);
476
485
495int coap_packet_append_payload(struct coap_packet *cpkt, const uint8_t *payload,
496 uint16_t payload_len);
497
512 struct coap_resource *resources,
513 struct coap_option *options,
514 uint8_t opt_num,
515 struct sockaddr *addr, socklen_t addr_len);
516
533};
534
544 enum coap_block_size block_size)
545{
546 return (1 << (block_size + 4));
547}
548
554 size_t current;
556};
557
568 enum coap_block_size block_size,
569 size_t total_size);
570
581 struct coap_block_context *ctx);
582
593 struct coap_block_context *ctx);
594
605 struct coap_block_context *ctx);
606
617 struct coap_block_context *ctx);
618
628int coap_get_option_int(const struct coap_packet *cpkt, uint16_t code);
629
639int coap_update_from_block(const struct coap_packet *cpkt,
640 struct coap_block_context *ctx);
641
653size_t coap_next_block(const struct coap_packet *cpkt,
654 struct coap_block_context *ctx);
655
664void coap_observer_init(struct coap_observer *observer,
665 const struct coap_packet *request,
666 const struct sockaddr *addr);
667
678 struct coap_observer *observer);
679
688 struct coap_observer *observer);
689
701 struct coap_observer *observers, size_t len,
702 const struct sockaddr *addr);
703
714 struct coap_observer *observers, size_t len);
715
722void coap_reply_init(struct coap_reply *reply,
723 const struct coap_packet *request);
724
740int coap_pending_init(struct coap_pending *pending,
741 const struct coap_packet *request,
742 const struct sockaddr *addr,
743 uint8_t retries);
744
756 struct coap_pending *pendings, size_t len);
757
769 struct coap_reply *replies, size_t len);
770
785 const struct coap_packet *response,
786 struct coap_pending *pendings, size_t len);
787
801 const struct coap_packet *response,
802 const struct sockaddr *from,
803 struct coap_reply *replies, size_t len);
804
816 struct coap_pending *pendings, size_t len);
817
826bool coap_pending_cycle(struct coap_pending *pending);
827
834void coap_pending_clear(struct coap_pending *pending);
835
843void coap_pendings_clear(struct coap_pending *pendings, size_t len);
844
851void coap_reply_clear(struct coap_reply *reply);
852
859void coap_replies_clear(struct coap_reply *replies, size_t len);
860
870
879bool coap_request_is_observe(const struct coap_packet *request);
880
881#ifdef __cplusplus
882}
883#endif
884
889#endif /* ZEPHYR_INCLUDE_NET_COAP_H_ */
void
Definition: eswifi_shell.c:15
void coap_pending_clear(struct coap_pending *pending)
Cancels the pending retransmission, so it again becomes available.
size_t coap_next_block(const struct coap_packet *cpkt, struct coap_block_context *ctx)
Updates ctx so after this is called the current entry indicates the correct offset in the body of dat...
coap_response_code
Set of response codes available for a response packet.
Definition: coap.h:122
int coap_get_option_int(const struct coap_packet *cpkt, uint16_t code)
Get the integer representation of a CoAP option.
int coap_packet_append_payload_marker(struct coap_packet *cpkt)
Append payload marker to CoAP packet.
struct coap_observer * coap_observer_next_unused(struct coap_observer *observers, size_t len)
Returns the next available observer representation.
int coap_packet_parse(struct coap_packet *cpkt, uint8_t *data, uint16_t len, struct coap_option *options, uint8_t opt_num)
Parses the CoAP packet in data, validating it and initializing cpkt. data must remain valid while cpk...
const uint8_t * coap_packet_get_payload(const struct coap_packet *cpkt, uint16_t *len)
Returns the data pointer and length of the CoAP packet.
int coap_packet_append_option(struct coap_packet *cpkt, uint16_t code, const uint8_t *value, uint16_t len)
Appends an option to the packet.
bool coap_pending_cycle(struct coap_pending *pending)
After a request is sent, user may want to cycle the pending retransmission so the timeout is updated.
unsigned int coap_option_value_to_int(const struct coap_option *option)
Converts an option to its integer representation.
int coap_append_block2_option(struct coap_packet *cpkt, struct coap_block_context *ctx)
Append BLOCK2 option to the packet.
void coap_reply_clear(struct coap_reply *reply)
Cancels awaiting for this reply, so it becomes available again. User responsibility to free the memor...
int coap_update_from_block(const struct coap_packet *cpkt, struct coap_block_context *ctx)
Retrieves BLOCK{1,2} and SIZE{1,2} from cpkt and updates ctx accordingly.
coap_msgtype
CoAP packets may be of one of these types.
Definition: coap.h:85
bool coap_register_observer(struct coap_resource *resource, struct coap_observer *observer)
After the observer is initialized, associate the observer with an resource.
void coap_observer_init(struct coap_observer *observer, const struct coap_packet *request, const struct sockaddr *addr)
Indicates that the remote device referenced by addr, with request, wants to observe a resource.
struct coap_reply * coap_response_received(const struct coap_packet *response, const struct sockaddr *from, struct coap_reply *replies, size_t len)
After a response is received, call coap_reply_t handler registered in coap_reply structure.
int coap_append_size1_option(struct coap_packet *cpkt, struct coap_block_context *ctx)
Append SIZE1 option to the packet.
struct coap_observer * coap_find_observer_by_addr(struct coap_observer *observers, size_t len, const struct sockaddr *addr)
Returns the observer that matches address addr.
bool coap_request_is_observe(const struct coap_packet *request)
Returns if this request is enabling observing a resource.
int(* coap_reply_t)(const struct coap_packet *response, struct coap_reply *reply, const struct sockaddr *from)
Helper function to be called when a response matches the a pending request.
Definition: coap.h:255
int coap_append_block1_option(struct coap_packet *cpkt, struct coap_block_context *ctx)
Append BLOCK1 option to the packet.
int coap_block_transfer_init(struct coap_block_context *ctx, enum coap_block_size block_size, size_t total_size)
Initializes the context of a block-wise transfer.
uint16_t coap_header_get_id(const struct coap_packet *cpkt)
Returns the message id associated with the CoAP packet.
#define coap_make_response_code(class, det)
Definition: coap.h:115
struct coap_reply * coap_reply_next_unused(struct coap_reply *replies, size_t len)
Returns the next available reply struct, so it can be used to track replies and notifications receive...
uint8_t * coap_next_token(void)
Returns a randomly generated array of 8 bytes, that can be used as a message's token.
void coap_remove_observer(struct coap_resource *resource, struct coap_observer *observer)
Remove this observer from the list of registered observers of that resource.
uint8_t coap_header_get_token(const struct coap_packet *cpkt, uint8_t *token)
Returns the token (if any) in the CoAP packet.
coap_method
Available request methods.
Definition: coap.h:71
int coap_append_option_int(struct coap_packet *cpkt, uint16_t code, unsigned int val)
Appends an integer value option to the packet.
void coap_pendings_clear(struct coap_pending *pendings, size_t len)
Cancels all pending retransmissions, so they become available again.
coap_block_size
Definition: coap.h:525
int(* coap_method_t)(struct coap_resource *resource, struct coap_packet *request, struct sockaddr *addr, socklen_t addr_len)
Type of the callback being called when a resource's method is invoked by the remote entity.
Definition: coap.h:186
coap_option_num
Set of CoAP packet options we are aware of.
Definition: coap.h:44
struct coap_pending * coap_pending_next_unused(struct coap_pending *pendings, size_t len)
Returns the next available pending struct, that can be used to track the retransmission status of a r...
int coap_handle_request(struct coap_packet *cpkt, struct coap_resource *resources, struct coap_option *options, uint8_t opt_num, struct sockaddr *addr, socklen_t addr_len)
When a request is received, call the appropriate methods of the matching resources.
int coap_packet_init(struct coap_packet *cpkt, uint8_t *data, uint16_t max_len, uint8_t ver, uint8_t type, uint8_t token_len, const uint8_t *token, uint8_t code, uint16_t id)
Creates a new CoAP Packet from input data.
coap_content_format
Set of Content-Format option values for CoAP.
Definition: coap.h:160
struct coap_pending * coap_pending_received(const struct coap_packet *response, struct coap_pending *pendings, size_t len)
After a response is received, returns if there is any matching pending request exits....
struct coap_pending * coap_pending_next_to_expire(struct coap_pending *pendings, size_t len)
Returns the next pending about to expire, pending->timeout informs how many ms to next expiration.
int coap_pending_init(struct coap_pending *pending, const struct coap_packet *request, const struct sockaddr *addr, uint8_t retries)
Initialize a pending request with a request.
void(* coap_notify_t)(struct coap_resource *resource, struct coap_observer *observer)
Type of the callback being called when a resource's has observers to be informed when an update happe...
Definition: coap.h:195
void coap_reply_init(struct coap_reply *reply, const struct coap_packet *request)
Indicates that a reply is expected for request.
int coap_resource_notify(struct coap_resource *resource)
Indicates that this resource was updated and that the notify callback should be called for every regi...
int coap_packet_append_payload(struct coap_packet *cpkt, const uint8_t *payload, uint16_t payload_len)
Append payload to CoAP packet.
void coap_replies_clear(struct coap_reply *replies, size_t len)
Cancels all replies, so they become available again.
uint16_t coap_next_id(void)
Helper to generate message ids.
uint8_t coap_header_get_code(const struct coap_packet *cpkt)
Returns the code of the CoAP packet.
int coap_ack_init(struct coap_packet *cpkt, const struct coap_packet *req, uint8_t *data, uint16_t max_len, uint8_t code)
Create a new CoAP Acknowledgment message for given request.
uint8_t coap_header_get_type(const struct coap_packet *cpkt)
Returns the type of the CoAP packet.
int coap_find_options(const struct coap_packet *cpkt, uint16_t code, struct coap_option *options, uint16_t veclen)
Return the values associated with the option of value code.
int coap_append_size2_option(struct coap_packet *cpkt, struct coap_block_context *ctx)
Append SIZE2 option to the packet.
uint8_t coap_header_get_version(const struct coap_packet *cpkt)
Returns the version present in a CoAP packet.
static uint16_t coap_block_size_to_bytes(enum coap_block_size block_size)
Helper for converting the enumeration to the size expressed in bytes.
Definition: coap.h:543
@ COAP_RESPONSE_CODE_OK
Definition: coap.h:123
@ COAP_RESPONSE_CODE_INTERNAL_ERROR
Definition: coap.h:142
@ COAP_RESPONSE_CODE_PRECONDITION_FAILED
Definition: coap.h:138
@ COAP_RESPONSE_CODE_NOT_ALLOWED
Definition: coap.h:135
@ COAP_RESPONSE_CODE_CHANGED
Definition: coap.h:127
@ COAP_RESPONSE_CODE_NOT_ACCEPTABLE
Definition: coap.h:136
@ COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED
Definition: coap.h:147
@ COAP_RESPONSE_CODE_BAD_REQUEST
Definition: coap.h:130
@ COAP_RESPONSE_CODE_INCOMPLETE
Definition: coap.h:137
@ COAP_RESPONSE_CODE_NOT_IMPLEMENTED
Definition: coap.h:143
@ COAP_RESPONSE_CODE_NOT_FOUND
Definition: coap.h:134
@ COAP_RESPONSE_CODE_BAD_GATEWAY
Definition: coap.h:144
@ COAP_RESPONSE_CODE_BAD_OPTION
Definition: coap.h:132
@ COAP_RESPONSE_CODE_REQUEST_TOO_LARGE
Definition: coap.h:139
@ COAP_RESPONSE_CODE_DELETED
Definition: coap.h:125
@ COAP_RESPONSE_CODE_UNAUTHORIZED
Definition: coap.h:131
@ COAP_RESPONSE_CODE_CREATED
Definition: coap.h:124
@ COAP_RESPONSE_CODE_CONTENT
Definition: coap.h:128
@ COAP_RESPONSE_CODE_CONTINUE
Definition: coap.h:129
@ COAP_RESPONSE_CODE_VALID
Definition: coap.h:126
@ COAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT
Definition: coap.h:140
@ COAP_RESPONSE_CODE_GATEWAY_TIMEOUT
Definition: coap.h:146
@ COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE
Definition: coap.h:145
@ COAP_RESPONSE_CODE_FORBIDDEN
Definition: coap.h:133
@ COAP_TYPE_RESET
Definition: coap.h:112
@ COAP_TYPE_NON_CON
Definition: coap.h:99
@ COAP_TYPE_CON
Definition: coap.h:92
@ COAP_TYPE_ACK
Definition: coap.h:105
@ COAP_METHOD_GET
Definition: coap.h:72
@ COAP_METHOD_PUT
Definition: coap.h:74
@ COAP_METHOD_POST
Definition: coap.h:73
@ COAP_METHOD_DELETE
Definition: coap.h:75
@ COAP_BLOCK_128
Definition: coap.h:529
@ COAP_BLOCK_32
Definition: coap.h:527
@ COAP_BLOCK_1024
Definition: coap.h:532
@ COAP_BLOCK_64
Definition: coap.h:528
@ COAP_BLOCK_16
Definition: coap.h:526
@ COAP_BLOCK_256
Definition: coap.h:530
@ COAP_BLOCK_512
Definition: coap.h:531
@ COAP_OPTION_OBSERVE
Definition: coap.h:49
@ COAP_OPTION_IF_NONE_MATCH
Definition: coap.h:48
@ COAP_OPTION_ETAG
Definition: coap.h:47
@ COAP_OPTION_SIZE2
Definition: coap.h:60
@ COAP_OPTION_PROXY_SCHEME
Definition: coap.h:62
@ COAP_OPTION_URI_PORT
Definition: coap.h:50
@ COAP_OPTION_URI_HOST
Definition: coap.h:46
@ COAP_OPTION_BLOCK2
Definition: coap.h:58
@ COAP_OPTION_IF_MATCH
Definition: coap.h:45
@ COAP_OPTION_SIZE1
Definition: coap.h:63
@ COAP_OPTION_BLOCK1
Definition: coap.h:59
@ COAP_OPTION_URI_PATH
Definition: coap.h:52
@ COAP_OPTION_MAX_AGE
Definition: coap.h:54
@ COAP_OPTION_CONTENT_FORMAT
Definition: coap.h:53
@ COAP_OPTION_LOCATION_QUERY
Definition: coap.h:57
@ COAP_OPTION_URI_QUERY
Definition: coap.h:55
@ COAP_OPTION_PROXY_URI
Definition: coap.h:61
@ COAP_OPTION_LOCATION_PATH
Definition: coap.h:51
@ COAP_OPTION_ACCEPT
Definition: coap.h:56
@ COAP_CONTENT_FORMAT_APP_LINK_FORMAT
Definition: coap.h:162
@ COAP_CONTENT_FORMAT_APP_EXI
Definition: coap.h:165
@ COAP_CONTENT_FORMAT_APP_CBOR
Definition: coap.h:167
@ COAP_CONTENT_FORMAT_APP_OCTET_STREAM
Definition: coap.h:164
@ COAP_CONTENT_FORMAT_APP_JSON
Definition: coap.h:166
@ COAP_CONTENT_FORMAT_APP_XML
Definition: coap.h:163
@ COAP_CONTENT_FORMAT_TEXT_PLAIN
Definition: coap.h:161
size_t socklen_t
Definition: net_ip.h:161
IPv6 and IPv4 definitions.
Single-linked list implementation.
struct _slist sys_slist_t
Definition: slist.h:40
struct _snode sys_snode_t
Definition: slist.h:33
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
Represents the current state of a block-wise transaction.
Definition: coap.h:552
size_t total_size
Definition: coap.h:553
enum coap_block_size block_size
Definition: coap.h:555
size_t current
Definition: coap.h:554
Represents a remote device that is observing a local resource.
Definition: coap.h:217
sys_snode_t list
Definition: coap.h:218
uint8_t token[8]
Definition: coap.h:220
uint8_t tkl
Definition: coap.h:221
struct sockaddr addr
Definition: coap.h:219
Definition: coap.h:239
uint8_t len
Definition: coap.h:245
uint16_t delta
Definition: coap.h:240
uint8_t value[12]
Definition: coap.h:246
Representation of a CoAP Packet.
Definition: coap.h:227
uint16_t offset
Definition: coap.h:229
uint16_t max_len
Definition: coap.h:230
uint8_t * data
Definition: coap.h:228
uint16_t delta
Definition: coap.h:233
uint8_t hdr_len
Definition: coap.h:231
uint16_t opt_len
Definition: coap.h:232
Represents a request awaiting for an acknowledgment (ACK).
Definition: coap.h:265
uint8_t * data
Definition: coap.h:270
uint16_t len
Definition: coap.h:271
uint32_t t0
Definition: coap.h:267
uint8_t retries
Definition: coap.h:272
uint16_t id
Definition: coap.h:269
uint32_t timeout
Definition: coap.h:268
struct sockaddr addr
Definition: coap.h:266
Represents the handler for the reply of a request, it is also used when observing resources.
Definition: coap.h:279
uint8_t tkl
Definition: coap.h:285
uint8_t token[8]
Definition: coap.h:284
coap_reply_t reply
Definition: coap.h:280
uint16_t id
Definition: coap.h:283
void * user_data
Definition: coap.h:281
int age
Definition: coap.h:282
Description of CoAP resource.
Definition: coap.h:204
coap_method_t post
Definition: coap.h:206
void * user_data
Definition: coap.h:209
coap_method_t put
Definition: coap.h:206
coap_notify_t notify
Definition: coap.h:207
sys_slist_t observers
Definition: coap.h:210
coap_method_t del
Definition: coap.h:206
const char *const * path
Definition: coap.h:208
int age
Definition: coap.h:211
coap_method_t get
Definition: coap.h:206
Definition: net_ip.h:335
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
static const intptr_t user_data[5]
Definition: main.c:590