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 // For details on compatibility and glibc support,
28 // Refer http://www.gnu.org/software/libc/manual/html_node/BSD-Random.html
29 #define _DEFAULT_SOURCE
31 #include "iotivity_config.h"
40 #include "caprotocolmessage.h"
42 #include "oic_malloc.h"
43 #include "oic_string.h"
45 #include "cacommonutil.h"
47 #define TAG "OIC_CA_PRTCL_MSG"
49 #define CA_PDU_MIN_SIZE (4)
50 #define CA_ENCODE_BUFFER_SIZE (4)
52 static const char COAP_URI_HEADER[] = "coap://[::]/";
55 static char g_chproxyUri[CA_MAX_URI_LENGTH];
57 CAResult_t CASetProxyUri(const char *uri)
59 VERIFY_NON_NULL(uri, TAG, "uri");
60 OICStrcpy(g_chproxyUri, sizeof (g_chproxyUri), uri);
65 CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
66 CARequestInfo_t *outReqInfo)
68 VERIFY_NON_NULL(pdu, TAG, "pdu");
69 VERIFY_NON_NULL(outReqInfo, TAG, "outReqInfo");
71 uint32_t code = CA_NOT_FOUND;
72 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outReqInfo->info));
73 outReqInfo->method = code;
78 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
79 const CAEndpoint_t *endpoint)
81 VERIFY_NON_NULL(pdu, TAG, "pdu");
82 VERIFY_NON_NULL(outResInfo, TAG, "outResInfo");
84 uint32_t code = CA_NOT_FOUND;
85 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outResInfo->info));
86 outResInfo->result = code;
91 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
92 CAErrorInfo_t *errorInfo)
94 VERIFY_NON_NULL(pdu, TAG, "pdu");
97 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &errorInfo->info);
102 coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint,
103 coap_list_t **optlist, coap_transport_t *transport)
105 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
106 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
107 VERIFY_NON_NULL_RET(optlist, TAG, "optlist", NULL);
109 OIC_LOG_V(DEBUG, TAG, "generate pdu for [%d]adapter, [%d]flags",
110 endpoint->adapter, endpoint->flags);
112 coap_pdu_t *pdu = NULL;
114 // RESET have to use only 4byte (empty message)
115 // and ACKNOWLEDGE can use empty message when code is empty.
116 if (CA_MSG_RESET == info->type || (CA_EMPTY == code && CA_MSG_ACKNOWLEDGE == info->type))
118 if (CA_EMPTY != code)
120 OIC_LOG(ERROR, TAG, "reset is not empty message");
124 if (info->payloadSize > 0 || info->payload || info->token || info->tokenLength > 0)
126 OIC_LOG(ERROR, TAG, "Empty message has unnecessary data after messageID");
130 OIC_LOG(DEBUG, TAG, "code is empty");
131 if (!(pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, NULL, transport)))
133 OIC_LOG(ERROR, TAG, "pdu NULL");
139 if (info->resourceUri)
141 OIC_LOG_V(DEBUG, TAG, "uri : %s", info->resourceUri);
143 uint32_t length = strlen(info->resourceUri);
144 if (CA_MAX_URI_LENGTH < length)
146 OIC_LOG(ERROR, TAG, "URI len err");
150 uint32_t uriLength = length + sizeof(COAP_URI_HEADER);
151 char *coapUri = (char *) OICCalloc(1, uriLength);
154 OIC_LOG(ERROR, TAG, "out of memory");
157 OICStrcat(coapUri, uriLength, COAP_URI_HEADER);
158 OICStrcat(coapUri, uriLength, info->resourceUri);
160 // parsing options in URI
161 CAResult_t res = CAParseURI(coapUri, optlist);
162 if (CA_STATUS_OK != res)
170 // parsing options in HeadOption
171 CAResult_t ret = CAParseHeadOption(code, info, optlist);
172 if (CA_STATUS_OK != ret)
177 pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, *optlist, transport);
180 OIC_LOG(ERROR, TAG, "pdu NULL");
185 // pdu print method : coap_show_pdu(pdu);
189 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode,
190 const CAEndpoint_t *endpoint)
192 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
193 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
195 coap_transport_t transport = COAP_UDP;
197 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
199 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)data)[0] >> 4);
203 coap_pdu_t *outpdu = coap_new_pdu2(transport, length);
206 OIC_LOG(ERROR, TAG, "outpdu is null");
210 OIC_LOG_V(DEBUG, TAG, "pdu parse-transport type : %d", transport);
212 int ret = coap_pdu_parse2((unsigned char *) data, length, outpdu, transport);
213 OIC_LOG_V(DEBUG, TAG, "pdu parse ret: %d", ret);
216 OIC_LOG(ERROR, TAG, "pdu parse failed");
221 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
223 OIC_LOG(INFO, TAG, "there is no version info in coap header");
228 if (outpdu->transport_hdr->udp.version != COAP_DEFAULT_VERSION)
230 OIC_LOG_V(ERROR, TAG, "coap version is not available : %d",
231 outpdu->transport_hdr->udp.version);
234 if (outpdu->transport_hdr->udp.token_length > CA_MAX_TOKEN_LEN)
236 OIC_LOG_V(ERROR, TAG, "token length has been exceed : %d",
237 outpdu->transport_hdr->udp.token_length);
244 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(outpdu, transport));
250 coap_delete_pdu(outpdu);
254 coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
255 const CAEndpoint_t *endpoint, coap_list_t *options,
256 coap_transport_t *transport)
258 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
259 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
260 VERIFY_NON_NULL_RET(transport, TAG, "transport", NULL);
262 unsigned int length = COAP_MAX_PDU_SIZE;
264 unsigned int msgLength = 0;
265 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
269 unsigned short prevOptNumber = 0;
270 for (coap_list_t *opt = options; opt; opt = opt->next)
272 unsigned short curOptNumber = COAP_OPTION_KEY(*(coap_option *) opt->data);
273 if (prevOptNumber > curOptNumber)
275 OIC_LOG(ERROR, TAG, "option list is wrong");
279 size_t optValueLen = COAP_OPTION_LENGTH(*(coap_option *) opt->data);
280 size_t optLength = coap_get_opt_header_length(curOptNumber - prevOptNumber, optValueLen);
283 OIC_LOG(ERROR, TAG, "Reserved for the Payload marker for the option");
286 msgLength += optLength;
287 prevOptNumber = curOptNumber;
288 OIC_LOG_V(DEBUG, TAG, "curOptNumber[%d], prevOptNumber[%d], optValueLen[%zu], "
289 "optLength[%zu], msgLength[%d]",
290 curOptNumber, prevOptNumber, optValueLen, optLength, msgLength);
294 if (info->payloadSize > 0)
296 msgLength = msgLength + info->payloadSize + PAYLOAD_MARKER;
298 *transport = coap_get_tcp_header_type_from_size(msgLength);
299 length = msgLength + coap_get_tcp_header_length_for_transport(*transport)
305 *transport = COAP_UDP;
308 coap_pdu_t *pdu = coap_new_pdu2(*transport, length);
312 OIC_LOG(ERROR, TAG, "malloc failed");
316 OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %zu",
317 *transport, info->payloadSize);
320 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
322 coap_add_length(pdu, *transport, msgLength);
327 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
328 uint16_t message_id = 0;
329 if (0 == info->messageId)
331 /* initialize message id */
332 prng((uint8_t * ) &message_id, sizeof(message_id));
334 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
338 /* use saved message id */
339 message_id = info->messageId;
341 pdu->transport_hdr->udp.id = message_id;
342 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->transport_hdr->udp.id);
344 pdu->transport_hdr->udp.type = info->type;
347 coap_add_code(pdu, *transport, code);
349 if (info->token && CA_EMPTY != code)
351 uint32_t tokenLength = info->tokenLength;
352 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
353 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
355 int32_t ret = coap_add_token2(pdu, tokenLength, (unsigned char *)info->token, *transport);
358 OIC_LOG(ERROR, TAG, "can't add token");
363 if (CA_ADAPTER_GATT_BTLE != endpoint->adapter
365 && !CAIsSupportedCoAPOverTCP(endpoint->adapter)
369 // option list will be added in blockwise-transfer
376 for (coap_list_t *opt = options; opt; opt = opt->next)
378 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
379 COAP_OPTION_DATA(*(coap_option *) opt->data));
381 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
382 coap_add_option2(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
383 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
384 COAP_OPTION_DATA(*(coap_option *) opt->data), *transport);
388 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
390 if (NULL != info->payload && 0 < info->payloadSize)
392 OIC_LOG(DEBUG, TAG, "payload is added");
393 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
399 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
401 VERIFY_NON_NULL(uriInfo, TAG, "uriInfo");
402 VERIFY_NON_NULL(optlist, TAG, "optlist");
404 /* split arg into Uri-* options */
406 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
408 if (uri.port != COAP_DEFAULT_PORT)
410 unsigned char portbuf[CA_ENCODE_BUFFER_SIZE] = { 0 };
411 int ret = coap_insert(optlist,
412 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
413 coap_encode_var_bytes(portbuf, uri.port),
418 return CA_STATUS_INVALID_PARAM;
422 if (uri.path.s && uri.path.length)
424 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
425 COAP_OPTION_URI_PATH, optlist);
426 if (CA_STATUS_OK != ret)
428 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
433 if (uri.query.s && uri.query.length)
435 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
437 if (CA_STATUS_OK != ret)
439 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
447 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
448 coap_list_t **optlist)
450 VERIFY_NON_NULL(optlist, TAG, "optlist");
452 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
454 // should never occur. Log just in case.
455 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
456 return CA_NOT_SUPPORTED;
458 else if (str && length)
460 unsigned char uriBuffer[CA_MAX_URI_LENGTH] = { 0 };
461 unsigned char *pBuf = uriBuffer;
462 size_t buflen = sizeof(uriBuffer);
463 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
464 coap_split_query(str, length, pBuf, &buflen);
471 int ret = coap_insert(optlist,
472 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
473 (char *)COAP_OPT_VALUE(pBuf)),
477 return CA_STATUS_INVALID_PARAM;
480 size_t optSize = COAP_OPT_SIZE(pBuf);
481 if ((prevIdx + optSize) < buflen)
490 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
491 return CA_STATUS_FAILED;
496 OIC_LOG(ERROR, TAG, "str or length is not available");
497 return CA_STATUS_FAILED;
503 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
506 VERIFY_NON_NULL_RET(info, TAG, "info", CA_STATUS_INVALID_PARAM);
508 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
512 OIC_LOG(ERROR, TAG, "optlist is null");
513 return CA_STATUS_INVALID_PARAM;
516 for (uint32_t i = 0; i < info->numOptions; i++)
518 if(!(info->options + i))
520 OIC_LOG(ERROR, TAG, "options is not available");
521 return CA_STATUS_FAILED;
524 uint32_t id = (info->options + i)->optionID;
525 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
527 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
531 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
532 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
533 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
534 int ret = coap_insert(optlist,
535 CACreateNewOptionNode(id, (info->options + i)->optionLength,
536 (info->options + i)->optionData),
540 return CA_STATUS_INVALID_PARAM;
545 // insert one extra header with the payload format if applicable.
546 if (CA_FORMAT_UNDEFINED != info->payloadFormat)
548 coap_list_t* node = NULL;
549 uint8_t buf[CA_ENCODE_BUFFER_SIZE] = {0};
550 switch (info->payloadFormat)
552 case CA_FORMAT_APPLICATION_CBOR:
553 node = CACreateNewOptionNode(
554 COAP_OPTION_CONTENT_FORMAT,
555 coap_encode_var_bytes(buf, (unsigned short)COAP_MEDIATYPE_APPLICATION_CBOR),
559 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->payloadFormat);
563 OIC_LOG(ERROR, TAG, "format option not created");
564 return CA_STATUS_INVALID_PARAM;
566 int ret = coap_insert(optlist, node, CAOrderOpts);
570 OIC_LOG(ERROR, TAG, "format option not inserted in header");
571 return CA_STATUS_INVALID_PARAM;
574 if (CA_FORMAT_UNDEFINED != info->acceptFormat)
576 coap_list_t* node = NULL;
577 uint8_t buf[CA_ENCODE_BUFFER_SIZE] = {0};
578 switch (info->acceptFormat)
580 case CA_FORMAT_APPLICATION_CBOR:
581 node = CACreateNewOptionNode(
583 coap_encode_var_bytes(buf, (unsigned short)COAP_MEDIATYPE_APPLICATION_CBOR),
587 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->acceptFormat);
591 OIC_LOG(ERROR, TAG, "format option not created");
592 return CA_STATUS_INVALID_PARAM;
594 int ret = coap_insert(optlist, node, CAOrderOpts);
598 OIC_LOG(ERROR, TAG, "format option not inserted in header");
599 return CA_STATUS_INVALID_PARAM;
606 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
608 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
610 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
613 OIC_LOG(ERROR, TAG, "Out of memory");
616 memset(option, 0, sizeof(coap_option) + length + 1);
618 COAP_OPTION_KEY(*option) = key;
620 coap_option_def_t* def = coap_opt_def(key);
621 if (NULL != def && coap_is_var_bytes(def))
623 if (length > def->max)
625 // make sure we shrink the value so it fits the coap option definition
626 // by truncating the value, disregard the leading bytes.
627 OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]",
628 def->key, length, def->max);
629 data = &(data[length-def->max]);
632 // Shrink the encoding length to a minimum size for coap
633 // options that support variable length encoding.
634 COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes(
635 COAP_OPTION_DATA(*option),
636 coap_decode_var_bytes((unsigned char *)data, length));
640 COAP_OPTION_LENGTH(*option) = length;
641 memcpy(COAP_OPTION_DATA(*option), data, length);
644 /* we can pass NULL here as delete function since option is released automatically */
645 coap_list_t *node = coap_new_listnode(option, NULL);
649 OIC_LOG(ERROR, TAG, "node is NULL");
657 int CAOrderOpts(void *a, void *b)
661 return a < b ? -1 : 1;
664 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
669 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
672 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
675 coap_opt_t *option = NULL;
677 while ((option = coap_option_next(&opt_iter)))
679 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
680 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
681 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type
682 && COAP_OPTION_CONTENT_FORMAT != opt_iter.type
683 && COAP_OPTION_ACCEPT != opt_iter.type
684 && COAP_OPTION_URI_HOST != opt_iter.type && COAP_OPTION_URI_PORT != opt_iter.type
685 && COAP_OPTION_ETAG != opt_iter.type && COAP_OPTION_MAXAGE != opt_iter.type
686 && COAP_OPTION_PROXY_SCHEME != opt_iter.type)
695 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
696 uint32_t *outCode, CAInfo_t *outInfo)
698 VERIFY_NON_NULL(pdu, TAG, "pdu");
699 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
700 VERIFY_NON_NULL(outCode, TAG, "outCode");
701 VERIFY_NON_NULL(outInfo, TAG, "outInfo");
703 coap_transport_t transport;
705 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
707 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->transport_hdr)[0] >> 4);
712 transport = COAP_UDP;
715 coap_opt_iterator_t opt_iter;
716 coap_option_iterator_init2((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport);
720 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(pdu, transport));
723 // init HeaderOption list
724 uint32_t count = CAGetOptionCount(opt_iter);
725 memset(outInfo, 0, sizeof(*outInfo));
727 outInfo->numOptions = count;
730 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
733 outInfo->type = CA_MSG_NONCONFIRM;
734 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
742 outInfo->type = pdu->transport_hdr->udp.type;
745 outInfo->messageId = pdu->transport_hdr->udp.id;
746 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
747 outInfo->acceptFormat = CA_FORMAT_UNDEFINED;
752 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
753 if (NULL == outInfo->options)
755 OIC_LOG(ERROR, TAG, "Out of memory");
756 return CA_MEMORY_ALLOC_FAILED;
760 coap_opt_t *option = NULL;
761 char optionResult[CA_MAX_URI_LENGTH] = {0};
763 uint32_t optionLength = 0;
764 bool isfirstsetflag = false;
765 bool isQueryBeingProcessed = false;
767 bool isProxyRequest = false;
770 while ((option = coap_option_next(&opt_iter)))
772 char buf[COAP_MAX_PDU_SIZE] = {0};
774 CAGetOptionData(opt_iter.type, (uint8_t *)(COAP_OPT_VALUE(option)),
775 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf));
778 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
779 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
781 if (false == isfirstsetflag)
783 isfirstsetflag = true;
784 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;
799 if (COAP_OPTION_URI_PATH == opt_iter.type)
801 // Make sure there is enough room in the optionResult buffer
802 if (optionLength < sizeof(optionResult))
804 optionResult[optionLength] = '/';
812 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
814 if (false == isQueryBeingProcessed)
816 // Make sure there is enough room in the optionResult buffer
817 if (optionLength < sizeof(optionResult))
819 optionResult[optionLength] = '?';
821 isQueryBeingProcessed = true;
830 // Make sure there is enough room in the optionResult buffer
831 if (optionLength < sizeof(optionResult))
833 optionResult[optionLength] = ';';
842 // Make sure there is enough room in the optionResult buffer
843 if ((optionLength + bufLength) < sizeof(optionResult))
845 memcpy(&optionResult[optionLength], buf, bufLength);
846 optionLength += bufLength;
854 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
855 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
857 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
859 else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type)
861 if (1 == COAP_OPT_LENGTH(option))
863 outInfo->payloadFormat = CAConvertFormat((uint8_t)buf[0]);
867 outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED;
868 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
869 opt_iter.type, (uint8_t)buf[0]);
872 else if (COAP_OPTION_ACCEPT == opt_iter.type)
874 if (1 == COAP_OPT_LENGTH(option))
876 outInfo->acceptFormat = CAConvertFormat((uint8_t)buf[0]);
880 outInfo->acceptFormat = CA_FORMAT_UNSUPPORTED;
882 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
883 opt_iter.type, (uint8_t)buf[0]);
885 else if (COAP_OPTION_URI_PORT == opt_iter.type ||
886 COAP_OPTION_URI_HOST == opt_iter.type ||
887 COAP_OPTION_ETAG == opt_iter.type ||
888 COAP_OPTION_MAXAGE == opt_iter.type ||
889 COAP_OPTION_PROXY_SCHEME== opt_iter.type)
891 OIC_LOG_V(INFO, TAG, "option[%d] has an unsupported format [%d]",
892 opt_iter.type, (uint8_t)buf[0]);
897 if (COAP_OPTION_PROXY_URI == opt_iter.type)
899 isProxyRequest = true;
904 if (bufLength <= sizeof(outInfo->options[0].optionData))
906 outInfo->options[idx].optionID = opt_iter.type;
907 outInfo->options[idx].optionLength = bufLength;
908 outInfo->options[idx].protocolID = CA_COAP_ID;
909 memcpy(outInfo->options[idx].optionData, buf, bufLength);
917 unsigned char* token = NULL;
918 unsigned int token_length = 0;
919 coap_get_token2(pdu->transport_hdr, transport, &token, &token_length);
922 if (token_length > 0)
924 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", token_length);
925 outInfo->token = (char *) OICMalloc(token_length);
926 if (NULL == outInfo->token)
928 OIC_LOG(ERROR, TAG, "Out of memory");
929 OICFree(outInfo->options);
930 return CA_MEMORY_ALLOC_FAILED;
932 memcpy(outInfo->token, token, token_length);
935 outInfo->tokenLength = token_length;
940 if (coap_get_data(pdu, &dataSize, &data))
942 OIC_LOG(DEBUG, TAG, "inside pdu->data");
943 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
944 if (NULL == outInfo->payload)
946 OIC_LOG(ERROR, TAG, "Out of memory");
947 OICFree(outInfo->options);
948 OICFree(outInfo->token);
949 return CA_MEMORY_ALLOC_FAILED;
951 memcpy(outInfo->payload, pdu->data, dataSize);
952 outInfo->payloadSize = dataSize;
955 if (optionResult[0] != '\0')
957 OIC_LOG_V(DEBUG, TAG, "URL length:%zu", strlen(optionResult));
958 outInfo->resourceUri = OICStrdup(optionResult);
959 if (!outInfo->resourceUri)
961 OIC_LOG(ERROR, TAG, "Out of memory");
962 OICFree(outInfo->options);
963 OICFree(outInfo->token);
964 return CA_MEMORY_ALLOC_FAILED;
968 else if(isProxyRequest && g_chproxyUri[0] != '\0')
971 * A request for CoAP-HTTP Proxy will not have any uri element as per CoAP specs
972 * and only COAP_OPTION_PROXY_URI will be present. Use preset proxy uri
975 outInfo->resourceUri = OICStrdup(g_chproxyUri);
976 if (!outInfo->resourceUri)
978 OIC_LOG(ERROR, TAG, "Out of memory");
979 OICFree(outInfo->options);
980 OICFree(outInfo->token);
981 return CA_MEMORY_ALLOC_FAILED;
988 OIC_LOG(ERROR, TAG, "buffer too small");
989 OICFree(outInfo->options);
990 return CA_STATUS_FAILED;
993 CAResult_t CAGetTokenFromPDU(const coap_hdr_transport_t *pdu_hdr,
995 const CAEndpoint_t *endpoint)
997 VERIFY_NON_NULL(pdu_hdr, TAG, "pdu_hdr");
998 VERIFY_NON_NULL(outInfo, TAG, "outInfo");
999 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
1001 coap_transport_t transport = COAP_UDP;
1003 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
1005 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4);
1009 unsigned char* token = NULL;
1010 unsigned int token_length = 0;
1011 coap_get_token2(pdu_hdr, transport, &token, &token_length);
1014 if (token_length > 0)
1016 OIC_LOG_V(DEBUG, TAG, "token len:%d", token_length);
1017 outInfo->token = (char *) OICMalloc(token_length);
1018 if (NULL == outInfo->token)
1020 OIC_LOG(ERROR, TAG, "Out of memory");
1021 return CA_MEMORY_ALLOC_FAILED;
1023 memcpy(outInfo->token, token, token_length);
1026 outInfo->tokenLength = token_length;
1028 return CA_STATUS_OK;
1031 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
1033 VERIFY_NON_NULL(token, TAG, "token");
1035 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
1037 OIC_LOG(ERROR, TAG, "invalid token length");
1038 return CA_STATUS_INVALID_PARAM;
1041 // memory allocation
1042 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
1045 OIC_LOG(ERROR, TAG, "Out of memory");
1046 return CA_MEMORY_ALLOC_FAILED;
1049 OCFillRandomMem((uint8_t *)temp, tokenLength);
1054 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
1055 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
1057 return CA_STATUS_OK;
1060 void CADestroyTokenInternal(CAToken_t token)
1065 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
1066 uint8_t *option, uint32_t buflen)
1070 OIC_LOG(ERROR, TAG, "buflen 0");
1076 OIC_LOG(ERROR, TAG, "option buffer too small");
1080 coap_option_def_t* def = coap_opt_def(key);
1081 if (NULL != def && coap_is_var_bytes(def) && 0 == len)
1083 // A 0 length option is permitted in CoAP but the
1084 // rest or the stack is unaware of variable byte encoding
1085 // should remain that way so a 0 byte of length 1 is inserted.
1091 memcpy(option, data, len);
1098 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
1100 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", CA_MSG_NONCONFIRM);
1102 // pdu minimum size is 4 byte.
1103 if (size < CA_PDU_MIN_SIZE)
1105 OIC_LOG(ERROR, TAG, "min size");
1106 return CA_MSG_NONCONFIRM;
1109 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1111 return (CAMessageType_t) hdr->type;
1114 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
1116 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", 0);
1118 // pdu minimum size is 4 byte.
1119 if (size < CA_PDU_MIN_SIZE)
1121 OIC_LOG(ERROR, TAG, "min size");
1125 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1130 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
1132 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", CA_NOT_FOUND);
1134 // pdu minimum size is 4 byte.
1135 if (size < CA_PDU_MIN_SIZE)
1137 OIC_LOG(ERROR, TAG, "min size");
1138 return CA_NOT_FOUND;
1141 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1143 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->code);
1146 CAPayloadFormat_t CAConvertFormat(uint8_t format)
1150 case COAP_MEDIATYPE_TEXT_PLAIN:
1151 return CA_FORMAT_TEXT_PLAIN;
1152 case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT:
1153 return CA_FORMAT_APPLICATION_LINK_FORMAT;
1154 case COAP_MEDIATYPE_APPLICATION_XML:
1155 return CA_FORMAT_APPLICATION_XML;
1156 case COAP_MEDIATYPE_APPLICATION_OCTET_STREAM:
1157 return CA_FORMAT_APPLICATION_OCTET_STREAM;
1158 case COAP_MEDIATYPE_APPLICATION_RDF_XML:
1159 return CA_FORMAT_APPLICATION_RDF_XML;
1160 case COAP_MEDIATYPE_APPLICATION_EXI:
1161 return CA_FORMAT_APPLICATION_EXI;
1162 case COAP_MEDIATYPE_APPLICATION_JSON:
1163 return CA_FORMAT_APPLICATION_JSON;
1164 case COAP_MEDIATYPE_APPLICATION_CBOR:
1165 return CA_FORMAT_APPLICATION_CBOR;
1167 return CA_FORMAT_UNSUPPORTED;
1172 bool CAIsSupportedBlockwiseTransfer(CATransportAdapter_t adapter)
1174 if (CA_ADAPTER_IP & adapter || CA_ADAPTER_NFC & adapter
1175 || CA_DEFAULT_ADAPTER == adapter)
1184 bool CAIsSupportedCoAPOverTCP(CATransportAdapter_t adapter)
1186 if (CA_ADAPTER_GATT_BTLE & adapter || CA_ADAPTER_RFCOMM_BTEDR & adapter
1187 || CA_ADAPTER_TCP & adapter || CA_DEFAULT_ADAPTER == adapter)