23 #include <lwip/pbuf.h>
28 #define COAP_DEFAULT_PORT 5683
29 #define COAPS_DEFAULT_PORT 5684
30 #define COAP_DEFAULT_MAX_AGE 60
31 #ifndef COAP_DEFAULT_MTU
32 #define COAP_DEFAULT_MTU 1152
36 #define COAP_MESSAGE_SIZE_OFFSET_TCP8 13
37 #define COAP_MESSAGE_SIZE_OFFSET_TCP16 269
38 #define COAP_MESSAGE_SIZE_OFFSET_TCP32 65805
41 #define COAP_MAX_MESSAGE_SIZE_TCP0 (COAP_MESSAGE_SIZE_OFFSET_TCP8-1)
42 #define COAP_MAX_MESSAGE_SIZE_TCP8 (COAP_MESSAGE_SIZE_OFFSET_TCP16-1)
43 #define COAP_MAX_MESSAGE_SIZE_TCP16 (COAP_MESSAGE_SIZE_OFFSET_TCP32-1)
44 #define COAP_MAX_MESSAGE_SIZE_TCP32 (COAP_MESSAGE_SIZE_OFFSET_TCP32+0xFFFFFFFF)
46 #ifndef COAP_DEFAULT_MAX_PDU_RX_SIZE
47 #if defined(WITH_CONTIKI) || defined(WITH_LWIP)
48 #define COAP_DEFAULT_MAX_PDU_RX_SIZE (COAP_MAX_MESSAGE_SIZE_TCP16+4)
51 #define COAP_DEFAULT_MAX_PDU_RX_SIZE (8*1024*1024+256)
55 #ifndef COAP_DEBUG_BUF_SIZE
56 #if defined(WITH_CONTIKI) || defined(WITH_LWIP)
57 #define COAP_DEBUG_BUF_SIZE 128
60 #define COAP_DEBUG_BUF_SIZE (8 + 1024 * 2)
64 #define COAP_DEFAULT_VERSION 1
65 #define COAP_DEFAULT_SCHEME "coap"
68 #define COAP_DEFAULT_URI_WELLKNOWN ".well-known/core"
72 #define COAP_MESSAGE_CON 0
73 #define COAP_MESSAGE_NON 1
74 #define COAP_MESSAGE_ACK 2
75 #define COAP_MESSAGE_RST 3
79 #define COAP_REQUEST_GET 1
80 #define COAP_REQUEST_POST 2
81 #define COAP_REQUEST_PUT 3
82 #define COAP_REQUEST_DELETE 4
83 #define COAP_REQUEST_FETCH 5
84 #define COAP_REQUEST_PATCH 6
85 #define COAP_REQUEST_IPATCH 7
92 #define COAP_OPTION_IF_MATCH 1
93 #define COAP_OPTION_URI_HOST 3
94 #define COAP_OPTION_ETAG 4
95 #define COAP_OPTION_IF_NONE_MATCH 5
96 #define COAP_OPTION_URI_PORT 7
97 #define COAP_OPTION_LOCATION_PATH 8
98 #define COAP_OPTION_URI_PATH 11
99 #define COAP_OPTION_CONTENT_FORMAT 12
100 #define COAP_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
101 #define COAP_OPTION_MAXAGE 14
102 #define COAP_OPTION_URI_QUERY 15
103 #define COAP_OPTION_ACCEPT 17
104 #define COAP_OPTION_LOCATION_QUERY 20
105 #define COAP_OPTION_SIZE2 28
106 #define COAP_OPTION_PROXY_URI 35
107 #define COAP_OPTION_PROXY_SCHEME 39
108 #define COAP_OPTION_SIZE1 60
112 #define COAP_OPTION_OBSERVE 6
113 #define COAP_OPTION_SUBSCRIPTION COAP_OPTION_OBSERVE
117 #define COAP_OPTION_BLOCK2 23
118 #define COAP_OPTION_BLOCK1 27
122 #define COAP_OPTION_NORESPONSE 258
124 #define COAP_MAX_OPT 65535
132 #define COAP_RESPONSE_CODE(N) (((N)/100 << 5) | (N)%100)
135 #define COAP_RESPONSE_CLASS(C) (((C) >> 5) & 0xFF)
137 #ifndef SHORT_ERROR_RESPONSE
150 #define COAP_ERROR_PHRASE_LENGTH 32
153 #define coap_response_phrase(x) ((char *)NULL)
155 #define COAP_ERROR_PHRASE_LENGTH 0
160 #define COAP_RESPONSE_100 40
162 #define COAP_RESPONSE_200 COAP_RESPONSE_CODE(200)
163 #define COAP_RESPONSE_201 COAP_RESPONSE_CODE(201)
164 #define COAP_RESPONSE_304 COAP_RESPONSE_CODE(203)
165 #define COAP_RESPONSE_400 COAP_RESPONSE_CODE(400)
166 #define COAP_RESPONSE_404 COAP_RESPONSE_CODE(404)
167 #define COAP_RESPONSE_405 COAP_RESPONSE_CODE(405)
168 #define COAP_RESPONSE_415 COAP_RESPONSE_CODE(415)
169 #define COAP_RESPONSE_500 COAP_RESPONSE_CODE(500)
170 #define COAP_RESPONSE_501 COAP_RESPONSE_CODE(501)
171 #define COAP_RESPONSE_503 COAP_RESPONSE_CODE(503)
172 #define COAP_RESPONSE_504 COAP_RESPONSE_CODE(504)
174 # define COAP_RESPONSE_X_240 240
175 # define COAP_RESPONSE_X_241 241
177 #define COAP_RESPONSE_X_242 COAP_RESPONSE_CODE(402)
179 #define COAP_SIGNALING_CODE(N) (((N)/100 << 5) | (N)%100)
180 #define COAP_SIGNALING_CSM COAP_SIGNALING_CODE(701)
181 #define COAP_SIGNALING_PING COAP_SIGNALING_CODE(702)
182 #define COAP_SIGNALING_PONG COAP_SIGNALING_CODE(703)
183 #define COAP_SIGNALING_RELEASE COAP_SIGNALING_CODE(704)
184 #define COAP_SIGNALING_ABORT COAP_SIGNALING_CODE(705)
187 #define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE 2
188 #define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER 4
190 #define COAP_SIGNALING_OPTION_CUSTODY 2
192 #define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS 2
193 #define COAP_SIGNALING_OPTION_HOLD_OFF 4
195 #define COAP_SIGNALING_OPTION_BAD_CSM_OPTION 2
199 #define COAP_MEDIATYPE_TEXT_PLAIN 0
200 #define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT 40
201 #define COAP_MEDIATYPE_APPLICATION_XML 41
202 #define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM 42
203 #define COAP_MEDIATYPE_APPLICATION_RDF_XML 43
204 #define COAP_MEDIATYPE_APPLICATION_EXI 47
205 #define COAP_MEDIATYPE_APPLICATION_JSON 50
206 #define COAP_MEDIATYPE_APPLICATION_CBOR 60
209 #define COAP_MEDIATYPE_APPLICATION_COSE_SIGN 98
210 #define COAP_MEDIATYPE_APPLICATION_COSE_SIGN1 18
211 #define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT 96
212 #define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT0 16
213 #define COAP_MEDIATYPE_APPLICATION_COSE_MAC 97
214 #define COAP_MEDIATYPE_APPLICATION_COSE_MAC0 17
216 #define COAP_MEDIATYPE_APPLICATION_COSE_KEY 101
217 #define COAP_MEDIATYPE_APPLICATION_COSE_KEY_SET 102
220 #define COAP_MEDIATYPE_APPLICATION_SENML_JSON 110
221 #define COAP_MEDIATYPE_APPLICATION_SENSML_JSON 111
222 #define COAP_MEDIATYPE_APPLICATION_SENML_CBOR 112
223 #define COAP_MEDIATYPE_APPLICATION_SENSML_CBOR 113
224 #define COAP_MEDIATYPE_APPLICATION_SENML_EXI 114
225 #define COAP_MEDIATYPE_APPLICATION_SENSML_EXI 115
226 #define COAP_MEDIATYPE_APPLICATION_SENML_XML 310
227 #define COAP_MEDIATYPE_APPLICATION_SENSML_XML 311
231 #define COAP_MEDIATYPE_ANY 0xff
241 #define COAP_INVALID_TID -1
247 #define COAP_DROPPED_RESPONSE -2
249 #define COAP_PDU_DELAYED -3
251 #define COAP_OPT_LONG 0x0F
255 #define COAP_OPT_END 0xF0
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
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.
uint8_t * coap_add_option_later(coap_pdu_t *pdu, uint16_t type, size_t len)
Adds option of given type to pdu that is passed as first parameter, but does not write a value.
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 copyt 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_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
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–10) or response code (value 40-255)
uint8_t token_length
length of Token
uint8_t hdr_size
actaul size used for protocol-specific header
uint8_t * data
first byte of payload, if any
uint16_t max_delta
highest option number
size_t used_size
used bytes of storage for token, options and payload
size_t alloc_size
allocated storage for token, options and payload