Fixed klockwork memory leaks and modified the logs
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / tizen / interfacesample.c
index bd95856..64d29a6 100644 (file)
 *
 ******************************************************************/
 
+//***************************************************************
+//
+// This file is for internal test only.
+// Do NOT use this file to test CA on Tizen.
+//
+//***************************************************************
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 
 #define MOD_NAME "TizenSample"
 
-//Comment any below for blocking testing of specific adapters
-//#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)
+#endif
+#if defined(BT_ADAPTER_TEST)
 #include "caedradapter.h"
 static  u_thread_pool_t gBTThreadPool = NULL;
-#elif defined(BLE_ADAPTER_TEST)
+#endif
+#if defined(BLE_ADAPTER_TEST)
 #include "caleadapter.h"
 static u_thread_pool_t gLEThreadPool = NULL;
 #endif
 
-static GMainLoop *mainloop;
-static GIOChannel *channel;
-static guint g_test_io_watch_id;
-static GError *g_err_Sample;
+static GMainLoop *mainloop = NULL;
+static GIOChannel *channel = NULL;
+static guint g_test_io_watch_id = 0;
+static GError *g_err_Sample = NULL;
 
-static CALocalConnectivity_t *localWifiEndpoint = NULL;
 //Hardcoded coap data for Test
 static char coapData[500] =
     "{\"oc:\[{href\":\"/a/light\",\"ref\":{\"power\":\"20\",\"state\":\"true\"}}]}";
@@ -79,34 +81,34 @@ static ConnectivityHandlerList *gConnectivityHandlers = NULL;
 void initializeThreadPool(CAConnectivityType_t type)
 {
 #ifdef BT_ADAPTER_TEST
-       if (CA_EDR == type && NULL == gBTThreadPool)
-       {
+    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_WIFI == type && NULL == gWiFiThreadPool)
+    {
         if (CA_STATUS_OK != u_thread_pool_init(3, &gWiFiThreadPool))
         {
-            printf("Failed to create thread pool for BT adapter!\n");
+            printf("Failed to create thread pool for WIFI adapter!\n");
             return;
         }
-       }
+    }
 #endif
 #ifdef BLE_ADAPTER_TEST
-       if (CA_LE == type && NULL == gLEThreadPool)
-       {
-        if (CA_STATUS_OK != u_thread_pool_init(3, &gLEThreadPool))
+    if (CA_LE == type && NULL == gLEThreadPool)
+    {
+        if (CA_STATUS_OK != u_thread_pool_init(5, &gLEThreadPool))
         {
-            printf("Failed to create thread pool for BT adapter!\n");
+            printf("Failed to create thread pool for BLE adapter!\n");
             return;
         }
-       }
+    }
 #endif
 }
 
@@ -167,14 +169,14 @@ void networkPacketHandler(CARemoteEndpoint_t *object, void *data, uint32_t dataL
     }
     else if (CA_LE == object->connectivityType)
     {
-               printf(object->addressInfo.LE.leMacAddress);
+        printf(object->addressInfo.LE.leMacAddress);
     }
     else if (CA_WIFI == object->connectivityType || CA_ETHERNET == object->connectivityType)
     {
         printf(object->addressInfo.IP.ipAddress);
     }
 
-    printf("\nReceived Data [Length: %d]: %s\n",dataLength,(char *)data);
+    printf("\nReceived Data [Length: %d]: %s\n", dataLength, (char *)data);
     printf("networkPacketHandler Exit in Sample\n");
 }
 
@@ -302,9 +304,6 @@ int16_t interfaceMulticastStartServer(CAConnectivityType_t connType, int serverT
         case 2: //Listening server
             startServer = tempConnectivityHandlers->handler.startListenServer;
             break;
-        case 3: //Notification server
-            startServer = tempConnectivityHandlers->handler.startNotifyServer;
-            break;
     }
 
     if (startServer)
@@ -312,6 +311,7 @@ int16_t interfaceMulticastStartServer(CAConnectivityType_t connType, int serverT
         printf("Invoking start server method\n");
         startServer();
     }
+    return 0;
 }
 
 int16_t interfaceSendUnicastData(CAConnectivityType_t connType)
