Implementation of connectivity abstraction feature Release v0.5
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / tizen / casample.c
index 9ea21a3..b493ad0 100644 (file)
 /******************************************************************
-*
-* Copyright 2014 Samsung Electronics All Rights Reserved.
-*
-*
-*
-* Licensed under the Apache License, Version 2.0 (the "License");
-* you may not use this file except in compliance with the License.
-* You may obtain a copy of the License at
-*
-*      http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing, software
-* distributed under the License is distributed on an "AS IS" BASIS,
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-* See the License for the specific language governing permissions and
-* limitations under the License.
-*
-******************************************************************/
+ *
+ * Copyright 2014 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
 #include <ctype.h>
 #include <fcntl.h>
 #include <errno.h>
-#include <wifi.h>
 #include <glib.h>
-
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
 #include "cacommon.h"
-#include "caadapterinterface.h"
-#include "uthreadpool.h"
-
-#define MOD_NAME "TizenSample"
-
-/**
- * Enable/disable one or more of the following macros to enable/disable 
- * functionality of that particular adapter type in the interfacesample.
- */
-//#define WIFI_ADAPTER_TEST
-#define BT_ADAPTER_TEST
-//#define BLE_ADAPTER_TEST
-
-#if defined(WIFI_ADAPTER_TEST)
-#include "cawifiadapter.h"
-#include "caethernetadapter.h"
-static u_thread_pool_t gWiFiThreadPool = NULL;
-#elif defined(BT_ADAPTER_TEST)
-#include "caedradapter.h"
-static  u_thread_pool_t gBTThreadPool = NULL;
-#elif defined(BLE_ADAPTER_TEST)
-#include "caleadapter.h"
-static u_thread_pool_t gLEThreadPool = NULL;
-#endif
+#include "cainterface.h"
 
 static GMainLoop *mainloop;
 static GIOChannel *channel;
 static guint g_test_io_watch_id;
 static GError *g_err_Sample;
 
-static CALocalConnectivity_t *localWifiEndpoint = NULL;
-//Hardcoded coap data for Test
-static char coapData[500] =
-    "{\"oc:\[{href\":\"/a/light\",\"ref\":{\"power\":\"20\",\"state\":\"true\"}}]}";
+pthread_t thread;
 
-void testInitializeBTInterface(void);
-void testTerminateBTInterface(void);
-void testInitializeWIFIInterface(void);
-void testTerminateWIFIInterface(void);
-void testInitializeBLEInterface(void);
-void testTerminateBLEInterface(void);
+#define MAX_BUF_LEN 1024
+#define MAX_OPT_LEN 16
 
-typedef struct ConnectivityHandlerList
-{
-    CAConnectivityType_t type;
-    CAConnectivityHandler_t handler;
-    struct ConnectivityHandlerList *nextHandler;
-} ConnectivityHandlerList;
+char get_menu();
+void process();
+
+void initialize();
+void start_listening_server();
+void start_discovery_server();
+void find_resource();
+void send_request();
+void send_response();
+void advertise_resource();
+void send_notification();
+void select_network();
+void unselect_network();
+void handle_request_response();
 
-static ConnectivityHandlerList *gConnectivityHandlers = NULL;
+void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo);
+void response_handler(const CARemoteEndpoint_t *object, const CAResponseInfo_t *responseInfo);
+void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token);
+void terminate();
 
-void initializeThreadPool(CAConnectivityType_t type)
+void pthread_func()
 {
-#ifdef BT_ADAPTER_TEST
-    if (CA_EDR == type && NULL == gBTThreadPool)
-    {
-        if (CA_STATUS_OK != u_thread_pool_init(3, &gBTThreadPool))
-        {
-            printf("Failed to create thread pool for BT adapter!\n");
-            return;
-        }
-    }
-#endif
-#ifdef WIFI_ADAPTER_TEST
-    if (CA_WIFI == type && NULL == gWiFiThreadPool)
-    {
-        if (CA_STATUS_OK != u_thread_pool_init(3, &gWiFiThreadPool))
-        {
-            printf("Failed to create thread pool for BT adapter!\n");
-            return;
-        }
-    }
-#endif
-#ifdef BLE_ADAPTER_TEST
-    if (CA_LE == type && NULL == gLEThreadPool)
-    {
-        if (CA_STATUS_OK != u_thread_pool_init(3, &gLEThreadPool))
-        {
-            printf("Failed to create thread pool for BT adapter!\n");
-            return;
-        }
-    }
-#endif
+    g_main_loop_run(mainloop);
 }
 
-
-void storeInterfaceCallbacks(ConnectivityHandlerList *newHandler)
+int main()
 {
-    printf("storeInterfaceCallbacks Entry in Sample\n");
-    newHandler->nextHandler = NULL;
-    ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
 
-    if (!tempConnectivityHandlers)
-    {
-        gConnectivityHandlers = newHandler;
-        printf("storeInterfaceCallbacks Exit in Sample\n");
-        return;
-    }
+    printf("=============================================\n");
+    printf("\t\tsample main\n");
+    printf("=============================================\n");
 
-    while (tempConnectivityHandlers->nextHandler)
-    {
-        tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
-    }
+    process();
 
-    tempConnectivityHandlers->nextHandler = newHandler;
-    printf("storeInterfaceCallbacks Exit in Sample\n");
+    return 0;
 }
 
