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