File size: 18,371 Bytes
69c3cbe | 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 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 | /*
###############################################################################
#
# Temboo CoAP Edge Device library
#
# Copyright (C) 2017, Temboo Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
#
###############################################################################
*/
#include <stdint.h>
#include <string.h>
#include "CoapMsg.h"
#include "TembooGlobal.h"
const uint8_t COAP_VERSION = 1;
const uint8_t HEADER_LENGTH = 4;
CoapMsg::CoapMsg(uint8_t* buffer, uint16_t bufferLen) : m_buffer(buffer), m_bufferLen(bufferLen) {
// Starting a new message. All messages have a header.
m_msgLen = HEADER_LENGTH;
memset(m_buffer, 0, m_bufferLen);
m_buffer[0] = COAP_VERSION << 6;
m_buildState = BUILD_BEGIN;
m_lastOptionCode = 0;
}
CoapMsg::CoapMsg(uint8_t* buffer, uint16_t bufferLen, uint16_t packetLen) : m_buffer(buffer), m_bufferLen(bufferLen) {
m_msgLen = packetLen;
m_buildState = BUILD_HAVE_PAYLOAD;
m_lastOptionCode = 0;
}
void CoapMsg::setType(CoapMsg::Type msgType) {
m_buffer[0] &= 0xCF; //11001111
m_buffer[0] |= msgType << 4;
}
CoapMsg::Type CoapMsg::getType() {
return (CoapMsg::Type)((m_buffer[0] & 0x30) >> 4);
}
void CoapMsg::setId(uint16_t msgId) {
m_buffer[2] = msgId >> 8;
m_buffer[3] = msgId & 0x00FF;
}
uint16_t CoapMsg::getId() {
return (m_buffer[2] << 8) + m_buffer[3];
}
CoapMsg::Result CoapMsg::setToken(const uint8_t* token, uint8_t tokenLen){
if (m_buildState >= BUILD_HAVE_TOKEN) {
return COAP_RESULT_BUILD_ORDER;
}
if (tokenLen > 8){
return COAP_RESULT_TOKEN_LENGTH;
}
if ((tokenLen > 0) && (NULL == token)) {
return COAP_RESULT_TOKEN_NULL;
}
if ((m_msgLen + tokenLen) > m_bufferLen) {
return COAP_RESULT_BUFFER_OVERRUN;
}
memcpy(m_buffer + m_msgLen, token, tokenLen);
m_msgLen += tokenLen;
m_buffer[0] &= 0xF0;
m_buffer[0] |= tokenLen;
m_buildState = BUILD_HAVE_TOKEN;
return COAP_RESULT_SUCCESS;
}
uint8_t* CoapMsg::getToken() {
if (getTokenLen() > 0) {
return m_buffer + HEADER_LENGTH;
}
return NULL;
}
uint8_t CoapMsg::getTokenLen() {
return m_buffer[0] & 0x0F;
}
void CoapMsg::setCode(CoapMsg::Code code) {
m_buffer[1] = (uint8_t)code;
}
CoapMsg::Code CoapMsg::getCode() {
return (CoapMsg::Code)(m_buffer[1]);
}
uint16_t CoapMsg::getHTTPStatus() {
uint8_t code = getCode();
// 3 MSbs are the most significant digit (0 - 6)
// 5 LSbs are the two least significant digits (0 - 31)
return (code >> 5) * 100 + (code & 0x1F);
}
CoapMsg::Result CoapMsg::setPayload(const uint8_t* payload, uint16_t payloadLen) {
if (m_buildState >= BUILD_HAVE_PAYLOAD) {
return COAP_RESULT_BUILD_ORDER;
}
if (payloadLen > 0 && payload == NULL) {
return COAP_RESULT_PAYLOAD_NULL;
}
if ((m_msgLen + payloadLen + 1) > m_bufferLen) {
return COAP_RESULT_BUFFER_OVERRUN;
}
// Add the special payload marker flag.
m_buffer[m_msgLen++] = 0xFF;
memcpy(m_buffer + m_msgLen, payload, payloadLen);
m_msgLen += payloadLen;
m_buildState = BUILD_HAVE_PAYLOAD;
return COAP_RESULT_SUCCESS;
}
uint8_t* CoapMsg::getPayload() {
if (m_buildState < BUILD_HAVE_PAYLOAD) {
return NULL;
}
uint8_t* payload = NULL;
uint16_t optionDelta = 0;
uint16_t optionLen = 0;
uint8_t* i = m_buffer + HEADER_LENGTH + getTokenLen();
while (i < m_buffer + m_msgLen) {
i = decodeOption(i, &optionDelta, &optionLen);
if ((optionDelta == 15) || (optionLen == 15)) {
//Technically, if either optionDelta or optionLen is 15, then
//both MUST be 15, else it's a malformed message. However,
//we're going to be a little loose with the spec here.
payload = i;
break;
}
}
return payload;
}
uint16_t CoapMsg::getPayloadLen() {
if (m_buildState < BUILD_HAVE_PAYLOAD) {
return 0;
}
uint8_t* payload = getPayload();
if (NULL == payload) {
return 0;
}
return m_buffer + m_msgLen - payload;
}
/*
0 1 2 3 4 5 6 7
+---------------+---------------+
| | |
| Option Delta | Option Length | 1 byte
| | |
+---------------+---------------+
\ \ 0 bytes if OptionDelta <= 12
/ Option Delta / 1 byte if OptionDelte == 13
\ (extended) \ 2 bytes if OptionDelta == 14
+-------------------------------+
\ \ 0 bytes if OptionLength <= 12
/ Option Length / 1 byte if OptionLength == 13
\ (extended) \ 2 bytes if OptionLength == 14
+-------------------------------+
\ \
/ /
\ \
/ Option Value / 0 or more bytes (i.e. OptionLength bytes)
\ \
/ /
\ \
+-------------------------------+
*/
CoapMsg::Result CoapMsg::addOption(CoapMsg::Option optionCode, const uint8_t* optionValue, uint16_t optionLen) {
if (m_buildState > BUILD_HAVE_OPTIONS) {
return COAP_RESULT_BUILD_ORDER;
}
if (m_lastOptionCode > optionCode) {
return COAP_RESULT_BUILD_ORDER;
}
if (optionLen > 0 && NULL == optionValue) {
return COAP_RESULT_OPTION_NULL;
}
CoapMsg::Result rc = validateOption(optionCode, optionValue, optionLen);
if (COAP_RESULT_SUCCESS != rc) {
return rc;
}
// See if there's enough room in the buffer to add this option.
uint16_t byteCount = 1;
uint16_t optionDelta = optionCode - m_lastOptionCode;
if (optionDelta >= 13) {
byteCount++;
}
if (optionDelta >= 269) {
byteCount++;
}
if (optionLen >= 13) {
byteCount++;
}
if (optionLen >= 269) {
byteCount++;
}
byteCount += optionLen;
if (m_msgLen + byteCount > m_bufferLen) {
return COAP_RESULT_BUFFER_OVERRUN;
}
// If we get this far, there's enough room.
byteCount = 0;
if (optionDelta >= 269) {
m_buffer[m_msgLen] = 14 << 4;
byteCount++;
m_buffer[m_msgLen + byteCount] = (optionDelta - 269) >> 8;
byteCount++;
m_buffer[m_msgLen + byteCount] = (optionDelta - 269) & 0x00FF;
} else if (optionDelta >= 13) {
m_buffer[m_msgLen] = 13 << 4;
byteCount++;
m_buffer[m_msgLen + byteCount] = (optionDelta - 13);
} else {
m_buffer[m_msgLen] = optionDelta << 4;
}
if (optionLen >= 269) {
m_buffer[m_msgLen] = (m_buffer[m_msgLen] & 0XF0) | 14;
byteCount++;
m_buffer[m_msgLen + byteCount] = (optionLen - 269) >> 8;
byteCount++;
m_buffer[m_msgLen + byteCount] = (optionLen - 269) & 0xFF;
} else if (optionLen >= 13) {
m_buffer[m_msgLen] = (m_buffer[m_msgLen] & 0xF0) | 13;
byteCount++;
m_buffer[m_msgLen + byteCount] = (optionLen - 13);
} else {
m_buffer[m_msgLen] = (m_buffer[m_msgLen] & 0xF0) | optionLen;
byteCount++;
}
m_msgLen += byteCount;
if (optionLen > 0) {
memcpy(m_buffer + m_msgLen, optionValue, optionLen);
}
m_msgLen += optionLen;
m_lastOptionCode = optionCode;
return COAP_RESULT_SUCCESS;
}
CoapMsg::Result CoapMsg::validateOption(CoapMsg::Option optionCode, const uint8_t* optionValue, uint16_t optionLen) {
CoapMsg::Result rc = COAP_RESULT_SUCCESS;
switch(optionCode) {
case COAP_OPTION_IF_MATCH:
rc = validateOptionValue(0, 8, optionValue, optionLen);
break;
case COAP_OPTION_URI_HOST:
rc = validateOptionValue(1, 255, optionValue, optionLen);
break;
case COAP_OPTION_ETAG:
rc = validateOptionValue(1, 8, optionValue, optionLen);
break;
case COAP_OPTION_IF_NONE_MATCH:
rc = validateOptionValue(0, 0, optionValue, optionLen);
break;
//TODO: case COAP_OPTION_OBSERVE:
// rc = validateOptionValue(0, 0, optionValue, optionLen);
// break;
case COAP_OPTION_URI_PORT:
rc = validateOptionValue(0, 2, optionValue, optionLen);
break;
case COAP_OPTION_LOCATION_PATH:
rc = validateOptionValue(0, 255, optionValue, optionLen);
break;
case COAP_OPTION_URI_PATH:
rc = validateOptionValue(0, 255, optionValue, optionLen);
break;
case COAP_OPTION_CONTENT_FORMAT:
rc = validateOptionValue(0, 2, optionValue, optionLen);
break;
case COAP_OPTION_MAX_AGE:
rc = validateOptionValue(0, 4, optionValue, optionLen);
break;
case COAP_OPTION_URI_QUERY:
rc = validateOptionValue(0, 255, optionValue, optionLen);
break;
case COAP_OPTION_ACCEPT:
rc = validateOptionValue(0, 2, optionValue, optionLen);
break;
case COAP_OPTION_LOCATION_QUERY:
rc = validateOptionValue(0, 255, optionValue, optionLen);
break;
case COAP_OPTION_BLOCK2:
rc = validateOptionValue(0, 3, optionValue, optionLen);
break;
case COAP_OPTION_BLOCK1:
rc = validateOptionValue(0, 3, optionValue, optionLen);
break;
case COAP_OPTION_SIZE2:
rc = validateOptionValue(0, 4, optionValue, optionLen);
break;
case COAP_OPTION_PROXY_URI:
rc = validateOptionValue(0, 1034, optionValue, optionLen);
break;
case COAP_OPTION_PROXY_SCHEME:
rc = validateOptionValue(1, 255, optionValue, optionLen);
break;
case COAP_OPTION_SIZE1:
rc = validateOptionValue(0, 4, optionValue, optionLen);
break;
default:
rc = COAP_RESULT_OPTION_UNKNOWN;
}
return rc;
}
CoapMsg::Result CoapMsg::validateOptionValue(uint16_t minLen, uint16_t maxLen, const uint8_t* optionValue, uint16_t optionLen) {
if (optionLen < minLen || optionLen > maxLen) {
return COAP_RESULT_OPTION_LENGTH;
}
if (optionLen > 0 && NULL == optionValue) {
return COAP_RESULT_OPTION_NULL;
}
//TODO: Maybe. Validate value format (uint vs string vs opaque)
return COAP_RESULT_SUCCESS;
}
uint8_t* CoapMsg::getMsgBytes() {
return m_buffer;
}
uint16_t CoapMsg::getMsgLen() {
return m_msgLen;
}
uint8_t* CoapMsg::decodeOption(uint8_t* buffer, uint16_t* optionDelta, uint16_t* optionLen) {
*optionDelta = *buffer >> 4;
*optionLen = *buffer & 0x0F;
buffer++;
if (13 == *optionDelta) {
*optionDelta = *buffer++ + 13;
} else if (14 == *optionDelta) {
*optionDelta = *buffer++ << 8;
*optionDelta += *buffer++;
*optionDelta += 269;
}
if (13 == *optionLen) {
*optionLen = *buffer++ + 13;
} else if (14 == *optionLen) {
*optionLen = *buffer++ << 8;
*optionLen += *buffer++;
*optionLen += 269;
}
if (*optionLen != 15) {
buffer += *optionLen;
}
return buffer;
}
uint16_t CoapMsg::getOptionCount(CoapMsg::Option optionCode) {
if (m_buildState < BUILD_HAVE_OPTIONS) {
return 0;
}
uint16_t count = 0;
uint16_t lastOption = 0;
uint16_t optionDelta = 0;
uint16_t optionLen = 0;
uint8_t* i = m_buffer + HEADER_LENGTH + getTokenLen();
while (i < (m_buffer + m_msgLen) && *i != 0xFF) {
i = decodeOption(i, &optionDelta, &optionLen);
lastOption += optionDelta;
if (lastOption == optionCode) {
count++;
}
}
return count;
}
uint16_t CoapMsg::getOptionLen(CoapMsg::Option optionCode, uint16_t index) {
uint16_t count = 0;
uint16_t lastOption = 0;
uint16_t optionDelta = 0;
uint16_t optionLen = 0;
uint8_t* i = m_buffer + HEADER_LENGTH + getTokenLen();
while (i < (m_buffer + m_msgLen) && *i != 0xFF) {
i = decodeOption(i, &optionDelta, &optionLen);
lastOption += optionDelta;
if (lastOption == optionCode) {
if (count == index) {
break;
}
count++;
}
optionLen = 0;
}
return optionLen;
}
uint8_t* CoapMsg::getOptionValue(CoapMsg::Option optionCode, uint16_t index) {
uint16_t count = 0;
uint16_t lastOption = 0;
uint16_t optionDelta = 0;
uint16_t optionLen = 0;
uint8_t* i = m_buffer + HEADER_LENGTH + getTokenLen();
while (i < (m_buffer + m_msgLen) && *i != 0xFF) {
i = decodeOption(i, &optionDelta, &optionLen);
lastOption += optionDelta;
if (lastOption == optionCode) {
if (count == index) {
return i - optionLen;
}
count++;
}
}
return NULL;
}
CoapMsg::Result CoapMsg::getOption(CoapMsg::Option optionCode, uint16_t index, uint8_t*& optionValue, uint16_t& optionLen) {
uint16_t count = 0;
uint16_t lastOption = 0;
uint16_t optionDelta = 0;
uint16_t optLen = 0;
uint8_t* i = m_buffer + HEADER_LENGTH + getTokenLen();
while (i < (m_buffer + m_msgLen) && *i != 0xFF) {
i = decodeOption(i, &optionDelta, &optLen);
lastOption += optionDelta;
if (lastOption == optionCode) {
if (count == index) {
optionValue = i - optLen;
optionLen = optLen;
return COAP_RESULT_SUCCESS;
}
count++;
}
}
return COAP_RESULT_OPTION_NOT_FOUND;
}
uint16_t CoapMsg::getBlock1Size() {
return getBlockSize(COAP_OPTION_BLOCK1);
}
uint16_t CoapMsg::getBlock2Size() {
return getBlockSize(COAP_OPTION_BLOCK2);
}
uint16_t CoapMsg::getBlockSize(CoapMsg::Option optionCode) {
if (m_buildState < BUILD_HAVE_OPTIONS) {
return 0;
}
uint16_t optionLen;
uint8_t* optionValue;
if (getOption(optionCode, 0, optionValue, optionLen) != COAP_RESULT_SUCCESS) {
return 0;
}
uint16_t blockLen = 16 << (optionValue[optionLen - 1] & 0x07);
if (blockLen > 1024) {
return 0;
}
return blockLen;
}
uint32_t CoapMsg::getBlock1Num() {
return getBlockNum(COAP_OPTION_BLOCK1);
}
uint32_t CoapMsg::getBlock2Num() {
return getBlockNum(COAP_OPTION_BLOCK2);
}
uint32_t CoapMsg::getBlockNum(CoapMsg::Option optionCode) {
if (m_buildState < BUILD_HAVE_OPTIONS) {
return 0;
}
uint16_t optionLen;
uint8_t* optionValue;
if (getOption(optionCode, 0, optionValue, optionLen) != COAP_RESULT_SUCCESS) {
return 0;
}
int32_t blockNum = 0;
for (;optionLen > 0; optionLen--) {
blockNum <<= 8;
blockNum += *optionValue;
optionValue++;
}
blockNum >>= 4;
return blockNum;
}
bool CoapMsg::getBlock1More() {
return getBlockMore(COAP_OPTION_BLOCK1);
}
bool CoapMsg::getBlock2More() {
return getBlockMore(COAP_OPTION_BLOCK2);
}
bool CoapMsg::getBlockMore(CoapMsg::Option optionCode) {
if (m_buildState < BUILD_HAVE_OPTIONS) {
return 0;
}
uint16_t optionLen;
uint8_t* optionValue;
if (getOption(optionCode, 0, optionValue, optionLen) != COAP_RESULT_SUCCESS) {
return 0;
}
if (optionLen == 0) {
return false;
}
return (optionValue[optionLen-1] & 0x08) > 0;
}
/**
* Convert this (existing received) message into a reset message.
*/
void CoapMsg::convertToReset() {
setType(COAP_RESET);
setCode(COAP_EMPTY);
// Set token length to 0
m_buffer[0] &= 0xF0;
m_msgLen = HEADER_LENGTH;
}
void CoapMsg::convertToEmptyAck() {
setType(COAP_ACK);
setCode(COAP_EMPTY);
m_buffer[0] &= 0xF0;
m_msgLen = HEADER_LENGTH;
}
bool CoapMsg::isValid() {
// check packet size
if (m_msgLen < 4) {
TEMBOO_TRACE("Packet must be a minimum of 4 bytes\n");
return false;
}
// check token length
if (getTokenLen() < 0 || getTokenLen() > 8) {
TEMBOO_TRACE("Invalid token length\n");
return false;
}
int16_t responseClass = m_buffer[1] >> 5;
// check HTTP Code is between 000-599
if (responseClass < 0 || responseClass > 5) {
TEMBOO_TRACE("Invalid code\n");
return false;
}
if (HEADER_LENGTH + getTokenLen() == m_msgLen) {
// nothing else in the packet
return true;
}
uint16_t count =0;
uint16_t lastOption = 0;
uint16_t optionDelta = 0;
uint16_t optionLen = 0;
uint8_t* i = m_buffer + HEADER_LENGTH + getTokenLen();
// validate options
while (i < (m_buffer + m_msgLen) && *i != 0xFF) {
i = decodeOption(i, &optionDelta, &optionLen);
lastOption += optionDelta;
if (validateOption((Option)lastOption, i - optionLen, optionLen)) {
TEMBOO_TRACE("Invalid option\n");
return false;
}
}
// if payload marker exists, make sure there is payload data
if (*i == 0xFF && (i - m_buffer) > m_msgLen) {
return false;
}
return true;
}
|