Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / linux / sample_main.c
index d7c2354..edfe4ef 100644 (file)
@@ -27,7 +27,6 @@
 
 #include "cacommon.h"
 #include "cainterface.h"
-#include "oic_malloc.h"
 #ifdef __WITH_DTLS__
 #include "ocsecurityconfig.h"
 #endif
 
 int g_received;
 uint16_t g_local_secure_port = SECURE_DEFAULT_PORT;
-CATransportType_t g_selected_nw_type = CA_IPV4;
+CATransportAdapter_t g_selected_nw_type = CA_ADAPTER_IP;
 const char *MESSAGE_TYPE[] = {"CON", "NON", "ACK", "RESET"};
 
+typedef struct
+{
+    char ipAddress[CA_IPADDR_SIZE];
+    uint16_t port;
+} addressSet_t;
+
 char get_menu();
 void process();
 CAResult_t get_network_type();
@@ -66,29 +71,36 @@ CAResult_t get_input_data(char *buf, int32_t length);
 
 void start_listening_server();
 void start_discovery_server();
-void find_resource();
 void send_request();
 void send_request_all();
-void advertise_resource();
 void send_notification();
 void select_network();
 void unselect_network();
 void handle_request_response();
-void find_fixed_resource();
 void get_network_info();
 void send_secure_request();
 
-void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
-void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo);
-void send_response(const CARemoteEndpoint_t *endpoint, const CAInfo_t *info);
+void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo);
+void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo);
+void error_handler(const CAEndpoint_t *object, const CAErrorInfo_t* errorInfo);
+void send_response(const CAEndpoint_t *endpoint, const CAInfo_t *info);
 void get_resource_uri(char *URI, char *resourceURI, int length);
 int get_secure_information(CAPayload_t payLoad);
+int get_address_set(const char *pAddress, addressSet_t* outAddress);
+void parsing_coap_uri(const char* uri, addressSet_t* address, CATransportFlags_t *flags);
+CAHeaderOption_t* get_option_data(CAInfo_t* requestData);
 
 static CAToken_t g_last_request_token = NULL;
-static const char SECURE_COAPS_PREFIX[] = "coaps://";
+
+static const char COAP_PREFIX[] =  "coap://";
+static const char COAPS_PREFIX[] = "coaps://";
+static const uint16_t COAP_PREFIX_LEN = sizeof(COAP_PREFIX) - 1;
+static const uint16_t COAPS_PREFIX_LEN = sizeof(COAPS_PREFIX) - 1;
+
 static const char SECURE_INFO_DATA[] =
                                     "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
-                                     "\"if\":[\"oic.if.baseline\"],\"obs\":1,\"sec\":1,\"port\":%d}}]}";
+                                     "\"if\":[\"oic.if.baseline\"],\"obs\":1,\"sec\":1,\"port\":"
+                                     "%d}}]}";
 static const char NORMAL_INFO_DATA[] =
                                     "{\"oc\":[{\"href\":\"%s\",\"prop\":{\"rt\":[\"core.led\"],"
                                      "\"if\":[\"oic.if.baseline\"],\"obs\":1}}]}";
@@ -222,7 +234,7 @@ int main()
 #endif
 
     // set handler.
-    CARegisterHandler(request_handler, response_handler);
+    CARegisterHandler(request_handler, response_handler, error_handler);
 
     process();
 
@@ -269,21 +281,11 @@ void process()
                 start_discovery_server();
                 break;
 
-            case 'f': // find resource
-            case 'F':
-                find_resource();
-                break;
-
             case 'r': // send request
             case 'R':
                 send_request();
                 break;
 
-            case 'a': // advertise resource
-            case 'A':
-                advertise_resource();
-                break;
-
             case 'b': // send notification
             case 'B':
                 send_notification();
@@ -304,21 +306,6 @@ void process()
                 handle_request_response();
                 break;
 
-            case 'y':
-            case 'Y':
-                while (1)
-                {
-                    g_received = 0;
-                    find_fixed_resource();
-                    while (g_received == 0)
-                    {
-                        sleep(1);
-                        handle_request_response();
-
-                    }
-                }
-                break;
-
             case 'w':
             case 'W':
                 g_received = 0;
@@ -383,80 +370,6 @@ void start_discovery_server()
     }
 }
 
