-
Notifications
You must be signed in to change notification settings - Fork 9
/
picocoap.h
executable file
·440 lines (393 loc) · 10.9 KB
/
picocoap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
///
/// @file coap.h
/// @author Patrick Barrett <[email protected]>
/// @date 2014-07-10
/// @brief CoAP Message Parsing
///
/// @details This file provides functions for parsing and building CoAP message packets
/// using only the actual binary of the message, not needing additional memory
/// for secondary data structures.
///
#ifndef _COAP_H_
#define _COAP_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <stddef.h>
#include <stdint.h>
///
/// CoAP Defined Parameters
///
#define COAP_ACK_TIMEOUT 2
#define COAP_ACK_RANDOM_FACTOR 1.5
#define COAP_MAX_RETRANSMIT 4
#define COAP_NSTART 1
#define COAP_DEFAULT_LEISURE 5
#define COAP_PROBING_RATE 1
#define COAP_MAX_TRANSMIT_SPAN 45
#define COAP_MAX_TRANSMIT_WAIT 93
#define COAP_MAX_LATENCY 100
#define COAP_PROCESSING_DELAY 2
#define COAP_MAX_RTT 202
#define COAP_EXCHANGE_LIFETIME 247
#define COAP_NON_LIFETIME 145
///
/// Status Codes
///
/// These codes represent the possible errors that functions in this library can
/// return.
///
typedef enum coap_error {
CE_NONE = 0,
CE_INVALID_PACKET,
CE_BAD_VERSION,
CE_TOKEN_LENGTH_OUT_OF_RANGE,
CE_UNKNOWN_CODE,
CE_TOO_MANY_OPTIONS,
CE_OUT_OF_ORDER_OPTIONS_LIST,
CE_INSUFFICIENT_BUFFER,
CE_FOUND_PAYLOAD_MARKER,
CE_END_OF_PACKET
} pcoap_error;
///
/// Protocol Versions
///
/// All known version of the protocol.
///
typedef enum coap_version {
COAP_V1 = 1
} pcoap_version;
///
/// Message Types
///
/// The four types of messages possible.
///
typedef enum coap_type {
CT_CON = 0,
CT_NON = 1,
CT_ACK = 2,
CT_RST = 3
} pcoap_type;
///
/// Message Codes
///
/// All known message request/response codes.
///
typedef enum pcoap_code {
/* CoAP method codes */
CC_EMPTY = 0,
CC_GET = 1,
CC_POST = 2,
CC_PUT = 3,
CC_DELETE = 4,
/* CoAP response codes */
CC_CREATED = 65,
CC_DELETED = 66,
CC_VALID = 67,
CC_CHANGED = 68,
CC_CONTENT = 69,
CC_CONTINUE = 95,
CC_BAD_REQUEST = 128,
CC_UNAUTHORIZED = 129,
CC_BAD_OPTION = 130,
CC_FORBIDDEN = 131,
CC_NOT_FOUND = 132,
CC_METHOD_NOT_ALLOWED = 133,
CC_NOT_ACCEPTABLE = 134,
CC_REQUEST_ENTITY_INCOMPLETE = 136,
CC_PRECONDITION_FAILED = 140,
CC_REQUEST_ENTITY_TOO_LARGE = 141,
CC_UNSUPPORTED_CONTENT = 143,
CC_INTERNAL_SERVER_ERROR = 160,
CC_NOT_IMPLEMENTED = 161,
CC_BAD_GATEWAY = 162,
CC_SERVICE_UNAVAILABLE = 163,
CC_GATEWAY_TIMEOUT = 164,
CC_PROXYING_NOT_SUPPORTED = 165
} pcoap_code;
///
/// Option Numbers
///
/// All known option numbers.
///
typedef enum pcoap_option_number {
CON_IF_MATCH = 1,
CON_URI_HOST = 3,
CON_ETAG = 4,
CON_IF_NONE_MATCH = 5,
CON_OBSERVE = 6,
CON_URI_PORT = 7,
CON_LOCATION_PATH = 8,
CON_URI_PATH = 11,
CON_CONTENT_FORMAT = 12,
CON_MAX_AGE = 14,
CON_URI_QUERY = 15,
CON_ACCEPT = 17,
CON_LOCATION_QUERY = 20,
CON_PROXY_URI = 35,
CON_PROXY_SCHEME = 39,
CON_SIZE1 = 60,
CON_NO_RESPONSE = 258
} pcoap_option_number;
///
/// Packet Data Unit
///
/// This contains all information about the message buffer.
///
typedef struct pcoap_pdu {
uint8_t *buf; /// pointer to buffer
uint16_t len; /// length of current message
uint16_t max; /// size of buffer
} pcoap_pdu;
///
/// CoAP Option
///
/// One option in a CoAP message.
///
typedef struct pcoap_option {
uint16_t num; /// size of buffer
uint16_t len; /// length of the value
uint8_t *val; /// pointer value
} pcoap_option;
///
/// CoAP Payload
///
/// Payload container.
///
typedef struct pcoap_payload {
uint16_t len; /// length of current message
uint8_t *val; /// pointer to buffer
} pcoap_payload;
// Finds the length of the CoAP header
// And consequently the length of the payload
uint8_t pcoap_get_coap_offset(pcoap_pdu *pdu);
///
/// Validate Packet
///
/// Parses the given packet to check if it is a valid CoAP message.
/// This function (or coap_init_pdu for creating new packets) must be
/// called and must return CE_NONE before you can use any of the
/// getters or setter.
/// @param [in] pdu pointer to the coap message struct.
/// @return error code (CE_NONE == 0 == no error).
/// @see coap_error
/// @see coap_init_pdu
///
pcoap_error pcoap_validate_pkt(pcoap_pdu *pdu);
//
// Getters
//
///
/// Get Version
///
/// Extracts the CoAP version from the given message.
/// @param [in] pdu pointer to the coap message struct.
/// @return version.
/// @see coap_version
///
static inline pcoap_version pcoap_get_version(pcoap_pdu *pdu) { return (pcoap_version) (pdu->buf[0] >> 6); }
///
/// Get Message Type
///
/// Extracts the message type from the given message.
/// @param [in] pdu pointer to the coap message struct.
/// @return type.
/// @see coap_type
///
static inline pcoap_type pcoap_get_type(pcoap_pdu *pdu) { return (pcoap_type) ((pdu->buf[0] >> 4) & 0x03); }
///
/// Get Token Length
///
/// Extracts the token length from the given message.
/// @param [in] pdu pointer to the coap message struct.
/// @return length.
/// @see coap_type
///
static inline uint8_t pcoap_get_tkl(pcoap_pdu *pdu) { return pdu->buf[0] & 0x0F; }
///
/// Get Message Code
///
/// Extracts the message code from the given message.
/// @param [in] pdu pointer to the coap message struct.
/// @return code.
/// @see coap_code
///
static inline pcoap_code pcoap_get_code(pcoap_pdu *pdu) { return (pcoap_code) (pdu->buf[1]); }
///
/// Get Message Code Class
///
/// Gets the class portion of the message code.
/// @param [in] pdu pointer to the coap message struct.
/// @see coap_get_code
///
static inline uint8_t pcoap_get_code_class(pcoap_pdu *pdu) { return pcoap_get_code(pdu) >> 5; }
///
/// Get Message Code Detail
///
/// Gets the detail portion of the message code.
/// @param [in] pdu pointer to the coap message struct.
/// @see coap_get_code
///
static inline uint8_t pcoap_get_code_detail(pcoap_pdu *pdu) { return pcoap_get_code(pdu) & 0x1F; }
///
/// Get Message ID
///
/// Extracts the message ID from the given message.
/// @param [in] pdu pointer to the coap message struct.
/// @return mid.
///
static inline uint16_t pcoap_get_mid(pcoap_pdu *pdu) { return (pdu->buf[2] << 8) | pdu->buf[3]; }
///
/// Get Message Token
///
/// Extracts the token from the given message.
/// @param [in] pdu pointer to the coap message struct.
/// @return token.
///
uint8_t pcoap_get_token(pcoap_pdu *pdu, uint8_t* ptr);
///
/// Get Option
///
/// Iterates over the options in the given message.
/// @param [in] pdu pointer to the coap message struct.
/// @param [in, out] pointer to the last/next option, pass
/// 0 for the first option.
/// @return coap_option
///
pcoap_option pcoap_get_option(pcoap_pdu *pdu, pcoap_option *last);
///
/// Get Option by Option Number
///
/// Gets a single specified by the option number and index of which occurrence
/// of that option number you'd like.
/// @param [in] pdu pointer to the coap message struct.
/// @param [in] num option number to get.
/// @param [in] occ occurrence of to get (0th, 1st, 2nd, etc)
/// 0 for the first option.
/// @return coap_option
///
pcoap_option pcoap_get_option_by_num(pcoap_pdu *pdu, pcoap_option_number num, uint8_t occ);
///
/// Get Option
///
/// Extracts the option with the given index in the given message.
/// @param [in] pdu pointer to the coap message struct.
/// @return coap_payload
///
pcoap_payload pcoap_get_payload(pcoap_pdu *pdu);
///
/// Internal Method
///
pcoap_error pcoap_decode_option(uint8_t *pkt_ptr, uint16_t pkt_len,
uint16_t *option_number, uint16_t *option_length, uint8_t **value);
//
// Setters
//
///
/// Initialize Packet
///
/// Initializes on an empty buffer for creating new CoAP packets.
/// This function (or coap_validate for parsing packets) must be
/// called and must return CE_NONE before you can use any of the
/// getters or setter. The packet is initialized to a CoAP Ping.
/// @param [in, out] pdu pointer to the coap message struct.
/// @return coap_error (0 == no error)
///
pcoap_error pcoap_init_pdu(pcoap_pdu *pdu);
///
/// Set Version
///
/// Sets the version number header field.
/// @param [in, out] pdu pointer to the coap message struct.
/// @param [in] ver version to set. Must be COAP_V1.
/// @return coap_error (0 == no error)
/// @see coap_version
///
pcoap_error pcoap_set_version(pcoap_pdu *pdu, pcoap_version ver);
///
/// Set Message Type
///
/// Sets the message type header field.
/// @param [in, out] pdu pointer to the coap message struct.
/// @param [in] mtype type to set.
/// @return coap_error (0 == no error)
/// @see coap_type
///
pcoap_error pcoap_set_type(pcoap_pdu *pdu, pcoap_type mtype);
///
/// Set Message Code
///
/// Sets the message type header field.
/// @param [in, out] pdu pointer to the coap message struct.
/// @param [in] code code to set.
/// @return coap_error (0 == no error)
/// @see coap_code
///
pcoap_error pcoap_set_code(pcoap_pdu *pdu, pcoap_code code);
///
/// Set Message ID
///
/// Sets the message ID header field.
/// @param [in, out] pdu pointer to the coap message struct.
/// @param [in] mid message ID to set.
/// @return coap_error (0 == no error)
///
pcoap_error pcoap_set_mid(pcoap_pdu *pdu, uint16_t mid);
///
/// Set Message Token
///
/// Sets the message token header field.
/// @param [in, out] pdu pointer to the coap message struct.
/// @param [in] token pointer to the token array to set.
/// @return coap_error (0 == no error)
///
pcoap_error pcoap_set_token(pcoap_pdu *pdu, uint8_t* token, uint8_t tkl);
///
/// Add Message Option
///
/// Adds an option to the existing message. Options SHOULD be added in order of
/// option number. In the case of multiple options of the same type, they are
/// sorted in the order that they are added.
/// @param [in, out] pdu pointer to the coap message struct.
/// @param [in] opt option container.
/// @return coap_error (0 == no error)
///
pcoap_error pcoap_add_option(pcoap_pdu *pdu, int32_t opt_num, uint8_t* value, uint16_t opt_len);
///
/// Add Message Option
///
/// Sets the payload of the given message to the value in `payload`.
/// @param [in, out] pdu pointer to the coap message struct.
/// @param [in] pl payload container.
/// @return coap_error (0 == no error)
///
pcoap_error pcoap_set_payload(pcoap_pdu *pdu, uint8_t *payload, uint16_t payload_len);
///
/// Build Message Code from Class and Detail
///
/// Gets the class portion of the message code.
/// @param [in] class the code class.
/// @param [in] detail the code detail.
/// @see coap_get_code
///
static inline uint8_t pcoap_build_code(uint8_t _class, uint8_t detail) { return (_class << 5) | detail; }
//
// Internal
//
///
/// Internal Method
///
pcoap_error pcoap_adjust_option_deltas(uint8_t *opts, uint16_t *opts_len, uint16_t max_len, int32_t offset);
///
/// Internal Method
///
int8_t pcoap_build_option_header(uint8_t *buf, uint16_t max_len, int32_t opt_delta, int32_t opt_len);
///
/// Internal Method
///
int8_t pcoap_compute_option_header_len(int32_t opt_delta, int32_t opt_len);
#ifdef __cplusplus
}
#endif
#endif /*_COAP_H_*/