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 && info->resourceUri)
151 uint32_t length = strlen(info->resourceUri);
152 if (CA_MAX_URI_LENGTH < length)
154 OIC_LOG(ERROR, TAG, "URI len err");
158 uint32_t uriLength = length + sizeof(COAP_URI_HEADER);
159 char *coapUri = (char *) OICCalloc(1, uriLength);
162 OIC_LOG(ERROR, TAG, "out of memory");
165 OICStrcat(coapUri, uriLength, COAP_URI_HEADER);
166 OICStrcat(coapUri, uriLength, info->resourceUri);
168 // parsing options in URI
169 CAResult_t res = CAParseURI(coapUri, &optlist);
170 if (CA_STATUS_OK != res)
174 coap_delete_list(optlist);
182 // parsing options in HeadOption
183 CAResult_t ret = CAParseHeadOption(code, info, &optlist);
184 if (CA_STATUS_OK != ret)
186 coap_delete_list(optlist);
190 pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, optlist);
193 OIC_LOG(ERROR, TAG, "pdu NULL");
194 coap_delete_list(optlist);
199 coap_delete_list(optlist);
202 // pdu print method : coap_show_pdu(pdu);
203 OIC_LOG(DEBUG, TAG, "OUT");
207 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode,
208 const CAEndpoint_t *endpoint)
210 OIC_LOG(DEBUG, TAG, "IN");
214 OIC_LOG(ERROR, TAG, "data is null");
218 coap_transport_type transport;
220 if (CA_ADAPTER_TCP == endpoint->adapter)
222 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)data)[0] >> 4);
227 transport = coap_udp;
230 coap_pdu_t *outpdu = coap_new_pdu(transport, length);
233 OIC_LOG(ERROR, TAG, "outpdu is null");
237 OIC_LOG_V(DEBUG, TAG, "pdu parse-transport type : %d", transport);
239 int ret = coap_pdu_parse((unsigned char *) data, length, outpdu, transport);
240 OIC_LOG_V(DEBUG, TAG, "pdu parse ret: %d", ret);
243 OIC_LOG(ERROR, TAG, "pdu parse failed");
244 coap_delete_pdu(outpdu);
249 if (CA_ADAPTER_TCP == endpoint->adapter)
251 OIC_LOG(INFO, TAG, "there is no version info in coap header");
256 if (outpdu->hdr->coap_hdr_udp_t.version != COAP_DEFAULT_VERSION)
258 OIC_LOG_V(ERROR, TAG, "coap version is not available : %d",
259 outpdu->hdr->coap_hdr_udp_t.version);
260 coap_delete_pdu(outpdu);
263 if (outpdu->hdr->coap_hdr_udp_t.token_length > CA_MAX_TOKEN_LEN)
265 OIC_LOG_V(ERROR, TAG, "token length has been exceed : %d",
266 outpdu->hdr->coap_hdr_udp_t.token_length);
267 coap_delete_pdu(outpdu);
274 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(outpdu, transport));
277 OIC_LOG(DEBUG, TAG, "OUT");
281 coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
282 const CAEndpoint_t *endpoint, coap_list_t *options)
284 OIC_LOG(DEBUG, TAG, "IN");
285 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
286 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
288 coap_transport_type transport;
289 unsigned int length = COAP_MAX_PDU_SIZE;
291 unsigned int msgLength = 0;
292 if (CA_ADAPTER_TCP == endpoint->adapter)
296 unsigned short prevOptNumber = 0;
297 for (coap_list_t *opt = options; opt; opt = opt->next)
299 unsigned short curOptNumber = COAP_OPTION_KEY(*(coap_option *) opt->data);
300 if (prevOptNumber > curOptNumber)
302 OIC_LOG(ERROR, TAG, "option list is wrong");
306 size_t optValueLen = COAP_OPTION_LENGTH(*(coap_option *) opt->data);
307 size_t optLength = coap_get_opt_header_length(curOptNumber - prevOptNumber, optValueLen);
310 OIC_LOG(ERROR, TAG, "Reserved for the Payload marker for the option");
313 msgLength += optLength;
314 prevOptNumber = curOptNumber;
315 OIC_LOG_V(DEBUG, TAG, "curOptNumber[%d], prevOptNumber[%d], optValueLen[%d], "
316 "optLength[%d], msgLength[%d]",
317 curOptNumber, prevOptNumber, optValueLen, optLength, msgLength);
321 if (info->payloadSize > 0)
323 msgLength = msgLength + info->payloadSize + PAYLOAD_MARKER;
325 transport = coap_get_tcp_header_type_from_size(msgLength);
326 length = msgLength + coap_get_tcp_header_length_for_transport(transport)
332 transport = coap_udp;
335 coap_pdu_t *pdu = coap_new_pdu(transport, length);
339 OIC_LOG(ERROR, TAG, "malloc failed");
343 OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %d",
344 transport, info->payloadSize);
347 if (CA_ADAPTER_TCP == endpoint->adapter)
349 coap_add_length(pdu, transport, msgLength);
354 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
356 if (0 == info->messageId)
358 /* initialize message id */
359 prng((uint8_t * ) &message_id, sizeof(message_id));
361 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
365 /* use saved message id */
366 message_id = info->messageId;
368 pdu->hdr->coap_hdr_udp_t.id = message_id;
369 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->coap_hdr_udp_t.id);
371 pdu->hdr->coap_hdr_udp_t.type = info->type;
374 coap_add_code(pdu, transport, code);
376 if (info->token && CA_EMPTY != code)
378 uint32_t tokenLength = info->tokenLength;
379 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
380 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
382 int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token, transport);
385 OIC_LOG(ERROR, TAG, "can't add token");
391 for (coap_list_t *opt = options; opt; opt = opt->next)
393 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
394 COAP_OPTION_DATA(*(coap_option *) opt->data));
396 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
397 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
398 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
399 COAP_OPTION_DATA(*(coap_option *) opt->data), transport);
403 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
405 bool enabledPayload = false;
407 enabledPayload = true;
410 if (enabledPayload || CA_ADAPTER_GATT_BTLE == endpoint->adapter
412 || CA_ADAPTER_TCP == endpoint->adapter
416 if (NULL != info->payload && 0 < info->payloadSize)
418 OIC_LOG(DEBUG, TAG, "payload is added");
419 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
423 OIC_LOG(DEBUG, TAG, "OUT");
427 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
429 OIC_LOG(DEBUG, TAG, "IN");
433 OIC_LOG(ERROR, TAG, "uriInfo is null");
434 return CA_STATUS_INVALID_PARAM;
437 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
441 OIC_LOG(ERROR, TAG, "optlist is null");
442 return CA_STATUS_INVALID_PARAM;
445 /* split arg into Uri-* options */
447 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
449 if (uri.port != COAP_DEFAULT_PORT)
451 unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 };
452 int ret = coap_insert(optlist,
453 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
454 coap_encode_var_bytes(portbuf, uri.port),
459 return CA_STATUS_INVALID_PARAM;
463 if (uri.path.s && uri.path.length)
465 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
466 COAP_OPTION_URI_PATH, optlist);
467 if (CA_STATUS_OK != ret)
469 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
474 if (uri.query.s && uri.query.length)
476 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
478 if (CA_STATUS_OK != ret)
480 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
485 OIC_LOG(DEBUG, TAG, "OUT");
489 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
490 coap_list_t **optlist)
494 OIC_LOG(ERROR, TAG, "optlist is null");
495 return CA_STATUS_INVALID_PARAM;
498 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
500 // should never occur. Log just in case.
501 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
502 return CA_NOT_SUPPORTED;
504 else if (str && length)
506 unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
507 unsigned char *pBuf = uriBuffer;
508 size_t buflen = sizeof(uriBuffer);
509 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
510 coap_split_query(str, length, pBuf, &buflen);
517 int ret = coap_insert(optlist,
518 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
519 (char *)COAP_OPT_VALUE(pBuf)),
523 return CA_STATUS_INVALID_PARAM;
526 size_t optSize = COAP_OPT_SIZE(pBuf);
527 if ((prevIdx + optSize) < buflen)
536 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
537 return CA_STATUS_FAILED;
542 OIC_LOG(ERROR, TAG, "str or length is not available");
543 return CA_STATUS_FAILED;
549 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
552 OIC_LOG(DEBUG, TAG, "IN");
553 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", CA_STATUS_INVALID_PARAM);
555 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
559 OIC_LOG(ERROR, TAG, "optlist is null");
560 return CA_STATUS_INVALID_PARAM;
563 for (uint32_t i = 0; i < info->numOptions; i++)
565 if(!(info->options + i))
567 OIC_LOG(ERROR, TAG, "options is not available");
568 return CA_STATUS_FAILED;
571 uint32_t id = (info->options + i)->optionID;
572 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
574 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
578 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
579 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
580 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
581 int ret = coap_insert(optlist,
582 CACreateNewOptionNode(id, (info->options + i)->optionLength,
583 (info->options + i)->optionData),
587 return CA_STATUS_INVALID_PARAM;
592 // insert one extra header with the payload format if applicable.
593 if (CA_FORMAT_UNDEFINED != info->payloadFormat)
595 coap_list_t* node = NULL;
596 uint8_t buf[3] = {0};
597 switch (info->payloadFormat) {
598 case CA_FORMAT_APPLICATION_CBOR:
599 node = CACreateNewOptionNode(
600 COAP_OPTION_CONTENT_FORMAT,
601 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
605 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->payloadFormat);
609 OIC_LOG(ERROR, TAG, "format option not created");
610 return CA_STATUS_INVALID_PARAM;
612 int ret = coap_insert(optlist, node, CAOrderOpts);
616 OIC_LOG(ERROR, TAG, "format option not inserted in header");
617 return CA_STATUS_INVALID_PARAM;
620 if (CA_FORMAT_UNDEFINED != info->acceptFormat)
622 coap_list_t* node = NULL;
623 uint8_t buf[3] = {0};
624 switch (info->acceptFormat) {
625 case CA_FORMAT_APPLICATION_CBOR:
626 node = CACreateNewOptionNode(
628 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
632 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->acceptFormat);
636 OIC_LOG(ERROR, TAG, "format option not created");
637 return CA_STATUS_INVALID_PARAM;
639 int ret = coap_insert(optlist, node, CAOrderOpts);
643 OIC_LOG(ERROR, TAG, "format option not inserted in header");
644 return CA_STATUS_INVALID_PARAM;
648 OIC_LOG(DEBUG, TAG, "OUT");
652 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
654 OIC_LOG(DEBUG, TAG, "IN");
658 OIC_LOG(ERROR, TAG, "invalid pointer parameter");
662 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
665 OIC_LOG(ERROR, TAG, "Out of memory");
668 memset(option, 0, sizeof(coap_option) + length + 1);
670 COAP_OPTION_KEY(*option) = key;
672 coap_option_def_t* def = coap_opt_def(key);
673 if (NULL != def && coap_is_var_bytes(def))
675 if (length > def->max)
677 // make sure we shrink the value so it fits the coap option definition
678 // by truncating the value, disregard the leading bytes.
679 OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]",
680 def->key, length, def->max);
681 data = &(data[length-def->max]);
684 // Shrink the encoding length to a minimum size for coap
685 // options that support variable length encoding.
686 COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes(
687 COAP_OPTION_DATA(*option),
688 coap_decode_var_bytes((unsigned char *)data, length));
692 COAP_OPTION_LENGTH(*option) = length;
693 memcpy(COAP_OPTION_DATA(*option), data, length);
696 /* we can pass NULL here as delete function since option is released automatically */
697 coap_list_t *node = coap_new_listnode(option, NULL);
701 OIC_LOG(ERROR, TAG, "node is NULL");
706 OIC_LOG(DEBUG, TAG, "OUT");
710 int CAOrderOpts(void *a, void *b)
712 OIC_LOG(DEBUG, TAG, "IN");
715 return a < b ? -1 : 1;
718 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
723 OIC_LOG(DEBUG, TAG, "OUT");
724 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
727 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
729 OIC_LOG(DEBUG, TAG, "IN");
733 while ((option = coap_option_next(&opt_iter)))
735 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
736 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
737 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type
738 && COAP_OPTION_CONTENT_FORMAT != opt_iter.type
739 && COAP_OPTION_ACCEPT != opt_iter.type)
745 OIC_LOG(DEBUG, TAG, "OUT");
749 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
750 uint32_t *outCode, CAInfo_t *outInfo)
752 OIC_LOG(DEBUG, TAG, "IN");
754 if (!pdu || !outCode || !outInfo)
756 OIC_LOG(ERROR, TAG, "NULL pointer param");
757 return CA_STATUS_INVALID_PARAM;
760 coap_transport_type transport;
762 if (CA_ADAPTER_TCP == endpoint->adapter)
764 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->hdr)[0] >> 4);
769 transport = coap_udp;
772 coap_opt_iterator_t opt_iter;
773 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport);
777 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(pdu, transport));
780 // init HeaderOption list
781 uint32_t count = CAGetOptionCount(opt_iter);
782 memset(outInfo, 0, sizeof(*outInfo));
784 outInfo->numOptions = count;
787 if (CA_ADAPTER_TCP == endpoint->adapter)
790 outInfo->type = CA_MSG_NONCONFIRM;
791 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
797 outInfo->type = pdu->hdr->coap_hdr_udp_t.type;
800 outInfo->messageId = pdu->hdr->coap_hdr_udp_t.id;
801 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
802 outInfo->acceptFormat = CA_FORMAT_UNDEFINED;
807 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
808 if (NULL == outInfo->options)
810 OIC_LOG(ERROR, TAG, "Out of memory");
811 return CA_MEMORY_ALLOC_FAILED;
816 char optionResult[CA_MAX_URI_LENGTH] = {0};
818 uint32_t optionLength = 0;
819 bool isfirstsetflag = false;
820 bool isQueryBeingProcessed = false;
822 while ((option = coap_option_next(&opt_iter)))
824 char buf[COAP_MAX_PDU_SIZE] = {0};
826 CAGetOptionData(opt_iter.type, (uint8_t *)(COAP_OPT_VALUE(option)),
827 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf));
830 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
831 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
833 if (false == isfirstsetflag)
835 isfirstsetflag = true;
836 optionResult[optionLength] = '/';
838 // Make sure there is enough room in the optionResult buffer
839 if ((optionLength + bufLength) < sizeof(optionResult))
841 memcpy(&optionResult[optionLength], buf, bufLength);
842 optionLength += bufLength;
851 if (COAP_OPTION_URI_PATH == opt_iter.type)
853 // Make sure there is enough room in the optionResult buffer
854 if (optionLength < sizeof(optionResult))
856 optionResult[optionLength] = '/';
864 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
866 if (false == isQueryBeingProcessed)
868 // Make sure there is enough room in the optionResult buffer
869 if (optionLength < sizeof(optionResult))
871 optionResult[optionLength] = '?';
873 isQueryBeingProcessed = true;
882 // Make sure there is enough room in the optionResult buffer
883 if (optionLength < sizeof(optionResult))
885 optionResult[optionLength] = ';';
894 // Make sure there is enough room in the optionResult buffer
895 if ((optionLength + bufLength) < sizeof(optionResult))
897 memcpy(&optionResult[optionLength], buf, bufLength);
898 optionLength += bufLength;
906 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
907 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
909 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
911 else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type)
913 if (1 == COAP_OPT_LENGTH(option))
915 outInfo->payloadFormat = CAConvertFormat((uint8_t)buf[0]);
919 outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED;
920 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
921 opt_iter.type, (uint8_t)buf[0]);
924 else if (COAP_OPTION_ACCEPT == opt_iter.type)
926 if (1 == COAP_OPT_LENGTH(option))
928 outInfo->acceptFormat = CAConvertFormat((uint8_t)buf[0]);
932 outInfo->acceptFormat = CA_FORMAT_UNSUPPORTED;
934 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
935 opt_iter.type, (uint8_t)buf[0]);
941 if (bufLength <= sizeof(outInfo->options[0].optionData))
943 outInfo->options[idx].optionID = opt_iter.type;
944 outInfo->options[idx].optionLength = bufLength;
945 outInfo->options[idx].protocolID = CA_COAP_ID;
946 memcpy(outInfo->options[idx].optionData, buf, bufLength);
954 unsigned char* token = NULL;
955 unsigned int token_length = 0;
956 coap_get_token(pdu->hdr, transport, &token, &token_length);
959 if (token_length > 0)
961 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", token_length);
962 outInfo->token = (char *) OICMalloc(token_length);
963 if (NULL == outInfo->token)
965 OIC_LOG(ERROR, TAG, "Out of memory");
966 OICFree(outInfo->options);
967 return CA_MEMORY_ALLOC_FAILED;
969 memcpy(outInfo->token, token, token_length);
972 outInfo->tokenLength = token_length;
977 if (coap_get_data(pdu, &dataSize, &data))
979 OIC_LOG(DEBUG, TAG, "inside pdu->data");
980 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
981 if (NULL == outInfo->payload)
983 OIC_LOG(ERROR, TAG, "Out of memory");
984 OICFree(outInfo->options);
985 OICFree(outInfo->token);
986 return CA_MEMORY_ALLOC_FAILED;
988 memcpy(outInfo->payload, pdu->data, dataSize);
989 outInfo->payloadSize = dataSize;
992 if (optionResult[0] != '\0')
994 OIC_LOG_V(DEBUG, TAG, "URL length:%d", strlen(optionResult));
995 outInfo->resourceUri = OICStrdup(optionResult);
996 if (!outInfo->resourceUri)
998 OIC_LOG(ERROR, TAG, "Out of memory");
999 OICFree(outInfo->options);
1000 OICFree(outInfo->token);
1001 return CA_MEMORY_ALLOC_FAILED;
1005 OIC_LOG(DEBUG, TAG, "OUT");
1006 return CA_STATUS_OK;
1009 OIC_LOG(ERROR, TAG, "buffer too small");
1010 OICFree(outInfo->options);
1011 return CA_STATUS_FAILED;
1014 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo,
1015 const CAEndpoint_t *endpoint)
1017 OIC_LOG(DEBUG, TAG, "IN");
1018 if (NULL == pdu_hdr)
1020 OIC_LOG(ERROR, TAG, "pdu_hdr is null");
1021 return CA_STATUS_INVALID_PARAM;
1024 if (NULL == outInfo)
1026 OIC_LOG(ERROR, TAG, "outInfo is null");
1027 return CA_STATUS_INVALID_PARAM;
1030 coap_transport_type transport;
1032 if (CA_ADAPTER_TCP == endpoint->adapter)
1034 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4);
1039 transport = coap_udp;
1042 unsigned char* token = NULL;
1043 unsigned int token_length = 0;
1044 coap_get_token(pdu_hdr, transport, &token, &token_length);
1047 if (token_length > 0)
1049 OIC_LOG_V(DEBUG, TAG, "token len:%d", token_length);
1050 outInfo->token = (char *) OICMalloc(token_length);
1051 if (NULL == outInfo->token)
1053 OIC_LOG(ERROR, TAG, "Out of memory");
1054 return CA_MEMORY_ALLOC_FAILED;
1056 memcpy(outInfo->token, token, token_length);
1059 outInfo->tokenLength = token_length;
1061 OIC_LOG(DEBUG, TAG, "OUT");
1063 return CA_STATUS_OK;
1066 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
1068 OIC_LOG(DEBUG, TAG, "IN");
1072 OIC_LOG(ERROR, TAG, "invalid token pointer");
1073 return CA_STATUS_INVALID_PARAM;
1076 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
1078 OIC_LOG(ERROR, TAG, "invalid token length");
1079 return CA_STATUS_INVALID_PARAM;
1089 if (SEED == (unsigned int)((time_t)-1))
1091 OIC_LOG(ERROR, TAG, "seed is not made");
1093 return CA_STATUS_FAILED;
1102 // memory allocation
1103 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
1106 OIC_LOG(ERROR, TAG, "Out of memory");
1107 return CA_MEMORY_ALLOC_FAILED;
1111 for (uint8_t index = 0; index < tokenLength; index++)
1113 // use valid characters
1115 temp[index] = rand() & 0x00FF;
1117 temp[index] = random() & 0x00FF;
1124 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
1125 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
1127 OIC_LOG(DEBUG, TAG, "OUT");
1128 return CA_STATUS_OK;
1131 void CADestroyTokenInternal(CAToken_t token)
1133 OIC_LOG(DEBUG, TAG, "IN");
1135 OIC_LOG(DEBUG, TAG, "OUT");
1138 void CADestroyInfo(CAInfo_t *info)
1140 OIC_LOG(DEBUG, TAG, "IN");
1144 OIC_LOG(DEBUG, TAG, "free options");
1145 OICFree(info->options);
1147 OIC_LOG(DEBUG, TAG, "free token");
1148 OICFree(info->token);
1150 OIC_LOG(DEBUG, TAG, "free payload");
1151 OICFree(info->payload);
1154 OIC_LOG(DEBUG, TAG, "OUT");
1157 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
1158 uint8_t *option, uint32_t buflen)
1162 OIC_LOG(ERROR, TAG, "buflen 0");
1168 OIC_LOG(ERROR, TAG, "option buffer too small");
1172 coap_option_def_t* def = coap_opt_def(key);
1173 if(NULL != def && coap_is_var_bytes(def) && 0 == len) {
1174 // A 0 length option is permitted in CoAP but the
1175 // rest or the stack is unaware of variable byte encoding
1176 // should remain that way so a 0 byte of length 1 is inserted.
1180 memcpy(option, data, len);
1187 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
1191 OIC_LOG(ERROR, TAG, "pdu is null");
1192 return CA_MSG_NONCONFIRM;
1195 // pdu minimum size is 4 byte.
1196 if (size < CA_PDU_MIN_SIZE)
1198 OIC_LOG(ERROR, TAG, "min size");
1199 return CA_MSG_NONCONFIRM;
1202 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1204 return (CAMessageType_t) hdr->coap_hdr_udp_t.type;
1207 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
1211 OIC_LOG(ERROR, TAG, "pdu is null");
1215 // pdu minimum size is 4 byte.
1216 if (size < CA_PDU_MIN_SIZE)
1218 OIC_LOG(ERROR, TAG, "min size");
1222 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1224 return hdr->coap_hdr_udp_t.id;
1227 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
1231 OIC_LOG(ERROR, TAG, "pdu is null");
1232 return CA_NOT_FOUND;
1235 // pdu minimum size is 4 byte.
1236 if (size < CA_PDU_MIN_SIZE)
1238 OIC_LOG(ERROR, TAG, "min size");
1239 return CA_NOT_FOUND;
1242 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1244 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->coap_hdr_udp_t.code);
1247 CAPayloadFormat_t CAConvertFormat(uint8_t format)
1251 case COAP_MEDIATYPE_TEXT_PLAIN:
1252 return CA_FORMAT_TEXT_PLAIN;
1253 case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT:
1254 return CA_FORMAT_APPLICATION_LINK_FORMAT;
1255 case COAP_MEDIATYPE_APPLICATION_XML:
1256 return CA_FORMAT_APPLICATION_XML;
1257 case COAP_MEDIATYPE_APPLICATION_OCTET_STREAM:
1258 return CA_FORMAT_APPLICATION_OCTET_STREAM;
1259 case COAP_MEDIATYPE_APPLICATION_RDF_XML:
1260 return CA_FORMAT_APPLICATION_RDF_XML;
1261 case COAP_MEDIATYPE_APPLICATION_EXI:
1262 return CA_FORMAT_APPLICATION_EXI;
1263 case COAP_MEDIATYPE_APPLICATION_JSON:
1264 return CA_FORMAT_APPLICATION_JSON;
1265 case COAP_MEDIATYPE_APPLICATION_CBOR:
1266 return CA_FORMAT_APPLICATION_CBOR;
1268 return CA_FORMAT_UNSUPPORTED;