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 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,
212 CATransportFlags_t flags)
214 OIC_LOG(DEBUG, TAG, "IN");
218 OIC_LOG(ERROR, TAG, "data is null");
222 coap_transport_type transport;
224 if (CA_IPV4_TCP == flags)
226 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)data)[0] >> 4);
231 transport = coap_udp;
234 coap_pdu_t *outpdu = coap_new_pdu(transport);
237 OIC_LOG(ERROR, TAG, "outpdu is null");
241 OIC_LOG_V(DEBUG, TAG, "pdu parse-transport type : %d", transport);
243 int ret = coap_pdu_parse((unsigned char *) data, length, outpdu, transport);
244 OIC_LOG_V(DEBUG, TAG, "pdu parse ret: %d", ret);
247 OIC_LOG(ERROR, TAG, "pdu parse failed");
248 coap_delete_pdu(outpdu);
253 if (CA_IPV4_TCP == flags)
255 OIC_LOG(INFO, TAG, "there is no version info in coap header");
260 if (outpdu->hdr->coap_hdr_udp_t.version != COAP_DEFAULT_VERSION)
262 OIC_LOG_V(ERROR, TAG, "coap version is not available : %d",
263 outpdu->hdr->coap_hdr_udp_t.version);
264 coap_delete_pdu(outpdu);
271 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(outpdu, transport));
274 OIC_LOG(DEBUG, TAG, "OUT");
278 coap_pdu_t *CAGeneratePDUImpl(code_t code, coap_list_t *options, const CAInfo_t *info,
279 const CAEndpoint_t *endpoint)
281 OIC_LOG(DEBUG, TAG, "IN");
282 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", NULL);
284 coap_transport_type transport;
287 if (CA_IPV4_TCP == endpoint->flags)
290 transport = coap_get_tcp_header_type_from_size(info->payloadSize);
295 transport = coap_udp;
298 coap_pdu_t *pdu = coap_new_pdu(transport);
302 OIC_LOG(ERROR, TAG, "malloc failed");
306 OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %d",
307 transport, info->payloadSize);
310 if (CA_IPV4_TCP == endpoint->flags)
312 coap_add_code(pdu, transport, code);
313 coap_add_length(pdu, transport, info->payloadSize);
318 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
320 if (0 == info->messageId)
322 /* initialize message id */
323 prng((uint8_t * ) &message_id, sizeof(message_id));
325 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
329 /* use saved message id */
330 message_id = info->messageId;
332 pdu->hdr->coap_hdr_udp_t.id = message_id;
333 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->coap_hdr_udp_t.id);
335 pdu->hdr->coap_hdr_udp_t.type = info->type;
336 coap_add_code(pdu, transport, code);
339 if (info->token && CA_EMPTY != code)
341 uint32_t tokenLength = info->tokenLength;
342 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
343 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
345 int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token, transport);
348 OIC_LOG(ERROR, TAG, "can't add token");
354 for (coap_list_t *opt = options; opt; opt = opt->next)
356 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
357 COAP_OPTION_DATA(*(coap_option *) opt->data));
359 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
360 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
361 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
362 COAP_OPTION_DATA(*(coap_option *) opt->data), transport);
366 bool enabledPayload = false;
368 enabledPayload = true;
371 if (enabledPayload || CA_ADAPTER_GATT_BTLE == endpoint->adapter
373 || CA_IPV4_TCP == endpoint->flags
377 if (NULL != info->payload && 0 < info->payloadSize)
379 OIC_LOG(DEBUG, TAG, "payload is added");
380 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
384 OIC_LOG(DEBUG, TAG, "OUT");
388 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
390 OIC_LOG(DEBUG, TAG, "IN");
394 OIC_LOG(ERROR, TAG, "uriInfo is null");
395 return CA_STATUS_INVALID_PARAM;
398 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
402 OIC_LOG(ERROR, TAG, "optlist is null");
403 return CA_STATUS_INVALID_PARAM;
406 /* split arg into Uri-* options */
408 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
410 if (uri.port != COAP_DEFAULT_PORT)
412 unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 };
413 int ret = coap_insert(optlist,
414 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
415 coap_encode_var_bytes(portbuf, uri.port),
420 return CA_STATUS_INVALID_PARAM;
424 if (uri.path.s && uri.path.length)
426 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
427 COAP_OPTION_URI_PATH, optlist);
428 if (CA_STATUS_OK != ret)
430 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
435 if (uri.query.s && uri.query.length)
437 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
439 if (CA_STATUS_OK != ret)
441 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
446 OIC_LOG(DEBUG, TAG, "OUT");
450 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
451 coap_list_t **optlist)
455 OIC_LOG(ERROR, TAG, "optlist is null");
456 return CA_STATUS_INVALID_PARAM;
459 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
461 // should never occur. Log just in case.
462 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
463 return CA_NOT_SUPPORTED;
465 else if (str && length)
467 unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
468 unsigned char *pBuf = uriBuffer;
469 size_t buflen = sizeof(uriBuffer);
470 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
471 coap_split_query(str, length, pBuf, &buflen);
478 int ret = coap_insert(optlist,
479 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
480 (char *)COAP_OPT_VALUE(pBuf)),
484 return CA_STATUS_INVALID_PARAM;
487 size_t optSize = COAP_OPT_SIZE(pBuf);
488 if ((prevIdx + optSize) < buflen)
497 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
498 return CA_STATUS_FAILED;
503 OIC_LOG(ERROR, TAG, "str or length is not available");
504 return CA_STATUS_FAILED;
510 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
513 OIC_LOG(DEBUG, TAG, "IN");
514 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", CA_STATUS_INVALID_PARAM);
516 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
520 OIC_LOG(ERROR, TAG, "optlist is null");
521 return CA_STATUS_INVALID_PARAM;
524 for (uint32_t i = 0; i < info->numOptions; i++)
526 if(!(info->options + i))
528 OIC_LOG(ERROR, TAG, "options is not available");
529 return CA_STATUS_FAILED;
532 uint32_t id = (info->options + i)->optionID;
533 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
535 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
539 if ((info->options + i)->optionData && (info->options + i)->optionLength > 0)
541 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
542 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
543 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
544 int ret = coap_insert(optlist,
545 CACreateNewOptionNode(id, (info->options + i)->optionLength,
546 (info->options + i)->optionData),
550 return CA_STATUS_INVALID_PARAM;
556 OIC_LOG(DEBUG, TAG, "OUT");
560 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
562 OIC_LOG(DEBUG, TAG, "IN");
566 OIC_LOG(ERROR, TAG, "invalid pointer parameter");
570 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
573 OIC_LOG(ERROR, TAG, "Out of memory");
576 memset(option, 0, sizeof(coap_option) + length + 1);
578 COAP_OPTION_KEY(*option) = key;
579 COAP_OPTION_LENGTH(*option) = length;
580 memcpy(COAP_OPTION_DATA(*option), data, length);
582 /* we can pass NULL here as delete function since option is released automatically */
583 coap_list_t *node = coap_new_listnode(option, NULL);
587 OIC_LOG(ERROR, TAG, "node is NULL");
592 OIC_LOG(DEBUG, TAG, "OUT");
596 int CAOrderOpts(void *a, void *b)
598 OIC_LOG(DEBUG, TAG, "IN");
601 return a < b ? -1 : 1;
604 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
609 OIC_LOG(DEBUG, TAG, "OUT");
610 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
613 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
615 OIC_LOG(DEBUG, TAG, "IN");
619 while ((option = coap_option_next(&opt_iter)))
621 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
622 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
623 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type)
629 OIC_LOG(DEBUG, TAG, "OUT");
633 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo,
634 CATransportFlags_t flags)
636 OIC_LOG(DEBUG, TAG, "IN");
638 if (!pdu || !outCode || !outInfo)
640 OIC_LOG(ERROR, TAG, "NULL pointer param");
641 return CA_STATUS_INVALID_PARAM;
644 coap_transport_type transport;
646 if (CA_IPV4_TCP == flags)
648 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->hdr)[0] >> 4);
653 transport = coap_udp;
656 coap_opt_iterator_t opt_iter;
657 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport);
661 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(pdu, transport));
664 // init HeaderOption list
665 uint32_t count = CAGetOptionCount(opt_iter);
666 memset(outInfo, 0, sizeof(*outInfo));
668 outInfo->numOptions = count;
671 if (CA_IPV4_TCP == flags)
674 outInfo->type = CA_MSG_NONCONFIRM;
680 outInfo->type = pdu->hdr->coap_hdr_udp_t.type;
683 outInfo->messageId = pdu->hdr->coap_hdr_udp_t.id;
689 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
690 if (NULL == outInfo->options)
692 OIC_LOG(ERROR, TAG, "Out of memory");
693 return CA_MEMORY_ALLOC_FAILED;
698 char optionResult[CA_MAX_URI_LENGTH] = {0};
700 uint32_t optionLength = 0;
701 bool isfirstsetflag = false;
702 bool isQueryBeingProcessed = false;
704 while ((option = coap_option_next(&opt_iter)))
706 char buf[COAP_MAX_PDU_SIZE] = {0};
707 if (CAGetOptionData((uint8_t *)(COAP_OPT_VALUE(option)),
708 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf)))
710 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
711 uint32_t bufLength = strlen(buf);
712 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
714 if (false == isfirstsetflag)
716 isfirstsetflag = true;
717 optionResult[optionLength] = '/';
719 // Make sure there is enough room in the optionResult buffer
720 if ((optionLength + bufLength) < sizeof(optionResult))
722 memcpy(&optionResult[optionLength], buf, bufLength);
723 optionLength += bufLength;
732 if (COAP_OPTION_URI_PATH == opt_iter.type)
734 // Make sure there is enough room in the optionResult buffer
735 if (optionLength < sizeof(optionResult))
737 optionResult[optionLength] = '/';
745 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
747 if (false == isQueryBeingProcessed)
749 // Make sure there is enough room in the optionResult buffer
750 if (optionLength < sizeof(optionResult))
752 optionResult[optionLength] = '?';
754 isQueryBeingProcessed = true;
763 // Make sure there is enough room in the optionResult buffer
764 if (optionLength < sizeof(optionResult))
766 optionResult[optionLength] = ';';
775 // Make sure there is enough room in the optionResult buffer
776 if ((optionLength + bufLength) < sizeof(optionResult))
778 memcpy(&optionResult[optionLength], buf, bufLength);
779 optionLength += bufLength;
787 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
788 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
790 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
796 uint32_t length = bufLength;
798 if (length <= CA_MAX_HEADER_OPTION_DATA_LENGTH)
800 outInfo->options[idx].optionID = opt_iter.type;
801 outInfo->options[idx].optionLength = length;
802 outInfo->options[idx].protocolID = CA_COAP_ID;
803 memcpy(outInfo->options[idx].optionData, buf, length);
811 unsigned char* token;
812 unsigned int token_length;
813 coap_get_token(pdu->hdr, transport, &token, &token_length);
816 if (token_length > 0)
818 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", token_length);
819 outInfo->token = (char *) OICMalloc(token_length);
820 if (NULL == outInfo->token)
822 OIC_LOG(ERROR, TAG, "Out of memory");
823 OICFree(outInfo->options);
824 return CA_MEMORY_ALLOC_FAILED;
826 memcpy(outInfo->token, token, token_length);
829 outInfo->tokenLength = token_length;
834 if (coap_get_data(pdu, &dataSize, &data))
836 OIC_LOG(DEBUG, TAG, "inside pdu->data");
837 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
838 if (NULL == outInfo->payload)
840 OIC_LOG(ERROR, TAG, "Out of memory");
841 OICFree(outInfo->options);
842 OICFree(outInfo->token);
843 return CA_MEMORY_ALLOC_FAILED;
845 memcpy(outInfo->payload, pdu->data, dataSize);
846 outInfo->payloadSize = dataSize;
849 if (optionResult[0] != '\0')
851 OIC_LOG_V(DEBUG, TAG, "URL length:%d", strlen(optionResult));
852 outInfo->resourceUri = OICStrdup(optionResult);
853 if (!outInfo->resourceUri)
855 OIC_LOG(ERROR, TAG, "Out of memory");
856 OICFree(outInfo->options);
857 OICFree(outInfo->token);
858 return CA_MEMORY_ALLOC_FAILED;
862 OIC_LOG(DEBUG, TAG, "OUT");
866 OIC_LOG(ERROR, TAG, "buffer too small");
867 OICFree(outInfo->options);
868 return CA_STATUS_FAILED;
871 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo,
872 CATransportFlags_t flags)
874 OIC_LOG(DEBUG, TAG, "IN");
877 OIC_LOG(ERROR, TAG, "pdu_hdr is null");
878 return CA_STATUS_INVALID_PARAM;
883 OIC_LOG(ERROR, TAG, "outInfo is null");
884 return CA_STATUS_INVALID_PARAM;
887 coap_transport_type transport;
889 if (CA_IPV4_TCP == flags)
891 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4);
896 transport = coap_udp;
899 unsigned char* token;
900 unsigned int token_length;
901 coap_get_token(pdu_hdr, transport, &token, &token_length);
904 if (token_length > 0)
906 OIC_LOG_V(DEBUG, TAG, "token len:%d", token_length);
907 outInfo->token = (char *) OICMalloc(token_length);
908 if (NULL == outInfo->token)
910 OIC_LOG(ERROR, TAG, "Out of memory");
911 return CA_MEMORY_ALLOC_FAILED;
913 memcpy(outInfo->token, token, token_length);
916 outInfo->tokenLength = token_length;
918 OIC_LOG(DEBUG, TAG, "OUT");
923 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
925 OIC_LOG(DEBUG, TAG, "IN");
929 OIC_LOG(ERROR, TAG, "invalid token pointer");
930 return CA_STATUS_INVALID_PARAM;
933 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
935 OIC_LOG(ERROR, TAG, "invalid token length");
936 return CA_STATUS_INVALID_PARAM;
946 if (SEED == (unsigned int)((time_t)-1))
948 OIC_LOG(ERROR, TAG, "seed is not made");
950 return CA_STATUS_FAILED;
960 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
963 OIC_LOG(ERROR, TAG, "Out of memory");
964 return CA_MEMORY_ALLOC_FAILED;
968 for (uint8_t index = 0; index < tokenLength; index++)
970 // use valid characters
972 temp[index] = rand() & 0x00FF;
974 temp[index] = random() & 0x00FF;
981 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
982 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
984 OIC_LOG(DEBUG, TAG, "OUT");
988 void CADestroyTokenInternal(CAToken_t token)
990 OIC_LOG(DEBUG, TAG, "IN");
992 OIC_LOG(DEBUG, TAG, "OUT");
995 void CADestroyInfo(CAInfo_t *info)
997 OIC_LOG(DEBUG, TAG, "IN");
1001 OIC_LOG(DEBUG, TAG, "free options");
1002 OICFree(info->options);
1004 OIC_LOG(DEBUG, TAG, "free token");
1005 OICFree(info->token);
1007 OIC_LOG(DEBUG, TAG, "free payload");
1008 OICFree(info->payload);
1011 OIC_LOG(DEBUG, TAG, "OUT");
1014 uint32_t CAGetOptionData(const uint8_t *data, uint32_t len, uint8_t *option, uint32_t buflen)
1016 if (0 == buflen || 0 == len)
1018 OIC_LOG(ERROR, TAG, "len 0");
1022 if (NULL == data || NULL == option)
1024 OIC_LOG(ERROR, TAG, "data/option NULL");
1030 OIC_LOG(ERROR, TAG, "option buffer too small");
1034 memcpy(option, data, len);
1040 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
1044 OIC_LOG(ERROR, TAG, "pdu is null");
1045 return CA_MSG_NONCONFIRM;
1048 // pdu minimum size is 4 byte.
1049 if (size < CA_PDU_MIN_SIZE)
1051 OIC_LOG(ERROR, TAG, "min size");
1052 return CA_MSG_NONCONFIRM;
1055 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1057 return (CAMessageType_t) hdr->coap_hdr_udp_t.type;
1060 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
1064 OIC_LOG(ERROR, TAG, "pdu is null");
1068 // pdu minimum size is 4 byte.
1069 if (size < CA_PDU_MIN_SIZE)
1071 OIC_LOG(ERROR, TAG, "min size");
1075 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1077 return hdr->coap_hdr_udp_t.id;
1080 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
1084 OIC_LOG(ERROR, TAG, "pdu is null");
1085 return CA_NOT_FOUND;
1088 // pdu minimum size is 4 byte.
1089 if (size < CA_PDU_MIN_SIZE)
1091 OIC_LOG(ERROR, TAG, "min size");
1092 return CA_NOT_FOUND;
1095 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1097 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->coap_hdr_udp_t.code);