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);
205 transport = coap_udp;
208 coap_pdu_t *outpdu = coap_new_pdu(transport, length);
211 OIC_LOG(ERROR, TAG, "outpdu is null");
215 OIC_LOG_V(DEBUG, TAG, "pdu parse-transport type : %d", transport);
217 int ret = coap_pdu_parse((unsigned char *) data, length, outpdu, transport);
218 OIC_LOG_V(DEBUG, TAG, "pdu parse ret: %d", ret);
221 OIC_LOG(ERROR, TAG, "pdu parse failed");
222 coap_delete_pdu(outpdu);
227 if (CA_ADAPTER_TCP == endpoint->adapter)
229 OIC_LOG(INFO, TAG, "there is no version info in coap header");
234 if (outpdu->hdr->coap_hdr_udp_t.version != COAP_DEFAULT_VERSION)
236 OIC_LOG_V(ERROR, TAG, "coap version is not available : %d",
237 outpdu->hdr->coap_hdr_udp_t.version);
238 coap_delete_pdu(outpdu);
241 if (outpdu->hdr->coap_hdr_udp_t.token_length > CA_MAX_TOKEN_LEN)
243 OIC_LOG_V(ERROR, TAG, "token length has been exceed : %d",
244 outpdu->hdr->coap_hdr_udp_t.token_length);
245 coap_delete_pdu(outpdu);
252 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(outpdu, transport));
258 coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
259 const CAEndpoint_t *endpoint, coap_list_t *options,
260 coap_transport_type *transport)
262 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
263 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
264 VERIFY_NON_NULL_RET(transport, TAG, "transport", NULL);
266 unsigned int length = COAP_MAX_PDU_SIZE;
268 unsigned int msgLength = 0;
269 if (CA_ADAPTER_TCP == endpoint->adapter)
273 unsigned short prevOptNumber = 0;
274 for (coap_list_t *opt = options; opt; opt = opt->next)
276 unsigned short curOptNumber = COAP_OPTION_KEY(*(coap_option *) opt->data);
277 if (prevOptNumber > curOptNumber)
279 OIC_LOG(ERROR, TAG, "option list is wrong");
283 size_t optValueLen = COAP_OPTION_LENGTH(*(coap_option *) opt->data);
284 size_t optLength = coap_get_opt_header_length(curOptNumber - prevOptNumber, optValueLen);
287 OIC_LOG(ERROR, TAG, "Reserved for the Payload marker for the option");
290 msgLength += optLength;
291 prevOptNumber = curOptNumber;
292 OIC_LOG_V(DEBUG, TAG, "curOptNumber[%d], prevOptNumber[%d], optValueLen[%d], "
293 "optLength[%d], msgLength[%d]",
294 curOptNumber, prevOptNumber, optValueLen, optLength, msgLength);
298 if (info->payloadSize > 0)
300 msgLength = msgLength + info->payloadSize + PAYLOAD_MARKER;
302 *transport = coap_get_tcp_header_type_from_size(msgLength);
303 length = msgLength + coap_get_tcp_header_length_for_transport(*transport)
309 *transport = coap_udp;
312 coap_pdu_t *pdu = coap_new_pdu(*transport, length);
316 OIC_LOG(ERROR, TAG, "malloc failed");
320 OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %d",
321 *transport, info->payloadSize);
324 if (CA_ADAPTER_TCP == endpoint->adapter)
326 coap_add_length(pdu, *transport, msgLength);
331 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
333 if (0 == info->messageId)
335 /* initialize message id */
336 prng((uint8_t * ) &message_id, sizeof(message_id));
338 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
342 /* use saved message id */
343 message_id = info->messageId;
345 pdu->hdr->coap_hdr_udp_t.id = message_id;
346 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->coap_hdr_udp_t.id);
348 pdu->hdr->coap_hdr_udp_t.type = info->type;
351 coap_add_code(pdu, *transport, code);
353 if (info->token && CA_EMPTY != code)
355 uint32_t tokenLength = info->tokenLength;
356 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
357 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
359 int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token, *transport);
362 OIC_LOG(ERROR, TAG, "can't add token");
367 if (CA_ADAPTER_GATT_BTLE != endpoint->adapter
369 && CA_ADAPTER_TCP != endpoint->adapter
373 // option list will be added in blockwise-transfer
380 for (coap_list_t *opt = options; opt; opt = opt->next)
382 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
383 COAP_OPTION_DATA(*(coap_option *) opt->data));
385 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
386 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
387 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
388 COAP_OPTION_DATA(*(coap_option *) opt->data), *transport);
392 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
394 if (NULL != info->payload && 0 < info->payloadSize)
396 OIC_LOG(DEBUG, TAG, "payload is added");
397 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
403 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
407 OIC_LOG(ERROR, TAG, "uriInfo is null");
408 return CA_STATUS_INVALID_PARAM;
411 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
415 OIC_LOG(ERROR, TAG, "optlist is null");
416 return CA_STATUS_INVALID_PARAM;
419 /* split arg into Uri-* options */
421 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
423 if (uri.port != COAP_DEFAULT_PORT)
425 unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 };
426 int ret = coap_insert(optlist,
427 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
428 coap_encode_var_bytes(portbuf, uri.port),
433 return CA_STATUS_INVALID_PARAM;
437 if (uri.path.s && uri.path.length)
439 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
440 COAP_OPTION_URI_PATH, optlist);
441 if (CA_STATUS_OK != ret)
443 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
448 if (uri.query.s && uri.query.length)
450 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
452 if (CA_STATUS_OK != ret)
454 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
462 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
463 coap_list_t **optlist)
467 OIC_LOG(ERROR, TAG, "optlist is null");
468 return CA_STATUS_INVALID_PARAM;
471 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
473 // should never occur. Log just in case.
474 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
475 return CA_NOT_SUPPORTED;
477 else if (str && length)
479 unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
480 unsigned char *pBuf = uriBuffer;
481 size_t buflen = sizeof(uriBuffer);
482 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
483 coap_split_query(str, length, pBuf, &buflen);
490 int ret = coap_insert(optlist,
491 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
492 (char *)COAP_OPT_VALUE(pBuf)),
496 return CA_STATUS_INVALID_PARAM;
499 size_t optSize = COAP_OPT_SIZE(pBuf);
500 if ((prevIdx + optSize) < buflen)
509 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
510 return CA_STATUS_FAILED;
515 OIC_LOG(ERROR, TAG, "str or length is not available");
516 return CA_STATUS_FAILED;
522 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
525 VERIFY_NON_NULL_RET(info, TAG, "info is NULL", CA_STATUS_INVALID_PARAM);
527 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
531 OIC_LOG(ERROR, TAG, "optlist is null");
532 return CA_STATUS_INVALID_PARAM;
535 for (uint32_t i = 0; i < info->numOptions; i++)
537 if(!(info->options + i))
539 OIC_LOG(ERROR, TAG, "options is not available");
540 return CA_STATUS_FAILED;
543 uint32_t id = (info->options + i)->optionID;
544 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
546 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
550 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
551 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
552 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
553 int ret = coap_insert(optlist,
554 CACreateNewOptionNode(id, (info->options + i)->optionLength,
555 (info->options + i)->optionData),
559 return CA_STATUS_INVALID_PARAM;
564 // insert one extra header with the payload format if applicable.
565 if (CA_FORMAT_UNDEFINED != info->payloadFormat)
567 coap_list_t* node = NULL;
568 uint8_t buf[3] = {0};
569 switch (info->payloadFormat) {
570 case CA_FORMAT_APPLICATION_CBOR:
571 node = CACreateNewOptionNode(
572 COAP_OPTION_CONTENT_FORMAT,
573 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
577 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->payloadFormat);
581 OIC_LOG(ERROR, TAG, "format option not created");
582 return CA_STATUS_INVALID_PARAM;
584 int ret = coap_insert(optlist, node, CAOrderOpts);
588 OIC_LOG(ERROR, TAG, "format option not inserted in header");
589 return CA_STATUS_INVALID_PARAM;
592 if (CA_FORMAT_UNDEFINED != info->acceptFormat)
594 coap_list_t* node = NULL;
595 uint8_t buf[3] = {0};
596 switch (info->acceptFormat) {
597 case CA_FORMAT_APPLICATION_CBOR:
598 node = CACreateNewOptionNode(
600 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
604 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->acceptFormat);
608 OIC_LOG(ERROR, TAG, "format option not created");
609 return CA_STATUS_INVALID_PARAM;
611 int ret = coap_insert(optlist, node, CAOrderOpts);
615 OIC_LOG(ERROR, TAG, "format option not inserted in header");
616 return CA_STATUS_INVALID_PARAM;
623 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
627 OIC_LOG(ERROR, TAG, "invalid pointer parameter");
631 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
634 OIC_LOG(ERROR, TAG, "Out of memory");
637 memset(option, 0, sizeof(coap_option) + length + 1);
639 COAP_OPTION_KEY(*option) = key;
641 coap_option_def_t* def = coap_opt_def(key);
642 if (NULL != def && coap_is_var_bytes(def))
644 if (length > def->max)
646 // make sure we shrink the value so it fits the coap option definition
647 // by truncating the value, disregard the leading bytes.
648 OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]",
649 def->key, length, def->max);
650 data = &(data[length-def->max]);
653 // Shrink the encoding length to a minimum size for coap
654 // options that support variable length encoding.
655 COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes(
656 COAP_OPTION_DATA(*option),
657 coap_decode_var_bytes((unsigned char *)data, length));
661 COAP_OPTION_LENGTH(*option) = length;
662 memcpy(COAP_OPTION_DATA(*option), data, length);
665 /* we can pass NULL here as delete function since option is released automatically */
666 coap_list_t *node = coap_new_listnode(option, NULL);
670 OIC_LOG(ERROR, TAG, "node is NULL");
678 int CAOrderOpts(void *a, void *b)
682 return a < b ? -1 : 1;
685 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
690 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
693 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
698 while ((option = coap_option_next(&opt_iter)))
700 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
701 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
702 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type
703 && COAP_OPTION_CONTENT_FORMAT != opt_iter.type
704 && COAP_OPTION_ACCEPT != opt_iter.type)
713 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
714 uint32_t *outCode, CAInfo_t *outInfo)
716 if (!pdu || !outCode || !outInfo)
718 OIC_LOG(ERROR, TAG, "NULL pointer param");
719 return CA_STATUS_INVALID_PARAM;
722 coap_transport_type transport;
724 if (CA_ADAPTER_TCP == endpoint->adapter)
726 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->hdr)[0] >> 4);
731 transport = coap_udp;
734 coap_opt_iterator_t opt_iter;
735 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport);
739 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(pdu, transport));
742 // init HeaderOption list
743 uint32_t count = CAGetOptionCount(opt_iter);
744 memset(outInfo, 0, sizeof(*outInfo));
746 outInfo->numOptions = count;
749 if (CA_ADAPTER_TCP == endpoint->adapter)
752 outInfo->type = CA_MSG_NONCONFIRM;
753 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
759 outInfo->type = pdu->hdr->coap_hdr_udp_t.type;
762 outInfo->messageId = pdu->hdr->coap_hdr_udp_t.id;
763 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
764 outInfo->acceptFormat = CA_FORMAT_UNDEFINED;
769 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
770 if (NULL == outInfo->options)
772 OIC_LOG(ERROR, TAG, "Out of memory");
773 return CA_MEMORY_ALLOC_FAILED;
778 char optionResult[CA_MAX_URI_LENGTH] = {0};
780 uint32_t optionLength = 0;
781 bool isfirstsetflag = false;
782 bool isQueryBeingProcessed = false;
784 while ((option = coap_option_next(&opt_iter)))
786 char buf[COAP_MAX_PDU_SIZE] = {0};
788 CAGetOptionData(opt_iter.type, (uint8_t *)(COAP_OPT_VALUE(option)),
789 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf));
792 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
793 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
795 if (false == isfirstsetflag)
797 isfirstsetflag = true;
798 optionResult[optionLength] = '/';
800 // Make sure there is enough room in the optionResult buffer
801 if ((optionLength + bufLength) < sizeof(optionResult))
803 memcpy(&optionResult[optionLength], buf, bufLength);
804 optionLength += bufLength;
813 if (COAP_OPTION_URI_PATH == opt_iter.type)
815 // Make sure there is enough room in the optionResult buffer
816 if (optionLength < sizeof(optionResult))
818 optionResult[optionLength] = '/';
826 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
828 if (false == isQueryBeingProcessed)
830 // Make sure there is enough room in the optionResult buffer
831 if (optionLength < sizeof(optionResult))
833 optionResult[optionLength] = '?';
835 isQueryBeingProcessed = true;
844 // Make sure there is enough room in the optionResult buffer
845 if (optionLength < sizeof(optionResult))
847 optionResult[optionLength] = ';';
856 // Make sure there is enough room in the optionResult buffer
857 if ((optionLength + bufLength) < sizeof(optionResult))
859 memcpy(&optionResult[optionLength], buf, bufLength);
860 optionLength += bufLength;
868 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
869 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
871 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
873 else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type)
875 if (1 == COAP_OPT_LENGTH(option))
877 outInfo->payloadFormat = CAConvertFormat((uint8_t)buf[0]);
881 outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED;
882 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
883 opt_iter.type, (uint8_t)buf[0]);
886 else if (COAP_OPTION_ACCEPT == opt_iter.type)
888 if (1 == COAP_OPT_LENGTH(option))
890 outInfo->acceptFormat = CAConvertFormat((uint8_t)buf[0]);
894 outInfo->acceptFormat = CA_FORMAT_UNSUPPORTED;
896 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
897 opt_iter.type, (uint8_t)buf[0]);
903 if (bufLength <= sizeof(outInfo->options[0].optionData))
905 outInfo->options[idx].optionID = opt_iter.type;
906 outInfo->options[idx].optionLength = bufLength;
907 outInfo->options[idx].protocolID = CA_COAP_ID;
908 memcpy(outInfo->options[idx].optionData, buf, bufLength);
916 unsigned char* token = NULL;
917 unsigned int token_length = 0;
918 coap_get_token(pdu->hdr, transport, &token, &token_length);
921 if (token_length > 0)
923 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", token_length);
924 outInfo->token = (char *) OICMalloc(token_length);
925 if (NULL == outInfo->token)
927 OIC_LOG(ERROR, TAG, "Out of memory");
928 OICFree(outInfo->options);
929 return CA_MEMORY_ALLOC_FAILED;
931 memcpy(outInfo->token, token, token_length);
934 outInfo->tokenLength = token_length;
939 if (coap_get_data(pdu, &dataSize, &data))
941 OIC_LOG(DEBUG, TAG, "inside pdu->data");
942 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
943 if (NULL == outInfo->payload)
945 OIC_LOG(ERROR, TAG, "Out of memory");
946 OICFree(outInfo->options);
947 OICFree(outInfo->token);
948 return CA_MEMORY_ALLOC_FAILED;
950 memcpy(outInfo->payload, pdu->data, dataSize);
951 outInfo->payloadSize = dataSize;
954 if (optionResult[0] != '\0')
956 OIC_LOG_V(DEBUG, TAG, "URL length:%d", strlen(optionResult));
957 outInfo->resourceUri = OICStrdup(optionResult);
958 if (!outInfo->resourceUri)
960 OIC_LOG(ERROR, TAG, "Out of memory");
961 OICFree(outInfo->options);
962 OICFree(outInfo->token);
963 return CA_MEMORY_ALLOC_FAILED;
970 OIC_LOG(ERROR, TAG, "buffer too small");
971 OICFree(outInfo->options);
972 return CA_STATUS_FAILED;
975 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo,
976 const CAEndpoint_t *endpoint)
980 OIC_LOG(ERROR, TAG, "pdu_hdr is null");
981 return CA_STATUS_INVALID_PARAM;
986 OIC_LOG(ERROR, TAG, "outInfo is null");
987 return CA_STATUS_INVALID_PARAM;
990 coap_transport_type transport;
992 if (CA_ADAPTER_TCP == endpoint->adapter)
994 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4);
999 transport = coap_udp;
1002 unsigned char* token = NULL;
1003 unsigned int token_length = 0;
1004 coap_get_token(pdu_hdr, transport, &token, &token_length);
1007 if (token_length > 0)
1009 OIC_LOG_V(DEBUG, TAG, "token len:%d", token_length);
1010 outInfo->token = (char *) OICMalloc(token_length);
1011 if (NULL == outInfo->token)
1013 OIC_LOG(ERROR, TAG, "Out of memory");
1014 return CA_MEMORY_ALLOC_FAILED;
1016 memcpy(outInfo->token, token, token_length);
1019 outInfo->tokenLength = token_length;
1021 return CA_STATUS_OK;
1024 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
1028 OIC_LOG(ERROR, TAG, "invalid token pointer");
1029 return CA_STATUS_INVALID_PARAM;
1032 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
1034 OIC_LOG(ERROR, TAG, "invalid token length");
1035 return CA_STATUS_INVALID_PARAM;
1045 if (SEED == (unsigned int)((time_t)-1))
1047 OIC_LOG(ERROR, TAG, "seed is not made");
1049 return CA_STATUS_FAILED;
1058 // memory allocation
1059 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
1062 OIC_LOG(ERROR, TAG, "Out of memory");
1063 return CA_MEMORY_ALLOC_FAILED;
1067 for (uint8_t index = 0; index < tokenLength; index++)
1069 // use valid characters
1071 temp[index] = rand() & 0x00FF;
1073 temp[index] = random() & 0x00FF;
1080 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
1081 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
1083 return CA_STATUS_OK;
1086 void CADestroyTokenInternal(CAToken_t token)
1091 void CADestroyInfo(CAInfo_t *info)
1095 OIC_LOG(DEBUG, TAG, "free options");
1096 OICFree(info->options);
1098 OIC_LOG(DEBUG, TAG, "free token");
1099 OICFree(info->token);
1101 OIC_LOG(DEBUG, TAG, "free payload");
1102 OICFree(info->payload);
1106 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
1107 uint8_t *option, uint32_t buflen)
1111 OIC_LOG(ERROR, TAG, "buflen 0");
1117 OIC_LOG(ERROR, TAG, "option buffer too small");
1121 coap_option_def_t* def = coap_opt_def(key);
1122 if(NULL != def && coap_is_var_bytes(def) && 0 == len) {
1123 // A 0 length option is permitted in CoAP but the
1124 // rest or the stack is unaware of variable byte encoding
1125 // should remain that way so a 0 byte of length 1 is inserted.
1129 memcpy(option, data, len);
1136 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
1140 OIC_LOG(ERROR, TAG, "pdu is null");
1141 return CA_MSG_NONCONFIRM;
1144 // pdu minimum size is 4 byte.
1145 if (size < CA_PDU_MIN_SIZE)
1147 OIC_LOG(ERROR, TAG, "min size");
1148 return CA_MSG_NONCONFIRM;
1151 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1153 return (CAMessageType_t) hdr->coap_hdr_udp_t.type;
1156 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
1160 OIC_LOG(ERROR, TAG, "pdu is null");
1164 // pdu minimum size is 4 byte.
1165 if (size < CA_PDU_MIN_SIZE)
1167 OIC_LOG(ERROR, TAG, "min size");
1171 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1173 return hdr->coap_hdr_udp_t.id;
1176 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
1180 OIC_LOG(ERROR, TAG, "pdu is null");
1181 return CA_NOT_FOUND;
1184 // pdu minimum size is 4 byte.
1185 if (size < CA_PDU_MIN_SIZE)
1187 OIC_LOG(ERROR, TAG, "min size");
1188 return CA_NOT_FOUND;
1191 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1193 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->coap_hdr_udp_t.code);
1196 CAPayloadFormat_t CAConvertFormat(uint8_t format)
1200 case COAP_MEDIATYPE_TEXT_PLAIN:
1201 return CA_FORMAT_TEXT_PLAIN;
1202 case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT:
1203 return CA_FORMAT_APPLICATION_LINK_FORMAT;
1204 case COAP_MEDIATYPE_APPLICATION_XML:
1205 return CA_FORMAT_APPLICATION_XML;
1206 case COAP_MEDIATYPE_APPLICATION_OCTET_STREAM:
1207 return CA_FORMAT_APPLICATION_OCTET_STREAM;
1208 case COAP_MEDIATYPE_APPLICATION_RDF_XML:
1209 return CA_FORMAT_APPLICATION_RDF_XML;
1210 case COAP_MEDIATYPE_APPLICATION_EXI:
1211 return CA_FORMAT_APPLICATION_EXI;
1212 case COAP_MEDIATYPE_APPLICATION_JSON:
1213 return CA_FORMAT_APPLICATION_JSON;
1214 case COAP_MEDIATYPE_APPLICATION_CBOR:
1215 return CA_FORMAT_APPLICATION_CBOR;
1217 return CA_FORMAT_UNSUPPORTED;