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