-void find_fixed_resource()
-{
-    // create token
-    CAToken_t token = NULL;
-    uint8_t tokenLength = CA_MAX_TOKEN_LEN;
-
-    CAResult_t res = CAGenerateToken(&token, tokenLength);
-    if ((CA_STATUS_OK != res) || (!token))
-    {
-        printf("Token generate error!!");
-        return;
-    }
-    printf("Generated token %s\n", token);
-
-    char buf[MAX_BUF_LEN] = { 0 };
-    strcpy(buf, "/a/light");
-
-    res = CAFindResource(buf, token, tokenLength);
-    if (CA_STATUS_OK != res)
-    {
-        printf("Find resource error : %d\n", res);
-    }
-    else
-    {
-        printf("Find resource to %s URI\n", buf);
-    }
-
-    // delete token
-    CADestroyToken(token);
-
-    printf("=============================================\n");
-}
-
-void find_resource()
-{
-    printf("\n=============================================\n");
-    printf("ex) /a/light\n");
-    printf("reference uri : ");
-
-    char buf[MAX_BUF_LEN] = { 0 };
-    if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
-    {
-        return;
-    }
-
-    // create token
-    CAToken_t token = NULL;
-    uint8_t tokenLength = CA_MAX_TOKEN_LEN;
-
-    CAResult_t res = CAGenerateToken(&token, tokenLength);
-    if ((CA_STATUS_OK != res) || (!token))
-    {
-        printf("Token generate error!!\n");
-        return;
-    }
-
-    printf("Generated token %s\n", token);
-
-    res = CAFindResource(buf, token, tokenLength);
-    if (CA_STATUS_OK != res)
-    {
-        printf("Find resource error : %d\n", res);
-        CADestroyToken(token);
-    }
-    else
-    {
-        printf("Find resource to %s URI\n", buf);
-        CADestroyToken(g_last_request_token);
-        g_last_request_token = token;
-    }
-
-    printf("=============================================\n");
-}
-
 void send_request()
 {
     CAResult_t res = get_network_type();
@@ -497,8 +410,15 @@ void send_request()
     }
 
     // create remote endpoint
-    CARemoteEndpoint_t *endpoint = NULL;
-    res = CACreateRemoteEndpoint(uri, g_selected_nw_type, &endpoint);
+    CAEndpoint_t *endpoint = NULL;
+    CATransportFlags_t flags;
+
+    printf("URI : %s\n", uri);
+    addressSet_t address = {};
+    parsing_coap_uri(uri, &address, &flags);
+
+    res = CACreateEndpoint(flags, g_selected_nw_type,
+                           (const char*)address.ipAddress, address.port, &endpoint);
     if (CA_STATUS_OK != res || !endpoint)
     {
         printf("Failed to create remote endpoint, error code : %d\n", res);
@@ -512,7 +432,7 @@ void send_request()
     char buf[MAX_BUF_LEN] = { 0 };
     if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
     {
-        CADestroyRemoteEndpoint(endpoint);
+        CADestroyEndpoint(endpoint);
         return;
     }
 
@@ -526,22 +446,22 @@ void send_request()
     if ((CA_STATUS_OK != res) || (!token))
     {
         printf("Token generate error, error code : %d\n", res);
-        CADestroyRemoteEndpoint(endpoint);
+        CADestroyEndpoint(endpoint);
         return;
     }
 
     printf("Generated token %s\n", token);
 
     // extract relative resourceuri from give uri
-    printf("URI : %s\n", uri);
-
     char resourceURI[RESOURCE_URI_LENGTH + 1] = {0};
     get_resource_uri(uri, resourceURI, RESOURCE_URI_LENGTH);
+    printf("resourceURI : %s\n", resourceURI);
 
     // create request data
     CAInfo_t requestData = { 0 };
     requestData.token = token;
     requestData.tokenLength = tokenLength;
+    requestData.resourceUri = (CAURI_t)resourceURI;
 
     if (strcmp(secureRequest, "1") == 0)
     {
@@ -550,11 +470,13 @@ void send_request()
         if (NULL == requestData.payload)
         {
             printf("Memory allocation fail\n");
-            CADestroyRemoteEndpoint(endpoint);
+            CADestroyEndpoint(endpoint);
             CADestroyToken(token);
             return;
         }
-        snprintf(requestData.payload, length, SECURE_INFO_DATA, resourceURI, g_local_secure_port);
+        snprintf((char *) requestData.payload, length, SECURE_INFO_DATA,
+                 (const char *) resourceURI, g_local_secure_port);
+        requestData.payloadSize = length;
     }
     else
     {
@@ -563,17 +485,21 @@ void send_request()
         if (NULL == requestData.payload)
         {
             printf("Memory allocation fail\n");
-            CADestroyRemoteEndpoint(endpoint);
+            CADestroyEndpoint(endpoint);
             CADestroyToken(token);
             return;
         }
-        snprintf(requestData.payload, length, NORMAL_INFO_DATA, resourceURI);
+        snprintf((char *) requestData.payload, length, NORMAL_INFO_DATA,
+                 (const char *) resourceURI);
+        requestData.payloadSize = length;
     }
     requestData.type = msgType;
+    CAHeaderOption_t* headerOpt = get_option_data(&requestData);
 
     CARequestInfo_t requestInfo = { 0 };
     requestInfo.method = CA_GET;
     requestInfo.info = requestData;
+    requestInfo.isMulticast = false;
 
     // send request
     res = CASendRequest(endpoint, &requestInfo);
@@ -582,13 +508,17 @@ void send_request()
         printf("Could not send request : %d\n", res);
     }
 
