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