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