Rectify gvariant type for frequency
[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 "setting.h"
30 #include "netdbus.h"
31 #include "emulator.h"
32 #include "neterror.h"
33 #include "wifi-agent.h"
34 #include "wifi-power.h"
35 #include "vpnsvc.h"
36 #include "mptcp.h"
37 #include "network-clock.h"
38 #include "network-dpm.h"
39 #include "network-state.h"
40 #include "network-monitor.h"
41 #include "signal-handler.h"
42 #include "network-statistics.h"
43 #include "network-dump.h"
44 #include "ethernet.h"
45 #include "wifi-firmware.h"
46
47 static GMainLoop *main_loop = NULL;
48
49 /*Poll the ethernet Cable Plug-in /Plug-out status at every 1000 ms*/
50 #define ETH_POLLING_TIME       1000
51
52 /* Callback to Poll the Ethernet Status*/
53 gboolean __net_ethernet_cable_status_polling_callback(gpointer data)
54 {
55         netconfig_ethernet_cable_plugin_status_check();
56         return TRUE;
57 }
58
59 void _got_name_cb_container(void)
60 {
61         register_gdbus_signal_container();
62         netconfig_update_initial_container_vconf();
63 }
64
65 void _got_name_cb(void)
66 {
67         wifi_object_create_and_init();
68         state_object_create_and_init();
69         statistics_object_create_and_init();
70         vpnsvc_create_and_init();
71         mptcp_object_create_and_init();
72         ethernet_object_create_and_init();
73         clock_object_create_and_init();
74
75         register_gdbus_signal();
76         connman_register_agent();
77
78         if (TIZEN_TV)
79                 __netconfig_set_ether_macaddr();
80 }
81
82 static void _objects_deinit(void)
83 {
84         cleanup_gdbus();
85         wifi_object_deinit();
86         state_object_deinit();
87         statistics_object_deinit();
88         vpnsvc_destroy_deinit();
89         mptcp_object_deinit();
90         ethernet_object_deinit();
91         clock_object_deinit();
92 }
93
94 int main(int argc, char *argv[])
95 {
96         int ret;
97         int check_ethernet_monitor_timer = 0;
98         char *ifname = NULL;
99         char *wifi_def_mac = NULL;
100
101         umask(0077);
102
103         DBG("Network Configuration service");
104         if (daemon(0, 0) != 0)
105                 DBG("Cannot start daemon");
106
107         if (mkdir(WIFI_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
108                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
109                 if (errno != EEXIST)
110                         ERR("Failed to create Wi-Fi directory");
111         }
112
113         if (mkdir(WIFI_CERT_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
114                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
115                 if (errno != EEXIST)
116                         ERR("Failed to create cert directory");
117         }
118
119 #if !GLIB_CHECK_VERSION(2, 36, 0)
120         g_type_init();
121 #endif
122
123         main_loop = g_main_loop_new(NULL, FALSE);
124         if (main_loop == NULL) {
125                 ERR("Couldn't create GMainLoop\n");
126                 log_cleanup();
127                 return 0;
128         }
129
130         if (access(CONTAINER_FILE, F_OK) == 0) {
131                 ret = setup_gdbus(_got_name_cb_container);
132                 if (ret != 0) {
133                         ERR("_netconfig_setup_gdbus is failed\n");
134                         return 0;
135                 }
136
137                 g_main_loop_run(main_loop);
138
139                 cleanup_gdbus();
140                 log_cleanup();
141                 deregister_gdbus_signal_container();
142
143                 return 0;
144         }
145
146         /* Backward compatibility:
147          * Execute only when the old style hal architecture is not running
148          */
149         if (!wifi_check_systemd_service()) {
150                 ifname = netconfig_get_default_ifname_from_file();
151
152                 if (ifname && netconfig_wifi_firmware_get_mac(ifname, &wifi_def_mac) == 0)
153                         wifi_set_default_mac(wifi_def_mac);
154
155                 g_free(ifname);
156         }
157
158         ret = setup_gdbus(_got_name_cb);
159         if (ret > 0) {
160                 ERR("_netconfig_setup_gdbus is failed\n");
161                 return 0;
162         }
163
164         netconfig_setting_init();
165
166         netconfig_error_init();
167
168         if (TIZEN_NTP_ENABLE)
169                 netconfig_clock_init();
170
171         /* If its environment uses Emulator, network configuration is set by emulator default */
172         emulator_test_and_start();
173
174         /* For device policy manager */
175         netconfig_dpm_init();
176
177         /* Start tcpdump if dump state is on */
178 #if defined TIZEN_DEBUG_ENABLE
179         check_dump_state_and_start();
180 #endif
181
182         /* Backward compatibility:
183          * Execute only when the old style hal architecture is running
184          */
185         if (TIZEN_WLAN_BOARD_SPRD && wifi_check_systemd_service())
186                 wifi_firmware_download();
187
188         /*In case no emulator, set the ETH0 Mac address*/
189         if (TIZEN_TV && emulator_is_emulated() == FALSE)
190                 __netconfig_set_ether_macaddr();
191
192         if (netconfig_check_feature_supported(NETCONFIG_SUPPORTED_FEATURE_ETHERNET)) {
193                 /* Register the callback to check the ethernet Plug-in /Plug-out Status */
194                 check_ethernet_monitor_timer = g_timeout_add(ETH_POLLING_TIME,
195                                 __net_ethernet_cable_status_polling_callback,
196                                 &check_ethernet_monitor_timer);
197         }
198
199         netconfig_plugin_init();
200
201         g_main_loop_run(main_loop);
202
203         netconfig_plugin_deinit();
204
205         _objects_deinit();
206
207         log_cleanup();
208
209         deregister_gdbus_signal();
210
211         netconfig_dpm_deinit();
212
213         netconfig_setting_deinit();
214
215         if (TIZEN_NTP_ENABLE)
216                 netconfig_clock_deinit();
217
218
219         /*remove the Timer*/
220         if (check_ethernet_monitor_timer > 0)
221                 g_source_remove(check_ethernet_monitor_timer);
222
223         wifi_state_notifier_cleanup();
224
225         /* Unregistering the agent */
226         connman_unregister_agent();
227
228         return 0;
229 }