+    if (headerOpt)
+    {
+        free(headerOpt);
+    }
+
     //destroy token
     CADestroyToken(token);
     // destroy remote endpoint
-    CADestroyRemoteEndpoint(endpoint);
+    CADestroyEndpoint(endpoint);
     free(requestData.payload);
 
-
     printf("=============================================\n");
 }
 
@@ -604,11 +534,11 @@ void send_secure_request()
     {
         return;
     }
-    snprintf(uri, MAX_BUF_LEN, "%s%s:5684/a/light", SECURE_COAPS_PREFIX, ipv4addr);
+    snprintf(uri, MAX_BUF_LEN, "%s%s:5684/a/light", COAPS_PREFIX, ipv4addr);
 
     // create remote endpoint
-    CARemoteEndpoint_t *endpoint = NULL;
-    CAResult_t res = CACreateRemoteEndpoint(uri, CA_IPV4, &endpoint);
+    CAEndpoint_t *endpoint = NULL;
+    CAResult_t res = CACreateEndpoint(0, CA_ADAPTER_IP, ipv4addr, SECURE_DEFAULT_PORT, &endpoint);
     if (CA_STATUS_OK != res)
     {
         printf("Failed to create remote endpoint, error code: %d\n", res);
@@ -638,6 +568,7 @@ void send_secure_request()
     CARequestInfo_t requestInfo = { 0 };
     requestInfo.method = CA_GET;
     requestInfo.info = requestData;
+    requestInfo.isMulticast = false;
 
     // send request
     CASendRequest(endpoint, &requestInfo);
@@ -645,7 +576,7 @@ void send_secure_request()
 exit:
     // cleanup
     CADestroyToken(token);
-    CADestroyRemoteEndpoint(endpoint);
+    CADestroyEndpoint(endpoint);
     printf("=============================================\n");
 }
 
@@ -662,30 +593,29 @@ void send_request_all()
     printf("ex) /a/light\n");
     printf("resource uri : ");
 
-    char buf[MAX_BUF_LEN] = { 0 };
-    if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
+    char resourceURI[MAX_BUF_LEN] = { 0 };
+    if (CA_STATUS_OK != get_input_data(resourceURI, MAX_BUF_LEN))
     {
         return;
     }
 
     // create remote endpoint
-    CARemoteEndpoint_t *endpoint = NULL;
-    res = CACreateRemoteEndpoint(buf, g_selected_nw_type, &endpoint);
+    CAEndpoint_t *endpoint = NULL;
+    res = CACreateEndpoint(0, g_selected_nw_type, NULL, 0, &endpoint);
     if (CA_STATUS_OK != res)
     {
         printf("Create remote endpoint error, error code: %d\n", res);
         return;
     }
 
-    CAGroupEndpoint_t *group = (CAGroupEndpoint_t *) malloc(sizeof(CAGroupEndpoint_t));
+    CAEndpoint_t *group = (CAEndpoint_t *) malloc(sizeof(CAEndpoint_t));
     if (NULL == group)
     {
         printf("Memory allocation failed!\n");
-        CADestroyRemoteEndpoint(endpoint);
+        CADestroyEndpoint(endpoint);
         return;
     }
-    group->transportType = endpoint->transportType;
-    group->resourceUri = endpoint->resourceUri;
+    group->adapter = endpoint->adapter;
 
     // create token
     CAToken_t token = NULL;
@@ -695,7 +625,7 @@ void send_request_all()
     if ((CA_STATUS_OK != res) || (!token))
     {
         printf("Token generate error!!\n");
-        CADestroyRemoteEndpoint(endpoint);
+        CADestroyEndpoint(endpoint);
         free(group);
         return;
     }
@@ -705,15 +635,20 @@ void send_request_all()
     CAInfo_t requestData = { 0 };
     requestData.token = token;
     requestData.tokenLength = tokenLength;
-    requestData.payload = "Temp Json Payload";
+    requestData.payload = (CAPayload_t) "TempJsonPayload";
+    requestData.payloadSize = strlen((const char *) requestData.payload);
     requestData.type = CA_MSG_NONCONFIRM;
+    requestData.resourceUri = (CAURI_t)resourceURI;
+
+    CAHeaderOption_t* headerOpt = get_option_data(&requestData);
 
     CARequestInfo_t requestInfo = { 0 };
     requestInfo.method = CA_GET;
     requestInfo.info = requestData;
