c3a3584c0037d71b43df10be8a709b6e3294e512
[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 "network-clock.h"
36 #include "network-dpm.h"
37 #include "network-state.h"
38 #include "network-monitor.h"
39 #include "signal-handler.h"
40 #include "network-statistics.h"
41
42 static GMainLoop *main_loop = NULL;
43
44 #define ETHERNET_FEATURE       "http://tizen.org/feature/network.ethernet"
45
46 /*Poll the ethernet Cable Plug-in /Plug-out status at every 1000 ms*/
47 #define ETH_POLLING_TIME       1000
48
49 /* Callback to Poll the Ethernet Status*/
50 gboolean __net_ethernet_cable_status_polling_callback(gpointer data)
51 {
52         netconfig_ethernet_cable_plugin_status_check();
53         return TRUE;
54 }
55
56 void _got_name_cb(void)
57 {
58         wifi_object_create_and_init();
59         state_object_create_and_init();
60         statistics_object_create_and_init();
61         vpnsvc_create_and_init();
62
63         register_gdbus_signal();
64         connman_register_agent();
65
66         if (TIZEN_TV)
67                 __netconfig_set_ether_macaddr();
68 }
69
70 static void _objects_deinit(void)
71 {
72         cleanup_gdbus();
73         wifi_object_deinit();
74         state_object_deinit();
75         statistics_object_deinit();
76         vpnsvc_destroy_deinit();
77 }
78
79 int main(int argc, char *argv[])
80 {
81         int ret;
82         int check_ethernet_monitor_timer = 0;
83         bool ethernet_feature_supported = FALSE;
84
85         umask(0077);
86
87         DBG("Network Configuration service");
88         if (daemon(0, 0) != 0)
89                 DBG("Cannot start daemon");
90
91         if (mkdir(WIFI_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
92                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
93                 if (errno != EEXIST)
94                         ERR("Failed to create Wi-Fi directory");
95         }
96
97         if (mkdir(WIFI_CERT_STORAGEDIR, S_IRUSR | S_IWUSR | S_IXUSR |
98                         S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) < 0) {
99                 if (errno != EEXIST)
100                         ERR("Failed to create cert directory");
101         }
102
103 #if !GLIB_CHECK_VERSION(2, 36, 0)
104         g_type_init();
105 #endif
106
107         main_loop = g_main_loop_new(NULL, FALSE);
108         if (main_loop == NULL) {
109                 ERR("Couldn't create GMainLoop\n");
110                 return 0;
111         }
112
113         ret = setup_gdbus(_got_name_cb);
114         if (ret > 0) {
115                 ERR("_netconfig_setup_gdbus is failed\n");
116                 return 0;
117         }
118
119         netconfig_error_init();
120
121         if (TIZEN_NTP_ENABLE)
122                 netconfig_clock_init();
123
124         /* If its environment uses Emulator, network configuration is set by emulator default */
125         emulator_test_and_start();
126
127         /* For device policy manager */
128         netconfig_dpm_init();
129
130         if (TIZEN_WLAN_BOARD_SPRD)
131                 wifi_firmware_download();
132
133         /*In case no emulator, set the ETH0 Mac address*/
134         if (TIZEN_TV && emulator_is_emulated() == FALSE)
135                 __netconfig_set_ether_macaddr();
136
137         if (!system_info_get_platform_bool(ETHERNET_FEATURE, &ethernet_feature_supported)) {
138                 if (ethernet_feature_supported == TRUE) {
139                         /* Register the callback to check the ethernet Plug-in /Plug-out Status */
140                         check_ethernet_monitor_timer = g_timeout_add(ETH_POLLING_TIME,
141                                         __net_ethernet_cable_status_polling_callback,
142                                         &check_ethernet_monitor_timer);
143                 }
144         } else {
145                 ERR("Error - Feature getting from System Info");
146         }
147
148         g_main_loop_run(main_loop);
149
150         _objects_deinit();
151
152         log_cleanup();
153
154         deregister_gdbus_signal();
155
156         netconfig_dpm_deinit();
157
158         if (TIZEN_NTP_ENABLE)
159                 netconfig_clock_deinit();
160
161
162         /*remove the Timer*/
163         if (check_ethernet_monitor_timer > 0)
164                 g_source_remove(check_ethernet_monitor_timer);
165
166         wifi_state_notifier_cleanup();
167
168         /* Unregistering the agent */
169         connman_unregister_agent();
170
171         return 0;
172 }