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 msgLength = 0;
299 if (CA_ADAPTER_TCP == endpoint->adapter)
303 unsigned short prevOptNumber = 0;
304 for (coap_list_t *opt = options; opt; opt = opt->next)
306 unsigned short curOptNumber = COAP_OPTION_KEY(*(coap_option *) opt->data);
307 if (prevOptNumber > curOptNumber)
309 OIC_LOG(ERROR, TAG, "option list is wrong");
313 size_t optValueLen = COAP_OPTION_LENGTH(*(coap_option *) opt->data);
314 size_t optLength = coap_get_opt_header_length(curOptNumber - prevOptNumber, optValueLen);
317 OIC_LOG(ERROR, TAG, "Reserved for the Payload marker for the option");
320 msgLength += optLength;
321 prevOptNumber = curOptNumber;
322 OIC_LOG_V(DEBUG, TAG, "curOptNumber[%d], prevOptNumber[%d], optValueLen[%d], "
323 "optLength[%d], msgLength[%d]",
324 curOptNumber, prevOptNumber, optValueLen, optLength, msgLength);
328 if (info->payloadSize > 0)
330 msgLength = msgLength + info->payloadSize + PAYLOAD_MARKER;
332 transport = coap_get_tcp_header_type_from_size(msgLength);
333 length = msgLength + coap_get_tcp_header_length_for_transport(transport)
339 transport = coap_udp;
342 coap_pdu_t *pdu = coap_new_pdu(transport, length);
346 OIC_LOG(ERROR, TAG, "malloc failed");
350 OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %d",
351 transport, info->payloadSize);
354 if (CA_ADAPTER_TCP == endpoint->adapter)
356 coap_add_length(pdu, transport, msgLength);
361 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
363 if (0 == info->messageId)
365 /* initialize message id */
366 prng((uint8_t * ) &message_id, sizeof(message_id));
368 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
372 /* use saved message id */
373 message_id = info->messageId;
375 pdu->hdr->coap_hdr_udp_t.id = message_id;
376 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->coap_hdr_udp_t.id);
378 pdu->hdr->coap_hdr_udp_t.type = info->type;
381 coap_add_code(pdu, transport, code);
383 if (info->token && CA_EMPTY != code)
385 uint32_t tokenLength = info->tokenLength;
386 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
387 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
389 int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token, transport);
392 OIC_LOG(ERROR, TAG, "can't add token");
398 for (coap_list_t *opt = options; opt; opt = opt->next)
400 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
401 COAP_OPTION_DATA(*(coap_option *) opt->data));
403 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
404 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
405 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
406 COAP_OPTION_DATA(*(coap_option *) opt->data), transport);
410 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
412 bool enabledPayload = false;
414 enabledPayload = true;
417 if (enabledPayload || CA_ADAPTER_GATT_BTLE == endpoint->adapter
419 || CA_ADAPTER_TCP == endpoint->adapter
423 if (NULL != info->payload && 0 < info->payloadSize)
425 OIC_LOG(DEBUG, TAG, "payload is added");
426 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
430 OIC_LOG(DEBUG, TAG, "OUT");
434 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
436 OIC_LOG(DEBUG, TAG, "IN");
440 OIC_LOG(ERROR, TAG, "uriInfo is null");
441 return CA_STATUS_INVALID_PARAM;
444 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
448 OIC_LOG(ERROR, TAG, "optlist is null");
449 return CA_STATUS_INVALID_PARAM;
452 /* split arg into Uri-* options */
454 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
456 if (uri.port != COAP_DEFAULT_PORT)
458 unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 };
459 int ret = coap_insert(optlist,
460 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
461 coap_encode_var_bytes(portbuf, uri.port),
466 return CA_STATUS_INVALID_PARAM;
470 if (uri.path.s && uri.path.length)
472 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
473 COAP_OPTION_URI_PATH, optlist);
474 if (CA_STATUS_OK != ret)
476 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
481 if (uri.query.s && uri.query.length)
483 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
485 if (CA_STATUS_OK != ret)
487 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
492 OIC_LOG(DEBUG, TAG, "OUT");
496 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
497 coap_list_t **optlist)
501 OIC_LOG(ERROR, TAG, "optlist is null");
502 return CA_STATUS_INVALID_PARAM;
505 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
507 // should never occur. Log just in case.
508 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
509 return CA_NOT_SUPPORTED;
511 else if (str && length)
513 unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
514 unsigned char *pBuf = uriBuffer;
515 size_t buflen = sizeof(uriBuffer);
516 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
517 coap_split_query(str, length, pBuf, &buflen);
524 int ret = coap_insert(optlist,
525 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
526 (char *)COAP_OPT_VALUE(pBuf)),
530 return CA_STATUS_INVALID_PARAM;
533 size_t optSize = COAP_OPT_SIZE(pBuf);
534 if ((prevIdx + optSize) < buflen)
543 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
544 return CA_STATUS_FAILED;
549 OIC_LOG(ERROR, TAG, "str or length is not available");
550 return CA_STATUS_FAILED;
556 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
559 OIC_LOG(DEBUG, TAG, "IN");
560 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", CA_STATUS_INVALID_PARAM);
562 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
566 OIC_LOG(ERROR, TAG, "optlist is null");
567 return CA_STATUS_INVALID_PARAM;
570 for (uint32_t i = 0; i < info->numOptions; i++)
572 if(!(info->options + i))
574 OIC_LOG(ERROR, TAG, "options is not available");
575 return CA_STATUS_FAILED;
578 uint32_t id = (info->options + i)->optionID;
579 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
581 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
585 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
586 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
587 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
588 int ret = coap_insert(optlist,
589 CACreateNewOptionNode(id, (info->options + i)->optionLength,
590 (info->options + i)->optionData),
594 return CA_STATUS_INVALID_PARAM;
599 // insert one extra header with the payload format if applicable.
600 if (CA_FORMAT_UNDEFINED != info->payloadFormat)
602 coap_list_t* node = NULL;
603 uint8_t buf[3] = {0};
604 switch (info->payloadFormat) {
605 case CA_FORMAT_APPLICATION_CBOR:
606 node = CACreateNewOptionNode(
607 COAP_OPTION_CONTENT_FORMAT,
608 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
612 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->payloadFormat);
616 OIC_LOG(ERROR, TAG, "format option not created");
617 return CA_STATUS_INVALID_PARAM;
619 int ret = coap_insert(optlist, node, CAOrderOpts);
623 OIC_LOG(ERROR, TAG, "format option not inserted in header");
624 return CA_STATUS_INVALID_PARAM;
627 if (CA_FORMAT_UNDEFINED != info->acceptFormat)
629 coap_list_t* node = NULL;
630 uint8_t buf[3] = {0};
631 switch (info->acceptFormat) {
632 case CA_FORMAT_APPLICATION_CBOR:
633 node = CACreateNewOptionNode(
635 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
639 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->acceptFormat);
643 OIC_LOG(ERROR, TAG, "format option not created");
644 return CA_STATUS_INVALID_PARAM;
646 int ret = coap_insert(optlist, node, CAOrderOpts);
650 OIC_LOG(ERROR, TAG, "format option not inserted in header");
651 return CA_STATUS_INVALID_PARAM;
655 OIC_LOG(DEBUG, TAG, "OUT");
659 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
661 OIC_LOG(DEBUG, TAG, "IN");
665 OIC_LOG(ERROR, TAG, "invalid pointer parameter");
669 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
672 OIC_LOG(ERROR, TAG, "Out of memory");
675 memset(option, 0, sizeof(coap_option) + length + 1);
677 COAP_OPTION_KEY(*option) = key;
679 coap_option_def_t* def = coap_opt_def(key);
680 if (NULL != def && coap_is_var_bytes(def))
682 if (length > def->max)
684 // make sure we shrink the value so it fits the coap option definition
685 // by truncating the value, disregard the leading bytes.
686 OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]",
687 def->key, length, def->max);
688 data = &(data[length-def->max]);
691 // Shrink the encoding length to a minimum size for coap
692 // options that support variable length encoding.
693 COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes(
694 COAP_OPTION_DATA(*option),
695 coap_decode_var_bytes((unsigned char *)data, length));
699 COAP_OPTION_LENGTH(*option) = length;
700 memcpy(COAP_OPTION_DATA(*option), data, length);
703 /* we can pass NULL here as delete function since option is released automatically */
704 coap_list_t *node = coap_new_listnode(option, NULL);
708 OIC_LOG(ERROR, TAG, "node is NULL");
713 OIC_LOG(DEBUG, TAG, "OUT");
717 int CAOrderOpts(void *a, void *b)
719 OIC_LOG(DEBUG, TAG, "IN");
722 return a < b ? -1 : 1;
725 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
730 OIC_LOG(DEBUG, TAG, "OUT");
731 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
734 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
736 OIC_LOG(DEBUG, TAG, "IN");
740 while ((option = coap_option_next(&opt_iter)))
742 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
743 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
744 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type
745 && COAP_OPTION_CONTENT_FORMAT != opt_iter.type
746 && COAP_OPTION_ACCEPT != opt_iter.type)
752 OIC_LOG(DEBUG, TAG, "OUT");
756 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
757 uint32_t *outCode, CAInfo_t *outInfo)
759 OIC_LOG(DEBUG, TAG, "IN");
761 if (!pdu || !outCode || !outInfo)
763 OIC_LOG(ERROR, TAG, "NULL pointer param");
764 return CA_STATUS_INVALID_PARAM;
767 coap_transport_type transport;
769 if (CA_ADAPTER_TCP == endpoint->adapter)
771 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->hdr)[0] >> 4);
776 transport = coap_udp;
779 coap_opt_iterator_t opt_iter;
780 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport);
784 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(pdu, transport));
787 // init HeaderOption list
788 uint32_t count = CAGetOptionCount(opt_iter);
789 memset(outInfo, 0, sizeof(*outInfo));
791 outInfo->numOptions = count;
794 if (CA_ADAPTER_TCP == endpoint->adapter)
797 outInfo->type = CA_MSG_NONCONFIRM;
798 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
804 outInfo->type = pdu->hdr->coap_hdr_udp_t.type;
807 outInfo->messageId = pdu->hdr->coap_hdr_udp_t.id;
808 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
809 outInfo->acceptFormat = CA_FORMAT_UNDEFINED;
814 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
815 if (NULL == outInfo->options)
817 OIC_LOG(ERROR, TAG, "Out of memory");
818 return CA_MEMORY_ALLOC_FAILED;
823 char optionResult[CA_MAX_URI_LENGTH] = {0};
825 uint32_t optionLength = 0;
826 bool isfirstsetflag = false;
827 bool isQueryBeingProcessed = false;
829 while ((option = coap_option_next(&opt_iter)))
831 char buf[COAP_MAX_PDU_SIZE] = {0};
833 CAGetOptionData(opt_iter.type, (uint8_t *)(COAP_OPT_VALUE(option)),
834 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf));
837 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
838 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
840 if (false == isfirstsetflag)
842 isfirstsetflag = true;
843 optionResult[optionLength] = '/';
845 // Make sure there is enough room in the optionResult buffer
846 if ((optionLength + bufLength) < sizeof(optionResult))
848 memcpy(&optionResult[optionLength], buf, bufLength);
849 optionLength += bufLength;
858 if (COAP_OPTION_URI_PATH == opt_iter.type)
860 // Make sure there is enough room in the optionResult buffer
861 if (optionLength < sizeof(optionResult))
863 optionResult[optionLength] = '/';
871 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
873 if (false == isQueryBeingProcessed)
875 // Make sure there is enough room in the optionResult buffer
876 if (optionLength < sizeof(optionResult))
878 optionResult[optionLength] = '?';
880 isQueryBeingProcessed = true;
889 // Make sure there is enough room in the optionResult buffer
890 if (optionLength < sizeof(optionResult))
892 optionResult[optionLength] = ';';
901 // Make sure there is enough room in the optionResult buffer
902 if ((optionLength + bufLength) < sizeof(optionResult))
904 memcpy(&optionResult[optionLength], buf, bufLength);
905 optionLength += bufLength;
913 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
914 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
916 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
918 else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type)
920 if (1 == COAP_OPT_LENGTH(option))
922 outInfo->payloadFormat = CAConvertFormat((uint8_t)buf[0]);
926 outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED;
927 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
928 opt_iter.type, (uint8_t)buf[0]);
931 else if (COAP_OPTION_ACCEPT == opt_iter.type)
933 if (1 == COAP_OPT_LENGTH(option))
935 outInfo->acceptFormat = CAConvertFormat((uint8_t)buf[0]);
939 outInfo->acceptFormat = CA_FORMAT_UNSUPPORTED;
941 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
942 opt_iter.type, (uint8_t)buf[0]);
948 if (bufLength <= sizeof(outInfo->options[0].optionData))
950 outInfo->options[idx].optionID = opt_iter.type;
951 outInfo->options[idx].optionLength = bufLength;
952 outInfo->options[idx].protocolID = CA_COAP_ID;
953 memcpy(outInfo->options[idx].optionData, buf, bufLength);
961 unsigned char* token = NULL;
962 unsigned int token_length = 0;
963 coap_get_token(pdu->hdr, transport, &token, &token_length);
966 if (token_length > 0)
968 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", token_length);
969 outInfo->token = (char *) OICMalloc(token_length);
970 if (NULL == outInfo->token)
972 OIC_LOG(ERROR, TAG, "Out of memory");
973 OICFree(outInfo->options);
974 return CA_MEMORY_ALLOC_FAILED;
976 memcpy(outInfo->token, token, token_length);
979 outInfo->tokenLength = token_length;
984 if (coap_get_data(pdu, &dataSize, &data))
986 OIC_LOG(DEBUG, TAG, "inside pdu->data");
987 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
988 if (NULL == outInfo->payload)
990 OIC_LOG(ERROR, TAG, "Out of memory");
991 OICFree(outInfo->options);
992 OICFree(outInfo->token);
993 return CA_MEMORY_ALLOC_FAILED;
995 memcpy(outInfo->payload, pdu->data, dataSize);
996 outInfo->payloadSize = dataSize;
999 if (optionResult[0] != '\0')
1001 OIC_LOG_V(DEBUG, TAG, "URL length:%d", strlen(optionResult));
1002 outInfo->resourceUri = OICStrdup(optionResult);
1003 if (!outInfo->resourceUri)
1005 OIC_LOG(ERROR, TAG, "Out of memory");
1006 OICFree(outInfo->options);
1007 OICFree(outInfo->token);
1008 return CA_MEMORY_ALLOC_FAILED;
1012 OIC_LOG(DEBUG, TAG, "OUT");
1013 return CA_STATUS_OK;
1016 OIC_LOG(ERROR, TAG, "buffer too small");
1017 OICFree(outInfo->options);
1018 return CA_STATUS_FAILED;
1021 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo,
1022 const CAEndpoint_t *endpoint)
1024 OIC_LOG(DEBUG, TAG, "IN");
1025 if (NULL == pdu_hdr)
1027 OIC_LOG(ERROR, TAG, "pdu_hdr is null");
1028 return CA_STATUS_INVALID_PARAM;
1031 if (NULL == outInfo)
1033 OIC_LOG(ERROR, TAG, "outInfo is null");
1034 return CA_STATUS_INVALID_PARAM;
1037 coap_transport_type transport;
1039 if (CA_ADAPTER_TCP == endpoint->adapter)
1041 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4);
1046 transport = coap_udp;
1049 unsigned char* token = NULL;
1050 unsigned int token_length = 0;
1051 coap_get_token(pdu_hdr, transport, &token, &token_length);
1054 if (token_length > 0)
1056 OIC_LOG_V(DEBUG, TAG, "token len:%d", token_length);
1057 outInfo->token = (char *) OICMalloc(token_length);
1058 if (NULL == outInfo->token)
1060 OIC_LOG(ERROR, TAG, "Out of memory");
1061 return CA_MEMORY_ALLOC_FAILED;
1063 memcpy(outInfo->token, token, token_length);
1066 outInfo->tokenLength = token_length;
1068 OIC_LOG(DEBUG, TAG, "OUT");
1070 return CA_STATUS_OK;
1073 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
1075 OIC_LOG(DEBUG, TAG, "IN");
1079 OIC_LOG(ERROR, TAG, "invalid token pointer");
1080 return CA_STATUS_INVALID_PARAM;
1083 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
1085 OIC_LOG(ERROR, TAG, "invalid token length");
1086 return CA_STATUS_INVALID_PARAM;
1096 if (SEED == (unsigned int)((time_t)-1))
1098 OIC_LOG(ERROR, TAG, "seed is not made");
1100 return CA_STATUS_FAILED;
1109 // memory allocation
1110 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
1113 OIC_LOG(ERROR, TAG, "Out of memory");
1114 return CA_MEMORY_ALLOC_FAILED;
1118 for (uint8_t index = 0; index < tokenLength; index++)
1120 // use valid characters
1122 temp[index] = rand() & 0x00FF;
1124 temp[index] = random() & 0x00FF;
1131 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
1132 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
1134 OIC_LOG(DEBUG, TAG, "OUT");
1135 return CA_STATUS_OK;
1138 void CADestroyTokenInternal(CAToken_t token)
1140 OIC_LOG(DEBUG, TAG, "IN");
1142 OIC_LOG(DEBUG, TAG, "OUT");
1145 void CADestroyInfo(CAInfo_t *info)
1147 OIC_LOG(DEBUG, TAG, "IN");
1151 OIC_LOG(DEBUG, TAG, "free options");
1152 OICFree(info->options);
1154 OIC_LOG(DEBUG, TAG, "free token");
1155 OICFree(info->token);
1157 OIC_LOG(DEBUG, TAG, "free payload");
1158 OICFree(info->payload);
1161 OIC_LOG(DEBUG, TAG, "OUT");
1164 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
1165 uint8_t *option, uint32_t buflen)
1169 OIC_LOG(ERROR, TAG, "buflen 0");
1175 OIC_LOG(ERROR, TAG, "option buffer too small");
1179 coap_option_def_t* def = coap_opt_def(key);
1180 if(NULL != def && coap_is_var_bytes(def) && 0 == len) {
1181 // A 0 length option is permitted in CoAP but the
1182 // rest or the stack is unaware of variable byte encoding
1183 // should remain that way so a 0 byte of length 1 is inserted.
1187 memcpy(option, data, len);
1194 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
1198 OIC_LOG(ERROR, TAG, "pdu is null");
1199 return CA_MSG_NONCONFIRM;
1202 // pdu minimum size is 4 byte.
1203 if (size < CA_PDU_MIN_SIZE)
1205 OIC_LOG(ERROR, TAG, "min size");
1206 return CA_MSG_NONCONFIRM;
1209 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1211 return (CAMessageType_t) hdr->coap_hdr_udp_t.type;
1214 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
1218 OIC_LOG(ERROR, TAG, "pdu is null");
1222 // pdu minimum size is 4 byte.
1223 if (size < CA_PDU_MIN_SIZE)
1225 OIC_LOG(ERROR, TAG, "min size");
1229 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1231 return hdr->coap_hdr_udp_t.id;
1234 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
1238 OIC_LOG(ERROR, TAG, "pdu is null");
1239 return CA_NOT_FOUND;
1242 // pdu minimum size is 4 byte.
1243 if (size < CA_PDU_MIN_SIZE)
1245 OIC_LOG(ERROR, TAG, "min size");
1246 return CA_NOT_FOUND;
1249 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1251 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->coap_hdr_udp_t.code);
1254 CAPayloadFormat_t CAConvertFormat(uint8_t format)
1258 case COAP_MEDIATYPE_TEXT_PLAIN:
1259 return CA_FORMAT_TEXT_PLAIN;
1260 case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT:
1261 return CA_FORMAT_APPLICATION_LINK_FORMAT;
1262 case COAP_MEDIATYPE_APPLICATION_XML:
1263 return CA_FORMAT_APPLICATION_XML;
1264 case COAP_MEDIATYPE_APPLICATION_OCTET_STREAM:
1265 return CA_FORMAT_APPLICATION_OCTET_STREAM;
1266 case COAP_MEDIATYPE_APPLICATION_RDF_XML:
1267 return CA_FORMAT_APPLICATION_RDF_XML;
1268 case COAP_MEDIATYPE_APPLICATION_EXI:
1269 return CA_FORMAT_APPLICATION_EXI;
1270 case COAP_MEDIATYPE_APPLICATION_JSON:
1271 return CA_FORMAT_APPLICATION_JSON;
1272 case COAP_MEDIATYPE_APPLICATION_CBOR:
1273 return CA_FORMAT_APPLICATION_CBOR;
1275 return CA_FORMAT_UNSUPPORTED;