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