+
+CAHeaderOption_t* get_option_data(CAInfo_t* requestData)
+{
+ char optionNumBuf[MAX_BUF_LEN] = { 0 };
+ char optionData[MAX_OPT_LEN] = { 0 } ;
+
+ printf("Option Num : ");
+ if (CA_STATUS_OK != get_input_data(optionNumBuf, MAX_BUF_LEN))
+ {
+ return NULL;
+ }
+ int optionNum = atoi(optionNumBuf);
+
+ CAHeaderOption_t * headerOpt = NULL;
+ if (0 >= optionNum)
+ {
+ printf("there is no headerOption!\n");
+ return NULL;
+ }
+ else if (optionNum > MAX_OPT_LEN)
+ {
+ printf("Too many header options!\n");
+ return NULL;
+ }
+ else
+ {
+ headerOpt = (CAHeaderOption_t *)calloc(optionNum, sizeof(CAHeaderOption_t));
+ if (NULL == headerOpt)
+ {
+ printf("Memory allocation failed!\n");
+ return NULL;
+ }
+
+ int i;
+ for (i = 0; i < optionNum; i++)
+ {
+ char getOptionID[MAX_BUF_LEN] = { 0 } ;
+
+ printf("[%d] Option ID : ", i + 1);
+ if (CA_STATUS_OK != get_input_data(getOptionID, MAX_BUF_LEN))
+ {
+ free(headerOpt);
+ return NULL;
+ }
+ int optionID = atoi(getOptionID);
+ headerOpt[i].optionID = optionID;
+
+ printf("[%d] Option Data : ", i + 1);
+ if (CA_STATUS_OK != get_input_data(optionData, MAX_OPT_LEN))
+ {
+ free(headerOpt);
+ return NULL;
+ }
+
+ OICStrcpy(headerOpt[i].optionData, sizeof(headerOpt[i].optionData), optionData);
+
+ headerOpt[i].optionLength = (uint16_t) strlen(optionData);
+ }
+ requestData->numOptions = optionNum;
+ requestData->options = headerOpt;
+ }
+ return headerOpt;
+}
+
+void parsing_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags)
+{
+ if (NULL == uri)
+ {
+ printf("parameter is null\n");
+ return;
+ }
+
+ // parse uri
+ // #1. check prefix
+ uint8_t startIndex = 0;
+ if (strncmp(COAPS_PREFIX, uri, COAPS_PREFIX_LEN) == 0)
+ {
+ printf("uri has '%s' prefix\n", COAPS_PREFIX);
+ startIndex = COAPS_PREFIX_LEN;
+ *flags = CA_SECURE;
+ }
+ else if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0)
+ {
+ printf("uri has '%s' prefix\n", COAP_PREFIX);
+ startIndex = COAP_PREFIX_LEN;
+ *flags = CA_IPV4;
+ }
+ else if (strncmp(COAP_TCP_PREFIX, uri, COAP_TCP_PREFIX_LEN) == 0)
+ {
+ printf("uri has '%s' prefix\n", COAP_TCP_PREFIX);
+ startIndex = COAP_TCP_PREFIX_LEN;
+ *flags = CA_IPV4;
+ }
+
+ // #2. copy uri for parse
+ int32_t len = strlen(uri) - startIndex;
+
+ if (len <= 0)
+ {
+ printf("uri length is 0!\n");
+ return;
+ }
+
+ char *cloneUri = (char *) calloc(len + 1, sizeof(char));
+ if (NULL == cloneUri)
+ {
+ printf("Out of memory\n");
+ return;
+ }
+
+ memcpy(cloneUri, &uri[startIndex], sizeof(char) * len);
+ cloneUri[len] = '\0';
+
+ char *pAddress = cloneUri;
+ printf("pAddress : %s\n", pAddress);
+
+ if (!get_address_set(pAddress, address))
+ {
+ printf("address parse error\n");
+
+ free(cloneUri);
+ return;
+ }
+ free(cloneUri);
+ return;
+}
+
+bool get_address_set(const char *pAddress, addressSet_t* outAddress)
+{
+ if (NULL == pAddress)
+ {
+ printf("parameter is null !\n");
+ return false;
+ }
+
+ size_t len = strlen(pAddress);
+ bool isIp = false;
+ size_t ipLen = 0;
+
+ for (size_t i = 0; i < len; i++)
+ {
+ if (pAddress[i] == '.')
+ {
+ isIp = true;
+ }
+
+ // found port number start index
+ if (isIp && pAddress[i] == ':')
+ {
+ ipLen = i;
+ break;
+ }
+ }
+
+ if (isIp)
+ {
+ if(ipLen && ipLen < sizeof(outAddress->ipAddress))
+ {
+ OICStrcpyPartial(outAddress->ipAddress, sizeof(outAddress->ipAddress),
+ pAddress, ipLen);
+ }
+ else if (!ipLen && len < sizeof(outAddress->ipAddress))
+ {
+ OICStrcpyPartial(outAddress->ipAddress, sizeof(outAddress->ipAddress),
+ pAddress, len);
+ }
+ else
+ {
+ printf("IP Address too long: %zu\n", (ipLen == 0) ? len : ipLen);
+ return false;
+ }
+
+ if (ipLen > 0)
+ {
+ outAddress->port = atoi(pAddress + ipLen + 1);
+ }
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+}
+
+void create_file(CAPayload_t bytes, size_t length)
+{
+ FILE *fp = fopen("sample_output.txt", "wb");
+ if (fp)
+ {
+ fwrite(bytes, 1, length, fp);
+ fclose(fp);
+ }
+}
+
+bool read_file(const char* name, CAPayload_t* bytes, size_t* length)
+{
+ if (NULL == name)
+ {
+ printf("parameter is null\n");
+ return false;
+ }
+
+ FILE* file = NULL;
+ CAPayload_t buffer = NULL;
+ unsigned long fileLen = 0;
+
+ // Open file
+ file = fopen(name, "rb");
+ if (!file)
+ {
+ fprintf(stderr, "Unable to open file, %s\n", name);
+ return false;
+ }
+
+ // Get file length
+ fseek(file, 0, SEEK_END);
+ fileLen = ftell(file);
+ if (-1 == fileLen)
+ {
+ fprintf(stderr, "Failed to get file length\n");
+ fclose(file);
+ return false;
+ }
+ fseek(file, 0, SEEK_SET);
+
+ // Allocate memory
+ buffer = calloc(1, sizeof(uint8_t) * fileLen + 1);
+ if (!buffer)
+ {
+ fprintf(stderr, "Memory error\n");
+ fclose(file);
+ return false;
+ }
+
+ // Read file contents into buffer
+ size_t ret = fread(buffer, fileLen, 1, file);
+ if (ret != 1)
+ {
+ printf("Failed to read data from file, %s\n", name);
+ fclose(file);
+ free(buffer);
+ return false;
+ }
+
+ fclose(file);
+
+ *bytes = buffer;
+ *length = fileLen;
+
+ return true;
+}