1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 // Defining _BSD_SOURCE or _DEFAULT_SOURCE causes header files to expose
22 // definitions that may otherwise be skipped. Skipping can cause implicit
23 // declaration warnings and/or bugs and subtle problems in code execution.
24 // For glibc information on feature test macros,
25 // Refer http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
27 // This file requires #define use due to random() and srandom()
28 // For details on compatibility and glibc support,
29 // Refer http://www.gnu.org/software/libc/manual/html_node/BSD-Random.html
30 #define _DEFAULT_SOURCE
40 #include "caprotocolmessage.h"
42 #include "oic_malloc.h"
43 #include "oic_string.h"
45 // ARM GCC compiler doesnt define srandom function.
46 #if defined(ARDUINO) && !defined(ARDUINO_ARCH_SAM)
47 #define HAVE_SRANDOM 1
50 #define TAG "CA_PRTCL_MSG"
53 * @def VERIFY_NON_NULL_RET
54 * @brief Macro to verify the validity of input argument
56 #define VERIFY_NON_NULL_RET(arg, log_tag, log_message,ret) \
58 OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
62 #define CA_BUFSIZE (128)
63 #define CA_PDU_MIN_SIZE (4)
64 #define CA_PORT_BUFFER_SIZE (4)
66 static const char COAP_URI_HEADER[] = "coap://[::]/";
68 static unsigned int SEED = 0;
70 CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, CARequestInfo_t *outReqInfo)
72 OIC_LOG(DEBUG, TAG, "IN");
74 if (NULL == pdu || NULL == outReqInfo)
76 OIC_LOG(ERROR, TAG, "parameter is null");
77 return CA_STATUS_INVALID_PARAM;
80 uint32_t code = CA_NOT_FOUND;
81 CAResult_t ret = CAGetInfoFromPDU(pdu, &code, &(outReqInfo->info));
82 outReqInfo->method = code;
84 OIC_LOG(DEBUG, TAG, "OUT");
88 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo)
90 OIC_LOG(DEBUG, TAG, "IN");
92 if (NULL == pdu || NULL == outResInfo)
94 OIC_LOG(ERROR, TAG, "parameter is null");
95 return CA_STATUS_INVALID_PARAM;
98 uint32_t code = CA_NOT_FOUND;
99 CAResult_t ret = CAGetInfoFromPDU(pdu, &code, &(outResInfo->info));
100 outResInfo->result = code;
102 OIC_LOG(DEBUG, TAG, "OUT");
106 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, CAErrorInfo_t *errorInfo)
108 OIC_LOG(DEBUG, TAG, "IN");
112 OIC_LOG(ERROR, TAG, "parameter is null");
113 return CA_STATUS_INVALID_PARAM;
117 CAResult_t ret = CAGetInfoFromPDU(pdu, &code, &errorInfo->info);
118 OIC_LOG(DEBUG, TAG, "OUT");
122 coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint)
124 OIC_LOG(DEBUG, TAG, "IN");
126 coap_pdu_t *pdu = NULL;
128 // RESET have to use only 4byte (empty message)
129 // and ACKNOWLEDGE can use empty message when code is empty.
130 if (CA_MSG_RESET == info->type || (CA_EMPTY == code && CA_MSG_ACKNOWLEDGE == info->type))
132 OIC_LOG(DEBUG, TAG, "code is empty");
133 if (!(pdu = CAGeneratePDUImpl((code_t) code, NULL, info, endpoint)))
135 OIC_LOG(ERROR, TAG, "pdu NULL");
141 coap_list_t *optlist = NULL;
143 if (CA_MSG_ACKNOWLEDGE != info->type)
145 const char *uri = info->resourceUri;
148 OIC_LOG(ERROR, TAG, "uri NULL");
152 uint32_t length = strlen(uri);
153 if (CA_MAX_URI_LENGTH < length)
155 OIC_LOG(ERROR, TAG, "URI len err");
159 uint32_t uriLength = length + sizeof(COAP_URI_HEADER);
160 char *coapUri = (char *) OICCalloc(1, uriLength);
163 OIC_LOG(ERROR, TAG, "out of memory");
166 OICStrcat(coapUri, uriLength, COAP_URI_HEADER);
167 OICStrcat(coapUri, uriLength, uri);
169 // parsing options in URI
170 CAResult_t res = CAParseURI(coapUri, &optlist);
171 if (CA_STATUS_OK != res)
175 coap_delete_list(optlist);
183 // parsing options in HeadOption
184 CAResult_t ret = CAParseHeadOption(code, info, &optlist);
185 if (CA_STATUS_OK != ret)
187 coap_delete_list(optlist);
191 pdu = CAGeneratePDUImpl((code_t) code, optlist, info, endpoint);
194 OIC_LOG(ERROR, TAG, "pdu NULL");
195 coap_delete_list(optlist);
200 coap_delete_list(optlist);
203 // pdu print method : coap_show_pdu(pdu);
204 OIC_LOG(DEBUG, TAG, "OUT");
208 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode)
210 OIC_LOG(DEBUG, TAG, "IN");
214 OIC_LOG(ERROR, TAG, "data is null");
218 coap_pdu_t *outpdu = coap_new_pdu();
221 OIC_LOG(ERROR, TAG, "outpdu is null");
225 if (0 >= coap_pdu_parse((unsigned char *) data, length, outpdu))
227 OIC_LOG(ERROR, TAG, "pdu parse failed");
228 coap_delete_pdu(outpdu);
232 if (outpdu->hdr->version != COAP_DEFAULT_VERSION)
234 OIC_LOG_V(ERROR, TAG, "coap version is not available : %d",
235 outpdu->hdr->version);
236 coap_delete_pdu(outpdu);
242 (*outCode) = (uint32_t) CA_RESPONSE_CODE(outpdu->hdr->code);
245 OIC_LOG(DEBUG, TAG, "OUT");
249 coap_pdu_t *CAGeneratePDUImpl(code_t code, coap_list_t *options, const CAInfo_t *info,
250 const CAEndpoint_t *endpoint)
252 OIC_LOG(DEBUG, TAG, "IN");
253 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", NULL);
255 coap_pdu_t *pdu = coap_new_pdu();
259 OIC_LOG(ERROR, TAG, "malloc failed");
263 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
265 if (0 == info->messageId)
267 /* initialize message id */
268 prng((uint8_t * ) &message_id, sizeof(message_id));
270 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
274 /* use saved message id */
275 message_id = info->messageId;
277 pdu->hdr->id = message_id;
278 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->id);
280 pdu->hdr->type = info->type;
281 pdu->hdr->code = COAP_RESPONSE_CODE(code);
283 if (info->token && CA_EMPTY != code)
285 uint32_t tokenLength = info->tokenLength;
286 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
287 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
289 int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token);
292 OIC_LOG(ERROR, TAG, "can't add token");
298 for (coap_list_t *opt = options; opt; opt = opt->next)
300 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
301 COAP_OPTION_DATA(*(coap_option *) opt->data));
302 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
303 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
304 COAP_OPTION_DATA(*(coap_option *) opt->data));
308 bool enabledPayload = false;
310 enabledPayload = true;
313 if (enabledPayload || CA_ADAPTER_GATT_BTLE == endpoint->adapter)
315 if (NULL != info->payload && 0 < info->payloadSize)
317 OIC_LOG(DEBUG, TAG, "payload is added");
318 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
322 OIC_LOG(DEBUG, TAG, "OUT");
326 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
328 OIC_LOG(DEBUG, TAG, "IN");
332 OIC_LOG(ERROR, TAG, "uriInfo is null");
333 return CA_STATUS_INVALID_PARAM;
336 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
340 OIC_LOG(ERROR, TAG, "optlist is null");
341 return CA_STATUS_INVALID_PARAM;
344 /* split arg into Uri-* options */
346 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
348 if (uri.port != COAP_DEFAULT_PORT)
350 unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 };
351 int ret = coap_insert(optlist,
352 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
353 coap_encode_var_bytes(portbuf, uri.port),
358 return CA_STATUS_INVALID_PARAM;
362 if (uri.path.s && uri.path.length)
364 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
365 COAP_OPTION_URI_PATH, optlist);
366 if (CA_STATUS_OK != ret)
368 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
373 if (uri.query.s && uri.query.length)
375 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
377 if (CA_STATUS_OK != ret)
379 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
384 OIC_LOG(DEBUG, TAG, "OUT");
388 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
389 coap_list_t **optlist)
393 OIC_LOG(ERROR, TAG, "optlist is null");
394 return CA_STATUS_INVALID_PARAM;
397 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
399 // should never occur. Log just in case.
400 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
401 return CA_NOT_SUPPORTED;
403 else if (str && length)
405 unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
406 unsigned char *pBuf = uriBuffer;
407 size_t buflen = sizeof(uriBuffer);
408 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
409 coap_split_query(str, length, pBuf, &buflen);
416 int ret = coap_insert(optlist,
417 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
418 (char *)COAP_OPT_VALUE(pBuf)),
422 return CA_STATUS_INVALID_PARAM;
425 size_t optSize = COAP_OPT_SIZE(pBuf);
426 if ((prevIdx + optSize) < buflen)
435 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
436 return CA_STATUS_FAILED;
441 OIC_LOG(ERROR, TAG, "str or length is not available");
442 return CA_STATUS_FAILED;
448 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
451 OIC_LOG(DEBUG, TAG, "IN");
452 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", CA_STATUS_INVALID_PARAM);
454 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
458 OIC_LOG(ERROR, TAG, "optlist is null");
459 return CA_STATUS_INVALID_PARAM;
462 for (uint32_t i = 0; i < info->numOptions; i++)
464 if(!(info->options + i))
466 OIC_LOG(ERROR, TAG, "options is not available");
467 return CA_STATUS_FAILED;
470 uint32_t id = (info->options + i)->optionID;
471 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
473 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
477 if ((info->options + i)->optionData && (info->options + i)->optionLength > 0)
479 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
480 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
481 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
482 int ret = coap_insert(optlist,
483 CACreateNewOptionNode(id, (info->options + i)->optionLength,
484 (info->options + i)->optionData),
488 return CA_STATUS_INVALID_PARAM;
494 OIC_LOG(DEBUG, TAG, "OUT");
498 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
500 OIC_LOG(DEBUG, TAG, "IN");
504 OIC_LOG(ERROR, TAG, "invalid pointer parameter");
508 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
511 OIC_LOG(ERROR, TAG, "Out of memory");
514 memset(option, 0, sizeof(coap_option) + length + 1);
516 COAP_OPTION_KEY(*option) = key;
517 COAP_OPTION_LENGTH(*option) = length;
518 memcpy(COAP_OPTION_DATA(*option), data, length);
520 /* we can pass NULL here as delete function since option is released automatically */
521 coap_list_t *node = coap_new_listnode(option, NULL);
525 OIC_LOG(ERROR, TAG, "node is NULL");
530 OIC_LOG(DEBUG, TAG, "OUT");
534 int CAOrderOpts(void *a, void *b)
536 OIC_LOG(DEBUG, TAG, "IN");
539 return a < b ? -1 : 1;
542 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
547 OIC_LOG(DEBUG, TAG, "OUT");
548 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
551 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
553 OIC_LOG(DEBUG, TAG, "IN");
557 while ((option = coap_option_next(&opt_iter)))
559 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
560 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
561 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type)
567 OIC_LOG(DEBUG, TAG, "OUT");
571 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo)
573 OIC_LOG(DEBUG, TAG, "IN");
575 if (!pdu || !outCode || !outInfo)
577 OIC_LOG(ERROR, TAG, "NULL pointer param");
578 return CA_STATUS_INVALID_PARAM;
581 coap_opt_iterator_t opt_iter;
582 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL);
586 (*outCode) = (uint32_t) CA_RESPONSE_CODE(pdu->hdr->code);
589 // init HeaderOption list
590 uint32_t count = CAGetOptionCount(opt_iter);
591 memset(outInfo, 0, sizeof(*outInfo));
593 outInfo->numOptions = count;
596 outInfo->type = pdu->hdr->type;
599 outInfo->messageId = pdu->hdr->id;
603 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
604 if (NULL == outInfo->options)
606 OIC_LOG(ERROR, TAG, "Out of memory");
607 return CA_MEMORY_ALLOC_FAILED;
612 char optionResult[CA_MAX_URI_LENGTH] = {0};
614 uint32_t optionLength = 0;
615 bool isfirstsetflag = false;
616 bool isQueryBeingProcessed = false;
618 while ((option = coap_option_next(&opt_iter)))
620 char buf[COAP_MAX_PDU_SIZE] = {0};
621 if (CAGetOptionData((uint8_t *)(COAP_OPT_VALUE(option)),
622 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf)))
624 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
625 uint32_t bufLength = strlen(buf);
626 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
628 if (false == isfirstsetflag)
630 isfirstsetflag = true;
631 optionResult[optionLength] = '/';
633 // Make sure there is enough room in the optionResult buffer
634 if ((optionLength + bufLength) < sizeof(optionResult))
636 memcpy(&optionResult[optionLength], buf, bufLength);
637 optionLength += bufLength;
646 if (COAP_OPTION_URI_PATH == opt_iter.type)
648 // Make sure there is enough room in the optionResult buffer
649 if (optionLength < sizeof(optionResult))
651 optionResult[optionLength] = '/';
659 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
661 if (false == isQueryBeingProcessed)
663 // Make sure there is enough room in the optionResult buffer
664 if (optionLength < sizeof(optionResult))
666 optionResult[optionLength] = '?';
668 isQueryBeingProcessed = true;
677 // Make sure there is enough room in the optionResult buffer
678 if (optionLength < sizeof(optionResult))
680 optionResult[optionLength] = ';';
689 // Make sure there is enough room in the optionResult buffer
690 if ((optionLength + bufLength) < sizeof(optionResult))
692 memcpy(&optionResult[optionLength], buf, bufLength);
693 optionLength += bufLength;
701 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
702 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
704 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
710 uint32_t length = bufLength;
712 if (length <= CA_MAX_HEADER_OPTION_DATA_LENGTH)
714 outInfo->options[idx].optionID = opt_iter.type;
715 outInfo->options[idx].optionLength = length;
716 outInfo->options[idx].protocolID = CA_COAP_ID;
717 memcpy(outInfo->options[idx].optionData, buf, length);
726 if (pdu->hdr->token_length > 0)
728 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", pdu->hdr->token_length);
729 outInfo->token = (char *) OICMalloc(pdu->hdr->token_length);
730 if (NULL == outInfo->token)
732 OIC_LOG(ERROR, TAG, "Out of memory");
733 OICFree(outInfo->options);
734 return CA_MEMORY_ALLOC_FAILED;
736 memcpy(outInfo->token, pdu->hdr->token, pdu->hdr->token_length);
739 outInfo->tokenLength = pdu->hdr->token_length;
744 if (coap_get_data(pdu, &dataSize, &data))
746 OIC_LOG(DEBUG, TAG, "inside pdu->data");
747 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
748 if (NULL == outInfo->payload)
750 OIC_LOG(ERROR, TAG, "Out of memory");
751 OICFree(outInfo->options);
752 OICFree(outInfo->token);
753 return CA_MEMORY_ALLOC_FAILED;
755 memcpy(outInfo->payload, pdu->data, dataSize);
756 outInfo->payloadSize = dataSize;
759 if (optionResult[0] != '\0')
761 OIC_LOG_V(DEBUG, TAG, "URL length:%d", strlen(optionResult));
762 outInfo->resourceUri = OICStrdup(optionResult);
763 if (!outInfo->resourceUri)
765 OIC_LOG(ERROR, TAG, "Out of memory");
766 OICFree(outInfo->options);
767 OICFree(outInfo->token);
768 return CA_MEMORY_ALLOC_FAILED;
772 OIC_LOG(DEBUG, TAG, "OUT");
776 OIC_LOG(ERROR, TAG, "buffer too small");
777 OICFree(outInfo->options);
778 return CA_STATUS_FAILED;
781 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo)
783 OIC_LOG(DEBUG, TAG, "IN");
786 OIC_LOG(ERROR, TAG, "pdu_hdr is null");
787 return CA_STATUS_INVALID_PARAM;
792 OIC_LOG(ERROR, TAG, "outInfo is null");
793 return CA_STATUS_INVALID_PARAM;
797 if (pdu_hdr->token_length > 0)
799 OIC_LOG_V(DEBUG, TAG, "token len:%d", pdu_hdr->token_length);
800 outInfo->token = (char *) OICMalloc(pdu_hdr->token_length);
801 if (NULL == outInfo->token)
803 OIC_LOG(ERROR, TAG, "out of memory");
804 return CA_MEMORY_ALLOC_FAILED;
806 memcpy(outInfo->token, pdu_hdr->token, pdu_hdr->token_length);
809 outInfo->tokenLength = pdu_hdr->token_length;
811 OIC_LOG(DEBUG, TAG, "OUT");
816 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
818 OIC_LOG(DEBUG, TAG, "IN");
822 OIC_LOG(ERROR, TAG, "invalid token pointer");
823 return CA_STATUS_INVALID_PARAM;
826 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
828 OIC_LOG(ERROR, TAG, "invalid token length");
829 return CA_STATUS_INVALID_PARAM;
839 if (SEED == (unsigned int)((time_t)-1))
841 OIC_LOG(ERROR, TAG, "seed is not made");
843 return CA_STATUS_FAILED;
853 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
856 OIC_LOG(ERROR, TAG, "Out of memory");
857 return CA_MEMORY_ALLOC_FAILED;
861 for (uint8_t index = 0; index < tokenLength; index++)
863 // use valid characters
865 temp[index] = rand() & 0x00FF;
867 temp[index] = random() & 0x00FF;
874 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
875 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
877 OIC_LOG(DEBUG, TAG, "OUT");
881 void CADestroyTokenInternal(CAToken_t token)
883 OIC_LOG(DEBUG, TAG, "IN");
885 OIC_LOG(DEBUG, TAG, "OUT");
888 void CADestroyInfo(CAInfo_t *info)
890 OIC_LOG(DEBUG, TAG, "IN");
894 OIC_LOG(DEBUG, TAG, "free options");
895 OICFree(info->options);
897 OIC_LOG(DEBUG, TAG, "free token");
898 OICFree(info->token);
900 OIC_LOG(DEBUG, TAG, "free payload");
901 OICFree(info->payload);
904 OIC_LOG(DEBUG, TAG, "OUT");
907 uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *option, uint32_t buflen)
909 if (0 == buflen || 0 == len)
911 OIC_LOG(ERROR, TAG, "len 0");
915 if (NULL == data || NULL == option)
917 OIC_LOG(ERROR, TAG, "data/option NULL");
923 OIC_LOG(ERROR, TAG, "option buffer too small");
927 memcpy(option, data, len);
933 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
937 OIC_LOG(ERROR, TAG, "pdu is null");
938 return CA_MSG_NONCONFIRM;
941 // pdu minimum size is 4 byte.
942 if (size < CA_PDU_MIN_SIZE)
944 OIC_LOG(ERROR, TAG, "min size");
945 return CA_MSG_NONCONFIRM;
948 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
950 return (CAMessageType_t) hdr->type;
953 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
957 OIC_LOG(ERROR, TAG, "pdu is null");
961 // pdu minimum size is 4 byte.
962 if (size < CA_PDU_MIN_SIZE)
964 OIC_LOG(ERROR, TAG, "min size");
968 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
973 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
977 OIC_LOG(ERROR, TAG, "pdu is null");
981 // pdu minimum size is 4 byte.
982 if (size < CA_PDU_MIN_SIZE)
984 OIC_LOG(ERROR, TAG, "min size");
988 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
990 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->code);