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