96c6c83846c91c2e18f665e09f813fe3697d9342
[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         const 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         if (reply != TRUE) {
288                 ERR("Fail to method: Get");
289
290                 return -ESRCH;
291         }
292
293         return 0;
294 }
295
296
297 static void __netconfig_set_bssid_scan_mode(gboolean enable)
298 {
299         if (netconfig_is_bssid_scan_started == enable)
300                 return;
301
302         netconfig_is_bssid_scan_started = enable;
303 }
304
305 void netconfig_wifi_bssid_signal_scandone(void)
306 {
307         bssid_list_count = 0;
308         _netconfig_wifi_bssid_get_bss();
309
310         netconfig_is_device_scanning = FALSE;
311
312         __netconfig_set_bssid_scan_mode(FALSE);
313 }
314
315 void netconfig_wifi_bssid_signal_scanaborted(void)
316 {
317         bssid_list_count = 0;
318         netconfig_is_bssid_scan_aborted = TRUE;
319         _netconfig_wifi_bssid_get_bss();
320
321         netconfig_is_device_scanning = FALSE;
322
323         __netconfig_set_bssid_scan_mode(FALSE);
324 }
325
326 static int __netconfig_wifi_bssid_request_scan(const char *if_path)
327 {
328         GDBusConnection *connection = NULL;
329         GVariant *message = NULL;
330         GVariantBuilder *builder = NULL;
331         const char *key1 = "Type";
332         const char *val1 = "passive";
333
334         if (if_path == NULL)
335                 if_path = netconfig_wifi_get_supplicant_interface();
336
337         if (if_path == NULL) {
338                 DBG("Fail to get wpa_supplicant DBus path");
339                 return -ESRCH;
340         }
341
342         connection = netdbus_get_connection();
343         if (connection == NULL) {
344                 ERR("Failed to get GDBusconnection");
345                 return -EIO;
346         }
347
348         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
349         g_variant_builder_add(builder, "{sv}", key1, g_variant_new_string(val1));
350         message = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
351         g_variant_builder_unref(builder);
352
353         g_dbus_connection_call(connection,
354                         SUPPLICANT_SERVICE,
355                         if_path,
356                         SUPPLICANT_INTERFACE ".Interface",
357                         "Scan",
358                         message,
359                         NULL,
360                         G_DBUS_CALL_FLAGS_NONE,
361                         NETCONFIG_DBUS_REPLY_TIMEOUT,
362                         netdbus_get_cancellable(),
363                         NULL,
364                         NULL);
365
366         netconfig_is_device_scanning = TRUE;
367
368         g_variant_unref(message);
369         /* Clear bss_info_list for the next scan result */
370         if (bssid_info_list) {
371                 g_slist_free_full(bssid_info_list, g_free);
372                 bssid_info_list = NULL;
373         }
374
375         netconfig_is_bssid_scan_aborted = FALSE;
376
377         return 0;
378 }
379
380 static void __netconfig_wifi_interface_create_result(
381                 GObject *source_object, GAsyncResult *res, gpointer user_data)
382 {
383         GVariant *message;
384         gchar *path = NULL;
385         GDBusConnection *conn = NULL;
386         GError *error = NULL;
387
388         conn = G_DBUS_CONNECTION(source_object);
389
390         message = g_dbus_connection_call_finish(conn, res, &error);
391         if (error == NULL) {
392                 g_variant_get(message, "(o)", &path);
393
394                 if (path) {
395                         __netconfig_wifi_bssid_request_scan(path);
396                         g_free(path);
397                 }
398         } else if (NULL != strstr(error->message, ".InterfaceExists")) {
399                 INFO("Error Message %s %s", error->message, path);
400                 g_variant_get(message, "(o)", &path);
401                 if (path) {
402                         __netconfig_wifi_bssid_request_scan(path);
403                         g_free(path);
404                 } else
405                         __netconfig_wifi_bssid_request_scan(NULL);
406         } else {
407                 ERR("Failed to create interface, Error: %d[%s]", error->code, error->message);
408                 __netconfig_set_bssid_scan_mode(FALSE);
409                 wifi_power_driver_and_supplicant(FALSE);
410         }
411
412         g_variant_unref(message);
413 }
414
415 static int  __netconfig_wifi_bssid_create_interface(void)
416 {
417         GDBusConnection *connection = NULL;
418         GVariant *message = NULL;
419         GVariantBuilder *builder = NULL;
420         const char *key = "Ifname";
421         const char *val = WIFI_IFNAME;
422
423         connection = netdbus_get_connection();
424         if (connection == NULL) {
425                 DBG("Failed to get GDBusconnection");
426                 return -EIO;
427         }
428
429         builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
430         g_variant_builder_add(builder, "{sv}", key, g_variant_new_string(val));
431         message = g_variant_new("(@a{sv})", g_variant_builder_end(builder));
432
433         g_dbus_connection_call(connection,
434                         SUPPLICANT_SERVICE,
435                         SUPPLICANT_PATH,
436                         SUPPLICANT_INTERFACE,
437                         "CreateInterface",
438                         message,
439                         NULL,
440                         G_DBUS_CALL_FLAGS_NONE,
441                         NETCONFIG_DBUS_REPLY_TIMEOUT,
442                         netdbus_get_cancellable(),
443                         (GAsyncReadyCallback) __netconfig_wifi_interface_create_result,
444                         NULL);
445
446         g_variant_unref(message);
447
448         return 0;
449 }
450
451 static int __netconfig_wifi_bssid_scan(void)
452 {
453         int err = 0;
454         wifi_tech_state_e wifi_tech_state;
455
456         if (netconfig_is_device_scanning == TRUE)
457                 return -EINPROGRESS;
458
459         wifi_tech_state = wifi_state_get_technology_state();
460         if (wifi_tech_state <= NETCONFIG_WIFI_TECH_OFF)
461                 err = wifi_power_driver_and_supplicant(TRUE);
462
463         if (err < 0 && err != -EALREADY)
464                 return err;
465
466         netconfig_is_device_scanning = TRUE;
467
468         DBG("BSSID scan requested");
469         if (wifi_tech_state >= NETCONFIG_WIFI_TECH_POWERED) {
470                 if (netconfig_wifi_get_scanning() == TRUE)
471                         return -EINPROGRESS;
472
473                 netconfig_wifi_bgscan_start(TRUE);
474
475                 if (wifi_tech_state == NETCONFIG_WIFI_TECH_CONNECTED)
476                         __netconfig_wifi_bssid_request_scan(NULL);
477         } else {
478                 err = __netconfig_wifi_bssid_create_interface();
479         }
480
481         return err;
482 }
483
484 gboolean handle_request_bssid_scan(Wifi *wifi, GDBusMethodInvocation *context)
485 {
486         int err, enabled = 0;
487         wifi_tech_state_e tech_state;
488
489         g_return_val_if_fail(wifi != NULL, FALSE);
490
491         if (netconfig_is_wifi_tethering_on() == TRUE) {
492                 ERR("Wi-Fi Tethering is enabled");
493                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_NO_SERVICE, "TetheringEnabled");
494                 return -EBUSY;
495         }
496
497 #if !defined TIZEN_WEARABLE
498         if (netconfig_wifi_is_bgscan_paused()) {
499                 ERR("Scan is paused");
500                 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_NO_SERVICE, "ScanPaused");
501                 return FALSE;
502         }
503 #endif
504
505         tech_state = wifi_state_get_technology_state();
506         if (tech_state <= NETCONFIG_WIFI_TECH_OFF) {
507 #if !defined TIZEN_WEARABLE
508                 enabled = 1;
509 #else
510                 enabled = 0;
511 #endif
512
513                 if (enabled == 0) {
514                         netconfig_error_permission_denied(context);
515                         return FALSE;
516                 }
517         }
518
519         __netconfig_set_bssid_scan_mode(TRUE);
520
521         err = __netconfig_wifi_bssid_scan();
522         if (err < 0) {
523                 if (err == -EINPROGRESS)
524                         netconfig_error_inprogress(context);
525                 else
526                         netconfig_error_wifi_driver_failed(context);
527
528                 return FALSE;
529         }
530
531         wifi_complete_request_bssid_scan(wifi, context);
532         return TRUE;
533 }