@@ -343,11 +343,11 @@ int16_t interfaceSendUnicastData(CAConnectivityType_t connType)
         if (strlen(remoteIPAddress) == 0)
         {
             printf("Invalid device address\n");
-            return;
+            return -1;
         }
         endpoint.connectivityType = CA_WIFI;
         strncpy(endpoint.addressInfo.IP.ipAddress, remoteIPAddress, CA_IPADDR_SIZE);
-        endpoint.addressInfo.IP.port = 5283; /* Send the corresponding port here */
+        endpoint.addressInfo.IP.port = 5683; /* Send the corresponding port here */
 
         int sdatalen = tempConnectivityHandlers->handler.sendData(&endpoint, coapData,
                        strlen(coapData));
@@ -373,7 +373,7 @@ int16_t interfaceSendUnicastData(CAConnectivityType_t connType)
         if (strlen(deviceaddress) == 0)
         {
             printf("Invlaid device address\n");
-            return;
+            return -1;
         }
 
         endpoint.connectivityType = CA_EDR;
@@ -382,7 +382,7 @@ int16_t interfaceSendUnicastData(CAConnectivityType_t connType)
         endpoint.resourceUri = NULL;
 
         printf("Sent Unicast data to device: %s\n", endpoint.addressInfo.BT.btMacAddress);
-        tempConnectivityHandlers->handler.sendData(&endpoint, coapData, strlen(coapData)+1);
+        tempConnectivityHandlers->handler.sendData(&endpoint, coapData, strlen(coapData) + 1);
     }
     else if (CA_LE == connType)
     {
@@ -397,7 +397,7 @@ int16_t interfaceSendUnicastData(CAConnectivityType_t connType)
         if (strlen(deviceaddress) == 0)
         {
             printf("Invlaid device address\n");
-            return;
+            return -1;
         }
 
         //Get the service uuid from user
@@ -408,12 +408,12 @@ int16_t interfaceSendUnicastData(CAConnectivityType_t connType)
         if (strlen(uuid) == 0)
         {
             printf("Invlaid service uuid\n");
-            return;
+            return -1;
         }
 
         endpoint.connectivityType = CA_LE;
-        strncpy(endpoint.addressInfo.BT.btMacAddress, deviceaddress, CA_MACADDR_SIZE - 1);
-        endpoint.addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
+        strncpy(endpoint.addressInfo.LE.leMacAddress, deviceaddress, CA_MACADDR_SIZE - 1);
+        endpoint.addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
         endpoint.resourceUri = strdup(uuid);
 
         tempConnectivityHandlers->handler.sendData(&endpoint, coapData, strlen(coapData));
@@ -424,106 +424,6 @@ int16_t interfaceSendUnicastData(CAConnectivityType_t connType)
     return 1;
 }
 
