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