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 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
127 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
129 coap_pdu_t *pdu = NULL;
131 // RESET have to use only 4byte (empty message)
132 // and ACKNOWLEDGE can use empty message when code is empty.
133 if (CA_MSG_RESET == info->type || (CA_EMPTY == code && CA_MSG_ACKNOWLEDGE == info->type))
135 OIC_LOG(DEBUG, TAG, "code is empty");
136 if (!(pdu = CAGeneratePDUImpl((code_t) code, NULL, info, endpoint)))
138 OIC_LOG(ERROR, TAG, "pdu NULL");
144 coap_list_t *optlist = NULL;
146 if (CA_MSG_ACKNOWLEDGE != info->type)
148 const char *uri = info->resourceUri;
151 OIC_LOG(ERROR, TAG, "uri NULL");
155 uint32_t length = strlen(uri);
156 if (CA_MAX_URI_LENGTH < length)
158 OIC_LOG(ERROR, TAG, "URI len err");
162 uint32_t uriLength = length + sizeof(COAP_URI_HEADER);
163 char *coapUri = (char *) OICCalloc(1, uriLength);
166 OIC_LOG(ERROR, TAG, "out of memory");
169 OICStrcat(coapUri, uriLength, COAP_URI_HEADER);
170 OICStrcat(coapUri, uriLength, uri);
172 // parsing options in URI
173 CAResult_t res = CAParseURI(coapUri, &optlist);
174 if (CA_STATUS_OK != res)
178 coap_delete_list(optlist);
186 // parsing options in HeadOption
187 CAResult_t ret = CAParseHeadOption(code, info, &optlist);
188 if (CA_STATUS_OK != ret)
190 coap_delete_list(optlist);
194 pdu = CAGeneratePDUImpl((code_t) code, optlist, info, endpoint);
197 OIC_LOG(ERROR, TAG, "pdu NULL");
198 coap_delete_list(optlist);
203 coap_delete_list(optlist);
206 // pdu print method : coap_show_pdu(pdu);
207 OIC_LOG(DEBUG, TAG, "OUT");
211 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode)
213 OIC_LOG(DEBUG, TAG, "IN");
217 OIC_LOG(ERROR, TAG, "data is null");
221 coap_pdu_t *outpdu = coap_new_pdu();
224 OIC_LOG(ERROR, TAG, "outpdu is null");
228 if (0 >= coap_pdu_parse((unsigned char *) data, length, outpdu))
230 OIC_LOG(ERROR, TAG, "pdu parse failed");
231 coap_delete_pdu(outpdu);
235 if (outpdu->hdr->version != COAP_DEFAULT_VERSION)
237 OIC_LOG_V(ERROR, TAG, "coap version is not available : %d",
238 outpdu->hdr->version);
239 coap_delete_pdu(outpdu);
245 (*outCode) = (uint32_t) CA_RESPONSE_CODE(outpdu->hdr->code);
248 OIC_LOG(DEBUG, TAG, "OUT");
252 coap_pdu_t *CAGeneratePDUImpl(code_t code, coap_list_t *options, const CAInfo_t *info,
253 const CAEndpoint_t *endpoint)
255 OIC_LOG(DEBUG, TAG, "IN");
256 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
257 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
259 coap_pdu_t *pdu = coap_new_pdu();
263 OIC_LOG(ERROR, TAG, "malloc failed");
267 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
269 if (0 == info->messageId)
271 /* initialize message id */
272 prng((uint8_t * ) &message_id, sizeof(message_id));
274 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
278 /* use saved message id */
279 message_id = info->messageId;
281 pdu->hdr->id = message_id;
282 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->id);
284 pdu->hdr->type = info->type;
285 pdu->hdr->code = COAP_RESPONSE_CODE(code);
287 if (info->token && CA_EMPTY != code)
289 uint32_t tokenLength = info->tokenLength;
290 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
291 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
293 int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token);
296 OIC_LOG(ERROR, TAG, "can't add token");
302 for (coap_list_t *opt = options; opt; opt = opt->next)
304 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
305 COAP_OPTION_DATA(*(coap_option *) opt->data));
306 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
307 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
308 COAP_OPTION_DATA(*(coap_option *) opt->data));
312 bool enabledPayload = false;
314 enabledPayload = true;
317 if (enabledPayload || CA_ADAPTER_GATT_BTLE == endpoint->adapter)
319 if (NULL != info->payload && 0 < info->payloadSize)
321 OIC_LOG(DEBUG, TAG, "payload is added");
322 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
326 OIC_LOG(DEBUG, TAG, "OUT");
330 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
332 OIC_LOG(DEBUG, TAG, "IN");
336 OIC_LOG(ERROR, TAG, "uriInfo is null");
337 return CA_STATUS_INVALID_PARAM;
340 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
344 OIC_LOG(ERROR, TAG, "optlist is null");
345 return CA_STATUS_INVALID_PARAM;
348 /* split arg into Uri-* options */
350 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
352 if (uri.port != COAP_DEFAULT_PORT)
354 unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 };
355 int ret = coap_insert(optlist,
356 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
357 coap_encode_var_bytes(portbuf, uri.port),
362 return CA_STATUS_INVALID_PARAM;
366 if (uri.path.s && uri.path.length)
368 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
369 COAP_OPTION_URI_PATH, optlist);
370 if (CA_STATUS_OK != ret)
372 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
377 if (uri.query.s && uri.query.length)
379 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
381 if (CA_STATUS_OK != ret)
383 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
388 OIC_LOG(DEBUG, TAG, "OUT");
392 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
393 coap_list_t **optlist)
397 OIC_LOG(ERROR, TAG, "optlist is null");
398 return CA_STATUS_INVALID_PARAM;
401 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
403 // should never occur. Log just in case.
404 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
405 return CA_NOT_SUPPORTED;
407 else if (str && length)
409 unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
410 unsigned char *pBuf = uriBuffer;
411 size_t buflen = sizeof(uriBuffer);
412 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
413 coap_split_query(str, length, pBuf, &buflen);
420 int ret = coap_insert(optlist,
421 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
422 (char *)COAP_OPT_VALUE(pBuf)),
426 return CA_STATUS_INVALID_PARAM;
429 size_t optSize = COAP_OPT_SIZE(pBuf);
430 if ((prevIdx + optSize) < buflen)
439 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
440 return CA_STATUS_FAILED;
445 OIC_LOG(ERROR, TAG, "str or length is not available");
446 return CA_STATUS_FAILED;
452 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
455 OIC_LOG(DEBUG, TAG, "IN");
456 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", CA_STATUS_INVALID_PARAM);
458 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
462 OIC_LOG(ERROR, TAG, "optlist is null");
463 return CA_STATUS_INVALID_PARAM;
466 for (uint32_t i = 0; i < info->numOptions; i++)
468 if(!(info->options + i))
470 OIC_LOG(ERROR, TAG, "options is not available");
471 return CA_STATUS_FAILED;
474 uint32_t id = (info->options + i)->optionID;
475 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
477 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
481 if ((info->options + i)->optionData && (info->options + i)->optionLength > 0)
483 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
484 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
485 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
486 int ret = coap_insert(optlist,
487 CACreateNewOptionNode(id, (info->options + i)->optionLength,
488 (info->options + i)->optionData),
492 return CA_STATUS_INVALID_PARAM;
498 OIC_LOG(DEBUG, TAG, "OUT");
502 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
504 OIC_LOG(DEBUG, TAG, "IN");
508 OIC_LOG(ERROR, TAG, "invalid pointer parameter");
512 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
515 OIC_LOG(ERROR, TAG, "Out of memory");
518 memset(option, 0, sizeof(coap_option) + length + 1);
520 COAP_OPTION_KEY(*option) = key;
521 COAP_OPTION_LENGTH(*option) = length;
522 memcpy(COAP_OPTION_DATA(*option), data, length);
524 /* we can pass NULL here as delete function since option is released automatically */
525 coap_list_t *node = coap_new_listnode(option, NULL);
529 OIC_LOG(ERROR, TAG, "node is NULL");
534 OIC_LOG(DEBUG, TAG, "OUT");
538 int CAOrderOpts(void *a, void *b)
540 OIC_LOG(DEBUG, TAG, "IN");
543 return a < b ? -1 : 1;
546 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
551 OIC_LOG(DEBUG, TAG, "OUT");
552 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
555 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
557 OIC_LOG(DEBUG, TAG, "IN");
561 while ((option = coap_option_next(&opt_iter)))
563 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
564 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
565 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type)
571 OIC_LOG(DEBUG, TAG, "OUT");
575 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo)
577 OIC_LOG(DEBUG, TAG, "IN");
579 if (!pdu || !outCode || !outInfo)
581 OIC_LOG(ERROR, TAG, "NULL pointer param");
582 return CA_STATUS_INVALID_PARAM;
585 coap_opt_iterator_t opt_iter;
586 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL);
590 (*outCode) = (uint32_t) CA_RESPONSE_CODE(pdu->hdr->code);
593 // init HeaderOption list
594 uint32_t count = CAGetOptionCount(opt_iter);
595 memset(outInfo, 0, sizeof(*outInfo));
597 outInfo->numOptions = count;
600 outInfo->type = pdu->hdr->type;
603 outInfo->messageId = pdu->hdr->id;
607 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
608 if (NULL == outInfo->options)
610 OIC_LOG(ERROR, TAG, "Out of memory");
611 return CA_MEMORY_ALLOC_FAILED;
616 char optionResult[CA_MAX_URI_LENGTH] = {0};
618 uint32_t optionLength = 0;
619 bool isfirstsetflag = false;
620 bool isQueryBeingProcessed = false;
622 while ((option = coap_option_next(&opt_iter)))
624 char buf[COAP_MAX_PDU_SIZE] = {0};
625 if (CAGetOptionData((uint8_t *)(COAP_OPT_VALUE(option)),
626 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf)))
628 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
629 uint32_t bufLength = strlen(buf);
630 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
632 if (false == isfirstsetflag)
634 isfirstsetflag = true;
635 optionResult[optionLength] = '/';
637 // Make sure there is enough room in the optionResult buffer
638 if ((optionLength + bufLength) < sizeof(optionResult))
640 memcpy(&optionResult[optionLength], buf, bufLength);
641 optionLength += bufLength;
650 if (COAP_OPTION_URI_PATH == opt_iter.type)
652 // Make sure there is enough room in the optionResult buffer
653 if (optionLength < sizeof(optionResult))
655 optionResult[optionLength] = '/';
663 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
665 if (false == isQueryBeingProcessed)
667 // Make sure there is enough room in the optionResult buffer
668 if (optionLength < sizeof(optionResult))
670 optionResult[optionLength] = '?';
672 isQueryBeingProcessed = true;
681 // Make sure there is enough room in the optionResult buffer
682 if (optionLength < sizeof(optionResult))
684 optionResult[optionLength] = ';';
693 // Make sure there is enough room in the optionResult buffer
694 if ((optionLength + bufLength) < sizeof(optionResult))
696 memcpy(&optionResult[optionLength], buf, bufLength);
697 optionLength += bufLength;
705 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
706 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
708 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
714 uint32_t length = bufLength;
716 if (length <= CA_MAX_HEADER_OPTION_DATA_LENGTH)
718 outInfo->options[idx].optionID = opt_iter.type;
719 outInfo->options[idx].optionLength = length;
720 outInfo->options[idx].protocolID = CA_COAP_ID;
721 memcpy(outInfo->options[idx].optionData, buf, length);
730 if (pdu->hdr->token_length > 0)
732 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", pdu->hdr->token_length);
733 outInfo->token = (char *) OICMalloc(pdu->hdr->token_length);
734 if (NULL == outInfo->token)
736 OIC_LOG(ERROR, TAG, "Out of memory");
737 OICFree(outInfo->options);
738 return CA_MEMORY_ALLOC_FAILED;
740 memcpy(outInfo->token, pdu->hdr->token, pdu->hdr->token_length);
743 outInfo->tokenLength = pdu->hdr->token_length;
748 if (coap_get_data(pdu, &dataSize, &data))
750 OIC_LOG(DEBUG, TAG, "inside pdu->data");
751 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
752 if (NULL == outInfo->payload)
754 OIC_LOG(ERROR, TAG, "Out of memory");
755 OICFree(outInfo->options);
756 OICFree(outInfo->token);
757 return CA_MEMORY_ALLOC_FAILED;
759 memcpy(outInfo->payload, pdu->data, dataSize);
760 outInfo->payloadSize = dataSize;
763 if (optionResult[0] != '\0')
765 OIC_LOG_V(DEBUG, TAG, "URL length:%d", strlen(optionResult));
766 outInfo->resourceUri = OICStrdup(optionResult);
767 if (!outInfo->resourceUri)
769 OIC_LOG(ERROR, TAG, "Out of memory");
770 OICFree(outInfo->options);
771 OICFree(outInfo->token);
772 return CA_MEMORY_ALLOC_FAILED;
776 OIC_LOG(DEBUG, TAG, "OUT");
780 OIC_LOG(ERROR, TAG, "buffer too small");
781 OICFree(outInfo->options);
782 return CA_STATUS_FAILED;
785 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo)
787 OIC_LOG(DEBUG, TAG, "IN");
790 OIC_LOG(ERROR, TAG, "pdu_hdr is null");
791 return CA_STATUS_INVALID_PARAM;
796 OIC_LOG(ERROR, TAG, "outInfo is null");
797 return CA_STATUS_INVALID_PARAM;
801 if (pdu_hdr->token_length > 0)
803 OIC_LOG_V(DEBUG, TAG, "token len:%d", pdu_hdr->token_length);
804 outInfo->token = (char *) OICMalloc(pdu_hdr->token_length);
805 if (NULL == outInfo->token)
807 OIC_LOG(ERROR, TAG, "out of memory");
808 return CA_MEMORY_ALLOC_FAILED;
810 memcpy(outInfo->token, pdu_hdr->token, pdu_hdr->token_length);
813 outInfo->tokenLength = pdu_hdr->token_length;
815 OIC_LOG(DEBUG, TAG, "OUT");
820 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
822 OIC_LOG(DEBUG, TAG, "IN");
826 OIC_LOG(ERROR, TAG, "invalid token pointer");
827 return CA_STATUS_INVALID_PARAM;
830 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
832 OIC_LOG(ERROR, TAG, "invalid token length");
833 return CA_STATUS_INVALID_PARAM;
843 if (SEED == (unsigned int)((time_t)-1))
845 OIC_LOG(ERROR, TAG, "seed is not made");
847 return CA_STATUS_FAILED;
857 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
860 OIC_LOG(ERROR, TAG, "Out of memory");
861 return CA_MEMORY_ALLOC_FAILED;
865 for (uint8_t index = 0; index < tokenLength; index++)
867 // use valid characters
869 temp[index] = rand() & 0x00FF;
871 temp[index] = random() & 0x00FF;
878 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
879 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
881 OIC_LOG(DEBUG, TAG, "OUT");
885 void CADestroyTokenInternal(CAToken_t token)
887 OIC_LOG(DEBUG, TAG, "IN");
889 OIC_LOG(DEBUG, TAG, "OUT");
892 void CADestroyInfo(CAInfo_t *info)
894 OIC_LOG(DEBUG, TAG, "IN");
898 OIC_LOG(DEBUG, TAG, "free options");
899 OICFree(info->options);
901 OIC_LOG(DEBUG, TAG, "free token");
902 OICFree(info->token);
904 OIC_LOG(DEBUG, TAG, "free payload");
905 OICFree(info->payload);
908 OIC_LOG(DEBUG, TAG, "OUT");
911 uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *option, uint32_t buflen)
913 if (0 == buflen || 0 == len)
915 OIC_LOG(ERROR, TAG, "len 0");
919 if (NULL == data || NULL == option)
921 OIC_LOG(ERROR, TAG, "data/option NULL");
927 OIC_LOG(ERROR, TAG, "option buffer too small");
931 memcpy(option, data, len);
937 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
941 OIC_LOG(ERROR, TAG, "pdu is null");
942 return CA_MSG_NONCONFIRM;
945 // pdu minimum size is 4 byte.
946 if (size < CA_PDU_MIN_SIZE)
948 OIC_LOG(ERROR, TAG, "min size");
949 return CA_MSG_NONCONFIRM;
952 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
954 return (CAMessageType_t) hdr->type;
957 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
961 OIC_LOG(ERROR, TAG, "pdu is null");
965 // pdu minimum size is 4 byte.
966 if (size < CA_PDU_MIN_SIZE)
968 OIC_LOG(ERROR, TAG, "min size");
972 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
977 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
981 OIC_LOG(ERROR, TAG, "pdu is null");
985 // pdu minimum size is 4 byte.
986 if (size < CA_PDU_MIN_SIZE)
988 OIC_LOG(ERROR, TAG, "min size");
992 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
994 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->code);