-void interfaceSendNotification(CAConnectivityType_t connType)
-{
-    ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
-    if (NULL == tempConnectivityHandlers)
-    {
-        printf(" None of the interface is initialized \n");
-        return;
-    }
-
-    while (tempConnectivityHandlers && tempConnectivityHandlers->type != connType)
-    {
-        tempConnectivityHandlers = tempConnectivityHandlers->nextHandler;
-    }
-
-    if (NULL == tempConnectivityHandlers)
-    {
-        printf( "No interface handler for type %d\n", connType);
-        return;
-    }
-
-    if (CA_WIFI == connType)
-    {
-        CARemoteEndpoint_t remoteEndpoint;
-        tempConnectivityHandlers->handler.sendNotification(&remoteEndpoint, coapData, strlen(coapData));
-    }
-    else if (CA_EDR == connType)
-    {
-        //create endpoint
-        CARemoteEndpoint_t endpoint;
-
-        //Get the device address from user
-        char deviceaddress[100] = {0};
-        printf("Enter the device address: \n");
-        scanf("%s", deviceaddress);
-
-        if (strlen(deviceaddress) == 0)
-        {
-            printf("Invlaid device address\n");
-            return;
-        }
-
-        //Get the service uuid from user
-        char uuid[100] = {0};
-        printf("Enter the service uuid: \n");
-        scanf("%s", uuid);
-
-        if (strlen(uuid) == 0)
-        {
-            printf("Invlaid service uuid\n");
-            return;
-        }
-
-        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 = strdup(uuid);
-
-        tempConnectivityHandlers->handler.sendNotification(&endpoint, coapData, strlen(coapData));
-        printf("Sent Unicast data \n");
-        free(endpoint.resourceUri);
-    }
-    else if (CA_LE == connType)
-    {
-        //create endpoint
-        CARemoteEndpoint_t endpoint;
-
-        //Get the device address from user
-        char deviceaddress[100] = {0};
-        printf("Enter the device address: \n");
-        scanf("%s", deviceaddress);
-
-        if (strlen(deviceaddress) == 0)
-        {
-            printf("Invlaid device address\n");
-            return;
-        }
-
-        //Get the service uuid from user
-        char uuid[100] = {0};
-        printf("Enter the service uuid: \n");
-        scanf("%s", uuid);
-
-        if (strlen(uuid) == 0)
-        {
-            printf("Invlaid service uuid\n");
-            return;
-        }
-
-        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);
-
-        tempConnectivityHandlers->handler.sendNotification(&endpoint, coapData, strlen(coapData));
-        printf("Sent Unicast data \n");
-        free(endpoint.resourceUri);
-        printf("Feature is not implemented !!\n");
-    }
-}
-
 int16_t interfaceSendMulticastData(CAConnectivityType_t connType)
 {
     ConnectivityHandlerList *tempConnectivityHandlers = gConnectivityHandlers;
@@ -553,6 +453,7 @@ int16_t interfaceSendMulticastData(CAConnectivityType_t connType)
     {
         tempConnectivityHandlers->handler.sendDataToAll(coapData, strlen(coapData));
     }
+    return 0;
 }
 
 void interfaceReadData(CAConnectivityType_t connType)
@@ -881,7 +782,7 @@ void testSendUnicastData()
     }
 }
 
-void testSendNotification()
+void testSendMulticastData()
 {
     int16_t type = selectConnectivityType();
     if (0 >= type || 3 < type)
@@ -895,21 +796,21 @@ void testSendNotification()
 #ifdef WIFI_ADAPTER_TEST
         case 1: //WIFI
             {
-                interfaceSendNotification(CA_WIFI);
+                interfaceSendMulticastData(CA_WIFI);
             }
             break;
 #endif
 #ifdef BT_ADAPTER_TEST
         case 2:   //BT
             {
-                interfaceSendNotification(CA_EDR);
+                interfaceSendMulticastData(CA_EDR);
             }
             break;
 #endif
 #ifdef BLE_ADAPTER_TEST
         case 3: //BLE
             {
-                interfaceSendNotification(CA_LE);
+                interfaceSendMulticastData(CA_LE);
             }
             break;
 #endif
@@ -918,7 +819,7 @@ void testSendNotification()
     }
 }
 
-void testSendMulticastData()
+void testReadData(void)
 {
     int16_t type = selectConnectivityType();
     if (0 >= type || 3 < type)
@@ -932,21 +833,21 @@ void testSendMulticastData()
 #ifdef WIFI_ADAPTER_TEST
         case 1: //WIFI
             {
-                interfaceSendMulticastData(CA_WIFI);
+                interfaceReadData(CA_WIFI);
             }
             break;
 #endif
 #ifdef BT_ADAPTER_TEST
         case 2:   //BT
             {
-                interfaceSendMulticastData(CA_EDR);
+                interfaceReadData(CA_EDR);
             }
             break;
 #endif
 #ifdef BLE_ADAPTER_TEST
         case 3: //BLE
             {
-                interfaceSendMulticastData(CA_LE);
+                interfaceReadData(CA_LE);
             }
             break;
 #endif
@@ -955,43 +856,6 @@ void testSendMulticastData()
     }
 }
 
