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 if (NULL == pdu || NULL == outReqInfo)
75 OIC_LOG(ERROR, TAG, "parameter is null");
76 return CA_STATUS_INVALID_PARAM;
79 uint32_t code = CA_NOT_FOUND;
80 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outReqInfo->info));
81 outReqInfo->method = code;
86 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
87 const CAEndpoint_t *endpoint)
89 if (NULL == pdu || NULL == outResInfo)
91 OIC_LOG(ERROR, TAG, "parameter is null");
92 return CA_STATUS_INVALID_PARAM;
95 uint32_t code = CA_NOT_FOUND;
96 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outResInfo->info));
97 outResInfo->result = code;
102 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
103 CAErrorInfo_t *errorInfo)
107 OIC_LOG(ERROR, TAG, "parameter is null");
108 return CA_STATUS_INVALID_PARAM;
112 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &errorInfo->info);
117 coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint,
118 coap_list_t **optlist, coap_transport_type *transport)
120 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
121 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
122 VERIFY_NON_NULL_RET(optlist, TAG, "optlist", NULL);
124 coap_pdu_t *pdu = NULL;
126 // RESET have to use only 4byte (empty message)
127 // and ACKNOWLEDGE can use empty message when code is empty.
128 if (CA_MSG_RESET == info->type || (CA_EMPTY == code && CA_MSG_ACKNOWLEDGE == info->type))
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 (CA_MSG_ACKNOWLEDGE != info->type && info->resourceUri)
141 uint32_t length = strlen(info->resourceUri);
142 if (CA_MAX_URI_LENGTH < length)
144 OIC_LOG(ERROR, TAG, "URI len err");
148 uint32_t uriLength = length + sizeof(COAP_URI_HEADER);
149 char *coapUri = (char *) OICCalloc(1, uriLength);
152 OIC_LOG(ERROR, TAG, "out of memory");
155 OICStrcat(coapUri, uriLength, COAP_URI_HEADER);
156 OICStrcat(coapUri, uriLength, info->resourceUri);
158 // parsing options in URI
159 CAResult_t res = CAParseURI(coapUri, optlist);
160 if (CA_STATUS_OK != res)
168 // parsing options in HeadOption
169 CAResult_t ret = CAParseHeadOption(code, info, optlist);
170 if (CA_STATUS_OK != ret)
175 pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, *optlist, transport);
178 OIC_LOG(ERROR, TAG, "pdu NULL");
183 // pdu print method : coap_show_pdu(pdu);
187 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode,
188 const CAEndpoint_t *endpoint)
192 OIC_LOG(ERROR, TAG, "data is null");
196 coap_transport_type transport;
198 if (CA_ADAPTER_TCP == endpoint->adapter)
200 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)data)[0] >> 4);
207 transport = coap_udp;
210 coap_pdu_t *outpdu = coap_new_pdu(transport, length);
213 OIC_LOG(ERROR, TAG, "outpdu is null");
217 OIC_LOG_V(DEBUG, TAG, "pdu parse-transport type : %d", transport);
219 int ret = coap_pdu_parse((unsigned char *) data, length, outpdu, transport);
220 OIC_LOG_V(DEBUG, TAG, "pdu parse ret: %d", ret);
223 OIC_LOG(ERROR, TAG, "pdu parse failed");
224 coap_delete_pdu(outpdu);
229 if (CA_ADAPTER_TCP == endpoint->adapter)
231 OIC_LOG(INFO, TAG, "there is no version info in coap header");
236 if (outpdu->hdr->coap_hdr_udp_t.version != COAP_DEFAULT_VERSION)
238 OIC_LOG_V(ERROR, TAG, "coap version is not available : %d",
239 outpdu->hdr->coap_hdr_udp_t.version);
240 coap_delete_pdu(outpdu);
243 if (outpdu->hdr->coap_hdr_udp_t.token_length > CA_MAX_TOKEN_LEN)
245 OIC_LOG_V(ERROR, TAG, "token length has been exceed : %d",
246 outpdu->hdr->coap_hdr_udp_t.token_length);
247 coap_delete_pdu(outpdu);
254 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(outpdu, transport));
260 coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
261 const CAEndpoint_t *endpoint, coap_list_t *options,
262 coap_transport_type *transport)
264 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
265 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
266 VERIFY_NON_NULL_RET(transport, TAG, "transport", NULL);
268 unsigned int length = COAP_MAX_PDU_SIZE;
270 unsigned int msgLength = 0;
271 if (CA_ADAPTER_TCP == endpoint->adapter)
275 unsigned short prevOptNumber = 0;
276 for (coap_list_t *opt = options; opt; opt = opt->next)
278 unsigned short curOptNumber = COAP_OPTION_KEY(*(coap_option *) opt->data);
279 if (prevOptNumber > curOptNumber)
281 OIC_LOG(ERROR, TAG, "option list is wrong");
285 size_t optValueLen = COAP_OPTION_LENGTH(*(coap_option *) opt->data);
286 size_t optLength = coap_get_opt_header_length(curOptNumber - prevOptNumber, optValueLen);
289 OIC_LOG(ERROR, TAG, "Reserved for the Payload marker for the option");
292 msgLength += optLength;
293 prevOptNumber = curOptNumber;
294 OIC_LOG_V(DEBUG, TAG, "curOptNumber[%d], prevOptNumber[%d], optValueLen[%zu], "
295 "optLength[%zu], msgLength[%d]",
296 curOptNumber, prevOptNumber, optValueLen, optLength, msgLength);
300 if (info->payloadSize > 0)
302 msgLength = msgLength + info->payloadSize + PAYLOAD_MARKER;
304 *transport = coap_get_tcp_header_type_from_size(msgLength);
305 length = msgLength + coap_get_tcp_header_length_for_transport(*transport)
311 *transport = coap_udp;
314 coap_pdu_t *pdu = coap_new_pdu(*transport, length);
318 OIC_LOG(ERROR, TAG, "malloc failed");
322 OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %zu",
323 *transport, info->payloadSize);
326 if (CA_ADAPTER_TCP == endpoint->adapter)
328 coap_add_length(pdu, *transport, msgLength);
333 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
335 if (0 == info->messageId)
337 /* initialize message id */
338 prng((uint8_t * ) &message_id, sizeof(message_id));
340 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
344 /* use saved message id */
345 message_id = info->messageId;
347 pdu->hdr->coap_hdr_udp_t.id = message_id;
348 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->coap_hdr_udp_t.id);
350 pdu->hdr->coap_hdr_udp_t.type = info->type;
353 coap_add_code(pdu, *transport, code);
355 if (info->token && CA_EMPTY != code)
357 uint32_t tokenLength = info->tokenLength;
358 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
359 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
361 int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token, *transport);
364 OIC_LOG(ERROR, TAG, "can't add token");
369 if (CA_ADAPTER_GATT_BTLE != endpoint->adapter
371 && CA_ADAPTER_TCP != endpoint->adapter
375 // option list will be added in blockwise-transfer
382 for (coap_list_t *opt = options; opt; opt = opt->next)
384 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
385 COAP_OPTION_DATA(*(coap_option *) opt->data));
387 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
388 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
389 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
390 COAP_OPTION_DATA(*(coap_option *) opt->data), *transport);
394 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
396 if (NULL != info->payload && 0 < info->payloadSize)
398 OIC_LOG(DEBUG, TAG, "payload is added");
399 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
405 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
409 OIC_LOG(ERROR, TAG, "uriInfo is null");
410 return CA_STATUS_INVALID_PARAM;
413 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
417 OIC_LOG(ERROR, TAG, "optlist is null");
418 return CA_STATUS_INVALID_PARAM;
421 /* split arg into Uri-* options */
423 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
425 if (uri.port != COAP_DEFAULT_PORT)
427 unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 };
428 int ret = coap_insert(optlist,
429 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
430 coap_encode_var_bytes(portbuf, uri.port),
435 return CA_STATUS_INVALID_PARAM;
439 if (uri.path.s && uri.path.length)
441 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
442 COAP_OPTION_URI_PATH, optlist);
443 if (CA_STATUS_OK != ret)
445 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
450 if (uri.query.s && uri.query.length)
452 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
454 if (CA_STATUS_OK != ret)
456 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
464 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
465 coap_list_t **optlist)
469 OIC_LOG(ERROR, TAG, "optlist is null");
470 return CA_STATUS_INVALID_PARAM;
473 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
475 // should never occur. Log just in case.
476 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
477 return CA_NOT_SUPPORTED;
479 else if (str && length)
481 unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
482 unsigned char *pBuf = uriBuffer;
483 size_t buflen = sizeof(uriBuffer);
484 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
485 coap_split_query(str, length, pBuf, &buflen);
492 int ret = coap_insert(optlist,
493 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
494 (char *)COAP_OPT_VALUE(pBuf)),
498 return CA_STATUS_INVALID_PARAM;
501 size_t optSize = COAP_OPT_SIZE(pBuf);
502 if ((prevIdx + optSize) < buflen)
511 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
512 return CA_STATUS_FAILED;
517 OIC_LOG(ERROR, TAG, "str or length is not available");
518 return CA_STATUS_FAILED;
524 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
527 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", CA_STATUS_INVALID_PARAM);
529 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
533 OIC_LOG(ERROR, TAG, "optlist is null");
534 return CA_STATUS_INVALID_PARAM;
537 for (uint32_t i = 0; i < info->numOptions; i++)
539 if(!(info->options + i))
541 OIC_LOG(ERROR, TAG, "options is not available");
542 return CA_STATUS_FAILED;
545 uint32_t id = (info->options + i)->optionID;
546 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
548 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
552 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
553 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
554 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
555 int ret = coap_insert(optlist,
556 CACreateNewOptionNode(id, (info->options + i)->optionLength,
557 (info->options + i)->optionData),
561 return CA_STATUS_INVALID_PARAM;
566 // insert one extra header with the payload format if applicable.
567 if (CA_FORMAT_UNDEFINED != info->payloadFormat)
569 coap_list_t* node = NULL;
570 uint8_t buf[3] = {0};
571 switch (info->payloadFormat) {
572 case CA_FORMAT_APPLICATION_CBOR:
573 node = CACreateNewOptionNode(
574 COAP_OPTION_CONTENT_FORMAT,
575 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
579 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->payloadFormat);
583 OIC_LOG(ERROR, TAG, "format option not created");
584 return CA_STATUS_INVALID_PARAM;
586 int ret = coap_insert(optlist, node, CAOrderOpts);
590 OIC_LOG(ERROR, TAG, "format option not inserted in header");
591 return CA_STATUS_INVALID_PARAM;
594 if (CA_FORMAT_UNDEFINED != info->acceptFormat)
596 coap_list_t* node = NULL;
597 uint8_t buf[3] = {0};
598 switch (info->acceptFormat) {
599 case CA_FORMAT_APPLICATION_CBOR:
600 node = CACreateNewOptionNode(
602 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
606 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->acceptFormat);
610 OIC_LOG(ERROR, TAG, "format option not created");
611 return CA_STATUS_INVALID_PARAM;
613 int ret = coap_insert(optlist, node, CAOrderOpts);
617 OIC_LOG(ERROR, TAG, "format option not inserted in header");
618 return CA_STATUS_INVALID_PARAM;
625 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
629 OIC_LOG(ERROR, TAG, "invalid pointer parameter");
633 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
636 OIC_LOG(ERROR, TAG, "Out of memory");
639 memset(option, 0, sizeof(coap_option) + length + 1);
641 COAP_OPTION_KEY(*option) = key;
643 coap_option_def_t* def = coap_opt_def(key);
644 if (NULL != def && coap_is_var_bytes(def))
646 if (length > def->max)
648 // make sure we shrink the value so it fits the coap option definition
649 // by truncating the value, disregard the leading bytes.
650 OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]",
651 def->key, length, def->max);
652 data = &(data[length-def->max]);
655 // Shrink the encoding length to a minimum size for coap
656 // options that support variable length encoding.
657 COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes(
658 COAP_OPTION_DATA(*option),
659 coap_decode_var_bytes((unsigned char *)data, length));
663 COAP_OPTION_LENGTH(*option) = length;
664 memcpy(COAP_OPTION_DATA(*option), data, length);
667 /* we can pass NULL here as delete function since option is released automatically */
668 coap_list_t *node = coap_new_listnode(option, NULL);
672 OIC_LOG(ERROR, TAG, "node is NULL");
680 int CAOrderOpts(void *a, void *b)
684 return a < b ? -1 : 1;
687 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
692 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
695 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
700 while ((option = coap_option_next(&opt_iter)))
702 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
703 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
704 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type
705 && COAP_OPTION_CONTENT_FORMAT != opt_iter.type
706 && COAP_OPTION_ACCEPT != opt_iter.type)
715 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
716 uint32_t *outCode, CAInfo_t *outInfo)
718 if (!pdu || !outCode || !outInfo)
720 OIC_LOG(ERROR, TAG, "NULL pointer param");
721 return CA_STATUS_INVALID_PARAM;
724 coap_transport_type transport;
726 if (CA_ADAPTER_TCP == endpoint->adapter)
728 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->hdr)[0] >> 4);
735 transport = coap_udp;
738 coap_opt_iterator_t opt_iter;
739 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport);
743 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(pdu, transport));
746 // init HeaderOption list
747 uint32_t count = CAGetOptionCount(opt_iter);
748 memset(outInfo, 0, sizeof(*outInfo));
750 outInfo->numOptions = count;
753 if (CA_ADAPTER_TCP == endpoint->adapter)
756 outInfo->type = CA_MSG_NONCONFIRM;
757 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
763 outInfo->type = pdu->hdr->coap_hdr_udp_t.type;
766 outInfo->messageId = pdu->hdr->coap_hdr_udp_t.id;
767 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
768 outInfo->acceptFormat = CA_FORMAT_UNDEFINED;
773 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
774 if (NULL == outInfo->options)
776 OIC_LOG(ERROR, TAG, "Out of memory");
777 return CA_MEMORY_ALLOC_FAILED;
782 char optionResult[CA_MAX_URI_LENGTH] = {0};
784 uint32_t optionLength = 0;
785 bool isfirstsetflag = false;
786 bool isQueryBeingProcessed = false;
788 while ((option = coap_option_next(&opt_iter)))
790 char buf[COAP_MAX_PDU_SIZE] = {0};
792 CAGetOptionData(opt_iter.type, (uint8_t *)(COAP_OPT_VALUE(option)),
793 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf));
796 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
797 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
799 if (false == isfirstsetflag)
801 isfirstsetflag = true;
802 optionResult[optionLength] = '/';
804 // Make sure there is enough room in the optionResult buffer
805 if ((optionLength + bufLength) < sizeof(optionResult))
807 memcpy(&optionResult[optionLength], buf, bufLength);
808 optionLength += bufLength;
817 if (COAP_OPTION_URI_PATH == opt_iter.type)
819 // Make sure there is enough room in the optionResult buffer
820 if (optionLength < sizeof(optionResult))
822 optionResult[optionLength] = '/';
830 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
832 if (false == isQueryBeingProcessed)
834 // Make sure there is enough room in the optionResult buffer
835 if (optionLength < sizeof(optionResult))
837 optionResult[optionLength] = '?';
839 isQueryBeingProcessed = true;
848 // Make sure there is enough room in the optionResult buffer
849 if (optionLength < sizeof(optionResult))
851 optionResult[optionLength] = ';';
860 // Make sure there is enough room in the optionResult buffer
861 if ((optionLength + bufLength) < sizeof(optionResult))
863 memcpy(&optionResult[optionLength], buf, bufLength);
864 optionLength += bufLength;
872 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
873 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
875 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
877 else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type)
879 if (1 == COAP_OPT_LENGTH(option))
881 outInfo->payloadFormat = CAConvertFormat((uint8_t)buf[0]);
885 outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED;
886 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
887 opt_iter.type, (uint8_t)buf[0]);
890 else if (COAP_OPTION_ACCEPT == opt_iter.type)
892 if (1 == COAP_OPT_LENGTH(option))
894 outInfo->acceptFormat = CAConvertFormat((uint8_t)buf[0]);
898 outInfo->acceptFormat = CA_FORMAT_UNSUPPORTED;
900 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
901 opt_iter.type, (uint8_t)buf[0]);
907 if (bufLength <= sizeof(outInfo->options[0].optionData))
909 outInfo->options[idx].optionID = opt_iter.type;
910 outInfo->options[idx].optionLength = bufLength;
911 outInfo->options[idx].protocolID = CA_COAP_ID;
912 memcpy(outInfo->options[idx].optionData, buf, bufLength);
920 unsigned char* token = NULL;
921 unsigned int token_length = 0;
922 coap_get_token(pdu->hdr, transport, &token, &token_length);
925 if (token_length > 0)
927 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", token_length);
928 outInfo->token = (char *) OICMalloc(token_length);
929 if (NULL == outInfo->token)
931 OIC_LOG(ERROR, TAG, "Out of memory");
932 OICFree(outInfo->options);
933 return CA_MEMORY_ALLOC_FAILED;
935 memcpy(outInfo->token, token, token_length);
938 outInfo->tokenLength = token_length;
943 if (coap_get_data(pdu, &dataSize, &data))
945 OIC_LOG(DEBUG, TAG, "inside pdu->data");
946 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
947 if (NULL == outInfo->payload)
949 OIC_LOG(ERROR, TAG, "Out of memory");
950 OICFree(outInfo->options);
951 OICFree(outInfo->token);
952 return CA_MEMORY_ALLOC_FAILED;
954 memcpy(outInfo->payload, pdu->data, dataSize);
955 outInfo->payloadSize = dataSize;
958 if (optionResult[0] != '\0')
960 OIC_LOG_V(DEBUG, TAG, "URL length:%zu", strlen(optionResult));
961 outInfo->resourceUri = OICStrdup(optionResult);
962 if (!outInfo->resourceUri)
964 OIC_LOG(ERROR, TAG, "Out of memory");
965 OICFree(outInfo->options);
966 OICFree(outInfo->token);
967 return CA_MEMORY_ALLOC_FAILED;
974 OIC_LOG(ERROR, TAG, "buffer too small");
975 OICFree(outInfo->options);
976 return CA_STATUS_FAILED;
979 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo,
980 const CAEndpoint_t *endpoint)
984 OIC_LOG(ERROR, TAG, "pdu_hdr is null");
985 return CA_STATUS_INVALID_PARAM;
990 OIC_LOG(ERROR, TAG, "outInfo is null");
991 return CA_STATUS_INVALID_PARAM;
994 coap_transport_type transport;
996 if (CA_ADAPTER_TCP == endpoint->adapter)
998 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4);
1005 transport = coap_udp;
1008 unsigned char* token = NULL;
1009 unsigned int token_length = 0;
1010 coap_get_token(pdu_hdr, transport, &token, &token_length);
1013 if (token_length > 0)
1015 OIC_LOG_V(DEBUG, TAG, "token len:%d", token_length);
1016 outInfo->token = (char *) OICMalloc(token_length);
1017 if (NULL == outInfo->token)
1019 OIC_LOG(ERROR, TAG, "Out of memory");
1020 return CA_MEMORY_ALLOC_FAILED;
1022 memcpy(outInfo->token, token, token_length);
1025 outInfo->tokenLength = token_length;
1027 return CA_STATUS_OK;
1030 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
1034 OIC_LOG(ERROR, TAG, "invalid token pointer");
1035 return CA_STATUS_INVALID_PARAM;
1038 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
1040 OIC_LOG(ERROR, TAG, "invalid token length");
1041 return CA_STATUS_INVALID_PARAM;
1051 if (SEED == (unsigned int)((time_t)-1))
1053 OIC_LOG(ERROR, TAG, "seed is not made");
1055 return CA_STATUS_FAILED;
1064 // memory allocation
1065 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
1068 OIC_LOG(ERROR, TAG, "Out of memory");
1069 return CA_MEMORY_ALLOC_FAILED;
1073 for (uint8_t index = 0; index < tokenLength; index++)
1075 // use valid characters
1077 temp[index] = rand() & 0x00FF;
1079 temp[index] = random() & 0x00FF;
1086 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
1087 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
1089 return CA_STATUS_OK;
1092 void CADestroyTokenInternal(CAToken_t token)
1097 void CADestroyInfo(CAInfo_t *info)
1101 OIC_LOG(DEBUG, TAG, "free options");
1102 OICFree(info->options);
1104 OIC_LOG(DEBUG, TAG, "free token");
1105 OICFree(info->token);
1107 OIC_LOG(DEBUG, TAG, "free payload");
1108 OICFree(info->payload);
1112 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
1113 uint8_t *option, uint32_t buflen)
1117 OIC_LOG(ERROR, TAG, "buflen 0");
1123 OIC_LOG(ERROR, TAG, "option buffer too small");
1127 coap_option_def_t* def = coap_opt_def(key);
1128 if(NULL != def && coap_is_var_bytes(def) && 0 == len) {
1129 // A 0 length option is permitted in CoAP but the
1130 // rest or the stack is unaware of variable byte encoding
1131 // should remain that way so a 0 byte of length 1 is inserted.
1135 memcpy(option, data, len);
1142 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
1146 OIC_LOG(ERROR, TAG, "pdu is null");
1147 return CA_MSG_NONCONFIRM;
1150 // pdu minimum size is 4 byte.
1151 if (size < CA_PDU_MIN_SIZE)
1153 OIC_LOG(ERROR, TAG, "min size");
1154 return CA_MSG_NONCONFIRM;
1157 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1159 return (CAMessageType_t) hdr->coap_hdr_udp_t.type;
1162 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
1166 OIC_LOG(ERROR, TAG, "pdu is null");
1170 // pdu minimum size is 4 byte.
1171 if (size < CA_PDU_MIN_SIZE)
1173 OIC_LOG(ERROR, TAG, "min size");
1177 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1179 return hdr->coap_hdr_udp_t.id;
1182 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
1186 OIC_LOG(ERROR, TAG, "pdu is null");
1187 return CA_NOT_FOUND;
1190 // pdu minimum size is 4 byte.
1191 if (size < CA_PDU_MIN_SIZE)
1193 OIC_LOG(ERROR, TAG, "min size");
1194 return CA_NOT_FOUND;
1197 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1199 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->coap_hdr_udp_t.code);
1202 CAPayloadFormat_t CAConvertFormat(uint8_t format)
1206 case COAP_MEDIATYPE_TEXT_PLAIN:
1207 return CA_FORMAT_TEXT_PLAIN;
1208 case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT:
1209 return CA_FORMAT_APPLICATION_LINK_FORMAT;
1210 case COAP_MEDIATYPE_APPLICATION_XML:
1211 return CA_FORMAT_APPLICATION_XML;
1212 case COAP_MEDIATYPE_APPLICATION_OCTET_STREAM:
1213 return CA_FORMAT_APPLICATION_OCTET_STREAM;
1214 case COAP_MEDIATYPE_APPLICATION_RDF_XML:
1215 return CA_FORMAT_APPLICATION_RDF_XML;
1216 case COAP_MEDIATYPE_APPLICATION_EXI:
1217 return CA_FORMAT_APPLICATION_EXI;
1218 case COAP_MEDIATYPE_APPLICATION_JSON:
1219 return CA_FORMAT_APPLICATION_JSON;
1220 case COAP_MEDIATYPE_APPLICATION_CBOR:
1221 return CA_FORMAT_APPLICATION_CBOR;
1223 return CA_FORMAT_UNSUPPORTED;