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