+    requestInfo.isMulticast = true;
 
     // send request
-    res = CASendRequestToAll(group, &requestInfo);
+    res = CASendRequest(group, &requestInfo);
     if (CA_STATUS_OK != res)
     {
         printf("Could not send request to all\n");
@@ -725,98 +660,16 @@ void send_request_all()
         g_last_request_token = token;
     }
 
-    // destroy remote endpoint
-    CADestroyRemoteEndpoint(endpoint);
-    free(group);
-
-    printf("=============================================\n");
-}
-
-void advertise_resource()
-{
-    printf("\n=============================================\n");
-    printf("uri : ");
-
-    char buf[MAX_BUF_LEN] = { 0 };
-    if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
+    if (headerOpt)
     {
-        return;
-    }
-
-    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;
-    }
-    int optionNum = atoi(optionNumBuf);
-
-    CAHeaderOption_t * headerOpt = (CAHeaderOption_t *)
-            calloc(1, optionNum * sizeof(CAHeaderOption_t));
-    if (NULL == headerOpt)
-    {
-        printf("Memory allocation failed!\n");
-        return;
-    }
-
-    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;
-        }
-        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;
-        }
-
-        memcpy(headerOpt[i].optionData, optionData, strlen(optionData));
-        printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData);
-
-        headerOpt[i].optionLength = (uint16_t) strlen(optionData);
-    }
-    printf("\n=============================================\n");
-
-    // create token
-    CAToken_t token = NULL;
-    uint8_t tokenLength = CA_MAX_TOKEN_LEN;
-
-    CAResult_t res = CAGenerateToken(&token, tokenLength);
-    if ((CA_STATUS_OK != res) || (!token))
-    {
-        printf("Token generate error!!\n");
         free(headerOpt);
-        return;
     }
 
-    printf("Generated token %s\n", token);
-
-    res = CAAdvertiseResource(buf, token, tokenLength, headerOpt, (uint8_t) optionNum);
-    if (CA_STATUS_OK != res)
-    {
-        printf("Could not start advertise resource\n");
-        CADestroyToken(token);
-    }
-    else
-    {
-        CADestroyToken(g_last_request_token);
-        g_last_request_token = token;
-    }
+    // destroy remote endpoint
+    CADestroyEndpoint(endpoint);
+    free(group);
 
-    free(headerOpt);
+    printf("=============================================\n");
 }
 
 void send_notification()
@@ -829,12 +682,12 @@ void send_notification()
 
     printf("\n=============================================\n");
     printf("Enter the URI like below....\n");
-    printf("10.11.12.13:4545/resource_uri ( for IP )\n");
-    printf("10:11:12:13:45:45/resource_uri ( for BT )\n");
+    printf("coap://10.11.12.13:4545/resource_uri ( for IP )\n");
+    printf("coap://10:11:12:13:45:45/resource_uri ( for BT )\n");
     printf("uri : ");
 
-    char buf[MAX_BUF_LEN] = { 0 };
-    if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
+    char uri[MAX_BUF_LEN] = { 0 };
+    if (CA_STATUS_OK != get_input_data(uri, MAX_BUF_LEN))
     {
         return;
     }
@@ -855,9 +708,13 @@ void send_notification()
 
     int messageType = messageTypeBuf[0] - '0';
 
+    CATransportFlags_t flags;
+    addressSet_t address = {};
+    parsing_coap_uri(uri, &address, &flags);
+
     // create remote endpoint
-    CARemoteEndpoint_t *endpoint = NULL;
-    res = CACreateRemoteEndpoint(buf, g_selected_nw_type, &endpoint);
+    CAEndpoint_t *endpoint = NULL;
+    res = CACreateEndpoint(flags, g_selected_nw_type, address.ipAddress, address.port, &endpoint);
     if (CA_STATUS_OK != res)
     {
         printf("Create remote endpoint error, error code: %d\n", res);
@@ -872,7 +729,7 @@ void send_notification()
     if ((CA_STATUS_OK != res) || (!token))
     {
         printf("Token generate error!!\n");
-        CADestroyRemoteEndpoint(endpoint);
+        CADestroyEndpoint(endpoint);
         return;
     }
 
@@ -880,11 +737,14 @@ void send_notification()
 
     CAInfo_t respondData = { 0 };
     respondData.token = token;
-    respondData.payload = "Temp Notification Data";
+    respondData.tokenLength = tokenLength;
+    respondData.payload = (CAPayload_t) "TempNotificationData";
+    respondData.payloadSize = strlen((const char *) respondData.payload);
     respondData.type = messageType;
+    respondData.resourceUri = (CAURI_t)uri;
 
     CAResponseInfo_t responseInfo = { 0 };
-    responseInfo.result = CA_SUCCESS;
+    responseInfo.result = CA_CONTENT;
     responseInfo.info = respondData;
 
     // send request
@@ -901,7 +761,7 @@ void send_notification()
     // destroy token
     CADestroyToken(token);
     // destroy remote endpoint
-    CADestroyRemoteEndpoint(endpoint);
+    CADestroyEndpoint(endpoint);
 
     printf("\n=============================================\n");
 }
