Fix dlog format error
[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 <stdio.h>
28 #include <arpa/inet.h>
29 #include <net/route.h>
30 #include <net/ethernet.h>
31 #include <net/if.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <sys/ioctl.h>
35 #include <vconf.h>
36 #include <vconf-keys.h>
37
38 #define ETHERNET_CABLE_STATUS   "/sys/class/net/eth0/carrier"
39
40 /* Check send notification status */
41 static gboolean g_chk_eth_send_notification = FALSE;
42
43 int netconfig_ethernet_cable_plugin_status_check()
44 {
45         int ret = -1;
46         FILE *fd = NULL;
47         char error_buf[MAX_SIZE_ERROR_BUFFER] = {0, };
48         if (0 == access(ETHERNET_CABLE_STATUS, F_OK)) {
49                 fd = fopen(ETHERNET_CABLE_STATUS, "r");
50                 if (fd == NULL) {
51                         ERR("Error! Could not open /sys/class/net/eth0/carrier file\n");
52                         return -1;
53                 }
54         } else {
55                 ERR("Error! Could not access /sys/class/net/eth0/carrier file\n");
56                 return -1;
57         }
58
59         int rv = 0;
60         errno = 0;
61         rv = fscanf(fd, "%d", &ret);
62         if (rv < 0) {
63                 ERR("Error! Failed to read from file, rv:[%d], error:[%s]",
64                                 rv, strerror_r(errno, error_buf, MAX_SIZE_ERROR_BUFFER));
65                 fclose(fd);
66                 return -1;
67         }
68
69         if (ret == 1) {
70                 if (!g_chk_eth_send_notification) {
71                         ERR("/sys/class/net/eth0/carrier : [%d]", ret);
72                         netconfig_network_notify_ethernet_cable_state("ATTACHED");
73                 }
74                 g_chk_eth_send_notification = TRUE;
75         } else if (ret == 0) {
76                 if (g_chk_eth_send_notification) {
77                         ERR("/sys/class/net/eth0/carrier : [%d]", ret);
78                         netconfig_network_notify_ethernet_cable_state("DETACHED");
79                 }
80                 g_chk_eth_send_notification = FALSE;
81         }
82
83         fclose(fd);
84         return 0;
85 }
86
87 int netconfig_get_ethernet_cable_state(int *status)
88 {
89         int error = 0;
90         if (status == NULL) {
91                 DBG("Error !!! Invalid Parameter\n");
92                 return -1;
93         }
94
95         if ((error = netconfig_ethernet_cable_plugin_status_check()) != 0) {
96                 DBG("Error !!! Failed to check ethernet cable status [%d]\n", error);
97                 return -1;
98         }
99
100         if (g_chk_eth_send_notification == TRUE)
101                 *status = 1;            /* Ethernet cable Attached */
102         else
103                 *status = 0;            /* Ethernet cable Deattached */
104         return 0;
105 }