Change the User-Agent field to Tizen
[platform/upstream/connman.git] / plugins / wifi.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2014  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <net/ethernet.h>
34 #include <linux/wireless.h>
35
36 #if defined TIZEN_EXT
37 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
38 #endif
39
40 #ifndef IFF_LOWER_UP
41 #define IFF_LOWER_UP    0x10000
42 #endif
43
44 #include <dbus/dbus.h>
45 #include <glib.h>
46
47 #define CONNMAN_API_SUBJECT_TO_CHANGE
48 #include <connman/plugin.h>
49 #include <connman/inet.h>
50 #include <connman/device.h>
51 #include <connman/rtnl.h>
52 #include <connman/technology.h>
53 #include <connman/service.h>
54 #include <connman/peer.h>
55 #include <connman/log.h>
56 #include <connman/storage.h>
57 #include <include/setting.h>
58 #include <connman/provision.h>
59 #include <connman/utsname.h>
60 #include <connman/machine.h>
61 #include <connman/tethering.h>
62
63 #include <gsupplicant/gsupplicant.h>
64
65 #include "src/shared/util.h"
66
67 #define CLEANUP_TIMEOUT   8     /* in seconds */
68 #define INACTIVE_TIMEOUT  12    /* in seconds */
69 #define FAVORITE_MAXIMUM_RETRIES 2
70
71 #define BGSCAN_DEFAULT "simple:30:-65:300"
72 #define AUTOSCAN_EXPONENTIAL "exponential:3:300"
73 #define AUTOSCAN_SINGLE "single:3"
74 #define SCAN_MAX_DURATION 10
75
76 #define P2P_FIND_TIMEOUT 30
77 #define P2P_CONNECTION_TIMEOUT 100
78 #define P2P_LISTEN_PERIOD 500
79 #define P2P_LISTEN_INTERVAL 2000
80
81 #define ASSOC_STATUS_AUTH_TIMEOUT 16
82 #define ASSOC_STATUS_NO_CLIENT 17
83 #if defined TIZEN_EXT
84 #define LOAD_SHAPING_MAX_RETRIES 7
85 #else
86 #define LOAD_SHAPING_MAX_RETRIES 3
87 #endif
88
89 #if defined TIZEN_EXT
90 #define WIFI_EAP_FAST_PAC_FILE          "/var/lib/wifi/wifi.pac"        /* path of Pac file for EAP-FAST */
91
92         /* Wi-Fi Signal Strength (for 2.4G (dB))
93          * Excellent :     ~ -63
94          * Good      : -64 ~ -74
95          * Weak      : -75 ~ -82
96          * Very weak : -83 ~ -88
97          * No signal : -89 ~
98          *
99          * Wi-Fi Signal Strength (for 5G (dB))
100          * Excellent :     ~ -67
101          * Good      : -68 ~ -76
102          * Weak      : -77 ~ -82
103          * Very weak : -83 ~ -88
104          * No signal : -89 ~
105          *
106          * Wi-Fi Signal Strength (for 6G (dB))
107          *
108          * Excellent :     ~ -72
109          * Good      : -73 ~ -77
110          * Weak      : -78 ~ -82
111          * Very weak : -83 ~ -88
112          * No signal : -89 ~
113          */
114 #define RSSI_LEVEL_2_6G  -78
115 #define RSSI_LEVEL_2_5G  -77
116 #define RSSI_LEVEL_2_2_4G -75
117 #define RSSI_LEVEL_3_6G  -73
118 #define RSSI_LEVEL_3_5G  -68
119 #define RSSI_LEVEL_3_2_4G -64
120 #define ROAM_SCAN_INTERVAL 60 /* 60 seconds */
121 #define FREQUENCY_5G_BASE 5000
122 #define FREQUENCY_6G_BASE 5925
123
124 static int min_snr = 0;
125 static int min_rssi_2_4GHz = 0;
126 static int min_rssi_5GHz = 0;
127 static int min_rssi_6GHz = 0;
128 #endif
129
130 static struct connman_technology *wifi_technology = NULL;
131 static struct connman_technology *p2p_technology = NULL;
132
133 #if defined TIZEN_EXT
134 static GSupplicantState assoc_last_state = G_SUPPLICANT_STATE_UNKNOWN;
135 #endif
136
137 enum wifi_ap_capability{
138         WIFI_AP_UNKNOWN         = 0,
139         WIFI_AP_SUPPORTED       = 1,
140         WIFI_AP_NOT_SUPPORTED   = 2,
141 };
142
143 enum wifi_scanning_type {
144         WIFI_SCANNING_UNKNOWN   = 0,
145         WIFI_SCANNING_PASSIVE   = 1,
146         WIFI_SCANNING_ACTIVE    = 2,
147 };
148
149 struct hidden_params {
150         char ssid[32];
151         unsigned int ssid_len;
152         char *identity;
153         char *anonymous_identity;
154         char *subject_match;
155         char *altsubject_match;
156         char *domain_suffix_match;
157         char *domain_match;
158         char *passphrase;
159         char *security;
160         GSupplicantScanParams *scan_params;
161         gpointer user_data;
162 };
163
164 /**
165  * Used for autoscan "emulation".
166  * Should be removed when wpa_s autoscan support will be by default.
167  */
168 struct autoscan_params {
169         int base;
170         int limit;
171         int interval;
172         unsigned int timeout;
173 };
174
175 struct wifi_tethering_info {
176         struct wifi_data *wifi;
177         struct connman_technology *technology;
178         char *ifname;
179         GSupplicantSSID *ssid;
180 };
181
182 struct wifi_data {
183         char *identifier;
184         struct connman_device *device;
185         struct connman_network *network;
186         struct connman_network *pending_network;
187         GSList *networks;
188         GSupplicantInterface *interface;
189         GSupplicantState state;
190         bool connected;
191         bool disconnecting;
192         bool tethering;
193         enum wifi_ap_capability ap_supported;
194         bool bridged;
195         bool interface_ready;
196         const char *bridge;
197         int index;
198         unsigned flags;
199         unsigned int watch;
200         int retries;
201         int load_shaping_retries;
202         struct hidden_params *hidden;
203         bool postpone_hidden;
204         struct wifi_tethering_info *tethering_param;
205         /**
206          * autoscan "emulation".
207          */
208         struct autoscan_params *autoscan;
209         enum wifi_scanning_type scanning_type;
210         GSupplicantScanParams *scan_params;
211         unsigned int p2p_find_timeout;
212         unsigned int p2p_connection_timeout;
213         struct connman_peer *pending_peer;
214         GSList *peers;
215         bool p2p_connecting;
216         bool p2p_device;
217         int servicing;
218 #if defined TIZEN_EXT
219         int assoc_retry_count;
220         struct connman_network *scan_pending_network;
221         bool allow_full_scan;
222         unsigned int automaxspeed_timeout;
223         GSupplicantScanParams *hidden_scan_params;
224         unsigned int mac_policy;
225         unsigned int preassoc_mac_policy;
226         unsigned int mac_lifetime;
227 #endif
228         int disconnect_code;
229         int assoc_code;
230 #if defined TIZEN_EXT_WIFI_MESH
231         bool mesh_interface;
232         struct wifi_mesh_info *mesh_info;
233 #endif
234 };
235
236 struct wifi_network {
237         unsigned int keymgmt;
238 };
239
240 struct disconnect_data {
241         struct wifi_data *wifi;
242         struct connman_network *network;
243 };
244
245 #if defined TIZEN_EXT
246 #include "connman.h"
247 #include "dbus.h"
248
249 #define TIZEN_ASSOC_RETRY_COUNT         4
250
251 static gboolean wifi_first_scan = false;
252 static gboolean found_with_first_scan = false;
253 static gboolean is_wifi_notifier_registered = false;
254 static GHashTable *failed_bssids = NULL;
255 static unsigned char buff_bssid[WIFI_BSSID_LEN_MAX] = { 0, };
256 #endif
257
258 static GList *iface_list = NULL;
259
260 static GList *pending_wifi_device = NULL;
261 static GList *p2p_iface_list = NULL;
262 static bool wfd_service_registered = false;
263
264 static void start_autoscan(struct connman_device *device);
265 static int tech_set_tethering(struct connman_technology *technology,
266                                 const char *bridge, bool enabled);
267
268 #if defined TIZEN_EXT
269 #define NETCONFIG_SERVICE "net.netconfig"
270 #define NETCONFIG_WIFI_PATH "/net/netconfig/wifi"
271 #define NETCONFIG_WIFI_INTERFACE NETCONFIG_SERVICE ".wifi"
272
273 struct enc_method_call_data {
274         DBusConnection *connection;
275         struct connman_network *network;
276 };
277
278 static struct enc_method_call_data encrypt_request_data;
279
280 static GSupplicantSecurity network_security(const char *security);
281
282 static void encryption_request_reply(DBusPendingCall *call,
283                                                 void *user_data)
284 {
285         DBusMessage *reply;
286         DBusError error;
287         DBusMessageIter args;
288         char *out_data;
289         struct connman_service *service;
290         gchar* encrypted_value = NULL;
291         struct connman_network *network = encrypt_request_data.network;
292
293         DBG("");
294
295         reply = dbus_pending_call_steal_reply(call);
296
297         dbus_error_init(&error);
298         if (dbus_set_error_from_message(&error, reply)) {
299                 DBG("send_encryption_request() %s %s", error.name, error.message);
300                 dbus_error_free(&error);
301                 goto done;
302         }
303
304         if (dbus_message_iter_init(reply, &args) == FALSE)
305                 goto done;
306
307         dbus_message_iter_get_basic(&args, &out_data);
308
309         encrypted_value = g_strdup((const gchar *)out_data);
310         service = connman_service_lookup_from_network(network);
311
312         if (!service) {
313                 DBG("encryption result: no service");
314                 goto done;
315         }
316
317         if (connman_service_get_favorite(service)) {
318                 __connman_service_set_passphrase(service, encrypted_value);
319                 __connman_service_save(service);
320         } else
321                 connman_network_set_string(network, "WiFi.Passphrase",
322                                                         encrypted_value);
323
324         DBG("encryption result: succeeded");
325
326 done:
327         dbus_message_unref(reply);
328         dbus_pending_call_unref(call);
329         dbus_connection_unref(encrypt_request_data.connection);
330         g_free(encrypted_value);
331
332         encrypt_request_data.connection = NULL;
333         encrypt_request_data.network = NULL;
334 }
335
336 static int send_encryption_request(const char *passphrase,
337                                 struct connman_network *network)
338 {
339         DBusConnection *connection = NULL;
340         DBusMessage *msg = NULL;
341         DBusPendingCall *call;
342
343         if (!passphrase) {
344                 DBG("Invalid parameter");
345                 return -EINVAL;
346         }
347
348         connection = connman_dbus_get_connection();
349         if (!connection) {
350                 DBG("dbus connection does not exist");
351                 return -EINVAL;
352         }
353
354         msg = dbus_message_new_method_call(NETCONFIG_SERVICE, NETCONFIG_WIFI_PATH,
355                         NETCONFIG_WIFI_INTERFACE, "EncryptPassphrase");
356         if (!msg) {
357                 dbus_connection_unref(connection);
358                 return -EINVAL;
359         }
360
361         dbus_message_append_args(msg, DBUS_TYPE_STRING, &passphrase,
362                                                         DBUS_TYPE_INVALID);
363
364         if (!dbus_connection_send_with_reply(connection, msg,
365                                 &call, DBUS_TIMEOUT_USE_DEFAULT)) {
366                 dbus_message_unref(msg);
367                 dbus_connection_unref(connection);
368                 return -EIO;
369         }
370
371         if (!call) {
372                 dbus_message_unref(msg);
373                 dbus_connection_unref(connection);
374                 return -EIO;
375         }
376
377         encrypt_request_data.connection = connection;
378         encrypt_request_data.network = network;
379
380         dbus_pending_call_set_notify(call, encryption_request_reply, NULL, NULL);
381         dbus_message_unref(msg);
382
383         return 0;
384 }
385 #endif
386
387 static int p2p_tech_probe(struct connman_technology *technology)
388 {
389         p2p_technology = technology;
390
391         return 0;
392 }
393
394 static void p2p_tech_remove(struct connman_technology *technology)
395 {
396         p2p_technology = NULL;
397 }
398
399 static struct connman_technology_driver p2p_tech_driver = {
400         .name           = "p2p",
401         .type           = CONNMAN_SERVICE_TYPE_P2P,
402         .probe          = p2p_tech_probe,
403         .remove         = p2p_tech_remove,
404 };
405
406 static bool is_p2p_connecting(void)
407 {
408         GList *list;
409
410         for (list = iface_list; list; list = list->next) {
411                 struct wifi_data *wifi = list->data;
412
413                 if (wifi->p2p_connecting)
414                         return true;
415         }
416
417         return false;
418 }
419
420 static void add_pending_wifi_device(struct wifi_data *wifi)
421 {
422         if (g_list_find(pending_wifi_device, wifi))
423                 return;
424
425         pending_wifi_device = g_list_append(pending_wifi_device, wifi);
426 }
427
428 #if defined TIZEN_EXT_WIFI_MESH
429 struct wifi_mesh_info {
430         struct wifi_data *wifi;
431         GSupplicantInterface *interface;
432         struct connman_mesh *mesh;
433         char *parent_ifname;
434         char *ifname;
435         char *identifier;
436         int index;
437 };
438
439 struct mesh_change_peer_status_info {
440         char *peer_address;
441         enum connman_mesh_peer_status peer_status;
442         mesh_change_peer_status_cb_t callback;
443         void *user_data;
444 };
445
446 static struct connman_technology_driver mesh_tech_driver = {
447         .name = "mesh",
448         .type = CONNMAN_SERVICE_TYPE_MESH,
449 };
450
451 static void mesh_interface_create_callback(int result,
452                                            GSupplicantInterface *interface,
453                                            void *user_data)
454 {
455         struct wifi_mesh_info *mesh_info = user_data;
456         struct wifi_data *wifi;
457         bool success = false;
458
459         DBG("result %d ifname %s, mesh_info %p", result,
460                                 g_supplicant_interface_get_ifname(interface),
461                                 mesh_info);
462
463         if (result < 0 || !mesh_info)
464                 goto done;
465
466         wifi = mesh_info->wifi;
467
468         mesh_info->interface = interface;
469         mesh_info->identifier = connman_inet_ifaddr(mesh_info->ifname);
470         mesh_info->index = connman_inet_ifindex(mesh_info->ifname);
471         DBG("Mesh Interface identifier %s", mesh_info->identifier);
472         wifi->mesh_interface = true;
473         wifi->mesh_info = mesh_info;
474         g_supplicant_interface_set_data(interface, wifi);
475         success = true;
476
477 done:
478         connman_mesh_notify_interface_create(success);
479 }
480
481 static int add_mesh_interface(const char *ifname, const char *parent_ifname)
482 {
483         GList *list;
484         struct wifi_data *wifi;
485         struct wifi_mesh_info *mesh_info;
486         const char *wifi_ifname;
487         bool parent_found = false;
488         const char *driver = "nl80211";
489
490         for (list = iface_list; list; list = list->next) {
491                 wifi = list->data;
492
493                 if (!g_supplicant_interface_has_mesh(wifi->interface))
494                         continue;
495
496                 wifi_ifname = g_supplicant_interface_get_ifname(wifi->interface);
497                 if (!wifi_ifname)
498                         continue;
499
500                 if (!g_strcmp0(wifi_ifname, parent_ifname)) {
501                         parent_found = true;
502                         break;
503                 }
504         }
505
506         if (!parent_found) {
507                 DBG("Parent interface %s doesn't exist", parent_ifname);
508                 return -ENODEV;
509         }
510
511         mesh_info = g_try_malloc0(sizeof(struct wifi_mesh_info));
512         if (!mesh_info)
513                 return -ENOMEM;
514
515         mesh_info->wifi = wifi;
516         mesh_info->ifname = g_strdup(ifname);
517         mesh_info->parent_ifname = g_strdup(parent_ifname);
518
519         g_supplicant_mesh_interface_create(ifname, driver, NULL, parent_ifname,
520                                                 mesh_interface_create_callback, mesh_info);
521         return -EINPROGRESS;
522 }
523
524 static void mesh_interface_remove_callback(int result,
525                                         GSupplicantInterface *interface,
526                                                         void *user_data)
527 {
528         struct wifi_data *wifi = user_data;
529         struct wifi_mesh_info *mesh_info = wifi->mesh_info;
530         bool success = false;
531
532         DBG("result %d mesh_info %p", result, mesh_info);
533
534         if (result < 0 || !mesh_info)
535                 goto done;
536
537         mesh_info->interface = NULL;
538         g_free(mesh_info->parent_ifname);
539         g_free(mesh_info->ifname);
540         g_free(mesh_info->identifier);
541         g_free(mesh_info);
542         wifi->mesh_interface = false;
543         wifi->mesh_info = NULL;
544         success = true;
545
546 done:
547         connman_mesh_notify_interface_remove(success);
548 }
549
550 static int remove_mesh_interface(const char *ifname)
551 {
552         GList *list;
553         struct wifi_data *wifi;
554         struct wifi_mesh_info *mesh_info;
555         bool mesh_if_found = false;
556         int ret;
557
558         for (list = iface_list; list; list = list->next) {
559                 wifi = list->data;
560
561                 if (wifi->mesh_interface) {
562                         mesh_if_found = true;
563                         break;
564                 }
565         }
566
567         if (!mesh_if_found) {
568                 DBG("Mesh interface %s doesn't exist", ifname);
569                 return -ENODEV;
570         }
571
572         mesh_info = wifi->mesh_info;
573         ret = g_supplicant_interface_remove(mesh_info->interface,
574                                                 mesh_interface_remove_callback, wifi);
575         if (ret < 0)
576                 return ret;
577
578         return -EINPROGRESS;
579 }
580
581 static void mesh_disconnect_callback(int result,
582                                         GSupplicantInterface *interface, void *user_data)
583 {
584         struct connman_mesh *mesh = user_data;
585
586         DBG("result %d interface %p mesh %p", result, interface, mesh);
587 }
588
589 static int mesh_peer_disconnect(struct connman_mesh *mesh)
590 {
591         GList *list;
592         struct wifi_data *wifi;
593         struct wifi_mesh_info *mesh_info;
594         bool mesh_if_found = false;
595         GSupplicantInterface *interface;
596
597         for (list = iface_list; list; list = list->next) {
598                 wifi = list->data;
599
600                 if (wifi->mesh_interface) {
601                         mesh_if_found = true;
602                         break;
603                 }
604         }
605
606         if (!mesh_if_found) {
607                 DBG("Mesh interface is not created");
608                 return -ENODEV;
609         }
610
611         mesh_info = wifi->mesh_info;
612
613         interface = mesh_info->interface;
614         return g_supplicant_interface_disconnect(interface,
615                                                 mesh_disconnect_callback, mesh);
616 }
617
618 static void mesh_connect_callback(int result, GSupplicantInterface *interface,
619                                                                   void *user_data)
620 {
621         struct connman_mesh *mesh = user_data;
622         DBG("mesh %p result %d", mesh, result);
623
624         if (result < 0)
625                 connman_mesh_peer_set_state(mesh, CONNMAN_MESH_STATE_FAILURE);
626         else
627                 connman_mesh_peer_set_state(mesh, CONNMAN_MESH_STATE_ASSOCIATION);
628 }
629
630 static GSupplicantSecurity mesh_network_security(const char *security)
631 {
632         if (g_str_equal(security, "none"))
633                 return G_SUPPLICANT_SECURITY_NONE;
634         else if (g_str_equal(security, "sae"))
635                 return G_SUPPLICANT_SECURITY_SAE;
636
637         return G_SUPPLICANT_SECURITY_UNKNOWN;
638 }
639
640 static void mesh_ssid_init(GSupplicantSSID *ssid, struct connman_mesh *mesh)
641 {
642         const char *name;
643         const char *security;
644
645         if (ssid->ssid)
646                 g_free(ssid->ssid);
647
648         memset(ssid, 0, sizeof(*ssid));
649         ssid->mode = G_SUPPLICANT_MODE_MESH;
650
651         security = connman_mesh_get_security(mesh);
652         ssid->security = mesh_network_security(security);
653
654         if (ssid->security == G_SUPPLICANT_SECURITY_SAE)
655                 ssid->passphrase = connman_mesh_get_passphrase(mesh);
656
657         ssid->freq = connman_mesh_get_frequency(mesh);
658         name = connman_mesh_get_name(mesh);
659         if (name) {
660                 ssid->ssid_len = strlen(name);
661                 ssid->ssid = g_malloc0(ssid->ssid_len + 1);
662                 memcpy(ssid->ssid, name, ssid->ssid_len);
663                 ssid->scan_ssid = 1;
664         }
665 }
666
667 static int mesh_peer_connect(struct connman_mesh *mesh)
668 {
669         GList *list;
670         struct wifi_data *wifi;
671         struct wifi_mesh_info *mesh_info;
672         bool mesh_if_found = false;
673         GSupplicantInterface *interface;
674         GSupplicantSSID *ssid;
675
676         for (list = iface_list; list; list = list->next) {
677                 wifi = list->data;
678
679                 if (wifi->mesh_interface) {
680                         mesh_if_found = true;
681                         break;
682                 }
683         }
684
685         if (!mesh_if_found) {
686                 DBG("Mesh interface is not created");
687                 return -ENODEV;
688         }
689
690         mesh_info = wifi->mesh_info;
691
692         interface = mesh_info->interface;
693
694         ssid = g_try_malloc0(sizeof(GSupplicantSSID));
695         if (!ssid)
696                 return -ENOMEM;
697
698         mesh_info->mesh = mesh;
699
700         mesh_ssid_init(ssid, mesh);
701         return g_supplicant_interface_connect(interface, ssid,
702                                                 mesh_connect_callback, mesh);
703 }
704
705 static void mesh_peer_change_status_callback(int result,
706                                              GSupplicantInterface *interface,
707                                              void *user_data)
708 {
709         struct mesh_change_peer_status_info *data = user_data;
710
711         DBG("result %d Peer Status %d", result, data->peer_status);
712
713         if (result == 0 && data->peer_status == CONNMAN_MESH_PEER_REMOVE) {
714                 /* WLAN_REASON_MESH_PEERING_CANCELLED = 52 */
715                 connman_mesh_remove_connected_peer(data->peer_address, 52);
716         }
717
718         if (data->callback)
719                 data->callback(result, data->user_data);
720
721         g_free(data->peer_address);
722         g_free(data);
723         return;
724 }
725
726 static int mesh_change_peer_status(const char *peer_address,
727                                    enum connman_mesh_peer_status status,
728                                    mesh_change_peer_status_cb_t callback, void *user_data)
729 {
730         GList *list;
731         struct wifi_data *wifi;
732         struct wifi_mesh_info *mesh_info;
733         bool mesh_if_found = false;
734         GSupplicantInterface *interface;
735         struct mesh_change_peer_status_info *data;
736         const char *method;
737
738         for (list = iface_list; list; list = list->next) {
739                 wifi = list->data;
740
741                 if (wifi->mesh_interface) {
742                         mesh_if_found = true;
743                         break;
744                 }
745         }
746
747         if (!mesh_if_found) {
748                 DBG("Mesh interface is not created");
749                 return -ENODEV;
750         }
751
752         mesh_info = wifi->mesh_info;
753
754         interface = mesh_info->interface;
755
756         switch (status) {
757         case CONNMAN_MESH_PEER_ADD:
758                 method = "MeshPeerAdd";
759                 break;
760         case CONNMAN_MESH_PEER_REMOVE:
761                 method = "MeshPeerRemove";
762                 break;
763         default:
764                 DBG("Invalid method");
765                 return -EINVAL;
766         }
767
768         data = g_try_malloc0(sizeof(struct mesh_change_peer_status_info));
769         if (data == NULL) {
770                 DBG("Memory allocation failed");
771                 return -ENOMEM;
772         }
773
774         data->peer_address = g_strdup(peer_address);
775         data->peer_status = status;
776         data->callback = callback;
777         data->user_data = user_data;
778
779         return g_supplicant_interface_mesh_peer_change_status(interface,
780                                                 mesh_peer_change_status_callback, peer_address, method,
781                                                 data);
782 }
783
784 static struct connman_mesh_driver mesh_driver = {
785         .add_interface      = add_mesh_interface,
786         .remove_interface   = remove_mesh_interface,
787         .connect            = mesh_peer_connect,
788         .disconnect         = mesh_peer_disconnect,
789         .change_peer_status = mesh_change_peer_status,
790 };
791
792 static void mesh_support(GSupplicantInterface *interface)
793 {
794         DBG("");
795
796         if (!g_supplicant_interface_has_mesh(interface))
797                 return;
798
799         if (connman_technology_driver_register(&mesh_tech_driver) < 0) {
800                 DBG("Could not register Mesh technology driver");
801                 return;
802         }
803
804         connman_mesh_driver_register(&mesh_driver);
805 }
806
807 static void check_mesh_technology(void)
808 {
809         bool mesh_exists = false;
810         GList *list;
811
812         for (list = iface_list; list; list = list->next) {
813                 struct wifi_data *w = list->data;
814
815                 if (w->interface &&
816                                 g_supplicant_interface_has_mesh(w->interface))
817                         mesh_exists = true;
818         }
819
820         if (!mesh_exists) {
821                 connman_technology_driver_unregister(&mesh_tech_driver);
822                 connman_mesh_driver_unregister(&mesh_driver);
823         }
824 }
825
826 static void mesh_group_started(GSupplicantInterface *interface)
827 {
828         struct wifi_data *wifi;
829         struct wifi_mesh_info *mesh_info;
830         struct connman_mesh *mesh;
831         const unsigned char *ssid;
832         unsigned int ssid_len;
833         char name[33];
834
835         ssid = g_supplicant_interface_get_mesh_group_ssid(interface, &ssid_len);
836         memcpy(name, ssid, ssid_len);
837         name[ssid_len] = '\0';
838         DBG("name %s", name);
839         wifi = g_supplicant_interface_get_data(interface);
840         DBG("wifi %p", wifi);
841
842         if (!wifi)
843                 return;
844
845         mesh_info = wifi->mesh_info;
846         if (!mesh_info)
847                 return;
848
849         mesh = mesh_info->mesh;
850         if (!mesh)
851                 return;
852
853         connman_mesh_peer_set_state(mesh, CONNMAN_MESH_STATE_CONFIGURATION);
854 }
855
856 static void mesh_group_removed(GSupplicantInterface *interface)
857 {
858         struct wifi_data *wifi;
859         struct wifi_mesh_info *mesh_info;
860         struct connman_mesh *mesh;
861         const unsigned char *ssid;
862         unsigned int ssid_len;
863         int disconnect_reason;
864         char name[33];
865
866         ssid = g_supplicant_interface_get_mesh_group_ssid(interface, &ssid_len);
867         memcpy(name, ssid, ssid_len);
868         name[ssid_len] = '\0';
869         DBG("name %s", name);
870
871         disconnect_reason = g_supplicant_mesh_get_disconnect_reason(interface);
872         DBG("Disconnect Reason %d", disconnect_reason);
873
874         wifi = g_supplicant_interface_get_data(interface);
875         DBG("wifi %p", wifi);
876
877         if (!wifi)
878                 return;
879
880         mesh_info = wifi->mesh_info;
881         if (!mesh_info)
882                 return;
883
884         mesh = connman_get_connected_mesh_from_name(name);
885         if (!mesh) {
886                 DBG("%s is not connected", name);
887                 mesh = connman_get_connecting_mesh_from_name(name);
888                 if (!mesh) {
889                         DBG("%s is not connecting", name);
890                         return;
891                 }
892         }
893
894         connman_mesh_peer_set_disconnect_reason(mesh, disconnect_reason);
895         connman_mesh_peer_set_state(mesh, CONNMAN_MESH_STATE_DISCONNECT);
896 }
897
898 static void mesh_peer_connected(GSupplicantMeshPeer *mesh_peer)
899 {
900         const char *peer_address;
901
902         peer_address = g_supplicant_mesh_peer_get_address(mesh_peer);
903
904         if (!peer_address)
905                 return;
906
907         DBG("Peer %s connected", peer_address);
908         connman_mesh_add_connected_peer(peer_address);
909 }
910
911 static void mesh_peer_disconnected(GSupplicantMeshPeer *mesh_peer)
912 {
913         const char *peer_address;
914         int reason;
915
916         peer_address = g_supplicant_mesh_peer_get_address(mesh_peer);
917
918         if (!peer_address)
919                 return;
920
921         reason = g_supplicant_mesh_peer_get_disconnect_reason(mesh_peer);
922
923         DBG("Peer %s disconnected with reason %d", peer_address, reason);
924         connman_mesh_remove_connected_peer(peer_address, reason);
925 }
926 #endif
927
928 static struct wifi_data *get_pending_wifi_data(const char *ifname)
929 {
930         GList *list;
931
932         for (list = pending_wifi_device; list; list = list->next) {
933                 struct wifi_data *wifi;
934                 const char *dev_name;
935
936                 wifi = list->data;
937                 if (!wifi || !wifi->device)
938                         continue;
939
940                 dev_name = connman_device_get_string(wifi->device, "Interface");
941                 if (!g_strcmp0(ifname, dev_name)) {
942                         pending_wifi_device = g_list_delete_link(
943                                                 pending_wifi_device, list);
944                         return wifi;
945                 }
946         }
947
948         return NULL;
949 }
950
951 static void remove_pending_wifi_device(struct wifi_data *wifi)
952 {
953         GList *link;
954
955         link = g_list_find(pending_wifi_device, wifi);
956
957         if (!link)
958                 return;
959
960         pending_wifi_device = g_list_delete_link(pending_wifi_device, link);
961 }
962
963 static void peer_cancel_timeout(struct wifi_data *wifi)
964 {
965         if (wifi->p2p_connection_timeout > 0)
966                 g_source_remove(wifi->p2p_connection_timeout);
967
968         wifi->p2p_connection_timeout = 0;
969         wifi->p2p_connecting = false;
970
971         if (wifi->pending_peer) {
972                 connman_peer_unref(wifi->pending_peer);
973                 wifi->pending_peer = NULL;
974         }
975 }
976
977 static gboolean peer_connect_timeout(gpointer data)
978 {
979         struct wifi_data *wifi = data;
980
981         DBG("");
982
983         if (wifi->p2p_connecting) {
984                 enum connman_peer_state state = CONNMAN_PEER_STATE_FAILURE;
985                 GSupplicantPeer *gs_peer =
986                         g_supplicant_interface_peer_lookup(wifi->interface,
987                                 connman_peer_get_identifier(wifi->pending_peer));
988
989                 if (g_supplicant_peer_has_requested_connection(gs_peer))
990                         state = CONNMAN_PEER_STATE_IDLE;
991
992                 connman_peer_set_state(wifi->pending_peer, state);
993         }
994
995         peer_cancel_timeout(wifi);
996
997         return FALSE;
998 }
999
1000 static void peer_connect_callback(int result, GSupplicantInterface *interface,
1001                                                         void *user_data)
1002 {
1003         struct wifi_data *wifi = user_data;
1004         struct connman_peer *peer = wifi->pending_peer;
1005
1006         DBG("peer %p - %d", peer, result);
1007
1008         if (!peer)
1009                 return;
1010
1011         if (result < 0) {
1012                 peer_connect_timeout(wifi);
1013                 return;
1014         }
1015
1016         connman_peer_set_state(peer, CONNMAN_PEER_STATE_ASSOCIATION);
1017
1018         wifi->p2p_connection_timeout = g_timeout_add_seconds(
1019                                                 P2P_CONNECTION_TIMEOUT,
1020                                                 peer_connect_timeout, wifi);
1021 }
1022
1023 static int peer_connect(struct connman_peer *peer,
1024                         enum connman_peer_wps_method wps_method,
1025                         const char *wps_pin)
1026 {
1027         struct connman_device *device = connman_peer_get_device(peer);
1028         GSupplicantPeerParams *peer_params;
1029         GSupplicantPeer *gs_peer;
1030         struct wifi_data *wifi;
1031         bool pbc, pin;
1032         int ret;
1033
1034         DBG("peer %p", peer);
1035
1036         if (!device)
1037                 return -ENODEV;
1038
1039         wifi = connman_device_get_data(device);
1040         if (!wifi || !wifi->interface)
1041                 return -ENODEV;
1042
1043         if (wifi->p2p_connecting)
1044                 return -EBUSY;
1045
1046         gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
1047                                         connman_peer_get_identifier(peer));
1048         if (!gs_peer)
1049                 return -EINVAL;
1050
1051         pbc = g_supplicant_peer_is_wps_pbc(gs_peer);
1052         pin = g_supplicant_peer_is_wps_pin(gs_peer);
1053
1054         switch (wps_method) {
1055         case CONNMAN_PEER_WPS_UNKNOWN:
1056                 if ((pbc && pin) || pin)
1057                         return -ENOKEY;
1058                 break;
1059         case CONNMAN_PEER_WPS_PBC:
1060                 if (!pbc)
1061                         return -EINVAL;
1062                 wps_pin = NULL;
1063                 break;
1064         case CONNMAN_PEER_WPS_PIN:
1065                 if (!pin || !wps_pin)
1066                         return -EINVAL;
1067                 break;
1068         }
1069
1070         peer_params = g_try_malloc0(sizeof(GSupplicantPeerParams));
1071         if (!peer_params)
1072                 return -ENOMEM;
1073
1074         peer_params->path = g_strdup(g_supplicant_peer_get_path(gs_peer));
1075         if (wps_pin)
1076                 peer_params->wps_pin = g_strdup(wps_pin);
1077
1078         peer_params->master = connman_peer_service_is_master();
1079
1080         ret = g_supplicant_interface_p2p_connect(wifi->interface, peer_params,
1081                                                 peer_connect_callback, wifi);
1082         if (ret == -EINPROGRESS) {
1083                 wifi->pending_peer = connman_peer_ref(peer);
1084                 wifi->p2p_connecting = true;
1085         } else if (ret < 0) {
1086                 g_free(peer_params->path);
1087                 g_free(peer_params->wps_pin);
1088                 g_free(peer_params);
1089         }
1090
1091         return ret;
1092 }
1093
1094 static int peer_disconnect(struct connman_peer *peer)
1095 {
1096         struct connman_device *device = connman_peer_get_device(peer);
1097         GSupplicantPeerParams peer_params = {};
1098         GSupplicantPeer *gs_peer;
1099         struct wifi_data *wifi;
1100         int ret;
1101
1102         DBG("peer %p", peer);
1103
1104         if (!device)
1105                 return -ENODEV;
1106
1107         wifi = connman_device_get_data(device);
1108         if (!wifi)
1109                 return -ENODEV;
1110
1111         gs_peer = g_supplicant_interface_peer_lookup(wifi->interface,
1112                                         connman_peer_get_identifier(peer));
1113         if (!gs_peer)
1114                 return -EINVAL;
1115
1116         peer_params.path = g_strdup(g_supplicant_peer_get_path(gs_peer));
1117
1118         ret = g_supplicant_interface_p2p_disconnect(wifi->interface,
1119                                                         &peer_params);
1120         g_free(peer_params.path);
1121
1122         if (ret == -EINPROGRESS) {
1123                 peer_cancel_timeout(wifi);
1124                 wifi->p2p_device = false;
1125         }
1126
1127         return ret;
1128 }
1129
1130 struct peer_service_registration {
1131         peer_service_registration_cb_t callback;
1132         void *user_data;
1133 };
1134
1135 static bool is_service_wfd(const unsigned char *specs, int length)
1136 {
1137         if (length < 9 || specs[0] != 0 || specs[1] != 0 || specs[2] != 6)
1138                 return false;
1139
1140         return true;
1141 }
1142
1143 static void apply_p2p_listen_on_iface(gpointer data, gpointer user_data)
1144 {
1145         struct wifi_data *wifi = data;
1146
1147         if (!wifi->interface ||
1148                         !g_supplicant_interface_has_p2p(wifi->interface))
1149                 return;
1150
1151         if (!wifi->servicing) {
1152                 g_supplicant_interface_p2p_listen(wifi->interface,
1153                                 P2P_LISTEN_PERIOD, P2P_LISTEN_INTERVAL);
1154         }
1155
1156         wifi->servicing++;
1157 }
1158
1159 static void register_wfd_service_cb(int result,
1160                                 GSupplicantInterface *iface, void *user_data)
1161 {
1162         struct peer_service_registration *reg_data = user_data;
1163
1164         DBG("");
1165
1166         if (result == 0)
1167                 g_list_foreach(iface_list, apply_p2p_listen_on_iface, NULL);
1168
1169         if (reg_data && reg_data->callback) {
1170                 reg_data->callback(result, reg_data->user_data);
1171                 g_free(reg_data);
1172         }
1173 }
1174
1175 static GSupplicantP2PServiceParams *fill_in_peer_service_params(
1176                                 const unsigned char *spec,
1177                                 int spec_length, const unsigned char *query,
1178                                 int query_length, int version)
1179 {
1180         GSupplicantP2PServiceParams *params;
1181
1182         params = g_try_malloc0(sizeof(GSupplicantP2PServiceParams));
1183         if (!params)
1184                 return NULL;
1185
1186         if (version > 0) {
1187                 params->version = version;
1188                 if (spec_length > 0) {
1189                         params->service = g_malloc(spec_length);
1190                         memcpy(params->service, spec, spec_length);
1191                 }
1192         } else if (query_length > 0 && spec_length > 0) {
1193                 params->query = g_malloc(query_length);
1194                 memcpy(params->query, query, query_length);
1195                 params->query_length = query_length;
1196
1197                 params->response = g_malloc(spec_length);
1198                 memcpy(params->response, spec, spec_length);
1199                 params->response_length = spec_length;
1200         } else {
1201                 if (spec_length > 0) {
1202                         params->wfd_ies = g_malloc(spec_length);
1203                         memcpy(params->wfd_ies, spec, spec_length);
1204                 }
1205                 params->wfd_ies_length = spec_length;
1206         }
1207
1208         return params;
1209 }
1210
1211 static void free_peer_service_params(GSupplicantP2PServiceParams *params)
1212 {
1213         if (!params)
1214                 return;
1215
1216         g_free(params->service);
1217         g_free(params->query);
1218         g_free(params->response);
1219         g_free(params->wfd_ies);
1220
1221         g_free(params);
1222 }
1223
1224 static int peer_register_wfd_service(const unsigned char *specification,
1225                                 int specification_length,
1226                                 peer_service_registration_cb_t callback,
1227                                 void *user_data)
1228 {
1229         struct peer_service_registration *reg_data = NULL;
1230         static GSupplicantP2PServiceParams *params;
1231         int ret;
1232
1233         DBG("");
1234
1235         if (wfd_service_registered)
1236                 return -EBUSY;
1237
1238         params = fill_in_peer_service_params(specification,
1239                                         specification_length, NULL, 0, 0);
1240         if (!params)
1241                 return -ENOMEM;
1242
1243         reg_data = g_try_malloc0(sizeof(*reg_data));
1244         if (!reg_data) {
1245                 ret = -ENOMEM;
1246                 goto error;
1247         }
1248
1249         reg_data->callback = callback;
1250         reg_data->user_data = user_data;
1251
1252         ret = g_supplicant_set_widi_ies(params,
1253                                         register_wfd_service_cb, reg_data);
1254         if (ret < 0 && ret != -EINPROGRESS)
1255                 goto error;
1256
1257         wfd_service_registered = true;
1258
1259         return ret;
1260 error:
1261         free_peer_service_params(params);
1262         g_free(reg_data);
1263
1264         return ret;
1265 }
1266
1267 static void register_peer_service_cb(int result,
1268                                 GSupplicantInterface *iface, void *user_data)
1269 {
1270         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
1271         struct peer_service_registration *reg_data = user_data;
1272
1273 #if defined TIZEN_EXT
1274         if (!wifi)
1275                 return;
1276 #endif
1277
1278         DBG("");
1279
1280         if (result == 0)
1281                 apply_p2p_listen_on_iface(wifi, NULL);
1282
1283         if (reg_data->callback)
1284                 reg_data->callback(result, reg_data->user_data);
1285
1286         g_free(reg_data);
1287 }
1288
1289 static int peer_register_service(const unsigned char *specification,
1290                                 int specification_length,
1291                                 const unsigned char *query,
1292                                 int query_length, int version,
1293                                 peer_service_registration_cb_t callback,
1294                                 void *user_data)
1295 {
1296         struct peer_service_registration *reg_data;
1297         GSupplicantP2PServiceParams *params;
1298         bool found = false;
1299         int ret, ret_f;
1300         GList *list;
1301
1302         DBG("");
1303
1304         if (specification && !version && !query &&
1305                         is_service_wfd(specification, specification_length)) {
1306                 return peer_register_wfd_service(specification,
1307                                 specification_length, callback, user_data);
1308         }
1309
1310         reg_data = g_try_malloc0(sizeof(*reg_data));
1311         if (!reg_data)
1312                 return -ENOMEM;
1313
1314         reg_data->callback = callback;
1315         reg_data->user_data = user_data;
1316
1317         ret_f = -EOPNOTSUPP;
1318
1319         for (list = iface_list; list; list = list->next) {
1320                 struct wifi_data *wifi = list->data;
1321                 GSupplicantInterface *iface = wifi->interface;
1322
1323                 if (!g_supplicant_interface_has_p2p(iface))
1324                         continue;
1325
1326                 params = fill_in_peer_service_params(specification,
1327                                                 specification_length, query,
1328                                                 query_length, version);
1329                 if (!params)
1330                         continue;
1331
1332                 if (!found) {
1333                         ret_f = g_supplicant_interface_p2p_add_service(iface,
1334                                 register_peer_service_cb, params, reg_data);
1335                         if (ret_f == 0 || ret_f == -EINPROGRESS)
1336                                 found = true;
1337                         ret = ret_f;
1338                 } else
1339                         ret = g_supplicant_interface_p2p_add_service(iface,
1340                                 register_peer_service_cb, params, NULL);
1341                 if (ret != 0 && ret != -EINPROGRESS)
1342                         free_peer_service_params(params);
1343         }
1344
1345         if (ret_f != 0 && ret_f != -EINPROGRESS)
1346                 g_free(reg_data);
1347
1348         return ret_f;
1349 }
1350
1351 static int peer_unregister_wfd_service(void)
1352 {
1353         GSupplicantP2PServiceParams *params;
1354         GList *list;
1355
1356         if (!wfd_service_registered)
1357                 return -EALREADY;
1358
1359         params = fill_in_peer_service_params(NULL, 0, NULL, 0, 0);
1360         if (!params)
1361                 return -ENOMEM;
1362
1363         wfd_service_registered = false;
1364
1365         g_supplicant_set_widi_ies(params, NULL, NULL);
1366
1367         for (list = iface_list; list; list = list->next) {
1368                 struct wifi_data *wifi = list->data;
1369
1370                 if (!g_supplicant_interface_has_p2p(wifi->interface))
1371                         continue;
1372
1373                 wifi->servicing--;
1374                 if (!wifi->servicing || wifi->servicing < 0) {
1375                         g_supplicant_interface_p2p_listen(wifi->interface,
1376                                                                         0, 0);
1377                         wifi->servicing = 0;
1378                 }
1379         }
1380
1381         return 0;
1382 }
1383
1384 static int peer_unregister_service(const unsigned char *specification,
1385                                                 int specification_length,
1386                                                 const unsigned char *query,
1387                                                 int query_length, int version)
1388 {
1389         GSupplicantP2PServiceParams *params;
1390         bool wfd = false;
1391         GList *list;
1392         int ret;
1393
1394         if (specification && !version && !query &&
1395                         is_service_wfd(specification, specification_length)) {
1396                 ret = peer_unregister_wfd_service();
1397                 if (ret != 0 && ret != -EINPROGRESS)
1398                         return ret;
1399                 wfd = true;
1400         }
1401
1402         for (list = iface_list; list; list = list->next) {
1403                 struct wifi_data *wifi = list->data;
1404                 GSupplicantInterface *iface = wifi->interface;
1405
1406                 if (wfd)
1407                         goto stop_listening;
1408
1409                 if (!g_supplicant_interface_has_p2p(iface))
1410                         continue;
1411
1412                 params = fill_in_peer_service_params(specification,
1413                                                 specification_length, query,
1414                                                 query_length, version);
1415                 if (!params)
1416                         continue;
1417
1418                 ret = g_supplicant_interface_p2p_del_service(iface, params);
1419                 if (ret != 0 && ret != -EINPROGRESS)
1420                         free_peer_service_params(params);
1421 stop_listening:
1422                 wifi->servicing--;
1423                 if (!wifi->servicing || wifi->servicing < 0) {
1424                         g_supplicant_interface_p2p_listen(iface, 0, 0);
1425                         wifi->servicing = 0;
1426                 }
1427         }
1428
1429         return 0;
1430 }
1431
1432 static struct connman_peer_driver peer_driver = {
1433         .connect    = peer_connect,
1434         .disconnect = peer_disconnect,
1435         .register_service = peer_register_service,
1436         .unregister_service = peer_unregister_service,
1437 };
1438
1439 static void handle_tethering(struct wifi_data *wifi)
1440 {
1441         if (!wifi->tethering)
1442                 return;
1443
1444         if (!wifi->bridge)
1445                 return;
1446
1447         if (wifi->bridged)
1448                 return;
1449
1450         DBG("index %d bridge %s", wifi->index, wifi->bridge);
1451
1452         if (connman_inet_add_to_bridge(wifi->index, wifi->bridge) < 0)
1453                 return;
1454
1455         wifi->bridged = true;
1456 }
1457
1458 static void wifi_newlink(unsigned flags, unsigned change, void *user_data)
1459 {
1460         struct connman_device *device = user_data;
1461         struct wifi_data *wifi = connman_device_get_data(device);
1462
1463         if (!wifi)
1464                 return;
1465
1466         DBG("index %d flags %d change %d", wifi->index, flags, change);
1467
1468         if ((wifi->flags & IFF_UP) != (flags & IFF_UP)) {
1469                 if (flags & IFF_UP)
1470                         DBG("interface up");
1471                 else
1472                         DBG("interface down");
1473         }
1474
1475         if ((wifi->flags & IFF_LOWER_UP) != (flags & IFF_LOWER_UP)) {
1476                 if (flags & IFF_LOWER_UP)
1477                         DBG("carrier on");
1478                 else
1479                         DBG("carrier off");
1480         }
1481
1482         if (flags & IFF_LOWER_UP)
1483                 handle_tethering(wifi);
1484
1485         wifi->flags = flags;
1486 }
1487
1488 static int wifi_probe(struct connman_device *device)
1489 {
1490         struct wifi_data *wifi;
1491
1492         DBG("device %p", device);
1493
1494         wifi = g_try_new0(struct wifi_data, 1);
1495         if (!wifi)
1496                 return -ENOMEM;
1497
1498         wifi->state = G_SUPPLICANT_STATE_INACTIVE;
1499         wifi->ap_supported = WIFI_AP_UNKNOWN;
1500         wifi->tethering_param = NULL;
1501
1502         connman_device_set_data(device, wifi);
1503         wifi->device = connman_device_ref(device);
1504
1505         wifi->index = connman_device_get_index(device);
1506         wifi->flags = 0;
1507
1508         wifi->watch = connman_rtnl_add_newlink_watch(wifi->index,
1509                                                         wifi_newlink, device);
1510         if (is_p2p_connecting())
1511                 add_pending_wifi_device(wifi);
1512         else
1513                 iface_list = g_list_append(iface_list, wifi);
1514
1515         return 0;
1516 }
1517
1518 static void remove_networks(struct connman_device *device,
1519                                 struct wifi_data *wifi)
1520 {
1521         GSList *list;
1522
1523         for (list = wifi->networks; list; list = list->next) {
1524                 struct connman_network *network = list->data;
1525
1526                 connman_device_remove_network(device, network);
1527                 connman_network_unref(network);
1528         }
1529
1530         g_slist_free(wifi->networks);
1531         wifi->networks = NULL;
1532 }
1533
1534 static void remove_peers(struct wifi_data *wifi)
1535 {
1536         GSList *list;
1537
1538         for (list = wifi->peers; list; list = list->next) {
1539                 struct connman_peer *peer = list->data;
1540
1541                 connman_peer_unregister(peer);
1542                 connman_peer_unref(peer);
1543         }
1544
1545         g_slist_free(wifi->peers);
1546         wifi->peers = NULL;
1547 }
1548
1549 static void reset_autoscan(struct connman_device *device)
1550 {
1551         struct wifi_data *wifi = connman_device_get_data(device);
1552         struct autoscan_params *autoscan;
1553
1554         DBG("");
1555
1556         if (!wifi || !wifi->autoscan)
1557                 return;
1558
1559         autoscan = wifi->autoscan;
1560
1561         autoscan->interval = 0;
1562
1563         if (autoscan->timeout == 0)
1564                 return;
1565
1566         g_source_remove(autoscan->timeout);
1567         autoscan->timeout = 0;
1568
1569         connman_device_unref(device);
1570 }
1571
1572 static void stop_autoscan(struct connman_device *device)
1573 {
1574         const struct wifi_data *wifi = connman_device_get_data(device);
1575
1576         if (!wifi || !wifi->autoscan)
1577                 return;
1578
1579         reset_autoscan(device);
1580
1581         connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_WIFI, false);
1582 }
1583
1584 static void check_p2p_technology(void)
1585 {
1586         bool p2p_exists = false;
1587         GList *list;
1588
1589         for (list = iface_list; list; list = list->next) {
1590                 struct wifi_data *w = list->data;
1591
1592                 if (w->interface &&
1593                                 g_supplicant_interface_has_p2p(w->interface))
1594                         p2p_exists = true;
1595         }
1596
1597         if (!p2p_exists) {
1598                 connman_technology_driver_unregister(&p2p_tech_driver);
1599                 connman_peer_driver_unregister(&peer_driver);
1600         }
1601 }
1602
1603 struct last_connected {
1604         GTimeVal modified;
1605         gchar *ssid;
1606         int freq;
1607 };
1608
1609 static gint sort_entry(gconstpointer a, gconstpointer b, gpointer user_data)
1610 {
1611         GTimeVal *aval = (GTimeVal *)a;
1612         GTimeVal *bval = (GTimeVal *)b;
1613
1614         /* Note that the sort order is descending */
1615         if (aval->tv_sec < bval->tv_sec)
1616                 return 1;
1617
1618         if (aval->tv_sec > bval->tv_sec)
1619                 return -1;
1620
1621         return 0;
1622 }
1623
1624 static void free_entry(gpointer data)
1625 {
1626         struct last_connected *entry = data;
1627
1628         g_free(entry->ssid);
1629         g_free(entry);
1630 }
1631
1632 static void wifi_remove(struct connman_device *device)
1633 {
1634         struct wifi_data *wifi = connman_device_get_data(device);
1635
1636         DBG("device %p wifi %p", device, wifi);
1637
1638         if (!wifi)
1639                 return;
1640
1641         stop_autoscan(device);
1642
1643         if (wifi->p2p_device)
1644                 p2p_iface_list = g_list_remove(p2p_iface_list, wifi);
1645         else
1646                 iface_list = g_list_remove(iface_list, wifi);
1647
1648         check_p2p_technology();
1649 #if defined TIZEN_EXT_WIFI_MESH
1650         check_mesh_technology();
1651 #endif
1652
1653         remove_pending_wifi_device(wifi);
1654
1655         if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_P2P)) {
1656                 g_source_remove(wifi->p2p_find_timeout);
1657                 connman_device_unref(wifi->device);
1658         }
1659
1660         if (wifi->p2p_connection_timeout)
1661                 g_source_remove(wifi->p2p_connection_timeout);
1662
1663 #if defined TIZEN_EXT
1664         if (wifi->automaxspeed_timeout != 0) {
1665                 g_source_remove(wifi->automaxspeed_timeout);
1666                 wifi->automaxspeed_timeout = 0;
1667         }
1668 #endif
1669
1670         remove_networks(device, wifi);
1671         remove_peers(wifi);
1672
1673         connman_device_set_powered(device, false);
1674         connman_device_set_data(device, NULL);
1675         connman_device_unref(wifi->device);
1676         connman_rtnl_remove_watch(wifi->watch);
1677
1678         g_supplicant_interface_set_data(wifi->interface, NULL);
1679
1680         g_supplicant_interface_cancel(wifi->interface);
1681
1682         if (wifi->scan_params)
1683                 g_supplicant_free_scan_params(wifi->scan_params);
1684 #if defined TIZEN_EXT
1685         if (wifi->hidden_scan_params) {
1686                 while (wifi->hidden_scan_params->ssids) {
1687                         struct scan_ssid *ssid;
1688                         ssid = wifi->hidden_scan_params->ssids->data;
1689                         wifi->hidden_scan_params->ssids = g_slist_remove(wifi->hidden_scan_params->ssids, ssid);
1690                 }
1691                 g_supplicant_free_scan_params(wifi->hidden_scan_params);
1692         }
1693 #endif
1694
1695         g_free(wifi->autoscan);
1696         g_free(wifi->identifier);
1697         g_free(wifi);
1698 }
1699
1700 static bool is_duplicate(GSList *list, gchar *ssid, int ssid_len)
1701 {
1702         GSList *iter;
1703
1704         for (iter = list; iter; iter = g_slist_next(iter)) {
1705                 struct scan_ssid *scan_ssid = iter->data;
1706
1707                 if (ssid_len == scan_ssid->ssid_len &&
1708                                 memcmp(ssid, scan_ssid->ssid, ssid_len) == 0)
1709                         return true;
1710         }
1711
1712         return false;
1713 }
1714
1715 static int add_scan_param(gchar *hex_ssid, char *raw_ssid, int ssid_len,
1716                         int freq, GSupplicantScanParams *scan_data,
1717                         int driver_max_scan_ssids, char *ssid_name)
1718 {
1719         unsigned int i;
1720         struct scan_ssid *scan_ssid;
1721
1722         if ((driver_max_scan_ssids == 0 ||
1723                         driver_max_scan_ssids > scan_data->num_ssids) &&
1724                         (hex_ssid || raw_ssid)) {
1725                 gchar *ssid;
1726                 unsigned int j = 0, hex;
1727
1728                 if (hex_ssid) {
1729                         size_t hex_ssid_len = strlen(hex_ssid);
1730
1731                         ssid = g_try_malloc0(hex_ssid_len / 2);
1732                         if (!ssid)
1733                                 return -ENOMEM;
1734
1735                         for (i = 0; i < hex_ssid_len; i += 2) {
1736                                 sscanf(hex_ssid + i, "%02x", &hex);
1737                                 ssid[j++] = hex;
1738                         }
1739                 } else {
1740                         ssid = raw_ssid;
1741                         j = ssid_len;
1742                 }
1743
1744                 /*
1745                  * If we have already added hidden AP to the list,
1746                  * then do not do it again. This might happen if you have
1747                  * used or are using multiple wifi cards, so in that case
1748                  * you might have multiple service files for same AP.
1749                  */
1750                 if (is_duplicate(scan_data->ssids, ssid, j)) {
1751                         if (hex_ssid)
1752                                 g_free(ssid);
1753                         return 0;
1754                 }
1755
1756                 scan_ssid = g_try_new(struct scan_ssid, 1);
1757                 if (!scan_ssid) {
1758                         if (hex_ssid)
1759                                 g_free(ssid);
1760                         return -ENOMEM;
1761                 }
1762
1763                 memcpy(scan_ssid->ssid, ssid, j);
1764                 scan_ssid->ssid_len = j;
1765                 scan_data->ssids = g_slist_prepend(scan_data->ssids,
1766                                                                 scan_ssid);
1767
1768                 scan_data->num_ssids++;
1769
1770                 DBG("SSID %s added to scanned list of %d entries", ssid_name,
1771                                                         scan_data->num_ssids);
1772
1773                 if (hex_ssid)
1774                         g_free(ssid);
1775         } else
1776                 return -EINVAL;
1777
1778         scan_data->ssids = g_slist_reverse(scan_data->ssids);
1779
1780         if (!scan_data->freqs) {
1781                 scan_data->freqs = g_try_malloc0(sizeof(uint16_t));
1782                 if (!scan_data->freqs) {
1783                         g_slist_free_full(scan_data->ssids, g_free);
1784                         return -ENOMEM;
1785                 }
1786
1787                 scan_data->num_freqs = 1;
1788                 scan_data->freqs[0] = freq;
1789         } else {
1790                 bool duplicate = false;
1791
1792                 /* Don't add duplicate entries */
1793                 for (i = 0; i < scan_data->num_freqs; i++) {
1794                         if (scan_data->freqs[i] == freq) {
1795                                 duplicate = true;
1796                                 break;
1797                         }
1798                 }
1799
1800                 if (!duplicate) {
1801                         scan_data->num_freqs++;
1802                         scan_data->freqs = g_try_realloc(scan_data->freqs,
1803                                 sizeof(uint16_t) * scan_data->num_freqs);
1804                         if (!scan_data->freqs) {
1805                                 g_slist_free_full(scan_data->ssids, g_free);
1806                                 return -ENOMEM;
1807                         }
1808                         scan_data->freqs[scan_data->num_freqs - 1] = freq;
1809                 }
1810         }
1811
1812         return 1;
1813 }
1814
1815 static int get_hidden_connections(GSupplicantScanParams *scan_data)
1816 {
1817         struct connman_config_entry **entries;
1818         GKeyFile *keyfile;
1819 #if defined TIZEN_EXT
1820         gchar **services = NULL;
1821 #else
1822         gchar **services;
1823 #endif /* defined TIZEN_EXT */
1824         char *ssid, *name;
1825         int i, ret;
1826         bool value;
1827         int num_ssids = 0, add_param_failed = 0;
1828 #if defined TIZEN_EXT
1829         GSequenceIter *iter;
1830         GSequence *latest_list;
1831         struct last_connected *entry;
1832         GTimeVal modified;
1833
1834         latest_list = g_sequence_new(free_entry);
1835         if (!latest_list)
1836                 goto out;
1837 #endif
1838         services = connman_storage_get_services();
1839         for (i = 0; services && services[i]; i++) {
1840                 if (strncmp(services[i], "wifi_", 5) != 0)
1841                         continue;
1842
1843                 keyfile = connman_storage_load_service(services[i]);
1844                 if (!keyfile)
1845                         continue;
1846
1847                 value = g_key_file_get_boolean(keyfile,
1848                                         services[i], "Hidden", NULL);
1849                 if (!value) {
1850                         g_key_file_free(keyfile);
1851                         continue;
1852                 }
1853
1854                 value = g_key_file_get_boolean(keyfile,
1855                                         services[i], "Favorite", NULL);
1856                 if (!value) {
1857                         g_key_file_free(keyfile);
1858                         continue;
1859                 }
1860
1861 #if defined TIZEN_EXT
1862                 value = g_key_file_get_boolean(keyfile,
1863                                         services[i], "AutoConnect", NULL);
1864                 if (!value) {
1865                         g_key_file_free(keyfile);
1866                         continue;
1867                 }
1868
1869                 gchar *str = g_key_file_get_string(keyfile,
1870                                         services[i], "Modified", NULL);
1871                 if (!str) {
1872                         g_key_file_free(keyfile);
1873                         continue;
1874                 }
1875                 g_time_val_from_iso8601(str, &modified);
1876                 g_free(str);
1877 #endif
1878
1879                 ssid = g_key_file_get_string(keyfile,
1880                                         services[i], "SSID", NULL);
1881
1882                 name = g_key_file_get_string(keyfile, services[i], "Name",
1883                                                                 NULL);
1884
1885 #if defined TIZEN_EXT
1886                 entry = g_try_new(struct last_connected, 1);
1887                 if (!entry) {
1888                         g_sequence_free(latest_list);
1889                         g_free(ssid);
1890                         g_free(name);
1891                         g_key_file_free(keyfile);
1892                         goto out;
1893                 }
1894
1895                 entry->modified = modified;
1896                 entry->ssid = ssid;
1897
1898                 g_sequence_insert_sorted(latest_list, entry,
1899                                 sort_entry, NULL);
1900 #else
1901                 ret = add_scan_param(ssid, NULL, 0, 0, scan_data, 0, name);
1902                 if (ret < 0)
1903                         add_param_failed++;
1904                 else if (ret > 0)
1905                         num_ssids++;
1906
1907                 g_free(ssid);
1908 #endif
1909                 g_free(name);
1910                 g_key_file_free(keyfile);
1911         }
1912
1913 #if defined TIZEN_EXT
1914         gint length = g_sequence_get_length(latest_list);
1915         iter = g_sequence_get_begin_iter(latest_list);
1916
1917         for (i = 0; i < length; i++) {
1918                 entry = g_sequence_get(iter);
1919
1920                 ret = add_scan_param(entry->ssid, NULL, 0, 0, scan_data, 0, entry->ssid);
1921                 if (ret < 0)
1922                         add_param_failed++;
1923                 else if (ret > 0)
1924                         num_ssids++;
1925
1926                 iter = g_sequence_iter_next(iter);
1927         }
1928
1929         g_sequence_free(latest_list);
1930 out:
1931 #endif
1932         /*
1933          * Check if there are any hidden AP that needs to be provisioned.
1934          */
1935         entries = connman_config_get_entries("wifi");
1936         for (i = 0; entries && entries[i]; i++) {
1937                 int len;
1938
1939                 if (!entries[i]->hidden)
1940                         continue;
1941
1942                 if (!entries[i]->ssid) {
1943                         ssid = entries[i]->name;
1944                         len = strlen(ssid);
1945                 } else {
1946                         ssid = entries[i]->ssid;
1947                         len = entries[i]->ssid_len;
1948                 }
1949
1950                 if (!ssid)
1951                         continue;
1952
1953                 ret = add_scan_param(NULL, ssid, len, 0, scan_data, 0, ssid);
1954                 if (ret < 0)
1955                         add_param_failed++;
1956                 else if (ret > 0)
1957                         num_ssids++;
1958         }
1959
1960         connman_config_free_entries(entries);
1961
1962         if (add_param_failed > 0)
1963                 DBG("Unable to scan %d out of %d SSIDs",
1964                                         add_param_failed, num_ssids);
1965
1966         g_strfreev(services);
1967
1968         return num_ssids;
1969 }
1970
1971 static int get_hidden_connections_params(struct wifi_data *wifi,
1972                                         GSupplicantScanParams *scan_params)
1973 {
1974         int driver_max_ssids, i;
1975         GSupplicantScanParams *orig_params;
1976
1977         /*
1978          * Scan hidden networks so that we can autoconnect to them.
1979          * We will assume 1 as a default number of ssid to scan.
1980          */
1981         driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
1982                                                         wifi->interface);
1983         if (driver_max_ssids == 0)
1984                 driver_max_ssids = 1;
1985
1986         DBG("max ssids %d", driver_max_ssids);
1987
1988 #if defined TIZEN_EXT
1989         if (!wifi->hidden_scan_params) {
1990                 wifi->hidden_scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
1991                 if (!wifi->hidden_scan_params)
1992                         return 0;
1993
1994                 if (get_hidden_connections(wifi->hidden_scan_params) == 0) {
1995                         g_supplicant_free_scan_params(wifi->hidden_scan_params);
1996                         wifi->hidden_scan_params = NULL;
1997
1998                         return 0;
1999                 }
2000         }
2001
2002         orig_params = wifi->hidden_scan_params;
2003 #else
2004         if (!wifi->scan_params) {
2005                 wifi->scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
2006                 if (!wifi->scan_params)
2007                         return 0;
2008
2009                 if (get_hidden_connections(wifi->scan_params) == 0) {
2010                         g_supplicant_free_scan_params(wifi->scan_params);
2011                         wifi->scan_params = NULL;
2012
2013                         return 0;
2014                 }
2015         }
2016
2017         orig_params = wifi->scan_params;
2018 #endif
2019
2020         /* Let's transfer driver_max_ssids params */
2021         for (i = 0; i < driver_max_ssids; i++) {
2022                 struct scan_ssid *ssid;
2023
2024 #if defined TIZEN_EXT
2025                 if (!wifi->hidden_scan_params->ssids)
2026 #else
2027                 if (!wifi->scan_params->ssids)
2028 #endif
2029                         break;
2030
2031                 ssid = orig_params->ssids->data;
2032                 orig_params->ssids = g_slist_remove(orig_params->ssids, ssid);
2033                 scan_params->ssids = g_slist_prepend(scan_params->ssids, ssid);
2034         }
2035
2036         if (i > 0) {
2037                 scan_params->num_ssids = i;
2038                 scan_params->ssids = g_slist_reverse(scan_params->ssids);
2039
2040                 if (orig_params->num_freqs <= 0)
2041                         goto err;
2042
2043                 scan_params->freqs =
2044                         g_malloc(sizeof(uint16_t) * orig_params->num_freqs);
2045                 memcpy(scan_params->freqs, orig_params->freqs,
2046                         sizeof(uint16_t) *orig_params->num_freqs);
2047
2048                 scan_params->num_freqs = orig_params->num_freqs;
2049
2050         } else
2051                 goto err;
2052
2053         orig_params->num_ssids -= scan_params->num_ssids;
2054
2055         return scan_params->num_ssids;
2056
2057 err:
2058         g_slist_free_full(scan_params->ssids, g_free);
2059 #if defined TIZEN_EXT
2060         g_supplicant_free_scan_params(wifi->hidden_scan_params);
2061         wifi->hidden_scan_params = NULL;
2062 #else
2063         g_supplicant_free_scan_params(wifi->scan_params);
2064         wifi->scan_params = NULL;
2065 #endif
2066
2067         return 0;
2068 }
2069
2070 static int throw_wifi_scan(struct connman_device *device,
2071                         GSupplicantInterfaceCallback callback)
2072 {
2073         struct wifi_data *wifi = connman_device_get_data(device);
2074         int ret;
2075
2076         if (!wifi)
2077                 return -ENODEV;
2078
2079         DBG("device %p %p", device, wifi->interface);
2080
2081         if (wifi->tethering)
2082                 return -EBUSY;
2083
2084 #if defined TIZEN_EXT
2085         if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI)
2086             && !wifi->allow_full_scan)
2087 #else
2088         if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI))
2089 #endif
2090                 return -EALREADY;
2091
2092         connman_device_ref(device);
2093
2094         ret = g_supplicant_interface_scan(wifi->interface, NULL,
2095                                                 callback, device);
2096         if (ret == 0) {
2097                 connman_device_set_scanning(device,
2098                                 CONNMAN_SERVICE_TYPE_WIFI, true);
2099         } else
2100                 connman_device_unref(device);
2101
2102         return ret;
2103 }
2104
2105 static void hidden_free(struct hidden_params *hidden)
2106 {
2107         if (!hidden)
2108                 return;
2109
2110         if (hidden->scan_params)
2111                 g_supplicant_free_scan_params(hidden->scan_params);
2112         g_free(hidden->identity);
2113         g_free(hidden->passphrase);
2114         g_free(hidden->security);
2115         g_free(hidden);
2116 }
2117
2118 #if defined TIZEN_EXT
2119 static void service_state_changed(struct connman_service *service,
2120                                         enum connman_service_state state);
2121
2122 static int network_connect(struct connman_network *network);
2123
2124 static struct connman_notifier notifier = {
2125         .name                   = "wifi",
2126         .priority               = CONNMAN_NOTIFIER_PRIORITY_DEFAULT,
2127         .service_state_changed  = service_state_changed,
2128 };
2129
2130 static void service_state_changed(struct connman_service *service,
2131                                         enum connman_service_state state)
2132 {
2133         enum connman_service_type type;
2134
2135         type = connman_service_get_type(service);
2136         if (type != CONNMAN_SERVICE_TYPE_WIFI)
2137                 return;
2138
2139         DBG("service %p state %d", service, state);
2140
2141         switch (state) {
2142         case CONNMAN_SERVICE_STATE_READY:
2143         case CONNMAN_SERVICE_STATE_ONLINE:
2144         case CONNMAN_SERVICE_STATE_FAILURE:
2145                 connman_notifier_unregister(&notifier);
2146                 is_wifi_notifier_registered = FALSE;
2147
2148                 __connman_device_request_scan(type);
2149                 break;
2150
2151         default:
2152                 break;
2153         }
2154 }
2155
2156 static gboolean need_bss_transition(uint16_t freq, int snr, int strength)
2157 {
2158         /*
2159          * Since bssid->strength is a positive value,
2160          * it need to be changed to its original value.
2161          */
2162         int signal = strength - 120;
2163
2164         /*
2165          * If the currently connected AP matches the following conditions,
2166          * scan for BSS transition is started.
2167          *   - SNR is less than min_snr or RSSI is less than
2168          *     min_rssi_2_4GHz, min_rssi_5GHz or min_rssi_6GHz.
2169          */
2170         if (min_snr != 0 && snr != 0 && snr < min_snr)
2171                 return TRUE;
2172         else if (freq > FREQUENCY_6G_BASE && signal <= min_rssi_6GHz)
2173                 return TRUE;
2174         else if (freq > FREQUENCY_5G_BASE && signal <= min_rssi_5GHz)
2175                 return TRUE;
2176         else if (signal <= min_rssi_2_4GHz)
2177                 return TRUE;
2178
2179         return FALSE;
2180 }
2181
2182 static gboolean check_bss_diff(int cur_level, int sel_level, int to_5ghz,
2183                 unsigned int cur_est, unsigned int sel_est)
2184 {
2185         int min_diff;
2186         int diff;
2187
2188         /* This code is from wpa_supplicant. */
2189         if (cur_level < -85) /* ..-86 dBm */
2190                 min_diff = 1;
2191         else if (cur_level < -80) /* -85..-81 dBm */
2192                 min_diff = 2;
2193         else if (cur_level < -75) /* -80..-76 dBm */
2194                 min_diff = 3;
2195         else if (cur_level < -70) /* -75..-71 dBm */
2196                 min_diff = 4;
2197         else if (cur_level < 0) /* -70..-1 dBm */
2198                 min_diff = 5;
2199         else /* unspecified units (not in dBm) */
2200                 min_diff = 2;
2201
2202         if (cur_est > sel_est * 1.5)
2203                 min_diff += 10;
2204         else if (cur_est > sel_est * 1.2)
2205                 min_diff += 5;
2206         else if (cur_est > sel_est * 1.1)
2207                 min_diff += 2;
2208         else if (cur_est > sel_est)
2209                 min_diff++;
2210         else if (sel_est > cur_est * 1.5)
2211                 min_diff -= 10;
2212         else if (sel_est > cur_est * 1.2)
2213                 min_diff -= 5;
2214         else if (sel_est > cur_est * 1.1)
2215                 min_diff -= 2;
2216         else if (sel_est > cur_est)
2217                 min_diff--;
2218
2219         if (to_5ghz)
2220                 min_diff -= 2;
2221
2222         diff = sel_level - cur_level;
2223
2224         if (diff < min_diff)
2225                 return FALSE;
2226
2227         return TRUE;
2228 }
2229
2230 static gboolean check_bss_condition(uint16_t freq, uint16_t strength,
2231                 unsigned int est_throughput, uint16_t cur_freq, uint16_t cur_strength,
2232                 unsigned int cur_est_throughput)
2233 {
2234         /*
2235          * Since bssid->strength is a positive value,
2236          * it need to be changed to its original value.
2237          */
2238         int signal = strength - 120;
2239         int cur_signal = cur_strength - 120;
2240         int to_5ghz = freq > 4000 && cur_freq < 4000;
2241
2242         DBG("cur_freq=%d cur_level=%d cur_est=%d sel_freq=%d sel_level=%d sel_est=%d",
2243                         cur_freq, cur_strength, cur_est_throughput,
2244                         freq, strength, est_throughput);
2245
2246         /*
2247          * If the AP that matches the following conditions exists in the SCAN result,
2248          * BSS transition is started.
2249          */
2250
2251         if (est_throughput > cur_est_throughput + 5000)
2252                 return TRUE;
2253
2254         if (cur_signal > signal + to_5ghz * 2 &&
2255                         est_throughput < cur_est_throughput * 1.2)
2256                 return FALSE;
2257
2258         if (cur_est_throughput > est_throughput + 5000)
2259                 return FALSE;
2260
2261         return check_bss_diff(cur_signal, signal,
2262                         to_5ghz, cur_est_throughput, est_throughput);
2263 }
2264
2265 static void scan_callback_hidden(int result,
2266                         GSupplicantInterface *interface, void *user_data);
2267
2268 static int network_disconnect(struct connman_network *network);
2269
2270 static void start_roaming(struct wifi_data *wifi)
2271 {
2272         bool roaming_ap_found = false;
2273         GSList *bssid_list = NULL;
2274
2275         if (!wifi || !wifi->network)
2276                 return;
2277
2278         if (!connman_setting_get_bool("WifiRoaming"))
2279                 return;
2280
2281         struct connman_network *network = wifi->network;
2282         bssid_list = connman_network_get_bssid_list(network);
2283
2284         if (g_slist_length(bssid_list) <= 1)
2285                 return;
2286
2287         if (!connman_network_get_connected(network))
2288                 return;
2289
2290         if (connman_network_get_bool(network, "WiFi.Roaming"))
2291                 return;
2292
2293         uint16_t cur_freq = connman_network_get_frequency(network);
2294         uint8_t cur_strength = connman_network_get_strength(network);
2295         unsigned int cur_est_throughput = connman_network_get_est_throughput(network);
2296
2297         if (!need_bss_transition(
2298                         cur_freq,
2299                         connman_network_get_snr(network),
2300                         cur_strength))
2301                 return;
2302
2303         unsigned char *cur_bssid = connman_network_get_bssid(network);
2304
2305         for (; bssid_list; bssid_list = bssid_list->next) {
2306                 struct g_connman_bssids *bssid = bssid_list->data;
2307
2308                 if (memcmp(cur_bssid, bssid->bssid, WIFI_BSSID_LEN_MAX) == 0)
2309                         continue;
2310
2311                 if (check_bss_condition(
2312                         bssid->frequency, bssid->strength, bssid->est_throughput,
2313                                 cur_freq, cur_strength, cur_est_throughput)) {
2314                         roaming_ap_found = true;
2315
2316                         char bssid_buff[WIFI_BSSID_STR_LEN] = {0,};
2317                         char *bssid_str = bssid_buff;
2318
2319                         snprintf(bssid_str,     WIFI_BSSID_STR_LEN, MACSTR, MAC2STR(bssid->bssid));
2320                         connman_network_set_string(network,
2321                                         "WiFi.RoamingDstBSSID", bssid_str);
2322                         break;
2323                 }
2324         }
2325
2326         if (roaming_ap_found) {
2327                 char bssid_buff[WIFI_BSSID_STR_LEN] = {0,};
2328                 char *bssid_str = bssid_buff;
2329                 unsigned char *bssid;
2330
2331                 bssid = connman_network_get_bssid(network);
2332                 snprintf(bssid_str, WIFI_BSSID_STR_LEN, MACSTR, MAC2STR(bssid));
2333                 connman_network_set_string(network,
2334                                 "WiFi.RoamingCurBSSID", bssid_str);
2335
2336                 network_disconnect(network);
2337                 wifi->pending_network = network;
2338                 connman_network_set_bool(network, "WiFi.Roaming", true);
2339         }
2340 }
2341 #endif
2342
2343 static void scan_callback(int result, GSupplicantInterface *interface,
2344                                                 void *user_data)
2345 {
2346         struct connman_device *device = user_data;
2347         struct wifi_data *wifi = connman_device_get_data(device);
2348         bool scanning;
2349 #if defined TIZEN_EXT
2350         GSList *list = NULL;
2351         bool favorite_exists = false;
2352         struct connman_network *network = NULL;
2353         struct connman_service *service = NULL;
2354 #endif
2355
2356         DBG("result %d wifi %p", result, wifi);
2357
2358         if (wifi) {
2359                 if (wifi->hidden && !wifi->postpone_hidden) {
2360                         connman_network_clear_hidden(wifi->hidden->user_data);
2361                         hidden_free(wifi->hidden);
2362                         wifi->hidden = NULL;
2363                 }
2364
2365                 if (wifi->scan_params) {
2366                         g_supplicant_free_scan_params(wifi->scan_params);
2367                         wifi->scan_params = NULL;
2368                 }
2369
2370 #if defined TIZEN_EXT
2371                 if (wifi->hidden_scan_params && !wifi->hidden_scan_params->ssids) {
2372                         g_supplicant_free_scan_params(wifi->hidden_scan_params);
2373                         wifi->hidden_scan_params = NULL;
2374                 }
2375 #endif
2376         }
2377
2378         if (result < 0)
2379                 connman_device_reset_scanning(device);
2380
2381         /* User is connecting to a hidden AP, let's wait for finished event */
2382         if (wifi && wifi->hidden && wifi->postpone_hidden) {
2383                 GSupplicantScanParams *scan_params;
2384                 int ret;
2385
2386                 wifi->postpone_hidden = false;
2387                 scan_params = wifi->hidden->scan_params;
2388                 wifi->hidden->scan_params = NULL;
2389
2390                 reset_autoscan(device);
2391
2392                 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
2393                                                         scan_callback, device);
2394                 if (ret == 0)
2395                         return;
2396
2397                 /* On error, let's recall scan_callback, which will cleanup */
2398                 return scan_callback(ret, interface, user_data);
2399         }
2400
2401 #if defined TIZEN_EXT
2402         if (wifi) {
2403                 for (list = wifi->networks; list; list = list->next) {
2404                         network = list->data;
2405                         service = connman_service_lookup_from_network(network);
2406
2407                         if (service != NULL &&
2408                                 (connman_service_get_favorite(service) == true) &&
2409                                 (connman_service_get_autoconnect(service) == true)) {
2410                                 DBG("Favorite service exists [%s]",
2411                                                 connman_network_get_string(network, "Name"));
2412                                 favorite_exists = true;
2413                                 break;
2414                         }
2415                 }
2416         }
2417
2418         if (favorite_exists == false) {
2419                 if (wifi && wifi->allow_full_scan) {
2420                         int ret;
2421                         DBG("Trigger full channel scan");
2422                         wifi->allow_full_scan = false;
2423
2424                         ret = g_supplicant_interface_scan(wifi->interface, NULL,
2425                                                                 scan_callback_hidden, device);
2426                         if (ret == 0)
2427                                 return;
2428
2429                         /* On error, let's recall scan_callback, which will cleanup */
2430                         return scan_callback(ret, interface, user_data);
2431                 }
2432         } else
2433                 wifi->allow_full_scan = false;
2434 #endif
2435
2436         scanning = connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI);
2437
2438         if (scanning) {
2439                 connman_device_set_scanning(device,
2440                                 CONNMAN_SERVICE_TYPE_WIFI, false);
2441         }
2442
2443         if (result != -ENOLINK)
2444 #if defined TIZEN_EXT
2445         if (result != -EIO)
2446 #endif
2447                 start_autoscan(device);
2448
2449         /*
2450          * If we are here then we were scanning; however, if we are
2451          * also mid-flight disabling the interface, then wifi_disable
2452          * has already cleared the device scanning state and
2453          * unreferenced the device, obviating the need to do it here.
2454          */
2455
2456         if (scanning)
2457                 connman_device_unref(device);
2458
2459 #if defined TIZEN_EXT
2460         if (!wifi)
2461                 goto done;
2462
2463         if (wifi->scan_pending_network && result != -EIO) {
2464                 network_connect(wifi->scan_pending_network);
2465                 wifi->scan_pending_network = NULL;
2466                 connman_network_set_connecting(wifi->network);
2467         } else {
2468                 start_roaming(wifi);
2469         }
2470
2471 done:
2472         if (is_wifi_notifier_registered != true &&
2473                         wifi_first_scan == true && found_with_first_scan == true) {
2474                 wifi_first_scan = false;
2475                 found_with_first_scan = false;
2476
2477                 connman_notifier_register(&notifier);
2478                 is_wifi_notifier_registered = true;
2479         }
2480 #endif
2481 }
2482
2483 static void scan_callback_hidden(int result,
2484                         GSupplicantInterface *interface, void *user_data)
2485 {
2486         struct connman_device *device = user_data;
2487         struct wifi_data *wifi = connman_device_get_data(device);
2488         GSupplicantScanParams *scan_params;
2489         int ret;
2490
2491         DBG("result %d wifi %p", result, wifi);
2492
2493         if (!wifi)
2494                 goto out;
2495
2496         /* User is trying to connect to a hidden AP */
2497         if (wifi->hidden && wifi->postpone_hidden)
2498                 goto out;
2499
2500         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
2501         if (!scan_params)
2502                 goto out;
2503
2504         if (get_hidden_connections_params(wifi, scan_params) > 0) {
2505                 ret = g_supplicant_interface_scan(wifi->interface,
2506                                                         scan_params,
2507 #if defined TIZEN_EXT
2508                                                         scan_callback,
2509 #else
2510                                                         scan_callback_hidden,
2511 #endif
2512                                                         device);
2513                 if (ret == 0)
2514                         return;
2515         }
2516
2517         g_supplicant_free_scan_params(scan_params);
2518
2519 out:
2520         scan_callback(result, interface, user_data);
2521 }
2522
2523 static gboolean autoscan_timeout(gpointer data)
2524 {
2525         struct connman_device *device = data;
2526         struct wifi_data *wifi = connman_device_get_data(device);
2527         struct autoscan_params *autoscan;
2528         int interval;
2529
2530         if (!wifi)
2531                 return FALSE;
2532
2533         autoscan = wifi->autoscan;
2534
2535 #if defined TIZEN_EXT
2536         if (!autoscan)
2537                 return FALSE;
2538 #endif
2539
2540         if (autoscan->interval <= 0) {
2541                 interval = autoscan->base;
2542                 goto set_interval;
2543         } else
2544                 interval = autoscan->interval * autoscan->base;
2545
2546 #if defined TIZEN_EXT
2547         if (autoscan->interval >= autoscan->limit)
2548 #else
2549         if (interval > autoscan->limit)
2550 #endif
2551                 interval = autoscan->limit;
2552
2553         throw_wifi_scan(wifi->device, scan_callback_hidden);
2554
2555         /*
2556          * In case BackgroundScanning is disabled, interval will reach the
2557          * limit exactly after the very first passive scanning. It allows
2558          * to ensure at most one passive scan is performed in such cases.
2559          */
2560         if (!connman_setting_get_bool("BackgroundScanning") &&
2561                                         interval == autoscan->limit) {
2562                 g_source_remove(autoscan->timeout);
2563                 autoscan->timeout = 0;
2564
2565                 connman_device_unref(device);
2566
2567                 return FALSE;
2568         }
2569
2570 set_interval:
2571         DBG("interval %d", interval);
2572
2573         autoscan->interval = interval;
2574
2575         autoscan->timeout = g_timeout_add_seconds(interval,
2576                                                 autoscan_timeout, device);
2577
2578         return FALSE;
2579 }
2580
2581 static void start_autoscan(struct connman_device *device)
2582 {
2583         struct wifi_data *wifi = connman_device_get_data(device);
2584         struct autoscan_params *autoscan;
2585
2586         DBG("");
2587
2588         if (!wifi)
2589                 return;
2590
2591         if (wifi->p2p_device)
2592                 return;
2593
2594         if (wifi->connected)
2595                 return;
2596
2597         autoscan = wifi->autoscan;
2598         if (!autoscan)
2599                 return;
2600
2601         if (autoscan->timeout > 0 || autoscan->interval > 0)
2602                 return;
2603
2604         connman_device_ref(device);
2605
2606         autoscan_timeout(device);
2607 }
2608
2609 static struct autoscan_params *parse_autoscan_params(const char *params)
2610 {
2611         struct autoscan_params *autoscan;
2612         char **list_params;
2613         int limit;
2614         int base;
2615
2616         DBG("");
2617
2618         list_params = g_strsplit(params, ":", 0);
2619         if (list_params == 0)
2620                 return NULL;
2621
2622         if (!g_strcmp0(list_params[0], "exponential") &&
2623                                 g_strv_length(list_params) == 3) {
2624                 base = atoi(list_params[1]);
2625                 limit = atoi(list_params[2]);
2626         } else if (!g_strcmp0(list_params[0], "single") &&
2627                                 g_strv_length(list_params) == 2)
2628                 base = limit = atoi(list_params[1]);
2629         else {
2630                 g_strfreev(list_params);
2631                 return NULL;
2632         }
2633
2634         DBG("Setup %s autoscanning", list_params[0]);
2635
2636         g_strfreev(list_params);
2637
2638         autoscan = g_try_malloc0(sizeof(struct autoscan_params));
2639         if (!autoscan) {
2640                 DBG("Could not allocate memory for autoscan");
2641                 return NULL;
2642         }
2643
2644         DBG("base %d - limit %d", base, limit);
2645         autoscan->base = base;
2646         autoscan->limit = limit;
2647
2648         return autoscan;
2649 }
2650
2651 static void setup_autoscan(struct wifi_data *wifi)
2652 {
2653         /*
2654          * If BackgroundScanning is enabled, setup exponential
2655          * autoscanning if it has not been previously done.
2656          */
2657         if (connman_setting_get_bool("BackgroundScanning")) {
2658                 wifi->autoscan = parse_autoscan_params(AUTOSCAN_EXPONENTIAL);
2659                 return;
2660         }
2661 #if defined TIZEN_EXT
2662         else {
2663                 if (wifi->autoscan) {
2664                         g_free(wifi->autoscan);
2665                         wifi->autoscan = NULL;
2666                 }
2667
2668                 DBG("BackgroundScanning is disabled");
2669
2670                 return;
2671         }
2672 #else
2673
2674         /*
2675          * On the contrary, if BackgroundScanning is disabled, update autoscan
2676          * parameters based on the type of scanning that is being performed.
2677          */
2678         if (wifi->autoscan) {
2679                 g_free(wifi->autoscan);
2680                 wifi->autoscan = NULL;
2681         }
2682
2683         switch (wifi->scanning_type) {
2684         case WIFI_SCANNING_PASSIVE:
2685                 /* Do not setup autoscan. */
2686                 break;
2687         case WIFI_SCANNING_ACTIVE:
2688                 /* Setup one single passive scan after active. */
2689                 wifi->autoscan = parse_autoscan_params(AUTOSCAN_SINGLE);
2690                 break;
2691         case WIFI_SCANNING_UNKNOWN:
2692                 /* Setup autoscan in this case but we should never fall here. */
2693                 wifi->autoscan = parse_autoscan_params(AUTOSCAN_SINGLE);
2694                 break;
2695         }
2696 #endif
2697 }
2698
2699 static void finalize_interface_creation(struct wifi_data *wifi)
2700 {
2701         DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
2702
2703         if (!wifi->device) {
2704                 connman_error("WiFi device not set");
2705                 return;
2706         }
2707
2708         connman_device_set_powered(wifi->device, true);
2709
2710         if (wifi->p2p_device)
2711                 return;
2712
2713         if (!wifi->autoscan)
2714                 setup_autoscan(wifi);
2715
2716         start_autoscan(wifi->device);
2717 }
2718
2719 static void interface_create_callback(int result,
2720                                         GSupplicantInterface *interface,
2721                                                         void *user_data)
2722 {
2723         struct wifi_data *wifi = user_data;
2724         char *bgscan_range_max;
2725         long value;
2726
2727         DBG("result %d ifname %s, wifi %p", result,
2728                                 g_supplicant_interface_get_ifname(interface),
2729                                 wifi);
2730
2731         if (result < 0 || !wifi)
2732                 return;
2733
2734         wifi->interface = interface;
2735         g_supplicant_interface_set_data(interface, wifi);
2736 #ifdef TIZEN_EXT
2737         if (!interface)
2738                 return;
2739
2740         if (wifi->device &&
2741                         !connman_device_get_wifi_5ghz_supported(wifi->device) &&
2742                         !connman_device_get_wifi_6ghz_supported(wifi->device)) {
2743                 bool is_5_0_ghz_supported = g_supplicant_interface_get_is_5_0_ghz_supported(interface);
2744                 bool is_6_0_ghz_supported = g_supplicant_interface_get_is_6_0_ghz_supported(interface);
2745
2746                 connman_device_set_wifi_5ghz_supported(wifi->device, is_5_0_ghz_supported);
2747                 connman_device_set_wifi_6ghz_supported(wifi->device, is_6_0_ghz_supported);
2748         }
2749 #endif
2750         if (g_supplicant_interface_get_ready(interface)) {
2751                 wifi->interface_ready = true;
2752                 finalize_interface_creation(wifi);
2753         }
2754
2755         /*
2756          * Set the BSS expiration age to match the long scanning
2757          * interval to avoid the loss of unconnected networks between
2758          * two scans.
2759          */
2760         bgscan_range_max = strrchr(BGSCAN_DEFAULT, ':');
2761         if (!bgscan_range_max || strlen(bgscan_range_max) < 1)
2762                 return;
2763
2764         value = strtol(bgscan_range_max + 1, NULL, 10);
2765         if (value <= 0 || errno == ERANGE)
2766                 return;
2767
2768         if (g_supplicant_interface_set_bss_expiration_age(interface,
2769                                         value + SCAN_MAX_DURATION) < 0) {
2770                 connman_warn("Failed to set bss expiration age");
2771         }
2772 }
2773
2774 static int wifi_enable(struct connman_device *device)
2775 {
2776         struct wifi_data *wifi = connman_device_get_data(device);
2777         int index;
2778         char *interface;
2779         const char *driver = connman_setting_get_string("wifi");
2780         int ret;
2781
2782         DBG("device %p %p", device, wifi);
2783
2784         index = connman_device_get_index(device);
2785         if (!wifi || index < 0)
2786                 return -ENODEV;
2787
2788         if (is_p2p_connecting())
2789                 return -EINPROGRESS;
2790
2791         interface = connman_inet_ifname(index);
2792         ret = g_supplicant_interface_create(interface, driver, NULL,
2793 #ifdef TIZEN_EXT
2794                         connman_device_get_mac_policy(device),
2795                         connman_device_get_preassoc_mac_policy(device),
2796                         connman_device_get_random_mac_lifetime(device),
2797 #endif /* TIZEN_EXT */
2798                                                 interface_create_callback,
2799                                                         wifi);
2800         g_free(interface);
2801
2802         if (ret < 0)
2803                 return ret;
2804
2805         return -EINPROGRESS;
2806 }
2807
2808 static int wifi_disable(struct connman_device *device)
2809 {
2810         struct wifi_data *wifi = connman_device_get_data(device);
2811         int ret;
2812
2813         DBG("device %p wifi %p", device, wifi);
2814
2815         if (!wifi)
2816                 return -ENODEV;
2817
2818         wifi->connected = false;
2819         wifi->disconnecting = false;
2820
2821         if (wifi->pending_network)
2822                 wifi->pending_network = NULL;
2823
2824 #if !defined TIZEN_EXT
2825         stop_autoscan(device);
2826 #endif
2827
2828         if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_P2P)) {
2829                 g_source_remove(wifi->p2p_find_timeout);
2830                 wifi->p2p_find_timeout = 0;
2831                 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
2832                 connman_device_unref(wifi->device);
2833         }
2834
2835 #if defined TIZEN_EXT
2836         if (wifi->automaxspeed_timeout != 0) {
2837                 g_source_remove(wifi->automaxspeed_timeout);
2838                 wifi->automaxspeed_timeout = 0;
2839         }
2840 #endif
2841
2842         /* In case of a user scan, device is still referenced */
2843         if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI)) {
2844                 connman_device_set_scanning(device,
2845                                 CONNMAN_SERVICE_TYPE_WIFI, false);
2846                 connman_device_unref(wifi->device);
2847         }
2848
2849 #if defined TIZEN_EXT
2850         stop_autoscan(device);
2851 #endif
2852
2853         remove_networks(device, wifi);
2854         remove_peers(wifi);
2855
2856 #if defined TIZEN_EXT
2857         wifi->scan_pending_network = NULL;
2858
2859         if (is_wifi_notifier_registered == true) {
2860                 connman_notifier_unregister(&notifier);
2861                 is_wifi_notifier_registered = false;
2862         }
2863 #endif
2864
2865         ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
2866         if (ret < 0)
2867                 return ret;
2868
2869         return -EINPROGRESS;
2870 }
2871
2872 static int get_latest_connections(int max_ssids,
2873                                 GSupplicantScanParams *scan_data)
2874 {
2875         GSequenceIter *iter;
2876         GSequence *latest_list;
2877         struct last_connected *entry;
2878         GKeyFile *keyfile;
2879         GTimeVal modified;
2880         gchar **services;
2881         gchar *str;
2882         char *ssid;
2883         int i, freq;
2884         int num_ssids = 0;
2885
2886         latest_list = g_sequence_new(free_entry);
2887         if (!latest_list)
2888                 return -ENOMEM;
2889
2890         services = connman_storage_get_services();
2891         for (i = 0; services && services[i]; i++) {
2892                 if (strncmp(services[i], "wifi_", 5) != 0)
2893                         continue;
2894
2895                 keyfile = connman_storage_load_service(services[i]);
2896                 if (!keyfile)
2897                         continue;
2898
2899                 str = g_key_file_get_string(keyfile,
2900                                         services[i], "Favorite", NULL);
2901                 if (!str || g_strcmp0(str, "true")) {
2902                         g_free(str);
2903                         g_key_file_free(keyfile);
2904                         continue;
2905                 }
2906                 g_free(str);
2907
2908                 str = g_key_file_get_string(keyfile,
2909                                         services[i], "AutoConnect", NULL);
2910                 if (!str || g_strcmp0(str, "true")) {
2911                         g_free(str);
2912                         g_key_file_free(keyfile);
2913                         continue;
2914                 }
2915                 g_free(str);
2916
2917                 str = g_key_file_get_string(keyfile,
2918                                         services[i], "Modified", NULL);
2919                 if (!str) {
2920                         g_key_file_free(keyfile);
2921                         continue;
2922                 }
2923 #if defined TIZEN_EXT
2924
2925                 util_iso8601_to_timeval(str, (struct timeval *)&modified);
2926 #else
2927                 util_iso8601_to_timeval(str, &modified);
2928 #endif
2929                 g_free(str);
2930
2931                 ssid = g_key_file_get_string(keyfile,
2932                                         services[i], "SSID", NULL);
2933
2934                 freq = g_key_file_get_integer(keyfile, services[i],
2935                                         "Frequency", NULL);
2936                 if (freq) {
2937                         entry = g_try_new(struct last_connected, 1);
2938                         if (!entry) {
2939                                 g_sequence_free(latest_list);
2940                                 g_key_file_free(keyfile);
2941                                 g_free(ssid);
2942 #if defined TIZEN_EXT
2943                                 g_strfreev(services);
2944 #endif
2945                                 return -ENOMEM;
2946                         }
2947
2948                         entry->ssid = ssid;
2949                         entry->modified = modified;
2950                         entry->freq = freq;
2951
2952                         g_sequence_insert_sorted(latest_list, entry,
2953                                                 sort_entry, NULL);
2954                         num_ssids++;
2955                 } else
2956                         g_free(ssid);
2957
2958                 g_key_file_free(keyfile);
2959         }
2960
2961         g_strfreev(services);
2962
2963         num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
2964
2965         iter = g_sequence_get_begin_iter(latest_list);
2966
2967         for (i = 0; i < num_ssids; i++) {
2968                 entry = g_sequence_get(iter);
2969
2970                 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
2971                                                 entry->modified.tv_sec);
2972
2973                 add_scan_param(entry->ssid, NULL, 0, entry->freq, scan_data,
2974                                                 max_ssids, entry->ssid);
2975
2976                 iter = g_sequence_iter_next(iter);
2977         }
2978
2979         g_sequence_free(latest_list);
2980         return num_ssids;
2981 }
2982
2983 static void wifi_update_scanner_type(struct wifi_data *wifi,
2984                                         enum wifi_scanning_type new_type)
2985 {
2986         DBG("");
2987
2988         if (!wifi || wifi->scanning_type == new_type)
2989                 return;
2990
2991         wifi->scanning_type = new_type;
2992
2993         setup_autoscan(wifi);
2994 }
2995
2996 static int wifi_scan_simple(struct connman_device *device)
2997 {
2998         struct wifi_data *wifi = connman_device_get_data(device);
2999
3000         reset_autoscan(device);
3001
3002         /* Distinguish between devices performing passive and active scanning */
3003         if (wifi)
3004                 wifi_update_scanner_type(wifi, WIFI_SCANNING_PASSIVE);
3005
3006         return throw_wifi_scan(device, scan_callback_hidden);
3007 }
3008
3009 static gboolean p2p_find_stop(gpointer data)
3010 {
3011         struct connman_device *device = data;
3012         struct wifi_data *wifi = connman_device_get_data(device);
3013
3014         DBG("");
3015
3016         if (wifi) {
3017                 wifi->p2p_find_timeout = 0;
3018
3019                 g_supplicant_interface_p2p_stop_find(wifi->interface);
3020         }
3021
3022         connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
3023
3024         connman_device_unref(device);
3025         start_autoscan(device);
3026
3027         return FALSE;
3028 }
3029
3030 static void p2p_find_callback(int result, GSupplicantInterface *interface,
3031                                                         void *user_data)
3032 {
3033         struct connman_device *device = user_data;
3034         struct wifi_data *wifi = connman_device_get_data(device);
3035
3036         DBG("result %d wifi %p", result, wifi);
3037
3038         if (!wifi)
3039                 goto error;
3040
3041         if (wifi->p2p_find_timeout) {
3042                 g_source_remove(wifi->p2p_find_timeout);
3043                 wifi->p2p_find_timeout = 0;
3044         }
3045
3046         if (result)
3047                 goto error;
3048
3049         wifi->p2p_find_timeout = g_timeout_add_seconds(P2P_FIND_TIMEOUT,
3050                                                         p2p_find_stop, device);
3051         if (!wifi->p2p_find_timeout)
3052                 goto error;
3053
3054         return;
3055 error:
3056         p2p_find_stop(device);
3057 }
3058
3059 static int p2p_find(struct connman_device *device)
3060 {
3061         struct wifi_data *wifi;
3062         int ret;
3063
3064         DBG("");
3065
3066         if (!p2p_technology)
3067                 return -ENOTSUP;
3068
3069         wifi = connman_device_get_data(device);
3070
3071         if (g_supplicant_interface_is_p2p_finding(wifi->interface))
3072                 return -EALREADY;
3073
3074         reset_autoscan(device);
3075         connman_device_ref(device);
3076
3077         ret = g_supplicant_interface_p2p_find(wifi->interface,
3078                                                 p2p_find_callback, device);
3079         if (ret) {
3080                 connman_device_unref(device);
3081                 start_autoscan(device);
3082         } else {
3083                 connman_device_set_scanning(device,
3084                                 CONNMAN_SERVICE_TYPE_P2P, true);
3085         }
3086
3087         return ret;
3088 }
3089
3090 #if defined TIZEN_EXT
3091 static void specific_scan_callback(int result, GSupplicantInterface *interface,
3092                                                 void *user_data)
3093 {
3094         struct connman_device *device = user_data;
3095         struct wifi_data *wifi = connman_device_get_data(device);
3096         bool scanning;
3097
3098         DBG("result %d device %p wifi %p", result, device, wifi);
3099
3100         if (wifi && wifi->scan_params) {
3101                 g_supplicant_free_scan_params(wifi->scan_params);
3102                 wifi->scan_params = NULL;
3103         }
3104
3105         scanning = connman_device_get_scanning(device,
3106                                                CONNMAN_SERVICE_TYPE_WIFI);
3107         if (scanning) {
3108                 connman_device_set_scanning(device,
3109                                 CONNMAN_SERVICE_TYPE_WIFI, false);
3110                 connman_device_unref(device);
3111         }
3112
3113         start_roaming(wifi);
3114 }
3115
3116 static int wifi_specific_scan(enum connman_service_type type,
3117                         struct connman_device *device, int scan_type,
3118                         GSList *specific_scan_list, void *user_data)
3119 {
3120         GSList *list = NULL;
3121         char *ssid = NULL;
3122         struct wifi_data *wifi = connman_device_get_data(device);
3123         GSupplicantScanParams *scan_params = NULL;
3124         struct scan_ssid *scan_ssid = NULL;
3125         bool scanning;
3126         int ret;
3127         int freq;
3128         int count = 0;
3129
3130         if (!wifi)
3131                 return -ENODEV;
3132
3133         if (wifi->p2p_device)
3134                 return 0;
3135
3136         if (type == CONNMAN_SERVICE_TYPE_P2P)
3137                 return p2p_find(device);
3138
3139         if (wifi->tethering)
3140                 return 0;
3141
3142         scanning =
3143                 connman_device_get_scanning(device,
3144                                             CONNMAN_SERVICE_TYPE_WIFI);
3145         if (scanning)
3146                 return -EALREADY;
3147
3148         DBG("scan_type: %d", scan_type);
3149         if (scan_type == CONNMAN_MULTI_SCAN_SSID) { /* ssid based scan */
3150                 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
3151                 if (!scan_params) {
3152                         DBG("Failed to allocate memory.");
3153                         return -ENOMEM;
3154                 }
3155
3156                 for (list = specific_scan_list; list; list = list->next) {
3157                         ssid = (char *)list->data;
3158                         int ssid_len = strlen(ssid);
3159
3160                         scan_ssid = g_try_new0(struct scan_ssid, 1);
3161                         if (!scan_ssid) {
3162                                 DBG("Failed to allocate memory.");
3163                                 g_supplicant_free_scan_params(scan_params);
3164                                 return -ENOMEM;
3165                         }
3166
3167                         memcpy(scan_ssid->ssid, ssid, (ssid_len + 1));
3168                         /* DBG("scan ssid %s len: %d", scan_ssid->ssid, ssid_len); */
3169                         scan_ssid->ssid_len = ssid_len;
3170                         scan_params->ssids = g_slist_prepend(scan_params->ssids, scan_ssid);
3171                         count++;
3172                 }
3173                 scan_params->num_ssids = count;
3174
3175         } else if (scan_type == CONNMAN_MULTI_SCAN_FREQ) { /* frequency based scan */
3176
3177                 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
3178                 if (!scan_params) {
3179                         DBG("Failed to allocate memory.");
3180                         return -ENOMEM;
3181                 }
3182
3183                 guint num_freqs = g_slist_length(specific_scan_list);
3184                 DBG("num_freqs: %d", num_freqs);
3185
3186                 scan_params->freqs = g_try_new0(uint16_t, num_freqs);
3187                 if (!scan_params->freqs) {
3188                         DBG("Failed to allocate memory.");
3189                         g_free(scan_params);
3190                         return -ENOMEM;
3191                 }
3192
3193                 count = 0;
3194                 for (list = specific_scan_list; list; list = list->next) {
3195                         freq = GPOINTER_TO_INT(list->data);
3196
3197                         scan_params->freqs[count] = freq;
3198                         DBG("scan_params->freqs[%d]: %d", count, scan_params->freqs[count]);
3199                         count++;
3200                 }
3201                 scan_params->num_freqs = count;
3202
3203         } else if (scan_type == CONNMAN_MULTI_SCAN_SSID_FREQ) { /* SSID & Frequency mixed scan */
3204                 int freq_count, ap_count;
3205                 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
3206                 if (!scan_params) {
3207                         DBG("Failed to allocate memory.");
3208                         return -ENOMEM;
3209                 }
3210
3211                 guint size = g_slist_length(specific_scan_list);
3212
3213                 scan_params->freqs = g_try_new0(uint16_t, size/2);
3214                 if (!scan_params->freqs) {
3215                         DBG("Failed to allocate memory.");
3216                         g_free(scan_params);
3217                         return -ENOMEM;
3218                 }
3219
3220                 ap_count = freq_count = 0;
3221                 for (list = specific_scan_list; list; list = list->next) {
3222                         if (((connman_multi_scan_ap_s *)list->data)->flag == true) { /** ssid */
3223                                 ssid = ((connman_multi_scan_ap_s *)list->data)->str;
3224                                 int ssid_len = strlen(ssid);
3225
3226                                 scan_ssid = g_try_new0(struct scan_ssid, 1);
3227                                 if (!scan_ssid) {
3228                                         DBG("Failed to allocate memory.");
3229                                         g_supplicant_free_scan_params(scan_params);
3230                                         return -ENOMEM;
3231                                 }
3232
3233                                 memcpy(scan_ssid->ssid, ssid, (ssid_len + 1));
3234                                 /* DBG("scan ssid %s len: %d", scan_ssid->ssid, ssid_len); */
3235                                 scan_ssid->ssid_len = ssid_len;
3236                                 scan_params->ssids = g_slist_prepend(scan_params->ssids, scan_ssid);
3237                                 ap_count++;
3238
3239                         } else { /* freq */
3240                                 freq = atoi(((connman_multi_scan_ap_s *)list->data)->str);
3241                                 scan_params->freqs[freq_count] = freq;
3242                                 DBG("scan_params->freqs[%d]: %d", freq_count, scan_params->freqs[freq_count]);
3243                                 freq_count++;
3244                         }
3245                 }
3246                 scan_params->num_ssids = ap_count;
3247                 scan_params->num_freqs = freq_count;
3248         } else {
3249                 DBG("Invalid scan");
3250                 return -EINVAL;
3251         }
3252
3253         reset_autoscan(device);
3254         connman_device_ref(device);
3255
3256         ret = g_supplicant_interface_scan(wifi->interface, scan_params,
3257                                                 specific_scan_callback, device);
3258
3259         if (ret == 0) {
3260                 connman_device_set_scanning(device,
3261                                 CONNMAN_SERVICE_TYPE_WIFI, true);
3262         } else {
3263                 g_supplicant_free_scan_params(scan_params);
3264                 connman_device_unref(device);
3265         }
3266
3267         return ret;
3268 }
3269
3270 static void wifi_mac_policy_callback(int result,
3271                                         unsigned int policy,
3272                                                 void *user_data)
3273 {
3274         struct connman_device *device = user_data;
3275
3276         if (result == 0)
3277                 connman_device_mac_policy_notify(device, result, policy);
3278
3279         connman_device_unref(device);
3280 }
3281
3282 int wifi_set_mac_policy(struct connman_device *device, unsigned int policy)
3283 {
3284         struct wifi_data *wifi = connman_device_get_data(device);
3285         int ret;
3286
3287         if (!wifi)
3288                 return -EINVAL;
3289
3290         connman_device_ref(device);
3291
3292         ret = g_supplicant_interface_set_mac_policy(wifi->interface,
3293                                         wifi_mac_policy_callback,
3294                                         policy, device);
3295         if (ret != 0)
3296                 connman_device_unref(device);
3297
3298         return ret;
3299 }
3300
3301 static void wifi_preassoc_mac_policy_callback(int result,
3302                                         unsigned int policy,
3303                                                 void *user_data)
3304 {
3305         struct connman_device *device = user_data;
3306
3307         if (result == 0)
3308                 connman_device_preassoc_mac_policy_notify(device, result, policy);
3309
3310         connman_device_unref(device);
3311 }
3312
3313 int wifi_set_preassoc_mac_policy(struct connman_device *device, unsigned int policy)
3314 {
3315         struct wifi_data *wifi = connman_device_get_data(device);
3316         int ret;
3317
3318         if (!wifi)
3319                 return -EINVAL;
3320
3321         connman_device_ref(device);
3322
3323         ret = g_supplicant_interface_set_preassoc_mac_policy(wifi->interface,
3324                                         wifi_preassoc_mac_policy_callback,
3325                                         policy, device);
3326         if (ret != 0)
3327                 connman_device_unref(device);
3328
3329         return ret;
3330 }
3331
3332 static void wifi_random_mac_lifetime_callback(int result,
3333                                         unsigned int lifetime,
3334                                                 void *user_data)
3335 {
3336         struct connman_device *device = user_data;
3337
3338         if (result == 0)
3339                 connman_device_random_mac_lifetime_notify(device, result, lifetime);
3340
3341         connman_device_unref(device);
3342 }
3343
3344 int wifi_set_random_mac_lifetime(struct connman_device *device, unsigned int lifetime)
3345 {
3346         struct wifi_data *wifi = connman_device_get_data(device);
3347         int ret;
3348
3349         if (!wifi)
3350                 return -EINVAL;
3351
3352         connman_device_ref(device);
3353
3354         ret = g_supplicant_interface_set_random_mac_lifetime(wifi->interface,
3355                                         wifi_random_mac_lifetime_callback,
3356                                         lifetime, device);
3357         if (ret != 0)
3358                 connman_device_unref(device);
3359
3360         return ret;
3361 }
3362 #endif
3363
3364 #if defined TIZEN_EXT_WIFI_MESH
3365 static void mesh_scan_callback(int result, GSupplicantInterface *interface,
3366                                                 void *user_data)
3367 {
3368         struct connman_device *device = user_data;
3369         struct wifi_data *wifi = connman_device_get_data(device);
3370         bool scanning;
3371
3372         DBG("result %d wifi %p", result, wifi);
3373
3374         scanning = connman_device_get_scanning(device,
3375                                                CONNMAN_SERVICE_TYPE_MESH);
3376         if (scanning)
3377                 connman_device_set_scanning(device,
3378                                 CONNMAN_SERVICE_TYPE_MESH, false);
3379
3380         if (scanning)
3381                 connman_device_unref(device);
3382 }
3383
3384 static int mesh_scan(struct connman_device *device)
3385 {
3386         struct wifi_data *wifi;
3387         struct wifi_mesh_info *mesh_info;
3388         int ret;
3389
3390         DBG("");
3391
3392         wifi = connman_device_get_data(device);
3393
3394         if (!wifi || !wifi->mesh_interface)
3395                 return -ENOTSUP;
3396
3397         mesh_info = wifi->mesh_info;
3398         reset_autoscan(device);
3399         connman_device_ref(device);
3400
3401         ret = g_supplicant_interface_scan(mesh_info->interface, NULL,
3402                                                 mesh_scan_callback, device);
3403         if (ret)
3404                 connman_device_unref(device);
3405         else
3406                 connman_device_set_scanning(device,
3407                                 CONNMAN_SERVICE_TYPE_MESH, true);
3408
3409         return ret;
3410 }
3411
3412 static void abort_scan_callback(int result, GSupplicantInterface *interface,
3413                                                 void *user_data)
3414 {
3415         struct connman_device *device = user_data;
3416         struct wifi_data *wifi = connman_device_get_data(device);
3417
3418         DBG("result %d wifi %p", result, wifi);
3419
3420         __connman_technology_notify_abort_scan(CONNMAN_SERVICE_TYPE_MESH, result);
3421 }
3422
3423 static int mesh_abort_scan(enum connman_service_type type,
3424                                                 struct connman_device *device)
3425 {
3426         struct wifi_data *wifi = connman_device_get_data(device);
3427         struct wifi_mesh_info *mesh_info;
3428         bool scanning;
3429         int ret;
3430
3431         if (!wifi || !wifi->mesh_interface)
3432                 return -ENODEV;
3433
3434         if (type != CONNMAN_SERVICE_TYPE_MESH)
3435                 return -EINVAL;
3436
3437         mesh_info = wifi->mesh_info;
3438
3439         scanning = connman_device_get_scanning(device,
3440                                                CONNMAN_SERVICE_TYPE_MESH);
3441         if (!scanning)
3442                 return -EEXIST;
3443
3444         ret = g_supplicant_interface_abort_scan(mesh_info->interface,
3445                                                 abort_scan_callback, device);
3446
3447         return ret;
3448 }
3449
3450 static int mesh_specific_scan(enum connman_service_type type,
3451                               struct connman_device *device, const char *ssid,
3452                               unsigned int freq, void *user_data)
3453 {
3454         struct wifi_data *wifi = connman_device_get_data(device);
3455         GSupplicantScanParams *scan_params = NULL;
3456         struct wifi_mesh_info *mesh_info;
3457         struct scan_ssid *scan_ssid;
3458         bool scanning;
3459         int ret;
3460
3461         if (!wifi || !wifi->mesh_interface)
3462                 return -ENODEV;
3463
3464         if (type != CONNMAN_SERVICE_TYPE_MESH)
3465                 return -EINVAL;
3466
3467         if (wifi->p2p_device)
3468                 return 0;
3469
3470         mesh_info = wifi->mesh_info;
3471
3472         scanning = connman_device_get_scanning(device,
3473                                                CONNMAN_SERVICE_TYPE_MESH);
3474         if (scanning)
3475                 return -EALREADY;
3476
3477         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
3478         if (!scan_params)
3479                 return -ENOMEM;
3480
3481         scan_ssid = g_try_new(struct scan_ssid, 1);
3482         if (!scan_ssid) {
3483                 g_free(scan_params);
3484                 return -ENOMEM;
3485         }
3486
3487         scan_ssid->ssid_len = strlen(ssid);
3488         memcpy(scan_ssid->ssid, ssid, scan_ssid->ssid_len);
3489         scan_params->ssids = g_slist_prepend(scan_params->ssids, scan_ssid);
3490         scan_params->num_ssids = 1;
3491
3492         scan_params->freqs = g_try_new(uint16_t, 1);
3493         if (!scan_params->freqs) {
3494                 g_slist_free_full(scan_params->ssids, g_free);
3495                 g_free(scan_params);
3496                 return -ENOMEM;
3497         }
3498
3499         scan_params->freqs[0] = freq;
3500         scan_params->num_freqs = 1;
3501
3502         reset_autoscan(device);
3503         connman_device_ref(device);
3504
3505         ret = g_supplicant_interface_scan(mesh_info->interface, scan_params,
3506                                                 mesh_scan_callback, device);
3507
3508         if (ret == 0) {
3509                 connman_device_set_scanning(device,
3510                                 CONNMAN_SERVICE_TYPE_MESH, true);
3511         } else {
3512                 g_supplicant_free_scan_params(scan_params);
3513                 connman_device_unref(device);
3514         }
3515
3516         return ret;
3517 }
3518 #endif
3519
3520 /*
3521  * Note that the hidden scan is only used when connecting to this specific
3522  * hidden AP first time. It is not used when system autoconnects to hidden AP.
3523  */
3524 static int wifi_scan(struct connman_device *device,
3525                         struct connman_device_scan_params *params)
3526 {
3527         struct wifi_data *wifi = connman_device_get_data(device);
3528         GSupplicantScanParams *scan_params = NULL;
3529         struct scan_ssid *scan_ssid;
3530         struct hidden_params *hidden;
3531         int ret;
3532         int driver_max_ssids = 0;
3533         bool do_hidden;
3534         bool scanning;
3535
3536         if (!wifi)
3537                 return -ENODEV;
3538
3539         if (wifi->p2p_device)
3540                 return -EBUSY;
3541
3542         if (wifi->tethering)
3543                 return -EBUSY;
3544
3545         if (params->type == CONNMAN_SERVICE_TYPE_P2P)
3546                 return p2p_find(device);
3547
3548 #if defined TIZEN_EXT_WIFI_MESH
3549         if (params->type == CONNMAN_SERVICE_TYPE_MESH)
3550                 return mesh_scan(device);
3551 #endif
3552
3553         DBG("device %p wifi %p hidden ssid %s", device, wifi->interface,
3554                 params->ssid);
3555
3556         scanning = connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI);
3557
3558         if (!params->ssid || params->ssid_len == 0 || params->ssid_len > 32) {
3559                 if (scanning)
3560                         return -EALREADY;
3561
3562                 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
3563                                                         wifi->interface);
3564                 DBG("max ssids %d", driver_max_ssids);
3565                 if (driver_max_ssids == 0)
3566                         return wifi_scan_simple(device);
3567
3568                 do_hidden = false;
3569         } else {
3570                 if (scanning && wifi->hidden && wifi->postpone_hidden)
3571                         return -EALREADY;
3572
3573                 do_hidden = true;
3574         }
3575
3576         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
3577         if (!scan_params)
3578                 return -ENOMEM;
3579
3580         if (do_hidden) {
3581                 scan_ssid = g_try_new(struct scan_ssid, 1);
3582                 if (!scan_ssid) {
3583                         g_free(scan_params);
3584                         return -ENOMEM;
3585                 }
3586
3587                 memcpy(scan_ssid->ssid, params->ssid, params->ssid_len);
3588                 scan_ssid->ssid_len = params->ssid_len;
3589                 scan_params->ssids = g_slist_prepend(scan_params->ssids,
3590                                                                 scan_ssid);
3591                 scan_params->num_ssids = 1;
3592
3593                 hidden = g_try_new0(struct hidden_params, 1);
3594                 if (!hidden) {
3595                         g_supplicant_free_scan_params(scan_params);
3596                         return -ENOMEM;
3597                 }
3598
3599                 if (wifi->hidden) {
3600                         hidden_free(wifi->hidden);
3601                         wifi->hidden = NULL;
3602                 }
3603
3604                 memcpy(hidden->ssid, params->ssid, params->ssid_len);
3605                 hidden->ssid_len = params->ssid_len;
3606                 hidden->identity = g_strdup(params->identity);
3607                 hidden->passphrase = g_strdup(params->passphrase);
3608                 hidden->security = g_strdup(params->security);
3609                 hidden->user_data = params->user_data;
3610                 wifi->hidden = hidden;
3611
3612                 if (scanning) {
3613                         /* Let's keep this active scan for later,
3614                          * when current scan will be over. */
3615                         wifi->postpone_hidden = TRUE;
3616                         hidden->scan_params = scan_params;
3617
3618                         return 0;
3619                 }
3620         } else if (wifi->connected) {
3621                 g_supplicant_free_scan_params(scan_params);
3622                 return wifi_scan_simple(device);
3623         } else if (!params->force_full_scan) {
3624                 ret = get_latest_connections(driver_max_ssids, scan_params);
3625                 if (ret <= 0) {
3626                         g_supplicant_free_scan_params(scan_params);
3627                         return wifi_scan_simple(device);
3628                 }
3629         }
3630
3631         /* Distinguish between devices performing passive and active scanning */
3632         wifi_update_scanner_type(wifi, WIFI_SCANNING_ACTIVE);
3633
3634         connman_device_ref(device);
3635
3636         reset_autoscan(device);
3637 #if defined TIZEN_EXT
3638         /*
3639          * When doing a full scan, stored hidden networks also need to be scanned
3640          * so that we can autoconnect to them.
3641          */
3642         if (params->force_full_scan)
3643                 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
3644                                                         scan_callback_hidden, device);
3645         else
3646 #endif
3647         ret = g_supplicant_interface_scan(wifi->interface, scan_params,
3648                                                 scan_callback, device);
3649         if (ret == 0) {
3650                 connman_device_set_scanning(device,
3651                                 CONNMAN_SERVICE_TYPE_WIFI, true);
3652 #if defined TIZEN_EXT
3653                 /*
3654                  * To allow the Full Scan after ssid based scan, set the flag here
3655                  * It is required because Tizen does not use the ConnMan specific
3656                  * backgroung Scan feature.Tizen has added the BG Scan feature in
3657                  * net-config. To sync with up ConnMan, we need to issue the Full Scan
3658                  * after SSID specific scan.
3659                  */
3660                 if (!params->force_full_scan && !do_hidden)
3661                         wifi->allow_full_scan = TRUE;
3662 #endif
3663         } else {
3664                 g_supplicant_free_scan_params(scan_params);
3665                 connman_device_unref(device);
3666
3667                 if (do_hidden) {
3668                         hidden_free(wifi->hidden);
3669                         wifi->hidden = NULL;
3670                 }
3671         }
3672
3673         return ret;
3674 }
3675
3676 static void wifi_stop_scan(enum connman_service_type type,
3677                         struct connman_device *device)
3678 {
3679         struct wifi_data *wifi = connman_device_get_data(device);
3680
3681         DBG("device %p wifi %p", device, wifi);
3682
3683         if (!wifi)
3684                 return;
3685
3686         if (type == CONNMAN_SERVICE_TYPE_P2P) {
3687                 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_P2P)) {
3688                         g_source_remove(wifi->p2p_find_timeout);
3689                         p2p_find_stop(device);
3690                 }
3691         }
3692 }
3693
3694 static void wifi_regdom_callback(int result,
3695                                         const char *alpha2,
3696                                                 void *user_data)
3697 {
3698         struct connman_device *device = user_data;
3699
3700         connman_device_regdom_notify(device, result, alpha2);
3701
3702         connman_device_unref(device);
3703 }
3704
3705 static int wifi_set_regdom(struct connman_device *device, const char *alpha2)
3706 {
3707         struct wifi_data *wifi = connman_device_get_data(device);
3708         int ret;
3709
3710         if (!wifi)
3711                 return -EINVAL;
3712
3713         connman_device_ref(device);
3714
3715         ret = g_supplicant_interface_set_country(wifi->interface,
3716                                                 wifi_regdom_callback,
3717                                                         alpha2, device);
3718         if (ret != 0)
3719                 connman_device_unref(device);
3720
3721         return ret;
3722 }
3723
3724 static struct connman_device_driver wifi_ng_driver = {
3725         .name           = "wifi",
3726         .type           = CONNMAN_DEVICE_TYPE_WIFI,
3727         .priority       = CONNMAN_DEVICE_PRIORITY_LOW,
3728         .probe          = wifi_probe,
3729         .remove         = wifi_remove,
3730         .enable         = wifi_enable,
3731         .disable        = wifi_disable,
3732         .scan           = wifi_scan,
3733         .stop_scan      = wifi_stop_scan,
3734         .set_regdom     = wifi_set_regdom,
3735 #if defined TIZEN_EXT
3736         .specific_scan  = wifi_specific_scan,
3737         .set_mac_policy           = wifi_set_mac_policy,
3738         .set_preassoc_mac_policy  = wifi_set_preassoc_mac_policy,
3739         .set_random_mac_lifetime  = wifi_set_random_mac_lifetime,
3740 #endif
3741 #if defined TIZEN_EXT_WIFI_MESH
3742         .abort_scan     = mesh_abort_scan,
3743         .mesh_specific_scan     = mesh_specific_scan,
3744 #endif
3745 };
3746
3747 static void system_ready(void)
3748 {
3749         DBG("");
3750
3751         if (connman_device_driver_register(&wifi_ng_driver) < 0)
3752                 connman_error("Failed to register WiFi driver");
3753 }
3754
3755 static void system_killed(void)
3756 {
3757         DBG("");
3758
3759         connman_device_driver_unregister(&wifi_ng_driver);
3760 }
3761
3762 static int network_probe(struct connman_network *network)
3763 {
3764 #if defined TIZEN_EXT
3765         if (!simplified_log)
3766 #endif
3767         DBG("network %p", network);
3768
3769         return 0;
3770 }
3771
3772 static void network_remove(struct connman_network *network)
3773 {
3774         struct connman_device *device = connman_network_get_device(network);
3775         struct wifi_data *wifi;
3776 #if defined TIZEN_EXT
3777         GSupplicantSSID *ssid;
3778         const void *ssid_data;
3779         const char *security;
3780 #endif
3781
3782         DBG("network %p", network);
3783
3784         wifi = connman_device_get_data(device);
3785         if (!wifi)
3786                 return;
3787
3788         if (wifi->network != network)
3789                 return;
3790
3791         wifi->network = NULL;
3792
3793 #if defined TIZEN_EXT
3794         wifi->disconnecting = false;
3795
3796         if (wifi->pending_network == network)
3797                 wifi->pending_network = NULL;
3798
3799         if (wifi->scan_pending_network == network)
3800                 wifi->scan_pending_network = NULL;
3801
3802         /*
3803          * If this remove network is for the same network
3804          * for which wpa_supplicant already has a profile
3805          * then need to remove that profile.
3806          */
3807         ssid = g_try_malloc0(sizeof(GSupplicantSSID));
3808         if (!ssid)
3809                 return;
3810
3811         ssid_data = connman_network_get_blob(network, "WiFi.SSID",
3812                                                 &ssid->ssid_len);
3813
3814         ssid->ssid = g_try_malloc0(ssid->ssid_len);
3815
3816         if (!ssid->ssid) {
3817                 g_free(ssid);
3818                 return;
3819         } else {
3820                 memcpy(ssid->ssid, ssid_data, ssid->ssid_len);
3821         }
3822
3823         security = connman_network_get_string(network, "WiFi.Security");
3824         ssid->security = network_security(security);
3825
3826         g_supplicant_interface_remove_network(wifi->interface, ssid);
3827
3828         g_free(ssid->ssid);
3829         g_free(ssid);
3830 #endif
3831 }
3832
3833 static void connect_callback(int result, GSupplicantInterface *interface,
3834                                                         void *user_data)
3835 {
3836 #if defined TIZEN_EXT
3837         GList *list;
3838         struct wifi_data *wifi;
3839 #endif
3840         struct connman_network *network = user_data;
3841
3842         DBG("network %p result %d", network, result);
3843
3844 #if defined TIZEN_EXT
3845         const char *ifname = g_supplicant_interface_get_ifname(interface);
3846         set_connman_bssid(RESET_BSSID, NULL, ifname);
3847
3848         for (list = iface_list; list; list = list->next) {
3849                 wifi = list->data;
3850
3851                 if (wifi && wifi->network == network)
3852                         goto found;
3853         }
3854
3855         /* wifi_data may be invalid because wifi is already disabled */
3856         return;
3857
3858 found:
3859         if (connman_network_get_bool(network, "WiFi.Roaming")) {
3860                 if (result < 0 ) {
3861                         connman_network_set_bool(network, "WiFi.Roaming", false);
3862                         connman_network_set_string(network,
3863                                         "WiFi.RoamingCurBSSID", NULL);
3864                 } else {
3865                         char bssid_buff[WIFI_BSSID_STR_LEN] = {0,};
3866                         char *bssid_str = bssid_buff;
3867                         unsigned char *bssid;
3868                         const char *cur_bssid;
3869
3870                         bssid = g_supplicant_interface_get_add_network_bssid(interface);
3871                         if (!bssid) {
3872                                 connman_network_set_bool(network, "WiFi.Roaming", false);
3873                                 connman_network_set_string(network,
3874                                                 "WiFi.RoamingCurBSSID", NULL);
3875                         } else {
3876                                 snprintf(bssid_str,
3877                                                 WIFI_BSSID_STR_LEN,
3878                                                 MACSTR, MAC2STR(bssid));
3879
3880                                 connman_network_set_string(network,
3881                                                 "WiFi.RoamingDstBSSID", bssid_str);
3882
3883                                 cur_bssid = connman_network_get_string(network,
3884                                                 "WiFi.RoamingCurBSSID");
3885
3886                                 __connman_technology_notify_roaming_state(ifname,
3887                                                 "started", cur_bssid, bssid_str);
3888                         }
3889                 }
3890         }
3891 #endif
3892         if (result == -ENOKEY) {
3893                 connman_network_set_error(network,
3894                                         CONNMAN_NETWORK_ERROR_INVALID_KEY);
3895         } else if (result < 0) {
3896                 connman_network_set_error(network,
3897                                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
3898         }
3899
3900         connman_network_unref(network);
3901 }
3902
3903 static GSupplicantSecurity network_security(const char *security)
3904 {
3905         if (g_str_equal(security, "none"))
3906                 return G_SUPPLICANT_SECURITY_NONE;
3907         else if (g_str_equal(security, "wep"))
3908                 return G_SUPPLICANT_SECURITY_WEP;
3909         else if (g_str_equal(security, "psk"))
3910                 return G_SUPPLICANT_SECURITY_PSK;
3911         else if (g_str_equal(security, "wpa"))
3912                 return G_SUPPLICANT_SECURITY_PSK;
3913         else if (g_str_equal(security, "rsn"))
3914                 return G_SUPPLICANT_SECURITY_PSK;
3915         else if (g_str_equal(security, "ieee8021x"))
3916                 return G_SUPPLICANT_SECURITY_IEEE8021X;
3917 #if defined TIZEN_EXT
3918         else if (g_str_equal(security, "ft_psk") == TRUE)
3919                 return G_SUPPLICANT_SECURITY_FT_PSK;
3920         else if (g_str_equal(security, "ft_ieee8021x") == TRUE)
3921                 return G_SUPPLICANT_SECURITY_FT_IEEE8021X;
3922         else if (g_str_equal(security, "sae"))
3923                 return G_SUPPLICANT_SECURITY_SAE;
3924         else if (g_str_equal(security, "owe"))
3925                 return G_SUPPLICANT_SECURITY_OWE;
3926         else if (g_str_equal(security, "dpp"))
3927                 return G_SUPPLICANT_SECURITY_DPP;
3928         else if (g_str_equal(security, "psk_sha256") == TRUE)
3929                 return G_SUPPLICANT_SECURITY_PSK_SHA256;
3930 #endif
3931
3932         return G_SUPPLICANT_SECURITY_UNKNOWN;
3933 }
3934
3935 #if defined TIZEN_EXT
3936 static GSupplicantEapKeymgmt network_eap_keymgmt(const char *security)
3937 {
3938         if (security == NULL)
3939                 return G_SUPPLICANT_EAP_KEYMGMT_NONE;
3940
3941         if (g_str_equal(security, "FT") == TRUE)
3942                 return G_SUPPLICANT_EAP_KEYMGMT_FT;
3943         else if (g_str_equal(security, "CCKM") == TRUE)
3944                 return G_SUPPLICANT_EAP_KEYMGMT_CCKM;
3945
3946         return G_SUPPLICANT_EAP_KEYMGMT_NONE;
3947 }
3948 #endif
3949
3950 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
3951 {
3952         struct wifi_network *network_data = connman_network_get_data(network);
3953         const char *security;
3954 #if defined TIZEN_EXT
3955         const void *ssid_data;
3956 #endif
3957
3958         memset(ssid, 0, sizeof(*ssid));
3959         ssid->mode = G_SUPPLICANT_MODE_INFRA;
3960 #if defined TIZEN_EXT
3961         ssid_data = connman_network_get_blob(network, "WiFi.SSID",
3962                                                 &ssid->ssid_len);
3963         ssid->ssid = g_try_malloc0(ssid->ssid_len);
3964
3965         if (!ssid->ssid)
3966                 ssid->ssid_len = 0;
3967         else
3968                 memcpy(ssid->ssid, ssid_data, ssid->ssid_len);
3969 #else
3970         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
3971                                                 &ssid->ssid_len);
3972 #endif
3973         ssid->scan_ssid = 1;
3974         security = connman_network_get_string(network, "WiFi.Security");
3975 #if defined TIZEN_EXT
3976         if (connman_network_get_psk_sha256(network))
3977                 security = "psk_sha256";
3978 #endif
3979         ssid->security = network_security(security);
3980
3981         ssid->keymgmt = network_data->keymgmt;
3982         ssid->ieee80211w = G_SUPPLICANT_MFP_OPTIONAL;
3983         ssid->passphrase = connman_network_get_string(network,
3984                                                 "WiFi.Passphrase");
3985
3986         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
3987
3988         /*
3989          * If our private key password is unset,
3990          * we use the supplied passphrase. That is needed
3991          * for PEAP where 2 passphrases (identity and client
3992          * cert may have to be provided.
3993          */
3994         if (!connman_network_get_string(network, "WiFi.PrivateKeyPassphrase"))
3995                 connman_network_set_string(network,
3996                                                 "WiFi.PrivateKeyPassphrase",
3997                                                 ssid->passphrase);
3998         /* We must have an identity for both PEAP and TLS */
3999         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
4000
4001         /* Use agent provided identity as a fallback */
4002         if (!ssid->identity || strlen(ssid->identity) == 0)
4003                 ssid->identity = connman_network_get_string(network,
4004                                                         "WiFi.AgentIdentity");
4005
4006         ssid->anonymous_identity = connman_network_get_string(network,
4007                                                 "WiFi.AnonymousIdentity");
4008         ssid->ca_cert_path = connman_network_get_string(network,
4009                                                         "WiFi.CACertFile");
4010         ssid->subject_match = connman_network_get_string(network,
4011                                                         "WiFi.SubjectMatch");
4012         ssid->altsubject_match = connman_network_get_string(network,
4013                                                         "WiFi.AltSubjectMatch");
4014         ssid->domain_suffix_match = connman_network_get_string(network,
4015                                                         "WiFi.DomainSuffixMatch");
4016         ssid->domain_match = connman_network_get_string(network,
4017                                                         "WiFi.DomainMatch");
4018         ssid->client_cert_path = connman_network_get_string(network,
4019                                                         "WiFi.ClientCertFile");
4020         ssid->private_key_path = connman_network_get_string(network,
4021                                                         "WiFi.PrivateKeyFile");
4022         ssid->private_key_passphrase = connman_network_get_string(network,
4023                                                 "WiFi.PrivateKeyPassphrase");
4024         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
4025
4026         ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
4027         ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
4028 #if defined TIZEN_EXT
4029         ssid->connector = connman_network_get_string(network,
4030                                                         "WiFi.Connector");
4031         ssid->c_sign_key = connman_network_get_string(network,
4032                                                         "WiFi.CSignKey");
4033         ssid->net_access_key = connman_network_get_string(network,
4034                                                 "WiFi.NetAccessKey");
4035 #endif
4036
4037 #if defined TIZEN_EXT
4038         const char *ifname = connman_device_get_string(
4039                         connman_network_get_device(network), "Interface");
4040         if (set_connman_bssid(CHECK_BSSID, NULL, ifname) == 6) {
4041                 ssid->bssid_for_connect_len = 6;
4042                 set_connman_bssid(GET_BSSID, (char *)ssid->bssid_for_connect, ifname);
4043                 DBG("BSSID : %02x:%02x:%02x:%02x:%02x:%02x",
4044                         ssid->bssid_for_connect[0], ssid->bssid_for_connect[1],
4045                         ssid->bssid_for_connect[2], ssid->bssid_for_connect[3],
4046                         ssid->bssid_for_connect[4], ssid->bssid_for_connect[5]);
4047         } else {
4048                 ssid->freq = connman_network_get_frequency(network);
4049         }
4050
4051         GSList *bssid_list = (GSList *)connman_network_get_bssid_list(network);
4052         if (bssid_list && g_slist_length(bssid_list) > 1) {
4053
4054                 /* If there are more than one bssid,
4055                  * the user-specified bssid is tried only once at the beginning.
4056                  * After that, the bssids in the list are tried in order.
4057                  */
4058                 if (set_connman_bssid(CHECK_BSSID, NULL, ifname) == 6) {
4059                         set_connman_bssid(RESET_BSSID, NULL, ifname);
4060                         goto done;
4061                 }
4062
4063                 GSList *list;
4064                 char buff[WIFI_BSSID_STR_LEN];
4065                 const char *dst_bssid = connman_network_get_string(network,
4066                                 "WiFi.RoamingDstBSSID");
4067                 for (list = bssid_list; list; list = list->next) {
4068                         struct connman_bssids * bssids = (struct connman_bssids *)list->data;
4069
4070                         g_snprintf(buff, WIFI_BSSID_STR_LEN, MACSTR, MAC2STR(bssids->bssid));
4071                         buff[WIFI_BSSID_STR_LEN - 1] = '\0';
4072
4073                         if (dst_bssid) {
4074                                 if (g_strcmp0(dst_bssid, (const gchar *)buff) == 0) {
4075                                         memcpy(buff_bssid, bssids->bssid, WIFI_BSSID_LEN_MAX);
4076                                         ssid->bssid = buff_bssid;
4077                                         ssid->freq = (unsigned int)bssids->frequency;
4078                                         break;
4079                                 }
4080                                 continue;
4081                         }
4082
4083                         gchar *curr_bssid = g_strdup((const gchar *)buff);
4084
4085                         if (g_hash_table_contains(failed_bssids, curr_bssid)) {
4086                                 DBG("bssid match, try next bssid");
4087                                 g_free(curr_bssid);
4088                                 continue;
4089                         } else {
4090                                 g_hash_table_add(failed_bssids, curr_bssid);
4091
4092                                 memcpy(buff_bssid, bssids->bssid, WIFI_BSSID_LEN_MAX);
4093                                 ssid->bssid = buff_bssid;
4094                                 ssid->freq = (unsigned int)bssids->frequency;
4095                                 break;
4096                         }
4097                 }
4098
4099                 if (dst_bssid)
4100                         connman_network_set_string(network, "WiFi.RoamingDstBSSID", NULL);
4101
4102                 if (!list) {
4103                         ssid->bssid = connman_network_get_bssid(network);
4104                         g_hash_table_remove_all(failed_bssids);
4105                 }
4106         } else
4107                 ssid->bssid = connman_network_get_bssid(network);
4108
4109 done:
4110         ssid->eap_keymgmt = network_eap_keymgmt(
4111                         connman_network_get_string(network, "WiFi.KeymgmtType"));
4112         ssid->phase1 = connman_network_get_string(network, "WiFi.Phase1");
4113
4114         if (g_strcmp0(ssid->eap, "fast") == 0)
4115                 ssid->pac_file = g_strdup(WIFI_EAP_FAST_PAC_FILE);
4116
4117         ssid->keymgmt = connman_network_get_keymgmt(network);
4118 #endif
4119
4120         if (connman_setting_get_bool("BackgroundScanning"))
4121                 ssid->bgscan = BGSCAN_DEFAULT;
4122 }
4123
4124 static int network_connect(struct connman_network *network)
4125 {
4126         struct connman_device *device = connman_network_get_device(network);
4127         struct wifi_data *wifi;
4128         GSupplicantInterface *interface;
4129         GSupplicantSSID *ssid;
4130
4131         DBG("network %p", network);
4132
4133         if (!device)
4134                 return -ENODEV;
4135
4136         wifi = connman_device_get_data(device);
4137         if (!wifi)
4138                 return -ENODEV;
4139
4140         ssid = g_try_malloc0(sizeof(GSupplicantSSID));
4141         if (!ssid)
4142                 return -ENOMEM;
4143
4144         interface = wifi->interface;
4145
4146         ssid_init(ssid, network);
4147
4148         if (wifi->disconnecting) {
4149                 wifi->pending_network = network;
4150 #if defined TIZEN_EXT
4151                 g_free(ssid->ssid);
4152 #endif
4153                 g_free(ssid);
4154         } else {
4155                 wifi->network = connman_network_ref(network);
4156                 wifi->retries = 0;
4157 #if defined TIZEN_EXT
4158                 wifi->scan_pending_network = NULL;
4159 #endif
4160
4161                 return g_supplicant_interface_connect(interface, ssid,
4162                                                 connect_callback, network);
4163         }
4164
4165         return -EINPROGRESS;
4166 }
4167
4168 static void disconnect_callback(int result, GSupplicantInterface *interface,
4169                                                                 void *user_data)
4170 {
4171         struct disconnect_data *dd = user_data;
4172         struct connman_network *network = dd->network;
4173 #if defined TIZEN_EXT
4174         GList *list;
4175         struct wifi_data *wifi = NULL;
4176
4177         g_free(dd);
4178         DBG("network %p result %d", network, result);
4179
4180         for (list = iface_list; list; list = list->next) {
4181                 wifi = list->data;
4182
4183                 if (!wifi)
4184                         continue;
4185
4186                 if (wifi->network == NULL && wifi->disconnecting == true)
4187                         wifi->disconnecting = false;
4188
4189                 if (wifi->network == network)
4190                         goto found;
4191         }
4192
4193         if (wifi && network == wifi->pending_network)
4194                 wifi->pending_network = NULL;
4195
4196         /* wifi_data may be invalid because wifi is already disabled */
4197         return;
4198
4199 found:
4200 #else
4201         struct wifi_data *wifi = dd->wifi;
4202         g_free(dd);
4203 #endif
4204
4205         DBG("result %d supplicant interface %p wifi %p networks: current %p "
4206                 "pending %p disconnected %p", result, interface, wifi,
4207                 wifi->network, wifi->pending_network, network);
4208
4209         if (result == -ECONNABORTED) {
4210                 DBG("wifi interface no longer available");
4211                 return;
4212         }
4213
4214 #if defined TIZEN_EXT
4215         if (g_slist_find(wifi->networks, network) &&
4216                         wifi->network != wifi->pending_network)
4217 #else
4218         if (g_slist_find(wifi->networks, network))
4219 #endif
4220                 connman_network_set_connected(network, false);
4221
4222         wifi->disconnecting = false;
4223
4224         if (network != wifi->network) {
4225                 if (network == wifi->pending_network)
4226                         wifi->pending_network = NULL;
4227                 DBG("current wifi network has changed since disconnection");
4228                 return;
4229         }
4230
4231         wifi->network = NULL;
4232
4233         wifi->connected = false;
4234
4235         if (wifi->pending_network) {
4236                 network_connect(wifi->pending_network);
4237                 wifi->pending_network = NULL;
4238         }
4239
4240         start_autoscan(wifi->device);
4241 }
4242
4243 static int network_disconnect(struct connman_network *network)
4244 {
4245         struct connman_device *device = connman_network_get_device(network);
4246         struct disconnect_data *dd;
4247         struct wifi_data *wifi;
4248         int err;
4249 #if defined TIZEN_EXT
4250         struct connman_service *service;
4251 #endif
4252
4253         DBG("network %p", network);
4254
4255         wifi = connman_device_get_data(device);
4256         if (!wifi || !wifi->interface)
4257                 return -ENODEV;
4258
4259 #if defined TIZEN_EXT
4260         if (connman_network_get_associating(network) == true) {
4261                 connman_network_clear_associating(network);
4262                 connman_network_set_bool(network, "WiFi.UseWPS", false);
4263         } else {
4264                 service = connman_service_lookup_from_network(network);
4265
4266                 if (service != NULL &&
4267                         (__connman_service_is_connected_state(service,
4268                                         CONNMAN_IPCONFIG_TYPE_IPV4) == false &&
4269                         __connman_service_is_connected_state(service,
4270                                         CONNMAN_IPCONFIG_TYPE_IPV6) == false) &&
4271                         (connman_service_get_favorite(service) == false))
4272                                         __connman_service_set_passphrase(service, NULL);
4273         }
4274
4275         if (wifi->pending_network == network)
4276                 wifi->pending_network = NULL;
4277
4278         if (wifi->scan_pending_network == network)
4279                 wifi->scan_pending_network = NULL;
4280
4281 #endif
4282         connman_network_set_associating(network, false);
4283
4284         if (wifi->disconnecting)
4285                 return -EALREADY;
4286
4287         wifi->disconnecting = true;
4288
4289         dd = g_malloc0(sizeof(*dd));
4290         dd->wifi = wifi;
4291         dd->network = network;
4292
4293 #if defined TIZEN_EXT
4294         err = g_supplicant_interface_disconnect(wifi->interface,
4295                                         disconnect_callback, dd, network);
4296 #else
4297         err = g_supplicant_interface_disconnect(wifi->interface,
4298                                                 disconnect_callback, dd);
4299 #endif
4300
4301         if (err < 0) {
4302                 wifi->disconnecting = false;
4303                 g_free(dd);
4304         }
4305
4306         return err;
4307 }
4308
4309 #if defined TIZEN_EXT
4310 static void set_connection_mode(struct connman_network *network,
4311                 int linkspeed)
4312 {
4313         ieee80211_modes_e phy_mode;
4314         connection_mode_e conn_mode;
4315
4316         phy_mode = connman_network_get_phy_mode(network);
4317         switch (phy_mode) {
4318         case IEEE80211_MODE_B:
4319                 if (linkspeed > 0 && linkspeed <= 11)
4320                         conn_mode = CONNECTION_MODE_IEEE80211B;
4321                 else
4322                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
4323
4324                 break;
4325         case IEEE80211_MODE_BG:
4326                 if (linkspeed > 0 && linkspeed <= 11)
4327                         conn_mode = CONNECTION_MODE_IEEE80211B;
4328                 else if (linkspeed > 11 && linkspeed <= 54)
4329                         conn_mode = CONNECTION_MODE_IEEE80211G;
4330                 else
4331                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
4332
4333                 break;
4334         case IEEE80211_MODE_BGN:
4335                 if (linkspeed > 0 && linkspeed <= 11)
4336                         conn_mode = CONNECTION_MODE_IEEE80211B;
4337                 else if (linkspeed > 11 && linkspeed <= 54)
4338                         conn_mode = CONNECTION_MODE_IEEE80211G;
4339                 else if (linkspeed > 54 && linkspeed <= 450)
4340                         conn_mode = CONNECTION_MODE_IEEE80211N;
4341                 else
4342                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
4343
4344                 break;
4345         case IEEE80211_MODE_A:
4346                 if (linkspeed > 0 && linkspeed <= 54)
4347                         conn_mode = CONNECTION_MODE_IEEE80211A;
4348                 else
4349                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
4350
4351                 break;
4352         case IEEE80211_MODE_AN:
4353                 if (linkspeed > 0 && linkspeed <= 54)
4354                         conn_mode = CONNECTION_MODE_IEEE80211A;
4355                 else if (linkspeed > 54 && linkspeed <= 450)
4356                         conn_mode = CONNECTION_MODE_IEEE80211N;
4357                 else
4358                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
4359
4360                 break;
4361         case IEEE80211_MODE_ANAC:
4362                 if (linkspeed > 0 && linkspeed <= 54)
4363                         conn_mode = CONNECTION_MODE_IEEE80211A;
4364                 else if (linkspeed > 54 && linkspeed <= 450)
4365                         conn_mode = CONNECTION_MODE_IEEE80211N;
4366                 else if (linkspeed > 450 && linkspeed <= 1300)
4367                         conn_mode = CONNECTION_MODE_IEEE80211AC;
4368                 else
4369                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
4370
4371                 break;
4372         default:
4373                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
4374                 break;
4375         }
4376
4377         DBG("connection mode(%d)", conn_mode);
4378         connman_network_set_connection_mode(network, conn_mode);
4379 }
4380
4381 static void signalpoll_callback(int result, int maxspeed, int strength,
4382                                 int snr, void *user_data, unsigned int est_throughput)
4383 {
4384         char bssid_buff[WIFI_BSSID_STR_LEN] = {0,};
4385         char *bssid_str = bssid_buff;
4386         unsigned char *bssid;
4387         struct timespec curr_time = {0};
4388         __time_t roam_scan_time;
4389         const char *interface = NULL;
4390         struct connman_device *device;
4391         struct connman_network *network = user_data;
4392         GSupplicantNetwork *supplicant_network;
4393         struct wifi_data *wifi = NULL;
4394         uint16_t freq = connman_network_get_frequency(network);
4395         const char *group = connman_network_get_group(network);
4396
4397         if (result != 0) {
4398                 DBG("Failed to get maxspeed from signalpoll !");
4399                 connman_network_unref(network);
4400                 return;
4401         }
4402
4403         device = connman_network_get_device(network);
4404         if (device)
4405                 wifi = connman_device_get_data(device);
4406
4407         if (group && wifi) {
4408                 supplicant_network = g_supplicant_interface_get_network(wifi->interface, group);
4409                 if (supplicant_network) {
4410                         g_supplicant_network_set_signal(supplicant_network, strength);
4411                         g_supplicant_network_set_bss_signal(supplicant_network, strength, snr);
4412                 }
4413         }
4414
4415         strength += 120;
4416         if (strength > 100)
4417                 strength = 100;
4418
4419         bssid = connman_network_get_bssid(network);
4420         snprintf(bssid_str, WIFI_BSSID_STR_LEN, MACSTR, MAC2STR(bssid));
4421
4422         DBG("network %p, bssid %s, freq %u, maxspeed %d, strength %d, snr %d, est_throughput %u",
4423                         network, bssid_str, freq, maxspeed, strength, snr, est_throughput);
4424
4425         connman_network_set_strength(network, (uint8_t)strength);
4426         connman_network_set_snr(network, snr);
4427         connman_network_set_est_throughput(network, est_throughput);
4428         connman_network_set_maxspeed(network, maxspeed);
4429         set_connection_mode(network, maxspeed);
4430
4431         if (connman_network_get_max_bssid_count(network) <= 1)
4432                 goto done;
4433
4434         clock_gettime(CLOCK_MONOTONIC, &curr_time);
4435         roam_scan_time = connman_network_get_roam_scan_time(network);
4436         if (curr_time.tv_sec <= roam_scan_time + ROAM_SCAN_INTERVAL)
4437                 goto done;
4438
4439         if (device && need_bss_transition(freq, snr, strength)) {
4440
4441                 interface = connman_device_get_string(device, "Interface");
4442                 __connman_technology_notify_roaming_state(interface, "required", bssid_str, NULL);
4443
4444                 if (connman_setting_get_bool("WifiRoamingScan") == false)
4445                         goto done;
4446
4447                 throw_wifi_scan(device, scan_callback);
4448                 connman_network_set_roam_scan_time(network, curr_time.tv_sec);
4449         }
4450
4451 done:
4452         connman_network_unref(network);
4453 }
4454
4455 static int network_signalpoll(struct wifi_data *wifi)
4456 {
4457         GSupplicantInterface *interface;
4458         struct connman_network *network;
4459
4460         if (!wifi || !wifi->network)
4461                 return -ENODEV;
4462
4463         wifi->network = connman_network_ref(wifi->network);
4464
4465         interface = wifi->interface;
4466         network = wifi->network;
4467
4468         DBG("network %p", network);
4469
4470         return g_supplicant_interface_signalpoll(interface, signalpoll_callback, network);
4471 }
4472
4473 static gboolean autosignalpoll_timeout(gpointer data)
4474 {
4475         struct wifi_data *wifi = data;
4476
4477         if (!wifi || !wifi->automaxspeed_timeout) {
4478                 DBG("automaxspeed_timeout is found to be zero. i.e. currently in disconnected state. !!");
4479                 return FALSE;
4480         }
4481
4482         int ret = network_signalpoll(wifi);
4483         if (ret < 0) {
4484                 DBG("Fail to get max speed !!");
4485                 wifi->automaxspeed_timeout = 0;
4486
4487                 if (wifi->network)
4488                         connman_network_unref(wifi->network);
4489
4490                 return FALSE;
4491         }
4492
4493         return TRUE;
4494 }
4495 #endif
4496
4497 static struct connman_network_driver network_driver = {
4498         .name           = "wifi",
4499         .type           = CONNMAN_NETWORK_TYPE_WIFI,
4500         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
4501         .probe          = network_probe,
4502         .remove         = network_remove,
4503         .connect        = network_connect,
4504         .disconnect     = network_disconnect,
4505 };
4506
4507 static void interface_added(GSupplicantInterface *interface)
4508 {
4509         const char *ifname = g_supplicant_interface_get_ifname(interface);
4510         const char *driver = g_supplicant_interface_get_driver(interface);
4511 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4512         /*
4513          * Note: If supplicant interface's driver is wired then skip it,
4514          * because it meanti only for ethernet not Wi-Fi.
4515          */
4516         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4517                 return;
4518 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4519
4520 #if defined TIZEN_EXT
4521         bool is_5_0_ghz_supported = g_supplicant_interface_get_is_5_0_ghz_supported(interface);
4522         bool is_6_0_ghz_supported = g_supplicant_interface_get_is_6_0_ghz_supported(interface);
4523 #endif
4524
4525         struct wifi_data *wifi;
4526
4527         wifi = g_supplicant_interface_get_data(interface);
4528         if (!wifi) {
4529                 wifi = get_pending_wifi_data(ifname);
4530                 if (!wifi)
4531                         return;
4532
4533                 wifi->interface = interface;
4534                 g_supplicant_interface_set_data(interface, wifi);
4535                 p2p_iface_list = g_list_append(p2p_iface_list, wifi);
4536                 wifi->p2p_device = true;
4537         }
4538
4539         DBG("ifname %s driver %s wifi %p tethering %d",
4540                         ifname, driver, wifi, wifi->tethering);
4541
4542         if (!wifi->device) {
4543                 connman_error("WiFi device not set");
4544                 return;
4545         }
4546
4547         connman_device_set_powered(wifi->device, true);
4548 #if defined TIZEN_EXT
4549         connman_device_set_wifi_5ghz_supported(wifi->device, is_5_0_ghz_supported);
4550         connman_device_set_wifi_6ghz_supported(wifi->device, is_6_0_ghz_supported);
4551         /* Max number of SSIDs supported by wlan chipset that can be scanned */
4552         int max_scan_ssids = g_supplicant_interface_get_max_scan_ssids(interface);
4553         connman_device_set_max_scan_ssids(wifi->device, max_scan_ssids);
4554 #endif
4555 }
4556
4557 static bool is_idle(struct wifi_data *wifi)
4558 {
4559         DBG("state %d", wifi->state);
4560
4561         switch (wifi->state) {
4562         case G_SUPPLICANT_STATE_UNKNOWN:
4563         case G_SUPPLICANT_STATE_DISABLED:
4564         case G_SUPPLICANT_STATE_DISCONNECTED:
4565         case G_SUPPLICANT_STATE_INACTIVE:
4566         case G_SUPPLICANT_STATE_SCANNING:
4567                 return true;
4568
4569         case G_SUPPLICANT_STATE_AUTHENTICATING:
4570         case G_SUPPLICANT_STATE_ASSOCIATING:
4571         case G_SUPPLICANT_STATE_ASSOCIATED:
4572         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4573         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4574         case G_SUPPLICANT_STATE_COMPLETED:
4575                 return false;
4576         }
4577
4578         return false;
4579 }
4580
4581 static bool is_idle_wps(GSupplicantInterface *interface,
4582                                                 struct wifi_data *wifi)
4583 {
4584         /* First, let's check if WPS processing did not went wrong */
4585         if (g_supplicant_interface_get_wps_state(interface) ==
4586                 G_SUPPLICANT_WPS_STATE_FAIL)
4587                 return false;
4588
4589         /* Unlike normal connection, being associated while processing wps
4590          * actually means that we are idling. */
4591         switch (wifi->state) {
4592         case G_SUPPLICANT_STATE_UNKNOWN:
4593         case G_SUPPLICANT_STATE_DISABLED:
4594         case G_SUPPLICANT_STATE_DISCONNECTED:
4595         case G_SUPPLICANT_STATE_INACTIVE:
4596         case G_SUPPLICANT_STATE_SCANNING:
4597         case G_SUPPLICANT_STATE_ASSOCIATED:
4598                 return true;
4599         case G_SUPPLICANT_STATE_AUTHENTICATING:
4600         case G_SUPPLICANT_STATE_ASSOCIATING:
4601         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4602         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4603         case G_SUPPLICANT_STATE_COMPLETED:
4604                 return false;
4605         }
4606
4607         return false;
4608 }
4609
4610 static bool handle_wps_completion(GSupplicantInterface *interface,
4611                                         struct connman_network *network,
4612                                         struct connman_device *device,
4613                                         struct wifi_data *wifi)
4614 {
4615         bool wps;
4616
4617         wps = connman_network_get_bool(network, "WiFi.UseWPS");
4618         if (wps) {
4619                 const unsigned char *ssid, *wps_ssid;
4620                 unsigned int ssid_len, wps_ssid_len;
4621                 struct disconnect_data *dd;
4622                 const char *wps_key;
4623
4624                 /* Checking if we got associated with requested
4625                  * network */
4626                 ssid = connman_network_get_blob(network, "WiFi.SSID",
4627                                                 &ssid_len);
4628
4629                 wps_ssid = g_supplicant_interface_get_wps_ssid(
4630                         interface, &wps_ssid_len);
4631
4632                 if (!wps_ssid || wps_ssid_len != ssid_len ||
4633                                 memcmp(ssid, wps_ssid, ssid_len) != 0) {
4634                         dd = g_malloc0(sizeof(*dd));
4635                         dd->wifi = wifi;
4636                         dd->network = network;
4637
4638                         connman_network_set_associating(network, false);
4639 #if defined TIZEN_EXT
4640                         g_supplicant_interface_disconnect(wifi->interface,
4641                                         disconnect_callback, dd, network);
4642 #else
4643                         g_supplicant_interface_disconnect(wifi->interface,
4644                                                 disconnect_callback, dd);
4645 #endif
4646 #if defined TIZEN_EXT
4647                         connman_network_set_bool(network, "WiFi.UseWPS", false);
4648                         connman_network_set_string(network, "WiFi.PinWPS", NULL);
4649 #endif
4650                         return false;
4651                 }
4652
4653                 wps_key = g_supplicant_interface_get_wps_key(interface);
4654 #if defined TIZEN_EXT
4655                 /* Check the passphrase and encrypt it
4656                  */
4657                  int ret;
4658                  gchar *passphrase = g_strdup(wps_key);
4659
4660                  connman_network_set_string(network, "WiFi.PinWPS", NULL);
4661
4662                  if (check_passphrase_ext(network, passphrase) < 0) {
4663                          DBG("[WPS] Invalid passphrase");
4664                          g_free(passphrase);
4665                          return true;
4666                  }
4667
4668                  ret = send_encryption_request(passphrase, network);
4669
4670                  g_free(passphrase);
4671
4672                  if (!ret)
4673                          DBG("[WPS] Encryption request succeeded");
4674                  else
4675                          DBG("[WPS] Encryption request failed %d", ret);
4676
4677 #else
4678                 connman_network_set_string(network, "WiFi.Passphrase",
4679                                         wps_key);
4680
4681                 connman_network_set_string(network, "WiFi.PinWPS", NULL);
4682 #endif
4683         }
4684
4685         return true;
4686 }
4687
4688 static bool handle_assoc_status_code(GSupplicantInterface *interface,
4689                                      struct wifi_data *wifi)
4690 {
4691 #if defined TIZEN_EXT
4692         if ((wifi->state == G_SUPPLICANT_STATE_ASSOCIATING ||
4693                         wifi->state == G_SUPPLICANT_STATE_AUTHENTICATING ||
4694                         wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) &&
4695 #else
4696         if (wifi->state == G_SUPPLICANT_STATE_ASSOCIATING &&
4697                         wifi->assoc_code == ASSOC_STATUS_NO_CLIENT &&
4698 #endif
4699                         wifi->load_shaping_retries < LOAD_SHAPING_MAX_RETRIES) {
4700                 wifi->load_shaping_retries ++;
4701                 return TRUE;
4702         }
4703         wifi->load_shaping_retries = 0;
4704         return FALSE;
4705 }
4706
4707 static bool handle_4way_handshake_failure(GSupplicantInterface *interface,
4708                                         struct connman_network *network,
4709                                         struct wifi_data *wifi)
4710 {
4711         struct connman_service *service;
4712
4713 #if defined TIZEN_EXT
4714         const char *security;
4715         if (wifi->connected)
4716                 return false;
4717
4718         security = connman_network_get_string(network, "WiFi.Security");
4719
4720         if (security && g_str_equal(security, "ieee8021x") == true &&
4721                         wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) {
4722                 wifi->retries = 0;
4723                 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
4724
4725                 return false;
4726         }
4727
4728         if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
4729                 return false;
4730 #else
4731         if ((wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE) &&
4732                         !((wifi->state == G_SUPPLICANT_STATE_ASSOCIATING) &&
4733                                 (wifi->assoc_code == ASSOC_STATUS_AUTH_TIMEOUT)))
4734                 return false;
4735
4736         if (wifi->connected)
4737                 return false;
4738 #endif
4739
4740         service = connman_service_lookup_from_network(network);
4741         if (!service)
4742                 return false;
4743
4744         wifi->retries++;
4745
4746         if (connman_service_get_favorite(service)) {
4747                 if (wifi->retries < FAVORITE_MAXIMUM_RETRIES)
4748                         return true;
4749         }
4750
4751         wifi->retries = 0;
4752         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
4753
4754         return false;
4755 }
4756
4757 #if defined TIZEN_EXT
4758 static bool handle_wifi_assoc_retry(struct connman_network *network,
4759                                         struct wifi_data *wifi)
4760 {
4761         const char *security;
4762
4763         if (!wifi->network || wifi->connected || wifi->disconnecting ||
4764                         connman_network_get_connecting(network) != true) {
4765                 wifi->assoc_retry_count = 0;
4766                 return false;
4767         }
4768
4769         if (wifi->state != G_SUPPLICANT_STATE_AUTHENTICATING &&
4770                         wifi->state != G_SUPPLICANT_STATE_ASSOCIATING &&
4771                         wifi->state != G_SUPPLICANT_STATE_ASSOCIATED) {
4772                 wifi->assoc_retry_count = 0;
4773                 return false;
4774         }
4775
4776         security = connman_network_get_string(network, "WiFi.Security");
4777         if (security && g_str_equal(security, "ieee8021x") == true &&
4778                         wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) {
4779                 wifi->assoc_retry_count = 0;
4780                 return false;
4781         }
4782
4783         if (wifi->state > assoc_last_state)
4784                 assoc_last_state = wifi->state;
4785
4786         if (++wifi->assoc_retry_count >= TIZEN_ASSOC_RETRY_COUNT) {
4787                 wifi->assoc_retry_count = 0;
4788
4789                 /* Honestly it's not an invalid-key error,
4790                  * however QA team recommends that the invalid-key error
4791                  * might be better to display for user experience.
4792                  */
4793                 switch (assoc_last_state) {
4794                 case G_SUPPLICANT_STATE_AUTHENTICATING:
4795                         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_AUTHENTICATE_FAIL);
4796                         break;
4797                 case G_SUPPLICANT_STATE_ASSOCIATED:
4798                         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
4799                         break;
4800                 default:
4801                         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
4802                         break;
4803                 }
4804
4805                 return false;
4806         }
4807
4808         return true;
4809 }
4810
4811 static void handle_wifi_roaming_complete(struct connman_network *network)
4812 {
4813         const char *cur_bssid;
4814         const char *dst_bssid;
4815         const char *ifname;
4816         struct connman_device *device;
4817         struct connman_service *service;
4818         struct connman_ipconfig *ipconfig_ipv4;
4819         enum connman_ipconfig_type type;
4820         enum connman_ipconfig_method method;
4821
4822         if (!connman_setting_get_bool("WifiRoaming") ||
4823                         !connman_network_get_bool(network, "WiFi.Roaming"))
4824                 return;
4825
4826         device = connman_network_get_device(network);
4827         if (device) {
4828                 ifname = connman_device_get_string(device, "Interface");
4829                 cur_bssid = connman_network_get_string(network,
4830                                 "WiFi.RoamingCurBSSID");
4831                 dst_bssid = connman_network_get_string(network,
4832                                 "WiFi.RoamingDstBSSID");
4833         }
4834
4835         if (device && ifname && cur_bssid && dst_bssid) {
4836                 __connman_technology_notify_roaming_state(ifname,
4837                                 "success", cur_bssid, dst_bssid);
4838                 connman_network_set_bool(network,
4839                                 "WiFi.Roaming", false);
4840                 connman_network_set_string(network,
4841                                 "WiFi.RoamingCurBSSID", NULL);
4842                 connman_network_set_string(network,
4843                                 "WiFi.RoamingDstBSSID", NULL);
4844
4845                 service = connman_service_lookup_from_network(network);
4846                 if (!service)
4847                         return;
4848
4849                 ipconfig_ipv4 = __connman_service_get_ip4config(service);
4850                 if (!ipconfig_ipv4) {
4851                         connman_error("Service has no IPv4 configuration");
4852                         return;
4853                 }
4854
4855                 type = __connman_ipconfig_get_config_type(ipconfig_ipv4);
4856                 if (type != CONNMAN_IPCONFIG_TYPE_IPV4)
4857                         return;
4858
4859                 method = __connman_ipconfig_get_method(ipconfig_ipv4);
4860                 if (method != CONNMAN_IPCONFIG_METHOD_DHCP)
4861                         return;
4862
4863                 connman_network_set_bool(network, "WiFi.RoamingDHCP", true);
4864
4865                 if (set_connected_dhcp(network) != 0)
4866                         connman_network_set_bool(network, "WiFi.RoamingDHCP", false);
4867         }
4868 }
4869 #endif
4870
4871 static void interface_state(GSupplicantInterface *interface)
4872 {
4873         struct connman_network *network;
4874         struct connman_device *device;
4875         struct wifi_data *wifi;
4876         GSupplicantState state = g_supplicant_interface_get_state(interface);
4877 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4878         /*
4879          * Note: If supplicant interface's driver is wired then skip it,
4880          * because it meanti only for ethernet not Wi-Fi.
4881          */
4882         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4883                 return;
4884 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4885
4886         bool wps;
4887         bool old_connected;
4888
4889         wifi = g_supplicant_interface_get_data(interface);
4890
4891         DBG("wifi %p interface state %d", wifi, state);
4892
4893         if (!wifi)
4894                 return;
4895
4896         device = wifi->device;
4897         if (!device)
4898                 return;
4899
4900         if (state == G_SUPPLICANT_STATE_COMPLETED) {
4901                 if (wifi->tethering_param) {
4902                         g_free(wifi->tethering_param->ssid);
4903                         g_free(wifi->tethering_param);
4904                         wifi->tethering_param = NULL;
4905                 }
4906
4907                 if (wifi->tethering)
4908                         stop_autoscan(device);
4909         }
4910
4911         if (g_supplicant_interface_get_ready(interface) &&
4912                                         !wifi->interface_ready) {
4913                 wifi->interface_ready = true;
4914                 finalize_interface_creation(wifi);
4915         }
4916
4917         network = wifi->network;
4918         if (!network)
4919                 return;
4920
4921         switch (state) {
4922         case G_SUPPLICANT_STATE_SCANNING:
4923 #if defined TIZEN_EXT
4924                 if (wifi->automaxspeed_timeout != 0) {
4925                         g_source_remove(wifi->automaxspeed_timeout);
4926                         wifi->automaxspeed_timeout = 0;
4927                         DBG("Remove signalpoll timer!!");
4928                 }
4929
4930                 if (!connman_network_get_bool(wifi->network, "WiFi.Roaming"))
4931 #endif
4932                 if (wifi->connected)
4933                         connman_network_set_connected(network, false);
4934
4935                 break;
4936
4937         case G_SUPPLICANT_STATE_AUTHENTICATING:
4938         case G_SUPPLICANT_STATE_ASSOCIATING:
4939 #if defined TIZEN_EXT
4940                 reset_autoscan(device);
4941 #else
4942                 stop_autoscan(device);
4943 #endif
4944 #if defined TIZEN_EXT
4945                 if (!connman_network_get_bool(wifi->network, "WiFi.Roaming"))
4946 #endif
4947                 if (!wifi->connected)
4948                         connman_network_set_associating(network, true);
4949
4950                 break;
4951
4952         case G_SUPPLICANT_STATE_COMPLETED:
4953 #if defined TIZEN_EXT
4954                 /* though it should be already reset: */
4955                 reset_autoscan(device);
4956
4957                 wifi->assoc_retry_count = 0;
4958
4959                 wifi->scan_pending_network = NULL;
4960
4961                 /* should be cleared scanning flag */
4962                 bool scanning = connman_device_get_scanning(device,
4963                                                CONNMAN_SERVICE_TYPE_WIFI);
4964                 if (scanning){
4965                         connman_device_set_scanning(device,
4966                                 CONNMAN_SERVICE_TYPE_WIFI, false);
4967                         connman_device_unref(device);
4968                 }
4969
4970                 if (!wifi->automaxspeed_timeout) {
4971                         DBG("Going to start signalpoll timer!!");
4972                         int ret = network_signalpoll(wifi);
4973                         if (ret < 0)
4974                                 DBG("Fail to get max speed !!");
4975                         else
4976                                 wifi->automaxspeed_timeout = g_timeout_add_seconds(10, autosignalpoll_timeout, wifi);
4977                 }
4978
4979                 g_hash_table_remove_all(failed_bssids);
4980                 handle_wifi_roaming_complete(network);
4981 #else
4982                 /* though it should be already stopped: */
4983                 stop_autoscan(device);
4984 #endif
4985
4986                 if (!handle_wps_completion(interface, network, device, wifi))
4987                         break;
4988
4989                 connman_network_set_connected(network, true);
4990
4991                 wifi->disconnect_code = 0;
4992                 wifi->assoc_code = 0;
4993                 wifi->load_shaping_retries = 0;
4994                 break;
4995
4996         case G_SUPPLICANT_STATE_DISCONNECTED:
4997 #if defined TIZEN_EXT
4998                 connman_network_set_strength(network, 0);
4999                 connman_network_set_maxspeed(network, 0);
5000
5001                 if (wifi->automaxspeed_timeout != 0) {
5002                         g_source_remove(wifi->automaxspeed_timeout);
5003                         wifi->automaxspeed_timeout = 0;
5004                         DBG("Remove signalpoll timer!!");
5005                 }
5006 #endif
5007                 /*
5008                  * If we're in one of the idle modes, we have
5009                  * not started association yet and thus setting
5010                  * those ones to FALSE could cancel an association
5011                  * in progress.
5012                  */
5013                 wps = connman_network_get_bool(network, "WiFi.UseWPS");
5014                 if (wps)
5015                         if (is_idle_wps(interface, wifi))
5016                                 break;
5017
5018                 if (is_idle(wifi))
5019                         break;
5020
5021 #if defined TIZEN_EXT
5022                 if (!wifi->connected && !wifi->disconnecting &&
5023                                 handle_assoc_status_code(interface, wifi)) {
5024                         GSList *bssid_list = NULL;
5025                         guint bssid_length = 0;
5026
5027                         if (TIZEN_INS_ENABLED) {
5028                                 const char *group = connman_network_get_group(network);
5029                                 GSupplicantNetwork *supplicant_network;
5030
5031                                 if (group) {
5032                                         supplicant_network = g_supplicant_interface_get_network(interface, group);
5033
5034                                         connman_network_set_assoc_reject_table(network,
5035                                                         g_supplicant_network_clone_assoc_reject_table(supplicant_network));
5036
5037                                         g_supplicant_network_update_assoc_reject(interface, supplicant_network);
5038                                 }
5039                         }
5040
5041                         bssid_list = (GSList *)connman_network_get_bssid_list(network);
5042                         if (bssid_list)
5043                                 bssid_length = g_slist_length(bssid_list);
5044
5045                         if (bssid_length > 1 && bssid_length > g_hash_table_size(failed_bssids)) {
5046                                 network_connect(network);
5047                                 break;
5048                         }
5049
5050                         wifi->load_shaping_retries = 0;
5051                 }
5052 #else
5053                 if (handle_assoc_status_code(interface, wifi))
5054                         break;
5055 #endif
5056
5057                 /* If previous state was 4way-handshake, then
5058                  * it's either: psk was incorrect and thus we retry
5059                  * or if we reach the maximum retries we declare the
5060                  * psk as wrong */
5061                 if (handle_4way_handshake_failure(interface,
5062                                                 network, wifi))
5063                         break;
5064
5065                 /* See table 8-36 Reason codes in IEEE Std 802.11 */
5066                 switch (wifi->disconnect_code) {
5067 #if defined TIZEN_EXT
5068                 case 1: /* Unspecified reason */
5069                         /* Let's assume it's because we got blocked */
5070 #endif
5071                 case 6: /* Class 2 frame received from nonauthenticated STA */
5072                         connman_network_set_error(network,
5073                                                 CONNMAN_NETWORK_ERROR_BLOCKED);
5074                         break;
5075
5076                 default:
5077                         break;
5078                 }
5079
5080 #if defined TIZEN_EXT
5081                 if (wifi->assoc_retry_count == 0)
5082                         assoc_last_state = G_SUPPLICANT_STATE_UNKNOWN;
5083
5084                 /* Some of Wi-Fi networks are not comply Wi-Fi specification.
5085                  * Retry association until its retry count is expired */
5086                 if (handle_wifi_assoc_retry(network, wifi) == true) {
5087                         throw_wifi_scan(wifi->device, scan_callback);
5088                         wifi->scan_pending_network = wifi->network;
5089                         break;
5090                 }
5091
5092                 if (wifi->disconnect_code > 0){
5093                         DBG("Set disconnect reason code(%d)", wifi->disconnect_code);
5094                         connman_network_set_disconnect_reason(network, wifi->disconnect_code);
5095                 }
5096 #endif
5097
5098                 if (network != wifi->pending_network) {
5099                         connman_network_set_connected(network, false);
5100                         connman_network_set_associating(network, false);
5101                 }
5102                 wifi->disconnecting = false;
5103
5104                 start_autoscan(device);
5105
5106                 break;
5107
5108         case G_SUPPLICANT_STATE_INACTIVE:
5109 #if defined TIZEN_EXT
5110                 if (handle_wps_completion(interface, network, device, wifi) == false)
5111                         break;
5112 #endif
5113                 connman_network_set_associating(network, false);
5114                 start_autoscan(device);
5115
5116                 break;
5117
5118         case G_SUPPLICANT_STATE_UNKNOWN:
5119         case G_SUPPLICANT_STATE_DISABLED:
5120         case G_SUPPLICANT_STATE_ASSOCIATED:
5121         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
5122         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
5123                 break;
5124         }
5125
5126         old_connected = wifi->connected;
5127         wifi->state = state;
5128
5129         /* Saving wpa_s state policy:
5130          * If connected and if the state changes are roaming related:
5131          * --> We stay connected
5132          * If completed
5133          * --> We are connected
5134          * All other case:
5135          * --> We are not connected
5136          * */
5137         switch (state) {
5138         case G_SUPPLICANT_STATE_AUTHENTICATING:
5139         case G_SUPPLICANT_STATE_ASSOCIATING:
5140         case G_SUPPLICANT_STATE_ASSOCIATED:
5141         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
5142         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
5143                 if (wifi->connected)
5144                         connman_warn("Probably roaming right now!"
5145                                                 " Staying connected...");
5146                 break;
5147         case G_SUPPLICANT_STATE_SCANNING:
5148                 wifi->connected = false;
5149
5150                 if (old_connected)
5151                         start_autoscan(device);
5152                 break;
5153         case G_SUPPLICANT_STATE_COMPLETED:
5154                 wifi->connected = true;
5155                 break;
5156         default:
5157                 wifi->connected = false;
5158                 break;
5159         }
5160
5161         DBG("DONE");
5162 }
5163
5164 static void interface_removed(GSupplicantInterface *interface)
5165 {
5166         const char *ifname = g_supplicant_interface_get_ifname(interface);
5167         struct wifi_data *wifi;
5168 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5169         /*
5170          * Note: If supplicant interface's driver is wired then skip it,
5171          * because it meanti only for ethernet not Wi-Fi.
5172          */
5173         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5174                 return;
5175 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5176
5177
5178         DBG("ifname %s", ifname);
5179
5180         wifi = g_supplicant_interface_get_data(interface);
5181
5182 #if defined TIZEN_EXT_WIFI_MESH
5183         if (wifi && wifi->mesh_interface) {
5184                 DBG("Notify mesh interface remove");
5185                 connman_mesh_notify_interface_remove(true);
5186                 struct wifi_mesh_info *mesh_info = wifi->mesh_info;
5187                 g_free(mesh_info->parent_ifname);
5188                 g_free(mesh_info->ifname);
5189                 g_free(mesh_info->identifier);
5190                 g_free(mesh_info);
5191                 wifi->mesh_interface = false;
5192                 wifi->mesh_info = NULL;
5193                 return;
5194         }
5195 #endif
5196
5197         if (wifi)
5198                 wifi->interface = NULL;
5199
5200         if (wifi && wifi->tethering)
5201                 return;
5202
5203         if (!wifi || !wifi->device) {
5204                 DBG("wifi interface already removed");
5205                 return;
5206         }
5207
5208         connman_device_set_powered(wifi->device, false);
5209
5210         check_p2p_technology();
5211 #if defined TIZEN_EXT_WIFI_MESH
5212         check_mesh_technology();
5213 #endif
5214 }
5215
5216 static void set_device_type(const char *type, char dev_type[17])
5217 {
5218         const char *oui = "0050F204";
5219         const char *category = "0001";
5220         const char *sub_category = "0000";
5221
5222         if (!g_strcmp0(type, "handset")) {
5223                 category = "000A";
5224                 sub_category = "0005";
5225         } else if (!g_strcmp0(type, "vm") || !g_strcmp0(type, "container"))
5226                 sub_category = "0001";
5227         else if (!g_strcmp0(type, "server"))
5228                 sub_category = "0002";
5229         else if (!g_strcmp0(type, "laptop"))
5230                 sub_category = "0005";
5231         else if (!g_strcmp0(type, "desktop"))
5232                 sub_category = "0006";
5233         else if (!g_strcmp0(type, "tablet"))
5234                 sub_category = "0009";
5235         else if (!g_strcmp0(type, "watch"))
5236                 category = "00FF";
5237
5238         snprintf(dev_type, 17, "%s%s%s", category, oui, sub_category);
5239 }
5240
5241 static void p2p_support(GSupplicantInterface *interface)
5242 {
5243         char dev_type[17] = {};
5244         const char *hostname;
5245 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5246         /*
5247          * Note: If supplicant interface's driver is wired then skip it,
5248          * because it meanti only for ethernet not Wi-Fi.
5249          */
5250         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5251                 return;
5252 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5253
5254
5255         DBG("");
5256
5257         if (!interface)
5258                 return;
5259
5260         if (!g_supplicant_interface_has_p2p(interface))
5261                 return;
5262
5263         if (connman_technology_driver_register(&p2p_tech_driver) < 0) {
5264                 DBG("Could not register P2P technology driver");
5265                 return;
5266         }
5267
5268         hostname = connman_utsname_get_hostname();
5269         if (!hostname)
5270                 hostname = "ConnMan";
5271
5272         set_device_type(connman_machine_get_type(), dev_type);
5273         g_supplicant_interface_set_p2p_device_config(interface,
5274                                                         hostname, dev_type);
5275         connman_peer_driver_register(&peer_driver);
5276 }
5277
5278 static void scan_started(GSupplicantInterface *interface)
5279 {
5280 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5281         /*
5282          * Note: If supplicant interface's driver is wired then skip it,
5283          * because it meanti only for ethernet not Wi-Fi.
5284          */
5285         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5286                 return;
5287 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5288
5289         DBG("");
5290 }
5291
5292 static void scan_finished(GSupplicantInterface *interface)
5293 {
5294 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5295         /*
5296          * Note: If supplicant interface's driver is wired then skip it,
5297          * because it meanti only for ethernet not Wi-Fi.
5298          */
5299         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5300                 return;
5301 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5302
5303 #if defined TIZEN_EXT
5304         struct wifi_data *wifi;
5305         bool is_associating = false;
5306         static bool is_scanning = true;
5307 #endif
5308
5309         DBG("");
5310
5311 #if defined TIZEN_EXT
5312         wifi = g_supplicant_interface_get_data(interface);
5313         if (wifi && wifi->scan_pending_network) {
5314                 network_connect(wifi->scan_pending_network);
5315                 wifi->scan_pending_network = NULL;
5316         }
5317
5318         //service state - associating
5319         if(!wifi || !wifi->network)
5320                 return;
5321
5322         is_associating = connman_network_get_associating(wifi->network);
5323         if(is_associating && is_scanning){
5324                 is_scanning = false;
5325                 DBG("send scan for connecting");
5326                 throw_wifi_scan(wifi->device, scan_callback);
5327
5328                 return;
5329         }
5330         is_scanning = true;
5331
5332         //go scan
5333
5334 #endif
5335 }
5336
5337 static void ap_create_fail(GSupplicantInterface *interface)
5338 {
5339 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5340         /*
5341          * Note: If supplicant interface's driver is wired then skip it,
5342          * because it meanti only for ethernet not Wi-Fi.
5343          */
5344         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5345                 return;
5346 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5347
5348         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
5349         int ret;
5350
5351         if ((wifi->tethering) && (wifi->tethering_param)) {
5352                 DBG("%s create AP fail \n",
5353                                 g_supplicant_interface_get_ifname(wifi->interface));
5354
5355                 connman_inet_remove_from_bridge(wifi->index, wifi->bridge);
5356                 wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
5357                 wifi->tethering = false;
5358
5359                 ret = tech_set_tethering(wifi->tethering_param->technology,
5360                                 wifi->bridge, true);
5361
5362                 if ((ret == -EOPNOTSUPP) && (wifi_technology)) {
5363                         connman_technology_tethering_notify(wifi_technology,false);
5364                 }
5365
5366                 g_free(wifi->tethering_param->ssid);
5367                 g_free(wifi->tethering_param);
5368                 wifi->tethering_param = NULL;
5369         }
5370 }
5371
5372 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
5373 {
5374         unsigned char strength;
5375
5376         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
5377 #if !defined TIZEN_EXT
5378         if (strength > 100)
5379                 strength = 100;
5380 #endif
5381
5382         return strength;
5383 }
5384
5385 #if defined TIZEN_EXT_WIFI_MESH
5386 static void mesh_peer_added(GSupplicantNetwork *supplicant_network)
5387 {
5388         GSupplicantInterface *interface;
5389         struct wifi_data *wifi;
5390         const char *name, *security;
5391         struct connman_mesh *connman_mesh;
5392         struct wifi_mesh_info *mesh_info;
5393         const unsigned char *bssid;
5394         const char *identifier;
5395         char *address;
5396         uint16_t frequency;
5397         int ret;
5398
5399         interface = g_supplicant_network_get_interface(supplicant_network);
5400         wifi = g_supplicant_interface_get_data(interface);
5401         if (!wifi || !wifi->mesh_interface) {
5402                 DBG("Virtual Mesh interface not created");
5403                 return;
5404         }
5405
5406         bssid = g_supplicant_network_get_bssid(supplicant_network);
5407         address = g_malloc0(19);
5408         snprintf(address, 19, "%02x:%02x:%02x:%02x:%02x:%02x", bssid[0], bssid[1],
5409                                                                  bssid[2], bssid[3], bssid[4], bssid[5]);
5410
5411         identifier = g_supplicant_network_get_identifier(supplicant_network);
5412         name = g_supplicant_network_get_name(supplicant_network);
5413         security = g_supplicant_network_get_security(supplicant_network);
5414         frequency = g_supplicant_network_get_frequency(supplicant_network);
5415
5416         mesh_info = wifi->mesh_info;
5417         connman_mesh = connman_mesh_get(mesh_info->identifier, identifier);
5418         if (connman_mesh)
5419                 goto done;
5420
5421         DBG("Mesh Peer name %s identifier %s security %s added", name, identifier,
5422                                         security);
5423         connman_mesh = connman_mesh_create(mesh_info->identifier, identifier);
5424         connman_mesh_set_name(connman_mesh, name);
5425         connman_mesh_set_security(connman_mesh, security);
5426         connman_mesh_set_frequency(connman_mesh, frequency);
5427         connman_mesh_set_address(connman_mesh, address);
5428         connman_mesh_set_index(connman_mesh, mesh_info->index);
5429         connman_mesh_set_strength(connman_mesh,
5430                                                 calculate_strength(supplicant_network));
5431         connman_mesh_set_peer_type(connman_mesh, CONNMAN_MESH_PEER_TYPE_DISCOVERED);
5432
5433         ret = connman_mesh_register(connman_mesh);
5434         if (ret == -EALREADY)
5435                 DBG("Mesh Peer is already registered");
5436
5437 done:
5438         g_free(address);
5439 }
5440
5441 static void mesh_peer_removed(GSupplicantNetwork *supplicant_network)
5442 {
5443         GSupplicantInterface *interface;
5444         struct wifi_data *wifi;
5445         struct connman_mesh *connman_mesh;
5446         struct wifi_mesh_info *mesh_info;
5447         const char *identifier;
5448
5449         interface = g_supplicant_network_get_interface(supplicant_network);
5450         wifi = g_supplicant_interface_get_data(interface);
5451         if (!wifi || !wifi->mesh_interface) {
5452                 DBG("Virtual Mesh interface not created");
5453                 return;
5454         }
5455
5456         identifier = g_supplicant_network_get_identifier(supplicant_network);
5457         if (!identifier) {
5458                 DBG("Failed to get Mesh Peer identifier");
5459                 return;
5460         }
5461
5462         mesh_info = wifi->mesh_info;
5463         connman_mesh = connman_mesh_get(mesh_info->identifier, identifier);
5464         if (connman_mesh) {
5465                 /* Do not unregister connected mesh peer */
5466                 if (connman_mesh_peer_is_connected_state(connman_mesh)) {
5467                         DBG("Mesh Peer %s is connected", identifier);
5468                         return;
5469                 }
5470                 DBG("Mesh Peer identifier %s removed", identifier);
5471                 connman_mesh_unregister(connman_mesh);
5472         }
5473 }
5474 #endif
5475
5476
5477 #if defined TIZEN_EXT
5478 static GSList *get_supported_security_list(unsigned int keymgmt,
5479                                         bool owe_transition_mode,
5480                                         GSupplicantNetwork *supplicant_network)
5481 {
5482         GSList *sec_list = NULL;
5483         dbus_bool_t privacy = g_supplicant_network_get_privacy(supplicant_network);
5484         const char *enc_mode = g_supplicant_network_get_enc_mode(supplicant_network);
5485
5486         if (keymgmt &
5487                         (G_SUPPLICANT_KEYMGMT_WPA_EAP |
5488                                         G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
5489                 sec_list = g_slist_prepend (sec_list, "ieee8021x");
5490         else if (keymgmt & G_SUPPLICANT_KEYMGMT_WPA_FT_EAP)
5491                 sec_list = g_slist_prepend (sec_list, "ft_ieee8021x");
5492
5493         if (sec_list)
5494                 return sec_list;
5495
5496         if (keymgmt &
5497                         (G_SUPPLICANT_KEYMGMT_WPA_PSK |
5498                                         G_SUPPLICANT_KEYMGMT_WPA_PSK_256)) {
5499                 if (!g_strcmp0(enc_mode, "aes"))
5500                         sec_list = g_slist_prepend (sec_list, "rsn");
5501                 else if (!g_strcmp0(enc_mode, "tkip"))
5502                         sec_list = g_slist_prepend (sec_list, "psk");
5503                 else if (!g_strcmp0(enc_mode, "mixed")) {
5504                         sec_list = g_slist_prepend (sec_list, "psk");
5505                         sec_list = g_slist_prepend (sec_list, "rsn");
5506                 }
5507         }
5508
5509         if (keymgmt & G_SUPPLICANT_KEYMGMT_WPA_FT_PSK)
5510                 sec_list = g_slist_prepend (sec_list, "ft_psk");
5511
5512         if (keymgmt & G_SUPPLICANT_KEYMGMT_SAE)
5513                 sec_list = g_slist_prepend (sec_list, "sae");
5514         if (keymgmt & G_SUPPLICANT_KEYMGMT_FT_SAE)
5515                 sec_list = g_slist_prepend (sec_list, "ft_sae");
5516
5517         if (keymgmt & G_SUPPLICANT_KEYMGMT_OWE || owe_transition_mode)
5518                 sec_list = g_slist_prepend (sec_list, "owe");
5519         if (keymgmt & G_SUPPLICANT_KEYMGMT_DPP)
5520                 sec_list = g_slist_prepend (sec_list, "dpp");
5521
5522         if (sec_list)
5523                 return sec_list;
5524
5525         if (privacy)
5526                 sec_list = g_slist_prepend (sec_list, "wep");
5527         else
5528                 sec_list = g_slist_prepend (sec_list, "none");
5529
5530         return sec_list;
5531 }
5532 #endif
5533
5534 static void network_added(GSupplicantNetwork *supplicant_network)
5535 {
5536         struct connman_network *network;
5537         GSupplicantInterface *interface;
5538         struct wifi_data *wifi;
5539         struct wifi_network *network_data;
5540         const char *name, *identifier, *security, *group, *mode;
5541         const unsigned char *ssid;
5542         unsigned int ssid_len;
5543         bool wps;
5544         bool wps_pbc;
5545         bool wps_ready;
5546         bool wps_advertizing;
5547
5548 #if defined TIZEN_EXT
5549         bool owe_transition_mode;
5550         const unsigned char *transition_mode_ssid;
5551         const unsigned char *transition_mode_bssid;
5552         unsigned int transition_mode_ssid_len;
5553         unsigned int keymgmt;
5554         GSList *vsie_list = NULL;
5555         GSList *sec_list = NULL;
5556         const unsigned char *country_code;
5557         ieee80211_modes_e phy_mode;
5558 #endif
5559
5560         mode = g_supplicant_network_get_mode(supplicant_network);
5561         identifier = g_supplicant_network_get_identifier(supplicant_network);
5562 #if defined TIZEN_EXT
5563         if (!simplified_log)
5564 #endif
5565         DBG("%s", identifier);
5566
5567         if (!g_strcmp0(mode, "adhoc"))
5568                 return;
5569
5570 #if defined TIZEN_EXT_WIFI_MESH
5571         if (!g_strcmp0(mode, "mesh")) {
5572                 mesh_peer_added(supplicant_network);
5573                 return;
5574         }
5575 #endif
5576
5577         interface = g_supplicant_network_get_interface(supplicant_network);
5578 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5579         /*
5580          * Note: If supplicant interface's driver is wired then skip it,
5581          * because it meanti only for ethernet not Wi-Fi.
5582          */
5583         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5584                 return;
5585 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5586
5587         wifi = g_supplicant_interface_get_data(interface);
5588         name = g_supplicant_network_get_name(supplicant_network);
5589         security = g_supplicant_network_get_security(supplicant_network);
5590         group = g_supplicant_network_get_identifier(supplicant_network);
5591         wps = g_supplicant_network_get_wps(supplicant_network);
5592         wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
5593         wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
5594         wps_advertizing = g_supplicant_network_is_wps_advertizing(
5595                                                         supplicant_network);
5596
5597         if (!wifi)
5598                 return;
5599
5600         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
5601
5602         network = connman_device_get_network(wifi->device, identifier);
5603
5604         if (!network) {
5605                 network = connman_network_create(identifier,
5606                                                 CONNMAN_NETWORK_TYPE_WIFI);
5607                 if (!network)
5608                         return;
5609
5610                 connman_network_set_index(network, wifi->index);
5611
5612                 if (connman_device_add_network(wifi->device, network) < 0) {
5613                         connman_network_unref(network);
5614                         return;
5615                 }
5616
5617                 wifi->networks = g_slist_prepend(wifi->networks, network);
5618
5619                 network_data = g_new0(struct wifi_network, 1);
5620                 connman_network_set_data(network, network_data);
5621         }
5622
5623         network_data = connman_network_get_data(network);
5624         network_data->keymgmt =
5625                 g_supplicant_network_get_keymgmt(supplicant_network);
5626
5627         if (name && name[0] != '\0')
5628                 connman_network_set_name(network, name);
5629
5630         connman_network_set_blob(network, "WiFi.SSID",
5631                                                 ssid, ssid_len);
5632 #if defined TIZEN_EXT
5633         vsie_list = (GSList *)g_supplicant_network_get_wifi_vsie(supplicant_network);
5634         if (vsie_list)
5635                 connman_network_set_vsie_list(network, vsie_list);
5636         else
5637                 DBG("vsie_list is NULL");
5638         country_code = g_supplicant_network_get_countrycode(supplicant_network);
5639         connman_network_set_countrycode(network, country_code);
5640         phy_mode = g_supplicant_network_get_phy_mode(supplicant_network);
5641         connman_network_set_phy_mode(network, phy_mode);
5642
5643         if (g_strcmp0(security, "psk_sha256") == 0) {
5644                 connman_network_set_psk_sha256(network, true);
5645                 security = "psk";
5646         }
5647 #endif
5648         connman_network_set_string(network, "WiFi.Security", security);
5649         connman_network_set_strength(network,
5650                                 calculate_strength(supplicant_network));
5651         connman_network_set_bool(network, "WiFi.WPS", wps);
5652         connman_network_set_bool(network, "WiFi.WPSAdvertising",
5653                                 wps_advertizing);
5654
5655         if (wps) {
5656                 /* Is AP advertizing for WPS association?
5657                  * If so, we decide to use WPS by default */
5658                 if (wps_ready && wps_pbc &&
5659                                                 wps_advertizing)
5660 #if !defined TIZEN_EXT
5661                         connman_network_set_bool(network, "WiFi.UseWPS", true);
5662 #else
5663                         DBG("wps is activating by ap but ignore it.");
5664 #endif
5665         }
5666
5667         connman_network_set_frequency(network,
5668                         g_supplicant_network_get_frequency(supplicant_network));
5669
5670 #if defined TIZEN_EXT
5671         keymgmt = g_supplicant_network_get_keymgmt(supplicant_network);
5672         connman_network_set_bssid(network,
5673                         g_supplicant_network_get_bssid(supplicant_network));
5674         owe_transition_mode = (bool)g_supplicant_network_get_transition_mode(supplicant_network);
5675         connman_network_set_bool(network, "WiFi.TRANSITION_MODE", owe_transition_mode);
5676         if (owe_transition_mode) {
5677                 transition_mode_ssid = (unsigned char *)g_supplicant_network_get_transition_mode_ssid(supplicant_network, &transition_mode_ssid_len);
5678                 connman_network_set_blob(network, "WiFi.TRANSITION_MODE_SSID",
5679                                                         transition_mode_ssid, transition_mode_ssid_len);
5680                 transition_mode_bssid = g_supplicant_network_get_transition_mode_bssid(supplicant_network);
5681                 connman_network_set_transition_mode_bssid(network, transition_mode_bssid);
5682         }
5683
5684         sec_list = get_supported_security_list(keymgmt,
5685                         owe_transition_mode, supplicant_network);
5686
5687         connman_network_set_sec_list(network, sec_list);
5688         connman_network_set_maxrate(network,
5689                         g_supplicant_network_get_maxrate(supplicant_network));
5690         connman_network_set_enc_mode(network,
5691                         g_supplicant_network_get_enc_mode(supplicant_network));
5692         connman_network_set_rsn_mode(network,
5693                         g_supplicant_network_get_rsn_mode(supplicant_network));
5694         connman_network_set_bool(network, "WiFi.PMFRequired",
5695                         (bool)g_supplicant_network_is_pmf_required(supplicant_network));
5696         connman_network_set_keymgmt(network, keymgmt);
5697         connman_network_set_bool(network, "WiFi.HS20AP",
5698                         g_supplicant_network_is_hs20AP(supplicant_network));
5699         connman_network_set_bssid_list(network,
5700                         (GSList *)g_supplicant_network_get_bssid_list(supplicant_network));
5701
5702         if (TIZEN_INS_ENABLED) {
5703                 connman_network_set_last_connected_bssid(network,
5704                                 g_supplicant_network_get_last_connected_bssid(supplicant_network));
5705                 connman_network_set_assoc_reject_table(network,
5706                                 g_supplicant_network_clone_assoc_reject_table(supplicant_network));
5707         }
5708 #endif
5709
5710         connman_network_set_available(network, true);
5711         connman_network_set_string(network, "WiFi.Mode", mode);
5712
5713 #if defined TIZEN_EXT
5714         if (group)
5715 #else
5716         if (ssid)
5717 #endif
5718                 connman_network_set_group(network, group);
5719
5720 #if defined TIZEN_EXT
5721         if (TIZEN_INS_ENABLED)
5722                 g_supplicant_network_set_last_connected_bssid(supplicant_network,
5723                                 connman_network_get_last_connected_bssid(network));
5724
5725         if (wifi_first_scan == true)
5726                 found_with_first_scan = true;
5727 #endif
5728
5729         if (wifi->hidden && ssid) {
5730 #if defined TIZEN_EXT
5731                 if (network_security(wifi->hidden->security) ==
5732                         network_security(security) &&
5733 #else
5734                 if (!g_strcmp0(wifi->hidden->security, security) &&
5735 #endif
5736                                 wifi->hidden->ssid_len == ssid_len &&
5737                                 !memcmp(wifi->hidden->ssid, ssid, ssid_len)) {
5738                         connman_network_connect_hidden(network,
5739                                         wifi->hidden->identity,
5740                                         wifi->hidden->passphrase,
5741                                         wifi->hidden->user_data);
5742                         wifi->hidden->user_data = NULL;
5743                         hidden_free(wifi->hidden);
5744                         wifi->hidden = NULL;
5745                 }
5746         }
5747 }
5748
5749 static void network_removed(GSupplicantNetwork *network)
5750 {
5751         GSupplicantInterface *interface;
5752         struct wifi_data *wifi;
5753         const char *name, *identifier;
5754         struct connman_network *connman_network;
5755
5756 #if defined TIZEN_EXT_WIFI_MESH
5757         const char *mode;
5758         mode = g_supplicant_network_get_mode(network);
5759         if (!g_strcmp0(mode, "mesh")) {
5760                 mesh_peer_removed(network);
5761                 return;
5762         }
5763 #endif
5764
5765         interface = g_supplicant_network_get_interface(network);
5766 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5767         /*
5768          * Note: If supplicant interface's driver is wired then skip it,
5769          * because it meanti only for ethernet not Wi-Fi.
5770          */
5771         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5772                 return;
5773 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5774
5775         wifi = g_supplicant_interface_get_data(interface);
5776         identifier = g_supplicant_network_get_identifier(network);
5777         name = g_supplicant_network_get_name(network);
5778
5779         DBG("name %s", name);
5780
5781         if (!wifi)
5782                 return;
5783
5784         connman_network = connman_device_get_network(wifi->device, identifier);
5785         if (!connman_network)
5786                 return;
5787
5788 #if defined TIZEN_EXT
5789         if (connman_network == wifi->scan_pending_network)
5790                 wifi->scan_pending_network = NULL;
5791
5792         if (connman_network == wifi->pending_network)
5793                 wifi->pending_network = NULL;
5794
5795         if(connman_network_get_connecting(connman_network) == true){
5796                 connman_network_set_connected(connman_network, false);
5797         }
5798 #endif
5799
5800         wifi->networks = g_slist_remove(wifi->networks, connman_network);
5801
5802         g_free(connman_network_get_data(connman_network));
5803         connman_device_remove_network(wifi->device, connman_network);
5804         connman_network_unref(connman_network);
5805 }
5806
5807 static void network_changed(GSupplicantNetwork *network, const char *property)
5808 {
5809         GSupplicantInterface *interface;
5810         struct wifi_data *wifi;
5811         const char *name, *identifier;
5812         struct connman_network *connman_network;
5813         bool update_needed;
5814
5815 #if defined TIZEN_EXT
5816         const unsigned char *bssid;
5817         unsigned int maxrate;
5818         uint16_t frequency;
5819         bool wps;
5820         const unsigned char *country_code;
5821         ieee80211_modes_e phy_mode;
5822         GSList *bssid_list;
5823 #endif
5824
5825         interface = g_supplicant_network_get_interface(network);
5826 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5827         /*
5828          * Note: If supplicant interface's driver is wired then skip it,
5829          * because it meanti only for ethernet not Wi-Fi.
5830          */
5831         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5832                 return;
5833 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5834
5835         wifi = g_supplicant_interface_get_data(interface);
5836         identifier = g_supplicant_network_get_identifier(network);
5837         name = g_supplicant_network_get_name(network);
5838
5839 #if defined TIZEN_EXT
5840         if (!simplified_log)
5841                 DBG("name %s property %s", name, property);
5842 #else
5843         DBG("name %s", name);
5844 #endif
5845
5846         if (!wifi)
5847                 return;
5848
5849         connman_network = connman_device_get_network(wifi->device, identifier);
5850         if (!connman_network)
5851                 return;
5852
5853         if (g_str_equal(property, "WPSCapabilities")) {
5854                 bool wps;
5855                 bool wps_pbc;
5856                 bool wps_ready;
5857                 bool wps_advertizing;
5858
5859                 wps = g_supplicant_network_get_wps(network);
5860                 wps_pbc = g_supplicant_network_is_wps_pbc(network);
5861                 wps_ready = g_supplicant_network_is_wps_active(network);
5862                 wps_advertizing =
5863                         g_supplicant_network_is_wps_advertizing(network);
5864
5865                 connman_network_set_bool(connman_network, "WiFi.WPS", wps);
5866                 connman_network_set_bool(connman_network,
5867                                 "WiFi.WPSAdvertising", wps_advertizing);
5868
5869                 if (wps) {
5870                         /*
5871                          * Is AP advertizing for WPS association?
5872                          * If so, we decide to use WPS by default
5873                          */
5874                         if (wps_ready && wps_pbc && wps_advertizing)
5875                                 connman_network_set_bool(connman_network,
5876                                                         "WiFi.UseWPS", true);
5877                 }
5878
5879                 update_needed = true;
5880         } else if (g_str_equal(property, "Signal")) {
5881                 connman_network_set_strength(connman_network,
5882                                         calculate_strength(network));
5883                 update_needed = true;
5884         }
5885 #if defined TIZEN_EXT
5886         else if (TIZEN_INS_ENABLED) {
5887                 if (g_str_equal(property, "LastConnectedBSSID")) {
5888                         const char *ident, *group;
5889                         char *service_ident;
5890                         bool need_save;
5891
5892                         ident = connman_device_get_ident(wifi->device);
5893                         group = connman_network_get_group(connman_network);
5894                         if (ident && group) {
5895                                 service_ident = g_strdup_printf("%s_%s_%s",
5896                                                 __connman_network_get_type(connman_network), ident, group);
5897
5898                                 need_save = connman_device_set_last_connected_ident(wifi->device, service_ident);
5899                                 if (need_save)
5900                                         connman_device_save_last_connected(wifi->device);
5901
5902                                 g_free(service_ident);
5903                         }
5904
5905                         connman_network_set_last_connected_bssid(connman_network,
5906                                         g_supplicant_network_get_last_connected_bssid(network));
5907
5908                         update_needed = true;
5909                 } else if (g_str_equal(property, "UpdateAssocReject")) {
5910                         connman_network_set_assoc_reject_table(connman_network,
5911                                         g_supplicant_network_clone_assoc_reject_table(network));
5912                         update_needed = true;
5913                 } else {
5914                         update_needed = false;
5915                 }
5916         }
5917 #endif /* defined TIZEN_EXT */
5918         else
5919                 update_needed = false;
5920
5921         if (update_needed)
5922                 connman_network_update(connman_network);
5923
5924 #if defined TIZEN_EXT
5925         bssid = g_supplicant_network_get_bssid(network);
5926         maxrate = g_supplicant_network_get_maxrate(network);
5927         frequency = g_supplicant_network_get_frequency(network);
5928         wps = g_supplicant_network_get_wps(network);
5929         phy_mode = g_supplicant_network_get_phy_mode(network);
5930
5931         connman_network_set_bssid(connman_network, bssid);
5932         connman_network_set_maxrate(connman_network, maxrate);
5933         connman_network_set_frequency(connman_network, frequency);
5934         connman_network_set_bool(connman_network, "WiFi.WPS", wps);
5935         country_code = g_supplicant_network_get_countrycode(network);
5936         connman_network_set_countrycode(connman_network, country_code);
5937         bssid_list = (GSList *)g_supplicant_network_get_bssid_list(network);
5938         connman_network_set_bssid_list(connman_network, bssid_list);
5939         connman_network_set_phy_mode(connman_network, phy_mode);
5940
5941         if (g_str_equal(property, "CheckMultiBssidConnect") &&
5942                         connman_network_get_associating(connman_network))
5943                 network_connect(connman_network);
5944 #endif
5945 }
5946
5947 static void network_associated(GSupplicantNetwork *network)
5948 {
5949         GSupplicantInterface *interface;
5950         struct wifi_data *wifi;
5951         struct connman_network *connman_network;
5952         const char *identifier;
5953
5954         DBG("");
5955
5956         interface = g_supplicant_network_get_interface(network);
5957         if (!interface)
5958                 return;
5959 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5960         /*
5961          * Note: If supplicant interface's driver is wired then skip it,
5962          * because it meanti only for ethernet not Wi-Fi.
5963          */
5964         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5965                 return;
5966 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5967
5968
5969         wifi = g_supplicant_interface_get_data(interface);
5970         if (!wifi)
5971                 return;
5972
5973         /* P2P networks must not be treated as WiFi networks */
5974         if (wifi->p2p_connecting || wifi->p2p_device)
5975                 return;
5976
5977         identifier = g_supplicant_network_get_identifier(network);
5978
5979         connman_network = connman_device_get_network(wifi->device, identifier);
5980         if (!connman_network)
5981                 return;
5982
5983         if (wifi->network) {
5984                 if (wifi->network == connman_network)
5985                         return;
5986 #if TIZEN_EXT
5987                 unsigned int ssid_len;
5988                 DBG("network1 ssid[%s] , OWE[%d],ssid[%s]",
5989                         (char *)connman_network_get_blob(wifi->network,"WiFi.SSID", &ssid_len),
5990                         connman_network_get_bool(wifi->network,"WiFi.TRANSITION_MODE"),
5991                         (char *)connman_network_get_blob(wifi->network,"WiFi.TRANSITION_MODE_SSID", &ssid_len));
5992
5993                 DBG("network1 ssid[%s], OWE[%d], ssid[%s]",
5994                         (char *)connman_network_get_blob(connman_network,"WiFi.SSID",&ssid_len),
5995                         connman_network_get_bool(connman_network,"WiFi.TRANSITION_MODE"),
5996                         (char *)connman_network_get_blob(connman_network,"WiFi.TRANSITION_MODE_SSID", &ssid_len));
5997                 if (connman_network_check_transition_mode(wifi->network, connman_network)) {//OWE trasition mode check
5998                         DBG("OWE transition mode is TRUE");
5999                         return;
6000                 }
6001 #endif
6002                 /*
6003                  * This should never happen, we got associated with
6004                  * a network different than the one we were expecting.
6005                  */
6006                 DBG("Associated to %p while expecting %p",
6007                                         connman_network, wifi->network);
6008
6009                 connman_network_set_associating(wifi->network, false);
6010         }
6011
6012         DBG("Reconnecting to previous network %p from wpa_s", connman_network);
6013
6014         wifi->network = connman_network_ref(connman_network);
6015         wifi->retries = 0;
6016
6017         /*
6018          * Interface state changes callback (interface_state) is always
6019          * called before network_associated callback thus we need to call
6020          * interface_state again in order to process the new state now that
6021          * we have the network properly set.
6022          */
6023         interface_state(interface);
6024 }
6025
6026 static void sta_authorized(GSupplicantInterface *interface,
6027                                         const char *addr)
6028 {
6029 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6030         /*
6031          * Note: If supplicant interface's driver is wired then skip it,
6032          * because it meanti only for ethernet not Wi-Fi.
6033          */
6034         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
6035                 return;
6036 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6037
6038         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
6039
6040         DBG("wifi %p station %s authorized", wifi, addr);
6041
6042         if (!wifi || !wifi->tethering)
6043                 return;
6044
6045         __connman_tethering_client_register(addr);
6046 }
6047
6048 static void sta_deauthorized(GSupplicantInterface *interface,
6049                                         const char *addr)
6050 {
6051 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6052         /*
6053          * Note: If supplicant interface's driver is wired then skip it,
6054          * because it meanti only for ethernet not Wi-Fi.
6055          */
6056         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
6057                 return;
6058 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6059
6060         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
6061
6062         DBG("wifi %p station %s deauthorized", wifi, addr);
6063
6064         if (!wifi || !wifi->tethering)
6065                 return;
6066
6067         __connman_tethering_client_unregister(addr);
6068 }
6069
6070 static void apply_peer_services(GSupplicantPeer *peer,
6071                                 struct connman_peer *connman_peer)
6072 {
6073         const unsigned char *data;
6074         int length;
6075
6076         DBG("");
6077
6078         connman_peer_reset_services(connman_peer);
6079
6080         data = g_supplicant_peer_get_widi_ies(peer, &length);
6081         if (data) {
6082                 connman_peer_add_service(connman_peer,
6083                         CONNMAN_PEER_SERVICE_WIFI_DISPLAY, data, length);
6084         }
6085 }
6086
6087 static void peer_found(GSupplicantPeer *peer)
6088 {
6089         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
6090         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
6091         struct connman_peer *connman_peer;
6092         const char *identifier, *name;
6093         int ret;
6094
6095 #if defined TIZEN_EXT
6096         if (!wifi)
6097                 return;
6098 #endif
6099         identifier = g_supplicant_peer_get_identifier(peer);
6100         name = g_supplicant_peer_get_name(peer);
6101
6102         DBG("ident: %s", identifier);
6103
6104         connman_peer = connman_peer_get(wifi->device, identifier);
6105         if (connman_peer)
6106                 return;
6107
6108         connman_peer = connman_peer_create(identifier);
6109         connman_peer_set_name(connman_peer, name);
6110         connman_peer_set_device(connman_peer, wifi->device);
6111         apply_peer_services(peer, connman_peer);
6112
6113         ret = connman_peer_register(connman_peer);
6114         if (ret < 0 && ret != -EALREADY)
6115                 connman_peer_unref(connman_peer);
6116         else
6117                 wifi->peers = g_slist_prepend(wifi->peers, connman_peer);
6118 }
6119
6120 static void peer_lost(GSupplicantPeer *peer)
6121 {
6122         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
6123         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
6124         struct connman_peer *connman_peer;
6125         const char *identifier;
6126
6127         if (!wifi)
6128                 return;
6129
6130         identifier = g_supplicant_peer_get_identifier(peer);
6131
6132         DBG("ident: %s", identifier);
6133
6134         connman_peer = connman_peer_get(wifi->device, identifier);
6135         if (connman_peer) {
6136                 if (wifi->p2p_connecting &&
6137                                 wifi->pending_peer == connman_peer) {
6138                         peer_connect_timeout(wifi);
6139                 }
6140                 connman_peer_unregister(connman_peer);
6141                 connman_peer_unref(connman_peer);
6142         }
6143
6144         wifi->peers = g_slist_remove(wifi->peers, connman_peer);
6145 }
6146
6147 static void peer_changed(GSupplicantPeer *peer, GSupplicantPeerState state)
6148 {
6149         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
6150         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
6151         enum connman_peer_state p_state = CONNMAN_PEER_STATE_UNKNOWN;
6152         struct connman_peer *connman_peer;
6153         const char *identifier;
6154
6155         identifier = g_supplicant_peer_get_identifier(peer);
6156
6157         DBG("ident: %s", identifier);
6158
6159         if (!wifi)
6160                 return;
6161
6162         connman_peer = connman_peer_get(wifi->device, identifier);
6163         if (!connman_peer)
6164                 return;
6165
6166         switch (state) {
6167         case G_SUPPLICANT_PEER_SERVICES_CHANGED:
6168                 apply_peer_services(peer, connman_peer);
6169                 connman_peer_services_changed(connman_peer);
6170                 return;
6171         case G_SUPPLICANT_PEER_GROUP_CHANGED:
6172                 if (!g_supplicant_peer_is_in_a_group(peer))
6173                         p_state = CONNMAN_PEER_STATE_IDLE;
6174                 else
6175                         p_state = CONNMAN_PEER_STATE_CONFIGURATION;
6176                 break;
6177         case G_SUPPLICANT_PEER_GROUP_STARTED:
6178                 break;
6179         case G_SUPPLICANT_PEER_GROUP_FINISHED:
6180                 p_state = CONNMAN_PEER_STATE_IDLE;
6181                 break;
6182         case G_SUPPLICANT_PEER_GROUP_JOINED:
6183                 connman_peer_set_iface_address(connman_peer,
6184                                 g_supplicant_peer_get_iface_address(peer));
6185                 break;
6186         case G_SUPPLICANT_PEER_GROUP_DISCONNECTED:
6187                 p_state = CONNMAN_PEER_STATE_IDLE;
6188                 break;
6189         case G_SUPPLICANT_PEER_GROUP_FAILED:
6190                 if (g_supplicant_peer_has_requested_connection(peer))
6191                         p_state = CONNMAN_PEER_STATE_IDLE;
6192                 else
6193                         p_state = CONNMAN_PEER_STATE_FAILURE;
6194                 break;
6195         }
6196
6197         if (p_state == CONNMAN_PEER_STATE_CONFIGURATION ||
6198                                         p_state == CONNMAN_PEER_STATE_FAILURE) {
6199                 if (wifi->p2p_connecting
6200                                 && connman_peer == wifi->pending_peer)
6201                         peer_cancel_timeout(wifi);
6202                 else
6203                         p_state = CONNMAN_PEER_STATE_UNKNOWN;
6204         }
6205
6206         if (p_state == CONNMAN_PEER_STATE_UNKNOWN)
6207                 return;
6208
6209         if (p_state == CONNMAN_PEER_STATE_CONFIGURATION) {
6210                 GSupplicantInterface *g_iface;
6211                 struct wifi_data *g_wifi;
6212
6213                 g_iface = g_supplicant_peer_get_group_interface(peer);
6214                 if (!g_iface)
6215                         return;
6216
6217                 g_wifi = g_supplicant_interface_get_data(g_iface);
6218                 if (!g_wifi)
6219                         return;
6220
6221                 connman_peer_set_as_master(connman_peer,
6222                                         !g_supplicant_peer_is_client(peer));
6223                 connman_peer_set_sub_device(connman_peer, g_wifi->device);
6224
6225                 /*
6226                  * If wpa_supplicant didn't create a dedicated p2p-group
6227                  * interface then mark this interface as p2p_device to avoid
6228                  * scan and auto-scan are launched on it while P2P is connected.
6229                  */
6230                 if (!g_list_find(p2p_iface_list, g_wifi))
6231                         wifi->p2p_device = true;
6232         }
6233
6234         connman_peer_set_state(connman_peer, p_state);
6235 }
6236
6237 static void peer_request(GSupplicantPeer *peer)
6238 {
6239         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
6240         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
6241         struct connman_peer *connman_peer;
6242         const char *identifier;
6243
6244 #if defined TIZEN_EXT
6245         if (!wifi)
6246                 return;
6247 #endif
6248
6249         identifier = g_supplicant_peer_get_identifier(peer);
6250
6251         DBG("ident: %s", identifier);
6252
6253         connman_peer = connman_peer_get(wifi->device, identifier);
6254         if (!connman_peer)
6255                 return;
6256
6257         connman_peer_request_connection(connman_peer);
6258 }
6259
6260 #if defined TIZEN_EXT
6261 static void system_power_off(void)
6262 {
6263         GList *list;
6264         struct wifi_data *wifi;
6265         struct connman_service *service;
6266         struct connman_ipconfig *ipconfig_ipv4;
6267
6268         if (connman_setting_get_bool("WiFiDHCPRelease") == true) {
6269                 for (list = iface_list; list; list = list->next) {
6270                         wifi = list->data;
6271
6272                         if (wifi->network != NULL) {
6273                                 service = connman_service_lookup_from_network(wifi->network);
6274                                 ipconfig_ipv4 = __connman_service_get_ip4config(service);
6275                                 __connman_dhcp_stop(ipconfig_ipv4);
6276                         }
6277                 }
6278         }
6279 }
6280
6281 static void network_merged(GSupplicantNetwork *network)
6282 {
6283         GSupplicantInterface *interface;
6284         GSupplicantState state;
6285         struct wifi_data *wifi;
6286         const char *identifier;
6287         struct connman_network *connman_network;
6288         bool ishs20AP = 0;
6289         char *temp = NULL;
6290
6291         interface = g_supplicant_network_get_interface(network);
6292         if (!interface)
6293                 return;
6294
6295 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6296         /*
6297          * Note: If supplicant interface's driver is wired then skip it,
6298          * because it meanti only for ethernet not Wi-Fi.
6299          */
6300         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
6301                 return;
6302 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6303
6304         state = g_supplicant_interface_get_state(interface);
6305         if (state < G_SUPPLICANT_STATE_AUTHENTICATING)
6306                 return;
6307
6308         wifi = g_supplicant_interface_get_data(interface);
6309         if (!wifi)
6310                 return;
6311
6312         identifier = g_supplicant_network_get_identifier(network);
6313
6314         connman_network = connman_device_get_network(wifi->device, identifier);
6315         if (!connman_network)
6316                 return;
6317
6318         DBG("merged identifier %s", identifier);
6319
6320         if (wifi->connected == FALSE) {
6321                 switch (state) {
6322                 case G_SUPPLICANT_STATE_AUTHENTICATING:
6323                 case G_SUPPLICANT_STATE_ASSOCIATING:
6324                 case G_SUPPLICANT_STATE_ASSOCIATED:
6325                 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
6326                 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
6327                         connman_network_set_associating(connman_network, TRUE);
6328                         break;
6329                 case G_SUPPLICANT_STATE_COMPLETED:
6330                         connman_network_set_connected(connman_network, TRUE);
6331                         break;
6332                 default:
6333                         DBG("Not handled the state : %d", state);
6334                         break;
6335                 }
6336         }
6337
6338         ishs20AP = g_supplicant_network_is_hs20AP(network);
6339
6340         if (ishs20AP &&
6341                 g_strcmp0(g_supplicant_network_get_security(network), "ieee8021x") == 0) {
6342                 temp = g_ascii_strdown(g_supplicant_network_get_eap(network), -1);
6343                 connman_network_set_string(connman_network, "WiFi.EAP",
6344                                 temp);
6345                 connman_network_set_string(connman_network, "WiFi.Identity",
6346                                 g_supplicant_network_get_identity(network));
6347                 connman_network_set_string(connman_network, "WiFi.Phase2",
6348                                 g_supplicant_network_get_phase2(network));
6349
6350                 g_free(temp);
6351         }
6352
6353         wifi->network = connman_network;
6354 }
6355
6356 static void assoc_failed(void *user_data)
6357 {
6358         struct connman_network *network = user_data;
6359         connman_network_set_associating(network, false);
6360 }
6361
6362 static void scan_done(GSupplicantInterface *interface)
6363 {
6364 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6365         /*
6366          * Note: If supplicant interface's driver is wired then skip it,
6367          * because it meanti only for ethernet not Wi-Fi.
6368          */
6369         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
6370                 return;
6371 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6372
6373         GList *list;
6374         int scan_type = CONNMAN_SCAN_TYPE_WPA_SUPPLICANT;
6375         struct wifi_data *wifi;
6376         bool scanning;
6377
6378         for (list = iface_list; list; list = list->next) {
6379                 wifi = list->data;
6380
6381                 if (interface == wifi->interface) {
6382                         scanning = connman_device_get_scanning(wifi->device,
6383                                         CONNMAN_SERVICE_TYPE_WIFI);
6384                         if (!scanning)
6385                                 __connman_technology_notify_scan_done(
6386                                                 connman_device_get_string(wifi->device, "Interface"), scan_type);
6387                         break;
6388                 }
6389         }
6390 }
6391 #endif
6392
6393 static void debug(const char *str)
6394 {
6395 #if defined TIZEN_EXT
6396         if (connman_setting_get_bool("ConnmanSupplicantDebug"))
6397 #else
6398         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
6399 #endif
6400                 connman_debug("%s", str);
6401 }
6402
6403 static void disconnect_reasoncode(GSupplicantInterface *interface,
6404                                 int reasoncode)
6405 {
6406 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6407         /*
6408          * Note: If supplicant interface's driver is wired then skip it,
6409          * because it meanti only for ethernet not Wi-Fi.
6410          */
6411         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
6412                 return;
6413 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6414
6415         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
6416
6417         if (wifi != NULL) {
6418                 wifi->disconnect_code = reasoncode;
6419         }
6420 }
6421
6422 static void assoc_status_code(GSupplicantInterface *interface, int status_code)
6423 {
6424 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6425         /*
6426          * Note: If supplicant interface's driver is wired then skip it,
6427          * because it meanti only for ethernet not Wi-Fi.
6428          */
6429         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
6430                 return;
6431 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6432
6433         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
6434
6435         if (wifi != NULL) {
6436                 wifi->assoc_code = status_code;
6437         }
6438 }
6439
6440 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6441 static GSupplicantCallbacks callbacks = {
6442 #else /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6443 static const GSupplicantCallbacks callbacks = {
6444 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6445         .system_ready           = system_ready,
6446         .system_killed          = system_killed,
6447         .interface_added        = interface_added,
6448         .interface_state        = interface_state,
6449         .interface_removed      = interface_removed,
6450         .p2p_support            = p2p_support,
6451         .scan_started           = scan_started,
6452         .scan_finished          = scan_finished,
6453         .ap_create_fail         = ap_create_fail,
6454         .network_added          = network_added,
6455         .network_removed        = network_removed,
6456         .network_changed        = network_changed,
6457         .network_associated     = network_associated,
6458         .sta_authorized         = sta_authorized,
6459         .sta_deauthorized       = sta_deauthorized,
6460         .peer_found             = peer_found,
6461         .peer_lost              = peer_lost,
6462         .peer_changed           = peer_changed,
6463         .peer_request           = peer_request,
6464 #if defined TIZEN_EXT
6465         .system_power_off       = system_power_off,
6466         .network_merged         = network_merged,
6467         .assoc_failed           = assoc_failed,
6468         .scan_done              = scan_done,
6469 #endif
6470         .debug                  = debug,
6471         .disconnect_reasoncode  = disconnect_reasoncode,
6472         .assoc_status_code      = assoc_status_code,
6473 #if defined TIZEN_EXT_WIFI_MESH
6474         .mesh_support           = mesh_support,
6475         .mesh_group_started = mesh_group_started,
6476         .mesh_group_removed = mesh_group_removed,
6477         .mesh_peer_connected = mesh_peer_connected,
6478         .mesh_peer_disconnected = mesh_peer_disconnected,
6479 #endif
6480 };
6481
6482
6483 static int tech_probe(struct connman_technology *technology)
6484 {
6485         wifi_technology = technology;
6486
6487         return 0;
6488 }
6489
6490 static void tech_remove(struct connman_technology *technology)
6491 {
6492         wifi_technology = NULL;
6493 }
6494
6495 static GSupplicantSSID *ssid_ap_init(const struct connman_technology *technology)
6496 {
6497         GSupplicantSSID *ap;
6498         const char *ssid, *passphrase;
6499         int freq;
6500         bool ret;
6501
6502         ap = g_try_malloc0(sizeof(GSupplicantSSID));
6503         if (!ap)
6504                 return NULL;
6505
6506         ret = connman_technology_get_wifi_tethering(technology,
6507                                                 &ssid, &passphrase,
6508                                                 &freq);
6509         if (ret == false) {
6510 #if defined TIZEN_EXT
6511                 g_free(ap);
6512 #endif
6513                 return NULL;
6514         }
6515
6516         ap->mode = G_SUPPLICANT_MODE_MASTER;
6517 #if defined TIZEN_EXT
6518         ap->ssid = (void *) ssid;
6519 #else
6520         ap->ssid = ssid;
6521 #endif
6522         ap->ssid_len = strlen(ssid);
6523         ap->scan_ssid = 0;
6524         if (freq)
6525                 ap->freq = freq;
6526         else
6527                 ap->freq = 2412;
6528
6529         if (!passphrase || strlen(passphrase) == 0) {
6530                 ap->security = G_SUPPLICANT_SECURITY_NONE;
6531                 ap->passphrase = NULL;
6532         } else {
6533                ap->security = G_SUPPLICANT_SECURITY_PSK;
6534                ap->protocol = G_SUPPLICANT_PROTO_RSN;
6535                ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
6536                ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
6537                ap->passphrase = passphrase;
6538         }
6539
6540         return ap;
6541 }
6542
6543 static void ap_start_callback(int result, GSupplicantInterface *interface,
6544                                                         void *user_data)
6545 {
6546         struct wifi_tethering_info *info = user_data;
6547
6548         DBG("result %d index %d bridge %s",
6549                 result, info->wifi->index, info->wifi->bridge);
6550
6551         if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
6552                 connman_inet_remove_from_bridge(info->wifi->index,
6553                                                         info->wifi->bridge);
6554
6555                 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
6556                         connman_technology_tethering_notify(info->technology, false);
6557                         g_free(info->wifi->tethering_param->ssid);
6558                         g_free(info->wifi->tethering_param);
6559                         info->wifi->tethering_param = NULL;
6560                 }
6561         }
6562
6563         g_free(info->ifname);
6564         g_free(info);
6565 }
6566
6567 static void ap_create_callback(int result,
6568                                 GSupplicantInterface *interface,
6569                                         void *user_data)
6570 {
6571         struct wifi_tethering_info *info = user_data;
6572
6573         DBG("result %d ifname %s", result,
6574                                 g_supplicant_interface_get_ifname(interface));
6575
6576         if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
6577                 connman_inet_remove_from_bridge(info->wifi->index,
6578                                                         info->wifi->bridge);
6579
6580                 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
6581                         connman_technology_tethering_notify(info->technology, false);
6582                         g_free(info->wifi->tethering_param->ssid);
6583                         g_free(info->wifi->tethering_param);
6584                         info->wifi->tethering_param = NULL;
6585
6586                 }
6587
6588                 g_free(info->ifname);
6589                 g_free(info->ssid);
6590                 g_free(info);
6591                 return;
6592         }
6593
6594         info->wifi->interface = interface;
6595         g_supplicant_interface_set_data(interface, info->wifi);
6596
6597         if (g_supplicant_interface_set_apscan(interface, 2) < 0)
6598                 connman_error("Failed to set interface ap_scan property");
6599
6600         g_supplicant_interface_connect(interface, info->ssid,
6601                                                 ap_start_callback, info);
6602 }
6603
6604 static void sta_remove_callback(int result,
6605                                 GSupplicantInterface *interface,
6606                                         void *user_data)
6607 {
6608         struct wifi_tethering_info *info = user_data;
6609         const char *driver = connman_setting_get_string("wifi");
6610
6611         DBG("ifname %s result %d ", info->ifname, result);
6612
6613         if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
6614                 info->wifi->tethering = false;
6615                 connman_technology_tethering_notify(info->technology, false);
6616
6617                 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
6618                         g_free(info->wifi->tethering_param->ssid);
6619                         g_free(info->wifi->tethering_param);
6620                         info->wifi->tethering_param = NULL;
6621                 }
6622
6623                 g_free(info->ifname);
6624                 g_free(info->ssid);
6625                 g_free(info);
6626                 return;
6627         }
6628
6629         info->wifi->interface = NULL;
6630
6631         g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
6632 #ifdef TIZEN_EXT
6633                         0, 0, 60,
6634 #endif /* TIZEN_EXT */
6635                                                 ap_create_callback,
6636                                                         info);
6637 }
6638
6639 static int enable_wifi_tethering(struct connman_technology *technology,
6640                                 const char *bridge, bool available)
6641 {
6642         GList *list;
6643         GSupplicantInterface *interface;
6644         struct wifi_data *wifi;
6645         struct wifi_tethering_info *info;
6646         const char *ifname;
6647         unsigned int mode;
6648         int err, berr = 0;
6649
6650         for (list = iface_list; list; list = list->next) {
6651                 wifi = list->data;
6652
6653                 DBG("wifi %p network %p pending_network %p", wifi,
6654                         wifi->network, wifi->pending_network);
6655
6656                 interface = wifi->interface;
6657
6658                 if (!interface)
6659                         continue;
6660
6661                 ifname = g_supplicant_interface_get_ifname(wifi->interface);
6662                 if (!ifname)
6663                         continue;
6664
6665                 if (wifi->ap_supported == WIFI_AP_NOT_SUPPORTED) {
6666                         DBG("%s does not support AP mode (detected)", ifname);
6667                         continue;
6668                 }
6669
6670                 mode = g_supplicant_interface_get_mode(interface);
6671                 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
6672                         wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
6673                         DBG("%s does not support AP mode (capability)", ifname);
6674                         continue;
6675                 }
6676
6677                 if (wifi->network && available)
6678                         continue;
6679
6680                 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
6681                 if (!info)
6682                         return -ENOMEM;
6683
6684                 wifi->tethering_param = g_try_malloc0(sizeof(struct wifi_tethering_info));
6685                 if (!wifi->tethering_param) {
6686                         g_free(info);
6687                         return -ENOMEM;
6688                 }
6689
6690                 info->wifi = wifi;
6691                 info->technology = technology;
6692                 info->wifi->bridge = bridge;
6693                 info->ssid = ssid_ap_init(technology);
6694                 if (!info->ssid)
6695                         goto failed;
6696
6697                 info->ifname = g_strdup(ifname);
6698
6699                 wifi->tethering_param->technology = technology;
6700                 wifi->tethering_param->ssid = ssid_ap_init(technology);
6701                 if (!wifi->tethering_param->ssid)
6702                         goto failed;
6703
6704                 info->wifi->tethering = true;
6705                 info->wifi->ap_supported = WIFI_AP_SUPPORTED;
6706
6707                 berr = connman_technology_tethering_notify(technology, true);
6708                 if (berr < 0)
6709                         goto failed;
6710
6711                 err = g_supplicant_interface_remove(interface,
6712                                                 sta_remove_callback,
6713                                                         info);
6714                 if (err >= 0) {
6715                         DBG("tethering wifi %p ifname %s", wifi, ifname);
6716                         return 0;
6717                 }
6718
6719         failed:
6720                 g_free(info->ifname);
6721                 g_free(info->ssid);
6722                 g_free(info);
6723                 g_free(wifi->tethering_param);
6724                 wifi->tethering_param = NULL;
6725
6726                 /*
6727                  * Remove bridge if it was correctly created but remove
6728                  * operation failed. Instead, if bridge creation failed then
6729                  * break out and do not try again on another interface,
6730                  * bridge set-up does not depend on it.
6731                  */
6732                 if (berr == 0)
6733                         connman_technology_tethering_notify(technology, false);
6734                 else
6735                         break;
6736         }
6737
6738         return -EOPNOTSUPP;
6739 }
6740
6741 static int tech_set_tethering(struct connman_technology *technology,
6742                                 const char *bridge, bool enabled)
6743 {
6744         GList *list;
6745         struct wifi_data *wifi;
6746         int err;
6747
6748         DBG("");
6749
6750         if (!enabled) {
6751                 for (list = iface_list; list; list = list->next) {
6752                         wifi = list->data;
6753
6754                         if (wifi->tethering) {
6755                                 wifi->tethering = false;
6756
6757                                 connman_inet_remove_from_bridge(wifi->index,
6758                                                                         bridge);
6759                                 wifi->bridged = false;
6760                         }
6761                 }
6762
6763                 connman_technology_tethering_notify(technology, false);
6764
6765                 return 0;
6766         }
6767
6768         DBG("trying tethering for available devices");
6769         err = enable_wifi_tethering(technology, bridge, true);
6770
6771         if (err < 0) {
6772                 DBG("trying tethering for any device");
6773                 err = enable_wifi_tethering(technology, bridge, false);
6774         }
6775
6776         return err;
6777 }
6778
6779 static void regdom_callback(int result, const char *alpha2, void *user_data)
6780 {
6781         DBG("");
6782
6783         if (!wifi_technology)
6784                 return;
6785
6786         if (result != 0)
6787                 alpha2 = NULL;
6788
6789         connman_technology_regdom_notify(wifi_technology, alpha2);
6790 }
6791
6792 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
6793 {
6794         return g_supplicant_set_country(alpha2, regdom_callback, NULL);
6795 }
6796
6797 #if defined TIZEN_EXT
6798 static void supp_ins_init(void)
6799 {
6800         const char *string;
6801         GSupplicantINSPreferredFreq preferred_freq;
6802
6803         string = connman_setting_get_string("INSPreferredFreqBSSID");
6804         if (g_strcmp0(string, "5GHz") == 0)
6805                 preferred_freq = G_SUPPLICANT_INS_PREFERRED_FREQ_5GHZ;
6806         else if (g_strcmp0(string, "2.4GHz") == 0)
6807                 preferred_freq = G_SUPPLICANT_INS_PREFERRED_FREQ_24GHZ;
6808         else
6809                 preferred_freq = G_SUPPLICANT_INS_PREFERRED_FREQ_UNKNOWN;
6810
6811         g_supplicant_set_ins_settings(preferred_freq,
6812                 connman_setting_get_bool("INSLastConnectedBSSID"),
6813                 connman_setting_get_bool("INSAssocReject"),
6814                 connman_setting_get_bool("INSSignalBSSID"),
6815                 connman_setting_get_uint("INSPreferredFreqBSSIDScore"),
6816                 connman_setting_get_uint("INSLastConnectedBSSIDScore"),
6817                 connman_setting_get_uint("INSAssocRejectScore"),
6818                 connman_setting_get_int("INSSignalLevel3_5GHz"),
6819                 connman_setting_get_int("INSSignalLevel3_2_4GHz")
6820         );
6821 }
6822 #endif /* defined TIZEN_EXT */
6823
6824 static struct connman_technology_driver tech_driver = {
6825         .name           = "wifi",
6826         .type           = CONNMAN_SERVICE_TYPE_WIFI,
6827         .probe          = tech_probe,
6828         .remove         = tech_remove,
6829         .set_tethering  = tech_set_tethering,
6830         .set_regdom     = tech_set_regdom,
6831 };
6832
6833 static int wifi_init(void)
6834 {
6835         int err;
6836
6837         err = connman_network_driver_register(&network_driver);
6838         if (err < 0)
6839                 return err;
6840
6841         err = g_supplicant_register(&callbacks);
6842         if (err < 0) {
6843                 connman_network_driver_unregister(&network_driver);
6844                 return err;
6845         }
6846
6847         err = connman_technology_driver_register(&tech_driver);
6848         if (err < 0) {
6849                 g_supplicant_unregister(&callbacks);
6850                 connman_network_driver_unregister(&network_driver);
6851                 return err;
6852         }
6853
6854 #if defined TIZEN_EXT
6855         failed_bssids = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
6856
6857         min_snr = connman_setting_get_int("WifiRoamingMinSNR");
6858
6859         min_rssi_2_4GHz = connman_setting_get_int("WifiRoamingMinRSSI_2_4GHz");
6860         if (min_rssi_2_4GHz >= 0)
6861                 min_rssi_2_4GHz = RSSI_LEVEL_2_2_4G;
6862
6863         min_rssi_5GHz = connman_setting_get_int("WifiRoamingMinRSSI_5GHz");
6864         if (min_rssi_5GHz >= 0)
6865                 min_rssi_5GHz = RSSI_LEVEL_2_5G;
6866
6867         min_rssi_6GHz = connman_setting_get_int("WifiRoamingMinRSSI_6GHz");
6868         if (min_rssi_6GHz >= 0)
6869                 min_rssi_6GHz = RSSI_LEVEL_2_6G;
6870
6871         DBG("Min SNR: %d, Min RSSI: %d(2.4GHz), %d(5GHz), %d(6GHz)",
6872                         min_snr, min_rssi_2_4GHz, min_rssi_5GHz, min_rssi_6GHz);
6873
6874         if (TIZEN_INS_ENABLED)
6875                 supp_ins_init();
6876 #endif /* defined TIZEN_EXT */
6877         return 0;
6878 }
6879
6880 static void wifi_exit(void)
6881 {
6882         DBG();
6883
6884         connman_technology_driver_unregister(&tech_driver);
6885
6886         g_supplicant_unregister(&callbacks);
6887
6888         connman_network_driver_unregister(&network_driver);
6889
6890 #if defined TIZEN_EXT
6891         g_hash_table_unref(failed_bssids);
6892 #endif
6893 }
6894
6895 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
6896                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)