Update snapshot(2017-12-06)
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / ip_adapter / tizen / caipnwmonitor.c
index 5486f32..079ecd5 100644 (file)
@@ -18,8 +18,6 @@
 *
 ******************************************************************/
 
-#include "caipinterface.h"
-
 #include <sys/types.h>
 #include <ifaddrs.h>
 #include <net/if.h>
 #include <errno.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
-#include <wifi.h>
 #include <linux/netlink.h>
 #include <linux/rtnetlink.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
+#include <net_connection.h>
 
+#include "caipinterface.h"
 #include "caipnwmonitor.h"
 #include "caadapterutils.h"
 #include "logger.h"
 #include "oic_string.h"
 #include <coap/utlist.h>
 
-#define TAG "IP_MONITOR"
+#define TAG "OIC_CA_IP_MONITOR"
+#define MAX_INTERFACE_INFO_LENGTH (1024)
 #define NETLINK_MESSAGE_LENGTH  (4096)
 #define IFC_LABEL_LOOP          "lo"
 #define IFC_ADDR_LOOP_IPV4      "127.0.0.1"
 #define IFC_ADDR_LOOP_IPV6      "::1"
 
 /**
+ * Used to storing a connection handle for managing data connections.
+ */
+static connection_h connection = NULL;
+
+/**
  * Used to storing adapter changes callback interface.
  */
 static struct CAIPCBData_t *g_adapterCallbackList = NULL;
@@ -73,13 +78,7 @@ static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status);
 /**
  * Callback function to received connection state changes.
  */
-static void CAWIFIConnectionStateChangedCb(wifi_connection_state_e state, wifi_ap_h ap,
-                                           void *userData);
-
-/**
- * Callback function to received device state changes.
- */
-static void CAWIFIDeviceStateChangedCb(wifi_device_state_e state, void *userData);
+static void CAIPConnectionStateChangedCb(connection_type_e type, void* userData);
 
 int CAGetPollingInterval(int interval)
 {
@@ -160,30 +159,34 @@ u_arraylist_t *CAFindInterfaceChange()
                           .msg_iov = &iov,
                           .msg_iovlen = 1 };
 
-    size_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
+    ssize_t len = recvmsg(caglobals.ip.netlinkFd, &msg, 0);
 
     for (struct nlmsghdr *nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, len); nh = NLMSG_NEXT(nh, len))
     {
-        if (nh != NULL && nh->nlmsg_type != RTM_NEWLINK)
+        if (nh != NULL && (nh->nlmsg_type != RTM_DELADDR && nh->nlmsg_type != RTM_NEWADDR))
         {
             continue;
         }
-        struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
 
-        if ((!ifi || (ifi->ifi_flags & IFF_LOOPBACK) || !(ifi->ifi_flags & IFF_RUNNING)))
+        struct ifinfomsg *ifi = (struct ifinfomsg *)NLMSG_DATA(nh);
+        if (!ifi)
         {
             continue;
         }
 
-        int ifiIndex = ifi->ifi_index;
+        if (RTM_DELADDR == nh->nlmsg_type)
+        {
+            CloseMulticastSocket();
+        }
 
+        int ifiIndex = ifi->ifi_index;
         iflist = CAIPGetInterfaceInformation(ifiIndex);
-
         if (!iflist)
         {
             OIC_LOG_V(ERROR, TAG, "get interface info failed: %s", strerror(errno));
             return NULL;
         }
+        CreateMulticastSocket();
     }
     return iflist;
 }
@@ -191,35 +194,26 @@ u_arraylist_t *CAFindInterfaceChange()
 CAResult_t CAIPStartNetworkMonitor(CAIPAdapterStateChangeCallback callback,
                                    CATransportAdapter_t adapter)
 {
-    OIC_LOG(DEBUG, TAG, "IN");
-
     if (!g_adapterCallbackList)
     {
-        // Initialize Wifi service
-       wifi_error_e ret = wifi_initialize();
-       if (WIFI_ERROR_NONE != ret)
-       {
-           OIC_LOG(ERROR, TAG, "wifi_initialize failed");
-           return CA_STATUS_FAILED;
-       }
-
-       // Set callback for receiving state changes
-       ret = wifi_set_device_state_changed_cb(CAWIFIDeviceStateChangedCb, NULL);
-       if (WIFI_ERROR_NONE != ret)
-       {
-           OIC_LOG(ERROR, TAG, "wifi_set_device_state_changed_cb failed");
-           return CA_STATUS_FAILED;
-       }
-
-       // Set callback for receiving connection state changes
-       ret = wifi_set_connection_state_changed_cb(CAWIFIConnectionStateChangedCb, NULL);
-       if (WIFI_ERROR_NONE != ret)
-       {
-           OIC_LOG(ERROR, TAG, "wifi_set_connection_state_changed_cb failed");
-           return CA_STATUS_FAILED;
-       }
+        // Initialize Connections.
+        connection_error_e ret = connection_create(&connection);
+        if (CONNECTION_ERROR_NONE != ret)
+        {
+            OIC_LOG(ERROR, TAG, "connection_create failed");
+            return CA_STATUS_FAILED;
+        }
+
+        // Set callback for receiving state changes.
+        ret = connection_set_type_changed_cb(connection, CAIPConnectionStateChangedCb, NULL);
+        if (CONNECTION_ERROR_NONE != ret)
+        {
+            OIC_LOG(ERROR, TAG, "connection_set_type_changed_cb failed");
+            return CA_STATUS_FAILED;
+        }
     }
 
+    OIC_LOG(DEBUG, TAG, "Initialize network monitoring successfully");
     return CAIPSetNetworkMonitorCallback(callback, adapter);
 }
 
