Maintain connection status when wifi roaming
[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 (connman_network_get_bool(wifi->network, "WiFi.Roaming"))
4008                 connman_network_set_bool(wifi->network, "WiFi.Roaming", false);
4009
4010         if (network == wifi->pending_network)
4011                 wifi->pending_network = NULL;
4012
4013         /* wifi_data may be invalid because wifi is already disabled */
4014         return;
4015
4016 found:
4017 #else
4018         struct wifi_data *wifi = dd->wifi;
4019         g_free(dd);
4020 #endif
4021
4022         DBG("result %d supplicant interface %p wifi %p networks: current %p "
4023                 "pending %p disconnected %p", result, interface, wifi,
4024                 wifi->network, wifi->pending_network, network);
4025
4026         if (result == -ECONNABORTED) {
4027                 DBG("wifi interface no longer available");
4028                 return;
4029         }
4030
4031 #if defined TIZEN_EXT
4032         if (g_slist_find(wifi->networks, network) &&
4033                         wifi->network != wifi->pending_network)
4034 #else
4035         if (g_slist_find(wifi->networks, network))
4036 #endif
4037                 connman_network_set_connected(network, false);
4038
4039         wifi->disconnecting = false;
4040
4041         if (network != wifi->network) {
4042                 if (network == wifi->pending_network)
4043                         wifi->pending_network = NULL;
4044                 DBG("current wifi network has changed since disconnection");
4045                 return;
4046         }
4047
4048         wifi->network = NULL;
4049
4050         wifi->connected = false;
4051
4052         if (wifi->pending_network) {
4053                 network_connect(wifi->pending_network);
4054                 wifi->pending_network = NULL;
4055         }
4056
4057         start_autoscan(wifi->device);
4058 }
4059
4060 static int network_disconnect(struct connman_network *network)
4061 {
4062         struct connman_device *device = connman_network_get_device(network);
4063         struct disconnect_data *dd;
4064         struct wifi_data *wifi;
4065         int err;
4066 #if defined TIZEN_EXT
4067         struct connman_service *service;
4068 #endif
4069
4070         DBG("network %p", network);
4071
4072         wifi = connman_device_get_data(device);
4073         if (!wifi || !wifi->interface)
4074                 return -ENODEV;
4075
4076 #if defined TIZEN_EXT
4077         if (connman_network_get_associating(network) == true) {
4078                 connman_network_clear_associating(network);
4079                 connman_network_set_bool(network, "WiFi.UseWPS", false);
4080         } else {
4081                 service = connman_service_lookup_from_network(network);
4082
4083                 if (service != NULL &&
4084                         (__connman_service_is_connected_state(service,
4085                                         CONNMAN_IPCONFIG_TYPE_IPV4) == false &&
4086                         __connman_service_is_connected_state(service,
4087                                         CONNMAN_IPCONFIG_TYPE_IPV6) == false) &&
4088                         (connman_service_get_favorite(service) == false))
4089                                         __connman_service_set_passphrase(service, NULL);
4090         }
4091
4092         if (wifi->pending_network == network)
4093                 wifi->pending_network = NULL;
4094
4095         if (wifi->scan_pending_network == network)
4096                 wifi->scan_pending_network = NULL;
4097
4098 #endif
4099         connman_network_set_associating(network, false);
4100
4101         if (wifi->disconnecting)
4102                 return -EALREADY;
4103
4104         wifi->disconnecting = true;
4105
4106         dd = g_malloc0(sizeof(*dd));
4107         dd->wifi = wifi;
4108         dd->network = network;
4109
4110         err = g_supplicant_interface_disconnect(wifi->interface,
4111                                                 disconnect_callback, dd);
4112         if (err < 0) {
4113                 wifi->disconnecting = false;
4114                 g_free(dd);
4115         }
4116
4117         return err;
4118 }
4119
4120 #if defined TIZEN_EXT
4121 static void set_connection_mode(struct connman_network *network,
4122                 int linkspeed)
4123 {
4124         ieee80211_modes_e phy_mode;
4125         connection_mode_e conn_mode;
4126
4127         phy_mode = connman_network_get_phy_mode(network);
4128         switch (phy_mode) {
4129         case IEEE80211_MODE_B:
4130                 if (linkspeed > 0 && linkspeed <= 11)
4131                         conn_mode = CONNECTION_MODE_IEEE80211B;
4132                 else
4133                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
4134
4135                 break;
4136         case IEEE80211_MODE_BG:
4137                 if (linkspeed > 0 && linkspeed <= 11)
4138                         conn_mode = CONNECTION_MODE_IEEE80211B;
4139                 else if (linkspeed > 11 && linkspeed <= 54)
4140                         conn_mode = CONNECTION_MODE_IEEE80211G;
4141                 else
4142                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
4143
4144                 break;
4145         case IEEE80211_MODE_BGN:
4146                 if (linkspeed > 0 && linkspeed <= 11)
4147                         conn_mode = CONNECTION_MODE_IEEE80211B;
4148                 else if (linkspeed > 11 && linkspeed <= 54)
4149                         conn_mode = CONNECTION_MODE_IEEE80211G;
4150                 else if (linkspeed > 54 && linkspeed <= 450)
4151                         conn_mode = CONNECTION_MODE_IEEE80211N;
4152                 else
4153                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
4154
4155                 break;
4156         case IEEE80211_MODE_A:
4157                 if (linkspeed > 0 && linkspeed <= 54)
4158                         conn_mode = CONNECTION_MODE_IEEE80211A;
4159                 else
4160                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
4161
4162                 break;
4163         case IEEE80211_MODE_AN:
4164                 if (linkspeed > 0 && linkspeed <= 54)
4165                         conn_mode = CONNECTION_MODE_IEEE80211A;
4166                 else if (linkspeed > 54 && linkspeed <= 450)
4167                         conn_mode = CONNECTION_MODE_IEEE80211N;
4168                 else
4169                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
4170
4171                 break;
4172         case IEEE80211_MODE_ANAC:
4173                 if (linkspeed > 0 && linkspeed <= 54)
4174                         conn_mode = CONNECTION_MODE_IEEE80211A;
4175                 else if (linkspeed > 54 && linkspeed <= 450)
4176                         conn_mode = CONNECTION_MODE_IEEE80211N;
4177                 else if (linkspeed > 450 && linkspeed <= 1300)
4178                         conn_mode = CONNECTION_MODE_IEEE80211AC;
4179                 else
4180                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
4181
4182                 break;
4183         default:
4184                         conn_mode = CONNECTION_MODE_IEEE80211_UNKNOWN;
4185                 break;
4186         }
4187
4188         DBG("connection mode(%d)", conn_mode);
4189         connman_network_set_connection_mode(network, conn_mode);
4190 }
4191
4192 static void signalpoll_callback(int result, int maxspeed, int strength,
4193                                 int snr, void *user_data)
4194 {
4195         char bssid_buff[WIFI_BSSID_STR_LEN] = {0,};
4196         char *bssid_str = bssid_buff;
4197         unsigned char *bssid;
4198         struct timespec curr_time = {0};
4199         __time_t roam_scan_time;
4200         const char *interface = NULL;
4201         struct connman_device *device;
4202         struct connman_network *network = user_data;
4203         GSupplicantNetwork *supplicant_network;
4204         struct wifi_data *wifi;
4205         uint16_t freq = connman_network_get_frequency(network);
4206         const char *group = connman_network_get_group(network);
4207
4208         if (result != 0) {
4209                 DBG("Failed to get maxspeed from signalpoll !");
4210                 connman_network_unref(network);
4211                 return;
4212         }
4213
4214         device = connman_network_get_device(network);
4215         if (device)
4216                 wifi = connman_device_get_data(device);
4217
4218         if (group) {
4219                 supplicant_network = g_supplicant_interface_get_network(wifi->interface, group);
4220                 if (supplicant_network) {
4221                         g_supplicant_network_set_signal(supplicant_network, strength);
4222                         g_supplicant_network_set_bss_signal(supplicant_network, strength, snr);
4223                 }
4224         }
4225
4226         strength += 120;
4227         if (strength > 100)
4228                 strength = 100;
4229
4230         bssid = connman_network_get_bssid(network);
4231         snprintf(bssid_str, WIFI_BSSID_STR_LEN, MACSTR, MAC2STR(bssid));
4232
4233         DBG("network %p, bssid %s, freq %u, maxspeed %d, strength %d, snr %d",
4234                         network, bssid_str, freq, maxspeed, strength, snr);
4235
4236         connman_network_set_strength(network, (uint8_t)strength);
4237         connman_network_set_snr(network, snr);
4238         connman_network_set_maxspeed(network, maxspeed);
4239         set_connection_mode(network, maxspeed);
4240
4241         if (connman_network_get_max_bssid_count(network) <= 1)
4242                 goto done;
4243
4244         clock_gettime(CLOCK_MONOTONIC, &curr_time);
4245         roam_scan_time = connman_network_get_roam_scan_time(network);
4246         if (curr_time.tv_sec <= roam_scan_time + ROAM_SCAN_INTERVAL)
4247                 goto done;
4248
4249         if (device && need_bss_transition(freq, snr, strength)) {
4250
4251                 interface = connman_device_get_string(device, "Interface");
4252                 __connman_technology_notify_roaming_state(interface, "required", bssid_str, NULL);
4253
4254                 if (connman_setting_get_bool("WifiRoamingScan") == false)
4255                         goto done;
4256
4257                 throw_wifi_scan(device, scan_callback);
4258                 connman_network_set_roam_scan_time(network, curr_time.tv_sec);
4259         }
4260
4261 done:
4262         connman_network_unref(network);
4263 }
4264
4265 static int network_signalpoll(struct wifi_data *wifi)
4266 {
4267         GSupplicantInterface *interface;
4268         struct connman_network *network;
4269
4270         if (!wifi || !wifi->network)
4271                 return -ENODEV;
4272
4273         wifi->network = connman_network_ref(wifi->network);
4274
4275         interface = wifi->interface;
4276         network = wifi->network;
4277
4278         DBG("network %p", network);
4279
4280         return g_supplicant_interface_signalpoll(interface, signalpoll_callback, network);
4281 }
4282
4283 static gboolean autosignalpoll_timeout(gpointer data)
4284 {
4285         struct wifi_data *wifi = data;
4286
4287         if (!wifi || !wifi->automaxspeed_timeout) {
4288                 DBG("automaxspeed_timeout is found to be zero. i.e. currently in disconnected state. !!");
4289                 return FALSE;
4290         }
4291
4292         int ret = network_signalpoll(wifi);
4293         if (ret < 0) {
4294                 DBG("Fail to get max speed !!");
4295                 wifi->automaxspeed_timeout = 0;
4296
4297                 if (wifi->network)
4298                         connman_network_unref(wifi->network);
4299
4300                 return FALSE;
4301         }
4302
4303         return TRUE;
4304 }
4305 #endif
4306
4307 static struct connman_network_driver network_driver = {
4308         .name           = "wifi",
4309         .type           = CONNMAN_NETWORK_TYPE_WIFI,
4310         .priority       = CONNMAN_NETWORK_PRIORITY_LOW,
4311         .probe          = network_probe,
4312         .remove         = network_remove,
4313         .connect        = network_connect,
4314         .disconnect     = network_disconnect,
4315 };
4316
4317 static void interface_added(GSupplicantInterface *interface)
4318 {
4319         const char *ifname = g_supplicant_interface_get_ifname(interface);
4320         const char *driver = g_supplicant_interface_get_driver(interface);
4321 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4322         /*
4323          * Note: If supplicant interface's driver is wired then skip it,
4324          * because it meanti only for ethernet not Wi-Fi.
4325          */
4326         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4327                 return;
4328 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4329
4330 #if defined TIZEN_EXT
4331         bool is_5_0_ghz_supported = g_supplicant_interface_get_is_5_0_ghz_supported(interface);
4332 #endif
4333
4334         struct wifi_data *wifi;
4335
4336         wifi = g_supplicant_interface_get_data(interface);
4337         if (!wifi) {
4338                 wifi = get_pending_wifi_data(ifname);
4339                 if (!wifi)
4340                         return;
4341
4342                 wifi->interface = interface;
4343                 g_supplicant_interface_set_data(interface, wifi);
4344                 p2p_iface_list = g_list_append(p2p_iface_list, wifi);
4345                 wifi->p2p_device = true;
4346         }
4347
4348         DBG("ifname %s driver %s wifi %p tethering %d",
4349                         ifname, driver, wifi, wifi->tethering);
4350
4351         if (!wifi->device) {
4352                 connman_error("WiFi device not set");
4353                 return;
4354         }
4355
4356         connman_device_set_powered(wifi->device, true);
4357 #if defined TIZEN_EXT
4358         connman_device_set_wifi_5ghz_supported(wifi->device, is_5_0_ghz_supported);
4359         /* Max number of SSIDs supported by wlan chipset that can be scanned */
4360         int max_scan_ssids = g_supplicant_interface_get_max_scan_ssids(interface);
4361         connman_device_set_max_scan_ssids(wifi->device, max_scan_ssids);
4362 #endif
4363 }
4364
4365 static bool is_idle(struct wifi_data *wifi)
4366 {
4367         DBG("state %d", wifi->state);
4368
4369         switch (wifi->state) {
4370         case G_SUPPLICANT_STATE_UNKNOWN:
4371         case G_SUPPLICANT_STATE_DISABLED:
4372         case G_SUPPLICANT_STATE_DISCONNECTED:
4373         case G_SUPPLICANT_STATE_INACTIVE:
4374         case G_SUPPLICANT_STATE_SCANNING:
4375                 return true;
4376
4377         case G_SUPPLICANT_STATE_AUTHENTICATING:
4378         case G_SUPPLICANT_STATE_ASSOCIATING:
4379         case G_SUPPLICANT_STATE_ASSOCIATED:
4380         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4381         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4382         case G_SUPPLICANT_STATE_COMPLETED:
4383                 return false;
4384         }
4385
4386         return false;
4387 }
4388
4389 static bool is_idle_wps(GSupplicantInterface *interface,
4390                                                 struct wifi_data *wifi)
4391 {
4392         /* First, let's check if WPS processing did not went wrong */
4393         if (g_supplicant_interface_get_wps_state(interface) ==
4394                 G_SUPPLICANT_WPS_STATE_FAIL)
4395                 return false;
4396
4397         /* Unlike normal connection, being associated while processing wps
4398          * actually means that we are idling. */
4399         switch (wifi->state) {
4400         case G_SUPPLICANT_STATE_UNKNOWN:
4401         case G_SUPPLICANT_STATE_DISABLED:
4402         case G_SUPPLICANT_STATE_DISCONNECTED:
4403         case G_SUPPLICANT_STATE_INACTIVE:
4404         case G_SUPPLICANT_STATE_SCANNING:
4405         case G_SUPPLICANT_STATE_ASSOCIATED:
4406                 return true;
4407         case G_SUPPLICANT_STATE_AUTHENTICATING:
4408         case G_SUPPLICANT_STATE_ASSOCIATING:
4409         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4410         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4411         case G_SUPPLICANT_STATE_COMPLETED:
4412                 return false;
4413         }
4414
4415         return false;
4416 }
4417
4418 static bool handle_wps_completion(GSupplicantInterface *interface,
4419                                         struct connman_network *network,
4420                                         struct connman_device *device,
4421                                         struct wifi_data *wifi)
4422 {
4423         bool wps;
4424
4425         wps = connman_network_get_bool(network, "WiFi.UseWPS");
4426         if (wps) {
4427                 const unsigned char *ssid, *wps_ssid;
4428                 unsigned int ssid_len, wps_ssid_len;
4429                 struct disconnect_data *dd;
4430                 const char *wps_key;
4431
4432                 /* Checking if we got associated with requested
4433                  * network */
4434                 ssid = connman_network_get_blob(network, "WiFi.SSID",
4435                                                 &ssid_len);
4436
4437                 wps_ssid = g_supplicant_interface_get_wps_ssid(
4438                         interface, &wps_ssid_len);
4439
4440                 if (!wps_ssid || wps_ssid_len != ssid_len ||
4441                                 memcmp(ssid, wps_ssid, ssid_len) != 0) {
4442                         dd = g_malloc0(sizeof(*dd));
4443                         dd->wifi = wifi;
4444                         dd->network = network;
4445
4446                         connman_network_set_associating(network, false);
4447                         g_supplicant_interface_disconnect(wifi->interface,
4448                                                 disconnect_callback, dd);
4449 #if defined TIZEN_EXT
4450                         connman_network_set_bool(network, "WiFi.UseWPS", false);
4451                         connman_network_set_string(network, "WiFi.PinWPS", NULL);
4452 #endif
4453                         return false;
4454                 }
4455
4456                 wps_key = g_supplicant_interface_get_wps_key(interface);
4457 #if defined TIZEN_EXT
4458                 /* Check the passphrase and encrypt it
4459                  */
4460                  int ret;
4461                  gchar *passphrase = g_strdup(wps_key);
4462
4463                  connman_network_set_string(network, "WiFi.PinWPS", NULL);
4464
4465                  if (check_passphrase_ext(network, passphrase) < 0) {
4466                          DBG("[WPS] Invalid passphrase");
4467                          g_free(passphrase);
4468                          return true;
4469                  }
4470
4471                  ret = send_encryption_request(passphrase, network);
4472
4473                  g_free(passphrase);
4474
4475                  if (!ret)
4476                          DBG("[WPS] Encryption request succeeded");
4477                  else
4478                          DBG("[WPS] Encryption request failed %d", ret);
4479
4480 #else
4481                 connman_network_set_string(network, "WiFi.Passphrase",
4482                                         wps_key);
4483
4484                 connman_network_set_string(network, "WiFi.PinWPS", NULL);
4485 #endif
4486         }
4487
4488         return true;
4489 }
4490
4491 static bool handle_assoc_status_code(GSupplicantInterface *interface,
4492                                      struct wifi_data *wifi)
4493 {
4494 #if defined TIZEN_EXT
4495         if ((wifi->state == G_SUPPLICANT_STATE_ASSOCIATING ||
4496                         wifi->state == G_SUPPLICANT_STATE_AUTHENTICATING ||
4497                         wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) &&
4498 #else
4499         if (wifi->state == G_SUPPLICANT_STATE_ASSOCIATING &&
4500                         wifi->assoc_code == ASSOC_STATUS_NO_CLIENT &&
4501 #endif
4502                         wifi->load_shaping_retries < LOAD_SHAPING_MAX_RETRIES) {
4503                 wifi->load_shaping_retries ++;
4504                 return TRUE;
4505         }
4506         wifi->load_shaping_retries = 0;
4507         return FALSE;
4508 }
4509
4510 static bool handle_4way_handshake_failure(GSupplicantInterface *interface,
4511                                         struct connman_network *network,
4512                                         struct wifi_data *wifi)
4513 {
4514         struct connman_service *service;
4515
4516 #if defined TIZEN_EXT
4517         const char *security;
4518         if (wifi->connected)
4519                 return false;
4520
4521         security = connman_network_get_string(network, "WiFi.Security");
4522
4523         if (security && g_str_equal(security, "ieee8021x") == true &&
4524                         wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) {
4525                 wifi->retries = 0;
4526                 connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
4527
4528                 return false;
4529         }
4530
4531         if (wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE)
4532                 return false;
4533 #else
4534         if ((wifi->state != G_SUPPLICANT_STATE_4WAY_HANDSHAKE) &&
4535                         !((wifi->state == G_SUPPLICANT_STATE_ASSOCIATING) &&
4536                                 (wifi->assoc_code == ASSOC_STATUS_AUTH_TIMEOUT)))
4537                 return false;
4538
4539         if (wifi->connected)
4540                 return false;
4541 #endif
4542
4543         service = connman_service_lookup_from_network(network);
4544         if (!service)
4545                 return false;
4546
4547         wifi->retries++;
4548
4549         if (connman_service_get_favorite(service)) {
4550                 if (wifi->retries < FAVORITE_MAXIMUM_RETRIES)
4551                         return true;
4552         }
4553
4554         wifi->retries = 0;
4555         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
4556
4557         return false;
4558 }
4559
4560 #if defined TIZEN_EXT
4561 static bool handle_wifi_assoc_retry(struct connman_network *network,
4562                                         struct wifi_data *wifi)
4563 {
4564         const char *security;
4565
4566         if (!wifi->network || wifi->connected || wifi->disconnecting ||
4567                         connman_network_get_connecting(network) != true) {
4568                 wifi->assoc_retry_count = 0;
4569                 return false;
4570         }
4571
4572         if (wifi->state != G_SUPPLICANT_STATE_AUTHENTICATING &&
4573                         wifi->state != G_SUPPLICANT_STATE_ASSOCIATING &&
4574                         wifi->state != G_SUPPLICANT_STATE_ASSOCIATED) {
4575                 wifi->assoc_retry_count = 0;
4576                 return false;
4577         }
4578
4579         security = connman_network_get_string(network, "WiFi.Security");
4580         if (security && g_str_equal(security, "ieee8021x") == true &&
4581                         wifi->state == G_SUPPLICANT_STATE_ASSOCIATED) {
4582                 wifi->assoc_retry_count = 0;
4583                 return false;
4584         }
4585
4586         if (++wifi->assoc_retry_count >= TIZEN_ASSOC_RETRY_COUNT) {
4587                 wifi->assoc_retry_count = 0;
4588
4589                 /* Honestly it's not an invalid-key error,
4590                  * however QA team recommends that the invalid-key error
4591                  * might be better to display for user experience.
4592                  */
4593                 switch (wifi->state) {
4594                 case G_SUPPLICANT_STATE_AUTHENTICATING:
4595                         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_AUTHENTICATE_FAIL);
4596                         break;
4597                 case G_SUPPLICANT_STATE_ASSOCIATED:
4598                         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_INVALID_KEY);
4599                         break;
4600                 default:
4601                         connman_network_set_error(network, CONNMAN_NETWORK_ERROR_ASSOCIATE_FAIL);
4602                         break;
4603                 }
4604
4605                 return false;
4606         }
4607
4608         return true;
4609 }
4610
4611 static void handle_wifi_roaming_complete(struct connman_network *network)
4612 {
4613         const char *cur_bssid;
4614         const char *dst_bssid;
4615         const char *ifname;
4616         struct connman_device *device;
4617         struct connman_service *service;
4618         struct connman_ipconfig *ipconfig_ipv4;
4619         enum connman_ipconfig_type type;
4620         enum connman_ipconfig_method method;
4621
4622         if (!connman_setting_get_bool("WifiRoaming") ||
4623                         !connman_network_get_bool(network, "WiFi.Roaming"))
4624                 return;
4625
4626         device = connman_network_get_device(network);
4627         if (device) {
4628                 ifname = connman_device_get_string(device, "Interface");
4629                 cur_bssid = connman_network_get_string(network,
4630                                 "WiFi.RoamingCurBSSID");
4631                 dst_bssid = connman_network_get_string(network,
4632                                 "WiFi.RoamingDstBSSID");
4633         }
4634
4635         if (device && ifname && cur_bssid && dst_bssid) {
4636                 __connman_technology_notify_roaming_state(ifname,
4637                                 "success", cur_bssid, dst_bssid);
4638                 connman_network_set_bool(network,
4639                                 "WiFi.Roaming", false);
4640                 connman_network_set_string(network,
4641                                 "WiFi.RoamingCurBSSID", NULL);
4642                 connman_network_set_string(network,
4643                                 "WiFi.RoamingDstBSSID", NULL);
4644
4645                 service = connman_service_lookup_from_network(network);
4646                 if (!service)
4647                         return;
4648
4649                 ipconfig_ipv4 = __connman_service_get_ip4config(service);
4650                 if (!ipconfig_ipv4) {
4651                         connman_error("Service has no IPv4 configuration");
4652                         return;
4653                 }
4654
4655                 type = __connman_ipconfig_get_config_type(ipconfig_ipv4);
4656                 if (type != CONNMAN_IPCONFIG_TYPE_IPV4)
4657                         return;
4658
4659                 method = __connman_ipconfig_get_method(ipconfig_ipv4);
4660                 if (method != CONNMAN_IPCONFIG_METHOD_DHCP)
4661                         return;
4662
4663                 connman_network_set_bool(network, "WiFi.RoamingDHCP", true);
4664
4665                 if (set_connected_dhcp(network) != 0)
4666                         connman_network_set_bool(network, "WiFi.RoamingDHCP", false);
4667         }
4668 }
4669 #endif
4670
4671 static void interface_state(GSupplicantInterface *interface)
4672 {
4673         struct connman_network *network;
4674         struct connman_device *device;
4675         struct wifi_data *wifi;
4676         GSupplicantState state = g_supplicant_interface_get_state(interface);
4677 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4678         /*
4679          * Note: If supplicant interface's driver is wired then skip it,
4680          * because it meanti only for ethernet not Wi-Fi.
4681          */
4682         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4683                 return;
4684 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4685
4686         bool wps;
4687         bool old_connected;
4688
4689         wifi = g_supplicant_interface_get_data(interface);
4690
4691         DBG("wifi %p interface state %d", wifi, state);
4692
4693         if (!wifi)
4694                 return;
4695
4696         device = wifi->device;
4697         if (!device)
4698                 return;
4699
4700         if (state == G_SUPPLICANT_STATE_COMPLETED) {
4701                 if (wifi->tethering_param) {
4702                         g_free(wifi->tethering_param->ssid);
4703                         g_free(wifi->tethering_param);
4704                         wifi->tethering_param = NULL;
4705                 }
4706
4707                 if (wifi->tethering)
4708                         stop_autoscan(device);
4709         }
4710
4711         if (g_supplicant_interface_get_ready(interface) &&
4712                                         !wifi->interface_ready) {
4713                 wifi->interface_ready = true;
4714                 finalize_interface_creation(wifi);
4715         }
4716
4717         network = wifi->network;
4718         if (!network)
4719                 return;
4720
4721         switch (state) {
4722         case G_SUPPLICANT_STATE_SCANNING:
4723 #if defined TIZEN_EXT
4724                 if (wifi->automaxspeed_timeout != 0) {
4725                         g_source_remove(wifi->automaxspeed_timeout);
4726                         wifi->automaxspeed_timeout = 0;
4727                         DBG("Remove signalpoll timer!!");
4728                 }
4729
4730                 if (!connman_network_get_bool(wifi->network, "WiFi.Roaming"))
4731 #endif
4732                 if (wifi->connected)
4733                         connman_network_set_connected(network, false);
4734
4735                 break;
4736
4737         case G_SUPPLICANT_STATE_AUTHENTICATING:
4738         case G_SUPPLICANT_STATE_ASSOCIATING:
4739 #if defined TIZEN_EXT
4740                 reset_autoscan(device);
4741 #else
4742                 stop_autoscan(device);
4743 #endif
4744 #if defined TIZEN_EXT
4745                 if (!connman_network_get_bool(wifi->network, "WiFi.Roaming"))
4746 #endif
4747                 if (!wifi->connected)
4748                         connman_network_set_associating(network, true);
4749
4750                 break;
4751
4752         case G_SUPPLICANT_STATE_COMPLETED:
4753 #if defined TIZEN_EXT
4754                 /* though it should be already reset: */
4755                 reset_autoscan(device);
4756
4757                 wifi->assoc_retry_count = 0;
4758
4759                 wifi->scan_pending_network = NULL;
4760
4761                 /* should be cleared scanning flag */
4762                 bool scanning = connman_device_get_scanning(device,
4763                                                CONNMAN_SERVICE_TYPE_WIFI);
4764                 if (scanning){
4765                         connman_device_set_scanning(device,
4766                                 CONNMAN_SERVICE_TYPE_WIFI, false);
4767                         connman_device_unref(device);
4768                 }
4769
4770                 if (!wifi->automaxspeed_timeout) {
4771                         DBG("Going to start signalpoll timer!!");
4772                         int ret = network_signalpoll(wifi);
4773                         if (ret < 0)
4774                                 DBG("Fail to get max speed !!");
4775                         else
4776                                 wifi->automaxspeed_timeout = g_timeout_add_seconds(10, autosignalpoll_timeout, wifi);
4777                 }
4778
4779                 g_hash_table_remove_all(failed_bssids);
4780                 handle_wifi_roaming_complete(network);
4781 #else
4782                 /* though it should be already stopped: */
4783                 stop_autoscan(device);
4784 #endif
4785
4786                 if (!handle_wps_completion(interface, network, device, wifi))
4787                         break;
4788
4789                 connman_network_set_connected(network, true);
4790
4791                 wifi->disconnect_code = 0;
4792                 wifi->assoc_code = 0;
4793                 wifi->load_shaping_retries = 0;
4794                 break;
4795
4796         case G_SUPPLICANT_STATE_DISCONNECTED:
4797 #if defined TIZEN_EXT
4798                 connman_network_set_strength(network, 0);
4799                 connman_network_set_maxspeed(network, 0);
4800
4801                 if (wifi->automaxspeed_timeout != 0) {
4802                         g_source_remove(wifi->automaxspeed_timeout);
4803                         wifi->automaxspeed_timeout = 0;
4804                         DBG("Remove signalpoll timer!!");
4805                 }
4806 #endif
4807                 /*
4808                  * If we're in one of the idle modes, we have
4809                  * not started association yet and thus setting
4810                  * those ones to FALSE could cancel an association
4811                  * in progress.
4812                  */
4813                 wps = connman_network_get_bool(network, "WiFi.UseWPS");
4814                 if (wps)
4815                         if (is_idle_wps(interface, wifi))
4816                                 break;
4817
4818                 if (is_idle(wifi))
4819                         break;
4820
4821 #if defined TIZEN_EXT
4822                 if (!wifi->connected && handle_assoc_status_code(interface, wifi)) {
4823                         const char *group = connman_network_get_group(network);
4824                         GSupplicantNetwork *supplicant_network;
4825                         GSList *bssid_list = NULL;
4826                         guint bssid_length = 0;
4827
4828                         if (group) {
4829                                 supplicant_network = g_supplicant_interface_get_network(interface, group);
4830
4831                                 connman_network_set_assoc_reject_table(network,
4832                                                 g_supplicant_network_clone_assoc_reject_table(supplicant_network));
4833
4834                                 g_supplicant_network_update_assoc_reject(interface, supplicant_network);
4835                         }
4836
4837                         bssid_list = (GSList *)connman_network_get_bssid_list(network);
4838                         if (bssid_list)
4839                                 bssid_length = g_slist_length(bssid_list);
4840
4841                         if (bssid_length > 1 && bssid_length > g_hash_table_size(failed_bssids)) {
4842                                 network_connect(network);
4843                                 break;
4844                         }
4845
4846                         wifi->load_shaping_retries = 0;
4847                 }
4848
4849                 g_hash_table_remove_all(failed_bssids);
4850 #else
4851                 if (handle_assoc_status_code(interface, wifi))
4852                         break;
4853 #endif
4854
4855                 /* If previous state was 4way-handshake, then
4856                  * it's either: psk was incorrect and thus we retry
4857                  * or if we reach the maximum retries we declare the
4858                  * psk as wrong */
4859                 if (handle_4way_handshake_failure(interface,
4860                                                 network, wifi))
4861                         break;
4862
4863                 /* See table 8-36 Reason codes in IEEE Std 802.11 */
4864                 switch (wifi->disconnect_code) {
4865 #if defined TIZEN_EXT
4866                 case 1: /* Unspecified reason */
4867                         /* Let's assume it's because we got blocked */
4868 #endif
4869                 case 6: /* Class 2 frame received from nonauthenticated STA */
4870                         connman_network_set_error(network,
4871                                                 CONNMAN_NETWORK_ERROR_BLOCKED);
4872                         break;
4873
4874                 default:
4875                         break;
4876                 }
4877
4878 #if defined TIZEN_EXT
4879                 /* Some of Wi-Fi networks are not comply Wi-Fi specification.
4880                  * Retry association until its retry count is expired */
4881                 if (handle_wifi_assoc_retry(network, wifi) == true) {
4882                         throw_wifi_scan(wifi->device, scan_callback);
4883                         wifi->scan_pending_network = wifi->network;
4884                         break;
4885                 }
4886
4887                 if (wifi->disconnect_code > 0){
4888                         DBG("Set disconnect reason code(%d)", wifi->disconnect_code);
4889                         connman_network_set_disconnect_reason(network, wifi->disconnect_code);
4890                 }
4891 #endif
4892
4893                 if (network != wifi->pending_network) {
4894                         connman_network_set_connected(network, false);
4895                         connman_network_set_associating(network, false);
4896                 }
4897                 wifi->disconnecting = false;
4898
4899                 start_autoscan(device);
4900
4901                 break;
4902
4903         case G_SUPPLICANT_STATE_INACTIVE:
4904 #if defined TIZEN_EXT
4905                 if (handle_wps_completion(interface, network, device, wifi) == false)
4906                         break;
4907 #endif
4908                 connman_network_set_associating(network, false);
4909                 start_autoscan(device);
4910
4911                 break;
4912
4913         case G_SUPPLICANT_STATE_UNKNOWN:
4914         case G_SUPPLICANT_STATE_DISABLED:
4915         case G_SUPPLICANT_STATE_ASSOCIATED:
4916         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4917         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4918                 break;
4919         }
4920
4921         old_connected = wifi->connected;
4922         wifi->state = state;
4923
4924         /* Saving wpa_s state policy:
4925          * If connected and if the state changes are roaming related:
4926          * --> We stay connected
4927          * If completed
4928          * --> We are connected
4929          * All other case:
4930          * --> We are not connected
4931          * */
4932         switch (state) {
4933         case G_SUPPLICANT_STATE_AUTHENTICATING:
4934         case G_SUPPLICANT_STATE_ASSOCIATING:
4935         case G_SUPPLICANT_STATE_ASSOCIATED:
4936         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4937         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4938                 if (wifi->connected)
4939                         connman_warn("Probably roaming right now!"
4940                                                 " Staying connected...");
4941                 break;
4942         case G_SUPPLICANT_STATE_SCANNING:
4943                 wifi->connected = false;
4944
4945                 if (old_connected)
4946                         start_autoscan(device);
4947                 break;
4948         case G_SUPPLICANT_STATE_COMPLETED:
4949                 wifi->connected = true;
4950                 break;
4951         default:
4952                 wifi->connected = false;
4953                 break;
4954         }
4955
4956         DBG("DONE");
4957 }
4958
4959 static void interface_removed(GSupplicantInterface *interface)
4960 {
4961         const char *ifname = g_supplicant_interface_get_ifname(interface);
4962         struct wifi_data *wifi;
4963 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
4964         /*
4965          * Note: If supplicant interface's driver is wired then skip it,
4966          * because it meanti only for ethernet not Wi-Fi.
4967          */
4968         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
4969                 return;
4970 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
4971
4972
4973         DBG("ifname %s", ifname);
4974
4975         wifi = g_supplicant_interface_get_data(interface);
4976
4977 #if defined TIZEN_EXT_WIFI_MESH
4978         if (wifi && wifi->mesh_interface) {
4979                 DBG("Notify mesh interface remove");
4980                 connman_mesh_notify_interface_remove(true);
4981                 struct wifi_mesh_info *mesh_info = wifi->mesh_info;
4982                 g_free(mesh_info->parent_ifname);
4983                 g_free(mesh_info->ifname);
4984                 g_free(mesh_info->identifier);
4985                 g_free(mesh_info);
4986                 wifi->mesh_interface = false;
4987                 wifi->mesh_info = NULL;
4988                 return;
4989         }
4990 #endif
4991
4992         if (wifi)
4993                 wifi->interface = NULL;
4994
4995         if (wifi && wifi->tethering)
4996                 return;
4997
4998         if (!wifi || !wifi->device) {
4999                 DBG("wifi interface already removed");
5000                 return;
5001         }
5002
5003         connman_device_set_powered(wifi->device, false);
5004
5005         check_p2p_technology();
5006 #if defined TIZEN_EXT_WIFI_MESH
5007         check_mesh_technology();
5008 #endif
5009 }
5010
5011 static void set_device_type(const char *type, char dev_type[17])
5012 {
5013         const char *oui = "0050F204";
5014         const char *category = "0001";
5015         const char *sub_category = "0000";
5016
5017         if (!g_strcmp0(type, "handset")) {
5018                 category = "000A";
5019                 sub_category = "0005";
5020         } else if (!g_strcmp0(type, "vm") || !g_strcmp0(type, "container"))
5021                 sub_category = "0001";
5022         else if (!g_strcmp0(type, "server"))
5023                 sub_category = "0002";
5024         else if (!g_strcmp0(type, "laptop"))
5025                 sub_category = "0005";
5026         else if (!g_strcmp0(type, "desktop"))
5027                 sub_category = "0006";
5028         else if (!g_strcmp0(type, "tablet"))
5029                 sub_category = "0009";
5030         else if (!g_strcmp0(type, "watch"))
5031                 category = "00FF";
5032
5033         snprintf(dev_type, 17, "%s%s%s", category, oui, sub_category);
5034 }
5035
5036 static void p2p_support(GSupplicantInterface *interface)
5037 {
5038         char dev_type[17] = {};
5039         const char *hostname;
5040 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5041         /*
5042          * Note: If supplicant interface's driver is wired then skip it,
5043          * because it meanti only for ethernet not Wi-Fi.
5044          */
5045         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5046                 return;
5047 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5048
5049
5050         DBG("");
5051
5052         if (!interface)
5053                 return;
5054
5055         if (!g_supplicant_interface_has_p2p(interface))
5056                 return;
5057
5058         if (connman_technology_driver_register(&p2p_tech_driver) < 0) {
5059                 DBG("Could not register P2P technology driver");
5060                 return;
5061         }
5062
5063         hostname = connman_utsname_get_hostname();
5064         if (!hostname)
5065                 hostname = "ConnMan";
5066
5067         set_device_type(connman_machine_get_type(), dev_type);
5068         g_supplicant_interface_set_p2p_device_config(interface,
5069                                                         hostname, dev_type);
5070         connman_peer_driver_register(&peer_driver);
5071 }
5072
5073 static void scan_started(GSupplicantInterface *interface)
5074 {
5075 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5076         /*
5077          * Note: If supplicant interface's driver is wired then skip it,
5078          * because it meanti only for ethernet not Wi-Fi.
5079          */
5080         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5081                 return;
5082 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5083
5084         DBG("");
5085 }
5086
5087 static void scan_finished(GSupplicantInterface *interface)
5088 {
5089 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5090         /*
5091          * Note: If supplicant interface's driver is wired then skip it,
5092          * because it meanti only for ethernet not Wi-Fi.
5093          */
5094         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5095                 return;
5096 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5097
5098 #if defined TIZEN_EXT
5099         struct wifi_data *wifi;
5100         bool is_associating = false;
5101         static bool is_scanning = true;
5102 #endif
5103
5104         DBG("");
5105
5106 #if defined TIZEN_EXT
5107         wifi = g_supplicant_interface_get_data(interface);
5108         if (wifi && wifi->scan_pending_network) {
5109                 network_connect(wifi->scan_pending_network);
5110                 wifi->scan_pending_network = NULL;
5111         }
5112
5113         //service state - associating
5114         if(!wifi || !wifi->network)
5115                 return;
5116
5117         is_associating = connman_network_get_associating(wifi->network);
5118         if(is_associating && is_scanning){
5119                 is_scanning = false;
5120                 DBG("send scan for connecting");
5121                 throw_wifi_scan(wifi->device, scan_callback);
5122
5123                 return;
5124         }
5125         is_scanning = true;
5126
5127         //go scan
5128
5129 #endif
5130 }
5131
5132 static void ap_create_fail(GSupplicantInterface *interface)
5133 {
5134 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5135         /*
5136          * Note: If supplicant interface's driver is wired then skip it,
5137          * because it meanti only for ethernet not Wi-Fi.
5138          */
5139         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5140                 return;
5141 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5142
5143         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
5144         int ret;
5145
5146         if ((wifi->tethering) && (wifi->tethering_param)) {
5147                 DBG("%s create AP fail \n",
5148                                 g_supplicant_interface_get_ifname(wifi->interface));
5149
5150                 connman_inet_remove_from_bridge(wifi->index, wifi->bridge);
5151                 wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
5152                 wifi->tethering = false;
5153
5154                 ret = tech_set_tethering(wifi->tethering_param->technology,
5155                                 wifi->tethering_param->ssid->ssid,
5156                                 wifi->tethering_param->ssid->passphrase,
5157                                 wifi->bridge, true);
5158
5159                 if ((ret == -EOPNOTSUPP) && (wifi_technology)) {
5160                         connman_technology_tethering_notify(wifi_technology,false);
5161                 }
5162
5163                 g_free(wifi->tethering_param->ssid);
5164                 g_free(wifi->tethering_param);
5165                 wifi->tethering_param = NULL;
5166         }
5167 }
5168
5169 static unsigned char calculate_strength(GSupplicantNetwork *supplicant_network)
5170 {
5171         unsigned char strength;
5172
5173         strength = 120 + g_supplicant_network_get_signal(supplicant_network);
5174 #if !defined TIZEN_EXT
5175         if (strength > 100)
5176                 strength = 100;
5177 #endif
5178
5179         return strength;
5180 }
5181
5182 #if defined TIZEN_EXT_WIFI_MESH
5183 static void mesh_peer_added(GSupplicantNetwork *supplicant_network)
5184 {
5185         GSupplicantInterface *interface;
5186         struct wifi_data *wifi;
5187         const char *name, *security;
5188         struct connman_mesh *connman_mesh;
5189         struct wifi_mesh_info *mesh_info;
5190         const unsigned char *bssid;
5191         const char *identifier;
5192         char *address;
5193         uint16_t frequency;
5194         int ret;
5195
5196         interface = g_supplicant_network_get_interface(supplicant_network);
5197         wifi = g_supplicant_interface_get_data(interface);
5198         if (!wifi || !wifi->mesh_interface) {
5199                 DBG("Virtual Mesh interface not created");
5200                 return;
5201         }
5202
5203         bssid = g_supplicant_network_get_bssid(supplicant_network);
5204         address = g_malloc0(19);
5205         snprintf(address, 19, "%02x:%02x:%02x:%02x:%02x:%02x", bssid[0], bssid[1],
5206                                                                  bssid[2], bssid[3], bssid[4], bssid[5]);
5207
5208         identifier = g_supplicant_network_get_identifier(supplicant_network);
5209         name = g_supplicant_network_get_name(supplicant_network);
5210         security = g_supplicant_network_get_security(supplicant_network);
5211         frequency = g_supplicant_network_get_frequency(supplicant_network);
5212
5213         mesh_info = wifi->mesh_info;
5214         connman_mesh = connman_mesh_get(mesh_info->identifier, identifier);
5215         if (connman_mesh)
5216                 goto done;
5217
5218         DBG("Mesh Peer name %s identifier %s security %s added", name, identifier,
5219                                         security);
5220         connman_mesh = connman_mesh_create(mesh_info->identifier, identifier);
5221         connman_mesh_set_name(connman_mesh, name);
5222         connman_mesh_set_security(connman_mesh, security);
5223         connman_mesh_set_frequency(connman_mesh, frequency);
5224         connman_mesh_set_address(connman_mesh, address);
5225         connman_mesh_set_index(connman_mesh, mesh_info->index);
5226         connman_mesh_set_strength(connman_mesh,
5227                                                 calculate_strength(supplicant_network));
5228         connman_mesh_set_peer_type(connman_mesh, CONNMAN_MESH_PEER_TYPE_DISCOVERED);
5229
5230         ret = connman_mesh_register(connman_mesh);
5231         if (ret == -EALREADY)
5232                 DBG("Mesh Peer is already registered");
5233
5234 done:
5235         g_free(address);
5236 }
5237
5238 static void mesh_peer_removed(GSupplicantNetwork *supplicant_network)
5239 {
5240         GSupplicantInterface *interface;
5241         struct wifi_data *wifi;
5242         struct connman_mesh *connman_mesh;
5243         struct wifi_mesh_info *mesh_info;
5244         const char *identifier;
5245
5246         interface = g_supplicant_network_get_interface(supplicant_network);
5247         wifi = g_supplicant_interface_get_data(interface);
5248         if (!wifi || !wifi->mesh_interface) {
5249                 DBG("Virtual Mesh interface not created");
5250                 return;
5251         }
5252
5253         identifier = g_supplicant_network_get_identifier(supplicant_network);
5254         if (!identifier) {
5255                 DBG("Failed to get Mesh Peer identifier");
5256                 return;
5257         }
5258
5259         mesh_info = wifi->mesh_info;
5260         connman_mesh = connman_mesh_get(mesh_info->identifier, identifier);
5261         if (connman_mesh) {
5262                 /* Do not unregister connected mesh peer */
5263                 if (connman_mesh_peer_is_connected_state(connman_mesh)) {
5264                         DBG("Mesh Peer %s is connected", identifier);
5265                         return;
5266                 }
5267                 DBG("Mesh Peer identifier %s removed", identifier);
5268                 connman_mesh_unregister(connman_mesh);
5269         }
5270 }
5271 #endif
5272
5273
5274 #if defined TIZEN_EXT
5275 static GSList *get_supported_security_list(unsigned int keymgmt,
5276                                         bool owe_transition_mode,
5277                                         GSupplicantNetwork *supplicant_network)
5278 {
5279         GSList *sec_list = NULL;
5280         dbus_bool_t privacy = g_supplicant_network_get_privacy(supplicant_network);
5281         const char *enc_mode = g_supplicant_network_get_enc_mode(supplicant_network);
5282
5283         if (keymgmt &
5284                         (G_SUPPLICANT_KEYMGMT_WPA_EAP |
5285                                         G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
5286                 sec_list = g_slist_prepend (sec_list, "ieee8021x");
5287         else if (keymgmt & G_SUPPLICANT_KEYMGMT_WPA_FT_EAP)
5288                 sec_list = g_slist_prepend (sec_list, "ft_ieee8021x");
5289
5290         if (sec_list)
5291                 return sec_list;
5292
5293         if (keymgmt &
5294                         (G_SUPPLICANT_KEYMGMT_WPA_PSK |
5295                                         G_SUPPLICANT_KEYMGMT_WPA_PSK_256)) {
5296                 if (!g_strcmp0(enc_mode, "aes"))
5297                         sec_list = g_slist_prepend (sec_list, "rsn");
5298                 else if (!g_strcmp0(enc_mode, "tkip"))
5299                         sec_list = g_slist_prepend (sec_list, "psk");
5300                 else if (!g_strcmp0(enc_mode, "mixed")) {
5301                         sec_list = g_slist_prepend (sec_list, "psk");
5302                         sec_list = g_slist_prepend (sec_list, "rsn");
5303                 }
5304         } else if (keymgmt & G_SUPPLICANT_KEYMGMT_WPA_FT_PSK)
5305                 sec_list = g_slist_prepend (sec_list, "ft_psk");
5306
5307         if (keymgmt & G_SUPPLICANT_KEYMGMT_SAE)
5308                 sec_list = g_slist_prepend (sec_list, "sae");
5309         if (keymgmt & G_SUPPLICANT_KEYMGMT_OWE || owe_transition_mode)
5310                 sec_list = g_slist_prepend (sec_list, "owe");
5311         if (keymgmt & G_SUPPLICANT_KEYMGMT_DPP)
5312                 sec_list = g_slist_prepend (sec_list, "dpp");
5313
5314         if (sec_list)
5315                 return sec_list;
5316
5317         if (privacy)
5318                 sec_list = g_slist_prepend (sec_list, "wep");
5319         else
5320                 sec_list = g_slist_prepend (sec_list, "none");
5321
5322         return sec_list;
5323 }
5324 #endif
5325
5326 static void network_added(GSupplicantNetwork *supplicant_network)
5327 {
5328         struct connman_network *network;
5329         GSupplicantInterface *interface;
5330         struct wifi_data *wifi;
5331         const char *name, *identifier, *security, *group, *mode;
5332         const unsigned char *ssid;
5333         unsigned int ssid_len;
5334         bool wps;
5335         bool wps_pbc;
5336         bool wps_ready;
5337         bool wps_advertizing;
5338
5339 #if defined TIZEN_EXT
5340         bool owe_transition_mode;
5341         const unsigned char *transition_mode_ssid;
5342         const unsigned char *transition_mode_bssid;
5343         unsigned int transition_mode_ssid_len;
5344         unsigned int keymgmt;
5345         GSList *vsie_list = NULL;
5346         GSList *sec_list = NULL;
5347         const unsigned char *country_code;
5348         ieee80211_modes_e phy_mode;
5349 #endif
5350
5351         mode = g_supplicant_network_get_mode(supplicant_network);
5352         identifier = g_supplicant_network_get_identifier(supplicant_network);
5353 #if defined TIZEN_EXT
5354         if (!simplified_log)
5355 #endif
5356         DBG("%s", identifier);
5357
5358         if (!g_strcmp0(mode, "adhoc"))
5359                 return;
5360
5361 #if defined TIZEN_EXT_WIFI_MESH
5362         if (!g_strcmp0(mode, "mesh")) {
5363                 mesh_peer_added(supplicant_network);
5364                 return;
5365         }
5366 #endif
5367
5368         interface = g_supplicant_network_get_interface(supplicant_network);
5369 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5370         /*
5371          * Note: If supplicant interface's driver is wired then skip it,
5372          * because it meanti only for ethernet not Wi-Fi.
5373          */
5374         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5375                 return;
5376 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5377
5378         wifi = g_supplicant_interface_get_data(interface);
5379         name = g_supplicant_network_get_name(supplicant_network);
5380         security = g_supplicant_network_get_security(supplicant_network);
5381         group = g_supplicant_network_get_identifier(supplicant_network);
5382         wps = g_supplicant_network_get_wps(supplicant_network);
5383         wps_pbc = g_supplicant_network_is_wps_pbc(supplicant_network);
5384         wps_ready = g_supplicant_network_is_wps_active(supplicant_network);
5385         wps_advertizing = g_supplicant_network_is_wps_advertizing(
5386                                                         supplicant_network);
5387
5388         if (!wifi)
5389                 return;
5390
5391         ssid = g_supplicant_network_get_ssid(supplicant_network, &ssid_len);
5392
5393         network = connman_device_get_network(wifi->device, identifier);
5394
5395         if (!network) {
5396                 network = connman_network_create(identifier,
5397                                                 CONNMAN_NETWORK_TYPE_WIFI);
5398                 if (!network)
5399                         return;
5400
5401                 connman_network_set_index(network, wifi->index);
5402
5403                 if (connman_device_add_network(wifi->device, network) < 0) {
5404                         connman_network_unref(network);
5405                         return;
5406                 }
5407
5408                 wifi->networks = g_slist_prepend(wifi->networks, network);
5409         }
5410
5411         if (name && name[0] != '\0')
5412                 connman_network_set_name(network, name);
5413
5414         connman_network_set_blob(network, "WiFi.SSID",
5415                                                 ssid, ssid_len);
5416 #if defined TIZEN_EXT
5417         vsie_list = (GSList *)g_supplicant_network_get_wifi_vsie(supplicant_network);
5418         if (vsie_list)
5419                 connman_network_set_vsie_list(network, vsie_list);
5420         else
5421                 DBG("vsie_list is NULL");
5422         country_code = g_supplicant_network_get_countrycode(supplicant_network);
5423         connman_network_set_countrycode(network, country_code);
5424         phy_mode = g_supplicant_network_get_phy_mode(supplicant_network);
5425         connman_network_set_phy_mode(network, phy_mode);
5426 #endif
5427         connman_network_set_string(network, "WiFi.Security", security);
5428         connman_network_set_strength(network,
5429                                 calculate_strength(supplicant_network));
5430         connman_network_set_bool(network, "WiFi.WPS", wps);
5431         connman_network_set_bool(network, "WiFi.WPSAdvertising",
5432                                 wps_advertizing);
5433
5434         if (wps) {
5435                 /* Is AP advertizing for WPS association?
5436                  * If so, we decide to use WPS by default */
5437                 if (wps_ready && wps_pbc &&
5438                                                 wps_advertizing)
5439 #if !defined TIZEN_EXT
5440                         connman_network_set_bool(network, "WiFi.UseWPS", true);
5441 #else
5442                         DBG("wps is activating by ap but ignore it.");
5443 #endif
5444         }
5445
5446         connman_network_set_frequency(network,
5447                         g_supplicant_network_get_frequency(supplicant_network));
5448
5449 #if defined TIZEN_EXT
5450         keymgmt = g_supplicant_network_get_keymgmt(supplicant_network);
5451         connman_network_set_bssid(network,
5452                         g_supplicant_network_get_bssid(supplicant_network));
5453         owe_transition_mode = (bool)g_supplicant_network_get_transition_mode(supplicant_network);
5454         connman_network_set_bool(network, "WiFi.TRANSITION_MODE", owe_transition_mode);
5455         if (owe_transition_mode) {
5456                 transition_mode_ssid = (unsigned char *)g_supplicant_network_get_transition_mode_ssid(supplicant_network, &transition_mode_ssid_len);
5457                 connman_network_set_blob(network, "WiFi.TRANSITION_MODE_SSID",
5458                                                         transition_mode_ssid, transition_mode_ssid_len);
5459                 transition_mode_bssid = g_supplicant_network_get_transition_mode_bssid(supplicant_network);
5460                 connman_network_set_transition_mode_bssid(network, transition_mode_bssid);
5461         }
5462
5463         sec_list = get_supported_security_list(keymgmt,
5464                         owe_transition_mode, supplicant_network);
5465
5466         connman_network_set_sec_list(network, sec_list);
5467         connman_network_set_maxrate(network,
5468                         g_supplicant_network_get_maxrate(supplicant_network));
5469         connman_network_set_enc_mode(network,
5470                         g_supplicant_network_get_enc_mode(supplicant_network));
5471         connman_network_set_rsn_mode(network,
5472                         g_supplicant_network_get_rsn_mode(supplicant_network));
5473         connman_network_set_bool(network, "WiFi.PMFRequired",
5474                         (bool)g_supplicant_network_is_pmf_required(supplicant_network));
5475         connman_network_set_keymgmt(network, keymgmt);
5476         connman_network_set_bool(network, "WiFi.HS20AP",
5477                         g_supplicant_network_is_hs20AP(supplicant_network));
5478         connman_network_set_bssid_list(network,
5479                         (GSList *)g_supplicant_network_get_bssid_list(supplicant_network));
5480         connman_network_set_last_connected_bssid(network,
5481                         g_supplicant_network_get_last_connected_bssid(supplicant_network));
5482         connman_network_set_assoc_reject_table(network,
5483                         g_supplicant_network_clone_assoc_reject_table(supplicant_network));
5484 #endif
5485         connman_network_set_available(network, true);
5486         connman_network_set_string(network, "WiFi.Mode", mode);
5487
5488 #if defined TIZEN_EXT
5489         if (group)
5490 #else
5491         if (ssid)
5492 #endif
5493                 connman_network_set_group(network, group);
5494
5495 #if defined TIZEN_EXT
5496         g_supplicant_network_set_last_connected_bssid(supplicant_network,
5497                         connman_network_get_last_connected_bssid(network));
5498 #endif
5499
5500 #if defined TIZEN_EXT
5501         if (wifi_first_scan == true)
5502                 found_with_first_scan = true;
5503 #endif
5504
5505         if (wifi->hidden && ssid) {
5506 #if defined TIZEN_EXT
5507                 if (network_security(wifi->hidden->security) ==
5508                         network_security(security) &&
5509 #else
5510                 if (!g_strcmp0(wifi->hidden->security, security) &&
5511 #endif
5512                                 wifi->hidden->ssid_len == ssid_len &&
5513                                 !memcmp(wifi->hidden->ssid, ssid, ssid_len)) {
5514                         connman_network_connect_hidden(network,
5515                                         wifi->hidden->identity,
5516                                         wifi->hidden->passphrase,
5517                                         wifi->hidden->user_data);
5518                         wifi->hidden->user_data = NULL;
5519                         hidden_free(wifi->hidden);
5520                         wifi->hidden = NULL;
5521                 }
5522         }
5523 }
5524
5525 static void network_removed(GSupplicantNetwork *network)
5526 {
5527         GSupplicantInterface *interface;
5528         struct wifi_data *wifi;
5529         const char *name, *identifier;
5530         struct connman_network *connman_network;
5531
5532 #if defined TIZEN_EXT_WIFI_MESH
5533         const char *mode;
5534         mode = g_supplicant_network_get_mode(network);
5535         if (!g_strcmp0(mode, "mesh")) {
5536                 mesh_peer_removed(network);
5537                 return;
5538         }
5539 #endif
5540
5541         interface = g_supplicant_network_get_interface(network);
5542 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5543         /*
5544          * Note: If supplicant interface's driver is wired then skip it,
5545          * because it meanti only for ethernet not Wi-Fi.
5546          */
5547         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5548                 return;
5549 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5550
5551         wifi = g_supplicant_interface_get_data(interface);
5552         identifier = g_supplicant_network_get_identifier(network);
5553         name = g_supplicant_network_get_name(network);
5554
5555         DBG("name %s", name);
5556
5557         if (!wifi)
5558                 return;
5559
5560         connman_network = connman_device_get_network(wifi->device, identifier);
5561         if (!connman_network)
5562                 return;
5563
5564 #if defined TIZEN_EXT
5565         if (connman_network == wifi->scan_pending_network)
5566                 wifi->scan_pending_network = NULL;
5567
5568         if (connman_network == wifi->pending_network)
5569                 wifi->pending_network = NULL;
5570
5571         if(connman_network_get_connecting(connman_network) == true){
5572                 connman_network_set_connected(connman_network, false);
5573         }
5574 #endif
5575
5576         wifi->networks = g_slist_remove(wifi->networks, connman_network);
5577
5578         connman_device_remove_network(wifi->device, connman_network);
5579         connman_network_unref(connman_network);
5580 }
5581
5582 static void network_changed(GSupplicantNetwork *network, const char *property)
5583 {
5584         GSupplicantInterface *interface;
5585         struct wifi_data *wifi;
5586         const char *name, *identifier;
5587         struct connman_network *connman_network;
5588         bool update_needed;
5589
5590 #if defined TIZEN_EXT
5591         const unsigned char *bssid;
5592         unsigned int maxrate;
5593         uint16_t frequency;
5594         bool wps;
5595         const unsigned char *country_code;
5596         ieee80211_modes_e phy_mode;
5597         GSList *bssid_list;
5598 #endif
5599
5600         interface = g_supplicant_network_get_interface(network);
5601 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5602         /*
5603          * Note: If supplicant interface's driver is wired then skip it,
5604          * because it meanti only for ethernet not Wi-Fi.
5605          */
5606         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5607                 return;
5608 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5609
5610         wifi = g_supplicant_interface_get_data(interface);
5611         identifier = g_supplicant_network_get_identifier(network);
5612         name = g_supplicant_network_get_name(network);
5613
5614 #if defined TIZEN_EXT
5615         if (!simplified_log)
5616                 DBG("name %s property %s", name, property);
5617 #else
5618         DBG("name %s", name);
5619 #endif
5620
5621         if (!wifi)
5622                 return;
5623
5624         connman_network = connman_device_get_network(wifi->device, identifier);
5625         if (!connman_network)
5626                 return;
5627
5628         if (g_str_equal(property, "WPSCapabilities")) {
5629                 bool wps;
5630                 bool wps_pbc;
5631                 bool wps_ready;
5632                 bool wps_advertizing;
5633
5634                 wps = g_supplicant_network_get_wps(network);
5635                 wps_pbc = g_supplicant_network_is_wps_pbc(network);
5636                 wps_ready = g_supplicant_network_is_wps_active(network);
5637                 wps_advertizing =
5638                         g_supplicant_network_is_wps_advertizing(network);
5639
5640                 connman_network_set_bool(connman_network, "WiFi.WPS", wps);
5641                 connman_network_set_bool(connman_network,
5642                                 "WiFi.WPSAdvertising", wps_advertizing);
5643
5644                 if (wps) {
5645                         /*
5646                          * Is AP advertizing for WPS association?
5647                          * If so, we decide to use WPS by default
5648                          */
5649                         if (wps_ready && wps_pbc && wps_advertizing)
5650                                 connman_network_set_bool(connman_network,
5651                                                         "WiFi.UseWPS", true);
5652                 }
5653
5654                 update_needed = true;
5655         } else if (g_str_equal(property, "Signal")) {
5656                 connman_network_set_strength(connman_network,
5657                                         calculate_strength(network));
5658                 update_needed = true;
5659         }
5660 #if defined TIZEN_EXT && defined TIZEN_EXT_INS
5661         else if (g_str_equal(property, "LastConnectedBSSID")) {
5662                 const char *ident, *group;
5663                 char *service_ident;
5664                 bool need_save;
5665
5666                 ident = connman_device_get_ident(wifi->device);
5667                 group = connman_network_get_group(connman_network);
5668                 if (ident && group) {
5669                         service_ident = g_strdup_printf("%s_%s_%s",
5670                                 __connman_network_get_type(connman_network), ident, group);
5671
5672                         need_save = connman_device_set_last_connected_ident(wifi->device, service_ident);
5673                         if (need_save)
5674                                 connman_device_save_last_connected(wifi->device);
5675
5676                         g_free(service_ident);
5677                 }
5678
5679                 connman_network_set_last_connected_bssid(connman_network,
5680                                         g_supplicant_network_get_last_connected_bssid(network));
5681
5682                 update_needed = true;
5683         }
5684 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_INS */
5685 #if defined TIZEN_EXT
5686         else if (g_str_equal(property, "UpdateAssocReject")) {
5687                 connman_network_set_assoc_reject_table(connman_network,
5688                                 g_supplicant_network_clone_assoc_reject_table(network));
5689                 update_needed = true;
5690         }
5691 #endif
5692         else
5693                 update_needed = false;
5694
5695         if (update_needed)
5696                 connman_network_update(connman_network);
5697
5698 #if defined TIZEN_EXT
5699         bssid = g_supplicant_network_get_bssid(network);
5700         maxrate = g_supplicant_network_get_maxrate(network);
5701         frequency = g_supplicant_network_get_frequency(network);
5702         wps = g_supplicant_network_get_wps(network);
5703         phy_mode = g_supplicant_network_get_phy_mode(network);
5704
5705         connman_network_set_bssid(connman_network, bssid);
5706         connman_network_set_maxrate(connman_network, maxrate);
5707         connman_network_set_frequency(connman_network, frequency);
5708         connman_network_set_bool(connman_network, "WiFi.WPS", wps);
5709         country_code = g_supplicant_network_get_countrycode(network);
5710         connman_network_set_countrycode(connman_network, country_code);
5711         bssid_list = (GSList *)g_supplicant_network_get_bssid_list(network);
5712         connman_network_set_bssid_list(connman_network, bssid_list);
5713         connman_network_set_phy_mode(connman_network, phy_mode);
5714
5715         if (g_str_equal(property, "CheckMultiBssidConnect") &&
5716                         connman_network_get_associating(connman_network))
5717                 network_connect(connman_network);
5718 #endif
5719 }
5720
5721 static void network_associated(GSupplicantNetwork *network)
5722 {
5723         GSupplicantInterface *interface;
5724         struct wifi_data *wifi;
5725         struct connman_network *connman_network;
5726         const char *identifier;
5727
5728         DBG("");
5729
5730         interface = g_supplicant_network_get_interface(network);
5731         if (!interface)
5732                 return;
5733 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5734         /*
5735          * Note: If supplicant interface's driver is wired then skip it,
5736          * because it meanti only for ethernet not Wi-Fi.
5737          */
5738         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5739                 return;
5740 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5741
5742
5743         wifi = g_supplicant_interface_get_data(interface);
5744         if (!wifi)
5745                 return;
5746
5747         /* P2P networks must not be treated as WiFi networks */
5748         if (wifi->p2p_connecting || wifi->p2p_device)
5749                 return;
5750
5751         identifier = g_supplicant_network_get_identifier(network);
5752
5753         connman_network = connman_device_get_network(wifi->device, identifier);
5754         if (!connman_network)
5755                 return;
5756
5757         if (wifi->network) {
5758                 if (wifi->network == connman_network)
5759                         return;
5760 #if TIZEN_EXT
5761                 unsigned int ssid_len;
5762                 DBG("network1 ssid[%s] , OWE[%d],ssid[%s]",
5763                         (char *)connman_network_get_blob(wifi->network,"WiFi.SSID", &ssid_len),
5764                         connman_network_get_bool(wifi->network,"WiFi.TRANSITION_MODE"),
5765                         (char *)connman_network_get_blob(wifi->network,"WiFi.TRANSITION_MODE_SSID", &ssid_len));
5766
5767                 DBG("network1 ssid[%s], OWE[%d], ssid[%s]",
5768                         (char *)connman_network_get_blob(connman_network,"WiFi.SSID",&ssid_len),
5769                         connman_network_get_bool(connman_network,"WiFi.TRANSITION_MODE"),
5770                         (char *)connman_network_get_blob(connman_network,"WiFi.TRANSITION_MODE_SSID", &ssid_len));
5771                 if (connman_network_check_transition_mode(wifi->network, connman_network)) {//OWE trasition mode check
5772                         DBG("OWE transition mode is TRUE");
5773                         return;
5774                 }
5775 #endif
5776                 /*
5777                  * This should never happen, we got associated with
5778                  * a network different than the one we were expecting.
5779                  */
5780                 DBG("Associated to %p while expecting %p",
5781                                         connman_network, wifi->network);
5782
5783                 connman_network_set_associating(wifi->network, false);
5784         }
5785
5786         DBG("Reconnecting to previous network %p from wpa_s", connman_network);
5787
5788         wifi->network = connman_network_ref(connman_network);
5789         wifi->retries = 0;
5790
5791         /*
5792          * Interface state changes callback (interface_state) is always
5793          * called before network_associated callback thus we need to call
5794          * interface_state again in order to process the new state now that
5795          * we have the network properly set.
5796          */
5797         interface_state(interface);
5798 }
5799
5800 static void sta_authorized(GSupplicantInterface *interface,
5801                                         const char *addr)
5802 {
5803 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5804         /*
5805          * Note: If supplicant interface's driver is wired then skip it,
5806          * because it meanti only for ethernet not Wi-Fi.
5807          */
5808         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5809                 return;
5810 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5811
5812         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
5813
5814         DBG("wifi %p station %s authorized", wifi, addr);
5815
5816         if (!wifi || !wifi->tethering)
5817                 return;
5818
5819         __connman_tethering_client_register(addr);
5820 }
5821
5822 static void sta_deauthorized(GSupplicantInterface *interface,
5823                                         const char *addr)
5824 {
5825 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
5826         /*
5827          * Note: If supplicant interface's driver is wired then skip it,
5828          * because it meanti only for ethernet not Wi-Fi.
5829          */
5830         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
5831                 return;
5832 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
5833
5834         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
5835
5836         DBG("wifi %p station %s deauthorized", wifi, addr);
5837
5838         if (!wifi || !wifi->tethering)
5839                 return;
5840
5841         __connman_tethering_client_unregister(addr);
5842 }
5843
5844 static void apply_peer_services(GSupplicantPeer *peer,
5845                                 struct connman_peer *connman_peer)
5846 {
5847         const unsigned char *data;
5848         int length;
5849
5850         DBG("");
5851
5852         connman_peer_reset_services(connman_peer);
5853
5854         data = g_supplicant_peer_get_widi_ies(peer, &length);
5855         if (data) {
5856                 connman_peer_add_service(connman_peer,
5857                         CONNMAN_PEER_SERVICE_WIFI_DISPLAY, data, length);
5858         }
5859 }
5860
5861 static void peer_found(GSupplicantPeer *peer)
5862 {
5863         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
5864         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
5865         struct connman_peer *connman_peer;
5866         const char *identifier, *name;
5867         int ret;
5868
5869 #if defined TIZEN_EXT
5870         if (!wifi)
5871                 return;
5872 #endif
5873         identifier = g_supplicant_peer_get_identifier(peer);
5874         name = g_supplicant_peer_get_name(peer);
5875
5876         DBG("ident: %s", identifier);
5877
5878         connman_peer = connman_peer_get(wifi->device, identifier);
5879         if (connman_peer)
5880                 return;
5881
5882         connman_peer = connman_peer_create(identifier);
5883         connman_peer_set_name(connman_peer, name);
5884         connman_peer_set_device(connman_peer, wifi->device);
5885         apply_peer_services(peer, connman_peer);
5886
5887         ret = connman_peer_register(connman_peer);
5888         if (ret < 0 && ret != -EALREADY)
5889                 connman_peer_unref(connman_peer);
5890         else
5891                 wifi->peers = g_slist_prepend(wifi->peers, connman_peer);
5892 }
5893
5894 static void peer_lost(GSupplicantPeer *peer)
5895 {
5896         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
5897         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
5898         struct connman_peer *connman_peer;
5899         const char *identifier;
5900
5901         if (!wifi)
5902                 return;
5903
5904         identifier = g_supplicant_peer_get_identifier(peer);
5905
5906         DBG("ident: %s", identifier);
5907
5908         connman_peer = connman_peer_get(wifi->device, identifier);
5909         if (connman_peer) {
5910                 if (wifi->p2p_connecting &&
5911                                 wifi->pending_peer == connman_peer) {
5912                         peer_connect_timeout(wifi);
5913                 }
5914                 connman_peer_unregister(connman_peer);
5915                 connman_peer_unref(connman_peer);
5916         }
5917
5918         wifi->peers = g_slist_remove(wifi->peers, connman_peer);
5919 }
5920
5921 static void peer_changed(GSupplicantPeer *peer, GSupplicantPeerState state)
5922 {
5923         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
5924         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
5925         enum connman_peer_state p_state = CONNMAN_PEER_STATE_UNKNOWN;
5926         struct connman_peer *connman_peer;
5927         const char *identifier;
5928
5929         identifier = g_supplicant_peer_get_identifier(peer);
5930
5931         DBG("ident: %s", identifier);
5932
5933         if (!wifi)
5934                 return;
5935
5936         connman_peer = connman_peer_get(wifi->device, identifier);
5937         if (!connman_peer)
5938                 return;
5939
5940         switch (state) {
5941         case G_SUPPLICANT_PEER_SERVICES_CHANGED:
5942                 apply_peer_services(peer, connman_peer);
5943                 connman_peer_services_changed(connman_peer);
5944                 return;
5945         case G_SUPPLICANT_PEER_GROUP_CHANGED:
5946                 if (!g_supplicant_peer_is_in_a_group(peer))
5947                         p_state = CONNMAN_PEER_STATE_IDLE;
5948                 else
5949                         p_state = CONNMAN_PEER_STATE_CONFIGURATION;
5950                 break;
5951         case G_SUPPLICANT_PEER_GROUP_STARTED:
5952                 break;
5953         case G_SUPPLICANT_PEER_GROUP_FINISHED:
5954                 p_state = CONNMAN_PEER_STATE_IDLE;
5955                 break;
5956         case G_SUPPLICANT_PEER_GROUP_JOINED:
5957                 connman_peer_set_iface_address(connman_peer,
5958                                 g_supplicant_peer_get_iface_address(peer));
5959                 break;
5960         case G_SUPPLICANT_PEER_GROUP_DISCONNECTED:
5961                 p_state = CONNMAN_PEER_STATE_IDLE;
5962                 break;
5963         case G_SUPPLICANT_PEER_GROUP_FAILED:
5964                 if (g_supplicant_peer_has_requested_connection(peer))
5965                         p_state = CONNMAN_PEER_STATE_IDLE;
5966                 else
5967                         p_state = CONNMAN_PEER_STATE_FAILURE;
5968                 break;
5969         }
5970
5971         if (p_state == CONNMAN_PEER_STATE_CONFIGURATION ||
5972                                         p_state == CONNMAN_PEER_STATE_FAILURE) {
5973                 if (wifi->p2p_connecting
5974                                 && connman_peer == wifi->pending_peer)
5975                         peer_cancel_timeout(wifi);
5976                 else
5977                         p_state = CONNMAN_PEER_STATE_UNKNOWN;
5978         }
5979
5980         if (p_state == CONNMAN_PEER_STATE_UNKNOWN)
5981                 return;
5982
5983         if (p_state == CONNMAN_PEER_STATE_CONFIGURATION) {
5984                 GSupplicantInterface *g_iface;
5985                 struct wifi_data *g_wifi;
5986
5987                 g_iface = g_supplicant_peer_get_group_interface(peer);
5988                 if (!g_iface)
5989                         return;
5990
5991                 g_wifi = g_supplicant_interface_get_data(g_iface);
5992                 if (!g_wifi)
5993                         return;
5994
5995                 connman_peer_set_as_master(connman_peer,
5996                                         !g_supplicant_peer_is_client(peer));
5997                 connman_peer_set_sub_device(connman_peer, g_wifi->device);
5998
5999                 /*
6000                  * If wpa_supplicant didn't create a dedicated p2p-group
6001                  * interface then mark this interface as p2p_device to avoid
6002                  * scan and auto-scan are launched on it while P2P is connected.
6003                  */
6004                 if (!g_list_find(p2p_iface_list, g_wifi))
6005                         wifi->p2p_device = true;
6006         }
6007
6008         connman_peer_set_state(connman_peer, p_state);
6009 }
6010
6011 static void peer_request(GSupplicantPeer *peer)
6012 {
6013         GSupplicantInterface *iface = g_supplicant_peer_get_interface(peer);
6014         struct wifi_data *wifi = g_supplicant_interface_get_data(iface);
6015         struct connman_peer *connman_peer;
6016         const char *identifier;
6017
6018 #if defined TIZEN_EXT
6019         if (!wifi)
6020                 return;
6021 #endif
6022
6023         identifier = g_supplicant_peer_get_identifier(peer);
6024
6025         DBG("ident: %s", identifier);
6026
6027         connman_peer = connman_peer_get(wifi->device, identifier);
6028         if (!connman_peer)
6029                 return;
6030
6031         connman_peer_request_connection(connman_peer);
6032 }
6033
6034 #if defined TIZEN_EXT
6035 static void system_power_off(void)
6036 {
6037         GList *list;
6038         struct wifi_data *wifi;
6039         struct connman_service *service;
6040         struct connman_ipconfig *ipconfig_ipv4;
6041
6042         if (connman_setting_get_bool("WiFiDHCPRelease") == true) {
6043                 for (list = iface_list; list; list = list->next) {
6044                         wifi = list->data;
6045
6046                         if (wifi->network != NULL) {
6047                                 service = connman_service_lookup_from_network(wifi->network);
6048                                 ipconfig_ipv4 = __connman_service_get_ip4config(service);
6049                                 __connman_dhcp_stop(ipconfig_ipv4);
6050                         }
6051                 }
6052         }
6053 }
6054
6055 static void network_merged(GSupplicantNetwork *network)
6056 {
6057         GSupplicantInterface *interface;
6058         GSupplicantState state;
6059         struct wifi_data *wifi;
6060         const char *identifier;
6061         struct connman_network *connman_network;
6062         bool ishs20AP = 0;
6063         char *temp = NULL;
6064
6065         interface = g_supplicant_network_get_interface(network);
6066         if (!interface)
6067                 return;
6068
6069 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6070         /*
6071          * Note: If supplicant interface's driver is wired then skip it,
6072          * because it meanti only for ethernet not Wi-Fi.
6073          */
6074         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
6075                 return;
6076 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6077
6078         state = g_supplicant_interface_get_state(interface);
6079         if (state < G_SUPPLICANT_STATE_AUTHENTICATING)
6080                 return;
6081
6082         wifi = g_supplicant_interface_get_data(interface);
6083         if (!wifi)
6084                 return;
6085
6086         identifier = g_supplicant_network_get_identifier(network);
6087
6088         connman_network = connman_device_get_network(wifi->device, identifier);
6089         if (!connman_network)
6090                 return;
6091
6092         DBG("merged identifier %s", identifier);
6093
6094         if (wifi->connected == FALSE) {
6095                 switch (state) {
6096                 case G_SUPPLICANT_STATE_AUTHENTICATING:
6097                 case G_SUPPLICANT_STATE_ASSOCIATING:
6098                 case G_SUPPLICANT_STATE_ASSOCIATED:
6099                 case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
6100                 case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
6101                         connman_network_set_associating(connman_network, TRUE);
6102                         break;
6103                 case G_SUPPLICANT_STATE_COMPLETED:
6104                         connman_network_set_connected(connman_network, TRUE);
6105                         break;
6106                 default:
6107                         DBG("Not handled the state : %d", state);
6108                         break;
6109                 }
6110         }
6111
6112         ishs20AP = g_supplicant_network_is_hs20AP(network);
6113
6114         if (ishs20AP &&
6115                 g_strcmp0(g_supplicant_network_get_security(network), "ieee8021x") == 0) {
6116                 temp = g_ascii_strdown(g_supplicant_network_get_eap(network), -1);
6117                 connman_network_set_string(connman_network, "WiFi.EAP",
6118                                 temp);
6119                 connman_network_set_string(connman_network, "WiFi.Identity",
6120                                 g_supplicant_network_get_identity(network));
6121                 connman_network_set_string(connman_network, "WiFi.Phase2",
6122                                 g_supplicant_network_get_phase2(network));
6123
6124                 g_free(temp);
6125         }
6126
6127         wifi->network = connman_network;
6128 }
6129
6130 static void assoc_failed(void *user_data)
6131 {
6132         struct connman_network *network = user_data;
6133         connman_network_set_associating(network, false);
6134 }
6135
6136 static void scan_done(GSupplicantInterface *interface)
6137 {
6138 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6139         /*
6140          * Note: If supplicant interface's driver is wired then skip it,
6141          * because it meanti only for ethernet not Wi-Fi.
6142          */
6143         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
6144                 return;
6145 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6146
6147         GList *list;
6148         int scan_type = CONNMAN_SCAN_TYPE_WPA_SUPPLICANT;
6149         struct wifi_data *wifi;
6150         bool scanning;
6151
6152         for (list = iface_list; list; list = list->next) {
6153                 wifi = list->data;
6154
6155                 if (interface == wifi->interface) {
6156                         scanning = connman_device_get_scanning(wifi->device,
6157                                         CONNMAN_SERVICE_TYPE_WIFI);
6158                         if (!scanning)
6159                                 __connman_technology_notify_scan_done(
6160                                                 connman_device_get_string(wifi->device, "Interface"), scan_type);
6161                         break;
6162                 }
6163         }
6164 }
6165 #endif
6166
6167 static void debug(const char *str)
6168 {
6169 #if defined TIZEN_EXT
6170         if (connman_setting_get_bool("ConnmanSupplicantDebug"))
6171 #else
6172         if (getenv("CONNMAN_SUPPLICANT_DEBUG"))
6173 #endif
6174                 connman_debug("%s", str);
6175 }
6176
6177 static void disconnect_reasoncode(GSupplicantInterface *interface,
6178                                 int reasoncode)
6179 {
6180 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6181         /*
6182          * Note: If supplicant interface's driver is wired then skip it,
6183          * because it meanti only for ethernet not Wi-Fi.
6184          */
6185         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
6186                 return;
6187 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6188
6189         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
6190
6191         if (wifi != NULL) {
6192                 wifi->disconnect_code = reasoncode;
6193         }
6194 }
6195
6196 static void assoc_status_code(GSupplicantInterface *interface, int status_code)
6197 {
6198 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6199         /*
6200          * Note: If supplicant interface's driver is wired then skip it,
6201          * because it meanti only for ethernet not Wi-Fi.
6202          */
6203         if (!g_strcmp0("wired", g_supplicant_interface_get_driver(interface)))
6204                 return;
6205 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6206
6207         struct wifi_data *wifi = g_supplicant_interface_get_data(interface);
6208
6209         if (wifi != NULL) {
6210                 wifi->assoc_code = status_code;
6211         }
6212 }
6213
6214 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
6215 static GSupplicantCallbacks callbacks = {
6216 #else /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6217 static const GSupplicantCallbacks callbacks = {
6218 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
6219         .system_ready           = system_ready,
6220         .system_killed          = system_killed,
6221         .interface_added        = interface_added,
6222         .interface_state        = interface_state,
6223         .interface_removed      = interface_removed,
6224         .p2p_support            = p2p_support,
6225         .scan_started           = scan_started,
6226         .scan_finished          = scan_finished,
6227         .ap_create_fail         = ap_create_fail,
6228         .network_added          = network_added,
6229         .network_removed        = network_removed,
6230         .network_changed        = network_changed,
6231         .network_associated     = network_associated,
6232         .sta_authorized         = sta_authorized,
6233         .sta_deauthorized       = sta_deauthorized,
6234         .peer_found             = peer_found,
6235         .peer_lost              = peer_lost,
6236         .peer_changed           = peer_changed,
6237         .peer_request           = peer_request,
6238 #if defined TIZEN_EXT
6239         .system_power_off       = system_power_off,
6240         .network_merged         = network_merged,
6241         .assoc_failed           = assoc_failed,
6242         .scan_done              = scan_done,
6243 #endif
6244         .debug                  = debug,
6245         .disconnect_reasoncode  = disconnect_reasoncode,
6246         .assoc_status_code      = assoc_status_code,
6247 #if defined TIZEN_EXT_WIFI_MESH
6248         .mesh_support           = mesh_support,
6249         .mesh_group_started = mesh_group_started,
6250         .mesh_group_removed = mesh_group_removed,
6251         .mesh_peer_connected = mesh_peer_connected,
6252         .mesh_peer_disconnected = mesh_peer_disconnected,
6253 #endif
6254 };
6255
6256
6257 static int tech_probe(struct connman_technology *technology)
6258 {
6259         wifi_technology = technology;
6260
6261         return 0;
6262 }
6263
6264 static void tech_remove(struct connman_technology *technology)
6265 {
6266         wifi_technology = NULL;
6267 }
6268
6269 static GSupplicantSSID *ssid_ap_init(const char *ssid, const char *passphrase)
6270 {
6271         GSupplicantSSID *ap;
6272
6273         ap = g_try_malloc0(sizeof(GSupplicantSSID));
6274         if (!ap)
6275                 return NULL;
6276
6277         ap->mode = G_SUPPLICANT_MODE_MASTER;
6278 #if defined TIZEN_EXT
6279         ap->ssid = (void *) ssid;
6280 #else
6281         ap->ssid = ssid;
6282 #endif
6283         ap->ssid_len = strlen(ssid);
6284         ap->scan_ssid = 0;
6285         ap->freq = 2412;
6286
6287         if (!passphrase || strlen(passphrase) == 0) {
6288                 ap->security = G_SUPPLICANT_SECURITY_NONE;
6289                 ap->passphrase = NULL;
6290         } else {
6291                ap->security = G_SUPPLICANT_SECURITY_PSK;
6292                ap->protocol = G_SUPPLICANT_PROTO_RSN;
6293                ap->pairwise_cipher = G_SUPPLICANT_PAIRWISE_CCMP;
6294                ap->group_cipher = G_SUPPLICANT_GROUP_CCMP;
6295                ap->passphrase = passphrase;
6296         }
6297
6298         return ap;
6299 }
6300
6301 static void ap_start_callback(int result, GSupplicantInterface *interface,
6302                                                         void *user_data)
6303 {
6304         struct wifi_tethering_info *info = user_data;
6305
6306         DBG("result %d index %d bridge %s",
6307                 result, info->wifi->index, info->wifi->bridge);
6308
6309         if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
6310                 connman_inet_remove_from_bridge(info->wifi->index,
6311                                                         info->wifi->bridge);
6312
6313                 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
6314                         connman_technology_tethering_notify(info->technology, false);
6315                         g_free(info->wifi->tethering_param->ssid);
6316                         g_free(info->wifi->tethering_param);
6317                         info->wifi->tethering_param = NULL;
6318                 }
6319         }
6320
6321         g_free(info->ifname);
6322         g_free(info);
6323 }
6324
6325 static void ap_create_callback(int result,
6326                                 GSupplicantInterface *interface,
6327                                         void *user_data)
6328 {
6329         struct wifi_tethering_info *info = user_data;
6330
6331         DBG("result %d ifname %s", result,
6332                                 g_supplicant_interface_get_ifname(interface));
6333
6334         if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
6335                 connman_inet_remove_from_bridge(info->wifi->index,
6336                                                         info->wifi->bridge);
6337
6338                 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
6339                         connman_technology_tethering_notify(info->technology, false);
6340                         g_free(info->wifi->tethering_param->ssid);
6341                         g_free(info->wifi->tethering_param);
6342                         info->wifi->tethering_param = NULL;
6343
6344                 }
6345
6346                 g_free(info->ifname);
6347                 g_free(info->ssid);
6348                 g_free(info);
6349                 return;
6350         }
6351
6352         info->wifi->interface = interface;
6353         g_supplicant_interface_set_data(interface, info->wifi);
6354
6355         if (g_supplicant_interface_set_apscan(interface, 2) < 0)
6356                 connman_error("Failed to set interface ap_scan property");
6357
6358         g_supplicant_interface_connect(interface, info->ssid,
6359                                                 ap_start_callback, info);
6360 }
6361
6362 static void sta_remove_callback(int result,
6363                                 GSupplicantInterface *interface,
6364                                         void *user_data)
6365 {
6366         struct wifi_tethering_info *info = user_data;
6367         const char *driver = connman_setting_get_string("wifi");
6368
6369         DBG("ifname %s result %d ", info->ifname, result);
6370
6371         if ((result < 0) || (info->wifi->ap_supported != WIFI_AP_SUPPORTED)) {
6372                 info->wifi->tethering = false;
6373                 connman_technology_tethering_notify(info->technology, false);
6374
6375                 if (info->wifi->ap_supported == WIFI_AP_SUPPORTED) {
6376                         g_free(info->wifi->tethering_param->ssid);
6377                         g_free(info->wifi->tethering_param);
6378                         info->wifi->tethering_param = NULL;
6379                 }
6380
6381                 g_free(info->ifname);
6382                 g_free(info->ssid);
6383                 g_free(info);
6384                 return;
6385         }
6386
6387         info->wifi->interface = NULL;
6388
6389         g_supplicant_interface_create(info->ifname, driver, info->wifi->bridge,
6390 #ifdef TIZEN_EXT
6391                         0, 0, 60,
6392 #endif /* TIZEN_EXT */
6393                                                 ap_create_callback,
6394                                                         info);
6395 }
6396
6397 static int enable_wifi_tethering(struct connman_technology *technology,
6398                                 const char *bridge, const char *identifier,
6399                                 const char *passphrase, bool available)
6400 {
6401         GList *list;
6402         GSupplicantInterface *interface;
6403         struct wifi_data *wifi;
6404         struct wifi_tethering_info *info;
6405         const char *ifname;
6406         unsigned int mode;
6407         int err, berr = 0;
6408
6409         for (list = iface_list; list; list = list->next) {
6410                 wifi = list->data;
6411
6412                 DBG("wifi %p network %p pending_network %p", wifi,
6413                         wifi->network, wifi->pending_network);
6414
6415                 interface = wifi->interface;
6416
6417                 if (!interface)
6418                         continue;
6419
6420                 ifname = g_supplicant_interface_get_ifname(wifi->interface);
6421                 if (!ifname)
6422                         continue;
6423
6424                 if (wifi->ap_supported == WIFI_AP_NOT_SUPPORTED) {
6425                         DBG("%s does not support AP mode (detected)", ifname);
6426                         continue;
6427                 }
6428
6429                 mode = g_supplicant_interface_get_mode(interface);
6430                 if ((mode & G_SUPPLICANT_CAPABILITY_MODE_AP) == 0) {
6431                         wifi->ap_supported = WIFI_AP_NOT_SUPPORTED;
6432                         DBG("%s does not support AP mode (capability)", ifname);
6433                         continue;
6434                 }
6435
6436                 if (wifi->network && available)
6437                         continue;
6438
6439                 info = g_try_malloc0(sizeof(struct wifi_tethering_info));
6440                 if (!info)
6441                         return -ENOMEM;
6442
6443                 wifi->tethering_param = g_try_malloc0(sizeof(struct wifi_tethering_info));
6444                 if (!wifi->tethering_param) {
6445                         g_free(info);
6446                         return -ENOMEM;
6447                 }
6448
6449                 info->wifi = wifi;
6450                 info->technology = technology;
6451                 info->wifi->bridge = bridge;
6452                 info->ssid = ssid_ap_init(identifier, passphrase);
6453                 if (!info->ssid)
6454                         goto failed;
6455
6456                 info->ifname = g_strdup(ifname);
6457
6458                 wifi->tethering_param->technology = technology;
6459                 wifi->tethering_param->ssid = ssid_ap_init(identifier, passphrase);
6460                 if (!wifi->tethering_param->ssid)
6461                         goto failed;
6462
6463                 info->wifi->tethering = true;
6464                 info->wifi->ap_supported = WIFI_AP_SUPPORTED;
6465
6466                 berr = connman_technology_tethering_notify(technology, true);
6467                 if (berr < 0)
6468                         goto failed;
6469
6470                 err = g_supplicant_interface_remove(interface,
6471                                                 sta_remove_callback,
6472                                                         info);
6473                 if (err >= 0) {
6474                         DBG("tethering wifi %p ifname %s", wifi, ifname);
6475                         return 0;
6476                 }
6477
6478         failed:
6479                 g_free(info->ifname);
6480                 g_free(info->ssid);
6481                 g_free(info);
6482                 g_free(wifi->tethering_param);
6483                 wifi->tethering_param = NULL;
6484
6485                 /*
6486                  * Remove bridge if it was correctly created but remove
6487                  * operation failed. Instead, if bridge creation failed then
6488                  * break out and do not try again on another interface,
6489                  * bridge set-up does not depend on it.
6490                  */
6491                 if (berr == 0)
6492                         connman_technology_tethering_notify(technology, false);
6493                 else
6494                         break;
6495         }
6496
6497         return -EOPNOTSUPP;
6498 }
6499
6500 static int tech_set_tethering(struct connman_technology *technology,
6501                                 const char *identifier, const char *passphrase,
6502                                 const char *bridge, bool enabled)
6503 {
6504         GList *list;
6505         struct wifi_data *wifi;
6506         int err;
6507
6508         DBG("");
6509
6510         if (!enabled) {
6511                 for (list = iface_list; list; list = list->next) {
6512                         wifi = list->data;
6513
6514                         if (wifi->tethering) {
6515                                 wifi->tethering = false;
6516
6517                                 connman_inet_remove_from_bridge(wifi->index,
6518                                                                         bridge);
6519                                 wifi->bridged = false;
6520                         }
6521                 }
6522
6523                 connman_technology_tethering_notify(technology, false);
6524
6525                 return 0;
6526         }
6527
6528         DBG("trying tethering for available devices");
6529         err = enable_wifi_tethering(technology, bridge, identifier, passphrase,
6530                                 true);
6531
6532         if (err < 0) {
6533                 DBG("trying tethering for any device");
6534                 err = enable_wifi_tethering(technology, bridge, identifier,
6535                                         passphrase, false);
6536         }
6537
6538         return err;
6539 }
6540
6541 static void regdom_callback(int result, const char *alpha2, void *user_data)
6542 {
6543         DBG("");
6544
6545         if (!wifi_technology)
6546                 return;
6547
6548         if (result != 0)
6549                 alpha2 = NULL;
6550
6551         connman_technology_regdom_notify(wifi_technology, alpha2);
6552 }
6553
6554 static int tech_set_regdom(struct connman_technology *technology, const char *alpha2)
6555 {
6556         return g_supplicant_set_country(alpha2, regdom_callback, NULL);
6557 }
6558
6559 #if defined TIZEN_EXT && defined TIZEN_EXT_INS
6560 static void supp_ins_init(void)
6561 {
6562         const char *string;
6563         GSupplicantINSPreferredFreq preferred_freq;
6564
6565         string = connman_setting_get_string("INSPreferredFreqBSSID");
6566         if (g_strcmp0(string, "5GHz") == 0)
6567                 preferred_freq = G_SUPPLICANT_INS_PREFERRED_FREQ_5GHZ;
6568         else if (g_strcmp0(string, "2.4GHz") == 0)
6569                 preferred_freq = G_SUPPLICANT_INS_PREFERRED_FREQ_24GHZ;
6570         else
6571                 preferred_freq = G_SUPPLICANT_INS_PREFERRED_FREQ_UNKNOWN;
6572
6573         g_supplicant_set_ins_settings(preferred_freq,
6574                 connman_setting_get_bool("INSLastConnectedBSSID"),
6575                 connman_setting_get_bool("INSAssocReject"),
6576                 connman_setting_get_bool("INSSignalBSSID"),
6577                 connman_setting_get_uint("INSPreferredFreqBSSIDScore"),
6578                 connman_setting_get_uint("INSLastConnectedBSSIDScore"),
6579                 connman_setting_get_uint("INSAssocRejectScore"),
6580                 connman_setting_get_int("INSSignalLevel3_5GHz"),
6581                 connman_setting_get_int("INSSignalLevel3_24GHz")
6582         );
6583 }
6584 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_INS */
6585
6586 static struct connman_technology_driver tech_driver = {
6587         .name           = "wifi",
6588         .type           = CONNMAN_SERVICE_TYPE_WIFI,
6589         .probe          = tech_probe,
6590         .remove         = tech_remove,
6591         .set_tethering  = tech_set_tethering,
6592         .set_regdom     = tech_set_regdom,
6593 };
6594
6595 static int wifi_init(void)
6596 {
6597         int err;
6598
6599         err = connman_network_driver_register(&network_driver);
6600         if (err < 0)
6601                 return err;
6602
6603         err = g_supplicant_register(&callbacks);
6604         if (err < 0) {
6605                 connman_network_driver_unregister(&network_driver);
6606                 return err;
6607         }
6608
6609         err = connman_technology_driver_register(&tech_driver);
6610         if (err < 0) {
6611                 g_supplicant_unregister(&callbacks);
6612                 connman_network_driver_unregister(&network_driver);
6613                 return err;
6614         }
6615
6616 #if defined TIZEN_EXT
6617         failed_bssids = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
6618 #endif
6619
6620 #if defined TIZEN_EXT && defined TIZEN_EXT_INS
6621         supp_ins_init();
6622 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_INS */
6623         return 0;
6624 }
6625
6626 static void wifi_exit(void)
6627 {
6628         DBG();
6629
6630         connman_technology_driver_unregister(&tech_driver);
6631
6632         g_supplicant_unregister(&callbacks);
6633
6634         connman_network_driver_unregister(&network_driver);
6635
6636 #if defined TIZEN_EXT
6637         g_hash_table_unref(failed_bssids);
6638 #endif
6639 }
6640
6641 CONNMAN_PLUGIN_DEFINE(wifi, "WiFi interface plugin", VERSION,
6642                 CONNMAN_PLUGIN_PRIORITY_DEFAULT, wifi_init, wifi_exit)