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)
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, NULL, 0)))
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);
190 pdu = CAGeneratePDUImpl((code_t) code, optlist, info, info->payload, info->payloadSize);
193 OIC_LOG(ERROR, TAG, "pdu NULL");
194 coap_delete_list(optlist);
199 coap_delete_list(optlist);
202 // pdu print method : coap_show_pdu(pdu);
203 OIC_LOG(DEBUG, TAG, "OUT");
207 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode)
209 OIC_LOG(DEBUG, TAG, "IN");
213 OIC_LOG(ERROR, TAG, "data is null");
217 coap_pdu_t *outpdu = coap_new_pdu();
220 OIC_LOG(ERROR, TAG, "outpdu is null");
224 if (0 >= coap_pdu_parse((unsigned char *) data, length, outpdu))
226 OIC_LOG(ERROR, TAG, "pdu parse failed");
227 coap_delete_pdu(outpdu);
233 (*outCode) = (uint32_t) CA_RESPONSE_CODE(outpdu->hdr->code);
236 OIC_LOG(DEBUG, TAG, "OUT");
240 coap_pdu_t *CAGeneratePDUImpl(code_t code, coap_list_t *options, const CAInfo_t *info,
241 const uint8_t *payload, size_t payloadSize)
243 OIC_LOG(DEBUG, TAG, "IN");
244 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", NULL);
246 coap_pdu_t *pdu = coap_new_pdu();
250 OIC_LOG(ERROR, TAG, "malloc failed");
254 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
256 if (0 == info->messageId)
258 /* initialize message id */
259 prng((uint8_t * ) &message_id, sizeof(message_id));
261 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
265 /* use saved message id */
266 message_id = info->messageId;
268 pdu->hdr->id = message_id;
269 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->id);
271 pdu->hdr->type = info->type;
272 pdu->hdr->code = COAP_RESPONSE_CODE(code);
274 if (info->token && CA_EMPTY != code)
276 uint32_t tokenLength = info->tokenLength;
277 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
278 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
280 int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token);
283 OIC_LOG(ERROR, TAG, "can't add token");
289 for (coap_list_t *opt = options; opt; opt = opt->next)
291 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
292 COAP_OPTION_DATA(*(coap_option *) opt->data));
293 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
294 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
295 COAP_OPTION_DATA(*(coap_option *) opt->data));
302 coap_add_data(pdu, payloadSize, (const unsigned char *) payload);
309 OIC_LOG(DEBUG, TAG, "OUT");
313 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
315 OIC_LOG(DEBUG, TAG, "IN");
319 OIC_LOG(ERROR, TAG, "uriInfo is null");
320 return CA_STATUS_INVALID_PARAM;
323 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
327 OIC_LOG(ERROR, TAG, "optlist is null");
328 return CA_STATUS_INVALID_PARAM;
331 /* split arg into Uri-* options */
333 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
335 if (uri.port != COAP_DEFAULT_PORT)
337 unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 };
338 int ret = coap_insert(optlist,
339 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
340 coap_encode_var_bytes(portbuf, uri.port),
345 return CA_STATUS_INVALID_PARAM;
349 if (uri.path.s && uri.path.length)
351 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
352 COAP_OPTION_URI_PATH, optlist);
353 if (CA_STATUS_OK != ret)
355 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
360 if (uri.query.s && uri.query.length)
362 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
364 if (CA_STATUS_OK != ret)
366 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
371 OIC_LOG(DEBUG, TAG, "OUT");
375 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
376 coap_list_t **optlist)
380 OIC_LOG(ERROR, TAG, "optlist is null");
381 return CA_STATUS_INVALID_PARAM;
384 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
386 // should never occur. Log just in case.
387 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
388 return CA_NOT_SUPPORTED;
390 else if (str && length)
392 unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
393 unsigned char *pBuf = uriBuffer;
394 size_t buflen = sizeof(uriBuffer);
395 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
396 coap_split_query(str, length, pBuf, &buflen);
403 int ret = coap_insert(optlist,
404 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
405 (char *)COAP_OPT_VALUE(pBuf)),
409 return CA_STATUS_INVALID_PARAM;
412 size_t optSize = COAP_OPT_SIZE(pBuf);
413 if ((prevIdx + optSize) < buflen)
422 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
423 return CA_STATUS_FAILED;
428 OIC_LOG(ERROR, TAG, "str or length is not available");
429 return CA_STATUS_FAILED;
435 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
438 OIC_LOG(DEBUG, TAG, "IN");
439 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", CA_STATUS_INVALID_PARAM);
441 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
445 OIC_LOG(ERROR, TAG, "optlist is null");
446 return CA_STATUS_INVALID_PARAM;
449 for (uint32_t i = 0; i < info->numOptions; i++)
451 if(!(info->options + i))
453 OIC_LOG(ERROR, TAG, "options is not available");
454 return CA_STATUS_FAILED;
457 uint32_t id = (info->options + i)->optionID;
458 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
460 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
464 if ((info->options + i)->optionData && (info->options + i)->optionLength > 0)
466 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
467 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
468 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
469 int ret = coap_insert(optlist,
470 CACreateNewOptionNode(id, (info->options + i)->optionLength,
471 (info->options + i)->optionData),
475 return CA_STATUS_INVALID_PARAM;
481 OIC_LOG(DEBUG, TAG, "OUT");
485 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
487 OIC_LOG(DEBUG, TAG, "IN");
491 OIC_LOG(ERROR, TAG, "invalid pointer parameter");
495 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
498 OIC_LOG(ERROR, TAG, "Out of memory");
501 memset(option, 0, sizeof(coap_option) + length + 1);
503 COAP_OPTION_KEY(*option) = key;
504 COAP_OPTION_LENGTH(*option) = length;
505 memcpy(COAP_OPTION_DATA(*option), data, length);
507 /* we can pass NULL here as delete function since option is released automatically */
508 coap_list_t *node = coap_new_listnode(option, NULL);
512 OIC_LOG(ERROR, TAG, "node is NULL");
517 OIC_LOG(DEBUG, TAG, "OUT");
521 int CAOrderOpts(void *a, void *b)
523 OIC_LOG(DEBUG, TAG, "IN");
526 return a < b ? -1 : 1;
529 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
534 OIC_LOG(DEBUG, TAG, "OUT");
535 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
538 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
540 OIC_LOG(DEBUG, TAG, "IN");
544 while ((option = coap_option_next(&opt_iter)))
546 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
547 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
548 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type)
554 OIC_LOG(DEBUG, TAG, "OUT");
558 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo)
560 OIC_LOG(DEBUG, TAG, "IN");
562 if (!pdu || !outCode || !outInfo)
564 OIC_LOG(ERROR, TAG, "NULL pointer param");
565 return CA_STATUS_INVALID_PARAM;
568 coap_opt_iterator_t opt_iter;
569 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL);
573 (*outCode) = (uint32_t) CA_RESPONSE_CODE(pdu->hdr->code);
576 // init HeaderOption list
577 uint32_t count = CAGetOptionCount(opt_iter);
578 memset(outInfo, 0, sizeof(*outInfo));
580 outInfo->numOptions = count;
583 outInfo->type = pdu->hdr->type;
586 outInfo->messageId = pdu->hdr->id;
590 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
591 if (NULL == outInfo->options)
593 OIC_LOG(ERROR, TAG, "Out of memory");
594 return CA_MEMORY_ALLOC_FAILED;
599 char optionResult[CA_MAX_URI_LENGTH] = {0};
601 uint32_t optionLength = 0;
602 bool isfirstsetflag = false;
603 bool isQueryBeingProcessed = false;
605 while ((option = coap_option_next(&opt_iter)))
607 char buf[COAP_MAX_PDU_SIZE] = {0};
608 if (CAGetOptionData((uint8_t *)(COAP_OPT_VALUE(option)),
609 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf)))
611 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
612 uint32_t bufLength = strlen(buf);
613 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
615 if (false == isfirstsetflag)
617 isfirstsetflag = true;
618 optionResult[optionLength] = '/';
620 // Make sure there is enough room in the optionResult buffer
621 if ((optionLength + bufLength) < sizeof(optionResult))
623 memcpy(&optionResult[optionLength], buf, bufLength);
624 optionLength += bufLength;
633 if (COAP_OPTION_URI_PATH == opt_iter.type)
635 // Make sure there is enough room in the optionResult buffer
636 if (optionLength < sizeof(optionResult))
638 optionResult[optionLength] = '/';
646 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
648 if (false == isQueryBeingProcessed)
650 // Make sure there is enough room in the optionResult buffer
651 if (optionLength < sizeof(optionResult))
653 optionResult[optionLength] = '?';
655 isQueryBeingProcessed = true;
664 // Make sure there is enough room in the optionResult buffer
665 if (optionLength < sizeof(optionResult))
667 optionResult[optionLength] = ';';
676 // Make sure there is enough room in the optionResult buffer
677 if ((optionLength + bufLength) < sizeof(optionResult))
679 memcpy(&optionResult[optionLength], buf, bufLength);
680 optionLength += bufLength;
688 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
689 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
691 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
697 uint32_t length = bufLength;
699 if (length <= CA_MAX_HEADER_OPTION_DATA_LENGTH)
701 outInfo->options[idx].optionID = opt_iter.type;
702 outInfo->options[idx].optionLength = length;
703 outInfo->options[idx].protocolID = CA_COAP_ID;
704 memcpy(outInfo->options[idx].optionData, buf, length);
713 if (pdu->hdr->token_length > 0)
715 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", pdu->hdr->token_length);
716 outInfo->token = (char *) OICMalloc(pdu->hdr->token_length);
717 if (NULL == outInfo->token)
719 OIC_LOG(ERROR, TAG, "Out of memory");
720 OICFree(outInfo->options);
721 return CA_MEMORY_ALLOC_FAILED;
723 memcpy(outInfo->token, pdu->hdr->token, pdu->hdr->token_length);
726 outInfo->tokenLength = pdu->hdr->token_length;
731 if (coap_get_data(pdu, &dataSize, &data))
733 OIC_LOG(DEBUG, TAG, "inside pdu->data");
734 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
735 if (NULL == outInfo->payload)
737 OIC_LOG(ERROR, TAG, "Out of memory");
738 OICFree(outInfo->options);
739 OICFree(outInfo->token);
740 return CA_MEMORY_ALLOC_FAILED;
742 memcpy(outInfo->payload, pdu->data, dataSize);
743 outInfo->payloadSize = dataSize;
746 if (optionResult[0] != '\0')
748 OIC_LOG_V(DEBUG, TAG, "URL length:%d", strlen(optionResult));
749 outInfo->resourceUri = OICStrdup(optionResult);
750 if (!outInfo->resourceUri)
752 OIC_LOG(ERROR, TAG, "Out of memory");
753 OICFree(outInfo->options);
754 OICFree(outInfo->token);
755 return CA_MEMORY_ALLOC_FAILED;
759 OIC_LOG(DEBUG, TAG, "OUT");
763 OIC_LOG(ERROR, TAG, "buffer too small");
764 OICFree(outInfo->options);
765 return CA_STATUS_FAILED;
768 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo)
770 OIC_LOG(DEBUG, TAG, "IN");
773 OIC_LOG(ERROR, TAG, "pdu_hdr is null");
774 return CA_STATUS_INVALID_PARAM;
779 OIC_LOG(ERROR, TAG, "outInfo is null");
780 return CA_STATUS_INVALID_PARAM;
784 if (pdu_hdr->token_length > 0)
786 OIC_LOG_V(DEBUG, TAG, "token len:%d", pdu_hdr->token_length);
787 outInfo->token = (char *) OICMalloc(pdu_hdr->token_length);
788 if (NULL == outInfo->token)
790 OIC_LOG(ERROR, TAG, "out of memory");
791 return CA_MEMORY_ALLOC_FAILED;
793 memcpy(outInfo->token, pdu_hdr->token, pdu_hdr->token_length);
796 outInfo->tokenLength = pdu_hdr->token_length;
798 OIC_LOG(DEBUG, TAG, "OUT");
803 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
805 OIC_LOG(DEBUG, TAG, "IN");
809 OIC_LOG(ERROR, TAG, "invalid token pointer");
810 return CA_STATUS_INVALID_PARAM;
813 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
815 OIC_LOG(ERROR, TAG, "invalid token length");
816 return CA_STATUS_INVALID_PARAM;
826 if (SEED == (unsigned int)((time_t)-1))
828 OIC_LOG(ERROR, TAG, "seed is not made");
830 return CA_STATUS_FAILED;
840 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
843 OIC_LOG(ERROR, TAG, "Out of memory");
844 return CA_MEMORY_ALLOC_FAILED;
848 for (uint8_t index = 0; index < tokenLength; index++)
850 // use valid characters
852 temp[index] = rand() & 0x00FF;
854 temp[index] = random() & 0x00FF;
861 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
862 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
864 OIC_LOG(DEBUG, TAG, "OUT");
868 void CADestroyTokenInternal(CAToken_t token)
870 OIC_LOG(DEBUG, TAG, "IN");
872 OIC_LOG(DEBUG, TAG, "OUT");
875 void CADestroyInfo(CAInfo_t *info)
877 OIC_LOG(DEBUG, TAG, "IN");
881 OIC_LOG(DEBUG, TAG, "free options");
882 OICFree(info->options);
884 OIC_LOG(DEBUG, TAG, "free token");
885 OICFree(info->token);
887 OIC_LOG(DEBUG, TAG, "free payload");
888 OICFree(info->payload);
891 OIC_LOG(DEBUG, TAG, "OUT");
894 uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *option, uint32_t buflen)
896 if (0 == buflen || 0 == len)
898 OIC_LOG(ERROR, TAG, "len 0");
902 if (NULL == data || NULL == option)
904 OIC_LOG(ERROR, TAG, "data/option NULL");
910 OIC_LOG(ERROR, TAG, "option buffer too small");
914 memcpy(option, data, len);
920 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
924 OIC_LOG(ERROR, TAG, "pdu is null");
925 return CA_MSG_NONCONFIRM;
928 // pdu minimum size is 4 byte.
929 if (size < CA_PDU_MIN_SIZE)
931 OIC_LOG(ERROR, TAG, "min size");
932 return CA_MSG_NONCONFIRM;
935 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
937 return (CAMessageType_t) hdr->type;
940 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
944 OIC_LOG(ERROR, TAG, "pdu is null");
948 // pdu minimum size is 4 byte.
949 if (size < CA_PDU_MIN_SIZE)
951 OIC_LOG(ERROR, TAG, "min size");
955 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
960 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
964 OIC_LOG(ERROR, TAG, "pdu is null");
968 // pdu minimum size is 4 byte.
969 if (size < CA_PDU_MIN_SIZE)
971 OIC_LOG(ERROR, TAG, "min size");
975 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
977 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->code);