429a68c57943a7b83bcfdb4ddb3d39bb10858063
[platform/core/connectivity/net-config.git] / src / wifi.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 <stdlib.h>
22 #include <unistd.h>
23 #include <vconf.h>
24 #include <vconf-keys.h>
25
26 #include "log.h"
27 #include "wifi.h"
28 #include "util.h"
29 #include "netdbus.h"
30 #include "neterror.h"
31 #include "wifi-eap.h"
32 #include "wifi-wps.h"
33 #include "wifi-power.h"
34 #include "wifi-state.h"
35 #include "wifi-agent.h"
36 #include "wifi-firmware.h"
37 #include "wifi-ssid-scan.h"
38 #include "wifi-passpoint.h"
39 #include "wifi-eap-config.h"
40 #include "wifi-background-scan.h"
41 #include "wifi-config.h"
42 #include "wifi-tdls.h"
43
44 #define SPRD_CP2_FIRMWARE_PATH "/usr/bin/cp2-downloader"
45 static int is_wifi_firmware_downloaded = FALSE;
46
47 static Wifi *wifi_object = NULL;
48 static NetConnmanAgent *connman_agent_object = NULL;
49 static WifiFirmware *wififirmware_object = NULL;
50
51 Wifi *get_wifi_object(void){
52         return wifi_object;
53 }
54
55 static gboolean handle_check_black_list(Wifi *wifi, GDBusMethodInvocation *context,
56                 const gchar *name, const gchar *security_type, const gchar *eap)
57 {
58         ERR("Name (%s)", name);
59         INFO("disable to check");
60         wifi_complete_check_black_list(wifi, context, TRUE);
61         return TRUE;
62 }
63
64 static void _set_wifi_mac_address(void)
65 {
66         gchar *mac_addr = NULL;
67
68         mac_addr = vconf_get_str(VCONFKEY_WIFI_BSSID_ADDRESS);
69         if (mac_addr != NULL) {
70                 if (strlen(mac_addr) == 0)
71                         netconfig_set_mac_address_from_file();
72                 free(mac_addr);
73         }
74 }
75
76 void __netconfig_wifi_connect_reply(GObject *source_object, GAsyncResult *res,
77                 gpointer user_data)
78 {
79         GDBusConnection *conn = NULL;
80         GError *error = NULL;
81
82         DBG("WiFi Connection Reply");
83
84         conn = G_DBUS_CONNECTION(source_object);
85         g_dbus_connection_call_finish(conn, res, &error);
86         if (error != NULL) {
87                 ERR("WiFi Connection Error [%s]", error->message);
88                 /* No need to emit WiFiConnectFail signal if Connection is
89                  * in progress */
90                 if (error->code == G_IO_ERROR_TIMED_OUT) {
91                         g_error_free(error);
92                         DBG("WiFi Connection in Progress");
93                         netconfig_gdbus_pending_call_unref();
94                         return;
95                 }
96                 g_error_free(error);
97                 if (netconfig_dbus_emit_signal(NULL, NETCONFIG_WIFI_PATH,
98                                 NETCONFIG_WIFI_INTERFACE, "WiFiConnectFail",
99                                 NULL) == FALSE)
100                         ERR("Failed to emit WiFiConnectFail signal");
101                 else
102                         DBG("Successfully sent WiFiConnectFail signal");
103         } else
104                 DBG("WiFi Connection has been initiated successfully");
105
106         netconfig_gdbus_pending_call_unref();
107         return;
108 }
109
110 int wifi_firmware_download(void)
111 {
112         int rv = 0;
113         const char *path = SPRD_CP2_FIRMWARE_PATH;
114         char *const args[] = { SPRD_CP2_FIRMWARE_PATH, NULL };
115         char *const envs[] = { NULL };
116
117         if (!is_wifi_firmware_downloaded) {
118                 rv = netconfig_execute_file(path, args, envs);
119                 if (rv < 0) {
120                         DBG("wifi firmware download fails");
121                         return -EIO;
122                 }
123                 is_wifi_firmware_downloaded = TRUE;
124                 DBG("wifi firmware download successes");
125         }
126
127         return 0;
128 }
129
130 void wifi_object_create_and_init(void)
131 {
132         DBG("Create wifi object.");
133         GDBusInterfaceSkeleton *interface_wifi = NULL;
134         GDBusInterfaceSkeleton *interface_connman_agent = NULL;
135         GDBusInterfaceSkeleton *interface_wifi_firmware = NULL;
136         GDBusConnection *connection = NULL;
137         GDBusObjectManagerServer *server = netdbus_get_wifi_manager();
138         if (server == NULL)
139                 return;
140
141         connection = netdbus_get_connection();
142         g_dbus_object_manager_server_set_connection(server, connection);
143
144         /*Interface netconfig.wifi*/
145         wifi_object = wifi_skeleton_new();
146         interface_wifi = G_DBUS_INTERFACE_SKELETON(wifi_object);
147
148         /* WIFI power */
149         g_signal_connect(wifi_object, "handle-load-driver",
150                         G_CALLBACK(handle_load_driver), NULL);
151         g_signal_connect(wifi_object, "handle-remove-driver",
152                         G_CALLBACK(handle_remove_driver), NULL);
153         g_signal_connect(wifi_object, "handle-load-p2p-driver",
154                                 G_CALLBACK(handle_load_p2p_driver), NULL);
155         g_signal_connect(wifi_object, "handle-remove-p2p-driver",
156                         G_CALLBACK(handle_remove_p2p_driver), NULL);
157
158         /* WIFI state */
159         g_signal_connect(wifi_object, "handle-get-wifi-state",
160                         G_CALLBACK(handle_get_wifi_state), NULL);
161
162         /* WIFI scan */
163         g_signal_connect(wifi_object, "handle-request-specific-scan",
164                         G_CALLBACK(handle_request_specific_scan), NULL);
165         g_signal_connect(wifi_object, "handle-request-bssid-scan",
166                         G_CALLBACK(handle_request_bssid_scan), NULL);
167
168         /* WPS Connect */
169         g_signal_connect(wifi_object, "handle-request-wps-connect",
170                         G_CALLBACK(handle_request_wps_connect), NULL);
171         g_signal_connect(wifi_object, "handle-request-wps-cancel",
172                         G_CALLBACK(handle_request_wps_cancel), NULL);
173
174         /* WIFI direct */
175         g_signal_connect(wifi_object, "handle-launch-direct",
176                         G_CALLBACK(handle_launch_direct), NULL);
177
178         /* EAP config */
179         g_signal_connect(wifi_object, "handle-create-eap-config",
180                         G_CALLBACK(handle_create_eap_config), NULL);
181         g_signal_connect(wifi_object, "handle-delete-eap-config",
182                         G_CALLBACK(handle_delete_eap_config), NULL);
183
184         /* VSIE methods */
185         g_signal_connect(wifi_object, "handle-add-vsie",
186                         G_CALLBACK(handle_add_vsie), NULL);
187         g_signal_connect(wifi_object, "handle-get-vsie",
188                         G_CALLBACK(handle_get_vsie), NULL);
189         g_signal_connect(wifi_object, "handle-remove-vsie",
190                         G_CALLBACK(handle_remove_vsie), NULL);
191
192         /* WIFI configuration */
193         g_signal_connect(wifi_object, "handle-save-configuration",
194                         G_CALLBACK(handle_save_configuration), NULL);
195         g_signal_connect(wifi_object, "handle-remove-configuration",
196                         G_CALLBACK(handle_remove_configuration), NULL);
197         g_signal_connect(wifi_object, "handle-get-config-ids",
198                         G_CALLBACK(handle_get_config_ids), NULL);
199         g_signal_connect(wifi_object, "handle-load-configuration",
200                         G_CALLBACK(handle_load_configuration), NULL);
201         g_signal_connect(wifi_object, "handle-set-config-field",
202                         G_CALLBACK(handle_set_config_field), NULL);
203         g_signal_connect(wifi_object, "handle-get-config-passphrase",
204                         G_CALLBACK(handle_get_config_passphrase), NULL);
205         /* WIFI EAP configuration */
206         g_signal_connect(wifi_object, "handle-save-eap-configuration",
207                         G_CALLBACK(handle_save_eap_configuration), NULL);
208         g_signal_connect(wifi_object, "handle-load-eap-configuration",
209                         G_CALLBACK(handle_load_eap_configuration), NULL);
210
211         /* BG scan mode */
212         g_signal_connect(wifi_object, "handle-set-bgscan",
213                         G_CALLBACK(handle_set_bgscan), NULL);
214         g_signal_connect(wifi_object, "handle-resume-bgscan",
215                         G_CALLBACK(handle_resume_bgscan), NULL);
216         g_signal_connect(wifi_object, "handle-pause-bgscan",
217                         G_CALLBACK(handle_pause_bgscan), NULL);
218
219         /*Auto Scan Mode */
220         g_signal_connect(wifi_object, "handle-get-autoscan",
221                         G_CALLBACK(handle_get_autoscan), NULL);
222         g_signal_connect(wifi_object, "handle-get-autoscanmode",
223                         G_CALLBACK(handle_get_autoscanmode), NULL);
224
225         /* Passpoint */
226         g_signal_connect(wifi_object, "handle-set-passpoint",
227                                 G_CALLBACK(handle_set_passpoint), NULL);
228         g_signal_connect(wifi_object, "handle-get-passpoint",
229                                         G_CALLBACK(handle_get_passpoint), NULL);
230
231         /* EAP authentication */
232         g_signal_connect(wifi_object, "handle-get-aka-auth",
233                                 G_CALLBACK(handle_get_aka_auth), NULL);
234         g_signal_connect(wifi_object, "handle-get-sim-auth",
235                                 G_CALLBACK(handle_get_sim_auth), NULL);
236         g_signal_connect(wifi_object, "handle-get-sim-imsi",
237                                 G_CALLBACK(handle_get_sim_imsi), NULL);
238         g_signal_connect(wifi_object, "handle-req-aka-auth",
239                         G_CALLBACK(handle_req_aka_auth), NULL);
240         g_signal_connect(wifi_object, "handle-req-sim-auth",
241                         G_CALLBACK(handle_req_sim_auth), NULL);
242
243         /* WIFI MDM blacklist */
244         g_signal_connect(wifi_object, "handle-check-black-list",
245                         G_CALLBACK(handle_check_black_list), NULL);
246
247         /* TDLS methods */
248         g_signal_connect(wifi_object, "handle-tdls-disconnect",
249                         G_CALLBACK(handle_tdls_disconnect), NULL);
250         g_signal_connect(wifi_object, "handle-tdls-connected-peer",
251                         G_CALLBACK(handle_tdls_connected_peer), NULL);
252         g_signal_connect(wifi_object, "handle-tdls-discover",
253                         G_CALLBACK(handle_tdls_discover), NULL);
254         g_signal_connect(wifi_object, "handle-tdls-connect",
255                         G_CALLBACK(handle_tdls_connect), NULL);
256         g_signal_connect(wifi_object, "handle-tdls-channel-switch",
257                         G_CALLBACK(handle_tdls_channel_switch), NULL);
258         g_signal_connect(wifi_object, "handle-tdls-cancel-channel-switch",
259                         G_CALLBACK(handle_tdls_cancel_channel_switch), NULL);
260
261         if (!g_dbus_interface_skeleton_export(interface_wifi, connection,
262                         NETCONFIG_WIFI_PATH, NULL)) {
263                 ERR("Export WIFI_PATH for wifi failed");
264         }
265
266         /* Interface connman.Agent */
267         connman_agent_object = net_connman_agent_skeleton_new();
268
269         interface_connman_agent = G_DBUS_INTERFACE_SKELETON(connman_agent_object);
270         g_signal_connect(connman_agent_object, "handle-report-error",
271                         G_CALLBACK(handle_report_error), NULL);
272         g_signal_connect(connman_agent_object, "handle-request-browser",
273                         G_CALLBACK(handle_request_browser), NULL);
274         g_signal_connect(connman_agent_object, "handle-request-input",
275                         G_CALLBACK(handle_request_input), NULL);
276         g_signal_connect(connman_agent_object, "handle-set-field",
277                         G_CALLBACK(handle_set_field), NULL);
278
279         if (!g_dbus_interface_skeleton_export(interface_connman_agent, connection,
280                         NETCONFIG_WIFI_PATH, NULL)) {
281                 ERR("Export WIFI_PATH for agent failed");
282         }
283
284         /*Interface netconfig.wifi.Firmware*/
285         wififirmware_object = wifi_firmware_skeleton_new();
286
287         interface_wifi_firmware = G_DBUS_INTERFACE_SKELETON(wififirmware_object);
288         g_signal_connect(wififirmware_object, "handle-start",
289                         G_CALLBACK(handle_start), NULL);
290         g_signal_connect(wififirmware_object, "handle-stop",
291                                 G_CALLBACK(handle_stop), NULL);
292
293         if (!g_dbus_interface_skeleton_export(interface_wifi_firmware, connection,
294                         NETCONFIG_WIFI_PATH, NULL)) {
295                 ERR("Export WIFI_PATH for firmware failed");
296         }
297
298         _set_wifi_mac_address();
299
300         wifi_power_initialize();
301
302         return;
303 }
304
305 void wifi_object_deinit(void)
306 {
307         g_object_unref(wifi_object);
308         g_object_unref(connman_agent_object);
309         g_object_unref(wififirmware_object);
310
311         wifi_power_deinitialize();
312 }