-void interfaceRegisterCallback(CAConnectivityHandler_t handler,
-                               CAConnectivityType_t connType)
+void process()
 {
-    printf("interfaceRegisterCallback Entry in Sample\n");
-    ConnectivityHandlerList *newConnectivityHandler = (ConnectivityHandlerList *) malloc(sizeof(
-                ConnectivityHandlerList));
-    if (NULL == newConnectivityHandler)
+    while (1)
     {
-        printf("Memory allocation failed\n");
-        return;
-    }
+        char menu = toupper(get_menu());
 
-    newConnectivityHandler->type = connType;
-    newConnectivityHandler->handler = handler;
-    storeInterfaceCallbacks(newConnectivityHandler);
-    printf("interfaceRegisterCallback Exit in Sample\n");
-}
+        switch (menu)
+        {
+            case 'Q': // quits the sample program
+                printf("quit..!!\n");
+                g_main_loop_quit(mainloop);
+                return;
 
-void networkPacketHandler(CARemoteEndpoint_t *object, void *data, uint32_t dataLength)
-{
-    printf("networkPacketHandler Entry in Sample\n");
-    if (object == NULL || data == NULL)
-    {
-        printf("NULL Object\n");
-        return;
-    }
+            case 'I': // Initialize interface
+                initialize();
+                break;
 
-    printf("Data Received from: ");
-    if (CA_EDR == object->connectivityType)
-    {
-        printf(object->addressInfo.BT.btMacAddress);
-    }
-    else if (CA_LE == object->connectivityType)
-    {
-        printf(object->addressInfo.LE.leMacAddress);
-    }
-    else if (CA_WIFI == object->connectivityType || CA_ETHERNET == object->connectivityType)
-    {
-        printf(object->addressInfo.IP.ipAddress);
-    }
+            case 'S': // start server
+                start_listening_server();
+                break;
 
-    printf("\nReceived Data [Length: %d]: %s\n", dataLength, (char *)data);
-    printf("networkPacketHandler Exit in Sample\n");
-}
+            case 'C': // start client
+                start_discovery_server();
+                break;
 
-void networkInterfaceCallback(CALocalConnectivity_t *localEndPoint,
-                              CANetworkStatus_t networkConnectivityState)
-{
-    printf("networkInterfaceCallback Entry in Sample\n");
-    if (localEndPoint == NULL)
-    {
-        printf("NULL Object\n");
-        return;
-    }
+            case 'F': // find resource
+                find_resource();
+                break;
 
-    if (networkConnectivityState == CA_INTERFACE_UP)
-    {
-        printf("Network Status is UP\n");
-    }
-    else
-    {
-        printf("Network Status is DOWN\n");
-    }
+            case 'R': // send request
+                send_request();
+                break;
 
-    printf("Address: ");
-    if (CA_EDR == localEndPoint->type)
-    {
-        printf("%s\n", localEndPoint->addressInfo.BT.btMacAddress);
-    }
-    else if (CA_LE == localEndPoint->type)
-    {
-        printf("%s\n", localEndPoint->addressInfo.LE.leMacAddress);
-    }
-    else if (CA_WIFI == localEndPoint->type || CA_ETHERNET == localEndPoint->type)
-    {
-        printf("%s\n", localEndPoint->addressInfo.IP.ipAddress);
-    }
+            case 'A': // advertise resource
+                advertise_resource();
+                break;
 
-    printf("networkInterfaceCallback Exit in Sample\n");
-}
+            case 'N': // select network
+                select_network();
+                break;
 
+            case 'X': // unselect network
+                unselect_network();
+                break;
 
-void freeData(void *data)
-{
-    printf("freeData Entry in Sample\n");
-    if (data)
-    {
-        free(data);
-    }
-    printf("freeData Exit in Sample\n");
-}
+            case 'H': // handle request response
+                handle_request_response();
+                break;
 
-int16_t selectConnectivityType()
-{
-    int32_t cType;
-    printf("*******Select the Connectivity Type*******\n");
-    printf("  [1] WIFI \n");
-    printf("  [2] BT \n");
-    printf("  [3] BLE \n");
-
-    fflush(stdin);
-    scanf("%d", &cType);
-    if (cType < 1 && cType > 3)
-    {
-        printf("Invalid Selection!!!!\n");
-        return 0;
+            case 'T': // Terminate interface
+                terminate();
+                break;
+
+            default:
+                printf("not supported menu!!\n");
+                break;
+        }
     }
-    return (int16_t)cType;
+
 }
 
-int16_t interfaceStartAdapter(CAConnectivityType_t connType)
+void initialize()
 {
-    ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
-    if (NULL == tempConnectivityHandlers)
-    {
-        printf("None of the interface is initialized\n");
-        return 0;
-    }
-
-    while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
-    {
-        tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
-    }
+    mainloop = g_main_loop_new(NULL, FALSE);
+    pthread_create (&thread, NULL, (void *) &pthread_func, NULL);
 
-    if (NULL == tempConnectivityHandlers)
-    {
-        printf( "No interface handler for type %d", connType);
-        return 0;
-    }
+    CAInitialize();
 
-    if (CA_STATUS_OK != tempConnectivityHandlers->handler.startAdapter())
-    {
-        printf("Failed to Start adapter\n");
-        return 0;
-    }
+    // network enable
+    // default
+    printf("select default network(WIFI)\n");
+    CASelectNetwork(CA_WIFI);
 
-    return 1;
+    // set handler.
+    CARegisterHandler(request_handler, response_handler);
 }
 
-int16_t interfaceMulticastStartServer(CAConnectivityType_t connType, int serverType)
+void start_listening_server()
 {
-    printf("interfaceMulticastStartServer Starting...\n");
-    ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
-    if (NULL == tempConnectivityHandlers)
-    {
-        printf("None of the interface is initialized\n");
-        return 0;
-    }
+    printf("start listening server!!\n");
 
-    while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
-    {
-        tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
-    }
-
-    if (NULL == tempConnectivityHandlers)
-    {
-        printf( "No interface handler for type %d", connType);
-        return 0;
-    }
+    CAStartListeningServer();
+}
 
