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 // For details on compatibility and glibc support,
28 // Refer http://www.gnu.org/software/libc/manual/html_node/BSD-Random.html
29 #define _DEFAULT_SOURCE
39 #include "caprotocolmessage.h"
41 #include "oic_malloc.h"
42 #include "oic_string.h"
44 #include "cacommonutil.h"
46 #define TAG "CA_PRTCL_MSG"
48 #define CA_BUFSIZE (128)
49 #define CA_PDU_MIN_SIZE (4)
50 #define CA_PORT_BUFFER_SIZE (4)
52 static const char COAP_URI_HEADER[] = "coap://[::]/";
54 CAResult_t CAGetRequestInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
55 CARequestInfo_t *outReqInfo)
57 VERIFY_NON_NULL(pdu, TAG, "pdu");
58 VERIFY_NON_NULL(outReqInfo, TAG, "outReqInfo");
60 uint32_t code = CA_NOT_FOUND;
61 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outReqInfo->info));
62 outReqInfo->method = code;
67 CAResult_t CAGetResponseInfoFromPDU(const coap_pdu_t *pdu, CAResponseInfo_t *outResInfo,
68 const CAEndpoint_t *endpoint)
70 VERIFY_NON_NULL(pdu, TAG, "pdu");
71 VERIFY_NON_NULL(outResInfo, TAG, "outResInfo");
73 uint32_t code = CA_NOT_FOUND;
74 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &(outResInfo->info));
75 outResInfo->result = code;
80 CAResult_t CAGetErrorInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
81 CAErrorInfo_t *errorInfo)
83 VERIFY_NON_NULL(pdu, TAG, "pdu");
86 CAResult_t ret = CAGetInfoFromPDU(pdu, endpoint, &code, &errorInfo->info);
91 coap_pdu_t *CAGeneratePDU(uint32_t code, const CAInfo_t *info, const CAEndpoint_t *endpoint,
92 coap_list_t **optlist, coap_transport_type *transport)
94 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
95 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
96 VERIFY_NON_NULL_RET(optlist, TAG, "optlist", NULL);
98 coap_pdu_t *pdu = NULL;
100 // RESET have to use only 4byte (empty message)
101 // and ACKNOWLEDGE can use empty message when code is empty.
102 if (CA_MSG_RESET == info->type || (CA_EMPTY == code && CA_MSG_ACKNOWLEDGE == info->type))
104 if (CA_EMPTY != code)
106 OIC_LOG(ERROR, TAG, "reset is not empty message");
110 if (info->payloadSize > 0 || info->payload || info->token || info->tokenLength > 0)
112 OIC_LOG(ERROR, TAG, "Empty message has unnecessary data after messageID");
116 OIC_LOG(DEBUG, TAG, "code is empty");
117 if (!(pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, NULL, transport)))
119 OIC_LOG(ERROR, TAG, "pdu NULL");
125 if (info->resourceUri)
127 uint32_t length = strlen(info->resourceUri);
128 if (CA_MAX_URI_LENGTH < length)
130 OIC_LOG(ERROR, TAG, "URI len err");
134 uint32_t uriLength = length + sizeof(COAP_URI_HEADER);
135 char *coapUri = (char *) OICCalloc(1, uriLength);
138 OIC_LOG(ERROR, TAG, "out of memory");
141 OICStrcat(coapUri, uriLength, COAP_URI_HEADER);
142 OICStrcat(coapUri, uriLength, info->resourceUri);
144 // parsing options in URI
145 CAResult_t res = CAParseURI(coapUri, optlist);
146 if (CA_STATUS_OK != res)
154 // parsing options in HeadOption
155 CAResult_t ret = CAParseHeadOption(code, info, optlist);
156 if (CA_STATUS_OK != ret)
161 pdu = CAGeneratePDUImpl((code_t) code, info, endpoint, *optlist, transport);
164 OIC_LOG(ERROR, TAG, "pdu NULL");
169 // pdu print method : coap_show_pdu(pdu);
173 coap_pdu_t *CAParsePDU(const char *data, uint32_t length, uint32_t *outCode,
174 const CAEndpoint_t *endpoint)
176 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
177 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
179 coap_transport_type transport;
181 if (CA_ADAPTER_TCP == endpoint->adapter)
183 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)data)[0] >> 4);
190 transport = coap_udp;
193 coap_pdu_t *outpdu = coap_new_pdu(transport, length);
196 OIC_LOG(ERROR, TAG, "outpdu is null");
200 OIC_LOG_V(DEBUG, TAG, "pdu parse-transport type : %d", transport);
202 int ret = coap_pdu_parse((unsigned char *) data, length, outpdu, transport);
203 OIC_LOG_V(DEBUG, TAG, "pdu parse ret: %d", ret);
206 OIC_LOG(ERROR, TAG, "pdu parse failed");
207 coap_delete_pdu(outpdu);
212 if (CA_ADAPTER_TCP == endpoint->adapter)
214 OIC_LOG(INFO, TAG, "there is no version info in coap header");
219 if (outpdu->hdr->coap_hdr_udp_t.version != COAP_DEFAULT_VERSION)
221 OIC_LOG_V(ERROR, TAG, "coap version is not available : %d",
222 outpdu->hdr->coap_hdr_udp_t.version);
223 coap_delete_pdu(outpdu);
226 if (outpdu->hdr->coap_hdr_udp_t.token_length > CA_MAX_TOKEN_LEN)
228 OIC_LOG_V(ERROR, TAG, "token length has been exceed : %d",
229 outpdu->hdr->coap_hdr_udp_t.token_length);
230 coap_delete_pdu(outpdu);
237 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(outpdu, transport));
243 coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
244 const CAEndpoint_t *endpoint, coap_list_t *options,
245 coap_transport_type *transport)
247 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
248 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
249 VERIFY_NON_NULL_RET(transport, TAG, "transport", NULL);
251 unsigned int length = COAP_MAX_PDU_SIZE;
253 unsigned int msgLength = 0;
254 if (CA_ADAPTER_TCP == endpoint->adapter)
258 unsigned short prevOptNumber = 0;
259 for (coap_list_t *opt = options; opt; opt = opt->next)
261 unsigned short curOptNumber = COAP_OPTION_KEY(*(coap_option *) opt->data);
262 if (prevOptNumber > curOptNumber)
264 OIC_LOG(ERROR, TAG, "option list is wrong");
268 size_t optValueLen = COAP_OPTION_LENGTH(*(coap_option *) opt->data);
269 size_t optLength = coap_get_opt_header_length(curOptNumber - prevOptNumber, optValueLen);
272 OIC_LOG(ERROR, TAG, "Reserved for the Payload marker for the option");
275 msgLength += optLength;
276 prevOptNumber = curOptNumber;
277 OIC_LOG_V(DEBUG, TAG, "curOptNumber[%d], prevOptNumber[%d], optValueLen[%zu], "
278 "optLength[%zu], msgLength[%d]",
279 curOptNumber, prevOptNumber, optValueLen, optLength, msgLength);
283 if (info->payloadSize > 0)
285 msgLength = msgLength + info->payloadSize + PAYLOAD_MARKER;
287 *transport = coap_get_tcp_header_type_from_size(msgLength);
288 length = msgLength + coap_get_tcp_header_length_for_transport(*transport)
294 *transport = coap_udp;
297 coap_pdu_t *pdu = coap_new_pdu(*transport, length);
301 OIC_LOG(ERROR, TAG, "malloc failed");
305 OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %zu",
306 *transport, info->payloadSize);
309 if (CA_ADAPTER_TCP == endpoint->adapter)
311 coap_add_length(pdu, *transport, msgLength);
316 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
318 if (0 == info->messageId)
320 /* initialize message id */
321 prng((uint8_t * ) &message_id, sizeof(message_id));
323 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
327 /* use saved message id */
328 message_id = info->messageId;
330 pdu->hdr->coap_hdr_udp_t.id = message_id;
331 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->coap_hdr_udp_t.id);
333 pdu->hdr->coap_hdr_udp_t.type = info->type;
336 coap_add_code(pdu, *transport, code);
338 if (info->token && CA_EMPTY != code)
340 uint32_t tokenLength = info->tokenLength;
341 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
342 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
344 int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token, *transport);
347 OIC_LOG(ERROR, TAG, "can't add token");
352 if (CA_ADAPTER_GATT_BTLE != endpoint->adapter
354 && CA_ADAPTER_TCP != endpoint->adapter
358 // option list will be added in blockwise-transfer
365 for (coap_list_t *opt = options; opt; opt = opt->next)
367 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
368 COAP_OPTION_DATA(*(coap_option *) opt->data));
370 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
371 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
372 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
373 COAP_OPTION_DATA(*(coap_option *) opt->data), *transport);
377 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
379 if (NULL != info->payload && 0 < info->payloadSize)
381 OIC_LOG(DEBUG, TAG, "payload is added");
382 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
388 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
390 VERIFY_NON_NULL(uriInfo, TAG, "uriInfo");
391 VERIFY_NON_NULL(optlist, TAG, "optlist");
393 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
395 /* split arg into Uri-* options */
397 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
399 if (uri.port != COAP_DEFAULT_PORT)
401 unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 };
402 int ret = coap_insert(optlist,
403 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
404 coap_encode_var_bytes(portbuf, uri.port),
409 return CA_STATUS_INVALID_PARAM;
413 if (uri.path.s && uri.path.length)
415 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
416 COAP_OPTION_URI_PATH, optlist);
417 if (CA_STATUS_OK != ret)
419 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
424 if (uri.query.s && uri.query.length)
426 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
428 if (CA_STATUS_OK != ret)
430 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
438 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
439 coap_list_t **optlist)
441 VERIFY_NON_NULL(optlist, TAG, "optlist");
443 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
445 // should never occur. Log just in case.
446 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
447 return CA_NOT_SUPPORTED;
449 else if (str && length)
451 unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
452 unsigned char *pBuf = uriBuffer;
453 size_t buflen = sizeof(uriBuffer);
454 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
455 coap_split_query(str, length, pBuf, &buflen);
462 int ret = coap_insert(optlist,
463 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
464 (char *)COAP_OPT_VALUE(pBuf)),
468 return CA_STATUS_INVALID_PARAM;
471 size_t optSize = COAP_OPT_SIZE(pBuf);
472 if ((prevIdx + optSize) < buflen)
481 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
482 return CA_STATUS_FAILED;
487 OIC_LOG(ERROR, TAG, "str or length is not available");
488 return CA_STATUS_FAILED;
494 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
497 VERIFY_NON_NULL_RET(info, TAG, "info", CA_STATUS_INVALID_PARAM);
499 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
503 OIC_LOG(ERROR, TAG, "optlist is null");
504 return CA_STATUS_INVALID_PARAM;
507 for (uint32_t i = 0; i < info->numOptions; i++)
509 if(!(info->options + i))
511 OIC_LOG(ERROR, TAG, "options is not available");
512 return CA_STATUS_FAILED;
515 uint32_t id = (info->options + i)->optionID;
516 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
518 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
522 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
523 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
524 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
525 int ret = coap_insert(optlist,
526 CACreateNewOptionNode(id, (info->options + i)->optionLength,
527 (info->options + i)->optionData),
531 return CA_STATUS_INVALID_PARAM;
536 // insert one extra header with the payload format if applicable.
537 if (CA_FORMAT_UNDEFINED != info->payloadFormat)
539 coap_list_t* node = NULL;
540 uint8_t buf[3] = {0};
541 switch (info->payloadFormat)
543 case CA_FORMAT_APPLICATION_CBOR:
544 node = CACreateNewOptionNode(
545 COAP_OPTION_CONTENT_FORMAT,
546 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
550 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->payloadFormat);
554 OIC_LOG(ERROR, TAG, "format option not created");
555 return CA_STATUS_INVALID_PARAM;
557 int ret = coap_insert(optlist, node, CAOrderOpts);
561 OIC_LOG(ERROR, TAG, "format option not inserted in header");
562 return CA_STATUS_INVALID_PARAM;
565 if (CA_FORMAT_UNDEFINED != info->acceptFormat)
567 coap_list_t* node = NULL;
568 uint8_t buf[3] = {0};
569 switch (info->acceptFormat)
571 case CA_FORMAT_APPLICATION_CBOR:
572 node = CACreateNewOptionNode(
574 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
578 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->acceptFormat);
582 OIC_LOG(ERROR, TAG, "format option not created");
583 return CA_STATUS_INVALID_PARAM;
585 int ret = coap_insert(optlist, node, CAOrderOpts);
589 OIC_LOG(ERROR, TAG, "format option not inserted in header");
590 return CA_STATUS_INVALID_PARAM;
597 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
599 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
601 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
604 OIC_LOG(ERROR, TAG, "Out of memory");
607 memset(option, 0, sizeof(coap_option) + length + 1);
609 COAP_OPTION_KEY(*option) = key;
611 coap_option_def_t* def = coap_opt_def(key);
612 if (NULL != def && coap_is_var_bytes(def))
614 if (length > def->max)
616 // make sure we shrink the value so it fits the coap option definition
617 // by truncating the value, disregard the leading bytes.
618 OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]",
619 def->key, length, def->max);
620 data = &(data[length-def->max]);
623 // Shrink the encoding length to a minimum size for coap
624 // options that support variable length encoding.
625 COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes(
626 COAP_OPTION_DATA(*option),
627 coap_decode_var_bytes((unsigned char *)data, length));
631 COAP_OPTION_LENGTH(*option) = length;
632 memcpy(COAP_OPTION_DATA(*option), data, length);
635 /* we can pass NULL here as delete function since option is released automatically */
636 coap_list_t *node = coap_new_listnode(option, NULL);
640 OIC_LOG(ERROR, TAG, "node is NULL");
648 int CAOrderOpts(void *a, void *b)
652 return a < b ? -1 : 1;
655 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
660 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
663 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
668 while ((option = coap_option_next(&opt_iter)))
670 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
671 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
672 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type
673 && COAP_OPTION_CONTENT_FORMAT != opt_iter.type
674 && COAP_OPTION_ACCEPT != opt_iter.type)
683 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
684 uint32_t *outCode, CAInfo_t *outInfo)
686 VERIFY_NON_NULL(pdu, TAG, "pdu");
687 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
688 VERIFY_NON_NULL(outCode, TAG, "outCode");
689 VERIFY_NON_NULL(outInfo, TAG, "outInfo");
691 coap_transport_type transport;
693 if (CA_ADAPTER_TCP == endpoint->adapter)
695 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->hdr)[0] >> 4);
702 transport = coap_udp;
705 coap_opt_iterator_t opt_iter;
706 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport);
710 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(pdu, transport));
713 // init HeaderOption list
714 uint32_t count = CAGetOptionCount(opt_iter);
715 memset(outInfo, 0, sizeof(*outInfo));
717 outInfo->numOptions = count;
720 if (CA_ADAPTER_TCP == endpoint->adapter)
723 outInfo->type = CA_MSG_NONCONFIRM;
724 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
730 outInfo->type = pdu->hdr->coap_hdr_udp_t.type;
733 outInfo->messageId = pdu->hdr->coap_hdr_udp_t.id;
734 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
735 outInfo->acceptFormat = CA_FORMAT_UNDEFINED;
740 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
741 if (NULL == outInfo->options)
743 OIC_LOG(ERROR, TAG, "Out of memory");
744 return CA_MEMORY_ALLOC_FAILED;
749 char optionResult[CA_MAX_URI_LENGTH] = {0};
751 uint32_t optionLength = 0;
752 bool isfirstsetflag = false;
753 bool isQueryBeingProcessed = false;
755 while ((option = coap_option_next(&opt_iter)))
757 char buf[COAP_MAX_PDU_SIZE] = {0};
759 CAGetOptionData(opt_iter.type, (uint8_t *)(COAP_OPT_VALUE(option)),
760 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf));
763 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
764 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
766 if (false == isfirstsetflag)
768 isfirstsetflag = true;
769 optionResult[optionLength] = '/';
771 // Make sure there is enough room in the optionResult buffer
772 if ((optionLength + bufLength) < sizeof(optionResult))
774 memcpy(&optionResult[optionLength], buf, bufLength);
775 optionLength += bufLength;
784 if (COAP_OPTION_URI_PATH == opt_iter.type)
786 // Make sure there is enough room in the optionResult buffer
787 if (optionLength < sizeof(optionResult))
789 optionResult[optionLength] = '/';
797 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
799 if (false == isQueryBeingProcessed)
801 // Make sure there is enough room in the optionResult buffer
802 if (optionLength < sizeof(optionResult))
804 optionResult[optionLength] = '?';
806 isQueryBeingProcessed = true;
815 // Make sure there is enough room in the optionResult buffer
816 if (optionLength < sizeof(optionResult))
818 optionResult[optionLength] = ';';
827 // Make sure there is enough room in the optionResult buffer
828 if ((optionLength + bufLength) < sizeof(optionResult))
830 memcpy(&optionResult[optionLength], buf, bufLength);
831 optionLength += bufLength;
839 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
840 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
842 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
844 else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type)
846 if (1 == COAP_OPT_LENGTH(option))
848 outInfo->payloadFormat = CAConvertFormat((uint8_t)buf[0]);
852 outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED;
853 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
854 opt_iter.type, (uint8_t)buf[0]);
857 else if (COAP_OPTION_ACCEPT == opt_iter.type)
859 if (1 == COAP_OPT_LENGTH(option))
861 outInfo->acceptFormat = CAConvertFormat((uint8_t)buf[0]);
865 outInfo->acceptFormat = CA_FORMAT_UNSUPPORTED;
867 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
868 opt_iter.type, (uint8_t)buf[0]);
874 if (bufLength <= sizeof(outInfo->options[0].optionData))
876 outInfo->options[idx].optionID = opt_iter.type;
877 outInfo->options[idx].optionLength = bufLength;
878 outInfo->options[idx].protocolID = CA_COAP_ID;
879 memcpy(outInfo->options[idx].optionData, buf, bufLength);
887 unsigned char* token = NULL;
888 unsigned int token_length = 0;
889 coap_get_token(pdu->hdr, transport, &token, &token_length);
892 if (token_length > 0)
894 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", token_length);
895 outInfo->token = (char *) OICMalloc(token_length);
896 if (NULL == outInfo->token)
898 OIC_LOG(ERROR, TAG, "Out of memory");
899 OICFree(outInfo->options);
900 return CA_MEMORY_ALLOC_FAILED;
902 memcpy(outInfo->token, token, token_length);
905 outInfo->tokenLength = token_length;
910 if (coap_get_data(pdu, &dataSize, &data))
912 OIC_LOG(DEBUG, TAG, "inside pdu->data");
913 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
914 if (NULL == outInfo->payload)
916 OIC_LOG(ERROR, TAG, "Out of memory");
917 OICFree(outInfo->options);
918 OICFree(outInfo->token);
919 return CA_MEMORY_ALLOC_FAILED;
921 memcpy(outInfo->payload, pdu->data, dataSize);
922 outInfo->payloadSize = dataSize;
925 if (optionResult[0] != '\0')
927 OIC_LOG_V(DEBUG, TAG, "URL length:%zu", strlen(optionResult));
928 outInfo->resourceUri = OICStrdup(optionResult);
929 if (!outInfo->resourceUri)
931 OIC_LOG(ERROR, TAG, "Out of memory");
932 OICFree(outInfo->options);
933 OICFree(outInfo->token);
934 return CA_MEMORY_ALLOC_FAILED;
941 OIC_LOG(ERROR, TAG, "buffer too small");
942 OICFree(outInfo->options);
943 return CA_STATUS_FAILED;
946 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo,
947 const CAEndpoint_t *endpoint)
949 VERIFY_NON_NULL(pdu_hdr, TAG, "pdu_hdr");
950 VERIFY_NON_NULL(outInfo, TAG, "outInfo");
951 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
953 coap_transport_type transport;
955 if (CA_ADAPTER_TCP == endpoint->adapter)
957 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4);
964 transport = coap_udp;
967 unsigned char* token = NULL;
968 unsigned int token_length = 0;
969 coap_get_token(pdu_hdr, transport, &token, &token_length);
972 if (token_length > 0)
974 OIC_LOG_V(DEBUG, TAG, "token len:%d", token_length);
975 outInfo->token = (char *) OICMalloc(token_length);
976 if (NULL == outInfo->token)
978 OIC_LOG(ERROR, TAG, "Out of memory");
979 return CA_MEMORY_ALLOC_FAILED;
981 memcpy(outInfo->token, token, token_length);
984 outInfo->tokenLength = token_length;
989 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
991 VERIFY_NON_NULL(token, TAG, "token");
993 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
995 OIC_LOG(ERROR, TAG, "invalid token length");
996 return CA_STATUS_INVALID_PARAM;
1000 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
1003 OIC_LOG(ERROR, TAG, "Out of memory");
1004 return CA_MEMORY_ALLOC_FAILED;
1007 OCFillRandomMem((uint8_t *)temp, tokenLength);
1012 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
1013 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
1015 return CA_STATUS_OK;
1018 void CADestroyTokenInternal(CAToken_t token)
1023 void CADestroyInfo(CAInfo_t *info)
1027 OIC_LOG(DEBUG, TAG, "free options");
1028 OICFree(info->options);
1030 OIC_LOG(DEBUG, TAG, "free token");
1031 OICFree(info->token);
1033 OIC_LOG(DEBUG, TAG, "free payload");
1034 OICFree(info->payload);
1038 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
1039 uint8_t *option, uint32_t buflen)
1043 OIC_LOG(ERROR, TAG, "buflen 0");
1049 OIC_LOG(ERROR, TAG, "option buffer too small");
1053 coap_option_def_t* def = coap_opt_def(key);
1054 if (NULL != def && coap_is_var_bytes(def) && 0 == len)
1056 // A 0 length option is permitted in CoAP but the
1057 // rest or the stack is unaware of variable byte encoding
1058 // should remain that way so a 0 byte of length 1 is inserted.
1062 memcpy(option, data, len);
1069 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
1071 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", CA_MSG_NONCONFIRM);
1073 // pdu minimum size is 4 byte.
1074 if (size < CA_PDU_MIN_SIZE)
1076 OIC_LOG(ERROR, TAG, "min size");
1077 return CA_MSG_NONCONFIRM;
1080 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1082 return (CAMessageType_t) hdr->coap_hdr_udp_t.type;
1085 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
1087 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", 0);
1089 // pdu minimum size is 4 byte.
1090 if (size < CA_PDU_MIN_SIZE)
1092 OIC_LOG(ERROR, TAG, "min size");
1096 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1098 return hdr->coap_hdr_udp_t.id;
1101 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
1103 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", CA_NOT_FOUND);
1105 // pdu minimum size is 4 byte.
1106 if (size < CA_PDU_MIN_SIZE)
1108 OIC_LOG(ERROR, TAG, "min size");
1109 return CA_NOT_FOUND;
1112 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1114 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->coap_hdr_udp_t.code);
1117 CAPayloadFormat_t CAConvertFormat(uint8_t format)
1121 case COAP_MEDIATYPE_TEXT_PLAIN:
1122 return CA_FORMAT_TEXT_PLAIN;
1123 case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT:
1124 return CA_FORMAT_APPLICATION_LINK_FORMAT;
1125 case COAP_MEDIATYPE_APPLICATION_XML:
1126 return CA_FORMAT_APPLICATION_XML;
1127 case COAP_MEDIATYPE_APPLICATION_OCTET_STREAM:
1128 return CA_FORMAT_APPLICATION_OCTET_STREAM;
1129 case COAP_MEDIATYPE_APPLICATION_RDF_XML:
1130 return CA_FORMAT_APPLICATION_RDF_XML;
1131 case COAP_MEDIATYPE_APPLICATION_EXI:
1132 return CA_FORMAT_APPLICATION_EXI;
1133 case COAP_MEDIATYPE_APPLICATION_JSON:
1134 return CA_FORMAT_APPLICATION_JSON;
1135 case COAP_MEDIATYPE_APPLICATION_CBOR:
1136 return CA_FORMAT_APPLICATION_CBOR;
1138 return CA_FORMAT_UNSUPPORTED;