21 #include <lwip/pbuf.h>
24 #define COAP_DEFAULT_PORT 5683
25 #define COAP_DEFAULT_MAX_AGE 60
26 #ifndef COAP_MAX_PDU_SIZE
27 #define COAP_MAX_PDU_SIZE 1400
30 #define COAP_DEFAULT_VERSION 1
31 #define COAP_DEFAULT_SCHEME "coap"
34 #define COAP_DEFAULT_URI_WELLKNOWN ".well-known/core"
36 #ifdef __COAP_DEFAULT_HASH
38 #define COAP_DEFAULT_WKC_HASHKEY "\345\130\144\245"
43 #define COAP_MESSAGE_CON 0
44 #define COAP_MESSAGE_NON 1
45 #define COAP_MESSAGE_ACK 2
46 #define COAP_MESSAGE_RST 3
50 #define COAP_REQUEST_GET 1
51 #define COAP_REQUEST_POST 2
52 #define COAP_REQUEST_PUT 3
53 #define COAP_REQUEST_DELETE 4
57 #define COAP_OPTION_IF_MATCH 1
58 #define COAP_OPTION_URI_HOST 3
59 #define COAP_OPTION_ETAG 4
60 #define COAP_OPTION_IF_NONE_MATCH 5
61 #define COAP_OPTION_URI_PORT 7
62 #define COAP_OPTION_LOCATION_PATH 8
63 #define COAP_OPTION_URI_PATH 11
64 #define COAP_OPTION_CONTENT_FORMAT 12
65 #define COAP_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
66 #define COAP_OPTION_MAXAGE 14
67 #define COAP_OPTION_URI_QUERY 15
68 #define COAP_OPTION_ACCEPT 17
69 #define COAP_OPTION_LOCATION_QUERY 20
70 #define COAP_OPTION_PROXY_URI 35
71 #define COAP_OPTION_PROXY_SCHEME 39
72 #define COAP_OPTION_SIZE1 60
76 #define COAP_OPTION_OBSERVE 6
77 #define COAP_OPTION_SUBSCRIPTION COAP_OPTION_OBSERVE
81 #define COAP_OPTION_BLOCK2 23
82 #define COAP_OPTION_BLOCK1 27
86 #define COAP_OPTION_NORESPONSE 258
88 #define COAP_MAX_OPT 65535
96 #define COAP_RESPONSE_CODE(N) (((N)/100 << 5) | (N)%100)
99 #define COAP_RESPONSE_CLASS(C) (((C) >> 5) & 0xFF)
101 #ifndef SHORT_ERROR_RESPONSE
114 #define COAP_ERROR_PHRASE_LENGTH 32
117 #define coap_response_phrase(x) ((char *)NULL)
119 #define COAP_ERROR_PHRASE_LENGTH 0
124 #define COAP_RESPONSE_100 40
126 #define COAP_RESPONSE_200 COAP_RESPONSE_CODE(200)
127 #define COAP_RESPONSE_201 COAP_RESPONSE_CODE(201)
128 #define COAP_RESPONSE_304 COAP_RESPONSE_CODE(203)
129 #define COAP_RESPONSE_400 COAP_RESPONSE_CODE(400)
130 #define COAP_RESPONSE_404 COAP_RESPONSE_CODE(404)
131 #define COAP_RESPONSE_405 COAP_RESPONSE_CODE(405)
132 #define COAP_RESPONSE_415 COAP_RESPONSE_CODE(415)
133 #define COAP_RESPONSE_500 COAP_RESPONSE_CODE(500)
134 #define COAP_RESPONSE_501 COAP_RESPONSE_CODE(501)
135 #define COAP_RESPONSE_503 COAP_RESPONSE_CODE(503)
136 #define COAP_RESPONSE_504 COAP_RESPONSE_CODE(504)
138 # define COAP_RESPONSE_X_240 240
139 # define COAP_RESPONSE_X_241 241
141 #define COAP_RESPONSE_X_242 COAP_RESPONSE_CODE(402)
145 #define COAP_MEDIATYPE_TEXT_PLAIN 0
146 #define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT 40
147 #define COAP_MEDIATYPE_APPLICATION_XML 41
148 #define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM 42
149 #define COAP_MEDIATYPE_APPLICATION_RDF_XML 43
150 #define COAP_MEDIATYPE_APPLICATION_EXI 47
151 #define COAP_MEDIATYPE_APPLICATION_JSON 50
152 #define COAP_MEDIATYPE_APPLICATION_CBOR 60
156 #define COAP_MEDIATYPE_ANY 0xff
166 #define COAP_INVALID_TID -1
172 #define COAP_DROPPED_RESPONSE -2
174 #ifdef WORDS_BIGENDIAN
176 unsigned int version:2;
178 unsigned int token_length:4;
182 unsigned char token[];
186 unsigned int token_length:4;
188 unsigned int version:2;
192 unsigned char token[];
196 #define COAP_MESSAGE_IS_EMPTY(MSG) ((MSG)->code == 0)
197 #define COAP_MESSAGE_IS_REQUEST(MSG) (!COAP_MESSAGE_IS_EMPTY(MSG) \
198 && ((MSG)->code < 32))
199 #define COAP_MESSAGE_IS_RESPONSE(MSG) ((MSG)->code >= 64)
201 #define COAP_OPT_LONG 0x0F
205 #define COAP_OPT_END 0xF0
207 #define COAP_PAYLOAD_START 0xFF
219 #define COAP_OPTION_KEY(option) (option).key
220 #define COAP_OPTION_LENGTH(option) (option).length
221 #define COAP_OPTION_DATA(option) ((unsigned char *)&(option) + sizeof(coap_option))
252 #define COAP_OPTION(node) ((coap_option *)(node)->options)
270 coap_pdu_t * coap_pdu_from_pbuf(
struct pbuf *pbuf);
344 const unsigned char *data);
357 const unsigned char *data);
377 const unsigned char *data);
386 unsigned char **data);
Structures for more convenient handling of options.
void coap_pdu_clear(coap_pdu_t *pdu, size_t size)
Clears any contents from pdu and resets version field, length and data pointers.
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
unsigned short length
PDU length (including header, options, data)
coap_pdu_t * coap_new_pdu(void)
Creates a new CoAP PDU.
coap_hdr_t * hdr
Address of the first byte of the CoAP message.
unsigned short max_delta
highest option number
int coap_add_token(coap_pdu_t *pdu, size_t len, const unsigned char *data)
Adds token of length len to pdu.
int coap_pdu_parse(unsigned char *data, size_t length, coap_pdu_t *result)
Parses data into the CoAP PDU structure given in result.
Header structure for CoAP PDUs.
int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data)
Adds given data to the pdu that is passed as first parameter.
size_t max_size
allocated storage for options and data
char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
void coap_delete_pdu(coap_pdu_t *)
int coap_get_data(coap_pdu_t *pdu, size_t *len, unsigned char **data)
Retrieves the length and data pointer of specified PDU.
size_t coap_add_option(coap_pdu_t *pdu, unsigned short type, unsigned int len, const unsigned char *data)
Adds option of given type to pdu that is passed as first parameter.
unsigned char * coap_add_option_later(coap_pdu_t *pdu, unsigned short type, unsigned int len)
Adds option of given type to pdu that is passed as first parameter, but does not write a value...
coap_pdu_t * coap_pdu_init(unsigned char type, unsigned char code, unsigned short id, size_t size)
Creates a new CoAP PDU of given size (must be large enough to hold the basic CoAP message header (coa...
unsigned char * data
payload