-    CAAdapterStartDiscoveryServer startServer = NULL;
-    switch (serverType)
-    {
-        case 1: //Discovery server
-            startServer = tempConnectivityHandlers->handler.startDiscoverServer;
-            break;
-        case 2: //Listening server
-            startServer = tempConnectivityHandlers->handler.startListenServer;
-            break;
-    }
+void start_discovery_server()
+{
+    printf("start discovery server at client!!\n");
 
-    if (startServer)
-    {
-        printf("Invoking start server method\n");
-        startServer();
-    }
+    CAStartDiscoveryServer();
 }
 
-int16_t interfaceSendUnicastData(CAConnectivityType_t connType)
+void find_resource()
 {
-    ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
-    if (NULL == tempConnectivityHandlers)
-    {
-        printf(" None of the interface is initialized \n");
-        return 0;
-    }
+    char buf[MAX_BUF_LEN];
 
-    while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
-    {
-        tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
-    }
+    memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
 
-    if (NULL == tempConnectivityHandlers)
-    {
-        printf( "No interface handler for type %d", connType);
-        return 0;
-    }
+    printf("\n=============================================\n");
+    printf("ex) a/light\n");
+    printf("reference uri : ");
 
-    if (CA_WIFI == connType)
-    {
-        CARemoteEndpoint_t endpoint;
-        char remoteIPAddress[CA_IPADDR_SIZE] = {0};
-        printf("\nEnter the Remote Endpoint IP: ");
-        scanf("%s", remoteIPAddress);
-        if (strlen(remoteIPAddress) == 0)
-        {
-            printf("Invalid device address\n");
-            return;
-        }
-        endpoint.connectivityType = CA_WIFI;
-        strncpy(endpoint.addressInfo.IP.ipAddress, remoteIPAddress, CA_IPADDR_SIZE);
-        endpoint.addressInfo.IP.port = 5283; /* Send the corresponding port here */
+    gets(buf);
 
-        int sdatalen = tempConnectivityHandlers->handler.sendData(&endpoint, coapData,
-                       strlen(coapData));
-        if (sdatalen == strlen(coapData))
-        {
-            printf("Send Unicast data success\n");
-        }
-        else
-        {
-            printf("Send Unicast data failed\n");
-        }
-    }
-    else if (CA_EDR == connType)
+       // create token
+    CAToken_t token = NULL;
+    CAResult_t res = CAGenerateToken(&token);
+    if (res != CA_STATUS_OK)
     {
-        //create endpoint
-        CARemoteEndpoint_t endpoint;
-
-        //Get the device address from user
-        char deviceaddress[100] = {0};
-        printf("Enter the device address: \n");
-        scanf("%s", deviceaddress);
+        printf("token generate error!!\n");
+        token = NULL;
+    }
 
-        if (strlen(deviceaddress) == 0)
-        {
-            printf("Invlaid device address\n");
-            return;
-        }
+    printf("generated token %s\n", (token != NULL) ? token : "");
 
-        endpoint.connectivityType = CA_EDR;
-        strncpy(endpoint.addressInfo.BT.btMacAddress, deviceaddress, CA_MACADDR_SIZE - 1);
-        endpoint.addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
-        endpoint.resourceUri = NULL;
+    res = CAFindResource(buf, token);
 
-        printf("Sent Unicast data to device: %s\n", endpoint.addressInfo.BT.btMacAddress);
-        tempConnectivityHandlers->handler.sendData(&endpoint, coapData, strlen(coapData) + 1);
+    if (res != CA_STATUS_OK)
+    {
+        printf("find resource error:%d !!\n", res);
     }
-    else if (CA_LE == connType)
+    else
     {
-        //create endpoint
-        CARemoteEndpoint_t endpoint;
-
-        //Get the device address from user
-        char deviceaddress[100] = {0};
-        printf("Enter the device address: \n");
-        scanf("%s", deviceaddress);
+        printf("find resource for %s URI\n", buf);
+    }
 
-        if (strlen(deviceaddress) == 0)
-        {
-            printf("Invlaid device address\n");
-            return;
-        }
+    printf("=============================================\n");
+}
 
-        //Get the service uuid from user
-        char uuid[100] = {0};
-        printf("Enter the service uuid: \n");
-        scanf("%s", uuid);
+void send_request()
+{
+    char buf[MAX_BUF_LEN];
 
-        if (strlen(uuid) == 0)
-        {
-            printf("Invlaid service uuid\n");
-            return;
-        }
+    memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
 
-        endpoint.connectivityType = CA_LE;
-        strncpy(endpoint.addressInfo.BT.btMacAddress, deviceaddress, CA_MACADDR_SIZE - 1);
-        endpoint.addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
-        endpoint.resourceUri = strdup(uuid);
+    printf("\n=============================================\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("uri : ");
 
-        tempConnectivityHandlers->handler.sendData(&endpoint, coapData, strlen(coapData));
-        printf("Sent Unicast data \n");
-        free(endpoint.resourceUri);
-    }
+    gets(buf);
 
-    return 1;
-}
+    // create remote endpoint
+    CARemoteEndpoint_t *endpoint = NULL;
+    CAResult_t res = CACreateRemoteEndpoint(buf, &endpoint);
 
