Start scan for wifi roaming when SNR and signal are weakened
[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         dbus_int32_t snr = 0;
6429         DBusMessageIter sub_iter, dict;
6430
6431         if (error) {
6432                 err = -EIO;
6433                 SUPPLICANT_DBG("error: %s", error);
6434                 goto out;
6435         }
6436
6437         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_VARIANT) {
6438                 err = -EINVAL;
6439                 SUPPLICANT_DBG("invalid reply");
6440                 goto out;
6441         }
6442
6443         dbus_message_iter_recurse(iter, &sub_iter);
6444         dbus_message_iter_recurse(&sub_iter, &dict);
6445
6446         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
6447                 DBusMessageIter entry, value;
6448                 const char *key;
6449
6450                 dbus_message_iter_recurse(&dict, &entry);
6451                 dbus_message_iter_get_basic(&entry, &key);
6452                 dbus_message_iter_next(&entry);
6453                 dbus_message_iter_recurse(&entry, &value);
6454
6455                 switch (dbus_message_iter_get_arg_type(&value)) {
6456                 case DBUS_TYPE_INT32:
6457                         if (g_strcmp0(key, "linkspeed") == 0) {
6458                                 dbus_message_iter_get_basic(&value, &maxspeed);
6459                                 SUPPLICANT_DBG("linkspeed = %d", maxspeed);
6460                                 break;
6461                         } else if (g_strcmp0(key, "rssi") == 0) {
6462                                 dbus_message_iter_get_basic(&value, &strength);
6463                                 SUPPLICANT_DBG("Strength = %d", strength);
6464                                 break;
6465                         } else if (g_strcmp0(key, "SNR") == 0) {
6466                                 dbus_message_iter_get_basic(&value, &snr);
6467                                 SUPPLICANT_DBG("SNR = %d", snr);
6468                                 break;
6469                         }
6470                 }
6471                 dbus_message_iter_next(&dict);
6472         }
6473
6474 out:
6475         if(data->callback)
6476                 data->callback(err, maxspeed, strength, snr, data->user_data);
6477
6478         g_free(data->path);
6479         dbus_free(data);
6480 }
6481
6482 int g_supplicant_interface_signalpoll(GSupplicantInterface *interface,
6483                                 GSupplicantMaxSpeedCallback callback,
6484                                 void *user_data)
6485 {
6486         struct interface_signalpoll_data *data;
6487         int ret;
6488
6489         if (!interface)
6490                 return -EINVAL;
6491
6492         if (!system_available)
6493                 return -EFAULT;
6494
6495         data = dbus_malloc0(sizeof(*data));
6496         if (!data)
6497                 return -ENOMEM;
6498
6499         data->interface = interface;
6500         data->path = g_strdup(interface->path);
6501         data->callback = callback;
6502         data->user_data = user_data;
6503
6504         ret = supplicant_dbus_method_call(interface->path,
6505                         SUPPLICANT_INTERFACE ".Interface", "SignalPoll",
6506                         NULL, interface_signalpoll_result, data,
6507                         interface);
6508
6509         if (ret < 0) {
6510                 g_free(data->path);
6511                 dbus_free(data);
6512         }
6513
6514         return ret;
6515 }
6516 #endif
6517
6518 static int parse_supplicant_error(DBusMessageIter *iter)
6519 {
6520         int err = -ECONNABORTED;
6521         char *key;
6522
6523         if (!iter)
6524                 return err;
6525
6526         /* If the given passphrase is malformed wpa_s returns
6527          * "invalid message format" but this error should be interpreted as
6528          * invalid-key.
6529          */
6530         while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
6531                 dbus_message_iter_get_basic(iter, &key);
6532                 if (strncmp(key, "psk", 3) == 0 ||
6533                                 strncmp(key, "wep_key", 7) == 0 ||
6534                                 strcmp(key, "invalid message format") == 0) {
6535                         err = -ENOKEY;
6536                         break;
6537                 }
6538                 dbus_message_iter_next(iter);
6539         }
6540
6541         return err;
6542 }
6543
6544 static void interface_select_network_result(const char *error,
6545                                 DBusMessageIter *iter, void *user_data)
6546 {
6547         struct interface_connect_data *data = user_data;
6548         int err;
6549
6550         SUPPLICANT_DBG("");
6551
6552         err = 0;
6553         if (error) {
6554 #if defined TIZEN_EXT
6555                 SUPPLICANT_DBG("SelectNetwork errorFreq %s", error);
6556 #else
6557                 SUPPLICANT_DBG("SelectNetwork error %s", error);
6558 #endif
6559                 err = parse_supplicant_error(iter);
6560         }
6561
6562         g_free(data->path);
6563
6564         if (data->callback)
6565                 data->callback(err, data->interface, data->user_data);
6566
6567 #if defined TIZEN_EXT
6568         g_free(data->ssid->ssid);
6569         g_free((char *)data->ssid->passphrase);
6570         g_free((char *)data->ssid->connector);
6571         g_free((char *)data->ssid->c_sign_key);
6572         g_free((char *)data->ssid->net_access_key);
6573 #endif
6574         g_free(data->ssid);
6575         dbus_free(data);
6576 }
6577
6578 static void interface_select_network_params(DBusMessageIter *iter,
6579                                                         void *user_data)
6580 {
6581         struct interface_connect_data *data = user_data;
6582         GSupplicantInterface *interface = data->interface;
6583 #if defined TIZEN_EXT
6584         GSupplicantSSID *ssid = data->ssid;
6585 #endif
6586
6587         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
6588                                         &interface->network_path);
6589 #if defined TIZEN_EXT
6590         if (!ssid->bssid_for_connect_len)
6591                 dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &ssid->freq);
6592 #endif
6593 }
6594
6595 static void interface_add_network_result(const char *error,
6596                                 DBusMessageIter *iter, void *user_data)
6597 {
6598         struct interface_connect_data *data = user_data;
6599         GSupplicantInterface *interface = data->interface;
6600         const char *path;
6601         int err;
6602
6603         if (error)
6604                 goto error;
6605
6606         dbus_message_iter_get_basic(iter, &path);
6607         if (!path)
6608                 goto error;
6609
6610         SUPPLICANT_DBG("PATH: %s", path);
6611
6612 #if defined TIZEN_EXT
6613         if (interface->network_path)
6614                 g_free(interface->network_path);
6615 #endif
6616         interface->network_path = g_strdup(path);
6617
6618         store_network_information(interface, data->ssid);
6619
6620 #if defined TIZEN_EXT
6621         SUPPLICANT_DBG(".Interface.SelectNetworkFreq");
6622         GSupplicantSSID *ssid = data->ssid;
6623
6624         if (!ssid->bssid_for_connect_len)
6625                 supplicant_dbus_method_call(data->interface->path,
6626                                 SUPPLICANT_INTERFACE ".Interface", "SelectNetworkFreq",
6627                                 interface_select_network_params,
6628                                 interface_select_network_result, data,
6629                                 interface);
6630         else
6631                 supplicant_dbus_method_call(data->interface->path,
6632                                 SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
6633                                 interface_select_network_params,
6634                                 interface_select_network_result, data,
6635                                 interface);
6636 #else
6637         supplicant_dbus_method_call(data->interface->path,
6638                         SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
6639                         interface_select_network_params,
6640                         interface_select_network_result, data,
6641                         interface);
6642 #endif
6643
6644         return;
6645
6646 error:
6647         SUPPLICANT_DBG("AddNetwork error %s", error);
6648
6649         if (interface_exists(data->interface, data->interface->path)) {
6650                 err = parse_supplicant_error(iter);
6651                 if (data->callback)
6652                         data->callback(err, data->interface, data->user_data);
6653
6654                 g_free(interface->network_path);
6655                 interface->network_path = NULL;
6656         }
6657
6658         g_free(data->path);
6659 #if defined TIZEN_EXT
6660         g_free(data->ssid->ssid);
6661         g_free((char *)data->ssid->passphrase);
6662         g_free((char *)data->ssid->connector);
6663         g_free((char *)data->ssid->c_sign_key);
6664         g_free((char *)data->ssid->net_access_key);
6665 #endif
6666         g_free(data->ssid);
6667         g_free(data);
6668 }
6669
6670 static void add_network_security_none(DBusMessageIter *dict)
6671 {
6672         const char *auth_alg = "OPEN";
6673
6674         supplicant_dbus_dict_append_basic(dict, "auth_alg",
6675                                         DBUS_TYPE_STRING, &auth_alg);
6676 }
6677
6678 static void add_network_security_wep(DBusMessageIter *dict,
6679                                         GSupplicantSSID *ssid)
6680 {
6681         const char *auth_alg = "OPEN SHARED";
6682         dbus_uint32_t key_index = 0;
6683
6684         supplicant_dbus_dict_append_basic(dict, "auth_alg",
6685                                         DBUS_TYPE_STRING, &auth_alg);
6686
6687         if (ssid->passphrase) {
6688                 int size = strlen(ssid->passphrase);
6689                 if (size == 10 || size == 26) {
6690                         unsigned char *key = g_try_malloc(13);
6691                         char tmp[3];
6692                         int i;
6693
6694                         memset(tmp, 0, sizeof(tmp));
6695                         if (!key)
6696                                 size = 0;
6697
6698                         for (i = 0; i < size / 2; i++) {
6699                                 memcpy(tmp, ssid->passphrase + (i * 2), 2);
6700                                 key[i] = (unsigned char) strtol(tmp, NULL, 16);
6701                         }
6702
6703                         supplicant_dbus_dict_append_fixed_array(dict,
6704                                                         "wep_key0",
6705                                                         DBUS_TYPE_BYTE,
6706                                                         &key, size / 2);
6707                         g_free(key);
6708                 } else if (size == 5 || size == 13) {
6709                         unsigned char *key = g_try_malloc(13);
6710                         int i;
6711
6712                         if (!key)
6713                                 size = 0;
6714
6715                         for (i = 0; i < size; i++)
6716                                 key[i] = (unsigned char) ssid->passphrase[i];
6717
6718                         supplicant_dbus_dict_append_fixed_array(dict,
6719                                                                 "wep_key0",
6720                                                                 DBUS_TYPE_BYTE,
6721                                                                 &key, size);
6722                         g_free(key);
6723                 } else
6724                         supplicant_dbus_dict_append_basic(dict,
6725                                                         "wep_key0",
6726                                                         DBUS_TYPE_STRING,
6727                                                         &ssid->passphrase);
6728
6729                 supplicant_dbus_dict_append_basic(dict, "wep_tx_keyidx",
6730                                         DBUS_TYPE_UINT32, &key_index);
6731         }
6732 }
6733
6734 static dbus_bool_t is_psk_raw_key(const char *psk)
6735 {
6736         int i;
6737
6738         /* A raw key is always 64 bytes length... */
6739         if (strlen(psk) != 64)
6740                 return FALSE;
6741
6742         /* ... and its content is in hex representation */
6743         for (i = 0; i < 64; i++)
6744                 if (!isxdigit((unsigned char) psk[i]))
6745                         return FALSE;
6746
6747         return TRUE;
6748 }
6749
6750 static unsigned char hexchar2bin(char c)
6751 {
6752         if ((c >= '0') && (c <= '9'))
6753                 return c - '0';
6754         else if ((c >= 'A') && (c <= 'F'))
6755                 return c - 'A' + 10;
6756         else if ((c >= 'a') && (c <= 'f'))
6757                 return c - 'a' + 10;
6758         else
6759                 return c;
6760 }
6761
6762 static void hexstring2bin(const char *string, unsigned char *data,
6763                                 size_t data_len)
6764 {
6765         size_t i;
6766
6767         for (i = 0; i < data_len; i++)
6768                 data[i] = (hexchar2bin(string[i * 2 + 0]) << 4 |
6769                            hexchar2bin(string[i * 2 + 1]) << 0);
6770 }
6771
6772 static void add_network_security_psk(DBusMessageIter *dict,
6773                                         GSupplicantSSID *ssid)
6774 {
6775         if (ssid->passphrase && strlen(ssid->passphrase) > 0) {
6776                 const char *key = "psk";
6777
6778                 if (is_psk_raw_key(ssid->passphrase)) {
6779                         unsigned char data[32];
6780                         unsigned char *datap = data;
6781
6782                         /* The above pointer alias is required by D-Bus because
6783                          * with D-Bus and GCC, non-heap-allocated arrays cannot
6784                          * be passed directly by their base pointer. */
6785
6786                         hexstring2bin(ssid->passphrase, datap, sizeof(data));
6787
6788                         supplicant_dbus_dict_append_fixed_array(dict,
6789                                                         key, DBUS_TYPE_BYTE,
6790                                                         &datap, sizeof(data));
6791                 } else
6792                         supplicant_dbus_dict_append_basic(dict,
6793                                                         key, DBUS_TYPE_STRING,
6794                                                         &ssid->passphrase);
6795         }
6796 }
6797
6798 static void add_network_security_tls(DBusMessageIter *dict,
6799                                         GSupplicantSSID *ssid)
6800 {
6801         /*
6802          * For TLS, we at least need:
6803          *              The client certificate
6804          *              The client private key file
6805          *              The client private key file password
6806          *
6807          * The Authority certificate is optional.
6808          */
6809         if (!ssid->client_cert_path)
6810                 return;
6811
6812         if (!ssid->private_key_path)
6813                 return;
6814
6815 #if !defined TIZEN_EXT
6816         if (!ssid->private_key_passphrase)
6817                 return;
6818 #endif
6819
6820         if (ssid->ca_cert_path)
6821                 supplicant_dbus_dict_append_basic(dict, "ca_cert",
6822                                         DBUS_TYPE_STRING, &ssid->ca_cert_path);
6823
6824         supplicant_dbus_dict_append_basic(dict, "private_key",
6825                                                 DBUS_TYPE_STRING,
6826                                                 &ssid->private_key_path);
6827 #if !defined TIZEN_EXT
6828         supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
6829                                                 DBUS_TYPE_STRING,
6830                                                 &ssid->private_key_passphrase);
6831 #endif
6832         supplicant_dbus_dict_append_basic(dict, "client_cert",
6833                                                 DBUS_TYPE_STRING,
6834                                                 &ssid->client_cert_path);
6835 }
6836
6837 static void add_network_security_peap(DBusMessageIter *dict,
6838                                         GSupplicantSSID *ssid)
6839 {
6840         char *phase2_auth;
6841
6842         /*
6843          * For PEAP/TTLS, we at least need
6844          *              The authority certificate
6845          *              The 2nd phase authentication method
6846          *              The 2nd phase passphrase
6847          *
6848          * The Client certificate is optional although strongly recommended
6849          * When setting it, we need in addition
6850          *              The Client private key file
6851          *              The Client private key file password
6852          */
6853         if (!ssid->passphrase)
6854                 return;
6855
6856         if (!ssid->phase2_auth)
6857                 return;
6858
6859         if (ssid->client_cert_path) {
6860                 if (!ssid->private_key_path)
6861                         return;
6862
6863 #if !defined TIZEN_EXT
6864                 if (!ssid->private_key_passphrase)
6865                         return;
6866 #endif
6867
6868                 supplicant_dbus_dict_append_basic(dict, "client_cert",
6869                                                 DBUS_TYPE_STRING,
6870                                                 &ssid->client_cert_path);
6871
6872                 supplicant_dbus_dict_append_basic(dict, "private_key",
6873                                                 DBUS_TYPE_STRING,
6874                                                 &ssid->private_key_path);
6875
6876 #if !defined TIZEN_EXT
6877                 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
6878                                                 DBUS_TYPE_STRING,
6879                                                 &ssid->private_key_passphrase);
6880 #endif
6881
6882         }
6883
6884         if(g_strcmp0(ssid->phase2_auth, "GTC") == 0 && g_strcmp0(ssid->eap, "ttls") == 0)
6885                 phase2_auth = g_strdup_printf("autheap=%s", ssid->phase2_auth);
6886         else if (g_str_has_prefix(ssid->phase2_auth, "EAP-")) {
6887                 phase2_auth = g_strdup_printf("autheap=%s",
6888                                         ssid->phase2_auth + strlen("EAP-"));
6889         } else
6890                 phase2_auth = g_strdup_printf("auth=%s", ssid->phase2_auth);
6891
6892         supplicant_dbus_dict_append_basic(dict, "password",
6893                                                 DBUS_TYPE_STRING,
6894                                                 &ssid->passphrase);
6895
6896         if (ssid->ca_cert_path)
6897                 supplicant_dbus_dict_append_basic(dict, "ca_cert",
6898                                                 DBUS_TYPE_STRING,
6899                                                 &ssid->ca_cert_path);
6900
6901         supplicant_dbus_dict_append_basic(dict, "phase2",
6902                                                 DBUS_TYPE_STRING,
6903                                                 &phase2_auth);
6904
6905         g_free(phase2_auth);
6906 }
6907
6908 #if defined TIZEN_EXT
6909 static void add_network_security_aka_sim(DBusMessageIter *dict,
6910                                         GSupplicantSSID *ssid)
6911 {
6912         if (!ssid->passphrase)
6913                 return;
6914
6915         supplicant_dbus_dict_append_basic(dict, "password",
6916                         DBUS_TYPE_STRING,
6917                         &ssid->passphrase);
6918 }
6919
6920 static void add_network_security_fast(DBusMessageIter *dict,
6921                 GSupplicantSSID *ssid)
6922 {
6923         /*
6924          * For FAST, we at least need:
6925          *              id / password
6926          *              phase1 (provisiong information)
6927          *              pac_file
6928          */
6929
6930         /* Allow provisioing both authenticated and unauthenticated */
6931         const char *phase1 = "fast_provisioning=2";
6932         supplicant_dbus_dict_append_basic(dict, "phase1",
6933                         DBUS_TYPE_STRING,
6934                         &phase1);
6935
6936         SUPPLICANT_DBG("pac_file [%s]", ssid->pac_file);
6937         if(ssid->pac_file)
6938                 supplicant_dbus_dict_append_basic(dict, "pac_file",
6939                                 DBUS_TYPE_STRING,
6940                                 &ssid->pac_file);
6941
6942         supplicant_dbus_dict_append_basic(dict, "password",
6943                         DBUS_TYPE_STRING,
6944                         &ssid->passphrase);
6945 }
6946 #endif
6947
6948 static void add_network_security_eap(DBusMessageIter *dict,
6949                                         GSupplicantSSID *ssid)
6950 {
6951         char *eap_value;
6952
6953 #if defined TIZEN_EXT
6954         if (!ssid->eap)
6955 #else
6956         if (!ssid->eap || !ssid->identity)
6957 #endif
6958                 return;
6959
6960         if (g_strcmp0(ssid->eap, "tls") == 0) {
6961                 add_network_security_tls(dict, ssid);
6962         } else if (g_strcmp0(ssid->eap, "peap") == 0 ||
6963                                 g_strcmp0(ssid->eap, "ttls") == 0) {
6964 #if defined TIZEN_EXT
6965                 if (!ssid->identity)
6966                         return;
6967 #endif
6968                 add_network_security_peap(dict, ssid);
6969
6970 #if defined TIZEN_EXT
6971         } else if (g_strcmp0(ssid->eap, "sim") == 0 ||
6972                         g_strcmp0(ssid->eap, "aka") == 0 ||
6973                         g_strcmp0(ssid->eap, "aka'") == 0) {
6974                 add_network_security_aka_sim(dict, ssid);
6975         } else if (g_strcmp0(ssid->eap, "pwd") == 0) {
6976                 if(!ssid->passphrase)
6977                         return;
6978                 supplicant_dbus_dict_append_basic(dict, "password",
6979                                 DBUS_TYPE_STRING,
6980                                 &ssid->passphrase);
6981         } else if (g_strcmp0(ssid->eap, "fast") == 0){
6982                 if (!ssid->identity || !ssid->passphrase)
6983                         return;
6984
6985                 add_network_security_fast(dict, ssid);
6986 #endif
6987         } else
6988                 return;
6989
6990         eap_value = g_ascii_strup(ssid->eap, -1);
6991
6992         supplicant_dbus_dict_append_basic(dict, "eap",
6993                                                 DBUS_TYPE_STRING,
6994                                                 &eap_value);
6995 #if defined TIZEN_EXT
6996         if (ssid->identity != NULL)
6997                 supplicant_dbus_dict_append_basic(dict, "identity",
6998                                                         DBUS_TYPE_STRING,
6999                                                         &ssid->identity);
7000 #else
7001         supplicant_dbus_dict_append_basic(dict, "identity",
7002                                                 DBUS_TYPE_STRING,
7003                                                 &ssid->identity);
7004 #endif
7005         if(ssid->anonymous_identity)
7006                 supplicant_dbus_dict_append_basic(dict, "anonymous_identity",
7007                                                      DBUS_TYPE_STRING,
7008                                                      &ssid->anonymous_identity);
7009
7010         if(ssid->subject_match)
7011                 supplicant_dbus_dict_append_basic(dict, "subject_match",
7012                                                      DBUS_TYPE_STRING,
7013                                                      &ssid->subject_match);
7014
7015         if(ssid->altsubject_match)
7016                 supplicant_dbus_dict_append_basic(dict, "altsubject_match",
7017                                                      DBUS_TYPE_STRING,
7018                                                      &ssid->altsubject_match);
7019
7020         if(ssid->domain_suffix_match)
7021                 supplicant_dbus_dict_append_basic(dict, "domain_suffix_match",
7022                                                      DBUS_TYPE_STRING,
7023                                                      &ssid->domain_suffix_match);
7024
7025         if(ssid->domain_match)
7026                 supplicant_dbus_dict_append_basic(dict, "domain_match",
7027                                                      DBUS_TYPE_STRING,
7028                                                      &ssid->domain_match);
7029
7030         g_free(eap_value);
7031 }
7032
7033 static void add_network_security_ciphers(DBusMessageIter *dict,
7034                                                 GSupplicantSSID *ssid)
7035 {
7036         unsigned int p_cipher, g_cipher, i;
7037         char *pairwise, *group;
7038         char *pair_ciphers[4];
7039         char *group_ciphers[5];
7040
7041         p_cipher = ssid->pairwise_cipher;
7042         g_cipher = ssid->group_cipher;
7043
7044         if (p_cipher == 0 && g_cipher == 0)
7045                 return;
7046
7047         i = 0;
7048
7049         if (p_cipher & G_SUPPLICANT_PAIRWISE_CCMP)
7050                 pair_ciphers[i++] = "CCMP";
7051
7052         if (p_cipher & G_SUPPLICANT_PAIRWISE_TKIP)
7053                 pair_ciphers[i++] = "TKIP";
7054
7055         if (p_cipher & G_SUPPLICANT_PAIRWISE_NONE)
7056                 pair_ciphers[i++] = "NONE";
7057
7058         pair_ciphers[i] = NULL;
7059
7060         i = 0;
7061
7062         if (g_cipher & G_SUPPLICANT_GROUP_CCMP)
7063                 group_ciphers[i++] = "CCMP";
7064
7065         if (g_cipher & G_SUPPLICANT_GROUP_TKIP)
7066                 group_ciphers[i++] = "TKIP";
7067
7068         if (g_cipher & G_SUPPLICANT_GROUP_WEP104)
7069                 group_ciphers[i++] = "WEP104";
7070
7071         if (g_cipher & G_SUPPLICANT_GROUP_WEP40)
7072                 group_ciphers[i++] = "WEP40";
7073
7074         group_ciphers[i] = NULL;
7075
7076         pairwise = g_strjoinv(" ", pair_ciphers);
7077         group = g_strjoinv(" ", group_ciphers);
7078
7079         SUPPLICANT_DBG("cipher %s %s", pairwise, group);
7080
7081         supplicant_dbus_dict_append_basic(dict, "pairwise",
7082                                                 DBUS_TYPE_STRING,
7083                                                 &pairwise);
7084         supplicant_dbus_dict_append_basic(dict, "group",
7085                                                 DBUS_TYPE_STRING,
7086                                                 &group);
7087
7088         g_free(pairwise);
7089         g_free(group);
7090 }
7091
7092 static void add_network_security_proto(DBusMessageIter *dict,
7093                                                 GSupplicantSSID *ssid)
7094 {
7095         unsigned int protocol, i;
7096         char *proto;
7097         char *protos[3];
7098
7099         protocol = ssid->protocol;
7100
7101         if (protocol == 0)
7102                 return;
7103
7104         i = 0;
7105
7106         if (protocol & G_SUPPLICANT_PROTO_RSN)
7107                 protos[i++] = "RSN";
7108
7109         if (protocol & G_SUPPLICANT_PROTO_WPA)
7110                 protos[i++] = "WPA";
7111
7112         protos[i] = NULL;
7113
7114         proto = g_strjoinv(" ", protos);
7115
7116         SUPPLICANT_DBG("proto %s", proto);
7117
7118         supplicant_dbus_dict_append_basic(dict, "proto",
7119                                                 DBUS_TYPE_STRING,
7120                                                 &proto);
7121
7122         g_free(proto);
7123 }
7124
7125 #if defined TIZEN_EXT
7126 static void add_network_ieee80211w(DBusMessageIter *dict, GSupplicantSSID *ssid)
7127 {
7128         if (ssid->security != G_SUPPLICANT_SECURITY_SAE
7129                         && ssid->security != G_SUPPLICANT_SECURITY_OWE
7130                         && ssid->security != G_SUPPLICANT_SECURITY_DPP)
7131                 return;
7132
7133         supplicant_dbus_dict_append_basic(dict, "ieee80211w", DBUS_TYPE_UINT32,
7134                                           &ssid->ieee80211w);
7135 }
7136
7137 static void add_network_security_connector(DBusMessageIter *dict, GSupplicantSSID *ssid)
7138 {
7139         if (ssid->connector && strlen(ssid->connector) > 0) {
7140                 const char *key = "dpp_connector";
7141
7142                 supplicant_dbus_dict_append_basic(dict,
7143                                 key, DBUS_TYPE_STRING,
7144                                 &ssid->connector);
7145         }
7146 }
7147
7148 static size_t convert_hexstr_to_bin(const char *hex_str, unsigned char **bin)
7149 {
7150         unsigned char *bin_res = NULL;
7151         unsigned int i, j, hex;
7152         size_t hex_str_len;
7153
7154         if (!hex_str || strlen(hex_str) == 0)
7155                 return 0;
7156
7157         hex_str_len = strlen(hex_str);
7158         bin_res = g_try_malloc0(hex_str_len / 2);
7159         if (!bin_res)
7160                 return 0;
7161
7162         j = 0;
7163         for (i = 0; i < hex_str_len; i+=2) {
7164                 sscanf(hex_str + i, "%02x", &hex);
7165                 bin_res[j++] = hex;
7166         }
7167
7168         *bin = bin_res;
7169         return hex_str_len / 2;
7170 }
7171
7172 static void add_network_security_c_sign_key(DBusMessageIter *dict, GSupplicantSSID *ssid)
7173 {
7174         if (ssid->c_sign_key && strlen(ssid->c_sign_key) > 0) {
7175                 const char *key = "dpp_csign";
7176                 unsigned char *bin_csign = NULL;
7177                 size_t bin_csign_len = convert_hexstr_to_bin(ssid->c_sign_key, &bin_csign);
7178                 if  (bin_csign_len != 0)
7179                         supplicant_dbus_dict_append_fixed_array(dict,
7180                                         key, DBUS_TYPE_BYTE,
7181                                         &bin_csign, bin_csign_len);
7182                 g_free(bin_csign);
7183         }
7184 }
7185
7186 static void add_network_security_net_access_key(DBusMessageIter *dict, GSupplicantSSID *ssid)
7187 {
7188         if (ssid->net_access_key && strlen(ssid->net_access_key) > 0) {
7189                 const char *key = "dpp_netaccesskey";
7190                 unsigned char *bin_netaccesskey = NULL;
7191                 size_t bin_netaccesskey_len = convert_hexstr_to_bin(ssid->net_access_key, &bin_netaccesskey);
7192                 if  (bin_netaccesskey_len != 0)
7193                         supplicant_dbus_dict_append_fixed_array(dict,
7194                                         key, DBUS_TYPE_BYTE,
7195                                         &bin_netaccesskey, bin_netaccesskey_len);
7196                 g_free(bin_netaccesskey);
7197         }
7198 }
7199
7200 #endif
7201
7202 static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid)
7203 {
7204         char *key_mgmt;
7205
7206         switch (ssid->security) {
7207         case G_SUPPLICANT_SECURITY_NONE:
7208                 key_mgmt = "NONE";
7209                 add_network_security_none(dict);
7210                 add_network_security_ciphers(dict, ssid);
7211                 break;
7212         case G_SUPPLICANT_SECURITY_UNKNOWN:
7213         case G_SUPPLICANT_SECURITY_WEP:
7214                 key_mgmt = "NONE";
7215                 add_network_security_wep(dict, ssid);
7216                 add_network_security_ciphers(dict, ssid);
7217                 break;
7218         case G_SUPPLICANT_SECURITY_PSK:
7219                 key_mgmt = "WPA-PSK";
7220                 add_network_security_psk(dict, ssid);
7221                 add_network_security_ciphers(dict, ssid);
7222                 add_network_security_proto(dict, ssid);
7223                 break;
7224         case G_SUPPLICANT_SECURITY_IEEE8021X:
7225                 key_mgmt = "WPA-EAP";
7226                 add_network_security_eap(dict, ssid);
7227                 add_network_security_ciphers(dict, ssid);
7228                 add_network_security_proto(dict, ssid);
7229                 break;
7230 #if defined TIZEN_EXT
7231         case G_SUPPLICANT_SECURITY_FT_PSK:
7232                 key_mgmt = "FT-PSK";
7233                 add_network_security_psk(dict, ssid);
7234                 add_network_security_ciphers(dict, ssid);
7235                 add_network_security_proto(dict, ssid);
7236                 break;
7237         case G_SUPPLICANT_SECURITY_FT_IEEE8021X:
7238                 key_mgmt = "FT-EAP";
7239                 add_network_security_eap(dict, ssid);
7240                 add_network_security_ciphers(dict, ssid);
7241                 add_network_security_proto(dict, ssid);
7242                 break;
7243         case G_SUPPLICANT_SECURITY_SAE:
7244                 if (ssid->keymgmt & G_SUPPLICANT_KEYMGMT_WPA_PSK)
7245                         key_mgmt = "SAE WPA-PSK"; // WFA (WPA3 & WPA2 Mixed -> WPA2 only)
7246                 else
7247                         key_mgmt = "SAE";
7248                 add_network_security_psk(dict, ssid);
7249                 break;
7250         case G_SUPPLICANT_SECURITY_OWE:
7251                 key_mgmt = "OWE";
7252                 add_network_security_ciphers(dict, ssid);
7253                 add_network_security_proto(dict, ssid);
7254                 break;
7255         case G_SUPPLICANT_SECURITY_DPP:
7256                 key_mgmt = "DPP";
7257                 add_network_security_connector(dict, ssid);
7258                 add_network_security_c_sign_key(dict, ssid);
7259                 add_network_security_net_access_key(dict, ssid);
7260                 break;
7261 #endif
7262         }
7263
7264         supplicant_dbus_dict_append_basic(dict, "key_mgmt",
7265                                 DBUS_TYPE_STRING, &key_mgmt);
7266 }
7267
7268 static void add_network_mode(DBusMessageIter *dict, GSupplicantSSID *ssid)
7269 {
7270         dbus_uint32_t mode;
7271
7272         switch (ssid->mode) {
7273         case G_SUPPLICANT_MODE_UNKNOWN:
7274         case G_SUPPLICANT_MODE_INFRA:
7275                 mode = 0;
7276                 break;
7277         case G_SUPPLICANT_MODE_IBSS:
7278                 mode = 1;
7279                 break;
7280         case G_SUPPLICANT_MODE_MASTER:
7281                 mode = 2;
7282                 break;
7283 #if defined TIZEN_EXT_WIFI_MESH
7284         case G_SUPPLICANT_MODE_MESH:
7285                 mode = 5;
7286                 break;
7287 #endif
7288         }
7289
7290         supplicant_dbus_dict_append_basic(dict, "mode",
7291                                 DBUS_TYPE_UINT32, &mode);
7292 }
7293
7294 static void interface_add_network_params(DBusMessageIter *iter, void *user_data)
7295 {
7296         DBusMessageIter dict;
7297         struct interface_connect_data *data = user_data;
7298         GSupplicantSSID *ssid = data->ssid;
7299 #if defined TIZEN_EXT
7300         GSupplicantInterface *interface = data->interface;
7301 #endif
7302
7303         supplicant_dbus_dict_open(iter, &dict);
7304
7305         if (ssid->scan_ssid)
7306                 supplicant_dbus_dict_append_basic(&dict, "scan_ssid",
7307                                          DBUS_TYPE_UINT32, &ssid->scan_ssid);
7308
7309         if (ssid->freq)
7310                 supplicant_dbus_dict_append_basic(&dict, "frequency",
7311                                          DBUS_TYPE_UINT32, &ssid->freq);
7312
7313         if (ssid->bgscan)
7314                 supplicant_dbus_dict_append_basic(&dict, "bgscan",
7315                                         DBUS_TYPE_STRING, &ssid->bgscan);
7316
7317         add_network_mode(&dict, ssid);
7318
7319         add_network_security(&dict, ssid);
7320
7321 #if defined TIZEN_EXT
7322         add_network_ieee80211w(&dict, ssid);
7323 #endif
7324
7325         supplicant_dbus_dict_append_fixed_array(&dict, "ssid",
7326                                         DBUS_TYPE_BYTE, &ssid->ssid,
7327                                                 ssid->ssid_len);
7328
7329 #if defined TIZEN_EXT
7330         bool owe_transition_mode = FALSE;
7331         if ((ssid->security == G_SUPPLICANT_SECURITY_OWE) && !(ssid->keymgmt & G_SUPPLICANT_KEYMGMT_OWE))
7332                 owe_transition_mode = TRUE;
7333
7334         if (ssid->bssid && !owe_transition_mode) {
7335                 char *bssid = NULL;
7336                 bssid = g_try_malloc0(18);
7337                 if (bssid == NULL) {
7338                         SUPPLICANT_DBG("memory allocation error");
7339                         supplicant_dbus_dict_close(iter, &dict);
7340                         return;
7341                 }
7342
7343                 if (ssid->bssid_for_connect_len) {
7344                         snprintf(bssid, 18, MACSTR, MAC2STR(ssid->bssid_for_connect));
7345                         memcpy(interface->add_network_bssid, ssid->bssid_for_connect, WIFI_BSSID_LEN_MAX);
7346                 } else {
7347                         snprintf(bssid, 18, MACSTR, MAC2STR(ssid->bssid));
7348                         memcpy(interface->add_network_bssid, ssid->bssid, WIFI_BSSID_LEN_MAX);
7349                 }
7350
7351                 SUPPLICANT_DBG("bssid [" MACSTR "]", MAC2STR(interface->add_network_bssid));
7352
7353                 supplicant_dbus_dict_append_basic(&dict, "bssid",
7354                                         DBUS_TYPE_STRING, &bssid);
7355                 g_free(bssid);
7356         }
7357 #endif
7358
7359         supplicant_dbus_dict_close(iter, &dict);
7360 }
7361
7362 static void interface_wps_start_result(const char *error,
7363                                 DBusMessageIter *iter, void *user_data)
7364 {
7365         struct interface_connect_data *data = user_data;
7366         int err;
7367
7368         SUPPLICANT_DBG("");
7369
7370         err = 0;
7371         if (error) {
7372                 SUPPLICANT_DBG("error: %s", error);
7373                 err = parse_supplicant_error(iter);
7374         }
7375
7376         if(data->callback)
7377                 data->callback(err, data->interface, data->user_data);
7378
7379         g_free(data->path);
7380         g_free(data->ssid);
7381         dbus_free(data);
7382 }
7383
7384 static void interface_add_wps_params(DBusMessageIter *iter, void *user_data)
7385 {
7386         struct interface_connect_data *data = user_data;
7387         GSupplicantSSID *ssid = data->ssid;
7388         const char *role = "enrollee", *type;
7389         DBusMessageIter dict;
7390
7391         SUPPLICANT_DBG("");
7392
7393         supplicant_dbus_dict_open(iter, &dict);
7394
7395         supplicant_dbus_dict_append_basic(&dict, "Role",
7396                                                 DBUS_TYPE_STRING, &role);
7397
7398         type = "pbc";
7399         if (ssid->pin_wps) {
7400                 type = "pin";
7401                 supplicant_dbus_dict_append_basic(&dict, "Pin",
7402                                         DBUS_TYPE_STRING, &ssid->pin_wps);
7403         }
7404
7405         supplicant_dbus_dict_append_basic(&dict, "Type",
7406                                         DBUS_TYPE_STRING, &type);
7407
7408 #if defined TIZEN_EXT
7409         if (ssid->bssid)
7410                 supplicant_dbus_dict_append_fixed_array(&dict, "Bssid",
7411                                                 DBUS_TYPE_BYTE, &ssid->bssid, 6);
7412 #endif
7413
7414         supplicant_dbus_dict_close(iter, &dict);
7415 }
7416
7417 static void wps_start(const char *error, DBusMessageIter *iter, void *user_data)
7418 {
7419         struct interface_connect_data *data = user_data;
7420
7421         SUPPLICANT_DBG("");
7422
7423         if (error) {
7424                 SUPPLICANT_DBG("error: %s", error);
7425                 g_free(data->path);
7426                 g_free(data->ssid);
7427                 dbus_free(data);
7428                 return;
7429         }
7430
7431 #if defined TIZEN_EXT
7432         GSupplicantSSID *ssid = data->ssid;
7433         if (ssid->pin_wps != NULL) {
7434                 if (!g_utf8_validate(ssid->pin_wps, 8, NULL)) {
7435                         SUPPLICANT_DBG("Invalid characters in WPS_PIN");
7436                         g_free(data->ssid);
7437                         dbus_free(data);
7438                         return;
7439                 }
7440         }
7441 #endif
7442         supplicant_dbus_method_call(data->interface->path,
7443                         SUPPLICANT_INTERFACE ".Interface.WPS", "Start",
7444                         interface_add_wps_params,
7445                         interface_wps_start_result, data, NULL);
7446 }
7447
7448 static void wps_process_credentials(DBusMessageIter *iter, void *user_data)
7449 {
7450         dbus_bool_t credentials = TRUE;
7451
7452         SUPPLICANT_DBG("");
7453
7454         dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &credentials);
7455 }
7456
7457
7458 #if defined TIZEN_EXT
7459 #define NETCONFIG_SERVICE "net.netconfig"
7460 #define NETCONFIG_WIFI_PATH "/net/netconfig/wifi"
7461 #define NETCONFIG_WIFI_INTERFACE NETCONFIG_SERVICE ".wifi"
7462
7463 struct dec_method_call_data {
7464         struct interface_connect_data *data;
7465         DBusPendingCall *pending_call;
7466 };
7467
7468 static struct dec_method_call_data decrypt_request_data;
7469
7470 static void crypt_method_call_cancel(void)
7471 {
7472         if (decrypt_request_data.pending_call) {
7473                 dbus_pending_call_cancel(decrypt_request_data.pending_call);
7474                 dbus_pending_call_unref(decrypt_request_data.pending_call);
7475                 decrypt_request_data.pending_call = NULL;
7476         }
7477
7478         g_free(decrypt_request_data.data->path);
7479         g_free(decrypt_request_data.data->ssid);
7480         dbus_free(decrypt_request_data.data);
7481         decrypt_request_data.data = NULL;
7482 }
7483
7484 static void decryption_request_reply(DBusPendingCall *call,
7485                                                 void *user_data)
7486 {
7487         DBusMessage *reply;
7488         DBusError error;
7489         DBusMessageIter args;
7490         char *out_data;
7491         int ret;
7492         struct interface_connect_data *data = user_data;
7493
7494         SUPPLICANT_DBG("");
7495
7496         reply = dbus_pending_call_steal_reply(call);
7497
7498         dbus_error_init(&error);
7499         if (dbus_set_error_from_message(&error, reply)) {
7500                 SUPPLICANT_DBG("decryption_request_reply() %s %s", error.name, error.message);
7501                 dbus_error_free(&error);
7502                 ret = -EINVAL;
7503                 goto done;
7504         }
7505
7506         if (dbus_message_iter_init(reply, &args) == FALSE) {
7507                 SUPPLICANT_DBG("dbus_message_iter_init() failed");
7508                 ret = -EINVAL;
7509                 goto done;
7510         }
7511
7512         dbus_message_iter_get_basic(&args, &out_data);
7513         data->ssid->passphrase = g_strdup((const gchar *)out_data);
7514
7515         ret = supplicant_dbus_method_call(data->interface->path,
7516                 SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
7517                 interface_add_network_params,
7518                 interface_add_network_result, data,
7519                 data->interface);
7520
7521 done:
7522         if (ret < 0) {
7523                 SUPPLICANT_DBG("AddNetwork failed %d", ret);
7524                 callback_assoc_failed(decrypt_request_data.data->user_data);
7525                 g_free(data->path);
7526                 g_free(data->ssid->ssid);
7527                 g_free((char *)data->ssid->passphrase);
7528                 g_free((char *)data->ssid->connector);
7529                 g_free((char *)data->ssid->c_sign_key);
7530                 g_free((char *)data->ssid->net_access_key);
7531                 g_free(data->ssid);
7532                 dbus_free(data);
7533         }
7534
7535         dbus_message_unref(reply);
7536         dbus_pending_call_unref(call);
7537
7538         decrypt_request_data.pending_call = NULL;
7539         decrypt_request_data.data = NULL;
7540 }
7541
7542 static int send_decryption_request(const char *passphrase,
7543                         struct interface_connect_data *data)
7544 {
7545         DBusMessage *msg = NULL;
7546         DBusPendingCall *call;
7547
7548         SUPPLICANT_DBG("Decryption request");
7549
7550         if (!passphrase) {
7551                 SUPPLICANT_DBG("Invalid parameter");
7552                 return -EINVAL;
7553         }
7554
7555         if (!connection)
7556                 return -EINVAL;
7557
7558         msg = dbus_message_new_method_call(NETCONFIG_SERVICE, NETCONFIG_WIFI_PATH,
7559                         NETCONFIG_WIFI_INTERFACE, "DecryptPassphrase");
7560         if (!msg)
7561                 return -EINVAL;
7562
7563 #if defined TIZEN_EXT
7564         if (!dbus_message_append_args(msg, DBUS_TYPE_STRING, &passphrase,
7565                                                         DBUS_TYPE_INVALID)) {
7566                 SUPPLICANT_DBG("Could not fulfill decryption request");
7567                 return -ENOMEM;
7568         }
7569 #else
7570         dbus_message_append_args(msg, DBUS_TYPE_STRING, &passphrase,
7571                                                         DBUS_TYPE_INVALID);
7572 #endif
7573
7574         if (!dbus_connection_send_with_reply(connection, msg,
7575                                 &call, DBUS_TIMEOUT_USE_DEFAULT)) {
7576                 dbus_message_unref(msg);
7577                 return -EIO;
7578         }
7579
7580         if (!call) {
7581                 dbus_message_unref(msg);
7582                 return -EIO;
7583         }
7584
7585         decrypt_request_data.pending_call = call;
7586         decrypt_request_data.data = data;
7587
7588         dbus_pending_call_set_notify(call, decryption_request_reply, data, NULL);
7589         dbus_message_unref(msg);
7590
7591         SUPPLICANT_DBG("Decryption request succeeded");
7592
7593         return 0;
7594 }
7595
7596 static void decrypt_conf_obj_reply(DBusPendingCall *call,
7597                                                 void *user_data)
7598 {
7599         DBusMessage *reply;
7600         DBusError error;
7601         DBusMessageIter iter, dict;
7602         char *out_data;
7603         int ret;
7604         struct interface_connect_data *data = user_data;
7605
7606         reply = dbus_pending_call_steal_reply(call);
7607
7608         dbus_error_init(&error);
7609         if (dbus_set_error_from_message(&error, reply)) {
7610                 SUPPLICANT_DBG("decryption_conf_obj_reply() %s %s", error.name, error.message);
7611                 dbus_error_free(&error);
7612                 ret = -EINVAL;
7613                 goto done;
7614         }
7615
7616         if (dbus_message_iter_init(reply, &iter) == FALSE) {
7617                 SUPPLICANT_DBG("dbus_message_iter_init() failed");
7618                 ret = -EINVAL;
7619                 goto done;
7620         }
7621
7622         dbus_message_iter_recurse(&iter, &dict);
7623
7624         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
7625                 DBusMessageIter entry, value;
7626                 const char *key;
7627
7628                 dbus_message_iter_recurse(&dict, &entry);
7629                 dbus_message_iter_get_basic(&entry, &key);
7630                 dbus_message_iter_next(&entry);
7631                 dbus_message_iter_recurse(&entry, &value);
7632                 if (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
7633                         if (g_strcmp0(key, "connector") == 0) {
7634                                 dbus_message_iter_get_basic(&value, &out_data);
7635                                 data->ssid->connector = g_strdup((const gchar *)out_data);
7636                                 SUPPLICANT_DBG("connector %s", data->ssid->connector);
7637                         } else if (g_strcmp0(key, "c_sign_key") == 0) {
7638                                 dbus_message_iter_get_basic(&value, &out_data);
7639                                 data->ssid->c_sign_key = g_strdup((const gchar *)out_data);
7640                                 SUPPLICANT_DBG("c_sign_key %s", data->ssid->c_sign_key);
7641                         } else if (g_strcmp0(key, "net_access_key") == 0) {
7642                                 dbus_message_iter_get_basic(&value, &out_data);
7643                                 data->ssid->net_access_key = g_strdup((const gchar *)out_data);
7644                                 SUPPLICANT_DBG("net_access_key %s", data->ssid->net_access_key);
7645                         }
7646                 }
7647                 dbus_message_iter_next(&dict);
7648         }
7649
7650         ret = supplicant_dbus_method_call(data->interface->path,
7651                 SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
7652                 interface_add_network_params,
7653                 interface_add_network_result, data,
7654                 data->interface);
7655
7656 done:
7657         if (ret < 0) {
7658                 SUPPLICANT_DBG("AddNetwork failed %d", ret);
7659                 callback_assoc_failed(decrypt_request_data.data->user_data);
7660                 g_free(data->path);
7661                 g_free(data->ssid->ssid);
7662                 g_free((char *)data->ssid->connector);
7663                 g_free((char *)data->ssid->c_sign_key);
7664                 g_free((char *)data->ssid->net_access_key);
7665                 g_free(data->ssid);
7666                 dbus_free(data);
7667         }
7668
7669         dbus_message_unref(reply);
7670         dbus_pending_call_unref(call);
7671
7672         decrypt_request_data.pending_call = NULL;
7673         decrypt_request_data.data = NULL;
7674 }
7675
7676 static int send_decryption_conf_obj_request(GSupplicantSSID *ssid,
7677                         struct interface_connect_data *data)
7678 {
7679         DBusMessage *msg = NULL;
7680         DBusPendingCall *call;
7681
7682         SUPPLICANT_DBG("Decryption configuration object request");
7683
7684         if (!ssid) {
7685                 SUPPLICANT_DBG("Invalid parameter");
7686                 return -EINVAL;
7687         }
7688
7689         if (!connection)
7690                 return -EINVAL;
7691
7692         msg = dbus_message_new_method_call(NETCONFIG_SERVICE, NETCONFIG_WIFI_PATH,
7693                         NETCONFIG_WIFI_INTERFACE, "DecryptConfObj");
7694         if (!msg)
7695                 return -EINVAL;
7696
7697         dbus_message_append_args(msg, DBUS_TYPE_STRING, &ssid->connector,
7698                                                         DBUS_TYPE_INVALID);
7699         dbus_message_append_args(msg, DBUS_TYPE_STRING, &ssid->c_sign_key,
7700                                                         DBUS_TYPE_INVALID);
7701         dbus_message_append_args(msg, DBUS_TYPE_STRING, &ssid->net_access_key,
7702                                                         DBUS_TYPE_INVALID);
7703
7704         if (!dbus_connection_send_with_reply(connection, msg,
7705                                 &call, DBUS_TIMEOUT_USE_DEFAULT)) {
7706                 dbus_message_unref(msg);
7707                 return -EIO;
7708         }
7709
7710         if (!call) {
7711                 dbus_message_unref(msg);
7712                 return -EIO;
7713         }
7714
7715         decrypt_request_data.pending_call = call;
7716         decrypt_request_data.data = data;
7717
7718         dbus_pending_call_set_notify(call, decrypt_conf_obj_reply, data, NULL);
7719         dbus_message_unref(msg);
7720
7721         SUPPLICANT_DBG("Decrypt Conf Obj request succeeded");
7722
7723         return 0;
7724 }
7725
7726 static bool is_valid_config_object(GSupplicantSSID *ssid)
7727 {
7728         return ((ssid->connector &&
7729                         g_strcmp0(ssid->connector, "") != 0) &&
7730                         (ssid->c_sign_key &&
7731                         g_strcmp0(ssid->c_sign_key, "") != 0) &&
7732                         (ssid->net_access_key &&
7733                         g_strcmp0(ssid->net_access_key, "") != 0));
7734 }
7735 #endif
7736
7737 int g_supplicant_interface_connect(GSupplicantInterface *interface,
7738                                 GSupplicantSSID *ssid,
7739                                 GSupplicantInterfaceCallback callback,
7740                                                         void *user_data)
7741 {
7742         struct interface_connect_data *data;
7743         struct interface_data *intf_data;
7744         int ret = 0;
7745
7746         SUPPLICANT_DBG("");
7747
7748         if (!interface)
7749                 return -EINVAL;
7750
7751         if (!system_available)
7752                 return -EFAULT;
7753
7754         /* TODO: Check if we're already connected and switch */
7755
7756         data = dbus_malloc0(sizeof(*data));
7757         if (!data)
7758                 return -ENOMEM;
7759
7760         data->interface = interface;
7761         data->path = g_strdup(interface->path);
7762         data->callback = callback;
7763         data->ssid = ssid;
7764         data->user_data = user_data;
7765
7766         if (ssid->use_wps) {
7767                 g_free(interface->wps_cred.key);
7768                 memset(&interface->wps_cred, 0,
7769                                 sizeof(struct _GSupplicantWpsCredentials));
7770
7771                 ret = supplicant_dbus_property_set(interface->path,
7772                         SUPPLICANT_INTERFACE ".Interface.WPS",
7773                         "ProcessCredentials", DBUS_TYPE_BOOLEAN_AS_STRING,
7774                         wps_process_credentials, wps_start, data, interface);
7775         } else {
7776                 /* By the time there is a request for connect and the network
7777                  * path is not NULL it means that connman has not removed the
7778                  * previous network pointer. This can happen in the case AP
7779                  * deauthenticated client and connman does not remove the
7780                  * previously connected network pointer. This causes supplicant
7781                  * to reallocate the memory for struct wpa_ssid again even if it
7782                  * is the same SSID. This causes memory usage of wpa_supplicnat
7783                  * to go high. The idea here is that if the previously connected
7784                  * network is not removed at the time of next connection attempt
7785                  * check if the network path is not NULL. In case it is non-NULL
7786                  * first remove the network and then once removal is successful, add
7787                  * the network.
7788                  */
7789
7790                 if (interface->network_path != NULL) {
7791                         g_free(data->path);
7792                         dbus_free(data);
7793
7794                         /*
7795                          * If this add network is for the same network for
7796                          * which wpa_supplicant already has a profile then do
7797                          * not need to add another profile. Only if the
7798                          * profile that needs to get added is different from
7799                          * what is there in wpa_s delete the current one. A
7800                          * network is identified by its SSID, security_type
7801                          * and passphrase (private passphrase in case security
7802                          * type is 802.11x).
7803                          */
7804                         if (compare_network_parameters(interface, ssid)) {
7805                                 return -EALREADY;
7806                         }
7807
7808                         intf_data = dbus_malloc0(sizeof(*intf_data));
7809                         if (!intf_data)
7810                                 return -ENOMEM;
7811
7812                         intf_data->interface = interface;
7813                         intf_data->path = g_strdup(interface->path);
7814                         intf_data->callback = callback;
7815                         intf_data->ssid = ssid;
7816                         intf_data->user_data = user_data;
7817                         intf_data->network_remove_in_progress = TRUE;
7818                         network_remove(intf_data);
7819                 } else {
7820 #if defined TIZEN_EXT
7821                         if (ssid->passphrase &&
7822                             g_strcmp0(ssid->passphrase, "") != 0 &&
7823 #if defined TIZEN_EXT_WIFI_MESH
7824                             ssid->mode != G_SUPPLICANT_MODE_MESH &&
7825 #endif
7826                             !ssid->eap) {
7827                                 ret = send_decryption_request(ssid->passphrase, data);
7828                                 if (ret < 0)
7829                                         SUPPLICANT_DBG("Decryption request failed %d", ret);
7830                         } else if (is_valid_config_object(ssid)) {
7831                                 ret = send_decryption_conf_obj_request(ssid, data);
7832                                 if (ret < 0)
7833                                         SUPPLICANT_DBG("Decryption Conf Obj request failed %d", ret);
7834
7835                         } else
7836 #endif
7837                         ret = supplicant_dbus_method_call(interface->path,
7838                                         SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
7839                                         interface_add_network_params,
7840                                         interface_add_network_result, data,
7841                                         interface);
7842                 }
7843         }
7844
7845         if (ret < 0) {
7846                 g_free(data->path);
7847                 dbus_free(data);
7848                 return ret;
7849         }
7850
7851         return -EINPROGRESS;
7852 }
7853
7854 static void network_remove_result(const char *error,
7855                                 DBusMessageIter *iter, void *user_data)
7856 {
7857         struct interface_data *data = user_data;
7858         struct interface_connect_data *connect_data;
7859         int result = 0;
7860
7861         SUPPLICANT_DBG("");
7862
7863         if (error) {
7864                 result = -EIO;
7865                 SUPPLICANT_DBG("error: %s", error);
7866
7867                 if (g_strcmp0("org.freedesktop.DBus.Error.UnknownMethod",
7868                                                 error) == 0)
7869                         result = -ECONNABORTED;
7870         }
7871
7872         g_free(data->interface->network_path);
7873         data->interface->network_path = NULL;
7874
7875         remove_network_information(data->interface);
7876
7877         if (data->network_remove_in_progress == TRUE) {
7878                 data->network_remove_in_progress = FALSE;
7879                 connect_data = dbus_malloc0(sizeof(*connect_data));
7880                 if (!connect_data)
7881                         return;
7882
7883                 connect_data->interface = data->interface;
7884                 connect_data->path = g_strdup(data->path);
7885                 connect_data->callback = data->callback;
7886                 connect_data->ssid = data->ssid;
7887                 connect_data->user_data = data->user_data;
7888
7889 #if defined TIZEN_EXT
7890                 int ret;
7891                 if (data->ssid->passphrase && g_strcmp0(data->ssid->passphrase, "") != 0
7892                         && !data->ssid->eap) {
7893                         ret = send_decryption_request(data->ssid->passphrase, connect_data);
7894                         if (ret < 0) {
7895                                 SUPPLICANT_DBG("Decryption request failed %d", ret);
7896                                 g_free(connect_data->ssid);
7897                                 g_free(connect_data->path);
7898                                 dbus_free(connect_data);
7899                         }
7900                 } else if (is_valid_config_object(data->ssid)) {
7901                         ret = send_decryption_conf_obj_request(data->ssid, connect_data);
7902                         if (ret < 0) {
7903                                 SUPPLICANT_DBG("Decryption Conf Obj request failed %d", ret);
7904                                 g_free(connect_data->ssid);
7905                                 g_free(connect_data->path);
7906                                 dbus_free(connect_data);
7907                         }
7908                 } else
7909 #endif
7910                 supplicant_dbus_method_call(data->interface->path,
7911                         SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
7912                         interface_add_network_params,
7913                         interface_add_network_result, connect_data,
7914                         connect_data->interface);
7915         } else {
7916                 if (data->callback)
7917                         data->callback(result, data->interface, data->user_data);
7918         }
7919         g_free(data->path);
7920         dbus_free(data);
7921 }
7922
7923 static void network_remove_params(DBusMessageIter *iter, void *user_data)
7924 {
7925         struct interface_data *data = user_data;
7926         const char *path = data->interface->network_path;
7927
7928         SUPPLICANT_DBG("path %s", path);
7929
7930         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
7931 }
7932
7933 static int network_remove(struct interface_data *data)
7934 {
7935         GSupplicantInterface *interface = data->interface;
7936
7937         SUPPLICANT_DBG("");
7938
7939 #if defined TIZEN_EXT
7940         GSupplicantInterface *intf = NULL;
7941         /*
7942          * Check if 'interface' is valid
7943          */
7944         intf = g_hash_table_lookup(interface_table, interface->path);
7945         if (intf == NULL)
7946                 return -EINVAL;
7947 #endif
7948
7949         return supplicant_dbus_method_call(interface->path,
7950                         SUPPLICANT_INTERFACE ".Interface", "RemoveNetwork",
7951                         network_remove_params, network_remove_result, data,
7952                         interface);
7953 }
7954
7955 static void interface_disconnect_result(const char *error,
7956                                 DBusMessageIter *iter, void *user_data)
7957 {
7958         struct interface_data *data = user_data;
7959         int result = 0;
7960
7961         SUPPLICANT_DBG("");
7962
7963         if (error) {
7964                 result = -EIO;
7965                 SUPPLICANT_DBG("error: %s", error);
7966
7967                 if (g_strcmp0("org.freedesktop.DBus.Error.UnknownMethod",
7968                                                 error) == 0)
7969                         result = -ECONNABORTED;
7970         }
7971
7972         /* If we are disconnecting from previous WPS successful
7973          * association. i.e.: it did not went through AddNetwork,
7974          * and interface->network_path was never set. */
7975         if (!data->interface->network_path) {
7976                 if (data->callback)
7977                         data->callback(result, data->interface,
7978                                                         data->user_data);
7979
7980                 g_free(data->path);
7981                 dbus_free(data);
7982                 return;
7983         }
7984
7985         if (result < 0 && data->callback) {
7986                 data->callback(result, data->interface, data->user_data);
7987                 data->callback = NULL;
7988         }
7989
7990         if (result != -ECONNABORTED) {
7991                 if (network_remove(data) < 0) {
7992                         g_free(data->path);
7993                         dbus_free(data);
7994                 }
7995         } else {
7996                 g_free(data->path);
7997                 dbus_free(data);
7998         }
7999 }
8000
8001 int g_supplicant_interface_disconnect(GSupplicantInterface *interface,
8002                                         GSupplicantInterfaceCallback callback,
8003                                                         void *user_data)
8004 {
8005         struct interface_data *data;
8006         int ret;
8007
8008         SUPPLICANT_DBG("");
8009
8010         if (!interface)
8011                 return -EINVAL;
8012
8013         if (!system_available)
8014                 return -EFAULT;
8015 #if defined TIZEN_EXT
8016         if (decrypt_request_data.pending_call &&
8017                         decrypt_request_data.data &&
8018                         decrypt_request_data.data->user_data == user_data) {
8019
8020                 callback_assoc_failed(decrypt_request_data.data->user_data);
8021                 crypt_method_call_cancel();
8022
8023                 return 0;
8024         }
8025 #endif
8026         data = dbus_malloc0(sizeof(*data));
8027         if (!data)
8028                 return -ENOMEM;
8029
8030         data->interface = interface;
8031         data->path = g_strdup(interface->path);
8032         data->callback = callback;
8033         data->user_data = user_data;
8034
8035         ret = supplicant_dbus_method_call(interface->path,
8036                         SUPPLICANT_INTERFACE ".Interface", "Disconnect",
8037                         NULL, interface_disconnect_result, data,
8038                         interface);
8039
8040         if (ret < 0) {
8041                 g_free(data->path);
8042                 dbus_free(data);
8043         }
8044
8045         return ret;
8046 }
8047
8048 static void interface_p2p_find_result(const char *error,
8049                                         DBusMessageIter *iter, void *user_data)
8050 {
8051         struct interface_scan_data *data = user_data;
8052         int err = 0;
8053
8054         SUPPLICANT_DBG("error %s", error);
8055
8056         if (error)
8057                 err = -EIO;
8058
8059         if (interface_exists(data->interface, data->path)) {
8060                 if (!data->interface->ready)
8061                         err = -ENOLINK;
8062                 if (!err)
8063                         data->interface->p2p_finding = true;
8064         }
8065
8066         if (data->callback)
8067                 data->callback(err, data->interface, data->user_data);
8068
8069         g_free(data->path);
8070         dbus_free(data);
8071 }
8072
8073 static void interface_p2p_find_params(DBusMessageIter *iter, void *user_data)
8074 {
8075         DBusMessageIter dict;
8076
8077         supplicant_dbus_dict_open(iter, &dict);
8078         supplicant_dbus_dict_close(iter, &dict);
8079 }
8080
8081 int g_supplicant_interface_p2p_find(GSupplicantInterface *interface,
8082                                         GSupplicantInterfaceCallback callback,
8083                                                         void *user_data)
8084 {
8085         struct interface_scan_data *data;
8086         int ret;
8087
8088         if (!interface->p2p_support)
8089                 return -ENOTSUP;
8090
8091         ret = interface_ready_to_scan(interface);
8092         if (ret && ret != -EALREADY)
8093                 return ret;
8094
8095         data = dbus_malloc0(sizeof(*data));
8096         if (!data)
8097                 return -ENOMEM;
8098
8099         data->interface = interface;
8100         data->path = g_strdup(interface->path);
8101         data->callback = callback;
8102         data->user_data = user_data;
8103
8104         ret = supplicant_dbus_method_call(interface->path,
8105                         SUPPLICANT_INTERFACE ".Interface.P2PDevice", "Find",
8106                         interface_p2p_find_params, interface_p2p_find_result,
8107                         data, interface);
8108         if (ret < 0) {
8109                 g_free(data->path);
8110                 dbus_free(data);
8111         }
8112
8113         return ret;
8114 }
8115
8116 bool g_supplicant_interface_is_p2p_finding(GSupplicantInterface *interface)
8117 {
8118         if (!interface)
8119                 return false;
8120
8121         return interface->p2p_finding;
8122 }
8123
8124 int g_supplicant_interface_p2p_stop_find(GSupplicantInterface *interface)
8125 {
8126         if (!interface->p2p_finding)
8127                 return 0;
8128
8129         SUPPLICANT_DBG("");
8130
8131         interface->p2p_finding = false;
8132
8133         return supplicant_dbus_method_call(interface->path,
8134                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "StopFind",
8135                 NULL, NULL, NULL, NULL);
8136 }
8137
8138 static void interface_p2p_connect_result(const char *error,
8139                                         DBusMessageIter *iter, void *user_data)
8140 {
8141         struct interface_connect_data *data = user_data;
8142         int err = 0;
8143
8144         SUPPLICANT_DBG("");
8145
8146         if (error) {
8147                 SUPPLICANT_DBG("error: %s", error);
8148                 err = parse_supplicant_error(iter);
8149         }
8150
8151         if (data->callback)
8152                 data->callback(err, data->interface, data->user_data);
8153
8154         g_free(data->path);
8155         g_free(data->peer->wps_pin);
8156         g_free(data->peer->path);
8157         g_free(data->peer);
8158         g_free(data);
8159 }
8160
8161 static void interface_p2p_connect_params(DBusMessageIter *iter, void *user_data)
8162 {
8163         struct interface_connect_data *data = user_data;
8164         const char *wps = "pbc";
8165         DBusMessageIter dict;
8166         int go_intent = 7;
8167
8168         SUPPLICANT_DBG("");
8169
8170         supplicant_dbus_dict_open(iter, &dict);
8171
8172         if (data->peer->master)
8173                 go_intent = 15;
8174
8175         if (data->peer->wps_pin)
8176                 wps = "pin";
8177
8178         supplicant_dbus_dict_append_basic(&dict, "peer",
8179                                 DBUS_TYPE_OBJECT_PATH, &data->peer->path);
8180         supplicant_dbus_dict_append_basic(&dict, "wps_method",
8181                                 DBUS_TYPE_STRING, &wps);
8182         if (data->peer->wps_pin) {
8183                 supplicant_dbus_dict_append_basic(&dict, "pin",
8184                                 DBUS_TYPE_STRING, &data->peer->wps_pin);
8185         }
8186
8187         supplicant_dbus_dict_append_basic(&dict, "go_intent",
8188                                         DBUS_TYPE_INT32, &go_intent);
8189
8190         supplicant_dbus_dict_close(iter, &dict);
8191 }
8192
8193 int g_supplicant_interface_p2p_connect(GSupplicantInterface *interface,
8194                                         GSupplicantPeerParams *peer_params,
8195                                         GSupplicantInterfaceCallback callback,
8196                                         void *user_data)
8197 {
8198         struct interface_connect_data *data;
8199         int ret;
8200
8201         SUPPLICANT_DBG("");
8202
8203         if (!interface->p2p_support)
8204                 return -ENOTSUP;
8205
8206         data = dbus_malloc0(sizeof(*data));
8207         if (!data)
8208                 return -ENOMEM;
8209
8210         data->interface = interface;
8211         data->path = g_strdup(interface->path);
8212         data->peer = peer_params;
8213         data->callback = callback;
8214         data->user_data = user_data;
8215
8216         ret = supplicant_dbus_method_call(interface->path,
8217                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "Connect",
8218                 interface_p2p_connect_params, interface_p2p_connect_result,
8219                 data, interface);
8220         if (ret < 0) {
8221                 g_free(data->path);
8222                 dbus_free(data);
8223                 return ret;
8224         }
8225
8226         return -EINPROGRESS;
8227 }
8228
8229 int g_supplicant_interface_p2p_disconnect(GSupplicantInterface *interface,
8230                                         GSupplicantPeerParams *peer_params)
8231 {
8232         GSupplicantPeer *peer;
8233         int count = 0;
8234         GSList *list;
8235
8236         SUPPLICANT_DBG("");
8237
8238         if (!interface->p2p_support)
8239                 return -ENOTSUP;
8240
8241         peer = g_hash_table_lookup(interface->peer_table, peer_params->path);
8242         if (!peer)
8243                 return -ENODEV;
8244
8245         for (list = peer->groups; list; list = list->next, count++) {
8246                 const char *group_obj_path = list->data;
8247                 GSupplicantInterface *g_interface;
8248                 GSupplicantGroup *group;
8249
8250                 group = g_hash_table_lookup(group_mapping, group_obj_path);
8251                 if (!group || !group->interface)
8252                         continue;
8253
8254                 g_interface = group->interface;
8255                 supplicant_dbus_method_call(g_interface->path,
8256                                 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
8257                                 "Disconnect", NULL, NULL, NULL, g_interface);
8258         }
8259
8260         if (count == 0 && peer->current_group_iface) {
8261                 supplicant_dbus_method_call(peer->current_group_iface->path,
8262                                 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
8263                                 "Disconnect", NULL, NULL, NULL,
8264                                 peer->current_group_iface->path);
8265         }
8266
8267         peer->current_group_iface = NULL;
8268
8269         return -EINPROGRESS;
8270 }
8271
8272 struct p2p_service_data {
8273         bool registration;
8274         GSupplicantInterface *interface;
8275         GSupplicantP2PServiceParams *service;
8276         GSupplicantInterfaceCallback callback;
8277         void *user_data;
8278 };
8279
8280 static void interface_p2p_service_result(const char *error,
8281                                         DBusMessageIter *iter, void *user_data)
8282 {
8283         struct p2p_service_data *data = user_data;
8284         int result = 0;
8285
8286         SUPPLICANT_DBG("%s result - %s", data->registration ?
8287                                 "Registration" : "Deletion",
8288                                 error ? error : "Success");
8289         if (error)
8290                 result = -EINVAL;
8291
8292         if (data->callback)
8293                 data->callback(result, data->interface, data->user_data);
8294
8295         g_free(data->service->query);
8296         g_free(data->service->response);
8297         g_free(data->service->service);
8298         g_free(data->service->wfd_ies);
8299         g_free(data->service);
8300         dbus_free(data);
8301 }
8302
8303 static void interface_p2p_service_params(DBusMessageIter *iter,
8304                                                         void *user_data)
8305 {
8306         struct p2p_service_data *data = user_data;
8307         GSupplicantP2PServiceParams *service;
8308         DBusMessageIter dict;
8309         const char *type;
8310
8311         SUPPLICANT_DBG("");
8312
8313         service = data->service;
8314
8315         supplicant_dbus_dict_open(iter, &dict);
8316
8317         if (service->query && service->response) {
8318                 type = "bonjour";
8319                 supplicant_dbus_dict_append_basic(&dict, "service_type",
8320                                                 DBUS_TYPE_STRING, &type);
8321                 supplicant_dbus_dict_append_fixed_array(&dict, "query",
8322                                         DBUS_TYPE_BYTE, &service->query,
8323                                         service->query_length);
8324                 supplicant_dbus_dict_append_fixed_array(&dict, "response",
8325                                         DBUS_TYPE_BYTE, &service->response,
8326                                         service->response_length);
8327         } else if (service->version && service->service) {
8328                 type = "upnp";
8329                 supplicant_dbus_dict_append_basic(&dict, "service_type",
8330                                                 DBUS_TYPE_STRING, &type);
8331                 supplicant_dbus_dict_append_basic(&dict, "version",
8332                                         DBUS_TYPE_INT32, &service->version);
8333                 supplicant_dbus_dict_append_basic(&dict, "service",
8334                                         DBUS_TYPE_STRING, &service->service);
8335         }
8336
8337         supplicant_dbus_dict_close(iter, &dict);
8338 }
8339
8340 int g_supplicant_interface_p2p_add_service(GSupplicantInterface *interface,
8341                                 GSupplicantInterfaceCallback callback,
8342                                 GSupplicantP2PServiceParams *p2p_service_params,
8343                                 void *user_data)
8344 {
8345         struct p2p_service_data *data;
8346         int ret;
8347
8348         SUPPLICANT_DBG("");
8349
8350         if (!interface->p2p_support)
8351                 return -ENOTSUP;
8352
8353         data = dbus_malloc0(sizeof(*data));
8354         if (!data)
8355                 return -ENOMEM;
8356
8357         data->registration = true;
8358         data->interface = interface;
8359         data->service = p2p_service_params;
8360         data->callback = callback;
8361         data->user_data = user_data;
8362
8363         ret = supplicant_dbus_method_call(interface->path,
8364                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "AddService",
8365                 interface_p2p_service_params, interface_p2p_service_result,
8366                 data, interface);
8367         if (ret < 0) {
8368                 dbus_free(data);
8369                 return ret;
8370         }
8371
8372         return -EINPROGRESS;
8373 }
8374
8375 int g_supplicant_interface_p2p_del_service(GSupplicantInterface *interface,
8376                                 GSupplicantP2PServiceParams *p2p_service_params)
8377 {
8378         struct p2p_service_data *data;
8379         int ret;
8380
8381         SUPPLICANT_DBG("");
8382
8383         if (!interface->p2p_support)
8384                 return -ENOTSUP;
8385
8386         data = dbus_malloc0(sizeof(*data));
8387         if (!data)
8388                 return -ENOMEM;
8389
8390         data->interface = interface;
8391         data->service = p2p_service_params;
8392
8393         ret = supplicant_dbus_method_call(interface->path,
8394                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeleteService",
8395                 interface_p2p_service_params, interface_p2p_service_result,
8396                 data, interface);
8397         if (ret < 0) {
8398                 dbus_free(data);
8399                 return ret;
8400         }
8401
8402         return -EINPROGRESS;
8403 }
8404
8405 struct p2p_listen_data {
8406         int period;
8407         int interval;
8408 };
8409
8410 static void interface_p2p_listen_params(DBusMessageIter *iter, void *user_data)
8411 {
8412         struct p2p_listen_data *params = user_data;
8413         DBusMessageIter dict;
8414
8415         supplicant_dbus_dict_open(iter, &dict);
8416
8417         supplicant_dbus_dict_append_basic(&dict, "period",
8418                                         DBUS_TYPE_INT32, &params->period);
8419         supplicant_dbus_dict_append_basic(&dict, "interval",
8420                                         DBUS_TYPE_INT32, &params->interval);
8421         supplicant_dbus_dict_close(iter, &dict);
8422 }
8423
8424 int g_supplicant_interface_p2p_listen(GSupplicantInterface *interface,
8425                                                 int period, int interval)
8426 {
8427         struct p2p_listen_data params;
8428
8429         SUPPLICANT_DBG("");
8430
8431         if (!interface->p2p_support)
8432                 return -ENOTSUP;
8433
8434         params.period = period;
8435         params.interval = interval;
8436
8437         return supplicant_dbus_method_call(interface->path,
8438                         SUPPLICANT_INTERFACE ".Interface.P2PDevice",
8439                         "ExtendedListen", interface_p2p_listen_params,
8440                         NULL, &params, NULL);
8441 }
8442
8443 static void widi_ies_params(DBusMessageIter *iter, void *user_data)
8444 {
8445         struct p2p_service_data *data = user_data;
8446         GSupplicantP2PServiceParams *service = data->service;
8447         DBusMessageIter array;
8448
8449         SUPPLICANT_DBG("%p - %d", service->wfd_ies, service->wfd_ies_length);
8450
8451         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
8452                                         DBUS_TYPE_BYTE_AS_STRING, &array);
8453
8454         if (service->wfd_ies && service->wfd_ies_length > 0) {
8455                 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
8456                                 &service->wfd_ies, service->wfd_ies_length);
8457         }
8458
8459         dbus_message_iter_close_container(iter, &array);
8460 }
8461
8462 int g_supplicant_set_widi_ies(GSupplicantP2PServiceParams *p2p_service_params,
8463                                         GSupplicantInterfaceCallback callback,
8464                                         void *user_data)
8465 {
8466         struct p2p_service_data *data;
8467         int ret;
8468
8469         SUPPLICANT_DBG("");
8470
8471         if (!system_available)
8472                 return -EFAULT;
8473
8474         data = dbus_malloc0(sizeof(*data));
8475         if (!data)
8476                 return -ENOMEM;
8477
8478         data->service = p2p_service_params;
8479         data->callback = callback;
8480         data->user_data = user_data;
8481
8482         if (p2p_service_params->wfd_ies)
8483                 data->registration = true;
8484
8485         ret = supplicant_dbus_property_set(SUPPLICANT_PATH,
8486                                         SUPPLICANT_INTERFACE, "WFDIEs",
8487                                         DBUS_TYPE_ARRAY_AS_STRING
8488                                         DBUS_TYPE_BYTE_AS_STRING,
8489                                         widi_ies_params,
8490                                         interface_p2p_service_result,
8491                                         data, NULL);
8492         if (ret < 0 && ret != -EINPROGRESS) {
8493                 dbus_free(data);
8494                 return ret;
8495         }
8496
8497         return -EINPROGRESS;
8498 }
8499
8500
8501 static const char *g_supplicant_rule0 = "type=signal,"
8502                                         "path=" DBUS_PATH_DBUS ","
8503                                         "sender=" DBUS_SERVICE_DBUS ","
8504                                         "interface=" DBUS_INTERFACE_DBUS ","
8505                                         "member=NameOwnerChanged,"
8506                                         "arg0=" SUPPLICANT_SERVICE;
8507 static const char *g_supplicant_rule1 = "type=signal,"
8508                         "interface=" SUPPLICANT_INTERFACE;
8509 static const char *g_supplicant_rule2 = "type=signal,"
8510                         "interface=" SUPPLICANT_INTERFACE ".Interface";
8511 static const char *g_supplicant_rule3 = "type=signal,"
8512                         "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
8513 static const char *g_supplicant_rule4 = "type=signal,"
8514                         "interface=" SUPPLICANT_INTERFACE ".BSS";
8515 static const char *g_supplicant_rule5 = "type=signal,"
8516                         "interface=" SUPPLICANT_INTERFACE ".Network";
8517 #if !defined TIZEN_EXT
8518 static const char *g_supplicant_rule6 = "type=signal,"
8519                 "interface=" SUPPLICANT_INTERFACE ".Interface.P2PDevice";
8520 static const char *g_supplicant_rule7 = "type=signal,"
8521                 "interface=" SUPPLICANT_INTERFACE ".Peer";
8522 static const char *g_supplicant_rule8 = "type=signal,"
8523                 "interface=" SUPPLICANT_INTERFACE ".Group";
8524 #endif
8525 #if defined TIZEN_EXT_WIFI_MESH
8526 static const char *g_supplicant_rule9 = "type=signal,"
8527                 "interface=" SUPPLICANT_INTERFACE ".Interface.Mesh";
8528 #endif
8529
8530 static void invoke_introspect_method(void)
8531 {
8532         DBusMessage *message;
8533
8534         message = dbus_message_new_method_call(SUPPLICANT_SERVICE,
8535                                         SUPPLICANT_PATH,
8536                                         DBUS_INTERFACE_INTROSPECTABLE,
8537                                         "Introspect");
8538
8539         if (!message)
8540                 return;
8541
8542         dbus_message_set_no_reply(message, TRUE);
8543         dbus_connection_send(connection, message, NULL);
8544         dbus_message_unref(message);
8545 }
8546
8547 #if defined TIZEN_EXT
8548 void g_supplicant_set_ins_settings(GSupplicantINSPreferredFreq preferred_freq_bssid,
8549                 bool last_connected_bssid, bool assoc_reject, bool signal_bssid,
8550                 unsigned int preferred_freq_bssid_score, unsigned int last_connected_bssid_score,
8551                 unsigned int assoc_reject_score, int signal_level3_5ghz, int signal_level3_24ghz)
8552 {
8553         ins_settings.preferred_freq_bssid = preferred_freq_bssid;
8554         ins_settings.last_connected_bssid = last_connected_bssid;
8555         ins_settings.assoc_reject = assoc_reject;
8556         ins_settings.signal_bssid = signal_bssid;
8557         ins_settings.preferred_freq_bssid_score = preferred_freq_bssid_score;
8558         ins_settings.last_connected_bssid_score = last_connected_bssid_score;
8559         ins_settings.assoc_reject_score = assoc_reject_score;
8560         ins_settings.signal_level3_5ghz = signal_level3_5ghz;
8561         ins_settings.signal_level3_24ghz = signal_level3_24ghz;
8562
8563         SUPPLICANT_DBG("preferred_freq_bssid [%s]", preferred_freq_bssid ? "true" : "false");
8564         SUPPLICANT_DBG("preferred_freq_bssid_score [%d]", preferred_freq_bssid_score);
8565         SUPPLICANT_DBG("last_connected_bssid [%s]", last_connected_bssid ? "true" : "false");
8566         SUPPLICANT_DBG("last_connected_bssid_score [%d]", last_connected_bssid_score);
8567         SUPPLICANT_DBG("assoc_reject [%s]", assoc_reject ? "true" : "false");
8568         SUPPLICANT_DBG("assoc_reject_score [%d]", assoc_reject_score);
8569         SUPPLICANT_DBG("signal_bssid [%s]", signal_bssid ? "true" : "false");
8570         SUPPLICANT_DBG("signal_level3_5ghz [%d]", signal_level3_5ghz);
8571         SUPPLICANT_DBG("signal_level3_24ghz [%d]", signal_level3_24ghz);
8572 }
8573 #endif
8574
8575 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
8576 void g_supplicant_register_eap_callback(g_supplicant_eap_callback cb)
8577 {
8578         SUPPLICANT_DBG("g_supplicant_register_eap_callback %p", cb);
8579
8580         if (!callbacks_pointer) {
8581                 SUPPLICANT_DBG("callbacks_pointer is NULL");
8582                 return;
8583         }
8584
8585         callbacks_pointer->eap = cb;
8586 }
8587
8588 void g_supplicant_unregister_eap_callback(void)
8589 {
8590         SUPPLICANT_DBG("g_supplicant_unregister_eap_callback");
8591
8592         if (!callbacks_pointer) {
8593                 SUPPLICANT_DBG("callbacks_pointer is NULL");
8594                 return;
8595         }
8596
8597         callbacks_pointer->eap = NULL;
8598 }
8599 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
8600
8601 #if defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET
8602 int g_supplicant_register(GSupplicantCallbacks *callbacks)
8603 #else /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
8604 int g_supplicant_register(const GSupplicantCallbacks *callbacks)
8605 #endif /* defined TIZEN_EXT && defined TIZEN_EXT_EAP_ON_ETHERNET */
8606 {
8607         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
8608         if (!connection)
8609                 return -EIO;
8610
8611         if (!dbus_connection_add_filter(connection, g_supplicant_filter,
8612                                                 NULL, NULL)) {
8613                 dbus_connection_unref(connection);
8614                 connection = NULL;
8615                 return -EIO;
8616         }
8617 #if defined TIZEN_EXT
8618         simplified_log = connman_setting_get_bool("SimplifiedLog");
8619 #endif
8620         callbacks_pointer = callbacks;
8621         eap_methods = 0;
8622
8623         interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
8624                                                 NULL, remove_interface);
8625
8626         bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
8627                                                                 NULL, NULL);
8628         peer_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
8629                                                                 NULL, NULL);
8630         group_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
8631                                                                 NULL, NULL);
8632         pending_peer_connection = g_hash_table_new_full(g_str_hash, g_str_equal,
8633                                                                 NULL, NULL);
8634         config_file_table = g_hash_table_new_full(g_str_hash, g_str_equal,
8635                                                                 g_free, g_free);
8636
8637         supplicant_dbus_setup(connection);
8638
8639         dbus_bus_add_match(connection, g_supplicant_rule0, NULL);
8640         dbus_bus_add_match(connection, g_supplicant_rule1, NULL);
8641         dbus_bus_add_match(connection, g_supplicant_rule2, NULL);
8642         dbus_bus_add_match(connection, g_supplicant_rule3, NULL);
8643         dbus_bus_add_match(connection, g_supplicant_rule4, NULL);
8644         dbus_bus_add_match(connection, g_supplicant_rule5, NULL);
8645 #if defined TIZEN_EXT
8646         dbus_bus_add_match(connection,
8647                         "type=signal,interface=org.tizen.system.deviced.PowerOff,"
8648                         "member=ChangeState", NULL);
8649 #endif
8650 #if !defined TIZEN_EXT
8651         dbus_bus_add_match(connection, g_supplicant_rule6, NULL);
8652         dbus_bus_add_match(connection, g_supplicant_rule7, NULL);
8653         dbus_bus_add_match(connection, g_supplicant_rule8, NULL);
8654 #endif
8655 #if defined TIZEN_EXT_WIFI_MESH
8656         dbus_bus_add_match(connection, g_supplicant_rule9, NULL);
8657 #endif
8658         dbus_connection_flush(connection);
8659
8660         if (dbus_bus_name_has_owner(connection,
8661                                         SUPPLICANT_SERVICE, NULL)) {
8662                 system_available = TRUE;
8663                 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
8664                                                 SUPPLICANT_INTERFACE,
8665                                                 service_property, NULL, NULL);
8666         } else
8667                 invoke_introspect_method();
8668
8669         SUPPLICANT_DBG("supplicant dbus setup completed");
8670         return 0;
8671 }
8672
8673 static void unregister_interface_remove_params(DBusMessageIter *iter,
8674                                                 void *user_data)
8675 {
8676         const char *path = user_data;
8677
8678         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
8679                                                         &path);
8680 }
8681
8682
8683 static void unregister_remove_interface(gpointer key, gpointer value,
8684                                                 gpointer user_data)
8685 {
8686         GSupplicantInterface *interface = value;
8687
8688         supplicant_dbus_method_call(SUPPLICANT_PATH,
8689                                         SUPPLICANT_INTERFACE,
8690                                         "RemoveInterface",
8691                                         unregister_interface_remove_params,
8692                                         NULL, interface->path, NULL);
8693 }
8694
8695 void g_supplicant_unregister(const GSupplicantCallbacks *callbacks)
8696 {
8697         SUPPLICANT_DBG("");
8698
8699         if (connection) {
8700 #if defined TIZEN_EXT_WIFI_MESH
8701                 dbus_bus_remove_match(connection, g_supplicant_rule9, NULL);
8702 #endif
8703 #if !defined TIZEN_EXT
8704                 dbus_bus_remove_match(connection, g_supplicant_rule8, NULL);
8705                 dbus_bus_remove_match(connection, g_supplicant_rule7, NULL);
8706                 dbus_bus_remove_match(connection, g_supplicant_rule6, NULL);
8707 #endif
8708                 dbus_bus_remove_match(connection, g_supplicant_rule5, NULL);
8709                 dbus_bus_remove_match(connection, g_supplicant_rule4, NULL);
8710                 dbus_bus_remove_match(connection, g_supplicant_rule3, NULL);
8711                 dbus_bus_remove_match(connection, g_supplicant_rule2, NULL);
8712                 dbus_bus_remove_match(connection, g_supplicant_rule1, NULL);
8713                 dbus_bus_remove_match(connection, g_supplicant_rule0, NULL);
8714                 dbus_connection_flush(connection);
8715
8716                 dbus_connection_remove_filter(connection,
8717                                                 g_supplicant_filter, NULL);
8718         }
8719
8720         if (config_file_table) {
8721                 g_hash_table_destroy(config_file_table);
8722                 config_file_table = NULL;
8723         }
8724
8725         if (bss_mapping) {
8726                 g_hash_table_destroy(bss_mapping);
8727                 bss_mapping = NULL;
8728         }
8729
8730         if (peer_mapping) {
8731                 g_hash_table_destroy(peer_mapping);
8732                 peer_mapping = NULL;
8733         }
8734
8735         if (group_mapping) {
8736                 g_hash_table_destroy(group_mapping);
8737                 group_mapping = NULL;
8738         }
8739
8740         if (interface_table) {
8741                 g_hash_table_foreach(interface_table,
8742                                         unregister_remove_interface, NULL);
8743                 g_hash_table_destroy(interface_table);
8744                 interface_table = NULL;
8745         }
8746
8747         if (system_available)
8748                 callback_system_killed();
8749
8750         if (connection) {
8751                 dbus_connection_unref(connection);
8752                 connection = NULL;
8753         }
8754
8755         callbacks_pointer = NULL;
8756         eap_methods = 0;
8757 }
8758
8759 #ifdef TIZEN_EXT
8760 struct supplicant_mac_policy {
8761         GSupplicantMacPolicyCallback callback;
8762         dbus_uint32_t policy;
8763         const void *user_data;
8764 };
8765
8766 static void mac_policy_result(const char *error,
8767                                 DBusMessageIter *iter, void *user_data)
8768 {
8769         struct supplicant_mac_policy *data = user_data;
8770         int result = 0;
8771
8772         if (!user_data)
8773                 return;
8774
8775         if (error) {
8776                 SUPPLICANT_DBG("Mac policy setting failure %s", error);
8777                 result = -EINVAL;
8778         }
8779
8780         if (data->callback)
8781                 data->callback(result, data->policy,
8782                                         (void *) data->user_data);
8783
8784         dbus_free(data);
8785 }
8786
8787 static void mac_policy_params(DBusMessageIter *iter, void *user_data)
8788 {
8789         struct supplicant_mac_policy *data = user_data;
8790
8791         dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &(data->policy));
8792 }
8793
8794 int g_supplicant_interface_set_mac_policy(GSupplicantInterface *interface,
8795                                         GSupplicantMacPolicyCallback callback,
8796                                                         unsigned int policy,
8797                                                         void *user_data)
8798 {
8799         struct supplicant_mac_policy *data = NULL;
8800         int ret;
8801
8802         if (!system_available)
8803                 return -EFAULT;
8804
8805         if (!interface)
8806                 return -EINVAL;
8807
8808         data = dbus_malloc0(sizeof(*data));
8809         if (!data)
8810                 return -ENOMEM;
8811
8812         data->callback = callback;
8813         data->policy = policy;
8814         data->user_data = user_data;
8815
8816         ret =  supplicant_dbus_property_set(interface->path,
8817                                 SUPPLICANT_INTERFACE ".Interface",
8818                                 "MacAddr", DBUS_TYPE_INT32_AS_STRING,
8819                                 mac_policy_params, mac_policy_result, data, NULL);
8820         if (ret < 0) {
8821                 SUPPLICANT_DBG("Unable to set MacAddr configuration");
8822                 dbus_free(data);
8823         }
8824
8825         return ret;
8826 }
8827
8828 int g_supplicant_interface_set_preassoc_mac_policy(GSupplicantInterface *interface,
8829                                         GSupplicantMacPolicyCallback callback,
8830                                                         unsigned int policy,
8831                                                         void *user_data)
8832 {
8833         struct supplicant_mac_policy *data;
8834         int ret;
8835
8836         if (!system_available)
8837                 return -EFAULT;
8838
8839         if (!interface)
8840                 return -EINVAL;
8841
8842         data = dbus_malloc0(sizeof(*data));
8843         if (!data)
8844                 return -ENOMEM;
8845
8846         data->callback = callback;
8847         data->policy = policy;
8848         data->user_data = user_data;
8849
8850         ret =  supplicant_dbus_property_set(interface->path,
8851                                 SUPPLICANT_INTERFACE ".Interface",
8852                                 "PreassocMacAddr", DBUS_TYPE_INT32_AS_STRING,
8853                                 mac_policy_params, mac_policy_result, data, NULL);
8854         if (ret < 0) {
8855                 SUPPLICANT_DBG("Unable to set PreassocMacAddr configuration");
8856                 dbus_free(data);
8857         }
8858
8859         return ret;
8860 }
8861
8862 struct supplicant_random_mac_lifetime {
8863         GSupplicantRandomMaclifetimeCallback callback;
8864         dbus_uint32_t lifetime;
8865         const void *user_data;
8866 };
8867
8868 static void random_mac_lifetime_result(const char *error,
8869                                 DBusMessageIter *iter, void *user_data)
8870 {
8871         struct supplicant_random_mac_lifetime *data = user_data;
8872         int result = 0;
8873
8874         if (!user_data)
8875                 return;
8876
8877         if (error) {
8878                 SUPPLICANT_DBG("Random Mac lifetime setting failure %s", error);
8879                 result = -EINVAL;
8880         }
8881
8882         if (data->callback)
8883                 data->callback(result, data->lifetime,
8884                                         (void *) data->user_data);
8885
8886         dbus_free(data);
8887 }
8888
8889 static void random_mac_lifetime_params(DBusMessageIter *iter, void *user_data)
8890 {
8891         struct supplicant_random_mac_lifetime *data = user_data;
8892
8893         dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &(data->lifetime));
8894 }
8895
8896 int g_supplicant_interface_set_random_mac_lifetime(GSupplicantInterface *interface,
8897                                         GSupplicantRandomMaclifetimeCallback callback,
8898                                                         unsigned int lifetime,
8899                                                         void *user_data)
8900 {
8901         struct supplicant_random_mac_lifetime *data;
8902         int ret;
8903
8904         if (!system_available)
8905                 return -EFAULT;
8906
8907         if (!interface)
8908                 return -EINVAL;
8909
8910         data = dbus_malloc0(sizeof(*data));
8911         if (!data)
8912                 return -ENOMEM;
8913
8914         data->callback = callback;
8915         data->lifetime = lifetime;
8916         data->user_data = user_data;
8917
8918         ret =  supplicant_dbus_property_set(interface->path,
8919                                 SUPPLICANT_INTERFACE ".Interface",
8920                                 "RandAddrLifetime", DBUS_TYPE_UINT32_AS_STRING,
8921                                 random_mac_lifetime_params, random_mac_lifetime_result,
8922                                 data, NULL);
8923         if (ret < 0) {
8924                 SUPPLICANT_DBG("Unable to set RandAddrLifetime configuration");
8925                 dbus_free(data);
8926         }
8927
8928         return ret;
8929 }
8930 #endif