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,
71 CATransportFlags_t flags)
73 OIC_LOG(DEBUG, TAG, "IN");
75 if (NULL == pdu || NULL == outReqInfo)
77 OIC_LOG(ERROR, TAG, "parameter is null");
78 return CA_STATUS_INVALID_PARAM;
81 uint32_t code = CA_NOT_FOUND;
82 CAResult_t ret = CAGetInfoFromPDU(pdu, &code, &(outReqInfo->info), flags);
83 outReqInfo->method = code;
85 OIC_LOG(DEBUG, TAG, "OUT");
89 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
90 CATransportFlags_t flags)
92 OIC_LOG(DEBUG, TAG, "IN");
94 if (NULL == pdu || NULL == outResInfo)
96 OIC_LOG(ERROR, TAG, "parameter is null");
97 return CA_STATUS_INVALID_PARAM;
100 uint32_t code = CA_NOT_FOUND;
101 CAResult_t ret = CAGetInfoFromPDU(pdu, &code, &(outResInfo->info), flags);
102 outResInfo->result = code;
104 OIC_LOG(DEBUG, TAG, "OUT");
108 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, CAErrorInfo_t *errorInfo,
109 CATransportFlags_t flags)
111 OIC_LOG(DEBUG, TAG, "IN");
115 OIC_LOG(ERROR, TAG, "parameter is null");
116 return CA_STATUS_INVALID_PARAM;
120 CAResult_t ret = CAGetInfoFromPDU(pdu, &code, &errorInfo->info, flags);
121 OIC_LOG(DEBUG, TAG, "OUT");
125 coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint)
127 OIC_LOG(DEBUG, TAG, "IN");
129 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
130 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
132 coap_pdu_t *pdu = NULL;
134 // RESET have to use only 4byte (empty message)
135 // and ACKNOWLEDGE can use empty message when code is empty.
136 if (CA_MSG_RESET == info->type || (CA_EMPTY == code && CA_MSG_ACKNOWLEDGE == info->type))
138 OIC_LOG(DEBUG, TAG, "code is empty");
139 if (!(pdu = CAGeneratePDUImpl((code_t) code, NULL, info, endpoint)))
141 OIC_LOG(ERROR, TAG, "pdu NULL");
147 coap_list_t *optlist = NULL;
149 if (CA_MSG_ACKNOWLEDGE != info->type)
151 const char *uri = info->resourceUri;
154 OIC_LOG(ERROR, TAG, "uri NULL");
158 uint32_t length = strlen(uri);
159 if (CA_MAX_URI_LENGTH < length)
161 OIC_LOG(ERROR, TAG, "URI len err");
165 uint32_t uriLength = length + sizeof(COAP_URI_HEADER);
166 char *coapUri = (char *) OICCalloc(1, uriLength);
169 OIC_LOG(ERROR, TAG, "out of memory");
172 OICStrcat(coapUri, uriLength, COAP_URI_HEADER);
173 OICStrcat(coapUri, uriLength, uri);
175 // parsing options in URI
176 CAResult_t res = CAParseURI(coapUri, &optlist);
177 if (CA_STATUS_OK != res)
181 coap_delete_list(optlist);
189 // parsing options in HeadOption
190 CAResult_t ret = CAParseHeadOption(code, info, &optlist);
191 if (CA_STATUS_OK != ret)
193 coap_delete_list(optlist);
197 pdu = CAGeneratePDUImpl((code_t) code, optlist, info, endpoint);
200 OIC_LOG(ERROR, TAG, "pdu NULL");
201 coap_delete_list(optlist);
206 coap_delete_list(optlist);
209 // pdu print method : coap_show_pdu(pdu);
210 OIC_LOG(DEBUG, TAG, "OUT");
214 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode,
215 CATransportFlags_t flags)
217 OIC_LOG(DEBUG, TAG, "IN");
221 OIC_LOG(ERROR, TAG, "data is null");
225 coap_transport_type transport;
227 if (CA_IPV4_TCP == flags)
229 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)data)[0] >> 4);
234 transport = coap_udp;
237 coap_pdu_t *outpdu = coap_new_pdu(transport);
240 OIC_LOG(ERROR, TAG, "outpdu is null");
244 OIC_LOG_V(DEBUG, TAG, "pdu parse-transport type : %d", transport);
246 int ret = coap_pdu_parse((unsigned char *) data, length, outpdu, transport);
247 OIC_LOG_V(DEBUG, TAG, "pdu parse ret: %d", ret);
250 OIC_LOG(ERROR, TAG, "pdu parse failed");
251 coap_delete_pdu(outpdu);
256 if (CA_IPV4_TCP == flags)
258 OIC_LOG(INFO, TAG, "there is no version info in coap header");
263 if (outpdu->hdr->coap_hdr_udp_t.version != COAP_DEFAULT_VERSION)
265 OIC_LOG_V(ERROR, TAG, "coap version is not available : %d",
266 outpdu->hdr->coap_hdr_udp_t.version);
267 coap_delete_pdu(outpdu);
270 if (outpdu->hdr->coap_hdr_udp_t.token_length > CA_MAX_TOKEN_LEN)
272 OIC_LOG_V(ERROR, TAG, "token length has been exceed : %d",
273 outpdu->hdr->coap_hdr_udp_t.token_length);
274 coap_delete_pdu(outpdu);
281 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(outpdu, transport));
284 OIC_LOG(DEBUG, TAG, "OUT");
288 coap_pdu_t *CAGeneratePDUImpl(code_t code, coap_list_t *options, const CAInfo_t *info,
289 const CAEndpoint_t *endpoint)
291 OIC_LOG(DEBUG, TAG, "IN");
292 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
293 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
295 coap_transport_type transport;
298 if (CA_IPV4_TCP == endpoint->flags)
301 transport = coap_get_tcp_header_type_from_size(info->payloadSize);
306 transport = coap_udp;
309 coap_pdu_t *pdu = coap_new_pdu(transport);
313 OIC_LOG(ERROR, TAG, "malloc failed");
317 OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %d",
318 transport, info->payloadSize);
321 if (CA_IPV4_TCP == endpoint->flags)
323 coap_add_code(pdu, transport, code);
324 coap_add_length(pdu, transport, info->payloadSize);
329 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
331 if (0 == info->messageId)
333 /* initialize message id */
334 prng((uint8_t * ) &message_id, sizeof(message_id));
336 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
340 /* use saved message id */
341 message_id = info->messageId;
343 pdu->hdr->coap_hdr_udp_t.id = message_id;
344 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->coap_hdr_udp_t.id);
346 pdu->hdr->coap_hdr_udp_t.type = info->type;
347 coap_add_code(pdu, transport, code);
350 if (info->token && CA_EMPTY != code)
352 uint32_t tokenLength = info->tokenLength;
353 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
354 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
356 int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token, transport);
359 OIC_LOG(ERROR, TAG, "can't add token");
365 for (coap_list_t *opt = options; opt; opt = opt->next)
367 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
368 COAP_OPTION_DATA(*(coap_option *) opt->data));
370 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
371 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
372 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
373 COAP_OPTION_DATA(*(coap_option *) opt->data), transport);
377 bool enabledPayload = false;
379 enabledPayload = true;
382 if (enabledPayload || CA_ADAPTER_GATT_BTLE == endpoint->adapter
384 || CA_IPV4_TCP == endpoint->flags
388 if (NULL != info->payload && 0 < info->payloadSize)
390 OIC_LOG(DEBUG, TAG, "payload is added");
391 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
395 OIC_LOG(DEBUG, TAG, "OUT");
399 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
401 OIC_LOG(DEBUG, TAG, "IN");
405 OIC_LOG(ERROR, TAG, "uriInfo is null");
406 return CA_STATUS_INVALID_PARAM;
409 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
413 OIC_LOG(ERROR, TAG, "optlist is null");
414 return CA_STATUS_INVALID_PARAM;
417 /* split arg into Uri-* options */
419 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
421 if (uri.port != COAP_DEFAULT_PORT)
423 unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 };
424 int ret = coap_insert(optlist,
425 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
426 coap_encode_var_bytes(portbuf, uri.port),
431 return CA_STATUS_INVALID_PARAM;
435 if (uri.path.s && uri.path.length)
437 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
438 COAP_OPTION_URI_PATH, optlist);
439 if (CA_STATUS_OK != ret)
441 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
446 if (uri.query.s && uri.query.length)
448 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
450 if (CA_STATUS_OK != ret)
452 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
457 OIC_LOG(DEBUG, TAG, "OUT");
461 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
462 coap_list_t **optlist)
466 OIC_LOG(ERROR, TAG, "optlist is null");
467 return CA_STATUS_INVALID_PARAM;
470 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
472 // should never occur. Log just in case.
473 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
474 return CA_NOT_SUPPORTED;
476 else if (str && length)
478 unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
479 unsigned char *pBuf = uriBuffer;
480 size_t buflen = sizeof(uriBuffer);
481 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
482 coap_split_query(str, length, pBuf, &buflen);
489 int ret = coap_insert(optlist,
490 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
491 (char *)COAP_OPT_VALUE(pBuf)),
495 return CA_STATUS_INVALID_PARAM;
498 size_t optSize = COAP_OPT_SIZE(pBuf);
499 if ((prevIdx + optSize) < buflen)
508 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
509 return CA_STATUS_FAILED;
514 OIC_LOG(ERROR, TAG, "str or length is not available");
515 return CA_STATUS_FAILED;
521 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
524 OIC_LOG(DEBUG, TAG, "IN");
525 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", CA_STATUS_INVALID_PARAM);
527 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
531 OIC_LOG(ERROR, TAG, "optlist is null");
532 return CA_STATUS_INVALID_PARAM;
535 for (uint32_t i = 0; i < info->numOptions; i++)
537 if(!(info->options + i))
539 OIC_LOG(ERROR, TAG, "options is not available");
540 return CA_STATUS_FAILED;
543 uint32_t id = (info->options + i)->optionID;
544 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
546 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
550 if ((info->options + i)->optionData && (info->options + i)->optionLength > 0)
552 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
553 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
554 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
555 int ret = coap_insert(optlist,
556 CACreateNewOptionNode(id, (info->options + i)->optionLength,
557 (info->options + i)->optionData),
561 return CA_STATUS_INVALID_PARAM;
567 OIC_LOG(DEBUG, TAG, "OUT");
571 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
573 OIC_LOG(DEBUG, TAG, "IN");
577 OIC_LOG(ERROR, TAG, "invalid pointer parameter");
581 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
584 OIC_LOG(ERROR, TAG, "Out of memory");
587 memset(option, 0, sizeof(coap_option) + length + 1);
589 COAP_OPTION_KEY(*option) = key;
590 COAP_OPTION_LENGTH(*option) = length;
591 memcpy(COAP_OPTION_DATA(*option), data, length);
593 /* we can pass NULL here as delete function since option is released automatically */
594 coap_list_t *node = coap_new_listnode(option, NULL);
598 OIC_LOG(ERROR, TAG, "node is NULL");
603 OIC_LOG(DEBUG, TAG, "OUT");
607 int CAOrderOpts(void *a, void *b)
609 OIC_LOG(DEBUG, TAG, "IN");
612 return a < b ? -1 : 1;
615 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
620 OIC_LOG(DEBUG, TAG, "OUT");
621 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
624 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
626 OIC_LOG(DEBUG, TAG, "IN");
630 while ((option = coap_option_next(&opt_iter)))
632 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
633 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
634 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type)
640 OIC_LOG(DEBUG, TAG, "OUT");
644 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo,
645 CATransportFlags_t flags)
647 OIC_LOG(DEBUG, TAG, "IN");
649 if (!pdu || !outCode || !outInfo)
651 OIC_LOG(ERROR, TAG, "NULL pointer param");
652 return CA_STATUS_INVALID_PARAM;
655 coap_transport_type transport;
657 if (CA_IPV4_TCP == flags)
659 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->hdr)[0] >> 4);
664 transport = coap_udp;
667 coap_opt_iterator_t opt_iter;
668 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport);
672 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(pdu, transport));
675 // init HeaderOption list
676 uint32_t count = CAGetOptionCount(opt_iter);
677 memset(outInfo, 0, sizeof(*outInfo));
679 outInfo->numOptions = count;
682 if (CA_IPV4_TCP == flags)
685 outInfo->type = CA_MSG_NONCONFIRM;
691 outInfo->type = pdu->hdr->coap_hdr_udp_t.type;
694 outInfo->messageId = pdu->hdr->coap_hdr_udp_t.id;
700 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
701 if (NULL == outInfo->options)
703 OIC_LOG(ERROR, TAG, "Out of memory");
704 return CA_MEMORY_ALLOC_FAILED;
709 char optionResult[CA_MAX_URI_LENGTH] = {0};
711 uint32_t optionLength = 0;
712 bool isfirstsetflag = false;
713 bool isQueryBeingProcessed = false;
715 while ((option = coap_option_next(&opt_iter)))
717 char buf[COAP_MAX_PDU_SIZE] = {0};
718 if (CAGetOptionData((uint8_t *)(COAP_OPT_VALUE(option)),
719 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf)))
721 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
722 uint32_t bufLength = strlen(buf);
723 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
725 if (false == isfirstsetflag)
727 isfirstsetflag = true;
728 optionResult[optionLength] = '/';
730 // Make sure there is enough room in the optionResult buffer
731 if ((optionLength + bufLength) < sizeof(optionResult))
733 memcpy(&optionResult[optionLength], buf, bufLength);
734 optionLength += bufLength;
743 if (COAP_OPTION_URI_PATH == opt_iter.type)
745 // Make sure there is enough room in the optionResult buffer
746 if (optionLength < sizeof(optionResult))
748 optionResult[optionLength] = '/';
756 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
758 if (false == isQueryBeingProcessed)
760 // Make sure there is enough room in the optionResult buffer
761 if (optionLength < sizeof(optionResult))
763 optionResult[optionLength] = '?';
765 isQueryBeingProcessed = true;
774 // Make sure there is enough room in the optionResult buffer
775 if (optionLength < sizeof(optionResult))
777 optionResult[optionLength] = ';';
786 // Make sure there is enough room in the optionResult buffer
787 if ((optionLength + bufLength) < sizeof(optionResult))
789 memcpy(&optionResult[optionLength], buf, bufLength);
790 optionLength += bufLength;
798 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
799 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
801 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
807 uint32_t length = bufLength;
809 if (length <= CA_MAX_HEADER_OPTION_DATA_LENGTH)
811 outInfo->options[idx].optionID = opt_iter.type;
812 outInfo->options[idx].optionLength = length;
813 outInfo->options[idx].protocolID = CA_COAP_ID;
814 memcpy(outInfo->options[idx].optionData, buf, length);
822 unsigned char* token;
823 unsigned int token_length;
824 coap_get_token(pdu->hdr, transport, &token, &token_length);
827 if (token_length > 0)
829 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", token_length);
830 outInfo->token = (char *) OICMalloc(token_length);
831 if (NULL == outInfo->token)
833 OIC_LOG(ERROR, TAG, "Out of memory");
834 OICFree(outInfo->options);
835 return CA_MEMORY_ALLOC_FAILED;
837 memcpy(outInfo->token, token, token_length);
840 outInfo->tokenLength = token_length;
845 if (coap_get_data(pdu, &dataSize, &data))
847 OIC_LOG(DEBUG, TAG, "inside pdu->data");
848 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
849 if (NULL == outInfo->payload)
851 OIC_LOG(ERROR, TAG, "Out of memory");
852 OICFree(outInfo->options);
853 OICFree(outInfo->token);
854 return CA_MEMORY_ALLOC_FAILED;
856 memcpy(outInfo->payload, pdu->data, dataSize);
857 outInfo->payloadSize = dataSize;
860 if (optionResult[0] != '\0')
862 OIC_LOG_V(DEBUG, TAG, "URL length:%d", strlen(optionResult));
863 outInfo->resourceUri = OICStrdup(optionResult);
864 if (!outInfo->resourceUri)
866 OIC_LOG(ERROR, TAG, "Out of memory");
867 OICFree(outInfo->options);
868 OICFree(outInfo->token);
869 return CA_MEMORY_ALLOC_FAILED;
873 OIC_LOG(DEBUG, TAG, "OUT");
877 OIC_LOG(ERROR, TAG, "buffer too small");
878 OICFree(outInfo->options);
879 return CA_STATUS_FAILED;
882 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo,
883 CATransportFlags_t flags)
885 OIC_LOG(DEBUG, TAG, "IN");
888 OIC_LOG(ERROR, TAG, "pdu_hdr is null");
889 return CA_STATUS_INVALID_PARAM;
894 OIC_LOG(ERROR, TAG, "outInfo is null");
895 return CA_STATUS_INVALID_PARAM;
898 coap_transport_type transport;
900 if (CA_IPV4_TCP == flags)
902 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4);
907 transport = coap_udp;
910 unsigned char* token;
911 unsigned int token_length;
912 coap_get_token(pdu_hdr, transport, &token, &token_length);
915 if (token_length > 0)
917 OIC_LOG_V(DEBUG, TAG, "token len:%d", token_length);
918 outInfo->token = (char *) OICMalloc(token_length);
919 if (NULL == outInfo->token)
921 OIC_LOG(ERROR, TAG, "Out of memory");
922 return CA_MEMORY_ALLOC_FAILED;
924 memcpy(outInfo->token, token, token_length);
927 outInfo->tokenLength = token_length;
929 OIC_LOG(DEBUG, TAG, "OUT");
934 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
936 OIC_LOG(DEBUG, TAG, "IN");
940 OIC_LOG(ERROR, TAG, "invalid token pointer");
941 return CA_STATUS_INVALID_PARAM;
944 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
946 OIC_LOG(ERROR, TAG, "invalid token length");
947 return CA_STATUS_INVALID_PARAM;
957 if (SEED == (unsigned int)((time_t)-1))
959 OIC_LOG(ERROR, TAG, "seed is not made");
961 return CA_STATUS_FAILED;
971 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
974 OIC_LOG(ERROR, TAG, "Out of memory");
975 return CA_MEMORY_ALLOC_FAILED;
979 for (uint8_t index = 0; index < tokenLength; index++)
981 // use valid characters
983 temp[index] = rand() & 0x00FF;
985 temp[index] = random() & 0x00FF;
992 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
993 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
995 OIC_LOG(DEBUG, TAG, "OUT");
999 void CADestroyTokenInternal(CAToken_t token)
1001 OIC_LOG(DEBUG, TAG, "IN");
1003 OIC_LOG(DEBUG, TAG, "OUT");
1006 void CADestroyInfo(CAInfo_t *info)
1008 OIC_LOG(DEBUG, TAG, "IN");
1012 OIC_LOG(DEBUG, TAG, "free options");
1013 OICFree(info->options);
1015 OIC_LOG(DEBUG, TAG, "free token");
1016 OICFree(info->token);
1018 OIC_LOG(DEBUG, TAG, "free payload");
1019 OICFree(info->payload);
1022 OIC_LOG(DEBUG, TAG, "OUT");
1025 uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *option, uint32_t buflen)
1027 if (0 == buflen || 0 == len)
1029 OIC_LOG(ERROR, TAG, "len 0");
1033 if (NULL == data || NULL == option)
1035 OIC_LOG(ERROR, TAG, "data/option NULL");
1041 OIC_LOG(ERROR, TAG, "option buffer too small");
1045 memcpy(option, data, len);
1051 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
1055 OIC_LOG(ERROR, TAG, "pdu is null");
1056 return CA_MSG_NONCONFIRM;
1059 // pdu minimum size is 4 byte.
1060 if (size < CA_PDU_MIN_SIZE)
1062 OIC_LOG(ERROR, TAG, "min size");
1063 return CA_MSG_NONCONFIRM;
1066 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1068 return (CAMessageType_t) hdr->coap_hdr_udp_t.type;
1071 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
1075 OIC_LOG(ERROR, TAG, "pdu is null");
1079 // pdu minimum size is 4 byte.
1080 if (size < CA_PDU_MIN_SIZE)
1082 OIC_LOG(ERROR, TAG, "min size");
1086 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1088 return hdr->coap_hdr_udp_t.id;
1091 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
1095 OIC_LOG(ERROR, TAG, "pdu is null");
1096 return CA_NOT_FOUND;
1099 // pdu minimum size is 4 byte.
1100 if (size < CA_PDU_MIN_SIZE)
1102 OIC_LOG(ERROR, TAG, "min size");
1103 return CA_NOT_FOUND;
1106 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1108 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->coap_hdr_udp_t.code);