Add setter/getter for handling MAC randomization policy
[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 #ifdef TIZEN_EXT
2439 int wifi_set_mac_policy(struct connman_device *device, unsigned int policy);
2440 int wifi_set_preassoc_mac_policy(struct connman_device *device, unsigned int policy);
2441 int wifi_set_random_mac_lifetime(struct connman_device *device, unsigned int lifetime);
2442 #endif /* TIZEN_EXT */
2443
2444 static void finalize_interface_creation(struct wifi_data *wifi)
2445 {
2446         DBG("interface is ready wifi %p tethering %d", wifi, wifi->tethering);
2447
2448         if (!wifi->device) {
2449                 connman_error("WiFi device not set");
2450                 return;
2451         }
2452
2453         connman_device_set_powered(wifi->device, true);
2454
2455 #ifdef TIZEN_EXT
2456         wifi_set_mac_policy(wifi->device,
2457                                 connman_device_get_mac_policy(wifi->device));
2458
2459         wifi_set_preassoc_mac_policy(wifi->device,
2460                                 connman_device_get_preassoc_mac_policy(wifi->device));
2461
2462         wifi_set_random_mac_lifetime(wifi->device,
2463                                 connman_device_get_random_mac_lifetime(wifi->device));
2464 #endif /* TIZEN_EXT */
2465
2466         if (wifi->p2p_device)
2467                 return;
2468
2469         if (!wifi->autoscan)
2470                 setup_autoscan(wifi);
2471
2472         start_autoscan(wifi->device);
2473 }
2474
2475 static void interface_create_callback(int result,
2476                                         GSupplicantInterface *interface,
2477                                                         void *user_data)
2478 {
2479         struct wifi_data *wifi = user_data;
2480
2481         DBG("result %d ifname %s, wifi %p", result,
2482                                 g_supplicant_interface_get_ifname(interface),
2483                                 wifi);
2484
2485         if (result < 0 || !wifi)
2486                 return;
2487
2488         wifi->interface = interface;
2489         g_supplicant_interface_set_data(interface, wifi);
2490
2491         if (g_supplicant_interface_get_ready(interface)) {
2492                 wifi->interface_ready = true;
2493                 finalize_interface_creation(wifi);
2494         }
2495 }
2496
2497 static int wifi_enable(struct connman_device *device)
2498 {
2499         struct wifi_data *wifi = connman_device_get_data(device);
2500         int index;
2501         char *interface;
2502         const char *driver = connman_option_get_string("wifi");
2503         int ret;
2504
2505         DBG("device %p %p", device, wifi);
2506
2507         index = connman_device_get_index(device);
2508         if (!wifi || index < 0)
2509                 return -ENODEV;
2510
2511         if (is_p2p_connecting())
2512                 return -EINPROGRESS;
2513
2514         interface = connman_inet_ifname(index);
2515         ret = g_supplicant_interface_create(interface, driver, NULL,
2516                                                 interface_create_callback,
2517                                                         wifi);
2518         g_free(interface);
2519
2520         if (ret < 0)
2521                 return ret;
2522
2523         return -EINPROGRESS;
2524 }
2525
2526 static int wifi_disable(struct connman_device *device)
2527 {
2528         struct wifi_data *wifi = connman_device_get_data(device);
2529         int ret;
2530
2531         DBG("device %p wifi %p", device, wifi);
2532
2533         if (!wifi)
2534                 return -ENODEV;
2535
2536         wifi->connected = false;
2537         wifi->disconnecting = false;
2538
2539         if (wifi->pending_network)
2540                 wifi->pending_network = NULL;
2541
2542 #if !defined TIZEN_EXT
2543         stop_autoscan(device);
2544 #endif
2545
2546         if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_P2P)) {
2547                 g_source_remove(wifi->p2p_find_timeout);
2548                 wifi->p2p_find_timeout = 0;
2549                 connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
2550                 connman_device_unref(wifi->device);
2551         }
2552
2553 #if defined TIZEN_EXT
2554         if (wifi->automaxspeed_timeout != 0) {
2555                 g_source_remove(wifi->automaxspeed_timeout);
2556                 wifi->automaxspeed_timeout = 0;
2557         }
2558 #endif
2559
2560         /* In case of a user scan, device is still referenced */
2561         if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI)) {
2562                 connman_device_set_scanning(device,
2563                                 CONNMAN_SERVICE_TYPE_WIFI, false);
2564                 connman_device_unref(wifi->device);
2565         }
2566
2567 #if defined TIZEN_EXT
2568         stop_autoscan(device);
2569 #endif
2570
2571         remove_networks(device, wifi);
2572         remove_peers(wifi);
2573
2574 #if defined TIZEN_EXT
2575         wifi->scan_pending_network = NULL;
2576
2577         if (is_wifi_notifier_registered == true) {
2578                 connman_notifier_unregister(&notifier);
2579                 is_wifi_notifier_registered = false;
2580         }
2581 #endif
2582
2583         ret = g_supplicant_interface_remove(wifi->interface, NULL, NULL);
2584         if (ret < 0)
2585                 return ret;
2586
2587         return -EINPROGRESS;
2588 }
2589
2590 static int get_latest_connections(int max_ssids,
2591                                 GSupplicantScanParams *scan_data)
2592 {
2593         GSequenceIter *iter;
2594         GSequence *latest_list;
2595         struct last_connected *entry;
2596         GKeyFile *keyfile;
2597         GTimeVal modified;
2598         gchar **services;
2599         gchar *str;
2600         char *ssid;
2601         int i, freq;
2602         int num_ssids = 0;
2603
2604         latest_list = g_sequence_new(free_entry);
2605         if (!latest_list)
2606                 return -ENOMEM;
2607
2608         services = connman_storage_get_services();
2609         for (i = 0; services && services[i]; i++) {
2610                 if (strncmp(services[i], "wifi_", 5) != 0)
2611                         continue;
2612
2613                 keyfile = connman_storage_load_service(services[i]);
2614                 if (!keyfile)
2615                         continue;
2616
2617                 str = g_key_file_get_string(keyfile,
2618                                         services[i], "Favorite", NULL);
2619                 if (!str || g_strcmp0(str, "true")) {
2620                         g_free(str);
2621                         g_key_file_free(keyfile);
2622                         continue;
2623                 }
2624                 g_free(str);
2625
2626                 str = g_key_file_get_string(keyfile,
2627                                         services[i], "AutoConnect", NULL);
2628                 if (!str || g_strcmp0(str, "true")) {
2629                         g_free(str);
2630                         g_key_file_free(keyfile);
2631                         continue;
2632                 }
2633                 g_free(str);
2634
2635                 str = g_key_file_get_string(keyfile,
2636                                         services[i], "Modified", NULL);
2637                 if (!str) {
2638                         g_key_file_free(keyfile);
2639                         continue;
2640                 }
2641                 util_iso8601_to_timeval(str, &modified);
2642                 g_free(str);
2643
2644                 ssid = g_key_file_get_string(keyfile,
2645                                         services[i], "SSID", NULL);
2646
2647                 freq = g_key_file_get_integer(keyfile, services[i],
2648                                         "Frequency", NULL);
2649                 if (freq) {
2650                         entry = g_try_new(struct last_connected, 1);
2651                         if (!entry) {
2652                                 g_sequence_free(latest_list);
2653                                 g_key_file_free(keyfile);
2654                                 g_free(ssid);
2655                                 return -ENOMEM;
2656                         }
2657
2658                         entry->ssid = ssid;
2659                         entry->modified = modified;
2660                         entry->freq = freq;
2661
2662                         g_sequence_insert_sorted(latest_list, entry,
2663                                                 sort_entry, NULL);
2664                         num_ssids++;
2665                 } else
2666                         g_free(ssid);
2667
2668                 g_key_file_free(keyfile);
2669         }
2670
2671         g_strfreev(services);
2672
2673         num_ssids = num_ssids > max_ssids ? max_ssids : num_ssids;
2674
2675         iter = g_sequence_get_begin_iter(latest_list);
2676
2677         for (i = 0; i < num_ssids; i++) {
2678                 entry = g_sequence_get(iter);
2679
2680                 DBG("ssid %s freq %d modified %lu", entry->ssid, entry->freq,
2681                                                 entry->modified.tv_sec);
2682
2683                 add_scan_param(entry->ssid, NULL, 0, entry->freq, scan_data,
2684                                                 max_ssids, entry->ssid);
2685
2686                 iter = g_sequence_iter_next(iter);
2687         }
2688
2689         g_sequence_free(latest_list);
2690         return num_ssids;
2691 }
2692
2693 static void wifi_update_scanner_type(struct wifi_data *wifi,
2694                                         enum wifi_scanning_type new_type)
2695 {
2696         DBG("");
2697
2698         if (!wifi || wifi->scanning_type == new_type)
2699                 return;
2700
2701         wifi->scanning_type = new_type;
2702
2703         setup_autoscan(wifi);
2704 }
2705
2706 static int wifi_scan_simple(struct connman_device *device)
2707 {
2708         struct wifi_data *wifi = connman_device_get_data(device);
2709
2710         reset_autoscan(device);
2711
2712         /* Distinguish between devices performing passive and active scanning */
2713         if (wifi)
2714                 wifi_update_scanner_type(wifi, WIFI_SCANNING_PASSIVE);
2715
2716         return throw_wifi_scan(device, scan_callback_hidden);
2717 }
2718
2719 static gboolean p2p_find_stop(gpointer data)
2720 {
2721         struct connman_device *device = data;
2722         struct wifi_data *wifi = connman_device_get_data(device);
2723
2724         DBG("");
2725
2726         if (wifi) {
2727                 wifi->p2p_find_timeout = 0;
2728
2729                 g_supplicant_interface_p2p_stop_find(wifi->interface);
2730         }
2731
2732         connman_device_set_scanning(device, CONNMAN_SERVICE_TYPE_P2P, false);
2733
2734         connman_device_unref(device);
2735         start_autoscan(device);
2736
2737         return FALSE;
2738 }
2739
2740 static void p2p_find_callback(int result, GSupplicantInterface *interface,
2741                                                         void *user_data)
2742 {
2743         struct connman_device *device = user_data;
2744         struct wifi_data *wifi = connman_device_get_data(device);
2745
2746         DBG("result %d wifi %p", result, wifi);
2747
2748         if (!wifi)
2749                 goto error;
2750
2751         if (wifi->p2p_find_timeout) {
2752                 g_source_remove(wifi->p2p_find_timeout);
2753                 wifi->p2p_find_timeout = 0;
2754         }
2755
2756         if (result)
2757                 goto error;
2758
2759         wifi->p2p_find_timeout = g_timeout_add_seconds(P2P_FIND_TIMEOUT,
2760                                                         p2p_find_stop, device);
2761         if (!wifi->p2p_find_timeout)
2762                 goto error;
2763
2764         return;
2765 error:
2766         p2p_find_stop(device);
2767 }
2768
2769 static int p2p_find(struct connman_device *device)
2770 {
2771         struct wifi_data *wifi;
2772         int ret;
2773
2774         DBG("");
2775
2776         if (!p2p_technology)
2777                 return -ENOTSUP;
2778
2779         wifi = connman_device_get_data(device);
2780
2781         if (g_supplicant_interface_is_p2p_finding(wifi->interface))
2782                 return -EALREADY;
2783
2784         reset_autoscan(device);
2785         connman_device_ref(device);
2786
2787         ret = g_supplicant_interface_p2p_find(wifi->interface,
2788                                                 p2p_find_callback, device);
2789         if (ret) {
2790                 connman_device_unref(device);
2791                 start_autoscan(device);
2792         } else {
2793                 connman_device_set_scanning(device,
2794                                 CONNMAN_SERVICE_TYPE_P2P, true);
2795         }
2796
2797         return ret;
2798 }
2799
2800 #if defined TIZEN_EXT
2801 static void specific_scan_callback(int result, GSupplicantInterface *interface,
2802                                                 void *user_data)
2803 {
2804         struct connman_device *device = user_data;
2805         struct wifi_data *wifi = connman_device_get_data(device);
2806         bool scanning;
2807
2808         DBG("result %d wifi %p", result, wifi);
2809
2810         if (wifi && wifi->scan_params) {
2811                 g_supplicant_free_scan_params(wifi->scan_params);
2812                 wifi->scan_params = NULL;
2813         }
2814
2815         scanning = connman_device_get_scanning(device,
2816                                                CONNMAN_SERVICE_TYPE_WIFI);
2817         if (scanning) {
2818                 connman_device_set_scanning(device,
2819                                 CONNMAN_SERVICE_TYPE_WIFI, false);
2820                 connman_device_unref(device);
2821         }
2822 }
2823
2824 static int wifi_specific_scan(enum connman_service_type type,
2825                         struct connman_device *device, int scan_type,
2826                         GSList *specific_scan_list, void *user_data)
2827 {
2828         GSList *list = NULL;
2829         char *ssid = NULL;
2830         struct wifi_data *wifi = connman_device_get_data(device);
2831         GSupplicantScanParams *scan_params = NULL;
2832         struct scan_ssid *scan_ssid = NULL;
2833         bool scanning;
2834         int ret;
2835         int freq;
2836         int count = 0;
2837
2838         if (!wifi)
2839                 return -ENODEV;
2840
2841         if (wifi->p2p_device)
2842                 return 0;
2843
2844         if (type == CONNMAN_SERVICE_TYPE_P2P)
2845                 return p2p_find(device);
2846
2847         if (wifi->tethering)
2848                 return 0;
2849
2850         scanning =
2851                 connman_device_get_scanning(device,
2852                                             CONNMAN_SERVICE_TYPE_WIFI);
2853         if (scanning)
2854                 return -EALREADY;
2855
2856         DBG("scan_type: %d", scan_type);
2857         if (scan_type == CONNMAN_MULTI_SCAN_SSID) { /* ssid based scan */
2858                 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
2859                 if (!scan_params) {
2860                         DBG("Failed to allocate memory.");
2861                         return -ENOMEM;
2862                 }
2863
2864                 for (list = specific_scan_list; list; list = list->next) {
2865                         ssid = (char *)list->data;
2866                         int ssid_len = strlen(ssid);
2867
2868                         scan_ssid = g_try_new0(struct scan_ssid, 1);
2869                         if (!scan_ssid) {
2870                                 DBG("Failed to allocate memory.");
2871                                 g_supplicant_free_scan_params(scan_params);
2872                                 return -ENOMEM;
2873                         }
2874
2875                         memcpy(scan_ssid->ssid, ssid, (ssid_len + 1));
2876                         /* DBG("scan ssid %s len: %d", scan_ssid->ssid, ssid_len); */
2877                         scan_ssid->ssid_len = ssid_len;
2878                         scan_params->ssids = g_slist_prepend(scan_params->ssids, scan_ssid);
2879                         count++;
2880                 }
2881                 scan_params->num_ssids = count;
2882
2883         } else if (scan_type == CONNMAN_MULTI_SCAN_FREQ) { /* frequency based scan */
2884
2885                 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
2886                 if (!scan_params) {
2887                         DBG("Failed to allocate memory.");
2888                         return -ENOMEM;
2889                 }
2890
2891                 guint num_freqs = g_slist_length(specific_scan_list);
2892                 DBG("num_freqs: %d", num_freqs);
2893
2894                 scan_params->freqs = g_try_new0(uint16_t, num_freqs);
2895                 if (!scan_params->freqs) {
2896                         DBG("Failed to allocate memory.");
2897                         g_free(scan_params);
2898                         return -ENOMEM;
2899                 }
2900
2901                 count = 0;
2902                 for (list = specific_scan_list; list; list = list->next) {
2903                         freq = (int)list->data;
2904
2905                         scan_params->freqs[count] = freq;
2906                         DBG("scan_params->freqs[%d]: %d", count, scan_params->freqs[count]);
2907                         count++;
2908                 }
2909                 scan_params->num_freqs = count;
2910
2911         } else if (scan_type == CONNMAN_MULTI_SCAN_SSID_FREQ) { /* SSID & Frequency mixed scan */
2912                 int freq_count, ap_count;
2913                 scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
2914                 if (!scan_params) {
2915                         DBG("Failed to allocate memory.");
2916                         return -ENOMEM;
2917                 }
2918
2919                 guint size = g_slist_length(specific_scan_list);
2920
2921                 scan_params->freqs = g_try_new0(uint16_t, size/2);
2922                 if (!scan_params->freqs) {
2923                         DBG("Failed to allocate memory.");
2924                         g_free(scan_params);
2925                         return -ENOMEM;
2926                 }
2927
2928                 ap_count = freq_count = 0;
2929                 for (list = specific_scan_list; list; list = list->next) {
2930                         if (((connman_multi_scan_ap_s *)list->data)->flag == true) { /** ssid */
2931                                 ssid = ((connman_multi_scan_ap_s *)list->data)->str;
2932                                 int ssid_len = strlen(ssid);
2933
2934                                 scan_ssid = g_try_new0(struct scan_ssid, 1);
2935                                 if (!scan_ssid) {
2936                                         DBG("Failed to allocate memory.");
2937                                         g_supplicant_free_scan_params(scan_params);
2938                                         return -ENOMEM;
2939                                 }
2940
2941                                 memcpy(scan_ssid->ssid, ssid, (ssid_len + 1));
2942                                 /* DBG("scan ssid %s len: %d", scan_ssid->ssid, ssid_len); */
2943                                 scan_ssid->ssid_len = ssid_len;
2944                                 scan_params->ssids = g_slist_prepend(scan_params->ssids, scan_ssid);
2945                                 ap_count++;
2946
2947                         } else { /* freq */
2948                                 freq = atoi(((connman_multi_scan_ap_s *)list->data)->str);
2949                                 scan_params->freqs[freq_count] = freq;
2950                                 DBG("scan_params->freqs[%d]: %d", freq_count, scan_params->freqs[freq_count]);
2951                                 freq_count++;
2952                         }
2953                 }
2954                 scan_params->num_ssids = ap_count;
2955                 scan_params->num_freqs = freq_count;
2956         } else {
2957                 DBG("Invalid scan");
2958                 return -EINVAL;
2959         }
2960
2961         reset_autoscan(device);
2962         connman_device_ref(device);
2963
2964         ret = g_supplicant_interface_scan(wifi->interface, scan_params,
2965                                                 specific_scan_callback, device);
2966
2967         if (ret == 0) {
2968                 connman_device_set_scanning(device,
2969                                 CONNMAN_SERVICE_TYPE_WIFI, true);
2970         } else {
2971                 g_supplicant_free_scan_params(scan_params);
2972                 connman_device_unref(device);
2973         }
2974
2975         return ret;
2976 }
2977
2978 static void wifi_mac_policy_callback(int result,
2979                                         unsigned int policy,
2980                                                 void *user_data)
2981 {
2982         struct connman_device *device = user_data;
2983
2984         if (result == 0)
2985                 connman_device_mac_policy_notify(device, result, policy);
2986
2987         connman_device_unref(device);
2988 }
2989
2990 int wifi_set_mac_policy(struct connman_device *device, unsigned int policy)
2991 {
2992         struct wifi_data *wifi = connman_device_get_data(device);
2993         int ret;
2994
2995         if (!wifi)
2996                 return -EINVAL;
2997
2998         connman_device_ref(device);
2999
3000         ret = g_supplicant_interface_set_mac_policy(wifi->interface,
3001                                         wifi_mac_policy_callback,
3002                                         policy, device);
3003         if (ret != 0)
3004                 connman_device_unref(device);
3005
3006         return ret;
3007 }
3008
3009 static void wifi_preassoc_mac_policy_callback(int result,
3010                                         unsigned int policy,
3011                                                 void *user_data)
3012 {
3013         struct connman_device *device = user_data;
3014
3015         if (result == 0)
3016                 connman_device_preassoc_mac_policy_notify(device, result, policy);
3017
3018         connman_device_unref(device);
3019 }
3020
3021 int wifi_set_preassoc_mac_policy(struct connman_device *device, unsigned int policy)
3022 {
3023         struct wifi_data *wifi = connman_device_get_data(device);
3024         int ret;
3025
3026         if (!wifi)
3027                 return -EINVAL;
3028
3029         connman_device_ref(device);
3030
3031         ret = g_supplicant_interface_set_preassoc_mac_policy(wifi->interface,
3032                                         wifi_preassoc_mac_policy_callback,
3033                                         policy, device);
3034         if (ret != 0)
3035                 connman_device_unref(device);
3036
3037         return ret;
3038 }
3039
3040 static void wifi_random_mac_lifetime_callback(int result,
3041                                         unsigned int lifetime,
3042                                                 void *user_data)
3043 {
3044         struct connman_device *device = user_data;
3045
3046         if (result == 0)
3047                 connman_device_random_mac_lifetime_notify(device, result, lifetime);
3048
3049         connman_device_unref(device);
3050 }
3051
3052 int wifi_set_random_mac_lifetime(struct connman_device *device, unsigned int lifetime)
3053 {
3054         struct wifi_data *wifi = connman_device_get_data(device);
3055         int ret;
3056
3057         if (!wifi)
3058                 return -EINVAL;
3059
3060         connman_device_ref(device);
3061
3062         ret = g_supplicant_interface_set_random_mac_lifetime(wifi->interface,
3063                                         wifi_random_mac_lifetime_callback,
3064                                         lifetime, device);
3065         if (ret != 0)
3066                 connman_device_unref(device);
3067
3068         return ret;
3069 }
3070 #endif
3071
3072 #if defined TIZEN_EXT_WIFI_MESH
3073 static void mesh_scan_callback(int result, GSupplicantInterface *interface,
3074                                                 void *user_data)
3075 {
3076         struct connman_device *device = user_data;
3077         struct wifi_data *wifi = connman_device_get_data(device);
3078         bool scanning;
3079
3080         DBG("result %d wifi %p", result, wifi);
3081
3082         scanning = connman_device_get_scanning(device,
3083                                                CONNMAN_SERVICE_TYPE_MESH);
3084         if (scanning)
3085                 connman_device_set_scanning(device,
3086                                 CONNMAN_SERVICE_TYPE_MESH, false);
3087
3088         if (scanning)
3089                 connman_device_unref(device);
3090 }
3091
3092 static int mesh_scan(struct connman_device *device)
3093 {
3094         struct wifi_data *wifi;
3095         struct wifi_mesh_info *mesh_info;
3096         int ret;
3097
3098         DBG("");
3099
3100         wifi = connman_device_get_data(device);
3101
3102         if (!wifi || !wifi->mesh_interface)
3103                 return -ENOTSUP;
3104
3105         mesh_info = wifi->mesh_info;
3106         reset_autoscan(device);
3107         connman_device_ref(device);
3108
3109         ret = g_supplicant_interface_scan(mesh_info->interface, NULL,
3110                                                 mesh_scan_callback, device);
3111         if (ret)
3112                 connman_device_unref(device);
3113         else
3114                 connman_device_set_scanning(device,
3115                                 CONNMAN_SERVICE_TYPE_MESH, true);
3116
3117         return ret;
3118 }
3119
3120 static void abort_scan_callback(int result, GSupplicantInterface *interface,
3121                                                 void *user_data)
3122 {
3123         struct connman_device *device = user_data;
3124         struct wifi_data *wifi = connman_device_get_data(device);
3125
3126         DBG("result %d wifi %p", result, wifi);
3127
3128         __connman_technology_notify_abort_scan(CONNMAN_SERVICE_TYPE_MESH, result);
3129 }
3130
3131 static int mesh_abort_scan(enum connman_service_type type,
3132                                                 struct connman_device *device)
3133 {
3134         struct wifi_data *wifi = connman_device_get_data(device);
3135         struct wifi_mesh_info *mesh_info;
3136         bool scanning;
3137         int ret;
3138
3139         if (!wifi || !wifi->mesh_interface)
3140                 return -ENODEV;
3141
3142         if (type != CONNMAN_SERVICE_TYPE_MESH)
3143                 return -EINVAL;
3144
3145         mesh_info = wifi->mesh_info;
3146
3147         scanning = connman_device_get_scanning(device,
3148                                                CONNMAN_SERVICE_TYPE_MESH);
3149         if (!scanning)
3150                 return -EEXIST;
3151
3152         ret = g_supplicant_interface_abort_scan(mesh_info->interface,
3153                                                 abort_scan_callback, device);
3154
3155         return ret;
3156 }
3157
3158 static int mesh_specific_scan(enum connman_service_type type,
3159                               struct connman_device *device, const char *ssid,
3160                               unsigned int freq, void *user_data)
3161 {
3162         struct wifi_data *wifi = connman_device_get_data(device);
3163         GSupplicantScanParams *scan_params = NULL;
3164         struct wifi_mesh_info *mesh_info;
3165         struct scan_ssid *scan_ssid;
3166         bool scanning;
3167         int ret;
3168
3169         if (!wifi || !wifi->mesh_interface)
3170                 return -ENODEV;
3171
3172         if (type != CONNMAN_SERVICE_TYPE_MESH)
3173                 return -EINVAL;
3174
3175         if (wifi->p2p_device)
3176                 return 0;
3177
3178         mesh_info = wifi->mesh_info;
3179
3180         scanning = connman_device_get_scanning(device,
3181                                                CONNMAN_SERVICE_TYPE_MESH);
3182         if (scanning)
3183                 return -EALREADY;
3184
3185         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
3186         if (!scan_params)
3187                 return -ENOMEM;
3188
3189         scan_ssid = g_try_new(struct scan_ssid, 1);
3190         if (!scan_ssid) {
3191                 g_free(scan_params);
3192                 return -ENOMEM;
3193         }
3194
3195         scan_ssid->ssid_len = strlen(ssid);
3196         memcpy(scan_ssid->ssid, ssid, scan_ssid->ssid_len);
3197         scan_params->ssids = g_slist_prepend(scan_params->ssids, scan_ssid);
3198         scan_params->num_ssids = 1;
3199
3200         scan_params->freqs = g_try_new(uint16_t, 1);
3201         if (!scan_params->freqs) {
3202                 g_slist_free_full(scan_params->ssids, g_free);
3203                 g_free(scan_params);
3204                 return -ENOMEM;
3205         }
3206
3207         scan_params->freqs[0] = freq;
3208         scan_params->num_freqs = 1;
3209
3210         reset_autoscan(device);
3211         connman_device_ref(device);
3212
3213         ret = g_supplicant_interface_scan(mesh_info->interface, scan_params,
3214                                                 mesh_scan_callback, device);
3215
3216         if (ret == 0) {
3217                 connman_device_set_scanning(device,
3218                                 CONNMAN_SERVICE_TYPE_MESH, true);
3219         } else {
3220                 g_supplicant_free_scan_params(scan_params);
3221                 connman_device_unref(device);
3222         }
3223
3224         return ret;
3225 }
3226 #endif
3227
3228 /*
3229  * Note that the hidden scan is only used when connecting to this specific
3230  * hidden AP first time. It is not used when system autoconnects to hidden AP.
3231  */
3232 static int wifi_scan(struct connman_device *device,
3233                         struct connman_device_scan_params *params)
3234 {
3235         struct wifi_data *wifi = connman_device_get_data(device);
3236         GSupplicantScanParams *scan_params = NULL;
3237         struct scan_ssid *scan_ssid;
3238         struct hidden_params *hidden;
3239         int ret;
3240         int driver_max_ssids = 0;
3241         bool do_hidden;
3242         bool scanning;
3243
3244         if (!wifi)
3245                 return -ENODEV;
3246
3247         if (wifi->p2p_device)
3248                 return -EBUSY;
3249
3250         if (wifi->tethering)
3251                 return -EBUSY;
3252
3253         if (params->type == CONNMAN_SERVICE_TYPE_P2P)
3254                 return p2p_find(device);
3255
3256 #if defined TIZEN_EXT_WIFI_MESH
3257         if (params->type == CONNMAN_SERVICE_TYPE_MESH)
3258                 return mesh_scan(device);
3259 #endif
3260
3261         DBG("device %p wifi %p hidden ssid %s", device, wifi->interface,
3262                 params->ssid);
3263
3264         scanning = connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_WIFI);
3265
3266         if (!params->ssid || params->ssid_len == 0 || params->ssid_len > 32) {
3267                 if (scanning)
3268                         return -EALREADY;
3269
3270                 driver_max_ssids = g_supplicant_interface_get_max_scan_ssids(
3271                                                         wifi->interface);
3272                 DBG("max ssids %d", driver_max_ssids);
3273                 if (driver_max_ssids == 0)
3274                         return wifi_scan_simple(device);
3275
3276                 do_hidden = false;
3277         } else {
3278                 if (scanning && wifi->hidden && wifi->postpone_hidden)
3279                         return -EALREADY;
3280
3281                 do_hidden = true;
3282         }
3283
3284         scan_params = g_try_malloc0(sizeof(GSupplicantScanParams));
3285         if (!scan_params)
3286                 return -ENOMEM;
3287
3288         if (do_hidden) {
3289                 scan_ssid = g_try_new(struct scan_ssid, 1);
3290                 if (!scan_ssid) {
3291                         g_free(scan_params);
3292                         return -ENOMEM;
3293                 }
3294
3295                 memcpy(scan_ssid->ssid, params->ssid, params->ssid_len);
3296                 scan_ssid->ssid_len = params->ssid_len;
3297                 scan_params->ssids = g_slist_prepend(scan_params->ssids,
3298                                                                 scan_ssid);
3299                 scan_params->num_ssids = 1;
3300
3301                 hidden = g_try_new0(struct hidden_params, 1);
3302                 if (!hidden) {
3303                         g_supplicant_free_scan_params(scan_params);
3304                         return -ENOMEM;
3305                 }
3306
3307                 if (wifi->hidden) {
3308                         hidden_free(wifi->hidden);
3309                         wifi->hidden = NULL;
3310                 }
3311
3312                 memcpy(hidden->ssid, params->ssid, params->ssid_len);
3313                 hidden->ssid_len = params->ssid_len;
3314                 hidden->identity = g_strdup(params->identity);
3315                 hidden->passphrase = g_strdup(params->passphrase);
3316                 hidden->security = g_strdup(params->security);
3317                 hidden->user_data = params->user_data;
3318                 wifi->hidden = hidden;
3319
3320                 if (scanning) {
3321                         /* Let's keep this active scan for later,
3322                          * when current scan will be over. */
3323                         wifi->postpone_hidden = TRUE;
3324                         hidden->scan_params = scan_params;
3325
3326                         return 0;
3327                 }
3328         } else if (wifi->connected) {
3329                 g_supplicant_free_scan_params(scan_params);
3330                 return wifi_scan_simple(device);
3331         } else if (!params->force_full_scan) {
3332                 ret = get_latest_connections(driver_max_ssids, scan_params);
3333                 if (ret <= 0) {
3334                         g_supplicant_free_scan_params(scan_params);
3335                         return wifi_scan_simple(device);
3336                 }
3337         }
3338
3339         /* Distinguish between devices performing passive and active scanning */
3340         wifi_update_scanner_type(wifi, WIFI_SCANNING_ACTIVE);
3341
3342         connman_device_ref(device);
3343
3344         reset_autoscan(device);
3345 #if defined TIZEN_EXT
3346         /*
3347          * When doing a full scan, stored hidden networks also need to be scanned
3348          * so that we can autoconnect to them.
3349          */
3350         if (params->force_full_scan)
3351                 ret = g_supplicant_interface_scan(wifi->interface, scan_params,
3352                                                         scan_callback_hidden, device);
3353         else
3354 #endif
3355         ret = g_supplicant_interface_scan(wifi->interface, scan_params,
3356                                                 scan_callback, device);
3357         if (ret == 0) {
3358                 connman_device_set_scanning(device,
3359                                 CONNMAN_SERVICE_TYPE_WIFI, true);
3360 #if defined TIZEN_EXT
3361                 /*
3362                  * To allow the Full Scan after ssid based scan, set the flag here
3363                  * It is required because Tizen does not use the ConnMan specific
3364                  * backgroung Scan feature.Tizen has added the BG Scan feature in
3365                  * net-config. To sync with up ConnMan, we need to issue the Full Scan
3366                  * after SSID specific scan.
3367                  */
3368                 if (!params->force_full_scan && !do_hidden)
3369                         wifi->allow_full_scan = TRUE;
3370 #endif
3371         } else {
3372                 g_supplicant_free_scan_params(scan_params);
3373                 connman_device_unref(device);
3374
3375                 if (do_hidden) {
3376                         hidden_free(wifi->hidden);
3377                         wifi->hidden = NULL;
3378                 }
3379         }
3380
3381         return ret;
3382 }
3383
3384 static void wifi_stop_scan(enum connman_service_type type,
3385                         struct connman_device *device)
3386 {
3387         struct wifi_data *wifi = connman_device_get_data(device);
3388
3389         DBG("device %p wifi %p", device, wifi);
3390
3391         if (!wifi)
3392                 return;
3393
3394         if (type == CONNMAN_SERVICE_TYPE_P2P) {
3395                 if (connman_device_get_scanning(device, CONNMAN_SERVICE_TYPE_P2P)) {
3396                         g_source_remove(wifi->p2p_find_timeout);
3397                         p2p_find_stop(device);
3398                 }
3399         }
3400 }
3401
3402 static void wifi_regdom_callback(int result,
3403                                         const char *alpha2,
3404                                                 void *user_data)
3405 {
3406         struct connman_device *device = user_data;
3407
3408         connman_device_regdom_notify(device, result, alpha2);
3409
3410         connman_device_unref(device);
3411 }
3412
3413 static int wifi_set_regdom(struct connman_device *device, const char *alpha2)
3414 {
3415         struct wifi_data *wifi = connman_device_get_data(device);
3416         int ret;
3417
3418         if (!wifi)
3419                 return -EINVAL;
3420
3421         connman_device_ref(device);
3422
3423         ret = g_supplicant_interface_set_country(wifi->interface,
3424                                                 wifi_regdom_callback,
3425                                                         alpha2, device);
3426         if (ret != 0)
3427                 connman_device_unref(device);
3428
3429         return ret;
3430 }
3431
3432 static struct connman_device_driver wifi_ng_driver = {
3433         .name           = "wifi",
3434         .type           = CONNMAN_DEVICE_TYPE_WIFI,
3435         .priority       = CONNMAN_DEVICE_PRIORITY_LOW,
3436         .probe          = wifi_probe,
3437         .remove         = wifi_remove,
3438         .enable         = wifi_enable,
3439         .disable        = wifi_disable,
3440         .scan           = wifi_scan,
3441         .stop_scan      = wifi_stop_scan,
3442         .set_regdom     = wifi_set_regdom,
3443 #if defined TIZEN_EXT
3444         .specific_scan  = wifi_specific_scan,
3445         .set_mac_policy           = wifi_set_mac_policy,
3446         .set_preassoc_mac_policy  = wifi_set_preassoc_mac_policy,
3447         .set_random_mac_lifetime  = wifi_set_random_mac_lifetime,
3448 #endif
3449 #if defined TIZEN_EXT_WIFI_MESH
3450         .abort_scan     = mesh_abort_scan,
3451         .mesh_specific_scan     = mesh_specific_scan,
3452 #endif
3453 };
3454
3455 static void system_ready(void)
3456 {
3457         DBG("");
3458
3459         if (connman_device_driver_register(&wifi_ng_driver) < 0)
3460                 connman_error("Failed to register WiFi driver");
3461 }
3462
3463 static void system_killed(void)
3464 {
3465         DBG("");
3466
3467         connman_device_driver_unregister(&wifi_ng_driver);
3468 }
3469
3470 static int network_probe(struct connman_network *network)
3471 {
3472 #if defined TIZEN_EXT
3473         if (!simplified_log)
3474 #endif
3475         DBG("network %p", network);
3476
3477         return 0;
3478 }
3479
3480 static void network_remove(struct connman_network *network)
3481 {
3482         struct connman_device *device = connman_network_get_device(network);
3483         struct wifi_data *wifi;
3484
3485         DBG("network %p", network);
3486
3487         wifi = connman_device_get_data(device);
3488         if (!wifi)
3489                 return;
3490
3491         if (wifi->network != network)
3492                 return;
3493
3494         wifi->network = NULL;
3495
3496 #if defined TIZEN_EXT
3497         wifi->disconnecting = false;
3498
3499         if (wifi->pending_network == network)
3500                 wifi->pending_network = NULL;
3501
3502         if (wifi->scan_pending_network == network)
3503                 wifi->scan_pending_network = NULL;
3504 #endif
3505 }
3506
3507 static void connect_callback(int result, GSupplicantInterface *interface,
3508                                                         void *user_data)
3509 {
3510 #if defined TIZEN_EXT
3511         GList *list;
3512         struct wifi_data *wifi;
3513 #endif
3514         struct connman_network *network = user_data;
3515
3516         DBG("network %p result %d", network, result);
3517
3518 #if defined TIZEN_EXT
3519         const char *ifname = g_supplicant_interface_get_ifname(interface);
3520         set_connman_bssid(RESET_BSSID, NULL, ifname);
3521
3522         for (list = iface_list; list; list = list->next) {
3523                 wifi = list->data;
3524
3525                 if (wifi && wifi->network == network)
3526                         goto found;
3527         }
3528
3529         /* wifi_data may be invalid because wifi is already disabled */
3530         return;
3531
3532 found:
3533 #endif
3534         if (result == -ENOKEY) {
3535                 connman_network_set_error(network,
3536                                         CONNMAN_NETWORK_ERROR_INVALID_KEY);
3537         } else if (result < 0) {
3538                 connman_network_set_error(network,
3539                                         CONNMAN_NETWORK_ERROR_CONFIGURE_FAIL);
3540         }
3541
3542         connman_network_unref(network);
3543 }
3544
3545 static GSupplicantSecurity network_security(const char *security)
3546 {
3547         if (g_str_equal(security, "none"))
3548                 return G_SUPPLICANT_SECURITY_NONE;
3549         else if (g_str_equal(security, "wep"))
3550                 return G_SUPPLICANT_SECURITY_WEP;
3551         else if (g_str_equal(security, "psk"))
3552                 return G_SUPPLICANT_SECURITY_PSK;
3553         else if (g_str_equal(security, "wpa"))
3554                 return G_SUPPLICANT_SECURITY_PSK;
3555         else if (g_str_equal(security, "rsn"))
3556                 return G_SUPPLICANT_SECURITY_PSK;
3557         else if (g_str_equal(security, "ieee8021x"))
3558                 return G_SUPPLICANT_SECURITY_IEEE8021X;
3559 #if defined TIZEN_EXT
3560         else if (g_str_equal(security, "ft_psk") == TRUE)
3561                 return G_SUPPLICANT_SECURITY_FT_PSK;
3562         else if (g_str_equal(security, "ft_ieee8021x") == TRUE)
3563                 return G_SUPPLICANT_SECURITY_FT_IEEE8021X;
3564         else if (g_str_equal(security, "sae"))
3565                 return G_SUPPLICANT_SECURITY_SAE;
3566         else if (g_str_equal(security, "owe"))
3567                 return G_SUPPLICANT_SECURITY_OWE;
3568         else if (g_str_equal(security, "dpp"))
3569                 return G_SUPPLICANT_SECURITY_DPP;
3570 #endif
3571
3572         return G_SUPPLICANT_SECURITY_UNKNOWN;
3573 }
3574
3575 #if defined TIZEN_EXT
3576 static GSupplicantEapKeymgmt network_eap_keymgmt(const char *security)
3577 {
3578         if (security == NULL)
3579                 return G_SUPPLICANT_EAP_KEYMGMT_NONE;
3580
3581         if (g_str_equal(security, "FT") == TRUE)
3582                 return G_SUPPLICANT_EAP_KEYMGMT_FT;
3583         else if (g_str_equal(security, "CCKM") == TRUE)
3584                 return G_SUPPLICANT_EAP_KEYMGMT_CCKM;
3585
3586         return G_SUPPLICANT_EAP_KEYMGMT_NONE;
3587 }
3588 #endif
3589
3590 static void ssid_init(GSupplicantSSID *ssid, struct connman_network *network)
3591 {
3592         const char *security;
3593 #if defined TIZEN_EXT
3594         const void *ssid_data;
3595 #endif
3596
3597         memset(ssid, 0, sizeof(*ssid));
3598         ssid->mode = G_SUPPLICANT_MODE_INFRA;
3599 #if defined TIZEN_EXT
3600         ssid_data = connman_network_get_blob(network, "WiFi.SSID",
3601                                                 &ssid->ssid_len);
3602         ssid->ssid = g_try_malloc0(ssid->ssid_len);
3603
3604         if (!ssid->ssid)
3605                 ssid->ssid_len = 0;
3606         else
3607                 memcpy(ssid->ssid, ssid_data, ssid->ssid_len);
3608 #else
3609         ssid->ssid = connman_network_get_blob(network, "WiFi.SSID",
3610                                                 &ssid->ssid_len);
3611 #endif
3612         ssid->scan_ssid = 1;
3613         security = connman_network_get_string(network, "WiFi.Security");
3614         ssid->security = network_security(security);
3615 #if defined TIZEN_EXT
3616         ssid->ieee80211w = 1;
3617 #endif
3618         ssid->passphrase = connman_network_get_string(network,
3619                                                 "WiFi.Passphrase");
3620
3621         ssid->eap = connman_network_get_string(network, "WiFi.EAP");
3622
3623         /*
3624          * If our private key password is unset,
3625          * we use the supplied passphrase. That is needed
3626          * for PEAP where 2 passphrases (identity and client
3627          * cert may have to be provided.
3628          */
3629         if (!connman_network_get_string(network, "WiFi.PrivateKeyPassphrase"))
3630                 connman_network_set_string(network,
3631                                                 "WiFi.PrivateKeyPassphrase",
3632                                                 ssid->passphrase);
3633         /* We must have an identity for both PEAP and TLS */
3634         ssid->identity = connman_network_get_string(network, "WiFi.Identity");
3635
3636         /* Use agent provided identity as a fallback */
3637         if (!ssid->identity || strlen(ssid->identity) == 0)
3638                 ssid->identity = connman_network_get_string(network,
3639                                                         "WiFi.AgentIdentity");
3640
3641         ssid->anonymous_identity = connman_network_get_string(network,
3642                                                 "WiFi.AnonymousIdentity");
3643         ssid->ca_cert_path = connman_network_get_string(network,
3644                                                         "WiFi.CACertFile");
3645         ssid->subject_match = connman_network_get_string(network,
3646                                                         "WiFi.SubjectMatch");
3647         ssid->altsubject_match = connman_network_get_string(network,
3648                                                         "WiFi.AltSubjectMatch");
3649         ssid->domain_suffix_match = connman_network_get_string(network,
3650                                                         "WiFi.DomainSuffixMatch");
3651         ssid->domain_match = connman_network_get_string(network,
3652                                                         "WiFi.DomainMatch");
3653         ssid->client_cert_path = connman_network_get_string(network,
3654                                                         "WiFi.ClientCertFile");
3655         ssid->private_key_path = connman_network_get_string(network,
3656                                                         "WiFi.PrivateKeyFile");
3657         ssid->private_key_passphrase = connman_network_get_string(network,
3658                                                 "WiFi.PrivateKeyPassphrase");
3659         ssid->phase2_auth = connman_network_get_string(network, "WiFi.Phase2");
3660
3661         ssid->use_wps = connman_network_get_bool(network, "WiFi.UseWPS");
3662         ssid->pin_wps = connman_network_get_string(network, "WiFi.PinWPS");
3663 #if defined TIZEN_EXT
3664         ssid->connector = connman_network_get_string(network,
3665                                                         "WiFi.Connector");
3666         ssid->c_sign_key = connman_network_get_string(network,
3667                                                         "WiFi.CSignKey");
3668         ssid->net_access_key = connman_network_get_string(network,
3669                                                 "WiFi.NetAccessKey");
3670 #endif
3671
3672 #if defined TIZEN_EXT
3673         const char *ifname = connman_device_get_string(
3674                         connman_network_get_device(network), "Interface");
3675         if (set_connman_bssid(CHECK_BSSID, NULL, ifname) == 6) {
3676                 ssid->bssid_for_connect_len = 6;
3677                 set_connman_bssid(GET_BSSID, (char *)ssid->bssid_for_connect, ifname);
3678                 DBG("BSSID : %02x:%02x:%02x:%02x:%02x:%02x",
3679                         ssid->bssid_for_connect[0], ssid->bssid_for_connect[1],
3680                         ssid->bssid_for_connect[2], ssid->bssid_for_connect[3],
3681                         ssid->bssid_for_connect[4], ssid->bssid_for_connect[5]);
3682         } else {
3683                 ssid->freq = connman_network_get_frequency(network);
3684         }
3685
3686         GSList *bssid_list = (GSList *)connman_network_get_bssid_list(network);
3687         if (bssid_list && g_slist_length(bssid_list) > 1) {
3688
3689                 /* If there are more than one bssid,
3690                  * the user-specified bssid is tried only once at the beginning.
3691                  * After that, the bssids in the list are tried in order.
3692                  */
3693                 if (set_connman_bssid(CHECK_BSSID, NULL, ifname) == 6) {
3694                         set_connman_bssid(RESET_BSSID, NULL, ifname);
3695                         goto done;
3696                 }
3697
3698                 GSList *list;
3699                 char buff[MAC_ADDRESS_LENGTH];
3700                 for (list = bssid_list; list; list = list->next) {
3701                         struct connman_bssids * bssids = (struct connman_bssids *)list->data;
3702
3703                         g_snprintf(buff, MAC_ADDRESS_LENGTH, "%02x:%02x:%02x:%02x:%02x:%02x",
3704                                         bssids->bssid[0], bssids->bssid[1], bssids->bssid[2],
3705                                         bssids->bssid[3], bssids->bssid[4], bssids->bssid[5]);
3706                         buff[MAC_ADDRESS_LENGTH - 1] = '\0';
3707
3708                         gchar *curr_bssid = g_strdup((const gchar *)buff);
3709
3710                         if (g_hash_table_contains(failed_bssids, curr_bssid)) {
3711                                 DBG("bssid match, try next bssid");
3712                                 g_free(curr_bssid);
3713                                 continue;
3714                         } else {
3715                                 g_hash_table_add(failed_bssids, curr_bssid);
3716
3717                                 memcpy(buff_bssid, bssids->bssid, WIFI_BSSID_LEN_MAX);
3718                                 ssid->bssid = buff_bssid;
3719                                 ssid->freq = (unsigned int)bssids->frequency;
3720                                 break;
3721                         }
3722                 }
3723
3724                 if (!list) {
3725                         ssid->bssid = connman_network_get_bssid(network);
3726                         g_hash_table_remove_all(failed_bssids);
3727                 }
3728         } else
3729                 ssid->bssid = connman_network_get_bssid(network);
3730
3731 done:
3732         ssid->eap_keymgmt = network_eap_keymgmt(
3733                         connman_network_get_string(network, "WiFi.KeymgmtType"));
3734         ssid->phase1 = connman_network_get_string(network, "WiFi.Phase1");
3735
3736         if(g_strcmp0(ssid->eap, "fast") == 0)
3737                 ssid->pac_file = g_strdup(WIFI_EAP_FAST_PAC_FILE);
3738
3739         ssid->keymgmt = connman_network_get_keymgmt(network);
3740 #endif
3741
3742         if (connman_setting_get_bool("BackgroundScanning"))
3743                 ssid->bgscan = BGSCAN_DEFAULT;
3744 }
3745
3746 static int network_connect(struct connman_network *network)
3747 {
3748         struct connman_device *device = connman_network_get_device(network);
3749         struct wifi_data *wifi;
3750         GSupplicantInterface *interface;
3751         GSupplicantSSID *ssid;
3752
3753         DBG("network %p", network);
3754
3755         if (!device)
3756                 return -ENODEV;
3757
3758         wifi = connman_device_get_data(device);
3759         if (!wifi)
3760                 return -ENODEV;
3761
3762         ssid = g_try_malloc0(sizeof(GSupplicantSSID));
3763         if (!ssid)
3764                 return -ENOMEM;
3765
3766         interface = wifi->interface;
3767
3768         ssid_init(ssid, network);
3769
3770         if (wifi->disconnecting) {
3771                 wifi->pending_network = network;
3772 #if defined TIZEN_EXT
3773                 g_free(ssid->ssid);
3774 #endif
3775                 g_free(ssid);
3776         } else {
3777                 wifi->network = connman_network_ref(network);
3778                 wifi->retries = 0;
3779 #if defined TIZEN_EXT
3780                 wifi->scan_pending_network = NULL;
3781 #endif
3782
3783                 return g_supplicant_interface_connect(interface, ssid,
3784                                                 connect_callback, network);
3785         }
3786
3787         return -EINPROGRESS;
3788 }
3789
3790 static void disconnect_callback(int result, GSupplicantInterface *interface,
3791                                                                 void *user_data)
3792 {
3793 #if defined TIZEN_EXT
3794         GList *list;
3795         struct wifi_data *wifi;
3796         struct connman_network *network = user_data;
3797
3798         DBG("network %p result %d", network, result);
3799
3800         for (list = iface_list; list; list = list->next) {
3801                 wifi = list->data;
3802
3803                 if (wifi->network == NULL && wifi->disconnecting == true)
3804                         wifi->disconnecting = false;
3805
3806                 if (wifi->network == network)
3807                         goto found;
3808         }
3809
3810         /* wifi_data may be invalid because wifi is already disabled */
3811         return;
3812
3813 found:
3814 #else
3815         struct wifi_data *wifi = user_data;
3816 #endif
3817
3818         DBG("result %d supplicant interface %p wifi %p",
3819                         result, interface, wifi);
3820
3821         if (result == -ECONNABORTED) {
3822                 DBG("wifi interface no longer available");
3823                 return;
3824         }
3825
3826         if (wifi->network && wifi->network != wifi->pending_network)
3827                 connman_network_set_connected(wifi->network, false);
3828         wifi->network = NULL;
3829
3830         wifi->disconnecting = false;
3831         wifi->connected = false;
3832
3833         if (wifi->pending_network) {
3834                 network_connect(wifi->pending_network);
3835                 wifi->pending_network = NULL;
3836         }
3837
3838         start_autoscan(wifi->device);
3839 }
3840
3841 static int network_disconnect(struct connman_network *network)
3842 {
3843         struct connman_device *device = connman_network_get_device(network);
3844         struct wifi_data *wifi;
3845         int err;
3846 #if defined TIZEN_EXT
3847         struct connman_service *service;
3848 #endif
3849
3850         DBG("network %p", network);
3851
3852         wifi = connman_device_get_data(device);
3853         if (!wifi || !wifi->interface)
3854                 return -ENODEV;
3855
3856 #if defined TIZEN_EXT
3857         if (connman_network_get_associating(network) == true) {
3858                 connman_network_clear_associating(network);
3859                 connman_network_set_bool(network, "WiFi.UseWPS", false);
3860         } else {
3861                 service = connman_service_lookup_from_network(network);
3862
3863                 if (service != NULL &&
3864                         (__connman_service_is_connected_state(service,
3865                                         CONNMAN_IPCONFIG_TYPE_IPV4) == false &&
3866                         __connman_service_is_connected_state(service,
3867                                         CONNMAN_IPCONFIG_TYPE_IPV6) == false) &&
3868                         (connman_service_get_favorite(service) == false))
3869                                         __connman_service_set_passphrase(service, NULL);
3870         }
3871
3872         if (wifi->pending_network == network)
3873                 wifi->pending_network = NULL;
3874
3875         if (wifi->scan_pending_network == network)
3876                 wifi->scan_pending_network = NULL;
3877
3878 #endif
3879         connman_network_set_associating(network, false);
3880
3881         if (wifi->disconnecting)
3882                 return -EALREADY;
3883
3884         wifi->disconnecting = true;
3885
3886 #if defined TIZEN_EXT
3887         err = g_supplicant_interface_disconnect(wifi->interface,
3888                                                 disconnect_callback, network);
3889 #else
3890         err = g_supplicant_interface_disconnect(wifi->interface,
3891                                                 disconnect_callback, wifi);
3892 #endif
3893
3894         if (err < 0)
3895                 wifi->disconnecting = false;
3896
3897         return err;
3898 }
3899
3900 #if defined TIZEN_EXT
3901 static void set_connection_mode(struct connman_network *network,
3902                 int linkspeed)
3903 {
3904         ieee80211_modes_e phy_mode;
3905         connection_mode_e conn_mode;
3906
3907         phy_mode = connman_network_get_phy_mode(network);
3908         switch (phy_mode) {
3909         case IEEE80211_MODE_B:
3910                 if (linkspeed > 0 && linkspeed <= 11)
3911                         conn_mode = CONNECTION_MODE_IEEE80211B;
3912                 else
3913                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
3914
3915                 break;
3916         case IEEE80211_MODE_BG:
3917                 if (linkspeed > 0 && linkspeed <= 11)
3918                         conn_mode = CONNECTION_MODE_IEEE80211B;
3919                 else if (linkspeed > 11 && linkspeed <= 54)
3920                         conn_mode = CONNECTION_MODE_IEEE80211G;
3921                 else
3922                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
3923
3924                 break;
3925         case IEEE80211_MODE_BGN:
3926                 if (linkspeed > 0 && linkspeed <= 11)
3927                         conn_mode = CONNECTION_MODE_IEEE80211B;
3928                 else if (linkspeed > 11 && linkspeed <= 54)
3929                         conn_mode = CONNECTION_MODE_IEEE80211G;
3930                 else if (linkspeed > 54 && linkspeed <= 450)
3931                         conn_mode = CONNECTION_MODE_IEEE80211N;
3932                 else
3933                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
3934
3935                 break;
3936         case IEEE80211_MODE_A:
3937                 if (linkspeed > 0 && linkspeed <= 54)
3938                         conn_mode = CONNECTION_MODE_IEEE80211A;
3939                 else
3940                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
3941
3942                 break;
3943         case IEEE80211_MODE_AN:
3944                 if (linkspeed > 0 && linkspeed <= 54)
3945                         conn_mode = CONNECTION_MODE_IEEE80211A;
3946                 else if (linkspeed > 54 && linkspeed <= 450)
3947                         conn_mode = CONNECTION_MODE_IEEE80211N;
3948                 else
3949                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
3950
3951                 break;
3952         case IEEE80211_MODE_ANAC:
3953                 if (linkspeed > 0 && linkspeed <= 54)
3954                         conn_mode = CONNECTION_MODE_IEEE80211A;
3955                 else if (linkspeed > 54 && linkspeed <= 450)
3956                         conn_mode = CONNECTION_MODE_IEEE80211N;
3957                 else if (linkspeed > 450 && linkspeed <= 1300)
3958                         conn_mode = CONNECTION_MODE_IEEE80211AC;
3959                 else
3960                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
3961
3962                 break;
3963         default:
3964                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
3965                 break;
3966         }
3967
3968         DBG("connection mode(%d)", conn_mode);
3969         connman_network_set_connection_mode(network, conn_mode);
3970 }
3971
3972 static void signalpoll_callback(int result, int maxspeed, int strength,
3973                                 void *user_data)
3974 {
3975         struct connman_network *network = user_data;
3976
3977         if (result != 0) {
3978                 DBG("Failed to get maxspeed from signalpoll !");
3979                 connman_network_unref(network);
3980                 return;
3981         }
3982
3983         strength += 120;
3984         if (strength > 100)
3985                 strength = 100;
3986
3987         DBG("maxspeed = %d, strength = %d", maxspeed, strength);
3988
3989         connman_network_set_strength(network, (uint8_t)strength);
3990         connman_network_set_maxspeed(network, maxspeed);
3991         set_connection_mode(network, maxspeed);
3992
3993         connman_network_unref(network);
3994 }
3995
3996 static int network_signalpoll(struct wifi_data *wifi)
3997 {
3998         GSupplicantInterface *interface;
3999         struct connman_network *network;
4000
4001         if (!wifi || !wifi->network)
4002                 return -ENODEV;
4003
4004         wifi->network = connman_network_ref(wifi->network);
4005
4006         interface = wifi->interface;
4007         network = wifi->network;
4008
4009         DBG("network %p", network);
4010
4011         return g_supplicant_interface_signalpoll(interface, signalpoll_callback, network);
4012 }
4013
4014 static gboolean autosignalpoll_timeout(gpointer data)
4015 {
4016         struct wifi_data *wifi = data;
4017
4018         if (!wifi || !wifi->automaxspeed_timeout) {
4019                 DBG("automaxspeed_timeout is found to be zero. i.e. currently in disconnected state. !!");
4020                 return FALSE;
4021         }
4022
4023         int ret = network_signalpoll(wifi);
4024         if (ret < 0) {
4025                 DBG("Fail to get max speed !!");
4026                 wifi->automaxspeed_timeout = 0;
4027
4028                 if (wifi->network)
4029                         connman_network_unref(wifi->network);
4030                 return FALSE;
4031         }
4032
4033         return TRUE;
4034 }
4035 #endif
4036
4037 static struct connman_network_driver network_driver = {
4038         .name           = "wifi",
4039         .type           = CONNMAN_NETWORK_TYPE_WIFI,
4040         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
4041         .probe          = network_probe,
4042         .remove         = network_remove,
4043         .connect        = network_connect,
4044         .disconnect     = network_disconnect,
4045 };
4046
4047 static void interface_added(GSupplicantInterface *interface)
4048 {
4049         const char *ifname = g_supplicant_interface_get_ifname(interface);
4050         const char *driver = g_supplicant_interface_get_driver(interface);
4051 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4052         /*
4053          * Note: If supplicant interface's driver is wired then skip it,
4054          * because it meanti only for ethernet not Wi-Fi.
4055          */
4056         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4057                 return;
4058 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4059
4060 #if defined TIZEN_EXT
4061         bool is_5_0_ghz_supported = g_supplicant_interface_get_is_5_0_ghz_supported(interface);
4062 #endif
4063
4064         struct wifi_data *wifi;
4065
4066         wifi = g_supplicant_interface_get_data(interface);
4067         if (!wifi) {
4068                 wifi = get_pending_wifi_data(ifname);
4069                 if (!wifi)
4070                         return;
4071
4072                 wifi->interface = interface;
4073                 g_supplicant_interface_set_data(interface, wifi);
4074                 p2p_iface_list = g_list_append(p2p_iface_list, wifi);
4075                 wifi->p2p_device = true;
4076         }
4077
4078         DBG("ifname %s driver %s wifi %p tethering %d",
4079                         ifname, driver, wifi, wifi->tethering);
4080
4081         if (!wifi->device) {
4082                 connman_error("WiFi device not set");
4083                 return;
4084         }
4085
4086         connman_device_set_powered(wifi->device, true);
4087 #if defined TIZEN_EXT
4088         connman_device_set_wifi_5ghz_supported(wifi->device, is_5_0_ghz_supported);
4089         /* Max number of SSIDs supported by wlan chipset that can be scanned */
4090         int max_scan_ssids = g_supplicant_interface_get_max_scan_ssids(interface);
4091         connman_device_set_max_scan_ssids(wifi->device, max_scan_ssids);
4092 #endif
4093 }
4094
4095 static bool is_idle(struct wifi_data *wifi)
4096 {
4097         DBG("state %d", wifi->state);
4098
4099         switch (wifi->state) {
4100         case G_SUPPLICANT_STATE_UNKNOWN:
4101         case G_SUPPLICANT_STATE_DISABLED:
4102         case G_SUPPLICANT_STATE_DISCONNECTED:
4103         case G_SUPPLICANT_STATE_INACTIVE:
4104         case G_SUPPLICANT_STATE_SCANNING:
4105                 return true;
4106
4107         case G_SUPPLICANT_STATE_AUTHENTICATING:
4108         case G_SUPPLICANT_STATE_ASSOCIATING:
4109         case G_SUPPLICANT_STATE_ASSOCIATED:
4110         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4111         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4112         case G_SUPPLICANT_STATE_COMPLETED:
4113                 return false;
4114         }
4115
4116         return false;
4117 }
4118
4119 static bool is_idle_wps(GSupplicantInterface *interface,
4120                                                 struct wifi_data *wifi)
4121 {
4122         /* First, let's check if WPS processing did not went wrong */
4123         if (g_supplicant_interface_get_wps_state(interface) ==
4124                 G_SUPPLICANT_WPS_STATE_FAIL)
4125                 return false;
4126
4127         /* Unlike normal connection, being associated while processing wps
4128          * actually means that we are idling. */
4129         switch (wifi->state) {
4130         case G_SUPPLICANT_STATE_UNKNOWN:
4131         case G_SUPPLICANT_STATE_DISABLED:
4132         case G_SUPPLICANT_STATE_DISCONNECTED:
4133         case G_SUPPLICANT_STATE_INACTIVE:
4134         case G_SUPPLICANT_STATE_SCANNING:
4135         case G_SUPPLICANT_STATE_ASSOCIATED:
4136                 return true;
4137         case G_SUPPLICANT_STATE_AUTHENTICATING:
4138         case G_SUPPLICANT_STATE_ASSOCIATING:
4139         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4140         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4141         case G_SUPPLICANT_STATE_COMPLETED:
4142                 return false;
4143         }
4144
4145         return false;
4146 }
4147
4148 static bool handle_wps_completion(GSupplicantInterface *interface,
4149                                         struct connman_network *network,
4150                                         struct connman_device *device,
4151                                         struct wifi_data *wifi)
4152 {
4153         bool wps;
4154
4155         wps = connman_network_get_bool(network, "WiFi.UseWPS");
4156         if (wps) {
4157                 const unsigned char *ssid, *wps_ssid;
4158                 unsigned int ssid_len, wps_ssid_len;
4159                 const char *wps_key;
4160
4161                 /* Checking if we got associated with requested
4162                  * network */
4163                 ssid = connman_network_get_blob(network, "WiFi.SSID",
4164                                                 &ssid_len);
4165
4166                 wps_ssid = g_supplicant_interface_get_wps_ssid(
4167                         interface, &wps_ssid_len);
4168
4169                 if (!wps_ssid || wps_ssid_len != ssid_len ||
4170                                 memcmp(ssid, wps_ssid, ssid_len) != 0) {
4171                         connman_network_set_associating(network, false);
4172 #if defined TIZEN_EXT
4173                         g_supplicant_interface_disconnect(wifi->interface,
4174                                                 disconnect_callback, wifi->network);
4175
4176                         connman_network_set_bool(network, "WiFi.UseWPS", false);
4177                         connman_network_set_string(network, "WiFi.PinWPS", NULL);
4178 #else
4179                         g_supplicant_interface_disconnect(wifi->interface,
4180                                                 disconnect_callback, wifi);
4181 #endif
4182                         return false;
4183                 }
4184
4185                 wps_key = g_supplicant_interface_get_wps_key(interface);
4186 #if defined TIZEN_EXT
4187                 /* Check the passphrase and encrypt it
4188                  */
4189                  int ret;
4190                  gchar *passphrase = g_strdup(wps_key);
4191
4192                  connman_network_set_string(network, "WiFi.PinWPS", NULL);
4193
4194                  if (check_passphrase_ext(network, passphrase) < 0) {
4195                          DBG("[WPS] Invalid passphrase");
4196                          g_free(passphrase);
4197                          return true;
4198                  }
4199
4200                  ret = send_encryption_request(passphrase, network);
4201
4202                  g_free(passphrase);
4203
4204                  if (!ret)
4205                          DBG("[WPS] Encryption request succeeded");
4206                  else
4207                          DBG("[WPS] Encryption request failed %d", ret);
4208
4209 #else
4210                 connman_network_set_string(network, "WiFi.Passphrase",
4211                                         wps_key);
4212
4213                 connman_network_set_string(network, "WiFi.PinWPS", NULL);
4214 #endif
4215         }
4216
4217         return true;
4218 }
4219
4220 static bool handle_assoc_status_code(GSupplicantInterface *interface,
4221                                      struct wifi_data *wifi)
4222 {
4223         if (wifi->state == G_SUPPLICANT_STATE_ASSOCIATING &&
4224 #if defined TIZEN_EXT
4225                         wifi->assoc_code > 0 &&
4226 #else
4227                         wifi->assoc_code == ASSOC_STATUS_NO_CLIENT &&
4228 #endif
4229                         wifi->load_shaping_retries < LOAD_SHAPING_MAX_RETRIES) {
4230                 wifi->load_shaping_retries ++;
4231                 return TRUE;
4232         }
4233         wifi->load_shaping_retries = 0;
4234         return FALSE;
4235 }
4236
4237 static bool handle_4way_handshake_failure(GSupplicantInterface *interface,
4238                                         struct connman_network *network,
4239                                         struct wifi_data *wifi)
4240 {
4241 #if defined TIZEN_EXT
4242         const char *security;
4243         struct connman_service *service;
4244
4245         if (wifi->connected)
4246                 return false;
4247
4248         security = connman_network_get_string(network, "WiFi.Security");
4249
4250         if (security && g_str_equal(security, "ieee8021x") == true &&
4251                         wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) {
4252                 wifi->retries = 0;
4253                 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
4254
4255                 return false;
4256         }
4257
4258         if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
4259                 return false;
4260 #else
4261         struct connman_service *service;
4262
4263         if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
4264                 return false;
4265
4266         if (wifi->connected)
4267                 return false;
4268 #endif
4269
4270         service = connman_service_lookup_from_network(network);
4271         if (!service)
4272                 return false;
4273
4274         wifi->retries++;
4275
4276         if (connman_service_get_favorite(service)) {
4277                 if (wifi->retries < FAVORITE_MAXIMUM_RETRIES)
4278                         return true;
4279         }
4280
4281         wifi->retries = 0;
4282         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
4283
4284         return false;
4285 }
4286
4287 #if defined TIZEN_EXT
4288 static bool handle_wifi_assoc_retry(struct connman_network *network,
4289                                         struct wifi_data *wifi)
4290 {
4291         const char *security;
4292
4293         if (!wifi->network || wifi->connected || wifi->disconnecting ||
4294                         connman_network_get_connecting(network) != true) {
4295                 wifi->assoc_retry_count = 0;
4296                 return false;
4297         }
4298
4299         if (wifi->state != G_SUPPLICANT_STATE_ASSOCIATING &&
4300                         wifi->state != G_SUPPLICANT_STATE_ASSOCIATED) {
4301                 wifi->assoc_retry_count = 0;
4302                 return false;
4303         }
4304
4305         security = connman_network_get_string(network, "WiFi.Security");
4306         if (security && g_str_equal(security, "ieee8021x") == true &&
4307                         wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) {
4308                 wifi->assoc_retry_count = 0;
4309                 return false;
4310         }
4311
4312         if (++wifi->assoc_retry_count >= TIZEN_ASSOC_RETRY_COUNT) {
4313                 wifi->assoc_retry_count = 0;
4314
4315                 /* Honestly it's not an invalid-key error,
4316                  * however QA team recommends that the invalid-key error
4317                  * might be better to display for user experience.
4318                  */
4319                 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
4320
4321                 return false;
4322         }
4323
4324         return true;
4325 }
4326 #endif
4327
4328 static void interface_state(GSupplicantInterface *interface)
4329 {
4330         struct connman_network *network;
4331         struct connman_device *device;
4332         struct wifi_data *wifi;
4333         GSupplicantState state = g_supplicant_interface_get_state(interface);
4334 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4335         /*
4336          * Note: If supplicant interface's driver is wired then skip it,
4337          * because it meanti only for ethernet not Wi-Fi.
4338          */
4339         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4340                 return;
4341 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4342
4343         bool wps;
4344         bool old_connected;
4345
4346         wifi = g_supplicant_interface_get_data(interface);
4347
4348         DBG("wifi %p interface state %d", wifi, state);
4349
4350         if (!wifi)
4351                 return;
4352
4353         device = wifi->device;
4354         if (!device)
4355                 return;
4356
4357         if (state == G_SUPPLICANT_STATE_COMPLETED) {
4358                 if (wifi->tethering_param) {
4359                         g_free(wifi->tethering_param->ssid);
4360                         g_free(wifi->tethering_param);
4361                         wifi->tethering_param = NULL;
4362                 }
4363
4364                 if (wifi->tethering)
4365                         stop_autoscan(device);
4366         }
4367
4368         if (g_supplicant_interface_get_ready(interface) &&
4369                                         !wifi->interface_ready) {
4370                 wifi->interface_ready = true;
4371                 finalize_interface_creation(wifi);
4372         }
4373
4374         network = wifi->network;
4375         if (!network)
4376                 return;
4377
4378         switch (state) {
4379         case G_SUPPLICANT_STATE_SCANNING:
4380                 if (wifi->connected)
4381                         connman_network_set_connected(network, false);
4382
4383                 break;
4384
4385         case G_SUPPLICANT_STATE_AUTHENTICATING:
4386         case G_SUPPLICANT_STATE_ASSOCIATING:
4387 #if defined TIZEN_EXT
4388                 reset_autoscan(device);
4389 #else
4390                 stop_autoscan(device);
4391 #endif
4392
4393                 if (!wifi->connected)
4394                         connman_network_set_associating(network, true);
4395
4396                 break;
4397
4398         case G_SUPPLICANT_STATE_COMPLETED:
4399 #if defined TIZEN_EXT
4400                 /* though it should be already reset: */
4401                 reset_autoscan(device);
4402
4403                 wifi->assoc_retry_count = 0;
4404
4405                 wifi->scan_pending_network = NULL;
4406
4407                 /* should be cleared scanning flag */
4408                 bool scanning = connman_device_get_scanning(device,
4409                                                CONNMAN_SERVICE_TYPE_WIFI);
4410                 if (scanning){
4411                         connman_device_set_scanning(device,
4412                                 CONNMAN_SERVICE_TYPE_WIFI, false);
4413                         connman_device_unref(device);
4414                 }
4415
4416                 if (!wifi->automaxspeed_timeout) {
4417                         DBG("Going to start signalpoll timer!!");
4418                         int ret = network_signalpoll(wifi);
4419                         if (ret < 0)
4420                                 DBG("Fail to get max speed !!");
4421                         else
4422                                 wifi->automaxspeed_timeout = g_timeout_add_seconds(10, autosignalpoll_timeout, wifi);
4423                 }
4424
4425                 g_hash_table_remove_all(failed_bssids);
4426 #else
4427                 /* though it should be already stopped: */
4428                 stop_autoscan(device);
4429 #endif
4430
4431                 if (!handle_wps_completion(interface, network, device, wifi))
4432                         break;
4433
4434                 connman_network_set_connected(network, true);
4435
4436                 wifi->disconnect_code = 0;
4437                 wifi->assoc_code = 0;
4438                 wifi->load_shaping_retries = 0;
4439                 break;
4440
4441         case G_SUPPLICANT_STATE_DISCONNECTED:
4442 #if defined TIZEN_EXT
4443                 connman_network_set_strength(network, 0);
4444                 connman_network_set_maxspeed(network, 0);
4445
4446                 if (wifi->automaxspeed_timeout != 0) {
4447                         g_source_remove(wifi->automaxspeed_timeout);
4448                         wifi->automaxspeed_timeout = 0;
4449                         DBG("Remove signalpoll timer!!");
4450                 }
4451 #endif
4452                 /*
4453                  * If we're in one of the idle modes, we have
4454                  * not started association yet and thus setting
4455                  * those ones to FALSE could cancel an association
4456                  * in progress.
4457                  */
4458                 wps = connman_network_get_bool(network, "WiFi.UseWPS");
4459                 if (wps)
4460                         if (is_idle_wps(interface, wifi))
4461                                 break;
4462
4463                 if (is_idle(wifi))
4464                         break;
4465
4466 #if defined TIZEN_EXT
4467                 if (handle_assoc_status_code(interface, wifi)) {
4468                         GSList *bssid_list = (GSList *)connman_network_get_bssid_list(network);
4469                         const char *group = connman_network_get_group(network);
4470                         GSupplicantNetwork *supplicant_network;
4471                         guint bssid_length = 0;
4472
4473                         if (group) {
4474                                 supplicant_network = g_supplicant_interface_get_network(interface, group);
4475
4476                                 connman_network_set_assoc_reject_table(network,
4477                                         g_supplicant_network_get_assoc_reject_table(supplicant_network));
4478
4479                                 g_supplicant_network_update_assoc_reject(interface, supplicant_network);
4480                         }
4481
4482                         if (bssid_list)
4483                                 bssid_length = g_slist_length(bssid_list);
4484
4485                         if (bssid_length > 1 && bssid_length > g_hash_table_size(failed_bssids)) {
4486                                 network_connect(network);
4487                                 break;
4488                         }
4489
4490                         wifi->load_shaping_retries = 0;
4491                 }
4492
4493                 g_hash_table_remove_all(failed_bssids);
4494 #else
4495                 if (handle_assoc_status_code(interface, wifi))
4496                         break;
4497 #endif
4498
4499                 /* If previous state was 4way-handshake, then
4500                  * it's either: psk was incorrect and thus we retry
4501                  * or if we reach the maximum retries we declare the
4502                  * psk as wrong */
4503                 if (handle_4way_handshake_failure(interface,
4504                                                 network, wifi))
4505                         break;
4506
4507                 /* See table 8-36 Reason codes in IEEE Std 802.11 */
4508                 switch (wifi->disconnect_code) {
4509                 case 1: /* Unspecified reason */
4510                         /* Let's assume it's because we got blocked */
4511
4512                 case 6: /* Class 2 frame received from nonauthenticated STA */
4513                         connman_network_set_error(network,
4514                                                 CONNMAN_NETWORK_ERROR_BLOCKED);
4515                         break;
4516
4517                 default:
4518                         break;
4519                 }
4520
4521 #if defined TIZEN_EXT
4522                 /* Some of Wi-Fi networks are not comply Wi-Fi specification.
4523                  * Retry association until its retry count is expired */
4524                 if (handle_wifi_assoc_retry(network, wifi) == true) {
4525                         throw_wifi_scan(wifi->device, scan_callback);
4526                         wifi->scan_pending_network = wifi->network;
4527                         break;
4528                 }
4529
4530                 if(wifi->disconnect_code > 0){
4531                         DBG("Set disconnect reason code(%d)", wifi->disconnect_code);
4532                         connman_network_set_disconnect_reason(network, wifi->disconnect_code);
4533                 }
4534 #endif
4535
4536                 if (network != wifi->pending_network) {
4537                         connman_network_set_connected(network, false);
4538                         connman_network_set_associating(network, false);
4539                 }
4540                 wifi->disconnecting = false;
4541
4542                 start_autoscan(device);
4543
4544                 break;
4545
4546         case G_SUPPLICANT_STATE_INACTIVE:
4547 #if defined TIZEN_EXT
4548                 if (handle_wps_completion(interface, network, device, wifi) == false)
4549                         break;
4550 #endif
4551                 connman_network_set_associating(network, false);
4552                 start_autoscan(device);
4553
4554                 break;
4555
4556         case G_SUPPLICANT_STATE_UNKNOWN:
4557         case G_SUPPLICANT_STATE_DISABLED:
4558         case G_SUPPLICANT_STATE_ASSOCIATED:
4559         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4560         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4561                 break;
4562         }
4563
4564         old_connected = wifi->connected;
4565         wifi->state = state;
4566
4567         /* Saving wpa_s state policy:
4568          * If connected and if the state changes are roaming related:
4569          * --> We stay connected
4570          * If completed
4571          * --> We are connected
4572          * All other case:
4573          * --> We are not connected
4574          * */
4575         switch (state) {
4576         case G_SUPPLICANT_STATE_AUTHENTICATING:
4577         case G_SUPPLICANT_STATE_ASSOCIATING:
4578         case G_SUPPLICANT_STATE_ASSOCIATED:
4579         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4580         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4581                 if (wifi->connected)
4582                         connman_warn("Probably roaming right now!"
4583                                                 " Staying connected...");
4584                 break;
4585         case G_SUPPLICANT_STATE_SCANNING:
4586                 wifi->connected = false;
4587
4588                 if (old_connected)
4589                         start_autoscan(device);
4590                 break;
4591         case G_SUPPLICANT_STATE_COMPLETED:
4592                 wifi->connected = true;
4593                 break;
4594         default:
4595                 wifi->connected = false;
4596                 break;
4597         }
4598
4599         DBG("DONE");
4600 }
4601
4602 static void interface_removed(GSupplicantInterface *interface)
4603 {
4604         const char *ifname = g_supplicant_interface_get_ifname(interface);
4605         struct wifi_data *wifi;
4606 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4607         /*
4608          * Note: If supplicant interface's driver is wired then skip it,
4609          * because it meanti only for ethernet not Wi-Fi.
4610          */
4611         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4612                 return;
4613 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4614
4615
4616         DBG("ifname %s", ifname);
4617
4618         wifi = g_supplicant_interface_get_data(interface);
4619
4620 #if defined TIZEN_EXT_WIFI_MESH
4621         if (wifi && wifi->mesh_interface) {
4622                 DBG("Notify mesh interface remove");
4623                 connman_mesh_notify_interface_remove(true);
4624                 struct wifi_mesh_info *mesh_info = wifi->mesh_info;
4625                 g_free(mesh_info->parent_ifname);
4626                 g_free(mesh_info->ifname);
4627                 g_free(mesh_info->identifier);
4628                 g_free(mesh_info);
4629                 wifi->mesh_interface = false;
4630                 wifi->mesh_info = NULL;
4631                 return;
4632         }
4633 #endif
4634
4635         if (wifi)
4636                 wifi->interface = NULL;
4637
4638         if (wifi && wifi->tethering)
4639                 return;
4640
4641         if (!wifi || !wifi->device) {
4642                 DBG("wifi interface already removed");
4643                 return;
4644         }
4645
4646         connman_device_set_powered(wifi->device, false);
4647
4648         check_p2p_technology();
4649 #if defined TIZEN_EXT_WIFI_MESH
4650         check_mesh_technology();
4651 #endif
4652 }
4653
4654 static void set_device_type(const char *type, char dev_type[17])
4655 {
4656         const char *oui = "0050F204";
4657         const char *category = "0001";
4658         const char *sub_category = "0000";
4659
4660         if (!g_strcmp0(type, "handset")) {
4661                 category = "000A";
4662                 sub_category = "0005";
4663         } else if (!g_strcmp0(type, "vm") || !g_strcmp0(type, "container"))
4664                 sub_category = "0001";
4665         else if (!g_strcmp0(type, "server"))
4666                 sub_category = "0002";
4667         else if (!g_strcmp0(type, "laptop"))
4668                 sub_category = "0005";
4669         else if (!g_strcmp0(type, "desktop"))
4670                 sub_category = "0006";
4671         else if (!g_strcmp0(type, "tablet"))
4672                 sub_category = "0009";
4673         else if (!g_strcmp0(type, "watch"))
4674                 category = "00FF";
4675
4676         snprintf(dev_type, 17, "%s%s%s", category, oui, sub_category);
4677 }
4678
4679 static void p2p_support(GSupplicantInterface *interface)
4680 {
4681         char dev_type[17] = {};
4682         const char *hostname;
4683 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4684         /*
4685          * Note: If supplicant interface's driver is wired then skip it,
4686          * because it meanti only for ethernet not Wi-Fi.
4687          */
4688         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4689                 return;
4690 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4691
4692
4693         DBG("");
4694
4695         if (!interface)
4696                 return;
4697
4698         if (!g_supplicant_interface_has_p2p(interface))
4699                 return;
4700
4701         if (connman_technology_driver_register(&p2p_tech_driver) < 0) {
4702                 DBG("Could not register P2P technology driver");
4703                 return;
4704         }
4705
4706         hostname = connman_utsname_get_hostname();
4707         if (!hostname)
4708                 hostname = "ConnMan";
4709
4710         set_device_type(connman_machine_get_type(), dev_type);
4711         g_supplicant_interface_set_p2p_device_config(interface,
4712                                                         hostname, dev_type);
4713         connman_peer_driver_register(&peer_driver);
4714 }
4715
4716 static void scan_started(GSupplicantInterface *interface)
4717 {
4718 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4719         /*
4720          * Note: If supplicant interface's driver is wired then skip it,
4721          * because it meanti only for ethernet not Wi-Fi.
4722          */
4723         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4724                 return;
4725 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4726
4727         DBG("");
4728 }
4729
4730 static void scan_finished(GSupplicantInterface *interface)
4731 {
4732 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4733         /*
4734          * Note: If supplicant interface's driver is wired then skip it,
4735          * because it meanti only for ethernet not Wi-Fi.
4736          */
4737         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4738                 return;
4739 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4740
4741 #if defined TIZEN_EXT
4742         struct wifi_data *wifi;
4743         bool is_associating = false;
4744         static bool is_scanning = true;
4745 #endif
4746
4747         DBG("");
4748
4749 #if defined TIZEN_EXT
4750         wifi = g_supplicant_interface_get_data(interface);
4751         if (wifi && wifi->scan_pending_network) {
4752                 network_connect(wifi->scan_pending_network);
4753                 wifi->scan_pending_network = NULL;
4754         }
4755
4756         //service state - associating
4757         if(!wifi || !wifi->network)
4758                 return;
4759
4760         is_associating = connman_network_get_associating(wifi->network);
4761         if(is_associating && is_scanning){
4762                 is_scanning = false;
4763                 DBG("send scan for connecting");
4764                 throw_wifi_scan(wifi->device, scan_callback);
4765
4766                 return;
4767         }
4768         is_scanning = true;
4769
4770         //go scan
4771
4772 #endif
4773 }
4774
4775 static void ap_create_fail(GSupplicantInterface *interface)
4776 {
4777 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4778         /*
4779          * Note: If supplicant interface's driver is wired then skip it,
4780          * because it meanti only for ethernet not Wi-Fi.
4781          */
4782         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4783                 return;
4784 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4785
4786         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
4787         int ret;
4788
4789         if ((wifi->tethering) && (wifi->tethering_param)) {
4790                 DBG("%s create AP fail \n",
4791                                 g_supplicant_interface_get_ifname(wifi->interface));
4792
4793                 connman_inet_remove_from_bridge(wifi->index, wifi->bridge);
4794                 wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
4795                 wifi->tethering = false;
4796
4797                 ret = tech_set_tethering(wifi->tethering_param->technology,
4798                                 wifi->tethering_param->ssid->ssid,
4799                                 wifi->tethering_param->ssid->passphrase,
4800                                 wifi->bridge, true);
4801
4802                 if ((ret == -EOPNOTSUPP) && (wifi_technology)) {
4803                         connman_technology_tethering_notify(wifi_technology,false);
4804                 }
4805
4806                 g_free(wifi->tethering_param->ssid);
4807                 g_free(wifi->tethering_param);
4808                 wifi->tethering_param = NULL;
4809         }
4810 }
4811
4812 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
4813 {
4814         unsigned char strength;
4815
4816         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
4817 #if !defined TIZEN_EXT
4818         if (strength > 100)
4819                 strength = 100;
4820 #endif
4821
4822         return strength;
4823 }
4824
4825 #if defined TIZEN_EXT_WIFI_MESH
4826 static void mesh_peer_added(GSupplicantNetwork *supplicant_network)
4827 {
4828         GSupplicantInterface *interface;
4829         struct wifi_data *wifi;
4830         const char *name, *security;
4831         struct connman_mesh *connman_mesh;
4832         struct wifi_mesh_info *mesh_info;
4833         const unsigned char *bssid;
4834         const char *identifier;
4835         char *address;
4836         uint16_t frequency;
4837         int ret;
4838
4839         interface = g_supplicant_network_get_interface(supplicant_network);
4840         wifi = g_supplicant_interface_get_data(interface);
4841         if (!wifi || !wifi->mesh_interface) {
4842                 DBG("Virtual Mesh interface not created");
4843                 return;
4844         }
4845
4846         bssid = g_supplicant_network_get_bssid(supplicant_network);
4847         address = g_malloc0(19);
4848         snprintf(address, 19, "%02x:%02x:%02x:%02x:%02x:%02x", bssid[0], bssid[1],
4849                                                                  bssid[2], bssid[3], bssid[4], bssid[5]);
4850
4851         identifier = g_supplicant_network_get_identifier(supplicant_network);
4852         name = g_supplicant_network_get_name(supplicant_network);
4853         security = g_supplicant_network_get_security(supplicant_network);
4854         frequency = g_supplicant_network_get_frequency(supplicant_network);
4855
4856         mesh_info = wifi->mesh_info;
4857         connman_mesh = connman_mesh_get(mesh_info->identifier, identifier);
4858         if (connman_mesh)
4859                 goto done;
4860
4861         DBG("Mesh Peer name %s identifier %s security %s added", name, identifier,
4862                                         security);
4863         connman_mesh = connman_mesh_create(mesh_info->identifier, identifier);
4864         connman_mesh_set_name(connman_mesh, name);
4865         connman_mesh_set_security(connman_mesh, security);
4866         connman_mesh_set_frequency(connman_mesh, frequency);
4867         connman_mesh_set_address(connman_mesh, address);
4868         connman_mesh_set_index(connman_mesh, mesh_info->index);
4869         connman_mesh_set_strength(connman_mesh,
4870                                                 calculate_strength(supplicant_network));
4871         connman_mesh_set_peer_type(connman_mesh, CONNMAN_MESH_PEER_TYPE_DISCOVERED);
4872
4873         ret = connman_mesh_register(connman_mesh);
4874         if (ret == -EALREADY)
4875                 DBG("Mesh Peer is already registered");
4876
4877 done:
4878         g_free(address);
4879 }
4880
4881 static void mesh_peer_removed(GSupplicantNetwork *supplicant_network)
4882 {
4883         GSupplicantInterface *interface;
4884         struct wifi_data *wifi;
4885         struct connman_mesh *connman_mesh;
4886         struct wifi_mesh_info *mesh_info;
4887         const char *identifier;
4888
4889         interface = g_supplicant_network_get_interface(supplicant_network);
4890         wifi = g_supplicant_interface_get_data(interface);
4891         if (!wifi || !wifi->mesh_interface) {
4892                 DBG("Virtual Mesh interface not created");
4893                 return;
4894         }
4895
4896         identifier = g_supplicant_network_get_identifier(supplicant_network);
4897         if (!identifier) {
4898                 DBG("Failed to get Mesh Peer identifier");
4899                 return;
4900         }
4901
4902         mesh_info = wifi->mesh_info;
4903         connman_mesh = connman_mesh_get(mesh_info->identifier, identifier);
4904         if (connman_mesh) {
4905                 /* Do not unregister connected mesh peer */
4906                 if (connman_mesh_peer_is_connected_state(connman_mesh)) {
4907                         DBG("Mesh Peer %s is connected", identifier);
4908                         return;
4909                 }
4910                 DBG("Mesh Peer identifier %s removed", identifier);
4911                 connman_mesh_unregister(connman_mesh);
4912         }
4913 }
4914 #endif
4915
4916 static void network_added(GSupplicantNetwork *supplicant_network)
4917 {
4918         struct connman_network *network;
4919         GSupplicantInterface *interface;
4920         struct wifi_data *wifi;
4921         const char *name, *identifier, *security, *group, *mode;
4922         const unsigned char *ssid;
4923         unsigned int ssid_len;
4924         bool wps;
4925         bool wps_pbc;
4926         bool wps_ready;
4927         bool wps_advertizing;
4928
4929 #if defined TIZEN_EXT
4930         bool owe_transition_mode;
4931         const unsigned char *transition_mode_ssid;
4932         const unsigned char *transition_mode_bssid;
4933         unsigned int transition_mode_ssid_len;
4934         GSList *vsie_list = NULL;
4935         const unsigned char *country_code;
4936         ieee80211_modes_e phy_mode;
4937 #endif
4938
4939         mode = g_supplicant_network_get_mode(supplicant_network);
4940         identifier = g_supplicant_network_get_identifier(supplicant_network);
4941 #if defined TIZEN_EXT
4942         if (!simplified_log)
4943 #endif
4944         DBG("%s", identifier);
4945
4946         if (!g_strcmp0(mode, "adhoc"))
4947                 return;
4948
4949 #if defined TIZEN_EXT_WIFI_MESH
4950         if (!g_strcmp0(mode, "mesh")) {
4951                 mesh_peer_added(supplicant_network);
4952                 return;
4953         }
4954 #endif
4955
4956         interface = g_supplicant_network_get_interface(supplicant_network);
4957 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4958         /*
4959          * Note: If supplicant interface's driver is wired then skip it,
4960          * because it meanti only for ethernet not Wi-Fi.
4961          */
4962         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4963                 return;
4964 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4965
4966         wifi = g_supplicant_interface_get_data(interface);
4967         name = g_supplicant_network_get_name(supplicant_network);
4968         security = g_supplicant_network_get_security(supplicant_network);
4969         group = g_supplicant_network_get_identifier(supplicant_network);
4970         wps = g_supplicant_network_get_wps(supplicant_network);
4971         wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
4972         wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
4973         wps_advertizing = g_supplicant_network_is_wps_advertizing(
4974                                                         supplicant_network);
4975
4976         if (!wifi)
4977                 return;
4978
4979         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
4980
4981         network = connman_device_get_network(wifi->device, identifier);
4982
4983         if (!network) {
4984                 network = connman_network_create(identifier,
4985                                                 CONNMAN_NETWORK_TYPE_WIFI);
4986                 if (!network)
4987                         return;
4988
4989                 connman_network_set_index(network, wifi->index);
4990
4991                 if (connman_device_add_network(wifi->device, network) < 0) {
4992                         connman_network_unref(network);
4993                         return;
4994                 }
4995
4996                 wifi->networks = g_slist_prepend(wifi->networks, network);
4997         }
4998
4999         if (name && name[0] != '\0')
5000                 connman_network_set_name(network, name);
5001
5002         connman_network_set_blob(network, "WiFi.SSID",
5003                                                 ssid, ssid_len);
5004 #if defined TIZEN_EXT
5005         vsie_list = (GSList *)g_supplicant_network_get_wifi_vsie(supplicant_network);
5006         if (vsie_list)
5007                 connman_network_set_vsie_list(network, vsie_list);
5008         else
5009                 DBG("vsie_list is NULL");
5010         country_code = g_supplicant_network_get_countrycode(supplicant_network);
5011         connman_network_set_countrycode(network, country_code);
5012         phy_mode = g_supplicant_network_get_phy_mode(supplicant_network);
5013         connman_network_set_phy_mode(network, phy_mode);
5014 #endif
5015         connman_network_set_string(network, "WiFi.Security", security);
5016         connman_network_set_strength(network,
5017                                 calculate_strength(supplicant_network));
5018         connman_network_set_bool(network, "WiFi.WPS", wps);
5019         connman_network_set_bool(network, "WiFi.WPSAdvertising",
5020                                 wps_advertizing);
5021
5022         if (wps) {
5023                 /* Is AP advertizing for WPS association?
5024                  * If so, we decide to use WPS by default */
5025                 if (wps_ready && wps_pbc &&
5026                                                 wps_advertizing)
5027 #if !defined TIZEN_EXT
5028                         connman_network_set_bool(network, "WiFi.UseWPS", true);
5029 #else
5030                         DBG("wps is activating by ap but ignore it.");
5031 #endif
5032         }
5033
5034         connman_network_set_frequency(network,
5035                         g_supplicant_network_get_frequency(supplicant_network));
5036
5037 #if defined TIZEN_EXT
5038         connman_network_set_bssid(network,
5039                         g_supplicant_network_get_bssid(supplicant_network));
5040         owe_transition_mode = (bool)g_supplicant_network_get_transition_mode(supplicant_network);
5041         connman_network_set_bool(network, "WiFi.TRANSITION_MODE", owe_transition_mode);
5042         if (owe_transition_mode) {
5043                 transition_mode_ssid = (unsigned char *)g_supplicant_network_get_transition_mode_ssid(supplicant_network, &transition_mode_ssid_len);
5044                 connman_network_set_blob(network, "WiFi.TRANSITION_MODE_SSID",
5045                                                         transition_mode_ssid, transition_mode_ssid_len);
5046                 transition_mode_bssid = g_supplicant_network_get_transition_mode_bssid(supplicant_network);
5047                 connman_network_set_transition_mode_bssid(network, transition_mode_bssid);
5048         }
5049         connman_network_set_maxrate(network,
5050                         g_supplicant_network_get_maxrate(supplicant_network));
5051         connman_network_set_enc_mode(network,
5052                         g_supplicant_network_get_enc_mode(supplicant_network));
5053         connman_network_set_rsn_mode(network,
5054                         g_supplicant_network_get_rsn_mode(supplicant_network));
5055         connman_network_set_keymgmt(network,
5056                         g_supplicant_network_get_keymgmt(supplicant_network));
5057         connman_network_set_bool(network, "WiFi.HS20AP",
5058                         g_supplicant_network_is_hs20AP(supplicant_network));
5059         connman_network_set_bssid_list(network,
5060                         (GSList *)g_supplicant_network_get_bssid_list(supplicant_network));
5061         connman_network_set_last_connected_bssid(network,
5062                         g_supplicant_network_get_last_connected_bssid(supplicant_network));
5063         connman_network_set_assoc_reject_table(network,
5064                         g_supplicant_network_get_assoc_reject_table(supplicant_network));
5065 #endif
5066         connman_network_set_available(network, true);
5067         connman_network_set_string(network, "WiFi.Mode", mode);
5068
5069 #if defined TIZEN_EXT
5070         if (group)
5071 #else
5072         if (ssid)
5073 #endif
5074                 connman_network_set_group(network, group);
5075
5076 #if defined TIZEN_EXT
5077         g_supplicant_network_set_last_connected_bssid(supplicant_network,
5078                         connman_network_get_last_connected_bssid(network));
5079 #endif
5080
5081 #if defined TIZEN_EXT
5082         if (wifi_first_scan == true)
5083                 found_with_first_scan = true;
5084 #endif
5085
5086         if (wifi->hidden && ssid) {
5087 #if defined TIZEN_EXT
5088                 if (network_security(wifi->hidden->security) ==
5089                         network_security(security) &&
5090 #else
5091                 if (!g_strcmp0(wifi->hidden->security, security) &&
5092 #endif
5093                                 wifi->hidden->ssid_len == ssid_len &&
5094                                 !memcmp(wifi->hidden->ssid, ssid, ssid_len)) {
5095                         connman_network_connect_hidden(network,
5096                                         wifi->hidden->identity,
5097                                         wifi->hidden->passphrase,
5098                                         wifi->hidden->user_data);
5099                         wifi->hidden->user_data = NULL;
5100                         hidden_free(wifi->hidden);
5101                         wifi->hidden = NULL;
5102                 }
5103         }
5104 }
5105
5106 static void network_removed(GSupplicantNetwork *network)
5107 {
5108         GSupplicantInterface *interface;
5109         struct wifi_data *wifi;
5110         const char *name, *identifier;
5111         struct connman_network *connman_network;
5112
5113 #if defined TIZEN_EXT_WIFI_MESH
5114         const char *mode;
5115         mode = g_supplicant_network_get_mode(network);
5116         if (!g_strcmp0(mode, "mesh")) {
5117                 mesh_peer_removed(network);
5118                 return;
5119         }
5120 #endif
5121
5122         interface = g_supplicant_network_get_interface(network);
5123 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5124         /*
5125          * Note: If supplicant interface's driver is wired then skip it,
5126          * because it meanti only for ethernet not Wi-Fi.
5127          */
5128         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5129                 return;
5130 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5131
5132         wifi = g_supplicant_interface_get_data(interface);
5133         identifier = g_supplicant_network_get_identifier(network);
5134         name = g_supplicant_network_get_name(network);
5135
5136         DBG("name %s", name);
5137
5138         if (!wifi)
5139                 return;
5140
5141         connman_network = connman_device_get_network(wifi->device, identifier);
5142         if (!connman_network)
5143                 return;
5144
5145 #if defined TIZEN_EXT
5146         if (connman_network == wifi->scan_pending_network)
5147                 wifi->scan_pending_network = NULL;
5148
5149         if (connman_network == wifi->pending_network)
5150                 wifi->pending_network = NULL;
5151
5152         if(connman_network_get_connecting(connman_network) == true){
5153                 connman_network_set_connected(connman_network, false);
5154         }
5155 #endif
5156
5157         wifi->networks = g_slist_remove(wifi->networks, connman_network);
5158
5159         connman_device_remove_network(wifi->device, connman_network);
5160         connman_network_unref(connman_network);
5161 }
5162
5163 static void network_changed(GSupplicantNetwork *network, const char *property)
5164 {
5165         GSupplicantInterface *interface;
5166         struct wifi_data *wifi;
5167         const char *name, *identifier;
5168         struct connman_network *connman_network;
5169         bool update_needed;
5170
5171 #if defined TIZEN_EXT
5172         const unsigned char *bssid;
5173         unsigned int maxrate;
5174         uint16_t frequency;
5175         bool wps;
5176         const unsigned char *country_code;
5177         ieee80211_modes_e phy_mode;
5178         GSList *bssid_list;
5179 #endif
5180
5181         interface = g_supplicant_network_get_interface(network);
5182 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5183         /*
5184          * Note: If supplicant interface's driver is wired then skip it,
5185          * because it meanti only for ethernet not Wi-Fi.
5186          */
5187         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5188                 return;
5189 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5190
5191         wifi = g_supplicant_interface_get_data(interface);
5192         identifier = g_supplicant_network_get_identifier(network);
5193         name = g_supplicant_network_get_name(network);
5194
5195 #if defined TIZEN_EXT
5196         if (!simplified_log)
5197                 DBG("name %s property %s", name, property);
5198 #else
5199         DBG("name %s", name);
5200 #endif
5201
5202         if (!wifi)
5203                 return;
5204
5205         connman_network = connman_device_get_network(wifi->device, identifier);
5206         if (!connman_network)
5207                 return;
5208
5209         if (g_str_equal(property, "WPSCapabilities")) {
5210                 bool wps;
5211                 bool wps_pbc;
5212                 bool wps_ready;
5213                 bool wps_advertizing;
5214
5215                 wps = g_supplicant_network_get_wps(network);
5216                 wps_pbc = g_supplicant_network_is_wps_pbc(network);
5217                 wps_ready = g_supplicant_network_is_wps_active(network);
5218                 wps_advertizing =
5219                         g_supplicant_network_is_wps_advertizing(network);
5220
5221                 connman_network_set_bool(connman_network, "WiFi.WPS", wps);
5222                 connman_network_set_bool(connman_network,
5223                                 "WiFi.WPSAdvertising", wps_advertizing);
5224
5225                 if (wps) {
5226                         /*
5227                          * Is AP advertizing for WPS association?
5228                          * If so, we decide to use WPS by default
5229                          */
5230                         if (wps_ready && wps_pbc && wps_advertizing)
5231                                 connman_network_set_bool(connman_network,
5232                                                         "WiFi.UseWPS", true);
5233                 }
5234
5235                 update_needed = true;
5236         } else if (g_str_equal(property, "Signal")) {
5237                 connman_network_set_strength(connman_network,
5238                                         calculate_strength(network));
5239                 update_needed = true;
5240         }
5241 #if defined TIZEN_EXT
5242         else if (g_str_equal(property, "LastConnectedBSSID")) {
5243                 const char *ident, *group;
5244                 char *service_ident;
5245                 bool need_save;
5246
5247                 ident = connman_device_get_ident(wifi->device);
5248                 group = connman_network_get_group(connman_network);
5249                 if (ident && group) {
5250                         service_ident = g_strdup_printf("%s_%s_%s",
5251                                 __connman_network_get_type(connman_network), ident, group);
5252
5253                         need_save = connman_device_set_last_connected_ident(wifi->device, service_ident);
5254                         if (need_save)
5255                                 connman_device_save_last_connected(wifi->device);
5256                 }
5257
5258                 connman_network_set_last_connected_bssid(connman_network,
5259                                         g_supplicant_network_get_last_connected_bssid(network));
5260
5261                 update_needed = true;
5262         } else if (g_str_equal(property, "UpdateAssocReject")) {
5263                 connman_network_set_assoc_reject_table(connman_network,
5264                                         g_supplicant_network_get_assoc_reject_table(network));
5265                 update_needed = true;
5266         }
5267 #endif
5268         else
5269                 update_needed = false;
5270
5271         if (update_needed)
5272                 connman_network_update(connman_network);
5273
5274 #if defined TIZEN_EXT
5275         bssid = g_supplicant_network_get_bssid(network);
5276         maxrate = g_supplicant_network_get_maxrate(network);
5277         frequency = g_supplicant_network_get_frequency(network);
5278         wps = g_supplicant_network_get_wps(network);
5279         phy_mode = g_supplicant_network_get_phy_mode(network);
5280
5281         connman_network_set_bssid(connman_network, bssid);
5282         connman_network_set_maxrate(connman_network, maxrate);
5283         connman_network_set_frequency(connman_network, frequency);
5284         connman_network_set_bool(connman_network, "WiFi.WPS", wps);
5285         country_code = g_supplicant_network_get_countrycode(network);
5286         connman_network_set_countrycode(connman_network, country_code);
5287         bssid_list = (GSList *)g_supplicant_network_get_bssid_list(network);
5288         connman_network_set_bssid_list(connman_network, bssid_list);
5289         connman_network_set_phy_mode(connman_network, phy_mode);
5290
5291         if (g_str_equal(property, "CheckMultiBssidConnect") &&
5292                         connman_network_get_associating(connman_network))
5293                 network_connect(connman_network);
5294 #endif
5295 }
5296
5297 static void network_associated(GSupplicantNetwork *network)
5298 {
5299         GSupplicantInterface *interface;
5300         struct wifi_data *wifi;
5301         struct connman_network *connman_network;
5302         const char *identifier;
5303
5304         DBG("");
5305
5306         interface = g_supplicant_network_get_interface(network);
5307         if (!interface)
5308                 return;
5309 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5310         /*
5311          * Note: If supplicant interface's driver is wired then skip it,
5312          * because it meanti only for ethernet not Wi-Fi.
5313          */
5314         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5315                 return;
5316 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5317
5318
5319         wifi = g_supplicant_interface_get_data(interface);
5320         if (!wifi)
5321                 return;
5322
5323         /* P2P networks must not be treated as WiFi networks */
5324         if (wifi->p2p_connecting || wifi->p2p_device)
5325                 return;
5326
5327         identifier = g_supplicant_network_get_identifier(network);
5328
5329         connman_network = connman_device_get_network(wifi->device, identifier);
5330         if (!connman_network)
5331                 return;
5332
5333         if (wifi->network) {
5334                 if (wifi->network == connman_network)
5335                         return;
5336 #if TIZEN_EXT
5337                 unsigned int ssid_len;
5338                 DBG("network1 ssid[%s] , OWE[%d],ssid[%s]",
5339                         (char *)connman_network_get_blob(wifi->network,"WiFi.SSID", &ssid_len),
5340                         connman_network_get_bool(wifi->network,"WiFi.TRANSITION_MODE"),
5341                         (char *)connman_network_get_blob(wifi->network,"WiFi.TRANSITION_MODE_SSID", &ssid_len));
5342
5343                 DBG("network1 ssid[%s], OWE[%d], ssid[%s]",
5344                         (char *)connman_network_get_blob(connman_network,"WiFi.SSID",&ssid_len),
5345                         connman_network_get_bool(connman_network,"WiFi.TRANSITION_MODE"),
5346                         (char *)connman_network_get_blob(connman_network,"WiFi.TRANSITION_MODE_SSID", &ssid_len));
5347                 if (connman_network_check_transition_mode(wifi->network, connman_network)) {//OWE trasition mode check
5348                         DBG("OWE transition mode is TRUE");
5349                         return;
5350                 }
5351 #endif
5352                 /*
5353                  * This should never happen, we got associated with
5354                  * a network different than the one we were expecting.
5355                  */
5356                 DBG("Associated to %p while expecting %p",
5357                                         connman_network, wifi->network);
5358
5359                 connman_network_set_associating(wifi->network, false);
5360         }
5361
5362         DBG("Reconnecting to previous network %p from wpa_s", connman_network);
5363
5364         wifi->network = connman_network_ref(connman_network);
5365         wifi->retries = 0;
5366
5367         /*
5368          * Interface state changes callback (interface_state) is always
5369          * called before network_associated callback thus we need to call
5370          * interface_state again in order to process the new state now that
5371          * we have the network properly set.
5372          */
5373         interface_state(interface);
5374 }
5375
5376 static void sta_authorized(GSupplicantInterface *interface,
5377                                         const char *addr)
5378 {
5379 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5380         /*
5381          * Note: If supplicant interface's driver is wired then skip it,
5382          * because it meanti only for ethernet not Wi-Fi.
5383          */
5384         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5385                 return;
5386 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5387
5388         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
5389
5390         DBG("wifi %p station %s authorized", wifi, addr);
5391
5392         if (!wifi || !wifi->tethering)
5393                 return;
5394
5395         __connman_tethering_client_register(addr);
5396 }
5397
5398 static void sta_deauthorized(GSupplicantInterface *interface,
5399                                         const char *addr)
5400 {
5401 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5402         /*
5403          * Note: If supplicant interface's driver is wired then skip it,
5404          * because it meanti only for ethernet not Wi-Fi.
5405          */
5406         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5407                 return;
5408 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5409
5410         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
5411
5412         DBG("wifi %p station %s deauthorized", wifi, addr);
5413
5414         if (!wifi || !wifi->tethering)
5415                 return;
5416
5417         __connman_tethering_client_unregister(addr);
5418 }
5419
5420 static void apply_peer_services(GSupplicantPeer *peer,
5421                                 struct connman_peer *connman_peer)
5422 {
5423         const unsigned char *data;
5424         int length;
5425
5426         DBG("");
5427
5428         connman_peer_reset_services(connman_peer);
5429
5430         data = g_supplicant_peer_get_widi_ies(peer, &length);
5431         if (data) {
5432                 connman_peer_add_service(connman_peer,
5433                         CONNMAN_PEER_SERVICE_WIFI_DISPLAY, data, length);
5434         }
5435 }
5436
5437 static void peer_found(GSupplicantPeer *peer)
5438 {
5439         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
5440         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
5441         struct connman_peer *connman_peer;
5442         const char *identifier, *name;
5443         int ret;
5444
5445 #if defined TIZEN_EXT
5446         if (!wifi)
5447                 return;
5448 #endif
5449         identifier = g_supplicant_peer_get_identifier(peer);
5450         name = g_supplicant_peer_get_name(peer);
5451
5452         DBG("ident: %s", identifier);
5453
5454         connman_peer = connman_peer_get(wifi->device, identifier);
5455         if (connman_peer)
5456                 return;
5457
5458         connman_peer = connman_peer_create(identifier);
5459         connman_peer_set_name(connman_peer, name);
5460         connman_peer_set_device(connman_peer, wifi->device);
5461         apply_peer_services(peer, connman_peer);
5462
5463         ret = connman_peer_register(connman_peer);
5464         if (ret < 0 && ret != -EALREADY)
5465                 connman_peer_unref(connman_peer);
5466         else
5467                 wifi->peers = g_slist_prepend(wifi->peers, connman_peer);
5468 }
5469
5470 static void peer_lost(GSupplicantPeer *peer)
5471 {
5472         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
5473         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
5474         struct connman_peer *connman_peer;
5475         const char *identifier;
5476
5477         if (!wifi)
5478                 return;
5479
5480         identifier = g_supplicant_peer_get_identifier(peer);
5481
5482         DBG("ident: %s", identifier);
5483
5484         connman_peer = connman_peer_get(wifi->device, identifier);
5485         if (connman_peer) {
5486                 if (wifi->p2p_connecting &&
5487                                 wifi->pending_peer == connman_peer) {
5488                         peer_connect_timeout(wifi);
5489                 }
5490                 connman_peer_unregister(connman_peer);
5491                 connman_peer_unref(connman_peer);
5492         }
5493
5494         wifi->peers = g_slist_remove(wifi->peers, connman_peer);
5495 }
5496
5497 static void peer_changed(GSupplicantPeer *peer, GSupplicantPeerState state)
5498 {
5499         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
5500         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
5501         enum connman_peer_state p_state = CONNMAN_PEER_STATE_UNKNOWN;
5502         struct connman_peer *connman_peer;
5503         const char *identifier;
5504
5505         identifier = g_supplicant_peer_get_identifier(peer);
5506
5507         DBG("ident: %s", identifier);
5508
5509         if (!wifi)
5510                 return;
5511
5512         connman_peer = connman_peer_get(wifi->device, identifier);
5513         if (!connman_peer)
5514                 return;
5515
5516         switch (state) {
5517         case G_SUPPLICANT_PEER_SERVICES_CHANGED:
5518                 apply_peer_services(peer, connman_peer);
5519                 connman_peer_services_changed(connman_peer);
5520                 return;
5521         case G_SUPPLICANT_PEER_GROUP_CHANGED:
5522                 if (!g_supplicant_peer_is_in_a_group(peer))
5523                         p_state = CONNMAN_PEER_STATE_IDLE;
5524                 else
5525                         p_state = CONNMAN_PEER_STATE_CONFIGURATION;
5526                 break;
5527         case G_SUPPLICANT_PEER_GROUP_STARTED:
5528                 break;
5529         case G_SUPPLICANT_PEER_GROUP_FINISHED:
5530                 p_state = CONNMAN_PEER_STATE_IDLE;
5531                 break;
5532         case G_SUPPLICANT_PEER_GROUP_JOINED:
5533                 connman_peer_set_iface_address(connman_peer,
5534                                 g_supplicant_peer_get_iface_address(peer));
5535                 break;
5536         case G_SUPPLICANT_PEER_GROUP_DISCONNECTED:
5537                 p_state = CONNMAN_PEER_STATE_IDLE;
5538                 break;
5539         case G_SUPPLICANT_PEER_GROUP_FAILED:
5540                 if (g_supplicant_peer_has_requested_connection(peer))
5541                         p_state = CONNMAN_PEER_STATE_IDLE;
5542                 else
5543                         p_state = CONNMAN_PEER_STATE_FAILURE;
5544                 break;
5545         }
5546
5547         if (p_state == CONNMAN_PEER_STATE_CONFIGURATION ||
5548                                         p_state == CONNMAN_PEER_STATE_FAILURE) {
5549                 if (wifi->p2p_connecting
5550                                 && connman_peer == wifi->pending_peer)
5551                         peer_cancel_timeout(wifi);
5552                 else
5553                         p_state = CONNMAN_PEER_STATE_UNKNOWN;
5554         }
5555
5556         if (p_state == CONNMAN_PEER_STATE_UNKNOWN)
5557                 return;
5558
5559         if (p_state == CONNMAN_PEER_STATE_CONFIGURATION) {
5560                 GSupplicantInterface *g_iface;
5561                 struct wifi_data *g_wifi;
5562
5563                 g_iface = g_supplicant_peer_get_group_interface(peer);
5564                 if (!g_iface)
5565                         return;
5566
5567                 g_wifi = g_supplicant_interface_get_data(g_iface);
5568                 if (!g_wifi)
5569                         return;
5570
5571                 connman_peer_set_as_master(connman_peer,
5572                                         !g_supplicant_peer_is_client(peer));
5573                 connman_peer_set_sub_device(connman_peer, g_wifi->device);
5574
5575                 /*
5576                  * If wpa_supplicant didn't create a dedicated p2p-group
5577                  * interface then mark this interface as p2p_device to avoid
5578                  * scan and auto-scan are launched on it while P2P is connected.
5579                  */
5580                 if (!g_list_find(p2p_iface_list, g_wifi))
5581                         wifi->p2p_device = true;
5582         }
5583
5584         connman_peer_set_state(connman_peer, p_state);
5585 }
5586
5587 static void peer_request(GSupplicantPeer *peer)
5588 {
5589         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
5590         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
5591         struct connman_peer *connman_peer;
5592         const char *identifier;
5593
5594 #if defined TIZEN_EXT
5595         if (!wifi)
5596                 return;
5597 #endif
5598
5599         identifier = g_supplicant_peer_get_identifier(peer);
5600
5601         DBG("ident: %s", identifier);
5602
5603         connman_peer = connman_peer_get(wifi->device, identifier);
5604         if (!connman_peer)
5605                 return;
5606
5607         connman_peer_request_connection(connman_peer);
5608 }
5609
5610 #if defined TIZEN_EXT
5611 static void system_power_off(void)
5612 {
5613         GList *list;
5614         struct wifi_data *wifi;
5615         struct connman_service *service;
5616         struct connman_ipconfig *ipconfig_ipv4;
5617
5618         if (connman_setting_get_bool("WiFiDHCPRelease") == true) {
5619                 for (list = iface_list; list; list = list->next) {
5620                         wifi = list->data;
5621
5622                         if (wifi->network != NULL) {
5623                                 service = connman_service_lookup_from_network(wifi->network);
5624                                 ipconfig_ipv4 = __connman_service_get_ip4config(service);
5625                                 __connman_dhcp_stop(ipconfig_ipv4);
5626                         }
5627                 }
5628         }
5629 }
5630
5631 static void network_merged(GSupplicantNetwork *network)
5632 {
5633         GSupplicantInterface *interface;
5634         GSupplicantState state;
5635         struct wifi_data *wifi;
5636         const char *identifier;
5637         struct connman_network *connman_network;
5638         bool ishs20AP = 0;
5639         char *temp = NULL;
5640
5641         interface = g_supplicant_network_get_interface(network);
5642         if (!interface)
5643                 return;
5644
5645 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5646         /*
5647          * Note: If supplicant interface's driver is wired then skip it,
5648          * because it meanti only for ethernet not Wi-Fi.
5649          */
5650         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5651                 return;
5652 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5653
5654         state = g_supplicant_interface_get_state(interface);
5655         if (state < G_SUPPLICANT_STATE_AUTHENTICATING)
5656                 return;
5657
5658         wifi = g_supplicant_interface_get_data(interface);
5659         if (!wifi)
5660                 return;
5661
5662         identifier = g_supplicant_network_get_identifier(network);
5663
5664         connman_network = connman_device_get_network(wifi->device, identifier);
5665         if (!connman_network)
5666                 return;
5667
5668         DBG("merged identifier %s", identifier);
5669
5670         if (wifi->connected == FALSE) {
5671                 switch (state) {
5672                 case G_SUPPLICANT_STATE_AUTHENTICATING:
5673                 case G_SUPPLICANT_STATE_ASSOCIATING:
5674                 case G_SUPPLICANT_STATE_ASSOCIATED:
5675                 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
5676                 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
5677                         connman_network_set_associating(connman_network, TRUE);
5678                         break;
5679                 case G_SUPPLICANT_STATE_COMPLETED:
5680                         connman_network_set_connected(connman_network, TRUE);
5681                         break;
5682                 default:
5683                         DBG("Not handled the state : %d", state);
5684                         break;
5685                 }
5686         }
5687
5688         ishs20AP = g_supplicant_network_is_hs20AP(network);
5689
5690         if (ishs20AP &&
5691                 g_strcmp0(g_supplicant_network_get_security(network), "ieee8021x") == 0) {
5692                 temp = g_ascii_strdown(g_supplicant_network_get_eap(network), -1);
5693                 connman_network_set_string(connman_network, "WiFi.EAP",
5694                                 temp);
5695                 connman_network_set_string(connman_network, "WiFi.Identity",
5696                                 g_supplicant_network_get_identity(network));
5697                 connman_network_set_string(connman_network, "WiFi.Phase2",
5698                                 g_supplicant_network_get_phase2(network));
5699
5700                 g_free(temp);
5701         }
5702
5703         wifi->network = connman_network;
5704 }
5705
5706 static void assoc_failed(void *user_data)
5707 {
5708         struct connman_network *network = user_data;
5709         connman_network_set_associating(network, false);
5710 }
5711
5712 static void scan_done(GSupplicantInterface *interface)
5713 {
5714 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5715         /*
5716          * Note: If supplicant interface's driver is wired then skip it,
5717          * because it meanti only for ethernet not Wi-Fi.
5718          */
5719         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5720                 return;
5721 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5722
5723         GList *list;
5724         int scan_type = CONNMAN_SCAN_TYPE_WPA_SUPPLICANT;
5725         struct wifi_data *wifi;
5726         bool scanning;
5727
5728         for (list = iface_list; list; list = list->next) {
5729                 wifi = list->data;
5730
5731                 if (interface == wifi->interface) {
5732                         scanning = connman_device_get_scanning(wifi->device,
5733                                         CONNMAN_SERVICE_TYPE_WIFI);
5734                         if (!scanning)
5735                                 __connman_technology_notify_scan_done(
5736                                                 connman_device_get_string(wifi->device, "Interface"), scan_type);
5737                         break;
5738                 }
5739         }
5740 }
5741 #endif
5742
5743 static void debug(const char *str)
5744 {
5745 #if defined TIZEN_EXT
5746         if (connman_setting_get_bool("ConnmanSupplicantDebug"))
5747 #else
5748         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
5749 #endif
5750                 connman_debug("%s", str);
5751 }
5752
5753 static void disconnect_reasoncode(GSupplicantInterface *interface,
5754                                 int reasoncode)
5755 {
5756 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5757         /*
5758          * Note: If supplicant interface's driver is wired then skip it,
5759          * because it meanti only for ethernet not Wi-Fi.
5760          */
5761         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5762                 return;
5763 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5764
5765         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
5766
5767         if (wifi != NULL) {
5768                 wifi->disconnect_code = reasoncode;
5769         }
5770 }
5771
5772 static void assoc_status_code(GSupplicantInterface *interface, int status_code)
5773 {
5774 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5775         /*
5776          * Note: If supplicant interface's driver is wired then skip it,
5777          * because it meanti only for ethernet not Wi-Fi.
5778          */
5779         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5780                 return;
5781 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5782
5783         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
5784
5785         if (wifi != NULL) {
5786                 wifi->assoc_code = status_code;
5787         }
5788 }
5789
5790 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5791 static GSupplicantCallbacks callbacks = {
5792 #else /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5793 static const GSupplicantCallbacks callbacks = {
5794 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5795         .system_ready           = system_ready,
5796         .system_killed          = system_killed,
5797         .interface_added        = interface_added,
5798         .interface_state        = interface_state,
5799         .interface_removed      = interface_removed,
5800         .p2p_support            = p2p_support,
5801         .scan_started           = scan_started,
5802         .scan_finished          = scan_finished,
5803         .ap_create_fail         = ap_create_fail,
5804         .network_added          = network_added,
5805         .network_removed        = network_removed,
5806         .network_changed        = network_changed,
5807         .network_associated     = network_associated,
5808         .sta_authorized         = sta_authorized,
5809         .sta_deauthorized       = sta_deauthorized,
5810         .peer_found             = peer_found,
5811         .peer_lost              = peer_lost,
5812         .peer_changed           = peer_changed,
5813         .peer_request           = peer_request,
5814 #if defined TIZEN_EXT
5815         .system_power_off       = system_power_off,
5816         .network_merged         = network_merged,
5817         .assoc_failed           = assoc_failed,
5818         .scan_done              = scan_done,
5819 #endif
5820         .debug                  = debug,
5821         .disconnect_reasoncode  = disconnect_reasoncode,
5822         .assoc_status_code      = assoc_status_code,
5823 #if defined TIZEN_EXT_WIFI_MESH
5824         .mesh_support           = mesh_support,
5825         .mesh_group_started = mesh_group_started,
5826         .mesh_group_removed = mesh_group_removed,
5827         .mesh_peer_connected = mesh_peer_connected,
5828         .mesh_peer_disconnected = mesh_peer_disconnected,
5829 #endif
5830 };
5831
5832
5833 static int tech_probe(struct connman_technology *technology)
5834 {
5835         wifi_technology = technology;
5836
5837         return 0;
5838 }
5839
5840 static void tech_remove(struct connman_technology *technology)
5841 {
5842         wifi_technology = NULL;
5843 }
5844
5845 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
5846 {
5847         GSupplicantSSID *ap;
5848
5849         ap = g_try_malloc0(sizeof(GSupplicantSSID));
5850         if (!ap)
5851                 return NULL;
5852
5853         ap->mode = G_SUPPLICANT_MODE_MASTER;
5854 #if defined TIZEN_EXT
5855         ap->ssid = (void *) ssid;
5856 #else
5857         ap->ssid = ssid;
5858 #endif
5859         ap->ssid_len = strlen(ssid);
5860         ap->scan_ssid = 0;
5861         ap->freq = 2412;
5862
5863         if (!passphrase || strlen(passphrase) == 0) {
5864                 ap->security = G_SUPPLICANT_SECURITY_NONE;
5865                 ap->passphrase = NULL;
5866         } else {
5867                ap->security = G_SUPPLICANT_SECURITY_PSK;
5868                ap->protocol = G_SUPPLICANT_PROTO_RSN;
5869                ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
5870                ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
5871                ap->passphrase = passphrase;
5872         }
5873
5874         return ap;
5875 }
5876
5877 static void ap_start_callback(int result, GSupplicantInterface *interface,
5878                                                         void *user_data)
5879 {
5880         struct wifi_tethering_info *info = user_data;
5881
5882         DBG("result %d index %d bridge %s",
5883                 result, info->wifi->index, info->wifi->bridge);
5884
5885         if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
5886                 connman_inet_remove_from_bridge(info->wifi->index,
5887                                                         info->wifi->bridge);
5888
5889                 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
5890                         connman_technology_tethering_notify(info->technology, false);
5891                         g_free(info->wifi->tethering_param->ssid);
5892                         g_free(info->wifi->tethering_param);
5893                         info->wifi->tethering_param = NULL;
5894                 }
5895         }
5896
5897         g_free(info->ifname);
5898         g_free(info);
5899 }
5900
5901 static void ap_create_callback(int result,
5902                                 GSupplicantInterface *interface,
5903                                         void *user_data)
5904 {
5905         struct wifi_tethering_info *info = user_data;
5906
5907         DBG("result %d ifname %s", result,
5908                                 g_supplicant_interface_get_ifname(interface));
5909
5910         if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
5911                 connman_inet_remove_from_bridge(info->wifi->index,
5912                                                         info->wifi->bridge);
5913
5914                 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
5915                         connman_technology_tethering_notify(info->technology, false);
5916                         g_free(info->wifi->tethering_param->ssid);
5917                         g_free(info->wifi->tethering_param);
5918                         info->wifi->tethering_param = NULL;
5919
5920                 }
5921
5922                 g_free(info->ifname);
5923                 g_free(info->ssid);
5924                 g_free(info);
5925                 return;
5926         }
5927
5928         info->wifi->interface = interface;
5929         g_supplicant_interface_set_data(interface, info->wifi);
5930
5931         if (g_supplicant_interface_set_apscan(interface, 2) < 0)
5932                 connman_error("Failed to set interface ap_scan property");
5933
5934         g_supplicant_interface_connect(interface, info->ssid,
5935                                                 ap_start_callback, info);
5936 }
5937
5938 static void sta_remove_callback(int result,
5939                                 GSupplicantInterface *interface,
5940                                         void *user_data)
5941 {
5942         struct wifi_tethering_info *info = user_data;
5943         const char *driver = connman_option_get_string("wifi");
5944
5945         DBG("ifname %s result %d ", info->ifname, result);
5946
5947         if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
5948                 info->wifi->tethering = false;
5949                 connman_technology_tethering_notify(info->technology, false);
5950
5951                 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
5952                         g_free(info->wifi->tethering_param->ssid);
5953                         g_free(info->wifi->tethering_param);
5954                         info->wifi->tethering_param = NULL;
5955                 }
5956
5957                 g_free(info->ifname);
5958                 g_free(info->ssid);
5959                 g_free(info);
5960                 return;
5961         }
5962
5963         info->wifi->interface = NULL;
5964
5965         g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
5966                                                 ap_create_callback,
5967                                                         info);
5968 }
5969
5970 static int enable_wifi_tethering(struct connman_technology *technology,
5971                                 const char *bridge, const char *identifier,
5972                                 const char *passphrase, bool available)
5973 {
5974         GList *list;
5975         GSupplicantInterface *interface;
5976         struct wifi_data *wifi;
5977         struct wifi_tethering_info *info;
5978         const char *ifname;
5979         unsigned int mode;
5980         int err, berr = 0;
5981
5982         for (list = iface_list; list; list = list->next) {
5983                 wifi = list->data;
5984
5985                 DBG("wifi %p network %p pending_network %p", wifi,
5986                         wifi->network, wifi->pending_network);
5987
5988                 interface = wifi->interface;
5989
5990                 if (!interface)
5991                         continue;
5992
5993                 ifname = g_supplicant_interface_get_ifname(wifi->interface);
5994                 if (!ifname)
5995                         continue;
5996
5997                 if (wifi->ap_supported == WIFI_AP_NOT_SUPPORTED) {
5998                         DBG("%s does not support AP mode (detected)", ifname);
5999                         continue;
6000                 }
6001
6002                 mode = g_supplicant_interface_get_mode(interface);
6003                 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
6004                         wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
6005                         DBG("%s does not support AP mode (capability)", ifname);
6006                         continue;
6007                 }
6008
6009                 if (wifi->network && available)
6010                         continue;
6011
6012                 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
6013                 if (!info)
6014                         return -ENOMEM;
6015
6016                 wifi->tethering_param = g_try_malloc0(sizeof(struct wifi_tethering_info));
6017                 if (!wifi->tethering_param) {
6018                         g_free(info);
6019                         return -ENOMEM;
6020                 }
6021
6022                 info->wifi = wifi;
6023                 info->technology = technology;
6024                 info->wifi->bridge = bridge;
6025                 info->ssid = ssid_ap_init(identifier, passphrase);
6026                 if (!info->ssid)
6027                         goto failed;
6028
6029                 info->ifname = g_strdup(ifname);
6030
6031                 wifi->tethering_param->technology = technology;
6032                 wifi->tethering_param->ssid = ssid_ap_init(identifier, passphrase);
6033                 if (!wifi->tethering_param->ssid)
6034                         goto failed;
6035
6036                 info->wifi->tethering = true;
6037                 info->wifi->ap_supported = WIFI_AP_SUPPORTED;
6038
6039                 berr = connman_technology_tethering_notify(technology, true);
6040                 if (berr < 0)
6041                         goto failed;
6042
6043                 err = g_supplicant_interface_remove(interface,
6044                                                 sta_remove_callback,
6045                                                         info);
6046                 if (err >= 0) {
6047                         DBG("tethering wifi %p ifname %s", wifi, ifname);
6048                         return 0;
6049                 }
6050
6051         failed:
6052                 g_free(info->ifname);
6053                 g_free(info->ssid);
6054                 g_free(info);
6055                 g_free(wifi->tethering_param);
6056                 wifi->tethering_param = NULL;
6057
6058                 /*
6059                  * Remove bridge if it was correctly created but remove
6060                  * operation failed. Instead, if bridge creation failed then
6061                  * break out and do not try again on another interface,
6062                  * bridge set-up does not depend on it.
6063                  */
6064                 if (berr == 0)
6065                         connman_technology_tethering_notify(technology, false);
6066                 else
6067                         break;
6068         }
6069
6070         return -EOPNOTSUPP;
6071 }
6072
6073 static int tech_set_tethering(struct connman_technology *technology,
6074                                 const char *identifier, const char *passphrase,
6075                                 const char *bridge, bool enabled)
6076 {
6077         GList *list;
6078         struct wifi_data *wifi;
6079         int err;
6080
6081         DBG("");
6082
6083         if (!enabled) {
6084                 for (list = iface_list; list; list = list->next) {
6085                         wifi = list->data;
6086
6087                         if (wifi->tethering) {
6088                                 wifi->tethering = false;
6089
6090                                 connman_inet_remove_from_bridge(wifi->index,
6091                                                                         bridge);
6092                                 wifi->bridged = false;
6093                         }
6094                 }
6095
6096                 connman_technology_tethering_notify(technology, false);
6097
6098                 return 0;
6099         }
6100
6101         DBG("trying tethering for available devices");
6102         err = enable_wifi_tethering(technology, bridge, identifier, passphrase,
6103                                 true);
6104
6105         if (err < 0) {
6106                 DBG("trying tethering for any device");
6107                 err = enable_wifi_tethering(technology, bridge, identifier,
6108                                         passphrase, false);
6109         }
6110
6111         return err;
6112 }
6113
6114 static void regdom_callback(int result, const char *alpha2, void *user_data)
6115 {
6116         DBG("");
6117
6118         if (!wifi_technology)
6119                 return;
6120
6121         if (result != 0)
6122                 alpha2 = NULL;
6123
6124         connman_technology_regdom_notify(wifi_technology, alpha2);
6125 }
6126
6127 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
6128 {
6129         return g_supplicant_set_country(alpha2, regdom_callback, NULL);
6130 }
6131
6132 #if defined TIZEN_EXT
6133 static void supp_ins_init(void)
6134 {
6135         const char *string;
6136         GSupplicantINSPreferredFreq preferred_freq;
6137
6138         string = connman_option_get_string("INSPreferredFreqBSSID");
6139         if (g_strcmp0(string, "5GHz") == 0)
6140                 preferred_freq = G_SUPPLICANT_INS_PREFERRED_FREQ_5GHZ;
6141         else if (g_strcmp0(string, "2.4GHz") == 0)
6142                 preferred_freq = G_SUPPLICANT_INS_PREFERRED_FREQ_24GHZ;
6143         else
6144                 preferred_freq = G_SUPPLICANT_INS_PREFERRED_FREQ_UNKNOWN;
6145
6146         g_supplicant_set_ins_settings(preferred_freq,
6147                 connman_setting_get_bool("INSLastConnectedBSSID"),
6148                 connman_setting_get_bool("INSAssocReject"),
6149                 connman_setting_get_bool("INSSignalBSSID"),
6150                 connman_setting_get_uint("INSPreferredFreqBSSIDScore"),
6151                 connman_setting_get_uint("INSLastConnectedBSSIDScore"),
6152                 connman_setting_get_uint("INSAssocRejectScore"),
6153                 connman_setting_get_int("INSSignalLevel3_5GHz"),
6154                 connman_setting_get_int("INSSignalLevel3_24GHz")
6155         );
6156 }
6157 #endif
6158
6159 static struct connman_technology_driver tech_driver = {
6160         .name           = "wifi",
6161         .type           = CONNMAN_SERVICE_TYPE_WIFI,
6162         .probe          = tech_probe,
6163         .remove         = tech_remove,
6164         .set_tethering  = tech_set_tethering,
6165         .set_regdom     = tech_set_regdom,
6166 };
6167
6168 static int wifi_init(void)
6169 {
6170         int err;
6171
6172         err = connman_network_driver_register(&network_driver);
6173         if (err < 0)
6174                 return err;
6175
6176         err = g_supplicant_register(&callbacks);
6177         if (err < 0) {
6178                 connman_network_driver_unregister(&network_driver);
6179                 return err;
6180         }
6181
6182         err = connman_technology_driver_register(&tech_driver);
6183         if (err < 0) {
6184                 g_supplicant_unregister(&callbacks);
6185                 connman_network_driver_unregister(&network_driver);
6186                 return err;
6187         }
6188
6189 #if defined TIZEN_EXT
6190         failed_bssids = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
6191 #endif
6192
6193 #if defined TIZEN_EXT
6194         supp_ins_init();
6195 #endif
6196         return 0;
6197 }
6198
6199 static void wifi_exit(void)
6200 {
6201         DBG();
6202
6203         connman_technology_driver_unregister(&tech_driver);
6204
6205         g_supplicant_unregister(&callbacks);
6206
6207         connman_network_driver_unregister(&network_driver);
6208
6209 #if defined TIZEN_EXT
6210         g_hash_table_unref(failed_bssids);
6211 #endif
6212 }
6213
6214 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
6215                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)