Apply coding rule
[platform/core/connectivity/net-config.git] / src / network-monitor.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
5  *
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  */
19
20 #include "log.h"
21 #include "util.h"
22 #include "netdbus.h"
23 #include "network-monitor.h"
24 #include "network-state.h"
25 #include "wifi-power.h"
26
27 #include <arpa/inet.h>
28 #include <net/route.h>
29 #include <net/ethernet.h>
30 #include <net/if.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <sys/ioctl.h>
34 #include <vconf.h>
35 #include <vconf-keys.h>
36
37 /* Check send notification status */
38 static gboolean g_chk_eth_send_notification = FALSE;
39
40 int netconfig_ethernet_cable_plugin_status_check()
41 {
42         struct ifreq ifr;
43         int soketfd = -1;
44         int error = 0;
45         int ret = 0;
46         struct _stMData *mdata;
47         struct timeval tv;
48         char error_buf[MAX_SIZE_ERROR_BUFFER] = {};
49
50         soketfd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
51         if (soketfd < 0) {
52                 ERR("Failed to create socket");
53                 return -errno;
54         }
55
56         /* Set Timeout for IOCTL Call */
57         tv.tv_sec = 1;
58         tv.tv_usec = 0;
59
60         if (setsockopt(soketfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
61                         sizeof(struct timeval)) < 0) {
62
63                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
64                 ERR("Failed to set socket option : [%d] [%s]", -errno, error_buf);
65                 goto done;
66         }
67
68         memset(&ifr, 0, sizeof(ifr));
69         g_strlcpy(ifr.ifr_name, "eth0", IFNAMSIZ);
70         if (ioctl(soketfd, SIOCGMIIPHY, &ifr) < 0) {
71                 error = -errno;
72                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
73                 ERR("SIOCGMIIPHY on eth0 failed : [%d] [%s]", errno, error_buf);
74                 goto done;
75         }
76
77         mdata = (struct _stMData *)&ifr.ifr_data;
78         mdata->reg_num = ETH_REG_BMSR;
79
80         if (ioctl(soketfd, SIOCGMIIREG, &ifr) < 0) {
81                 error = -errno;
82                 strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER);
83                 ERR("SIOCGMIIREG on %s failed , [%d] [%s] ", ifr.ifr_name, errno, error_buf);
84                 goto done;
85         }
86         ret = mdata->val_out;
87         ret = ret & BMSR_LINK_VALID;
88
89         if (ret == 4) {
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;
97         }
98         error = 0;
99 done:
100         close(soketfd);
101         return error;
102 }
103
104 int netconfig_get_ethernet_cable_state(int *status)
105 {
106         int error = 0;
107         if (status == NULL) {
108                 DBG("Error !!! Invalid Parameter\n");
109                 return -1;
110         }
111
112         if ((error = netconfig_ethernet_cable_plugin_status_check()) != 0) {
113                 DBG("Error !!! Failed to check ethernet cable status [%d]\n", error);
114                 return -1;
115         }
116
117         if (g_chk_eth_send_notification == TRUE)
118                 *status = 1;            /* Ethernet cable Attached */
119         else
120                 *status = 0;            /* Ethernet cable Deattached */
121         return 0;
122 }