libcoap  4.0.3
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pdu.h
Go to the documentation of this file.
1 /* pdu.h -- CoAP message structure
2  *
3  * Copyright (C) 2010--2012 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #ifndef _PDU_H_
10 #define _PDU_H_
11 
12 #include "config.h"
13 #include "coap_list.h"
14 #include "uri.h"
15 
16 /* pre-defined constants that reflect defaults for CoAP */
17 
18 #define COAP_DEFAULT_RESPONSE_TIMEOUT 2 /* response timeout in seconds */
19 #define COAP_DEFAULT_MAX_RETRANSMIT 4 /* max number of retransmissions */
20 #define COAP_DEFAULT_PORT 5683 /* CoAP default UDP port */
21 #define COAP_DEFAULT_MAX_AGE 60 /* default maximum object lifetime in seconds */
22 #ifndef COAP_MAX_PDU_SIZE
23 #define COAP_MAX_PDU_SIZE 1400 /* maximum size of a CoAP PDU */
24 #endif /* COAP_MAX_PDU_SIZE */
25 
26 #define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */
27 #define COAP_DEFAULT_SCHEME "coap" /* the default scheme for CoAP URIs */
28 
30 #define COAP_DEFAULT_URI_WELLKNOWN ".well-known/core"
31 
32 #ifdef __COAP_DEFAULT_HASH
33 /* pre-calculated hash key for the default well-known URI */
34 #define COAP_DEFAULT_WKC_HASHKEY "\345\130\144\245"
35 #endif
36 
37 /* CoAP message types */
38 
39 #define COAP_MESSAGE_CON 0 /* confirmable message (requires ACK/RST) */
40 #define COAP_MESSAGE_NON 1 /* non-confirmable message (one-shot message) */
41 #define COAP_MESSAGE_ACK 2 /* used to acknowledge confirmable messages */
42 #define COAP_MESSAGE_RST 3 /* indicates error in received messages */
43 
44 /* CoAP request methods */
45 
46 #define COAP_REQUEST_GET 1
47 #define COAP_REQUEST_POST 2
48 #define COAP_REQUEST_PUT 3
49 #define COAP_REQUEST_DELETE 4
50 
51 /* CoAP option types (be sure to update check_critical when adding options */
52 
53 #define COAP_OPTION_IF_MATCH 1 /* C, opaque, 0-8 B, (none) */
54 #define COAP_OPTION_URI_HOST 3 /* C, String, 1-255 B, destination address */
55 #define COAP_OPTION_ETAG 4 /* E, opaque, 1-8 B, (none) */
56 #define COAP_OPTION_IF_NONE_MATCH 5 /* empty, 0 B, (none) */
57 #define COAP_OPTION_URI_PORT 7 /* C, uint, 0-2 B, destination port */
58 #define COAP_OPTION_LOCATION_PATH 8 /* E, String, 0-255 B, - */
59 #define COAP_OPTION_URI_PATH 11 /* C, String, 0-255 B, (none) */
60 #define COAP_OPTION_CONTENT_FORMAT 12 /* E, uint, 0-2 B, (none) */
61 #define COAP_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
62 #define COAP_OPTION_MAXAGE 14 /* E, uint, 0--4 B, 60 Seconds */
63 #define COAP_OPTION_URI_QUERY 15 /* C, String, 1-255 B, (none) */
64 #define COAP_OPTION_ACCEPT 17 /* C, uint, 0-2 B, (none) */
65 #define COAP_OPTION_LOCATION_QUERY 20 /* E, String, 0-255 B, (none) */
66 #define COAP_OPTION_PROXY_URI 35 /* C, String, 1-1034 B, (none) */
67 #define COAP_OPTION_PROXY_SCHEME 39 /* C, String, 1-255 B, (none) */
68 #define COAP_OPTION_SIZE1 60 /* E, uint, 0-4 B, (none) */
69 
70 /* option types from draft-ietf-coap-observe-09 */
71 
72 #define COAP_OPTION_OBSERVE 6 /* E, empty/uint, 0 B/0-3 B, (none) */
73 #define COAP_OPTION_SUBSCRIPTION COAP_OPTION_OBSERVE
74 
75 /* selected option types from draft-core-block-04 */
76 
77 #define COAP_OPTION_BLOCK2 23 /* C, uint, 0--3 B, (none) */
78 #define COAP_OPTION_BLOCK1 27 /* C, uint, 0--3 B, (none) */
79 
80 #define COAP_MAX_OPT 63
82 /* CoAP result codes (HTTP-Code / 100 * 40 + HTTP-Code % 100) */
83 
84 /* As of draft-ietf-core-coap-04, response codes are encoded to base
85  * 32, i.e. the three upper bits determine the response class while
86  * the remaining five fine-grained information specific to that class.
87  */
88 #define COAP_RESPONSE_CODE(N) (((N)/100 << 5) | (N)%100)
89 
90 /* Determines the class of response code C */
91 #define COAP_RESPONSE_CLASS(C) (((C) >> 5) & 0xFF)
92 
93 #ifndef SHORT_ERROR_RESPONSE
94 
104 char *coap_response_phrase(unsigned char code);
105 
106 #define COAP_ERROR_PHRASE_LENGTH 32
108 #else
109 #define coap_response_phrase(x) ((char *)NULL)
110 
111 #define COAP_ERROR_PHRASE_LENGTH 0
112 #endif /* SHORT_ERROR_RESPONSE */
113 
114 /* The following definitions exist for backwards compatibility */
115 #if 0 /* this does not exist any more */
116 #define COAP_RESPONSE_100 40 /* 100 Continue */
117 #endif
118 #define COAP_RESPONSE_200 COAP_RESPONSE_CODE(200) /* 2.00 OK */
119 #define COAP_RESPONSE_201 COAP_RESPONSE_CODE(201) /* 2.01 Created */
120 #define COAP_RESPONSE_304 COAP_RESPONSE_CODE(203) /* 2.03 Valid */
121 #define COAP_RESPONSE_400 COAP_RESPONSE_CODE(400) /* 4.00 Bad Request */
122 #define COAP_RESPONSE_404 COAP_RESPONSE_CODE(404) /* 4.04 Not Found */
123 #define COAP_RESPONSE_405 COAP_RESPONSE_CODE(405) /* 4.05 Method Not Allowed */
124 #define COAP_RESPONSE_415 COAP_RESPONSE_CODE(415) /* 4.15 Unsupported Media Type */
125 #define COAP_RESPONSE_500 COAP_RESPONSE_CODE(500) /* 5.00 Internal Server Error */
126 #define COAP_RESPONSE_501 COAP_RESPONSE_CODE(501) /* 5.01 Not Implemented */
127 #define COAP_RESPONSE_503 COAP_RESPONSE_CODE(503) /* 5.03 Service Unavailable */
128 #define COAP_RESPONSE_504 COAP_RESPONSE_CODE(504) /* 5.04 Gateway Timeout */
129 #if 0 /* these response codes do not have a valid code any more */
130 # define COAP_RESPONSE_X_240 240 /* Token Option required by server */
131 # define COAP_RESPONSE_X_241 241 /* Uri-Authority Option required by server */
132 #endif
133 #define COAP_RESPONSE_X_242 COAP_RESPONSE_CODE(402) /* Critical Option not supported */
134 
135 /* CoAP media type encoding */
136 
137 #define COAP_MEDIATYPE_TEXT_PLAIN 0 /* text/plain (UTF-8) */
138 #define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT 40 /* application/link-format */
139 #define COAP_MEDIATYPE_APPLICATION_XML 41 /* application/xml */
140 #define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM 42 /* application/octet-stream */
141 #define COAP_MEDIATYPE_APPLICATION_RDF_XML 43 /* application/rdf+xml */
142 #define COAP_MEDIATYPE_APPLICATION_EXI 47 /* application/exi */
143 #define COAP_MEDIATYPE_APPLICATION_JSON 50 /* application/json */
144 
145 /* Note that identifiers for registered media types are in the range 0-65535. We
146  * use an unallocated type here and hope for the best. */
147 #define COAP_MEDIATYPE_ANY 0xff /* any media type */
148 
149 /* CoAP transaction id */
150 /*typedef unsigned short coap_tid_t; */
151 typedef int coap_tid_t;
152 #define COAP_INVALID_TID -1
153 
154 #ifdef WORDS_BIGENDIAN
155 typedef struct {
156  unsigned int version:2; /* protocol version */
157  unsigned int type:2; /* type flag */
158  unsigned int token_length:4; /* length of Token */
159  unsigned int code:8; /* request method (value 1--10) or response code (value 40-255) */
160  unsigned short id; /* message id */
161  unsigned char token[]; /* the actual token, if any */
162 } coap_hdr_t;
163 #else
164 typedef struct {
165  unsigned int token_length:4; /* length of Token */
166  unsigned int type:2; /* type flag */
167  unsigned int version:2; /* protocol version */
168  unsigned int code:8; /* request method (value 1--10) or response code (value 40-255) */
169  unsigned short id; /* transaction id (network byte order!) */
170  unsigned char token[]; /* the actual token, if any */
171 } coap_hdr_t;
172 #endif
173 
174 #define COAP_MESSAGE_IS_EMPTY(MSG) ((MSG)->code == 0)
175 #define COAP_MESSAGE_IS_REQUEST(MSG) (!COAP_MESSAGE_IS_EMPTY(MSG) \
176  && ((MSG)->code < 32))
177 #define COAP_MESSAGE_IS_RESPONSE(MSG) ((MSG)->code >= 64 && (MSG)->code <= 191)
178 
179 #define COAP_OPT_LONG 0x0F /* OC == 0b1111 indicates that the option list in a
180  * CoAP message is limited by 0b11110000 marker */
182 #define COAP_OPT_END 0xF0 /* end marker */
184 #define COAP_PAYLOAD_START 0xFF /* payload marker */
185 
191 typedef struct {
192  unsigned short key; /* the option key (no delta coding) */
193  unsigned int length;
194 } coap_option;
196 #define COAP_OPTION_KEY(option) (option).key
197 #define COAP_OPTION_LENGTH(option) (option).length
198 #define COAP_OPTION_DATA(option) ((unsigned char *)&(option) + sizeof(coap_option))
199 
202 typedef struct {
203  size_t max_size;
206  unsigned short max_delta;
207  unsigned short length;
208  unsigned char *data;
209 } coap_pdu_t;
210 
212 #define COAP_OPTION(node) ((coap_option *)(node)->options)
213 
228 coap_pdu_t *
229 coap_pdu_init(unsigned char type, unsigned char code,
230  unsigned short id, size_t size);
231 
238 void coap_pdu_clear(coap_pdu_t *pdu, size_t size);
239 
248 
250 
263 int coap_pdu_parse(unsigned char *data, size_t length, coap_pdu_t *result);
264 
277 int coap_add_token(coap_pdu_t *pdu, size_t len, const unsigned char *data);
278 
287 size_t coap_add_option(coap_pdu_t *pdu, unsigned short type,
288  unsigned int len, const unsigned char *data);
289 
295 int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data);
296 
302 int coap_get_data(coap_pdu_t *pdu, size_t *len, unsigned char **data);
303 
304 #endif /* _PDU_H_ */