-int16_t interfaceSendMulticastData(CAConnectivityType_t connType)
-{
-    ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
-    if (NULL == tempConnectivityHandlers)
+    if (res != CA_STATUS_OK)
     {
-        printf("None of the interface is initialized\n");
-        return 0;
+        printf("create remote endpoint error!!");
+        CADestroyRemoteEndpoint(endpoint);
+        return;
     }
 
-    while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
-    {
-        tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
-    }
+    // create token
+    CAToken_t token = NULL;
+    res = CAGenerateToken(&token);
 
-    if (NULL == tempConnectivityHandlers)
+    if (res != CA_STATUS_OK)
     {
-        printf( "No interface handler for type %d", connType);
-        return 0;
+        printf("token generate error!!");
+        token = NULL;
     }
 
-    if (connType == CA_WIFI)
-    {
-        tempConnectivityHandlers->handler.sendDataToAll(coapData,
-                strlen(coapData) + 1);
-    }
-    else if (connType == CA_EDR || connType == CA_LE)
-    {
-        tempConnectivityHandlers->handler.sendDataToAll(coapData, strlen(coapData));
-    }
-}
+    printf("generated token %s\n", (token != NULL) ? token : "");
 
-void interfaceReadData(CAConnectivityType_t connType)
-{
-    ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
-    if (NULL == tempConnectivityHandlers)
-    {
-        printf("None of the interface is initialized\n");
-        return;
-    }
+    CAInfo_t requestData;
+    memset(&requestData, 0, sizeof(CAInfo_t));
+    requestData.token = token;
+    requestData.payload = "Temp Json Payload";
 
-    while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
-    {
-        tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
-    }
+    CARequestInfo_t requestInfo;
+    memset(&requestInfo, 0, sizeof(CARequestInfo_t));
+    requestInfo.method = CA_GET;
+    requestInfo.info = requestData;
 
-    if (NULL == tempConnectivityHandlers)
+    // send request
+    CASendRequest(endpoint, &requestInfo);
+
+    if (token != NULL)
     {
-        printf( "No interface handler for type %d", connType);
-        return;
+        CADestroyToken(token);
     }
 
-    if (CA_STATUS_OK != tempConnectivityHandlers->handler.readData())
+    // destroy remote endpoint
+    if (endpoint != NULL)
     {
-        printf("Failed to Read Data\n");
-        return;
+        CADestroyRemoteEndpoint(endpoint);
     }
 
-    printf("Read Data is successfull\n");
-    return;
+    printf("=============================================\n");
 }
 
-void interfaceGetNetworkInfo(CAConnectivityType_t connType)
+void advertise_resource()
 {
-    int i = 0;
+    char buf[MAX_BUF_LEN];
 
-    ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
-    if (NULL == tempConnectivityHandlers)
-    {
-        printf("None of the interface is initialized\n");
-        return;
-    }
+    memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
 
-    while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
-    {
-        tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
-    }
+    printf("\n=============================================\n");
+    printf("uri : ");
 
-    if (NULL == tempConnectivityHandlers)
-    {
-        printf( "No interface handler for type %d", connType);
-        return;
-    }
+    scanf("%s", buf);
 
-    //Get the network interface info
-    CALocalConnectivity_t *info = NULL;
-    uint32_t size = 0;
-    if (CA_STATUS_OK != tempConnectivityHandlers->handler.GetnetInfo(&info, &size))
-    {
-        printf("Failed to get network info\n");
-        return;
-    }
+    int optionNum = 0;
+    char optionData[MAX_OPT_LEN];
 
-    if (0 >= size || info == NULL)
-    {
-        printf("No network found !!!\n");
-        return;
-    }
+    printf("Option Num : ");
+    scanf("%d", &optionNum);
+    CAHeaderOption_t *headerOpt;
 
-    printf("Network Information: \n");
-    for (; i < size; i++)
+    if(optionNum > 0)
     {
-        if (connType == CA_WIFI)
+        headerOpt = (CAHeaderOption_t *) malloc(sizeof(CAHeaderOption_t) * optionNum);
+        if (NULL == headerOpt)
         {
-            printf("Type : %s\n", (connType == CA_ETHERNET) ? "CA_ETHERNET" : "CA_WIFI");
-            printf("Address : %s\n", info[i].addressInfo.IP.ipAddress);
-        }
-        else
-        {
-            printf("Type : %s\n", (connType == CA_EDR) ? "CA_EDR" : "CA_LE");
-            printf("Address : %s\n\n", info[i].addressInfo.BT.btMacAddress);
+            printf("memory allocation failed!\n");
+            return;
         }
+        memset(headerOpt, 0, sizeof(CAHeaderOption_t) * optionNum);
     }
-}
 
-int16_t interfaceStopAdapter(CAConnectivityType_t connType)
-{
-    ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
-    if (NULL == tempConnectivityHandlers)
+    int i;
+    for (i = 0 ; i < optionNum ; i++)
     {
-        printf("None of the interface is initialized\n");
-        return 0;
-    }
+        int optionID = 0;
+        printf("[%d] Option ID : ", i + 1);
+        scanf("%d", &optionID);
+        headerOpt[i].optionID = optionID;
 
-    while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
-    {
-        tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
-    }
+        memset(optionData, 0, sizeof(char) * MAX_OPT_LEN);
+        printf("[%d] Option Data : ", i + 1);
+        scanf("%s", optionData);
+        memcpy(headerOpt[i].optionData, optionData, MAX_OPT_LEN);
+        printf("[%d] inputed option : ID : %d, data : %s\n", i + 1, optionID, optionData );
 
-    if (NULL == tempConnectivityHandlers)
-    {
-        printf( "No interface handler for type %d", connType);
-        return 0;
+        headerOpt[i].optionLength = (uint16_t)strlen(optionData);
     }
+    printf("\n=============================================\n");
 
-    if (CA_STATUS_OK != tempConnectivityHandlers->handler.stopAdapter())
+    // create token
+    CAToken_t token = NULL;
+    CAResult_t res = CAGenerateToken(&token);
+    if (res != CA_STATUS_OK)
     {
-        printf("Failed to Stop adapter\n");
-        return 0;
+        printf("token generate error!!\n");
+        token = NULL;
     }
 
-    printf("Stopped the adapter\n");
-    return 1;
-}
+    printf("generated token %s\n", (token != NULL) ? token : "");
 
