liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | /*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*
|
| 2 |
|
| 3 |
|
| 4 | ===========================================================================*/
|
| 5 |
|
| 6 |
|
| 7 | /*===========================================================================
|
| 8 |
|
| 9 | INCLUDE FILES FOR MODULE
|
| 10 |
|
| 11 | ===========================================================================*/
|
| 12 |
|
| 13 | #include <string.h>
|
| 14 | #include <stdio.h>
|
| 15 | #include <stdlib.h>
|
| 16 | #include "mbtk_coap_pdu.h"
|
| 17 | #include "mbtk_coap_api.h"
|
| 18 |
|
| 19 | /*===========================================================================
|
| 20 |
|
| 21 | LOCAL DATA STRUCTURES
|
| 22 |
|
| 23 | ===========================================================================*/
|
| 24 | //extern "C"
|
| 25 | //{
|
| 26 |
|
| 27 | static CoapPDU * g_CoapPDU = NULL;
|
| 28 | static CoapPDU * g_CoapRecvPointer = NULL;
|
| 29 |
|
| 30 | static void createCoapPDU()
|
| 31 | {
|
| 32 | if(g_CoapPDU == NULL)
|
| 33 | g_CoapPDU = new CoapPDU();
|
| 34 | }
|
| 35 |
|
| 36 | void coapReleaseCoapPUD()
|
| 37 | {
|
| 38 | if(g_CoapPDU)
|
| 39 | {
|
| 40 | delete g_CoapPDU;
|
| 41 | g_CoapPDU = NULL;
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | int coapReset()
|
| 46 | {
|
| 47 | createCoapPDU();
|
| 48 | return g_CoapPDU->reset();
|
| 49 | }
|
| 50 |
|
| 51 | int coapValidate()
|
| 52 | {
|
| 53 | createCoapPDU();
|
| 54 | return g_CoapPDU->validate();
|
| 55 | }
|
| 56 |
|
| 57 |
|
| 58 | uint8_t* coapGetPDUPointer()
|
| 59 | {
|
| 60 | return g_CoapPDU->getPDUPointer();
|
| 61 | }
|
| 62 |
|
| 63 | void coapSetPDULength(int len)
|
| 64 | {
|
| 65 | createCoapPDU();
|
| 66 | g_CoapPDU->setPDULength(len);
|
| 67 | }
|
| 68 |
|
| 69 | int coapSetURI(char *uri)
|
| 70 | {
|
| 71 | createCoapPDU();
|
| 72 | return g_CoapPDU->setURI(uri);
|
| 73 | }
|
| 74 |
|
| 75 | int coapAddURIQuery(char *query)
|
| 76 | {
|
| 77 | createCoapPDU();
|
| 78 | return g_CoapPDU->addURIQuery(query);
|
| 79 | }
|
| 80 |
|
| 81 | int coapGetURI(char *dst, int dstlen, int *outLen)
|
| 82 | {
|
| 83 | createCoapPDU();
|
| 84 | return g_CoapPDU->getURI(dst, dstlen, outLen);
|
| 85 | }
|
| 86 |
|
| 87 | int coapSetVersion(uint8_t version)
|
| 88 | {
|
| 89 | createCoapPDU();
|
| 90 | return g_CoapPDU->setVersion(version);
|
| 91 | }
|
| 92 |
|
| 93 | uint8_t coapGetVersion()
|
| 94 | {
|
| 95 | createCoapPDU();
|
| 96 | return g_CoapPDU->getVersion();
|
| 97 | }
|
| 98 |
|
| 99 | void coapSetType(mbtk_coap_type mt)
|
| 100 | {
|
| 101 | createCoapPDU();
|
| 102 | g_CoapPDU->setType(mt);
|
| 103 | }
|
| 104 |
|
| 105 | mbtk_coap_type coapGetType()
|
| 106 | {
|
| 107 | createCoapPDU();
|
| 108 | return (mbtk_coap_type)g_CoapPDU->getType();
|
| 109 | }
|
| 110 |
|
| 111 | int GetTokenLength(uint8_t tokenLength)
|
| 112 | {
|
| 113 | createCoapPDU();
|
| 114 | return g_CoapPDU->setTokenLength(tokenLength);
|
| 115 | }
|
| 116 |
|
| 117 | int coapGetTokenLength()
|
| 118 | {
|
| 119 | createCoapPDU();
|
| 120 | return g_CoapPDU->getTokenLength();
|
| 121 | }
|
| 122 |
|
| 123 | uint8_t* coapGetTokenPointer()
|
| 124 | {
|
| 125 | createCoapPDU();
|
| 126 | return g_CoapPDU->getTokenPointer();
|
| 127 | }
|
| 128 |
|
| 129 | int coapSetToken(uint8_t *token, uint8_t tokenLength)
|
| 130 | {
|
| 131 | createCoapPDU();
|
| 132 | return g_CoapPDU->setToken(token, tokenLength) ;
|
| 133 | }
|
| 134 |
|
| 135 | void coapSetCode(mbtk_coap_code_type code)
|
| 136 | {
|
| 137 | createCoapPDU();
|
| 138 | g_CoapPDU->setCode(code);
|
| 139 | }
|
| 140 |
|
| 141 | mbtk_coap_code_type coapGetCode()
|
| 142 | {
|
| 143 | createCoapPDU();
|
| 144 | return (mbtk_coap_code_type)g_CoapPDU->getCode();
|
| 145 | }
|
| 146 |
|
| 147 | mbtk_coap_code_type coapHttpStatusToCode(int httpStatus)
|
| 148 | {
|
| 149 | createCoapPDU();
|
| 150 | return (mbtk_coap_code_type)g_CoapPDU->httpStatusToCode(httpStatus);
|
| 151 | }
|
| 152 |
|
| 153 | int coapSetMessageID(uint16_t messageID)
|
| 154 | {
|
| 155 | createCoapPDU();
|
| 156 | return g_CoapPDU->setMessageID(messageID);
|
| 157 | }
|
| 158 |
|
| 159 | uint16_t coapGetMessageID()
|
| 160 | {
|
| 161 | createCoapPDU();
|
| 162 | return g_CoapPDU->getMessageID();
|
| 163 | }
|
| 164 |
|
| 165 | /// Returns the length of the PDU.
|
| 166 | int coapGetPDULength()
|
| 167 | {
|
| 168 | createCoapPDU();
|
| 169 | return g_CoapPDU->getPDULength();
|
| 170 | }
|
| 171 |
|
| 172 | /// Return the number of options that the PDU has.
|
| 173 | int coapGetNumOptions()
|
| 174 | {
|
| 175 | createCoapPDU();
|
| 176 | return g_CoapPDU->getNumOptions();
|
| 177 | }
|
| 178 |
|
| 179 | //CoapOption* getOptions() {
|
| 180 | // return g_CoapPDU->getOptions();
|
| 181 | //}
|
| 182 |
|
| 183 |
|
| 184 | int coapAddOption(uint16_t insertedOptionNumber, uint16_t optionValueLength, uint8_t *optionValue)
|
| 185 | {
|
| 186 | createCoapPDU();
|
| 187 | return g_CoapPDU->addOption(insertedOptionNumber, optionValueLength, optionValue);
|
| 188 | }
|
| 189 |
|
| 190 | uint8_t* coapMallocPayload(int len)
|
| 191 | {
|
| 192 | createCoapPDU();
|
| 193 | return g_CoapPDU->mallocPayload(len);
|
| 194 | }
|
| 195 |
|
| 196 | int coapSetPayload(uint8_t *payload, int len)
|
| 197 | {
|
| 198 | createCoapPDU();
|
| 199 | return g_CoapPDU->setPayload(payload, len);
|
| 200 | }
|
| 201 |
|
| 202 | /// Returns a pointer to the payload buffer.
|
| 203 | uint8_t* coapGetPayloadPointer()
|
| 204 | {
|
| 205 | createCoapPDU();
|
| 206 | return g_CoapPDU->getPayloadPointer();
|
| 207 | }
|
| 208 |
|
| 209 | /// Gets the length of the payload buffer.
|
| 210 | int coapGetPayloadLength()
|
| 211 | {
|
| 212 | createCoapPDU();
|
| 213 | return g_CoapPDU->getPayloadLength();
|
| 214 | }
|
| 215 |
|
| 216 | /// Returns a pointer to a buffer which is a copy of the payload buffer (dynamically allocated).
|
| 217 | uint8_t* coapGetPayloadCopy()
|
| 218 | {
|
| 219 | createCoapPDU();
|
| 220 | return g_CoapPDU->getPayloadCopy();
|
| 221 | }
|
| 222 |
|
| 223 | int coapSetContentFormat(mbtk_content_format_type format)
|
| 224 | {
|
| 225 | createCoapPDU();
|
| 226 | return g_CoapPDU->setContentFormat(format);
|
| 227 | }
|
| 228 |
|
| 229 | int coapHasOption(uint16_t optionNumber )
|
| 230 | {
|
| 231 | createCoapPDU();
|
| 232 | return g_CoapPDU->hasOption(optionNumber);
|
| 233 | }
|
| 234 | void coapDeleteRecv()
|
| 235 | {
|
| 236 | if(g_CoapRecvPointer != NULL)
|
| 237 | {
|
| 238 | delete g_CoapRecvPointer;
|
| 239 | g_CoapRecvPointer = NULL;
|
| 240 | }
|
| 241 |
|
| 242 | }
|
| 243 | int coapCreateRecv(uint8_t *pdu, int pduLength)
|
| 244 | {
|
| 245 | coapDeleteRecv();
|
| 246 | g_CoapRecvPointer = new CoapPDU(pdu, pduLength, pduLength);
|
| 247 | if(g_CoapRecvPointer != NULL)
|
| 248 | {
|
| 249 | if(g_CoapRecvPointer->validate())
|
| 250 | return g_CoapRecvPointer->getPDULength();
|
| 251 | else
|
| 252 | {
|
| 253 | coapDeleteRecv();
|
| 254 | return 0;
|
| 255 | }
|
| 256 | }
|
| 257 | else
|
| 258 | return 0;
|
| 259 |
|
| 260 | }
|
| 261 |
|
| 262 | void coapPrintHuman(char * outBuffer)
|
| 263 | {
|
| 264 | char type[20];
|
| 265 | switch(g_CoapRecvPointer->getType())
|
| 266 | {
|
| 267 | case COAP_CONFIRMABLE:
|
| 268 | strcpy(type, "Confirmable");
|
| 269 | break;
|
| 270 |
|
| 271 | case COAP_NON_CONFIRMABLE:
|
| 272 | strcpy(type, "Non-Confirmable");
|
| 273 | break;
|
| 274 |
|
| 275 | case COAP_ACKNOWLEDGEMENT:
|
| 276 | strcpy(type, "Acknowledgement");
|
| 277 | break;
|
| 278 |
|
| 279 | case COAP_RESET:
|
| 280 | strcpy(type, "Reset");
|
| 281 | break;
|
| 282 | }
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 283 | char code[40];
|
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 284 | switch(g_CoapRecvPointer->getCode())
|
| 285 | {
|
| 286 | case COAP_EMPTY:
|
| 287 | strcpy(code, "0.00 Empty");
|
| 288 | break;
|
| 289 | case COAP_GET:
|
| 290 | strcpy(code, "0.01 GET");
|
| 291 | break;
|
| 292 | case COAP_POST:
|
| 293 | strcpy(code, "0.02 POST");
|
| 294 | break;
|
| 295 | case COAP_PUT:
|
| 296 | strcpy(code, "0.03 PUT");
|
| 297 | break;
|
| 298 | case COAP_DELETE:
|
| 299 | strcpy(code, "0.04 DELETE");
|
| 300 | break;
|
| 301 | case COAP_CREATED:
|
| 302 | strcpy(code, "2.01 Created");
|
| 303 | break;
|
| 304 | case COAP_DELETED:
|
| 305 | strcpy(code, "2.02 Deleted");
|
| 306 | break;
|
| 307 | case COAP_VALID:
|
| 308 | strcpy(code, "2.03 Valid");
|
| 309 | break;
|
| 310 | case COAP_CHANGED:
|
| 311 | strcpy(code, "2.04 Changed");
|
| 312 | break;
|
| 313 | case COAP_CONTENT:
|
| 314 | strcpy(code, "2.05 Content");
|
| 315 | break;
|
| 316 | case COAP_BAD_REQUEST:
|
| 317 | strcpy(code, "4.00 Bad Request");
|
| 318 | break;
|
| 319 | case COAP_UNAUTHORIZED:
|
| 320 | strcpy(code, "4.01 Unauthorized");
|
| 321 | break;
|
| 322 | case COAP_BAD_OPTION:
|
| 323 | strcpy(code, "4.02 Bad Option");
|
| 324 | break;
|
| 325 | case COAP_FORBIDDEN:
|
| 326 | strcpy(code, "4.03 Forbidden");
|
| 327 | break;
|
| 328 | case COAP_NOT_FOUND:
|
| 329 | strcpy(code, "4.04 Not Found");
|
| 330 | break;
|
| 331 | case COAP_METHOD_NOT_ALLOWED:
|
| 332 | strcpy(code, "4.05 Method Not Allowed");
|
| 333 | break;
|
| 334 | case COAP_NOT_ACCEPTABLE:
|
| 335 | strcpy(type, "4.06 Not Acceptable");
|
| 336 | break;
|
| 337 | case COAP_PRECONDITION_FAILED:
|
| 338 | strcpy(code, "4.12 Precondition Failed");
|
| 339 | break;
|
| 340 | case COAP_REQUEST_ENTITY_TOO_LARGE:
|
| 341 | strcpy(code, "4.13 Request Entity Too Large");
|
| 342 | break;
|
| 343 | case COAP_UNSUPPORTED_CONTENT_FORMAT:
|
| 344 | strcpy(code, "4.15 Unsupported Content-Format");
|
| 345 | break;
|
| 346 | case COAP_INTERNAL_SERVER_ERROR:
|
| 347 | strcpy(code, "5.00 Internal Server Error");
|
| 348 | break;
|
| 349 | case COAP_NOT_IMPLEMENTED:
|
| 350 | strcpy(code, "5.01 Not Implemented");
|
| 351 | break;
|
| 352 | case COAP_BAD_GATEWAY:
|
| 353 | strcpy(code, "5.02 Bad Gateway");
|
| 354 | break;
|
| 355 | case COAP_SERVICE_UNAVAILABLE:
|
| 356 | strcpy(code, "5.03 Service Unavailable");
|
| 357 | break;
|
| 358 | case COAP_GATEWAY_TIMEOUT:
|
| 359 | strcpy(code, "5.04 Gateway Timeout");
|
| 360 | break;
|
| 361 | case COAP_PROXYING_NOT_SUPPORTED:
|
| 362 | strcpy(code, "5.05 Proxying Not Supported");
|
| 363 | break;
|
| 364 | default:
|
| 365 | {
|
| 366 | sprintf(code, "Undefined Code %u", g_CoapRecvPointer->getCode());
|
| 367 | }
|
| 368 | }
|
| 369 |
|
| 370 | sprintf(outBuffer,"PDU is %d bytes long\r\n"
|
| 371 | "CoAP Version: %d\r\n"
|
| 372 | "Message Type: %s\r\n"
|
| 373 | "Code: %s\r\n"
|
| 374 | "Message ID: %u\r\n",
|
| 375 | g_CoapRecvPointer->getPDULength(),
|
| 376 | g_CoapRecvPointer->getVersion(),
|
| 377 | type,
|
| 378 | code,
|
| 379 | g_CoapRecvPointer->getMessageID());
|
| 380 |
|
| 381 | // print token value
|
| 382 | int tokenLength = g_CoapRecvPointer->getTokenLength();
|
| 383 | uint8_t *tokenPointer = g_CoapRecvPointer->getPDUPointer()+COAP_HDR_SIZE;
|
| 384 | if(tokenLength==0)
|
| 385 | {
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 386 | sprintf(outBuffer + strlen(outBuffer),"No token\r\n");
|
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 387 | }
|
| 388 | else
|
| 389 | {
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 390 | sprintf(outBuffer + strlen(outBuffer),"Token of %d bytes.\r\n"
|
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 391 | "Value: %s\r\n",
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 392 | tokenLength, (char*)tokenPointer);
|
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 393 | for(int j=0; j<tokenLength; j++)
|
| 394 | {
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 395 | sprintf(outBuffer + strlen(outBuffer),"%.2x", tokenPointer[j]);
|
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 396 | }
|
| 397 | }
|
| 398 | // print options
|
| 399 | CoapPDU::CoapOption* options = g_CoapRecvPointer->getOptions();
|
| 400 | if(options==NULL)
|
| 401 | {
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 402 | sprintf(outBuffer + strlen(outBuffer),"\r\nNO options\r\n");
|
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 403 | }
|
| 404 |
|
| 405 | for(int i=0; i<g_CoapRecvPointer->getNumOptions(); i++)
|
| 406 | {
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 407 | char optionNumberBuffer[40];
|
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 408 | switch(options[i].optionNumber)
|
| 409 | {
|
| 410 | case COAP_OPTION_IF_MATCH:
|
| 411 | strcpy(optionNumberBuffer, "IF_MATCH");
|
| 412 | break;
|
| 413 | case COAP_OPTION_URI_HOST:
|
| 414 | strcpy(optionNumberBuffer, "URI_HOST");
|
| 415 | break;
|
| 416 | case COAP_OPTION_ETAG:
|
| 417 | strcpy(optionNumberBuffer, "ETAG");
|
| 418 | break;
|
| 419 | case COAP_OPTION_IF_NONE_MATCH:
|
| 420 | strcpy(optionNumberBuffer, "IF_NONE_MATCH");
|
| 421 | break;
|
| 422 | case COAP_OPTION_OBSERVE:
|
| 423 | strcpy(optionNumberBuffer, "OBSERVE");
|
| 424 | break;
|
| 425 | case COAP_OPTION_URI_PORT:
|
| 426 | strcpy(optionNumberBuffer, "URI_PORT");
|
| 427 | break;
|
| 428 | case COAP_OPTION_LOCATION_PATH:
|
| 429 | strcpy(optionNumberBuffer, "LOCATION_PATH");
|
| 430 | break;
|
| 431 | case COAP_OPTION_URI_PATH:
|
| 432 | strcpy(optionNumberBuffer, "URI_PATH");
|
| 433 | break;
|
| 434 | case COAP_OPTION_CONTENT_FORMAT:
|
| 435 | strcpy(optionNumberBuffer, "CONTENT_FORMAT");
|
| 436 | break;
|
| 437 | case COAP_OPTION_MAX_AGE:
|
| 438 | strcpy(optionNumberBuffer, "MAX_AGE");
|
| 439 | break;
|
| 440 | case COAP_OPTION_URI_QUERY:
|
| 441 | strcpy(optionNumberBuffer, "URI_QUERY");
|
| 442 | break;
|
| 443 | case COAP_OPTION_ACCEPT:
|
| 444 | strcpy(optionNumberBuffer, "ACCEPT");
|
| 445 | break;
|
| 446 | case COAP_OPTION_LOCATION_QUERY:
|
| 447 | strcpy(optionNumberBuffer, "LOCATION_QUERY");
|
| 448 | break;
|
| 449 | case COAP_OPTION_PROXY_URI:
|
| 450 | strcpy(optionNumberBuffer, "PROXY_URI");
|
| 451 | break;
|
| 452 | case COAP_OPTION_PROXY_SCHEME:
|
| 453 | strcpy(optionNumberBuffer, "PROXY_SCHEME");
|
| 454 | break;
|
| 455 | case COAP_OPTION_BLOCK1:
|
| 456 | strcpy(optionNumberBuffer, "BLOCK1");
|
| 457 | break;
|
| 458 | case COAP_OPTION_BLOCK2:
|
| 459 | strcpy(optionNumberBuffer, "BLOCK2");
|
| 460 | break;
|
| 461 | case COAP_OPTION_SIZE1:
|
| 462 | strcpy(optionNumberBuffer, "SIZE1");
|
| 463 | break;
|
| 464 | case COAP_OPTION_SIZE2:
|
| 465 | strcpy(optionNumberBuffer, "SIZE2");
|
| 466 | break;
|
| 467 | default:
|
| 468 | sprintf(optionNumberBuffer, "Unknown option %u",(unsigned)options[i].optionNumber);
|
| 469 | break;
|
| 470 | }
|
| 471 |
|
| 472 | /*char optionValue[options[i].optionValueLength + 1];
|
| 473 | for(int j=0; j<options[i].optionValueLength; j++) {
|
| 474 | char c = options[i].optionValuePointer[j];
|
| 475 | if((c>='!'&&c<='~')||c==' ') {
|
| 476 | sprintf(optionValue, "%s%c",optionValue,c);
|
| 477 | } else {
|
| 478 | sprintf(optionValue,"%s\\%.2d",optionValue,c);
|
| 479 | }
|
| 480 | }
|
| 481 | sprintf(optionValue,"%s\"\r\n",optionValue);*/
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 482 | sprintf(outBuffer + strlen(outBuffer),"\r\nOPTION (%d/%d)\r\n"
|
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 483 | "Option number (delta): %hu (%hu)\r\n"
|
| 484 | "Name: %s\r\n"
|
| 485 | "Value length: %u\r\n"
|
| 486 | "Value: \"%s",
|
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 487 | i + 1,g_CoapRecvPointer->getNumOptions(),
|
| 488 | options[i].optionNumber,options[i].optionDelta,
|
| 489 | optionNumberBuffer,
|
| 490 | options[i].optionValueLength,
|
| 491 | (char *)options[i].optionValuePointer);
|
| 492 |
|
| 493 | }
|
| 494 | if(options)
|
| 495 | free(options);
|
| 496 | // print payload
|
| 497 | if(g_CoapRecvPointer->getPayloadLength() == 0)
|
| 498 | {
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 499 | sprintf(outBuffer + strlen(outBuffer),"No payload\r\n");
|
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 500 | }
|
| 501 | else
|
| 502 | {
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 503 | sprintf(outBuffer + strlen(outBuffer), "Payload of %d bytes\r\n"
|
| 504 | "Value: \"%s\"\r\n",
|
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 505 | g_CoapRecvPointer->getPayloadLength(),
|
| 506 | (char *)g_CoapRecvPointer->getPayloadPointer());
|
| 507 | /*sprintf(outBuffer,"%sPayload of %d bytes\r\n", outBuffer,g_CoapRecvPointer->getPayloadLength());
|
| 508 | sprintf(outBuffer, "%sValue: \"", outBuffer);
|
| 509 | uint8_t* _payloadPointer = g_CoapRecvPointer->getPayloadPointer();
|
| 510 | for(int j=0; j<g_CoapRecvPointer->getPayloadLength(); j++) {
|
| 511 | char c = _payloadPointer[j];
|
| 512 | if((c>='!'&&c<='~')||c==' ') {
|
| 513 | sprintf(outBuffer, "%s%c", outBuffer,c);
|
| 514 | } else {
|
| 515 | sprintf(outBuffer, "%s\\%.2x",outBuffer,c);
|
| 516 | }
|
| 517 | }
|
| 518 | sprintf(outBuffer, "%s\"\r\n", outBuffer);*/
|
| 519 | }
|
| 520 |
|
| 521 | }
|
| 522 |
|
| 523 | void coapGetOptionValueById(uint16_t optionNumber, uint16_t * optionValueLength, uint8_t * optionValuePointer)
|
| 524 | {
|
| 525 | if(g_CoapRecvPointer)
|
| 526 | g_CoapRecvPointer->getOptionValueById(optionNumber, optionValueLength, optionValuePointer);
|
| 527 | }
|
| 528 |
|
| 529 | uint16_t coapGetRecvMessageID()
|
| 530 | {
|
| 531 | return g_CoapRecvPointer->getMessageID();
|
| 532 | }
|
| 533 |
|
| 534 | mbtk_coap_type coapGetRecvType()
|
| 535 | {
|
| 536 | return (mbtk_coap_type)g_CoapRecvPointer->getType();
|
| 537 | }
|
| 538 |
|
| 539 | mbtk_coap_code_type coapGetRecvCode()
|
| 540 | {
|
| 541 | return (mbtk_coap_code_type)g_CoapRecvPointer->getCode();
|
| 542 | }
|
| 543 |
|
| 544 | int mbtk_coap_get_pdu_Length()
|
| 545 | {
|
| 546 | return g_CoapRecvPointer->getPDULength();
|
| 547 | }
|
| 548 |
|
| 549 |
|
| 550 | int coapGetRecvTokenLength()
|
| 551 | {
|
| 552 | return g_CoapRecvPointer->getTokenLength();
|
| 553 | }
|
| 554 |
|
| 555 | uint8_t* coapGetRecvTokenPointer()
|
| 556 | {
|
| 557 | return g_CoapRecvPointer->getTokenPointer();
|
| 558 | }
|
| 559 |
|
| 560 | /// Returns a pointer to the payload buffer.
|
| 561 | uint8_t* coapGetRecvPayloadPointer()
|
| 562 | {
|
| 563 | return g_CoapRecvPointer->getPayloadPointer();
|
| 564 | }
|
| 565 |
|
| 566 | /// Gets the length of the payload buffer.
|
| 567 | int coapGetRecvPayloadLength()
|
| 568 | {
|
| 569 | return g_CoapRecvPointer->getPayloadLength();
|
| 570 | }
|
| 571 |
|
| 572 | uint8_t coapGetRecvVersion()
|
| 573 | {
|
| 574 | return g_CoapRecvPointer->getVersion();
|
| 575 | }
|
| 576 |
|
| 577 | // Return If-Match length,or 0 if fail.
|
| 578 | uint16_t coapGetRecvOptionIfMatch(uint16_t *optionValueLength, uint8_t *optionValuePointer)
|
| 579 | {
|
| 580 | *optionValueLength = 0;
|
| 581 | coapGetOptionValueById(COAP_OPTION_IF_MATCH,optionValueLength,optionValuePointer);
|
| 582 | return *optionValueLength;
|
| 583 | }
|
| 584 |
|
| 585 | // Return Uri-Host length,or 0 if fail.
|
| 586 | uint16_t coapGetRecvOptionUriHost(uint16_t *optionValueLength, uint8_t *optionValuePointer)
|
| 587 | {
|
| 588 | *optionValueLength = 0;
|
| 589 | coapGetOptionValueById(COAP_OPTION_URI_HOST,optionValueLength,optionValuePointer);
|
| 590 | return *optionValueLength;
|
| 591 | }
|
| 592 |
|
| 593 | // Return ETag length,or 0 if fail.
|
| 594 | uint16_t coapGetRecvOptionETag(uint16_t *optionValueLength, uint8_t *optionValuePointer)
|
| 595 | {
|
| 596 | *optionValueLength = 0;
|
| 597 | coapGetOptionValueById(COAP_OPTION_ETAG,optionValueLength,optionValuePointer);
|
| 598 | return *optionValueLength;
|
| 599 | }
|
| 600 |
|
| 601 | // Return If-None-Match length,or 0 if fail.
|
| 602 | uint16_t coapGetRecvOptionIfNoneMatch(uint16_t *optionValueLength, uint8_t *optionValuePointer)
|
| 603 | {
|
| 604 | *optionValueLength = 0;
|
| 605 | coapGetOptionValueById(COAP_OPTION_IF_NONE_MATCH,optionValueLength,optionValuePointer);
|
| 606 | return *optionValueLength;
|
| 607 | }
|
| 608 |
|
| 609 | // Return Location-Path length,or 0 if fail.
|
| 610 | uint16_t coapGetRecvOptionLocationPath(uint16_t *optionValueLength, uint8_t *optionValuePointer)
|
| 611 | {
|
| 612 | *optionValueLength = 0;
|
| 613 | coapGetOptionValueById(COAP_OPTION_LOCATION_PATH,optionValueLength,optionValuePointer);
|
| 614 | return *optionValueLength;
|
| 615 | }
|
| 616 |
|
| 617 | // Return Location-Query length,or 0 if fail.
|
| 618 | uint16_t coapGetRecvOptionLocationQuery(uint16_t *optionValueLength, uint8_t *optionValuePointer)
|
| 619 | {
|
| 620 | *optionValueLength = 0;
|
| 621 | coapGetOptionValueById(COAP_OPTION_LOCATION_QUERY,optionValueLength,optionValuePointer);
|
| 622 | return *optionValueLength;
|
| 623 | }
|
| 624 |
|
| 625 | // Return Proxy-Uri length,or 0 if fail.
|
| 626 | uint16_t coapGetRecvOptionProxyUri(uint16_t *optionValueLength, uint8_t *optionValuePointer)
|
| 627 | {
|
| 628 | *optionValueLength = 0;
|
| 629 | coapGetOptionValueById(COAP_OPTION_PROXY_URI,optionValueLength,optionValuePointer);
|
| 630 | return *optionValueLength;
|
| 631 | }
|
| 632 |
|
| 633 | // Return Proxy-Scheme length,or 0 if fail.
|
| 634 | uint16_t coapGetRecvOptionProxyScheme(uint16_t *optionValueLength, uint8_t *optionValuePointer)
|
| 635 | {
|
| 636 | *optionValueLength = 0;
|
| 637 | coapGetOptionValueById(COAP_OPTION_PROXY_SCHEME,optionValueLength,optionValuePointer);
|
| 638 | return *optionValueLength;
|
| 639 | }
|
| 640 |
|
| 641 | // Return Uri-Path length,or 0 if fail.
|
| 642 | uint16_t coapGetRecvOptionUriPath(uint16_t *optionValueLength, uint8_t *optionValuePointer)
|
| 643 | {
|
| 644 | *optionValueLength = 0;
|
| 645 | coapGetOptionValueById(COAP_OPTION_URI_PATH,optionValueLength,optionValuePointer);
|
| 646 | return *optionValueLength;
|
| 647 | }
|
| 648 |
|
| 649 | // Return Uri-Query length,or 0 if fail.
|
| 650 | uint16_t coapGetRecvOptionUriQuery(uint16_t *optionValueLength, uint8_t *optionValuePointer)
|
| 651 | {
|
| 652 | *optionValueLength = 0;
|
| 653 | coapGetOptionValueById(COAP_OPTION_URI_QUERY,optionValueLength,optionValuePointer);
|
| 654 | return *optionValueLength;
|
| 655 | }
|
| 656 |
|
| 657 | // Return 1 if get Observe success,or 0 if fail.
|
| 658 | uint16_t coapGetRecvOptionObserve(uint16_t *observe)
|
| 659 | {
|
| 660 | uint8_t buff[10];
|
| 661 | uint16_t buff_len = 0;
|
| 662 | memset(buff,0x0,10);
|
| 663 | coapGetOptionValueById(COAP_OPTION_OBSERVE,&buff_len,buff);
|
| 664 | if(buff_len > 0)
|
| 665 | {
|
| 666 | *observe = (uint16_t)atoi((char*)buff);
|
| 667 | return 1;
|
| 668 | }
|
| 669 | else
|
| 670 | {
|
| 671 | return 0;
|
| 672 | }
|
| 673 | }
|
| 674 |
|
| 675 | // Return 1 if get Max-Age success,or 0 if fail.
|
| 676 | uint16_t coapGetRecvOptionMaxAge(uint16_t *max_age)
|
| 677 | {
|
| 678 | uint8_t buff[10];
|
| 679 | uint16_t buff_len = 0;
|
| 680 | memset(buff,0x0,10);
|
| 681 | coapGetOptionValueById(COAP_OPTION_MAX_AGE,&buff_len,buff);
|
| 682 | if(buff_len > 0)
|
| 683 | {
|
| 684 | *max_age = (uint16_t)atoi((char*)buff);
|
| 685 | return 1;
|
| 686 | }
|
| 687 | else
|
| 688 | {
|
| 689 | return 0;
|
| 690 | }
|
| 691 | }
|
| 692 |
|
| 693 | // Return 1 if get Uri-Port success,or 0 if fail.
|
| 694 | uint16_t coapGetRecvOptionUriPort(uint16_t *uri_port)
|
| 695 | {
|
| 696 | uint8_t buff[10];
|
| 697 | uint16_t buff_len = 0;
|
| 698 | memset(buff,0x0,10);
|
| 699 | coapGetOptionValueById(COAP_OPTION_URI_PORT,&buff_len,buff);
|
| 700 | if(buff_len > 0)
|
| 701 | {
|
| 702 | *uri_port = (uint16_t)atoi((char*)buff);
|
| 703 | return 1;
|
| 704 | }
|
| 705 | else
|
| 706 | {
|
| 707 | return 0;
|
| 708 | }
|
| 709 | }
|
| 710 |
|
| 711 | // Return 1 if get Size2 success,or 0 if fail.
|
| 712 | uint16_t coapGetRecvOptionSize2(uint16_t *size2)
|
| 713 | {
|
| 714 | uint8_t buff[10];
|
| 715 | uint16_t buff_len = 0;
|
| 716 | memset(buff,0x0,10);
|
| 717 | coapGetOptionValueById(COAP_OPTION_SIZE2,&buff_len,buff);
|
| 718 | if(buff_len > 0)
|
| 719 | {
|
| 720 | *size2 = (uint16_t)atoi((char*)buff);
|
| 721 | return 1;
|
| 722 | }
|
| 723 | else
|
| 724 | {
|
| 725 | return 0;
|
| 726 | }
|
| 727 | }
|
| 728 |
|
| 729 | // Return 1 if get Size1 success,or 0 if fail.
|
| 730 | uint16_t coapGetRecvOptionSize1(uint16_t *size1)
|
| 731 | {
|
| 732 | uint8_t buff[10];
|
| 733 | uint16_t buff_len = 0;
|
| 734 | memset(buff,0x0,10);
|
| 735 | coapGetOptionValueById(COAP_OPTION_SIZE1,&buff_len,buff);
|
| 736 | if(buff_len > 0)
|
| 737 | {
|
| 738 | *size1 = (uint16_t)atoi((char*)buff);
|
| 739 | return 1;
|
| 740 | }
|
| 741 | else
|
| 742 | {
|
| 743 | return 0;
|
| 744 | }
|
| 745 | }
|
| 746 |
|
| 747 | // Return 1 if get Block2 success,or 0 if fail.
|
| 748 | uint16_t coapGetRecvOptionBlock2(uint32_t *number,uint8_t *more_flag,uint8_t *size)
|
| 749 | {
|
| 750 | uint8_t buff[10];
|
| 751 | uint16_t buff_len = 0;
|
| 752 | memset(buff,0x0,10);
|
| 753 | coapGetOptionValueById(COAP_OPTION_BLOCK2,&buff_len,buff);
|
| 754 | if(buff_len > 0)
|
| 755 | {
|
| 756 | if(buff_len == 1)
|
| 757 | {
|
| 758 | *size = (uint8_t)(buff[0] & 0x07);
|
| 759 | *more_flag = (uint8_t)((buff[0] & 0x08) >> 3);
|
| 760 | *number = (uint32_t)((buff[0] & 0xF0) >> 4);
|
| 761 | }
|
| 762 | else if(buff_len == 2)
|
| 763 | {
|
| 764 | *size = (uint8_t)(buff[1] & 0x07);
|
| 765 | *more_flag = (uint8_t)((buff[1] & 0x08) >> 3);
|
| 766 | *number = (uint32_t)(((buff[0] << 8) | (buff[1] & 0xF0)) >> 4);
|
| 767 | }
|
| 768 | else if(buff_len == 3)
|
| 769 | {
|
| 770 | *size = (uint8_t)(buff[2] & 0x07);
|
| 771 | *more_flag = (uint8_t)((buff[2] & 0x08) >> 3);
|
| 772 | *number = (uint32_t)(((buff[0] << 16) | (buff[1] << 8) | (buff[2] & 0xF0)) >> 4);
|
| 773 | }
|
| 774 | else
|
| 775 | {
|
| 776 | return 0;
|
| 777 | }
|
| 778 | return 1;
|
| 779 | }
|
| 780 | else
|
| 781 | {
|
| 782 | return 0;
|
| 783 | }
|
| 784 | }
|
| 785 |
|
| 786 | // Return 1 if get Block1 success,or 0 if fail.
|
| 787 | uint16_t coapGetRecvOptionBlock1(uint32_t *number,uint8_t *more_flag,uint8_t *size)
|
| 788 | {
|
| 789 | uint8_t buff[10];
|
| 790 | uint16_t buff_len = 0;
|
| 791 | memset(buff,0x0,10);
|
| 792 | coapGetOptionValueById(COAP_OPTION_BLOCK1,&buff_len,buff);
|
| 793 | if(buff_len > 0)
|
| 794 | {
|
| 795 | *size = (uint8_t)(buff[0] & 0x07);
|
| 796 | *more_flag = (uint8_t)(buff[0] & 0x08);
|
| 797 | if(buff_len == 1)
|
| 798 | {
|
| 799 | *number = (uint32_t)(buff[0] & 0xF0);
|
| 800 | }
|
| 801 | else if(buff_len == 2)
|
| 802 | {
|
| 803 | *number = (uint32_t)((buff[1] << 8) | (buff[0] & 0xF0));
|
| 804 | }
|
| 805 | else if(buff_len == 3)
|
| 806 | {
|
| 807 | *number = (uint32_t)((buff[2] << 16) | (buff[1] << 8) | (buff[0] & 0xF0));
|
| 808 | }
|
| 809 | else
|
| 810 | {
|
| 811 | return 0;
|
| 812 | }
|
| 813 | return 1;
|
| 814 | }
|
| 815 | else
|
| 816 | {
|
| 817 | return 0;
|
| 818 | }
|
| 819 | }
|
| 820 |
|
| 821 | // Return 1 if get Content-Format success,or 0 if fail.
|
| 822 | uint16_t coapGetRecvOptionContentFormat(mbtk_content_format_type *type)
|
| 823 | {
|
| 824 | uint8_t buff[10];
|
| 825 | uint16_t buff_len = 0;
|
| 826 | memset(buff,0x0,10);
|
| 827 | coapGetOptionValueById(COAP_OPTION_CONTENT_FORMAT,&buff_len,buff);
|
| 828 | if(buff_len > 0)
|
| 829 | {
|
| 830 | *type = (mbtk_content_format_type)atoi((char*)buff);
|
| 831 | return 1;
|
| 832 | }
|
| 833 | else
|
| 834 | {
|
| 835 | return 0;
|
| 836 | }
|
| 837 | }
|
| 838 |
|
| 839 | // Return 1 if get Accept success,or 0 if fail.
|
| 840 | uint16_t coapGetRecvOptionAccept(mbtk_content_format_type *type)
|
| 841 | {
|
| 842 | uint8_t buff[10];
|
| 843 | uint16_t buff_len = 0;
|
| 844 | memset(buff,0x0,10);
|
| 845 | coapGetOptionValueById(COAP_OPTION_ACCEPT,&buff_len,buff);
|
| 846 | if(buff_len > 0)
|
| 847 | {
|
| 848 | *type = (mbtk_content_format_type)atoi((char*)buff);
|
| 849 | return 1;
|
| 850 | }
|
| 851 | else
|
| 852 | {
|
| 853 | return 0;
|
| 854 | }
|
| 855 | }
|
| 856 |
|
| 857 |
|
| 858 | int coapPrintRecvPayload(char *out)
|
| 859 | {
|
| 860 | // print payload
|
| 861 | int payloadLength = g_CoapRecvPointer->getPayloadLength();
|
| 862 | if(payloadLength==0)
|
| 863 | {
|
| 864 | return 0;
|
| 865 | }
|
| 866 | else
|
| 867 | {
|
| 868 | uint8_t* payloadPointer = g_CoapRecvPointer->getPayloadPointer();
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 869 | sprintf(out + strlen(out),":%d\r\n",payloadLength*2);
|
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 870 | for(int j=0; j<payloadLength; j++)
|
| 871 | {
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 872 | sprintf(out + strlen(out),"%.2X", payloadPointer[j]);
|
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 873 | }
|
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 874 | sprintf(out + strlen(out),"\r\n");
|
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 875 | return 1;
|
| 876 | }
|
| 877 | }
|
| 878 |
|
| 879 | const char* coapPrintHumanByIndex(int index)
|
| 880 | {
|
| 881 | if(index == 0)
|
| 882 | {
|
| 883 | createCoapPDU();
|
| 884 | return g_CoapPDU->printHuman();
|
| 885 | }
|
| 886 | else if(index == 1)
|
| 887 | return g_CoapRecvPointer->printHuman();
|
| 888 | return NULL;
|
| 889 | }
|
| 890 |
|
| 891 | const char* coapPrintHexByIndex(int index)
|
| 892 | {
|
| 893 | if(index == 0)
|
| 894 | {
|
| 895 | createCoapPDU();
|
| 896 | return g_CoapPDU->printHex();
|
| 897 | }
|
| 898 | else if(index == 1)
|
| 899 | return g_CoapRecvPointer->printHex();
|
| 900 | return NULL;
|
| 901 | }
|
| 902 |
|
| 903 | //}
|
| 904 |
|
| 905 |
|