1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 // Defining _BSD_SOURCE or _DEFAULT_SOURCE causes header files to expose
22 // definitions that may otherwise be skipped. Skipping can cause implicit
23 // declaration warnings and/or bugs and subtle problems in code execution.
24 // For glibc information on feature test macros,
25 // Refer http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
27 // This file requires #define use due to random() and srandom()
28 // For details on compatibility and glibc support,
29 // Refer http://www.gnu.org/software/libc/manual/html_node/BSD-Random.html
30 #define _DEFAULT_SOURCE
40 #include "caprotocolmessage.h"
42 #include "oic_malloc.h"
43 #include "oic_string.h"
45 // ARM GCC compiler doesnt define srandom function.
46 #if defined(ARDUINO) && !defined(ARDUINO_ARCH_SAM)
47 #define HAVE_SRANDOM 1
50 #define TAG "CA_PRTCL_MSG"
53 * @def VERIFY_NON_NULL_RET
54 * @brief Macro to verify the validity of input argument
56 #define VERIFY_NON_NULL_RET(arg, log_tag, log_message,ret) \
58 OIC_LOG_V(ERROR, log_tag, "Invalid input:%s", log_message); \
62 #define CA_BUFSIZE (128)
63 #define CA_PDU_MIN_SIZE (4)
64 #define CA_PORT_BUFFER_SIZE (4)
66 static const char COAP_URI_HEADER[] = "coap://[::]/";
68 static unsigned int SEED = 0;
70 CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
71 CARequestInfo_t *outReqInfo)
73 OIC_LOG(DEBUG, TAG, "IN");
75 if (NULL == pdu || NULL == outReqInfo)
77 OIC_LOG(ERROR, TAG, "parameter is null");
78 return CA_STATUS_INVALID_PARAM;
81 uint32_t code = CA_NOT_FOUND;
82 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outReqInfo->info));
83 outReqInfo->method = code;
85 OIC_LOG(DEBUG, TAG, "OUT");
89 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
90 const CAEndpoint_t *endpoint)
92 OIC_LOG(DEBUG, TAG, "IN");
94 if (NULL == pdu || NULL == outResInfo)
96 OIC_LOG(ERROR, TAG, "parameter is null");
97 return CA_STATUS_INVALID_PARAM;
100 uint32_t code = CA_NOT_FOUND;
101 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outResInfo->info));
102 outResInfo->result = code;
104 OIC_LOG(DEBUG, TAG, "OUT");
108 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
109 CAErrorInfo_t *errorInfo)
111 OIC_LOG(DEBUG, TAG, "IN");
115 OIC_LOG(ERROR, TAG, "parameter is null");
116 return CA_STATUS_INVALID_PARAM;
120 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &errorInfo->info);
121 OIC_LOG(DEBUG, TAG, "OUT");
125 coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint)
127 OIC_LOG(DEBUG, TAG, "IN");
129 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
130 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
132 coap_pdu_t *pdu = NULL;
134 // RESET have to use only 4byte (empty message)
135 // and ACKNOWLEDGE can use empty message when code is empty.
136 if (CA_MSG_RESET == info->type || (CA_EMPTY == code && CA_MSG_ACKNOWLEDGE == info->type))
138 OIC_LOG(DEBUG, TAG, "code is empty");
139 if (!(pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, NULL)))
141 OIC_LOG(ERROR, TAG, "pdu NULL");
147 coap_list_t *optlist = NULL;
149 if (CA_MSG_ACKNOWLEDGE != info->type)
151 const char *uri = info->resourceUri;
154 OIC_LOG(ERROR, TAG, "uri NULL");
158 uint32_t length = strlen(uri);
159 if (CA_MAX_URI_LENGTH < length)
161 OIC_LOG(ERROR, TAG, "URI len err");
165 uint32_t uriLength = length + sizeof(COAP_URI_HEADER);
166 char *coapUri = (char *) OICCalloc(1, uriLength);
169 OIC_LOG(ERROR, TAG, "out of memory");
172 OICStrcat(coapUri, uriLength, COAP_URI_HEADER);
173 OICStrcat(coapUri, uriLength, uri);
175 // parsing options in URI
176 CAResult_t res = CAParseURI(coapUri, &optlist);
177 if (CA_STATUS_OK != res)
181 coap_delete_list(optlist);
189 // parsing options in HeadOption
190 CAResult_t ret = CAParseHeadOption(code, info, &optlist);
191 if (CA_STATUS_OK != ret)
193 coap_delete_list(optlist);
197 pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, optlist);
200 OIC_LOG(ERROR, TAG, "pdu NULL");
201 coap_delete_list(optlist);
206 coap_delete_list(optlist);
209 // pdu print method : coap_show_pdu(pdu);
210 OIC_LOG(DEBUG, TAG, "OUT");
214 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode,
215 const CAEndpoint_t *endpoint)
217 OIC_LOG(DEBUG, TAG, "IN");
221 OIC_LOG(ERROR, TAG, "data is null");
225 coap_transport_type transport;
227 if (CA_ADAPTER_TCP == endpoint->adapter)
229 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)data)[0] >> 4);
234 transport = coap_udp;
237 coap_pdu_t *outpdu = coap_new_pdu(transport, length);
240 OIC_LOG(ERROR, TAG, "outpdu is null");
244 OIC_LOG_V(DEBUG, TAG, "pdu parse-transport type : %d", transport);
246 int ret = coap_pdu_parse((unsigned char *) data, length, outpdu, transport);
247 OIC_LOG_V(DEBUG, TAG, "pdu parse ret: %d", ret);
250 OIC_LOG(ERROR, TAG, "pdu parse failed");
251 coap_delete_pdu(outpdu);
256 if (CA_ADAPTER_TCP == endpoint->adapter)
258 OIC_LOG(INFO, TAG, "there is no version info in coap header");
263 if (outpdu->hdr->coap_hdr_udp_t.version != COAP_DEFAULT_VERSION)
265 OIC_LOG_V(ERROR, TAG, "coap version is not available : %d",
266 outpdu->hdr->coap_hdr_udp_t.version);
267 coap_delete_pdu(outpdu);
270 if (outpdu->hdr->coap_hdr_udp_t.token_length > CA_MAX_TOKEN_LEN)
272 OIC_LOG_V(ERROR, TAG, "token length has been exceed : %d",
273 outpdu->hdr->coap_hdr_udp_t.token_length);
274 coap_delete_pdu(outpdu);
281 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(outpdu, transport));
284 OIC_LOG(DEBUG, TAG, "OUT");
288 coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
289 const CAEndpoint_t *endpoint, coap_list_t *options)
291 OIC_LOG(DEBUG, TAG, "IN");
292 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
293 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
295 coap_transport_type transport;
296 unsigned int length = COAP_MAX_PDU_SIZE;
298 unsigned int headerLength = 0;
299 if (CA_ADAPTER_TCP == endpoint->adapter)
303 for (coap_list_t *opt = options; opt; opt = opt->next)
305 headerLength += COAP_OPTION_LENGTH(*(coap_option *) opt->data) + 1;
309 if (info->payloadSize > 0)
311 headerLength = headerLength + info->payloadSize + 1;
313 transport = coap_get_tcp_header_type_from_size(headerLength);
314 length = headerLength + coap_get_tcp_header_length_for_transport(transport)
320 transport = coap_udp;
323 coap_pdu_t *pdu = coap_new_pdu(transport, length);
327 OIC_LOG(ERROR, TAG, "malloc failed");
331 OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %d",
332 transport, info->payloadSize);
335 if (CA_ADAPTER_TCP == endpoint->adapter)
337 coap_add_length(pdu, transport, headerLength);
342 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
344 if (0 == info->messageId)
346 /* initialize message id */
347 prng((uint8_t * ) &message_id, sizeof(message_id));
349 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
353 /* use saved message id */
354 message_id = info->messageId;
356 pdu->hdr->coap_hdr_udp_t.id = message_id;
357 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->coap_hdr_udp_t.id);
359 pdu->hdr->coap_hdr_udp_t.type = info->type;
362 coap_add_code(pdu, transport, code);
364 if (info->token && CA_EMPTY != code)
366 uint32_t tokenLength = info->tokenLength;
367 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
368 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
370 int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token, transport);
373 OIC_LOG(ERROR, TAG, "can't add token");
379 for (coap_list_t *opt = options; opt; opt = opt->next)
381 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
382 COAP_OPTION_DATA(*(coap_option *) opt->data));
384 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
385 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
386 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
387 COAP_OPTION_DATA(*(coap_option *) opt->data), transport);
391 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
393 bool enabledPayload = false;
395 enabledPayload = true;
398 if (enabledPayload || CA_ADAPTER_GATT_BTLE == endpoint->adapter
400 || CA_ADAPTER_TCP == endpoint->adapter
404 if (NULL != info->payload && 0 < info->payloadSize)
406 OIC_LOG(DEBUG, TAG, "payload is added");
407 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
411 OIC_LOG(DEBUG, TAG, "OUT");
415 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
417 OIC_LOG(DEBUG, TAG, "IN");
421 OIC_LOG(ERROR, TAG, "uriInfo is null");
422 return CA_STATUS_INVALID_PARAM;
425 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
429 OIC_LOG(ERROR, TAG, "optlist is null");
430 return CA_STATUS_INVALID_PARAM;
433 /* split arg into Uri-* options */
435 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
437 if (uri.port != COAP_DEFAULT_PORT)
439 unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 };
440 int ret = coap_insert(optlist,
441 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
442 coap_encode_var_bytes(portbuf, uri.port),
447 return CA_STATUS_INVALID_PARAM;
451 if (uri.path.s && uri.path.length)
453 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
454 COAP_OPTION_URI_PATH, optlist);
455 if (CA_STATUS_OK != ret)
457 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
462 if (uri.query.s && uri.query.length)
464 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
466 if (CA_STATUS_OK != ret)
468 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
473 OIC_LOG(DEBUG, TAG, "OUT");
477 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
478 coap_list_t **optlist)
482 OIC_LOG(ERROR, TAG, "optlist is null");
483 return CA_STATUS_INVALID_PARAM;
486 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
488 // should never occur. Log just in case.
489 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
490 return CA_NOT_SUPPORTED;
492 else if (str && length)
494 unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
495 unsigned char *pBuf = uriBuffer;
496 size_t buflen = sizeof(uriBuffer);
497 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
498 coap_split_query(str, length, pBuf, &buflen);
505 int ret = coap_insert(optlist,
506 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
507 (char *)COAP_OPT_VALUE(pBuf)),
511 return CA_STATUS_INVALID_PARAM;
514 size_t optSize = COAP_OPT_SIZE(pBuf);
515 if ((prevIdx + optSize) < buflen)
524 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
525 return CA_STATUS_FAILED;
530 OIC_LOG(ERROR, TAG, "str or length is not available");
531 return CA_STATUS_FAILED;
537 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
540 OIC_LOG(DEBUG, TAG, "IN");
541 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", CA_STATUS_INVALID_PARAM);
543 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
547 OIC_LOG(ERROR, TAG, "optlist is null");
548 return CA_STATUS_INVALID_PARAM;
551 for (uint32_t i = 0; i < info->numOptions; i++)
553 if(!(info->options + i))
555 OIC_LOG(ERROR, TAG, "options is not available");
556 return CA_STATUS_FAILED;
559 uint32_t id = (info->options + i)->optionID;
560 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
562 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
566 if ((info->options + i)->optionData && (info->options + i)->optionLength > 0)
568 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
569 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
570 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
571 int ret = coap_insert(optlist,
572 CACreateNewOptionNode(id, (info->options + i)->optionLength,
573 (info->options + i)->optionData),
577 return CA_STATUS_INVALID_PARAM;
583 // insert one extra header with the payload format if applicable.
584 if (CA_FORMAT_UNDEFINED != info->payloadFormat)
586 coap_list_t* node = NULL;
587 uint8_t buf[3] = {0};
588 switch (info->payloadFormat) {
589 case CA_FORMAT_APPLICATION_CBOR:
590 node = CACreateNewOptionNode(
591 COAP_OPTION_CONTENT_FORMAT,
592 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
596 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->payloadFormat);
600 OIC_LOG(ERROR, TAG, "format option not created");
601 return CA_STATUS_INVALID_PARAM;
603 int ret = coap_insert(optlist, node, CAOrderOpts);
607 OIC_LOG(ERROR, TAG, "format option not inserted in header");
608 return CA_STATUS_INVALID_PARAM;
611 if (CA_FORMAT_UNDEFINED != info->acceptFormat)
613 coap_list_t* node = NULL;
614 uint8_t buf[3] = {0};
615 switch (info->acceptFormat) {
616 case CA_FORMAT_APPLICATION_CBOR:
617 node = CACreateNewOptionNode(
619 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
623 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->acceptFormat);
627 OIC_LOG(ERROR, TAG, "format option not created");
628 return CA_STATUS_INVALID_PARAM;
630 int ret = coap_insert(optlist, node, CAOrderOpts);
634 OIC_LOG(ERROR, TAG, "format option not inserted in header");
635 return CA_STATUS_INVALID_PARAM;
639 OIC_LOG(DEBUG, TAG, "OUT");
643 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
645 OIC_LOG(DEBUG, TAG, "IN");
649 OIC_LOG(ERROR, TAG, "invalid pointer parameter");
653 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
656 OIC_LOG(ERROR, TAG, "Out of memory");
659 memset(option, 0, sizeof(coap_option) + length + 1);
661 COAP_OPTION_KEY(*option) = key;
663 coap_option_def_t* def = coap_opt_def(key);
664 if (NULL != def && coap_is_var_bytes(def))
666 if (length > def->max)
668 // make sure we shrink the value so it fits the coap option definition
669 // by truncating the value, disregard the leading bytes.
670 OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]",
671 def->key, length, def->max);
672 data = &(data[length-def->max]);
675 // Shrink the encoding length to a minimum size for coap
676 // options that support variable length encoding.
677 COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes(
678 COAP_OPTION_DATA(*option),
679 coap_decode_var_bytes((unsigned char *)data, length));
683 COAP_OPTION_LENGTH(*option) = length;
684 memcpy(COAP_OPTION_DATA(*option), data, length);
687 /* we can pass NULL here as delete function since option is released automatically */
688 coap_list_t *node = coap_new_listnode(option, NULL);
692 OIC_LOG(ERROR, TAG, "node is NULL");
697 OIC_LOG(DEBUG, TAG, "OUT");
701 int CAOrderOpts(void *a, void *b)
703 OIC_LOG(DEBUG, TAG, "IN");
706 return a < b ? -1 : 1;
709 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
714 OIC_LOG(DEBUG, TAG, "OUT");
715 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
718 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
720 OIC_LOG(DEBUG, TAG, "IN");
724 while ((option = coap_option_next(&opt_iter)))
726 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
727 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
728 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type
729 && COAP_OPTION_CONTENT_FORMAT != opt_iter.type
730 && COAP_OPTION_ACCEPT != opt_iter.type)
736 OIC_LOG(DEBUG, TAG, "OUT");
740 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
741 uint32_t *outCode, CAInfo_t *outInfo)
743 OIC_LOG(DEBUG, TAG, "IN");
745 if (!pdu || !outCode || !outInfo)
747 OIC_LOG(ERROR, TAG, "NULL pointer param");
748 return CA_STATUS_INVALID_PARAM;
751 coap_transport_type transport;
753 if (CA_ADAPTER_TCP == endpoint->adapter)
755 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->hdr)[0] >> 4);
760 transport = coap_udp;
763 coap_opt_iterator_t opt_iter;
764 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport);
768 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(pdu, transport));
771 // init HeaderOption list
772 uint32_t count = CAGetOptionCount(opt_iter);
773 memset(outInfo, 0, sizeof(*outInfo));
775 outInfo->numOptions = count;
778 if (CA_ADAPTER_TCP == endpoint->adapter)
781 outInfo->type = CA_MSG_NONCONFIRM;
782 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
788 outInfo->type = pdu->hdr->coap_hdr_udp_t.type;
791 outInfo->messageId = pdu->hdr->coap_hdr_udp_t.id;
792 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
793 outInfo->acceptFormat = CA_FORMAT_UNDEFINED;
798 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
799 if (NULL == outInfo->options)
801 OIC_LOG(ERROR, TAG, "Out of memory");
802 return CA_MEMORY_ALLOC_FAILED;
807 char optionResult[CA_MAX_URI_LENGTH] = {0};
809 uint32_t optionLength = 0;
810 bool isfirstsetflag = false;
811 bool isQueryBeingProcessed = false;
813 while ((option = coap_option_next(&opt_iter)))
815 char buf[COAP_MAX_PDU_SIZE] = {0};
817 CAGetOptionData(opt_iter.type, (uint8_t *)(COAP_OPT_VALUE(option)),
818 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf));
821 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
822 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
824 if (false == isfirstsetflag)
826 isfirstsetflag = true;
827 optionResult[optionLength] = '/';
829 // Make sure there is enough room in the optionResult buffer
830 if ((optionLength + bufLength) < sizeof(optionResult))
832 memcpy(&optionResult[optionLength], buf, bufLength);
833 optionLength += bufLength;
842 if (COAP_OPTION_URI_PATH == opt_iter.type)
844 // Make sure there is enough room in the optionResult buffer
845 if (optionLength < sizeof(optionResult))
847 optionResult[optionLength] = '/';
855 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
857 if (false == isQueryBeingProcessed)
859 // Make sure there is enough room in the optionResult buffer
860 if (optionLength < sizeof(optionResult))
862 optionResult[optionLength] = '?';
864 isQueryBeingProcessed = true;
873 // Make sure there is enough room in the optionResult buffer
874 if (optionLength < sizeof(optionResult))
876 optionResult[optionLength] = ';';
885 // Make sure there is enough room in the optionResult buffer
886 if ((optionLength + bufLength) < sizeof(optionResult))
888 memcpy(&optionResult[optionLength], buf, bufLength);
889 optionLength += bufLength;
897 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
898 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
900 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
902 else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type)
904 if (1 == COAP_OPT_LENGTH(option))
906 outInfo->payloadFormat = CAConvertFormat((uint8_t)buf[0]);
910 outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED;
911 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
912 opt_iter.type, (uint8_t)buf[0]);
915 else if (COAP_OPTION_ACCEPT == opt_iter.type)
917 if (1 == COAP_OPT_LENGTH(option))
919 outInfo->acceptFormat = CAConvertFormat((uint8_t)buf[0]);
923 outInfo->acceptFormat = CA_FORMAT_UNSUPPORTED;
925 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
926 opt_iter.type, (uint8_t)buf[0]);
932 uint32_t length = bufLength;
934 if (length <= CA_MAX_HEADER_OPTION_DATA_LENGTH)
936 outInfo->options[idx].optionID = opt_iter.type;
937 outInfo->options[idx].optionLength = length;
938 outInfo->options[idx].protocolID = CA_COAP_ID;
939 memcpy(outInfo->options[idx].optionData, buf, length);
947 unsigned char* token = NULL;
948 unsigned int token_length = 0;
949 coap_get_token(pdu->hdr, transport, &token, &token_length);
952 if (token_length > 0)
954 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", token_length);
955 outInfo->token = (char *) OICMalloc(token_length);
956 if (NULL == outInfo->token)
958 OIC_LOG(ERROR, TAG, "Out of memory");
959 OICFree(outInfo->options);
960 return CA_MEMORY_ALLOC_FAILED;
962 memcpy(outInfo->token, token, token_length);
965 outInfo->tokenLength = token_length;
970 if (coap_get_data(pdu, &dataSize, &data))
972 OIC_LOG(DEBUG, TAG, "inside pdu->data");
973 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
974 if (NULL == outInfo->payload)
976 OIC_LOG(ERROR, TAG, "Out of memory");
977 OICFree(outInfo->options);
978 OICFree(outInfo->token);
979 return CA_MEMORY_ALLOC_FAILED;
981 memcpy(outInfo->payload, pdu->data, dataSize);
982 outInfo->payloadSize = dataSize;
985 if (optionResult[0] != '\0')
987 OIC_LOG_V(DEBUG, TAG, "URL length:%d", strlen(optionResult));
988 outInfo->resourceUri = OICStrdup(optionResult);
989 if (!outInfo->resourceUri)
991 OIC_LOG(ERROR, TAG, "Out of memory");
992 OICFree(outInfo->options);
993 OICFree(outInfo->token);
994 return CA_MEMORY_ALLOC_FAILED;
998 OIC_LOG(DEBUG, TAG, "OUT");
1002 OIC_LOG(ERROR, TAG, "buffer too small");
1003 OICFree(outInfo->options);
1004 return CA_STATUS_FAILED;
1007 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo,
1008 const CAEndpoint_t *endpoint)
1010 OIC_LOG(DEBUG, TAG, "IN");
1011 if (NULL == pdu_hdr)
1013 OIC_LOG(ERROR, TAG, "pdu_hdr is null");
1014 return CA_STATUS_INVALID_PARAM;
1017 if (NULL == outInfo)
1019 OIC_LOG(ERROR, TAG, "outInfo is null");
1020 return CA_STATUS_INVALID_PARAM;
1023 coap_transport_type transport;
1025 if (CA_ADAPTER_TCP == endpoint->adapter)
1027 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4);
1032 transport = coap_udp;
1035 unsigned char* token = NULL;
1036 unsigned int token_length = 0;
1037 coap_get_token(pdu_hdr, transport, &token, &token_length);
1040 if (token_length > 0)
1042 OIC_LOG_V(DEBUG, TAG, "token len:%d", token_length);
1043 outInfo->token = (char *) OICMalloc(token_length);
1044 if (NULL == outInfo->token)
1046 OIC_LOG(ERROR, TAG, "Out of memory");
1047 return CA_MEMORY_ALLOC_FAILED;
1049 memcpy(outInfo->token, token, token_length);
1052 outInfo->tokenLength = token_length;
1054 OIC_LOG(DEBUG, TAG, "OUT");
1056 return CA_STATUS_OK;
1059 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
1061 OIC_LOG(DEBUG, TAG, "IN");
1065 OIC_LOG(ERROR, TAG, "invalid token pointer");
1066 return CA_STATUS_INVALID_PARAM;
1069 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
1071 OIC_LOG(ERROR, TAG, "invalid token length");
1072 return CA_STATUS_INVALID_PARAM;
1082 if (SEED == (unsigned int)((time_t)-1))
1084 OIC_LOG(ERROR, TAG, "seed is not made");
1086 return CA_STATUS_FAILED;
1095 // memory allocation
1096 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
1099 OIC_LOG(ERROR, TAG, "Out of memory");
1100 return CA_MEMORY_ALLOC_FAILED;
1104 for (uint8_t index = 0; index < tokenLength; index++)
1106 // use valid characters
1108 temp[index] = rand() & 0x00FF;
1110 temp[index] = random() & 0x00FF;
1117 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
1118 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
1120 OIC_LOG(DEBUG, TAG, "OUT");
1121 return CA_STATUS_OK;
1124 void CADestroyTokenInternal(CAToken_t token)
1126 OIC_LOG(DEBUG, TAG, "IN");
1128 OIC_LOG(DEBUG, TAG, "OUT");
1131 void CADestroyInfo(CAInfo_t *info)
1133 OIC_LOG(DEBUG, TAG, "IN");
1137 OIC_LOG(DEBUG, TAG, "free options");
1138 OICFree(info->options);
1140 OIC_LOG(DEBUG, TAG, "free token");
1141 OICFree(info->token);
1143 OIC_LOG(DEBUG, TAG, "free payload");
1144 OICFree(info->payload);
1147 OIC_LOG(DEBUG, TAG, "OUT");
1150 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
1151 uint8_t *option, uint32_t buflen)
1155 OIC_LOG(ERROR, TAG, "buflen 0");
1159 if (NULL == data || NULL == option)
1161 OIC_LOG(ERROR, TAG, "data/option NULL");
1167 OIC_LOG(ERROR, TAG, "option buffer too small");
1171 coap_option_def_t* def = coap_opt_def(key);
1172 if(NULL != def && coap_is_var_bytes(def) && 0 == len) {
1173 // A 0 length option is permitted in CoAP but the
1174 // rest or the stack is unaware of variable byte encoding
1175 // should remain that way so a 0 byte of length 1 is inserted.
1179 memcpy(option, data, len);
1186 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
1190 OIC_LOG(ERROR, TAG, "pdu is null");
1191 return CA_MSG_NONCONFIRM;
1194 // pdu minimum size is 4 byte.
1195 if (size < CA_PDU_MIN_SIZE)
1197 OIC_LOG(ERROR, TAG, "min size");
1198 return CA_MSG_NONCONFIRM;
1201 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1203 return (CAMessageType_t) hdr->coap_hdr_udp_t.type;
1206 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
1210 OIC_LOG(ERROR, TAG, "pdu is null");
1214 // pdu minimum size is 4 byte.
1215 if (size < CA_PDU_MIN_SIZE)
1217 OIC_LOG(ERROR, TAG, "min size");
1221 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1223 return hdr->coap_hdr_udp_t.id;
1226 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
1230 OIC_LOG(ERROR, TAG, "pdu is null");
1231 return CA_NOT_FOUND;
1234 // pdu minimum size is 4 byte.
1235 if (size < CA_PDU_MIN_SIZE)
1237 OIC_LOG(ERROR, TAG, "min size");
1238 return CA_NOT_FOUND;
1241 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1243 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->coap_hdr_udp_t.code);
1246 CAPayloadFormat_t CAConvertFormat(uint8_t format)
1250 case COAP_MEDIATYPE_TEXT_PLAIN:
1251 return CA_FORMAT_TEXT_PLAIN;
1252 case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT:
1253 return CA_FORMAT_APPLICATION_LINK_FORMAT;
1254 case COAP_MEDIATYPE_APPLICATION_XML:
1255 return CA_FORMAT_APPLICATION_XML;
1256 case COAP_MEDIATYPE_APPLICATION_OCTET_STREAM:
1257 return CA_FORMAT_APPLICATION_OCTET_STREAM;
1258 case COAP_MEDIATYPE_APPLICATION_RDF_XML:
1259 return CA_FORMAT_APPLICATION_RDF_XML;
1260 case COAP_MEDIATYPE_APPLICATION_EXI:
1261 return CA_FORMAT_APPLICATION_EXI;
1262 case COAP_MEDIATYPE_APPLICATION_JSON:
1263 return CA_FORMAT_APPLICATION_JSON;
1264 case COAP_MEDIATYPE_APPLICATION_CBOR:
1265 return CA_FORMAT_APPLICATION_CBOR;
1267 return CA_FORMAT_UNSUPPORTED;