-void testInitializeInterface()
-{
-    printf("testInitializeInterface Entry\n");
+    CAAdvertiseResource(buf, token, headerOpt, (uint8_t)optionNum);
 
-    int16_t type = selectConnectivityType();
-    if (0 >= type || 3 < type)
-    {
-        printf("Invalid selection...\n");
-        return;
-    }
+    free(headerOpt);
 
-    switch (type)
-    {
-#ifdef WIFI_ADAPTER_TEST
-        case 1: //WIFI
-            {
-                testInitializeWIFIInterface();
-            }
-            break;
-#endif
-#ifdef BT_ADAPTER_TEST
-        case 2:   //BT
-            {
-                testInitializeBTInterface();
-            }
-            break;
-#endif
-#ifdef BLE_ADAPTER_TEST
-        case 3: //BLE
-            {
-                testInitializeBLEInterface();
-            }
-            break;
-#endif
-        default:
-            printf("Feature is not enabled or not implemented\n");
-    }
 }
 
-void testTerminateInterface()
+void select_network()
 {
-    int16_t type = selectConnectivityType();
-    if (0 >= type || 3 < type)
-    {
-        printf("Invalid selection...\n");
-        return;
-    }
+    char buf[MAX_BUF_LEN];
 
-#ifdef WIFI_ADAPTER_TEST
-    if (1 == type)   /* WIFI */
-    {
-        testTerminateWIFIInterface();
-    }
-#endif
-#ifdef BT_ADAPTER_TEST
-    if (2 == type)   /*BT*/
-    {
-        testTerminateBTInterface();
-    }
-#endif
-#ifdef BLE_ADAPTER_TEST
-    if (3 == type)   /*BLE*/
-    {
-        testTerminateBLEInterface();
-    }
-#endif
+    printf("\n=============================================\n");
+    printf("\tselect network\n");
+    printf("ETHERNET : 0\n");
+    printf("WIFI : 1\n");
+    printf("EDR : 2\n");
+    printf("LE : 3\n");
+    printf("select : ");
 
-    ConnectivityHandlerList *currentConnectivityHandler = gConnectivityHandlers;
-    ConnectivityHandlerList *prevConnectivityHandler = NULL;
+    memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
+    gets(buf);
 
-    printf("Linked list delete start\n");
-    while (currentConnectivityHandler != NULL)
-    {
-        printf("Iterating through the list to find the matching interface\n");
-        if (currentConnectivityHandler->type == type)
-        {
-            printf("Matching interface found\n");
-            if (prevConnectivityHandler == NULL)
-            {
-                currentConnectivityHandler = currentConnectivityHandler->nextHandler;
-                freeData(gConnectivityHandlers);
-                gConnectivityHandlers = NULL;
-                printf( "Node deleted with interface type %d", type);
-            }
-            else
-            {
-                prevConnectivityHandler->nextHandler = currentConnectivityHandler->nextHandler;
-                freeData(currentConnectivityHandler);
-                currentConnectivityHandler = prevConnectivityHandler->nextHandler;
-                printf( "Node deleted with interface type %d from linked list", type);
-            }
-        }
-        else
-        {
-            prevConnectivityHandler = currentConnectivityHandler;
-            currentConnectivityHandler = currentConnectivityHandler->nextHandler;
-        }
-    }
-    gConnectivityHandlers = NULL;
-    return;
-}
+    int number = buf[0] - '0';
 
-void testStartAdapter()
-{
-    int type = selectConnectivityType();
-    if (0 >= type || 3 < type)
-    {
-        printf("Invalid selection...\n");
-        return;
-    }
+    number = (number < 0 || number > 3) ? 1 : number;
 
-    switch (type)
-    {
-#ifdef WIFI_ADAPTER_TEST
-        case 1: //WIFI
-            {
-                interfaceStartAdapter(CA_WIFI);
-            }
-            break;
-#endif
-#ifdef BT_ADAPTER_TEST
-        case 2:   //BT
-            {
-                interfaceStartAdapter(CA_EDR);
-            }
-            break;
-#endif
-#ifdef BLE_ADAPTER_TEST
-        case 3: //BLE
-            {
-                interfaceStartAdapter(CA_LE);
-            }
-            break;
-#endif
-        default:
-            printf("Feature is not enabled or not implemented\n");
-    }
+    CASelectNetwork(1 << number);
+
+    printf("=============================================\n");
 }
 
