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