@@ -910,9 +770,9 @@ void select_network()
 {
     printf("\n=============================================\n");
     printf("\tselect network\n");
-    printf("IPv4 : 0\n");
-    printf("EDR  : 2\n");
-    printf("LE   : 3\n");
+    printf("IP     : 0\n");
+    printf("GATT   : 1\n");
+    printf("RFCOMM : 2\n");
     printf("select : ");
 
     char buf[MAX_BUF_LEN] = { 0 };
@@ -946,9 +806,9 @@ void unselect_network()
 {
     printf("\n=============================================\n");
     printf("\tunselect enabled network\n");
-    printf("IPv4 : 0\n");
-    printf("EDR : 2\n");
-    printf("LE : 3\n");
+    printf("IP     : 0\n");
+    printf("GATT   : 1\n");
+    printf("RFCOMM : 2\n");
     printf("select : ");
 
     char buf[MAX_BUF_LEN] = { 0 };
@@ -984,16 +844,13 @@ char get_menu()
     printf("\t\tMenu\n");
     printf("\ts : start server\n");
     printf("\tc : start client\n");
-    printf("\tf : find resource\n");
     printf("\tr : send request\n");
     printf("\tt : send request to all\n");
-    printf("\ta : advertise resource\n");
     printf("\tb : send notification\n");
     printf("\tn : select network\n");
     printf("\tx : unselect network\n");
     printf("\tg : get network information\n");
     printf("\th : handle request response\n");
-    printf("\ty : run static client\n");
     printf("\tz : run static server\n");
     printf("\tw : send secure request\n");
     printf("\tq : quit\n");
@@ -1026,7 +883,7 @@ void handle_request_response()
 
 void get_network_info()
 {
-    CALocalConnectivity_t *tempInfo = NULL;
+    CAEndpoint_t *tempInfo = NULL;
     uint32_t tempSize = 0;
 
     CAResult_t res = CAGetNetworkInformation(&tempInfo, &tempSize);
@@ -1043,25 +900,25 @@ void get_network_info()
     int index;
     for (index = 0; index < tempSize; index++)
     {
-        printf("Type: %d\n", tempInfo[index].type);
-        if (CA_IPV4 == tempInfo[index].type)
+        printf("Type: %d\n", tempInfo[index].adapter);
+        if (CA_ADAPTER_IP == tempInfo[index].adapter)
         {
-            printf("Address: %s\n", tempInfo[index].addressInfo.IP.ipAddress);
-            printf("Port: %d\n", tempInfo[index].addressInfo.IP.port);
+            printf("Address: %s\n", tempInfo[index].addr);
+            printf("Port: %d\n", tempInfo[index].port);
         }
-        else if (CA_EDR == tempInfo[index].type)
+        else if (CA_ADAPTER_RFCOMM_BTEDR == tempInfo[index].adapter)
         {
-            printf("Address: %s\n", tempInfo[index].addressInfo.BT.btMacAddress);
+            printf("Address: %s\n", tempInfo[index].addr);
         }
-        else if (CA_LE == tempInfo[index].type)
+        else if (CA_ADAPTER_GATT_BTLE == tempInfo[index].adapter)
         {
-            printf("Address: %s\n", tempInfo[index].addressInfo.LE.leMacAddress);
+            printf("Address: %s\n", tempInfo[index].addr);
         }
-        printf("Secured: %s\n\n", tempInfo[index].isSecured ? "true" : "false");
+        printf("Secured: %s\n\n", (tempInfo[index].flags & CA_SECURE) ? "true" : "false");
 
-        if (tempInfo[index].isSecured)
+        if (tempInfo[index].flags & CA_SECURE)
         {
-            g_local_secure_port = tempInfo[index].addressInfo.IP.port;
+            g_local_secure_port = tempInfo[index].port;
             printf("Secured: in global %d\n\n", g_local_secure_port);
         }
     }
@@ -1070,7 +927,7 @@ void get_network_info()
     printf("##############################################################");
 }
 
-void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
+void request_handler(const CAEndpoint_t *object, const CARequestInfo_t *requestInfo)
 {
     if (NULL == object || NULL == requestInfo)
     {
@@ -1087,19 +944,18 @@ void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *re
     }
 
     printf("##########received request from remote device #############\n");
-    printf("Uri: %s\n", object->resourceUri);
-    if (CA_IPV4 == object->transportType)
+    if (CA_ADAPTER_IP == object->adapter)
     {
-        printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
-               object->addressInfo.IP.port, object->isSecured);
+        printf("Remote Address: %s Port: %d secured:%d\n", object->addr,
+               object->port, object->flags & CA_SECURE);
     }
-    else if (CA_EDR == object->transportType)
+    else if (CA_ADAPTER_RFCOMM_BTEDR == object->adapter)
     {
-        printf("Remote Address: %s \n", object->addressInfo.BT.btMacAddress);
+        printf("Remote Address: %s \n", object->addr);
     }
-    else if (CA_LE == object->transportType)
+    else if (CA_ADAPTER_GATT_BTLE == object->adapter)
     {
-        printf("Remote Address: %s \n", object->addressInfo.LE.leMacAddress);
+        printf("Remote Address: %s \n", object->addr);
     }
     printf("Data: %s\n", requestInfo->info.payload);
     printf("Message type: %s\n", MESSAGE_TYPE[requestInfo->info.type]);
@@ -1120,39 +976,22 @@ void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *re
 
     //Check if this has secure communication information
     if (requestInfo->info.payload &&
-            (CA_IPV4 == object->transportType))
+            (CA_ADAPTER_IP == object->adapter))
     {
         int securePort = get_secure_information(requestInfo->info.payload);
         if (0 < securePort) //Set the remote endpoint secure details and send response
         {
             printf("This is secure resource...\n");
 
-            //length of "coaps://"
-            size_t length = sizeof(SECURE_COAPS_PREFIX) - 1;
-
-            // length of "ipaddress:port"
-            length += strlen(object->addressInfo.IP.ipAddress) + PORT_LENGTH;
-            length += strlen(object->resourceUri) + 1;
-
-            char *uri = calloc(1, sizeof(char) * length);
-            if (!uri)
-            {
-                printf("Failed to create new uri\n");
-                return;
-            }
-            sprintf(uri, "%s%s:%d/%s", SECURE_COAPS_PREFIX, object->addressInfo.IP.ipAddress,
-                    object->addressInfo.IP.port, object->resourceUri);
-
-            CARemoteEndpoint_t *endpoint = NULL;
-            if (CA_STATUS_OK != CACreateRemoteEndpoint(uri, object->transportType, &endpoint))
+            CAEndpoint_t *endpoint = NULL;
+            if (CA_STATUS_OK != CACreateEndpoint(0, object->adapter, object->addr,
+                                                 object->port, &endpoint))
             {
                 printf("Failed to create duplicate of remote endpoint!\n");
                 return;
             }
-            endpoint->isSecured = true;
+            endpoint->flags = CA_SECURE;
             object = endpoint;
-
-            free(uri);
         }
     }
 
@@ -1162,22 +1001,21 @@ void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *re
     g_received = 1;
 }
 
