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