forked from Oryx-Embedded/CycloneTCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoap_message.c
567 lines (458 loc) · 13.3 KB
/
coap_message.c
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/**
* @file coap_message.c
* @brief CoAP message formatting and parsing
*
* @section License
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
*
* This file is part of CycloneTCP Open.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @author Oryx Embedded SARL (www.oryx-embedded.com)
* @version 1.9.6
**/
//Switch to the appropriate trace level
#define TRACE_LEVEL COAP_TRACE_LEVEL
//Dependencies
#include "core/net.h"
#include "coap/coap_client.h"
//#include "coap/coap_server.h"
#include "coap/coap_message.h"
#include "debug.h"
//Check TCP/IP stack configuration
#if (COAP_CLIENT_SUPPORT == ENABLED || COAP_SERVER_SUPPORT == ENABLED)
/**
* @brief Parse CoAP message
* @param[in] message Pointer to the CoAP message
* @return Error code
**/
error_t coapParseMessage(const CoapMessage *message)
{
error_t error;
size_t n;
size_t length;
const uint8_t *p;
//Point to the first byte of the CoAP message
p = message->buffer;
//Retrieve the length of the message
length = message->length;
//Parse message header
error = coapParseMessageHeader(p, length, &n);
//Any error to report?
if(error)
return error;
//Point to the first option of the message
p += n;
//Number of bytes left to process
length -= n;
//Parse the list of options
error = coapParseOptions(p, length, &n);
//Any error to report?
if(error)
return error;
//Point to the payload
p += n;
//Number of bytes left to process
length -= n;
//The payload is optional
if(length > 0)
{
//The payload is prefixed by a fixed, one-byte payload marker
p++;
//Retrieve the length of the payload
length--;
//The presence of a marker followed by a zero-length payload must be
//processed as a message format error
if(length == 0)
return ERROR_INVALID_MESSAGE;
}
//Successful processing
return NO_ERROR;
}
/**
* @brief Parse CoAP message header
* @param[in] p Input stream where to read the CoAP message header
* @param[in] length Number of bytes available in the input stream
* @param[out] consumed Total number of bytes that have been consumed
* @return Error code
**/
error_t coapParseMessageHeader(const uint8_t *p, size_t length,
size_t *consumed)
{
CoapMessageHeader *header;
//Malformed CoAP message?
if(length < sizeof(CoapMessageHeader))
return ERROR_INVALID_MESSAGE;
//Point to the CoAP message header
header = (CoapMessageHeader *) p;
//Check version field
if(header->version != COAP_VERSION_1)
return ERROR_INVALID_VERSION;
//The length of the Token field must 0-8 bytes
if(header->tokenLen > COAP_MAX_TOKEN_LEN)
return ERROR_INVALID_MESSAGE;
//Malformed CoAP message?
if(length < (sizeof(CoapMessageHeader) + header->tokenLen))
return ERROR_INVALID_MESSAGE;
//Total number of bytes that have been consumed
*consumed = sizeof(CoapMessageHeader) + header->tokenLen;
//Successful processing
return NO_ERROR;
}
/**
* @brief Set message type
* @param[in] message Pointer to the CoAP message
* @param[in] type Message type (Confirmable or Non-confirmable)
* @return Error code
**/
error_t coapSetType(CoapMessage *message, CoapMessageType type)
{
CoapMessageHeader *header;
//Malformed CoAP message?
if(message->length < sizeof(CoapMessageHeader))
return ERROR_INVALID_MESSAGE;
//Point to the CoAP message header
header = (CoapMessageHeader *) message->buffer;
//Set message type
header->type = type;
//Successful processing
return NO_ERROR;
}
/**
* @brief Get message type
* @param[in] message Pointer to the CoAP message
* @param[out] type Message type
* @return Error code
**/
error_t coapGetType(const CoapMessage *message, CoapMessageType *type)
{
CoapMessageHeader *header;
//Malformed CoAP message?
if(message->length < sizeof(CoapMessageHeader))
return ERROR_INVALID_MESSAGE;
//Point to the CoAP message header
header = (CoapMessageHeader *) message->buffer;
//Retrieve message type
*type = (CoapMessageType) header->type;
//Successful processing
return NO_ERROR;
}
/**
* @brief Set method or response code
* @param[in] message Pointer to the CoAP message
* @param[in] code Method or response code
* @return Error code
**/
error_t coapSetCode(CoapMessage *message, CoapCode code)
{
CoapMessageHeader *header;
//Malformed CoAP message?
if(message->length < sizeof(CoapMessageHeader))
return ERROR_INVALID_MESSAGE;
//Point to the CoAP message header
header = (CoapMessageHeader *) message->buffer;
//Set method or response code
header->code = code;
//Successful processing
return NO_ERROR;
}
/**
* @brief Get method or response code
* @param[in] message Pointer to the CoAP message
* @param[out] code Method or response code
* @return Error code
**/
error_t coapGetCode(const CoapMessage *message, CoapCode *code)
{
CoapMessageHeader *header;
//Malformed CoAP message?
if(message->length < sizeof(CoapMessageHeader))
return ERROR_INVALID_MESSAGE;
//Point to the CoAP message header
header = (CoapMessageHeader *) message->buffer;
//Retrieve method or response code
*code = (CoapCode) header->code;
//Successful processing
return NO_ERROR;
}
/**
* @brief Set CoAP message payload
* @param[in] message Pointer to the CoAP message
* @param[out] payload Pointer to payload data
* @param[out] payloadLen Length of the payload, in bytes
* @return Error code
**/
error_t coapSetPayload(CoapMessage *message, const void *payload,
size_t payloadLen)
{
error_t error;
size_t n;
size_t length;
uint8_t *p;
//Point to the first byte of the CoAP message
p = message->buffer;
//Retrieve the length of the message
length = message->length;
//Parse message header
error = coapParseMessageHeader(p, length, &n);
//Any error to report?
if(error)
return error;
//Point to the first option of the message
p += n;
//Number of bytes left to process
length -= n;
//Parse the list of options
error = coapParseOptions(p, length, &n);
//Any error to report?
if(error)
return error;
//Point to the payload
p += n;
//Number of bytes left to process
length -= n;
//Trim the existing payload
message->length -= length;
//The payload is optional
if(payloadLen > 0)
{
//Make sure the output buffer is large enough to hold the payload
if((message->length + payloadLen + 1) > COAP_MAX_MSG_SIZE)
return ERROR_BUFFER_OVERFLOW;
//The payload is prefixed by a fixed, one-byte payload marker
p[0] = COAP_PAYLOAD_MARKER;
//The payload data extends from after the marker to the end of the
//UDP datagram
memcpy(p + 1, payload, payloadLen);
//Terminate the payload with a NULL character
p[payloadLen + 1] = '\0';
//Adjust the length of the CoAP message
message->length += payloadLen + 1;
}
//Successful processing
return NO_ERROR;
}
/**
* @brief Get CoAP message payload
* @param[in] message Pointer to the CoAP message
* @param[out] payload Pointer to the first byte of the payload
* @param[out] payloadLen Length of the payload, in bytes
* @return Error code
**/
error_t coapGetPayload(const CoapMessage *message, const uint8_t **payload,
size_t *payloadLen)
{
error_t error;
size_t n;
size_t length;
const uint8_t *p;
//Point to the first byte of the CoAP message
p = message->buffer;
//Retrieve the length of the message
length = message->length;
//Parse message header
error = coapParseMessageHeader(p, length, &n);
//Any error to report?
if(error)
return error;
//Point to the first option of the message
p += n;
//Number of bytes left to process
length -= n;
//Parse the list of options
error = coapParseOptions(p, length, &n);
//Any error to report?
if(error)
return error;
//Point to the payload
p += n;
//Number of bytes left to process
length -= n;
//The payload is optional
if(length > 0)
{
//The payload is prefixed by a fixed, one-byte payload marker
p++;
//Retrieve the length of the payload
length--;
}
//Point to the first byte of the payload, if any
*payload = p;
//Save the length of the payload
*payloadLen = length;
//Successful processing
return NO_ERROR;
}
/**
* @brief Write payload data
* @param[in] message Pointer to the CoAP message
* @param[in] data Pointer to a buffer containing the data to be written
* @param[in] length Number of bytes to written
* @return Error code
**/
error_t coapWritePayload(CoapMessage *message, const void *data,
size_t length)
{
error_t error;
size_t k;
size_t n;
uint8_t *p;
//Point to the first byte of the CoAP message
p = message->buffer;
//Retrieve the length of the message
n = message->length;
//Parse message header
error = coapParseMessageHeader(p, n, &k);
//Any error to report?
if(error)
return error;
//Point to the first option of the message
p += k;
//Number of bytes left to process
n -= k;
//Parse the list of options
error = coapParseOptions(p, n, &k);
//Any error to report?
if(error)
return error;
//Point to the payload
p += k;
//Number of bytes left to process
n -= k;
//Any data to write?
if(length > 0)
{
//The absence of the payload marker denotes a zero-length payload
if(n == 0)
{
//Make sure the output buffer is large enough to hold the payload
if((message->length + length + 1) > COAP_MAX_MSG_SIZE)
return ERROR_BUFFER_OVERFLOW;
//The payload is prefixed by a fixed, one-byte payload marker
p[n++] = COAP_PAYLOAD_MARKER;
//Adjust the length of the CoAP message
message->length++;
}
else
{
//Make sure the output buffer is large enough to hold the payload
if((message->length + length) > COAP_MAX_MSG_SIZE)
return ERROR_BUFFER_OVERFLOW;
}
//Copy data
memcpy(p + n, data, length);
//Terminate the payload with a NULL character
p[n + length] = '\0';
//Adjust the length of the CoAP message
message->length += length;
}
//Successful processing
return NO_ERROR;
}
/**
* @brief Read payload data
* @param[in] message Pointer to the CoAP message
* @param[out] data Buffer into which received data will be placed
* @param[in] size Maximum number of bytes that can be received
* @param[out] length Number of bytes that have been received
* @return Error code
**/
error_t coapReadPayload(CoapMessage *message, void *data, size_t size,
size_t *length)
{
error_t error;
size_t k;
size_t n;
const uint8_t *p;
//Point to the first byte of the CoAP message
p = message->buffer;
//Retrieve the length of the message
n = message->length;
//Parse message header
error = coapParseMessageHeader(p, n, &k);
//Any error to report?
if(error)
return error;
//Point to the first option of the message
p += k;
//Number of bytes left to process
n -= k;
//Parse the list of options
error = coapParseOptions(p, n, &k);
//Any error to report?
if(error)
return error;
//Point to the payload
p += k;
//Number of bytes left to process
n -= k;
//The payload is optional
if(n > 0)
{
//The payload is prefixed by a fixed, one-byte payload marker
p++;
//Retrieve the length of the payload
n--;
}
//Any data to be copied?
if(message->pos < n)
{
//Limit the number of bytes to copy at a time
n = MIN(n - message->pos, size);
//Copy data
memcpy(data, p + message->pos, n);
//Advance current position
message->pos += n;
//Total number of data that have been read
*length = n;
//Successful processing
error = NO_ERROR;
}
else
{
//No more data available
error = ERROR_END_OF_STREAM;
}
//Return status code
return error;
}
/**
* @brief Token comparison
* @param[in] header1 Pointer to the first CoAP message header
* @param[in] header2 Pointer to the second CoAP message header
* @return TRUE if the tokens match, else FALSE
**/
bool_t coapCompareToken(const CoapMessageHeader *header1,
const CoapMessageHeader *header2)
{
bool_t res = FALSE;
//Check token lengths
if(header1->tokenLen == header2->tokenLen)
{
//Compare tokens
if(!memcmp(header1->token, header2->token, header1->tokenLen))
{
//The tokens match
res = TRUE;
}
}
//Return comparison result
return res;
}
#endif