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