g_free 'field' and 'value' if you break out of a while loop.
[platform/core/connectivity/net-config.git] / src / wifi-bssid-scan.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 <glib.h>
21
22 #include "netsupplicant.h"
23 #include "log.h"
24 #include "wifi-bssid-scan.h"
25 #include "util.h"
26 #include "wifi-power.h"
27 #include "wifi-state.h"
28 #include "wifi-background-scan.h"
29
30 #define NETCONFIG_SSID_LEN                                              32
31 #define NETCONFIG_BSSID_LEN                                             6
32
33 #define VCONF_WIFI_ALWAYS_ALLOW_SCANNING \
34         "file/private/wifi/always_allow_scanning"
35
36 static gboolean netconfig_is_bssid_scan_started = FALSE;
37 static gboolean netconfig_is_device_scanning = FALSE;
38 static gboolean netconfig_is_bssid_scan_aborted = FALSE;
39 static int bssid_list_count = 0;
40 static GSList *bssid_info_list = NULL;
41
42 struct bssid_scan_info_t {
43         unsigned char ssid[NETCONFIG_SSID_LEN + 1];
44         unsigned char bssid[NETCONFIG_BSSID_LEN + 1];
45         int ssid_len;
46         int rssi;
47         int mode;
48 };
49
50 gboolean netconfig_wifi_is_bssid_scan_started(void)
51 {
52         return netconfig_is_bssid_scan_started;
53 }
54
55 static void __netconfig_wifi_bssid_notify_scan_done(void)
56 {
57         GVariantBuilder *builder = NULL;
58         GVariantBuilder *builder1 = NULL;
59         GSList* list = NULL;
60         const char *prop_ssid = "ssid";
61         const char *prop_bssid = "bssid";
62         const char *prop_rssi = "rssi";
63         const char *prop_mode = "mode";
64
65         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
66         for (list = bssid_info_list; list != NULL; list = list->next) {
67                 struct bssid_scan_info_t *bss_info = (struct bssid_scan_info_t *)list->data;
68
69                 if (bss_info) {
70                         gchar bssid_buff[18] = { 0, };
71                         gchar *bssid_str = bssid_buff;
72                         unsigned char *ssid = (unsigned char *)bss_info->ssid;
73                         int ssid_len = (int)bss_info->ssid_len;
74                         int rssi = (int)bss_info->rssi;
75                         int mode = (int)bss_info->mode;
76                         int i = 0;
77                         g_snprintf(bssid_buff, 18, "%02x:%02x:%02x:%02x:%02x:%02x",
78                                         bss_info->bssid[0], bss_info->bssid[1], bss_info->bssid[2],
79                                         bss_info->bssid[3], bss_info->bssid[4], bss_info->bssid[5]);
80
81                         DBG("BSS found; SSID %s, BSSID %s, RSSI %d MODE %d", ssid, bssid_str, rssi, mode);
82
83                         builder1 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
84                         for (i = 0; i < ssid_len; i++)
85                                 g_variant_builder_add(builder1, "y", ssid[i]);
86                         g_variant_builder_add(builder, "{sv}", prop_ssid, g_variant_builder_end(builder1));
87                         g_variant_builder_unref(builder1);
88
89                         g_variant_builder_add(builder, "{sv}", prop_bssid, g_variant_new_string(bssid_str));
90                         g_variant_builder_add(builder, "{sv}", prop_rssi, g_variant_new_int32(rssi));
91                         g_variant_builder_add(builder, "{sv}", prop_mode, g_variant_new_int32(mode));
92                 }
93         }
94
95         wifi_emit_bssid_scan_completed((Wifi *)get_wifi_object(), g_variant_builder_end(builder));
96         g_variant_builder_unref(builder);
97
98         if (bssid_info_list != NULL)
99                 g_slist_free_full(bssid_info_list, g_free);
100
101         bssid_info_list = NULL;
102         bssid_list_count = 0;
103         INFO("BSSIDScanCompleted");
104
105         return;
106 }
107
108 static void __netconfig_wifi_bssid_get_bss_info_result(
109                 GObject *source_object, GAsyncResult *res, gpointer user_data)
110 {
111         GVariant *reply = NULL;
112         GVariant *value;
113         GVariantIter *iter;
114         gchar *key;
115         struct bssid_scan_info_t *bss_info;
116         GDBusConnection *conn = NULL;
117         GError *error = NULL;
118
119         conn = G_DBUS_CONNECTION(source_object);
120         reply = g_dbus_connection_call_finish(conn, res, &error);
121
122         if (error != NULL) {
123                 ERR("Error code: [%d] Error message: [%s]", error->code, error->message);
124                 g_error_free(error);
125                 goto done;
126         }
127
128         bss_info = g_try_new0(struct bssid_scan_info_t, 1);
129         if (bss_info == NULL)
130                 goto done;
131
132         g_variant_get(reply, "(a{sv})", &iter);
133         while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
134                 if (key != NULL) {
135                         if (g_strcmp0(key, "BSSID") == 0) {
136                                 const guchar *bssid;
137                                 gsize bssid_len;
138
139                                 bssid = g_variant_get_fixed_array(value, &bssid_len, sizeof(guchar));
140                                 if (bssid_len == NETCONFIG_BSSID_LEN)
141                                         memcpy(bss_info->bssid, bssid, bssid_len);
142                         } else if (g_strcmp0(key, "SSID") == 0) {
143                                 const guchar *ssid;
144                                 gsize ssid_len;
145
146                                 ssid = g_variant_get_fixed_array(value, &ssid_len, sizeof(guchar));
147                                 if (ssid != NULL && ssid_len > 0 && ssid_len <= NETCONFIG_SSID_LEN) {
148                                         memcpy(bss_info->ssid, ssid, ssid_len);
149                                         bss_info->ssid_len = ssid_len;
150                                 } else {
151                                         memset(bss_info->ssid, 0, sizeof(bss_info->ssid));
152                                         bss_info->ssid_len = 0;
153                                 }
154                         } else if (g_strcmp0(key, "Mode") == 0) {
155                                 gchar *mode = NULL;
156
157                                 g_variant_get(value, "s", &mode);
158                                 if (mode == NULL)
159                                         bss_info->mode = 0;
160                                 else {
161                                         if (g_strcmp0(mode, "infrastructure") == 0)
162                                                 bss_info->mode = 1;
163                                         else if (g_strcmp0(mode, "ad-hoc") == 0)
164                                                 bss_info->mode = 2;
165                                         else
166                                                 bss_info->mode = 0;
167                                         g_free(mode);
168                                 }
169                         } else if (g_strcmp0(key, "Signal") == 0) {
170                                 gint16 signal;
171
172                                 signal = g_variant_get_int16(value);
173                                 bss_info->rssi = signal;
174                         }
175                 }
176         }
177
178         if (bss_info->ssid[0] == '\0')
179                 g_free(bss_info);
180         else
181                 bssid_info_list = g_slist_append(bssid_info_list, bss_info);
182
183         g_variant_iter_free(iter);
184 done:
185         if (reply)
186                 g_variant_unref(reply);
187
188         netconfig_gdbus_pending_call_unref();
189
190         bssid_list_count--;
191         if (bssid_list_count <= 0) {
192                 __netconfig_wifi_bssid_notify_scan_done();
193
194                 if (netconfig_is_bssid_scan_aborted == FALSE)
195                         wifi_power_driver_and_supplicant(FALSE);
196         }
197 }
198
199 static void __netconfig_wifi_bssid_get_bss_info(const char *path, int index)
200 {
201         gboolean reply = FALSE;
202         GVariant *param = NULL;
203
204         param = g_variant_new("(s)", SUPPLICANT_IFACE_BSS);
205
206         reply = netconfig_invoke_dbus_method_nonblock(SUPPLICANT_SERVICE,
207                         path, DBUS_INTERFACE_PROPERTIES,
208                         "GetAll", param, __netconfig_wifi_bssid_get_bss_info_result);
209         if (reply != TRUE)
210                 ERR("Fail to invoke_dbus_method_nonblock GetAll");
211
212         return;
213 }
214
215 static void __netconfig_wifi_bssid_get_bss_result(GObject *source_object,
216                 GAsyncResult *res, gpointer user_data)
217 {
218         GVariant *reply = NULL;
219         GVariant *value = NULL;
220         GVariantIter *iter = NULL;
221         GDBusConnection *conn = NULL;
222         gchar *path = NULL;
223         gboolean counter_flag = FALSE;
224         GError *error = NULL;
225
226         conn = G_DBUS_CONNECTION(source_object);
227         reply = g_dbus_connection_call_finish(conn, res, &error);
228         if (error != NULL) {
229                 ERR("Error code: [%d] Error message: [%s]", error->code, error->message);
230                 g_error_free(error);
231                 goto done;
232         }
233
234         g_variant_get(reply, "(v)", &value);
235         if (g_variant_is_of_type(value, G_VARIANT_TYPE_OBJECT_PATH_ARRAY)) {
236                 g_variant_get(value, "ao", &iter);
237                 while (g_variant_iter_next(iter, "o", &path)) {
238                         if (path != NULL && g_strcmp0(path, "/") != 0) {
239                                 __netconfig_wifi_bssid_get_bss_info(path, ++bssid_list_count);
240
241                                 counter_flag = TRUE;
242                         }
243
244                         if (path)
245                                 g_free(path);
246                 }
247         }
248
249         if (iter)
250                 g_variant_iter_free(iter);
251
252         if (value)
253                 g_variant_unref(value);
254
255 done:
256         if (reply)
257                 g_variant_unref(reply);
258
259         netconfig_gdbus_pending_call_unref();
260
261         /* Send BssidScanCompleted signal even when the BSS count is 0 */
262         if (bssid_list_count <= 0 && counter_flag == FALSE) {
263                 __netconfig_wifi_bssid_notify_scan_done();
264
265                 if (netconfig_is_bssid_scan_aborted == FALSE)
266                         wifi_power_driver_and_supplicant(FALSE);
267         }
268 }
269
270 static int _netconfig_wifi_bssid_get_bss(void)
271 {
272         gboolean reply = FALSE;
273         char *if_path = NULL;
274         GVariant *params = NULL;
275
276         if_path = netconfig_wifi_get_supplicant_interface();
277         if (if_path == NULL) {
278                 DBG("Fail to get wpa_supplicant DBus path");
279                 return -ESRCH;
280         }
281
282         params = g_variant_new("(ss)", SUPPLICANT_IFACE_INTERFACE, "BSSs");
283
284         reply = netconfig_invoke_dbus_method_nonblock(SUPPLICANT_SERVICE,
285                         if_path, DBUS_INTERFACE_PROPERTIES,
286                         "Get", params, __netconfig_wifi_bssid_get_bss_result);
287
288         g_free(if_path);
289         if (reply != TRUE) {
290                 ERR("Fail to method: Get");
291
292                 return -ESRCH;
293         }
294
295         return 0;
296 }
297
298
299 static void __netconfig_set_bssid_scan_mode(gboolean enable)
300 {
301         if (netconfig_is_bssid_scan_started == enable)
302                 return;
303
304         netconfig_is_bssid_scan_started = enable;
305 }
306
307 void netconfig_wifi_bssid_signal_scandone(void)
308 {
309         bssid_list_count = 0;
310         _netconfig_wifi_bssid_get_bss();
311
312         netconfig_is_device_scanning = FALSE;
313
314         __netconfig_set_bssid_scan_mode(FALSE);
315 }
316
317 void netconfig_wifi_bssid_signal_scanaborted(void)
318 {
319         bssid_list_count = 0;
320         netconfig_is_bssid_scan_aborted = TRUE;
321         _netconfig_wifi_bssid_get_bss();
322
323         netconfig_is_device_scanning = FALSE;
324
325         __netconfig_set_bssid_scan_mode(FALSE);
326 }
327
328 static int __netconfig_wifi_bssid_request_scan(char *if_path)
329 {
330         GDBusConnection *connection = NULL;
331         GVariant *message = NULL;
332         GVariantBuilder *builder = NULL;
333         const char *key1 = "Type";
334         const char *val1 = "passive";
335         gboolean is_free_required = FALSE;
336
337         if (if_path == NULL) {
338                 if_path = netconfig_wifi_get_supplicant_interface();
339                 is_free_required = TRUE;
340         }
341
342         if (if_path == NULL) {
343                 DBG("Fail to get wpa_supplicant DBus path");
344                 return -ESRCH;
345         }
346
347         connection = netdbus_get_connection();
348         if (connection == NULL) {
349                 ERR("Failed to get GDBusconnection");
350                 return -EIO;
351         }
352
353         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
354         g_variant_builder_add(builder, "{sv}", key1, g_variant_new_string(val1));
355         message = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
356         g_variant_builder_unref(builder);
357
358         g_dbus_connection_call(connection,
359                         SUPPLICANT_SERVICE,
360                         if_path,
361                         SUPPLICANT_INTERFACE ".Interface",
362                         "Scan",
363                         message,
364                         NULL,
365                         G_DBUS_CALL_FLAGS_NONE,
366                         NETCONFIG_DBUS_REPLY_TIMEOUT,
367                         netdbus_get_cancellable(),
368                         NULL,
369                         NULL);
370
371         netconfig_is_device_scanning = TRUE;
372
373         if (is_free_required)
374                 g_free(if_path);
375         g_variant_unref(message);
376         /* Clear bss_info_list for the next scan result */
377         if (bssid_info_list) {
378                 g_slist_free_full(bssid_info_list, g_free);
379                 bssid_info_list = NULL;
380         }
381
382         netconfig_is_bssid_scan_aborted = FALSE;
383
384         return 0;
385 }
386
387 static void __netconfig_wifi_interface_create_result(
388                 GObject *source_object, GAsyncResult *res, gpointer user_data)
389 {
390         GVariant *message;
391         gchar *path = NULL;
392         GDBusConnection *conn = NULL;
393         GError *error = NULL;
394
395         conn = G_DBUS_CONNECTION(source_object);
396
397         message = g_dbus_connection_call_finish(conn, res, &error);
398         if (error == NULL) {
399                 g_variant_get(message, "(o)", &path);
400
401                 if (path) {
402                         __netconfig_wifi_bssid_request_scan(path);
403                         g_free(path);
404                 }
405         } else if (NULL != strstr(error->message, ".InterfaceExists")) {
406                 INFO("Error Message %s %s", error->message, path);
407                 g_variant_get(message, "(o)", &path);
408                 if (path) {
409                         __netconfig_wifi_bssid_request_scan(path);
410                         g_free(path);
411                 } else
412                         __netconfig_wifi_bssid_request_scan(NULL);
413         } else {
414                 ERR("Failed to create interface, Error: %d[%s]", error->code, error->message);
415                 __netconfig_set_bssid_scan_mode(FALSE);
416                 wifi_power_driver_and_supplicant(FALSE);
417         }
418
419         g_variant_unref(message);
420 }
421
422 static int  __netconfig_wifi_bssid_create_interface(void)
423 {
424         GDBusConnection *connection = NULL;
425         GVariant *message = NULL;
426         GVariantBuilder *builder = NULL;
427         const char *key = "Ifname";
428         const char *val = WIFI_IFNAME;
429
430         connection = netdbus_get_connection();
431         if (connection == NULL) {
432                 DBG("Failed to get GDBusconnection");
433                 return -EIO;
434         }
435
436         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
437         g_variant_builder_add(builder, "{sv}", key, g_variant_new_string(val));
438         message = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
439         g_variant_builder_unref(builder);
440
441         g_dbus_connection_call(connection,
442                         SUPPLICANT_SERVICE,
443                         SUPPLICANT_PATH,
444                         SUPPLICANT_INTERFACE,
445                         "CreateInterface",
446                         message,
447                         NULL,
448                         G_DBUS_CALL_FLAGS_NONE,
449                         NETCONFIG_DBUS_REPLY_TIMEOUT,
450                         netdbus_get_cancellable(),
451                         (GAsyncReadyCallback) __netconfig_wifi_interface_create_result,
452                         NULL);
453
454         g_variant_unref(message);
455
456         return 0;
457 }
458
459 static int __netconfig_wifi_bssid_scan(void)
460 {
461         int err = 0;
462         wifi_tech_state_e wifi_tech_state;
463
464         if (netconfig_is_device_scanning == TRUE)
465                 return -EINPROGRESS;
466
467         wifi_tech_state = wifi_state_get_technology_state();
468         if (wifi_tech_state <= NETCONFIG_WIFI_TECH_OFF)
469                 err = wifi_power_driver_and_supplicant(TRUE);
470
471         if (err < 0 && err != -EALREADY)
472                 return err;
473
474         netconfig_is_device_scanning = TRUE;
475
476         DBG("BSSID scan requested");
477         if (wifi_tech_state >= NETCONFIG_WIFI_TECH_POWERED) {
478                 if (netconfig_wifi_get_scanning() == TRUE)
479                         return -EINPROGRESS;
480
481                 netconfig_wifi_bgscan_start(TRUE);
482
483                 if (wifi_tech_state == NETCONFIG_WIFI_TECH_CONNECTED)
484                         __netconfig_wifi_bssid_request_scan(NULL);
485         } else {
486                 err = __netconfig_wifi_bssid_create_interface();
487         }
488
489         return err;
490 }
491
492 gboolean handle_request_bssid_scan(Wifi *wifi, GDBusMethodInvocation *context)
493 {
494         int err, enabled = 0;
495         wifi_tech_state_e tech_state;
496
497         g_return_val_if_fail(wifi != NULL, TRUE);
498
499         if (netconfig_is_wifi_tethering_on() == TRUE) {
500                 ERR("Wi-Fi Tethering is enabled");
501                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_NO_SERVICE, "TetheringEnabled");
502                 return TRUE;
503         }
504
505 #if !defined TIZEN_WEARABLE
506         if (netconfig_wifi_is_bgscan_paused()) {
507                 ERR("Scan is paused");
508                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_NO_SERVICE, "ScanPaused");
509                 return TRUE;
510         }
511 #endif
512
513         tech_state = wifi_state_get_technology_state();
514         if (tech_state <= NETCONFIG_WIFI_TECH_OFF) {
515 #if !defined TIZEN_WEARABLE
516                 enabled = 1;
517 #else
518                 enabled = 0;
519 #endif
520
521                 if (enabled == 0) {
522                         netconfig_error_permission_denied(context);
523                         return TRUE;
524                 }
525         }
526
527         __netconfig_set_bssid_scan_mode(TRUE);
528
529         err = __netconfig_wifi_bssid_scan();
530         if (err < 0) {
531                 if (err == -EINPROGRESS)
532                         netconfig_error_inprogress(context);
533                 else
534                         netconfig_error_wifi_driver_failed(context);
535
536                 return TRUE;
537         }
538
539         wifi_complete_request_bssid_scan(wifi, context);
540         return TRUE;
541 }
542
543 gboolean handle_get_bssid_list(Wifi *wifi, GDBusMethodInvocation *context)
544 {
545         _netconfig_wifi_bssid_get_bss();
546         wifi_complete_get_bssid_list(wifi, context);
547         return TRUE;
548 }