21 #include <lwip/pbuf.h>
26 #define COAP_DEFAULT_PORT 5683
27 #define COAPS_DEFAULT_PORT 5684
28 #define COAP_DEFAULT_MAX_AGE 60
29 #ifndef COAP_DEFAULT_MTU
30 #define COAP_DEFAULT_MTU 1152
33 #ifndef COAP_DEFAULT_HOP_LIMIT
34 #define COAP_DEFAULT_HOP_LIMIT 16
38 #define COAP_MESSAGE_SIZE_OFFSET_TCP8 13
39 #define COAP_MESSAGE_SIZE_OFFSET_TCP16 269
40 #define COAP_MESSAGE_SIZE_OFFSET_TCP32 65805
43 #define COAP_MAX_MESSAGE_SIZE_TCP0 (COAP_MESSAGE_SIZE_OFFSET_TCP8-1)
44 #define COAP_MAX_MESSAGE_SIZE_TCP8 (COAP_MESSAGE_SIZE_OFFSET_TCP16-1)
45 #define COAP_MAX_MESSAGE_SIZE_TCP16 (COAP_MESSAGE_SIZE_OFFSET_TCP32-1)
46 #define COAP_MAX_MESSAGE_SIZE_TCP32 (COAP_MESSAGE_SIZE_OFFSET_TCP32+0xFFFFFFFF)
48 #ifndef COAP_DEFAULT_MAX_PDU_RX_SIZE
49 #if defined(WITH_CONTIKI) || defined(WITH_LWIP)
50 #define COAP_DEFAULT_MAX_PDU_RX_SIZE (COAP_MAX_MESSAGE_SIZE_TCP16+4UL)
53 #define COAP_DEFAULT_MAX_PDU_RX_SIZE (8UL*1024*1024+256)
57 #ifndef COAP_DEBUG_BUF_SIZE
58 #if defined(WITH_CONTIKI) || defined(WITH_LWIP)
59 #define COAP_DEBUG_BUF_SIZE 128
62 #define COAP_DEBUG_BUF_SIZE (8 + 1024 * 2)
66 #define COAP_DEFAULT_VERSION 1
67 #define COAP_DEFAULT_SCHEME "coap"
70 #define COAP_DEFAULT_URI_WELLKNOWN ".well-known/core"
74 #define COAP_MESSAGE_CON 0
75 #define COAP_MESSAGE_NON 1
76 #define COAP_MESSAGE_ACK 2
77 #define COAP_MESSAGE_RST 3
122 #define COAP_OPTION_IF_MATCH 1
123 #define COAP_OPTION_URI_HOST 3
124 #define COAP_OPTION_ETAG 4
125 #define COAP_OPTION_IF_NONE_MATCH 5
126 #define COAP_OPTION_OBSERVE 6
127 #define COAP_OPTION_URI_PORT 7
128 #define COAP_OPTION_LOCATION_PATH 8
129 #define COAP_OPTION_OSCORE 9
130 #define COAP_OPTION_URI_PATH 11
131 #define COAP_OPTION_CONTENT_FORMAT 12
132 #define COAP_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
134 #define COAP_OPTION_MAXAGE 14
135 #define COAP_OPTION_URI_QUERY 15
136 #define COAP_OPTION_HOP_LIMIT 16
137 #define COAP_OPTION_ACCEPT 17
138 #define COAP_OPTION_LOCATION_QUERY 20
139 #define COAP_OPTION_BLOCK2 23
140 #define COAP_OPTION_BLOCK1 27
141 #define COAP_OPTION_SIZE2 28
142 #define COAP_OPTION_PROXY_URI 35
143 #define COAP_OPTION_PROXY_SCHEME 39
144 #define COAP_OPTION_SIZE1 60
145 #define COAP_OPTION_NORESPONSE 258
147 #define COAP_MAX_OPT 65535
155 #define COAP_RESPONSE_CODE(N) (((N)/100 << 5) | (N)%100)
158 #define COAP_RESPONSE_CLASS(C) (((C) >> 5) & 0xFF)
160 #ifndef SHORT_ERROR_RESPONSE
173 #define COAP_ERROR_PHRASE_LENGTH 32
176 #define coap_response_phrase(x) ((char *)NULL)
178 #define COAP_ERROR_PHRASE_LENGTH 0
181 #define COAP_SIGNALING_CODE(N) (((N)/100 << 5) | (N)%100)
182 #define COAP_SIGNALING_CSM COAP_SIGNALING_CODE(701)
183 #define COAP_SIGNALING_PING COAP_SIGNALING_CODE(702)
184 #define COAP_SIGNALING_PONG COAP_SIGNALING_CODE(703)
185 #define COAP_SIGNALING_RELEASE COAP_SIGNALING_CODE(704)
186 #define COAP_SIGNALING_ABORT COAP_SIGNALING_CODE(705)
189 #define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE 2
190 #define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER 4
192 #define COAP_SIGNALING_OPTION_CUSTODY 2
194 #define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS 2
195 #define COAP_SIGNALING_OPTION_HOLD_OFF 4
197 #define COAP_SIGNALING_OPTION_BAD_CSM_OPTION 2
201 #define COAP_MEDIATYPE_TEXT_PLAIN 0
202 #define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT 40
203 #define COAP_MEDIATYPE_APPLICATION_XML 41
204 #define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM 42
205 #define COAP_MEDIATYPE_APPLICATION_RDF_XML 43
206 #define COAP_MEDIATYPE_APPLICATION_EXI 47
207 #define COAP_MEDIATYPE_APPLICATION_JSON 50
208 #define COAP_MEDIATYPE_APPLICATION_CBOR 60
209 #define COAP_MEDIATYPE_APPLICATION_CWT 61
212 #define COAP_MEDIATYPE_APPLICATION_COSE_SIGN 98
213 #define COAP_MEDIATYPE_APPLICATION_COSE_SIGN1 18
214 #define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT 96
215 #define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT0 16
216 #define COAP_MEDIATYPE_APPLICATION_COSE_MAC 97
217 #define COAP_MEDIATYPE_APPLICATION_COSE_MAC0 17
219 #define COAP_MEDIATYPE_APPLICATION_COSE_KEY 101
220 #define COAP_MEDIATYPE_APPLICATION_COSE_KEY_SET 102
223 #define COAP_MEDIATYPE_APPLICATION_SENML_JSON 110
224 #define COAP_MEDIATYPE_APPLICATION_SENSML_JSON 111
225 #define COAP_MEDIATYPE_APPLICATION_SENML_CBOR 112
226 #define COAP_MEDIATYPE_APPLICATION_SENSML_CBOR 113
227 #define COAP_MEDIATYPE_APPLICATION_SENML_EXI 114
228 #define COAP_MEDIATYPE_APPLICATION_SENSML_EXI 115
229 #define COAP_MEDIATYPE_APPLICATION_SENML_XML 310
230 #define COAP_MEDIATYPE_APPLICATION_SENSML_XML 311
233 #define COAP_MEDIATYPE_APPLICATION_DOTS_CBOR 271
237 #define COAP_MEDIATYPE_ANY 0xff
247 #define COAP_INVALID_TID -1
253 #define COAP_DROPPED_RESPONSE -2
255 #define COAP_PDU_DELAYED -3
257 #define COAP_PAYLOAD_START 0xFF
271 #define COAP_OPTION_KEY(option) (option).key
272 #define COAP_OPTION_LENGTH(option) (option).length
273 #define COAP_OPTION_DATA(option) ((unsigned char *)&(option) + sizeof(coap_option))
312 #define COAP_PDU_IS_EMPTY(pdu) ((pdu)->code == 0)
313 #define COAP_PDU_IS_REQUEST(pdu) (!COAP_PDU_IS_EMPTY(pdu) && (pdu)->code < 32)
314 #define COAP_PDU_IS_RESPONSE(pdu) ((pdu)->code >= 64 && (pdu)->code < 224)
315 #define COAP_PDU_IS_SIGNALING(pdu) ((pdu)->code >= 224)
317 #define COAP_PDU_MAX_UDP_HEADER_SIZE 4
318 #define COAP_PDU_MAX_TCP_HEADER_SIZE 6
336 coap_pdu_t * coap_pdu_from_pbuf(
struct pbuf *pbuf);
343 #define COAP_PROTO_NONE 0
344 #define COAP_PROTO_UDP 1
345 #define COAP_PROTO_DTLS 2
346 #define COAP_PROTO_TCP 3
347 #define COAP_PROTO_TLS 4
520 size_t len,
const uint8_t *data);
int coap_update_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
Updates existing first option of given type in the pdu with the new data.
int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto)
Decode the protocol specific header for the specified PDU.
size_t coap_pdu_parse_header_size(coap_proto_t proto, const uint8_t *data)
Interprets data to determine the number of bytes in the header.
int coap_get_data(const coap_pdu_t *pdu, size_t *len, uint8_t **data)
Retrieves the length and data pointer of specified PDU.
struct coap_pdu_t coap_pdu_t
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
coap_pdu_t * coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
coap_pdu_t * coap_new_pdu(const struct coap_session_t *session)
Creates a new CoAP PDU.
void coap_pdu_clear(coap_pdu_t *pdu, size_t size)
Clears any contents from pdu and resets used_size, and data pointers.
size_t coap_add_option(coap_pdu_t *pdu, uint16_t type, size_t len, const uint8_t *data)
Adds option of given type to pdu that is passed as first parameter.
int coap_pdu_parse_opt(coap_pdu_t *pdu)
Verify consistency in the given CoAP PDU structure and locate the data.
void coap_delete_pdu(coap_pdu_t *)
Dispose of an CoAP PDU and frees associated storage.
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 copy it.
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto)
Compose the protocol specific header for the specified PDU.
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.
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
size_t coap_insert_option(coap_pdu_t *, uint16_t, size_t, const uint8_t *)
Inserts option of given type in the pdu with the appropriate data.
int coap_pdu_check_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size if needed.
size_t coap_pdu_parse_size(coap_proto_t proto, const uint8_t *data, size_t length)
Parses data to extract the message size.
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size)
Dynamically grows the size of pdu to new_size.
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.
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
uint8_t max_hdr_size
space reserved for protocol-specific header
uint16_t max_opt
highest option number in PDU
uint8_t * token
first byte of token, if any, or options
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
uint16_t tid
transaction id, if any, in regular host byte order
uint8_t code
request method (value 1–31) or response code (value 64-255)
uint8_t token_length
length of Token
uint8_t hdr_size
actual size used for protocol-specific header
uint8_t * data
first byte of payload, if any
size_t used_size
used bytes of storage for token, options and payload
size_t alloc_size
allocated storage for token, options and payload