-void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo)
+void response_handler(const CAEndpoint_t *object, const CAResponseInfo_t *responseInfo)
 {
     printf("##########Received response from remote device #############\n");
-    printf("Uri: %s\n", object->resourceUri);
-    if (CA_IPV4 == object->transportType)
+    if (CA_ADAPTER_IP == object->adapter)
     {
-        printf("Remote Address: %s Port: %d secured:%d\n", object->addressInfo.IP.ipAddress,
-               object->addressInfo.IP.port, object->isSecured);
+        printf("Remote Address: %s Port: %d secured:%d\n", object->addr,
+               object->port, object->flags & CA_SECURE);
     }
-    else if (CA_EDR == object->transportType)
+    else if (CA_ADAPTER_RFCOMM_BTEDR == object->adapter)
     {
-        printf("Remote Address: %s \n", object->addressInfo.BT.btMacAddress);
+        printf("Remote Address: %s \n", object->addr);
     }
-    else if (CA_LE == object->transportType)
+    else if (CA_ADAPTER_GATT_BTLE == object->adapter)
     {
-        printf("Remote Address: %s \n", object->addressInfo.LE.leMacAddress);
+        printf("Remote Address: %s \n", object->addr);
     }
     printf("response result : %d\n", responseInfo->result);
     printf("Data: %s\n", responseInfo->info.payload);
@@ -1209,7 +1047,48 @@ void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *
     }
 }
 
