2 * Network Configuration Module
4 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
23 #include "network-monitor.h"
24 #include "network-state.h"
25 #include "wifi-power.h"
27 #include <arpa/inet.h>
28 #include <net/route.h>
29 #include <net/ethernet.h>
33 #include <sys/ioctl.h>
35 #include <vconf-keys.h>
37 /* Check send notification status */
38 static gboolean g_chk_eth_send_notification = FALSE;
40 int netconfig_ethernet_cable_plugin_status_check()
46 struct _stMData *mdata;
48 char error_buf[MAX_SIZE_ERROR_BUFFER] = {};
50 soketfd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
52 ERR("Failed to create socket");
56 /* Set Timeout for IOCTL Call */
60 if (setsockopt(soketfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
61 sizeof(struct timeval)) < 0) {
63 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
64 ERR("Failed to set socket option : [%d] [%s]", -errno, error_buf);
68 memset(&ifr, 0, sizeof(ifr));
69 g_strlcpy(ifr.ifr_name, "eth0", IFNAMSIZ);
70 if (ioctl(soketfd, SIOCGMIIPHY, &ifr) < 0) {
72 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
73 ERR("SIOCGMIIPHY on eth0 failed : [%d] [%s]", errno, error_buf);
77 mdata = (struct _stMData *)&ifr.ifr_data;
78 mdata->reg_num = ETH_REG_BMSR;
80 if (ioctl(soketfd, SIOCGMIIREG, &ifr) < 0) {
82 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
83 ERR("SIOCGMIIREG on %s failed , [%d] [%s] ", ifr.ifr_name, errno, error_buf);
87 ret = ret & BMSR_LINK_VALID;
90 if (!g_chk_eth_send_notification)
91 netconfig_network_notify_ethernet_cable_state("ATTACHED");
92 g_chk_eth_send_notification = TRUE;
93 } else if (ret == 0) {
94 if (g_chk_eth_send_notification)
95 netconfig_network_notify_ethernet_cable_state("DETACHED");
96 g_chk_eth_send_notification = FALSE;
104 int netconfig_get_ethernet_cable_state(int *status)
107 if (status == NULL) {
108 DBG("Error !!! Invalid Parameter\n");
112 if ((error = netconfig_ethernet_cable_plugin_status_check()) != 0) {
113 DBG("Error !!! Failed to check ethernet cable status [%d]\n", error);
117 if (g_chk_eth_send_notification == TRUE)
118 *status = 1; /* Ethernet cable Attached */
120 *status = 0; /* Ethernet cable Deattached */