-void testStartServer(int serverType)
+void unselect_network()
 {
-    int type = selectConnectivityType();
-    if (0 >= type || 3 < type)
-    {
-        printf("Invalid selection...\n");
-        return;
-    }
+    char buf[MAX_BUF_LEN];
 
-    switch (type)
-    {
-#ifdef WIFI_ADAPTER_TEST
-        case 1: //WIFI
-            {
-                interfaceMulticastStartServer(CA_WIFI, serverType);
-            }
-            break;
-#endif
-#ifdef BT_ADAPTER_TEST
-        case 2:   //BT
-            {
-                interfaceMulticastStartServer(CA_EDR, serverType);
-            }
-            break;
-#endif
-#ifdef BLE_ADAPTER_TEST
-        case 3: //BLE
-            {
-                interfaceMulticastStartServer(CA_LE, serverType);
-            }
-            break;
-#endif
-        default:
-            printf("Feature is not enabled or not implemented\n");
-    }
-}
+    printf("\n=============================================\n");
+    printf("\tunselect enabled network\n");
+    printf("ETHERNET : 0\n");
+    printf("WIFI : 1\n");
+    printf("EDR : 2\n");
+    printf("LE : 3\n");
+    printf("select : ");
 
-void testSendUnicastData()
-{
-    int16_t type = selectConnectivityType();
-    if (0 >= type || 3 < type)
-    {
-        printf("Invalid selection...\n");
-        return;
-    }
+    memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
+    gets(buf);
 
-    switch (type)
-    {
-#ifdef WIFI_ADAPTER_TEST
-        case 1: //WIFI
-            {
-                interfaceSendUnicastData(CA_WIFI);
-            }
-            break;
-#endif
-#ifdef BT_ADAPTER_TEST
-        case 2:   //BT
-            {
-                interfaceSendUnicastData(CA_EDR);
-            }
-            break;
-#endif
-#ifdef BLE_ADAPTER_TEST
-        case 3: //BLE
-            {
-                interfaceSendUnicastData(CA_LE);
-            }
-            break;
-#endif
-        default:
-            printf("Feature is not enabled or not implemented\n");
-    }
-}
+    int number = buf[0] - '0';
 
-void testSendMulticastData()
-{
-    int16_t type = selectConnectivityType();
-    if (0 >= type || 3 < type)
-    {
-        printf("Invalid selection...\n");
-        return;
-    }
+    number = (number < 0 || number > 3) ? 1 : number;
 
-    switch (type)
-    {
-#ifdef WIFI_ADAPTER_TEST
-        case 1: //WIFI
-            {
-                interfaceSendMulticastData(CA_WIFI);
-            }
-            break;
-#endif
-#ifdef BT_ADAPTER_TEST
-        case 2:   //BT
-            {
-                interfaceSendMulticastData(CA_EDR);
-            }
-            break;
-#endif
-#ifdef BLE_ADAPTER_TEST
-        case 3: //BLE
-            {
-                interfaceSendMulticastData(CA_LE);
-            }
-            break;
-#endif
-        default:
-            printf("Feature is not enabled or not implemented\n");
-    }
+    CAUnSelectNetwork(1 << number);
+    printf("Terminating...\n");
+    CATerminate();
+    //pthread_join(thread, NULL);
+    printf("=============================================\n");
 }
 
-void testReadData(void)
+char get_menu()
 {
-    int16_t type = selectConnectivityType();
-    if (0 >= type || 3 < type)
-    {
-        printf("Invalid selection...\n");
-        return;
-    }
+    char buf[MAX_BUF_LEN];
 
-    switch (type)
-    {
-#ifdef WIFI_ADAPTER_TEST
-        case 1: //WIFI
-            {
-                interfaceReadData(CA_WIFI);
-            }
-            break;
-#endif
-#ifdef BT_ADAPTER_TEST
-        case 2:   //BT
-            {
-                interfaceReadData(CA_EDR);
-            }
-            break;
-#endif
-#ifdef BLE_ADAPTER_TEST
-        case 3: //BLE
-            {
-                interfaceReadData(CA_LE);
-            }
-            break;
-#endif
-        default:
-            printf("Feature is not enabled or not implemented\n");
-    }
-}
+    printf("\n=============================================\n");
+    printf("\t\tMenu\n");
+    printf("\ti : Initialize\n");
+    printf("\ts : start server\n");
+    printf("\tc : start client\n");
+    printf("\tf : find resource\n");
+    printf("\tr : send request\n");
+    printf("\ta : advertise resource\n");
+    printf("\tn : select network\n");
+    printf("\tx : unselect network\n");
+    printf("\th : handle request response\n");
+    printf("\tt : terminate\n");
+    printf("\tq : quit\n");
+    printf("=============================================\n");
+    printf("select : ");
 
-void testGetNetworkInfo(void)
-{
-    int16_t type = selectConnectivityType();
-    if (0 >= type || 3 < type)
-    {
-        printf("Invalid selection...\n");
-        return;
-    }
+    memset(buf, 0, sizeof(char) * MAX_BUF_LEN);
 
-    switch (type)
-    {
-#ifdef WIFI_ADAPTER_TEST
-        case 1: //WIFI
-            {
-                interfaceGetNetworkInfo(CA_WIFI);
-            }
-            break;
-#endif
-#ifdef BT_ADAPTER_TEST
-        case 2:   //BT
-            {
-                interfaceGetNetworkInfo(CA_EDR);
-            }
-            break;
-#endif
-#ifdef BLE_ADAPTER_TEST
-        case 3: //BLE
-            {
-                interfaceGetNetworkInfo(CA_LE);
-            }
-            break;
-#endif
-        default:
-            printf("Feature is not enabled or not implemented\n");
-    }
+    gets(buf);
+
+    return buf[0];
 }
 