-void testReadData(void)
-{
-       int16_t type = selectConnectivityType();
-       if (0 >= type || 3 < type)
-       {
-               printf("Invalid selection...\n");
-               return;
-       }
-
-       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");
-       }
-}
-
 void testGetNetworkInfo(void)
 {
     int16_t type = selectConnectivityType();
@@ -1071,12 +935,12 @@ void testInitializeBTInterface(void)
 {
     printf("Initiazing EDR\n");
 
-       printf("Initializing BT Adapter threadpool\n");
-       initializeThreadPool(CA_EDR);
+    printf("Initializing BT Adapter threadpool\n");
+    initializeThreadPool(CA_EDR);
 
     //Start bluetooth communication adapter
     CAResult_t err = CAInitializeEDR(interfaceRegisterCallback, networkPacketHandler,
-                             networkInterfaceCallback, gBTThreadPool);
+                                     networkInterfaceCallback, gBTThreadPool);
     if (CA_STATUS_OK != err && CA_ADAPTER_NOT_ENABLED != err)
     {
         printf("Failed to initialize bluetooth communication adapter!\n");
@@ -1092,6 +956,7 @@ void testTerminateBTInterface(void)
 
     printf( "Terminating BT Adapter thread pool");
     u_thread_pool_free(gBTThreadPool);
+    gBTThreadPool = NULL;
 }
 #endif //BT_ADAPTER_TEST
 
@@ -1100,8 +965,8 @@ void testInitializeWIFIInterface(void)
 {
     printf("testInitializeWIFIInterface IN\n");
 
-       printf("Initializing WIFI adapter threadpool\n");
-       initializeThreadPool(CA_WIFI);
+    printf("Initializing WIFI adapter threadpool\n");
+    initializeThreadPool(CA_WIFI);
 
     //Start Wifi communication adapter
     if (0 != CAInitializeWifi(interfaceRegisterCallback, networkPacketHandler,
@@ -1124,6 +989,7 @@ void testTerminateWIFIInterface(void)
     // Freeing threadpool for wifi communication adapter
     printf( "Terminating WIFI Adapter thread pool");
     u_thread_pool_free(gWiFiThreadPool);
+    gWiFiThreadPool = NULL;
 
     //Terminate the Wifi communication adapter
     CATerminateWIfI();
@@ -1138,8 +1004,8 @@ void testInitializeBLEInterface(void)
 {
     printf("testInitializeBLEInterface IN\n");
 
-       printf("Initializing BLE adapter threadpool\n");
-       initializeThreadPool(CA_LE);
+    printf("Initializing BLE adapter threadpool\n");
+    initializeThreadPool(CA_LE);
 
     //Start bluetooth communication adapter
     if (0 != CAInitializeLE(interfaceRegisterCallback, networkPacketHandler,
@@ -1161,6 +1027,7 @@ void testTerminateBLEInterface(void)
 
     printf( "Terminating BLE Adapter thread pool");
     u_thread_pool_free(gLEThreadPool);
+    gLEThreadPool = NULL;
 
     printf("testTerminateBLEInterface OUT\n");
 }
@@ -1179,14 +1046,12 @@ static void testPrintHelp(void)
     printf("|  b - Stop adapter                                                   |\n");
     printf("|  sd- Start Discovery Server                                         |\n");
     printf("|  sl- Start Listening Server                                         |\n");
-    printf("|  sn- Start Notification Server                                      |\n");
     printf("|  u - Send Unicast Data                                              |\n");
     printf("|  m - Send Multicast Data                                            |\n");
-    printf("|  n - Send Notification 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("|  h - Help menu.                                                     |\n");
     printf(" =====================================================================\n");
 }
 
@@ -1195,7 +1060,6 @@ static gboolean testThread(GIOChannel *source, GIOCondition condition , gpointer
     gchar buf[10] = {'\0'};
     gsize read = 0;
 
-
     if (g_io_channel_read(channel, buf, 10, &read) != G_IO_ERROR_NONE)
     {
         printf("g_io_channel_read error!!!\n");
@@ -1244,10 +1108,6 @@ static gboolean testThread(GIOChannel *source, GIOCondition condition , gpointer
                 {
                     testStartServer(2);
                 }
-                if (buf[1] == 'n')
-                {
-                    testStartServer(3);
-                }
             }
             break;
         case 'u':
@@ -1256,9 +1116,6 @@ static gboolean testThread(GIOChannel *source, GIOCondition condition , gpointer
         case 'm':
             testSendMulticastData();
             break;
-        case 'n':
-            testSendNotification();
-            break;
         case 'r':
             testReadData();
             break;