blob: 0a6dc19f2d5d8706328aa67dbbd60212afed4a40 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/*====*====*====*====*====*====*====*====*====*====*====*====*====*====*====*
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
27static CoapPDU * g_CoapPDU = NULL;
28static CoapPDU * g_CoapRecvPointer = NULL;
29
30static void createCoapPDU()
31{
32 if(g_CoapPDU == NULL)
33 g_CoapPDU = new CoapPDU();
34}
35
36void coapReleaseCoapPUD()
37{
38 if(g_CoapPDU)
39 {
40 delete g_CoapPDU;
41 g_CoapPDU = NULL;
42 }
43}
44
45int coapReset()
46{
47 createCoapPDU();
48 return g_CoapPDU->reset();
49}
50
51int coapValidate()
52{
53 createCoapPDU();
54 return g_CoapPDU->validate();
55}
56
57
58uint8_t* coapGetPDUPointer()
59{
60 return g_CoapPDU->getPDUPointer();
61}
62
63void coapSetPDULength(int len)
64{
65 createCoapPDU();
66 g_CoapPDU->setPDULength(len);
67}
68
69int coapSetURI(char *uri)
70{
71 createCoapPDU();
72 return g_CoapPDU->setURI(uri);
73}
74
75int coapAddURIQuery(char *query)
76{
77 createCoapPDU();
78 return g_CoapPDU->addURIQuery(query);
79}
80
81int coapGetURI(char *dst, int dstlen, int *outLen)
82{
83 createCoapPDU();
84 return g_CoapPDU->getURI(dst, dstlen, outLen);
85}
86
87int coapSetVersion(uint8_t version)
88{
89 createCoapPDU();
90 return g_CoapPDU->setVersion(version);
91}
92
93uint8_t coapGetVersion()
94{
95 createCoapPDU();
96 return g_CoapPDU->getVersion();
97}
98
99void coapSetType(mbtk_coap_type mt)
100{
101 createCoapPDU();
102 g_CoapPDU->setType(mt);
103}
104
105mbtk_coap_type coapGetType()
106{
107 createCoapPDU();
108 return (mbtk_coap_type)g_CoapPDU->getType();
109}
110
111int GetTokenLength(uint8_t tokenLength)
112{
113 createCoapPDU();
114 return g_CoapPDU->setTokenLength(tokenLength);
115}
116
117int coapGetTokenLength()
118{
119 createCoapPDU();
120 return g_CoapPDU->getTokenLength();
121}
122
123uint8_t* coapGetTokenPointer()
124{
125 createCoapPDU();
126 return g_CoapPDU->getTokenPointer();
127}
128
129int coapSetToken(uint8_t *token, uint8_t tokenLength)
130{
131 createCoapPDU();
132 return g_CoapPDU->setToken(token, tokenLength) ;
133}
134
135void coapSetCode(mbtk_coap_code_type code)
136{
137 createCoapPDU();
138 g_CoapPDU->setCode(code);
139}
140
141mbtk_coap_code_type coapGetCode()
142{
143 createCoapPDU();
144 return (mbtk_coap_code_type)g_CoapPDU->getCode();
145}
146
147mbtk_coap_code_type coapHttpStatusToCode(int httpStatus)
148{
149 createCoapPDU();
150 return (mbtk_coap_code_type)g_CoapPDU->httpStatusToCode(httpStatus);
151}
152
153int coapSetMessageID(uint16_t messageID)
154{
155 createCoapPDU();
156 return g_CoapPDU->setMessageID(messageID);
157}
158
159uint16_t coapGetMessageID()
160{
161 createCoapPDU();
162 return g_CoapPDU->getMessageID();
163}
164
165/// Returns the length of the PDU.
166int coapGetPDULength()
167{
168 createCoapPDU();
169 return g_CoapPDU->getPDULength();
170}
171
172/// Return the number of options that the PDU has.
173int coapGetNumOptions()
174{
175 createCoapPDU();
176 return g_CoapPDU->getNumOptions();
177}
178
179//CoapOption* getOptions() {
180// return g_CoapPDU->getOptions();
181//}
182
183
184int coapAddOption(uint16_t insertedOptionNumber, uint16_t optionValueLength, uint8_t *optionValue)
185{
186 createCoapPDU();
187 return g_CoapPDU->addOption(insertedOptionNumber, optionValueLength, optionValue);
188}
189
190uint8_t* coapMallocPayload(int len)
191{
192 createCoapPDU();
193 return g_CoapPDU->mallocPayload(len);
194}
195
196int 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.
203uint8_t* coapGetPayloadPointer()
204{
205 createCoapPDU();
206 return g_CoapPDU->getPayloadPointer();
207}
208
209/// Gets the length of the payload buffer.
210int 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).
217uint8_t* coapGetPayloadCopy()
218{
219 createCoapPDU();
220 return g_CoapPDU->getPayloadCopy();
221}
222
223int coapSetContentFormat(mbtk_content_format_type format)
224{
225 createCoapPDU();
226 return g_CoapPDU->setContentFormat(format);
227}
228
229int coapHasOption(uint16_t optionNumber )
230{
231 createCoapPDU();
232 return g_CoapPDU->hasOption(optionNumber);
233}
234void coapDeleteRecv()
235{
236 if(g_CoapRecvPointer != NULL)
237 {
238 delete g_CoapRecvPointer;
239 g_CoapRecvPointer = NULL;
240 }
241
242}
243int 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
262void 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 }
283 char code[30];
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 {
386 sprintf(outBuffer,"%sNo token\r\n",outBuffer);
387 }
388 else
389 {
390 sprintf(outBuffer,"%sToken of %d bytes.\r\n"
391 "Value: %s\r\n",
392 outBuffer,tokenLength, (char*)tokenPointer);
393 for(int j=0; j<tokenLength; j++)
394 {
395 sprintf(outBuffer,"%s%.2x",outBuffer, tokenPointer[j]);
396 }
397 }
398 // print options
399 CoapPDU::CoapOption* options = g_CoapRecvPointer->getOptions();
400 if(options==NULL)
401 {
402 sprintf(outBuffer,"\r\n%sNO options\r\n", outBuffer);
403 }
404
405 for(int i=0; i<g_CoapRecvPointer->getNumOptions(); i++)
406 {
407 char optionNumberBuffer[20];
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);*/
482 sprintf(outBuffer,"\r\n%sOPTION (%d/%d)\r\n"
483 "Option number (delta): %hu (%hu)\r\n"
484 "Name: %s\r\n"
485 "Value length: %u\r\n"
486 "Value: \"%s",
487 outBuffer,
488 i + 1,g_CoapRecvPointer->getNumOptions(),
489 options[i].optionNumber,options[i].optionDelta,
490 optionNumberBuffer,
491 options[i].optionValueLength,
492 (char *)options[i].optionValuePointer);
493
494 }
495 if(options)
496 free(options);
497 // print payload
498 if(g_CoapRecvPointer->getPayloadLength() == 0)
499 {
500 sprintf(outBuffer,"%sNo payload\r\n", outBuffer);
501 }
502 else
503 {
504 sprintf(outBuffer, "%sPayload of %d bytes\r\n"
505 "Value: \"%s\"\r\n", outBuffer,
506 g_CoapRecvPointer->getPayloadLength(),
507 (char *)g_CoapRecvPointer->getPayloadPointer());
508 /*sprintf(outBuffer,"%sPayload of %d bytes\r\n", outBuffer,g_CoapRecvPointer->getPayloadLength());
509 sprintf(outBuffer, "%sValue: \"", outBuffer);
510 uint8_t* _payloadPointer = g_CoapRecvPointer->getPayloadPointer();
511 for(int j=0; j<g_CoapRecvPointer->getPayloadLength(); j++) {
512 char c = _payloadPointer[j];
513 if((c>='!'&&c<='~')||c==' ') {
514 sprintf(outBuffer, "%s%c", outBuffer,c);
515 } else {
516 sprintf(outBuffer, "%s\\%.2x",outBuffer,c);
517 }
518 }
519 sprintf(outBuffer, "%s\"\r\n", outBuffer);*/
520 }
521
522}
523
524void coapGetOptionValueById(uint16_t optionNumber, uint16_t * optionValueLength, uint8_t * optionValuePointer)
525{
526 if(g_CoapRecvPointer)
527 g_CoapRecvPointer->getOptionValueById(optionNumber, optionValueLength, optionValuePointer);
528}
529
530uint16_t coapGetRecvMessageID()
531{
532 return g_CoapRecvPointer->getMessageID();
533}
534
535mbtk_coap_type coapGetRecvType()
536{
537 return (mbtk_coap_type)g_CoapRecvPointer->getType();
538}
539
540mbtk_coap_code_type coapGetRecvCode()
541{
542 return (mbtk_coap_code_type)g_CoapRecvPointer->getCode();
543}
544
545int mbtk_coap_get_pdu_Length()
546{
547 return g_CoapRecvPointer->getPDULength();
548}
549
550
551int coapGetRecvTokenLength()
552{
553 return g_CoapRecvPointer->getTokenLength();
554}
555
556uint8_t* coapGetRecvTokenPointer()
557{
558 return g_CoapRecvPointer->getTokenPointer();
559}
560
561/// Returns a pointer to the payload buffer.
562uint8_t* coapGetRecvPayloadPointer()
563{
564 return g_CoapRecvPointer->getPayloadPointer();
565}
566
567/// Gets the length of the payload buffer.
568int coapGetRecvPayloadLength()
569{
570 return g_CoapRecvPointer->getPayloadLength();
571}
572
573uint8_t coapGetRecvVersion()
574{
575 return g_CoapRecvPointer->getVersion();
576}
577
578// Return If-Match length,or 0 if fail.
579uint16_t coapGetRecvOptionIfMatch(uint16_t *optionValueLength, uint8_t *optionValuePointer)
580{
581 *optionValueLength = 0;
582 coapGetOptionValueById(COAP_OPTION_IF_MATCH,optionValueLength,optionValuePointer);
583 return *optionValueLength;
584}
585
586// Return Uri-Host length,or 0 if fail.
587uint16_t coapGetRecvOptionUriHost(uint16_t *optionValueLength, uint8_t *optionValuePointer)
588{
589 *optionValueLength = 0;
590 coapGetOptionValueById(COAP_OPTION_URI_HOST,optionValueLength,optionValuePointer);
591 return *optionValueLength;
592}
593
594// Return ETag length,or 0 if fail.
595uint16_t coapGetRecvOptionETag(uint16_t *optionValueLength, uint8_t *optionValuePointer)
596{
597 *optionValueLength = 0;
598 coapGetOptionValueById(COAP_OPTION_ETAG,optionValueLength,optionValuePointer);
599 return *optionValueLength;
600}
601
602// Return If-None-Match length,or 0 if fail.
603uint16_t coapGetRecvOptionIfNoneMatch(uint16_t *optionValueLength, uint8_t *optionValuePointer)
604{
605 *optionValueLength = 0;
606 coapGetOptionValueById(COAP_OPTION_IF_NONE_MATCH,optionValueLength,optionValuePointer);
607 return *optionValueLength;
608}
609
610// Return Location-Path length,or 0 if fail.
611uint16_t coapGetRecvOptionLocationPath(uint16_t *optionValueLength, uint8_t *optionValuePointer)
612{
613 *optionValueLength = 0;
614 coapGetOptionValueById(COAP_OPTION_LOCATION_PATH,optionValueLength,optionValuePointer);
615 return *optionValueLength;
616}
617
618// Return Location-Query length,or 0 if fail.
619uint16_t coapGetRecvOptionLocationQuery(uint16_t *optionValueLength, uint8_t *optionValuePointer)
620{
621 *optionValueLength = 0;
622 coapGetOptionValueById(COAP_OPTION_LOCATION_QUERY,optionValueLength,optionValuePointer);
623 return *optionValueLength;
624}
625
626// Return Proxy-Uri length,or 0 if fail.
627uint16_t coapGetRecvOptionProxyUri(uint16_t *optionValueLength, uint8_t *optionValuePointer)
628{
629 *optionValueLength = 0;
630 coapGetOptionValueById(COAP_OPTION_PROXY_URI,optionValueLength,optionValuePointer);
631 return *optionValueLength;
632}
633
634// Return Proxy-Scheme length,or 0 if fail.
635uint16_t coapGetRecvOptionProxyScheme(uint16_t *optionValueLength, uint8_t *optionValuePointer)
636{
637 *optionValueLength = 0;
638 coapGetOptionValueById(COAP_OPTION_PROXY_SCHEME,optionValueLength,optionValuePointer);
639 return *optionValueLength;
640}
641
642// Return Uri-Path length,or 0 if fail.
643uint16_t coapGetRecvOptionUriPath(uint16_t *optionValueLength, uint8_t *optionValuePointer)
644{
645 *optionValueLength = 0;
646 coapGetOptionValueById(COAP_OPTION_URI_PATH,optionValueLength,optionValuePointer);
647 return *optionValueLength;
648}
649
650// Return Uri-Query length,or 0 if fail.
651uint16_t coapGetRecvOptionUriQuery(uint16_t *optionValueLength, uint8_t *optionValuePointer)
652{
653 *optionValueLength = 0;
654 coapGetOptionValueById(COAP_OPTION_URI_QUERY,optionValueLength,optionValuePointer);
655 return *optionValueLength;
656}
657
658// Return 1 if get Observe success,or 0 if fail.
659uint16_t coapGetRecvOptionObserve(uint16_t *observe)
660{
661 uint8_t buff[10];
662 uint16_t buff_len = 0;
663 memset(buff,0x0,10);
664 coapGetOptionValueById(COAP_OPTION_OBSERVE,&buff_len,buff);
665 if(buff_len > 0)
666 {
667 *observe = (uint16_t)atoi((char*)buff);
668 return 1;
669 }
670 else
671 {
672 return 0;
673 }
674}
675
676// Return 1 if get Max-Age success,or 0 if fail.
677uint16_t coapGetRecvOptionMaxAge(uint16_t *max_age)
678{
679 uint8_t buff[10];
680 uint16_t buff_len = 0;
681 memset(buff,0x0,10);
682 coapGetOptionValueById(COAP_OPTION_MAX_AGE,&buff_len,buff);
683 if(buff_len > 0)
684 {
685 *max_age = (uint16_t)atoi((char*)buff);
686 return 1;
687 }
688 else
689 {
690 return 0;
691 }
692}
693
694// Return 1 if get Uri-Port success,or 0 if fail.
695uint16_t coapGetRecvOptionUriPort(uint16_t *uri_port)
696{
697 uint8_t buff[10];
698 uint16_t buff_len = 0;
699 memset(buff,0x0,10);
700 coapGetOptionValueById(COAP_OPTION_URI_PORT,&buff_len,buff);
701 if(buff_len > 0)
702 {
703 *uri_port = (uint16_t)atoi((char*)buff);
704 return 1;
705 }
706 else
707 {
708 return 0;
709 }
710}
711
712// Return 1 if get Size2 success,or 0 if fail.
713uint16_t coapGetRecvOptionSize2(uint16_t *size2)
714{
715 uint8_t buff[10];
716 uint16_t buff_len = 0;
717 memset(buff,0x0,10);
718 coapGetOptionValueById(COAP_OPTION_SIZE2,&buff_len,buff);
719 if(buff_len > 0)
720 {
721 *size2 = (uint16_t)atoi((char*)buff);
722 return 1;
723 }
724 else
725 {
726 return 0;
727 }
728}
729
730// Return 1 if get Size1 success,or 0 if fail.
731uint16_t coapGetRecvOptionSize1(uint16_t *size1)
732{
733 uint8_t buff[10];
734 uint16_t buff_len = 0;
735 memset(buff,0x0,10);
736 coapGetOptionValueById(COAP_OPTION_SIZE1,&buff_len,buff);
737 if(buff_len > 0)
738 {
739 *size1 = (uint16_t)atoi((char*)buff);
740 return 1;
741 }
742 else
743 {
744 return 0;
745 }
746}
747
748// Return 1 if get Block2 success,or 0 if fail.
749uint16_t coapGetRecvOptionBlock2(uint32_t *number,uint8_t *more_flag,uint8_t *size)
750{
751 uint8_t buff[10];
752 uint16_t buff_len = 0;
753 memset(buff,0x0,10);
754 coapGetOptionValueById(COAP_OPTION_BLOCK2,&buff_len,buff);
755 if(buff_len > 0)
756 {
757 if(buff_len == 1)
758 {
759 *size = (uint8_t)(buff[0] & 0x07);
760 *more_flag = (uint8_t)((buff[0] & 0x08) >> 3);
761 *number = (uint32_t)((buff[0] & 0xF0) >> 4);
762 }
763 else if(buff_len == 2)
764 {
765 *size = (uint8_t)(buff[1] & 0x07);
766 *more_flag = (uint8_t)((buff[1] & 0x08) >> 3);
767 *number = (uint32_t)(((buff[0] << 8) | (buff[1] & 0xF0)) >> 4);
768 }
769 else if(buff_len == 3)
770 {
771 *size = (uint8_t)(buff[2] & 0x07);
772 *more_flag = (uint8_t)((buff[2] & 0x08) >> 3);
773 *number = (uint32_t)(((buff[0] << 16) | (buff[1] << 8) | (buff[2] & 0xF0)) >> 4);
774 }
775 else
776 {
777 return 0;
778 }
779 return 1;
780 }
781 else
782 {
783 return 0;
784 }
785}
786
787// Return 1 if get Block1 success,or 0 if fail.
788uint16_t coapGetRecvOptionBlock1(uint32_t *number,uint8_t *more_flag,uint8_t *size)
789{
790 uint8_t buff[10];
791 uint16_t buff_len = 0;
792 memset(buff,0x0,10);
793 coapGetOptionValueById(COAP_OPTION_BLOCK1,&buff_len,buff);
794 if(buff_len > 0)
795 {
796 *size = (uint8_t)(buff[0] & 0x07);
797 *more_flag = (uint8_t)(buff[0] & 0x08);
798 if(buff_len == 1)
799 {
800 *number = (uint32_t)(buff[0] & 0xF0);
801 }
802 else if(buff_len == 2)
803 {
804 *number = (uint32_t)((buff[1] << 8) | (buff[0] & 0xF0));
805 }
806 else if(buff_len == 3)
807 {
808 *number = (uint32_t)((buff[2] << 16) | (buff[1] << 8) | (buff[0] & 0xF0));
809 }
810 else
811 {
812 return 0;
813 }
814 return 1;
815 }
816 else
817 {
818 return 0;
819 }
820}
821
822// Return 1 if get Content-Format success,or 0 if fail.
823uint16_t coapGetRecvOptionContentFormat(mbtk_content_format_type *type)
824{
825 uint8_t buff[10];
826 uint16_t buff_len = 0;
827 memset(buff,0x0,10);
828 coapGetOptionValueById(COAP_OPTION_CONTENT_FORMAT,&buff_len,buff);
829 if(buff_len > 0)
830 {
831 *type = (mbtk_content_format_type)atoi((char*)buff);
832 return 1;
833 }
834 else
835 {
836 return 0;
837 }
838}
839
840// Return 1 if get Accept success,or 0 if fail.
841uint16_t coapGetRecvOptionAccept(mbtk_content_format_type *type)
842{
843 uint8_t buff[10];
844 uint16_t buff_len = 0;
845 memset(buff,0x0,10);
846 coapGetOptionValueById(COAP_OPTION_ACCEPT,&buff_len,buff);
847 if(buff_len > 0)
848 {
849 *type = (mbtk_content_format_type)atoi((char*)buff);
850 return 1;
851 }
852 else
853 {
854 return 0;
855 }
856}
857
858
859int coapPrintRecvPayload(char *out)
860{
861 // print payload
862 int payloadLength = g_CoapRecvPointer->getPayloadLength();
863 if(payloadLength==0)
864 {
865 return 0;
866 }
867 else
868 {
869 uint8_t* payloadPointer = g_CoapRecvPointer->getPayloadPointer();
870 sprintf(out,"%s:%d\r\n",out, payloadLength*2);
871 for(int j=0; j<payloadLength; j++)
872 {
873 sprintf(out,"%s%.2X",out,payloadPointer[j]);
874 }
875 sprintf(out,"%s\r\n",out);
876 return 1;
877 }
878}
879
880const char* coapPrintHumanByIndex(int index)
881{
882 if(index == 0)
883 {
884 createCoapPDU();
885 return g_CoapPDU->printHuman();
886 }
887 else if(index == 1)
888 return g_CoapRecvPointer->printHuman();
889 return NULL;
890}
891
892const char* coapPrintHexByIndex(int index)
893{
894 if(index == 0)
895 {
896 createCoapPDU();
897 return g_CoapPDU->printHex();
898 }
899 else if(index == 1)
900 return g_CoapRecvPointer->printHex();
901 return NULL;
902}
903
904//}
905
906