[net-config] Added support for EAPoL connection.
[platform/core/connectivity/net-config.git] / src / main.c
1 /*
2  * Network Configuration Module
3  *
4  * Copyright (c) 2000 - 2012 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 <stdio.h>
21 #include <errno.h>
22 #include <unistd.h>
23 #include <sys/stat.h>
24 #include <system_info.h>
25
26 #include "log.h"
27 #include "util.h"
28 #include "wifi.h"
29 #include "netdbus.h"
30 #include "emulator.h"
31 #include "neterror.h"
32 #include "wifi-agent.h"
33 #include "wifi-power.h"
34 #include "vpnsvc.h"
35 #include "mptcp.h"
36 #include "network-clock.h"
37 #include "network-dpm.h"
38 #include "network-state.h"
39 #include "network-monitor.h"
40 #include "signal-handler.h"
41 #include "network-statistics.h"
42 #include "network-dump.h"
43 #include "ethernet.h"
44
45 static GMainLoop *main_loop = NULL;
46
47 /*Poll the ethernet Cable Plug-in /Plug-out status at every 1000 ms*/
48 #define ETH_POLLING_TIME       1000
49
50 /* Callback to Poll the Ethernet Status*/
51 gboolean __net_ethernet_cable_status_polling_callback(gpointer data)
52 {
53         netconfig_ethernet_cable_plugin_status_check();
54         return TRUE;
55 }
56
57 void _got_name_cb(void)
58 {
59         wifi_object_create_and_init();
60         state_object_create_and_init();
61         statistics_object_create_and_init();
62         vpnsvc_create_and_init();
63         mptcp_object_create_and_init();
64         ethernet_object_create_and_init();
65
66         register_gdbus_signal();
67         connman_register_agent();
68
69         if (TIZEN_TV)
70                 __netconfig_set_ether_macaddr();
71 }
72
73 static void _objects_deinit(void)
74 {
75         cleanup_gdbus();
76         wifi_object_deinit();
77         state_object_deinit();
78         statistics_object_deinit();
79         vpnsvc_destroy_deinit();
80         mptcp_object_deinit();
81         ethernet_object_deinit();
82 }
83
84 int main(int argc, char *argv[])
85 {
86         int ret;
87         int check_ethernet_monitor_timer = 0;
88
89         umask(0077);
90
91         DBG("Network Configuration service");
92         if (daemon(0, 0) != 0)
93                 DBG("Cannot start daemon");
94
95         if (mkdir(WIFI_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
96                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
97                 if (errno != EEXIST)
98                         ERR("Failed to create Wi-Fi directory");
99         }
100
101         if (mkdir(WIFI_CERT_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
102                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
103                 if (errno != EEXIST)
104                         ERR("Failed to create cert directory");
105         }
106
107 #if !GLIB_CHECK_VERSION(2, 36, 0)
108         g_type_init();
109 #endif
110
111         /*
112          * TODO: Remove this temp change to start wpa_supplicant
113          * when ConnMan device some mechanism for EAP on Ethernet.
114          */
115         __execute_supplicant(true);
116
117         main_loop = g_main_loop_new(NULL, FALSE);
118         if (main_loop == NULL) {
119                 ERR("Couldn't create GMainLoop\n");
120                 return 0;
121         }
122
123         ret = setup_gdbus(_got_name_cb);
124         if (ret > 0) {
125                 ERR("_netconfig_setup_gdbus is failed\n");
126                 return 0;
127         }
128
129         netconfig_error_init();
130
131         if (TIZEN_NTP_ENABLE)
132                 netconfig_clock_init();
133
134         /* If its environment uses Emulator, network configuration is set by emulator default */
135         emulator_test_and_start();
136
137         /* For device policy manager */
138         netconfig_dpm_init();
139
140         /* Start tcpdump if dump state is on */
141 #if defined TIZEN_DEBUG_ENABLE
142         check_dump_state_and_start();
143 #endif
144
145         if (TIZEN_WLAN_BOARD_SPRD)
146                 wifi_firmware_download();
147
148         /*In case no emulator, set the ETH0 Mac address*/
149         if (TIZEN_TV && emulator_is_emulated() == FALSE)
150                 __netconfig_set_ether_macaddr();
151
152         if (netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_ETHERNET)) {
153                 /* Register the callback to check the ethernet Plug-in /Plug-out Status */
154                 check_ethernet_monitor_timer = g_timeout_add(ETH_POLLING_TIME,
155                                 __net_ethernet_cable_status_polling_callback,
156                                 &check_ethernet_monitor_timer);
157         }
158
159         netconfig_plugin_init();
160
161         g_main_loop_run(main_loop);
162
163         netconfig_plugin_deinit();
164
165         _objects_deinit();
166
167         log_cleanup();
168
169         deregister_gdbus_signal();
170
171         netconfig_dpm_deinit();
172
173         if (TIZEN_NTP_ENABLE)
174                 netconfig_clock_deinit();
175
176
177         /*remove the Timer*/
178         if (check_ethernet_monitor_timer > 0)
179                 g_source_remove(check_ethernet_monitor_timer);
180
181         wifi_state_notifier_cleanup();
182
183         /* Unregistering the agent */
184         connman_unregister_agent();
185
186         return 0;
187 }