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