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, const CAEndpoint_t *endpoint,
71 CARequestInfo_t *outReqInfo)
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, endpoint, &code, &(outReqInfo->info));
83 outReqInfo->method = code;
85 OIC_LOG(DEBUG, TAG, "OUT");
89 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
90 const CAEndpoint_t *endpoint)
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, endpoint, &code, &(outResInfo->info));
102 outResInfo->result = code;
104 OIC_LOG(DEBUG, TAG, "OUT");
108 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
109 CAErrorInfo_t *errorInfo)
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, endpoint, &code, &errorInfo->info);
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, info, endpoint, NULL)))
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, info, endpoint, optlist);
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 const CAEndpoint_t *endpoint)
217 OIC_LOG(DEBUG, TAG, "IN");
221 OIC_LOG(ERROR, TAG, "data is null");
225 coap_transport_type transport;
227 if (CA_ADAPTER_TCP == endpoint->adapter)
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, length);
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_ADAPTER_TCP == endpoint->adapter)
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, const CAInfo_t *info,
289 const CAEndpoint_t *endpoint, coap_list_t *options)
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;
296 unsigned int length = COAP_MAX_PDU_SIZE;
298 unsigned int headerLength = 0;
299 if (CA_ADAPTER_TCP == endpoint->adapter)
303 for (coap_list_t *opt = options; opt; opt = opt->next)
305 headerLength += COAP_OPTION_LENGTH(*(coap_option *) opt->data) + 1;
309 if (info->payloadSize > 0)
311 headerLength = headerLength + info->payloadSize + 1;
313 transport = coap_get_tcp_header_type_from_size(headerLength);
314 length = headerLength + coap_get_tcp_header_length_for_transport(transport)
320 transport = coap_udp;
323 coap_pdu_t *pdu = coap_new_pdu(transport, length);
327 OIC_LOG(ERROR, TAG, "malloc failed");
331 OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %d",
332 transport, info->payloadSize);
335 if (CA_ADAPTER_TCP == endpoint->adapter)
337 coap_add_length(pdu, transport, headerLength);
342 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
344 if (0 == info->messageId)
346 /* initialize message id */
347 prng((uint8_t * ) &message_id, sizeof(message_id));
349 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
353 /* use saved message id */
354 message_id = info->messageId;
356 pdu->hdr->coap_hdr_udp_t.id = message_id;
357 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->coap_hdr_udp_t.id);
359 pdu->hdr->coap_hdr_udp_t.type = info->type;
362 coap_add_code(pdu, transport, code);
364 if (info->token && CA_EMPTY != code)
366 uint32_t tokenLength = info->tokenLength;
367 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
368 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
370 int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token, transport);
373 OIC_LOG(ERROR, TAG, "can't add token");
379 for (coap_list_t *opt = options; opt; opt = opt->next)
381 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
382 COAP_OPTION_DATA(*(coap_option *) opt->data));
384 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
385 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
386 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
387 COAP_OPTION_DATA(*(coap_option *) opt->data), transport);
391 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
393 bool enabledPayload = false;
395 enabledPayload = true;
398 if (enabledPayload || CA_ADAPTER_GATT_BTLE == endpoint->adapter
400 || CA_ADAPTER_TCP == endpoint->adapter
404 if (NULL != info->payload && 0 < info->payloadSize)
406 OIC_LOG(DEBUG, TAG, "payload is added");
407 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
411 OIC_LOG(DEBUG, TAG, "OUT");
415 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
417 OIC_LOG(DEBUG, TAG, "IN");
421 OIC_LOG(ERROR, TAG, "uriInfo is null");
422 return CA_STATUS_INVALID_PARAM;
425 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
429 OIC_LOG(ERROR, TAG, "optlist is null");
430 return CA_STATUS_INVALID_PARAM;
433 /* split arg into Uri-* options */
435 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
437 if (uri.port != COAP_DEFAULT_PORT)
439 unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 };
440 int ret = coap_insert(optlist,
441 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
442 coap_encode_var_bytes(portbuf, uri.port),
447 return CA_STATUS_INVALID_PARAM;
451 if (uri.path.s && uri.path.length)
453 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
454 COAP_OPTION_URI_PATH, optlist);
455 if (CA_STATUS_OK != ret)
457 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
462 if (uri.query.s && uri.query.length)
464 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
466 if (CA_STATUS_OK != ret)
468 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
473 OIC_LOG(DEBUG, TAG, "OUT");
477 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
478 coap_list_t **optlist)
482 OIC_LOG(ERROR, TAG, "optlist is null");
483 return CA_STATUS_INVALID_PARAM;
486 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
488 // should never occur. Log just in case.
489 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
490 return CA_NOT_SUPPORTED;
492 else if (str && length)
494 unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
495 unsigned char *pBuf = uriBuffer;
496 size_t buflen = sizeof(uriBuffer);
497 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
498 coap_split_query(str, length, pBuf, &buflen);
505 int ret = coap_insert(optlist,
506 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
507 (char *)COAP_OPT_VALUE(pBuf)),
511 return CA_STATUS_INVALID_PARAM;
514 size_t optSize = COAP_OPT_SIZE(pBuf);
515 if ((prevIdx + optSize) < buflen)
524 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
525 return CA_STATUS_FAILED;
530 OIC_LOG(ERROR, TAG, "str or length is not available");
531 return CA_STATUS_FAILED;
537 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
540 OIC_LOG(DEBUG, TAG, "IN");
541 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", CA_STATUS_INVALID_PARAM);
543 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
547 OIC_LOG(ERROR, TAG, "optlist is null");
548 return CA_STATUS_INVALID_PARAM;
551 for (uint32_t i = 0; i < info->numOptions; i++)
553 if(!(info->options + i))
555 OIC_LOG(ERROR, TAG, "options is not available");
556 return CA_STATUS_FAILED;
559 uint32_t id = (info->options + i)->optionID;
560 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
562 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
566 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
567 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
568 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
569 int ret = coap_insert(optlist,
570 CACreateNewOptionNode(id, (info->options + i)->optionLength,
571 (info->options + i)->optionData),
575 return CA_STATUS_INVALID_PARAM;
580 // insert one extra header with the payload format if applicable.
581 if (CA_FORMAT_UNDEFINED != info->payloadFormat)
583 coap_list_t* node = NULL;
584 uint8_t buf[3] = {0};
585 switch (info->payloadFormat) {
586 case CA_FORMAT_APPLICATION_CBOR:
587 node = CACreateNewOptionNode(
588 COAP_OPTION_CONTENT_FORMAT,
589 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
593 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->payloadFormat);
597 OIC_LOG(ERROR, TAG, "format option not created");
598 return CA_STATUS_INVALID_PARAM;
600 int ret = coap_insert(optlist, node, CAOrderOpts);
604 OIC_LOG(ERROR, TAG, "format option not inserted in header");
605 return CA_STATUS_INVALID_PARAM;
608 if (CA_FORMAT_UNDEFINED != info->acceptFormat)
610 coap_list_t* node = NULL;
611 uint8_t buf[3] = {0};
612 switch (info->acceptFormat) {
613 case CA_FORMAT_APPLICATION_CBOR:
614 node = CACreateNewOptionNode(
616 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
620 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->acceptFormat);
624 OIC_LOG(ERROR, TAG, "format option not created");
625 return CA_STATUS_INVALID_PARAM;
627 int ret = coap_insert(optlist, node, CAOrderOpts);
631 OIC_LOG(ERROR, TAG, "format option not inserted in header");
632 return CA_STATUS_INVALID_PARAM;
636 OIC_LOG(DEBUG, TAG, "OUT");
640 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
642 OIC_LOG(DEBUG, TAG, "IN");
646 OIC_LOG(ERROR, TAG, "invalid pointer parameter");
650 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
653 OIC_LOG(ERROR, TAG, "Out of memory");
656 memset(option, 0, sizeof(coap_option) + length + 1);
658 COAP_OPTION_KEY(*option) = key;
660 coap_option_def_t* def = coap_opt_def(key);
661 if (NULL != def && coap_is_var_bytes(def))
663 if (length > def->max)
665 // make sure we shrink the value so it fits the coap option definition
666 // by truncating the value, disregard the leading bytes.
667 OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]",
668 def->key, length, def->max);
669 data = &(data[length-def->max]);
672 // Shrink the encoding length to a minimum size for coap
673 // options that support variable length encoding.
674 COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes(
675 COAP_OPTION_DATA(*option),
676 coap_decode_var_bytes((unsigned char *)data, length));
680 COAP_OPTION_LENGTH(*option) = length;
681 memcpy(COAP_OPTION_DATA(*option), data, length);
684 /* we can pass NULL here as delete function since option is released automatically */
685 coap_list_t *node = coap_new_listnode(option, NULL);
689 OIC_LOG(ERROR, TAG, "node is NULL");
694 OIC_LOG(DEBUG, TAG, "OUT");
698 int CAOrderOpts(void *a, void *b)
700 OIC_LOG(DEBUG, TAG, "IN");
703 return a < b ? -1 : 1;
706 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
711 OIC_LOG(DEBUG, TAG, "OUT");
712 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
715 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
717 OIC_LOG(DEBUG, TAG, "IN");
721 while ((option = coap_option_next(&opt_iter)))
723 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
724 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
725 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type
726 && COAP_OPTION_CONTENT_FORMAT != opt_iter.type
727 && COAP_OPTION_ACCEPT != opt_iter.type)
733 OIC_LOG(DEBUG, TAG, "OUT");
737 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
738 uint32_t *outCode, CAInfo_t *outInfo)
740 OIC_LOG(DEBUG, TAG, "IN");
742 if (!pdu || !outCode || !outInfo)
744 OIC_LOG(ERROR, TAG, "NULL pointer param");
745 return CA_STATUS_INVALID_PARAM;
748 coap_transport_type transport;
750 if (CA_ADAPTER_TCP == endpoint->adapter)
752 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->hdr)[0] >> 4);
757 transport = coap_udp;
760 coap_opt_iterator_t opt_iter;
761 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport);
765 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(pdu, transport));
768 // init HeaderOption list
769 uint32_t count = CAGetOptionCount(opt_iter);
770 memset(outInfo, 0, sizeof(*outInfo));
772 outInfo->numOptions = count;
775 if (CA_ADAPTER_TCP == endpoint->adapter)
778 outInfo->type = CA_MSG_NONCONFIRM;
779 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
785 outInfo->type = pdu->hdr->coap_hdr_udp_t.type;
788 outInfo->messageId = pdu->hdr->coap_hdr_udp_t.id;
789 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
790 outInfo->acceptFormat = CA_FORMAT_UNDEFINED;
795 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
796 if (NULL == outInfo->options)
798 OIC_LOG(ERROR, TAG, "Out of memory");
799 return CA_MEMORY_ALLOC_FAILED;
804 char optionResult[CA_MAX_URI_LENGTH] = {0};
806 uint32_t optionLength = 0;
807 bool isfirstsetflag = false;
808 bool isQueryBeingProcessed = false;
810 while ((option = coap_option_next(&opt_iter)))
812 char buf[COAP_MAX_PDU_SIZE] = {0};
814 CAGetOptionData(opt_iter.type, (uint8_t *)(COAP_OPT_VALUE(option)),
815 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf));
818 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
819 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
821 if (false == isfirstsetflag)
823 isfirstsetflag = true;
824 optionResult[optionLength] = '/';
826 // Make sure there is enough room in the optionResult buffer
827 if ((optionLength + bufLength) < sizeof(optionResult))
829 memcpy(&optionResult[optionLength], buf, bufLength);
830 optionLength += bufLength;
839 if (COAP_OPTION_URI_PATH == opt_iter.type)
841 // Make sure there is enough room in the optionResult buffer
842 if (optionLength < sizeof(optionResult))
844 optionResult[optionLength] = '/';
852 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
854 if (false == isQueryBeingProcessed)
856 // Make sure there is enough room in the optionResult buffer
857 if (optionLength < sizeof(optionResult))
859 optionResult[optionLength] = '?';
861 isQueryBeingProcessed = true;
870 // Make sure there is enough room in the optionResult buffer
871 if (optionLength < sizeof(optionResult))
873 optionResult[optionLength] = ';';
882 // Make sure there is enough room in the optionResult buffer
883 if ((optionLength + bufLength) < sizeof(optionResult))
885 memcpy(&optionResult[optionLength], buf, bufLength);
886 optionLength += bufLength;
894 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
895 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
897 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
899 else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type)
901 if (1 == COAP_OPT_LENGTH(option))
903 outInfo->payloadFormat = CAConvertFormat((uint8_t)buf[0]);
907 outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED;
908 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
909 opt_iter.type, (uint8_t)buf[0]);
912 else if (COAP_OPTION_ACCEPT == opt_iter.type)
914 if (1 == COAP_OPT_LENGTH(option))
916 outInfo->acceptFormat = CAConvertFormat((uint8_t)buf[0]);
920 outInfo->acceptFormat = CA_FORMAT_UNSUPPORTED;
922 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
923 opt_iter.type, (uint8_t)buf[0]);
929 if (bufLength <= sizeof(outInfo->options[0].optionData))
931 outInfo->options[idx].optionID = opt_iter.type;
932 outInfo->options[idx].optionLength = bufLength;
933 outInfo->options[idx].protocolID = CA_COAP_ID;
934 memcpy(outInfo->options[idx].optionData, buf, bufLength);
942 unsigned char* token = NULL;
943 unsigned int token_length = 0;
944 coap_get_token(pdu->hdr, transport, &token, &token_length);
947 if (token_length > 0)
949 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", token_length);
950 outInfo->token = (char *) OICMalloc(token_length);
951 if (NULL == outInfo->token)
953 OIC_LOG(ERROR, TAG, "Out of memory");
954 OICFree(outInfo->options);
955 return CA_MEMORY_ALLOC_FAILED;
957 memcpy(outInfo->token, token, token_length);
960 outInfo->tokenLength = token_length;
965 if (coap_get_data(pdu, &dataSize, &data))
967 OIC_LOG(DEBUG, TAG, "inside pdu->data");
968 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
969 if (NULL == outInfo->payload)
971 OIC_LOG(ERROR, TAG, "Out of memory");
972 OICFree(outInfo->options);
973 OICFree(outInfo->token);
974 return CA_MEMORY_ALLOC_FAILED;
976 memcpy(outInfo->payload, pdu->data, dataSize);
977 outInfo->payloadSize = dataSize;
980 if (optionResult[0] != '\0')
982 OIC_LOG_V(DEBUG, TAG, "URL length:%d", strlen(optionResult));
983 outInfo->resourceUri = OICStrdup(optionResult);
984 if (!outInfo->resourceUri)
986 OIC_LOG(ERROR, TAG, "Out of memory");
987 OICFree(outInfo->options);
988 OICFree(outInfo->token);
989 return CA_MEMORY_ALLOC_FAILED;
993 OIC_LOG(DEBUG, TAG, "OUT");
997 OIC_LOG(ERROR, TAG, "buffer too small");
998 OICFree(outInfo->options);
999 return CA_STATUS_FAILED;
1002 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo,
1003 const CAEndpoint_t *endpoint)
1005 OIC_LOG(DEBUG, TAG, "IN");
1006 if (NULL == pdu_hdr)
1008 OIC_LOG(ERROR, TAG, "pdu_hdr is null");
1009 return CA_STATUS_INVALID_PARAM;
1012 if (NULL == outInfo)
1014 OIC_LOG(ERROR, TAG, "outInfo is null");
1015 return CA_STATUS_INVALID_PARAM;
1018 coap_transport_type transport;
1020 if (CA_ADAPTER_TCP == endpoint->adapter)
1022 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4);
1027 transport = coap_udp;
1030 unsigned char* token = NULL;
1031 unsigned int token_length = 0;
1032 coap_get_token(pdu_hdr, transport, &token, &token_length);
1035 if (token_length > 0)
1037 OIC_LOG_V(DEBUG, TAG, "token len:%d", token_length);
1038 outInfo->token = (char *) OICMalloc(token_length);
1039 if (NULL == outInfo->token)
1041 OIC_LOG(ERROR, TAG, "Out of memory");
1042 return CA_MEMORY_ALLOC_FAILED;
1044 memcpy(outInfo->token, token, token_length);
1047 outInfo->tokenLength = token_length;
1049 OIC_LOG(DEBUG, TAG, "OUT");
1051 return CA_STATUS_OK;
1054 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
1056 OIC_LOG(DEBUG, TAG, "IN");
1060 OIC_LOG(ERROR, TAG, "invalid token pointer");
1061 return CA_STATUS_INVALID_PARAM;
1064 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
1066 OIC_LOG(ERROR, TAG, "invalid token length");
1067 return CA_STATUS_INVALID_PARAM;
1077 if (SEED == (unsigned int)((time_t)-1))
1079 OIC_LOG(ERROR, TAG, "seed is not made");
1081 return CA_STATUS_FAILED;
1090 // memory allocation
1091 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
1094 OIC_LOG(ERROR, TAG, "Out of memory");
1095 return CA_MEMORY_ALLOC_FAILED;
1099 for (uint8_t index = 0; index < tokenLength; index++)
1101 // use valid characters
1103 temp[index] = rand() & 0x00FF;
1105 temp[index] = random() & 0x00FF;
1112 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
1113 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
1115 OIC_LOG(DEBUG, TAG, "OUT");
1116 return CA_STATUS_OK;
1119 void CADestroyTokenInternal(CAToken_t token)
1121 OIC_LOG(DEBUG, TAG, "IN");
1123 OIC_LOG(DEBUG, TAG, "OUT");
1126 void CADestroyInfo(CAInfo_t *info)
1128 OIC_LOG(DEBUG, TAG, "IN");
1132 OIC_LOG(DEBUG, TAG, "free options");
1133 OICFree(info->options);
1135 OIC_LOG(DEBUG, TAG, "free token");
1136 OICFree(info->token);
1138 OIC_LOG(DEBUG, TAG, "free payload");
1139 OICFree(info->payload);
1142 OIC_LOG(DEBUG, TAG, "OUT");
1145 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
1146 uint8_t *option, uint32_t buflen)
1150 OIC_LOG(ERROR, TAG, "buflen 0");
1156 OIC_LOG(ERROR, TAG, "option buffer too small");
1160 coap_option_def_t* def = coap_opt_def(key);
1161 if(NULL != def && coap_is_var_bytes(def) && 0 == len) {
1162 // A 0 length option is permitted in CoAP but the
1163 // rest or the stack is unaware of variable byte encoding
1164 // should remain that way so a 0 byte of length 1 is inserted.
1168 memcpy(option, data, len);
1175 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
1179 OIC_LOG(ERROR, TAG, "pdu is null");
1180 return CA_MSG_NONCONFIRM;
1183 // pdu minimum size is 4 byte.
1184 if (size < CA_PDU_MIN_SIZE)
1186 OIC_LOG(ERROR, TAG, "min size");
1187 return CA_MSG_NONCONFIRM;
1190 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1192 return (CAMessageType_t) hdr->coap_hdr_udp_t.type;
1195 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
1199 OIC_LOG(ERROR, TAG, "pdu is null");
1203 // pdu minimum size is 4 byte.
1204 if (size < CA_PDU_MIN_SIZE)
1206 OIC_LOG(ERROR, TAG, "min size");
1210 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1212 return hdr->coap_hdr_udp_t.id;
1215 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
1219 OIC_LOG(ERROR, TAG, "pdu is null");
1220 return CA_NOT_FOUND;
1223 // pdu minimum size is 4 byte.
1224 if (size < CA_PDU_MIN_SIZE)
1226 OIC_LOG(ERROR, TAG, "min size");
1227 return CA_NOT_FOUND;
1230 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1232 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->coap_hdr_udp_t.code);
1235 CAPayloadFormat_t CAConvertFormat(uint8_t format)
1239 case COAP_MEDIATYPE_TEXT_PLAIN:
1240 return CA_FORMAT_TEXT_PLAIN;
1241 case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT:
1242 return CA_FORMAT_APPLICATION_LINK_FORMAT;
1243 case COAP_MEDIATYPE_APPLICATION_XML:
1244 return CA_FORMAT_APPLICATION_XML;
1245 case COAP_MEDIATYPE_APPLICATION_OCTET_STREAM:
1246 return CA_FORMAT_APPLICATION_OCTET_STREAM;
1247 case COAP_MEDIATYPE_APPLICATION_RDF_XML:
1248 return CA_FORMAT_APPLICATION_RDF_XML;
1249 case COAP_MEDIATYPE_APPLICATION_EXI:
1250 return CA_FORMAT_APPLICATION_EXI;
1251 case COAP_MEDIATYPE_APPLICATION_JSON:
1252 return CA_FORMAT_APPLICATION_JSON;
1253 case COAP_MEDIATYPE_APPLICATION_CBOR:
1254 return CA_FORMAT_APPLICATION_CBOR;
1256 return CA_FORMAT_UNSUPPORTED;