-void send_response(const CARemoteEndpoint_t *endpoint, const CAInfo_t *info)
+void error_handler(const CAEndpoint_t *rep, const CAErrorInfo_t* errorInfo)
+{
+    printf("+++++++++++++++++++++++++++++++++++ErrorInfo+++++++++++++++++++++++++++++++++++\n");
+
+    if(errorInfo)
+    {
+        const CAInfo_t *info = &errorInfo->info;
+        printf("Error Handler, ErrorInfo :\n");
+        printf("Error Handler result    : %d\n", errorInfo->result);
+        printf("Error Handler token     : %s\n", info->token);
+        printf("Error Handler messageId : %d\n", (uint16_t) info->messageId);
+        printf("Error Handler type      : %d\n", info->type);
+        printf("Error Handler resourceUri : %s\n", info->resourceUri);
+        printf("Error Handler payload   : %s\n", info->payload);
+
+        if(CA_ADAPTER_NOT_ENABLED == errorInfo->result)
+        {
+            printf("CA_ADAPTER_NOT_ENABLED, enable the adapter\n");
+        }
+        else if(CA_SEND_FAILED == errorInfo->result)
+        {
+            printf("CA_SEND_FAILED, unable to send the message, check parameters\n");
+        }
+        else if(CA_MEMORY_ALLOC_FAILED == errorInfo->result)
+        {
+            printf("CA_MEMORY_ALLOC_FAILED, insufficient memory\n");
+        }
+        else if(CA_SOCKET_OPERATION_FAILED == errorInfo->result)
+        {
+            printf("CA_SOCKET_OPERATION_FAILED, socket operation failed\n");
+        }
+        else if(CA_STATUS_FAILED == errorInfo->result)
+        {
+            printf("CA_STATUS_FAILED, message could not be delivered, internal error\n");
+        }
+    }
+    printf("++++++++++++++++++++++++++++++++End of ErrorInfo++++++++++++++++++++++++++++++++\n");
+
+    return;
+}
+
+void send_response(const CAEndpoint_t *endpoint, const CAInfo_t *info)
 {
     printf("entering send_response\n");
 
@@ -1244,6 +1123,9 @@ void send_response(const CARemoteEndpoint_t *endpoint, const CAInfo_t *info)
         printf("SUCCESS                  : 200\n");
         printf("CREATED                  : 201\n");
         printf("DELETED                  : 202\n");
+        printf("VALID                    : 203\n");
+        printf("CHANGED                  : 204\n");
+        printf("CONTENT                  : 205\n");
         printf("BAD_REQ                  : 400\n");
         printf("BAD_OPT                  : 402\n");
         printf("NOT_FOUND                : 404\n");
@@ -1260,38 +1142,42 @@ void send_response(const CARemoteEndpoint_t *endpoint, const CAInfo_t *info)
     CAInfo_t responseData = { 0 };
     responseData.type = messageType;
     responseData.messageId = (info != NULL) ? info->messageId : 0;
+    responseData.resourceUri = (info != NULL) ? info->resourceUri : 0;
 
     if(CA_MSG_RESET != messageType)
     {
         responseData.token = (info != NULL) ? info->token : NULL;
         responseData.tokenLength = (info != NULL) ? info->tokenLength : 0;
 
-        if (endpoint->isSecured)
+        if (endpoint->flags & CA_SECURE)
         {
             printf("Sending response on secure communication\n");
 
-            uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(endpoint->resourceUri);
+            uint32_t length = sizeof(SECURE_INFO_DATA) + strlen(responseData.resourceUri);
             responseData.payload = (CAPayload_t) calloc(length,  sizeof(char));
             if (NULL == responseData.payload)
             {
                 printf("Memory allocation fail\n");
                 return;
             }
-            snprintf(responseData.payload, length, SECURE_INFO_DATA, endpoint->resourceUri,
-                     g_local_secure_port);
+            snprintf((char *) responseData.payload, length, SECURE_INFO_DATA,
+                     (const char *) responseData.resourceUri, g_local_secure_port);
+            responseData.payloadSize = length;
         }
         else
         {
             printf("Sending response on non-secure communication\n");
 
-            uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(endpoint->resourceUri);
+            uint32_t length = sizeof(NORMAL_INFO_DATA) + strlen(responseData.resourceUri);
             responseData.payload = (CAPayload_t) calloc(length, sizeof(char));
             if (NULL == responseData.payload)
             {
                 printf("Memory allocation fail\n");
                 return;
             }
-            snprintf(responseData.payload, length, NORMAL_INFO_DATA, endpoint->resourceUri);
+            snprintf((char *) responseData.payload, length, NORMAL_INFO_DATA,
+                     (const char *) responseData.resourceUri);
+            responseData.payloadSize = length;
         }
     }
 
@@ -1310,6 +1196,11 @@ void send_response(const CARemoteEndpoint_t *endpoint, const CAInfo_t *info)
         printf("Send response success\n");
     }
 
+    if (responseData.payload)
+    {
+        free(responseData.payload);
+    }
+
     printf("=============================================\n");
 }
 
