Add support for handling wifi authentication failure
[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_AUTHENTICATING &&
4486                         wifi->state != G_SUPPLICANT_STATE_ASSOCIATING &&
4487                         wifi->state != G_SUPPLICANT_STATE_ASSOCIATED) {
4488                 wifi->assoc_retry_count = 0;
4489                 return false;
4490         }
4491
4492         security = connman_network_get_string(network, "WiFi.Security");
4493         if (security && g_str_equal(security, "ieee8021x") == true &&
4494                         wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) {
4495                 wifi->assoc_retry_count = 0;
4496                 return false;
4497         }
4498
4499         if (++wifi->assoc_retry_count >= TIZEN_ASSOC_RETRY_COUNT) {
4500                 wifi->assoc_retry_count = 0;
4501
4502                 /* Honestly it's not an invalid-key error,
4503                  * however QA team recommends that the invalid-key error
4504                  * might be better to display for user experience.
4505                  */
4506                 switch (wifi->state) {
4507                 case G_SUPPLICANT_STATE_AUTHENTICATING:
4508                         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_AUTHENTICATE_FAIL);
4509                         break;
4510                 case G_SUPPLICANT_STATE_ASSOCIATED:
4511                         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
4512                         break;
4513                 default:
4514                         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
4515                         break;
4516                 }
4517
4518                 return false;
4519         }
4520
4521         return true;
4522 }
4523 #endif
4524
4525 static void interface_state(GSupplicantInterface *interface)
4526 {
4527         struct connman_network *network;
4528         struct connman_device *device;
4529         struct wifi_data *wifi;
4530         GSupplicantState state = g_supplicant_interface_get_state(interface);
4531 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4532         /*
4533          * Note: If supplicant interface's driver is wired then skip it,
4534          * because it meanti only for ethernet not Wi-Fi.
4535          */
4536         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4537                 return;
4538 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4539
4540         bool wps;
4541         bool old_connected;
4542
4543         wifi = g_supplicant_interface_get_data(interface);
4544
4545         DBG("wifi %p interface state %d", wifi, state);
4546
4547         if (!wifi)
4548                 return;
4549
4550         device = wifi->device;
4551         if (!device)
4552                 return;
4553
4554         if (state == G_SUPPLICANT_STATE_COMPLETED) {
4555                 if (wifi->tethering_param) {
4556                         g_free(wifi->tethering_param->ssid);
4557                         g_free(wifi->tethering_param);
4558                         wifi->tethering_param = NULL;
4559                 }
4560
4561                 if (wifi->tethering)
4562                         stop_autoscan(device);
4563         }
4564
4565         if (g_supplicant_interface_get_ready(interface) &&
4566                                         !wifi->interface_ready) {
4567                 wifi->interface_ready = true;
4568                 finalize_interface_creation(wifi);
4569         }
4570
4571         network = wifi->network;
4572         if (!network)
4573                 return;
4574
4575         switch (state) {
4576         case G_SUPPLICANT_STATE_SCANNING:
4577 #if defined TIZEN_EXT
4578                 if (wifi->automaxspeed_timeout != 0) {
4579                         g_source_remove(wifi->automaxspeed_timeout);
4580                         wifi->automaxspeed_timeout = 0;
4581                         DBG("Remove signalpoll timer!!");
4582                 }
4583 #endif
4584                 if (wifi->connected)
4585                         connman_network_set_connected(network, false);
4586
4587                 break;
4588
4589         case G_SUPPLICANT_STATE_AUTHENTICATING:
4590         case G_SUPPLICANT_STATE_ASSOCIATING:
4591 #if defined TIZEN_EXT
4592                 reset_autoscan(device);
4593 #else
4594                 stop_autoscan(device);
4595 #endif
4596
4597                 if (!wifi->connected)
4598                         connman_network_set_associating(network, true);
4599
4600                 break;
4601
4602         case G_SUPPLICANT_STATE_COMPLETED:
4603 #if defined TIZEN_EXT
4604                 /* though it should be already reset: */
4605                 reset_autoscan(device);
4606
4607                 wifi->assoc_retry_count = 0;
4608
4609                 wifi->scan_pending_network = NULL;
4610
4611                 /* should be cleared scanning flag */
4612                 bool scanning = connman_device_get_scanning(device,
4613                                                CONNMAN_SERVICE_TYPE_WIFI);
4614                 if (scanning){
4615                         connman_device_set_scanning(device,
4616                                 CONNMAN_SERVICE_TYPE_WIFI, false);
4617                         connman_device_unref(device);
4618                 }
4619
4620                 if (!wifi->automaxspeed_timeout) {
4621                         DBG("Going to start signalpoll timer!!");
4622                         int ret = network_signalpoll(wifi);
4623                         if (ret < 0)
4624                                 DBG("Fail to get max speed !!");
4625                         else
4626                                 wifi->automaxspeed_timeout = g_timeout_add_seconds(10, autosignalpoll_timeout, wifi);
4627                 }
4628
4629                 g_hash_table_remove_all(failed_bssids);
4630 #else
4631                 /* though it should be already stopped: */
4632                 stop_autoscan(device);
4633 #endif
4634
4635                 if (!handle_wps_completion(interface, network, device, wifi))
4636                         break;
4637
4638                 connman_network_set_connected(network, true);
4639
4640                 wifi->disconnect_code = 0;
4641                 wifi->assoc_code = 0;
4642                 wifi->load_shaping_retries = 0;
4643                 break;
4644
4645         case G_SUPPLICANT_STATE_DISCONNECTED:
4646 #if defined TIZEN_EXT
4647                 connman_network_set_strength(network, 0);
4648                 connman_network_set_maxspeed(network, 0);
4649
4650                 if (wifi->automaxspeed_timeout != 0) {
4651                         g_source_remove(wifi->automaxspeed_timeout);
4652                         wifi->automaxspeed_timeout = 0;
4653                         DBG("Remove signalpoll timer!!");
4654                 }
4655 #endif
4656                 /*
4657                  * If we're in one of the idle modes, we have
4658                  * not started association yet and thus setting
4659                  * those ones to FALSE could cancel an association
4660                  * in progress.
4661                  */
4662                 wps = connman_network_get_bool(network, "WiFi.UseWPS");
4663                 if (wps)
4664                         if (is_idle_wps(interface, wifi))
4665                                 break;
4666
4667                 if (is_idle(wifi))
4668                         break;
4669
4670 #if defined TIZEN_EXT
4671                 if (handle_assoc_status_code(interface, wifi)) {
4672                         const char *group = connman_network_get_group(network);
4673                         GSupplicantNetwork *supplicant_network;
4674                         GSList *bssid_list = NULL;
4675                         guint bssid_length = 0;
4676
4677                         if (group) {
4678                                 supplicant_network = g_supplicant_interface_get_network(interface, group);
4679
4680                                 connman_network_set_assoc_reject_table(network,
4681                                         g_supplicant_network_get_assoc_reject_table(supplicant_network));
4682
4683                                 g_supplicant_network_update_assoc_reject(interface, supplicant_network);
4684                         }
4685
4686                         bssid_list = (GSList *)connman_network_get_bssid_list(network);
4687                         if (bssid_list)
4688                                 bssid_length = g_slist_length(bssid_list);
4689
4690                         if (bssid_length > 1 && bssid_length > g_hash_table_size(failed_bssids)) {
4691                                 network_connect(network);
4692                                 break;
4693                         }
4694
4695                         wifi->load_shaping_retries = 0;
4696                 }
4697
4698                 g_hash_table_remove_all(failed_bssids);
4699 #else
4700                 if (handle_assoc_status_code(interface, wifi))
4701                         break;
4702 #endif
4703
4704                 /* If previous state was 4way-handshake, then
4705                  * it's either: psk was incorrect and thus we retry
4706                  * or if we reach the maximum retries we declare the
4707                  * psk as wrong */
4708                 if (handle_4way_handshake_failure(interface,
4709                                                 network, wifi))
4710                         break;
4711
4712                 /* See table 8-36 Reason codes in IEEE Std 802.11 */
4713                 switch (wifi->disconnect_code) {
4714                 case 1: /* Unspecified reason */
4715                         /* Let's assume it's because we got blocked */
4716
4717                 case 6: /* Class 2 frame received from nonauthenticated STA */
4718                         connman_network_set_error(network,
4719                                                 CONNMAN_NETWORK_ERROR_BLOCKED);
4720                         break;
4721
4722                 default:
4723                         break;
4724                 }
4725
4726 #if defined TIZEN_EXT
4727                 /* Some of Wi-Fi networks are not comply Wi-Fi specification.
4728                  * Retry association until its retry count is expired */
4729                 if (handle_wifi_assoc_retry(network, wifi) == true) {
4730                         throw_wifi_scan(wifi->device, scan_callback);
4731                         wifi->scan_pending_network = wifi->network;
4732                         break;
4733                 }
4734
4735                 if(wifi->disconnect_code > 0){
4736                         DBG("Set disconnect reason code(%d)", wifi->disconnect_code);
4737                         connman_network_set_disconnect_reason(network, wifi->disconnect_code);
4738                 }
4739 #endif
4740
4741                 if (network != wifi->pending_network) {
4742                         connman_network_set_connected(network, false);
4743                         connman_network_set_associating(network, false);
4744                 }
4745                 wifi->disconnecting = false;
4746
4747                 start_autoscan(device);
4748
4749                 break;
4750
4751         case G_SUPPLICANT_STATE_INACTIVE:
4752 #if defined TIZEN_EXT
4753                 if (handle_wps_completion(interface, network, device, wifi) == false)
4754                         break;
4755 #endif
4756                 connman_network_set_associating(network, false);
4757                 start_autoscan(device);
4758
4759                 break;
4760
4761         case G_SUPPLICANT_STATE_UNKNOWN:
4762         case G_SUPPLICANT_STATE_DISABLED:
4763         case G_SUPPLICANT_STATE_ASSOCIATED:
4764         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4765         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4766                 break;
4767         }
4768
4769         old_connected = wifi->connected;
4770         wifi->state = state;
4771
4772         /* Saving wpa_s state policy:
4773          * If connected and if the state changes are roaming related:
4774          * --> We stay connected
4775          * If completed
4776          * --> We are connected
4777          * All other case:
4778          * --> We are not connected
4779          * */
4780         switch (state) {
4781         case G_SUPPLICANT_STATE_AUTHENTICATING:
4782         case G_SUPPLICANT_STATE_ASSOCIATING:
4783         case G_SUPPLICANT_STATE_ASSOCIATED:
4784         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4785         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4786                 if (wifi->connected)
4787                         connman_warn("Probably roaming right now!"
4788                                                 " Staying connected...");
4789                 break;
4790         case G_SUPPLICANT_STATE_SCANNING:
4791                 wifi->connected = false;
4792
4793                 if (old_connected)
4794                         start_autoscan(device);
4795                 break;
4796         case G_SUPPLICANT_STATE_COMPLETED:
4797                 wifi->connected = true;
4798                 break;
4799         default:
4800                 wifi->connected = false;
4801                 break;
4802         }
4803
4804         DBG("DONE");
4805 }
4806
4807 static void interface_removed(GSupplicantInterface *interface)
4808 {
4809         const char *ifname = g_supplicant_interface_get_ifname(interface);
4810         struct wifi_data *wifi;
4811 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4812         /*
4813          * Note: If supplicant interface's driver is wired then skip it,
4814          * because it meanti only for ethernet not Wi-Fi.
4815          */
4816         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4817                 return;
4818 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4819
4820
4821         DBG("ifname %s", ifname);
4822
4823         wifi = g_supplicant_interface_get_data(interface);
4824
4825 #if defined TIZEN_EXT_WIFI_MESH
4826         if (wifi && wifi->mesh_interface) {
4827                 DBG("Notify mesh interface remove");
4828                 connman_mesh_notify_interface_remove(true);
4829                 struct wifi_mesh_info *mesh_info = wifi->mesh_info;
4830                 g_free(mesh_info->parent_ifname);
4831                 g_free(mesh_info->ifname);
4832                 g_free(mesh_info->identifier);
4833                 g_free(mesh_info);
4834                 wifi->mesh_interface = false;
4835                 wifi->mesh_info = NULL;
4836                 return;
4837         }
4838 #endif
4839
4840         if (wifi)
4841                 wifi->interface = NULL;
4842
4843         if (wifi && wifi->tethering)
4844                 return;
4845
4846         if (!wifi || !wifi->device) {
4847                 DBG("wifi interface already removed");
4848                 return;
4849         }
4850
4851         connman_device_set_powered(wifi->device, false);
4852
4853         check_p2p_technology();
4854 #if defined TIZEN_EXT_WIFI_MESH
4855         check_mesh_technology();
4856 #endif
4857 }
4858
4859 static void set_device_type(const char *type, char dev_type[17])
4860 {
4861         const char *oui = "0050F204";
4862         const char *category = "0001";
4863         const char *sub_category = "0000";
4864
4865         if (!g_strcmp0(type, "handset")) {
4866                 category = "000A";
4867                 sub_category = "0005";
4868         } else if (!g_strcmp0(type, "vm") || !g_strcmp0(type, "container"))
4869                 sub_category = "0001";
4870         else if (!g_strcmp0(type, "server"))
4871                 sub_category = "0002";
4872         else if (!g_strcmp0(type, "laptop"))
4873                 sub_category = "0005";
4874         else if (!g_strcmp0(type, "desktop"))
4875                 sub_category = "0006";
4876         else if (!g_strcmp0(type, "tablet"))
4877                 sub_category = "0009";
4878         else if (!g_strcmp0(type, "watch"))
4879                 category = "00FF";
4880
4881         snprintf(dev_type, 17, "%s%s%s", category, oui, sub_category);
4882 }
4883
4884 static void p2p_support(GSupplicantInterface *interface)
4885 {
4886         char dev_type[17] = {};
4887         const char *hostname;
4888 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4889         /*
4890          * Note: If supplicant interface's driver is wired then skip it,
4891          * because it meanti only for ethernet not Wi-Fi.
4892          */
4893         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4894                 return;
4895 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4896
4897
4898         DBG("");
4899
4900         if (!interface)
4901                 return;
4902
4903         if (!g_supplicant_interface_has_p2p(interface))
4904                 return;
4905
4906         if (connman_technology_driver_register(&p2p_tech_driver) < 0) {
4907                 DBG("Could not register P2P technology driver");
4908                 return;
4909         }
4910
4911         hostname = connman_utsname_get_hostname();
4912         if (!hostname)
4913                 hostname = "ConnMan";
4914
4915         set_device_type(connman_machine_get_type(), dev_type);
4916         g_supplicant_interface_set_p2p_device_config(interface,
4917                                                         hostname, dev_type);
4918         connman_peer_driver_register(&peer_driver);
4919 }
4920
4921 static void scan_started(GSupplicantInterface *interface)
4922 {
4923 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4924         /*
4925          * Note: If supplicant interface's driver is wired then skip it,
4926          * because it meanti only for ethernet not Wi-Fi.
4927          */
4928         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4929                 return;
4930 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4931
4932         DBG("");
4933 }
4934
4935 static void scan_finished(GSupplicantInterface *interface)
4936 {
4937 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4938         /*
4939          * Note: If supplicant interface's driver is wired then skip it,
4940          * because it meanti only for ethernet not Wi-Fi.
4941          */
4942         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4943                 return;
4944 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4945
4946 #if defined TIZEN_EXT
4947         struct wifi_data *wifi;
4948         bool is_associating = false;
4949         static bool is_scanning = true;
4950 #endif
4951
4952         DBG("");
4953
4954 #if defined TIZEN_EXT
4955         wifi = g_supplicant_interface_get_data(interface);
4956         if (wifi && wifi->scan_pending_network) {
4957                 network_connect(wifi->scan_pending_network);
4958                 wifi->scan_pending_network = NULL;
4959         }
4960
4961         //service state - associating
4962         if(!wifi || !wifi->network)
4963                 return;
4964
4965         is_associating = connman_network_get_associating(wifi->network);
4966         if(is_associating && is_scanning){
4967                 is_scanning = false;
4968                 DBG("send scan for connecting");
4969                 throw_wifi_scan(wifi->device, scan_callback);
4970
4971                 return;
4972         }
4973         is_scanning = true;
4974
4975         //go scan
4976
4977 #endif
4978 }
4979
4980 static void ap_create_fail(GSupplicantInterface *interface)
4981 {
4982 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4983         /*
4984          * Note: If supplicant interface's driver is wired then skip it,
4985          * because it meanti only for ethernet not Wi-Fi.
4986          */
4987         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4988                 return;
4989 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4990
4991         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
4992         int ret;
4993
4994         if ((wifi->tethering) && (wifi->tethering_param)) {
4995                 DBG("%s create AP fail \n",
4996                                 g_supplicant_interface_get_ifname(wifi->interface));
4997
4998                 connman_inet_remove_from_bridge(wifi->index, wifi->bridge);
4999                 wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
5000                 wifi->tethering = false;
5001
5002                 ret = tech_set_tethering(wifi->tethering_param->technology,
5003                                 wifi->tethering_param->ssid->ssid,
5004                                 wifi->tethering_param->ssid->passphrase,
5005                                 wifi->bridge, true);
5006
5007                 if ((ret == -EOPNOTSUPP) && (wifi_technology)) {
5008                         connman_technology_tethering_notify(wifi_technology,false);
5009                 }
5010
5011                 g_free(wifi->tethering_param->ssid);
5012                 g_free(wifi->tethering_param);
5013                 wifi->tethering_param = NULL;
5014         }
5015 }
5016
5017 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
5018 {
5019         unsigned char strength;
5020
5021         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
5022 #if !defined TIZEN_EXT
5023         if (strength > 100)
5024                 strength = 100;
5025 #endif
5026
5027         return strength;
5028 }
5029
5030 #if defined TIZEN_EXT_WIFI_MESH
5031 static void mesh_peer_added(GSupplicantNetwork *supplicant_network)
5032 {
5033         GSupplicantInterface *interface;
5034         struct wifi_data *wifi;
5035         const char *name, *security;
5036         struct connman_mesh *connman_mesh;
5037         struct wifi_mesh_info *mesh_info;
5038         const unsigned char *bssid;
5039         const char *identifier;
5040         char *address;
5041         uint16_t frequency;
5042         int ret;
5043
5044         interface = g_supplicant_network_get_interface(supplicant_network);
5045         wifi = g_supplicant_interface_get_data(interface);
5046         if (!wifi || !wifi->mesh_interface) {
5047                 DBG("Virtual Mesh interface not created");
5048                 return;
5049         }
5050
5051         bssid = g_supplicant_network_get_bssid(supplicant_network);
5052         address = g_malloc0(19);
5053         snprintf(address, 19, "%02x:%02x:%02x:%02x:%02x:%02x", bssid[0], bssid[1],
5054                                                                  bssid[2], bssid[3], bssid[4], bssid[5]);
5055
5056         identifier = g_supplicant_network_get_identifier(supplicant_network);
5057         name = g_supplicant_network_get_name(supplicant_network);
5058         security = g_supplicant_network_get_security(supplicant_network);
5059         frequency = g_supplicant_network_get_frequency(supplicant_network);
5060
5061         mesh_info = wifi->mesh_info;
5062         connman_mesh = connman_mesh_get(mesh_info->identifier, identifier);
5063         if (connman_mesh)
5064                 goto done;
5065
5066         DBG("Mesh Peer name %s identifier %s security %s added", name, identifier,
5067                                         security);
5068         connman_mesh = connman_mesh_create(mesh_info->identifier, identifier);
5069         connman_mesh_set_name(connman_mesh, name);
5070         connman_mesh_set_security(connman_mesh, security);
5071         connman_mesh_set_frequency(connman_mesh, frequency);
5072         connman_mesh_set_address(connman_mesh, address);
5073         connman_mesh_set_index(connman_mesh, mesh_info->index);
5074         connman_mesh_set_strength(connman_mesh,
5075                                                 calculate_strength(supplicant_network));
5076         connman_mesh_set_peer_type(connman_mesh, CONNMAN_MESH_PEER_TYPE_DISCOVERED);
5077
5078         ret = connman_mesh_register(connman_mesh);
5079         if (ret == -EALREADY)
5080                 DBG("Mesh Peer is already registered");
5081
5082 done:
5083         g_free(address);
5084 }
5085
5086 static void mesh_peer_removed(GSupplicantNetwork *supplicant_network)
5087 {
5088         GSupplicantInterface *interface;
5089         struct wifi_data *wifi;
5090         struct connman_mesh *connman_mesh;
5091         struct wifi_mesh_info *mesh_info;
5092         const char *identifier;
5093
5094         interface = g_supplicant_network_get_interface(supplicant_network);
5095         wifi = g_supplicant_interface_get_data(interface);
5096         if (!wifi || !wifi->mesh_interface) {
5097                 DBG("Virtual Mesh interface not created");
5098                 return;
5099         }
5100
5101         identifier = g_supplicant_network_get_identifier(supplicant_network);
5102         if (!identifier) {
5103                 DBG("Failed to get Mesh Peer identifier");
5104                 return;
5105         }
5106
5107         mesh_info = wifi->mesh_info;
5108         connman_mesh = connman_mesh_get(mesh_info->identifier, identifier);
5109         if (connman_mesh) {
5110                 /* Do not unregister connected mesh peer */
5111                 if (connman_mesh_peer_is_connected_state(connman_mesh)) {
5112                         DBG("Mesh Peer %s is connected", identifier);
5113                         return;
5114                 }
5115                 DBG("Mesh Peer identifier %s removed", identifier);
5116                 connman_mesh_unregister(connman_mesh);
5117         }
5118 }
5119 #endif
5120
5121
5122 #if defined TIZEN_EXT
5123 static GSList *get_supported_security_list(unsigned int keymgmt,
5124                                         bool owe_transition_mode,
5125                                         GSupplicantNetwork *supplicant_network)
5126 {
5127         GSList *sec_list = NULL;
5128         dbus_bool_t privacy = g_supplicant_network_get_privacy(supplicant_network);
5129         const char *enc_mode = g_supplicant_network_get_enc_mode(supplicant_network);
5130
5131         if (keymgmt &
5132                         (G_SUPPLICANT_KEYMGMT_WPA_EAP |
5133                                         G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
5134                 sec_list = g_slist_prepend (sec_list, "ieee8021x");
5135         else if (keymgmt & G_SUPPLICANT_KEYMGMT_WPA_FT_EAP)
5136                 sec_list = g_slist_prepend (sec_list, "ft_ieee8021x");
5137
5138         if (sec_list)
5139                 return sec_list;
5140
5141         if (keymgmt &
5142                         (G_SUPPLICANT_KEYMGMT_WPA_PSK |
5143                                         G_SUPPLICANT_KEYMGMT_WPA_PSK_256)) {
5144                 if (!g_strcmp0(enc_mode, "aes"))
5145                         sec_list = g_slist_prepend (sec_list, "rsn");
5146                 else if (!g_strcmp0(enc_mode, "tkip"))
5147                         sec_list = g_slist_prepend (sec_list, "psk");
5148                 else if (!g_strcmp0(enc_mode, "mixed")) {
5149                         sec_list = g_slist_prepend (sec_list, "psk");
5150                         sec_list = g_slist_prepend (sec_list, "rsn");
5151                 }
5152         } else if (keymgmt & G_SUPPLICANT_KEYMGMT_WPA_FT_PSK)
5153                 sec_list = g_slist_prepend (sec_list, "ft_psk");
5154
5155         if (keymgmt & G_SUPPLICANT_KEYMGMT_SAE)
5156                 sec_list = g_slist_prepend (sec_list, "sae");
5157         if (keymgmt & G_SUPPLICANT_KEYMGMT_OWE || owe_transition_mode)
5158                 sec_list = g_slist_prepend (sec_list, "owe");
5159         if (keymgmt & G_SUPPLICANT_KEYMGMT_DPP)
5160                 sec_list = g_slist_prepend (sec_list, "dpp");
5161
5162         if (sec_list)
5163                 return sec_list;
5164
5165         if (privacy)
5166                 sec_list = g_slist_prepend (sec_list, "wep");
5167         else
5168                 sec_list = g_slist_prepend (sec_list, "none");
5169
5170         return sec_list;
5171 }
5172 #endif
5173
5174 static void network_added(GSupplicantNetwork *supplicant_network)
5175 {
5176         struct connman_network *network;
5177         GSupplicantInterface *interface;
5178         struct wifi_data *wifi;
5179         const char *name, *identifier, *security, *group, *mode;
5180         const unsigned char *ssid;
5181         unsigned int ssid_len;
5182         bool wps;
5183         bool wps_pbc;
5184         bool wps_ready;
5185         bool wps_advertizing;
5186
5187 #if defined TIZEN_EXT
5188         bool owe_transition_mode;
5189         const unsigned char *transition_mode_ssid;
5190         const unsigned char *transition_mode_bssid;
5191         unsigned int transition_mode_ssid_len;
5192         unsigned int keymgmt;
5193         GSList *vsie_list = NULL;
5194         GSList *sec_list = NULL;
5195         const unsigned char *country_code;
5196         ieee80211_modes_e phy_mode;
5197 #endif
5198
5199         mode = g_supplicant_network_get_mode(supplicant_network);
5200         identifier = g_supplicant_network_get_identifier(supplicant_network);
5201 #if defined TIZEN_EXT
5202         if (!simplified_log)
5203 #endif
5204         DBG("%s", identifier);
5205
5206         if (!g_strcmp0(mode, "adhoc"))
5207                 return;
5208
5209 #if defined TIZEN_EXT_WIFI_MESH
5210         if (!g_strcmp0(mode, "mesh")) {
5211                 mesh_peer_added(supplicant_network);
5212                 return;
5213         }
5214 #endif
5215
5216         interface = g_supplicant_network_get_interface(supplicant_network);
5217 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5218         /*
5219          * Note: If supplicant interface's driver is wired then skip it,
5220          * because it meanti only for ethernet not Wi-Fi.
5221          */
5222         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5223                 return;
5224 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5225
5226         wifi = g_supplicant_interface_get_data(interface);
5227         name = g_supplicant_network_get_name(supplicant_network);
5228         security = g_supplicant_network_get_security(supplicant_network);
5229         group = g_supplicant_network_get_identifier(supplicant_network);
5230         wps = g_supplicant_network_get_wps(supplicant_network);
5231         wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
5232         wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
5233         wps_advertizing = g_supplicant_network_is_wps_advertizing(
5234                                                         supplicant_network);
5235
5236         if (!wifi)
5237                 return;
5238
5239         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
5240
5241         network = connman_device_get_network(wifi->device, identifier);
5242
5243         if (!network) {
5244                 network = connman_network_create(identifier,
5245                                                 CONNMAN_NETWORK_TYPE_WIFI);
5246                 if (!network)
5247                         return;
5248
5249                 connman_network_set_index(network, wifi->index);
5250
5251                 if (connman_device_add_network(wifi->device, network) < 0) {
5252                         connman_network_unref(network);
5253                         return;
5254                 }
5255
5256                 wifi->networks = g_slist_prepend(wifi->networks, network);
5257         }
5258
5259         if (name && name[0] != '\0')
5260                 connman_network_set_name(network, name);
5261
5262         connman_network_set_blob(network, "WiFi.SSID",
5263                                                 ssid, ssid_len);
5264 #if defined TIZEN_EXT
5265         vsie_list = (GSList *)g_supplicant_network_get_wifi_vsie(supplicant_network);
5266         if (vsie_list)
5267                 connman_network_set_vsie_list(network, vsie_list);
5268         else
5269                 DBG("vsie_list is NULL");
5270         country_code = g_supplicant_network_get_countrycode(supplicant_network);
5271         connman_network_set_countrycode(network, country_code);
5272         phy_mode = g_supplicant_network_get_phy_mode(supplicant_network);
5273         connman_network_set_phy_mode(network, phy_mode);
5274 #endif
5275         connman_network_set_string(network, "WiFi.Security", security);
5276         connman_network_set_strength(network,
5277                                 calculate_strength(supplicant_network));
5278         connman_network_set_bool(network, "WiFi.WPS", wps);
5279         connman_network_set_bool(network, "WiFi.WPSAdvertising",
5280                                 wps_advertizing);
5281
5282         if (wps) {
5283                 /* Is AP advertizing for WPS association?
5284                  * If so, we decide to use WPS by default */
5285                 if (wps_ready && wps_pbc &&
5286                                                 wps_advertizing)
5287 #if !defined TIZEN_EXT
5288                         connman_network_set_bool(network, "WiFi.UseWPS", true);
5289 #else
5290                         DBG("wps is activating by ap but ignore it.");
5291 #endif
5292         }
5293
5294         connman_network_set_frequency(network,
5295                         g_supplicant_network_get_frequency(supplicant_network));
5296
5297 #if defined TIZEN_EXT
5298         keymgmt = g_supplicant_network_get_keymgmt(supplicant_network);
5299         connman_network_set_bssid(network,
5300                         g_supplicant_network_get_bssid(supplicant_network));
5301         owe_transition_mode = (bool)g_supplicant_network_get_transition_mode(supplicant_network);
5302         connman_network_set_bool(network, "WiFi.TRANSITION_MODE", owe_transition_mode);
5303         if (owe_transition_mode) {
5304                 transition_mode_ssid = (unsigned char *)g_supplicant_network_get_transition_mode_ssid(supplicant_network, &transition_mode_ssid_len);
5305                 connman_network_set_blob(network, "WiFi.TRANSITION_MODE_SSID",
5306                                                         transition_mode_ssid, transition_mode_ssid_len);
5307                 transition_mode_bssid = g_supplicant_network_get_transition_mode_bssid(supplicant_network);
5308                 connman_network_set_transition_mode_bssid(network, transition_mode_bssid);
5309         }
5310
5311         sec_list = get_supported_security_list(keymgmt,
5312                         owe_transition_mode, supplicant_network);
5313
5314         connman_network_set_sec_list(network, sec_list);
5315         connman_network_set_maxrate(network,
5316                         g_supplicant_network_get_maxrate(supplicant_network));
5317         connman_network_set_enc_mode(network,
5318                         g_supplicant_network_get_enc_mode(supplicant_network));
5319         connman_network_set_rsn_mode(network,
5320                         g_supplicant_network_get_rsn_mode(supplicant_network));
5321         connman_network_set_keymgmt(network, keymgmt);
5322         connman_network_set_bool(network, "WiFi.HS20AP",
5323                         g_supplicant_network_is_hs20AP(supplicant_network));
5324         connman_network_set_bssid_list(network,
5325                         (GSList *)g_supplicant_network_get_bssid_list(supplicant_network));
5326         connman_network_set_last_connected_bssid(network,
5327                         g_supplicant_network_get_last_connected_bssid(supplicant_network));
5328         connman_network_set_assoc_reject_table(network,
5329                         g_supplicant_network_get_assoc_reject_table(supplicant_network));
5330 #endif
5331         connman_network_set_available(network, true);
5332         connman_network_set_string(network, "WiFi.Mode", mode);
5333
5334 #if defined TIZEN_EXT
5335         if (group)
5336 #else
5337         if (ssid)
5338 #endif
5339                 connman_network_set_group(network, group);
5340
5341 #if defined TIZEN_EXT
5342         g_supplicant_network_set_last_connected_bssid(supplicant_network,
5343                         connman_network_get_last_connected_bssid(network));
5344 #endif
5345
5346 #if defined TIZEN_EXT
5347         if (wifi_first_scan == true)
5348                 found_with_first_scan = true;
5349 #endif
5350
5351         if (wifi->hidden && ssid) {
5352 #if defined TIZEN_EXT
5353                 if (network_security(wifi->hidden->security) ==
5354                         network_security(security) &&
5355 #else
5356                 if (!g_strcmp0(wifi->hidden->security, security) &&
5357 #endif
5358                                 wifi->hidden->ssid_len == ssid_len &&
5359                                 !memcmp(wifi->hidden->ssid, ssid, ssid_len)) {
5360                         connman_network_connect_hidden(network,
5361                                         wifi->hidden->identity,
5362                                         wifi->hidden->passphrase,
5363                                         wifi->hidden->user_data);
5364                         wifi->hidden->user_data = NULL;
5365                         hidden_free(wifi->hidden);
5366                         wifi->hidden = NULL;
5367                 }
5368         }
5369 }
5370
5371 static void network_removed(GSupplicantNetwork *network)
5372 {
5373         GSupplicantInterface *interface;
5374         struct wifi_data *wifi;
5375         const char *name, *identifier;
5376         struct connman_network *connman_network;
5377
5378 #if defined TIZEN_EXT_WIFI_MESH
5379         const char *mode;
5380         mode = g_supplicant_network_get_mode(network);
5381         if (!g_strcmp0(mode, "mesh")) {
5382                 mesh_peer_removed(network);
5383                 return;
5384         }
5385 #endif
5386
5387         interface = g_supplicant_network_get_interface(network);
5388 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5389         /*
5390          * Note: If supplicant interface's driver is wired then skip it,
5391          * because it meanti only for ethernet not Wi-Fi.
5392          */
5393         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5394                 return;
5395 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5396
5397         wifi = g_supplicant_interface_get_data(interface);
5398         identifier = g_supplicant_network_get_identifier(network);
5399         name = g_supplicant_network_get_name(network);
5400
5401         DBG("name %s", name);
5402
5403         if (!wifi)
5404                 return;
5405
5406         connman_network = connman_device_get_network(wifi->device, identifier);
5407         if (!connman_network)
5408                 return;
5409
5410 #if defined TIZEN_EXT
5411         if (connman_network == wifi->scan_pending_network)
5412                 wifi->scan_pending_network = NULL;
5413
5414         if (connman_network == wifi->pending_network)
5415                 wifi->pending_network = NULL;
5416
5417         if(connman_network_get_connecting(connman_network) == true){
5418                 connman_network_set_connected(connman_network, false);
5419         }
5420 #endif
5421
5422         wifi->networks = g_slist_remove(wifi->networks, connman_network);
5423
5424         connman_device_remove_network(wifi->device, connman_network);
5425         connman_network_unref(connman_network);
5426 }
5427
5428 static void network_changed(GSupplicantNetwork *network, const char *property)
5429 {
5430         GSupplicantInterface *interface;
5431         struct wifi_data *wifi;
5432         const char *name, *identifier;
5433         struct connman_network *connman_network;
5434         bool update_needed;
5435
5436 #if defined TIZEN_EXT
5437         const unsigned char *bssid;
5438         unsigned int maxrate;
5439         uint16_t frequency;
5440         bool wps;
5441         const unsigned char *country_code;
5442         ieee80211_modes_e phy_mode;
5443         GSList *bssid_list;
5444 #endif
5445
5446         interface = g_supplicant_network_get_interface(network);
5447 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5448         /*
5449          * Note: If supplicant interface's driver is wired then skip it,
5450          * because it meanti only for ethernet not Wi-Fi.
5451          */
5452         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5453                 return;
5454 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5455
5456         wifi = g_supplicant_interface_get_data(interface);
5457         identifier = g_supplicant_network_get_identifier(network);
5458         name = g_supplicant_network_get_name(network);
5459
5460 #if defined TIZEN_EXT
5461         if (!simplified_log)
5462                 DBG("name %s property %s", name, property);
5463 #else
5464         DBG("name %s", name);
5465 #endif
5466
5467         if (!wifi)
5468                 return;
5469
5470         connman_network = connman_device_get_network(wifi->device, identifier);
5471         if (!connman_network)
5472                 return;
5473
5474         if (g_str_equal(property, "WPSCapabilities")) {
5475                 bool wps;
5476                 bool wps_pbc;
5477                 bool wps_ready;
5478                 bool wps_advertizing;
5479
5480                 wps = g_supplicant_network_get_wps(network);
5481                 wps_pbc = g_supplicant_network_is_wps_pbc(network);
5482                 wps_ready = g_supplicant_network_is_wps_active(network);
5483                 wps_advertizing =
5484                         g_supplicant_network_is_wps_advertizing(network);
5485
5486                 connman_network_set_bool(connman_network, "WiFi.WPS", wps);
5487                 connman_network_set_bool(connman_network,
5488                                 "WiFi.WPSAdvertising", wps_advertizing);
5489
5490                 if (wps) {
5491                         /*
5492                          * Is AP advertizing for WPS association?
5493                          * If so, we decide to use WPS by default
5494                          */
5495                         if (wps_ready && wps_pbc && wps_advertizing)
5496                                 connman_network_set_bool(connman_network,
5497                                                         "WiFi.UseWPS", true);
5498                 }
5499
5500                 update_needed = true;
5501         } else if (g_str_equal(property, "Signal")) {
5502                 connman_network_set_strength(connman_network,
5503                                         calculate_strength(network));
5504                 update_needed = true;
5505         }
5506 #if defined TIZEN_EXT && defined TIZEN_EXT_INS
5507         else if (g_str_equal(property, "LastConnectedBSSID")) {
5508                 const char *ident, *group;
5509                 char *service_ident;
5510                 bool need_save;
5511
5512                 ident = connman_device_get_ident(wifi->device);
5513                 group = connman_network_get_group(connman_network);
5514                 if (ident && group) {
5515                         service_ident = g_strdup_printf("%s_%s_%s",
5516                                 __connman_network_get_type(connman_network), ident, group);
5517
5518                         need_save = connman_device_set_last_connected_ident(wifi->device, service_ident);
5519                         if (need_save)
5520                                 connman_device_save_last_connected(wifi->device);
5521
5522                         g_free(service_ident);
5523                 }
5524
5525                 connman_network_set_last_connected_bssid(connman_network,
5526                                         g_supplicant_network_get_last_connected_bssid(network));
5527
5528                 update_needed = true;
5529         }
5530 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_INS */
5531 #if defined TIZEN_EXT
5532         else if (g_str_equal(property, "UpdateAssocReject")) {
5533                 connman_network_set_assoc_reject_table(connman_network,
5534                                         g_supplicant_network_get_assoc_reject_table(network));
5535                 update_needed = true;
5536         }
5537 #endif
5538         else
5539                 update_needed = false;
5540
5541         if (update_needed)
5542                 connman_network_update(connman_network);
5543
5544 #if defined TIZEN_EXT
5545         bssid = g_supplicant_network_get_bssid(network);
5546         maxrate = g_supplicant_network_get_maxrate(network);
5547         frequency = g_supplicant_network_get_frequency(network);
5548         wps = g_supplicant_network_get_wps(network);
5549         phy_mode = g_supplicant_network_get_phy_mode(network);
5550
5551         connman_network_set_bssid(connman_network, bssid);
5552         connman_network_set_maxrate(connman_network, maxrate);
5553         connman_network_set_frequency(connman_network, frequency);
5554         connman_network_set_bool(connman_network, "WiFi.WPS", wps);
5555         country_code = g_supplicant_network_get_countrycode(network);
5556         connman_network_set_countrycode(connman_network, country_code);
5557         bssid_list = (GSList *)g_supplicant_network_get_bssid_list(network);
5558         connman_network_set_bssid_list(connman_network, bssid_list);
5559         connman_network_set_phy_mode(connman_network, phy_mode);
5560
5561         if (g_str_equal(property, "CheckMultiBssidConnect") &&
5562                         connman_network_get_associating(connman_network))
5563                 network_connect(connman_network);
5564 #endif
5565 }
5566
5567 static void network_associated(GSupplicantNetwork *network)
5568 {
5569         GSupplicantInterface *interface;
5570         struct wifi_data *wifi;
5571         struct connman_network *connman_network;
5572         const char *identifier;
5573
5574         DBG("");
5575
5576         interface = g_supplicant_network_get_interface(network);
5577         if (!interface)
5578                 return;
5579 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5580         /*
5581          * Note: If supplicant interface's driver is wired then skip it,
5582          * because it meanti only for ethernet not Wi-Fi.
5583          */
5584         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5585                 return;
5586 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5587
5588
5589         wifi = g_supplicant_interface_get_data(interface);
5590         if (!wifi)
5591                 return;
5592
5593         /* P2P networks must not be treated as WiFi networks */
5594         if (wifi->p2p_connecting || wifi->p2p_device)
5595                 return;
5596
5597         identifier = g_supplicant_network_get_identifier(network);
5598
5599         connman_network = connman_device_get_network(wifi->device, identifier);
5600         if (!connman_network)
5601                 return;
5602
5603         if (wifi->network) {
5604                 if (wifi->network == connman_network)
5605                         return;
5606 #if TIZEN_EXT
5607                 unsigned int ssid_len;
5608                 DBG("network1 ssid[%s] , OWE[%d],ssid[%s]",
5609                         (char *)connman_network_get_blob(wifi->network,"WiFi.SSID", &ssid_len),
5610                         connman_network_get_bool(wifi->network,"WiFi.TRANSITION_MODE"),
5611                         (char *)connman_network_get_blob(wifi->network,"WiFi.TRANSITION_MODE_SSID", &ssid_len));
5612
5613                 DBG("network1 ssid[%s], OWE[%d], ssid[%s]",
5614                         (char *)connman_network_get_blob(connman_network,"WiFi.SSID",&ssid_len),
5615                         connman_network_get_bool(connman_network,"WiFi.TRANSITION_MODE"),
5616                         (char *)connman_network_get_blob(connman_network,"WiFi.TRANSITION_MODE_SSID", &ssid_len));
5617                 if (connman_network_check_transition_mode(wifi->network, connman_network)) {//OWE trasition mode check
5618                         DBG("OWE transition mode is TRUE");
5619                         return;
5620                 }
5621 #endif
5622                 /*
5623                  * This should never happen, we got associated with
5624                  * a network different than the one we were expecting.
5625                  */
5626                 DBG("Associated to %p while expecting %p",
5627                                         connman_network, wifi->network);
5628
5629                 connman_network_set_associating(wifi->network, false);
5630         }
5631
5632         DBG("Reconnecting to previous network %p from wpa_s", connman_network);
5633
5634         wifi->network = connman_network_ref(connman_network);
5635         wifi->retries = 0;
5636
5637         /*
5638          * Interface state changes callback (interface_state) is always
5639          * called before network_associated callback thus we need to call
5640          * interface_state again in order to process the new state now that
5641          * we have the network properly set.
5642          */
5643         interface_state(interface);
5644 }
5645
5646 static void sta_authorized(GSupplicantInterface *interface,
5647                                         const char *addr)
5648 {
5649 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5650         /*
5651          * Note: If supplicant interface's driver is wired then skip it,
5652          * because it meanti only for ethernet not Wi-Fi.
5653          */
5654         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5655                 return;
5656 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5657
5658         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
5659
5660         DBG("wifi %p station %s authorized", wifi, addr);
5661
5662         if (!wifi || !wifi->tethering)
5663                 return;
5664
5665         __connman_tethering_client_register(addr);
5666 }
5667
5668 static void sta_deauthorized(GSupplicantInterface *interface,
5669                                         const char *addr)
5670 {
5671 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5672         /*
5673          * Note: If supplicant interface's driver is wired then skip it,
5674          * because it meanti only for ethernet not Wi-Fi.
5675          */
5676         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5677                 return;
5678 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5679
5680         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
5681
5682         DBG("wifi %p station %s deauthorized", wifi, addr);
5683
5684         if (!wifi || !wifi->tethering)
5685                 return;
5686
5687         __connman_tethering_client_unregister(addr);
5688 }
5689
5690 static void apply_peer_services(GSupplicantPeer *peer,
5691                                 struct connman_peer *connman_peer)
5692 {
5693         const unsigned char *data;
5694         int length;
5695
5696         DBG("");
5697
5698         connman_peer_reset_services(connman_peer);
5699
5700         data = g_supplicant_peer_get_widi_ies(peer, &length);
5701         if (data) {
5702                 connman_peer_add_service(connman_peer,
5703                         CONNMAN_PEER_SERVICE_WIFI_DISPLAY, data, length);
5704         }
5705 }
5706
5707 static void peer_found(GSupplicantPeer *peer)
5708 {
5709         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
5710         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
5711         struct connman_peer *connman_peer;
5712         const char *identifier, *name;
5713         int ret;
5714
5715 #if defined TIZEN_EXT
5716         if (!wifi)
5717                 return;
5718 #endif
5719         identifier = g_supplicant_peer_get_identifier(peer);
5720         name = g_supplicant_peer_get_name(peer);
5721
5722         DBG("ident: %s", identifier);
5723
5724         connman_peer = connman_peer_get(wifi->device, identifier);
5725         if (connman_peer)
5726                 return;
5727
5728         connman_peer = connman_peer_create(identifier);
5729         connman_peer_set_name(connman_peer, name);
5730         connman_peer_set_device(connman_peer, wifi->device);
5731         apply_peer_services(peer, connman_peer);
5732
5733         ret = connman_peer_register(connman_peer);
5734         if (ret < 0 && ret != -EALREADY)
5735                 connman_peer_unref(connman_peer);
5736         else
5737                 wifi->peers = g_slist_prepend(wifi->peers, connman_peer);
5738 }
5739
5740 static void peer_lost(GSupplicantPeer *peer)
5741 {
5742         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
5743         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
5744         struct connman_peer *connman_peer;
5745         const char *identifier;
5746
5747         if (!wifi)
5748                 return;
5749
5750         identifier = g_supplicant_peer_get_identifier(peer);
5751
5752         DBG("ident: %s", identifier);
5753
5754         connman_peer = connman_peer_get(wifi->device, identifier);
5755         if (connman_peer) {
5756                 if (wifi->p2p_connecting &&
5757                                 wifi->pending_peer == connman_peer) {
5758                         peer_connect_timeout(wifi);
5759                 }
5760                 connman_peer_unregister(connman_peer);
5761                 connman_peer_unref(connman_peer);
5762         }
5763
5764         wifi->peers = g_slist_remove(wifi->peers, connman_peer);
5765 }
5766
5767 static void peer_changed(GSupplicantPeer *peer, GSupplicantPeerState state)
5768 {
5769         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
5770         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
5771         enum connman_peer_state p_state = CONNMAN_PEER_STATE_UNKNOWN;
5772         struct connman_peer *connman_peer;
5773         const char *identifier;
5774
5775         identifier = g_supplicant_peer_get_identifier(peer);
5776
5777         DBG("ident: %s", identifier);
5778
5779         if (!wifi)
5780                 return;
5781
5782         connman_peer = connman_peer_get(wifi->device, identifier);
5783         if (!connman_peer)
5784                 return;
5785
5786         switch (state) {
5787         case G_SUPPLICANT_PEER_SERVICES_CHANGED:
5788                 apply_peer_services(peer, connman_peer);
5789                 connman_peer_services_changed(connman_peer);
5790                 return;
5791         case G_SUPPLICANT_PEER_GROUP_CHANGED:
5792                 if (!g_supplicant_peer_is_in_a_group(peer))
5793                         p_state = CONNMAN_PEER_STATE_IDLE;
5794                 else
5795                         p_state = CONNMAN_PEER_STATE_CONFIGURATION;
5796                 break;
5797         case G_SUPPLICANT_PEER_GROUP_STARTED:
5798                 break;
5799         case G_SUPPLICANT_PEER_GROUP_FINISHED:
5800                 p_state = CONNMAN_PEER_STATE_IDLE;
5801                 break;
5802         case G_SUPPLICANT_PEER_GROUP_JOINED:
5803                 connman_peer_set_iface_address(connman_peer,
5804                                 g_supplicant_peer_get_iface_address(peer));
5805                 break;
5806         case G_SUPPLICANT_PEER_GROUP_DISCONNECTED:
5807                 p_state = CONNMAN_PEER_STATE_IDLE;
5808                 break;
5809         case G_SUPPLICANT_PEER_GROUP_FAILED:
5810                 if (g_supplicant_peer_has_requested_connection(peer))
5811                         p_state = CONNMAN_PEER_STATE_IDLE;
5812                 else
5813                         p_state = CONNMAN_PEER_STATE_FAILURE;
5814                 break;
5815         }
5816
5817         if (p_state == CONNMAN_PEER_STATE_CONFIGURATION ||
5818                                         p_state == CONNMAN_PEER_STATE_FAILURE) {
5819                 if (wifi->p2p_connecting
5820                                 && connman_peer == wifi->pending_peer)
5821                         peer_cancel_timeout(wifi);
5822                 else
5823                         p_state = CONNMAN_PEER_STATE_UNKNOWN;
5824         }
5825
5826         if (p_state == CONNMAN_PEER_STATE_UNKNOWN)
5827                 return;
5828
5829         if (p_state == CONNMAN_PEER_STATE_CONFIGURATION) {
5830                 GSupplicantInterface *g_iface;
5831                 struct wifi_data *g_wifi;
5832
5833                 g_iface = g_supplicant_peer_get_group_interface(peer);
5834                 if (!g_iface)
5835                         return;
5836
5837                 g_wifi = g_supplicant_interface_get_data(g_iface);
5838                 if (!g_wifi)
5839                         return;
5840
5841                 connman_peer_set_as_master(connman_peer,
5842                                         !g_supplicant_peer_is_client(peer));
5843                 connman_peer_set_sub_device(connman_peer, g_wifi->device);
5844
5845                 /*
5846                  * If wpa_supplicant didn't create a dedicated p2p-group
5847                  * interface then mark this interface as p2p_device to avoid
5848                  * scan and auto-scan are launched on it while P2P is connected.
5849                  */
5850                 if (!g_list_find(p2p_iface_list, g_wifi))
5851                         wifi->p2p_device = true;
5852         }
5853
5854         connman_peer_set_state(connman_peer, p_state);
5855 }
5856
5857 static void peer_request(GSupplicantPeer *peer)
5858 {
5859         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
5860         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
5861         struct connman_peer *connman_peer;
5862         const char *identifier;
5863
5864 #if defined TIZEN_EXT
5865         if (!wifi)
5866                 return;
5867 #endif
5868
5869         identifier = g_supplicant_peer_get_identifier(peer);
5870
5871         DBG("ident: %s", identifier);
5872
5873         connman_peer = connman_peer_get(wifi->device, identifier);
5874         if (!connman_peer)
5875                 return;
5876
5877         connman_peer_request_connection(connman_peer);
5878 }
5879
5880 #if defined TIZEN_EXT
5881 static void system_power_off(void)
5882 {
5883         GList *list;
5884         struct wifi_data *wifi;
5885         struct connman_service *service;
5886         struct connman_ipconfig *ipconfig_ipv4;
5887
5888         if (connman_setting_get_bool("WiFiDHCPRelease") == true) {
5889                 for (list = iface_list; list; list = list->next) {
5890                         wifi = list->data;
5891
5892                         if (wifi->network != NULL) {
5893                                 service = connman_service_lookup_from_network(wifi->network);
5894                                 ipconfig_ipv4 = __connman_service_get_ip4config(service);
5895                                 __connman_dhcp_stop(ipconfig_ipv4);
5896                         }
5897                 }
5898         }
5899 }
5900
5901 static void network_merged(GSupplicantNetwork *network)
5902 {
5903         GSupplicantInterface *interface;
5904         GSupplicantState state;
5905         struct wifi_data *wifi;
5906         const char *identifier;
5907         struct connman_network *connman_network;
5908         bool ishs20AP = 0;
5909         char *temp = NULL;
5910
5911         interface = g_supplicant_network_get_interface(network);
5912         if (!interface)
5913                 return;
5914
5915 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5916         /*
5917          * Note: If supplicant interface's driver is wired then skip it,
5918          * because it meanti only for ethernet not Wi-Fi.
5919          */
5920         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5921                 return;
5922 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5923
5924         state = g_supplicant_interface_get_state(interface);
5925         if (state < G_SUPPLICANT_STATE_AUTHENTICATING)
5926                 return;
5927
5928         wifi = g_supplicant_interface_get_data(interface);
5929         if (!wifi)
5930                 return;
5931
5932         identifier = g_supplicant_network_get_identifier(network);
5933
5934         connman_network = connman_device_get_network(wifi->device, identifier);
5935         if (!connman_network)
5936                 return;
5937
5938         DBG("merged identifier %s", identifier);
5939
5940         if (wifi->connected == FALSE) {
5941                 switch (state) {
5942                 case G_SUPPLICANT_STATE_AUTHENTICATING:
5943                 case G_SUPPLICANT_STATE_ASSOCIATING:
5944                 case G_SUPPLICANT_STATE_ASSOCIATED:
5945                 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
5946                 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
5947                         connman_network_set_associating(connman_network, TRUE);
5948                         break;
5949                 case G_SUPPLICANT_STATE_COMPLETED:
5950                         connman_network_set_connected(connman_network, TRUE);
5951                         break;
5952                 default:
5953                         DBG("Not handled the state : %d", state);
5954                         break;
5955                 }
5956         }
5957
5958         ishs20AP = g_supplicant_network_is_hs20AP(network);
5959
5960         if (ishs20AP &&
5961                 g_strcmp0(g_supplicant_network_get_security(network), "ieee8021x") == 0) {
5962                 temp = g_ascii_strdown(g_supplicant_network_get_eap(network), -1);
5963                 connman_network_set_string(connman_network, "WiFi.EAP",
5964                                 temp);
5965                 connman_network_set_string(connman_network, "WiFi.Identity",
5966                                 g_supplicant_network_get_identity(network));
5967                 connman_network_set_string(connman_network, "WiFi.Phase2",
5968                                 g_supplicant_network_get_phase2(network));
5969
5970                 g_free(temp);
5971         }
5972
5973         wifi->network = connman_network;
5974 }
5975
5976 static void assoc_failed(void *user_data)
5977 {
5978         struct connman_network *network = user_data;
5979         connman_network_set_associating(network, false);
5980 }
5981
5982 static void scan_done(GSupplicantInterface *interface)
5983 {
5984 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5985         /*
5986          * Note: If supplicant interface's driver is wired then skip it,
5987          * because it meanti only for ethernet not Wi-Fi.
5988          */
5989         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5990                 return;
5991 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5992
5993         GList *list;
5994         int scan_type = CONNMAN_SCAN_TYPE_WPA_SUPPLICANT;
5995         struct wifi_data *wifi;
5996         bool scanning;
5997
5998         for (list = iface_list; list; list = list->next) {
5999                 wifi = list->data;
6000
6001                 if (interface == wifi->interface) {
6002                         scanning = connman_device_get_scanning(wifi->device,
6003                                         CONNMAN_SERVICE_TYPE_WIFI);
6004                         if (!scanning)
6005                                 __connman_technology_notify_scan_done(
6006                                                 connman_device_get_string(wifi->device, "Interface"), scan_type);
6007                         break;
6008                 }
6009         }
6010 }
6011 #endif
6012
6013 static void debug(const char *str)
6014 {
6015 #if defined TIZEN_EXT
6016         if (connman_setting_get_bool("ConnmanSupplicantDebug"))
6017 #else
6018         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
6019 #endif
6020                 connman_debug("%s", str);
6021 }
6022
6023 static void disconnect_reasoncode(GSupplicantInterface *interface,
6024                                 int reasoncode)
6025 {
6026 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6027         /*
6028          * Note: If supplicant interface's driver is wired then skip it,
6029          * because it meanti only for ethernet not Wi-Fi.
6030          */
6031         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
6032                 return;
6033 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6034
6035         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
6036
6037         if (wifi != NULL) {
6038                 wifi->disconnect_code = reasoncode;
6039         }
6040 }
6041
6042 static void assoc_status_code(GSupplicantInterface *interface, int status_code)
6043 {
6044 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6045         /*
6046          * Note: If supplicant interface's driver is wired then skip it,
6047          * because it meanti only for ethernet not Wi-Fi.
6048          */
6049         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
6050                 return;
6051 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6052
6053         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
6054
6055         if (wifi != NULL) {
6056                 wifi->assoc_code = status_code;
6057         }
6058 }
6059
6060 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6061 static GSupplicantCallbacks callbacks = {
6062 #else /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6063 static const GSupplicantCallbacks callbacks = {
6064 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6065         .system_ready           = system_ready,
6066         .system_killed          = system_killed,
6067         .interface_added        = interface_added,
6068         .interface_state        = interface_state,
6069         .interface_removed      = interface_removed,
6070         .p2p_support            = p2p_support,
6071         .scan_started           = scan_started,
6072         .scan_finished          = scan_finished,
6073         .ap_create_fail         = ap_create_fail,
6074         .network_added          = network_added,
6075         .network_removed        = network_removed,
6076         .network_changed        = network_changed,
6077         .network_associated     = network_associated,
6078         .sta_authorized         = sta_authorized,
6079         .sta_deauthorized       = sta_deauthorized,
6080         .peer_found             = peer_found,
6081         .peer_lost              = peer_lost,
6082         .peer_changed           = peer_changed,
6083         .peer_request           = peer_request,
6084 #if defined TIZEN_EXT
6085         .system_power_off       = system_power_off,
6086         .network_merged         = network_merged,
6087         .assoc_failed           = assoc_failed,
6088         .scan_done              = scan_done,
6089 #endif
6090         .debug                  = debug,
6091         .disconnect_reasoncode  = disconnect_reasoncode,
6092         .assoc_status_code      = assoc_status_code,
6093 #if defined TIZEN_EXT_WIFI_MESH
6094         .mesh_support           = mesh_support,
6095         .mesh_group_started = mesh_group_started,
6096         .mesh_group_removed = mesh_group_removed,
6097         .mesh_peer_connected = mesh_peer_connected,
6098         .mesh_peer_disconnected = mesh_peer_disconnected,
6099 #endif
6100 };
6101
6102
6103 static int tech_probe(struct connman_technology *technology)
6104 {
6105         wifi_technology = technology;
6106
6107         return 0;
6108 }
6109
6110 static void tech_remove(struct connman_technology *technology)
6111 {
6112         wifi_technology = NULL;
6113 }
6114
6115 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
6116 {
6117         GSupplicantSSID *ap;
6118
6119         ap = g_try_malloc0(sizeof(GSupplicantSSID));
6120         if (!ap)
6121                 return NULL;
6122
6123         ap->mode = G_SUPPLICANT_MODE_MASTER;
6124 #if defined TIZEN_EXT
6125         ap->ssid = (void *) ssid;
6126 #else
6127         ap->ssid = ssid;
6128 #endif
6129         ap->ssid_len = strlen(ssid);
6130         ap->scan_ssid = 0;
6131         ap->freq = 2412;
6132
6133         if (!passphrase || strlen(passphrase) == 0) {
6134                 ap->security = G_SUPPLICANT_SECURITY_NONE;
6135                 ap->passphrase = NULL;
6136         } else {
6137                ap->security = G_SUPPLICANT_SECURITY_PSK;
6138                ap->protocol = G_SUPPLICANT_PROTO_RSN;
6139                ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
6140                ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
6141                ap->passphrase = passphrase;
6142         }
6143
6144         return ap;
6145 }
6146
6147 static void ap_start_callback(int result, GSupplicantInterface *interface,
6148                                                         void *user_data)
6149 {
6150         struct wifi_tethering_info *info = user_data;
6151
6152         DBG("result %d index %d bridge %s",
6153                 result, info->wifi->index, info->wifi->bridge);
6154
6155         if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
6156                 connman_inet_remove_from_bridge(info->wifi->index,
6157                                                         info->wifi->bridge);
6158
6159                 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
6160                         connman_technology_tethering_notify(info->technology, false);
6161                         g_free(info->wifi->tethering_param->ssid);
6162                         g_free(info->wifi->tethering_param);
6163                         info->wifi->tethering_param = NULL;
6164                 }
6165         }
6166
6167         g_free(info->ifname);
6168         g_free(info);
6169 }
6170
6171 static void ap_create_callback(int result,
6172                                 GSupplicantInterface *interface,
6173                                         void *user_data)
6174 {
6175         struct wifi_tethering_info *info = user_data;
6176
6177         DBG("result %d ifname %s", result,
6178                                 g_supplicant_interface_get_ifname(interface));
6179
6180         if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
6181                 connman_inet_remove_from_bridge(info->wifi->index,
6182                                                         info->wifi->bridge);
6183
6184                 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
6185                         connman_technology_tethering_notify(info->technology, false);
6186                         g_free(info->wifi->tethering_param->ssid);
6187                         g_free(info->wifi->tethering_param);
6188                         info->wifi->tethering_param = NULL;
6189
6190                 }
6191
6192                 g_free(info->ifname);
6193                 g_free(info->ssid);
6194                 g_free(info);
6195                 return;
6196         }
6197
6198         info->wifi->interface = interface;
6199         g_supplicant_interface_set_data(interface, info->wifi);
6200
6201         if (g_supplicant_interface_set_apscan(interface, 2) < 0)
6202                 connman_error("Failed to set interface ap_scan property");
6203
6204         g_supplicant_interface_connect(interface, info->ssid,
6205                                                 ap_start_callback, info);
6206 }
6207
6208 static void sta_remove_callback(int result,
6209                                 GSupplicantInterface *interface,
6210                                         void *user_data)
6211 {
6212         struct wifi_tethering_info *info = user_data;
6213         const char *driver = connman_option_get_string("wifi");
6214
6215         DBG("ifname %s result %d ", info->ifname, result);
6216
6217         if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
6218                 info->wifi->tethering = false;
6219                 connman_technology_tethering_notify(info->technology, false);
6220
6221                 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
6222                         g_free(info->wifi->tethering_param->ssid);
6223                         g_free(info->wifi->tethering_param);
6224                         info->wifi->tethering_param = NULL;
6225                 }
6226
6227                 g_free(info->ifname);
6228                 g_free(info->ssid);
6229                 g_free(info);
6230                 return;
6231         }
6232
6233         info->wifi->interface = NULL;
6234
6235         g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
6236 #ifdef TIZEN_EXT
6237                         0, 0, 60,
6238 #endif /* TIZEN_EXT */
6239                                                 ap_create_callback,
6240                                                         info);
6241 }
6242
6243 static int enable_wifi_tethering(struct connman_technology *technology,
6244                                 const char *bridge, const char *identifier,
6245                                 const char *passphrase, bool available)
6246 {
6247         GList *list;
6248         GSupplicantInterface *interface;
6249         struct wifi_data *wifi;
6250         struct wifi_tethering_info *info;
6251         const char *ifname;
6252         unsigned int mode;
6253         int err, berr = 0;
6254
6255         for (list = iface_list; list; list = list->next) {
6256                 wifi = list->data;
6257
6258                 DBG("wifi %p network %p pending_network %p", wifi,
6259                         wifi->network, wifi->pending_network);
6260
6261                 interface = wifi->interface;
6262
6263                 if (!interface)
6264                         continue;
6265
6266                 ifname = g_supplicant_interface_get_ifname(wifi->interface);
6267                 if (!ifname)
6268                         continue;
6269
6270                 if (wifi->ap_supported == WIFI_AP_NOT_SUPPORTED) {
6271                         DBG("%s does not support AP mode (detected)", ifname);
6272                         continue;
6273                 }
6274
6275                 mode = g_supplicant_interface_get_mode(interface);
6276                 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
6277                         wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
6278                         DBG("%s does not support AP mode (capability)", ifname);
6279                         continue;
6280                 }
6281
6282                 if (wifi->network && available)
6283                         continue;
6284
6285                 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
6286                 if (!info)
6287                         return -ENOMEM;
6288
6289                 wifi->tethering_param = g_try_malloc0(sizeof(struct wifi_tethering_info));
6290                 if (!wifi->tethering_param) {
6291                         g_free(info);
6292                         return -ENOMEM;
6293                 }
6294
6295                 info->wifi = wifi;
6296                 info->technology = technology;
6297                 info->wifi->bridge = bridge;
6298                 info->ssid = ssid_ap_init(identifier, passphrase);
6299                 if (!info->ssid)
6300                         goto failed;
6301
6302                 info->ifname = g_strdup(ifname);
6303
6304                 wifi->tethering_param->technology = technology;
6305                 wifi->tethering_param->ssid = ssid_ap_init(identifier, passphrase);
6306                 if (!wifi->tethering_param->ssid)
6307                         goto failed;
6308
6309                 info->wifi->tethering = true;
6310                 info->wifi->ap_supported = WIFI_AP_SUPPORTED;
6311
6312                 berr = connman_technology_tethering_notify(technology, true);
6313                 if (berr < 0)
6314                         goto failed;
6315
6316                 err = g_supplicant_interface_remove(interface,
6317                                                 sta_remove_callback,
6318                                                         info);
6319                 if (err >= 0) {
6320                         DBG("tethering wifi %p ifname %s", wifi, ifname);
6321                         return 0;
6322                 }
6323
6324         failed:
6325                 g_free(info->ifname);
6326                 g_free(info->ssid);
6327                 g_free(info);
6328                 g_free(wifi->tethering_param);
6329                 wifi->tethering_param = NULL;
6330
6331                 /*
6332                  * Remove bridge if it was correctly created but remove
6333                  * operation failed. Instead, if bridge creation failed then
6334                  * break out and do not try again on another interface,
6335                  * bridge set-up does not depend on it.
6336                  */
6337                 if (berr == 0)
6338                         connman_technology_tethering_notify(technology, false);
6339                 else
6340                         break;
6341         }
6342
6343         return -EOPNOTSUPP;
6344 }
6345
6346 static int tech_set_tethering(struct connman_technology *technology,
6347                                 const char *identifier, const char *passphrase,
6348                                 const char *bridge, bool enabled)
6349 {
6350         GList *list;
6351         struct wifi_data *wifi;
6352         int err;
6353
6354         DBG("");
6355
6356         if (!enabled) {
6357                 for (list = iface_list; list; list = list->next) {
6358                         wifi = list->data;
6359
6360                         if (wifi->tethering) {
6361                                 wifi->tethering = false;
6362
6363                                 connman_inet_remove_from_bridge(wifi->index,
6364                                                                         bridge);
6365                                 wifi->bridged = false;
6366                         }
6367                 }
6368
6369                 connman_technology_tethering_notify(technology, false);
6370
6371                 return 0;
6372         }
6373
6374         DBG("trying tethering for available devices");
6375         err = enable_wifi_tethering(technology, bridge, identifier, passphrase,
6376                                 true);
6377
6378         if (err < 0) {
6379                 DBG("trying tethering for any device");
6380                 err = enable_wifi_tethering(technology, bridge, identifier,
6381                                         passphrase, false);
6382         }
6383
6384         return err;
6385 }
6386
6387 static void regdom_callback(int result, const char *alpha2, void *user_data)
6388 {
6389         DBG("");
6390
6391         if (!wifi_technology)
6392                 return;
6393
6394         if (result != 0)
6395                 alpha2 = NULL;
6396
6397         connman_technology_regdom_notify(wifi_technology, alpha2);
6398 }
6399
6400 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
6401 {
6402         return g_supplicant_set_country(alpha2, regdom_callback, NULL);
6403 }
6404
6405 #if defined TIZEN_EXT && defined TIZEN_EXT_INS
6406 static void supp_ins_init(void)
6407 {
6408         const char *string;
6409         GSupplicantINSPreferredFreq preferred_freq;
6410
6411         string = connman_option_get_string("INSPreferredFreqBSSID");
6412         if (g_strcmp0(string, "5GHz") == 0)
6413                 preferred_freq = G_SUPPLICANT_INS_PREFERRED_FREQ_5GHZ;
6414         else if (g_strcmp0(string, "2.4GHz") == 0)
6415                 preferred_freq = G_SUPPLICANT_INS_PREFERRED_FREQ_24GHZ;
6416         else
6417                 preferred_freq = G_SUPPLICANT_INS_PREFERRED_FREQ_UNKNOWN;
6418
6419         g_supplicant_set_ins_settings(preferred_freq,
6420                 connman_setting_get_bool("INSLastConnectedBSSID"),
6421                 connman_setting_get_bool("INSAssocReject"),
6422                 connman_setting_get_bool("INSSignalBSSID"),
6423                 connman_setting_get_uint("INSPreferredFreqBSSIDScore"),
6424                 connman_setting_get_uint("INSLastConnectedBSSIDScore"),
6425                 connman_setting_get_uint("INSAssocRejectScore"),
6426                 connman_setting_get_int("INSSignalLevel3_5GHz"),
6427                 connman_setting_get_int("INSSignalLevel3_24GHz")
6428         );
6429 }
6430 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_INS */
6431
6432 static struct connman_technology_driver tech_driver = {
6433         .name           = "wifi",
6434         .type           = CONNMAN_SERVICE_TYPE_WIFI,
6435         .probe          = tech_probe,
6436         .remove         = tech_remove,
6437         .set_tethering  = tech_set_tethering,
6438         .set_regdom     = tech_set_regdom,
6439 };
6440
6441 static int wifi_init(void)
6442 {
6443         int err;
6444
6445         err = connman_network_driver_register(&network_driver);
6446         if (err < 0)
6447                 return err;
6448
6449         err = g_supplicant_register(&callbacks);
6450         if (err < 0) {
6451                 connman_network_driver_unregister(&network_driver);
6452                 return err;
6453         }
6454
6455         err = connman_technology_driver_register(&tech_driver);
6456         if (err < 0) {
6457                 g_supplicant_unregister(&callbacks);
6458                 connman_network_driver_unregister(&network_driver);
6459                 return err;
6460         }
6461
6462 #if defined TIZEN_EXT
6463         failed_bssids = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
6464 #endif
6465
6466 #if defined TIZEN_EXT && defined TIZEN_EXT_INS
6467         supp_ins_init();
6468 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_INS */
6469         return 0;
6470 }
6471
6472 static void wifi_exit(void)
6473 {
6474         DBG();
6475
6476         connman_technology_driver_unregister(&tech_driver);
6477
6478         g_supplicant_unregister(&callbacks);
6479
6480         connman_network_driver_unregister(&network_driver);
6481
6482 #if defined TIZEN_EXT
6483         g_hash_table_unref(failed_bssids);
6484 #endif
6485 }
6486
6487 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
6488                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)