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);
243 if (outpdu->hdr->token_length > CA_MAX_TOKEN_LEN)
245 OIC_LOG_V(ERROR, TAG, "token length has been exceed : %d",
246 outpdu->hdr->token_length);
247 coap_delete_pdu(outpdu);
253 (*outCode) = (uint32_t) CA_RESPONSE_CODE(outpdu->hdr->code);
256 OIC_LOG(DEBUG, TAG, "OUT");
260 coap_pdu_t *CAGeneratePDUImpl(code_t code, coap_list_t *options, const CAInfo_t *info,
261 const CAEndpoint_t *endpoint)
263 OIC_LOG(DEBUG, TAG, "IN");
264 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
265 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
267 coap_pdu_t *pdu = coap_new_pdu();
271 OIC_LOG(ERROR, TAG, "malloc failed");
275 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
277 if (0 == info->messageId)
279 /* initialize message id */
280 prng((uint8_t * ) &message_id, sizeof(message_id));
282 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
286 /* use saved message id */
287 message_id = info->messageId;
289 pdu->hdr->id = message_id;
290 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->id);
292 pdu->hdr->type = info->type;
293 pdu->hdr->code = COAP_RESPONSE_CODE(code);
295 if (info->token && CA_EMPTY != code)
297 uint32_t tokenLength = info->tokenLength;
298 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
299 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
301 int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token);
304 OIC_LOG(ERROR, TAG, "can't add token");
310 for (coap_list_t *opt = options; opt; opt = opt->next)
312 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
313 COAP_OPTION_DATA(*(coap_option *) opt->data));
314 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
315 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
316 COAP_OPTION_DATA(*(coap_option *) opt->data));
320 bool enabledPayload = false;
322 enabledPayload = true;
325 if (enabledPayload || CA_ADAPTER_GATT_BTLE == endpoint->adapter)
327 if (NULL != info->payload && 0 < info->payloadSize)
329 OIC_LOG(DEBUG, TAG, "payload is added");
330 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
334 OIC_LOG(DEBUG, TAG, "OUT");
338 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
340 OIC_LOG(DEBUG, TAG, "IN");
344 OIC_LOG(ERROR, TAG, "uriInfo is null");
345 return CA_STATUS_INVALID_PARAM;
348 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
352 OIC_LOG(ERROR, TAG, "optlist is null");
353 return CA_STATUS_INVALID_PARAM;
356 /* split arg into Uri-* options */
358 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
360 if (uri.port != COAP_DEFAULT_PORT)
362 unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 };
363 int ret = coap_insert(optlist,
364 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
365 coap_encode_var_bytes(portbuf, uri.port),
370 return CA_STATUS_INVALID_PARAM;
374 if (uri.path.s && uri.path.length)
376 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
377 COAP_OPTION_URI_PATH, optlist);
378 if (CA_STATUS_OK != ret)
380 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
385 if (uri.query.s && uri.query.length)
387 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
389 if (CA_STATUS_OK != ret)
391 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
396 OIC_LOG(DEBUG, TAG, "OUT");
400 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
401 coap_list_t **optlist)
405 OIC_LOG(ERROR, TAG, "optlist is null");
406 return CA_STATUS_INVALID_PARAM;
409 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
411 // should never occur. Log just in case.
412 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
413 return CA_NOT_SUPPORTED;
415 else if (str && length)
417 unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
418 unsigned char *pBuf = uriBuffer;
419 size_t buflen = sizeof(uriBuffer);
420 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
421 coap_split_query(str, length, pBuf, &buflen);
428 int ret = coap_insert(optlist,
429 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
430 (char *)COAP_OPT_VALUE(pBuf)),
434 return CA_STATUS_INVALID_PARAM;
437 size_t optSize = COAP_OPT_SIZE(pBuf);
438 if ((prevIdx + optSize) < buflen)
447 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
448 return CA_STATUS_FAILED;
453 OIC_LOG(ERROR, TAG, "str or length is not available");
454 return CA_STATUS_FAILED;
460 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
463 OIC_LOG(DEBUG, TAG, "IN");
464 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", CA_STATUS_INVALID_PARAM);
466 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
470 OIC_LOG(ERROR, TAG, "optlist is null");
471 return CA_STATUS_INVALID_PARAM;
474 for (uint32_t i = 0; i < info->numOptions; i++)
476 if(!(info->options + i))
478 OIC_LOG(ERROR, TAG, "options is not available");
479 return CA_STATUS_FAILED;
482 uint32_t id = (info->options + i)->optionID;
483 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
485 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
489 if ((info->options + i)->optionData && (info->options + i)->optionLength > 0)
491 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
492 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
493 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
494 int ret = coap_insert(optlist,
495 CACreateNewOptionNode(id, (info->options + i)->optionLength,
496 (info->options + i)->optionData),
500 return CA_STATUS_INVALID_PARAM;
506 // insert one extra header with the payload format if applicable.
507 if (CA_FORMAT_UNDEFINED != info->payloadFormat)
509 coap_list_t* node = NULL;
510 uint8_t buf[3] = {0};
511 switch (info->payloadFormat) {
512 case CA_FORMAT_APPLICATION_CBOR:
513 node = CACreateNewOptionNode(
514 COAP_OPTION_CONTENT_FORMAT,
515 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
519 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->payloadFormat);
523 OIC_LOG(ERROR, TAG, "format option not created");
524 return CA_STATUS_INVALID_PARAM;
526 int ret = coap_insert(optlist, node, CAOrderOpts);
530 OIC_LOG(ERROR, TAG, "format option not inserted in header");
531 return CA_STATUS_INVALID_PARAM;
534 if (CA_FORMAT_UNDEFINED != info->acceptFormat)
536 coap_list_t* node = NULL;
537 uint8_t buf[3] = {0};
538 switch (info->acceptFormat) {
539 case CA_FORMAT_APPLICATION_CBOR:
540 node = CACreateNewOptionNode(
542 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
546 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->acceptFormat);
550 OIC_LOG(ERROR, TAG, "format option not created");
551 return CA_STATUS_INVALID_PARAM;
553 int ret = coap_insert(optlist, node, CAOrderOpts);
557 OIC_LOG(ERROR, TAG, "format option not inserted in header");
558 return CA_STATUS_INVALID_PARAM;
562 OIC_LOG(DEBUG, TAG, "OUT");
566 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
568 OIC_LOG(DEBUG, TAG, "IN");
572 OIC_LOG(ERROR, TAG, "invalid pointer parameter");
576 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
579 OIC_LOG(ERROR, TAG, "Out of memory");
582 memset(option, 0, sizeof(coap_option) + length + 1);
584 COAP_OPTION_KEY(*option) = key;
586 coap_option_def_t* def = coap_opt_def(key);
587 if (NULL != def && coap_is_var_bytes(def))
589 if (length > def->max)
591 // make sure we shrink the value so it fits the coap option definition
592 // by truncating the value, disregard the leading bytes.
593 OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]",
594 def->key, length, def->max);
595 data = &(data[length-def->max]);
598 // Shrink the encoding length to a minimum size for coap
599 // options that support variable length encoding.
600 COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes(
601 COAP_OPTION_DATA(*option),
602 coap_decode_var_bytes((unsigned char *)data, length));
606 COAP_OPTION_LENGTH(*option) = length;
607 memcpy(COAP_OPTION_DATA(*option), data, length);
610 /* we can pass NULL here as delete function since option is released automatically */
611 coap_list_t *node = coap_new_listnode(option, NULL);
615 OIC_LOG(ERROR, TAG, "node is NULL");
620 OIC_LOG(DEBUG, TAG, "OUT");
624 int CAOrderOpts(void *a, void *b)
626 OIC_LOG(DEBUG, TAG, "IN");
629 return a < b ? -1 : 1;
632 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
637 OIC_LOG(DEBUG, TAG, "OUT");
638 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
641 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
643 OIC_LOG(DEBUG, TAG, "IN");
647 while ((option = coap_option_next(&opt_iter)))
649 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
650 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
651 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type
652 && COAP_OPTION_CONTENT_FORMAT != opt_iter.type
653 && COAP_OPTION_ACCEPT != opt_iter.type)
659 OIC_LOG(DEBUG, TAG, "OUT");
663 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo)
665 OIC_LOG(DEBUG, TAG, "IN");
667 if (!pdu || !outCode || !outInfo)
669 OIC_LOG(ERROR, TAG, "NULL pointer param");
670 return CA_STATUS_INVALID_PARAM;
673 coap_opt_iterator_t opt_iter;
674 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL);
678 (*outCode) = (uint32_t) CA_RESPONSE_CODE(pdu->hdr->code);
681 // init HeaderOption list
682 uint32_t count = CAGetOptionCount(opt_iter);
683 memset(outInfo, 0, sizeof(*outInfo));
685 outInfo->numOptions = count;
688 outInfo->type = pdu->hdr->type;
691 outInfo->messageId = pdu->hdr->id;
692 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
693 outInfo->acceptFormat = CA_FORMAT_UNDEFINED;
697 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
698 if (NULL == outInfo->options)
700 OIC_LOG(ERROR, TAG, "Out of memory");
701 return CA_MEMORY_ALLOC_FAILED;
706 char optionResult[CA_MAX_URI_LENGTH] = {0};
708 uint32_t optionLength = 0;
709 bool isfirstsetflag = false;
710 bool isQueryBeingProcessed = false;
712 while ((option = coap_option_next(&opt_iter)))
714 char buf[COAP_MAX_PDU_SIZE] = {0};
716 CAGetOptionData(opt_iter.type, (uint8_t *)(COAP_OPT_VALUE(option)),
717 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf));
720 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
721 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
723 if (false == isfirstsetflag)
725 isfirstsetflag = true;
726 optionResult[optionLength] = '/';
728 // Make sure there is enough room in the optionResult buffer
729 if ((optionLength + bufLength) < sizeof(optionResult))
731 memcpy(&optionResult[optionLength], buf, bufLength);
732 optionLength += bufLength;
741 if (COAP_OPTION_URI_PATH == opt_iter.type)
743 // Make sure there is enough room in the optionResult buffer
744 if (optionLength < sizeof(optionResult))
746 optionResult[optionLength] = '/';
754 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
756 if (false == isQueryBeingProcessed)
758 // Make sure there is enough room in the optionResult buffer
759 if (optionLength < sizeof(optionResult))
761 optionResult[optionLength] = '?';
763 isQueryBeingProcessed = true;
772 // Make sure there is enough room in the optionResult buffer
773 if (optionLength < sizeof(optionResult))
775 optionResult[optionLength] = ';';
784 // Make sure there is enough room in the optionResult buffer
785 if ((optionLength + bufLength) < sizeof(optionResult))
787 memcpy(&optionResult[optionLength], buf, bufLength);
788 optionLength += bufLength;
796 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
797 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
799 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
801 else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type)
803 if (1 == COAP_OPT_LENGTH(option))
805 outInfo->payloadFormat = CAConvertFormat((uint8_t)buf[0]);
809 outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED;
810 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
811 opt_iter.type, (uint8_t)buf[0]);
814 else if (COAP_OPTION_ACCEPT == opt_iter.type)
816 if (1 == COAP_OPT_LENGTH(option))
818 outInfo->acceptFormat = CAConvertFormat((uint8_t)buf[0]);
822 outInfo->acceptFormat = CA_FORMAT_UNSUPPORTED;
824 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
825 opt_iter.type, (uint8_t)buf[0]);
831 uint32_t length = bufLength;
833 if (length <= CA_MAX_HEADER_OPTION_DATA_LENGTH)
835 outInfo->options[idx].optionID = opt_iter.type;
836 outInfo->options[idx].optionLength = length;
837 outInfo->options[idx].protocolID = CA_COAP_ID;
838 memcpy(outInfo->options[idx].optionData, buf, length);
847 if (pdu->hdr->token_length > 0)
849 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", pdu->hdr->token_length);
850 outInfo->token = (char *) OICMalloc(pdu->hdr->token_length);
851 if (NULL == outInfo->token)
853 OIC_LOG(ERROR, TAG, "Out of memory");
854 OICFree(outInfo->options);
855 return CA_MEMORY_ALLOC_FAILED;
857 memcpy(outInfo->token, pdu->hdr->token, pdu->hdr->token_length);
860 outInfo->tokenLength = pdu->hdr->token_length;
865 if (coap_get_data(pdu, &dataSize, &data))
867 OIC_LOG(DEBUG, TAG, "inside pdu->data");
868 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
869 if (NULL == outInfo->payload)
871 OIC_LOG(ERROR, TAG, "Out of memory");
872 OICFree(outInfo->options);
873 OICFree(outInfo->token);
874 return CA_MEMORY_ALLOC_FAILED;
876 memcpy(outInfo->payload, pdu->data, dataSize);
877 outInfo->payloadSize = dataSize;
880 if (optionResult[0] != '\0')
882 OIC_LOG_V(DEBUG, TAG, "URL length:%d", strlen(optionResult));
883 outInfo->resourceUri = OICStrdup(optionResult);
884 if (!outInfo->resourceUri)
886 OIC_LOG(ERROR, TAG, "Out of memory");
887 OICFree(outInfo->options);
888 OICFree(outInfo->token);
889 return CA_MEMORY_ALLOC_FAILED;
893 OIC_LOG(DEBUG, TAG, "OUT");
897 OIC_LOG(ERROR, TAG, "buffer too small");
898 OICFree(outInfo->options);
899 return CA_STATUS_FAILED;
902 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo)
904 OIC_LOG(DEBUG, TAG, "IN");
907 OIC_LOG(ERROR, TAG, "pdu_hdr is null");
908 return CA_STATUS_INVALID_PARAM;
913 OIC_LOG(ERROR, TAG, "outInfo is null");
914 return CA_STATUS_INVALID_PARAM;
918 if (pdu_hdr->token_length > 0)
920 OIC_LOG_V(DEBUG, TAG, "token len:%d", pdu_hdr->token_length);
921 outInfo->token = (char *) OICMalloc(pdu_hdr->token_length);
922 if (NULL == outInfo->token)
924 OIC_LOG(ERROR, TAG, "out of memory");
925 return CA_MEMORY_ALLOC_FAILED;
927 memcpy(outInfo->token, pdu_hdr->token, pdu_hdr->token_length);
930 outInfo->tokenLength = pdu_hdr->token_length;
932 OIC_LOG(DEBUG, TAG, "OUT");
937 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
939 OIC_LOG(DEBUG, TAG, "IN");
943 OIC_LOG(ERROR, TAG, "invalid token pointer");
944 return CA_STATUS_INVALID_PARAM;
947 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
949 OIC_LOG(ERROR, TAG, "invalid token length");
950 return CA_STATUS_INVALID_PARAM;
960 if (SEED == (unsigned int)((time_t)-1))
962 OIC_LOG(ERROR, TAG, "seed is not made");
964 return CA_STATUS_FAILED;
974 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
977 OIC_LOG(ERROR, TAG, "Out of memory");
978 return CA_MEMORY_ALLOC_FAILED;
982 for (uint8_t index = 0; index < tokenLength; index++)
984 // use valid characters
986 temp[index] = rand() & 0x00FF;
988 temp[index] = random() & 0x00FF;
995 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
996 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
998 OIC_LOG(DEBUG, TAG, "OUT");
1002 void CADestroyTokenInternal(CAToken_t token)
1004 OIC_LOG(DEBUG, TAG, "IN");
1006 OIC_LOG(DEBUG, TAG, "OUT");
1009 void CADestroyInfo(CAInfo_t *info)
1011 OIC_LOG(DEBUG, TAG, "IN");
1015 OIC_LOG(DEBUG, TAG, "free options");
1016 OICFree(info->options);
1018 OIC_LOG(DEBUG, TAG, "free token");
1019 OICFree(info->token);
1021 OIC_LOG(DEBUG, TAG, "free payload");
1022 OICFree(info->payload);
1025 OIC_LOG(DEBUG, TAG, "OUT");
1028 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
1029 uint8_t *option, uint32_t buflen)
1033 OIC_LOG(ERROR, TAG, "buflen 0");
1037 if (NULL == data || NULL == option)
1039 OIC_LOG(ERROR, TAG, "data/option NULL");
1045 OIC_LOG(ERROR, TAG, "option buffer too small");
1049 coap_option_def_t* def = coap_opt_def(key);
1050 if(NULL != def && coap_is_var_bytes(def) && 0 == len) {
1051 // A 0 length option is permitted in CoAP but the
1052 // rest or the stack is unaware of variable byte encoding
1053 // should remain that way so a 0 byte of length 1 is inserted.
1057 memcpy(option, data, len);
1064 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
1068 OIC_LOG(ERROR, TAG, "pdu is null");
1069 return CA_MSG_NONCONFIRM;
1072 // pdu minimum size is 4 byte.
1073 if (size < CA_PDU_MIN_SIZE)
1075 OIC_LOG(ERROR, TAG, "min size");
1076 return CA_MSG_NONCONFIRM;
1079 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1081 return (CAMessageType_t) hdr->type;
1084 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
1088 OIC_LOG(ERROR, TAG, "pdu is null");
1092 // pdu minimum size is 4 byte.
1093 if (size < CA_PDU_MIN_SIZE)
1095 OIC_LOG(ERROR, TAG, "min size");
1099 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1104 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
1108 OIC_LOG(ERROR, TAG, "pdu is null");
1109 return CA_NOT_FOUND;
1112 // pdu minimum size is 4 byte.
1113 if (size < CA_PDU_MIN_SIZE)
1115 OIC_LOG(ERROR, TAG, "min size");
1116 return CA_NOT_FOUND;
1119 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1121 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->code);
1124 CAPayloadFormat_t CAConvertFormat(uint8_t format)
1128 case COAP_MEDIATYPE_TEXT_PLAIN:
1129 return CA_FORMAT_TEXT_PLAIN;
1130 case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT:
1131 return CA_FORMAT_APPLICATION_LINK_FORMAT;
1132 case COAP_MEDIATYPE_APPLICATION_XML:
1133 return CA_FORMAT_APPLICATION_XML;
1134 case COAP_MEDIATYPE_APPLICATION_OCTET_STREAM:
1135 return CA_FORMAT_APPLICATION_OCTET_STREAM;
1136 case COAP_MEDIATYPE_APPLICATION_RDF_XML:
1137 return CA_FORMAT_APPLICATION_RDF_XML;
1138 case COAP_MEDIATYPE_APPLICATION_EXI:
1139 return CA_FORMAT_APPLICATION_EXI;
1140 case COAP_MEDIATYPE_APPLICATION_JSON:
1141 return CA_FORMAT_APPLICATION_JSON;
1142 case COAP_MEDIATYPE_APPLICATION_CBOR:
1143 return CA_FORMAT_APPLICATION_CBOR;
1145 return CA_FORMAT_UNSUPPORTED;