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