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