-void testStopAdapter()
+void handle_request_response()
 {
-    int16_t type = selectConnectivityType();
-    if (0 >= type || 3 < type)
-    {
-        printf("Invalid selection...\n");
-        return;
-    }
-
-    switch (type)
-    {
-#ifdef WIFI_ADAPTER_TEST
-        case 1: //WIFI
-            {
-                interfaceStopAdapter(CA_WIFI);
-            }
-            break;
-#endif
-#ifdef BT_ADAPTER_TEST
-        case 2:   //BT
-            {
-                interfaceStopAdapter(CA_EDR);
-            }
-            break;
-#endif
-#ifdef BLE_ADAPTER_TEST
-        case 3: //BLE
-            {
-                interfaceStopAdapter(CA_LE);
-            }
-            break;
-#endif
-        default:
-            printf("Feature is not enabled or not implemented\n");
-    }
+    printf("handle_request_response\n");
+    CAHandleRequestResponse();
 }
 
-#ifdef BT_ADAPTER_TEST
-void testInitializeBTInterface(void)
+void request_handler(const CARemoteEndpoint_t *object, const CARequestInfo_t *requestInfo)
 {
-    printf("Initiazing EDR\n");
 
-    printf("Initializing BT Adapter threadpool\n");
-    initializeThreadPool(CA_EDR);
+    printf("[CALLBACK] request_handler, uri : %s, data : %s\n", (object != NULL) ? object->resourceUri : "",
+            (requestInfo != NULL) ? requestInfo->info.payload : "");
+
+    printf("[CALLBACK] request_handler, address : %s\n", (object != NULL) ? object->addressInfo.IP.ipAddress : "");
 
-    //Start bluetooth communication adapter
-    CAResult_t err = CAInitializeEDR(interfaceRegisterCallback, networkPacketHandler,
-                                     networkInterfaceCallback, gBTThreadPool);
-    if (CA_STATUS_OK != err && CA_ADAPTER_NOT_ENABLED != err)
+    if(requestInfo->info.options)
     {
-        printf("Failed to initialize bluetooth communication adapter!\n");
+        uint32_t len =  requestInfo->info.numOptions;
+        uint32_t i;
+        for(i = 0 ; i < len ; i++)
+        {
+            printf("[CALLBACK] request_handler, option ID : %d\n", requestInfo->info.options[i].optionID);
+            printf("[CALLBACK] request_handler, options data length : %d\n", requestInfo->info.options[i].optionLength);
+            printf("[CALLBACK] request_handler, options data : %s\n", requestInfo->info.options[i].optionData );
+        }
     }
-}
 
-void testTerminateBTInterface(void)
-{
-    printf("Terminating EDR\n");
-
-    //Terminate the Bluetooth communication adapter
-    CATerminateEDR();
+    printf("send response with URI\n");
+    send_response(object, (requestInfo != NULL) ? requestInfo->info.token : "");
 
-    printf( "Terminating BT Adapter thread pool");
-    u_thread_pool_free(gBTThreadPool);
 }
-#endif //BT_ADAPTER_TEST
 
-#ifdef WIFI_ADAPTER_TEST
-void testInitializeWIFIInterface(void)
+void response_handler(const CARemoteEndpoint_t* object, const CAResponseInfo_t* responseInfo)
 {
-    printf("testInitializeWIFIInterface IN\n");
 
-    printf("Initializing WIFI adapter threadpool\n");
-    initializeThreadPool(CA_WIFI);
+    printf("[CALLBACK] response_handler, uri : %s, data : %s\n", (object != NULL) ? object->resourceUri : "",
+            (responseInfo != NULL) ? responseInfo->info.payload : "");
 
-    //Start Wifi communication adapter
-    if (0 != CAInitializeWifi(interfaceRegisterCallback, networkPacketHandler,
-                              networkInterfaceCallback, gWiFiThreadPool))
+    printf("[CALLBACK] response_handler, address : %s\n", (object != NULL) ? object->addressInfo.IP.ipAddress : "");
+
+    if(responseInfo->info.options)
     {
-        printf("testInitializeWIFIInterface Failed to initialize bluetooth communication adapter\n");
-        return;
+        uint32_t len =  responseInfo->info.numOptions;
+        uint32_t i;
+        for(i = 0 ; i < len ; i++)
+        {
+            printf("[CALLBACK] response_handler, option ID : %d\n", responseInfo->info.options[i].optionID);
+            printf("[CALLBACK] response_handler, options data length : %d\n", responseInfo->info.options[i].optionLength);
+            printf("[CALLBACK] response_handler, options data : %s\n", responseInfo->info.options[i].optionData );
+        }
     }
 
-    printf("testInitializeWIFIInterface OUT\n");
+    //printf("send request with URI\n");
+    //send_request_tmp(object, (responseInfo != NULL) ? responseInfo->info.token : "");
 }
 
-void testTerminateWIFIInterface(void)
+void send_response(CARemoteEndpoint_t *endpoint, CAToken_t request_token)
 {
-    printf("testTerminateWIFIInterface IN\n");
-
-    // Stop if Wifi communication adapter is running
-    interfaceStopAdapter(CA_WIFI);
-
-    // Freeing threadpool for wifi communication adapter
-    printf( "Terminating WIFI Adapter thread pool");
-    u_thread_pool_free(gWiFiThreadPool);
 
-    //Terminate the Wifi communication adapter
-    CATerminateWIfI();
+    printf("\n=============================================\n");
 
+    CAInfo_t responseData;
+    //responseData = (CAInfo*) malloc(sizeof(CAInfo));
+    memset(&responseData, 0, sizeof(CAInfo_t));
+    responseData.token = request_token;
+    responseData.payload = "response payload";
 
-    printf("testTerminateWIFIInterface OUT\n");
-}
-#endif //WIFI_ADAPTER_TEST
+    CAResponseInfo_t responseInfo;
+    //responseInfo = (CAResponseInfo*) malloc(sizeof(CAResponseInfo));
+    memset(&responseInfo, 0, sizeof(CAResponseInfo_t));
+    responseInfo.result = 203;
+    responseInfo.info = responseData;
 
