libcoap 4.3.5-develop-4c3f4af
Loading...
Searching...
No Matches
coap_pdu.h
Go to the documentation of this file.
1/*
2 * coap_pdu.h -- CoAP message structure
3 *
4 * Copyright (C) 2010-2014 Olaf Bergmann <bergmann@tzi.org>
5 * Copyright (C) 2021-2026 Jon Shallow <supjps-libcoap@jpshallow.com>
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 *
9 * This file is part of the CoAP library libcoap. Please see README for terms
10 * of use.
11 */
12
17
18#ifndef COAP_PDU_H_
19#define COAP_PDU_H_
20
21#include "coap_option.h"
22#include "coap_uri.h"
23
24#ifdef WITH_LWIP
25#include <lwip/pbuf.h>
26#endif
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
38
39#define COAP_DEFAULT_PORT 5683 /* CoAP default UDP/TCP port */
40#define COAPS_DEFAULT_PORT 5684 /* CoAP default UDP/TCP port for secure transmission */
41#define COAP_DEFAULT_MAX_AGE 60 /* default maximum object lifetime in seconds */
42#ifndef COAP_DEFAULT_MTU
43#define COAP_DEFAULT_MTU 1152
44#endif /* COAP_DEFAULT_MTU */
45
46#define COAP_BERT_BASE 1152
47
48#ifndef COAP_DEFAULT_HOP_LIMIT
49#define COAP_DEFAULT_HOP_LIMIT 16
50#endif /* COAP_DEFAULT_HOP_LIMIT */
51
52#define COAP_DEFAULT_SCHEME "coap" /* the default scheme for CoAP URIs */
53
55#define COAP_DEFAULT_URI_WELLKNOWN ".well-known/core"
56
57/* Extended Token constants */
58#define COAP_TOKEN_DEFAULT_MAX 8
59#if (UINT_MAX > 65804UL)
60#define COAP_TOKEN_EXT_MAX 65804UL /* 13 + 256 + 65535 */
61#else /* UINT_MAX < 65804UL */
62#define COAP_TOKEN_EXT_MAX 4096
63#endif /* UINT_MAX < 65804UL */
64
65/* CoAP message types */
66
70typedef enum coap_pdu_type_t {
71 COAP_MESSAGE_CON, /* 0 confirmable message (requires ACK/RST) */
72 COAP_MESSAGE_NON, /* 1 non-confirmable message (one-shot message) */
73 COAP_MESSAGE_ACK, /* 2 used to acknowledge confirmable messages */
74 COAP_MESSAGE_RST /* 3 indicates error in received messages */
76
89
90/* CoAP result codes (HTTP-Code / 100 * 40 + HTTP-Code % 100) */
91
92/* As of draft-ietf-core-coap-04, response codes are encoded to base
93 * 32, i.e. the three upper bits determine the response class while
94 * the remaining five fine-grained information specific to that class.
95 */
96#define COAP_RESPONSE_CODE(N) (((N)/100 << 5) | (N)%100)
97
98/* Determines the class of response code C */
99#define COAP_RESPONSE_CLASS(C) (((C) >> 5) & 0xFF)
100
101#ifndef SHORT_ERROR_RESPONSE
112const char *coap_response_phrase(unsigned char code);
113
114#define COAP_ERROR_PHRASE_LENGTH 32
115
116#else
117#define coap_response_phrase(x) ((char *)NULL)
118
119#define COAP_ERROR_PHRASE_LENGTH 0
120#endif /* SHORT_ERROR_RESPONSE */
121
122#define COAP_SIGNALING_CODE(N) (((N)/100 << 5) | (N)%100)
123
131
132/* CoAP media type encoding */
133
134#define COAP_MEDIATYPE_TEXT_PLAIN 0 /* text/plain (UTF-8) */
135#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT 40 /* application/link-format */
136#define COAP_MEDIATYPE_APPLICATION_XML 41 /* application/xml */
137#define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM 42 /* application/octet-stream */
138#define COAP_MEDIATYPE_APPLICATION_RDF_XML 43 /* application/rdf+xml */
139#define COAP_MEDIATYPE_APPLICATION_EXI 47 /* application/exi */
140#define COAP_MEDIATYPE_APPLICATION_JSON 50 /* application/json */
141#define COAP_MEDIATYPE_APPLICATION_CBOR 60 /* application/cbor */
142#define COAP_MEDIATYPE_APPLICATION_CWT 61 /* application/cwt, RFC 8392 */
143
144/* Content formats from RFC 7390 */
145#define COAP_MEDIATYPE_APPLICATION_COAP_GROUP_JSON 256 /* application/coap-group+json */
146
147/* Content formats from RFC 8152 */
148#define COAP_MEDIATYPE_APPLICATION_COSE_SIGN 98 /* application/cose; cose-type="cose-sign" */
149#define COAP_MEDIATYPE_APPLICATION_COSE_SIGN1 18 /* application/cose; cose-type="cose-sign1" */
150#define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT 96 /* application/cose; cose-type="cose-encrypt" */
151#define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT0 16 /* application/cose; cose-type="cose-encrypt0" */
152#define COAP_MEDIATYPE_APPLICATION_COSE_MAC 97 /* application/cose; cose-type="cose-mac" */
153#define COAP_MEDIATYPE_APPLICATION_COSE_MAC0 17 /* application/cose; cose-type="cose-mac0" */
154
155#define COAP_MEDIATYPE_APPLICATION_COSE_KEY 101 /* application/cose-key */
156#define COAP_MEDIATYPE_APPLICATION_COSE_KEY_SET 102 /* application/cose-key-set */
157
158/* Content formats from RFC 8428 */
159#define COAP_MEDIATYPE_APPLICATION_SENML_JSON 110 /* application/senml+json */
160#define COAP_MEDIATYPE_APPLICATION_SENSML_JSON 111 /* application/sensml+json */
161#define COAP_MEDIATYPE_APPLICATION_SENML_CBOR 112 /* application/senml+cbor */
162#define COAP_MEDIATYPE_APPLICATION_SENSML_CBOR 113 /* application/sensml+cbor */
163#define COAP_MEDIATYPE_APPLICATION_SENML_EXI 114 /* application/senml-exi */
164#define COAP_MEDIATYPE_APPLICATION_SENSML_EXI 115 /* application/sensml-exi */
165#define COAP_MEDIATYPE_APPLICATION_SENML_XML 310 /* application/senml+xml */
166#define COAP_MEDIATYPE_APPLICATION_SENSML_XML 311 /* application/sensml+xml */
167
168/* Content formats from RFC 8782 */
169#define COAP_MEDIATYPE_APPLICATION_DOTS_CBOR 271 /* application/dots+cbor */
170
171/* Content formats from RFC 9200 */
172#define COAP_MEDIATYPE_APPLICATION_ACE_CBOR 19 /* application/ace+cbor */
173
174/* Content formats from RFC9177 */
175#define COAP_MEDIATYPE_APPLICATION_MB_CBOR_SEQ 272 /* application/missing-blocks+cbor-seq */
176
177/* Content formats from RFC 8613 */
178#define COAP_MEDIATYPE_APPLICATION_OSCORE 10001 /* application/oscore */
179
184typedef int coap_mid_t;
185
187#define COAP_INVALID_MID -1
188
193#define COAP_INVALID_TID COAP_INVALID_MID
194
202COAP_DEPRECATED typedef struct {
203 coap_option_num_t key; /* the option key (no delta coding) */
204 unsigned int length;
206
207#define COAP_OPTION_KEY(option) (option).key
208#define COAP_OPTION_LENGTH(option) (option).length
209#define COAP_OPTION_DATA(option) ((unsigned char *)&(option) + sizeof(coap_option))
210
211#ifdef WITH_LWIP
227coap_pdu_t *coap_pdu_from_pbuf(struct pbuf *pbuf);
228#endif
229
244
248typedef enum coap_pdu_code_t {
250
258
286
293
298
314 coap_mid_t mid, size_t size);
315
327 coap_session_t *session);
328
343
358 coap_session_t *session,
359 size_t token_length,
360 const uint8_t *token,
361 coap_opt_filter_t *drop_options);
362
378 const uint8_t *data,
379 size_t length,
380 coap_pdu_t *pdu);
381
397 size_t len,
398 const uint8_t *data);
399
421size_t coap_add_option(coap_pdu_t *pdu,
422 coap_option_num_t number,
423 size_t len,
424 const uint8_t *data);
425
438 size_t len,
439 const uint8_t *data);
440
453uint8_t *coap_add_data_after(coap_pdu_t *pdu, size_t len);
454
467int coap_get_data(const coap_pdu_t *pdu,
468 size_t *len,
469 const uint8_t **data);
470
488int coap_get_data_large(const coap_pdu_t *pdu,
489 size_t *len,
490 const uint8_t **data,
491 size_t *offset,
492 size_t *total);
493
502
510
519
527
536
545
554
556
557#ifdef __cplusplus
558}
559#endif
560
561#endif /* COAP_PDU_H_ */
#define COAP_API
Helpers for handling options in CoAP PDUs.
uint16_t coap_option_num_t
Definition coap_option.h:37
Helper functions for URI treatment.
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu)
Gets the PDU code associated with pdu.
Definition coap_pdu.c:1699
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition coap_pdu.c:1022
COAP_API void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
Definition coap_pdu.c:190
uint8_t * coap_add_data_after(coap_pdu_t *pdu, size_t len)
Adds given data to the pdu that is passed as first parameter but does not.
Definition coap_pdu.c:928
coap_bool_t
Definition coap_pdu.h:294
void coap_pdu_set_code(coap_pdu_t *pdu, coap_pdu_code_t code)
Sets the PDU code in the pdu.
Definition coap_pdu.c:1704
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:184
COAP_API coap_pdu_t * coap_pdu_duplicate(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options)
Duplicate an existing PDU.
Definition coap_pdu.c:215
coap_request_t
CoAP PDU Request methods.
Definition coap_pdu.h:80
#define COAP_RESPONSE_CODE(N)
Definition coap_pdu.h:96
#define COAP_SIGNALING_CODE(N)
Definition coap_pdu.h:122
coap_proto_t
CoAP protocol types Note: coap_layers_coap[] needs updating if extended.
Definition coap_pdu.h:234
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:248
coap_pdu_type_t
CoAP PDU message type definitions.
Definition coap_pdu.h:70
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
Definition coap_pdu.c:413
void coap_pdu_set_type(coap_pdu_t *pdu, coap_pdu_type_t type)
Sets the PDU type in the pdu.
Definition coap_pdu.c:1717
size_t coap_add_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
Definition coap_pdu.c:837
coap_pdu_signaling_proto_t
Definition coap_pdu.h:124
coap_pdu_type_t coap_pdu_get_type(const coap_pdu_t *pdu)
Gets the PDU type associated with pdu.
Definition coap_pdu.c:1712
int coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data)
Retrieves the length and data pointer of specified PDU.
Definition coap_pdu.c:947
int coap_pdu_parse(coap_proto_t proto, const uint8_t *data, size_t length, coap_pdu_t *pdu)
Parses data into the CoAP PDU structure given in result.
Definition coap_pdu.c:1569
void coap_pdu_set_mid(coap_pdu_t *pdu, coap_mid_t mid)
Sets the message id in the pdu.
Definition coap_pdu.c:1733
COAP_API coap_pdu_t * coap_new_pdu(coap_pdu_type_t type, coap_pdu_code_t code, coap_session_t *session)
Creates a new CoAP PDU.
Definition coap_pdu.c:165
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
Definition coap_pdu.c:104
int coap_get_data_large(const coap_pdu_t *pdu, size_t *len, const uint8_t **data, size_t *offset, size_t *total)
Retrieves the data from a PDU, with support for large bodies of data that spans multiple PDUs.
Definition coap_pdu.c:955
coap_mid_t coap_pdu_get_mid(const coap_pdu_t *pdu)
Gets the message id associated with pdu.
Definition coap_pdu.c:1728
int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds given data to the pdu that is passed as first parameter.
Definition coap_pdu.c:916
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu)
Gets the token associated with pdu.
Definition coap_pdu.c:1723
@ COAP_BOOL_FALSE
Definition coap_pdu.h:295
@ COAP_BOOL_TRUE
Definition coap_pdu.h:296
@ COAP_REQUEST_PUT
Definition coap_pdu.h:83
@ COAP_REQUEST_DELETE
Definition coap_pdu.h:84
@ COAP_REQUEST_GET
Definition coap_pdu.h:81
@ COAP_REQUEST_FETCH
Definition coap_pdu.h:85
@ COAP_REQUEST_PATCH
Definition coap_pdu.h:86
@ COAP_REQUEST_IPATCH
Definition coap_pdu.h:87
@ COAP_REQUEST_POST
Definition coap_pdu.h:82
@ COAP_PROTO_WS
Definition coap_pdu.h:240
@ COAP_PROTO_DTLS
Definition coap_pdu.h:237
@ COAP_PROTO_UDP
Definition coap_pdu.h:236
@ COAP_PROTO_NONE
Definition coap_pdu.h:235
@ COAP_PROTO_TLS
Definition coap_pdu.h:239
@ COAP_PROTO_WSS
Definition coap_pdu.h:241
@ COAP_PROTO_TCP
Definition coap_pdu.h:238
@ COAP_PROTO_LAST
Definition coap_pdu.h:242
@ COAP_RESPONSE_CODE_INTERNAL_ERROR
Definition coap_pdu.h:279
@ COAP_RESPONSE_CODE_HOP_LIMIT_REACHED
Definition coap_pdu.h:285
@ COAP_SIGNALING_CODE_ABORT
Definition coap_pdu.h:291
@ COAP_RESPONSE_CODE_PRECONDITION_FAILED
Definition coap_pdu.h:274
@ COAP_RESPONSE_CODE_NOT_ALLOWED
Definition coap_pdu.h:270
@ COAP_RESPONSE_CODE_CHANGED
Definition coap_pdu.h:262
@ COAP_REQUEST_CODE_PUT
Definition coap_pdu.h:253
@ COAP_REQUEST_CODE_POST
Definition coap_pdu.h:252
@ COAP_RESPONSE_CODE_NOT_ACCEPTABLE
Definition coap_pdu.h:271
@ COAP_SIGNALING_CODE_CSM
Definition coap_pdu.h:287
@ COAP_REQUEST_CODE_PATCH
Definition coap_pdu.h:256
@ COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED
Definition coap_pdu.h:284
@ COAP_RESPONSE_CODE_BAD_REQUEST
Definition coap_pdu.h:265
@ COAP_SIGNALING_CODE_PING
Definition coap_pdu.h:288
@ COAP_RESPONSE_CODE_INCOMPLETE
Definition coap_pdu.h:272
@ COAP_REQUEST_CODE_DELETE
Definition coap_pdu.h:254
@ COAP_SIGNALING_CODE_PONG
Definition coap_pdu.h:289
@ COAP_RESPONSE_CODE_NOT_IMPLEMENTED
Definition coap_pdu.h:280
@ COAP_RESPONSE_CODE_NOT_FOUND
Definition coap_pdu.h:269
@ COAP_RESPONSE_CODE_BAD_GATEWAY
Definition coap_pdu.h:281
@ COAP_RESPONSE_CODE_BAD_OPTION
Definition coap_pdu.h:267
@ COAP_RESPONSE_CODE_REQUEST_TOO_LARGE
Definition coap_pdu.h:275
@ COAP_RESPONSE_CODE_TOO_MANY_REQUESTS
Definition coap_pdu.h:278
@ COAP_RESPONSE_CODE_CONFLICT
Definition coap_pdu.h:273
@ COAP_RESPONSE_CODE_DELETED
Definition coap_pdu.h:260
@ COAP_RESPONSE_CODE_UNAUTHORIZED
Definition coap_pdu.h:266
@ COAP_RESPONSE_CODE_CREATED
Definition coap_pdu.h:259
@ COAP_EMPTY_CODE
Definition coap_pdu.h:249
@ COAP_RESPONSE_CODE_CONTENT
Definition coap_pdu.h:263
@ COAP_RESPONSE_CODE_UNPROCESSABLE
Definition coap_pdu.h:277
@ COAP_RESPONSE_CODE_CONTINUE
Definition coap_pdu.h:264
@ COAP_RESPONSE_CODE_VALID
Definition coap_pdu.h:261
@ COAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT
Definition coap_pdu.h:276
@ COAP_REQUEST_CODE_GET
Definition coap_pdu.h:251
@ COAP_SIGNALING_CODE_RELEASE
Definition coap_pdu.h:290
@ COAP_REQUEST_CODE_FETCH
Definition coap_pdu.h:255
@ COAP_RESPONSE_CODE_GATEWAY_TIMEOUT
Definition coap_pdu.h:283
@ COAP_REQUEST_CODE_IPATCH
Definition coap_pdu.h:257
@ COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE
Definition coap_pdu.h:282
@ COAP_RESPONSE_CODE_FORBIDDEN
Definition coap_pdu.h:268
@ COAP_MESSAGE_NON
Definition coap_pdu.h:72
@ COAP_MESSAGE_ACK
Definition coap_pdu.h:73
@ COAP_MESSAGE_CON
Definition coap_pdu.h:71
@ COAP_MESSAGE_RST
Definition coap_pdu.h:74
@ COAP_SIGNALING_RELEASE
Definition coap_pdu.h:128
@ COAP_SIGNALING_CSM
Definition coap_pdu.h:125
@ COAP_SIGNALING_PONG
Definition coap_pdu.h:127
@ COAP_SIGNALING_PING
Definition coap_pdu.h:126
@ COAP_SIGNALING_ABORT
Definition coap_pdu.h:129
#define COAP_DEPRECATED
Definition libcoap.h:66
CoAP binary data definition with const data.
Definition coap_str.h:65
unsigned int length
Definition coap_pdu.h:204
coap_option_num_t key
Definition coap_pdu.h:203
structure for CoAP PDUs
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...