17 #include <lwip/pbuf.h>
22 #define COAP_DEFAULT_RESPONSE_TIMEOUT 2
23 #define COAP_DEFAULT_MAX_RETRANSMIT 4
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
84 #define COAP_MAX_OPT 63
92 #define COAP_RESPONSE_CODE(N) (((N)/100 << 5) | (N)%100)
95 #define COAP_RESPONSE_CLASS(C) (((C) >> 5) & 0xFF)
97 #ifndef SHORT_ERROR_RESPONSE
110 #define COAP_ERROR_PHRASE_LENGTH 32
113 #define coap_response_phrase(x) ((char *)NULL)
115 #define COAP_ERROR_PHRASE_LENGTH 0
120 #define COAP_RESPONSE_100 40
122 #define COAP_RESPONSE_200 COAP_RESPONSE_CODE(200)
123 #define COAP_RESPONSE_201 COAP_RESPONSE_CODE(201)
124 #define COAP_RESPONSE_304 COAP_RESPONSE_CODE(203)
125 #define COAP_RESPONSE_400 COAP_RESPONSE_CODE(400)
126 #define COAP_RESPONSE_404 COAP_RESPONSE_CODE(404)
127 #define COAP_RESPONSE_405 COAP_RESPONSE_CODE(405)
128 #define COAP_RESPONSE_415 COAP_RESPONSE_CODE(415)
129 #define COAP_RESPONSE_500 COAP_RESPONSE_CODE(500)
130 #define COAP_RESPONSE_501 COAP_RESPONSE_CODE(501)
131 #define COAP_RESPONSE_503 COAP_RESPONSE_CODE(503)
132 #define COAP_RESPONSE_504 COAP_RESPONSE_CODE(504)
134 # define COAP_RESPONSE_X_240 240
135 # define COAP_RESPONSE_X_241 241
137 #define COAP_RESPONSE_X_242 COAP_RESPONSE_CODE(402)
141 #define COAP_MEDIATYPE_TEXT_PLAIN 0
142 #define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT 40
143 #define COAP_MEDIATYPE_APPLICATION_XML 41
144 #define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM 42
145 #define COAP_MEDIATYPE_APPLICATION_RDF_XML 43
146 #define COAP_MEDIATYPE_APPLICATION_EXI 47
147 #define COAP_MEDIATYPE_APPLICATION_JSON 50
151 #define COAP_MEDIATYPE_ANY 0xff
156 #define COAP_INVALID_TID -1
158 #ifdef WORDS_BIGENDIAN
160 unsigned int version:2;
162 unsigned int token_length:4;
165 unsigned char token[];
169 unsigned int token_length:4;
171 unsigned int version:2;
174 unsigned char token[];
178 #define COAP_MESSAGE_IS_EMPTY(MSG) ((MSG)->code == 0)
179 #define COAP_MESSAGE_IS_REQUEST(MSG) (!COAP_MESSAGE_IS_EMPTY(MSG) \
180 && ((MSG)->code < 32))
181 #define COAP_MESSAGE_IS_RESPONSE(MSG) ((MSG)->code >= 64 && (MSG)->code <= 191)
183 #define COAP_OPT_LONG 0x0F
186 #define COAP_OPT_END 0xF0
188 #define COAP_PAYLOAD_START 0xFF
200 #define COAP_OPTION_KEY(option) (option).key
201 #define COAP_OPTION_LENGTH(option) (option).length
202 #define COAP_OPTION_DATA(option) ((unsigned char *)&(option) + sizeof(coap_option))
221 #define COAP_OPTION(node) ((coap_option *)(node)->options)
241 coap_pdu_t * coap_pdu_from_pbuf(
struct pbuf *pbuf);
260 unsigned short id,
size_t size);
318 unsigned int len,
const 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.
unsigned short length
PDU length (including header, options, data)
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
coap_pdu_t * coap_new_pdu()
Creates a new CoAP PDU.