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 "OIC_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 (CAIsSupportedCoAPOverTCP(endpoint->adapter))
183 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)data)[0] >> 4);
188 transport = coap_udp;
191 coap_pdu_t *outpdu = coap_new_pdu(transport, length);
194 OIC_LOG(ERROR, TAG, "outpdu is null");
198 OIC_LOG_V(DEBUG, TAG, "pdu parse-transport type : %d", transport);
200 int ret = coap_pdu_parse((unsigned char *) data, length, outpdu, transport);
201 OIC_LOG_V(DEBUG, TAG, "pdu parse ret: %d", ret);
204 OIC_LOG(ERROR, TAG, "pdu parse failed");
205 coap_delete_pdu(outpdu);
210 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
212 OIC_LOG(INFO, TAG, "there is no version info in coap header");
217 if (outpdu->hdr->coap_hdr_udp_t.version != COAP_DEFAULT_VERSION)
219 OIC_LOG_V(ERROR, TAG, "coap version is not available : %d",
220 outpdu->hdr->coap_hdr_udp_t.version);
221 coap_delete_pdu(outpdu);
224 if (outpdu->hdr->coap_hdr_udp_t.token_length > CA_MAX_TOKEN_LEN)
226 OIC_LOG_V(ERROR, TAG, "token length has been exceed : %d",
227 outpdu->hdr->coap_hdr_udp_t.token_length);
228 coap_delete_pdu(outpdu);
235 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(outpdu, transport));
241 coap_pdu_t *CAGeneratePDUImpl(code_t code, const CAInfo_t *info,
242 const CAEndpoint_t *endpoint, coap_list_t *options,
243 coap_transport_type *transport)
245 VERIFY_NON_NULL_RET(info, TAG, "info", NULL);
246 VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL);
247 VERIFY_NON_NULL_RET(transport, TAG, "transport", NULL);
249 unsigned int length = COAP_MAX_PDU_SIZE;
251 unsigned int msgLength = 0;
252 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
256 unsigned short prevOptNumber = 0;
257 for (coap_list_t *opt = options; opt; opt = opt->next)
259 unsigned short curOptNumber = COAP_OPTION_KEY(*(coap_option *) opt->data);
260 if (prevOptNumber > curOptNumber)
262 OIC_LOG(ERROR, TAG, "option list is wrong");
266 size_t optValueLen = COAP_OPTION_LENGTH(*(coap_option *) opt->data);
267 size_t optLength = coap_get_opt_header_length(curOptNumber - prevOptNumber, optValueLen);
270 OIC_LOG(ERROR, TAG, "Reserved for the Payload marker for the option");
273 msgLength += optLength;
274 prevOptNumber = curOptNumber;
275 OIC_LOG_V(DEBUG, TAG, "curOptNumber[%d], prevOptNumber[%d], optValueLen[%zu], "
276 "optLength[%zu], msgLength[%d]",
277 curOptNumber, prevOptNumber, optValueLen, optLength, msgLength);
281 if (info->payloadSize > 0)
283 msgLength = msgLength + info->payloadSize + PAYLOAD_MARKER;
285 *transport = coap_get_tcp_header_type_from_size(msgLength);
286 length = msgLength + coap_get_tcp_header_length_for_transport(*transport)
292 *transport = coap_udp;
295 coap_pdu_t *pdu = coap_new_pdu(*transport, length);
299 OIC_LOG(ERROR, TAG, "malloc failed");
303 OIC_LOG_V(DEBUG, TAG, "transport type: %d, payload size: %zu",
304 *transport, info->payloadSize);
307 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
309 coap_add_length(pdu, *transport, msgLength);
314 OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId);
316 if (0 == info->messageId)
318 /* initialize message id */
319 prng((uint8_t * ) &message_id, sizeof(message_id));
321 OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id);
325 /* use saved message id */
326 message_id = info->messageId;
328 pdu->hdr->coap_hdr_udp_t.id = message_id;
329 OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->coap_hdr_udp_t.id);
331 pdu->hdr->coap_hdr_udp_t.type = info->type;
334 coap_add_code(pdu, *transport, code);
336 if (info->token && CA_EMPTY != code)
338 uint32_t tokenLength = info->tokenLength;
339 OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength);
340 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength);
342 int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token, *transport);
345 OIC_LOG(ERROR, TAG, "can't add token");
350 if (CA_ADAPTER_GATT_BTLE != endpoint->adapter
352 && !CAIsSupportedCoAPOverTCP(endpoint->adapter)
356 // option list will be added in blockwise-transfer
363 for (coap_list_t *opt = options; opt; opt = opt->next)
365 OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.",
366 COAP_OPTION_DATA(*(coap_option *) opt->data));
368 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length", pdu->length);
369 coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data),
370 COAP_OPTION_LENGTH(*(coap_option *) opt->data),
371 COAP_OPTION_DATA(*(coap_option *) opt->data), *transport);
375 OIC_LOG_V(DEBUG, TAG, "[%d] pdu length after option", pdu->length);
377 if (NULL != info->payload && 0 < info->payloadSize)
379 OIC_LOG(DEBUG, TAG, "payload is added");
380 coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload);
386 CAResult_t CAParseURI(const char *uriInfo, coap_list_t **optlist)
388 VERIFY_NON_NULL(uriInfo, TAG, "uriInfo");
389 VERIFY_NON_NULL(optlist, TAG, "optlist");
391 OIC_LOG_V(DEBUG, TAG, "url : %s", uriInfo);
393 /* split arg into Uri-* options */
395 coap_split_uri((unsigned char *) uriInfo, strlen(uriInfo), &uri);
397 if (uri.port != COAP_DEFAULT_PORT)
399 unsigned char portbuf[CA_PORT_BUFFER_SIZE] = { 0 };
400 int ret = coap_insert(optlist,
401 CACreateNewOptionNode(COAP_OPTION_URI_PORT,
402 coap_encode_var_bytes(portbuf, uri.port),
407 return CA_STATUS_INVALID_PARAM;
411 if (uri.path.s && uri.path.length)
413 CAResult_t ret = CAParseUriPartial(uri.path.s, uri.path.length,
414 COAP_OPTION_URI_PATH, optlist);
415 if (CA_STATUS_OK != ret)
417 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri path)");
422 if (uri.query.s && uri.query.length)
424 CAResult_t ret = CAParseUriPartial(uri.query.s, uri.query.length, COAP_OPTION_URI_QUERY,
426 if (CA_STATUS_OK != ret)
428 OIC_LOG(ERROR, TAG, "CAParseUriPartial failed(uri query)");
436 CAResult_t CAParseUriPartial(const unsigned char *str, size_t length, int target,
437 coap_list_t **optlist)
439 VERIFY_NON_NULL(optlist, TAG, "optlist");
441 if ((target != COAP_OPTION_URI_PATH) && (target != COAP_OPTION_URI_QUERY))
443 // should never occur. Log just in case.
444 OIC_LOG(DEBUG, TAG, "Unexpected URI component.");
445 return CA_NOT_SUPPORTED;
447 else if (str && length)
449 unsigned char uriBuffer[CA_BUFSIZE] = { 0 };
450 unsigned char *pBuf = uriBuffer;
451 size_t buflen = sizeof(uriBuffer);
452 int res = (target == COAP_OPTION_URI_PATH) ? coap_split_path(str, length, pBuf, &buflen) :
453 coap_split_query(str, length, pBuf, &buflen);
460 int ret = coap_insert(optlist,
461 CACreateNewOptionNode(target, COAP_OPT_LENGTH(pBuf),
462 (char *)COAP_OPT_VALUE(pBuf)),
466 return CA_STATUS_INVALID_PARAM;
469 size_t optSize = COAP_OPT_SIZE(pBuf);
470 if ((prevIdx + optSize) < buflen)
479 OIC_LOG_V(ERROR, TAG, "Problem parsing URI : %d for %d", res, target);
480 return CA_STATUS_FAILED;
485 OIC_LOG(ERROR, TAG, "str or length is not available");
486 return CA_STATUS_FAILED;
492 CAResult_t CAParseHeadOption(uint32_t code, const CAInfo_t *info, coap_list_t **optlist)
495 VERIFY_NON_NULL_RET(info, TAG, "info", CA_STATUS_INVALID_PARAM);
497 OIC_LOG_V(DEBUG, TAG, "parse Head Opt: %d", info->numOptions);
501 OIC_LOG(ERROR, TAG, "optlist is null");
502 return CA_STATUS_INVALID_PARAM;
505 for (uint32_t i = 0; i < info->numOptions; i++)
507 if(!(info->options + i))
509 OIC_LOG(ERROR, TAG, "options is not available");
510 return CA_STATUS_FAILED;
513 uint32_t id = (info->options + i)->optionID;
514 if (COAP_OPTION_URI_PATH == id || COAP_OPTION_URI_QUERY == id)
516 OIC_LOG_V(DEBUG, TAG, "not Header Opt: %d", id);
520 OIC_LOG_V(DEBUG, TAG, "Head opt ID: %d", id);
521 OIC_LOG_V(DEBUG, TAG, "Head opt data: %s", (info->options + i)->optionData);
522 OIC_LOG_V(DEBUG, TAG, "Head opt length: %d", (info->options + i)->optionLength);
523 int ret = coap_insert(optlist,
524 CACreateNewOptionNode(id, (info->options + i)->optionLength,
525 (info->options + i)->optionData),
529 return CA_STATUS_INVALID_PARAM;
534 // insert one extra header with the payload format if applicable.
535 if (CA_FORMAT_UNDEFINED != info->payloadFormat)
537 coap_list_t* node = NULL;
538 uint8_t buf[3] = {0};
539 switch (info->payloadFormat)
541 case CA_FORMAT_APPLICATION_CBOR:
542 node = CACreateNewOptionNode(
543 COAP_OPTION_CONTENT_FORMAT,
544 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
548 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->payloadFormat);
552 OIC_LOG(ERROR, TAG, "format option not created");
553 return CA_STATUS_INVALID_PARAM;
555 int ret = coap_insert(optlist, node, CAOrderOpts);
559 OIC_LOG(ERROR, TAG, "format option not inserted in header");
560 return CA_STATUS_INVALID_PARAM;
563 if (CA_FORMAT_UNDEFINED != info->acceptFormat)
565 coap_list_t* node = NULL;
566 uint8_t buf[3] = {0};
567 switch (info->acceptFormat)
569 case CA_FORMAT_APPLICATION_CBOR:
570 node = CACreateNewOptionNode(
572 coap_encode_var_bytes(buf, (uint16_t)COAP_MEDIATYPE_APPLICATION_CBOR),
576 OIC_LOG_V(ERROR, TAG, "format option:[%d] not supported", info->acceptFormat);
580 OIC_LOG(ERROR, TAG, "format option not created");
581 return CA_STATUS_INVALID_PARAM;
583 int ret = coap_insert(optlist, node, CAOrderOpts);
587 OIC_LOG(ERROR, TAG, "format option not inserted in header");
588 return CA_STATUS_INVALID_PARAM;
595 coap_list_t *CACreateNewOptionNode(uint16_t key, uint32_t length, const char *data)
597 VERIFY_NON_NULL_RET(data, TAG, "data", NULL);
599 coap_option *option = coap_malloc(sizeof(coap_option) + length + 1);
602 OIC_LOG(ERROR, TAG, "Out of memory");
605 memset(option, 0, sizeof(coap_option) + length + 1);
607 COAP_OPTION_KEY(*option) = key;
609 coap_option_def_t* def = coap_opt_def(key);
610 if (NULL != def && coap_is_var_bytes(def))
612 if (length > def->max)
614 // make sure we shrink the value so it fits the coap option definition
615 // by truncating the value, disregard the leading bytes.
616 OIC_LOG_V(DEBUG, TAG, "Option [%d] data size [%d] shrunk to [%d]",
617 def->key, length, def->max);
618 data = &(data[length-def->max]);
621 // Shrink the encoding length to a minimum size for coap
622 // options that support variable length encoding.
623 COAP_OPTION_LENGTH(*option) = coap_encode_var_bytes(
624 COAP_OPTION_DATA(*option),
625 coap_decode_var_bytes((unsigned char *)data, length));
629 COAP_OPTION_LENGTH(*option) = length;
630 memcpy(COAP_OPTION_DATA(*option), data, length);
633 /* we can pass NULL here as delete function since option is released automatically */
634 coap_list_t *node = coap_new_listnode(option, NULL);
638 OIC_LOG(ERROR, TAG, "node is NULL");
646 int CAOrderOpts(void *a, void *b)
650 return a < b ? -1 : 1;
653 if (COAP_OPTION_KEY(*(coap_option *) a) < COAP_OPTION_KEY(*(coap_option * ) b))
658 return COAP_OPTION_KEY(*(coap_option *) a) == COAP_OPTION_KEY(*(coap_option * ) b);
661 uint32_t CAGetOptionCount(coap_opt_iterator_t opt_iter)
666 while ((option = coap_option_next(&opt_iter)))
668 if (COAP_OPTION_URI_PATH != opt_iter.type && COAP_OPTION_URI_QUERY != opt_iter.type
669 && COAP_OPTION_BLOCK1 != opt_iter.type && COAP_OPTION_BLOCK2 != opt_iter.type
670 && COAP_OPTION_SIZE1 != opt_iter.type && COAP_OPTION_SIZE2 != opt_iter.type
671 && COAP_OPTION_CONTENT_FORMAT != opt_iter.type
672 && COAP_OPTION_ACCEPT != opt_iter.type)
681 CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, const CAEndpoint_t *endpoint,
682 uint32_t *outCode, CAInfo_t *outInfo)
684 VERIFY_NON_NULL(pdu, TAG, "pdu");
685 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
686 VERIFY_NON_NULL(outCode, TAG, "outCode");
687 VERIFY_NON_NULL(outInfo, TAG, "outInfo");
689 coap_transport_type transport;
691 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
693 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu->hdr)[0] >> 4);
698 transport = coap_udp;
701 coap_opt_iterator_t opt_iter;
702 coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL, transport);
706 (*outCode) = (uint32_t) CA_RESPONSE_CODE(coap_get_code(pdu, transport));
709 // init HeaderOption list
710 uint32_t count = CAGetOptionCount(opt_iter);
711 memset(outInfo, 0, sizeof(*outInfo));
713 outInfo->numOptions = count;
716 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
719 outInfo->type = CA_MSG_NONCONFIRM;
720 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
728 outInfo->type = pdu->hdr->coap_hdr_udp_t.type;
731 outInfo->messageId = pdu->hdr->coap_hdr_udp_t.id;
732 outInfo->payloadFormat = CA_FORMAT_UNDEFINED;
733 outInfo->acceptFormat = CA_FORMAT_UNDEFINED;
738 outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t));
739 if (NULL == outInfo->options)
741 OIC_LOG(ERROR, TAG, "Out of memory");
742 return CA_MEMORY_ALLOC_FAILED;
747 char optionResult[CA_MAX_URI_LENGTH] = {0};
749 uint32_t optionLength = 0;
750 bool isfirstsetflag = false;
751 bool isQueryBeingProcessed = false;
753 while ((option = coap_option_next(&opt_iter)))
755 char buf[COAP_MAX_PDU_SIZE] = {0};
757 CAGetOptionData(opt_iter.type, (uint8_t *)(COAP_OPT_VALUE(option)),
758 COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf));
761 OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf);
762 if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type)
764 if (false == isfirstsetflag)
766 isfirstsetflag = true;
767 optionResult[optionLength] = '/';
769 // Make sure there is enough room in the optionResult buffer
770 if ((optionLength + bufLength) < sizeof(optionResult))
772 memcpy(&optionResult[optionLength], buf, bufLength);
773 optionLength += bufLength;
782 if (COAP_OPTION_URI_PATH == opt_iter.type)
784 // Make sure there is enough room in the optionResult buffer
785 if (optionLength < sizeof(optionResult))
787 optionResult[optionLength] = '/';
795 else if (COAP_OPTION_URI_QUERY == opt_iter.type)
797 if (false == isQueryBeingProcessed)
799 // Make sure there is enough room in the optionResult buffer
800 if (optionLength < sizeof(optionResult))
802 optionResult[optionLength] = '?';
804 isQueryBeingProcessed = true;
813 // Make sure there is enough room in the optionResult buffer
814 if (optionLength < sizeof(optionResult))
816 optionResult[optionLength] = ';';
825 // Make sure there is enough room in the optionResult buffer
826 if ((optionLength + bufLength) < sizeof(optionResult))
828 memcpy(&optionResult[optionLength], buf, bufLength);
829 optionLength += bufLength;
837 else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type
838 || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type)
840 OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type);
842 else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type)
844 if (1 == COAP_OPT_LENGTH(option))
846 outInfo->payloadFormat = CAConvertFormat((uint8_t)buf[0]);
850 outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED;
851 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
852 opt_iter.type, (uint8_t)buf[0]);
855 else if (COAP_OPTION_ACCEPT == opt_iter.type)
857 if (1 == COAP_OPT_LENGTH(option))
859 outInfo->acceptFormat = CAConvertFormat((uint8_t)buf[0]);
863 outInfo->acceptFormat = CA_FORMAT_UNSUPPORTED;
865 OIC_LOG_V(DEBUG, TAG, "option[%d] has an unsupported format [%d]",
866 opt_iter.type, (uint8_t)buf[0]);
872 if (bufLength <= sizeof(outInfo->options[0].optionData))
874 outInfo->options[idx].optionID = opt_iter.type;
875 outInfo->options[idx].optionLength = bufLength;
876 outInfo->options[idx].protocolID = CA_COAP_ID;
877 memcpy(outInfo->options[idx].optionData, buf, bufLength);
885 unsigned char* token = NULL;
886 unsigned int token_length = 0;
887 coap_get_token(pdu->hdr, transport, &token, &token_length);
890 if (token_length > 0)
892 OIC_LOG_V(DEBUG, TAG, "inside token length : %d", token_length);
893 outInfo->token = (char *) OICMalloc(token_length);
894 if (NULL == outInfo->token)
896 OIC_LOG(ERROR, TAG, "Out of memory");
897 OICFree(outInfo->options);
898 return CA_MEMORY_ALLOC_FAILED;
900 memcpy(outInfo->token, token, token_length);
903 outInfo->tokenLength = token_length;
908 if (coap_get_data(pdu, &dataSize, &data))
910 OIC_LOG(DEBUG, TAG, "inside pdu->data");
911 outInfo->payload = (uint8_t *) OICMalloc(dataSize);
912 if (NULL == outInfo->payload)
914 OIC_LOG(ERROR, TAG, "Out of memory");
915 OICFree(outInfo->options);
916 OICFree(outInfo->token);
917 return CA_MEMORY_ALLOC_FAILED;
919 memcpy(outInfo->payload, pdu->data, dataSize);
920 outInfo->payloadSize = dataSize;
923 if (optionResult[0] != '\0')
925 OIC_LOG_V(DEBUG, TAG, "URL length:%zu", strlen(optionResult));
926 outInfo->resourceUri = OICStrdup(optionResult);
927 if (!outInfo->resourceUri)
929 OIC_LOG(ERROR, TAG, "Out of memory");
930 OICFree(outInfo->options);
931 OICFree(outInfo->token);
932 return CA_MEMORY_ALLOC_FAILED;
939 OIC_LOG(ERROR, TAG, "buffer too small");
940 OICFree(outInfo->options);
941 return CA_STATUS_FAILED;
944 CAResult_t CAGetTokenFromPDU(const coap_hdr_t *pdu_hdr, CAInfo_t *outInfo,
945 const CAEndpoint_t *endpoint)
947 VERIFY_NON_NULL(pdu_hdr, TAG, "pdu_hdr");
948 VERIFY_NON_NULL(outInfo, TAG, "outInfo");
949 VERIFY_NON_NULL(endpoint, TAG, "endpoint");
951 coap_transport_type transport;
953 if (CAIsSupportedCoAPOverTCP(endpoint->adapter))
955 transport = coap_get_tcp_header_type_from_initbyte(((unsigned char *)pdu_hdr)[0] >> 4);
960 transport = coap_udp;
963 unsigned char* token = NULL;
964 unsigned int token_length = 0;
965 coap_get_token(pdu_hdr, transport, &token, &token_length);
968 if (token_length > 0)
970 OIC_LOG_V(DEBUG, TAG, "token len:%d", token_length);
971 outInfo->token = (char *) OICMalloc(token_length);
972 if (NULL == outInfo->token)
974 OIC_LOG(ERROR, TAG, "Out of memory");
975 return CA_MEMORY_ALLOC_FAILED;
977 memcpy(outInfo->token, token, token_length);
980 outInfo->tokenLength = token_length;
985 CAResult_t CAGenerateTokenInternal(CAToken_t *token, uint8_t tokenLength)
987 VERIFY_NON_NULL(token, TAG, "token");
989 if ((tokenLength > CA_MAX_TOKEN_LEN) || (0 == tokenLength))
991 OIC_LOG(ERROR, TAG, "invalid token length");
992 return CA_STATUS_INVALID_PARAM;
996 char *temp = (char *) OICCalloc(tokenLength, sizeof(char));
999 OIC_LOG(ERROR, TAG, "Out of memory");
1000 return CA_MEMORY_ALLOC_FAILED;
1003 OCFillRandomMem((uint8_t *)temp, tokenLength);
1008 OIC_LOG_V(DEBUG, TAG, "token len:%d, token:", tokenLength);
1009 OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)(*token), tokenLength);
1011 return CA_STATUS_OK;
1014 void CADestroyTokenInternal(CAToken_t token)
1019 void CADestroyInfo(CAInfo_t *info)
1023 OIC_LOG(DEBUG, TAG, "free options");
1024 OICFree(info->options);
1026 OIC_LOG(DEBUG, TAG, "free token");
1027 OICFree(info->token);
1029 OIC_LOG(DEBUG, TAG, "free payload");
1030 OICFree(info->payload);
1034 uint32_t CAGetOptionData(uint16_t key, const uint8_t *data, uint32_t len,
1035 uint8_t *option, uint32_t buflen)
1039 OIC_LOG(ERROR, TAG, "buflen 0");
1045 OIC_LOG(ERROR, TAG, "option buffer too small");
1049 coap_option_def_t* def = coap_opt_def(key);
1050 if (NULL != def && coap_is_var_bytes(def) && 0 == len)
1052 // A 0 length option is permitted in CoAP but the
1053 // rest or the stack is unaware of variable byte encoding
1054 // should remain that way so a 0 byte of length 1 is inserted.
1058 memcpy(option, data, len);
1065 CAMessageType_t CAGetMessageTypeFromPduBinaryData(const void *pdu, uint32_t size)
1067 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", CA_MSG_NONCONFIRM);
1069 // pdu minimum size is 4 byte.
1070 if (size < CA_PDU_MIN_SIZE)
1072 OIC_LOG(ERROR, TAG, "min size");
1073 return CA_MSG_NONCONFIRM;
1076 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1078 return (CAMessageType_t) hdr->coap_hdr_udp_t.type;
1081 uint16_t CAGetMessageIdFromPduBinaryData(const void *pdu, uint32_t size)
1083 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", 0);
1085 // pdu minimum size is 4 byte.
1086 if (size < CA_PDU_MIN_SIZE)
1088 OIC_LOG(ERROR, TAG, "min size");
1092 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1094 return hdr->coap_hdr_udp_t.id;
1097 CAResponseResult_t CAGetCodeFromPduBinaryData(const void *pdu, uint32_t size)
1099 VERIFY_NON_NULL_RET(pdu, TAG, "pdu", CA_NOT_FOUND);
1101 // pdu minimum size is 4 byte.
1102 if (size < CA_PDU_MIN_SIZE)
1104 OIC_LOG(ERROR, TAG, "min size");
1105 return CA_NOT_FOUND;
1108 coap_hdr_t *hdr = (coap_hdr_t *) pdu;
1110 return (CAResponseResult_t) CA_RESPONSE_CODE(hdr->coap_hdr_udp_t.code);
1113 CAPayloadFormat_t CAConvertFormat(uint8_t format)
1117 case COAP_MEDIATYPE_TEXT_PLAIN:
1118 return CA_FORMAT_TEXT_PLAIN;
1119 case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT:
1120 return CA_FORMAT_APPLICATION_LINK_FORMAT;
1121 case COAP_MEDIATYPE_APPLICATION_XML:
1122 return CA_FORMAT_APPLICATION_XML;
1123 case COAP_MEDIATYPE_APPLICATION_OCTET_STREAM:
1124 return CA_FORMAT_APPLICATION_OCTET_STREAM;
1125 case COAP_MEDIATYPE_APPLICATION_RDF_XML:
1126 return CA_FORMAT_APPLICATION_RDF_XML;
1127 case COAP_MEDIATYPE_APPLICATION_EXI:
1128 return CA_FORMAT_APPLICATION_EXI;
1129 case COAP_MEDIATYPE_APPLICATION_JSON:
1130 return CA_FORMAT_APPLICATION_JSON;
1131 case COAP_MEDIATYPE_APPLICATION_CBOR:
1132 return CA_FORMAT_APPLICATION_CBOR;
1134 return CA_FORMAT_UNSUPPORTED;
1139 bool CAIsSupportedCoAPOverTCP(CATransportAdapter_t adapter)
1141 if (CA_ADAPTER_GATT_BTLE == adapter || CA_ADAPTER_RFCOMM_BTEDR == adapter
1142 || CA_ADAPTER_TCP == adapter)