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