Imported Upstream version 1.37
[platform/upstream/connman.git] / gsupplicant / supplicant.c
1 /*
2  *
3  *  WPA supplicant library with GLib integration
4  *
5  *  Copyright (C) 2012-2013  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 <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <syslog.h>
32 #include <ctype.h>
33 #include <stdbool.h>
34 #include <netinet/if_ether.h>
35 #include <netinet/in.h>
36
37 #include <glib.h>
38 #include <gdbus.h>
39
40 #include "dbus.h"
41 #include "gsupplicant.h"
42
43 #define IEEE80211_CAP_ESS       0x0001
44 #define IEEE80211_CAP_IBSS      0x0002
45 #define IEEE80211_CAP_PRIVACY   0x0010
46
47 #if defined TIZEN_EXT
48 #define WLAN_EID_HT_CAP 45
49 #define WLAN_EID_VHT_CAP 191
50 #define WLAN_EID_SUPP_RATES 1
51 #define WLAN_EID_EXT_SUPP_RATES 50
52 #define COUNTRY_CODE_LENGTH     2
53 #define WIFI_BSSID_LEN_MAX 6
54 #endif
55
56 #define BSS_UNKNOWN_STRENGTH    -90
57
58 static DBusConnection *connection;
59
60 static const GSupplicantCallbacks *callbacks_pointer;
61
62 static dbus_bool_t system_available = FALSE;
63 static dbus_bool_t system_ready = FALSE;
64
65 static dbus_int32_t debug_level;
66 static dbus_bool_t debug_timestamp = FALSE;
67 static dbus_bool_t debug_showkeys = FALSE;
68
69 static const char *debug_strings[] = {
70         "msgdump", "debug", "info", "warning", "error", NULL
71 };
72
73 static unsigned int eap_methods;
74
75 struct strvalmap {
76         const char *str;
77         unsigned int val;
78 };
79
80 static struct strvalmap eap_method_map[] = {
81         { "MD5",        G_SUPPLICANT_EAP_METHOD_MD5     },
82         { "TLS",        G_SUPPLICANT_EAP_METHOD_TLS     },
83         { "MSCHAPV2",   G_SUPPLICANT_EAP_METHOD_MSCHAPV2        },
84         { "PEAP",       G_SUPPLICANT_EAP_METHOD_PEAP    },
85         { "TTLS",       G_SUPPLICANT_EAP_METHOD_TTLS    },
86         { "GTC",        G_SUPPLICANT_EAP_METHOD_GTC     },
87         { "OTP",        G_SUPPLICANT_EAP_METHOD_OTP     },
88         { "LEAP",       G_SUPPLICANT_EAP_METHOD_LEAP    },
89         { "WSC",        G_SUPPLICANT_EAP_METHOD_WSC     },
90         { }
91 };
92
93 static struct strvalmap keymgmt_map[] = {
94         { "none",               G_SUPPLICANT_KEYMGMT_NONE               },
95         { "ieee8021x",          G_SUPPLICANT_KEYMGMT_IEEE8021X  },
96         { "wpa-none",           G_SUPPLICANT_KEYMGMT_WPA_NONE   },
97         { "wpa-psk",            G_SUPPLICANT_KEYMGMT_WPA_PSK    },
98         { "wpa-psk-sha256",     G_SUPPLICANT_KEYMGMT_WPA_PSK_256        },
99         { "wpa-ft-psk",         G_SUPPLICANT_KEYMGMT_WPA_FT_PSK },
100         { "wpa-ft-eap",         G_SUPPLICANT_KEYMGMT_WPA_FT_EAP },
101         { "wpa-eap",            G_SUPPLICANT_KEYMGMT_WPA_EAP    },
102         { "wpa-eap-sha256",     G_SUPPLICANT_KEYMGMT_WPA_EAP_256        },
103         { "wps",                G_SUPPLICANT_KEYMGMT_WPS                },
104 #if defined TIZEN_EXT
105         { "sae",                G_SUPPLICANT_KEYMGMT_SAE                },
106         { "owe",                G_SUPPLICANT_KEYMGMT_OWE                },
107 #endif
108         { }
109 };
110
111 static struct strvalmap authalg_capa_map[] = {
112         { "open",       G_SUPPLICANT_CAPABILITY_AUTHALG_OPEN    },
113         { "shared",     G_SUPPLICANT_CAPABILITY_AUTHALG_SHARED  },
114         { "leap",       G_SUPPLICANT_CAPABILITY_AUTHALG_LEAP    },
115         { }
116 };
117
118 static struct strvalmap proto_capa_map[] = {
119         { "wpa",        G_SUPPLICANT_CAPABILITY_PROTO_WPA               },
120         { "rsn",        G_SUPPLICANT_CAPABILITY_PROTO_RSN               },
121         { }
122 };
123
124 static struct strvalmap group_map[] = {
125         { "wep40",      G_SUPPLICANT_GROUP_WEP40        },
126         { "wep104",     G_SUPPLICANT_GROUP_WEP104       },
127         { "tkip",       G_SUPPLICANT_GROUP_TKIP },
128         { "ccmp",       G_SUPPLICANT_GROUP_CCMP },
129         { }
130 };
131
132 static struct strvalmap pairwise_map[] = {
133         { "none",       G_SUPPLICANT_PAIRWISE_NONE      },
134         { "tkip",       G_SUPPLICANT_PAIRWISE_TKIP      },
135         { "ccmp",       G_SUPPLICANT_PAIRWISE_CCMP      },
136         { }
137 };
138
139 static struct strvalmap scan_capa_map[] = {
140         { "active",     G_SUPPLICANT_CAPABILITY_SCAN_ACTIVE     },
141         { "passive",    G_SUPPLICANT_CAPABILITY_SCAN_PASSIVE    },
142         { "ssid",       G_SUPPLICANT_CAPABILITY_SCAN_SSID               },
143         { }
144 };
145
146 static struct strvalmap mode_capa_map[] = {
147         { "infrastructure",     G_SUPPLICANT_CAPABILITY_MODE_INFRA      },
148         { "ad-hoc",             G_SUPPLICANT_CAPABILITY_MODE_IBSS       },
149         { "ap",                 G_SUPPLICANT_CAPABILITY_MODE_AP         },
150         { "p2p",                G_SUPPLICANT_CAPABILITY_MODE_P2P        },
151 #if defined TIZEN_EXT_WIFI_MESH
152         { "mesh",               G_SUPPLICANT_CAPABILITY_MODE_MESH       },
153 #endif
154         { }
155 };
156
157 static GHashTable *interface_table;
158 static GHashTable *bss_mapping;
159 static GHashTable *peer_mapping;
160 static GHashTable *group_mapping;
161 static GHashTable *pending_peer_connection;
162 static GHashTable *config_file_table;
163
164 struct _GSupplicantWpsCredentials {
165         unsigned char ssid[32];
166         unsigned int ssid_len;
167         char *key;
168 };
169
170 struct added_network_information {
171         char * ssid;
172         GSupplicantSecurity security;
173         char * passphrase;
174         char * private_passphrase;
175 };
176
177 #if defined TIZEN_EXT_WIFI_MESH
178 struct _GSupplicantMeshGroupInfo {
179         unsigned char ssid[32];
180         unsigned int ssid_len;
181         int disconnect_reason;
182 };
183 #endif
184
185 struct _GSupplicantInterface {
186         char *path;
187         char *network_path;
188         unsigned int keymgmt_capa;
189         unsigned int authalg_capa;
190         unsigned int proto_capa;
191         unsigned int group_capa;
192         unsigned int pairwise_capa;
193         unsigned int scan_capa;
194         unsigned int mode_capa;
195         unsigned int max_scan_ssids;
196         bool p2p_support;
197         bool p2p_finding;
198         bool ap_create_in_progress;
199         dbus_bool_t ready;
200         GSupplicantState state;
201         dbus_bool_t scanning;
202         GSupplicantInterfaceCallback scan_callback;
203         void *scan_data;
204         int apscan;
205         char *ifname;
206         char *driver;
207         char *bridge;
208         struct _GSupplicantWpsCredentials wps_cred;
209         GSupplicantWpsState wps_state;
210         GHashTable *network_table;
211         GHashTable *peer_table;
212         GHashTable *group_table;
213         GHashTable *bss_mapping;
214         void *data;
215         const char *pending_peer_path;
216         GSupplicantNetwork *current_network;
217         struct added_network_information network_info;
218 #if defined TIZEN_EXT
219         dbus_bool_t is_5_0_Ghz_supported;
220         int disconnect_reason;
221 #endif
222 #if defined TIZEN_EXT_WIFI_MESH
223         bool mesh_support;
224         struct _GSupplicantMeshGroupInfo group_info;
225 #endif
226 };
227
228 struct g_supplicant_bss {
229         GSupplicantInterface *interface;
230         char *path;
231         unsigned char bssid[6];
232         unsigned char ssid[32];
233         unsigned int ssid_len;
234         dbus_uint16_t frequency;
235         dbus_uint32_t maxrate;
236         dbus_int16_t signal;
237         GSupplicantMode mode;
238         GSupplicantSecurity security;
239         dbus_bool_t rsn_selected;
240         unsigned int wpa_keymgmt;
241         unsigned int wpa_pairwise;
242         unsigned int wpa_group;
243         unsigned int rsn_keymgmt;
244         unsigned int rsn_pairwise;
245         unsigned int rsn_group;
246         unsigned int keymgmt;
247         dbus_bool_t privacy;
248         dbus_bool_t psk;
249         dbus_bool_t ieee8021x;
250 #if defined TIZEN_EXT
251         dbus_bool_t ft_psk;
252         dbus_bool_t ft_ieee8021x;
253         GSList *vsie_list;
254         dbus_bool_t hs20;
255         unsigned char country_code[COUNTRY_CODE_LENGTH];
256         GSupplicantPhy_mode phy_mode;
257 #endif
258         unsigned int wps_capabilities;
259 #if defined TIZEN_EXT
260         dbus_bool_t sae;
261         dbus_bool_t owe;
262 #endif
263 };
264
265 struct _GSupplicantNetwork {
266         GSupplicantInterface *interface;
267         char *path;
268         char *group;
269         char *name;
270         unsigned char ssid[32];
271         unsigned int ssid_len;
272         dbus_int16_t signal;
273         dbus_uint16_t frequency;
274         struct g_supplicant_bss *best_bss;
275         GSupplicantMode mode;
276         GSupplicantSecurity security;
277         dbus_bool_t wps;
278         unsigned int wps_capabilities;
279         GHashTable *bss_table;
280         GHashTable *config_table;
281 #if defined TIZEN_EXT
282         bool isHS20AP;
283         char *eap;
284         char *identity;
285         char *phase2;
286         unsigned int keymgmt;
287         GSList *vsie_list;
288         unsigned char country_code[COUNTRY_CODE_LENGTH];
289         GSupplicantPhy_mode phy_mode;
290 #endif
291 };
292
293 struct _GSupplicantPeer {
294         GSupplicantInterface *interface;
295         char *path;
296         unsigned char device_address[ETH_ALEN];
297         unsigned char iface_address[ETH_ALEN];
298         char *name;
299         unsigned char *widi_ies;
300         int widi_ies_length;
301         char *identifier;
302         unsigned int wps_capabilities;
303         GSList *groups;
304         const GSupplicantInterface *current_group_iface;
305         bool connection_requested;
306 };
307
308 struct _GSupplicantGroup {
309         GSupplicantInterface *interface;
310         GSupplicantInterface *orig_interface;
311         char *path;
312         int role;
313         GSList *members;
314 };
315
316 struct interface_data {
317         GSupplicantInterface *interface;
318         char *path; /* Interface path cannot be taken from interface (above) as
319                      * it might have been freed already.
320                      */
321         GSupplicantInterfaceCallback callback;
322         void *user_data;
323         bool network_remove_in_progress;
324         GSupplicantSSID *ssid;
325 };
326
327 #if defined TIZEN_EXT
328 struct interface_signalpoll_data {
329         GSupplicantInterface *interface;
330         char *path;
331         GSupplicantMaxSpeedCallback callback;
332         void *user_data;
333 };
334 #endif
335
336 struct interface_create_data {
337         char *ifname;
338         char *driver;
339         char *bridge;
340 #if defined TIZEN_EXT_WIFI_MESH
341         char *parent_ifname;
342         bool is_mesh_interface;
343 #endif
344         GSupplicantInterface *interface;
345         GSupplicantInterfaceCallback callback;
346         void *user_data;
347 };
348
349 struct interface_connect_data {
350         GSupplicantInterface *interface;
351         char *path;
352         GSupplicantInterfaceCallback callback;
353         void *user_data;
354         union {
355                 GSupplicantSSID *ssid;
356                 GSupplicantPeerParams *peer;
357         };
358 };
359
360 struct interface_scan_data {
361         GSupplicantInterface *interface;
362         char *path;
363         GSupplicantInterfaceCallback callback;
364         GSupplicantScanParams *scan_params;
365         void *user_data;
366 };
367
368 #if defined TIZEN_EXT
369 struct g_connman_bssids {
370         unsigned char bssid[WIFI_BSSID_LEN_MAX];
371         uint16_t strength;
372         uint16_t frequency;
373 };
374 #endif
375
376 static int network_remove(struct interface_data *data);
377
378 #if defined TIZEN_EXT_WIFI_MESH
379 struct _GSupplicantMeshPeer {
380         GSupplicantInterface *interface;
381         char *peer_address;
382         int disconnect_reason;
383 };
384 #endif
385
386 static inline void debug(const char *format, ...)
387 {
388         char str[256];
389         va_list ap;
390
391         if (!callbacks_pointer || !callbacks_pointer->debug)
392                 return;
393
394         va_start(ap, format);
395
396         if (vsnprintf(str, sizeof(str), format, ap) > 0)
397                 callbacks_pointer->debug(str);
398
399         va_end(ap);
400 }
401
402 #define SUPPLICANT_DBG(fmt, arg...) \
403         debug("%s:%s() " fmt, __FILE__, __FUNCTION__ , ## arg);
404
405 static GSupplicantMode string2mode(const char *mode)
406 {
407         if (!mode)
408                 return G_SUPPLICANT_MODE_UNKNOWN;
409
410         if (g_str_equal(mode, "infrastructure"))
411                 return G_SUPPLICANT_MODE_INFRA;
412         else if (g_str_equal(mode, "ad-hoc"))
413                 return G_SUPPLICANT_MODE_IBSS;
414 #if defined TIZEN_EXT_WIFI_MESH
415         else if (g_str_equal(mode, "mesh"))
416                 return G_SUPPLICANT_MODE_MESH;
417 #endif
418
419         return G_SUPPLICANT_MODE_UNKNOWN;
420 }
421
422 static const char *mode2string(GSupplicantMode mode)
423 {
424         switch (mode) {
425         case G_SUPPLICANT_MODE_UNKNOWN:
426                 break;
427         case G_SUPPLICANT_MODE_INFRA:
428                 return "managed";
429         case G_SUPPLICANT_MODE_IBSS:
430                 return "adhoc";
431         case G_SUPPLICANT_MODE_MASTER:
432                 return "ap";
433 #if defined TIZEN_EXT_WIFI_MESH
434         case G_SUPPLICANT_MODE_MESH:
435                 return "mesh";
436 #endif
437         }
438
439         return NULL;
440 }
441
442 static const char *security2string(GSupplicantSecurity security)
443 {
444         switch (security) {
445         case G_SUPPLICANT_SECURITY_UNKNOWN:
446                 break;
447         case G_SUPPLICANT_SECURITY_NONE:
448                 return "none";
449         case G_SUPPLICANT_SECURITY_WEP:
450                 return "wep";
451         case G_SUPPLICANT_SECURITY_PSK:
452                 return "psk";
453         case G_SUPPLICANT_SECURITY_IEEE8021X:
454                 return "ieee8021x";
455 #if defined TIZEN_EXT
456         case G_SUPPLICANT_SECURITY_FT_PSK:
457                 return "ft_psk";
458         case G_SUPPLICANT_SECURITY_FT_IEEE8021X:
459                 return "ft_ieee8021x";
460         case G_SUPPLICANT_SECURITY_SAE:
461                 return "sae";
462         case G_SUPPLICANT_SECURITY_OWE:
463                 return "owe";
464 #endif
465         }
466
467         return NULL;
468 }
469
470 static GSupplicantState string2state(const char *state)
471 {
472         if (!state)
473                 return G_SUPPLICANT_STATE_UNKNOWN;
474
475         if (g_str_equal(state, "unknown"))
476                 return G_SUPPLICANT_STATE_UNKNOWN;
477         else if (g_str_equal(state, "interface_disabled"))
478                 return G_SUPPLICANT_STATE_DISABLED;
479         else if (g_str_equal(state, "disconnected"))
480                 return G_SUPPLICANT_STATE_DISCONNECTED;
481         else if (g_str_equal(state, "inactive"))
482                 return G_SUPPLICANT_STATE_INACTIVE;
483         else if (g_str_equal(state, "scanning"))
484                 return G_SUPPLICANT_STATE_SCANNING;
485         else if (g_str_equal(state, "authenticating"))
486                 return G_SUPPLICANT_STATE_AUTHENTICATING;
487         else if (g_str_equal(state, "associating"))
488                 return G_SUPPLICANT_STATE_ASSOCIATING;
489         else if (g_str_equal(state, "associated"))
490                 return G_SUPPLICANT_STATE_ASSOCIATED;
491         else if (g_str_equal(state, "group_handshake"))
492                 return G_SUPPLICANT_STATE_GROUP_HANDSHAKE;
493         else if (g_str_equal(state, "4way_handshake"))
494                 return G_SUPPLICANT_STATE_4WAY_HANDSHAKE;
495         else if (g_str_equal(state, "completed"))
496                 return G_SUPPLICANT_STATE_COMPLETED;
497
498         return G_SUPPLICANT_STATE_UNKNOWN;
499 }
500
501 static bool compare_network_parameters(GSupplicantInterface *interface,
502                                 GSupplicantSSID *ssid)
503 {
504 #if defined TIZEN_EXT
505         if (!interface->network_info.ssid)
506                 return FALSE;
507 #endif
508
509         if (memcmp(interface->network_info.ssid, ssid->ssid, ssid->ssid_len))
510                 return FALSE;
511
512         if (interface->network_info.security != ssid->security)
513                 return FALSE;
514
515         if (interface->network_info.passphrase &&
516                         g_strcmp0(interface->network_info.passphrase,
517                                 ssid->passphrase) != 0) {
518                 return FALSE;
519         }
520
521         if (interface->network_info.private_passphrase &&
522                         g_strcmp0(interface->network_info.private_passphrase,
523                                 ssid->private_key_passphrase) != 0) {
524                 return FALSE;
525         }
526
527         return TRUE;
528 }
529
530 static void remove_network_information(GSupplicantInterface * interface)
531 {
532         g_free(interface->network_info.ssid);
533         g_free(interface->network_info.passphrase);
534         g_free(interface->network_info.private_passphrase);
535         interface->network_info.ssid = NULL;
536         interface->network_info.passphrase = NULL;
537         interface->network_info.private_passphrase = NULL;
538 }
539
540 static int store_network_information(GSupplicantInterface * interface,
541                                 GSupplicantSSID *ssid)
542 {
543         interface->network_info.ssid = g_malloc(ssid->ssid_len + 1);
544         if (interface->network_info.ssid != NULL) {
545                 memcpy(interface->network_info.ssid, ssid->ssid,
546                         ssid->ssid_len);
547                 interface->network_info.ssid[ssid->ssid_len] = '\0';
548         } else {
549                 return -ENOMEM;
550         }
551
552         interface->network_info.security = ssid->security;
553
554         if ((ssid->security == G_SUPPLICANT_SECURITY_WEP ||
555                 ssid->security == G_SUPPLICANT_SECURITY_PSK ||
556                 ssid->security == G_SUPPLICANT_SECURITY_NONE) &&
557                 ssid->passphrase) {
558                 interface->network_info.passphrase = g_strdup(ssid->passphrase);
559         }
560
561         if (ssid->security == G_SUPPLICANT_SECURITY_IEEE8021X &&
562                         ssid->private_key_passphrase) {
563                 interface->network_info.private_passphrase =
564                         g_strdup(ssid->private_key_passphrase);
565         }
566
567         return 0;
568 }
569
570 static void callback_system_ready(void)
571 {
572         if (system_ready)
573                 return;
574
575         system_ready = TRUE;
576
577         if (!callbacks_pointer)
578                 return;
579
580         if (!callbacks_pointer->system_ready)
581                 return;
582
583         callbacks_pointer->system_ready();
584 }
585
586 static void callback_system_killed(void)
587 {
588         system_ready = FALSE;
589
590         if (!callbacks_pointer)
591                 return;
592
593         if (!callbacks_pointer->system_killed)
594                 return;
595
596         callbacks_pointer->system_killed();
597 }
598
599 static void callback_interface_added(GSupplicantInterface *interface)
600 {
601         SUPPLICANT_DBG("");
602
603         if (!callbacks_pointer)
604                 return;
605
606         if (!callbacks_pointer->interface_added)
607                 return;
608
609         callbacks_pointer->interface_added(interface);
610 }
611
612 static void callback_interface_state(GSupplicantInterface *interface)
613 {
614         if (!callbacks_pointer)
615                 return;
616
617         if (!callbacks_pointer->interface_state)
618                 return;
619
620         callbacks_pointer->interface_state(interface);
621 }
622
623 static void callback_interface_removed(GSupplicantInterface *interface)
624 {
625         if (!callbacks_pointer)
626                 return;
627
628         if (!callbacks_pointer->interface_removed)
629                 return;
630
631         callbacks_pointer->interface_removed(interface);
632 }
633
634 #if !defined TIZEN_EXT
635 static void callback_p2p_support(GSupplicantInterface *interface)
636 {
637         SUPPLICANT_DBG("");
638
639         if (!interface->p2p_support)
640                 return;
641
642         if (callbacks_pointer && callbacks_pointer->p2p_support)
643                 callbacks_pointer->p2p_support(interface);
644 }
645 #endif
646
647 #if defined TIZEN_EXT_WIFI_MESH
648 static void callback_mesh_support(GSupplicantInterface *interface)
649 {
650         SUPPLICANT_DBG("");
651
652         if (!interface->mesh_support)
653                 return;
654
655         if (callbacks_pointer && callbacks_pointer->mesh_support)
656                 callbacks_pointer->mesh_support(interface);
657 }
658
659 bool g_supplicant_interface_has_mesh(GSupplicantInterface *interface)
660 {
661         if (!interface)
662                 return false;
663
664         return interface->mesh_support;
665 }
666 #endif
667
668 static void callback_scan_started(GSupplicantInterface *interface)
669 {
670         if (!callbacks_pointer)
671                 return;
672
673         if (!callbacks_pointer->scan_started)
674                 return;
675
676         callbacks_pointer->scan_started(interface);
677 }
678
679 static void callback_ap_create_fail(GSupplicantInterface *interface)
680 {
681         if (!callbacks_pointer)
682                 return;
683
684         if (!callbacks_pointer->ap_create_fail)
685                 return;
686
687         callbacks_pointer->ap_create_fail(interface);
688 }
689
690 static void callback_scan_finished(GSupplicantInterface *interface)
691 {
692         if (!callbacks_pointer)
693                 return;
694
695         if (!callbacks_pointer->scan_finished)
696                 return;
697
698         callbacks_pointer->scan_finished(interface);
699 }
700
701 static void callback_network_added(GSupplicantNetwork *network)
702 {
703         if (!callbacks_pointer)
704                 return;
705
706         if (!callbacks_pointer->network_added)
707                 return;
708
709         callbacks_pointer->network_added(network);
710 }
711
712 static void callback_network_removed(GSupplicantNetwork *network)
713 {
714         if (!callbacks_pointer)
715                 return;
716
717         if (!callbacks_pointer->network_removed)
718                 return;
719
720         callbacks_pointer->network_removed(network);
721 }
722
723 #if defined TIZEN_EXT
724 static void callback_network_merged(GSupplicantNetwork *network)
725 {
726         if (!callbacks_pointer)
727                 return;
728
729         if (!callbacks_pointer->network_merged)
730                 return;
731
732         callbacks_pointer->network_merged(network);
733 }
734
735 static void callback_assoc_failed(void *user_data)
736 {
737         if (!callbacks_pointer)
738                 return;
739
740         if (!callbacks_pointer->assoc_failed)
741                 return;
742
743         callbacks_pointer->assoc_failed(user_data);
744 }
745 #endif
746
747 static void callback_network_changed(GSupplicantNetwork *network,
748                                         const char *property)
749 {
750         if (!callbacks_pointer)
751                 return;
752
753         if (!callbacks_pointer->network_changed)
754                 return;
755
756         callbacks_pointer->network_changed(network, property);
757 }
758
759 static void callback_network_associated(GSupplicantNetwork *network)
760 {
761         if (!callbacks_pointer)
762                 return;
763
764         if (!callbacks_pointer->network_associated)
765                 return;
766
767         callbacks_pointer->network_associated(network);
768 }
769
770 static void callback_sta_authorized(GSupplicantInterface *interface,
771                                         const char *addr)
772 {
773         if (!callbacks_pointer)
774                 return;
775
776         if (!callbacks_pointer->sta_authorized)
777                 return;
778
779         callbacks_pointer->sta_authorized(interface, addr);
780 }
781
782 static void callback_sta_deauthorized(GSupplicantInterface *interface,
783                                         const char *addr)
784 {
785         if (!callbacks_pointer)
786                 return;
787
788         if (!callbacks_pointer->sta_deauthorized)
789                 return;
790
791         callbacks_pointer->sta_deauthorized(interface, addr);
792 }
793
794 static void callback_peer_found(GSupplicantPeer *peer)
795 {
796         if (!callbacks_pointer)
797                 return;
798
799         if (!callbacks_pointer->peer_found)
800                 return;
801
802         callbacks_pointer->peer_found(peer);
803 }
804
805 static void callback_peer_lost(GSupplicantPeer *peer)
806 {
807         if (!callbacks_pointer)
808                 return;
809
810         if (!callbacks_pointer->peer_lost)
811                 return;
812
813         callbacks_pointer->peer_lost(peer);
814 }
815
816 static void callback_peer_changed(GSupplicantPeer *peer,
817                                                 GSupplicantPeerState state)
818 {
819         if (!callbacks_pointer)
820                 return;
821
822         if (!callbacks_pointer->peer_changed)
823                 return;
824
825         callbacks_pointer->peer_changed(peer, state);
826 }
827
828 static void callback_peer_request(GSupplicantPeer *peer)
829 {
830         if (!callbacks_pointer)
831                 return;
832
833         if (!callbacks_pointer->peer_request)
834                 return;
835
836         peer->connection_requested = true;
837
838         callbacks_pointer->peer_request(peer);
839 }
840
841 static void callback_disconnect_reason_code(GSupplicantInterface *interface,
842                                         int reason_code)
843 {
844         if (!callbacks_pointer)
845                 return;
846
847         if (!callbacks_pointer->disconnect_reasoncode)
848                 return;
849
850         if (reason_code != 0)
851                 callbacks_pointer->disconnect_reasoncode(interface,
852                                                         reason_code);
853 }
854
855 static void callback_assoc_status_code(GSupplicantInterface *interface,
856                                 int status_code)
857 {
858         if (!callbacks_pointer)
859                 return;
860
861         if (!callbacks_pointer->assoc_status_code)
862                 return;
863
864         callbacks_pointer->assoc_status_code(interface, status_code);
865
866 }
867
868 static void remove_group(gpointer data)
869 {
870         GSupplicantGroup *group = data;
871
872         if (group->members)
873                 g_slist_free_full(group->members, g_free);
874
875         g_free(group->path);
876         g_free(group);
877 }
878
879 static void remove_interface(gpointer data)
880 {
881         GSupplicantInterface *interface = data;
882
883         g_hash_table_destroy(interface->bss_mapping);
884         g_hash_table_destroy(interface->network_table);
885         g_hash_table_destroy(interface->peer_table);
886         g_hash_table_destroy(interface->group_table);
887
888         if (interface->scan_callback) {
889                 SUPPLICANT_DBG("call interface %p callback %p scanning %d",
890                                 interface, interface->scan_callback,
891                                 interface->scanning);
892
893                 interface->scan_callback(-EIO, interface, interface->scan_data);
894                 interface->scan_callback = NULL;
895                 interface->scan_data = NULL;
896
897                 if (interface->scanning) {
898                         interface->scanning = FALSE;
899                         callback_scan_finished(interface);
900                 }
901         }
902
903         callback_interface_removed(interface);
904
905         g_free(interface->wps_cred.key);
906         g_free(interface->path);
907         g_free(interface->network_path);
908 #if defined TIZEN_EXT
909         interface->network_path = NULL;
910 #endif
911         g_free(interface->ifname);
912         g_free(interface->driver);
913         g_free(interface->bridge);
914         remove_network_information(interface);
915         g_free(interface);
916 }
917
918 static void remove_network(gpointer data)
919 {
920         GSupplicantNetwork *network = data;
921
922         g_hash_table_destroy(network->bss_table);
923
924         callback_network_removed(network);
925
926         g_hash_table_destroy(network->config_table);
927
928         g_free(network->path);
929         g_free(network->group);
930         g_free(network->name);
931 #if defined TIZEN_EXT
932         g_free(network->eap);
933         g_free(network->identity);
934         g_free(network->phase2);
935 #endif
936 #if defined TIZEN_EXT
937         g_slist_free_full(network->vsie_list, g_free);
938 #endif
939
940         g_free(network);
941 }
942
943 static void remove_bss(gpointer data)
944 {
945         struct g_supplicant_bss *bss = data;
946
947         supplicant_dbus_property_call_cancel_all(bss);
948
949         g_free(bss->path);
950 #if defined TIZEN_EXT
951         g_slist_free_full(bss->vsie_list, g_free);
952 #endif
953         g_free(bss);
954 }
955
956 static void remove_peer(gpointer data)
957 {
958         GSupplicantPeer *peer = data;
959
960         callback_peer_lost(peer);
961
962         if (peer->groups)
963                 g_slist_free_full(peer->groups, g_free);
964
965         if (peer_mapping)
966                 g_hash_table_remove(peer_mapping, peer->path);
967
968         if (pending_peer_connection)
969                 g_hash_table_remove(pending_peer_connection, peer->path);
970
971         g_free(peer->path);
972         g_free(peer->name);
973         g_free(peer->identifier);
974         g_free(peer->widi_ies);
975
976         g_free(peer);
977 }
978
979 static void debug_strvalmap(const char *label, struct strvalmap *map,
980                                                         unsigned int val)
981 {
982         int i;
983
984         for (i = 0; map[i].str; i++) {
985                 if (val & map[i].val)
986                         SUPPLICANT_DBG("%s: %s", label, map[i].str);
987         }
988 }
989
990 static void interface_capability_keymgmt(DBusMessageIter *iter, void *user_data)
991 {
992         GSupplicantInterface *interface = user_data;
993         const char *str = NULL;
994         int i;
995
996         dbus_message_iter_get_basic(iter, &str);
997         if (!str)
998                 return;
999
1000         for (i = 0; keymgmt_map[i].str; i++)
1001                 if (strcmp(str, keymgmt_map[i].str) == 0) {
1002                         interface->keymgmt_capa |= keymgmt_map[i].val;
1003                         break;
1004                 }
1005 }
1006
1007 static void interface_capability_authalg(DBusMessageIter *iter, void *user_data)
1008 {
1009         GSupplicantInterface *interface = user_data;
1010         const char *str = NULL;
1011         int i;
1012
1013         dbus_message_iter_get_basic(iter, &str);
1014         if (!str)
1015                 return;
1016
1017         for (i = 0; authalg_capa_map[i].str; i++)
1018                 if (strcmp(str, authalg_capa_map[i].str) == 0) {
1019                         interface->authalg_capa |= authalg_capa_map[i].val;
1020                         break;
1021                 }
1022 }
1023
1024 static void interface_capability_proto(DBusMessageIter *iter, void *user_data)
1025 {
1026         GSupplicantInterface *interface = user_data;
1027         const char *str = NULL;
1028         int i;
1029
1030         dbus_message_iter_get_basic(iter, &str);
1031         if (!str)
1032                 return;
1033
1034         for (i = 0; proto_capa_map[i].str; i++)
1035                 if (strcmp(str, proto_capa_map[i].str) == 0) {
1036                         interface->proto_capa |= proto_capa_map[i].val;
1037                         break;
1038                 }
1039 }
1040
1041 static void interface_capability_pairwise(DBusMessageIter *iter,
1042                                                         void *user_data)
1043 {
1044         GSupplicantInterface *interface = user_data;
1045         const char *str = NULL;
1046         int i;
1047
1048         dbus_message_iter_get_basic(iter, &str);
1049         if (!str)
1050                 return;
1051
1052         for (i = 0; pairwise_map[i].str; i++)
1053                 if (strcmp(str, pairwise_map[i].str) == 0) {
1054                         interface->pairwise_capa |= pairwise_map[i].val;
1055                         break;
1056                 }
1057 }
1058
1059 static void interface_capability_group(DBusMessageIter *iter, void *user_data)
1060 {
1061         GSupplicantInterface *interface = user_data;
1062         const char *str = NULL;
1063         int i;
1064
1065         dbus_message_iter_get_basic(iter, &str);
1066         if (!str)
1067                 return;
1068
1069         for (i = 0; group_map[i].str; i++)
1070                 if (strcmp(str, group_map[i].str) == 0) {
1071                         interface->group_capa |= group_map[i].val;
1072                         break;
1073                 }
1074 }
1075
1076 static void interface_capability_scan(DBusMessageIter *iter, void *user_data)
1077 {
1078         GSupplicantInterface *interface = user_data;
1079         const char *str = NULL;
1080         int i;
1081
1082         dbus_message_iter_get_basic(iter, &str);
1083         if (!str)
1084                 return;
1085
1086         for (i = 0; scan_capa_map[i].str; i++)
1087                 if (strcmp(str, scan_capa_map[i].str) == 0) {
1088                         interface->scan_capa |= scan_capa_map[i].val;
1089                         break;
1090                 }
1091 }
1092
1093 static void interface_capability_mode(DBusMessageIter *iter, void *user_data)
1094 {
1095         GSupplicantInterface *interface = user_data;
1096         const char *str = NULL;
1097         int i;
1098
1099         dbus_message_iter_get_basic(iter, &str);
1100         if (!str)
1101                 return;
1102
1103         for (i = 0; mode_capa_map[i].str; i++)
1104                 if (strcmp(str, mode_capa_map[i].str) == 0) {
1105                         interface->mode_capa |= mode_capa_map[i].val;
1106                         break;
1107                 }
1108 }
1109
1110 static void interface_capability(const char *key, DBusMessageIter *iter,
1111                                                         void *user_data)
1112 {
1113         GSupplicantInterface *interface = user_data;
1114
1115         if (!key)
1116                 return;
1117
1118         if (g_strcmp0(key, "KeyMgmt") == 0)
1119                 supplicant_dbus_array_foreach(iter,
1120                                 interface_capability_keymgmt, interface);
1121         else if (g_strcmp0(key, "AuthAlg") == 0)
1122                 supplicant_dbus_array_foreach(iter,
1123                                 interface_capability_authalg, interface);
1124         else if (g_strcmp0(key, "Protocol") == 0)
1125                 supplicant_dbus_array_foreach(iter,
1126                                 interface_capability_proto, interface);
1127         else if (g_strcmp0(key, "Pairwise") == 0)
1128                 supplicant_dbus_array_foreach(iter,
1129                                 interface_capability_pairwise, interface);
1130         else if (g_strcmp0(key, "Group") == 0)
1131                 supplicant_dbus_array_foreach(iter,
1132                                 interface_capability_group, interface);
1133         else if (g_strcmp0(key, "Scan") == 0)
1134                 supplicant_dbus_array_foreach(iter,
1135                                 interface_capability_scan, interface);
1136         else if (g_strcmp0(key, "Modes") == 0)
1137                 supplicant_dbus_array_foreach(iter,
1138                                 interface_capability_mode, interface);
1139         else if (g_strcmp0(key, "MaxScanSSID") == 0) {
1140                 dbus_int32_t max_scan_ssid;
1141
1142                 dbus_message_iter_get_basic(iter, &max_scan_ssid);
1143                 if (max_scan_ssid < 2)
1144                         max_scan_ssid = 0;
1145                 interface->max_scan_ssids = max_scan_ssid;
1146 #if defined TIZEN_EXT
1147         } else if (g_strcmp0(key, "Is5GhzSupported") == 0) {
1148                 dbus_bool_t is_5_0_Ghz_supported;
1149
1150                 dbus_message_iter_get_basic(iter, &is_5_0_Ghz_supported);
1151                 interface->is_5_0_Ghz_supported = is_5_0_Ghz_supported;
1152 #endif
1153         } else
1154                 SUPPLICANT_DBG("key %s type %c",
1155                                 key, dbus_message_iter_get_arg_type(iter));
1156 }
1157
1158 struct set_apscan_data
1159 {
1160         unsigned int ap_scan;
1161         GSupplicantInterface *interface;
1162 };
1163
1164 static void set_apscan(DBusMessageIter *iter, void *user_data)
1165 {
1166         struct set_apscan_data *data = user_data;
1167         unsigned int ap_scan = data->ap_scan;
1168
1169         dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &ap_scan);
1170 }
1171
1172 static void set_apscan_complete(const char *error,
1173                 DBusMessageIter *iter, void *user_data)
1174 {
1175         struct set_apscan_data *data = user_data;
1176         GSupplicantInterface *interface = data->interface;
1177
1178         if (error) {
1179                 interface->ap_create_in_progress = false;
1180                 SUPPLICANT_DBG("Set AP scan error %s", error);
1181                 goto error;
1182         }
1183
1184         interface->ap_create_in_progress = true;
1185 error:
1186         dbus_free(data);
1187 }
1188
1189 int g_supplicant_interface_set_apscan(GSupplicantInterface *interface,
1190                                                         unsigned int ap_scan)
1191 {
1192         struct set_apscan_data *data;
1193         int ret;
1194
1195         data = dbus_malloc0(sizeof(*data));
1196
1197         if (!data)
1198                 return -ENOMEM;
1199
1200         data->ap_scan = ap_scan;
1201         data->interface = interface;
1202
1203         ret = supplicant_dbus_property_set(interface->path,
1204                         SUPPLICANT_INTERFACE ".Interface",
1205                         "ApScan", DBUS_TYPE_UINT32_AS_STRING,
1206                         set_apscan, set_apscan_complete, data, NULL);
1207         if (ret < 0)
1208                 dbus_free(data);
1209
1210         return ret;
1211 }
1212
1213 void g_supplicant_interface_set_data(GSupplicantInterface *interface,
1214                                                                 void *data)
1215 {
1216         if (!interface)
1217                 return;
1218
1219         interface->data = data;
1220
1221         if (!data)
1222                 interface->scan_callback = NULL;
1223 }
1224
1225 void *g_supplicant_interface_get_data(GSupplicantInterface *interface)
1226 {
1227         if (!interface)
1228                 return NULL;
1229
1230         return interface->data;
1231 }
1232
1233 const char *g_supplicant_interface_get_ifname(GSupplicantInterface *interface)
1234 {
1235         if (!interface)
1236                 return NULL;
1237
1238         return interface->ifname;
1239 }
1240
1241 #if defined TIZEN_EXT
1242 bool g_supplicant_interface_get_is_5_0_ghz_supported(GSupplicantInterface *interface)
1243 {
1244         if (!interface)
1245                 return NULL;
1246
1247         return interface->is_5_0_Ghz_supported;
1248 }
1249 #endif
1250
1251 const char *g_supplicant_interface_get_driver(GSupplicantInterface *interface)
1252 {
1253         if (!interface)
1254                 return NULL;
1255
1256         return interface->driver;
1257 }
1258
1259 GSupplicantState g_supplicant_interface_get_state(
1260                                         GSupplicantInterface *interface)
1261 {
1262         if (!interface)
1263                 return G_SUPPLICANT_STATE_UNKNOWN;
1264
1265         return interface->state;
1266 }
1267
1268 const char *g_supplicant_interface_get_wps_key(GSupplicantInterface *interface)
1269 {
1270         if (!interface)
1271                 return NULL;
1272
1273         return (const char *)interface->wps_cred.key;
1274 }
1275
1276 const void *g_supplicant_interface_get_wps_ssid(GSupplicantInterface *interface,
1277                                                         unsigned int *ssid_len)
1278 {
1279         if (!ssid_len)
1280                 return NULL;
1281
1282         if (!interface || interface->wps_cred.ssid_len == 0) {
1283                 *ssid_len = 0;
1284                 return NULL;
1285         }
1286
1287         *ssid_len = interface->wps_cred.ssid_len;
1288         return interface->wps_cred.ssid;
1289 }
1290
1291 GSupplicantWpsState g_supplicant_interface_get_wps_state(
1292                                         GSupplicantInterface *interface)
1293 {
1294         if (!interface)
1295                 return G_SUPPLICANT_WPS_STATE_UNKNOWN;
1296
1297         return interface->wps_state;
1298 }
1299
1300 unsigned int g_supplicant_interface_get_mode(GSupplicantInterface *interface)
1301 {
1302         if (!interface)
1303                 return 0;
1304
1305         return interface->mode_capa;
1306 }
1307
1308 unsigned int g_supplicant_interface_get_max_scan_ssids(
1309                                 GSupplicantInterface *interface)
1310 {
1311         if (!interface)
1312                 return 0;
1313
1314         if (interface->max_scan_ssids == 0)
1315                 return WPAS_MAX_SCAN_SSIDS;
1316
1317         return interface->max_scan_ssids;
1318 }
1319
1320 static void set_network_enabled(DBusMessageIter *iter, void *user_data)
1321 {
1322         dbus_bool_t enable = *(dbus_bool_t *)user_data;
1323
1324         dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &enable);
1325 }
1326
1327 int g_supplicant_interface_enable_selected_network(GSupplicantInterface *interface,
1328                                                         dbus_bool_t enable)
1329 {
1330         if (!interface)
1331                 return -1;
1332
1333         if (!interface->network_path)
1334                 return -1;
1335
1336         SUPPLICANT_DBG(" ");
1337         return supplicant_dbus_property_set(interface->network_path,
1338                                 SUPPLICANT_INTERFACE ".Network",
1339                                 "Enabled", DBUS_TYPE_BOOLEAN_AS_STRING,
1340                                 set_network_enabled, NULL, &enable, NULL);
1341 }
1342
1343 dbus_bool_t g_supplicant_interface_get_ready(GSupplicantInterface *interface)
1344 {
1345         if (!interface)
1346                 return FALSE;
1347
1348         return interface->ready;
1349 }
1350
1351 GSupplicantInterface *g_supplicant_network_get_interface(
1352                                         GSupplicantNetwork *network)
1353 {
1354         if (!network)
1355                 return NULL;
1356
1357         return network->interface;
1358 }
1359
1360 const char *g_supplicant_network_get_name(GSupplicantNetwork *network)
1361 {
1362         if (!network || !network->name)
1363                 return "";
1364
1365         return network->name;
1366 }
1367
1368 const char *g_supplicant_network_get_identifier(GSupplicantNetwork *network)
1369 {
1370         if (!network || !network->group)
1371                 return "";
1372
1373         return network->group;
1374 }
1375
1376 const char *g_supplicant_network_get_path(GSupplicantNetwork *network)
1377 {
1378         if (!network || !network->path)
1379                 return NULL;
1380
1381         return network->path;
1382 }
1383
1384 const char *g_supplicant_network_get_mode(GSupplicantNetwork *network)
1385 {
1386         if (!network)
1387                 return G_SUPPLICANT_MODE_UNKNOWN;
1388
1389         return mode2string(network->mode);
1390 }
1391
1392 const char *g_supplicant_network_get_security(GSupplicantNetwork *network)
1393 {
1394         if (!network)
1395                 return G_SUPPLICANT_SECURITY_UNKNOWN;
1396
1397         return security2string(network->security);
1398 }
1399
1400 const void *g_supplicant_network_get_ssid(GSupplicantNetwork *network,
1401                                                 unsigned int *ssid_len)
1402 {
1403         if (!network) {
1404                 *ssid_len = 0;
1405                 return NULL;
1406         }
1407
1408         *ssid_len = network->ssid_len;
1409         return network->ssid;
1410 }
1411
1412 dbus_int16_t g_supplicant_network_get_signal(GSupplicantNetwork *network)
1413 {
1414         if (!network)
1415                 return 0;
1416
1417         return network->signal;
1418 }
1419
1420 dbus_uint16_t g_supplicant_network_get_frequency(GSupplicantNetwork *network)
1421 {
1422         if (!network)
1423                 return 0;
1424
1425         return network->frequency;
1426 }
1427
1428 dbus_bool_t g_supplicant_network_get_wps(GSupplicantNetwork *network)
1429 {
1430         if (!network)
1431                 return FALSE;
1432
1433         return network->wps;
1434 }
1435
1436 dbus_bool_t g_supplicant_network_is_wps_active(GSupplicantNetwork *network)
1437 {
1438         if (!network)
1439                 return FALSE;
1440
1441         if (network->wps_capabilities & G_SUPPLICANT_WPS_CONFIGURED)
1442                 return TRUE;
1443
1444         return FALSE;
1445 }
1446
1447 dbus_bool_t g_supplicant_network_is_wps_pbc(GSupplicantNetwork *network)
1448 {
1449         if (!network)
1450                 return FALSE;
1451
1452         if (network->wps_capabilities & G_SUPPLICANT_WPS_PBC)
1453                 return TRUE;
1454
1455         return FALSE;
1456 }
1457
1458 dbus_bool_t g_supplicant_network_is_wps_advertizing(GSupplicantNetwork *network)
1459 {
1460         if (!network)
1461                 return FALSE;
1462
1463         if (network->wps_capabilities & G_SUPPLICANT_WPS_REGISTRAR)
1464                 return TRUE;
1465
1466         return FALSE;
1467 }
1468
1469 #ifdef TIZEN_EXT
1470 GSupplicantPhy_mode g_supplicant_network_get_phy_mode(GSupplicantNetwork *network)
1471 {
1472         if (!network)
1473                 return G_SUPPLICANT_MODE_IEEE80211_UNKNOWN;
1474
1475         return network->phy_mode;
1476 }
1477 #endif
1478
1479 GSupplicantInterface *g_supplicant_peer_get_interface(GSupplicantPeer *peer)
1480 {
1481         if (!peer)
1482                 return NULL;
1483
1484         return peer->interface;
1485 }
1486
1487 const char *g_supplicant_peer_get_path(GSupplicantPeer *peer)
1488 {
1489         if (!peer)
1490                 return NULL;
1491
1492         return peer->path;
1493 }
1494
1495 const char *g_supplicant_peer_get_identifier(GSupplicantPeer *peer)
1496 {
1497         if (!peer)
1498                 return NULL;
1499
1500         return peer->identifier;
1501 }
1502
1503 const void *g_supplicant_peer_get_device_address(GSupplicantPeer *peer)
1504 {
1505         if (!peer)
1506                 return NULL;
1507
1508         return peer->device_address;
1509 }
1510
1511 const void *g_supplicant_peer_get_iface_address(GSupplicantPeer *peer)
1512 {
1513         if (!peer)
1514                 return NULL;
1515
1516         return peer->iface_address;
1517 }
1518
1519 const char *g_supplicant_peer_get_name(GSupplicantPeer *peer)
1520 {
1521         if (!peer)
1522                 return NULL;
1523
1524         return peer->name;
1525 }
1526
1527 #if defined TIZEN_EXT
1528 bool g_supplicant_network_is_hs20AP(GSupplicantNetwork *network)
1529 {
1530         if (!network)
1531                 return 0;
1532
1533         return network->isHS20AP;
1534 }
1535
1536 const char *g_supplicant_network_get_eap(GSupplicantNetwork *network)
1537 {
1538         if (!network || !network->eap)
1539                 return NULL;
1540
1541         return network->eap;
1542 }
1543
1544 const char *g_supplicant_network_get_identity(GSupplicantNetwork *network)
1545 {
1546         if (!network || !network->identity)
1547                 return NULL;
1548
1549         return network->identity;
1550 }
1551
1552 const char *g_supplicant_network_get_phase2(GSupplicantNetwork *network)
1553 {
1554         if (!network || !network->phase2)
1555                 return NULL;
1556
1557         return network->phase2;
1558 }
1559
1560 unsigned int g_supplicant_network_get_keymgmt(GSupplicantNetwork *network)
1561 {
1562         if (network == NULL)
1563                 return 0;
1564
1565         return network->keymgmt;
1566 }
1567
1568 const unsigned char *g_supplicant_network_get_countrycode(GSupplicantNetwork
1569                                                           *network)
1570 {
1571         if (!network)
1572                 return NULL;
1573
1574         return network->country_code;
1575 }
1576 #endif
1577
1578 const unsigned char *g_supplicant_peer_get_widi_ies(GSupplicantPeer *peer,
1579                                                                 int *length)
1580 {
1581         if (!peer || !length)
1582                 return NULL;
1583
1584         *length = peer->widi_ies_length;
1585         return peer->widi_ies;
1586 }
1587
1588 bool g_supplicant_peer_is_wps_pbc(GSupplicantPeer *peer)
1589 {
1590         if (!peer)
1591                 return false;
1592
1593         if (peer->wps_capabilities & G_SUPPLICANT_WPS_PBC)
1594                 return true;
1595
1596         return false;
1597 }
1598
1599 bool g_supplicant_peer_is_wps_pin(GSupplicantPeer *peer)
1600 {
1601         if (!peer)
1602                 return false;
1603
1604         if (peer->wps_capabilities & G_SUPPLICANT_WPS_PIN)
1605                 return true;
1606
1607         return false;
1608 }
1609
1610 bool g_supplicant_peer_is_in_a_group(GSupplicantPeer *peer)
1611 {
1612         if (!peer || !peer->groups)
1613                 return false;
1614
1615         return true;
1616 }
1617
1618 GSupplicantInterface *g_supplicant_peer_get_group_interface(GSupplicantPeer *peer)
1619 {
1620         if (!peer)
1621                 return NULL;
1622
1623         return (GSupplicantInterface *) peer->current_group_iface;
1624 }
1625
1626 bool g_supplicant_peer_is_client(GSupplicantPeer *peer)
1627 {
1628         GSupplicantGroup *group;
1629         GSList *list;
1630
1631         if (!peer)
1632                 return false;
1633
1634         for (list = peer->groups; list; list = list->next) {
1635                 const char *path = list->data;
1636
1637                 group = g_hash_table_lookup(group_mapping, path);
1638                 if (!group)
1639                         continue;
1640
1641                 if (group->role != G_SUPPLICANT_GROUP_ROLE_CLIENT ||
1642                                 group->orig_interface != peer->interface)
1643                         continue;
1644
1645                 if (group->interface == peer->current_group_iface)
1646                         return true;
1647         }
1648
1649         return false;
1650 }
1651
1652 bool g_supplicant_peer_has_requested_connection(GSupplicantPeer *peer)
1653 {
1654         if (!peer)
1655                 return false;
1656
1657         return peer->connection_requested;
1658 }
1659
1660 #if defined TIZEN_EXT
1661 /*
1662  * Description: Network client requires additional wifi specific info
1663  */
1664 const unsigned char *g_supplicant_network_get_bssid(GSupplicantNetwork *network)
1665 {
1666         if (network == NULL || network->best_bss == NULL)
1667                 return NULL;
1668
1669         return (const unsigned char *)network->best_bss->bssid;
1670 }
1671
1672 unsigned int g_supplicant_network_get_maxrate(GSupplicantNetwork *network)
1673 {
1674         if (network == NULL || network->best_bss == NULL)
1675                 return 0;
1676
1677         return network->best_bss->maxrate;
1678 }
1679
1680 const char *g_supplicant_network_get_enc_mode(GSupplicantNetwork *network)
1681 {
1682         if (network == NULL || network->best_bss == NULL)
1683                 return NULL;
1684
1685         if (network->best_bss->security == G_SUPPLICANT_SECURITY_PSK ||
1686 #if defined TIZEN_EXT
1687             network->best_bss->security == G_SUPPLICANT_SECURITY_SAE ||
1688             network->best_bss->security == G_SUPPLICANT_SECURITY_OWE ||
1689 #endif /* TIZEN_EXT */
1690             network->best_bss->security == G_SUPPLICANT_SECURITY_IEEE8021X) {
1691                 unsigned int pairwise;
1692
1693                 pairwise = network->best_bss->rsn_pairwise |
1694                                 network->best_bss->wpa_pairwise;
1695
1696                 if ((pairwise & G_SUPPLICANT_PAIRWISE_CCMP) &&
1697                     (pairwise & G_SUPPLICANT_PAIRWISE_TKIP))
1698                         return "mixed";
1699                 else if (pairwise & G_SUPPLICANT_PAIRWISE_CCMP)
1700                         return "aes";
1701                 else if (pairwise & G_SUPPLICANT_PAIRWISE_TKIP)
1702                         return "tkip";
1703
1704         } else if (network->best_bss->security == G_SUPPLICANT_SECURITY_WEP)
1705                 return "wep";
1706         else if (network->best_bss->security == G_SUPPLICANT_SECURITY_NONE)
1707                 return "none";
1708
1709         return NULL;
1710 }
1711
1712 bool g_supplicant_network_get_rsn_mode(GSupplicantNetwork *network)
1713 {
1714         if (network == NULL || network->best_bss == NULL)
1715                 return 0;
1716
1717 #if defined TIZEN_EXT
1718         if (network->best_bss->security == G_SUPPLICANT_SECURITY_SAE ||
1719                         network->best_bss->security == G_SUPPLICANT_SECURITY_OWE)
1720                 return false;
1721 #endif /* TIZEN_EXT */
1722
1723         if (network->best_bss->rsn_selected) {
1724                 const char *mode = g_supplicant_network_get_enc_mode(network);
1725                 if (g_strcmp0(mode, "aes") == 0 ||
1726                                 g_strcmp0(mode, "mixed") == 0)
1727                         return true;
1728                 else
1729                         return false;
1730         } else
1731                 return false;
1732 }
1733
1734 void *g_supplicant_network_get_wifi_vsie(GSupplicantNetwork *network)
1735 {
1736         GSList *vsie_list = NULL;
1737
1738         if (!network)
1739                 return NULL;
1740
1741         if (g_slist_length(network->vsie_list) > 0) {
1742                 GSList *list = NULL;
1743                 unsigned char *vsie = NULL;
1744                 for (list = network->vsie_list; list; list = list->next) {
1745                         unsigned char *ie = (unsigned char *)list->data;
1746                         if (ie == NULL)
1747                                 continue;
1748                         vsie = (unsigned char *)g_try_malloc0(ie[1]+2); // tag number size(1), tag length size(1)
1749
1750                         if (vsie) {
1751                                 memcpy(vsie, ie, ie[1]+2);
1752                                 vsie_list = g_slist_append(vsie_list, vsie);
1753                         } else
1754                                 SUPPLICANT_DBG("Failed to allocate memory");
1755                 }
1756         }
1757
1758         return vsie_list;
1759 }
1760
1761 static void update_bssid_list(gpointer key, gpointer value, gpointer user_data)
1762 {
1763         struct g_supplicant_bss *bss = value;
1764         struct g_connman_bssids *bssids = NULL;
1765         GSList **list = (GSList **)user_data;
1766
1767         bssids = (struct g_connman_bssids *)g_try_malloc0(sizeof(struct g_connman_bssids));
1768
1769         if (bssids) {
1770                 memcpy(bssids->bssid, bss->bssid, WIFI_BSSID_LEN_MAX);
1771
1772                 bssids->strength = bss->signal;
1773                 bssids->strength += 120;
1774
1775                 if (bssids->strength > 100)
1776                         bssids->strength = 100;
1777
1778                 bssids->frequency = bss->frequency;
1779                 *list = g_slist_append(*list, bssids);
1780         } else
1781                 SUPPLICANT_DBG("Failed to allocate memory");
1782 }
1783
1784 static gint cmp_bss(gconstpointer a, gconstpointer b)
1785 {
1786         struct g_connman_bssids *entry_a = (struct g_connman_bssids *)a;
1787         struct g_connman_bssids *entry_b = (struct g_connman_bssids *)b;
1788
1789         if (entry_a->strength > entry_b->strength)
1790                 return -1;
1791
1792         if (entry_a->strength < entry_b->strength)
1793                 return 1;
1794
1795         return 0;
1796 }
1797
1798 void *g_supplicant_network_get_bssid_list(GSupplicantNetwork *network)
1799 {
1800         GSList *bssid_list = NULL;
1801
1802         if (g_hash_table_size(network->bss_table) < 1)
1803                 return NULL;
1804
1805         g_hash_table_foreach(network->bss_table, update_bssid_list, &bssid_list);
1806         bssid_list = g_slist_sort(bssid_list, cmp_bss);
1807
1808         return bssid_list;
1809 }
1810 #endif
1811
1812 static void merge_network(GSupplicantNetwork *network)
1813 {
1814         GString *str;
1815         const char *ssid, *mode, *key_mgmt;
1816 #if defined TIZEN_EXT
1817         const char *isHS20AP;
1818         const char *eap, *identity, *phase2;
1819 #endif
1820         unsigned int i, ssid_len;
1821         char *group;
1822
1823         ssid = g_hash_table_lookup(network->config_table, "ssid");
1824         mode = g_hash_table_lookup(network->config_table, "mode");
1825         key_mgmt = g_hash_table_lookup(network->config_table, "key_mgmt");
1826 #if defined TIZEN_EXT
1827         isHS20AP = g_hash_table_lookup(network->config_table, "isHS20AP");
1828         eap = g_hash_table_lookup(network->config_table, "eap");
1829         identity = g_hash_table_lookup(network->config_table, "identity");
1830         phase2 = g_hash_table_lookup(network->config_table, "phase2");
1831 #endif
1832
1833         SUPPLICANT_DBG("ssid %s mode %s", ssid, mode);
1834
1835         if (ssid)
1836                 ssid_len = strlen(ssid);
1837         else
1838                 ssid_len = 0;
1839
1840         str = g_string_sized_new((ssid_len * 2) + 24);
1841         if (!str)
1842                 return;
1843
1844         for (i = 0; i < ssid_len; i++)
1845 #if defined TIZEN_EXT
1846         {
1847                 if (ssid[i] != '"')
1848 #endif
1849                 g_string_append_printf(str, "%02x", ssid[i]);
1850 #if defined TIZEN_EXT
1851         }
1852 #endif
1853
1854         if (g_strcmp0(mode, "0") == 0)
1855                 g_string_append_printf(str, "_managed");
1856         else if (g_strcmp0(mode, "1") == 0)
1857                 g_string_append_printf(str, "_adhoc");
1858 #if defined TIZEN_EXT_WIFI_MESH
1859         else if (g_strcmp0(mode, "5") == 0)
1860                 g_string_append_printf(str, "_mesh");
1861 #endif
1862
1863         if (g_strcmp0(key_mgmt, "WPA-PSK") == 0)
1864                 g_string_append_printf(str, "_psk");
1865 #if defined TIZEN_EXT
1866         else if (g_strcmp0(key_mgmt, "WPA-EAP") == 0)
1867                 g_string_append_printf(str, "_ieee8021x");
1868         else
1869                 g_string_append_printf(str, "_none");
1870 #endif
1871
1872         group = g_string_free(str, FALSE);
1873
1874         SUPPLICANT_DBG("%s", group);
1875
1876 #if defined TIZEN_EXT
1877         if (g_strcmp0(isHS20AP, "1") == 0) {
1878                 network->isHS20AP = 1;
1879                 if (network->eap)
1880                         g_free(network->eap);
1881                 network->eap = g_strdup(eap);
1882
1883                 if (network->identity)
1884                         g_free(network->identity);
1885                 network->identity = g_strdup(identity);
1886
1887                 if (network->phase2)
1888                         g_free(network->phase2);
1889                 network->phase2 = g_strdup(phase2);
1890         } else
1891                 network->isHS20AP = 0;
1892
1893         network->group = g_strdup(group);
1894         callback_network_merged(network);
1895         g_free(network->group);
1896 #endif
1897
1898         g_free(group);
1899
1900         g_hash_table_destroy(network->config_table);
1901
1902         g_free(network->path);
1903         g_free(network);
1904 }
1905
1906 static void network_property(const char *key, DBusMessageIter *iter,
1907                                                         void *user_data)
1908 {
1909         GSupplicantNetwork *network = user_data;
1910
1911         if (!network->interface)
1912                 return;
1913
1914         if (!key) {
1915                 merge_network(network);
1916                 return;
1917         }
1918
1919         if (g_strcmp0(key, "Enabled") == 0) {
1920                 dbus_bool_t enabled = FALSE;
1921
1922                 dbus_message_iter_get_basic(iter, &enabled);
1923         } else if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
1924                 const char *str = NULL;
1925
1926                 dbus_message_iter_get_basic(iter, &str);
1927                 if (str) {
1928                         g_hash_table_replace(network->config_table,
1929                                                 g_strdup(key), g_strdup(str));
1930                 }
1931         } else
1932                 SUPPLICANT_DBG("key %s type %c",
1933                                 key, dbus_message_iter_get_arg_type(iter));
1934 }
1935
1936 static void interface_network_added(DBusMessageIter *iter, void *user_data)
1937 {
1938         GSupplicantInterface *interface = user_data;
1939         GSupplicantNetwork *network;
1940         const char *path = NULL;
1941
1942         SUPPLICANT_DBG("");
1943
1944         dbus_message_iter_get_basic(iter, &path);
1945         if (!path)
1946                 return;
1947
1948         if (g_strcmp0(path, "/") == 0)
1949                 return;
1950
1951         network = g_try_new0(GSupplicantNetwork, 1);
1952         if (!network)
1953                 return;
1954
1955         network->interface = interface;
1956         network->path = g_strdup(path);
1957
1958         network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1959                                                         g_free, g_free);
1960
1961         dbus_message_iter_next(iter);
1962         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1963                 supplicant_dbus_property_foreach(iter, network_property,
1964                                                                 network);
1965                 network_property(NULL, NULL, network);
1966                 return;
1967         }
1968
1969         supplicant_dbus_property_get_all(path,
1970                                 SUPPLICANT_INTERFACE ".Network",
1971                                         network_property, network, NULL);
1972 }
1973
1974 static void interface_network_removed(DBusMessageIter *iter, void *user_data)
1975 {
1976         SUPPLICANT_DBG("");
1977 }
1978
1979 static char *create_name(unsigned char *ssid, int ssid_len)
1980 {
1981         GString *string;
1982         const gchar *remainder, *invalid;
1983         int valid_bytes, remaining_bytes;
1984
1985         if (ssid_len < 1 || ssid[0] == '\0')
1986                 return g_strdup("");
1987
1988         string = NULL;
1989         remainder = (const gchar *)ssid;
1990         remaining_bytes = ssid_len;
1991
1992         while (remaining_bytes != 0) {
1993                 if (g_utf8_validate(remainder, remaining_bytes,
1994                                         &invalid)) {
1995                         break;
1996                 }
1997
1998                 valid_bytes = invalid - remainder;
1999
2000                 if (!string)
2001                         string = g_string_sized_new(remaining_bytes);
2002
2003                 g_string_append_len(string, remainder, valid_bytes);
2004
2005                 /* append U+FFFD REPLACEMENT CHARACTER */
2006                 g_string_append(string, "\357\277\275");
2007
2008                 remaining_bytes -= valid_bytes + 1;
2009                 remainder = invalid + 1;
2010         }
2011
2012         if (!string)
2013                 return g_strndup((const gchar *)ssid, ssid_len + 1);
2014
2015         g_string_append(string, remainder);
2016
2017         return g_string_free(string, FALSE);
2018 }
2019
2020 static char *create_group(struct g_supplicant_bss *bss)
2021 {
2022         GString *str;
2023         unsigned int i;
2024         const char *mode, *security;
2025
2026         str = g_string_sized_new((bss->ssid_len * 2) + 24);
2027         if (!str)
2028                 return NULL;
2029
2030         if (bss->ssid_len > 0 && bss->ssid[0] != '\0') {
2031                 for (i = 0; i < bss->ssid_len; i++)
2032                         g_string_append_printf(str, "%02x", bss->ssid[i]);
2033         } else
2034                 g_string_append_printf(str, "hidden");
2035
2036         mode = mode2string(bss->mode);
2037         if (mode)
2038                 g_string_append_printf(str, "_%s", mode);
2039
2040         security = security2string(bss->security);
2041         if (security)
2042                 g_string_append_printf(str, "_%s", security);
2043
2044         return g_string_free(str, FALSE);
2045 }
2046
2047 static int add_or_replace_bss_to_network(struct g_supplicant_bss *bss)
2048 {
2049         GSupplicantInterface *interface = bss->interface;
2050         GSupplicantNetwork *network;
2051         char *group;
2052         bool is_new_network;
2053
2054         group = create_group(bss);
2055         SUPPLICANT_DBG("New group created: %s", group);
2056
2057         if (!group)
2058                 return -ENOMEM;
2059
2060         network = g_hash_table_lookup(interface->network_table, group);
2061         if (network) {
2062                 g_free(group);
2063                 SUPPLICANT_DBG("Network %s already exist", network->name);
2064                 is_new_network = false;
2065
2066                 goto done;
2067         }
2068
2069         is_new_network = true;
2070
2071         network = g_try_new0(GSupplicantNetwork, 1);
2072         if (!network) {
2073                 g_free(group);
2074                 return -ENOMEM;
2075         }
2076
2077         network->interface = interface;
2078         if (!network->path)
2079                 network->path = g_strdup(bss->path);
2080         network->group = group;
2081         network->name = create_name(bss->ssid, bss->ssid_len);
2082         network->mode = bss->mode;
2083         network->security = bss->security;
2084         network->ssid_len = bss->ssid_len;
2085         memcpy(network->ssid, bss->ssid, bss->ssid_len);
2086         network->signal = bss->signal;
2087         network->frequency = bss->frequency;
2088         network->best_bss = bss;
2089
2090         if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) {
2091                 network->wps = TRUE;
2092                 network->wps_capabilities = bss->wps_capabilities;
2093         }
2094
2095 #if defined TIZEN_EXT
2096         network->keymgmt = bss->keymgmt;
2097
2098         if (g_slist_length(bss->vsie_list) > 0) {
2099                 GSList *list = NULL;
2100                 unsigned char *vsie = NULL;
2101                 for (list = bss->vsie_list; list; list = list->next) {
2102                         unsigned char *ie = (unsigned char *)list->data;
2103                         vsie = (unsigned char *)g_try_malloc0(ie[1]+2); // tag number size(1), tag length size(1)
2104
2105                         if (vsie) {
2106                                 memcpy(vsie, ie, ie[1]+2);
2107                                 network->vsie_list = g_slist_append(network->vsie_list, vsie);
2108                         } else
2109                                 SUPPLICANT_DBG("Failed to allocate memory.");
2110                 }
2111         }
2112
2113         network->isHS20AP = bss->hs20;
2114         memcpy(network->country_code, bss->country_code, COUNTRY_CODE_LENGTH);
2115         network->phy_mode = bss->phy_mode;
2116 #endif
2117
2118         SUPPLICANT_DBG("New network %s created", network->name);
2119
2120         network->bss_table = g_hash_table_new_full(g_str_hash, g_str_equal,
2121                                                         NULL, remove_bss);
2122
2123         network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
2124                                                         g_free, g_free);
2125
2126         g_hash_table_replace(interface->network_table,
2127                                                 network->group, network);
2128
2129         callback_network_added(network);
2130
2131 done:
2132         /* We update network's WPS properties if only bss provides WPS. */
2133         if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) {
2134                 network->wps = TRUE;
2135                 network->wps_capabilities = bss->wps_capabilities;
2136
2137                 if (!is_new_network)
2138                         callback_network_changed(network, "WPSCapabilities");
2139         }
2140
2141         /*
2142          * Do not change best BSS if we are connected. It will be done through
2143          * CurrentBSS property in case of misalignment with wpa_s or roaming.
2144          */
2145         if (network != interface->current_network &&
2146                                 bss->signal > network->signal) {
2147                 network->signal = bss->signal;
2148                 network->best_bss = bss;
2149                 callback_network_changed(network, "Signal");
2150         }
2151
2152         g_hash_table_replace(interface->bss_mapping, bss->path, network);
2153         g_hash_table_replace(network->bss_table, bss->path, bss);
2154
2155         g_hash_table_replace(bss_mapping, bss->path, interface);
2156
2157         return 0;
2158 }
2159
2160 static void bss_rates(DBusMessageIter *iter, void *user_data)
2161 {
2162         struct g_supplicant_bss *bss = user_data;
2163         dbus_uint32_t rate = 0;
2164
2165         dbus_message_iter_get_basic(iter, &rate);
2166         if (rate == 0)
2167                 return;
2168
2169         if (rate > bss->maxrate)
2170                 bss->maxrate = rate;
2171 }
2172
2173 static void bss_keymgmt(DBusMessageIter *iter, void *user_data)
2174 {
2175         unsigned int *keymgmt = user_data;
2176         const char *str = NULL;
2177         int i;
2178
2179         dbus_message_iter_get_basic(iter, &str);
2180         if (!str)
2181                 return;
2182
2183         for (i = 0; keymgmt_map[i].str; i++)
2184                 if (strcmp(str, keymgmt_map[i].str) == 0) {
2185                         SUPPLICANT_DBG("Keymgmt: %s", str);
2186                         *keymgmt |= keymgmt_map[i].val;
2187                         break;
2188                 }
2189 }
2190
2191 static void bss_group(DBusMessageIter *iter, void *user_data)
2192 {
2193         unsigned int *group = user_data;
2194         const char *str = NULL;
2195         int i;
2196
2197         dbus_message_iter_get_basic(iter, &str);
2198         if (!str)
2199                 return;
2200
2201         for (i = 0; group_map[i].str; i++)
2202                 if (strcmp(str, group_map[i].str) == 0) {
2203                         SUPPLICANT_DBG("Group: %s", str);
2204                         *group |= group_map[i].val;
2205                         break;
2206                 }
2207 }
2208
2209 static void bss_pairwise(DBusMessageIter *iter, void *user_data)
2210 {
2211         unsigned int *pairwise = user_data;
2212         const char *str = NULL;
2213         int i;
2214
2215         dbus_message_iter_get_basic(iter, &str);
2216         if (!str)
2217                 return;
2218
2219         for (i = 0; pairwise_map[i].str; i++)
2220                 if (strcmp(str, pairwise_map[i].str) == 0) {
2221                         SUPPLICANT_DBG("Pairwise: %s", str);
2222                         *pairwise |= pairwise_map[i].val;
2223                         break;
2224                 }
2225 }
2226
2227 static void bss_wpa(const char *key, DBusMessageIter *iter,
2228                         void *user_data)
2229 {
2230         struct g_supplicant_bss *bss = user_data;
2231         unsigned int value = 0;
2232
2233         SUPPLICANT_DBG("Key: %s", key);
2234
2235         if (g_strcmp0(key, "KeyMgmt") == 0) {
2236                 supplicant_dbus_array_foreach(iter, bss_keymgmt, &value);
2237
2238                 if (bss->rsn_selected)
2239                         bss->rsn_keymgmt = value;
2240                 else
2241                         bss->wpa_keymgmt = value;
2242         } else if (g_strcmp0(key, "Group") == 0) {
2243                 supplicant_dbus_array_foreach(iter, bss_group, &value);
2244
2245                 if (bss->rsn_selected)
2246                         bss->rsn_group = value;
2247                 else
2248                         bss->wpa_group = value;
2249         } else if (g_strcmp0(key, "Pairwise") == 0) {
2250                 supplicant_dbus_array_foreach(iter, bss_pairwise, &value);
2251
2252                 if (bss->rsn_selected)
2253                         bss->rsn_pairwise = value;
2254                 else
2255                         bss->wpa_pairwise = value;
2256         }
2257 }
2258
2259 static unsigned int get_tlv(unsigned char *ie, unsigned int ie_size,
2260                                                         unsigned int type)
2261 {
2262         unsigned int len = 0;
2263
2264         while (len + 4 < ie_size) {
2265                 unsigned int hi = ie[len];
2266                 unsigned int lo = ie[len + 1];
2267                 unsigned int tmp_type = (hi << 8) + lo;
2268                 unsigned int v_len = 0;
2269
2270                 /* hi and lo are used to recreate an unsigned int
2271                  * based on 2 8bits length unsigned int. */
2272
2273                 hi = ie[len + 2];
2274                 lo = ie[len + 3];
2275                 v_len = (hi << 8) + lo;
2276
2277                 if (tmp_type == type) {
2278                         unsigned int ret_value = 0;
2279                         unsigned char *value = (unsigned char *)&ret_value;
2280
2281                         SUPPLICANT_DBG("IE: match type 0x%x", type);
2282
2283                         /* Verifying length relevance */
2284                         if (v_len > sizeof(unsigned int) ||
2285                                 len + 4 + v_len > ie_size)
2286                                 break;
2287
2288                         memcpy(value, ie + len + 4, v_len);
2289
2290                         SUPPLICANT_DBG("returning 0x%x", ret_value);
2291                         return ret_value;
2292                 }
2293
2294                 len += v_len + 4;
2295         }
2296
2297         SUPPLICANT_DBG("returning 0");
2298         return 0;
2299 }
2300
2301 #if defined TIZEN_EXT
2302 static void get_bss_phy_mode(unsigned int max_rate,
2303                 unsigned int max_ext_rate, bool ht, bool vht, void *data)
2304 {
2305         struct g_supplicant_bss *bss = data;
2306         unsigned int freq = bss->frequency;
2307
2308         /* Following conditions are used to determine
2309          * IEEE 802.11 Protocol Modes:-
2310          *
2311          * 1. If “Supported rates” is only till 11 Mbps,
2312          *    and frequency is in 2.4GHz band, then protocol is 11B.
2313          * 2. If “Supported rates” is till 54Mbps or
2314          *    “Extended supported rates” are present,
2315          *    and frequency is in 2.4GHz band, then protocol is 11G.
2316          * 3. If “Supported rates” is only till 54 Mbps,
2317          *    frequency is in 5GHz band , then protocol is 11A.
2318          * 4. If “HT capabilities” is supported , then protocol is 11N.
2319          * 5. If “HT capabilities” & “VHT” is supported and
2320          *    frequency is in 5 GHz band, then protocol is 11AC.
2321          * */
2322
2323         if (freq >= 2412 && freq <= 2484) { /* 2.4 Ghz Band */
2324                 if (max_rate <= 11 && max_ext_rate <= 0 && !ht)
2325                         bss->phy_mode = G_SUPPLICANT_MODE_IEEE80211B;
2326                 else if ((max_rate <= 54 || max_ext_rate > 0) && !ht)
2327                         bss->phy_mode = G_SUPPLICANT_MODE_IEEE80211BG;
2328                 else if ((max_rate >= 54 || max_ext_rate > 0) && ht)
2329                         bss->phy_mode = G_SUPPLICANT_MODE_IEEE80211BGN;
2330                 else
2331                         bss->phy_mode = G_SUPPLICANT_MODE_UNKNOWN;
2332         } else if (freq >= 5180 && freq <= 5825) { /* 5 Ghz Band */
2333                 if (max_rate <= 54 && !ht)
2334                         bss->phy_mode = G_SUPPLICANT_MODE_IEEE80211A;
2335                 else if ((max_rate >= 54 || max_ext_rate > 0) && ht && !vht)
2336                         bss->phy_mode = G_SUPPLICANT_MODE_IEEE80211AN;
2337                 else if ((max_rate >= 54 || max_ext_rate > 0) && ht && vht)
2338                         bss->phy_mode = G_SUPPLICANT_MODE_IEEE80211ANAC;
2339                 else
2340                         bss->phy_mode = G_SUPPLICANT_MODE_UNKNOWN;
2341         }
2342 }
2343 #endif
2344
2345 static void bss_process_ies(DBusMessageIter *iter, void *user_data)
2346 {
2347         struct g_supplicant_bss *bss = user_data;
2348         const unsigned char WPS_OUI[] = { 0x00, 0x50, 0xf2, 0x04 };
2349         unsigned char *ie, *ie_end;
2350         DBusMessageIter array;
2351         unsigned int value;
2352         int ie_len;
2353 #if defined TIZEN_EXT
2354         int r_len, j;
2355         unsigned char *rates = NULL;
2356         unsigned char *ext_rates = NULL;
2357         unsigned int max_rate = 0;
2358         unsigned int max_ext_rate = 0;
2359         bool ht = false;
2360         bool vht = false;
2361 #endif
2362
2363 #define WMM_WPA1_WPS_INFO 221
2364 #define WPS_INFO_MIN_LEN  6
2365 #define WPS_VERSION_TLV   0x104A
2366 #define WPS_STATE_TLV     0x1044
2367 #define WPS_METHODS_TLV   0x1012
2368 #define WPS_REGISTRAR_TLV 0x1041
2369 #define WPS_VERSION       0x10
2370 #define WPS_PBC           0x04
2371 #define WPS_PIN           0x00
2372 #define WPS_CONFIGURED    0x02
2373 #if defined TIZEN_EXT
2374 #define VENDOR_SPECIFIC_INFO 0xDD
2375 #define WLAN_EID_COUNTRY 7
2376 #endif
2377
2378         dbus_message_iter_recurse(iter, &array);
2379         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
2380
2381         if (!ie || ie_len < 2)
2382                 return;
2383
2384         bss->wps_capabilities = 0;
2385         bss->keymgmt = 0;
2386
2387         for (ie_end = ie + ie_len; ie < ie_end && ie + ie[1] + 1 <= ie_end;
2388                                                         ie += ie[1] + 2) {
2389 #if defined TIZEN_EXT
2390                 unsigned char *vsie;
2391                 int vsie_len = 0;
2392                 if(ie[0] == VENDOR_SPECIFIC_INFO && memcmp(ie+2, WPS_OUI, sizeof(WPS_OUI)) != 0) {
2393                         SUPPLICANT_DBG("IE: match vendor specific data");
2394
2395                         vsie_len = ie[1]+2;     // tag number size(1), tag length size(1)
2396                         vsie = (unsigned char *)g_try_malloc0(vsie_len);
2397
2398                         if (vsie) {
2399                                 memcpy(vsie, ie, vsie_len);
2400                                 bss->vsie_list = g_slist_append(bss->vsie_list, vsie);
2401                         } else
2402                                 SUPPLICANT_DBG("Failed to allocate memory");
2403                         continue;
2404                 }
2405
2406                 if(ie[0] == WLAN_EID_COUNTRY && ie[1] >= 2) {
2407                         /* Add country code only if it is a valid alphabet */
2408                         if (ie[2] >= 65 && ie[2] <= 90 && ie[3] >= 65 && ie[3] <= 90) {
2409                                 memcpy(bss->country_code, ie+2, COUNTRY_CODE_LENGTH);
2410                                 continue;
2411                         }
2412                 }
2413
2414                 if (ie[0] == WLAN_EID_HT_CAP && ie[1]) {
2415                         ht = true;
2416                         continue;
2417                 }
2418
2419                 if (ie[0] == WLAN_EID_VHT_CAP && ie[1]) {
2420                         vht = true;
2421                         continue;
2422                 }
2423
2424                 if (ie[0] == WLAN_EID_SUPP_RATES && ie[1]) {
2425                         r_len = ie[1];
2426                         rates = g_malloc0(r_len);
2427                         if (!rates)
2428                                 continue;
2429
2430                         for (j = 0; ie && j < r_len; j++) {
2431                                 rates[j] = ((ie[j + 2] & 0x7f) * 500000)/1000000;
2432                                 if (max_rate < rates[j])
2433                                         max_rate = rates[j];
2434                         }
2435                         continue;
2436                 }
2437
2438                 if (ie[0] == WLAN_EID_EXT_SUPP_RATES && ie[1] > 0) {
2439                         r_len = ie[1];
2440                         ext_rates = g_malloc0(r_len);
2441                         if (!ext_rates)
2442                                 continue;
2443
2444                         for (j = 0; ie && j < r_len; j++) {
2445                                 ext_rates[j] = ((ie[j + 2] & 0x7f) * 500000)/1000000;
2446                                 if (max_ext_rate < ext_rates[j])
2447                                         max_ext_rate = ext_rates[j];
2448                         }
2449                         continue;
2450                 }
2451 #endif
2452                 if (ie[0] != WMM_WPA1_WPS_INFO || ie[1] < WPS_INFO_MIN_LEN ||
2453                         memcmp(ie+2, WPS_OUI, sizeof(WPS_OUI)) != 0)
2454                         continue;
2455
2456                 SUPPLICANT_DBG("IE: match WPS_OUI");
2457
2458                 value = get_tlv(&ie[6], ie[1], WPS_STATE_TLV);
2459                 if (get_tlv(&ie[6], ie[1], WPS_VERSION_TLV) == WPS_VERSION &&
2460                                                                 value != 0) {
2461                         bss->keymgmt |= G_SUPPLICANT_KEYMGMT_WPS;
2462
2463                         if (value == WPS_CONFIGURED)
2464                                 bss->wps_capabilities |=
2465                                         G_SUPPLICANT_WPS_CONFIGURED;
2466                 }
2467
2468                 value = get_tlv(&ie[6], ie[1], WPS_METHODS_TLV);
2469                 if (value != 0) {
2470                         if (GUINT16_FROM_BE(value) == WPS_PBC)
2471                                 bss->wps_capabilities |= G_SUPPLICANT_WPS_PBC;
2472                         if (GUINT16_FROM_BE(value) == WPS_PIN)
2473                                 bss->wps_capabilities |= G_SUPPLICANT_WPS_PIN;
2474                 } else
2475                         bss->wps_capabilities |=
2476                                 G_SUPPLICANT_WPS_PBC | G_SUPPLICANT_WPS_PIN;
2477
2478                 /* If the AP sends this it means it's advertizing
2479                  * as a registrar and the WPS process is launched
2480                  * on its side */
2481                 if (get_tlv(&ie[6], ie[1], WPS_REGISTRAR_TLV) != 0)
2482                         bss->wps_capabilities |= G_SUPPLICANT_WPS_REGISTRAR;
2483
2484                 SUPPLICANT_DBG("WPS Methods 0x%x", bss->wps_capabilities);
2485         }
2486 #ifdef TIZEN_EXT
2487         get_bss_phy_mode(max_rate, max_ext_rate, ht, vht, user_data);
2488         if (rates)
2489                 g_free(rates);
2490         if (ext_rates)
2491                 g_free(ext_rates);
2492 #endif
2493 }
2494
2495 static void bss_compute_security(struct g_supplicant_bss *bss)
2496 {
2497         /*
2498          * Combining RSN and WPA keymgmt
2499          * We combine it since parsing IEs might have set something for WPS. */
2500         bss->keymgmt |= bss->rsn_keymgmt | bss->wpa_keymgmt;
2501
2502         bss->ieee8021x = FALSE;
2503         bss->psk = FALSE;
2504 #if defined TIZEN_EXT
2505         bss->ft_ieee8021x = FALSE;
2506         bss->ft_psk = FALSE;
2507 #endif
2508
2509 #if defined TIZEN_EXT
2510         if (bss->keymgmt &
2511                         (G_SUPPLICANT_KEYMGMT_WPA_EAP |
2512                                         G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
2513                 bss->ieee8021x = TRUE;
2514         else if (bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPA_FT_EAP)
2515                 bss->ft_ieee8021x = TRUE;
2516 #else
2517         if (bss->keymgmt &
2518                         (G_SUPPLICANT_KEYMGMT_WPA_EAP |
2519                                 G_SUPPLICANT_KEYMGMT_WPA_FT_EAP |
2520                                 G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
2521                 bss->ieee8021x = TRUE;
2522 #endif
2523
2524 #if defined TIZEN_EXT
2525         if (bss->keymgmt &
2526                         (G_SUPPLICANT_KEYMGMT_WPA_PSK |
2527                                         G_SUPPLICANT_KEYMGMT_WPA_PSK_256))
2528                 bss->psk = TRUE;
2529         else if (bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPA_FT_PSK)
2530                 bss->ft_psk = TRUE;
2531 #else
2532         if (bss->keymgmt &
2533                         (G_SUPPLICANT_KEYMGMT_WPA_PSK |
2534                                 G_SUPPLICANT_KEYMGMT_WPA_FT_PSK |
2535                                 G_SUPPLICANT_KEYMGMT_WPA_PSK_256))
2536                 bss->psk = TRUE;
2537 #endif
2538
2539 #if defined TIZEN_EXT
2540         if (bss->keymgmt & G_SUPPLICANT_KEYMGMT_SAE)
2541                 bss->sae = TRUE;
2542         if (bss->keymgmt & G_SUPPLICANT_KEYMGMT_OWE)
2543                 bss->owe = TRUE;
2544 #endif
2545
2546         if (bss->ieee8021x)
2547                 bss->security = G_SUPPLICANT_SECURITY_IEEE8021X;
2548         else if (bss->psk)
2549                 bss->security = G_SUPPLICANT_SECURITY_PSK;
2550 #if defined TIZEN_EXT
2551         else if (bss->ft_psk)
2552                 bss->security = G_SUPPLICANT_SECURITY_FT_PSK;
2553         else if (bss->ft_ieee8021x == TRUE)
2554                 bss->security = G_SUPPLICANT_SECURITY_IEEE8021X;
2555         else if (bss->sae)
2556                 bss->security = G_SUPPLICANT_SECURITY_SAE;
2557         else if (bss->owe)
2558                 bss->security = G_SUPPLICANT_SECURITY_OWE;
2559 #endif
2560         else if (bss->privacy)
2561                 bss->security = G_SUPPLICANT_SECURITY_WEP;
2562         else
2563                 bss->security = G_SUPPLICANT_SECURITY_NONE;
2564 }
2565
2566
2567 static void bss_property(const char *key, DBusMessageIter *iter,
2568                                                         void *user_data)
2569 {
2570         struct g_supplicant_bss *bss = user_data;
2571
2572         if (!bss->interface)
2573                 return;
2574
2575         SUPPLICANT_DBG("key %s", key);
2576
2577         if (!key)
2578                 return;
2579
2580         if (g_strcmp0(key, "BSSID") == 0) {
2581                 DBusMessageIter array;
2582                 unsigned char *addr;
2583                 int addr_len;
2584
2585                 dbus_message_iter_recurse(iter, &array);
2586                 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
2587
2588                 if (addr_len == 6)
2589                         memcpy(bss->bssid, addr, addr_len);
2590         } else if (g_strcmp0(key, "SSID") == 0) {
2591                 DBusMessageIter array;
2592                 unsigned char *ssid;
2593                 int ssid_len;
2594
2595                 dbus_message_iter_recurse(iter, &array);
2596                 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
2597
2598                 if (ssid_len > 0 && ssid_len < 33) {
2599                         memcpy(bss->ssid, ssid, ssid_len);
2600                         bss->ssid_len = ssid_len;
2601                 } else {
2602                         memset(bss->ssid, 0, sizeof(bss->ssid));
2603                         bss->ssid_len = 0;
2604                 }
2605         } else if (g_strcmp0(key, "Capabilities") == 0) {
2606                 dbus_uint16_t capabilities = 0x0000;
2607
2608                 dbus_message_iter_get_basic(iter, &capabilities);
2609
2610                 if (capabilities & IEEE80211_CAP_ESS)
2611                         bss->mode = G_SUPPLICANT_MODE_INFRA;
2612                 else if (capabilities & IEEE80211_CAP_IBSS)
2613                         bss->mode = G_SUPPLICANT_MODE_IBSS;
2614
2615                 if (capabilities & IEEE80211_CAP_PRIVACY)
2616                         bss->privacy = TRUE;
2617         } else if (g_strcmp0(key, "Mode") == 0) {
2618                 const char *mode = NULL;
2619
2620                 dbus_message_iter_get_basic(iter, &mode);
2621                 bss->mode = string2mode(mode);
2622         } else if (g_strcmp0(key, "Frequency") == 0) {
2623                 dbus_uint16_t frequency = 0;
2624
2625                 dbus_message_iter_get_basic(iter, &frequency);
2626                 bss->frequency = frequency;
2627         } else if (g_strcmp0(key, "Signal") == 0) {
2628                 dbus_int16_t signal = 0;
2629
2630                 dbus_message_iter_get_basic(iter, &signal);
2631
2632                 bss->signal = signal;
2633                 if (!bss->signal)
2634                         bss->signal = BSS_UNKNOWN_STRENGTH;
2635
2636         } else if (g_strcmp0(key, "Level") == 0) {
2637                 dbus_int32_t level = 0;
2638
2639                 dbus_message_iter_get_basic(iter, &level);
2640         } else if (g_strcmp0(key, "Rates") == 0) {
2641                 supplicant_dbus_array_foreach(iter, bss_rates, bss);
2642         } else if (g_strcmp0(key, "MaxRate") == 0) {
2643                 dbus_uint32_t maxrate = 0;
2644
2645                 dbus_message_iter_get_basic(iter, &maxrate);
2646                 if (maxrate != 0)
2647                         bss->maxrate = maxrate;
2648         } else if (g_strcmp0(key, "Privacy") == 0) {
2649                 dbus_bool_t privacy = FALSE;
2650
2651                 dbus_message_iter_get_basic(iter, &privacy);
2652                 bss->privacy = privacy;
2653         } else if (g_strcmp0(key, "RSN") == 0) {
2654                 bss->rsn_selected = TRUE;
2655
2656                 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
2657         } else if (g_strcmp0(key, "WPA") == 0) {
2658                 bss->rsn_selected = FALSE;
2659
2660                 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
2661 #if defined TIZEN_EXT
2662         } else if (g_strcmp0(key, "HS20") == 0) {
2663                 dbus_bool_t hs20 = FALSE;
2664                 dbus_message_iter_get_basic(iter, &hs20);
2665                 bss->hs20 = hs20;
2666 #endif
2667         } else if (g_strcmp0(key, "IEs") == 0)
2668                 bss_process_ies(iter, bss);
2669         else
2670                 SUPPLICANT_DBG("key %s type %c",
2671                                 key, dbus_message_iter_get_arg_type(iter));
2672 }
2673
2674 static struct g_supplicant_bss *interface_bss_added(DBusMessageIter *iter,
2675                                                         void *user_data)
2676 {
2677         GSupplicantInterface *interface = user_data;
2678         GSupplicantNetwork *network;
2679         struct g_supplicant_bss *bss;
2680         const char *path = NULL;
2681
2682         SUPPLICANT_DBG("");
2683
2684         dbus_message_iter_get_basic(iter, &path);
2685         if (!path)
2686                 return NULL;
2687
2688         if (g_strcmp0(path, "/") == 0)
2689                 return NULL;
2690
2691         SUPPLICANT_DBG("%s", path);
2692
2693         network = g_hash_table_lookup(interface->bss_mapping, path);
2694         if (network) {
2695                 bss = g_hash_table_lookup(network->bss_table, path);
2696                 if (bss)
2697                         return NULL;
2698         }
2699
2700         bss = g_try_new0(struct g_supplicant_bss, 1);
2701         if (!bss)
2702                 return NULL;
2703
2704         bss->interface = interface;
2705         bss->path = g_strdup(path);
2706         bss->signal = BSS_UNKNOWN_STRENGTH;
2707
2708         return bss;
2709 }
2710
2711 static void interface_bss_added_with_keys(DBusMessageIter *iter,
2712                                                 void *user_data)
2713 {
2714         struct g_supplicant_bss *bss;
2715
2716         SUPPLICANT_DBG("");
2717
2718         bss = interface_bss_added(iter, user_data);
2719         if (!bss)
2720                 return;
2721
2722         dbus_message_iter_next(iter);
2723
2724         if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_INVALID)
2725                 return;
2726
2727         supplicant_dbus_property_foreach(iter, bss_property, bss);
2728
2729         bss_compute_security(bss);
2730         if (add_or_replace_bss_to_network(bss) < 0)
2731                 SUPPLICANT_DBG("add_or_replace_bss_to_network failed");
2732 }
2733
2734 static void interface_bss_added_without_keys(DBusMessageIter *iter,
2735                                                 void *user_data)
2736 {
2737         struct g_supplicant_bss *bss;
2738
2739         SUPPLICANT_DBG("");
2740
2741         bss = interface_bss_added(iter, user_data);
2742         if (!bss)
2743                 return;
2744
2745         supplicant_dbus_property_get_all(bss->path,
2746                                         SUPPLICANT_INTERFACE ".BSS",
2747                                         bss_property, bss, bss);
2748
2749         bss_compute_security(bss);
2750         if (add_or_replace_bss_to_network(bss) < 0)
2751                         SUPPLICANT_DBG("add_or_replace_bss_to_network failed");
2752 }
2753
2754 static void update_signal(gpointer key, gpointer value,
2755                                                 gpointer user_data)
2756 {
2757         struct g_supplicant_bss *bss = value;
2758         GSupplicantNetwork *network = user_data;
2759
2760         if (bss->signal > network->signal) {
2761                 network->signal = bss->signal;
2762                 network->best_bss = bss;
2763         }
2764 }
2765
2766 static void update_network_signal(GSupplicantNetwork *network)
2767 {
2768         if (g_hash_table_size(network->bss_table) <= 1 && network->best_bss)
2769                 return;
2770
2771         g_hash_table_foreach(network->bss_table,
2772                                 update_signal, network);
2773
2774         SUPPLICANT_DBG("New network signal %d", network->signal);
2775 }
2776
2777 static void interface_current_bss(GSupplicantInterface *interface,
2778                                                 DBusMessageIter *iter)
2779 {
2780         GSupplicantNetwork *network;
2781         struct g_supplicant_bss *bss;
2782         const char *path;
2783
2784         dbus_message_iter_get_basic(iter, &path);
2785         if (g_strcmp0(path, "/") == 0) {
2786                 interface->current_network = NULL;
2787                 return;
2788         }
2789
2790         interface_bss_added_without_keys(iter, interface);
2791
2792         network = g_hash_table_lookup(interface->bss_mapping, path);
2793         if (!network)
2794                 return;
2795
2796         bss = g_hash_table_lookup(network->bss_table, path);
2797         if (!bss)
2798                 return;
2799
2800         interface->current_network = network;
2801
2802         if (bss != network->best_bss) {
2803                 /*
2804                  * This is the case where either wpa_s got associated
2805                  * to a BSS different than the one ConnMan considers
2806                  * the best, or we are roaming.
2807                  */
2808                 SUPPLICANT_DBG("Update best BSS for %s", network->name);
2809
2810                 network->best_bss = bss;
2811
2812                 if (network->signal != bss->signal) {
2813                         SUPPLICANT_DBG("New network signal %d dBm",
2814                                                 bss->signal);
2815
2816                         network->signal = bss->signal;
2817                         callback_network_changed(network, "Signal");
2818                 }
2819         }
2820
2821         /*
2822          * wpa_s could notify about CurrentBSS in any state once
2823          * it got associated. It is not sure such notification will
2824          * arrive together with transition to ASSOCIATED state.
2825          * In fact, for networks with security WEP or OPEN, it
2826          * always arrives together with transition to COMPLETED.
2827          */
2828         switch (interface->state) {
2829         case G_SUPPLICANT_STATE_UNKNOWN:
2830         case G_SUPPLICANT_STATE_DISABLED:
2831         case G_SUPPLICANT_STATE_DISCONNECTED:
2832         case G_SUPPLICANT_STATE_INACTIVE:
2833         case G_SUPPLICANT_STATE_SCANNING:
2834         case G_SUPPLICANT_STATE_AUTHENTICATING:
2835         case G_SUPPLICANT_STATE_ASSOCIATING:
2836                 return;
2837         case G_SUPPLICANT_STATE_ASSOCIATED:
2838         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2839         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2840         case G_SUPPLICANT_STATE_COMPLETED:
2841                 callback_network_associated(network);
2842                 break;
2843         }
2844 }
2845
2846 static void interface_bss_removed(DBusMessageIter *iter, void *user_data)
2847 {
2848         GSupplicantInterface *interface = user_data;
2849         GSupplicantNetwork *network;
2850         struct g_supplicant_bss *bss = NULL;
2851         const char *path = NULL;
2852         bool is_current_network_bss = false;
2853
2854         dbus_message_iter_get_basic(iter, &path);
2855         if (!path)
2856                 return;
2857
2858         network = g_hash_table_lookup(interface->bss_mapping, path);
2859         if (!network)
2860                 return;
2861
2862         bss = g_hash_table_lookup(network->bss_table, path);
2863         if (network->best_bss == bss) {
2864                 network->best_bss = NULL;
2865                 network->signal = BSS_UNKNOWN_STRENGTH;
2866                 is_current_network_bss = true;
2867         }
2868
2869         g_hash_table_remove(bss_mapping, path);
2870
2871         g_hash_table_remove(interface->bss_mapping, path);
2872         g_hash_table_remove(network->bss_table, path);
2873
2874         update_network_signal(network);
2875
2876         if (g_hash_table_size(network->bss_table) == 0) {
2877                 g_hash_table_remove(interface->network_table, network->group);
2878         } else {
2879                 if (is_current_network_bss && network->best_bss)
2880 #if defined TIZEN_EXT
2881                         callback_network_changed(network, "CheckMultiBssidConnect");
2882 #else
2883                         callback_network_changed(network, "");
2884 #endif
2885         }
2886 }
2887
2888 static void set_config_methods(DBusMessageIter *iter, void *user_data)
2889 {
2890         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, user_data);
2891 }
2892
2893 static void wps_property(const char *key, DBusMessageIter *iter,
2894                                                         void *user_data)
2895 {
2896         GSupplicantInterface *interface = user_data;
2897
2898         if (!interface)
2899                 return;
2900
2901         SUPPLICANT_DBG("key: %s", key);
2902
2903         if (g_strcmp0(key, "ConfigMethods") == 0) {
2904                 const char *config_methods = "push_button", *str = NULL;
2905
2906                 dbus_message_iter_get_basic(iter, &str);
2907                 if (str && strlen(str) > 0) {
2908                         /* It was already set at wpa_s level, don't modify it. */
2909                         SUPPLICANT_DBG("%s", str);
2910                         return;
2911                 }
2912
2913                 supplicant_dbus_property_set(interface->path,
2914                         SUPPLICANT_INTERFACE ".Interface.WPS",
2915                         "ConfigMethods", DBUS_TYPE_STRING_AS_STRING,
2916                         set_config_methods, NULL, &config_methods, NULL);
2917
2918                 SUPPLICANT_DBG("No value. Set %s", config_methods);
2919         }
2920
2921 }
2922
2923 static void interface_property(const char *key, DBusMessageIter *iter,
2924                                                         void *user_data)
2925 {
2926         GSupplicantInterface *interface = user_data;
2927
2928         if (!interface)
2929                 return;
2930
2931         SUPPLICANT_DBG("%s", key);
2932
2933         if (!key) {
2934                 debug_strvalmap("KeyMgmt capability", keymgmt_map,
2935                                                 interface->keymgmt_capa);
2936                 debug_strvalmap("AuthAlg capability", authalg_capa_map,
2937                                                 interface->authalg_capa);
2938                 debug_strvalmap("Protocol capability", proto_capa_map,
2939                                                 interface->proto_capa);
2940                 debug_strvalmap("Pairwise capability", pairwise_map,
2941                                                 interface->pairwise_capa);
2942                 debug_strvalmap("Group capability", group_map,
2943                                                 interface->group_capa);
2944                 debug_strvalmap("Scan capability", scan_capa_map,
2945                                                 interface->scan_capa);
2946                 debug_strvalmap("Mode capability", mode_capa_map,
2947                                                 interface->mode_capa);
2948
2949                 supplicant_dbus_property_get_all(interface->path,
2950                                 SUPPLICANT_INTERFACE ".Interface.WPS",
2951                                 wps_property, interface, interface);
2952
2953                 if (interface->ready)
2954                         callback_interface_added(interface);
2955
2956                 return;
2957         }
2958
2959         if (g_strcmp0(key, "Capabilities") == 0) {
2960                 supplicant_dbus_property_foreach(iter, interface_capability,
2961                                                                 interface);
2962 #if !defined TIZEN_EXT
2963                 if (interface->mode_capa & G_SUPPLICANT_CAPABILITY_MODE_P2P)
2964                         interface->p2p_support = true;
2965 #endif
2966 #if defined TIZEN_EXT_WIFI_MESH
2967                 if (interface->mode_capa & G_SUPPLICANT_CAPABILITY_MODE_MESH)
2968                         interface->mesh_support = true;
2969 #endif
2970         } else if (g_strcmp0(key, "State") == 0) {
2971                 const char *str = NULL;
2972
2973                 dbus_message_iter_get_basic(iter, &str);
2974                 if (str)
2975                         if (string2state(str) != interface->state) {
2976                                 interface->state = string2state(str);
2977                                 callback_interface_state(interface);
2978                         }
2979
2980                 if (interface->ap_create_in_progress) {
2981                         if (interface->state == G_SUPPLICANT_STATE_DISCONNECTED)
2982                                 callback_ap_create_fail(interface);
2983
2984                         interface->ap_create_in_progress = false;
2985                 }
2986
2987                 if (interface->state == G_SUPPLICANT_STATE_DISABLED)
2988                         interface->ready = FALSE;
2989                 else
2990                         interface->ready = TRUE;
2991
2992                 SUPPLICANT_DBG("state %s (%d)", str, interface->state);
2993         } else if (g_strcmp0(key, "Scanning") == 0) {
2994                 dbus_bool_t scanning = FALSE;
2995
2996                 dbus_message_iter_get_basic(iter, &scanning);
2997                 interface->scanning = scanning;
2998
2999                 if (interface->ready) {
3000                         if (interface->scanning)
3001                                 callback_scan_started(interface);
3002                         else
3003                                 callback_scan_finished(interface);
3004                 }
3005         } else if (g_strcmp0(key, "ApScan") == 0) {
3006                 int apscan = 1;
3007
3008                 dbus_message_iter_get_basic(iter, &apscan);
3009                 interface->apscan = apscan;
3010         } else if (g_strcmp0(key, "Ifname") == 0) {
3011                 const char *str = NULL;
3012
3013                 dbus_message_iter_get_basic(iter, &str);
3014                 if (str) {
3015                         g_free(interface->ifname);
3016                         interface->ifname = g_strdup(str);
3017                 }
3018         } else if (g_strcmp0(key, "Driver") == 0) {
3019                 const char *str = NULL;
3020
3021                 dbus_message_iter_get_basic(iter, &str);
3022                 if (str) {
3023                         g_free(interface->driver);
3024                         interface->driver = g_strdup(str);
3025                 }
3026         } else if (g_strcmp0(key, "BridgeIfname") == 0) {
3027                 const char *str = NULL;
3028
3029                 dbus_message_iter_get_basic(iter, &str);
3030                 if (str) {
3031                         g_free(interface->bridge);
3032                         interface->bridge = g_strdup(str);
3033                 }
3034         } else if (g_strcmp0(key, "ConfigFile") == 0) {
3035                 const char *str = NULL;
3036
3037                 dbus_message_iter_get_basic(iter, &str);
3038                 if (str && strlen(str) > 0 && interface->ifname) {
3039                         SUPPLICANT_DBG("New {%s, %s}", interface->ifname, str);
3040                         g_hash_table_replace(config_file_table,
3041                                 g_strdup(interface->ifname), g_strdup(str));
3042                 }
3043         } else if (g_strcmp0(key, "CurrentBSS") == 0) {
3044                 interface_current_bss(interface, iter);
3045         } else if (g_strcmp0(key, "CurrentNetwork") == 0) {
3046 #if defined TIZEN_EXT
3047                 if (interface->state != G_SUPPLICANT_STATE_COMPLETED)
3048 #endif
3049                 interface_network_added(iter, interface);
3050         } else if (g_strcmp0(key, "BSSs") == 0) {
3051                 supplicant_dbus_array_foreach(iter,
3052                                         interface_bss_added_without_keys,
3053                                         interface);
3054         } else if (g_strcmp0(key, "Blobs") == 0) {
3055                 /* Nothing */
3056         } else if (g_strcmp0(key, "Networks") == 0) {
3057                 supplicant_dbus_array_foreach(iter, interface_network_added,
3058                                                                 interface);
3059         } else if (g_strcmp0(key, "DisconnectReason") == 0) {
3060                 int reason_code;
3061                 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
3062                         dbus_message_iter_get_basic(iter, &reason_code);
3063                         callback_disconnect_reason_code(interface, reason_code);
3064                 }
3065         } else if (g_strcmp0(key, "AssocStatusCode") == 0) {
3066                 int status_code;
3067                 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
3068                         dbus_message_iter_get_basic(iter, &status_code);
3069                         callback_assoc_status_code(interface, status_code);
3070                 }
3071         } else {
3072                 SUPPLICANT_DBG("key %s type %c",
3073                                 key, dbus_message_iter_get_arg_type(iter));
3074         }
3075 }
3076
3077 static void scan_network_update(DBusMessageIter *iter, void *user_data)
3078 {
3079         GSupplicantInterface *interface = user_data;
3080         GSupplicantNetwork *network;
3081         char *path;
3082
3083         if (!iter)
3084                 return;
3085
3086         dbus_message_iter_get_basic(iter, &path);
3087
3088         if (!path)
3089                 return;
3090
3091         if (g_strcmp0(path, "/") == 0)
3092                 return;
3093
3094         /* Update the network details based on scan BSS data */
3095         network = g_hash_table_lookup(interface->bss_mapping, path);
3096         if (network)
3097                 callback_network_added(network);
3098 }
3099
3100 static void scan_bss_data(const char *key, DBusMessageIter *iter,
3101                                 void *user_data)
3102 {
3103         GSupplicantInterface *interface = user_data;
3104
3105 /*Fixed : stucking in scanning state when scan failed*/
3106 #if defined TIZEN_EXT
3107                 GSupplicantInterfaceCallback scan_callback;
3108 #endif
3109
3110         if (iter)
3111                 supplicant_dbus_array_foreach(iter, scan_network_update,
3112                                                 interface);
3113
3114 #if defined TIZEN_EXT
3115                 scan_callback = interface->scan_callback;
3116 #endif
3117
3118         if (interface->scan_callback)
3119                 interface->scan_callback(0, interface, interface->scan_data);
3120
3121 #if defined TIZEN_EXT
3122                 if (interface->scan_callback == scan_callback) {
3123 #endif
3124         interface->scan_callback = NULL;
3125         interface->scan_data = NULL;
3126 #if defined TIZEN_EXT
3127         }
3128 #endif
3129 }
3130
3131 static GSupplicantInterface *interface_alloc(const char *path)
3132 {
3133         GSupplicantInterface *interface;
3134
3135         interface = g_try_new0(GSupplicantInterface, 1);
3136         if (!interface)
3137                 return NULL;
3138
3139         interface->path = g_strdup(path);
3140
3141         interface->network_table = g_hash_table_new_full(g_str_hash,
3142                                         g_str_equal, NULL, remove_network);
3143         interface->peer_table = g_hash_table_new_full(g_str_hash,
3144                                         g_str_equal, NULL, remove_peer);
3145         interface->group_table = g_hash_table_new_full(g_str_hash,
3146                                         g_str_equal, NULL, remove_group);
3147         interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
3148                                                                 NULL, NULL);
3149
3150         g_hash_table_replace(interface_table, interface->path, interface);
3151
3152         return interface;
3153 }
3154
3155 static void interface_added(DBusMessageIter *iter, void *user_data)
3156 {
3157         GSupplicantInterface *interface;
3158         const char *path = NULL;
3159         bool properties_appended = GPOINTER_TO_UINT(user_data);
3160
3161         SUPPLICANT_DBG("");
3162
3163         dbus_message_iter_get_basic(iter, &path);
3164         if (!path)
3165                 return;
3166
3167         if (g_strcmp0(path, "/") == 0)
3168                 return;
3169
3170         interface = g_hash_table_lookup(interface_table, path);
3171         if (interface)
3172                 return;
3173
3174         interface = interface_alloc(path);
3175         if (!interface)
3176                 return;
3177
3178         if (!properties_appended) {
3179                 supplicant_dbus_property_get_all(path,
3180                                                 SUPPLICANT_INTERFACE ".Interface",
3181                                                 interface_property, interface,
3182                                                 interface);
3183                 return;
3184         }
3185
3186         dbus_message_iter_next(iter);
3187         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
3188                 supplicant_dbus_property_foreach(iter, interface_property,
3189                                                                 interface);
3190                 interface_property(NULL, NULL, interface);
3191         }
3192 }
3193
3194 static void interface_removed(DBusMessageIter *iter, void *user_data)
3195 {
3196         const char *path = NULL;
3197         GSupplicantInterface *interface = user_data;
3198
3199         dbus_message_iter_get_basic(iter, &path);
3200         if (!path)
3201                 return;
3202
3203         interface = g_hash_table_lookup(interface_table, path);
3204         g_supplicant_interface_cancel(interface);
3205
3206         g_hash_table_remove(interface_table, path);
3207 }
3208
3209 static void eap_method(DBusMessageIter *iter, void *user_data)
3210 {
3211         const char *str = NULL;
3212         int i;
3213
3214         dbus_message_iter_get_basic(iter, &str);
3215         if (!str)
3216                 return;
3217
3218         for (i = 0; eap_method_map[i].str; i++)
3219                 if (strcmp(str, eap_method_map[i].str) == 0) {
3220                         eap_methods |= eap_method_map[i].val;
3221                         break;
3222                 }
3223 }
3224
3225 static void service_property(const char *key, DBusMessageIter *iter,
3226                                                         void *user_data)
3227 {
3228         if (!key) {
3229                 callback_system_ready();
3230                 return;
3231         }
3232
3233         if (g_strcmp0(key, "DebugLevel") == 0) {
3234                 const char *str = NULL;
3235                 int i;
3236
3237                 dbus_message_iter_get_basic(iter, &str);
3238                 for (i = 0; debug_strings[i]; i++)
3239                         if (g_strcmp0(debug_strings[i], str) == 0) {
3240                                 debug_level = i;
3241                                 break;
3242                         }
3243                 SUPPLICANT_DBG("Debug level %d", debug_level);
3244         } else if (g_strcmp0(key, "DebugTimestamp") == 0) {
3245                 dbus_message_iter_get_basic(iter, &debug_timestamp);
3246                 SUPPLICANT_DBG("Debug timestamp %u", debug_timestamp);
3247         } else if (g_strcmp0(key, "DebugShowKeys") == 0) {
3248                 dbus_message_iter_get_basic(iter, &debug_showkeys);
3249                 SUPPLICANT_DBG("Debug show keys %u", debug_showkeys);
3250         } else if (g_strcmp0(key, "Interfaces") == 0) {
3251                 supplicant_dbus_array_foreach(iter, interface_added, NULL);
3252         } else if (g_strcmp0(key, "EapMethods") == 0) {
3253                 supplicant_dbus_array_foreach(iter, eap_method, NULL);
3254                 debug_strvalmap("EAP method", eap_method_map, eap_methods);
3255         } else if (g_strcmp0(key, "Country") == 0) {
3256                 const char *country = NULL;
3257
3258                 dbus_message_iter_get_basic(iter, &country);
3259                 SUPPLICANT_DBG("Country %s", country);
3260         } else
3261                 SUPPLICANT_DBG("key %s type %c",
3262                                 key, dbus_message_iter_get_arg_type(iter));
3263 }
3264
3265 static void signal_name_owner_changed(const char *path, DBusMessageIter *iter)
3266 {
3267         const char *name = NULL, *old = NULL, *new = NULL;
3268
3269         SUPPLICANT_DBG("");
3270
3271         if (g_strcmp0(path, DBUS_PATH_DBUS) != 0)
3272                 return;
3273
3274         dbus_message_iter_get_basic(iter, &name);
3275         if (!name)
3276                 return;
3277
3278         if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0)
3279                 return;
3280
3281         dbus_message_iter_next(iter);
3282         dbus_message_iter_get_basic(iter, &old);
3283         dbus_message_iter_next(iter);
3284         dbus_message_iter_get_basic(iter, &new);
3285
3286         if (!old || !new)
3287                 return;
3288
3289         if (strlen(old) > 0 && strlen(new) == 0) {
3290                 system_available = FALSE;
3291                 g_hash_table_remove_all(bss_mapping);
3292                 g_hash_table_remove_all(peer_mapping);
3293                 g_hash_table_remove_all(group_mapping);
3294                 g_hash_table_remove_all(config_file_table);
3295                 g_hash_table_remove_all(interface_table);
3296                 callback_system_killed();
3297         }
3298
3299         if (strlen(new) > 0 && strlen(old) == 0) {
3300                 system_available = TRUE;
3301                 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
3302                                                 SUPPLICANT_INTERFACE,
3303                                                 service_property, NULL, NULL);
3304         }
3305 }
3306
3307 static void signal_properties_changed(const char *path, DBusMessageIter *iter)
3308 {
3309         SUPPLICANT_DBG("");
3310
3311         if (g_strcmp0(path, SUPPLICANT_PATH) != 0)
3312                 return;
3313
3314         supplicant_dbus_property_foreach(iter, service_property, NULL);
3315 }
3316
3317 static void signal_interface_added(const char *path, DBusMessageIter *iter)
3318 {
3319         SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
3320
3321         if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
3322                 interface_added(iter, GUINT_TO_POINTER(true));
3323 }
3324
3325 static void signal_interface_removed(const char *path, DBusMessageIter *iter)
3326 {
3327         SUPPLICANT_DBG("");
3328
3329         if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
3330                 interface_removed(iter, NULL);
3331 }
3332
3333 static void signal_interface_changed(const char *path, DBusMessageIter *iter)
3334 {
3335         GSupplicantInterface *interface;
3336
3337         SUPPLICANT_DBG("");
3338
3339         interface = g_hash_table_lookup(interface_table, path);
3340         if (!interface)
3341                 return;
3342
3343         supplicant_dbus_property_foreach(iter, interface_property, interface);
3344 }
3345
3346 static void signal_scan_done(const char *path, DBusMessageIter *iter)
3347 {
3348         GSupplicantInterface *interface;
3349         dbus_bool_t success = FALSE;
3350
3351         SUPPLICANT_DBG("");
3352
3353         interface = g_hash_table_lookup(interface_table, path);
3354         if (!interface)
3355                 return;
3356
3357         dbus_message_iter_get_basic(iter, &success);
3358
3359         if (interface->scanning) {
3360                 callback_scan_finished(interface);
3361                 interface->scanning = FALSE;
3362         }
3363
3364         /*
3365          * If scan is unsuccessful return -EIO else get the scanned BSSs
3366          * and update the network details accordingly
3367          */
3368         if (!success) {
3369                 if (interface->scan_callback)
3370                         interface->scan_callback(-EIO, interface,
3371                                                 interface->scan_data);
3372
3373                 interface->scan_callback = NULL;
3374                 interface->scan_data = NULL;
3375
3376                 return;
3377         }
3378
3379         supplicant_dbus_property_get(path, SUPPLICANT_INTERFACE ".Interface",
3380                                 "BSSs", scan_bss_data, interface, interface);
3381 }
3382
3383 static void signal_bss_added(const char *path, DBusMessageIter *iter)
3384 {
3385         GSupplicantInterface *interface;
3386
3387         SUPPLICANT_DBG("");
3388
3389         interface = g_hash_table_lookup(interface_table, path);
3390         if (!interface)
3391                 return;
3392
3393         interface_bss_added_with_keys(iter, interface);
3394 }
3395
3396 static void signal_bss_removed(const char *path, DBusMessageIter *iter)
3397 {
3398         GSupplicantInterface *interface;
3399
3400         SUPPLICANT_DBG("");
3401
3402         interface = g_hash_table_lookup(interface_table, path);
3403         if (!interface)
3404                 return;
3405
3406         interface_bss_removed(iter, interface);
3407 }
3408
3409 static void signal_network_added(const char *path, DBusMessageIter *iter)
3410 {
3411         GSupplicantInterface *interface;
3412
3413         SUPPLICANT_DBG("");
3414
3415         interface = g_hash_table_lookup(interface_table, path);
3416         if (!interface)
3417                 return;
3418
3419         interface_network_added(iter, interface);
3420 }
3421
3422 static void signal_network_removed(const char *path, DBusMessageIter *iter)
3423 {
3424         GSupplicantInterface *interface;
3425
3426         SUPPLICANT_DBG("");
3427
3428         interface = g_hash_table_lookup(interface_table, path);
3429         if (!interface)
3430                 return;
3431
3432         interface_network_removed(iter, interface);
3433 }
3434 #if defined TIZEN_EXT
3435 void *copy_vsie_list(gconstpointer src, gpointer data)
3436 {
3437         return g_strdup(src);
3438 }
3439 #endif
3440
3441
3442 static void signal_sta_authorized(const char *path, DBusMessageIter *iter)
3443 {
3444         GSupplicantInterface *interface;
3445         const char *addr = NULL;
3446
3447         SUPPLICANT_DBG("");
3448
3449         interface = g_hash_table_lookup(interface_table, path);
3450         if (!interface)
3451                 return;
3452
3453         dbus_message_iter_get_basic(iter, &addr);
3454         if (!addr)
3455                 return;
3456
3457         callback_sta_authorized(interface, addr);
3458 }
3459
3460 static void signal_sta_deauthorized(const char *path, DBusMessageIter *iter)
3461 {
3462         GSupplicantInterface *interface;
3463         const char *addr = NULL;
3464
3465         SUPPLICANT_DBG("");
3466
3467         interface = g_hash_table_lookup(interface_table, path);
3468         if (!interface)
3469                 return;
3470
3471         dbus_message_iter_get_basic(iter, &addr);
3472         if (!addr)
3473                 return;
3474
3475         callback_sta_deauthorized(interface, addr);
3476 }
3477
3478 static void signal_bss_changed(const char *path, DBusMessageIter *iter)
3479 {
3480         GSupplicantInterface *interface;
3481         GSupplicantNetwork *network;
3482         GSupplicantSecurity old_security;
3483         unsigned int old_wps_capabilities;
3484         struct g_supplicant_bss *bss;
3485
3486         SUPPLICANT_DBG("");
3487
3488         interface = g_hash_table_lookup(bss_mapping, path);
3489         if (!interface)
3490                 return;
3491
3492         network = g_hash_table_lookup(interface->bss_mapping, path);
3493         if (!network)
3494                 return;
3495
3496         bss = g_hash_table_lookup(network->bss_table, path);
3497         if (!bss)
3498                 return;
3499
3500         supplicant_dbus_property_foreach(iter, bss_property, bss);
3501
3502 #if defined TIZEN_EXT
3503         network->frequency = bss->frequency;
3504         network->phy_mode = bss->phy_mode;
3505 #endif
3506         old_security = network->security;
3507         bss_compute_security(bss);
3508
3509         if (old_security != bss->security) {
3510                 struct g_supplicant_bss *new_bss;
3511
3512                 SUPPLICANT_DBG("New network security for %s with path %s",
3513                                bss->ssid, bss->path);
3514
3515                 /*
3516                  * Security change policy:
3517                  * - We first copy the current bss into a new one with
3518                  *   its own pointer (path)
3519                  * - Clear the old bss pointer and remove the network completely
3520                  *   if there are no more BSSs in the bss table.
3521                  * - The new bss will be added either to an existing network
3522                  *   or an additional network will be created
3523                  */
3524
3525                 new_bss = g_try_new0(struct g_supplicant_bss, 1);
3526                 if (!new_bss)
3527                         return;
3528
3529                 memcpy(new_bss, bss, sizeof(struct g_supplicant_bss));
3530                 new_bss->path = g_strdup(bss->path);
3531 #if defined TIZEN_EXT
3532                 new_bss->vsie_list = g_slist_copy_deep(bss->vsie_list, copy_vsie_list, NULL);
3533 #endif
3534
3535                 if (network->best_bss == bss) {
3536                         network->best_bss = NULL;
3537                         network->signal = BSS_UNKNOWN_STRENGTH;
3538                 }
3539
3540                 g_hash_table_remove(bss_mapping, path);
3541
3542                 g_hash_table_remove(interface->bss_mapping, path);
3543                 g_hash_table_remove(network->bss_table, path);
3544
3545                 update_network_signal(network);
3546
3547                 if (g_hash_table_size(network->bss_table) == 0)
3548                         g_hash_table_remove(interface->network_table,
3549                                             network->group);
3550
3551                 if (add_or_replace_bss_to_network(new_bss) < 0) {
3552                         /*
3553                          * Prevent a memory leak on failure in
3554                          * add_or_replace_bss_to_network
3555                          */
3556                         SUPPLICANT_DBG("Failed to add bss %s to network table",
3557                                        new_bss->path);
3558                         g_free(new_bss->path);
3559                         g_free(new_bss);
3560                 }
3561
3562                 return;
3563         }
3564
3565         old_wps_capabilities = network->wps_capabilities;
3566
3567         if (old_wps_capabilities != bss->wps_capabilities) {
3568                 network->wps_capabilities = bss->wps_capabilities;
3569                 callback_network_changed(network, "WPSCapabilities");
3570         }
3571
3572 #if defined TIZEN_EXT
3573         if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) {
3574                 network->wps = TRUE;
3575                 network->wps_capabilities |= bss->wps_capabilities;
3576         } else
3577                 network->wps = FALSE;
3578 #endif
3579
3580         /* Consider only property changes of the connected BSS */
3581         if (network == interface->current_network && bss != network->best_bss)
3582                 return;
3583
3584         if (bss->signal == network->signal)
3585 #ifndef TIZEN_EXT
3586                 return;
3587 #else
3588         {
3589                 callback_network_changed(network, "");
3590                 return;
3591         }
3592 #endif
3593
3594         /*
3595          * If the new signal is lower than the SSID signal, we need
3596          * to check for the new maximum.
3597          */
3598         if (bss->signal < network->signal) {
3599                 if (bss != network->best_bss)
3600 #ifndef TIZEN_EXT
3601                         return;
3602 #else
3603                 {
3604                         callback_network_changed(network, "");
3605                         return;
3606                 }
3607 #endif
3608                 network->signal = bss->signal;
3609                 update_network_signal(network);
3610         } else {
3611                 network->signal = bss->signal;
3612                 network->best_bss = bss;
3613         }
3614
3615         SUPPLICANT_DBG("New network signal for %s %d dBm", network->ssid,
3616                         network->signal);
3617
3618         callback_network_changed(network, "Signal");
3619 }
3620
3621 static void wps_credentials(const char *key, DBusMessageIter *iter,
3622                         void *user_data)
3623 {
3624         GSupplicantInterface *interface = user_data;
3625
3626         if (!key)
3627                 return;
3628
3629         SUPPLICANT_DBG("key %s", key);
3630
3631         if (g_strcmp0(key, "Key") == 0) {
3632                 DBusMessageIter array;
3633                 unsigned char *key_val;
3634                 int key_len;
3635
3636                 dbus_message_iter_recurse(iter, &array);
3637                 dbus_message_iter_get_fixed_array(&array, &key_val, &key_len);
3638
3639                 g_free(interface->wps_cred.key);
3640                 interface->wps_cred.key = g_try_malloc0(
3641                                                 sizeof(char) * key_len + 1);
3642
3643                 if (!interface->wps_cred.key)
3644                         return;
3645
3646                 memcpy(interface->wps_cred.key, key_val,
3647                                                 sizeof(char) * key_len);
3648
3649                 SUPPLICANT_DBG("WPS key present");
3650         } else if (g_strcmp0(key, "SSID") == 0) {
3651                 DBusMessageIter array;
3652                 unsigned char *ssid;
3653                 int ssid_len;
3654
3655                 dbus_message_iter_recurse(iter, &array);
3656                 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
3657
3658                 if (ssid_len > 0 && ssid_len < 33) {
3659                         memcpy(interface->wps_cred.ssid, ssid, ssid_len);
3660                         interface->wps_cred.ssid_len = ssid_len;
3661                 } else {
3662                         memset(interface->wps_cred.ssid, 0, 32);
3663                         interface->wps_cred.ssid_len = 0;
3664                 }
3665         }
3666 }
3667
3668 static void signal_wps_credentials(const char *path, DBusMessageIter *iter)
3669 {
3670         GSupplicantInterface *interface;
3671
3672         SUPPLICANT_DBG("");
3673
3674         interface = g_hash_table_lookup(interface_table, path);
3675         if (!interface)
3676                 return;
3677
3678         supplicant_dbus_property_foreach(iter, wps_credentials, interface);
3679 }
3680
3681 static void wps_event_args(const char *key, DBusMessageIter *iter,
3682                         void *user_data)
3683 {
3684         GSupplicantInterface *interface = user_data;
3685
3686         if (!key || !interface)
3687                 return;
3688
3689         SUPPLICANT_DBG("Arg Key %s", key);
3690 }
3691
3692 static void signal_wps_event(const char *path, DBusMessageIter *iter)
3693 {
3694         GSupplicantInterface *interface;
3695         const char *name = NULL;
3696
3697         SUPPLICANT_DBG("");
3698
3699         interface = g_hash_table_lookup(interface_table, path);
3700         if (!interface)
3701                 return;
3702
3703         dbus_message_iter_get_basic(iter, &name);
3704
3705         SUPPLICANT_DBG("Name: %s", name);
3706
3707         if (g_strcmp0(name, "success") == 0)
3708                 interface->wps_state = G_SUPPLICANT_WPS_STATE_SUCCESS;
3709         else if (g_strcmp0(name, "fail") == 0)
3710                 interface->wps_state = G_SUPPLICANT_WPS_STATE_FAIL;
3711         else
3712                 interface->wps_state = G_SUPPLICANT_WPS_STATE_UNKNOWN;
3713
3714         if (!dbus_message_iter_has_next(iter))
3715                 return;
3716
3717         dbus_message_iter_next(iter);
3718
3719         supplicant_dbus_property_foreach(iter, wps_event_args, interface);
3720 }
3721
3722 #if defined TIZEN_EXT
3723 static void signal_power_off(const char *path, DBusMessageIter *iter)
3724 {
3725         int poweroff_state = 0;
3726
3727         dbus_message_iter_get_basic(iter, &poweroff_state);
3728
3729         SUPPLICANT_DBG("poweroff_state(%d)", poweroff_state);
3730
3731         /* POWER_OFF_DIRECT 2 && POWER_OFF_RESTART 3 */
3732         if (poweroff_state != 2 && poweroff_state != 3)
3733                 return;
3734
3735         if (callbacks_pointer == NULL)
3736                 return;
3737
3738         if (callbacks_pointer->system_power_off == NULL)
3739                 return;
3740
3741         callbacks_pointer->system_power_off();
3742 }
3743 #endif
3744
3745 static void create_peer_identifier(GSupplicantPeer *peer)
3746 {
3747         const unsigned char test[ETH_ALEN] = {};
3748
3749         if (!peer)
3750                 return;
3751
3752         if (!memcmp(peer->device_address, test, ETH_ALEN)) {
3753                 peer->identifier = g_strdup(peer->name);
3754                 return;
3755         }
3756
3757         peer->identifier = g_malloc0(19);
3758         snprintf(peer->identifier, 19, "%02x%02x%02x%02x%02x%02x",
3759                                                 peer->device_address[0],
3760                                                 peer->device_address[1],
3761                                                 peer->device_address[2],
3762                                                 peer->device_address[3],
3763                                                 peer->device_address[4],
3764                                                 peer->device_address[5]);
3765 }
3766
3767 struct peer_property_data {
3768         GSupplicantPeer *peer;
3769         GSList *old_groups;
3770         bool groups_changed;
3771         bool services_changed;
3772 };
3773
3774 static void peer_groups_relation(DBusMessageIter *iter, void *user_data)
3775 {
3776         struct peer_property_data *data = user_data;
3777         GSupplicantPeer *peer = data->peer;
3778         GSupplicantGroup *group;
3779         const char *str = NULL;
3780         GSList *elem;
3781
3782         dbus_message_iter_get_basic(iter, &str);
3783         if (!str)
3784                 return;
3785
3786         group = g_hash_table_lookup(group_mapping, str);
3787         if (!group)
3788                 return;
3789
3790         elem = g_slist_find_custom(data->old_groups, str, (GCompareFunc)g_strcmp0);
3791         if (elem) {
3792                 data->old_groups = g_slist_remove_link(data->old_groups, elem);
3793                 peer->groups = g_slist_concat(elem, peer->groups);
3794         } else {
3795                 peer->groups = g_slist_prepend(peer->groups, g_strdup(str));
3796                 data->groups_changed = true;
3797         }
3798 }
3799
3800 static void peer_property(const char *key, DBusMessageIter *iter,
3801                                                         void *user_data)
3802 {
3803         GSupplicantPeer *pending_peer;
3804         struct peer_property_data *data = user_data;
3805         GSupplicantPeer *peer = data->peer;
3806
3807         SUPPLICANT_DBG("key: %s", key);
3808
3809         if (!peer->interface)
3810                 return;
3811
3812         if (!key) {
3813                 if (peer->name) {
3814                         create_peer_identifier(peer);
3815                         callback_peer_found(peer);
3816                         pending_peer = g_hash_table_lookup(
3817                                         pending_peer_connection, peer->path);
3818
3819                         if (pending_peer && pending_peer == peer) {
3820                                 callback_peer_request(peer);
3821                                 g_hash_table_remove(pending_peer_connection,
3822                                                 peer->path);
3823                         }
3824
3825                         dbus_free(data);
3826                 }
3827
3828                 return;
3829         }
3830
3831         if (g_strcmp0(key, "DeviceAddress") == 0) {
3832                 unsigned char *dev_addr;
3833                 DBusMessageIter array;
3834                 int len;
3835
3836                 dbus_message_iter_recurse(iter, &array);
3837                 dbus_message_iter_get_fixed_array(&array, &dev_addr, &len);
3838
3839                 if (len == ETH_ALEN)
3840                         memcpy(peer->device_address, dev_addr, len);
3841         } else if (g_strcmp0(key, "DeviceName") == 0) {
3842                 const char *str = NULL;
3843
3844                 dbus_message_iter_get_basic(iter, &str);
3845                 if (str)
3846                         peer->name = g_strdup(str);
3847         } else if (g_strcmp0(key, "config_method") == 0) {
3848                 uint16_t wps_config;
3849
3850                 dbus_message_iter_get_basic(iter, &wps_config);
3851
3852                 if (wps_config & G_SUPPLICANT_WPS_CONFIG_PBC)
3853                         peer->wps_capabilities |= G_SUPPLICANT_WPS_PBC;
3854                 if (wps_config & ~G_SUPPLICANT_WPS_CONFIG_PBC)
3855                         peer->wps_capabilities |= G_SUPPLICANT_WPS_PIN;
3856         } else if (g_strcmp0(key, "Groups") == 0) {
3857                 data->old_groups = peer->groups;
3858                 peer->groups = NULL;
3859
3860                 supplicant_dbus_array_foreach(iter,
3861                                                 peer_groups_relation, data);
3862                 if (g_slist_length(data->old_groups) > 0) {
3863                         g_slist_free_full(data->old_groups, g_free);
3864                         data->groups_changed = true;
3865                 }
3866         } else if (g_strcmp0(key, "IEs") == 0) {
3867                 DBusMessageIter array;
3868                 unsigned char *ie;
3869                 int ie_len;
3870
3871                 dbus_message_iter_recurse(iter, &array);
3872                 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
3873
3874                 if (!ie || ie_len < 2)
3875                         return;
3876
3877                 if (peer->widi_ies) {
3878                         if (memcmp(peer->widi_ies, ie, ie_len) == 0)
3879                                 return;
3880
3881                         g_free(peer->widi_ies);
3882                         peer->widi_ies_length = 0;
3883                 }
3884
3885                 peer->widi_ies = g_malloc0(ie_len * sizeof(unsigned char));
3886
3887                 memcpy(peer->widi_ies, ie, ie_len);
3888                 peer->widi_ies_length = ie_len;
3889                 data->services_changed = true;
3890         }
3891 }
3892
3893 static void signal_peer_found(const char *path, DBusMessageIter *iter)
3894 {
3895         struct peer_property_data *property_data;
3896         GSupplicantInterface *interface;
3897         const char *obj_path = NULL;
3898         GSupplicantPeer *peer;
3899
3900         SUPPLICANT_DBG("");
3901
3902         interface = g_hash_table_lookup(interface_table, path);
3903         if (!interface)
3904                 return;
3905
3906         dbus_message_iter_get_basic(iter, &obj_path);
3907         if (!obj_path || g_strcmp0(obj_path, "/") == 0)
3908                 return;
3909
3910         peer = g_hash_table_lookup(interface->peer_table, obj_path);
3911         if (peer)
3912                 return;
3913
3914         peer = g_try_new0(GSupplicantPeer, 1);
3915         if (!peer)
3916                 return;
3917
3918         peer->interface = interface;
3919         peer->path = g_strdup(obj_path);
3920         g_hash_table_insert(interface->peer_table, peer->path, peer);
3921         g_hash_table_replace(peer_mapping, peer->path, interface);
3922
3923         property_data = dbus_malloc0(sizeof(struct peer_property_data));
3924         property_data->peer = peer;
3925
3926         dbus_message_iter_next(iter);
3927         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
3928                 supplicant_dbus_property_foreach(iter, peer_property,
3929                                                         property_data);
3930                 peer_property(NULL, NULL, property_data);
3931                 return;
3932         }
3933
3934         supplicant_dbus_property_get_all(obj_path,
3935                                         SUPPLICANT_INTERFACE ".Peer",
3936                                         peer_property, property_data, NULL);
3937 }
3938
3939 static void signal_peer_lost(const char *path, DBusMessageIter *iter)
3940 {
3941         GSupplicantInterface *interface;
3942         const char *obj_path = NULL;
3943         GSupplicantPeer *peer;
3944
3945         SUPPLICANT_DBG("");
3946
3947         interface = g_hash_table_lookup(interface_table, path);
3948         if (!interface)
3949                 return;
3950
3951         dbus_message_iter_get_basic(iter, &obj_path);
3952         if (!obj_path || g_strcmp0(obj_path, "/") == 0)
3953                 return;
3954
3955         peer = g_hash_table_lookup(interface->peer_table, obj_path);
3956         if (!peer)
3957                 return;
3958
3959         g_hash_table_remove(interface->peer_table, obj_path);
3960 }
3961
3962 static void signal_peer_changed(const char *path, DBusMessageIter *iter)
3963 {
3964         struct peer_property_data *property_data;
3965         GSupplicantInterface *interface;
3966         GSupplicantPeer *peer;
3967
3968         SUPPLICANT_DBG("");
3969
3970         interface = g_hash_table_lookup(peer_mapping, path);
3971         if (!interface)
3972                 return;
3973
3974         peer = g_hash_table_lookup(interface->peer_table, path);
3975         if (!peer) {
3976                 g_hash_table_remove(peer_mapping, path);
3977                 return;
3978         }
3979
3980         property_data = dbus_malloc0(sizeof(struct peer_property_data));
3981         property_data->peer = peer;
3982
3983         supplicant_dbus_property_foreach(iter, peer_property, property_data);
3984         if (property_data->services_changed)
3985                 callback_peer_changed(peer,
3986                                         G_SUPPLICANT_PEER_SERVICES_CHANGED);
3987
3988         if (property_data->groups_changed)
3989                 callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_CHANGED);
3990
3991         dbus_free(property_data);
3992
3993         if (!g_supplicant_peer_is_in_a_group(peer))
3994                 peer->connection_requested = false;
3995 }
3996
3997 struct group_sig_data {
3998         const char *peer_obj_path;
3999         unsigned char iface_address[ETH_ALEN];
4000         const char *interface_obj_path;
4001         const char *group_obj_path;
4002         int role;
4003 };
4004
4005 static void group_sig_property(const char *key, DBusMessageIter *iter,
4006                                                         void *user_data)
4007 {
4008         struct group_sig_data *data = user_data;
4009
4010         if (!key)
4011                 return;
4012
4013         if (g_strcmp0(key, "peer_interface_addr") == 0) {
4014                 unsigned char *dev_addr;
4015                 DBusMessageIter array;
4016                 int len;
4017
4018                 dbus_message_iter_recurse(iter, &array);
4019                 dbus_message_iter_get_fixed_array(&array, &dev_addr, &len);
4020
4021                 if (len == ETH_ALEN)
4022                         memcpy(data->iface_address, dev_addr, len);
4023         } else if (g_strcmp0(key, "role") == 0) {
4024                 const char *str = NULL;
4025
4026                 dbus_message_iter_get_basic(iter, &str);
4027                 if (g_strcmp0(str, "GO") == 0)
4028                         data->role = G_SUPPLICANT_GROUP_ROLE_GO;
4029                 else
4030                         data->role = G_SUPPLICANT_GROUP_ROLE_CLIENT;
4031         } else if (g_strcmp0(key, "peer_object") == 0)
4032                 dbus_message_iter_get_basic(iter, &data->peer_obj_path);
4033         else if (g_strcmp0(key, "interface_object") == 0)
4034                 dbus_message_iter_get_basic(iter, &data->interface_obj_path);
4035         else if (g_strcmp0(key, "group_object") == 0)
4036                 dbus_message_iter_get_basic(iter, &data->group_obj_path);
4037
4038 }
4039
4040 static void signal_group_success(const char *path, DBusMessageIter *iter)
4041 {
4042         GSupplicantInterface *interface;
4043         struct group_sig_data data = {};
4044         GSupplicantPeer *peer;
4045
4046         SUPPLICANT_DBG("");
4047
4048         interface = g_hash_table_lookup(interface_table, path);
4049         if (!interface)
4050                 return;
4051
4052         supplicant_dbus_property_foreach(iter, group_sig_property, &data);
4053         if (!data.peer_obj_path)
4054                 return;
4055
4056         peer = g_hash_table_lookup(interface->peer_table, data.peer_obj_path);
4057         if (!peer)
4058                 return;
4059
4060         memcpy(peer->iface_address, data.iface_address, ETH_ALEN);
4061         interface->pending_peer_path = peer->path;
4062 }
4063
4064 static void signal_group_failure(const char *path, DBusMessageIter *iter)
4065 {
4066         GSupplicantInterface *interface;
4067         struct group_sig_data data = {};
4068         GSupplicantPeer *peer;
4069
4070         SUPPLICANT_DBG("");
4071
4072         interface = g_hash_table_lookup(interface_table, path);
4073         if (!interface)
4074                 return;
4075
4076         supplicant_dbus_property_foreach(iter, group_sig_property, &data);
4077         if (!data.peer_obj_path)
4078                 return;
4079
4080         peer = g_hash_table_lookup(interface->peer_table, data.peer_obj_path);
4081         if (!peer)
4082                 return;
4083
4084         callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_FAILED);
4085         peer->connection_requested = false;
4086 }
4087
4088 static void signal_group_started(const char *path, DBusMessageIter *iter)
4089 {
4090         GSupplicantInterface *interface, *g_interface;
4091         struct group_sig_data data = {};
4092         GSupplicantGroup *group;
4093         GSupplicantPeer *peer;
4094
4095         SUPPLICANT_DBG("");
4096
4097         interface = g_hash_table_lookup(interface_table, path);
4098         if (!interface)
4099                 return;
4100
4101         supplicant_dbus_property_foreach(iter, group_sig_property, &data);
4102         if (!data.interface_obj_path || !data.group_obj_path)
4103                 return;
4104
4105         peer = g_hash_table_lookup(interface->peer_table,
4106                                                 interface->pending_peer_path);
4107         interface->pending_peer_path = NULL;
4108         if (!peer)
4109                 return;
4110
4111         g_interface = g_hash_table_lookup(interface_table,
4112                                                 data.interface_obj_path);
4113         if (!g_interface)
4114                 return;
4115
4116         group = g_hash_table_lookup(interface->group_table,
4117                                                 data.group_obj_path);
4118         if (group)
4119                 return;
4120
4121         group = g_try_new0(GSupplicantGroup, 1);
4122         if (!group)
4123                 return;
4124
4125         group->interface = g_interface;
4126         group->orig_interface = interface;
4127         group->path = g_strdup(data.group_obj_path);
4128         group->role = data.role;
4129
4130         g_hash_table_insert(interface->group_table, group->path, group);
4131         g_hash_table_replace(group_mapping, group->path, group);
4132
4133         peer->current_group_iface = g_interface;
4134         callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_STARTED);
4135 }
4136
4137 static void remove_peer_group_interface(GHashTable *group_table,
4138                                 const char* path)
4139 {
4140         GSupplicantGroup *group;
4141         GHashTableIter iter;
4142         gpointer value, key;
4143
4144         if (!group_table)
4145                 return;
4146
4147         group = g_hash_table_lookup(group_table, path);
4148
4149         if (!group || !group->orig_interface)
4150                 return;
4151
4152         g_hash_table_iter_init(&iter, group->orig_interface->peer_table);
4153
4154         while (g_hash_table_iter_next(&iter, &key, &value)) {
4155                 GSupplicantPeer *peer = value;
4156
4157                 if (peer->current_group_iface == group->interface)
4158                         peer->current_group_iface = NULL;
4159         }
4160 }
4161
4162 static void signal_group_finished(const char *path, DBusMessageIter *iter)
4163 {
4164         GSupplicantInterface *interface;
4165         struct group_sig_data data = {};
4166
4167         SUPPLICANT_DBG("");
4168
4169         interface = g_hash_table_lookup(interface_table, path);
4170         if (!interface)
4171                 return;
4172
4173         supplicant_dbus_property_foreach(iter, group_sig_property, &data);
4174         if (!data.interface_obj_path || !data.group_obj_path)
4175                 return;
4176
4177         remove_peer_group_interface(interface->group_table, data.group_obj_path);
4178
4179         g_hash_table_remove(group_mapping, data.group_obj_path);
4180
4181         g_hash_table_remove(interface->group_table, data.group_obj_path);
4182 }
4183
4184 static void signal_group_request(const char *path, DBusMessageIter *iter)
4185 {
4186         GSupplicantInterface *interface;
4187         GSupplicantPeer *peer;
4188         const char *obj_path;
4189
4190         SUPPLICANT_DBG("");
4191
4192         interface = g_hash_table_lookup(interface_table, path);
4193         if (!interface)
4194                 return;
4195
4196         dbus_message_iter_get_basic(iter, &obj_path);
4197         if (!obj_path || !g_strcmp0(obj_path, "/"))
4198                 return;
4199
4200         peer = g_hash_table_lookup(interface->peer_table, obj_path);
4201         if (!peer)
4202                 return;
4203
4204         /*
4205          * Peer has been previously found and property set,
4206          * otherwise, defer connection to when peer property
4207          * is set.
4208          */
4209         if (peer->identifier)
4210                 callback_peer_request(peer);
4211         else
4212                 g_hash_table_replace(pending_peer_connection, peer->path, peer);
4213 }
4214
4215 static void signal_group_peer_joined(const char *path, DBusMessageIter *iter)
4216 {
4217         const char *peer_path = NULL;
4218         GSupplicantInterface *interface;
4219         GSupplicantGroup *group;
4220         GSupplicantPeer *peer;
4221
4222         SUPPLICANT_DBG("");
4223
4224         group = g_hash_table_lookup(group_mapping, path);
4225         if (!group)
4226                 return;
4227
4228         dbus_message_iter_get_basic(iter, &peer_path);
4229         if (!peer_path)
4230                 return;
4231
4232         interface = g_hash_table_lookup(peer_mapping, peer_path);
4233         if (!interface)
4234                 return;
4235
4236         peer = g_hash_table_lookup(interface->peer_table, peer_path);
4237         if (!peer)
4238                 return;
4239
4240         group->members = g_slist_prepend(group->members, g_strdup(peer_path));
4241
4242         callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_JOINED);
4243 }
4244
4245 static void signal_group_peer_disconnected(const char *path, DBusMessageIter *iter)
4246 {
4247         const char *peer_path = NULL;
4248         GSupplicantInterface *interface;
4249         GSupplicantGroup *group;
4250         GSupplicantPeer *peer;
4251         GSList *elem;
4252
4253         SUPPLICANT_DBG("");
4254
4255         group = g_hash_table_lookup(group_mapping, path);
4256         if (!group)
4257                 return;
4258
4259         dbus_message_iter_get_basic(iter, &peer_path);
4260         if (!peer_path)
4261                 return;
4262
4263         for (elem = group->members; elem; elem = elem->next) {
4264                 if (!g_strcmp0(elem->data, peer_path))
4265                         break;
4266         }
4267
4268         if (!elem)
4269                 return;
4270
4271         g_free(elem->data);
4272         group->members = g_slist_delete_link(group->members, elem);
4273
4274         interface = g_hash_table_lookup(peer_mapping, peer_path);
4275         if (!interface)
4276                 return;
4277
4278         peer = g_hash_table_lookup(interface->peer_table, peer_path);
4279         if (!peer)
4280                 return;
4281
4282         callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_DISCONNECTED);
4283         peer->connection_requested = false;
4284 }
4285
4286 #if defined TIZEN_EXT_WIFI_MESH
4287 const void *g_supplicant_interface_get_mesh_group_ssid(
4288                                                         GSupplicantInterface *interface,
4289                                                         unsigned int *ssid_len)
4290 {
4291         if (!ssid_len)
4292                 return NULL;
4293
4294         if (!interface || interface->group_info.ssid_len == 0) {
4295                 *ssid_len = 0;
4296                 return NULL;
4297         }
4298
4299         *ssid_len = interface->group_info.ssid_len;
4300         return interface->group_info.ssid;
4301 }
4302
4303 int g_supplicant_mesh_get_disconnect_reason(GSupplicantInterface *interface)
4304 {
4305         if (!interface)
4306                 return -EINVAL;
4307
4308         return interface->group_info.disconnect_reason;
4309 }
4310
4311 const char *g_supplicant_mesh_peer_get_address(GSupplicantMeshPeer *mesh_peer)
4312 {
4313         if (!mesh_peer || !mesh_peer->peer_address)
4314                 return NULL;
4315
4316         return mesh_peer->peer_address;
4317 }
4318
4319 int g_supplicant_mesh_peer_get_disconnect_reason(GSupplicantMeshPeer *mesh_peer)
4320 {
4321         if (!mesh_peer)
4322                 return -EINVAL;
4323
4324         return mesh_peer->disconnect_reason;
4325 }
4326
4327 static void callback_mesh_group_started(GSupplicantInterface *interface)
4328 {
4329         if (!callbacks_pointer)
4330                 return;
4331
4332         if (!callbacks_pointer->mesh_group_started)
4333                 return;
4334
4335         callbacks_pointer->mesh_group_started(interface);
4336 }
4337
4338 static void callback_mesh_group_removed(GSupplicantInterface *interface)
4339 {
4340         if (!callbacks_pointer)
4341                 return;
4342
4343         if (!callbacks_pointer->mesh_group_removed)
4344                 return;
4345
4346         callbacks_pointer->mesh_group_removed(interface);
4347 }
4348
4349 static void mesh_group_info(const char *key, DBusMessageIter *iter,
4350                                                         void *user_data)
4351 {
4352         GSupplicantInterface *interface = user_data;
4353         if (!key)
4354                 return;
4355
4356         if (g_strcmp0(key, "SSID") == 0) {
4357                 DBusMessageIter array;
4358                 unsigned char *ssid;
4359                 int ssid_len;
4360
4361                 dbus_message_iter_recurse(iter, &array);
4362                 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
4363
4364                 if (ssid_len > 0 && ssid_len < 33) {
4365                         memcpy(interface->group_info.ssid, ssid, ssid_len);
4366                         interface->group_info.ssid_len = ssid_len;
4367                 } else {
4368                         memset(interface->group_info.ssid, 0, 32);
4369                         interface->group_info.ssid_len = 0;
4370                 }
4371         } else if (g_strcmp0(key, "DisconnectReason") == 0) {
4372                 int disconnect_reason = 0;
4373                 dbus_message_iter_get_basic(iter, &disconnect_reason);
4374                 interface->group_info.disconnect_reason = disconnect_reason;
4375         }
4376 }
4377
4378 static void signal_mesh_group_started(const char *path, DBusMessageIter *iter)
4379 {
4380         GSupplicantInterface *interface;
4381
4382         interface = g_hash_table_lookup(interface_table, path);
4383         if (!interface)
4384                 return;
4385
4386         supplicant_dbus_property_foreach(iter, mesh_group_info, interface);
4387
4388         callback_mesh_group_started(interface);
4389 }
4390
4391 static void signal_mesh_group_removed(const char *path, DBusMessageIter *iter)
4392 {
4393         GSupplicantInterface *interface;
4394
4395         interface = g_hash_table_lookup(interface_table, path);
4396         if (!interface)
4397                 return;
4398
4399         supplicant_dbus_property_foreach(iter, mesh_group_info, interface);
4400
4401         callback_mesh_group_removed(interface);
4402 }
4403
4404 static void callback_mesh_peer_connected(GSupplicantMeshPeer *mesh_peer)
4405 {
4406         if (!callbacks_pointer)
4407                 return;
4408
4409         if (!callbacks_pointer->mesh_peer_connected)
4410                 return;
4411
4412         callbacks_pointer->mesh_peer_connected(mesh_peer);
4413 }
4414
4415 static void callback_mesh_peer_disconnected(GSupplicantMeshPeer *mesh_peer)
4416 {
4417         if (!callbacks_pointer)
4418                 return;
4419
4420         if (!callbacks_pointer->mesh_peer_disconnected)
4421                 return;
4422
4423         callbacks_pointer->mesh_peer_disconnected(mesh_peer);
4424 }
4425
4426 static void mesh_peer_info(const char *key, DBusMessageIter *iter,
4427                                                         void *user_data)
4428 {
4429         GSupplicantMeshPeer *mesh_peer = user_data;
4430         if (!key)
4431                 return;
4432
4433         if (g_strcmp0(key, "PeerAddress") == 0) {
4434                 DBusMessageIter array;
4435                 unsigned char *addr;
4436                 int addr_len;
4437
4438                 dbus_message_iter_recurse(iter, &array);
4439                 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
4440
4441                 if (addr_len == 6) {
4442                         mesh_peer->peer_address = g_malloc0(19);
4443                         snprintf(mesh_peer->peer_address, 19,
4444                                          "%02x:%02x:%02x:%02x:%02x:%02x", addr[0], addr[1],
4445                                          addr[2], addr[3], addr[4], addr[5]);
4446                 }
4447         } else if (g_strcmp0(key, "DisconnectReason") == 0) {
4448                 int disconnect_reason = 0;
4449                 dbus_message_iter_get_basic(iter, &disconnect_reason);
4450                 mesh_peer->disconnect_reason = disconnect_reason;
4451         }
4452 }
4453
4454 static void signal_mesh_peer_connected(const char *path, DBusMessageIter *iter)
4455 {
4456         GSupplicantInterface *interface;
4457         GSupplicantMeshPeer *mesh_peer;
4458
4459         interface = g_hash_table_lookup(interface_table, path);
4460         if (!interface)
4461                 return;
4462
4463         mesh_peer = dbus_malloc0(sizeof(GSupplicantMeshPeer));
4464         mesh_peer->interface = interface;
4465
4466         supplicant_dbus_property_foreach(iter, mesh_peer_info, mesh_peer);
4467
4468         callback_mesh_peer_connected(mesh_peer);
4469         g_free(mesh_peer->peer_address);
4470         g_free(mesh_peer);
4471 }
4472
4473 static void signal_mesh_peer_disconnected(const char *path,
4474                                                                 DBusMessageIter *iter)
4475 {
4476         GSupplicantInterface *interface;
4477         GSupplicantMeshPeer *mesh_peer;
4478
4479         interface = g_hash_table_lookup(interface_table, path);
4480         if (!interface)
4481                 return;
4482
4483         mesh_peer = dbus_malloc0(sizeof(GSupplicantMeshPeer));
4484         mesh_peer->interface = interface;
4485
4486         supplicant_dbus_property_foreach(iter, mesh_peer_info, mesh_peer);
4487
4488         callback_mesh_peer_disconnected(mesh_peer);
4489         g_free(mesh_peer->peer_address);
4490         g_free(mesh_peer);
4491 }
4492 #endif
4493
4494 static struct {
4495         const char *interface;
4496         const char *member;
4497         void (*function) (const char *path, DBusMessageIter *iter);
4498 } signal_map[] = {
4499         { DBUS_INTERFACE_DBUS,  "NameOwnerChanged",  signal_name_owner_changed },
4500
4501         { SUPPLICANT_INTERFACE, "PropertiesChanged", signal_properties_changed },
4502         { SUPPLICANT_INTERFACE, "InterfaceAdded",    signal_interface_added    },
4503         { SUPPLICANT_INTERFACE, "InterfaceCreated",  signal_interface_added    },
4504         { SUPPLICANT_INTERFACE, "InterfaceRemoved",  signal_interface_removed  },
4505
4506         { SUPPLICANT_INTERFACE ".Interface", "PropertiesChanged", signal_interface_changed },
4507         { SUPPLICANT_INTERFACE ".Interface", "ScanDone",          signal_scan_done         },
4508         { SUPPLICANT_INTERFACE ".Interface", "BSSAdded",          signal_bss_added         },
4509         { SUPPLICANT_INTERFACE ".Interface", "BSSRemoved",        signal_bss_removed       },
4510         { SUPPLICANT_INTERFACE ".Interface", "NetworkAdded",      signal_network_added     },
4511         { SUPPLICANT_INTERFACE ".Interface", "NetworkRemoved",    signal_network_removed   },
4512         { SUPPLICANT_INTERFACE ".Interface", "StaAuthorized",     signal_sta_authorized    },
4513         { SUPPLICANT_INTERFACE ".Interface", "StaDeauthorized",   signal_sta_deauthorized  },
4514
4515         { SUPPLICANT_INTERFACE ".BSS", "PropertiesChanged", signal_bss_changed   },
4516
4517         { SUPPLICANT_INTERFACE ".Interface.WPS", "Credentials", signal_wps_credentials },
4518         { SUPPLICANT_INTERFACE ".Interface.WPS", "Event",       signal_wps_event       },
4519 #if defined TIZEN_EXT
4520         { "org.tizen.system.deviced.PowerOff", "ChangeState", signal_power_off },
4521 #endif
4522
4523         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeviceFound", signal_peer_found },
4524         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeviceLost",  signal_peer_lost  },
4525
4526         { SUPPLICANT_INTERFACE ".Peer", "PropertiesChanged", signal_peer_changed },
4527
4528         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationSuccess", signal_group_success },
4529         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationFailure", signal_group_failure },
4530         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GroupStarted", signal_group_started },
4531         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GroupFinished", signal_group_finished },
4532         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationRequest", signal_group_request },
4533
4534         { SUPPLICANT_INTERFACE ".Group", "PeerJoined", signal_group_peer_joined },
4535         { SUPPLICANT_INTERFACE ".Group", "PeerDisconnected", signal_group_peer_disconnected },
4536 #if defined TIZEN_EXT_WIFI_MESH
4537         { SUPPLICANT_INTERFACE ".Interface.Mesh", "MeshGroupStarted",
4538                 signal_mesh_group_started },
4539         { SUPPLICANT_INTERFACE ".Interface.Mesh", "MeshGroupRemoved",
4540                 signal_mesh_group_removed },
4541         { SUPPLICANT_INTERFACE ".Interface.Mesh", "MeshPeerConnected",
4542                 signal_mesh_peer_connected },
4543         { SUPPLICANT_INTERFACE ".Interface.Mesh", "MeshPeerDisconnected",
4544                 signal_mesh_peer_disconnected },
4545 #endif
4546
4547         { }
4548 };
4549
4550 static DBusHandlerResult g_supplicant_filter(DBusConnection *conn,
4551                                         DBusMessage *message, void *data)
4552 {
4553         DBusMessageIter iter;
4554         const char *path;
4555         int i;
4556
4557         path = dbus_message_get_path(message);
4558         if (!path)
4559                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4560
4561         if (!dbus_message_iter_init(message, &iter))
4562                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4563
4564         for (i = 0; signal_map[i].interface; i++) {
4565                 if (!dbus_message_has_interface(message, signal_map[i].interface))
4566                         continue;
4567
4568                 if (!dbus_message_has_member(message, signal_map[i].member))
4569                         continue;
4570
4571                 signal_map[i].function(path, &iter);
4572                 break;
4573         }
4574
4575         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
4576 }
4577
4578 void g_supplicant_interface_cancel(GSupplicantInterface *interface)
4579 {
4580         SUPPLICANT_DBG("Cancelling any pending DBus calls");
4581         supplicant_dbus_method_call_cancel_all(interface);
4582         supplicant_dbus_property_call_cancel_all(interface);
4583 }
4584
4585 struct supplicant_regdom {
4586         GSupplicantCountryCallback callback;
4587         const char *alpha2;
4588         const void *user_data;
4589 };
4590
4591 static void country_result(const char *error,
4592                                 DBusMessageIter *iter, void *user_data)
4593 {
4594         struct supplicant_regdom *regdom = user_data;
4595         int result = 0;
4596
4597         SUPPLICANT_DBG("Country setting result");
4598
4599         if (!user_data)
4600                 return;
4601
4602         if (error) {
4603                 SUPPLICANT_DBG("Country setting failure %s", error);
4604                 result = -EINVAL;
4605         }
4606
4607         if (regdom->callback)
4608                 regdom->callback(result, regdom->alpha2,
4609                                         (void *) regdom->user_data);
4610
4611         g_free(regdom);
4612 }
4613
4614 static void country_params(DBusMessageIter *iter, void *user_data)
4615 {
4616         struct supplicant_regdom *regdom = user_data;
4617
4618         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
4619                                                         &regdom->alpha2);
4620 }
4621
4622 int g_supplicant_set_country(const char *alpha2,
4623                                 GSupplicantCountryCallback callback,
4624                                         const void *user_data)
4625 {
4626         struct supplicant_regdom *regdom;
4627         int ret;
4628
4629         SUPPLICANT_DBG("Country setting %s", alpha2);
4630
4631         if (!system_available)
4632                 return -EFAULT;
4633
4634         regdom = dbus_malloc0(sizeof(*regdom));
4635         if (!regdom)
4636                 return -ENOMEM;
4637
4638         regdom->callback = callback;
4639         regdom->alpha2 = alpha2;
4640         regdom->user_data = user_data;
4641
4642         ret =  supplicant_dbus_property_set(SUPPLICANT_PATH, SUPPLICANT_INTERFACE,
4643                                         "Country", DBUS_TYPE_STRING_AS_STRING,
4644                                         country_params, country_result,
4645                                         regdom, NULL);
4646         if (ret < 0) {
4647                 dbus_free(regdom);
4648                 SUPPLICANT_DBG("Unable to set Country configuration");
4649         }
4650         return ret;
4651 }
4652
4653 int g_supplicant_interface_set_country(GSupplicantInterface *interface,
4654                                         GSupplicantCountryCallback callback,
4655                                                         const char *alpha2,
4656                                                         void *user_data)
4657 {
4658         struct supplicant_regdom *regdom;
4659         int ret;
4660
4661         regdom = dbus_malloc0(sizeof(*regdom));
4662         if (!regdom)
4663                 return -ENOMEM;
4664
4665         regdom->callback = callback;
4666         regdom->alpha2 = alpha2;
4667         regdom->user_data = user_data;
4668
4669         ret =  supplicant_dbus_property_set(interface->path,
4670                                 SUPPLICANT_INTERFACE ".Interface",
4671                                 "Country", DBUS_TYPE_STRING_AS_STRING,
4672                                 country_params, country_result,
4673                                         regdom, NULL);
4674         if (ret < 0) {
4675                 dbus_free(regdom);
4676                 SUPPLICANT_DBG("Unable to set Country configuration");
4677         }
4678
4679         return ret;
4680 }
4681
4682 bool g_supplicant_interface_has_p2p(GSupplicantInterface *interface)
4683 {
4684         if (!interface)
4685                 return false;
4686
4687         return interface->p2p_support;
4688 }
4689
4690 struct supplicant_p2p_dev_config {
4691         char *device_name;
4692         char *dev_type;
4693 };
4694
4695 static void p2p_device_config_result(const char *error,
4696                                         DBusMessageIter *iter, void *user_data)
4697 {
4698         struct supplicant_p2p_dev_config *config = user_data;
4699
4700         if (error)
4701                 SUPPLICANT_DBG("Unable to set P2P Device configuration: %s",
4702                                                                         error);
4703
4704         g_free(config->device_name);
4705         g_free(config->dev_type);
4706         dbus_free(config);
4707 }
4708
4709 static int dev_type_str2bin(const char *type, unsigned char dev_type[8])
4710 {
4711         int length, pos, end;
4712         char b[3] = {};
4713         char *e = NULL;
4714
4715         end = strlen(type);
4716         for (length = pos = 0; type[pos] != '\0' && length < 8; length++) {
4717                 if (pos+2 > end)
4718                         return 0;
4719
4720                 b[0] = type[pos];
4721                 b[1] = type[pos+1];
4722
4723                 dev_type[length] = strtol(b, &e, 16);
4724                 if (e && *e != '\0')
4725                         return 0;
4726
4727                 pos += 2;
4728         }
4729
4730         return 8;
4731 }
4732
4733 static void p2p_device_config_params(DBusMessageIter *iter, void *user_data)
4734 {
4735         struct supplicant_p2p_dev_config *config = user_data;
4736         DBusMessageIter dict;
4737
4738         supplicant_dbus_dict_open(iter, &dict);
4739
4740         supplicant_dbus_dict_append_basic(&dict, "DeviceName",
4741                                 DBUS_TYPE_STRING, &config->device_name);
4742
4743         if (config->dev_type) {
4744                 unsigned char dev_type[8] = {}, *type;
4745                 int len;
4746
4747                 len = dev_type_str2bin(config->dev_type, dev_type);
4748                 if (len) {
4749                         type = dev_type;
4750                         supplicant_dbus_dict_append_fixed_array(&dict,
4751                                         "PrimaryDeviceType",
4752                                         DBUS_TYPE_BYTE, &type, len);
4753                 }
4754         }
4755
4756         supplicant_dbus_dict_close(iter, &dict);
4757 }
4758
4759 int g_supplicant_interface_set_p2p_device_config(GSupplicantInterface *interface,
4760                                         const char *device_name,
4761                                         const char *primary_dev_type)
4762 {
4763         struct supplicant_p2p_dev_config *config;
4764         int ret;
4765
4766         SUPPLICANT_DBG("P2P Device settings %s/%s",
4767                                         device_name, primary_dev_type);
4768
4769         config = dbus_malloc0(sizeof(*config));
4770         if (!config)
4771                 return -ENOMEM;
4772
4773         config->device_name = g_strdup(device_name);
4774         config->dev_type = g_strdup(primary_dev_type);
4775
4776         ret = supplicant_dbus_property_set(interface->path,
4777                                 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
4778                                 "P2PDeviceConfig",
4779                                 DBUS_TYPE_ARRAY_AS_STRING
4780                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
4781                                 DBUS_TYPE_STRING_AS_STRING
4782                                 DBUS_TYPE_VARIANT_AS_STRING
4783                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
4784                                 p2p_device_config_params,
4785                                 p2p_device_config_result, config, NULL);
4786         if (ret < 0) {
4787                 g_free(config->device_name);
4788                 g_free(config->dev_type);
4789                 dbus_free(config);
4790                 SUPPLICANT_DBG("Unable to set P2P Device configuration");
4791         }
4792
4793         return ret;
4794 }
4795
4796 static gboolean peer_lookup_by_identifier(gpointer key, gpointer value,
4797                                                         gpointer user_data)
4798 {
4799         const GSupplicantPeer *peer = value;
4800         const char *identifier = user_data;
4801
4802         if (!g_strcmp0(identifier, peer->identifier))
4803                 return TRUE;
4804
4805         return FALSE;
4806 }
4807
4808 GSupplicantPeer *g_supplicant_interface_peer_lookup(GSupplicantInterface *interface,
4809                                                         const char *identifier)
4810 {
4811         GSupplicantPeer *peer;
4812
4813         peer = g_hash_table_find(interface->peer_table,
4814                                         peer_lookup_by_identifier,
4815                                         (void *) identifier);
4816         return peer;
4817 }
4818
4819 static void interface_create_data_free(struct interface_create_data *data)
4820 {
4821         g_free(data->ifname);
4822         g_free(data->driver);
4823         g_free(data->bridge);
4824 #if defined TIZEN_EXT_WIFI_MESH
4825         g_free(data->parent_ifname);
4826 #endif
4827         dbus_free(data);
4828 }
4829
4830 static bool interface_exists(GSupplicantInterface *interface,
4831                                 const char *path)
4832 {
4833         GSupplicantInterface *tmp;
4834
4835         tmp = g_hash_table_lookup(interface_table, path);
4836         if (tmp && tmp == interface)
4837                 return true;
4838
4839         return false;
4840 }
4841
4842 static void interface_create_property(const char *key, DBusMessageIter *iter,
4843                                                         void *user_data)
4844 {
4845         struct interface_create_data *data = user_data;
4846         GSupplicantInterface *interface = data->interface;
4847
4848         if (!key) {
4849                 if (data->callback) {
4850                         data->callback(0, data->interface, data->user_data);
4851 #if !defined TIZEN_EXT
4852                         callback_p2p_support(interface);
4853 #endif
4854 #if defined TIZEN_EXT_WIFI_MESH
4855                         callback_mesh_support(interface);
4856 #endif
4857                 }
4858
4859                 interface_create_data_free(data);
4860         }
4861
4862         interface_property(key, iter, interface);
4863 }
4864
4865 static void interface_create_result(const char *error,
4866                                 DBusMessageIter *iter, void *user_data)
4867 {
4868         struct interface_create_data *data = user_data;
4869         const char *path = NULL;
4870         int err;
4871
4872         SUPPLICANT_DBG("");
4873
4874         if (error) {
4875                 g_warning("error %s", error);
4876                 err = -EIO;
4877                 goto done;
4878         }
4879
4880         dbus_message_iter_get_basic(iter, &path);
4881         if (!path) {
4882                 err = -EINVAL;
4883                 goto done;
4884         }
4885
4886         if (!system_available) {
4887                 err = -EFAULT;
4888                 goto done;
4889         }
4890
4891         data->interface = g_hash_table_lookup(interface_table, path);
4892         if (!data->interface) {
4893                 data->interface = interface_alloc(path);
4894                 if (!data->interface) {
4895                         err = -ENOMEM;
4896                         goto done;
4897                 }
4898         }
4899
4900         err = supplicant_dbus_property_get_all(path,
4901                                         SUPPLICANT_INTERFACE ".Interface",
4902                                         interface_create_property, data,
4903                                         NULL);
4904         if (err == 0)
4905                 return;
4906
4907 done:
4908         if (data->callback)
4909                 data->callback(err, NULL, data->user_data);
4910
4911         interface_create_data_free(data);
4912 }
4913
4914 static void interface_create_params(DBusMessageIter *iter, void *user_data)
4915 {
4916         struct interface_create_data *data = user_data;
4917         DBusMessageIter dict;
4918         char *config_file = NULL;
4919
4920         SUPPLICANT_DBG("");
4921
4922         supplicant_dbus_dict_open(iter, &dict);
4923
4924         supplicant_dbus_dict_append_basic(&dict, "Ifname",
4925                                         DBUS_TYPE_STRING, &data->ifname);
4926
4927         if (data->driver)
4928                 supplicant_dbus_dict_append_basic(&dict, "Driver",
4929                                         DBUS_TYPE_STRING, &data->driver);
4930
4931         if (data->bridge)
4932                 supplicant_dbus_dict_append_basic(&dict, "BridgeIfname",
4933                                         DBUS_TYPE_STRING, &data->bridge);
4934
4935         config_file = g_hash_table_lookup(config_file_table, data->ifname);
4936         if (config_file) {
4937                 SUPPLICANT_DBG("[%s] ConfigFile %s", data->ifname, config_file);
4938
4939                 supplicant_dbus_dict_append_basic(&dict, "ConfigFile",
4940                                         DBUS_TYPE_STRING, &config_file);
4941         }
4942
4943 #if defined TIZEN_EXT_WIFI_MESH
4944         if (data->is_mesh_interface) {
4945                 if (data->parent_ifname)
4946                         supplicant_dbus_dict_append_basic(&dict, "ParentIfname",
4947                                         DBUS_TYPE_STRING, &data->parent_ifname);
4948
4949                 supplicant_dbus_dict_append_basic(&dict, "IsMeshInterface",
4950                                         DBUS_TYPE_BOOLEAN, &data->is_mesh_interface);
4951         }
4952 #endif
4953
4954         supplicant_dbus_dict_close(iter, &dict);
4955 }
4956
4957 static void interface_get_result(const char *error,
4958                                 DBusMessageIter *iter, void *user_data)
4959 {
4960         struct interface_create_data *data = user_data;
4961         GSupplicantInterface *interface;
4962         const char *path = NULL;
4963         int err;
4964
4965         SUPPLICANT_DBG("");
4966
4967         if (error) {
4968                 SUPPLICANT_DBG("Interface not created yet");
4969                 goto create;
4970         }
4971
4972         dbus_message_iter_get_basic(iter, &path);
4973         if (!path) {
4974                 err = -EINVAL;
4975                 goto done;
4976         }
4977
4978         interface = g_hash_table_lookup(interface_table, path);
4979         if (!interface) {
4980                 err = -ENOENT;
4981                 goto done;
4982         }
4983
4984         if (data->callback) {
4985                 data->callback(0, interface, data->user_data);
4986 #if !defined TIZEN_EXT
4987                 callback_p2p_support(interface);
4988 #endif
4989 #if defined TIZEN_EXT_WIFI_MESH
4990                 callback_mesh_support(interface);
4991 #endif
4992         }
4993
4994         interface_create_data_free(data);
4995
4996         return;
4997
4998 create:
4999         if (!system_available) {
5000                 err = -EFAULT;
5001                 goto done;
5002         }
5003
5004         SUPPLICANT_DBG("Creating interface");
5005
5006         err = supplicant_dbus_method_call(SUPPLICANT_PATH,
5007                                                 SUPPLICANT_INTERFACE,
5008                                                 "CreateInterface",
5009                                                 interface_create_params,
5010                                                 interface_create_result, data,
5011                                                 NULL);
5012         if (err == 0)
5013                 return;
5014
5015 done:
5016         if (data->callback)
5017                 data->callback(err, NULL, data->user_data);
5018
5019         interface_create_data_free(data);
5020 }
5021
5022 static void interface_get_params(DBusMessageIter *iter, void *user_data)
5023 {
5024         struct interface_create_data *data = user_data;
5025
5026         SUPPLICANT_DBG("");
5027
5028         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
5029 }
5030
5031 #if defined TIZEN_EXT_WIFI_MESH
5032 int g_supplicant_mesh_interface_create(const char *ifname, const char *driver,
5033                                                 const char *bridge, const char *parent_ifname,
5034                                                 GSupplicantInterfaceCallback callback, void *user_data)
5035 {
5036         struct interface_create_data *data;
5037         int ret;
5038
5039         SUPPLICANT_DBG("ifname %s", ifname);
5040
5041         if (!ifname || !parent_ifname)
5042                 return -EINVAL;
5043
5044         if (!system_available)
5045                 return -EFAULT;
5046
5047         data = dbus_malloc0(sizeof(*data));
5048         if (!data)
5049                 return -ENOMEM;
5050
5051         data->ifname = g_strdup(ifname);
5052         data->driver = g_strdup(driver);
5053         data->bridge = g_strdup(bridge);
5054         data->is_mesh_interface = true;
5055         data->parent_ifname = g_strdup(parent_ifname);
5056         data->callback = callback;
5057         data->user_data = user_data;
5058
5059         ret = supplicant_dbus_method_call(SUPPLICANT_PATH,
5060                                                 SUPPLICANT_INTERFACE,
5061                                                 "CreateInterface",
5062                                                 interface_create_params,
5063                                                 interface_create_result, data,
5064                                                 NULL);
5065         return ret;
5066 }
5067
5068 struct interface_mesh_peer_data {
5069         char *peer_address;
5070         char *method;
5071         GSupplicantInterface *interface;
5072         GSupplicantInterfaceCallback callback;
5073         void *user_data;
5074 };
5075
5076 static void interface_mesh_change_peer_params(DBusMessageIter *iter,
5077                                                 void *user_data)
5078 {
5079         struct interface_mesh_peer_data *data = user_data;
5080
5081         SUPPLICANT_DBG("");
5082
5083         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->peer_address);
5084 }
5085
5086 static void interface_mesh_change_peer_result(const char *error,
5087                                 DBusMessageIter *iter, void *user_data)
5088 {
5089         struct interface_mesh_peer_data *data = user_data;
5090         int err = 0;
5091
5092         SUPPLICANT_DBG("%s", data->method);
5093
5094         if (error) {
5095                 err = -EIO;
5096                 SUPPLICANT_DBG("error %s", error);
5097         }
5098
5099         if (data->callback)
5100                 data->callback(err, data->interface, data->user_data);
5101
5102         g_free(data->peer_address);
5103         g_free(data->method);
5104         dbus_free(data);
5105 }
5106
5107 int g_supplicant_interface_mesh_peer_change_status(
5108                                 GSupplicantInterface *interface,
5109                                 GSupplicantInterfaceCallback callback, const char *peer_address,
5110                                 const char *method, void *user_data)
5111 {
5112         struct interface_mesh_peer_data *data;
5113         int ret;
5114
5115         if (!peer_address)
5116                 return -EINVAL;
5117
5118         data = dbus_malloc0(sizeof(*data));
5119         if (!data)
5120                 return -ENOMEM;
5121
5122         data->peer_address = g_strdup(peer_address);
5123         data->method = g_strdup(method);
5124         data->interface = interface;
5125         data->callback = callback;
5126         data->user_data = user_data;
5127
5128         ret = supplicant_dbus_method_call(interface->path,
5129                                                 SUPPLICANT_INTERFACE ".Interface.Mesh",
5130                                                 method, interface_mesh_change_peer_params,
5131                                                 interface_mesh_change_peer_result, data, NULL);
5132         if (ret < 0) {
5133                 g_free(data->peer_address);
5134                 g_free(data->method);
5135                 dbus_free(data);
5136         }
5137
5138         return ret;
5139 }
5140 #endif
5141
5142 int g_supplicant_interface_create(const char *ifname, const char *driver,
5143                                         const char *bridge,
5144                                         GSupplicantInterfaceCallback callback,
5145                                                         void *user_data)
5146 {
5147         struct interface_create_data *data;
5148         int ret;
5149
5150         SUPPLICANT_DBG("ifname %s", ifname);
5151
5152         if (!ifname)
5153                 return -EINVAL;
5154
5155         if (!system_available)
5156                 return -EFAULT;
5157
5158         data = dbus_malloc0(sizeof(*data));
5159         if (!data)
5160                 return -ENOMEM;
5161
5162         data->ifname = g_strdup(ifname);
5163         data->driver = g_strdup(driver);
5164         data->bridge = g_strdup(bridge);
5165         data->callback = callback;
5166         data->user_data = user_data;
5167
5168         ret = supplicant_dbus_method_call(SUPPLICANT_PATH,
5169                                                 SUPPLICANT_INTERFACE,
5170                                                 "GetInterface",
5171                                                 interface_get_params,
5172                                                 interface_get_result, data,
5173                                                 NULL);
5174         if (ret < 0)
5175                 interface_create_data_free(data);
5176
5177         return ret;
5178 }
5179
5180 static void interface_remove_result(const char *error,
5181                                 DBusMessageIter *iter, void *user_data)
5182 {
5183         struct interface_data *data = user_data;
5184         int err;
5185
5186         if (error) {
5187                 err = -EIO;
5188                 SUPPLICANT_DBG("error: %s", error);
5189                 goto done;
5190         }
5191
5192         if (!system_available) {
5193                 err = -EFAULT;
5194                 goto done;
5195         }
5196
5197         /*
5198          * The gsupplicant interface is already freed by the InterfaceRemoved
5199          * signal callback. Simply invoke the interface_data callback.
5200          */
5201         err = 0;
5202
5203 done:
5204         g_free(data->path);
5205
5206         if (data->callback)
5207                 data->callback(err, NULL, data->user_data);
5208
5209         dbus_free(data);
5210 }
5211
5212
5213 static void interface_remove_params(DBusMessageIter *iter, void *user_data)
5214 {
5215         struct interface_data *data = user_data;
5216
5217         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
5218                                                         &data->interface->path);
5219 }
5220
5221
5222 int g_supplicant_interface_remove(GSupplicantInterface *interface,
5223                         GSupplicantInterfaceCallback callback,
5224                                                         void *user_data)
5225 {
5226         struct interface_data *data;
5227         int ret;
5228
5229         if (!interface)
5230                 return -EINVAL;
5231
5232         if (!system_available)
5233                 return -EFAULT;
5234
5235         g_supplicant_interface_cancel(interface);
5236
5237         data = dbus_malloc0(sizeof(*data));
5238         if (!data)
5239                 return -ENOMEM;
5240
5241         data->interface = interface;
5242         data->path = g_strdup(interface->path);
5243         data->callback = callback;
5244         data->user_data = user_data;
5245
5246         ret = supplicant_dbus_method_call(SUPPLICANT_PATH,
5247                                                 SUPPLICANT_INTERFACE,
5248                                                 "RemoveInterface",
5249                                                 interface_remove_params,
5250                                                 interface_remove_result, data,
5251                                                 NULL);
5252         if (ret < 0) {
5253                 g_free(data->path);
5254                 dbus_free(data);
5255         }
5256         return ret;
5257 }
5258
5259 static void interface_scan_result(const char *error,
5260                                 DBusMessageIter *iter, void *user_data)
5261 {
5262         struct interface_scan_data *data = user_data;
5263         int err = 0;
5264
5265         if (error) {
5266                 SUPPLICANT_DBG("error %s", error);
5267                 err = -EIO;
5268         }
5269
5270         /* A non ready interface cannot send/receive anything */
5271         if (interface_exists(data->interface, data->path)) {
5272                 if (!data->interface->ready)
5273                         err = -ENOLINK;
5274         }
5275
5276         g_free(data->path);
5277
5278         if (err != 0) {
5279                 if (data->callback)
5280                         data->callback(err, data->interface, data->user_data);
5281         } else {
5282                 data->interface->scan_callback = data->callback;
5283                 data->interface->scan_data = data->user_data;
5284         }
5285
5286         if (data->scan_params)
5287                 g_supplicant_free_scan_params(data->scan_params);
5288
5289         dbus_free(data);
5290 }
5291
5292 static void add_scan_frequency(DBusMessageIter *iter, unsigned int freq)
5293 {
5294         DBusMessageIter data;
5295         unsigned int width = 0; /* Not used by wpa_supplicant atm */
5296
5297         dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &data);
5298
5299         dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &freq);
5300         dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &width);
5301
5302         dbus_message_iter_close_container(iter, &data);
5303 }
5304
5305 static void add_scan_frequencies(DBusMessageIter *iter,
5306                                                 void *user_data)
5307 {
5308         GSupplicantScanParams *scan_data = user_data;
5309         unsigned int freq;
5310         int i;
5311
5312         for (i = 0; i < scan_data->num_freqs; i++) {
5313                 freq = scan_data->freqs[i];
5314                 if (!freq)
5315                         break;
5316
5317                 add_scan_frequency(iter, freq);
5318         }
5319 }
5320
5321 static void append_ssid(DBusMessageIter *iter,
5322                         const void *ssid, unsigned int len)
5323 {
5324         DBusMessageIter array;
5325
5326         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
5327         DBUS_TYPE_BYTE_AS_STRING, &array);
5328
5329         dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
5330                                                                 &ssid, len);
5331         dbus_message_iter_close_container(iter, &array);
5332 }
5333
5334 static void append_ssids(DBusMessageIter *iter, void *user_data)
5335 {
5336         GSupplicantScanParams *scan_data = user_data;
5337         GSList *list;
5338
5339         for (list = scan_data->ssids; list; list = list->next) {
5340                 struct scan_ssid *scan_ssid = list->data;
5341
5342                 append_ssid(iter, scan_ssid->ssid, scan_ssid->ssid_len);
5343         }
5344 }
5345
5346 static void supplicant_add_scan_frequency(DBusMessageIter *dict,
5347                 supplicant_dbus_array_function function,
5348                                         void *user_data)
5349 {
5350         GSupplicantScanParams *scan_params = user_data;
5351         DBusMessageIter entry, value, array;
5352         const char *key = "Channels";
5353
5354         if (scan_params->freqs && scan_params->freqs[0] != 0) {
5355                 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
5356                                                 NULL, &entry);
5357
5358                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
5359
5360                 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
5361                                         DBUS_TYPE_ARRAY_AS_STRING
5362                                         DBUS_STRUCT_BEGIN_CHAR_AS_STRING
5363                                         DBUS_TYPE_UINT32_AS_STRING
5364                                         DBUS_TYPE_UINT32_AS_STRING
5365                                         DBUS_STRUCT_END_CHAR_AS_STRING,
5366                                         &value);
5367
5368                 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
5369                                         DBUS_STRUCT_BEGIN_CHAR_AS_STRING
5370                                         DBUS_TYPE_UINT32_AS_STRING
5371                                         DBUS_TYPE_UINT32_AS_STRING
5372                                         DBUS_STRUCT_END_CHAR_AS_STRING,
5373                                         &array);
5374
5375                 if (function)
5376                         function(&array, user_data);
5377
5378                 dbus_message_iter_close_container(&value, &array);
5379                 dbus_message_iter_close_container(&entry, &value);
5380                 dbus_message_iter_close_container(dict, &entry);
5381         }
5382 }
5383
5384 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
5385 {
5386         DBusMessageIter dict;
5387         const char *type = "passive";
5388         struct interface_scan_data *data = user_data;
5389
5390         supplicant_dbus_dict_open(iter, &dict);
5391
5392         if (data && data->scan_params) {
5393                 type = "active";
5394
5395                 supplicant_dbus_dict_append_basic(&dict, "Type",
5396                                         DBUS_TYPE_STRING, &type);
5397
5398 #if defined TIZEN_EXT
5399                 SUPPLICANT_DBG("[specific_scan] num_ssids %d",
5400                                data->scan_params->num_ssids);
5401 #endif
5402
5403                 if (data->scan_params->ssids) {
5404                         supplicant_dbus_dict_append_array(&dict, "SSIDs",
5405                                                         DBUS_TYPE_STRING,
5406                                                         append_ssids,
5407                                                         data->scan_params);
5408                 }
5409                 supplicant_add_scan_frequency(&dict, add_scan_frequencies,
5410                                                 data->scan_params);
5411         } else
5412                 supplicant_dbus_dict_append_basic(&dict, "Type",
5413                                         DBUS_TYPE_STRING, &type);
5414
5415         supplicant_dbus_dict_close(iter, &dict);
5416 }
5417
5418 static int interface_ready_to_scan(GSupplicantInterface *interface)
5419 {
5420         if (!interface)
5421                 return -EINVAL;
5422
5423         if (!system_available)
5424                 return -EFAULT;
5425
5426         if (interface->scanning)
5427                 return -EALREADY;
5428
5429         switch (interface->state) {
5430         case G_SUPPLICANT_STATE_AUTHENTICATING:
5431         case G_SUPPLICANT_STATE_ASSOCIATING:
5432         case G_SUPPLICANT_STATE_ASSOCIATED:
5433         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
5434         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
5435                 return -EBUSY;
5436 #if defined TIZEN_EXT
5437         case G_SUPPLICANT_STATE_DISABLED:
5438                 return -ENOLINK;
5439         case G_SUPPLICANT_STATE_UNKNOWN:
5440 #else
5441         case G_SUPPLICANT_STATE_UNKNOWN:
5442         case G_SUPPLICANT_STATE_DISABLED:
5443 #endif
5444         case G_SUPPLICANT_STATE_DISCONNECTED:
5445         case G_SUPPLICANT_STATE_INACTIVE:
5446         case G_SUPPLICANT_STATE_SCANNING:
5447         case G_SUPPLICANT_STATE_COMPLETED:
5448                 break;
5449         }
5450
5451         return 0;
5452 }
5453
5454 #if defined TIZEN_EXT_WIFI_MESH
5455 static void interface_abort_scan_result(const char *error,
5456                                 DBusMessageIter *iter, void *user_data)
5457 {
5458         struct interface_scan_data *data = user_data;
5459         int err = 0;
5460
5461         if (error) {
5462                 SUPPLICANT_DBG("error %s", error);
5463                 err = -EIO;
5464         }
5465
5466         g_free(data->path);
5467
5468                 if (data->callback)
5469                         data->callback(err, data->interface, data->user_data);
5470
5471         dbus_free(data);
5472 }
5473
5474 int g_supplicant_interface_abort_scan(GSupplicantInterface *interface,
5475                                 GSupplicantInterfaceCallback callback, void *user_data)
5476 {
5477         struct interface_scan_data *data;
5478         int ret;
5479
5480         if (!interface->scanning)
5481                 return -EEXIST;
5482
5483         data = dbus_malloc0(sizeof(*data));
5484         if (!data)
5485                 return -ENOMEM;
5486
5487         data->interface = interface;
5488         data->path = g_strdup(interface->path);
5489         data->callback = callback;
5490         data->user_data = user_data;
5491
5492         ret = supplicant_dbus_method_call(interface->path,
5493                         SUPPLICANT_INTERFACE ".Interface", "AbortScan", NULL,
5494                         interface_abort_scan_result, data, interface);
5495
5496         if (ret < 0) {
5497                 g_free(data->path);
5498                 dbus_free(data);
5499         }
5500
5501         return ret;
5502 }
5503 #endif
5504
5505 int g_supplicant_interface_scan(GSupplicantInterface *interface,
5506                                 GSupplicantScanParams *scan_data,
5507                                 GSupplicantInterfaceCallback callback,
5508                                                         void *user_data)
5509 {
5510         struct interface_scan_data *data;
5511         int ret;
5512
5513         ret = interface_ready_to_scan(interface);
5514         if (ret)
5515                 return ret;
5516
5517         data = dbus_malloc0(sizeof(*data));
5518         if (!data)
5519                 return -ENOMEM;
5520
5521         data->interface = interface;
5522         data->path = g_strdup(interface->path);
5523 #if defined TIZEN_EXT
5524         data->interface->scan_callback = data->callback = callback;
5525         data->interface->scan_data = data->user_data = user_data;
5526 #else
5527         data->callback = callback;
5528         data->user_data = user_data;
5529 #endif
5530         data->scan_params = scan_data;
5531
5532         interface->scan_callback = callback;
5533         interface->scan_data = user_data;
5534
5535         ret = supplicant_dbus_method_call(interface->path,
5536                         SUPPLICANT_INTERFACE ".Interface", "Scan",
5537                         interface_scan_params, interface_scan_result, data,
5538                         interface);
5539
5540         if (ret < 0) {
5541                 g_free(data->path);
5542                 dbus_free(data);
5543         }
5544
5545         return ret;
5546 }
5547
5548 #if defined TIZEN_EXT
5549 static void interface_signalpoll_result(const char *error,
5550                                 DBusMessageIter *iter, void *user_data)
5551 {
5552         struct interface_signalpoll_data *data = user_data;
5553         int err = 0;
5554         dbus_int32_t maxspeed = 0;
5555         unsigned char strength = 0;
5556         DBusMessageIter sub_iter, dict;
5557
5558         if (error) {
5559                 err = -EIO;
5560                 SUPPLICANT_DBG("error: %s", error);
5561                 goto out;
5562         }
5563
5564         dbus_message_iter_get_arg_type(iter);
5565         dbus_message_iter_recurse(iter, &sub_iter);
5566         dbus_message_iter_recurse(&sub_iter, &dict);
5567
5568         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
5569                 DBusMessageIter entry, value;
5570                 const char *key;
5571
5572                 dbus_message_iter_recurse(&dict, &entry);
5573                 dbus_message_iter_get_basic(&entry, &key);
5574                 dbus_message_iter_next(&entry);
5575                 dbus_message_iter_recurse(&entry, &value);
5576
5577                 switch (dbus_message_iter_get_arg_type(&value)) {
5578                 case DBUS_TYPE_INT32:
5579                         if (g_strcmp0(key, "linkspeed") == 0) {
5580                                 dbus_message_iter_get_basic(&value, &maxspeed);
5581                                 SUPPLICANT_DBG("linkspeed = %d", maxspeed);
5582                                 break;
5583                         }
5584                 case DBUS_TYPE_BYTE:
5585                         if (g_strcmp0(key, "rssi") == 0) {
5586                                 dbus_message_iter_get_basic(&value, &strength);
5587                                 SUPPLICANT_DBG("Strength = %d", strength);
5588                                 break;
5589                         }
5590                 }
5591                 dbus_message_iter_next(&dict);
5592         }
5593
5594 out:
5595         if(data->callback)
5596                 data->callback(err, maxspeed, strength, data->user_data);
5597
5598         g_free(data->path);
5599         dbus_free(data);
5600 }
5601
5602 int g_supplicant_interface_signalpoll(GSupplicantInterface *interface,
5603                                 GSupplicantMaxSpeedCallback callback,
5604                                 void *user_data)
5605 {
5606         struct interface_signalpoll_data *data;
5607         int ret;
5608
5609         if (!interface)
5610                 return -EINVAL;
5611
5612         if (!system_available)
5613                 return -EFAULT;
5614
5615         data = dbus_malloc0(sizeof(*data));
5616         if (!data)
5617                 return -ENOMEM;
5618
5619         data->interface = interface;
5620         data->path = g_strdup(interface->path);
5621         data->callback = callback;
5622         data->user_data = user_data;
5623
5624         ret = supplicant_dbus_method_call(interface->path,
5625                         SUPPLICANT_INTERFACE ".Interface", "SignalPoll",
5626                         NULL, interface_signalpoll_result, data,
5627                         interface);
5628
5629         if (ret < 0) {
5630                 g_free(data->path);
5631                 dbus_free(data);
5632         }
5633
5634         return ret;
5635 }
5636 #endif
5637
5638 static int parse_supplicant_error(DBusMessageIter *iter)
5639 {
5640         int err = -ECONNABORTED;
5641         char *key;
5642
5643         if (!iter)
5644                 return err;
5645
5646         /* If the given passphrase is malformed wpa_s returns
5647          * "invalid message format" but this error should be interpreted as
5648          * invalid-key.
5649          */
5650         while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
5651                 dbus_message_iter_get_basic(iter, &key);
5652                 if (strncmp(key, "psk", 3) == 0 ||
5653                                 strncmp(key, "wep_key", 7) == 0 ||
5654                                 strcmp(key, "invalid message format") == 0) {
5655                         err = -ENOKEY;
5656                         break;
5657                 }
5658                 dbus_message_iter_next(iter);
5659         }
5660
5661         return err;
5662 }
5663
5664 static void interface_select_network_result(const char *error,
5665                                 DBusMessageIter *iter, void *user_data)
5666 {
5667         struct interface_connect_data *data = user_data;
5668         int err;
5669
5670         SUPPLICANT_DBG("");
5671
5672         err = 0;
5673         if (error) {
5674 #if defined TIZEN_EXT
5675                 SUPPLICANT_DBG("SelectNetwork errorFreq %s", error);
5676 #else
5677                 SUPPLICANT_DBG("SelectNetwork error %s", error);
5678 #endif
5679                 err = parse_supplicant_error(iter);
5680         }
5681
5682         g_free(data->path);
5683
5684         if (data->callback)
5685                 data->callback(err, data->interface, data->user_data);
5686
5687 #if defined TIZEN_EXT
5688         g_free(data->ssid->ssid);
5689         g_free((char *)data->ssid->passphrase);
5690 #endif
5691         g_free(data->ssid);
5692         dbus_free(data);
5693 }
5694
5695 static void interface_select_network_params(DBusMessageIter *iter,
5696                                                         void *user_data)
5697 {
5698         struct interface_connect_data *data = user_data;
5699         GSupplicantInterface *interface = data->interface;
5700 #if defined TIZEN_EXT
5701         GSupplicantSSID *ssid = data->ssid;
5702 #endif
5703
5704         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
5705                                         &interface->network_path);
5706 #if defined TIZEN_EXT
5707         if (!ssid->bssid_for_connect_len)
5708                 dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &ssid->freq);
5709 #endif
5710 }
5711
5712 static void interface_add_network_result(const char *error,
5713                                 DBusMessageIter *iter, void *user_data)
5714 {
5715         struct interface_connect_data *data = user_data;
5716         GSupplicantInterface *interface = data->interface;
5717         const char *path;
5718         int err;
5719
5720         if (error)
5721                 goto error;
5722
5723         dbus_message_iter_get_basic(iter, &path);
5724         if (!path)
5725                 goto error;
5726
5727         SUPPLICANT_DBG("PATH: %s", path);
5728
5729 #if defined TIZEN_EXT
5730         if (interface->network_path)
5731                 g_free(interface->network_path);
5732 #endif
5733         interface->network_path = g_strdup(path);
5734
5735         store_network_information(interface, data->ssid);
5736
5737 #if defined TIZEN_EXT
5738         SUPPLICANT_DBG(".Interface.SelectNetworkFreq");
5739         GSupplicantSSID *ssid = data->ssid;
5740
5741         if (!ssid->bssid_for_connect_len)
5742                 supplicant_dbus_method_call(data->interface->path,
5743                                 SUPPLICANT_INTERFACE ".Interface", "SelectNetworkFreq",
5744                                 interface_select_network_params,
5745                                 interface_select_network_result, data,
5746                                 interface);
5747         else
5748                 supplicant_dbus_method_call(data->interface->path,
5749                                 SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
5750                                 interface_select_network_params,
5751                                 interface_select_network_result, data,
5752                                 interface);
5753 #else
5754         supplicant_dbus_method_call(data->interface->path,
5755                         SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
5756                         interface_select_network_params,
5757                         interface_select_network_result, data,
5758                         interface);
5759 #endif
5760
5761         return;
5762
5763 error:
5764         SUPPLICANT_DBG("AddNetwork error %s", error);
5765
5766         if (interface_exists(data->interface, data->interface->path)) {
5767                 err = parse_supplicant_error(iter);
5768                 if (data->callback)
5769                         data->callback(err, data->interface, data->user_data);
5770
5771                 g_free(interface->network_path);
5772                 interface->network_path = NULL;
5773         }
5774
5775         g_free(data->path);
5776 #if defined TIZEN_EXT
5777         g_free(data->ssid->ssid);
5778         g_free((char *)data->ssid->passphrase);
5779 #endif
5780         g_free(data->ssid);
5781         g_free(data);
5782 }
5783
5784 static void add_network_security_none(DBusMessageIter *dict)
5785 {
5786         const char *auth_alg = "OPEN";
5787
5788         supplicant_dbus_dict_append_basic(dict, "auth_alg",
5789                                         DBUS_TYPE_STRING, &auth_alg);
5790 }
5791
5792 static void add_network_security_wep(DBusMessageIter *dict,
5793                                         GSupplicantSSID *ssid)
5794 {
5795         const char *auth_alg = "OPEN SHARED";
5796         dbus_uint32_t key_index = 0;
5797
5798         supplicant_dbus_dict_append_basic(dict, "auth_alg",
5799                                         DBUS_TYPE_STRING, &auth_alg);
5800
5801         if (ssid->passphrase) {
5802                 int size = strlen(ssid->passphrase);
5803                 if (size == 10 || size == 26) {
5804                         unsigned char *key = g_try_malloc(13);
5805                         char tmp[3];
5806                         int i;
5807
5808                         memset(tmp, 0, sizeof(tmp));
5809                         if (!key)
5810                                 size = 0;
5811
5812                         for (i = 0; i < size / 2; i++) {
5813                                 memcpy(tmp, ssid->passphrase + (i * 2), 2);
5814                                 key[i] = (unsigned char) strtol(tmp, NULL, 16);
5815                         }
5816
5817                         supplicant_dbus_dict_append_fixed_array(dict,
5818                                                         "wep_key0",
5819                                                         DBUS_TYPE_BYTE,
5820                                                         &key, size / 2);
5821                         g_free(key);
5822                 } else if (size == 5 || size == 13) {
5823                         unsigned char *key = g_try_malloc(13);
5824                         int i;
5825
5826                         if (!key)
5827                                 size = 0;
5828
5829                         for (i = 0; i < size; i++)
5830                                 key[i] = (unsigned char) ssid->passphrase[i];
5831
5832                         supplicant_dbus_dict_append_fixed_array(dict,
5833                                                                 "wep_key0",
5834                                                                 DBUS_TYPE_BYTE,
5835                                                                 &key, size);
5836                         g_free(key);
5837                 } else
5838                         supplicant_dbus_dict_append_basic(dict,
5839                                                         "wep_key0",
5840                                                         DBUS_TYPE_STRING,
5841                                                         &ssid->passphrase);
5842
5843                 supplicant_dbus_dict_append_basic(dict, "wep_tx_keyidx",
5844                                         DBUS_TYPE_UINT32, &key_index);
5845         }
5846 }
5847
5848 static dbus_bool_t is_psk_raw_key(const char *psk)
5849 {
5850         int i;
5851
5852         /* A raw key is always 64 bytes length... */
5853         if (strlen(psk) != 64)
5854                 return FALSE;
5855
5856         /* ... and its content is in hex representation */
5857         for (i = 0; i < 64; i++)
5858                 if (!isxdigit((unsigned char) psk[i]))
5859                         return FALSE;
5860
5861         return TRUE;
5862 }
5863
5864 static unsigned char hexchar2bin(char c)
5865 {
5866         if ((c >= '0') && (c <= '9'))
5867                 return c - '0';
5868         else if ((c >= 'A') && (c <= 'F'))
5869                 return c - 'A' + 10;
5870         else if ((c >= 'a') && (c <= 'f'))
5871                 return c - 'a' + 10;
5872         else
5873                 return c;
5874 }
5875
5876 static void hexstring2bin(const char *string, unsigned char *data,
5877                                 size_t data_len)
5878 {
5879         size_t i;
5880
5881         for (i = 0; i < data_len; i++)
5882                 data[i] = (hexchar2bin(string[i * 2 + 0]) << 4 |
5883                            hexchar2bin(string[i * 2 + 1]) << 0);
5884 }
5885
5886 static void add_network_security_psk(DBusMessageIter *dict,
5887                                         GSupplicantSSID *ssid)
5888 {
5889         if (ssid->passphrase && strlen(ssid->passphrase) > 0) {
5890                 const char *key = "psk";
5891
5892                 if (is_psk_raw_key(ssid->passphrase)) {
5893                         unsigned char data[32];
5894                         unsigned char *datap = data;
5895
5896                         /* The above pointer alias is required by D-Bus because
5897                          * with D-Bus and GCC, non-heap-allocated arrays cannot
5898                          * be passed directly by their base pointer. */
5899
5900                         hexstring2bin(ssid->passphrase, datap, sizeof(data));
5901
5902                         supplicant_dbus_dict_append_fixed_array(dict,
5903                                                         key, DBUS_TYPE_BYTE,
5904                                                         &datap, sizeof(data));
5905                 } else
5906                         supplicant_dbus_dict_append_basic(dict,
5907                                                         key, DBUS_TYPE_STRING,
5908                                                         &ssid->passphrase);
5909         }
5910 }
5911
5912 static void add_network_security_tls(DBusMessageIter *dict,
5913                                         GSupplicantSSID *ssid)
5914 {
5915         /*
5916          * For TLS, we at least need:
5917          *              The client certificate
5918          *              The client private key file
5919          *              The client private key file password
5920          *
5921          * The Authority certificate is optional.
5922          */
5923         if (!ssid->client_cert_path)
5924                 return;
5925
5926         if (!ssid->private_key_path)
5927                 return;
5928
5929 #if !defined TIZEN_EXT
5930         if (!ssid->private_key_passphrase)
5931                 return;
5932 #endif
5933
5934         if (ssid->ca_cert_path)
5935                 supplicant_dbus_dict_append_basic(dict, "ca_cert",
5936                                         DBUS_TYPE_STRING, &ssid->ca_cert_path);
5937
5938         supplicant_dbus_dict_append_basic(dict, "private_key",
5939                                                 DBUS_TYPE_STRING,
5940                                                 &ssid->private_key_path);
5941 #if !defined TIZEN_EXT
5942         supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
5943                                                 DBUS_TYPE_STRING,
5944                                                 &ssid->private_key_passphrase);
5945 #endif
5946         supplicant_dbus_dict_append_basic(dict, "client_cert",
5947                                                 DBUS_TYPE_STRING,
5948                                                 &ssid->client_cert_path);
5949 }
5950
5951 static void add_network_security_peap(DBusMessageIter *dict,
5952                                         GSupplicantSSID *ssid)
5953 {
5954         char *phase2_auth;
5955
5956         /*
5957          * For PEAP/TTLS, we at least need
5958          *              The authority certificate
5959          *              The 2nd phase authentication method
5960          *              The 2nd phase passphrase
5961          *
5962          * The Client certificate is optional although strongly recommended
5963          * When setting it, we need in addition
5964          *              The Client private key file
5965          *              The Client private key file password
5966          */
5967         if (!ssid->passphrase)
5968                 return;
5969
5970         if (!ssid->phase2_auth)
5971                 return;
5972
5973         if (ssid->client_cert_path) {
5974                 if (!ssid->private_key_path)
5975                         return;
5976
5977 #if !defined TIZEN_EXT
5978                 if (!ssid->private_key_passphrase)
5979                         return;
5980 #endif
5981
5982                 supplicant_dbus_dict_append_basic(dict, "client_cert",
5983                                                 DBUS_TYPE_STRING,
5984                                                 &ssid->client_cert_path);
5985
5986                 supplicant_dbus_dict_append_basic(dict, "private_key",
5987                                                 DBUS_TYPE_STRING,
5988                                                 &ssid->private_key_path);
5989
5990 #if !defined TIZEN_EXT
5991                 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
5992                                                 DBUS_TYPE_STRING,
5993                                                 &ssid->private_key_passphrase);
5994 #endif
5995
5996         }
5997
5998         if(g_strcmp0(ssid->phase2_auth, "GTC") == 0 && g_strcmp0(ssid->eap, "ttls") == 0)
5999                 phase2_auth = g_strdup_printf("autheap=%s", ssid->phase2_auth);
6000         else if (g_str_has_prefix(ssid->phase2_auth, "EAP-")) {
6001                 phase2_auth = g_strdup_printf("autheap=%s",
6002                                         ssid->phase2_auth + strlen("EAP-"));
6003         } else
6004                 phase2_auth = g_strdup_printf("auth=%s", ssid->phase2_auth);
6005
6006         supplicant_dbus_dict_append_basic(dict, "password",
6007                                                 DBUS_TYPE_STRING,
6008                                                 &ssid->passphrase);
6009
6010         if (ssid->ca_cert_path)
6011                 supplicant_dbus_dict_append_basic(dict, "ca_cert",
6012                                                 DBUS_TYPE_STRING,
6013                                                 &ssid->ca_cert_path);
6014
6015         supplicant_dbus_dict_append_basic(dict, "phase2",
6016                                                 DBUS_TYPE_STRING,
6017                                                 &phase2_auth);
6018
6019         g_free(phase2_auth);
6020 }
6021
6022 #if defined TIZEN_EXT
6023 static void add_network_security_aka_sim(DBusMessageIter *dict,
6024                                         GSupplicantSSID *ssid)
6025 {
6026         if (!ssid->passphrase)
6027                 return;
6028
6029         supplicant_dbus_dict_append_basic(dict, "password",
6030                         DBUS_TYPE_STRING,
6031                         &ssid->passphrase);
6032 }
6033
6034 static void add_network_security_fast(DBusMessageIter *dict,
6035                 GSupplicantSSID *ssid)
6036 {
6037         /*
6038          * For FAST, we at least need:
6039          *              id / password
6040          *              phase1 (provisiong information)
6041          *              pac_file
6042          */
6043
6044         /* Allow provisioing both authenticated and unauthenticated */
6045         const char *phase1 = "fast_provisioning=2";
6046         supplicant_dbus_dict_append_basic(dict, "phase1",
6047                         DBUS_TYPE_STRING,
6048                         &phase1);
6049
6050         SUPPLICANT_DBG("pac_file [%s]", ssid->pac_file);
6051         if(ssid->pac_file)
6052                 supplicant_dbus_dict_append_basic(dict, "pac_file",
6053                                 DBUS_TYPE_STRING,
6054                                 &ssid->pac_file);
6055
6056         supplicant_dbus_dict_append_basic(dict, "password",
6057                         DBUS_TYPE_STRING,
6058                         &ssid->passphrase);
6059 }
6060 #endif
6061
6062 static void add_network_security_eap(DBusMessageIter *dict,
6063                                         GSupplicantSSID *ssid)
6064 {
6065         char *eap_value;
6066
6067 #if defined TIZEN_EXT
6068         if (!ssid->eap)
6069 #else
6070         if (!ssid->eap || !ssid->identity)
6071 #endif
6072                 return;
6073
6074         if (g_strcmp0(ssid->eap, "tls") == 0) {
6075                 add_network_security_tls(dict, ssid);
6076         } else if (g_strcmp0(ssid->eap, "peap") == 0 ||
6077                                 g_strcmp0(ssid->eap, "ttls") == 0) {
6078 #if defined TIZEN_EXT
6079                 if (!ssid->identity)
6080                         return;
6081 #endif
6082                 add_network_security_peap(dict, ssid);
6083
6084 #if defined TIZEN_EXT
6085         } else if (g_strcmp0(ssid->eap, "sim") == 0 ||
6086                         g_strcmp0(ssid->eap, "aka") == 0 ||
6087                         g_strcmp0(ssid->eap, "aka'") == 0) {
6088                 add_network_security_aka_sim(dict, ssid);
6089         } else if (g_strcmp0(ssid->eap, "pwd") == 0) {
6090                 if(!ssid->passphrase)
6091                         return;
6092                 supplicant_dbus_dict_append_basic(dict, "password",
6093                                 DBUS_TYPE_STRING,
6094                                 &ssid->passphrase);
6095         } else if (g_strcmp0(ssid->eap, "fast") == 0){
6096                 if (!ssid->identity || !ssid->passphrase)
6097                         return;
6098
6099                 add_network_security_fast(dict, ssid);
6100 #endif
6101         } else
6102                 return;
6103
6104         eap_value = g_ascii_strup(ssid->eap, -1);
6105
6106         supplicant_dbus_dict_append_basic(dict, "eap",
6107                                                 DBUS_TYPE_STRING,
6108                                                 &eap_value);
6109 #if defined TIZEN_EXT
6110         if (ssid->identity != NULL)
6111                 supplicant_dbus_dict_append_basic(dict, "identity",
6112                                                         DBUS_TYPE_STRING,
6113                                                         &ssid->identity);
6114 #else
6115         supplicant_dbus_dict_append_basic(dict, "identity",
6116                                                 DBUS_TYPE_STRING,
6117                                                 &ssid->identity);
6118 #endif
6119         if(ssid->anonymous_identity)
6120                 supplicant_dbus_dict_append_basic(dict, "anonymous_identity",
6121                                                      DBUS_TYPE_STRING,
6122                                                      &ssid->anonymous_identity);
6123
6124         if(ssid->subject_match)
6125                 supplicant_dbus_dict_append_basic(dict, "subject_match",
6126                                                      DBUS_TYPE_STRING,
6127                                                      &ssid->subject_match);
6128
6129         if(ssid->altsubject_match)
6130                 supplicant_dbus_dict_append_basic(dict, "altsubject_match",
6131                                                      DBUS_TYPE_STRING,
6132                                                      &ssid->altsubject_match);
6133
6134         if(ssid->domain_suffix_match)
6135                 supplicant_dbus_dict_append_basic(dict, "domain_suffix_match",
6136                                                      DBUS_TYPE_STRING,
6137                                                      &ssid->domain_suffix_match);
6138
6139         if(ssid->domain_match)
6140                 supplicant_dbus_dict_append_basic(dict, "domain_match",
6141                                                      DBUS_TYPE_STRING,
6142                                                      &ssid->domain_match);
6143
6144         g_free(eap_value);
6145 }
6146
6147 static void add_network_security_ciphers(DBusMessageIter *dict,
6148                                                 GSupplicantSSID *ssid)
6149 {
6150         unsigned int p_cipher, g_cipher, i;
6151         char *pairwise, *group;
6152         char *pair_ciphers[4];
6153         char *group_ciphers[5];
6154
6155         p_cipher = ssid->pairwise_cipher;
6156         g_cipher = ssid->group_cipher;
6157
6158         if (p_cipher == 0 && g_cipher == 0)
6159                 return;
6160
6161         i = 0;
6162
6163         if (p_cipher & G_SUPPLICANT_PAIRWISE_CCMP)
6164                 pair_ciphers[i++] = "CCMP";
6165
6166         if (p_cipher & G_SUPPLICANT_PAIRWISE_TKIP)
6167                 pair_ciphers[i++] = "TKIP";
6168
6169         if (p_cipher & G_SUPPLICANT_PAIRWISE_NONE)
6170                 pair_ciphers[i++] = "NONE";
6171
6172         pair_ciphers[i] = NULL;
6173
6174         i = 0;
6175
6176         if (g_cipher & G_SUPPLICANT_GROUP_CCMP)
6177                 group_ciphers[i++] = "CCMP";
6178
6179         if (g_cipher & G_SUPPLICANT_GROUP_TKIP)
6180                 group_ciphers[i++] = "TKIP";
6181
6182         if (g_cipher & G_SUPPLICANT_GROUP_WEP104)
6183                 group_ciphers[i++] = "WEP104";
6184
6185         if (g_cipher & G_SUPPLICANT_GROUP_WEP40)
6186                 group_ciphers[i++] = "WEP40";
6187
6188         group_ciphers[i] = NULL;
6189
6190         pairwise = g_strjoinv(" ", pair_ciphers);
6191         group = g_strjoinv(" ", group_ciphers);
6192
6193         SUPPLICANT_DBG("cipher %s %s", pairwise, group);
6194
6195         supplicant_dbus_dict_append_basic(dict, "pairwise",
6196                                                 DBUS_TYPE_STRING,
6197                                                 &pairwise);
6198         supplicant_dbus_dict_append_basic(dict, "group",
6199                                                 DBUS_TYPE_STRING,
6200                                                 &group);
6201
6202         g_free(pairwise);
6203         g_free(group);
6204 }
6205
6206 static void add_network_security_proto(DBusMessageIter *dict,
6207                                                 GSupplicantSSID *ssid)
6208 {
6209         unsigned int protocol, i;
6210         char *proto;
6211         char *protos[3];
6212
6213         protocol = ssid->protocol;
6214
6215         if (protocol == 0)
6216                 return;
6217
6218         i = 0;
6219
6220         if (protocol & G_SUPPLICANT_PROTO_RSN)
6221                 protos[i++] = "RSN";
6222
6223         if (protocol & G_SUPPLICANT_PROTO_WPA)
6224                 protos[i++] = "WPA";
6225
6226         protos[i] = NULL;
6227
6228         proto = g_strjoinv(" ", protos);
6229
6230         SUPPLICANT_DBG("proto %s", proto);
6231
6232         supplicant_dbus_dict_append_basic(dict, "proto",
6233                                                 DBUS_TYPE_STRING,
6234                                                 &proto);
6235
6236         g_free(proto);
6237 }
6238
6239 #if defined TIZEN_EXT
6240 static void add_network_ieee80211w(DBusMessageIter *dict, GSupplicantSSID *ssid)
6241 {
6242         if (ssid->security != G_SUPPLICANT_SECURITY_SAE
6243                         && ssid->security != G_SUPPLICANT_SECURITY_OWE)
6244                 return;
6245
6246         supplicant_dbus_dict_append_basic(dict, "ieee80211w", DBUS_TYPE_UINT32,
6247                                           &ssid->ieee80211w);
6248 }
6249 #endif
6250
6251 static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid)
6252 {
6253         char *key_mgmt;
6254
6255         switch (ssid->security) {
6256         case G_SUPPLICANT_SECURITY_NONE:
6257                 key_mgmt = "NONE";
6258                 add_network_security_none(dict);
6259                 add_network_security_ciphers(dict, ssid);
6260                 break;
6261         case G_SUPPLICANT_SECURITY_UNKNOWN:
6262         case G_SUPPLICANT_SECURITY_WEP:
6263                 key_mgmt = "NONE";
6264                 add_network_security_wep(dict, ssid);
6265                 add_network_security_ciphers(dict, ssid);
6266                 break;
6267         case G_SUPPLICANT_SECURITY_PSK:
6268                 key_mgmt = "WPA-PSK";
6269                 add_network_security_psk(dict, ssid);
6270                 add_network_security_ciphers(dict, ssid);
6271                 add_network_security_proto(dict, ssid);
6272                 break;
6273         case G_SUPPLICANT_SECURITY_IEEE8021X:
6274                 key_mgmt = "WPA-EAP";
6275                 add_network_security_eap(dict, ssid);
6276                 add_network_security_ciphers(dict, ssid);
6277                 add_network_security_proto(dict, ssid);
6278                 break;
6279 #if defined TIZEN_EXT
6280         case G_SUPPLICANT_SECURITY_FT_PSK:
6281                 key_mgmt = "FT-PSK";
6282                 add_network_security_psk(dict, ssid);
6283                 add_network_security_ciphers(dict, ssid);
6284                 add_network_security_proto(dict, ssid);
6285                 break;
6286         case G_SUPPLICANT_SECURITY_FT_IEEE8021X:
6287                 key_mgmt = "FT-EAP";
6288                 add_network_security_eap(dict, ssid);
6289                 add_network_security_ciphers(dict, ssid);
6290                 add_network_security_proto(dict, ssid);
6291                 break;
6292         case G_SUPPLICANT_SECURITY_SAE:
6293                 key_mgmt = "SAE";
6294                 add_network_security_psk(dict, ssid);
6295                 break;
6296         case G_SUPPLICANT_SECURITY_OWE:
6297                 key_mgmt = "OWE";
6298                 add_network_security_ciphers(dict, ssid);
6299                 add_network_security_proto(dict, ssid);
6300                 break;
6301 #endif
6302         }
6303
6304         supplicant_dbus_dict_append_basic(dict, "key_mgmt",
6305                                 DBUS_TYPE_STRING, &key_mgmt);
6306 }
6307
6308 static void add_network_mode(DBusMessageIter *dict, GSupplicantSSID *ssid)
6309 {
6310         dbus_uint32_t mode;
6311
6312         switch (ssid->mode) {
6313         case G_SUPPLICANT_MODE_UNKNOWN:
6314         case G_SUPPLICANT_MODE_INFRA:
6315                 mode = 0;
6316                 break;
6317         case G_SUPPLICANT_MODE_IBSS:
6318                 mode = 1;
6319                 break;
6320         case G_SUPPLICANT_MODE_MASTER:
6321                 mode = 2;
6322                 break;
6323 #if defined TIZEN_EXT_WIFI_MESH
6324         case G_SUPPLICANT_MODE_MESH:
6325                 mode = 5;
6326                 break;
6327 #endif
6328         }
6329
6330         supplicant_dbus_dict_append_basic(dict, "mode",
6331                                 DBUS_TYPE_UINT32, &mode);
6332 }
6333
6334 static void interface_add_network_params(DBusMessageIter *iter, void *user_data)
6335 {
6336         DBusMessageIter dict;
6337         struct interface_connect_data *data = user_data;
6338         GSupplicantSSID *ssid = data->ssid;
6339
6340         supplicant_dbus_dict_open(iter, &dict);
6341
6342         if (ssid->scan_ssid)
6343                 supplicant_dbus_dict_append_basic(&dict, "scan_ssid",
6344                                          DBUS_TYPE_UINT32, &ssid->scan_ssid);
6345
6346         if (ssid->freq)
6347                 supplicant_dbus_dict_append_basic(&dict, "frequency",
6348                                          DBUS_TYPE_UINT32, &ssid->freq);
6349
6350         if (ssid->bgscan)
6351                 supplicant_dbus_dict_append_basic(&dict, "bgscan",
6352                                         DBUS_TYPE_STRING, &ssid->bgscan);
6353
6354         add_network_mode(&dict, ssid);
6355
6356         add_network_security(&dict, ssid);
6357
6358 #if defined TIZEN_EXT
6359         add_network_ieee80211w(&dict, ssid);
6360 #endif
6361
6362         supplicant_dbus_dict_append_fixed_array(&dict, "ssid",
6363                                         DBUS_TYPE_BYTE, &ssid->ssid,
6364                                                 ssid->ssid_len);
6365
6366 #if defined TIZEN_EXT
6367         if (ssid->bssid) {
6368                 char *bssid = NULL;
6369                 bssid = g_try_malloc0(18);
6370                 if (bssid == NULL) {
6371                         SUPPLICANT_DBG("memory allocation error");
6372                         supplicant_dbus_dict_close(iter, &dict);
6373                         return;
6374                 }
6375
6376                 if (ssid->bssid_for_connect_len)
6377                         snprintf(bssid, 18, "%02x:%02x:%02x:%02x:%02x:%02x",
6378                                         ssid->bssid_for_connect[0], ssid->bssid_for_connect[1], ssid->bssid_for_connect[2],
6379                                         ssid->bssid_for_connect[3], ssid->bssid_for_connect[4], ssid->bssid_for_connect[5]);
6380                 else
6381                         snprintf(bssid, 18, "%02x:%02x:%02x:%02x:%02x:%02x",
6382                                         ssid->bssid[0], ssid->bssid[1], ssid->bssid[2],
6383                                         ssid->bssid[3], ssid->bssid[4], ssid->bssid[5]);
6384
6385                 supplicant_dbus_dict_append_basic(&dict, "bssid",
6386                                         DBUS_TYPE_STRING, &bssid);
6387                 g_free(bssid);
6388         }
6389 #endif
6390
6391         supplicant_dbus_dict_close(iter, &dict);
6392 }
6393
6394 static void interface_wps_start_result(const char *error,
6395                                 DBusMessageIter *iter, void *user_data)
6396 {
6397         struct interface_connect_data *data = user_data;
6398         int err;
6399
6400         SUPPLICANT_DBG("");
6401
6402         err = 0;
6403         if (error) {
6404                 SUPPLICANT_DBG("error: %s", error);
6405                 err = parse_supplicant_error(iter);
6406         }
6407
6408         if(data->callback)
6409                 data->callback(err, data->interface, data->user_data);
6410
6411         g_free(data->path);
6412         g_free(data->ssid);
6413         dbus_free(data);
6414 }
6415
6416 static void interface_add_wps_params(DBusMessageIter *iter, void *user_data)
6417 {
6418         struct interface_connect_data *data = user_data;
6419         GSupplicantSSID *ssid = data->ssid;
6420         const char *role = "enrollee", *type;
6421         DBusMessageIter dict;
6422
6423         SUPPLICANT_DBG("");
6424
6425         supplicant_dbus_dict_open(iter, &dict);
6426
6427         supplicant_dbus_dict_append_basic(&dict, "Role",
6428                                                 DBUS_TYPE_STRING, &role);
6429
6430         type = "pbc";
6431         if (ssid->pin_wps) {
6432                 type = "pin";
6433                 supplicant_dbus_dict_append_basic(&dict, "Pin",
6434                                         DBUS_TYPE_STRING, &ssid->pin_wps);
6435         }
6436
6437         supplicant_dbus_dict_append_basic(&dict, "Type",
6438                                         DBUS_TYPE_STRING, &type);
6439
6440 #if defined TIZEN_EXT
6441         if (ssid->bssid)
6442                 supplicant_dbus_dict_append_fixed_array(&dict, "Bssid",
6443                                                 DBUS_TYPE_BYTE, &ssid->bssid, 6);
6444 #endif
6445
6446         supplicant_dbus_dict_close(iter, &dict);
6447 }
6448
6449 static void wps_start(const char *error, DBusMessageIter *iter, void *user_data)
6450 {
6451         struct interface_connect_data *data = user_data;
6452
6453         SUPPLICANT_DBG("");
6454
6455         if (error) {
6456                 SUPPLICANT_DBG("error: %s", error);
6457                 g_free(data->path);
6458                 g_free(data->ssid);
6459                 dbus_free(data);
6460                 return;
6461         }
6462
6463 #if defined TIZEN_EXT
6464         GSupplicantSSID *ssid = data->ssid;
6465         if (ssid->pin_wps != NULL) {
6466                 if (!g_utf8_validate(ssid->pin_wps, 8, NULL)) {
6467                         SUPPLICANT_DBG("Invalid characters in WPS_PIN");
6468                         g_free(data->ssid);
6469                         dbus_free(data);
6470                         return;
6471                 }
6472         }
6473 #endif
6474         supplicant_dbus_method_call(data->interface->path,
6475                         SUPPLICANT_INTERFACE ".Interface.WPS", "Start",
6476                         interface_add_wps_params,
6477                         interface_wps_start_result, data, NULL);
6478 }
6479
6480 static void wps_process_credentials(DBusMessageIter *iter, void *user_data)
6481 {
6482         dbus_bool_t credentials = TRUE;
6483
6484         SUPPLICANT_DBG("");
6485
6486         dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &credentials);
6487 }
6488
6489
6490 #if defined TIZEN_EXT
6491 #define NETCONFIG_SERVICE "net.netconfig"
6492 #define NETCONFIG_WIFI_PATH "/net/netconfig/wifi"
6493 #define NETCONFIG_WIFI_INTERFACE NETCONFIG_SERVICE ".wifi"
6494
6495 struct dec_method_call_data {
6496         struct interface_connect_data *data;
6497         DBusPendingCall *pending_call;
6498 };
6499
6500 static struct dec_method_call_data decrypt_request_data;
6501
6502 static void crypt_method_call_cancel(void)
6503 {
6504         if (decrypt_request_data.pending_call) {
6505                 dbus_pending_call_cancel(decrypt_request_data.pending_call);
6506                 dbus_pending_call_unref(decrypt_request_data.pending_call);
6507                 decrypt_request_data.pending_call = NULL;
6508         }
6509
6510         g_free(decrypt_request_data.data->path);
6511         g_free(decrypt_request_data.data->ssid);
6512         dbus_free(decrypt_request_data.data);
6513         decrypt_request_data.data = NULL;
6514 }
6515
6516 static void decryption_request_reply(DBusPendingCall *call,
6517                                                 void *user_data)
6518 {
6519         DBusMessage *reply;
6520         DBusError error;
6521         DBusMessageIter args;
6522         char *out_data;
6523         int ret;
6524         struct interface_connect_data *data = user_data;
6525
6526         SUPPLICANT_DBG("");
6527
6528         reply = dbus_pending_call_steal_reply(call);
6529
6530         dbus_error_init(&error);
6531         if (dbus_set_error_from_message(&error, reply)) {
6532                 SUPPLICANT_DBG("decryption_request_reply() %s %s", error.name, error.message);
6533                 dbus_error_free(&error);
6534                 ret = -EINVAL;
6535                 goto done;
6536         }
6537
6538         if (dbus_message_iter_init(reply, &args) == FALSE) {
6539                 SUPPLICANT_DBG("dbus_message_iter_init() failed");
6540                 ret = -EINVAL;
6541                 goto done;
6542         }
6543
6544         dbus_message_iter_get_basic(&args, &out_data);
6545         data->ssid->passphrase = g_strdup((const gchar *)out_data);
6546
6547         ret = supplicant_dbus_method_call(data->interface->path,
6548                 SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
6549                 interface_add_network_params,
6550                 interface_add_network_result, data,
6551                 data->interface);
6552
6553 done:
6554         if (ret < 0) {
6555                 SUPPLICANT_DBG("AddNetwork failed %d", ret);
6556                 callback_assoc_failed(decrypt_request_data.data->user_data);
6557                 g_free(data->path);
6558                 g_free(data->ssid->ssid);
6559                 g_free((char *)data->ssid->passphrase);
6560                 g_free(data->ssid);
6561                 dbus_free(data);
6562         }
6563
6564         dbus_message_unref(reply);
6565         dbus_pending_call_unref(call);
6566
6567         decrypt_request_data.pending_call = NULL;
6568         decrypt_request_data.data = NULL;
6569 }
6570
6571 static int send_decryption_request(const char *passphrase,
6572                         struct interface_connect_data *data)
6573 {
6574         DBusMessage *msg = NULL;
6575         DBusPendingCall *call;
6576
6577         SUPPLICANT_DBG("Decryption request");
6578
6579         if (!passphrase) {
6580                 SUPPLICANT_DBG("Invalid parameter");
6581                 return -EINVAL;
6582         }
6583
6584         if (!connection)
6585                 return -EINVAL;
6586
6587         msg = dbus_message_new_method_call(NETCONFIG_SERVICE, NETCONFIG_WIFI_PATH,
6588                         NETCONFIG_WIFI_INTERFACE, "DecryptPassphrase");
6589         if (!msg)
6590                 return -EINVAL;
6591
6592         dbus_message_append_args(msg, DBUS_TYPE_STRING, &passphrase,
6593                                                         DBUS_TYPE_INVALID);
6594
6595         if (!dbus_connection_send_with_reply(connection, msg,
6596                                 &call, DBUS_TIMEOUT_USE_DEFAULT)) {
6597                 dbus_message_unref(msg);
6598                 return -EIO;
6599         }
6600
6601         if (!call) {
6602                 dbus_message_unref(msg);
6603                 return -EIO;
6604         }
6605
6606         decrypt_request_data.pending_call = call;
6607         decrypt_request_data.data = data;
6608
6609         dbus_pending_call_set_notify(call, decryption_request_reply, data, NULL);
6610         dbus_message_unref(msg);
6611
6612         SUPPLICANT_DBG("Decryption request succeeded");
6613
6614         return 0;
6615 }
6616 #endif
6617
6618 int g_supplicant_interface_connect(GSupplicantInterface *interface,
6619                                 GSupplicantSSID *ssid,
6620                                 GSupplicantInterfaceCallback callback,
6621                                                         void *user_data)
6622 {
6623         struct interface_connect_data *data;
6624         struct interface_data *intf_data;
6625         int ret = 0;
6626
6627         SUPPLICANT_DBG("");
6628
6629         if (!interface)
6630                 return -EINVAL;
6631
6632         if (!system_available)
6633                 return -EFAULT;
6634
6635         /* TODO: Check if we're already connected and switch */
6636
6637         data = dbus_malloc0(sizeof(*data));
6638         if (!data)
6639                 return -ENOMEM;
6640
6641         data->interface = interface;
6642         data->path = g_strdup(interface->path);
6643         data->callback = callback;
6644         data->ssid = ssid;
6645         data->user_data = user_data;
6646
6647         if (ssid->use_wps) {
6648                 g_free(interface->wps_cred.key);
6649                 memset(&interface->wps_cred, 0,
6650                                 sizeof(struct _GSupplicantWpsCredentials));
6651
6652                 ret = supplicant_dbus_property_set(interface->path,
6653                         SUPPLICANT_INTERFACE ".Interface.WPS",
6654                         "ProcessCredentials", DBUS_TYPE_BOOLEAN_AS_STRING,
6655                         wps_process_credentials, wps_start, data, interface);
6656         } else {
6657                 /* By the time there is a request for connect and the network
6658                  * path is not NULL it means that connman has not removed the
6659                  * previous network pointer. This can happen in the case AP
6660                  * deauthenticated client and connman does not remove the
6661                  * previously connected network pointer. This causes supplicant
6662                  * to reallocate the memory for struct wpa_ssid again even if it
6663                  * is the same SSID. This causes memory usage of wpa_supplicnat
6664                  * to go high. The idea here is that if the previously connected
6665                  * network is not removed at the time of next connection attempt
6666                  * check if the network path is not NULL. In case it is non-NULL
6667                  * first remove the network and then once removal is successful, add
6668                  * the network.
6669                  */
6670
6671                 if (interface->network_path != NULL) {
6672                         g_free(data->path);
6673                         dbus_free(data);
6674
6675                         /*
6676                          * If this add network is for the same network for
6677                          * which wpa_supplicant already has a profile then do
6678                          * not need to add another profile. Only if the
6679                          * profile that needs to get added is different from
6680                          * what is there in wpa_s delete the current one. A
6681                          * network is identified by its SSID, security_type
6682                          * and passphrase (private passphrase in case security
6683                          * type is 802.11x).
6684                          */
6685                         if (compare_network_parameters(interface, ssid)) {
6686                                 return -EALREADY;
6687                         }
6688
6689                         intf_data = dbus_malloc0(sizeof(*intf_data));
6690                         if (!intf_data)
6691                                 return -ENOMEM;
6692
6693                         intf_data->interface = interface;
6694                         intf_data->path = g_strdup(interface->path);
6695                         intf_data->callback = callback;
6696                         intf_data->ssid = ssid;
6697                         intf_data->user_data = user_data;
6698                         intf_data->network_remove_in_progress = TRUE;
6699                         network_remove(intf_data);
6700                 } else {
6701 #if defined TIZEN_EXT
6702                         if (ssid->passphrase &&
6703                             g_strcmp0(ssid->passphrase, "") != 0 &&
6704 #if defined TIZEN_EXT_WIFI_MESH
6705                             ssid->mode != G_SUPPLICANT_MODE_MESH &&
6706 #endif
6707                             !ssid->eap) {
6708                                 ret = send_decryption_request(ssid->passphrase, data);
6709                                 if (ret < 0)
6710                                         SUPPLICANT_DBG("Decryption request failed %d", ret);
6711                         } else
6712 #endif
6713                         ret = supplicant_dbus_method_call(interface->path,
6714                                         SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
6715                                         interface_add_network_params,
6716                                         interface_add_network_result, data,
6717                                         interface);
6718                 }
6719         }
6720
6721         if (ret < 0) {
6722                 g_free(data->path);
6723                 dbus_free(data);
6724                 return ret;
6725         }
6726
6727         return -EINPROGRESS;
6728 }
6729
6730 static void network_remove_result(const char *error,
6731                                 DBusMessageIter *iter, void *user_data)
6732 {
6733         struct interface_data *data = user_data;
6734         struct interface_connect_data *connect_data;
6735         int result = 0;
6736
6737         SUPPLICANT_DBG("");
6738
6739         if (error) {
6740                 result = -EIO;
6741                 SUPPLICANT_DBG("error: %s", error);
6742
6743                 if (g_strcmp0("org.freedesktop.DBus.Error.UnknownMethod",
6744                                                 error) == 0)
6745                         result = -ECONNABORTED;
6746         }
6747
6748         g_free(data->interface->network_path);
6749         data->interface->network_path = NULL;
6750
6751         remove_network_information(data->interface);
6752
6753         if (data->network_remove_in_progress == TRUE) {
6754                 data->network_remove_in_progress = FALSE;
6755                 connect_data = dbus_malloc0(sizeof(*connect_data));
6756                 if (!connect_data)
6757                         return;
6758
6759                 connect_data->interface = data->interface;
6760                 connect_data->path = g_strdup(data->path);
6761                 connect_data->callback = data->callback;
6762                 connect_data->ssid = data->ssid;
6763                 connect_data->user_data = data->user_data;
6764
6765 #if defined TIZEN_EXT
6766                 int ret;
6767                 if (data->ssid->passphrase && g_strcmp0(data->ssid->passphrase, "") != 0
6768                         && !data->ssid->eap) {
6769                         ret = send_decryption_request(data->ssid->passphrase, connect_data);
6770                         if (ret < 0) {
6771                                 SUPPLICANT_DBG("Decryption request failed %d", ret);
6772                                 g_free(connect_data->ssid);
6773                                 g_free(connect_data->path);
6774                                 dbus_free(connect_data);
6775                         }
6776                 } else
6777 #endif
6778                 supplicant_dbus_method_call(data->interface->path,
6779                         SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
6780                         interface_add_network_params,
6781                         interface_add_network_result, connect_data,
6782                         connect_data->interface);
6783         } else {
6784                 if (data->callback)
6785                         data->callback(result, data->interface, data->user_data);
6786         }
6787         g_free(data->path);
6788         dbus_free(data);
6789 }
6790
6791 static void network_remove_params(DBusMessageIter *iter, void *user_data)
6792 {
6793         struct interface_data *data = user_data;
6794         const char *path = data->interface->network_path;
6795
6796         SUPPLICANT_DBG("path %s", path);
6797
6798         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
6799 }
6800
6801 static int network_remove(struct interface_data *data)
6802 {
6803         GSupplicantInterface *interface = data->interface;
6804
6805         SUPPLICANT_DBG("");
6806
6807 #if defined TIZEN_EXT
6808         GSupplicantInterface *intf = NULL;
6809         /*
6810          * Check if 'interface' is valid
6811          */
6812         intf = g_hash_table_lookup(interface_table, interface->path);
6813         if (intf == NULL)
6814                 return -EINVAL;
6815 #endif
6816
6817         return supplicant_dbus_method_call(interface->path,
6818                         SUPPLICANT_INTERFACE ".Interface", "RemoveNetwork",
6819                         network_remove_params, network_remove_result, data,
6820                         interface);
6821 }
6822
6823 static void interface_disconnect_result(const char *error,
6824                                 DBusMessageIter *iter, void *user_data)
6825 {
6826         struct interface_data *data = user_data;
6827         int result = 0;
6828
6829         SUPPLICANT_DBG("");
6830
6831         if (error) {
6832                 result = -EIO;
6833                 SUPPLICANT_DBG("error: %s", error);
6834
6835                 if (g_strcmp0("org.freedesktop.DBus.Error.UnknownMethod",
6836                                                 error) == 0)
6837                         result = -ECONNABORTED;
6838         }
6839
6840         /* If we are disconnecting from previous WPS successful
6841          * association. i.e.: it did not went through AddNetwork,
6842          * and interface->network_path was never set. */
6843         if (!data->interface->network_path) {
6844                 if (data->callback)
6845                         data->callback(result, data->interface,
6846                                                         data->user_data);
6847
6848                 g_free(data->path);
6849                 dbus_free(data);
6850                 return;
6851         }
6852
6853         if (result < 0 && data->callback) {
6854                 data->callback(result, data->interface, data->user_data);
6855                 data->callback = NULL;
6856         }
6857
6858         if (result != -ECONNABORTED) {
6859                 if (network_remove(data) < 0) {
6860                         g_free(data->path);
6861                         dbus_free(data);
6862                 }
6863         } else {
6864                 g_free(data->path);
6865                 dbus_free(data);
6866         }
6867 }
6868
6869 int g_supplicant_interface_disconnect(GSupplicantInterface *interface,
6870                                         GSupplicantInterfaceCallback callback,
6871                                                         void *user_data)
6872 {
6873         struct interface_data *data;
6874         int ret;
6875
6876         SUPPLICANT_DBG("");
6877
6878         if (!interface)
6879                 return -EINVAL;
6880
6881         if (!system_available)
6882                 return -EFAULT;
6883 #if defined TIZEN_EXT
6884         if (decrypt_request_data.pending_call &&
6885                         decrypt_request_data.data &&
6886                         decrypt_request_data.data->user_data == user_data) {
6887
6888                 callback_assoc_failed(decrypt_request_data.data->user_data);
6889                 crypt_method_call_cancel();
6890
6891                 return 0;
6892         }
6893 #endif
6894         data = dbus_malloc0(sizeof(*data));
6895         if (!data)
6896                 return -ENOMEM;
6897
6898         data->interface = interface;
6899         data->path = g_strdup(interface->path);
6900         data->callback = callback;
6901         data->user_data = user_data;
6902
6903         ret = supplicant_dbus_method_call(interface->path,
6904                         SUPPLICANT_INTERFACE ".Interface", "Disconnect",
6905                         NULL, interface_disconnect_result, data,
6906                         interface);
6907
6908         if (ret < 0) {
6909                 g_free(data->path);
6910                 dbus_free(data);
6911         }
6912
6913         return ret;
6914 }
6915
6916 static void interface_p2p_find_result(const char *error,
6917                                         DBusMessageIter *iter, void *user_data)
6918 {
6919         struct interface_scan_data *data = user_data;
6920         int err = 0;
6921
6922         SUPPLICANT_DBG("error %s", error);
6923
6924         if (error)
6925                 err = -EIO;
6926
6927         if (interface_exists(data->interface, data->path)) {
6928                 if (!data->interface->ready)
6929                         err = -ENOLINK;
6930                 if (!err)
6931                         data->interface->p2p_finding = true;
6932         }
6933
6934         if (data->callback)
6935                 data->callback(err, data->interface, data->user_data);
6936
6937         g_free(data->path);
6938         dbus_free(data);
6939 }
6940
6941 static void interface_p2p_find_params(DBusMessageIter *iter, void *user_data)
6942 {
6943         DBusMessageIter dict;
6944
6945         supplicant_dbus_dict_open(iter, &dict);
6946         supplicant_dbus_dict_close(iter, &dict);
6947 }
6948
6949 int g_supplicant_interface_p2p_find(GSupplicantInterface *interface,
6950                                         GSupplicantInterfaceCallback callback,
6951                                                         void *user_data)
6952 {
6953         struct interface_scan_data *data;
6954         int ret;
6955
6956         if (!interface->p2p_support)
6957                 return -ENOTSUP;
6958
6959         ret = interface_ready_to_scan(interface);
6960         if (ret && ret != -EALREADY)
6961                 return ret;
6962
6963         data = dbus_malloc0(sizeof(*data));
6964         if (!data)
6965                 return -ENOMEM;
6966
6967         data->interface = interface;
6968         data->path = g_strdup(interface->path);
6969         data->callback = callback;
6970         data->user_data = user_data;
6971
6972         ret = supplicant_dbus_method_call(interface->path,
6973                         SUPPLICANT_INTERFACE ".Interface.P2PDevice", "Find",
6974                         interface_p2p_find_params, interface_p2p_find_result,
6975                         data, interface);
6976         if (ret < 0) {
6977                 g_free(data->path);
6978                 dbus_free(data);
6979         }
6980
6981         return ret;
6982 }
6983
6984 bool g_supplicant_interface_is_p2p_finding(GSupplicantInterface *interface)
6985 {
6986         if (!interface)
6987                 return false;
6988
6989         return interface->p2p_finding;
6990 }
6991
6992 int g_supplicant_interface_p2p_stop_find(GSupplicantInterface *interface)
6993 {
6994         if (!interface->p2p_finding)
6995                 return 0;
6996
6997         SUPPLICANT_DBG("");
6998
6999         interface->p2p_finding = false;
7000
7001         return supplicant_dbus_method_call(interface->path,
7002                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "StopFind",
7003                 NULL, NULL, NULL, NULL);
7004 }
7005
7006 static void interface_p2p_connect_result(const char *error,
7007                                         DBusMessageIter *iter, void *user_data)
7008 {
7009         struct interface_connect_data *data = user_data;
7010         int err = 0;
7011
7012         SUPPLICANT_DBG("");
7013
7014         if (error) {
7015                 SUPPLICANT_DBG("error: %s", error);
7016                 err = parse_supplicant_error(iter);
7017         }
7018
7019         if (data->callback)
7020                 data->callback(err, data->interface, data->user_data);
7021
7022         g_free(data->path);
7023         g_free(data->peer->wps_pin);
7024         g_free(data->peer->path);
7025         g_free(data->peer);
7026         g_free(data);
7027 }
7028
7029 static void interface_p2p_connect_params(DBusMessageIter *iter, void *user_data)
7030 {
7031         struct interface_connect_data *data = user_data;
7032         const char *wps = "pbc";
7033         DBusMessageIter dict;
7034         int go_intent = 7;
7035
7036         SUPPLICANT_DBG("");
7037
7038         supplicant_dbus_dict_open(iter, &dict);
7039
7040         if (data->peer->master)
7041                 go_intent = 15;
7042
7043         if (data->peer->wps_pin)
7044                 wps = "pin";
7045
7046         supplicant_dbus_dict_append_basic(&dict, "peer",
7047                                 DBUS_TYPE_OBJECT_PATH, &data->peer->path);
7048         supplicant_dbus_dict_append_basic(&dict, "wps_method",
7049                                 DBUS_TYPE_STRING, &wps);
7050         if (data->peer->wps_pin) {
7051                 supplicant_dbus_dict_append_basic(&dict, "pin",
7052                                 DBUS_TYPE_STRING, &data->peer->wps_pin);
7053         }
7054
7055         supplicant_dbus_dict_append_basic(&dict, "go_intent",
7056                                         DBUS_TYPE_INT32, &go_intent);
7057
7058         supplicant_dbus_dict_close(iter, &dict);
7059 }
7060
7061 int g_supplicant_interface_p2p_connect(GSupplicantInterface *interface,
7062                                         GSupplicantPeerParams *peer_params,
7063                                         GSupplicantInterfaceCallback callback,
7064                                         void *user_data)
7065 {
7066         struct interface_connect_data *data;
7067         int ret;
7068
7069         SUPPLICANT_DBG("");
7070
7071         if (!interface->p2p_support)
7072                 return -ENOTSUP;
7073
7074         data = dbus_malloc0(sizeof(*data));
7075         if (!data)
7076                 return -ENOMEM;
7077
7078         data->interface = interface;
7079         data->path = g_strdup(interface->path);
7080         data->peer = peer_params;
7081         data->callback = callback;
7082         data->user_data = user_data;
7083
7084         ret = supplicant_dbus_method_call(interface->path,
7085                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "Connect",
7086                 interface_p2p_connect_params, interface_p2p_connect_result,
7087                 data, interface);
7088         if (ret < 0) {
7089                 g_free(data->path);
7090                 dbus_free(data);
7091                 return ret;
7092         }
7093
7094         return -EINPROGRESS;
7095 }
7096
7097 int g_supplicant_interface_p2p_disconnect(GSupplicantInterface *interface,
7098                                         GSupplicantPeerParams *peer_params)
7099 {
7100         GSupplicantPeer *peer;
7101         int count = 0;
7102         GSList *list;
7103
7104         SUPPLICANT_DBG("");
7105
7106         if (!interface->p2p_support)
7107                 return -ENOTSUP;
7108
7109         peer = g_hash_table_lookup(interface->peer_table, peer_params->path);
7110         if (!peer)
7111                 return -ENODEV;
7112
7113         for (list = peer->groups; list; list = list->next, count++) {
7114                 const char *group_obj_path = list->data;
7115                 GSupplicantInterface *g_interface;
7116                 GSupplicantGroup *group;
7117
7118                 group = g_hash_table_lookup(group_mapping, group_obj_path);
7119                 if (!group || !group->interface)
7120                         continue;
7121
7122                 g_interface = group->interface;
7123                 supplicant_dbus_method_call(g_interface->path,
7124                                 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
7125                                 "Disconnect", NULL, NULL, NULL, g_interface);
7126         }
7127
7128         if (count == 0 && peer->current_group_iface) {
7129                 supplicant_dbus_method_call(peer->current_group_iface->path,
7130                                 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
7131                                 "Disconnect", NULL, NULL, NULL,
7132                                 peer->current_group_iface->path);
7133         }
7134
7135         peer->current_group_iface = NULL;
7136
7137         return -EINPROGRESS;
7138 }
7139
7140 struct p2p_service_data {
7141         bool registration;
7142         GSupplicantInterface *interface;
7143         GSupplicantP2PServiceParams *service;
7144         GSupplicantInterfaceCallback callback;
7145         void *user_data;
7146 };
7147
7148 static void interface_p2p_service_result(const char *error,
7149                                         DBusMessageIter *iter, void *user_data)
7150 {
7151         struct p2p_service_data *data = user_data;
7152         int result = 0;
7153
7154         SUPPLICANT_DBG("%s result - %s", data->registration ?
7155                                 "Registration" : "Deletion",
7156                                 error ? error : "Success");
7157         if (error)
7158                 result = -EINVAL;
7159
7160         if (data->callback)
7161                 data->callback(result, data->interface, data->user_data);
7162
7163         g_free(data->service->query);
7164         g_free(data->service->response);
7165         g_free(data->service->service);
7166         g_free(data->service->wfd_ies);
7167         g_free(data->service);
7168         dbus_free(data);
7169 }
7170
7171 static void interface_p2p_service_params(DBusMessageIter *iter,
7172                                                         void *user_data)
7173 {
7174         struct p2p_service_data *data = user_data;
7175         GSupplicantP2PServiceParams *service;
7176         DBusMessageIter dict;
7177         const char *type;
7178
7179         SUPPLICANT_DBG("");
7180
7181         service = data->service;
7182
7183         supplicant_dbus_dict_open(iter, &dict);
7184
7185         if (service->query && service->response) {
7186                 type = "bonjour";
7187                 supplicant_dbus_dict_append_basic(&dict, "service_type",
7188                                                 DBUS_TYPE_STRING, &type);
7189                 supplicant_dbus_dict_append_fixed_array(&dict, "query",
7190                                         DBUS_TYPE_BYTE, &service->query,
7191                                         service->query_length);
7192                 supplicant_dbus_dict_append_fixed_array(&dict, "response",
7193                                         DBUS_TYPE_BYTE, &service->response,
7194                                         service->response_length);
7195         } else if (service->version && service->service) {
7196                 type = "upnp";
7197                 supplicant_dbus_dict_append_basic(&dict, "service_type",
7198                                                 DBUS_TYPE_STRING, &type);
7199                 supplicant_dbus_dict_append_basic(&dict, "version",
7200                                         DBUS_TYPE_INT32, &service->version);
7201                 supplicant_dbus_dict_append_basic(&dict, "service",
7202                                         DBUS_TYPE_STRING, &service->service);
7203         }
7204
7205         supplicant_dbus_dict_close(iter, &dict);
7206 }
7207
7208 int g_supplicant_interface_p2p_add_service(GSupplicantInterface *interface,
7209                                 GSupplicantInterfaceCallback callback,
7210                                 GSupplicantP2PServiceParams *p2p_service_params,
7211                                 void *user_data)
7212 {
7213         struct p2p_service_data *data;
7214         int ret;
7215
7216         SUPPLICANT_DBG("");
7217
7218         if (!interface->p2p_support)
7219                 return -ENOTSUP;
7220
7221         data = dbus_malloc0(sizeof(*data));
7222         if (!data)
7223                 return -ENOMEM;
7224
7225         data->registration = true;
7226         data->interface = interface;
7227         data->service = p2p_service_params;
7228         data->callback = callback;
7229         data->user_data = user_data;
7230
7231         ret = supplicant_dbus_method_call(interface->path,
7232                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "AddService",
7233                 interface_p2p_service_params, interface_p2p_service_result,
7234                 data, interface);
7235         if (ret < 0) {
7236                 dbus_free(data);
7237                 return ret;
7238         }
7239
7240         return -EINPROGRESS;
7241 }
7242
7243 int g_supplicant_interface_p2p_del_service(GSupplicantInterface *interface,
7244                                 GSupplicantP2PServiceParams *p2p_service_params)
7245 {
7246         struct p2p_service_data *data;
7247         int ret;
7248
7249         SUPPLICANT_DBG("");
7250
7251         if (!interface->p2p_support)
7252                 return -ENOTSUP;
7253
7254         data = dbus_malloc0(sizeof(*data));
7255         if (!data)
7256                 return -ENOMEM;
7257
7258         data->interface = interface;
7259         data->service = p2p_service_params;
7260
7261         ret = supplicant_dbus_method_call(interface->path,
7262                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeleteService",
7263                 interface_p2p_service_params, interface_p2p_service_result,
7264                 data, interface);
7265         if (ret < 0) {
7266                 dbus_free(data);
7267                 return ret;
7268         }
7269
7270         return -EINPROGRESS;
7271 }
7272
7273 struct p2p_listen_data {
7274         int period;
7275         int interval;
7276 };
7277
7278 static void interface_p2p_listen_params(DBusMessageIter *iter, void *user_data)
7279 {
7280         struct p2p_listen_data *params = user_data;
7281         DBusMessageIter dict;
7282
7283         supplicant_dbus_dict_open(iter, &dict);
7284
7285         supplicant_dbus_dict_append_basic(&dict, "period",
7286                                         DBUS_TYPE_INT32, &params->period);
7287         supplicant_dbus_dict_append_basic(&dict, "interval",
7288                                         DBUS_TYPE_INT32, &params->interval);
7289         supplicant_dbus_dict_close(iter, &dict);
7290 }
7291
7292 int g_supplicant_interface_p2p_listen(GSupplicantInterface *interface,
7293                                                 int period, int interval)
7294 {
7295         struct p2p_listen_data params;
7296
7297         SUPPLICANT_DBG("");
7298
7299         if (!interface->p2p_support)
7300                 return -ENOTSUP;
7301
7302         params.period = period;
7303         params.interval = interval;
7304
7305         return supplicant_dbus_method_call(interface->path,
7306                         SUPPLICANT_INTERFACE ".Interface.P2PDevice",
7307                         "ExtendedListen", interface_p2p_listen_params,
7308                         NULL, &params, NULL);
7309 }
7310
7311 static void widi_ies_params(DBusMessageIter *iter, void *user_data)
7312 {
7313         struct p2p_service_data *data = user_data;
7314         GSupplicantP2PServiceParams *service = data->service;
7315         DBusMessageIter array;
7316
7317         SUPPLICANT_DBG("%p - %d", service->wfd_ies, service->wfd_ies_length);
7318
7319         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
7320                                         DBUS_TYPE_BYTE_AS_STRING, &array);
7321
7322         if (service->wfd_ies && service->wfd_ies_length > 0) {
7323                 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
7324                                 &service->wfd_ies, service->wfd_ies_length);
7325         }
7326
7327         dbus_message_iter_close_container(iter, &array);
7328 }
7329
7330 int g_supplicant_set_widi_ies(GSupplicantP2PServiceParams *p2p_service_params,
7331                                         GSupplicantInterfaceCallback callback,
7332                                         void *user_data)
7333 {
7334         struct p2p_service_data *data;
7335         int ret;
7336
7337         SUPPLICANT_DBG("");
7338
7339         if (!system_available)
7340                 return -EFAULT;
7341
7342         data = dbus_malloc0(sizeof(*data));
7343         if (!data)
7344                 return -ENOMEM;
7345
7346         data->service = p2p_service_params;
7347         data->callback = callback;
7348         data->user_data = user_data;
7349
7350         if (p2p_service_params->wfd_ies)
7351                 data->registration = true;
7352
7353         ret = supplicant_dbus_property_set(SUPPLICANT_PATH,
7354                                         SUPPLICANT_INTERFACE, "WFDIEs",
7355                                         DBUS_TYPE_ARRAY_AS_STRING
7356                                         DBUS_TYPE_BYTE_AS_STRING,
7357                                         widi_ies_params,
7358                                         interface_p2p_service_result,
7359                                         data, NULL);
7360         if (ret < 0 && ret != -EINPROGRESS) {
7361                 dbus_free(data);
7362                 return ret;
7363         }
7364
7365         return -EINPROGRESS;
7366 }
7367
7368
7369 static const char *g_supplicant_rule0 = "type=signal,"
7370                                         "path=" DBUS_PATH_DBUS ","
7371                                         "sender=" DBUS_SERVICE_DBUS ","
7372                                         "interface=" DBUS_INTERFACE_DBUS ","
7373                                         "member=NameOwnerChanged,"
7374                                         "arg0=" SUPPLICANT_SERVICE;
7375 static const char *g_supplicant_rule1 = "type=signal,"
7376                         "interface=" SUPPLICANT_INTERFACE;
7377 static const char *g_supplicant_rule2 = "type=signal,"
7378                         "interface=" SUPPLICANT_INTERFACE ".Interface";
7379 static const char *g_supplicant_rule3 = "type=signal,"
7380                         "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
7381 static const char *g_supplicant_rule4 = "type=signal,"
7382                         "interface=" SUPPLICANT_INTERFACE ".BSS";
7383 static const char *g_supplicant_rule5 = "type=signal,"
7384                         "interface=" SUPPLICANT_INTERFACE ".Network";
7385 #if !defined TIZEN_EXT
7386 static const char *g_supplicant_rule6 = "type=signal,"
7387                 "interface=" SUPPLICANT_INTERFACE ".Interface.P2PDevice";
7388 static const char *g_supplicant_rule7 = "type=signal,"
7389                 "interface=" SUPPLICANT_INTERFACE ".Peer";
7390 static const char *g_supplicant_rule8 = "type=signal,"
7391                 "interface=" SUPPLICANT_INTERFACE ".Group";
7392 #endif
7393 #if defined TIZEN_EXT_WIFI_MESH
7394 static const char *g_supplicant_rule9 = "type=signal,"
7395                 "interface=" SUPPLICANT_INTERFACE ".Interface.Mesh";
7396 #endif
7397
7398 static void invoke_introspect_method(void)
7399 {
7400         DBusMessage *message;
7401
7402         message = dbus_message_new_method_call(SUPPLICANT_SERVICE,
7403                                         SUPPLICANT_PATH,
7404                                         DBUS_INTERFACE_INTROSPECTABLE,
7405                                         "Introspect");
7406
7407         if (!message)
7408                 return;
7409
7410         dbus_message_set_no_reply(message, TRUE);
7411         dbus_connection_send(connection, message, NULL);
7412         dbus_message_unref(message);
7413 }
7414
7415 int g_supplicant_register(const GSupplicantCallbacks *callbacks)
7416 {
7417         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
7418         if (!connection)
7419                 return -EIO;
7420
7421         if (!dbus_connection_add_filter(connection, g_supplicant_filter,
7422                                                 NULL, NULL)) {
7423                 dbus_connection_unref(connection);
7424                 connection = NULL;
7425                 return -EIO;
7426         }
7427
7428         callbacks_pointer = callbacks;
7429         eap_methods = 0;
7430
7431         interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
7432                                                 NULL, remove_interface);
7433
7434         bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
7435                                                                 NULL, NULL);
7436         peer_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
7437                                                                 NULL, NULL);
7438         group_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
7439                                                                 NULL, NULL);
7440         pending_peer_connection = g_hash_table_new_full(g_str_hash, g_str_equal,
7441                                                                 NULL, NULL);
7442         config_file_table = g_hash_table_new_full(g_str_hash, g_str_equal,
7443                                                                 g_free, g_free);
7444
7445         supplicant_dbus_setup(connection);
7446
7447         dbus_bus_add_match(connection, g_supplicant_rule0, NULL);
7448         dbus_bus_add_match(connection, g_supplicant_rule1, NULL);
7449         dbus_bus_add_match(connection, g_supplicant_rule2, NULL);
7450         dbus_bus_add_match(connection, g_supplicant_rule3, NULL);
7451         dbus_bus_add_match(connection, g_supplicant_rule4, NULL);
7452         dbus_bus_add_match(connection, g_supplicant_rule5, NULL);
7453 #if defined TIZEN_EXT
7454         dbus_bus_add_match(connection,
7455                         "type=signal,interface=org.tizen.system.deviced.PowerOff,"
7456                         "member=ChangeState", NULL);
7457 #endif
7458 #if !defined TIZEN_EXT
7459         dbus_bus_add_match(connection, g_supplicant_rule6, NULL);
7460         dbus_bus_add_match(connection, g_supplicant_rule7, NULL);
7461         dbus_bus_add_match(connection, g_supplicant_rule8, NULL);
7462 #endif
7463 #if defined TIZEN_EXT_WIFI_MESH
7464         dbus_bus_add_match(connection, g_supplicant_rule9, NULL);
7465 #endif
7466         dbus_connection_flush(connection);
7467
7468         if (dbus_bus_name_has_owner(connection,
7469                                         SUPPLICANT_SERVICE, NULL)) {
7470                 system_available = TRUE;
7471                 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
7472                                                 SUPPLICANT_INTERFACE,
7473                                                 service_property, NULL, NULL);
7474         } else
7475                 invoke_introspect_method();
7476
7477         return 0;
7478 }
7479
7480 static void unregister_interface_remove_params(DBusMessageIter *iter,
7481                                                 void *user_data)
7482 {
7483         const char *path = user_data;
7484
7485         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
7486                                                         &path);
7487 }
7488
7489
7490 static void unregister_remove_interface(gpointer key, gpointer value,
7491                                                 gpointer user_data)
7492 {
7493         GSupplicantInterface *interface = value;
7494
7495         supplicant_dbus_method_call(SUPPLICANT_PATH,
7496                                         SUPPLICANT_INTERFACE,
7497                                         "RemoveInterface",
7498                                         unregister_interface_remove_params,
7499                                         NULL, interface->path, NULL);
7500 }
7501
7502 void g_supplicant_unregister(const GSupplicantCallbacks *callbacks)
7503 {
7504         SUPPLICANT_DBG("");
7505
7506         if (connection) {
7507 #if defined TIZEN_EXT_WIFI_MESH
7508                 dbus_bus_remove_match(connection, g_supplicant_rule9, NULL);
7509 #endif
7510 #if !defined TIZEN_EXT
7511                 dbus_bus_remove_match(connection, g_supplicant_rule8, NULL);
7512                 dbus_bus_remove_match(connection, g_supplicant_rule7, NULL);
7513                 dbus_bus_remove_match(connection, g_supplicant_rule6, NULL);
7514 #endif
7515                 dbus_bus_remove_match(connection, g_supplicant_rule5, NULL);
7516                 dbus_bus_remove_match(connection, g_supplicant_rule4, NULL);
7517                 dbus_bus_remove_match(connection, g_supplicant_rule3, NULL);
7518                 dbus_bus_remove_match(connection, g_supplicant_rule2, NULL);
7519                 dbus_bus_remove_match(connection, g_supplicant_rule1, NULL);
7520                 dbus_bus_remove_match(connection, g_supplicant_rule0, NULL);
7521                 dbus_connection_flush(connection);
7522
7523                 dbus_connection_remove_filter(connection,
7524                                                 g_supplicant_filter, NULL);
7525         }
7526
7527         if (config_file_table) {
7528                 g_hash_table_destroy(config_file_table);
7529                 config_file_table = NULL;
7530         }
7531
7532         if (bss_mapping) {
7533                 g_hash_table_destroy(bss_mapping);
7534                 bss_mapping = NULL;
7535         }
7536
7537         if (peer_mapping) {
7538                 g_hash_table_destroy(peer_mapping);
7539                 peer_mapping = NULL;
7540         }
7541
7542         if (group_mapping) {
7543                 g_hash_table_destroy(group_mapping);
7544                 group_mapping = NULL;
7545         }
7546
7547         if (interface_table) {
7548                 g_hash_table_foreach(interface_table,
7549                                         unregister_remove_interface, NULL);
7550                 g_hash_table_destroy(interface_table);
7551                 interface_table = NULL;
7552         }
7553
7554         if (system_available)
7555                 callback_system_killed();
7556
7557         if (connection) {
7558                 dbus_connection_unref(connection);
7559                 connection = NULL;
7560         }
7561
7562         callbacks_pointer = NULL;
7563         eap_methods = 0;
7564 }