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