Add API(ethernet cable state) 51/39951/1
authortaesub.kim <taesub.kim@samsung.com>
Wed, 27 May 2015 00:33:06 +0000 (09:33 +0900)
committertaesub.kim <taesub.kim@samsung.com>
Wed, 27 May 2015 00:33:15 +0000 (09:33 +0900)
Change-Id: Idca74e003d61c7163b0cf1dfd11b764555e404ae
Signed-off-by: Taesub Kim <taesub.kim@samsung.com>
CMakeLists.txt
include/netdbus.h
include/network-monitor.h [new file with mode: 0755]
include/network-state.h
include/util.h
interfaces/netconfig-iface-network-state.xml
packaging/net-config.spec
src/main.c
src/network-monitor.c [new file with mode: 0755]
src/network-state.c

index 2bdc82c..81c9901 100644 (file)
@@ -16,6 +16,7 @@ SET(SRCS
        src/wifi-passpoint.c
        src/network-clock.c
        src/network-state.c
+       src/network-monitor.c
        src/network-statistics.c
        src/wifi-indicator.c
        src/signal-handler.c
index 8f58900..db577e1 100644 (file)
@@ -47,6 +47,8 @@ extern "C" {
 
 #define NETCONFIG_WIFI_INTERFACE                       "net.netconfig.wifi"
 #define NETCONFIG_WIFI_PATH                            "/net/netconfig/wifi"
+#define NETCONFIG_NETWORK_PATH                 "/net/netconfig/network"
+#define NETCONFIG_NETWORK_INTERFACE            "net.netconfig.network"
 
 #define DBUS_PATH_MAX_BUFLEN           512
 #define DBUS_STATE_MAX_BUFLEN          64
diff --git a/include/network-monitor.h b/include/network-monitor.h
new file mode 100755 (executable)
index 0000000..16ed406
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Network Configuration Module
+ *
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd. 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.
+ *
+ */
+
+#ifndef __NETCONFIG_NETWORK_MONITOR_H__
+#define __NETCONFIG_NETWORK_MONITOR_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define ETH_REG_BMSR           0x01
+#define BMSR_LINK_VALID                0x0004
+
+#define SIOCGMIIPHY            0x8947          /* Get address of MII PHY in use. */
+#define SIOCGMIIREG            0x8948          /* Read MII PHY register.       */
+
+struct _stMData {
+    unsigned short phy_id;
+    unsigned short reg_num;
+    unsigned short val_in;
+    unsigned short val_out;
+};
+
+int netconfig_ethernet_cable_plugin_status_check();
+int netconfig_get_ethernet_cable_state(int *status);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __NETCONFIG_NETWORK_MONITOR_H__ */
index 0f180c8..8833695 100644 (file)
@@ -43,6 +43,7 @@ typedef struct NetconfigNetworkStateClass     NetconfigNetworkStateClass;
 GType netconfig_network_state_get_type(void);
 
 gpointer netconfig_network_state_create_and_init(DBusGConnection *conn);
+void netconfig_network_notify_ethernet_cable_state(const char *key);
 
 const char *netconfig_get_default_profile(void);
 const char *netconfig_get_default_ipaddress(void);
index 8a37f8a..c6c9b8d 100644 (file)
@@ -28,6 +28,8 @@ extern "C" {
 
 #include "wifi.h"
 
+#define MAX_SIZE_ERROR_BUFFER 256
+
 GKeyFile *netconfig_keyfile_load(const char *pathname);
 void netconfig_keyfile_save(GKeyFile *keyfile, const char *pathname);
 
index d2dae90..c5ba188 100644 (file)
@@ -13,5 +13,8 @@
                        <arg  type="s" name="interface" direction="in" />
                        <arg  type="b" name="result" direction="out" />
                </method>
+               <method name="EthernetCableState">
+                       <arg  type="i" name="state" direction="out"/>
+               </method>
        </interface>
 </node>
index a3a97b9..2137b88 100644 (file)
@@ -1,6 +1,6 @@
 Name:       net-config
 Summary:    TIZEN Network Configuration Module
-Version:    0.1.90_30
+Version:    0.1.90_31
 Release:    1
 Group:      System/Network
 License:    Apache-2.0
index ccdc669..e9e019c 100644 (file)
  *
  */
 
+#include <stdio.h>
 #include <systemd/sd-daemon.h>
 #include <getopt.h>
 #include <unistd.h>
 #include <signal.h>
 #include <errno.h>
+//#include <system_info.h>
 
 #include "log.h"
 #include "wifi.h"
 #include "network-statistics.h"
 #include "signal-handler.h"
 #include "wifi-agent.h"
+#include "network-monitor.h"
 
 static GMainLoop *main_loop = NULL;
 
+//#define ETHERNET_FEATURE     "http://tizen.org/feature/network.ethernet"
 static int no_fork = FALSE;
 
+/*Poll the ethernet Cable Plug-in /Plug-out status at every 1000 ms*/
+#define ETH_POLLING_TIME       1000
+
+/* Callback to Poll the Ethernet Status*/
+gboolean __net_ethernet_cable_status_polling_callback(gpointer data)
+{
+       netconfig_ethernet_cable_plugin_status_check();
+       return TRUE;
+}
+
 void netconfig_signal_handler_SIGTERM(int signum)
 {
        g_main_loop_quit(main_loop);
@@ -81,6 +95,8 @@ int netconfig_test_input_parameters(int argc, char* argv[])
 int main(int argc, char* argv[])
 {
        DBusGConnection *connection;
+       int check_ethernet_monitor_timer = 0;
+       //bool ethernet_feature_supported = FALSE;
 
        DBG("Network Configuration Module");
 
@@ -124,6 +140,18 @@ int main(int argc, char* argv[])
 
        /* If its environment uses Emulator, network configuration is set by emulator default */
        netconfig_emulator_test_and_start();
+/*
+       if (!system_info_get_platform_bool(ETHERNET_FEATURE, &ethernet_feature_supported)) {
+               if (ethernet_feature_supported == TRUE) {
+                       //Register the callback to check the ethernet Plug-in /Plug-out Status
+                       check_ethernet_monitor_timer = g_timeout_add(ETH_POLLING_TIME,
+                                       __net_ethernet_cable_status_polling_callback,
+                                       &check_ethernet_monitor_timer);
+               }
+       } else {
+               ERR("Error - Feature getting from System Info");
+       }
+*/
 
        // Notyfication to systemd
        sd_notify(0, "READY=1");
@@ -133,6 +161,10 @@ int main(int argc, char* argv[])
        netconfig_deregister_signal();
        netconfig_wifi_state_notifier_cleanup();
 
+       /*remove the Timer*/
+       if(check_ethernet_monitor_timer >0)
+               g_source_remove(check_ethernet_monitor_timer);
+
        /* Unregistering the agent */
        netconfig_agent_unregister();
 
diff --git a/src/network-monitor.c b/src/network-monitor.c
new file mode 100755 (executable)
index 0000000..29f8772
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ * Network Configuration Module
+ *
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd. 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 "log.h"
+#include "util.h"
+#include "netdbus.h"
+#include "network-monitor.h"
+#include "network-state.h"
+#include "wifi-power.h"
+
+#include <arpa/inet.h>
+#include <net/route.h>
+#include <net/ethernet.h>
+#include <net/if.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <vconf.h>
+#include <vconf-keys.h>
+
+/* Check send notification status */
+static gboolean g_chk_eth_send_notification = FALSE;
+
+int netconfig_ethernet_cable_plugin_status_check()
+{
+       struct ifreq ifr;
+       int soketfd = -1;
+       int error = 0;
+       int ret = 0;
+       struct _stMData *mdata;
+       struct timeval tv;
+       char error_buf[MAX_SIZE_ERROR_BUFFER] = {};
+
+       soketfd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+       if (soketfd < 0) {
+               ERR("Failed to create socket");
+               return -errno;
+       }
+
+       /* Set Timeout for IOCTL Call */
+       tv.tv_sec = 1;
+       tv.tv_usec = 0;
+
+       if (setsockopt(soketfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
+                       sizeof(struct timeval)) < 0) {
+
+               strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
+               ERR("Failed to set socket option : [%d] [%s]", -errno, error_buf);
+               goto done;
+       }
+
+       memset(&ifr, 0, sizeof(ifr));
+       g_strlcpy(ifr.ifr_name, "eth0", IFNAMSIZ);
+       if (ioctl(soketfd, SIOCGMIIPHY, &ifr) < 0){
+               error = -errno;
+               strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
+               ERR("SIOCGMIIPHY on eth0 failed : [%d] [%s]", errno, error_buf);
+               goto done;
+       }
+
+       mdata = (struct _stMData *)&ifr.ifr_data;
+       mdata->reg_num = ETH_REG_BMSR;
+
+       if (ioctl(soketfd, SIOCGMIIREG, &ifr) < 0){
+               error = -errno;
+               strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
+               ERR("SIOCGMIIREG on %s failed , [%d] [%s] ", ifr.ifr_name,errno,error_buf);
+               goto done;
+       }
+       ret = mdata->val_out;
+       ret = ret & BMSR_LINK_VALID;
+
+       if(ret == 4) { 
+               if(!g_chk_eth_send_notification)
+                       netconfig_network_notify_ethernet_cable_state("ATTACHED");
+               g_chk_eth_send_notification = TRUE;
+       } else if (ret == 0) {
+               if(g_chk_eth_send_notification)
+                       netconfig_network_notify_ethernet_cable_state("DETACHED");
+               g_chk_eth_send_notification = FALSE;
+       }
+       error = 0;
+done:
+       close(soketfd);
+       return error;
+}
+
+int netconfig_get_ethernet_cable_state(int *status)
+{
+       int error = 0;
+       if(status == NULL) {
+               DBG("Error !!! Invalid Parameter\n");
+               return -1;
+       }
+
+       if((error = netconfig_ethernet_cable_plugin_status_check()) != 0) {
+               DBG("Error !!! Failed to check ethernet cable status [%d]\n", error);
+               return -1;
+       }
+
+       if(g_chk_eth_send_notification == TRUE)
+               *status = 1;            /* Ethernet cable Attached */
+       else
+               *status = 0;            /* Ethernet cable Deattached */
+       return 0;
+}
index d890e1d..f753213 100644 (file)
@@ -27,6 +27,7 @@
 #include "neterror.h"
 #include "emulator.h"
 #include "wifi-state.h"
+#include "network-monitor.h"
 #include "network-state.h"
 
 #define NETCONFIG_NETWORK_STATE_PATH   "/net/netconfig/network"
@@ -46,6 +47,9 @@ gboolean netconfig_iface_network_state_remove_route(
                gchar *ip_addr, gchar *netmask,
                gchar *interface, gboolean *result, GError **error);
 
+gboolean netconfig_iface_network_state_ethernet_cable_state(
+               NetconfigNetworkState *master, gint32 *state, GError **error);
+
 #include "netconfig-iface-network-state-glue.h"
 
 enum {
@@ -488,6 +492,54 @@ static void __netconfig_update_default_connection_info(void)
        }
 }
 
+static void __netconfig_network_notify_result(const char *sig_name, const char *key)
+{
+       DBusMessage *signals;
+       DBusConnection *connection = NULL;
+       DBusMessageIter iter, dict_iter, type, value;
+       DBusError error;
+       const char *prop_key = "key";
+
+       dbus_error_init(&error);
+
+       connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
+       if (connection == NULL) {
+               ERR("Error!!! Failed to get system DBus, error [%s]", error.message);
+               dbus_error_free(&error);
+
+               return;
+       }
+
+       signals = dbus_message_new_signal(NETCONFIG_NETWORK_PATH,
+                       NETCONFIG_NETWORK_INTERFACE, sig_name);
+       if (signals == NULL) {
+               dbus_connection_unref(connection);
+               return;
+       }
+
+       dbus_message_iter_init_append(signals, &iter);
+       dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}",
+                       &dict_iter);
+
+       /* Packing the key */
+       dbus_message_iter_open_container(&dict_iter, DBUS_TYPE_DICT_ENTRY, 0, &type);
+       dbus_message_iter_append_basic(&type, DBUS_TYPE_STRING, &prop_key);
+       dbus_message_iter_open_container(&type, DBUS_TYPE_VARIANT, DBUS_TYPE_STRING_AS_STRING, &value);
+
+       dbus_message_iter_append_basic(&value, DBUS_TYPE_STRING, &key);
+       dbus_message_iter_close_container(&type, &value);
+       dbus_message_iter_close_container(&dict_iter, &type);
+
+       dbus_message_iter_close_container(&iter, &dict_iter);
+
+       dbus_connection_send(connection, signals, NULL);
+
+       dbus_message_unref(signals);
+       dbus_connection_unref(connection);
+       ERR("Sent signal (%s), key (%s)", sig_name, key);
+       return;
+}
+
 const char *netconfig_get_default_profile(void)
 {
        return netconfig_default_connection_info.profile;
@@ -566,6 +618,12 @@ void netconfig_set_default_profile(const char *profile)
        __netconfig_update_default_connection_info();
 }
 
+/* Check Ethernet Cable Plug-in /Plug-out Status */
+void netconfig_network_notify_ethernet_cable_state(const char *key)
+{
+       __netconfig_network_notify_result("EthernetCableState", key);
+}
+
 gboolean netconfig_iface_network_state_add_route(
                NetconfigNetworkState *master,
                gchar *ip_addr, gchar *netmask,
@@ -638,6 +696,21 @@ done:
        return ret;
 }
 
+gboolean netconfig_iface_network_state_ethernet_cable_state(
+               NetconfigNetworkState *master, gint32 *state, GError **error)
+{
+       int ret = 0;
+
+       ret = netconfig_get_ethernet_cable_state(state);
+       if(ret != 0) {
+               DBG("Failed to get ethernet cable state");
+               return FALSE;
+       }
+
+       DBG("Successfully get ethernet cable state[%d]", state);
+       return TRUE;
+}
+
 gpointer netconfig_network_state_create_and_init(DBusGConnection *conn)
 {
        GObject *object;