@@ -230,28 +224,26 @@ CAResult_t CAIPStopNetworkMonitor(CATransportAdapter_t adapter)
     CAIPUnSetNetworkMonitorCallback(adapter);
     if (!g_adapterCallbackList)
     {
-        // Reset callback for receiving state changes
-       wifi_error_e ret = wifi_unset_device_state_changed_cb();
-       if (WIFI_ERROR_NONE != ret)
-       {
-           OIC_LOG(ERROR, TAG, "wifi_unset_device_state_changed_cb failed");
-       }
-
-       // Reset callback for receiving connection state changes
-       ret = wifi_unset_connection_state_changed_cb();
-       if (WIFI_ERROR_NONE != ret)
-       {
-           OIC_LOG(ERROR, TAG, "wifi_unset_connection_state_changed_cb failed");
-       }
-
-       // Deinitialize Wifi service
-       ret = wifi_deinitialize();
-       if (WIFI_ERROR_NONE != ret)
-       {
-           OIC_LOG(ERROR, TAG, "wifi_deinitialize failed");
-       }
+        // Reset callback for receiving state changes.
+        if (connection)
+        {
+            connection_error_e ret = connection_unset_type_changed_cb(connection);
+            if (CONNECTION_ERROR_NONE != ret)
+            {
+                OIC_LOG(ERROR, TAG, "connection_unset_type_changed_cb failed");
+            }
+
+            // Deinitialize Wifi service.
+            ret = connection_destroy(connection);
+            if (CONNECTION_ERROR_NONE != ret)
+            {
+                OIC_LOG(ERROR, TAG, "connection_destroy failed");
+            }
+            connection = NULL;
+        }
     }
 
+    OIC_LOG(DEBUG, TAG, "Network monitoring terminated successfully");
     return CA_STATUS_OK;
 }
 
@@ -392,43 +384,29 @@ static CAInterface_t *CANewInterfaceItem(int index, char *name, int family,
     return ifitem;
 }
 
-void CAWIFIConnectionStateChangedCb(wifi_connection_state_e state, wifi_ap_h ap,
-                                    void *userData)
+void CAIPConnectionStateChangedCb(connection_type_e type, void* userData)
 {
-    OIC_LOG(DEBUG, TAG, "IN");
+    (void)userData;
 
-    if (WIFI_CONNECTION_STATE_ASSOCIATION == state
-        || WIFI_CONNECTION_STATE_CONFIGURATION == state)
+    switch (type)
     {
-        OIC_LOG(DEBUG, TAG, "Connection is in Association State");
-        return;
+        case CONNECTION_TYPE_DISCONNECTED:
+            OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_DISCONNECTED");
+            CAIPPassNetworkChangesToAdapter(CA_INTERFACE_DOWN);
+            break;
+        case CONNECTION_TYPE_ETHERNET:
+            OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_ETHERNET");
+            CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);
+            break;
+        case CONNECTION_TYPE_WIFI:
+            OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_WIFI");
+            CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);
+            break;
+        case CONNECTION_TYPE_CELLULAR:
+            OIC_LOG(DEBUG, TAG, "Connection is in CONNECTION_TYPE_CELLULAR");
+            CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);
+            break;
+        default:
+            break;
     }
-
-    if (WIFI_CONNECTION_STATE_CONNECTED == state)
-    {
-        CAIPPassNetworkChangesToAdapter(CA_INTERFACE_UP);
-    }
-    else
-    {
-        CAIPPassNetworkChangesToAdapter(CA_INTERFACE_DOWN);
-    }
-
-    OIC_LOG(DEBUG, TAG, "OUT");
-}
-
-void CAWIFIDeviceStateChangedCb(wifi_device_state_e state, void *userData)
-{
-    OIC_LOG(DEBUG, TAG, "IN");
-
-    if (WIFI_DEVICE_STATE_ACTIVATED == state)
-    {
-        OIC_LOG(DEBUG, TAG, "Wifi is in Activated State");
-    }
-    else
-    {
-        CAWIFIConnectionStateChangedCb(WIFI_CONNECTION_STATE_DISCONNECTED, NULL, NULL);
-        OIC_LOG(DEBUG, TAG, "Wifi is in Deactivated State");
-    }
-
-    OIC_LOG(DEBUG, TAG, "OUT");
 }