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