Using updated clatd's Dbus interface and Path values
[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         int fd;
120         mode_t mode = S_IRGRP | S_IWUSR | S_IXGRP;
121
122         fd = creat(SPRD_CP2_FIRMWARE_STATE_PATH, mode);
123         if (fd >= 0)
124                 close(fd);
125         else
126                 DBG("Failed to create wifi firmware state file");
127
128         is_wifi_firmware_downloaded = TRUE;
129 }
130
131 static int _get_wifi_firmware_state(void)
132 {
133         if (!is_wifi_firmware_downloaded
134                         && access(SPRD_CP2_FIRMWARE_STATE_PATH, F_OK) != 0)
135                 is_wifi_firmware_downloaded = FALSE;
136         else
137                 is_wifi_firmware_downloaded = TRUE;
138         return is_wifi_firmware_downloaded;
139 }
140
141 int wifi_firmware_download(void)
142 {
143         int rv = 0;
144         const char *path = SPRD_CP2_FIRMWARE_PATH;
145         char *const args[] = { SPRD_CP2_FIRMWARE_PATH, NULL };
146         char *const envs[] = { NULL };
147
148         if (!_get_wifi_firmware_state()) {
149                 rv = netconfig_execute_file(path, args, envs);
150                 if (rv < 0) {
151                         DBG("wifi firmware download fails");
152                         return -EIO;
153                 }
154                 _update_wifi_firmware_state();
155                 DBG("wifi firmware download successes");
156         }
157
158         return 0;
159 }
160
161 void wifi_object_create_and_init(void)
162 {
163         DBG("Create wifi object.");
164         GDBusInterfaceSkeleton *interface_wifi = NULL;
165         GDBusInterfaceSkeleton *interface_connman_agent = NULL;
166         GDBusInterfaceSkeleton *interface_wifi_firmware = NULL;
167         GDBusConnection *connection = NULL;
168         GDBusObjectManagerServer *server = netdbus_get_wifi_manager();
169         if (server == NULL)
170                 return;
171
172         connection = netdbus_get_connection();
173         g_dbus_object_manager_server_set_connection(server, connection);
174
175         /*Interface netconfig.wifi*/
176         wifi_object = wifi_skeleton_new();
177         interface_wifi = G_DBUS_INTERFACE_SKELETON(wifi_object);
178
179         /* WIFI power */
180         g_signal_connect(wifi_object, "handle-load-driver",
181                         G_CALLBACK(handle_load_driver), NULL);
182         g_signal_connect(wifi_object, "handle-remove-driver",
183                         G_CALLBACK(handle_remove_driver), NULL);
184         g_signal_connect(wifi_object, "handle-load-p2p-driver",
185                                 G_CALLBACK(handle_load_p2p_driver), NULL);
186         g_signal_connect(wifi_object, "handle-remove-p2p-driver",
187                         G_CALLBACK(handle_remove_p2p_driver), NULL);
188
189         /* WIFI state */
190         g_signal_connect(wifi_object, "handle-get-wifi-state",
191                         G_CALLBACK(handle_get_wifi_state), NULL);
192
193         /* WIFI scan */
194         g_signal_connect(wifi_object, "handle-request-bssid-scan",
195                         G_CALLBACK(handle_request_bssid_scan), NULL);
196         g_signal_connect(wifi_object, "handle-get-bssid-list",
197                         G_CALLBACK(handle_get_bssid_list), NULL);
198         g_signal_connect(wifi_object, "handle-netlink-scan",
199                         G_CALLBACK(handle_netlink_scan), NULL);
200
201         /* WPS Connect */
202         g_signal_connect(wifi_object, "handle-request-wps-connect",
203                         G_CALLBACK(handle_request_wps_connect), NULL);
204         g_signal_connect(wifi_object, "handle-request-wps-cancel",
205                         G_CALLBACK(handle_request_wps_cancel), NULL);
206
207         /* WIFI direct */
208         g_signal_connect(wifi_object, "handle-launch-direct",
209                         G_CALLBACK(handle_launch_direct), NULL);
210
211         /* EAP config */
212         g_signal_connect(wifi_object, "handle-create-eap-config",
213                         G_CALLBACK(handle_create_eap_config), NULL);
214         g_signal_connect(wifi_object, "handle-delete-eap-config",
215                         G_CALLBACK(handle_delete_eap_config), NULL);
216
217         /* VSIE methods */
218         g_signal_connect(wifi_object, "handle-add-vsie",
219                         G_CALLBACK(handle_add_vsie), NULL);
220         g_signal_connect(wifi_object, "handle-get-vsie",
221                         G_CALLBACK(handle_get_vsie), NULL);
222         g_signal_connect(wifi_object, "handle-remove-vsie",
223                         G_CALLBACK(handle_remove_vsie), NULL);
224
225         /* IP conflict methods */
226         g_signal_connect(wifi_object, "handle-ip-conflict-set-enable",
227                         G_CALLBACK(handle_ip_conflict_set_enable), NULL);
228         g_signal_connect(wifi_object, "handle-is-ip-conflict-detect-enabled",
229                         G_CALLBACK(handle_is_ip_conflict_detect_enabled), NULL);
230         g_signal_connect(wifi_object, "handle-set-ip-conflict-period",
231                         G_CALLBACK(handle_set_ip_conflict_period), NULL);
232         g_signal_connect(wifi_object, "handle-get-ip-conflict-state",
233                         G_CALLBACK(handle_get_ip_conflict_state), NULL);
234         g_signal_connect(wifi_object, "handle-get-ip-conflict-period",
235                         G_CALLBACK(handle_get_ip_conflict_period), NULL);
236
237         /* WIFI configuration */
238         g_signal_connect(wifi_object, "handle-save-configuration",
239                         G_CALLBACK(handle_save_configuration), NULL);
240         g_signal_connect(wifi_object, "handle-remove-configuration",
241                         G_CALLBACK(handle_remove_configuration), NULL);
242         g_signal_connect(wifi_object, "handle-get-config-ids",
243                         G_CALLBACK(handle_get_config_ids), NULL);
244         g_signal_connect(wifi_object, "handle-load-configuration",
245                         G_CALLBACK(handle_load_configuration), NULL);
246         g_signal_connect(wifi_object, "handle-set-config-field",
247                         G_CALLBACK(handle_set_config_field), NULL);
248         g_signal_connect(wifi_object, "handle-get-config-passphrase",
249                         G_CALLBACK(handle_get_config_passphrase), NULL);
250
251         /* WIFI EAP configuration */
252         g_signal_connect(wifi_object, "handle-save-eap-configuration",
253                         G_CALLBACK(handle_save_eap_configuration), NULL);
254         g_signal_connect(wifi_object, "handle-load-eap-configuration",
255                         G_CALLBACK(handle_load_eap_configuration), NULL);
256
257         /* BG scan mode */
258         g_signal_connect(wifi_object, "handle-set-bgscan",
259                         G_CALLBACK(handle_set_bgscan), NULL);
260         g_signal_connect(wifi_object, "handle-resume-bgscan",
261                         G_CALLBACK(handle_resume_bgscan), NULL);
262         g_signal_connect(wifi_object, "handle-pause-bgscan",
263                         G_CALLBACK(handle_pause_bgscan), NULL);
264         g_signal_connect(wifi_object, "handle-reset-bgscan-interval",
265                         G_CALLBACK(handle_reset_bgscan_interval), NULL);
266
267         /* Auto Scan Mode */
268         g_signal_connect(wifi_object, "handle-get-autoscan",
269                         G_CALLBACK(handle_get_autoscan), NULL);
270         g_signal_connect(wifi_object, "handle-get-autoscanmode",
271                         G_CALLBACK(handle_get_autoscanmode), NULL);
272
273         /* Extension API methods */
274         g_signal_connect(wifi_object, "handle-flush-bss",
275                         G_CALLBACK(handle_flush_bss), NULL);
276
277         /* Passpoint */
278         g_signal_connect(wifi_object, "handle-set-passpoint",
279                                 G_CALLBACK(handle_set_passpoint), NULL);
280         g_signal_connect(wifi_object, "handle-get-passpoint",
281                                         G_CALLBACK(handle_get_passpoint), NULL);
282
283         /* EAP authentication */
284         g_signal_connect(wifi_object, "handle-get-aka-auth",
285                                 G_CALLBACK(handle_get_aka_auth), NULL);
286         g_signal_connect(wifi_object, "handle-get-sim-auth",
287                                 G_CALLBACK(handle_get_sim_auth), NULL);
288         g_signal_connect(wifi_object, "handle-get-sim-imsi",
289                                 G_CALLBACK(handle_get_sim_imsi), NULL);
290         g_signal_connect(wifi_object, "handle-req-aka-auth",
291                         G_CALLBACK(handle_req_aka_auth), NULL);
292         g_signal_connect(wifi_object, "handle-req-sim-auth",
293                         G_CALLBACK(handle_req_sim_auth), NULL);
294
295         /* WIFI MDM blacklist */
296         g_signal_connect(wifi_object, "handle-check-black-list",
297                         G_CALLBACK(handle_check_black_list), NULL);
298
299         /* TDLS methods */
300         g_signal_connect(wifi_object, "handle-tdls-disconnect",
301                         G_CALLBACK(handle_tdls_disconnect), NULL);
302         g_signal_connect(wifi_object, "handle-tdls-connected-peer",
303                         G_CALLBACK(handle_tdls_connected_peer), NULL);
304         g_signal_connect(wifi_object, "handle-tdls-discover",
305                         G_CALLBACK(handle_tdls_discover), NULL);
306         g_signal_connect(wifi_object, "handle-tdls-connect",
307                         G_CALLBACK(handle_tdls_connect), NULL);
308         g_signal_connect(wifi_object, "handle-tdls-channel-switch",
309                         G_CALLBACK(handle_tdls_channel_switch), NULL);
310         g_signal_connect(wifi_object, "handle-tdls-cancel-channel-switch",
311                         G_CALLBACK(handle_tdls_cancel_channel_switch), NULL);
312
313         /* Passphrase Encryption */
314         g_signal_connect(wifi_object, "handle-encrypt-passphrase",
315                         G_CALLBACK(handle_encrypt_passphrase), NULL);
316         g_signal_connect(wifi_object, "handle-decrypt-passphrase",
317                         G_CALLBACK(handle_decrypt_passphrase), NULL);
318
319         if (!g_dbus_interface_skeleton_export(interface_wifi, connection,
320                         NETCONFIG_WIFI_PATH, NULL)) {
321                 ERR("Export WIFI_PATH for wifi failed");
322         }
323
324         /* Interface connman.Agent */
325         connman_agent_object = net_connman_agent_skeleton_new();
326
327         interface_connman_agent = G_DBUS_INTERFACE_SKELETON(connman_agent_object);
328         g_signal_connect(connman_agent_object, "handle-report-error",
329                         G_CALLBACK(handle_report_error), NULL);
330         g_signal_connect(connman_agent_object, "handle-request-browser",
331                         G_CALLBACK(handle_request_browser), NULL);
332         g_signal_connect(connman_agent_object, "handle-request-input",
333                         G_CALLBACK(handle_request_input), NULL);
334         g_signal_connect(connman_agent_object, "handle-set-field",
335                         G_CALLBACK(handle_set_field), NULL);
336
337         if (!g_dbus_interface_skeleton_export(interface_connman_agent, connection,
338                         NETCONFIG_WIFI_PATH, NULL)) {
339                 ERR("Export WIFI_PATH for agent failed");
340         }
341
342         /*Interface netconfig.wifi.Firmware*/
343         wififirmware_object = wifi_firmware_skeleton_new();
344
345         interface_wifi_firmware = G_DBUS_INTERFACE_SKELETON(wififirmware_object);
346         g_signal_connect(wififirmware_object, "handle-start",
347                         G_CALLBACK(handle_start), NULL);
348         g_signal_connect(wififirmware_object, "handle-stop",
349                                 G_CALLBACK(handle_stop), NULL);
350
351         if (!g_dbus_interface_skeleton_export(interface_wifi_firmware, connection,
352                         NETCONFIG_WIFI_PATH, NULL)) {
353                 ERR("Export WIFI_PATH for firmware failed");
354         }
355
356         _set_wifi_mac_address();
357
358         wifi_power_initialize();
359
360         return;
361 }
362
363 void wifi_object_deinit(void)
364 {
365         g_object_unref(wifi_object);
366         g_object_unref(connman_agent_object);
367         g_object_unref(wififirmware_object);
368
369         wifi_power_deinitialize();
370 }