@@ -1324,13 +1215,13 @@ int get_secure_information(CAPayload_t payLoad)
     }
 
     char *subString = NULL;
-    if (NULL == (subString = strstr(payLoad, "\"sec\":1")))
+    if (NULL == (subString = strstr((const char *) payLoad, "\"sec\":1")))
     {
         printf("This is not secure resource\n");
         return -1;
     }
 
-    if (NULL == (subString = strstr(payLoad, "\"port\":")))
+    if (NULL == (subString = strstr((const char *) payLoad, "\"port\":")))
     {
         printf("This secure resource does not have port information\n");
         return -1;
@@ -1350,7 +1241,7 @@ int get_secure_information(CAPayload_t payLoad)
         return -1;
     }
 
-    char portStr[4] = {0};
+    char portStr[6] = {0};
     memcpy(portStr, startPos + 1, (endPos - 1) - startPos);
 
     printf("secured port is: %s\n", portStr);
@@ -1392,9 +1283,9 @@ CAResult_t get_network_type()
 
     printf("\n=============================================\n");
     printf("\tselect network type\n");
-    printf("IPv4 : 0\n");
-    printf("BT : 2\n");
-    printf("LE : 3\n");
+    printf("IP     : 0\n");
+    printf("GATT   : 1\n");
+    printf("RFCOMM : 2\n");
     printf("select : ");
 
     if (CA_STATUS_OK != get_input_data(buf, MAX_BUF_LEN))
@@ -1406,29 +1297,23 @@ CAResult_t get_network_type()
 
     number = (number < 0 || number > 3) ? 0 : 1 << number;
 
-    if (!(number & 0xf))
-    {
-        return CA_NOT_SUPPORTED;
-    }
-    if (number & CA_IPV4)
+    if (number == 1)
     {
-        g_selected_nw_type = CA_IPV4;
+        g_selected_nw_type = CA_ADAPTER_IP;
         return CA_STATUS_OK;
     }
-    if (number & CA_EDR)
+    if (number == 2)
     {
-        g_selected_nw_type = CA_EDR;
+        g_selected_nw_type = CA_ADAPTER_GATT_BTLE;
         return CA_STATUS_OK;
     }
-    if (number & CA_LE)
+    if (number == 3)
     {
-        g_selected_nw_type = CA_LE;
+        g_selected_nw_type = CA_ADAPTER_RFCOMM_BTEDR;
         return CA_STATUS_OK;
     }
 
-    printf("\n=============================================\n");
-
-    return CA_STATUS_FAILED;
+    return CA_NOT_SUPPORTED;
 }
 
 CAResult_t get_input_data(char *buf, int32_t length)
@@ -1447,3 +1332,173 @@ CAResult_t get_input_data(char *buf, int32_t length)
 
     return CA_STATUS_OK;
 }
+
+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
+    {
+        headerOpt = (CAHeaderOption_t *)calloc(1, 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;
+            }
+
+            memcpy(headerOpt[i].optionData, optionData, strlen(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_DEFAULT_FLAGS;
+    }
+
+    // #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);
+
+    int res = get_address_set(pAddress, address);
+    if (res == -1)
+    {
+        printf("address parse error\n");
+
+        free(cloneUri);
+        return;
+    }
+    free(cloneUri);
+    return;
+}
+
+int get_address_set(const char *pAddress, addressSet_t* outAddress)
+{
+    if (NULL == pAddress)
+    {
+        printf("parameter is null !\n");
+        return -1;
+    }
+
+    int32_t len = strlen(pAddress);
+    int32_t isIp = 0;
+    int32_t ipLen = 0;
+
+    for (int i = 0; i < len; i++)
+    {
+        if (pAddress[i] == '.')
+        {
+            isIp = 1;
+        }
+
+        // found port number start index
+        if (isIp && pAddress[i] == ':')
+        {
+            ipLen = i;
+            break;
+        }
+    }
+
+    if (isIp)
+    {
+        if(ipLen && ipLen < sizeof(outAddress->ipAddress))
+        {
+            strncpy(outAddress->ipAddress, pAddress, ipLen);
+            outAddress->ipAddress[ipLen] = '\0';
+        }
+        else if (!ipLen && len < sizeof(outAddress->ipAddress))
+        {
+            strncpy(outAddress->ipAddress, pAddress, len);
+            outAddress->ipAddress[len] = '\0';
+        }
+        else
+        {
+            printf("IP Address too long: %d\n", ipLen==0 ? len : ipLen);
+            return -1;
+        }
+
+        if (ipLen > 0)
+        {
+            outAddress->port = atoi(pAddress + ipLen + 1);
+        }
+    }
+
+    return isIp;
+}