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