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