-#ifdef BLE_ADAPTER_TEST
-void testInitializeBLEInterface(void)
-{
-    printf("testInitializeBLEInterface IN\n");
+    // send request (connectivityType from remoteEndpoint of request Info)
+    CASendResponse(endpoint, &responseInfo);
 
-    printf("Initializing BLE adapter threadpool\n");
-    initializeThreadPool(CA_LE);
+    printf("=============================================\n");
 
-    //Start bluetooth communication adapter
-    if (0 != CAInitializeLE(interfaceRegisterCallback, networkPacketHandler,
-                            networkInterfaceCallback, gLEThreadPool))
-    {
-        printf("testInitializeBLEInterface Failed due to CAInitializeLE\n");
-        return;
-    }
-
-    printf("testInitializeBLEInterface OUT\n");
 }
 
-void testTerminateBLEInterface(void)
+void send_request_tmp(CARemoteEndpoint_t *endpoint, CAToken_t token)
 {
-    printf("testTerminateBLEInterface IN\n");
 
-    //Terminate the BLE server & Client
-    CATerminateLE();
+    printf("\n=============================================\n");
 
-    printf( "Terminating BLE Adapter thread pool");
-    u_thread_pool_free(gLEThreadPool);
-
-    printf("testTerminateBLEInterface OUT\n");
-}
-#endif //BLE_ADAPTER_TEST
+    CAInfo_t requestData;
+    memset(&requestData, 0, sizeof(CAInfo_t));
+    requestData.token = token;
+    requestData.payload = "Temp Json Payload";
 
-static void testPrintHelp(void)
-{
-    printf(" =====================================================================\n");
-    printf("|                 Welcome to Connectivity Abstraction                 |\n");
-    printf("|                   - CA Unit Test v1.0 -                             |\n");
-    printf("|---------------------------------------------------------------------|\n");
-    printf("|                           ** Options **                             |\n");
-    printf("|  i - Initialize the Interface                                       |\n");
-    printf("|  d - Terminate the Interface                                        |\n");
-    printf("|  a - Start adapter                                                  |\n");
-    printf("|  b - Stop adapter                                                   |\n");
-    printf("|  sd- Start Discovery Server                                         |\n");
-    printf("|  sl- Start Listening Server                                         |\n");
-    printf("|  u - Send Unicast Data                                              |\n");
-    printf("|  m - Send Multicast Data                                            |\n");
-    printf("|  g - Get Network Info                                               |\n");
-    printf("|  r - Read data synchronously                                        |\n");
-    printf("|  x - quit the test.                                                 |\n");
-    printf("|  h - Help menu.                                                     |\n");
-    printf(" =====================================================================\n");
-}
+    CARequestInfo_t requestInfo;
+    memset(&requestInfo, 0, sizeof(CARequestInfo_t));
+    requestInfo.method = CA_GET;
+    requestInfo.info = requestData;
 
-static gboolean testThread(GIOChannel *source, GIOCondition condition , gpointer data)
-{
-    gchar buf[10] = {'\0'};
-    gsize read = 0;
+    // send request
+    endpoint->connectivityType = CA_WIFI;
+    CASendRequest(endpoint, &requestInfo);
 
+    printf("=============================================\n");
 
-    if (g_io_channel_read(channel, buf, 10, &read) != G_IO_ERROR_NONE)
-    {
-        printf("g_io_channel_read error!!!\n");
-        return 1;
-    }
-    buf[read] = '\0';
-    g_strstrip(buf);
-
-    /*if ((!has_register) && (((buf[0]!= 'i') && (buf[0]!= 'h') && (buf[0]!= 'q')) || (read != 2))) {
-        testPrintHelp();
-        printf("***Warning***: You should Register firstly!\n");
-        return 1;
-    }*/
-    switch (buf[0])
-    {
-        case 'i':
-            testInitializeInterface();
-            break;
-        case 'x':
-            testTerminateInterface();
-            if (g_source_remove(g_test_io_watch_id))
-            {
-                printf("g_source_remove() OK!!!\n");
-                g_io_channel_shutdown(channel, TRUE, &g_err_Sample);
-                g_io_channel_unref(channel);
-                g_main_loop_quit(mainloop);
-            }
-            break;
-        case 'd':
-            testTerminateInterface();
-            break;
-        case 'a':
-            testStartAdapter();
-            break;
-        case 'b':
-            testStopAdapter();
-            break;
-        case 's':
-            if (read == 3)
-            {
-                if (buf[1] == 'd')
-                {
-                    testStartServer(1);
-                }
-                if (buf[1] == 'l')
-                {
-                    testStartServer(2);
-                }
-            }
-            break;
-        case 'u':
-            testSendUnicastData();
-            break;
-        case 'm':
-            testSendMulticastData();
-            break;
-        case 'r':
-            testReadData();
-            break;
-        case 'g':
-            testGetNetworkInfo();
-            break;
-        case 'h':
-            testPrintHelp();
-    }
-    return 1;
 }
 
-int main(int argc, char *argv[])
+void terminate()
 {
-    printf("Starting sample\n");
-    mainloop = g_main_loop_new(NULL, FALSE);
-    channel = g_io_channel_unix_new(0);/* read from stdin */
-    g_test_io_watch_id = g_io_add_watch(channel,
-                                        (GIOCondition)(G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL), testThread,
-                                        NULL);
-    printf("CM Test Thread created...\n");
-    testPrintHelp();
-    g_main_loop_run(mainloop);
-    return 0;
+    unselect_network();
 }
-