8449fb97a45ed664eb8e754f9944b4a8b7386df4
[platform/upstream/connman.git] / gsupplicant / supplicant.c
1 /*
2  *
3  *  WPA supplicant library with GLib integration
4  *
5  *  Copyright (C) 2012-2013  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <syslog.h>
32 #include <ctype.h>
33 #include <stdbool.h>
34 #include <netinet/if_ether.h>
35 #include <netinet/in.h>
36
37 #include <glib.h>
38 #include <gdbus.h>
39
40 #include "dbus.h"
41 #include "gsupplicant.h"
42
43 #define IEEE80211_CAP_ESS       0x0001
44 #define IEEE80211_CAP_IBSS      0x0002
45 #define IEEE80211_CAP_PRIVACY   0x0010
46
47 #define BSS_UNKNOWN_STRENGTH    -90
48
49 static DBusConnection *connection;
50
51 static const GSupplicantCallbacks *callbacks_pointer;
52
53 static dbus_bool_t system_available = FALSE;
54 static dbus_bool_t system_ready = FALSE;
55
56 static dbus_int32_t debug_level;
57 static dbus_bool_t debug_timestamp = FALSE;
58 static dbus_bool_t debug_showkeys = FALSE;
59
60 static const char *debug_strings[] = {
61         "msgdump", "debug", "info", "warning", "error", NULL
62 };
63
64 static unsigned int eap_methods;
65
66 struct strvalmap {
67         const char *str;
68         unsigned int val;
69 };
70
71 static struct strvalmap eap_method_map[] = {
72         { "MD5",        G_SUPPLICANT_EAP_METHOD_MD5     },
73         { "TLS",        G_SUPPLICANT_EAP_METHOD_TLS     },
74         { "MSCHAPV2",   G_SUPPLICANT_EAP_METHOD_MSCHAPV2        },
75         { "PEAP",       G_SUPPLICANT_EAP_METHOD_PEAP    },
76         { "TTLS",       G_SUPPLICANT_EAP_METHOD_TTLS    },
77         { "GTC",        G_SUPPLICANT_EAP_METHOD_GTC     },
78         { "OTP",        G_SUPPLICANT_EAP_METHOD_OTP     },
79         { "LEAP",       G_SUPPLICANT_EAP_METHOD_LEAP    },
80         { "WSC",        G_SUPPLICANT_EAP_METHOD_WSC     },
81         { }
82 };
83
84 static struct strvalmap keymgmt_map[] = {
85         { "none",               G_SUPPLICANT_KEYMGMT_NONE               },
86         { "ieee8021x",          G_SUPPLICANT_KEYMGMT_IEEE8021X  },
87         { "wpa-none",           G_SUPPLICANT_KEYMGMT_WPA_NONE   },
88         { "wpa-psk",            G_SUPPLICANT_KEYMGMT_WPA_PSK    },
89         { "wpa-psk-sha256",     G_SUPPLICANT_KEYMGMT_WPA_PSK_256        },
90         { "wpa-ft-psk",         G_SUPPLICANT_KEYMGMT_WPA_FT_PSK },
91         { "wpa-ft-eap",         G_SUPPLICANT_KEYMGMT_WPA_FT_EAP },
92         { "wpa-eap",            G_SUPPLICANT_KEYMGMT_WPA_EAP    },
93         { "wpa-eap-sha256",     G_SUPPLICANT_KEYMGMT_WPA_EAP_256        },
94         { "wps",                G_SUPPLICANT_KEYMGMT_WPS                },
95         { }
96 };
97
98 static struct strvalmap authalg_capa_map[] = {
99         { "open",       G_SUPPLICANT_CAPABILITY_AUTHALG_OPEN    },
100         { "shared",     G_SUPPLICANT_CAPABILITY_AUTHALG_SHARED  },
101         { "leap",       G_SUPPLICANT_CAPABILITY_AUTHALG_LEAP    },
102         { }
103 };
104
105 static struct strvalmap proto_capa_map[] = {
106         { "wpa",        G_SUPPLICANT_CAPABILITY_PROTO_WPA               },
107         { "rsn",        G_SUPPLICANT_CAPABILITY_PROTO_RSN               },
108         { }
109 };
110
111 static struct strvalmap group_map[] = {
112         { "wep40",      G_SUPPLICANT_GROUP_WEP40        },
113         { "wep104",     G_SUPPLICANT_GROUP_WEP104       },
114         { "tkip",       G_SUPPLICANT_GROUP_TKIP },
115         { "ccmp",       G_SUPPLICANT_GROUP_CCMP },
116         { }
117 };
118
119 static struct strvalmap pairwise_map[] = {
120         { "none",       G_SUPPLICANT_PAIRWISE_NONE      },
121         { "tkip",       G_SUPPLICANT_PAIRWISE_TKIP      },
122         { "ccmp",       G_SUPPLICANT_PAIRWISE_CCMP      },
123         { }
124 };
125
126 static struct strvalmap scan_capa_map[] = {
127         { "active",     G_SUPPLICANT_CAPABILITY_SCAN_ACTIVE     },
128         { "passive",    G_SUPPLICANT_CAPABILITY_SCAN_PASSIVE    },
129         { "ssid",       G_SUPPLICANT_CAPABILITY_SCAN_SSID               },
130         { }
131 };
132
133 static struct strvalmap mode_capa_map[] = {
134         { "infrastructure",     G_SUPPLICANT_CAPABILITY_MODE_INFRA      },
135         { "ad-hoc",             G_SUPPLICANT_CAPABILITY_MODE_IBSS       },
136         { "ap",                 G_SUPPLICANT_CAPABILITY_MODE_AP         },
137         { "p2p",                G_SUPPLICANT_CAPABILITY_MODE_P2P        },
138         { }
139 };
140
141 static GHashTable *interface_table;
142 static GHashTable *bss_mapping;
143 static GHashTable *peer_mapping;
144 static GHashTable *group_mapping;
145 static GHashTable *pending_peer_connection;
146
147 struct _GSupplicantWpsCredentials {
148         unsigned char ssid[32];
149         unsigned int ssid_len;
150         char *key;
151 };
152
153 struct _GSupplicantInterface {
154         char *path;
155         char *network_path;
156         unsigned int keymgmt_capa;
157         unsigned int authalg_capa;
158         unsigned int proto_capa;
159         unsigned int group_capa;
160         unsigned int pairwise_capa;
161         unsigned int scan_capa;
162         unsigned int mode_capa;
163         unsigned int max_scan_ssids;
164         bool p2p_support;
165         bool p2p_finding;
166         dbus_bool_t ready;
167         GSupplicantState state;
168         dbus_bool_t scanning;
169         GSupplicantInterfaceCallback scan_callback;
170         void *scan_data;
171         int apscan;
172         char *ifname;
173         char *driver;
174         char *bridge;
175         struct _GSupplicantWpsCredentials wps_cred;
176         GSupplicantWpsState wps_state;
177         GHashTable *network_table;
178         GHashTable *peer_table;
179         GHashTable *group_table;
180         GHashTable *net_mapping;
181         GHashTable *bss_mapping;
182         void *data;
183         const char *pending_peer_path;
184 };
185
186 struct g_supplicant_bss {
187         GSupplicantInterface *interface;
188         char *path;
189         unsigned char bssid[6];
190         unsigned char ssid[32];
191         unsigned int ssid_len;
192         dbus_uint16_t frequency;
193         dbus_uint32_t maxrate;
194         dbus_int16_t signal;
195         GSupplicantMode mode;
196         GSupplicantSecurity security;
197         dbus_bool_t rsn_selected;
198         unsigned int wpa_keymgmt;
199         unsigned int wpa_pairwise;
200         unsigned int wpa_group;
201         unsigned int rsn_keymgmt;
202         unsigned int rsn_pairwise;
203         unsigned int rsn_group;
204         unsigned int keymgmt;
205         dbus_bool_t privacy;
206         dbus_bool_t psk;
207         dbus_bool_t ieee8021x;
208 #if defined TIZEN_EXT
209         dbus_bool_t ft_psk;
210         dbus_bool_t ft_ieee8021x;
211 #endif
212         unsigned int wps_capabilities;
213 };
214
215 struct _GSupplicantNetwork {
216         GSupplicantInterface *interface;
217         char *path;
218         char *group;
219         char *name;
220         unsigned char ssid[32];
221         unsigned int ssid_len;
222         dbus_int16_t signal;
223         dbus_uint16_t frequency;
224         struct g_supplicant_bss *best_bss;
225         GSupplicantMode mode;
226         GSupplicantSecurity security;
227         dbus_bool_t wps;
228         unsigned int wps_capabilities;
229         GHashTable *bss_table;
230         GHashTable *config_table;
231 #if defined TIZEN_EXT
232         unsigned int isHS20AP;
233         char *eap;
234         char *identity;
235         char *phase2;
236         unsigned int keymgmt;
237 #endif
238 };
239
240 struct _GSupplicantPeer {
241         GSupplicantInterface *interface;
242         char *path;
243         unsigned char device_address[ETH_ALEN];
244         unsigned char iface_address[ETH_ALEN];
245         char *name;
246         unsigned char *widi_ies;
247         int widi_ies_length;
248         char *identifier;
249         unsigned int wps_capabilities;
250         GSList *groups;
251         const GSupplicantInterface *current_group_iface;
252         bool connection_requested;
253 };
254
255 struct _GSupplicantGroup {
256         GSupplicantInterface *interface;
257         GSupplicantInterface *orig_interface;
258         char *path;
259         int role;
260         GSList *members;
261 };
262
263 static inline void debug(const char *format, ...)
264 {
265         char str[256];
266         va_list ap;
267
268         if (!callbacks_pointer->debug)
269                 return;
270
271         va_start(ap, format);
272
273         if (vsnprintf(str, sizeof(str), format, ap) > 0)
274                 callbacks_pointer->debug(str);
275
276         va_end(ap);
277 }
278
279 #define SUPPLICANT_DBG(fmt, arg...) \
280         debug("%s:%s() " fmt, __FILE__, __FUNCTION__ , ## arg);
281
282 static GSupplicantMode string2mode(const char *mode)
283 {
284         if (!mode)
285                 return G_SUPPLICANT_MODE_UNKNOWN;
286
287         if (g_str_equal(mode, "infrastructure"))
288                 return G_SUPPLICANT_MODE_INFRA;
289         else if (g_str_equal(mode, "ad-hoc"))
290                 return G_SUPPLICANT_MODE_IBSS;
291
292         return G_SUPPLICANT_MODE_UNKNOWN;
293 }
294
295 static const char *mode2string(GSupplicantMode mode)
296 {
297         switch (mode) {
298         case G_SUPPLICANT_MODE_UNKNOWN:
299                 break;
300         case G_SUPPLICANT_MODE_INFRA:
301                 return "managed";
302         case G_SUPPLICANT_MODE_IBSS:
303                 return "adhoc";
304         case G_SUPPLICANT_MODE_MASTER:
305                 return "ap";
306         }
307
308         return NULL;
309 }
310
311 static const char *security2string(GSupplicantSecurity security)
312 {
313         switch (security) {
314         case G_SUPPLICANT_SECURITY_UNKNOWN:
315                 break;
316         case G_SUPPLICANT_SECURITY_NONE:
317                 return "none";
318         case G_SUPPLICANT_SECURITY_WEP:
319                 return "wep";
320         case G_SUPPLICANT_SECURITY_PSK:
321                 return "psk";
322         case G_SUPPLICANT_SECURITY_IEEE8021X:
323                 return "ieee8021x";
324 #if defined TIZEN_EXT
325         case G_SUPPLICANT_SECURITY_FT_PSK:
326                 return "ft_psk";
327         case G_SUPPLICANT_SECURITY_FT_IEEE8021X:
328                 return "ft_ieee8021x";
329 #endif
330         }
331
332         return NULL;
333 }
334
335 static GSupplicantState string2state(const char *state)
336 {
337         if (!state)
338                 return G_SUPPLICANT_STATE_UNKNOWN;
339
340         if (g_str_equal(state, "unknown"))
341                 return G_SUPPLICANT_STATE_UNKNOWN;
342         else if (g_str_equal(state, "interface_disabled"))
343                 return G_SUPPLICANT_STATE_DISABLED;
344         else if (g_str_equal(state, "disconnected"))
345                 return G_SUPPLICANT_STATE_DISCONNECTED;
346         else if (g_str_equal(state, "inactive"))
347                 return G_SUPPLICANT_STATE_INACTIVE;
348         else if (g_str_equal(state, "scanning"))
349                 return G_SUPPLICANT_STATE_SCANNING;
350         else if (g_str_equal(state, "authenticating"))
351                 return G_SUPPLICANT_STATE_AUTHENTICATING;
352         else if (g_str_equal(state, "associating"))
353                 return G_SUPPLICANT_STATE_ASSOCIATING;
354         else if (g_str_equal(state, "associated"))
355                 return G_SUPPLICANT_STATE_ASSOCIATED;
356         else if (g_str_equal(state, "group_handshake"))
357                 return G_SUPPLICANT_STATE_GROUP_HANDSHAKE;
358         else if (g_str_equal(state, "4way_handshake"))
359                 return G_SUPPLICANT_STATE_4WAY_HANDSHAKE;
360         else if (g_str_equal(state, "completed"))
361                 return G_SUPPLICANT_STATE_COMPLETED;
362
363         return G_SUPPLICANT_STATE_UNKNOWN;
364 }
365
366 static void callback_system_ready(void)
367 {
368         if (system_ready)
369                 return;
370
371         system_ready = TRUE;
372
373         if (!callbacks_pointer)
374                 return;
375
376         if (!callbacks_pointer->system_ready)
377                 return;
378
379         callbacks_pointer->system_ready();
380 }
381
382 static void callback_system_killed(void)
383 {
384         system_ready = FALSE;
385
386         if (!callbacks_pointer)
387                 return;
388
389         if (!callbacks_pointer->system_killed)
390                 return;
391
392         callbacks_pointer->system_killed();
393 }
394
395 static void callback_interface_added(GSupplicantInterface *interface)
396 {
397         SUPPLICANT_DBG("");
398
399         if (!callbacks_pointer)
400                 return;
401
402         if (!callbacks_pointer->interface_added)
403                 return;
404
405         callbacks_pointer->interface_added(interface);
406 }
407
408 static void callback_interface_state(GSupplicantInterface *interface)
409 {
410         if (!callbacks_pointer)
411                 return;
412
413         if (!callbacks_pointer->interface_state)
414                 return;
415
416         callbacks_pointer->interface_state(interface);
417 }
418
419 static void callback_interface_removed(GSupplicantInterface *interface)
420 {
421         if (!callbacks_pointer)
422                 return;
423
424         if (!callbacks_pointer->interface_removed)
425                 return;
426
427         callbacks_pointer->interface_removed(interface);
428 }
429
430 #if !defined TIZEN_EXT
431 static void callback_p2p_support(GSupplicantInterface *interface)
432 {
433         SUPPLICANT_DBG("");
434
435         if (!interface->p2p_support)
436                 return;
437
438         if (callbacks_pointer && callbacks_pointer->p2p_support)
439                 callbacks_pointer->p2p_support(interface);
440 }
441 #endif
442
443 static void callback_scan_started(GSupplicantInterface *interface)
444 {
445         if (!callbacks_pointer)
446                 return;
447
448         if (!callbacks_pointer->scan_started)
449                 return;
450
451         callbacks_pointer->scan_started(interface);
452 }
453
454 static void callback_scan_finished(GSupplicantInterface *interface)
455 {
456         if (!callbacks_pointer)
457                 return;
458
459         if (!callbacks_pointer->scan_finished)
460                 return;
461
462         callbacks_pointer->scan_finished(interface);
463 }
464
465 static void callback_network_added(GSupplicantNetwork *network)
466 {
467         if (!callbacks_pointer)
468                 return;
469
470         if (!callbacks_pointer->network_added)
471                 return;
472
473         callbacks_pointer->network_added(network);
474 }
475
476 static void callback_network_removed(GSupplicantNetwork *network)
477 {
478         if (!callbacks_pointer)
479                 return;
480
481         if (!callbacks_pointer->network_removed)
482                 return;
483
484         callbacks_pointer->network_removed(network);
485 }
486
487 #if defined TIZEN_EXT
488 static void callback_network_merged(GSupplicantNetwork *network)
489 {
490         if (!callbacks_pointer)
491                 return;
492
493         if (!callbacks_pointer->network_merged)
494                 return;
495
496         callbacks_pointer->network_merged(network);
497 }
498 #endif
499
500 static void callback_network_changed(GSupplicantNetwork *network,
501                                         const char *property)
502 {
503         if (!callbacks_pointer)
504                 return;
505
506         if (!callbacks_pointer->network_changed)
507                 return;
508
509         callbacks_pointer->network_changed(network, property);
510 }
511
512 static void callback_peer_found(GSupplicantPeer *peer)
513 {
514         if (!callbacks_pointer)
515                 return;
516
517         if (!callbacks_pointer->peer_found)
518                 return;
519
520         callbacks_pointer->peer_found(peer);
521 }
522
523 static void callback_peer_lost(GSupplicantPeer *peer)
524 {
525         if (!callbacks_pointer)
526                 return;
527
528         if (!callbacks_pointer->peer_lost)
529                 return;
530
531         callbacks_pointer->peer_lost(peer);
532 }
533
534 static void callback_peer_changed(GSupplicantPeer *peer,
535                                                 GSupplicantPeerState state)
536 {
537         if (!callbacks_pointer)
538                 return;
539
540         if (!callbacks_pointer->peer_changed)
541                 return;
542
543         callbacks_pointer->peer_changed(peer, state);
544 }
545
546 static void callback_peer_request(GSupplicantPeer *peer)
547 {
548         if (!callbacks_pointer)
549                 return;
550
551         if (!callbacks_pointer->peer_request)
552                 return;
553
554         peer->connection_requested = true;
555
556         callbacks_pointer->peer_request(peer);
557 }
558
559 static void remove_group(gpointer data)
560 {
561         GSupplicantGroup *group = data;
562
563         if (group->members)
564                 g_slist_free_full(group->members, g_free);
565
566         g_free(group->path);
567         g_free(group);
568 }
569
570 static void remove_interface(gpointer data)
571 {
572         GSupplicantInterface *interface = data;
573
574         g_hash_table_destroy(interface->bss_mapping);
575         g_hash_table_destroy(interface->net_mapping);
576         g_hash_table_destroy(interface->network_table);
577         g_hash_table_destroy(interface->peer_table);
578         g_hash_table_destroy(interface->group_table);
579
580         if (interface->scan_callback) {
581                 SUPPLICANT_DBG("call interface %p callback %p scanning %d",
582                                 interface, interface->scan_callback,
583                                 interface->scanning);
584
585                 interface->scan_callback(-EIO, interface, interface->scan_data);
586                 interface->scan_callback = NULL;
587                 interface->scan_data = NULL;
588
589                 if (interface->scanning) {
590                         interface->scanning = FALSE;
591                         callback_scan_finished(interface);
592                 }
593         }
594
595         callback_interface_removed(interface);
596
597         g_free(interface->wps_cred.key);
598         g_free(interface->path);
599         g_free(interface->network_path);
600 #if defined TIZEN_EXT
601         interface->network_path = NULL;
602 #endif
603         g_free(interface->ifname);
604         g_free(interface->driver);
605         g_free(interface->bridge);
606         g_free(interface);
607 }
608
609 static void remove_network(gpointer data)
610 {
611         GSupplicantNetwork *network = data;
612
613         g_hash_table_destroy(network->bss_table);
614
615         callback_network_removed(network);
616
617         g_hash_table_destroy(network->config_table);
618
619         g_free(network->path);
620         g_free(network->group);
621         g_free(network->name);
622 #if defined TIZEN_EXT
623         g_free(network->eap);
624         g_free(network->identity);
625         g_free(network->phase2);
626 #endif
627         g_free(network);
628 }
629
630 static void remove_bss(gpointer data)
631 {
632         struct g_supplicant_bss *bss = data;
633
634         g_free(bss->path);
635         g_free(bss);
636 }
637
638 static void remove_peer(gpointer data)
639 {
640         GSupplicantPeer *peer = data;
641
642         callback_peer_lost(peer);
643
644         if (peer->groups)
645                 g_slist_free_full(peer->groups, g_free);
646
647         if (peer_mapping)
648                 g_hash_table_remove(peer_mapping, peer->path);
649
650         if (pending_peer_connection)
651                 g_hash_table_remove(pending_peer_connection, peer->path);
652
653         g_free(peer->path);
654         g_free(peer->name);
655         g_free(peer->identifier);
656
657         g_free(peer);
658 }
659
660 static void debug_strvalmap(const char *label, struct strvalmap *map,
661                                                         unsigned int val)
662 {
663         int i;
664
665         for (i = 0; map[i].str; i++) {
666                 if (val & map[i].val)
667                         SUPPLICANT_DBG("%s: %s", label, map[i].str);
668         }
669 }
670
671 static void interface_capability_keymgmt(DBusMessageIter *iter, void *user_data)
672 {
673         GSupplicantInterface *interface = user_data;
674         const char *str = NULL;
675         int i;
676
677         dbus_message_iter_get_basic(iter, &str);
678         if (!str)
679                 return;
680
681         for (i = 0; keymgmt_map[i].str; i++)
682                 if (strcmp(str, keymgmt_map[i].str) == 0) {
683                         interface->keymgmt_capa |= keymgmt_map[i].val;
684                         break;
685                 }
686 }
687
688 static void interface_capability_authalg(DBusMessageIter *iter, void *user_data)
689 {
690         GSupplicantInterface *interface = user_data;
691         const char *str = NULL;
692         int i;
693
694         dbus_message_iter_get_basic(iter, &str);
695         if (!str)
696                 return;
697
698         for (i = 0; authalg_capa_map[i].str; i++)
699                 if (strcmp(str, authalg_capa_map[i].str) == 0) {
700                         interface->authalg_capa |= authalg_capa_map[i].val;
701                         break;
702                 }
703 }
704
705 static void interface_capability_proto(DBusMessageIter *iter, void *user_data)
706 {
707         GSupplicantInterface *interface = user_data;
708         const char *str = NULL;
709         int i;
710
711         dbus_message_iter_get_basic(iter, &str);
712         if (!str)
713                 return;
714
715         for (i = 0; proto_capa_map[i].str; i++)
716                 if (strcmp(str, proto_capa_map[i].str) == 0) {
717                         interface->proto_capa |= proto_capa_map[i].val;
718                         break;
719                 }
720 }
721
722 static void interface_capability_pairwise(DBusMessageIter *iter,
723                                                         void *user_data)
724 {
725         GSupplicantInterface *interface = user_data;
726         const char *str = NULL;
727         int i;
728
729         dbus_message_iter_get_basic(iter, &str);
730         if (!str)
731                 return;
732
733         for (i = 0; pairwise_map[i].str; i++)
734                 if (strcmp(str, pairwise_map[i].str) == 0) {
735                         interface->pairwise_capa |= pairwise_map[i].val;
736                         break;
737                 }
738 }
739
740 static void interface_capability_group(DBusMessageIter *iter, void *user_data)
741 {
742         GSupplicantInterface *interface = user_data;
743         const char *str = NULL;
744         int i;
745
746         dbus_message_iter_get_basic(iter, &str);
747         if (!str)
748                 return;
749
750         for (i = 0; group_map[i].str; i++)
751                 if (strcmp(str, group_map[i].str) == 0) {
752                         interface->group_capa |= group_map[i].val;
753                         break;
754                 }
755 }
756
757 static void interface_capability_scan(DBusMessageIter *iter, void *user_data)
758 {
759         GSupplicantInterface *interface = user_data;
760         const char *str = NULL;
761         int i;
762
763         dbus_message_iter_get_basic(iter, &str);
764         if (!str)
765                 return;
766
767         for (i = 0; scan_capa_map[i].str; i++)
768                 if (strcmp(str, scan_capa_map[i].str) == 0) {
769                         interface->scan_capa |= scan_capa_map[i].val;
770                         break;
771                 }
772 }
773
774 static void interface_capability_mode(DBusMessageIter *iter, void *user_data)
775 {
776         GSupplicantInterface *interface = user_data;
777         const char *str = NULL;
778         int i;
779
780         dbus_message_iter_get_basic(iter, &str);
781         if (!str)
782                 return;
783
784         for (i = 0; mode_capa_map[i].str; i++)
785                 if (strcmp(str, mode_capa_map[i].str) == 0) {
786                         interface->mode_capa |= mode_capa_map[i].val;
787                         break;
788                 }
789 }
790
791 static void interface_capability(const char *key, DBusMessageIter *iter,
792                                                         void *user_data)
793 {
794         GSupplicantInterface *interface = user_data;
795
796         if (!key)
797                 return;
798
799         if (g_strcmp0(key, "KeyMgmt") == 0)
800                 supplicant_dbus_array_foreach(iter,
801                                 interface_capability_keymgmt, interface);
802         else if (g_strcmp0(key, "AuthAlg") == 0)
803                 supplicant_dbus_array_foreach(iter,
804                                 interface_capability_authalg, interface);
805         else if (g_strcmp0(key, "Protocol") == 0)
806                 supplicant_dbus_array_foreach(iter,
807                                 interface_capability_proto, interface);
808         else if (g_strcmp0(key, "Pairwise") == 0)
809                 supplicant_dbus_array_foreach(iter,
810                                 interface_capability_pairwise, interface);
811         else if (g_strcmp0(key, "Group") == 0)
812                 supplicant_dbus_array_foreach(iter,
813                                 interface_capability_group, interface);
814         else if (g_strcmp0(key, "Scan") == 0)
815                 supplicant_dbus_array_foreach(iter,
816                                 interface_capability_scan, interface);
817         else if (g_strcmp0(key, "Modes") == 0)
818                 supplicant_dbus_array_foreach(iter,
819                                 interface_capability_mode, interface);
820         else if (g_strcmp0(key, "MaxScanSSID") == 0) {
821                 dbus_int32_t max_scan_ssid;
822
823                 dbus_message_iter_get_basic(iter, &max_scan_ssid);
824                 if (max_scan_ssid < 2)
825                         max_scan_ssid = 0;
826                 interface->max_scan_ssids = max_scan_ssid;
827
828         } else
829                 SUPPLICANT_DBG("key %s type %c",
830                                 key, dbus_message_iter_get_arg_type(iter));
831 }
832
833 static void set_apscan(DBusMessageIter *iter, void *user_data)
834 {
835         unsigned int ap_scan = *(unsigned int *)user_data;
836
837         dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &ap_scan);
838 }
839
840 int g_supplicant_interface_set_apscan(GSupplicantInterface *interface,
841                                                         unsigned int ap_scan)
842 {
843         return supplicant_dbus_property_set(interface->path,
844                         SUPPLICANT_INTERFACE ".Interface",
845                                 "ApScan", DBUS_TYPE_UINT32_AS_STRING,
846                                         set_apscan, NULL, &ap_scan, NULL);
847 }
848
849 void g_supplicant_interface_set_data(GSupplicantInterface *interface,
850                                                                 void *data)
851 {
852         if (!interface)
853                 return;
854
855         interface->data = data;
856
857         if (!data)
858                 interface->scan_callback = NULL;
859 }
860
861 void *g_supplicant_interface_get_data(GSupplicantInterface *interface)
862 {
863         if (!interface)
864                 return NULL;
865
866         return interface->data;
867 }
868
869 const char *g_supplicant_interface_get_ifname(GSupplicantInterface *interface)
870 {
871         if (!interface)
872                 return NULL;
873
874         return interface->ifname;
875 }
876
877 const char *g_supplicant_interface_get_driver(GSupplicantInterface *interface)
878 {
879         if (!interface)
880                 return NULL;
881
882         return interface->driver;
883 }
884
885 GSupplicantState g_supplicant_interface_get_state(
886                                         GSupplicantInterface *interface)
887 {
888         if (!interface)
889                 return G_SUPPLICANT_STATE_UNKNOWN;
890
891         return interface->state;
892 }
893
894 const char *g_supplicant_interface_get_wps_key(GSupplicantInterface *interface)
895 {
896         if (!interface)
897                 return NULL;
898
899         return (const char *)interface->wps_cred.key;
900 }
901
902 const void *g_supplicant_interface_get_wps_ssid(GSupplicantInterface *interface,
903                                                         unsigned int *ssid_len)
904 {
905         if (!ssid_len)
906                 return NULL;
907
908         if (!interface || interface->wps_cred.ssid_len == 0) {
909                 *ssid_len = 0;
910                 return NULL;
911         }
912
913         *ssid_len = interface->wps_cred.ssid_len;
914         return interface->wps_cred.ssid;
915 }
916
917 GSupplicantWpsState g_supplicant_interface_get_wps_state(
918                                         GSupplicantInterface *interface)
919 {
920         if (!interface)
921                 return G_SUPPLICANT_WPS_STATE_UNKNOWN;
922
923         return interface->wps_state;
924 }
925
926 unsigned int g_supplicant_interface_get_mode(GSupplicantInterface *interface)
927 {
928         if (!interface)
929                 return 0;
930
931         return interface->mode_capa;
932 }
933
934 unsigned int g_supplicant_interface_get_max_scan_ssids(
935                                 GSupplicantInterface *interface)
936 {
937         if (!interface)
938                 return 0;
939
940         if (interface->max_scan_ssids == 0)
941                 return WPAS_MAX_SCAN_SSIDS;
942
943         return interface->max_scan_ssids;
944 }
945
946 static void set_network_enabled(DBusMessageIter *iter, void *user_data)
947 {
948         dbus_bool_t enable = *(dbus_bool_t *)user_data;
949
950         dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &enable);
951 }
952
953 int g_supplicant_interface_enable_selected_network(GSupplicantInterface *interface,
954                                                         dbus_bool_t enable)
955 {
956         if (!interface)
957                 return -1;
958
959         if (!interface->network_path)
960                 return -1;
961
962         SUPPLICANT_DBG(" ");
963         return supplicant_dbus_property_set(interface->network_path,
964                                 SUPPLICANT_INTERFACE ".Network",
965                                 "Enabled", DBUS_TYPE_BOOLEAN_AS_STRING,
966                                 set_network_enabled, NULL, &enable, NULL);
967 }
968
969 dbus_bool_t g_supplicant_interface_get_ready(GSupplicantInterface *interface)
970 {
971         if (!interface)
972                 return FALSE;
973
974         return interface->ready;
975 }
976
977 GSupplicantInterface *g_supplicant_network_get_interface(
978                                         GSupplicantNetwork *network)
979 {
980         if (!network)
981                 return NULL;
982
983         return network->interface;
984 }
985
986 const char *g_supplicant_network_get_name(GSupplicantNetwork *network)
987 {
988         if (!network || !network->name)
989                 return "";
990
991         return network->name;
992 }
993
994 const char *g_supplicant_network_get_identifier(GSupplicantNetwork *network)
995 {
996         if (!network || !network->group)
997                 return "";
998
999         return network->group;
1000 }
1001
1002 const char *g_supplicant_network_get_path(GSupplicantNetwork *network)
1003 {
1004         if (!network || !network->path)
1005                 return NULL;
1006
1007         return network->path;
1008 }
1009
1010 const char *g_supplicant_network_get_mode(GSupplicantNetwork *network)
1011 {
1012         if (!network)
1013                 return G_SUPPLICANT_MODE_UNKNOWN;
1014
1015         return mode2string(network->mode);
1016 }
1017
1018 const char *g_supplicant_network_get_security(GSupplicantNetwork *network)
1019 {
1020         if (!network)
1021                 return G_SUPPLICANT_SECURITY_UNKNOWN;
1022
1023         return security2string(network->security);
1024 }
1025
1026 const void *g_supplicant_network_get_ssid(GSupplicantNetwork *network,
1027                                                 unsigned int *ssid_len)
1028 {
1029         if (!network) {
1030                 *ssid_len = 0;
1031                 return NULL;
1032         }
1033
1034         *ssid_len = network->ssid_len;
1035         return network->ssid;
1036 }
1037
1038 dbus_int16_t g_supplicant_network_get_signal(GSupplicantNetwork *network)
1039 {
1040         if (!network)
1041                 return 0;
1042
1043         return network->signal;
1044 }
1045
1046 dbus_uint16_t g_supplicant_network_get_frequency(GSupplicantNetwork *network)
1047 {
1048         if (!network)
1049                 return 0;
1050
1051         return network->frequency;
1052 }
1053
1054 dbus_bool_t g_supplicant_network_get_wps(GSupplicantNetwork *network)
1055 {
1056         if (!network)
1057                 return FALSE;
1058
1059         return network->wps;
1060 }
1061
1062 dbus_bool_t g_supplicant_network_is_wps_active(GSupplicantNetwork *network)
1063 {
1064         if (!network)
1065                 return FALSE;
1066
1067         if (network->wps_capabilities & G_SUPPLICANT_WPS_CONFIGURED)
1068                 return TRUE;
1069
1070         return FALSE;
1071 }
1072
1073 dbus_bool_t g_supplicant_network_is_wps_pbc(GSupplicantNetwork *network)
1074 {
1075         if (!network)
1076                 return FALSE;
1077
1078         if (network->wps_capabilities & G_SUPPLICANT_WPS_PBC)
1079                 return TRUE;
1080
1081         return FALSE;
1082 }
1083
1084 dbus_bool_t g_supplicant_network_is_wps_advertizing(GSupplicantNetwork *network)
1085 {
1086         if (!network)
1087                 return FALSE;
1088
1089         if (network->wps_capabilities & G_SUPPLICANT_WPS_REGISTRAR)
1090                 return TRUE;
1091
1092         return FALSE;
1093 }
1094
1095 GSupplicantInterface *g_supplicant_peer_get_interface(GSupplicantPeer *peer)
1096 {
1097         if (!peer)
1098                 return NULL;
1099
1100         return peer->interface;
1101 }
1102
1103 const char *g_supplicant_peer_get_path(GSupplicantPeer *peer)
1104 {
1105         if (!peer)
1106                 return NULL;
1107
1108         return peer->path;
1109 }
1110
1111 const char *g_supplicant_peer_get_identifier(GSupplicantPeer *peer)
1112 {
1113         if (!peer)
1114                 return NULL;
1115
1116         return peer->identifier;
1117 }
1118
1119 const void *g_supplicant_peer_get_device_address(GSupplicantPeer *peer)
1120 {
1121         if (!peer)
1122                 return NULL;
1123
1124         return peer->device_address;
1125 }
1126
1127 const void *g_supplicant_peer_get_iface_address(GSupplicantPeer *peer)
1128 {
1129         if (!peer)
1130                 return NULL;
1131
1132         return peer->iface_address;
1133 }
1134
1135 const char *g_supplicant_peer_get_name(GSupplicantPeer *peer)
1136 {
1137         if (!peer)
1138                 return NULL;
1139
1140         return peer->name;
1141 }
1142
1143 #if defined TIZEN_EXT
1144 unsigned int g_supplicant_network_is_hs20AP(GSupplicantNetwork *network)
1145 {
1146         if (!network)
1147                 return 0;
1148
1149         return network->isHS20AP;
1150 }
1151
1152 const char *g_supplicant_network_get_eap(GSupplicantNetwork *network)
1153 {
1154         if (!network || !network->eap)
1155                 return NULL;
1156
1157         return network->eap;
1158 }
1159
1160 const char *g_supplicant_network_get_identity(GSupplicantNetwork *network)
1161 {
1162         if (!network || !network->identity)
1163                 return NULL;
1164
1165         return network->identity;
1166 }
1167
1168 const char *g_supplicant_network_get_phase2(GSupplicantNetwork *network)
1169 {
1170         if (!network || !network->phase2)
1171                 return NULL;
1172
1173         return network->phase2;
1174 }
1175
1176 unsigned int g_supplicant_network_get_keymgmt(GSupplicantNetwork *network)
1177 {
1178         if (network == NULL)
1179                 return 0;
1180
1181         return network->keymgmt;
1182 }
1183 #endif
1184
1185 const unsigned char *g_supplicant_peer_get_widi_ies(GSupplicantPeer *peer,
1186                                                                 int *length)
1187 {
1188         if (!peer || !length)
1189                 return NULL;
1190
1191         *length = peer->widi_ies_length;
1192         return peer->widi_ies;
1193 }
1194
1195 bool g_supplicant_peer_is_wps_pbc(GSupplicantPeer *peer)
1196 {
1197         if (!peer)
1198                 return false;
1199
1200         if (peer->wps_capabilities & G_SUPPLICANT_WPS_PBC)
1201                 return true;
1202
1203         return false;
1204 }
1205
1206 bool g_supplicant_peer_is_wps_pin(GSupplicantPeer *peer)
1207 {
1208         if (!peer)
1209                 return false;
1210
1211         if (peer->wps_capabilities & G_SUPPLICANT_WPS_PIN)
1212                 return true;
1213
1214         return false;
1215 }
1216
1217 bool g_supplicant_peer_is_in_a_group(GSupplicantPeer *peer)
1218 {
1219         if (!peer || !peer->groups)
1220                 return false;
1221
1222         return true;
1223 }
1224
1225 GSupplicantInterface *g_supplicant_peer_get_group_interface(GSupplicantPeer *peer)
1226 {
1227         if (!peer)
1228                 return NULL;
1229
1230         return (GSupplicantInterface *) peer->current_group_iface;
1231 }
1232
1233 bool g_supplicant_peer_is_client(GSupplicantPeer *peer)
1234 {
1235         GSupplicantGroup *group;
1236         GSList *list;
1237
1238         if (!peer)
1239                 return false;
1240
1241         for (list = peer->groups; list; list = list->next) {
1242                 const char *path = list->data;
1243
1244                 group = g_hash_table_lookup(group_mapping, path);
1245                 if (!group)
1246                         continue;
1247
1248                 if (group->role != G_SUPPLICANT_GROUP_ROLE_CLIENT ||
1249                                 group->orig_interface != peer->interface)
1250                         continue;
1251
1252                 if (group->interface == peer->current_group_iface)
1253                         return true;
1254         }
1255
1256         return false;
1257 }
1258
1259 bool g_supplicant_peer_has_requested_connection(GSupplicantPeer *peer)
1260 {
1261         if (!peer)
1262                 return false;
1263
1264         return peer->connection_requested;
1265 }
1266
1267 #if defined TIZEN_EXT
1268 /*
1269  * Description: Network client requires additional wifi specific info
1270  */
1271 const unsigned char *g_supplicant_network_get_bssid(GSupplicantNetwork *network)
1272 {
1273         if (network == NULL || network->best_bss == NULL)
1274                 return NULL;
1275
1276         return (const unsigned char *)network->best_bss->bssid;
1277 }
1278
1279 unsigned int g_supplicant_network_get_maxrate(GSupplicantNetwork *network)
1280 {
1281         if (network == NULL || network->best_bss == NULL)
1282                 return 0;
1283
1284         return network->best_bss->maxrate;
1285 }
1286
1287 const char *g_supplicant_network_get_enc_mode(GSupplicantNetwork *network)
1288 {
1289         if (network == NULL || network->best_bss == NULL)
1290                 return NULL;
1291
1292         if (network->best_bss->security == G_SUPPLICANT_SECURITY_PSK ||
1293             network->best_bss->security == G_SUPPLICANT_SECURITY_IEEE8021X) {
1294                 unsigned int pairwise;
1295
1296                 pairwise = network->best_bss->rsn_pairwise |
1297                                 network->best_bss->wpa_pairwise;
1298
1299                 if ((pairwise & G_SUPPLICANT_PAIRWISE_CCMP) &&
1300                     (pairwise & G_SUPPLICANT_PAIRWISE_TKIP))
1301                         return "mixed";
1302                 else if (pairwise & G_SUPPLICANT_PAIRWISE_CCMP)
1303                         return "aes";
1304                 else if (pairwise & G_SUPPLICANT_PAIRWISE_TKIP)
1305                         return "tkip";
1306
1307         } else if (network->best_bss->security == G_SUPPLICANT_SECURITY_WEP)
1308                 return "wep";
1309         else if (network->best_bss->security == G_SUPPLICANT_SECURITY_NONE)
1310                 return "none";
1311
1312         return NULL;
1313 }
1314
1315 bool g_supplicant_network_get_rsn_selected(GSupplicantNetwork *network)
1316 {
1317         if (network == NULL || network->best_bss == NULL)
1318                 return 0;
1319
1320         if (network->best_bss->rsn_selected)
1321                 return true;
1322         else
1323                 return false;
1324 }
1325
1326 #endif
1327
1328 static void merge_network(GSupplicantNetwork *network)
1329 {
1330         GString *str;
1331         const char *ssid, *mode, *key_mgmt;
1332 #if defined TIZEN_EXT
1333         GSupplicantInterface *interface;
1334         const char *isHS20AP;
1335         const char *eap, *identity, *phase2;
1336 #endif
1337         unsigned int i, ssid_len;
1338         char *group;
1339
1340         ssid = g_hash_table_lookup(network->config_table, "ssid");
1341         mode = g_hash_table_lookup(network->config_table, "mode");
1342         key_mgmt = g_hash_table_lookup(network->config_table, "key_mgmt");
1343 #if defined TIZEN_EXT
1344         isHS20AP = g_hash_table_lookup(network->config_table, "isHS20AP");
1345         eap = g_hash_table_lookup(network->config_table, "eap");
1346         identity = g_hash_table_lookup(network->config_table, "identity");
1347         phase2 = g_hash_table_lookup(network->config_table, "phase2");
1348         interface = network->interface;
1349 #endif
1350
1351         SUPPLICANT_DBG("ssid %s mode %s", ssid, mode);
1352
1353         if (ssid)
1354                 ssid_len = strlen(ssid);
1355         else
1356                 ssid_len = 0;
1357
1358         str = g_string_sized_new((ssid_len * 2) + 24);
1359         if (!str)
1360                 return;
1361
1362         for (i = 0; i < ssid_len; i++)
1363 #if defined TIZEN_EXT
1364         {
1365                 if (ssid[i] != '"')
1366 #endif
1367                 g_string_append_printf(str, "%02x", ssid[i]);
1368 #if defined TIZEN_EXT
1369         }
1370 #endif
1371
1372         if (g_strcmp0(mode, "0") == 0)
1373                 g_string_append_printf(str, "_managed");
1374         else if (g_strcmp0(mode, "1") == 0)
1375                 g_string_append_printf(str, "_adhoc");
1376
1377         if (g_strcmp0(key_mgmt, "WPA-PSK") == 0)
1378                 g_string_append_printf(str, "_psk");
1379 #if defined TIZEN_EXT
1380         else if (g_strcmp0(key_mgmt, "WPA-EAP") == 0)
1381                 g_string_append_printf(str, "_ieee8021x");
1382         else
1383                 g_string_append_printf(str, "_none");
1384 #endif
1385
1386         group = g_string_free(str, FALSE);
1387
1388         SUPPLICANT_DBG("%s", group);
1389
1390 #if defined TIZEN_EXT
1391         if (g_strcmp0(isHS20AP, "1") == 0) {
1392                 network->isHS20AP = 1;
1393                 if (network->eap)
1394                         g_free(network->eap);
1395                 network->eap = g_strdup(eap);
1396
1397                 if (network->identity)
1398                         g_free(network->identity);
1399                 network->identity = g_strdup(identity);
1400
1401                 if (network->phase2)
1402                         g_free(network->phase2);
1403                 network->phase2 = g_strdup(phase2);
1404         } else
1405                 network->isHS20AP = 0;
1406
1407         if (interface)
1408                 interface->network_path = g_strdup(network->path);
1409
1410         network->group = g_strdup(group);
1411         callback_network_merged(network);
1412 #endif
1413
1414         g_free(group);
1415
1416         g_hash_table_destroy(network->config_table);
1417
1418         g_free(network->path);
1419         g_free(network);
1420 }
1421
1422 static void network_property(const char *key, DBusMessageIter *iter,
1423                                                         void *user_data)
1424 {
1425         GSupplicantNetwork *network = user_data;
1426
1427         if (!network->interface)
1428                 return;
1429
1430         if (!key) {
1431                 merge_network(network);
1432                 return;
1433         }
1434
1435         if (g_strcmp0(key, "Enabled") == 0) {
1436                 dbus_bool_t enabled = FALSE;
1437
1438                 dbus_message_iter_get_basic(iter, &enabled);
1439         } else if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
1440                 const char *str = NULL;
1441
1442                 dbus_message_iter_get_basic(iter, &str);
1443                 if (str) {
1444                         g_hash_table_replace(network->config_table,
1445                                                 g_strdup(key), g_strdup(str));
1446                 }
1447         } else
1448                 SUPPLICANT_DBG("key %s type %c",
1449                                 key, dbus_message_iter_get_arg_type(iter));
1450 }
1451
1452 static void interface_network_added(DBusMessageIter *iter, void *user_data)
1453 {
1454         GSupplicantInterface *interface = user_data;
1455         GSupplicantNetwork *network;
1456         const char *path = NULL;
1457
1458         SUPPLICANT_DBG("");
1459
1460         dbus_message_iter_get_basic(iter, &path);
1461         if (!path)
1462                 return;
1463
1464         if (g_strcmp0(path, "/") == 0)
1465                 return;
1466
1467         network = g_hash_table_lookup(interface->net_mapping, path);
1468         if (network)
1469                 return;
1470
1471         network = g_try_new0(GSupplicantNetwork, 1);
1472         if (!network)
1473                 return;
1474
1475         network->interface = interface;
1476         network->path = g_strdup(path);
1477
1478         network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1479                                                         g_free, g_free);
1480
1481         dbus_message_iter_next(iter);
1482         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1483                 supplicant_dbus_property_foreach(iter, network_property,
1484                                                                 network);
1485                 network_property(NULL, NULL, network);
1486                 return;
1487         }
1488
1489         supplicant_dbus_property_get_all(path,
1490                                 SUPPLICANT_INTERFACE ".Network",
1491                                         network_property, network, NULL);
1492 }
1493
1494 static void interface_network_removed(DBusMessageIter *iter, void *user_data)
1495 {
1496         GSupplicantInterface *interface = user_data;
1497         GSupplicantNetwork *network;
1498         const char *path = NULL;
1499
1500         dbus_message_iter_get_basic(iter, &path);
1501         if (!path)
1502                 return;
1503
1504         network = g_hash_table_lookup(interface->net_mapping, path);
1505         if (!network)
1506                 return;
1507
1508         g_hash_table_remove(interface->net_mapping, path);
1509 }
1510
1511 static char *create_name(unsigned char *ssid, int ssid_len)
1512 {
1513         GString *string;
1514         const gchar *remainder, *invalid;
1515         int valid_bytes, remaining_bytes;
1516
1517         if (ssid_len < 1 || ssid[0] == '\0')
1518                 return g_strdup("");
1519
1520         string = NULL;
1521         remainder = (const gchar *)ssid;
1522         remaining_bytes = ssid_len;
1523
1524         while (remaining_bytes != 0) {
1525                 if (g_utf8_validate(remainder, remaining_bytes,
1526                                         &invalid)) {
1527                         break;
1528                 }
1529
1530                 valid_bytes = invalid - remainder;
1531
1532                 if (!string)
1533                         string = g_string_sized_new(remaining_bytes);
1534
1535                 g_string_append_len(string, remainder, valid_bytes);
1536
1537                 /* append U+FFFD REPLACEMENT CHARACTER */
1538                 g_string_append(string, "\357\277\275");
1539
1540                 remaining_bytes -= valid_bytes + 1;
1541                 remainder = invalid + 1;
1542         }
1543
1544         if (!string)
1545                 return g_strndup((const gchar *)ssid, ssid_len + 1);
1546
1547         g_string_append(string, remainder);
1548
1549         return g_string_free(string, FALSE);
1550 }
1551
1552 static char *create_group(struct g_supplicant_bss *bss)
1553 {
1554         GString *str;
1555         unsigned int i;
1556         const char *mode, *security;
1557
1558         str = g_string_sized_new((bss->ssid_len * 2) + 24);
1559         if (!str)
1560                 return NULL;
1561
1562         if (bss->ssid_len > 0 && bss->ssid[0] != '\0') {
1563                 for (i = 0; i < bss->ssid_len; i++)
1564                         g_string_append_printf(str, "%02x", bss->ssid[i]);
1565         } else
1566                 g_string_append_printf(str, "hidden");
1567
1568         mode = mode2string(bss->mode);
1569         if (mode)
1570                 g_string_append_printf(str, "_%s", mode);
1571
1572         security = security2string(bss->security);
1573         if (security)
1574                 g_string_append_printf(str, "_%s", security);
1575
1576         return g_string_free(str, FALSE);
1577 }
1578
1579 static void add_or_replace_bss_to_network(struct g_supplicant_bss *bss)
1580 {
1581         GSupplicantInterface *interface = bss->interface;
1582         GSupplicantNetwork *network;
1583         char *group;
1584
1585         group = create_group(bss);
1586         SUPPLICANT_DBG("New group created: %s", group);
1587
1588         if (!group)
1589                 return;
1590
1591         network = g_hash_table_lookup(interface->network_table, group);
1592         if (network) {
1593                 g_free(group);
1594                 SUPPLICANT_DBG("Network %s already exist", network->name);
1595
1596                 goto done;
1597         }
1598
1599         network = g_try_new0(GSupplicantNetwork, 1);
1600         if (!network) {
1601                 g_free(group);
1602                 return;
1603         }
1604
1605         network->interface = interface;
1606         if (!network->path)
1607                 network->path = g_strdup(bss->path);
1608         network->group = group;
1609         network->name = create_name(bss->ssid, bss->ssid_len);
1610         network->mode = bss->mode;
1611         network->security = bss->security;
1612         network->ssid_len = bss->ssid_len;
1613         memcpy(network->ssid, bss->ssid, bss->ssid_len);
1614         network->signal = bss->signal;
1615         network->frequency = bss->frequency;
1616         network->best_bss = bss;
1617
1618 #if defined TIZEN_EXT
1619         network->keymgmt = bss->keymgmt;
1620 #endif
1621
1622         SUPPLICANT_DBG("New network %s created", network->name);
1623
1624         network->bss_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1625                                                         NULL, remove_bss);
1626
1627         network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1628                                                         g_free, g_free);
1629
1630         g_hash_table_replace(interface->network_table,
1631                                                 network->group, network);
1632
1633         callback_network_added(network);
1634
1635 done:
1636         /* We update network's WPS properties if only bss provides WPS. */
1637         if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) {
1638                 network->wps = TRUE;
1639                 network->wps_capabilities |= bss->wps_capabilities;
1640         }
1641
1642         if (bss->signal > network->signal) {
1643                 network->signal = bss->signal;
1644                 network->best_bss = bss;
1645                 callback_network_changed(network, "Signal");
1646         }
1647
1648         g_hash_table_replace(interface->bss_mapping, bss->path, network);
1649         g_hash_table_replace(network->bss_table, bss->path, bss);
1650
1651         g_hash_table_replace(bss_mapping, bss->path, interface);
1652 }
1653
1654 static void bss_rates(DBusMessageIter *iter, void *user_data)
1655 {
1656         struct g_supplicant_bss *bss = user_data;
1657         dbus_uint32_t rate = 0;
1658
1659         dbus_message_iter_get_basic(iter, &rate);
1660         if (rate == 0)
1661                 return;
1662
1663         if (rate > bss->maxrate)
1664                 bss->maxrate = rate;
1665 }
1666
1667 static void bss_keymgmt(DBusMessageIter *iter, void *user_data)
1668 {
1669         unsigned int *keymgmt = user_data;
1670         const char *str = NULL;
1671         int i;
1672
1673         dbus_message_iter_get_basic(iter, &str);
1674         if (!str)
1675                 return;
1676
1677         for (i = 0; keymgmt_map[i].str; i++)
1678                 if (strcmp(str, keymgmt_map[i].str) == 0) {
1679                         SUPPLICANT_DBG("Keymgmt: %s", str);
1680                         *keymgmt |= keymgmt_map[i].val;
1681                         break;
1682                 }
1683 }
1684
1685 static void bss_group(DBusMessageIter *iter, void *user_data)
1686 {
1687         unsigned int *group = user_data;
1688         const char *str = NULL;
1689         int i;
1690
1691         dbus_message_iter_get_basic(iter, &str);
1692         if (!str)
1693                 return;
1694
1695         for (i = 0; group_map[i].str; i++)
1696                 if (strcmp(str, group_map[i].str) == 0) {
1697                         SUPPLICANT_DBG("Group: %s", str);
1698                         *group |= group_map[i].val;
1699                         break;
1700                 }
1701 }
1702
1703 static void bss_pairwise(DBusMessageIter *iter, void *user_data)
1704 {
1705         unsigned int *pairwise = user_data;
1706         const char *str = NULL;
1707         int i;
1708
1709         dbus_message_iter_get_basic(iter, &str);
1710         if (!str)
1711                 return;
1712
1713         for (i = 0; pairwise_map[i].str; i++)
1714                 if (strcmp(str, pairwise_map[i].str) == 0) {
1715                         SUPPLICANT_DBG("Pairwise: %s", str);
1716                         *pairwise |= pairwise_map[i].val;
1717                         break;
1718                 }
1719 }
1720
1721 static void bss_wpa(const char *key, DBusMessageIter *iter,
1722                         void *user_data)
1723 {
1724         struct g_supplicant_bss *bss = user_data;
1725         unsigned int value = 0;
1726
1727         SUPPLICANT_DBG("Key: %s", key);
1728
1729         if (g_strcmp0(key, "KeyMgmt") == 0) {
1730                 supplicant_dbus_array_foreach(iter, bss_keymgmt, &value);
1731
1732                 if (bss->rsn_selected)
1733                         bss->rsn_keymgmt = value;
1734                 else
1735                         bss->wpa_keymgmt = value;
1736         } else if (g_strcmp0(key, "Group") == 0) {
1737                 supplicant_dbus_array_foreach(iter, bss_group, &value);
1738
1739                 if (bss->rsn_selected)
1740                         bss->rsn_group = value;
1741                 else
1742                         bss->wpa_group = value;
1743         } else if (g_strcmp0(key, "Pairwise") == 0) {
1744                 supplicant_dbus_array_foreach(iter, bss_pairwise, &value);
1745
1746                 if (bss->rsn_selected)
1747                         bss->rsn_pairwise = value;
1748                 else
1749                         bss->wpa_pairwise = value;
1750         }
1751 }
1752
1753 static unsigned int get_tlv(unsigned char *ie, unsigned int ie_size,
1754                                                         unsigned int type)
1755 {
1756         unsigned int len = 0;
1757
1758         while (len + 4 < ie_size) {
1759                 unsigned int hi = ie[len];
1760                 unsigned int lo = ie[len + 1];
1761                 unsigned int tmp_type = (hi << 8) + lo;
1762                 unsigned int v_len = 0;
1763
1764                 /* hi and lo are used to recreate an unsigned int
1765                  * based on 2 8bits length unsigned int. */
1766
1767                 hi = ie[len + 2];
1768                 lo = ie[len + 3];
1769                 v_len = (hi << 8) + lo;
1770
1771                 if (tmp_type == type) {
1772                         unsigned int ret_value = 0;
1773                         unsigned char *value = (unsigned char *)&ret_value;
1774
1775                         SUPPLICANT_DBG("IE: match type 0x%x", type);
1776
1777                         /* Verifying length relevance */
1778                         if (v_len > sizeof(unsigned int) ||
1779                                 len + 4 + v_len > ie_size)
1780                                 break;
1781
1782                         memcpy(value, ie + len + 4, v_len);
1783
1784                         SUPPLICANT_DBG("returning 0x%x", ret_value);
1785                         return ret_value;
1786                 }
1787
1788                 len += v_len + 4;
1789         }
1790
1791         SUPPLICANT_DBG("returning 0");
1792         return 0;
1793 }
1794
1795 static void bss_process_ies(DBusMessageIter *iter, void *user_data)
1796 {
1797         struct g_supplicant_bss *bss = user_data;
1798         const unsigned char WPS_OUI[] = { 0x00, 0x50, 0xf2, 0x04 };
1799         unsigned char *ie, *ie_end;
1800         DBusMessageIter array;
1801         unsigned int value;
1802         int ie_len;
1803
1804 #define WMM_WPA1_WPS_INFO 221
1805 #define WPS_INFO_MIN_LEN  6
1806 #define WPS_VERSION_TLV   0x104A
1807 #define WPS_STATE_TLV     0x1044
1808 #define WPS_METHODS_TLV   0x1012
1809 #define WPS_REGISTRAR_TLV 0x1041
1810 #define WPS_VERSION       0x10
1811 #define WPS_PBC           0x04
1812 #define WPS_PIN           0x00
1813 #define WPS_CONFIGURED    0x02
1814
1815         dbus_message_iter_recurse(iter, &array);
1816         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1817
1818         if (!ie || ie_len < 2)
1819                 return;
1820
1821         bss->wps_capabilities = 0;
1822         bss->keymgmt = 0;
1823
1824         for (ie_end = ie + ie_len; ie < ie_end && ie + ie[1] + 1 <= ie_end;
1825                                                         ie += ie[1] + 2) {
1826
1827                 if (ie[0] != WMM_WPA1_WPS_INFO || ie[1] < WPS_INFO_MIN_LEN ||
1828                         memcmp(ie+2, WPS_OUI, sizeof(WPS_OUI)) != 0)
1829                         continue;
1830
1831                 SUPPLICANT_DBG("IE: match WPS_OUI");
1832
1833                 value = get_tlv(&ie[6], ie[1], WPS_STATE_TLV);
1834                 if (get_tlv(&ie[6], ie[1], WPS_VERSION_TLV) == WPS_VERSION &&
1835                                                                 value != 0) {
1836                         bss->keymgmt |= G_SUPPLICANT_KEYMGMT_WPS;
1837
1838                         if (value == WPS_CONFIGURED)
1839                                 bss->wps_capabilities |=
1840                                         G_SUPPLICANT_WPS_CONFIGURED;
1841                 }
1842
1843                 value = get_tlv(&ie[6], ie[1], WPS_METHODS_TLV);
1844                 if (value != 0) {
1845                         if (GUINT16_FROM_BE(value) == WPS_PBC)
1846                                 bss->wps_capabilities |= G_SUPPLICANT_WPS_PBC;
1847                         if (GUINT16_FROM_BE(value) == WPS_PIN)
1848                                 bss->wps_capabilities |= G_SUPPLICANT_WPS_PIN;
1849                 } else
1850                         bss->wps_capabilities |=
1851                                 G_SUPPLICANT_WPS_PBC | G_SUPPLICANT_WPS_PIN;
1852
1853                 /* If the AP sends this it means it's advertizing
1854                  * as a registrar and the WPS process is launched
1855                  * on its side */
1856                 if (get_tlv(&ie[6], ie[1], WPS_REGISTRAR_TLV) != 0)
1857                         bss->wps_capabilities |= G_SUPPLICANT_WPS_REGISTRAR;
1858
1859                 SUPPLICANT_DBG("WPS Methods 0x%x", bss->wps_capabilities);
1860         }
1861 }
1862
1863 static void bss_compute_security(struct g_supplicant_bss *bss)
1864 {
1865         /*
1866          * Combining RSN and WPA keymgmt
1867          * We combine it since parsing IEs might have set something for WPS. */
1868         bss->keymgmt |= bss->rsn_keymgmt | bss->wpa_keymgmt;
1869
1870         bss->ieee8021x = FALSE;
1871         bss->psk = FALSE;
1872 #if defined TIZEN_EXT
1873         bss->ft_ieee8021x = FALSE;
1874         bss->ft_psk = FALSE;
1875 #endif
1876
1877 #if defined TIZEN_EXT
1878         if (bss->keymgmt &
1879                         (G_SUPPLICANT_KEYMGMT_WPA_EAP |
1880                                         G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
1881                 bss->ieee8021x = TRUE;
1882         else if (bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPA_FT_EAP)
1883                 bss->ft_ieee8021x = TRUE;
1884 #else
1885         if (bss->keymgmt &
1886                         (G_SUPPLICANT_KEYMGMT_WPA_EAP |
1887                                 G_SUPPLICANT_KEYMGMT_WPA_FT_EAP |
1888                                 G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
1889                 bss->ieee8021x = TRUE;
1890 #endif
1891
1892 #if defined TIZEN_EXT
1893         if (bss->keymgmt &
1894                         (G_SUPPLICANT_KEYMGMT_WPA_PSK |
1895                                         G_SUPPLICANT_KEYMGMT_WPA_PSK_256))
1896                 bss->psk = TRUE;
1897         else if (bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPA_FT_PSK)
1898                 bss->ft_psk = TRUE;
1899 #else
1900         if (bss->keymgmt &
1901                         (G_SUPPLICANT_KEYMGMT_WPA_PSK |
1902                                 G_SUPPLICANT_KEYMGMT_WPA_FT_PSK |
1903                                 G_SUPPLICANT_KEYMGMT_WPA_PSK_256))
1904                 bss->psk = TRUE;
1905 #endif
1906
1907         if (bss->ieee8021x)
1908                 bss->security = G_SUPPLICANT_SECURITY_IEEE8021X;
1909         else if (bss->psk)
1910                 bss->security = G_SUPPLICANT_SECURITY_PSK;
1911 #if defined TIZEN_EXT
1912         else if (bss->ft_psk)
1913                 bss->security = G_SUPPLICANT_SECURITY_FT_PSK;
1914         else if (bss->ft_ieee8021x == TRUE)
1915                 bss->security = G_SUPPLICANT_SECURITY_IEEE8021X;
1916 #endif
1917         else if (bss->privacy)
1918                 bss->security = G_SUPPLICANT_SECURITY_WEP;
1919         else
1920                 bss->security = G_SUPPLICANT_SECURITY_NONE;
1921 }
1922
1923
1924 static void bss_property(const char *key, DBusMessageIter *iter,
1925                                                         void *user_data)
1926 {
1927         struct g_supplicant_bss *bss = user_data;
1928
1929         if (!bss->interface)
1930                 return;
1931
1932         SUPPLICANT_DBG("key %s", key);
1933
1934         if (!key)
1935                 return;
1936
1937         if (g_strcmp0(key, "BSSID") == 0) {
1938                 DBusMessageIter array;
1939                 unsigned char *addr;
1940                 int addr_len;
1941
1942                 dbus_message_iter_recurse(iter, &array);
1943                 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1944
1945                 if (addr_len == 6)
1946                         memcpy(bss->bssid, addr, addr_len);
1947         } else if (g_strcmp0(key, "SSID") == 0) {
1948                 DBusMessageIter array;
1949                 unsigned char *ssid;
1950                 int ssid_len;
1951
1952                 dbus_message_iter_recurse(iter, &array);
1953                 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1954
1955                 if (ssid_len > 0 && ssid_len < 33) {
1956                         memcpy(bss->ssid, ssid, ssid_len);
1957                         bss->ssid_len = ssid_len;
1958                 } else {
1959                         memset(bss->ssid, 0, sizeof(bss->ssid));
1960                         bss->ssid_len = 0;
1961                 }
1962         } else if (g_strcmp0(key, "Capabilities") == 0) {
1963                 dbus_uint16_t capabilities = 0x0000;
1964
1965                 dbus_message_iter_get_basic(iter, &capabilities);
1966
1967                 if (capabilities & IEEE80211_CAP_ESS)
1968                         bss->mode = G_SUPPLICANT_MODE_INFRA;
1969                 else if (capabilities & IEEE80211_CAP_IBSS)
1970                         bss->mode = G_SUPPLICANT_MODE_IBSS;
1971
1972                 if (capabilities & IEEE80211_CAP_PRIVACY)
1973                         bss->privacy = TRUE;
1974         } else if (g_strcmp0(key, "Mode") == 0) {
1975                 const char *mode = NULL;
1976
1977                 dbus_message_iter_get_basic(iter, &mode);
1978                 bss->mode = string2mode(mode);
1979         } else if (g_strcmp0(key, "Frequency") == 0) {
1980                 dbus_uint16_t frequency = 0;
1981
1982                 dbus_message_iter_get_basic(iter, &frequency);
1983                 bss->frequency = frequency;
1984         } else if (g_strcmp0(key, "Signal") == 0) {
1985                 dbus_int16_t signal = 0;
1986
1987                 dbus_message_iter_get_basic(iter, &signal);
1988
1989                 bss->signal = signal;
1990                 if (!bss->signal)
1991                         bss->signal = BSS_UNKNOWN_STRENGTH;
1992
1993         } else if (g_strcmp0(key, "Level") == 0) {
1994                 dbus_int32_t level = 0;
1995
1996                 dbus_message_iter_get_basic(iter, &level);
1997         } else if (g_strcmp0(key, "Rates") == 0) {
1998                 supplicant_dbus_array_foreach(iter, bss_rates, bss);
1999         } else if (g_strcmp0(key, "MaxRate") == 0) {
2000                 dbus_uint32_t maxrate = 0;
2001
2002                 dbus_message_iter_get_basic(iter, &maxrate);
2003                 if (maxrate != 0)
2004                         bss->maxrate = maxrate;
2005         } else if (g_strcmp0(key, "Privacy") == 0) {
2006                 dbus_bool_t privacy = FALSE;
2007
2008                 dbus_message_iter_get_basic(iter, &privacy);
2009                 bss->privacy = privacy;
2010         } else if (g_strcmp0(key, "RSN") == 0) {
2011                 bss->rsn_selected = TRUE;
2012
2013                 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
2014         } else if (g_strcmp0(key, "WPA") == 0) {
2015                 bss->rsn_selected = FALSE;
2016
2017                 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
2018         } else if (g_strcmp0(key, "IEs") == 0)
2019                 bss_process_ies(iter, bss);
2020         else
2021                 SUPPLICANT_DBG("key %s type %c",
2022                                 key, dbus_message_iter_get_arg_type(iter));
2023 }
2024
2025 static struct g_supplicant_bss *interface_bss_added(DBusMessageIter *iter,
2026                                                         void *user_data)
2027 {
2028         GSupplicantInterface *interface = user_data;
2029         GSupplicantNetwork *network;
2030         struct g_supplicant_bss *bss;
2031         const char *path = NULL;
2032
2033         SUPPLICANT_DBG("");
2034
2035         dbus_message_iter_get_basic(iter, &path);
2036         if (!path)
2037                 return NULL;
2038
2039         if (g_strcmp0(path, "/") == 0)
2040                 return NULL;
2041
2042         SUPPLICANT_DBG("%s", path);
2043
2044         network = g_hash_table_lookup(interface->bss_mapping, path);
2045         if (network) {
2046                 bss = g_hash_table_lookup(network->bss_table, path);
2047                 if (bss)
2048                         return NULL;
2049         }
2050
2051         bss = g_try_new0(struct g_supplicant_bss, 1);
2052         if (!bss)
2053                 return NULL;
2054
2055         bss->interface = interface;
2056         bss->path = g_strdup(path);
2057         bss->signal = BSS_UNKNOWN_STRENGTH;
2058
2059         return bss;
2060 }
2061
2062 static void interface_bss_added_with_keys(DBusMessageIter *iter,
2063                                                 void *user_data)
2064 {
2065         struct g_supplicant_bss *bss;
2066
2067         SUPPLICANT_DBG("");
2068
2069         bss = interface_bss_added(iter, user_data);
2070         if (!bss)
2071                 return;
2072
2073         dbus_message_iter_next(iter);
2074
2075         if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_INVALID)
2076                 return;
2077
2078         supplicant_dbus_property_foreach(iter, bss_property, bss);
2079
2080         bss_compute_security(bss);
2081         add_or_replace_bss_to_network(bss);
2082 }
2083
2084 static void interface_bss_added_without_keys(DBusMessageIter *iter,
2085                                                 void *user_data)
2086 {
2087         struct g_supplicant_bss *bss;
2088
2089         SUPPLICANT_DBG("");
2090
2091         bss = interface_bss_added(iter, user_data);
2092         if (!bss)
2093                 return;
2094
2095         supplicant_dbus_property_get_all(bss->path,
2096                                         SUPPLICANT_INTERFACE ".BSS",
2097                                         bss_property, bss, NULL);
2098
2099         bss_compute_security(bss);
2100         add_or_replace_bss_to_network(bss);
2101 }
2102
2103 static void update_signal(gpointer key, gpointer value,
2104                                                 gpointer user_data)
2105 {
2106         struct g_supplicant_bss *bss = value;
2107         GSupplicantNetwork *network = user_data;
2108
2109         if (bss->signal > network->signal) {
2110                 network->signal = bss->signal;
2111                 network->best_bss = bss;
2112         }
2113 }
2114
2115 static void update_network_signal(GSupplicantNetwork *network)
2116 {
2117         if (g_hash_table_size(network->bss_table) <= 1 && network->best_bss)
2118                 return;
2119
2120         g_hash_table_foreach(network->bss_table,
2121                                 update_signal, network);
2122
2123         SUPPLICANT_DBG("New network signal %d", network->signal);
2124 }
2125
2126 static void interface_bss_removed(DBusMessageIter *iter, void *user_data)
2127 {
2128         GSupplicantInterface *interface = user_data;
2129         GSupplicantNetwork *network;
2130         struct g_supplicant_bss *bss = NULL;
2131         const char *path = NULL;
2132
2133         dbus_message_iter_get_basic(iter, &path);
2134         if (!path)
2135                 return;
2136
2137         network = g_hash_table_lookup(interface->bss_mapping, path);
2138         if (!network)
2139                 return;
2140
2141         bss = g_hash_table_lookup(network->bss_table, path);
2142         if (network->best_bss == bss) {
2143                 network->best_bss = NULL;
2144                 network->signal = BSS_UNKNOWN_STRENGTH;
2145         }
2146
2147         g_hash_table_remove(bss_mapping, path);
2148
2149         g_hash_table_remove(interface->bss_mapping, path);
2150         g_hash_table_remove(network->bss_table, path);
2151
2152         update_network_signal(network);
2153
2154         if (g_hash_table_size(network->bss_table) == 0)
2155                 g_hash_table_remove(interface->network_table, network->group);
2156 }
2157
2158 static void set_config_methods(DBusMessageIter *iter, void *user_data)
2159 {
2160         const char *config_methods = "push_button";
2161
2162         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
2163                                                         &config_methods);
2164 }
2165
2166 static void interface_property(const char *key, DBusMessageIter *iter,
2167                                                         void *user_data)
2168 {
2169         GSupplicantInterface *interface = user_data;
2170
2171         if (!interface)
2172                 return;
2173
2174         SUPPLICANT_DBG("%s", key);
2175
2176         if (!key) {
2177                 debug_strvalmap("KeyMgmt capability", keymgmt_map,
2178                                                 interface->keymgmt_capa);
2179                 debug_strvalmap("AuthAlg capability", authalg_capa_map,
2180                                                 interface->authalg_capa);
2181                 debug_strvalmap("Protocol capability", proto_capa_map,
2182                                                 interface->proto_capa);
2183                 debug_strvalmap("Pairwise capability", pairwise_map,
2184                                                 interface->pairwise_capa);
2185                 debug_strvalmap("Group capability", group_map,
2186                                                 interface->group_capa);
2187                 debug_strvalmap("Scan capability", scan_capa_map,
2188                                                 interface->scan_capa);
2189                 debug_strvalmap("Mode capability", mode_capa_map,
2190                                                 interface->mode_capa);
2191
2192
2193                 supplicant_dbus_property_set(interface->path,
2194                                 SUPPLICANT_INTERFACE ".Interface.WPS",
2195                                 "ConfigMethods", DBUS_TYPE_STRING_AS_STRING,
2196                                 set_config_methods, NULL, NULL, NULL);
2197
2198                 if (interface->ready)
2199                         callback_interface_added(interface);
2200
2201                 return;
2202         }
2203
2204         if (g_strcmp0(key, "Capabilities") == 0) {
2205                 supplicant_dbus_property_foreach(iter, interface_capability,
2206                                                                 interface);
2207 #if !defined TIZEN_EXT
2208                 if (interface->mode_capa & G_SUPPLICANT_CAPABILITY_MODE_P2P)
2209                         interface->p2p_support = true;
2210 #endif
2211         } else if (g_strcmp0(key, "State") == 0) {
2212                 const char *str = NULL;
2213
2214                 dbus_message_iter_get_basic(iter, &str);
2215                 if (str)
2216                         if (string2state(str) != interface->state) {
2217                                 interface->state = string2state(str);
2218                                 callback_interface_state(interface);
2219                         }
2220                 if (interface->state == G_SUPPLICANT_STATE_DISABLED)
2221                         interface->ready = FALSE;
2222                 else
2223                         interface->ready = TRUE;
2224
2225                 SUPPLICANT_DBG("state %s (%d)", str, interface->state);
2226         } else if (g_strcmp0(key, "Scanning") == 0) {
2227                 dbus_bool_t scanning = FALSE;
2228
2229                 dbus_message_iter_get_basic(iter, &scanning);
2230                 interface->scanning = scanning;
2231
2232                 if (interface->ready) {
2233                         if (interface->scanning)
2234                                 callback_scan_started(interface);
2235                         else
2236                                 callback_scan_finished(interface);
2237                 }
2238         } else if (g_strcmp0(key, "ApScan") == 0) {
2239                 int apscan = 1;
2240
2241                 dbus_message_iter_get_basic(iter, &apscan);
2242                 interface->apscan = apscan;
2243         } else if (g_strcmp0(key, "Ifname") == 0) {
2244                 const char *str = NULL;
2245
2246                 dbus_message_iter_get_basic(iter, &str);
2247                 if (str) {
2248                         g_free(interface->ifname);
2249                         interface->ifname = g_strdup(str);
2250                 }
2251         } else if (g_strcmp0(key, "Driver") == 0) {
2252                 const char *str = NULL;
2253
2254                 dbus_message_iter_get_basic(iter, &str);
2255                 if (str) {
2256                         g_free(interface->driver);
2257                         interface->driver = g_strdup(str);
2258                 }
2259         } else if (g_strcmp0(key, "BridgeIfname") == 0) {
2260                 const char *str = NULL;
2261
2262                 dbus_message_iter_get_basic(iter, &str);
2263                 if (str) {
2264                         g_free(interface->bridge);
2265                         interface->bridge = g_strdup(str);
2266                 }
2267         } else if (g_strcmp0(key, "CurrentBSS") == 0) {
2268                 interface_bss_added_without_keys(iter, interface);
2269         } else if (g_strcmp0(key, "CurrentNetwork") == 0) {
2270 #if defined TIZEN_EXT
2271                 if (interface->state != G_SUPPLICANT_STATE_COMPLETED)
2272 #endif
2273                 interface_network_added(iter, interface);
2274         } else if (g_strcmp0(key, "BSSs") == 0) {
2275                 supplicant_dbus_array_foreach(iter,
2276                                         interface_bss_added_without_keys,
2277                                         interface);
2278         } else if (g_strcmp0(key, "Blobs") == 0) {
2279                 /* Nothing */
2280         } else if (g_strcmp0(key, "Networks") == 0) {
2281                 supplicant_dbus_array_foreach(iter, interface_network_added,
2282                                                                 interface);
2283         } else
2284                 SUPPLICANT_DBG("key %s type %c",
2285                                 key, dbus_message_iter_get_arg_type(iter));
2286 }
2287
2288 static void scan_network_update(DBusMessageIter *iter, void *user_data)
2289 {
2290         GSupplicantInterface *interface = user_data;
2291         GSupplicantNetwork *network;
2292         char *path;
2293
2294         if (!iter)
2295                 return;
2296
2297         dbus_message_iter_get_basic(iter, &path);
2298
2299         if (!path)
2300                 return;
2301
2302         if (g_strcmp0(path, "/") == 0)
2303                 return;
2304
2305         /* Update the network details based on scan BSS data */
2306         network = g_hash_table_lookup(interface->bss_mapping, path);
2307         if (network)
2308                 callback_network_added(network);
2309 }
2310
2311 static void scan_bss_data(const char *key, DBusMessageIter *iter,
2312                                 void *user_data)
2313 {
2314         GSupplicantInterface *interface = user_data;
2315
2316 /*Fixed : stucking in scanning state when scan failed*/
2317 #if defined TIZEN_EXT
2318                 GSupplicantInterfaceCallback scan_callback;
2319 #endif
2320
2321         if (iter)
2322                 supplicant_dbus_array_foreach(iter, scan_network_update,
2323                                                 interface);
2324
2325 #if defined TIZEN_EXT
2326                 scan_callback = interface->scan_callback;
2327 #endif
2328
2329         if (interface->scan_callback)
2330                 interface->scan_callback(0, interface, interface->scan_data);
2331
2332 #if defined TIZEN_EXT
2333                 if (interface->scan_callback == scan_callback) {
2334 #endif
2335         interface->scan_callback = NULL;
2336         interface->scan_data = NULL;
2337 #if defined TIZEN_EXT
2338         }
2339 #endif
2340 }
2341
2342 static GSupplicantInterface *interface_alloc(const char *path)
2343 {
2344         GSupplicantInterface *interface;
2345
2346         interface = g_try_new0(GSupplicantInterface, 1);
2347         if (!interface)
2348                 return NULL;
2349
2350         interface->path = g_strdup(path);
2351
2352         interface->network_table = g_hash_table_new_full(g_str_hash,
2353                                         g_str_equal, NULL, remove_network);
2354         interface->peer_table = g_hash_table_new_full(g_str_hash,
2355                                         g_str_equal, NULL, remove_peer);
2356         interface->group_table = g_hash_table_new_full(g_str_hash,
2357                                         g_str_equal, NULL, remove_group);
2358         interface->net_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
2359                                                                 NULL, NULL);
2360         interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
2361                                                                 NULL, NULL);
2362
2363         g_hash_table_replace(interface_table, interface->path, interface);
2364
2365         return interface;
2366 }
2367
2368 static void interface_added(DBusMessageIter *iter, void *user_data)
2369 {
2370         GSupplicantInterface *interface;
2371         const char *path = NULL;
2372
2373         SUPPLICANT_DBG("");
2374
2375         dbus_message_iter_get_basic(iter, &path);
2376         if (!path)
2377                 return;
2378
2379         if (g_strcmp0(path, "/") == 0)
2380                 return;
2381
2382         interface = g_hash_table_lookup(interface_table, path);
2383         if (interface)
2384                 return;
2385
2386         interface = interface_alloc(path);
2387         if (!interface)
2388                 return;
2389
2390         dbus_message_iter_next(iter);
2391         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
2392                 supplicant_dbus_property_foreach(iter, interface_property,
2393                                                                 interface);
2394                 interface_property(NULL, NULL, interface);
2395                 return;
2396         }
2397
2398         supplicant_dbus_property_get_all(path,
2399                                         SUPPLICANT_INTERFACE ".Interface",
2400                                         interface_property, interface,
2401                                         interface);
2402 }
2403
2404 static void interface_removed(DBusMessageIter *iter, void *user_data)
2405 {
2406         const char *path = NULL;
2407         GSupplicantInterface *interface = user_data;
2408
2409         dbus_message_iter_get_basic(iter, &path);
2410         if (!path)
2411                 return;
2412
2413         interface = g_hash_table_lookup(interface_table, path);
2414         g_supplicant_interface_cancel(interface);
2415
2416         g_hash_table_remove(interface_table, path);
2417 }
2418
2419 static void eap_method(DBusMessageIter *iter, void *user_data)
2420 {
2421         const char *str = NULL;
2422         int i;
2423
2424         dbus_message_iter_get_basic(iter, &str);
2425         if (!str)
2426                 return;
2427
2428         for (i = 0; eap_method_map[i].str; i++)
2429                 if (strcmp(str, eap_method_map[i].str) == 0) {
2430                         eap_methods |= eap_method_map[i].val;
2431                         break;
2432                 }
2433 }
2434
2435 static void service_property(const char *key, DBusMessageIter *iter,
2436                                                         void *user_data)
2437 {
2438         if (!key) {
2439                 callback_system_ready();
2440                 return;
2441         }
2442
2443         if (g_strcmp0(key, "DebugLevel") == 0) {
2444                 const char *str = NULL;
2445                 int i;
2446
2447                 dbus_message_iter_get_basic(iter, &str);
2448                 for (i = 0; debug_strings[i]; i++)
2449                         if (g_strcmp0(debug_strings[i], str) == 0) {
2450                                 debug_level = i;
2451                                 break;
2452                         }
2453                 SUPPLICANT_DBG("Debug level %d", debug_level);
2454         } else if (g_strcmp0(key, "DebugTimestamp") == 0) {
2455                 dbus_message_iter_get_basic(iter, &debug_timestamp);
2456                 SUPPLICANT_DBG("Debug timestamp %u", debug_timestamp);
2457         } else if (g_strcmp0(key, "DebugShowKeys") == 0) {
2458                 dbus_message_iter_get_basic(iter, &debug_showkeys);
2459                 SUPPLICANT_DBG("Debug show keys %u", debug_showkeys);
2460         } else if (g_strcmp0(key, "Interfaces") == 0) {
2461                 supplicant_dbus_array_foreach(iter, interface_added, NULL);
2462         } else if (g_strcmp0(key, "EapMethods") == 0) {
2463                 supplicant_dbus_array_foreach(iter, eap_method, NULL);
2464                 debug_strvalmap("EAP method", eap_method_map, eap_methods);
2465         } else if (g_strcmp0(key, "Country") == 0) {
2466                 const char *country = NULL;
2467
2468                 dbus_message_iter_get_basic(iter, &country);
2469                 SUPPLICANT_DBG("Country %s", country);
2470         } else
2471                 SUPPLICANT_DBG("key %s type %c",
2472                                 key, dbus_message_iter_get_arg_type(iter));
2473 }
2474
2475 static void signal_name_owner_changed(const char *path, DBusMessageIter *iter)
2476 {
2477         const char *name = NULL, *old = NULL, *new = NULL;
2478
2479         SUPPLICANT_DBG("");
2480
2481         if (g_strcmp0(path, DBUS_PATH_DBUS) != 0)
2482                 return;
2483
2484         dbus_message_iter_get_basic(iter, &name);
2485         if (!name)
2486                 return;
2487
2488         if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0)
2489                 return;
2490
2491         dbus_message_iter_next(iter);
2492         dbus_message_iter_get_basic(iter, &old);
2493         dbus_message_iter_next(iter);
2494         dbus_message_iter_get_basic(iter, &new);
2495
2496         if (!old || !new)
2497                 return;
2498
2499         if (strlen(old) > 0 && strlen(new) == 0) {
2500                 system_available = FALSE;
2501                 g_hash_table_remove_all(bss_mapping);
2502                 g_hash_table_remove_all(peer_mapping);
2503                 g_hash_table_remove_all(group_mapping);
2504                 g_hash_table_remove_all(interface_table);
2505                 callback_system_killed();
2506         }
2507
2508         if (strlen(new) > 0 && strlen(old) == 0) {
2509                 system_available = TRUE;
2510                 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
2511                                                 SUPPLICANT_INTERFACE,
2512                                                 service_property, NULL, NULL);
2513         }
2514 }
2515
2516 static void signal_properties_changed(const char *path, DBusMessageIter *iter)
2517 {
2518         SUPPLICANT_DBG("");
2519
2520         if (g_strcmp0(path, SUPPLICANT_PATH) != 0)
2521                 return;
2522
2523         supplicant_dbus_property_foreach(iter, service_property, NULL);
2524 }
2525
2526 static void signal_interface_added(const char *path, DBusMessageIter *iter)
2527 {
2528         SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
2529
2530         if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
2531                 interface_added(iter, NULL);
2532 }
2533
2534 static void signal_interface_removed(const char *path, DBusMessageIter *iter)
2535 {
2536         SUPPLICANT_DBG("");
2537
2538         if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
2539                 interface_removed(iter, NULL);
2540 }
2541
2542 static void signal_interface_changed(const char *path, DBusMessageIter *iter)
2543 {
2544         GSupplicantInterface *interface;
2545
2546         SUPPLICANT_DBG("");
2547
2548         interface = g_hash_table_lookup(interface_table, path);
2549         if (!interface)
2550                 return;
2551
2552         supplicant_dbus_property_foreach(iter, interface_property, interface);
2553 }
2554
2555 static void signal_scan_done(const char *path, DBusMessageIter *iter)
2556 {
2557         GSupplicantInterface *interface;
2558         dbus_bool_t success = FALSE;
2559
2560         SUPPLICANT_DBG("");
2561
2562         interface = g_hash_table_lookup(interface_table, path);
2563         if (!interface)
2564                 return;
2565
2566         dbus_message_iter_get_basic(iter, &success);
2567
2568         if (interface->scanning) {
2569                 callback_scan_finished(interface);
2570                 interface->scanning = FALSE;
2571         }
2572
2573         /*
2574          * If scan is unsuccessful return -EIO else get the scanned BSSs
2575          * and update the network details accordingly
2576          */
2577         if (!success) {
2578                 if (interface->scan_callback)
2579                         interface->scan_callback(-EIO, interface,
2580                                                 interface->scan_data);
2581
2582                 interface->scan_callback = NULL;
2583                 interface->scan_data = NULL;
2584
2585                 return;
2586         }
2587
2588         supplicant_dbus_property_get(path, SUPPLICANT_INTERFACE ".Interface",
2589                                 "BSSs", scan_bss_data, interface, interface);
2590 }
2591
2592 static void signal_bss_added(const char *path, DBusMessageIter *iter)
2593 {
2594         GSupplicantInterface *interface;
2595
2596         SUPPLICANT_DBG("");
2597
2598         interface = g_hash_table_lookup(interface_table, path);
2599         if (!interface)
2600                 return;
2601
2602         interface_bss_added_with_keys(iter, interface);
2603 }
2604
2605 static void signal_bss_removed(const char *path, DBusMessageIter *iter)
2606 {
2607         GSupplicantInterface *interface;
2608
2609         SUPPLICANT_DBG("");
2610
2611         interface = g_hash_table_lookup(interface_table, path);
2612         if (!interface)
2613                 return;
2614
2615         interface_bss_removed(iter, interface);
2616 }
2617
2618 static void signal_network_added(const char *path, DBusMessageIter *iter)
2619 {
2620         GSupplicantInterface *interface;
2621
2622         SUPPLICANT_DBG("");
2623
2624         interface = g_hash_table_lookup(interface_table, path);
2625         if (!interface)
2626                 return;
2627
2628         interface_network_added(iter, interface);
2629 }
2630
2631 static void signal_network_removed(const char *path, DBusMessageIter *iter)
2632 {
2633         GSupplicantInterface *interface;
2634
2635         SUPPLICANT_DBG("");
2636
2637         interface = g_hash_table_lookup(interface_table, path);
2638         if (!interface)
2639                 return;
2640
2641         interface_network_removed(iter, interface);
2642 }
2643
2644 static void signal_bss_changed(const char *path, DBusMessageIter *iter)
2645 {
2646         GSupplicantInterface *interface;
2647         GSupplicantNetwork *network;
2648         GSupplicantSecurity old_security;
2649         struct g_supplicant_bss *bss;
2650
2651         SUPPLICANT_DBG("");
2652
2653         interface = g_hash_table_lookup(bss_mapping, path);
2654         if (!interface)
2655                 return;
2656
2657         network = g_hash_table_lookup(interface->bss_mapping, path);
2658         if (!network)
2659                 return;
2660
2661         bss = g_hash_table_lookup(network->bss_table, path);
2662         if (!bss)
2663                 return;
2664
2665         supplicant_dbus_property_foreach(iter, bss_property, bss);
2666
2667         old_security = network->security;
2668         bss_compute_security(bss);
2669
2670         if (old_security != bss->security) {
2671                 struct g_supplicant_bss *new_bss;
2672
2673                 SUPPLICANT_DBG("New network security for %s", bss->ssid);
2674
2675                 /* Security change policy:
2676                  * - we first copy the current bss into a new one with
2677                  * its own pointer (path)
2678                  * - we remove the current bss related network which will
2679                  * tell the plugin about such removal. This is done due
2680                  * to the fact that a security change means a group change
2681                  * so a complete network change.
2682                  * (current bss becomes invalid as well)
2683                  * - we add the new bss: it adds new network and tell the
2684                  * plugin about it. */
2685
2686                 new_bss = g_try_new0(struct g_supplicant_bss, 1);
2687                 if (!new_bss)
2688                         return;
2689
2690                 memcpy(new_bss, bss, sizeof(struct g_supplicant_bss));
2691                 new_bss->path = g_strdup(bss->path);
2692
2693                 g_hash_table_remove(interface->network_table, network->group);
2694
2695                 add_or_replace_bss_to_network(new_bss);
2696
2697                 return;
2698         }
2699
2700 #if defined TIZEN_EXT
2701         if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) {
2702                 network->wps = TRUE;
2703                 network->wps_capabilities |= bss->wps_capabilities;
2704         } else
2705                 network->wps = FALSE;
2706 #endif
2707
2708         if (bss->signal == network->signal)
2709 #ifndef TIZEN_EXT
2710                 return;
2711 #else
2712         {
2713                 callback_network_changed(network, "");
2714                 return;
2715         }
2716 #endif
2717
2718         /*
2719          * If the new signal is lower than the SSID signal, we need
2720          * to check for the new maximum.
2721          */
2722         if (bss->signal < network->signal) {
2723                 if (bss != network->best_bss)
2724 #ifndef TIZEN_EXT
2725                         return;
2726 #else
2727                 {
2728                         callback_network_changed(network, "");
2729                         return;
2730                 }
2731 #endif
2732                 network->signal = bss->signal;
2733                 update_network_signal(network);
2734         } else {
2735                 network->signal = bss->signal;
2736                 network->best_bss = bss;
2737         }
2738
2739         SUPPLICANT_DBG("New network signal for %s %d dBm", network->ssid,
2740                         network->signal);
2741
2742         callback_network_changed(network, "Signal");
2743 }
2744
2745 static void wps_credentials(const char *key, DBusMessageIter *iter,
2746                         void *user_data)
2747 {
2748         GSupplicantInterface *interface = user_data;
2749
2750         if (!key)
2751                 return;
2752
2753         SUPPLICANT_DBG("key %s", key);
2754
2755         if (g_strcmp0(key, "Key") == 0) {
2756                 DBusMessageIter array;
2757                 unsigned char *key_val;
2758                 int key_len;
2759
2760                 dbus_message_iter_recurse(iter, &array);
2761                 dbus_message_iter_get_fixed_array(&array, &key_val, &key_len);
2762
2763                 g_free(interface->wps_cred.key);
2764                 interface->wps_cred.key = g_try_malloc0(
2765                                                 sizeof(char) * key_len + 1);
2766
2767                 if (!interface->wps_cred.key)
2768                         return;
2769
2770                 memcpy(interface->wps_cred.key, key_val,
2771                                                 sizeof(char) * key_len);
2772
2773                 SUPPLICANT_DBG("WPS key present");
2774         } else if (g_strcmp0(key, "SSID") == 0) {
2775                 DBusMessageIter array;
2776                 unsigned char *ssid;
2777                 int ssid_len;
2778
2779                 dbus_message_iter_recurse(iter, &array);
2780                 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
2781
2782                 if (ssid_len > 0 && ssid_len < 33) {
2783                         memcpy(interface->wps_cred.ssid, ssid, ssid_len);
2784                         interface->wps_cred.ssid_len = ssid_len;
2785                 } else {
2786                         memset(interface->wps_cred.ssid, 0, 32);
2787                         interface->wps_cred.ssid_len = 0;
2788                 }
2789         }
2790 }
2791
2792 static void signal_wps_credentials(const char *path, DBusMessageIter *iter)
2793 {
2794         GSupplicantInterface *interface;
2795
2796         SUPPLICANT_DBG("");
2797
2798         interface = g_hash_table_lookup(interface_table, path);
2799         if (!interface)
2800                 return;
2801
2802         supplicant_dbus_property_foreach(iter, wps_credentials, interface);
2803 }
2804
2805 static void wps_event_args(const char *key, DBusMessageIter *iter,
2806                         void *user_data)
2807 {
2808         GSupplicantInterface *interface = user_data;
2809
2810         if (!key || !interface)
2811                 return;
2812
2813         SUPPLICANT_DBG("Arg Key %s", key);
2814 }
2815
2816 static void signal_wps_event(const char *path, DBusMessageIter *iter)
2817 {
2818         GSupplicantInterface *interface;
2819         const char *name = NULL;
2820
2821         SUPPLICANT_DBG("");
2822
2823         interface = g_hash_table_lookup(interface_table, path);
2824         if (!interface)
2825                 return;
2826
2827         dbus_message_iter_get_basic(iter, &name);
2828
2829         SUPPLICANT_DBG("Name: %s", name);
2830
2831         if (g_strcmp0(name, "success") == 0)
2832                 interface->wps_state = G_SUPPLICANT_WPS_STATE_SUCCESS;
2833         else if (g_strcmp0(name, "failed") == 0)
2834                 interface->wps_state = G_SUPPLICANT_WPS_STATE_FAIL;
2835         else
2836                 interface->wps_state = G_SUPPLICANT_WPS_STATE_UNKNOWN;
2837
2838         if (!dbus_message_iter_has_next(iter))
2839                 return;
2840
2841         dbus_message_iter_next(iter);
2842
2843         supplicant_dbus_property_foreach(iter, wps_event_args, interface);
2844 }
2845
2846 #if defined TIZEN_EXT
2847 static void signal_power_off(const char *path, DBusMessageIter *iter)
2848 {
2849         int poweroff_state = 0;
2850
2851         dbus_message_iter_get_basic(iter, &poweroff_state);
2852
2853         SUPPLICANT_DBG("poweroff_state(%d)", poweroff_state);
2854
2855         /* POWER_OFF_DIRECT 2 && POWER_OFF_RESTART 3 */
2856         if (poweroff_state != 2 && poweroff_state != 3)
2857                 return;
2858
2859         if (callbacks_pointer == NULL)
2860                 return;
2861
2862         if (callbacks_pointer->system_power_off == NULL)
2863                 return;
2864
2865         callbacks_pointer->system_power_off();
2866 }
2867 #endif
2868
2869 static void signal_station_connected(const char *path, DBusMessageIter *iter)
2870 {
2871         GSupplicantInterface *interface;
2872         const char *sta_mac = NULL;
2873
2874         SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
2875
2876         if (callbacks_pointer->add_station == NULL)
2877                 return;
2878
2879         if (g_strcmp0(path, "/") == 0)
2880                 return;
2881
2882         interface = g_hash_table_lookup(interface_table, path);
2883         if (interface == NULL)
2884                 return;
2885
2886         dbus_message_iter_get_basic(iter, &sta_mac);
2887         if (sta_mac == NULL)
2888                 return;
2889
2890         SUPPLICANT_DBG("New station %s connected", sta_mac);
2891         callbacks_pointer->add_station(sta_mac);
2892 }
2893
2894 static void signal_station_disconnected(const char *path, DBusMessageIter *iter)
2895 {
2896         GSupplicantInterface *interface;
2897         const char *sta_mac = NULL;
2898
2899         SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
2900
2901         if (callbacks_pointer->remove_station == NULL)
2902                 return;
2903
2904         if (g_strcmp0(path, "/") == 0)
2905                 return;
2906
2907         interface = g_hash_table_lookup(interface_table, path);
2908         if (interface == NULL)
2909                 return;
2910
2911         dbus_message_iter_get_basic(iter, &sta_mac);
2912         if (sta_mac == NULL)
2913                 return;
2914
2915         SUPPLICANT_DBG("Station %s disconnected", sta_mac);
2916         callbacks_pointer->remove_station(sta_mac);
2917 }
2918
2919 static void create_peer_identifier(GSupplicantPeer *peer)
2920 {
2921         const unsigned char test[ETH_ALEN] = {};
2922
2923         if (!peer)
2924                 return;
2925
2926         if (!memcmp(peer->device_address, test, ETH_ALEN)) {
2927                 peer->identifier = g_strdup(peer->name);
2928                 return;
2929         }
2930
2931         peer->identifier = g_malloc0(19);
2932         snprintf(peer->identifier, 19, "%02x%02x%02x%02x%02x%02x",
2933                                                 peer->device_address[0],
2934                                                 peer->device_address[1],
2935                                                 peer->device_address[2],
2936                                                 peer->device_address[3],
2937                                                 peer->device_address[4],
2938                                                 peer->device_address[5]);
2939 }
2940
2941 struct peer_property_data {
2942         GSupplicantPeer *peer;
2943         GSList *old_groups;
2944         bool groups_changed;
2945         bool services_changed;
2946 };
2947
2948 static void peer_groups_relation(DBusMessageIter *iter, void *user_data)
2949 {
2950         struct peer_property_data *data = user_data;
2951         GSupplicantPeer *peer = data->peer;
2952         GSupplicantGroup *group;
2953         const char *str = NULL;
2954         GSList *elem;
2955
2956         dbus_message_iter_get_basic(iter, &str);
2957         if (!str)
2958                 return;
2959
2960         group = g_hash_table_lookup(group_mapping, str);
2961         if (!group)
2962                 return;
2963
2964         elem = g_slist_find_custom(data->old_groups, str, g_str_equal);
2965         if (elem) {
2966                 data->old_groups = g_slist_remove_link(data->old_groups, elem);
2967                 peer->groups = g_slist_concat(elem, peer->groups);
2968         } else {
2969                 peer->groups = g_slist_prepend(peer->groups, g_strdup(str));
2970                 data->groups_changed = true;
2971         }
2972 }
2973
2974 static void peer_property(const char *key, DBusMessageIter *iter,
2975                                                         void *user_data)
2976 {
2977         GSupplicantPeer *pending_peer;
2978         struct peer_property_data *data = user_data;
2979         GSupplicantPeer *peer = data->peer;
2980
2981         SUPPLICANT_DBG("key: %s", key);
2982
2983         if (!peer->interface)
2984                 return;
2985
2986         if (!key) {
2987                 if (peer->name) {
2988                         create_peer_identifier(peer);
2989                         callback_peer_found(peer);
2990                         pending_peer = g_hash_table_lookup(
2991                                         pending_peer_connection, peer->path);
2992
2993                         if (pending_peer && pending_peer == peer) {
2994                                 callback_peer_request(peer);
2995                                 g_hash_table_remove(pending_peer_connection,
2996                                                 peer->path);
2997                         }
2998
2999                         dbus_free(data);
3000                 }
3001
3002                 return;
3003         }
3004
3005         if (g_strcmp0(key, "DeviceAddress") == 0) {
3006                 unsigned char *dev_addr;
3007                 DBusMessageIter array;
3008                 int len;
3009
3010                 dbus_message_iter_recurse(iter, &array);
3011                 dbus_message_iter_get_fixed_array(&array, &dev_addr, &len);
3012
3013                 if (len == ETH_ALEN)
3014                         memcpy(peer->device_address, dev_addr, len);
3015         } else if (g_strcmp0(key, "DeviceName") == 0) {
3016                 const char *str = NULL;
3017
3018                 dbus_message_iter_get_basic(iter, &str);
3019                 if (str)
3020                         peer->name = g_strdup(str);
3021         } else if (g_strcmp0(key, "config_method") == 0) {
3022                 uint16_t wps_config;
3023
3024                 dbus_message_iter_get_basic(iter, &wps_config);
3025
3026                 if (wps_config & G_SUPPLICANT_WPS_CONFIG_PBC)
3027                         peer->wps_capabilities |= G_SUPPLICANT_WPS_PBC;
3028                 if (wps_config & ~G_SUPPLICANT_WPS_CONFIG_PBC)
3029                         peer->wps_capabilities |= G_SUPPLICANT_WPS_PIN;
3030         } else if (g_strcmp0(key, "Groups") == 0) {
3031                 data->old_groups = peer->groups;
3032                 peer->groups = NULL;
3033
3034                 supplicant_dbus_array_foreach(iter,
3035                                                 peer_groups_relation, data);
3036                 if (g_slist_length(data->old_groups) > 0) {
3037                         g_slist_free_full(data->old_groups, g_free);
3038                         data->groups_changed = true;
3039                 }
3040         } else if (g_strcmp0(key, "IEs") == 0) {
3041                 DBusMessageIter array;
3042                 unsigned char *ie;
3043                 int ie_len;
3044
3045                 dbus_message_iter_recurse(iter, &array);
3046                 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
3047
3048                 if (!ie || ie_len < 2)
3049                         return;
3050
3051                 if (peer->widi_ies) {
3052                         if (memcmp(peer->widi_ies, ie, ie_len) == 0)
3053                                 return;
3054
3055                         g_free(peer->widi_ies);
3056                         peer->widi_ies_length = 0;
3057                 }
3058
3059                 peer->widi_ies = g_malloc0(ie_len * sizeof(unsigned char));
3060
3061                 memcpy(peer->widi_ies, ie, ie_len);
3062                 peer->widi_ies_length = ie_len;
3063                 data->services_changed = true;
3064         }
3065 }
3066
3067 static void signal_peer_found(const char *path, DBusMessageIter *iter)
3068 {
3069         struct peer_property_data *property_data;
3070         GSupplicantInterface *interface;
3071         const char *obj_path = NULL;
3072         GSupplicantPeer *peer;
3073
3074         SUPPLICANT_DBG("");
3075
3076         interface = g_hash_table_lookup(interface_table, path);
3077         if (!interface)
3078                 return;
3079
3080         dbus_message_iter_get_basic(iter, &obj_path);
3081         if (!obj_path || g_strcmp0(obj_path, "/") == 0)
3082                 return;
3083
3084         peer = g_hash_table_lookup(interface->peer_table, obj_path);
3085         if (peer)
3086                 return;
3087
3088         peer = g_try_new0(GSupplicantPeer, 1);
3089         if (!peer)
3090                 return;
3091
3092         peer->interface = interface;
3093         peer->path = g_strdup(obj_path);
3094         g_hash_table_insert(interface->peer_table, peer->path, peer);
3095         g_hash_table_replace(peer_mapping, peer->path, interface);
3096
3097         property_data = dbus_malloc0(sizeof(struct peer_property_data));
3098         property_data->peer = peer;
3099
3100         dbus_message_iter_next(iter);
3101         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
3102                 supplicant_dbus_property_foreach(iter, peer_property,
3103                                                         property_data);
3104                 peer_property(NULL, NULL, property_data);
3105                 return;
3106         }
3107
3108         supplicant_dbus_property_get_all(obj_path,
3109                                         SUPPLICANT_INTERFACE ".Peer",
3110                                         peer_property, property_data, NULL);
3111 }
3112
3113 static void signal_peer_lost(const char *path, DBusMessageIter *iter)
3114 {
3115         GSupplicantInterface *interface;
3116         const char *obj_path = NULL;
3117         GSupplicantPeer *peer;
3118
3119         SUPPLICANT_DBG("");
3120
3121         interface = g_hash_table_lookup(interface_table, path);
3122         if (!interface)
3123                 return;
3124
3125         dbus_message_iter_get_basic(iter, &obj_path);
3126         if (!obj_path || g_strcmp0(obj_path, "/") == 0)
3127                 return;
3128
3129         peer = g_hash_table_lookup(interface->peer_table, obj_path);
3130         if (!peer)
3131                 return;
3132
3133         g_hash_table_remove(interface->peer_table, obj_path);
3134 }
3135
3136 static void signal_peer_changed(const char *path, DBusMessageIter *iter)
3137 {
3138         struct peer_property_data *property_data;
3139         GSupplicantInterface *interface;
3140         GSupplicantPeer *peer;
3141
3142         SUPPLICANT_DBG("");
3143
3144         interface = g_hash_table_lookup(peer_mapping, path);
3145         if (!interface)
3146                 return;
3147
3148         peer = g_hash_table_lookup(interface->peer_table, path);
3149         if (!peer) {
3150                 g_hash_table_remove(peer_mapping, path);
3151                 return;
3152         }
3153
3154         property_data = dbus_malloc0(sizeof(struct peer_property_data));
3155         property_data->peer = peer;
3156
3157         supplicant_dbus_property_foreach(iter, peer_property, property_data);
3158         if (property_data->services_changed)
3159                 callback_peer_changed(peer,
3160                                         G_SUPPLICANT_PEER_SERVICES_CHANGED);
3161
3162         if (property_data->groups_changed)
3163                 callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_CHANGED);
3164
3165         dbus_free(property_data);
3166
3167         if (!g_supplicant_peer_is_in_a_group(peer))
3168                 peer->connection_requested = false;
3169 }
3170
3171 struct group_sig_data {
3172         const char *peer_obj_path;
3173         unsigned char iface_address[ETH_ALEN];
3174         const char *interface_obj_path;
3175         const char *group_obj_path;
3176         int role;
3177 };
3178
3179 static void group_sig_property(const char *key, DBusMessageIter *iter,
3180                                                         void *user_data)
3181 {
3182         struct group_sig_data *data = user_data;
3183
3184         if (!key)
3185                 return;
3186
3187         if (g_strcmp0(key, "peer_interface_addr") == 0) {
3188                 unsigned char *dev_addr;
3189                 DBusMessageIter array;
3190                 int len;
3191
3192                 dbus_message_iter_recurse(iter, &array);
3193                 dbus_message_iter_get_fixed_array(&array, &dev_addr, &len);
3194
3195                 if (len == ETH_ALEN)
3196                         memcpy(data->iface_address, dev_addr, len);
3197         } else if (g_strcmp0(key, "role") == 0) {
3198                 const char *str = NULL;
3199
3200                 dbus_message_iter_get_basic(iter, &str);
3201                 if (g_strcmp0(str, "GO") == 0)
3202                         data->role = G_SUPPLICANT_GROUP_ROLE_GO;
3203                 else
3204                         data->role = G_SUPPLICANT_GROUP_ROLE_CLIENT;
3205         } else if (g_strcmp0(key, "peer_object") == 0)
3206                 dbus_message_iter_get_basic(iter, &data->peer_obj_path);
3207         else if (g_strcmp0(key, "interface_object") == 0)
3208                 dbus_message_iter_get_basic(iter, &data->interface_obj_path);
3209         else if (g_strcmp0(key, "group_object") == 0)
3210                 dbus_message_iter_get_basic(iter, &data->group_obj_path);
3211
3212 }
3213
3214 static void signal_group_success(const char *path, DBusMessageIter *iter)
3215 {
3216         GSupplicantInterface *interface;
3217         struct group_sig_data data = {};
3218         GSupplicantPeer *peer;
3219
3220         SUPPLICANT_DBG("");
3221
3222         interface = g_hash_table_lookup(interface_table, path);
3223         if (!interface)
3224                 return;
3225
3226         supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3227         if (!data.peer_obj_path)
3228                 return;
3229
3230         peer = g_hash_table_lookup(interface->peer_table, data.peer_obj_path);
3231         if (!peer)
3232                 return;
3233
3234         memcpy(peer->iface_address, data.iface_address, ETH_ALEN);
3235         interface->pending_peer_path = peer->path;
3236 }
3237
3238 static void signal_group_failure(const char *path, DBusMessageIter *iter)
3239 {
3240         GSupplicantInterface *interface;
3241         struct group_sig_data data = {};
3242         GSupplicantPeer *peer;
3243
3244         SUPPLICANT_DBG("");
3245
3246         interface = g_hash_table_lookup(interface_table, path);
3247         if (!interface)
3248                 return;
3249
3250         supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3251         if (!data.peer_obj_path)
3252                 return;
3253
3254         peer = g_hash_table_lookup(interface->peer_table, data.peer_obj_path);
3255         if (!peer)
3256                 return;
3257
3258         callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_FAILED);
3259         peer->connection_requested = false;
3260 }
3261
3262 static void signal_group_started(const char *path, DBusMessageIter *iter)
3263 {
3264         GSupplicantInterface *interface, *g_interface;
3265         struct group_sig_data data = {};
3266         GSupplicantGroup *group;
3267         GSupplicantPeer *peer;
3268
3269         SUPPLICANT_DBG("");
3270
3271         interface = g_hash_table_lookup(interface_table, path);
3272         if (!interface)
3273                 return;
3274
3275         supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3276         if (!data.interface_obj_path || !data.group_obj_path)
3277                 return;
3278
3279         peer = g_hash_table_lookup(interface->peer_table,
3280                                                 interface->pending_peer_path);
3281         interface->pending_peer_path = NULL;
3282         if (!peer)
3283                 return;
3284
3285         g_interface = g_hash_table_lookup(interface_table,
3286                                                 data.interface_obj_path);
3287         if (!g_interface)
3288                 return;
3289
3290         group = g_hash_table_lookup(interface->group_table,
3291                                                 data.group_obj_path);
3292         if (group)
3293                 return;
3294
3295         group = g_try_new0(GSupplicantGroup, 1);
3296         if (!group)
3297                 return;
3298
3299         group->interface = g_interface;
3300         group->orig_interface = interface;
3301         group->path = g_strdup(data.group_obj_path);
3302         group->role = data.role;
3303
3304         g_hash_table_insert(interface->group_table, group->path, group);
3305         g_hash_table_replace(group_mapping, group->path, group);
3306
3307         peer->current_group_iface = g_interface;
3308         callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_STARTED);
3309 }
3310
3311 static void remove_peer_group_interface(GHashTable *group_table,
3312                                 const char* path)
3313 {
3314         GSupplicantGroup *group;
3315         GHashTableIter iter;
3316         gpointer value, key;
3317
3318         if (!group_table)
3319                 return;
3320
3321         group = g_hash_table_lookup(group_table, path);
3322
3323         if (!group || !group->orig_interface)
3324                 return;
3325
3326         g_hash_table_iter_init(&iter, group->orig_interface->peer_table);
3327
3328         while (g_hash_table_iter_next(&iter, &key, &value)) {
3329                 GSupplicantPeer *peer = value;
3330
3331                 if (peer->current_group_iface == group->interface)
3332                         peer->current_group_iface = NULL;
3333         }
3334 }
3335
3336 static void signal_group_finished(const char *path, DBusMessageIter *iter)
3337 {
3338         GSupplicantInterface *interface;
3339         struct group_sig_data data = {};
3340
3341         SUPPLICANT_DBG("");
3342
3343         interface = g_hash_table_lookup(interface_table, path);
3344         if (!interface)
3345                 return;
3346
3347         supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3348         if (!data.interface_obj_path || !data.group_obj_path)
3349                 return;
3350
3351         remove_peer_group_interface(interface->group_table, data.group_obj_path);
3352
3353         g_hash_table_remove(group_mapping, data.group_obj_path);
3354
3355         g_hash_table_remove(interface->group_table, data.group_obj_path);
3356 }
3357
3358 static void signal_group_request(const char *path, DBusMessageIter *iter)
3359 {
3360         GSupplicantInterface *interface;
3361         GSupplicantPeer *peer;
3362         const char *obj_path;
3363
3364         SUPPLICANT_DBG("");
3365
3366         interface = g_hash_table_lookup(interface_table, path);
3367         if (!interface)
3368                 return;
3369
3370         dbus_message_iter_get_basic(iter, &obj_path);
3371         if (!obj_path || !g_strcmp0(obj_path, "/"))
3372                 return;
3373
3374         peer = g_hash_table_lookup(interface->peer_table, obj_path);
3375         if (!peer)
3376                 return;
3377
3378         /*
3379          * Peer has been previously found and property set,
3380          * otherwise, defer connection to when peer property
3381          * is set.
3382          */
3383         if (peer->identifier)
3384                 callback_peer_request(peer);
3385         else
3386                 g_hash_table_replace(pending_peer_connection, peer->path, peer);
3387 }
3388
3389 static void signal_group_peer_joined(const char *path, DBusMessageIter *iter)
3390 {
3391         const char *peer_path = NULL;
3392         GSupplicantInterface *interface;
3393         GSupplicantGroup *group;
3394         GSupplicantPeer *peer;
3395
3396         SUPPLICANT_DBG("");
3397
3398         group = g_hash_table_lookup(group_mapping, path);
3399         if (!group)
3400                 return;
3401
3402         dbus_message_iter_get_basic(iter, &peer_path);
3403         if (!peer_path)
3404                 return;
3405
3406         interface = g_hash_table_lookup(peer_mapping, peer_path);
3407         if (!interface)
3408                 return;
3409
3410         peer = g_hash_table_lookup(interface->peer_table, peer_path);
3411         if (!peer)
3412                 return;
3413
3414         group->members = g_slist_prepend(group->members, g_strdup(peer_path));
3415
3416         callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_JOINED);
3417 }
3418
3419 static void signal_group_peer_disconnected(const char *path, DBusMessageIter *iter)
3420 {
3421         const char *peer_path = NULL;
3422         GSupplicantInterface *interface;
3423         GSupplicantGroup *group;
3424         GSupplicantPeer *peer;
3425         GSList *elem;
3426
3427         SUPPLICANT_DBG("");
3428
3429         group = g_hash_table_lookup(group_mapping, path);
3430         if (!group)
3431                 return;
3432
3433         dbus_message_iter_get_basic(iter, &peer_path);
3434         if (!peer_path)
3435                 return;
3436
3437         for (elem = group->members; elem; elem = elem->next) {
3438                 if (!g_strcmp0(elem->data, peer_path))
3439                         break;
3440         }
3441
3442         if (!elem)
3443                 return;
3444
3445         g_free(elem->data);
3446         group->members = g_slist_delete_link(group->members, elem);
3447
3448         interface = g_hash_table_lookup(peer_mapping, peer_path);
3449         if (!interface)
3450                 return;
3451
3452         peer = g_hash_table_lookup(interface->peer_table, peer_path);
3453         if (!peer)
3454                 return;
3455
3456         callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_DISCONNECTED);
3457         peer->connection_requested = false;
3458 }
3459
3460 static struct {
3461         const char *interface;
3462         const char *member;
3463         void (*function) (const char *path, DBusMessageIter *iter);
3464 } signal_map[] = {
3465         { DBUS_INTERFACE_DBUS,  "NameOwnerChanged",  signal_name_owner_changed },
3466
3467         { SUPPLICANT_INTERFACE, "PropertiesChanged", signal_properties_changed },
3468         { SUPPLICANT_INTERFACE, "InterfaceAdded",    signal_interface_added    },
3469         { SUPPLICANT_INTERFACE, "InterfaceCreated",  signal_interface_added    },
3470         { SUPPLICANT_INTERFACE, "InterfaceRemoved",  signal_interface_removed  },
3471
3472         { SUPPLICANT_INTERFACE ".Interface", "PropertiesChanged", signal_interface_changed },
3473         { SUPPLICANT_INTERFACE ".Interface", "ScanDone",          signal_scan_done         },
3474         { SUPPLICANT_INTERFACE ".Interface", "BSSAdded",          signal_bss_added         },
3475         { SUPPLICANT_INTERFACE ".Interface", "BSSRemoved",        signal_bss_removed       },
3476         { SUPPLICANT_INTERFACE ".Interface", "NetworkAdded",      signal_network_added     },
3477         { SUPPLICANT_INTERFACE ".Interface", "NetworkRemoved",    signal_network_removed   },
3478
3479         { SUPPLICANT_INTERFACE ".BSS", "PropertiesChanged", signal_bss_changed   },
3480
3481         { SUPPLICANT_INTERFACE ".Interface.WPS", "Credentials", signal_wps_credentials },
3482         { SUPPLICANT_INTERFACE ".Interface.WPS", "Event",       signal_wps_event       },
3483 #if defined TIZEN_EXT
3484         { "org.tizen.system.deviced.PowerOff", "ChangeState", signal_power_off },
3485 #endif
3486
3487         { SUPPLICANT_INTERFACE".Interface", "StaAuthorized", signal_station_connected      },
3488         { SUPPLICANT_INTERFACE".Interface", "StaDeauthorized", signal_station_disconnected },
3489
3490         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeviceFound", signal_peer_found },
3491         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeviceLost",  signal_peer_lost  },
3492
3493         { SUPPLICANT_INTERFACE ".Peer", "PropertiesChanged", signal_peer_changed },
3494
3495         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationSuccess", signal_group_success },
3496         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationFailure", signal_group_failure },
3497         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GroupStarted", signal_group_started },
3498         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GroupFinished", signal_group_finished },
3499         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationRequest", signal_group_request },
3500
3501         { SUPPLICANT_INTERFACE ".Group", "PeerJoined", signal_group_peer_joined },
3502         { SUPPLICANT_INTERFACE ".Group", "PeerDisconnected", signal_group_peer_disconnected },
3503
3504         { }
3505 };
3506
3507 static DBusHandlerResult g_supplicant_filter(DBusConnection *conn,
3508                                         DBusMessage *message, void *data)
3509 {
3510         DBusMessageIter iter;
3511         const char *path;
3512         int i;
3513
3514         path = dbus_message_get_path(message);
3515         if (!path)
3516                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3517
3518         if (!dbus_message_iter_init(message, &iter))
3519                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3520
3521         for (i = 0; signal_map[i].interface; i++) {
3522                 if (!dbus_message_has_interface(message, signal_map[i].interface))
3523                         continue;
3524
3525                 if (!dbus_message_has_member(message, signal_map[i].member))
3526                         continue;
3527
3528                 signal_map[i].function(path, &iter);
3529                 break;
3530         }
3531
3532         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3533 }
3534
3535 void g_supplicant_interface_cancel(GSupplicantInterface *interface)
3536 {
3537         SUPPLICANT_DBG("Cancelling any pending DBus calls");
3538         supplicant_dbus_method_call_cancel_all(interface);
3539         supplicant_dbus_property_call_cancel_all(interface);
3540 }
3541
3542 struct supplicant_regdom {
3543         GSupplicantCountryCallback callback;
3544         const char *alpha2;
3545         const void *user_data;
3546 };
3547
3548 static void country_result(const char *error,
3549                                 DBusMessageIter *iter, void *user_data)
3550 {
3551         struct supplicant_regdom *regdom = user_data;
3552         int result = 0;
3553
3554         SUPPLICANT_DBG("Country setting result");
3555
3556         if (!user_data)
3557                 return;
3558
3559         if (error) {
3560                 SUPPLICANT_DBG("Country setting failure %s", error);
3561                 result = -EINVAL;
3562         }
3563
3564         if (regdom->callback)
3565                 regdom->callback(result, regdom->alpha2,
3566                                         (void *) regdom->user_data);
3567
3568         g_free(regdom);
3569 }
3570
3571 static void country_params(DBusMessageIter *iter, void *user_data)
3572 {
3573         struct supplicant_regdom *regdom = user_data;
3574
3575         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
3576                                                         &regdom->alpha2);
3577 }
3578
3579 int g_supplicant_set_country(const char *alpha2,
3580                                 GSupplicantCountryCallback callback,
3581                                         const void *user_data)
3582 {
3583         struct supplicant_regdom *regdom;
3584
3585         SUPPLICANT_DBG("Country setting %s", alpha2);
3586
3587         if (!system_available)
3588                 return -EFAULT;
3589
3590         regdom = dbus_malloc0(sizeof(*regdom));
3591         if (!regdom)
3592                 return -ENOMEM;
3593
3594         regdom->callback = callback;
3595         regdom->alpha2 = alpha2;
3596         regdom->user_data = user_data;
3597
3598         return supplicant_dbus_property_set(SUPPLICANT_PATH, SUPPLICANT_INTERFACE,
3599                                         "Country", DBUS_TYPE_STRING_AS_STRING,
3600                                         country_params, country_result,
3601                                         regdom, NULL);
3602 }
3603
3604 int g_supplicant_interface_set_country(GSupplicantInterface *interface,
3605                                         GSupplicantCountryCallback callback,
3606                                                         const char *alpha2,
3607                                                         void *user_data)
3608 {
3609         struct supplicant_regdom *regdom;
3610
3611         regdom = dbus_malloc0(sizeof(*regdom));
3612         if (!regdom)
3613                 return -ENOMEM;
3614
3615         regdom->callback = callback;
3616         regdom->alpha2 = alpha2;
3617         regdom->user_data = user_data;
3618
3619         return supplicant_dbus_property_set(interface->path,
3620                                 SUPPLICANT_INTERFACE ".Interface",
3621                                 "Country", DBUS_TYPE_STRING_AS_STRING,
3622                                 country_params, country_result,
3623                                         regdom, NULL);
3624 }
3625
3626 bool g_supplicant_interface_has_p2p(GSupplicantInterface *interface)
3627 {
3628         if (!interface)
3629                 return false;
3630
3631         return interface->p2p_support;
3632 }
3633
3634 struct supplicant_p2p_dev_config {
3635         char *device_name;
3636         char *dev_type;
3637 };
3638
3639 static void p2p_device_config_result(const char *error,
3640                                         DBusMessageIter *iter, void *user_data)
3641 {
3642         struct supplicant_p2p_dev_config *config = user_data;
3643
3644         if (error)
3645                 SUPPLICANT_DBG("Unable to set P2P Device configuration: %s",
3646                                                                         error);
3647
3648         g_free(config->device_name);
3649         g_free(config->dev_type);
3650         dbus_free(config);
3651 }
3652
3653 static int dev_type_str2bin(const char *type, unsigned char dev_type[8])
3654 {
3655         int length, pos, end;
3656         char b[3] = {};
3657         char *e = NULL;
3658
3659         end = strlen(type);
3660         for (length = pos = 0; type[pos] != '\0' && length < 8; length++) {
3661                 if (pos+2 > end)
3662                         return 0;
3663
3664                 b[0] = type[pos];
3665                 b[1] = type[pos+1];
3666
3667                 dev_type[length] = strtol(b, &e, 16);
3668                 if (e && *e != '\0')
3669                         return 0;
3670
3671                 pos += 2;
3672         }
3673
3674         return 8;
3675 }
3676
3677 static void p2p_device_config_params(DBusMessageIter *iter, void *user_data)
3678 {
3679         struct supplicant_p2p_dev_config *config = user_data;
3680         DBusMessageIter dict;
3681
3682         supplicant_dbus_dict_open(iter, &dict);
3683
3684         supplicant_dbus_dict_append_basic(&dict, "DeviceName",
3685                                 DBUS_TYPE_STRING, &config->device_name);
3686
3687         if (config->dev_type) {
3688                 unsigned char dev_type[8] = {}, *type;
3689                 int len;
3690
3691                 len = dev_type_str2bin(config->dev_type, dev_type);
3692                 if (len) {
3693                         type = dev_type;
3694                         supplicant_dbus_dict_append_fixed_array(&dict,
3695                                         "PrimaryDeviceType",
3696                                         DBUS_TYPE_BYTE, &type, len);
3697                 }
3698         }
3699
3700         supplicant_dbus_dict_close(iter, &dict);
3701 }
3702
3703 int g_supplicant_interface_set_p2p_device_config(GSupplicantInterface *interface,
3704                                         const char *device_name,
3705                                         const char *primary_dev_type)
3706 {
3707         struct supplicant_p2p_dev_config *config;
3708         int ret;
3709
3710         SUPPLICANT_DBG("P2P Device settings %s/%s",
3711                                         device_name, primary_dev_type);
3712
3713         config = dbus_malloc0(sizeof(*config));
3714         if (!config)
3715                 return -ENOMEM;
3716
3717         config->device_name = g_strdup(device_name);
3718         config->dev_type = g_strdup(primary_dev_type);
3719
3720         ret = supplicant_dbus_property_set(interface->path,
3721                                 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
3722                                 "P2PDeviceConfig",
3723                                 DBUS_TYPE_ARRAY_AS_STRING
3724                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
3725                                 DBUS_TYPE_STRING_AS_STRING
3726                                 DBUS_TYPE_VARIANT_AS_STRING
3727                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
3728                                 p2p_device_config_params,
3729                                 p2p_device_config_result, config, NULL);
3730         if (ret < 0) {
3731                 g_free(config->device_name);
3732                 g_free(config->dev_type);
3733                 dbus_free(config);
3734                 SUPPLICANT_DBG("Unable to set P2P Device configuration");
3735         }
3736
3737         return ret;
3738 }
3739
3740 static gboolean peer_lookup_by_identifier(gpointer key, gpointer value,
3741                                                         gpointer user_data)
3742 {
3743         const GSupplicantPeer *peer = value;
3744         const char *identifier = user_data;
3745
3746         if (!g_strcmp0(identifier, peer->identifier))
3747                 return TRUE;
3748
3749         return FALSE;
3750 }
3751
3752 GSupplicantPeer *g_supplicant_interface_peer_lookup(GSupplicantInterface *interface,
3753                                                         const char *identifier)
3754 {
3755         GSupplicantPeer *peer;
3756
3757         peer = g_hash_table_find(interface->peer_table,
3758                                         peer_lookup_by_identifier,
3759                                         (void *) identifier);
3760         return peer;
3761 }
3762
3763 struct interface_data {
3764         GSupplicantInterface *interface;
3765         char *path; /* Interface path cannot be taken from interface (above) as
3766                      * it might have been freed already.
3767                      */
3768         GSupplicantInterfaceCallback callback;
3769         void *user_data;
3770 };
3771
3772 struct interface_create_data {
3773         char *ifname;
3774         char *driver;
3775         char *bridge;
3776         GSupplicantInterface *interface;
3777         GSupplicantInterfaceCallback callback;
3778         void *user_data;
3779 };
3780
3781 struct interface_connect_data {
3782         GSupplicantInterface *interface;
3783         char *path;
3784         GSupplicantInterfaceCallback callback;
3785         union {
3786                 GSupplicantSSID *ssid;
3787                 GSupplicantPeerParams *peer;
3788         };
3789         void *user_data;
3790 };
3791
3792 struct interface_scan_data {
3793         GSupplicantInterface *interface;
3794         char *path;
3795         GSupplicantInterfaceCallback callback;
3796         GSupplicantScanParams *scan_params;
3797         void *user_data;
3798 };
3799
3800 static void interface_create_data_free(struct interface_create_data *data)
3801 {
3802         g_free(data->ifname);
3803         g_free(data->driver);
3804         g_free(data->bridge);
3805         dbus_free(data);
3806 }
3807
3808 static bool interface_exists(GSupplicantInterface *interface,
3809                                 const char *path)
3810 {
3811         GSupplicantInterface *tmp;
3812
3813         tmp = g_hash_table_lookup(interface_table, path);
3814         if (tmp && tmp == interface)
3815                 return true;
3816
3817         return false;
3818 }
3819
3820 static void interface_create_property(const char *key, DBusMessageIter *iter,
3821                                                         void *user_data)
3822 {
3823         struct interface_create_data *data = user_data;
3824         GSupplicantInterface *interface = data->interface;
3825
3826         if (!key) {
3827                 if (data->callback) {
3828                         data->callback(0, data->interface, data->user_data);
3829 #if !defined TIZEN_EXT
3830                         callback_p2p_support(interface);
3831 #endif
3832                 }
3833
3834                 interface_create_data_free(data);
3835         }
3836
3837         interface_property(key, iter, interface);
3838 }
3839
3840 static void interface_create_result(const char *error,
3841                                 DBusMessageIter *iter, void *user_data)
3842 {
3843         struct interface_create_data *data = user_data;
3844         const char *path = NULL;
3845         int err;
3846
3847         SUPPLICANT_DBG("");
3848
3849         if (error) {
3850                 g_warning("error %s", error);
3851                 err = -EIO;
3852                 goto done;
3853         }
3854
3855         dbus_message_iter_get_basic(iter, &path);
3856         if (!path) {
3857                 err = -EINVAL;
3858                 goto done;
3859         }
3860
3861         if (!system_available) {
3862                 err = -EFAULT;
3863                 goto done;
3864         }
3865
3866         data->interface = g_hash_table_lookup(interface_table, path);
3867         if (!data->interface) {
3868                 data->interface = interface_alloc(path);
3869                 if (!data->interface) {
3870                         err = -ENOMEM;
3871                         goto done;
3872                 }
3873         }
3874
3875         err = supplicant_dbus_property_get_all(path,
3876                                         SUPPLICANT_INTERFACE ".Interface",
3877                                         interface_create_property, data,
3878                                         NULL);
3879         if (err == 0)
3880                 return;
3881
3882 done:
3883         if (data->callback)
3884                 data->callback(err, NULL, data->user_data);
3885
3886         interface_create_data_free(data);
3887 }
3888
3889 static void interface_create_params(DBusMessageIter *iter, void *user_data)
3890 {
3891         struct interface_create_data *data = user_data;
3892         DBusMessageIter dict;
3893
3894         SUPPLICANT_DBG("");
3895
3896         supplicant_dbus_dict_open(iter, &dict);
3897
3898         supplicant_dbus_dict_append_basic(&dict, "Ifname",
3899                                         DBUS_TYPE_STRING, &data->ifname);
3900
3901         if (data->driver)
3902                 supplicant_dbus_dict_append_basic(&dict, "Driver",
3903                                         DBUS_TYPE_STRING, &data->driver);
3904
3905         if (data->bridge)
3906                 supplicant_dbus_dict_append_basic(&dict, "BridgeIfname",
3907                                         DBUS_TYPE_STRING, &data->bridge);
3908
3909         supplicant_dbus_dict_close(iter, &dict);
3910 }
3911
3912 static void interface_get_result(const char *error,
3913                                 DBusMessageIter *iter, void *user_data)
3914 {
3915         struct interface_create_data *data = user_data;
3916         GSupplicantInterface *interface;
3917         const char *path = NULL;
3918         int err;
3919
3920         SUPPLICANT_DBG("");
3921
3922         if (error) {
3923                 SUPPLICANT_DBG("Interface not created yet");
3924                 goto create;
3925         }
3926
3927         dbus_message_iter_get_basic(iter, &path);
3928         if (!path) {
3929                 err = -EINVAL;
3930                 goto done;
3931         }
3932
3933         interface = g_hash_table_lookup(interface_table, path);
3934         if (!interface) {
3935                 err = -ENOENT;
3936                 goto done;
3937         }
3938
3939         if (data->callback) {
3940                 data->callback(0, interface, data->user_data);
3941 #if !defined TIZEN_EXT
3942                 callback_p2p_support(interface);
3943 #endif
3944         }
3945
3946         interface_create_data_free(data);
3947
3948         return;
3949
3950 create:
3951         if (!system_available) {
3952                 err = -EFAULT;
3953                 goto done;
3954         }
3955
3956         SUPPLICANT_DBG("Creating interface");
3957
3958         err = supplicant_dbus_method_call(SUPPLICANT_PATH,
3959                                                 SUPPLICANT_INTERFACE,
3960                                                 "CreateInterface",
3961                                                 interface_create_params,
3962                                                 interface_create_result, data,
3963                                                 NULL);
3964         if (err == 0)
3965                 return;
3966
3967 done:
3968         if (data->callback)
3969                 data->callback(err, NULL, data->user_data);
3970
3971         interface_create_data_free(data);
3972 }
3973
3974 static void interface_get_params(DBusMessageIter *iter, void *user_data)
3975 {
3976         struct interface_create_data *data = user_data;
3977
3978         SUPPLICANT_DBG("");
3979
3980         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
3981 }
3982
3983 int g_supplicant_interface_create(const char *ifname, const char *driver,
3984                                         const char *bridge,
3985                                         GSupplicantInterfaceCallback callback,
3986                                                         void *user_data)
3987 {
3988         struct interface_create_data *data;
3989         int ret;
3990
3991         SUPPLICANT_DBG("ifname %s", ifname);
3992
3993         if (!ifname)
3994                 return -EINVAL;
3995
3996         if (!system_available)
3997                 return -EFAULT;
3998
3999         data = dbus_malloc0(sizeof(*data));
4000         if (!data)
4001                 return -ENOMEM;
4002
4003         data->ifname = g_strdup(ifname);
4004         data->driver = g_strdup(driver);
4005         data->bridge = g_strdup(bridge);
4006         data->callback = callback;
4007         data->user_data = user_data;
4008
4009         ret = supplicant_dbus_method_call(SUPPLICANT_PATH,
4010                                                 SUPPLICANT_INTERFACE,
4011                                                 "GetInterface",
4012                                                 interface_get_params,
4013                                                 interface_get_result, data,
4014                                                 NULL);
4015         if (ret < 0)
4016                 interface_create_data_free(data);
4017
4018         return ret;
4019 }
4020
4021 static void interface_remove_result(const char *error,
4022                                 DBusMessageIter *iter, void *user_data)
4023 {
4024         struct interface_data *data = user_data;
4025         int err;
4026
4027         if (error) {
4028                 err = -EIO;
4029                 goto done;
4030         }
4031
4032         if (!system_available) {
4033                 err = -EFAULT;
4034                 goto done;
4035         }
4036
4037         /*
4038          * The gsupplicant interface is already freed by the InterfaceRemoved
4039          * signal callback. Simply invoke the interface_data callback.
4040          */
4041         err = 0;
4042
4043 done:
4044         g_free(data->path);
4045
4046         if (data->callback)
4047                 data->callback(err, NULL, data->user_data);
4048
4049         dbus_free(data);
4050 }
4051
4052
4053 static void interface_remove_params(DBusMessageIter *iter, void *user_data)
4054 {
4055         struct interface_data *data = user_data;
4056
4057         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
4058                                                         &data->interface->path);
4059 }
4060
4061
4062 int g_supplicant_interface_remove(GSupplicantInterface *interface,
4063                         GSupplicantInterfaceCallback callback,
4064                                                         void *user_data)
4065 {
4066         struct interface_data *data;
4067         int ret;
4068
4069         if (!interface)
4070                 return -EINVAL;
4071
4072         if (!system_available)
4073                 return -EFAULT;
4074
4075         g_supplicant_interface_cancel(interface);
4076
4077         data = dbus_malloc0(sizeof(*data));
4078         if (!data)
4079                 return -ENOMEM;
4080
4081         data->interface = interface;
4082         data->path = g_strdup(interface->path);
4083         data->callback = callback;
4084         data->user_data = user_data;
4085
4086         ret = supplicant_dbus_method_call(SUPPLICANT_PATH,
4087                                                 SUPPLICANT_INTERFACE,
4088                                                 "RemoveInterface",
4089                                                 interface_remove_params,
4090                                                 interface_remove_result, data,
4091                                                 NULL);
4092         if (ret < 0) {
4093                 g_free(data->path);
4094                 dbus_free(data);
4095         }
4096         return ret;
4097 }
4098
4099 static void interface_scan_result(const char *error,
4100                                 DBusMessageIter *iter, void *user_data)
4101 {
4102         struct interface_scan_data *data = user_data;
4103         int err = 0;
4104
4105         if (error) {
4106                 SUPPLICANT_DBG("error %s", error);
4107                 err = -EIO;
4108         }
4109
4110         /* A non ready interface cannot send/receive anything */
4111         if (interface_exists(data->interface, data->path)) {
4112                 if (!data->interface->ready)
4113                         err = -ENOLINK;
4114         }
4115
4116         g_free(data->path);
4117
4118         if (err != 0) {
4119                 if (data->callback)
4120                         data->callback(err, data->interface, data->user_data);
4121         } else {
4122                 data->interface->scan_callback = data->callback;
4123                 data->interface->scan_data = data->user_data;
4124         }
4125
4126         if (data->scan_params)
4127                 g_supplicant_free_scan_params(data->scan_params);
4128
4129         dbus_free(data);
4130 }
4131
4132 static void add_scan_frequency(DBusMessageIter *iter, unsigned int freq)
4133 {
4134         DBusMessageIter data;
4135         unsigned int width = 0; /* Not used by wpa_supplicant atm */
4136
4137         dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &data);
4138
4139         dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &freq);
4140         dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &width);
4141
4142         dbus_message_iter_close_container(iter, &data);
4143 }
4144
4145 static void add_scan_frequencies(DBusMessageIter *iter,
4146                                                 void *user_data)
4147 {
4148         GSupplicantScanParams *scan_data = user_data;
4149         unsigned int freq;
4150         int i;
4151
4152         for (i = 0; i < scan_data->num_freqs; i++) {
4153                 freq = scan_data->freqs[i];
4154                 if (!freq)
4155                         break;
4156
4157                 add_scan_frequency(iter, freq);
4158         }
4159 }
4160
4161 static void append_ssid(DBusMessageIter *iter,
4162                         const void *ssid, unsigned int len)
4163 {
4164         DBusMessageIter array;
4165
4166         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
4167         DBUS_TYPE_BYTE_AS_STRING, &array);
4168
4169         dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
4170                                                                 &ssid, len);
4171         dbus_message_iter_close_container(iter, &array);
4172 }
4173
4174 static void append_ssids(DBusMessageIter *iter, void *user_data)
4175 {
4176         GSupplicantScanParams *scan_data = user_data;
4177         GSList *list;
4178
4179         for (list = scan_data->ssids; list; list = list->next) {
4180                 struct scan_ssid *scan_ssid = list->data;
4181
4182                 append_ssid(iter, scan_ssid->ssid, scan_ssid->ssid_len);
4183         }
4184 }
4185
4186 static void supplicant_add_scan_frequency(DBusMessageIter *dict,
4187                 supplicant_dbus_array_function function,
4188                                         void *user_data)
4189 {
4190         GSupplicantScanParams *scan_params = user_data;
4191         DBusMessageIter entry, value, array;
4192         const char *key = "Channels";
4193
4194         if (scan_params->freqs && scan_params->freqs[0] != 0) {
4195                 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
4196                                                 NULL, &entry);
4197
4198                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
4199
4200                 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
4201                                         DBUS_TYPE_ARRAY_AS_STRING
4202                                         DBUS_STRUCT_BEGIN_CHAR_AS_STRING
4203                                         DBUS_TYPE_UINT32_AS_STRING
4204                                         DBUS_TYPE_UINT32_AS_STRING
4205                                         DBUS_STRUCT_END_CHAR_AS_STRING,
4206                                         &value);
4207
4208                 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
4209                                         DBUS_STRUCT_BEGIN_CHAR_AS_STRING
4210                                         DBUS_TYPE_UINT32_AS_STRING
4211                                         DBUS_TYPE_UINT32_AS_STRING
4212                                         DBUS_STRUCT_END_CHAR_AS_STRING,
4213                                         &array);
4214
4215                 if (function)
4216                         function(&array, user_data);
4217
4218                 dbus_message_iter_close_container(&value, &array);
4219                 dbus_message_iter_close_container(&entry, &value);
4220                 dbus_message_iter_close_container(dict, &entry);
4221         }
4222 }
4223
4224 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
4225 {
4226         DBusMessageIter dict;
4227         const char *type = "passive";
4228         struct interface_scan_data *data = user_data;
4229
4230         supplicant_dbus_dict_open(iter, &dict);
4231
4232         if (data && data->scan_params) {
4233                 type = "active";
4234
4235                 supplicant_dbus_dict_append_basic(&dict, "Type",
4236                                         DBUS_TYPE_STRING, &type);
4237
4238                 supplicant_dbus_dict_append_array(&dict, "SSIDs",
4239                                                 DBUS_TYPE_STRING,
4240                                                 append_ssids,
4241                                                 data->scan_params);
4242
4243                 supplicant_add_scan_frequency(&dict, add_scan_frequencies,
4244                                                 data->scan_params);
4245         } else
4246                 supplicant_dbus_dict_append_basic(&dict, "Type",
4247                                         DBUS_TYPE_STRING, &type);
4248
4249         supplicant_dbus_dict_close(iter, &dict);
4250 }
4251
4252 static int interface_ready_to_scan(GSupplicantInterface *interface)
4253 {
4254         if (!interface)
4255                 return -EINVAL;
4256
4257         if (!system_available)
4258                 return -EFAULT;
4259
4260         if (interface->scanning)
4261                 return -EALREADY;
4262
4263         switch (interface->state) {
4264         case G_SUPPLICANT_STATE_AUTHENTICATING:
4265         case G_SUPPLICANT_STATE_ASSOCIATING:
4266         case G_SUPPLICANT_STATE_ASSOCIATED:
4267         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4268         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4269                 return -EBUSY;
4270         case G_SUPPLICANT_STATE_UNKNOWN:
4271         case G_SUPPLICANT_STATE_DISABLED:
4272         case G_SUPPLICANT_STATE_DISCONNECTED:
4273         case G_SUPPLICANT_STATE_INACTIVE:
4274         case G_SUPPLICANT_STATE_SCANNING:
4275         case G_SUPPLICANT_STATE_COMPLETED:
4276                 break;
4277         }
4278
4279         return 0;
4280 }
4281
4282 int g_supplicant_interface_scan(GSupplicantInterface *interface,
4283                                 GSupplicantScanParams *scan_data,
4284                                 GSupplicantInterfaceCallback callback,
4285                                                         void *user_data)
4286 {
4287         struct interface_scan_data *data;
4288         int ret;
4289
4290         ret = interface_ready_to_scan(interface);
4291         if (ret)
4292                 return ret;
4293
4294         data = dbus_malloc0(sizeof(*data));
4295         if (!data)
4296                 return -ENOMEM;
4297
4298         data->interface = interface;
4299         data->path = g_strdup(interface->path);
4300 #if defined TIZEN_EXT
4301         data->interface->scan_callback = data->callback = callback;
4302         data->interface->scan_data = data->user_data = user_data;
4303 #else
4304         data->callback = callback;
4305         data->user_data = user_data;
4306 #endif
4307         data->scan_params = scan_data;
4308
4309         interface->scan_callback = callback;
4310         interface->scan_data = user_data;
4311
4312         ret = supplicant_dbus_method_call(interface->path,
4313                         SUPPLICANT_INTERFACE ".Interface", "Scan",
4314                         interface_scan_params, interface_scan_result, data,
4315                         interface);
4316
4317         if (ret < 0) {
4318                 g_free(data->path);
4319                 dbus_free(data);
4320         }
4321
4322         return ret;
4323 }
4324
4325 static int parse_supplicant_error(DBusMessageIter *iter)
4326 {
4327         int err = -ECANCELED;
4328         char *key;
4329
4330         if (!iter)
4331                 return err;
4332
4333         /* If the given passphrase is malformed wpa_s returns
4334          * "invalid message format" but this error should be interpreted as
4335          * invalid-key.
4336          */
4337         while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
4338                 dbus_message_iter_get_basic(iter, &key);
4339                 if (strncmp(key, "psk", 3) == 0 ||
4340                                 strncmp(key, "wep_key", 7) == 0 ||
4341                                 strcmp(key, "invalid message format") == 0) {
4342                         err = -ENOKEY;
4343                         break;
4344                 }
4345                 dbus_message_iter_next(iter);
4346         }
4347
4348         return err;
4349 }
4350
4351 static void interface_select_network_result(const char *error,
4352                                 DBusMessageIter *iter, void *user_data)
4353 {
4354         struct interface_connect_data *data = user_data;
4355         int err;
4356
4357         SUPPLICANT_DBG("");
4358
4359         err = 0;
4360         if (error) {
4361                 SUPPLICANT_DBG("SelectNetwork error %s", error);
4362                 err = parse_supplicant_error(iter);
4363         }
4364
4365         g_free(data->path);
4366
4367         if (data->callback)
4368                 data->callback(err, data->interface, data->user_data);
4369
4370         g_free(data->ssid);
4371         dbus_free(data);
4372 }
4373
4374 static void interface_select_network_params(DBusMessageIter *iter,
4375                                                         void *user_data)
4376 {
4377         struct interface_connect_data *data = user_data;
4378         GSupplicantInterface *interface = data->interface;
4379
4380         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
4381                                         &interface->network_path);
4382 }
4383
4384 static void interface_add_network_result(const char *error,
4385                                 DBusMessageIter *iter, void *user_data)
4386 {
4387         struct interface_connect_data *data = user_data;
4388         GSupplicantInterface *interface = data->interface;
4389         const char *path;
4390         int err;
4391
4392         if (error)
4393                 goto error;
4394
4395         dbus_message_iter_get_basic(iter, &path);
4396         if (!path)
4397                 goto error;
4398
4399         SUPPLICANT_DBG("PATH: %s", path);
4400
4401         g_free(interface->network_path);
4402         interface->network_path = g_strdup(path);
4403
4404         supplicant_dbus_method_call(data->interface->path,
4405                         SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
4406                         interface_select_network_params,
4407                         interface_select_network_result, data,
4408                         interface);
4409
4410         return;
4411
4412 error:
4413         SUPPLICANT_DBG("AddNetwork error %s", error);
4414
4415         if (interface_exists(data->interface, data->interface->path)) {
4416                 err = parse_supplicant_error(iter);
4417                 if (data->callback)
4418                         data->callback(err, data->interface, data->user_data);
4419
4420                 g_free(interface->network_path);
4421                 interface->network_path = NULL;
4422         }
4423
4424         g_free(data->path);
4425         g_free(data->ssid);
4426         g_free(data);
4427 }
4428
4429 static void add_network_security_none(DBusMessageIter *dict)
4430 {
4431         const char *auth_alg = "OPEN";
4432
4433         supplicant_dbus_dict_append_basic(dict, "auth_alg",
4434                                         DBUS_TYPE_STRING, &auth_alg);
4435 }
4436
4437 static void add_network_security_wep(DBusMessageIter *dict,
4438                                         GSupplicantSSID *ssid)
4439 {
4440         const char *auth_alg = "OPEN SHARED";
4441         dbus_uint32_t key_index = 0;
4442
4443         supplicant_dbus_dict_append_basic(dict, "auth_alg",
4444                                         DBUS_TYPE_STRING, &auth_alg);
4445
4446         if (ssid->passphrase) {
4447                 int size = strlen(ssid->passphrase);
4448                 if (size == 10 || size == 26) {
4449                         unsigned char *key = g_try_malloc(13);
4450                         char tmp[3];
4451                         int i;
4452
4453                         memset(tmp, 0, sizeof(tmp));
4454                         if (!key)
4455                                 size = 0;
4456
4457                         for (i = 0; i < size / 2; i++) {
4458                                 memcpy(tmp, ssid->passphrase + (i * 2), 2);
4459                                 key[i] = (unsigned char) strtol(tmp, NULL, 16);
4460                         }
4461
4462                         supplicant_dbus_dict_append_fixed_array(dict,
4463                                                         "wep_key0",
4464                                                         DBUS_TYPE_BYTE,
4465                                                         &key, size / 2);
4466                         g_free(key);
4467                 } else if (size == 5 || size == 13) {
4468                         unsigned char *key = g_try_malloc(13);
4469                         int i;
4470
4471                         if (!key)
4472                                 size = 0;
4473
4474                         for (i = 0; i < size; i++)
4475                                 key[i] = (unsigned char) ssid->passphrase[i];
4476
4477                         supplicant_dbus_dict_append_fixed_array(dict,
4478                                                                 "wep_key0",
4479                                                                 DBUS_TYPE_BYTE,
4480                                                                 &key, size);
4481                         g_free(key);
4482                 } else
4483                         supplicant_dbus_dict_append_basic(dict,
4484                                                         "wep_key0",
4485                                                         DBUS_TYPE_STRING,
4486                                                         &ssid->passphrase);
4487
4488                 supplicant_dbus_dict_append_basic(dict, "wep_tx_keyidx",
4489                                         DBUS_TYPE_UINT32, &key_index);
4490         }
4491 }
4492
4493 static dbus_bool_t is_psk_raw_key(const char *psk)
4494 {
4495         int i;
4496
4497         /* A raw key is always 64 bytes length... */
4498         if (strlen(psk) != 64)
4499                 return FALSE;
4500
4501         /* ... and its content is in hex representation */
4502         for (i = 0; i < 64; i++)
4503                 if (!isxdigit((unsigned char) psk[i]))
4504                         return FALSE;
4505
4506         return TRUE;
4507 }
4508
4509 static unsigned char hexchar2bin(char c)
4510 {
4511         if ((c >= '0') && (c <= '9'))
4512                 return c - '0';
4513         else if ((c >= 'A') && (c <= 'F'))
4514                 return c - 'A' + 10;
4515         else if ((c >= 'a') && (c <= 'f'))
4516                 return c - 'a' + 10;
4517         else
4518                 return c;
4519 }
4520
4521 static void hexstring2bin(const char *string, unsigned char *data,
4522                                 size_t data_len)
4523 {
4524         size_t i;
4525
4526         for (i = 0; i < data_len; i++)
4527                 data[i] = (hexchar2bin(string[i * 2 + 0]) << 4 |
4528                            hexchar2bin(string[i * 2 + 1]) << 0);
4529 }
4530
4531 static void add_network_security_psk(DBusMessageIter *dict,
4532                                         GSupplicantSSID *ssid)
4533 {
4534         if (ssid->passphrase && strlen(ssid->passphrase) > 0) {
4535                 const char *key = "psk";
4536
4537                 if (is_psk_raw_key(ssid->passphrase)) {
4538                         unsigned char data[32];
4539                         unsigned char *datap = data;
4540
4541                         /* The above pointer alias is required by D-Bus because
4542                          * with D-Bus and GCC, non-heap-allocated arrays cannot
4543                          * be passed directly by their base pointer. */
4544
4545                         hexstring2bin(ssid->passphrase, datap, sizeof(data));
4546
4547                         supplicant_dbus_dict_append_fixed_array(dict,
4548                                                         key, DBUS_TYPE_BYTE,
4549                                                         &datap, sizeof(data));
4550                 } else
4551                         supplicant_dbus_dict_append_basic(dict,
4552                                                         key, DBUS_TYPE_STRING,
4553                                                         &ssid->passphrase);
4554         }
4555 }
4556
4557 static void add_network_security_tls(DBusMessageIter *dict,
4558                                         GSupplicantSSID *ssid)
4559 {
4560         /*
4561          * For TLS, we at least need:
4562          *              The client certificate
4563          *              The client private key file
4564          *              The client private key file password
4565          *
4566          * The Authority certificate is optional.
4567          */
4568         if (!ssid->client_cert_path)
4569                 return;
4570
4571         if (!ssid->private_key_path)
4572                 return;
4573
4574 #if !defined TIZEN_EXT
4575         if (!ssid->private_key_passphrase)
4576                 return;
4577 #endif
4578
4579         if (ssid->ca_cert_path)
4580                 supplicant_dbus_dict_append_basic(dict, "ca_cert",
4581                                         DBUS_TYPE_STRING, &ssid->ca_cert_path);
4582
4583         supplicant_dbus_dict_append_basic(dict, "private_key",
4584                                                 DBUS_TYPE_STRING,
4585                                                 &ssid->private_key_path);
4586 #if !defined TIZEN_EXT
4587         supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
4588                                                 DBUS_TYPE_STRING,
4589                                                 &ssid->private_key_passphrase);
4590 #endif
4591         supplicant_dbus_dict_append_basic(dict, "client_cert",
4592                                                 DBUS_TYPE_STRING,
4593                                                 &ssid->client_cert_path);
4594 }
4595
4596 static void add_network_security_peap(DBusMessageIter *dict,
4597                                         GSupplicantSSID *ssid)
4598 {
4599         char *phase2_auth;
4600
4601         /*
4602          * For PEAP/TTLS, we at least need
4603          *              The authority certificate
4604          *              The 2nd phase authentication method
4605          *              The 2nd phase passphrase
4606          *
4607          * The Client certificate is optional although strongly recommended
4608          * When setting it, we need in addition
4609          *              The Client private key file
4610          *              The Client private key file password
4611          */
4612         if (!ssid->passphrase)
4613                 return;
4614
4615         if (!ssid->phase2_auth)
4616                 return;
4617
4618         if (ssid->client_cert_path) {
4619                 if (!ssid->private_key_path)
4620                         return;
4621
4622 #if !defined TIZEN_EXT
4623                 if (!ssid->private_key_passphrase)
4624                         return;
4625 #endif
4626
4627                 supplicant_dbus_dict_append_basic(dict, "client_cert",
4628                                                 DBUS_TYPE_STRING,
4629                                                 &ssid->client_cert_path);
4630
4631                 supplicant_dbus_dict_append_basic(dict, "private_key",
4632                                                 DBUS_TYPE_STRING,
4633                                                 &ssid->private_key_path);
4634
4635 #if !defined TIZEN_EXT
4636                 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
4637                                                 DBUS_TYPE_STRING,
4638                                                 &ssid->private_key_passphrase);
4639 #endif
4640
4641         }
4642
4643         if (g_str_has_prefix(ssid->phase2_auth, "EAP-")) {
4644                 phase2_auth = g_strdup_printf("autheap=%s",
4645                                         ssid->phase2_auth + strlen("EAP-"));
4646         } else
4647                 phase2_auth = g_strdup_printf("auth=%s", ssid->phase2_auth);
4648
4649         supplicant_dbus_dict_append_basic(dict, "password",
4650                                                 DBUS_TYPE_STRING,
4651                                                 &ssid->passphrase);
4652
4653         if (ssid->ca_cert_path)
4654                 supplicant_dbus_dict_append_basic(dict, "ca_cert",
4655                                                 DBUS_TYPE_STRING,
4656                                                 &ssid->ca_cert_path);
4657
4658         supplicant_dbus_dict_append_basic(dict, "phase2",
4659                                                 DBUS_TYPE_STRING,
4660                                                 &phase2_auth);
4661
4662         g_free(phase2_auth);
4663 }
4664
4665 static void add_network_security_eap(DBusMessageIter *dict,
4666                                         GSupplicantSSID *ssid)
4667 {
4668         char *eap_value;
4669
4670 #if defined TIZEN_EXT
4671         if (!ssid->eap)
4672 #else
4673         if (!ssid->eap || !ssid->identity)
4674 #endif
4675                 return;
4676
4677         if (g_strcmp0(ssid->eap, "tls") == 0) {
4678                 add_network_security_tls(dict, ssid);
4679         } else if (g_strcmp0(ssid->eap, "peap") == 0 ||
4680                                 g_strcmp0(ssid->eap, "ttls") == 0) {
4681 #if defined TIZEN_EXT
4682                 if (!ssid->identity)
4683                         return;
4684 #endif
4685                 add_network_security_peap(dict, ssid);
4686
4687 #if defined TIZEN_EXT
4688         } else if (g_strcmp0(ssid->eap, "sim") == 0 ||
4689                         g_strcmp0(ssid->eap, "aka") == 0) {
4690 #endif
4691         } else
4692                 return;
4693
4694         eap_value = g_ascii_strup(ssid->eap, -1);
4695
4696         supplicant_dbus_dict_append_basic(dict, "eap",
4697                                                 DBUS_TYPE_STRING,
4698                                                 &eap_value);
4699 #if defined TIZEN_EXT
4700         if (ssid->identity != NULL)
4701                 supplicant_dbus_dict_append_basic(dict, "identity",
4702                                                         DBUS_TYPE_STRING,
4703                                                         &ssid->identity);
4704 #else
4705         supplicant_dbus_dict_append_basic(dict, "identity",
4706                                                 DBUS_TYPE_STRING,
4707                                                 &ssid->identity);
4708 #endif
4709
4710         g_free(eap_value);
4711 }
4712
4713 static void add_network_security_ciphers(DBusMessageIter *dict,
4714                                                 GSupplicantSSID *ssid)
4715 {
4716         unsigned int p_cipher, g_cipher, i;
4717         char *pairwise, *group;
4718         char *pair_ciphers[4];
4719         char *group_ciphers[5];
4720
4721         p_cipher = ssid->pairwise_cipher;
4722         g_cipher = ssid->group_cipher;
4723
4724         if (p_cipher == 0 && g_cipher == 0)
4725                 return;
4726
4727         i = 0;
4728
4729         if (p_cipher & G_SUPPLICANT_PAIRWISE_CCMP)
4730                 pair_ciphers[i++] = "CCMP";
4731
4732         if (p_cipher & G_SUPPLICANT_PAIRWISE_TKIP)
4733                 pair_ciphers[i++] = "TKIP";
4734
4735         if (p_cipher & G_SUPPLICANT_PAIRWISE_NONE)
4736                 pair_ciphers[i++] = "NONE";
4737
4738         pair_ciphers[i] = NULL;
4739
4740         i = 0;
4741
4742         if (g_cipher & G_SUPPLICANT_GROUP_CCMP)
4743                 group_ciphers[i++] = "CCMP";
4744
4745         if (g_cipher & G_SUPPLICANT_GROUP_TKIP)
4746                 group_ciphers[i++] = "TKIP";
4747
4748         if (g_cipher & G_SUPPLICANT_GROUP_WEP104)
4749                 group_ciphers[i++] = "WEP104";
4750
4751         if (g_cipher & G_SUPPLICANT_GROUP_WEP40)
4752                 group_ciphers[i++] = "WEP40";
4753
4754         group_ciphers[i] = NULL;
4755
4756         pairwise = g_strjoinv(" ", pair_ciphers);
4757         group = g_strjoinv(" ", group_ciphers);
4758
4759         SUPPLICANT_DBG("cipher %s %s", pairwise, group);
4760
4761         supplicant_dbus_dict_append_basic(dict, "pairwise",
4762                                                 DBUS_TYPE_STRING,
4763                                                 &pairwise);
4764         supplicant_dbus_dict_append_basic(dict, "group",
4765                                                 DBUS_TYPE_STRING,
4766                                                 &group);
4767
4768         g_free(pairwise);
4769         g_free(group);
4770 }
4771
4772 static void add_network_security_proto(DBusMessageIter *dict,
4773                                                 GSupplicantSSID *ssid)
4774 {
4775         unsigned int protocol, i;
4776         char *proto;
4777         char *protos[3];
4778
4779         protocol = ssid->protocol;
4780
4781         if (protocol == 0)
4782                 return;
4783
4784         i = 0;
4785
4786         if (protocol & G_SUPPLICANT_PROTO_RSN)
4787                 protos[i++] = "RSN";
4788
4789         if (protocol & G_SUPPLICANT_PROTO_WPA)
4790                 protos[i++] = "WPA";
4791
4792         protos[i] = NULL;
4793
4794         proto = g_strjoinv(" ", protos);
4795
4796         SUPPLICANT_DBG("proto %s", proto);
4797
4798         supplicant_dbus_dict_append_basic(dict, "proto",
4799                                                 DBUS_TYPE_STRING,
4800                                                 &proto);
4801
4802         g_free(proto);
4803 }
4804
4805 static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid)
4806 {
4807         char *key_mgmt;
4808
4809         switch (ssid->security) {
4810         case G_SUPPLICANT_SECURITY_NONE:
4811                 key_mgmt = "NONE";
4812                 add_network_security_none(dict);
4813                 add_network_security_ciphers(dict, ssid);
4814                 break;
4815         case G_SUPPLICANT_SECURITY_UNKNOWN:
4816         case G_SUPPLICANT_SECURITY_WEP:
4817                 key_mgmt = "NONE";
4818                 add_network_security_wep(dict, ssid);
4819                 add_network_security_ciphers(dict, ssid);
4820                 break;
4821         case G_SUPPLICANT_SECURITY_PSK:
4822                 key_mgmt = "WPA-PSK";
4823                 add_network_security_psk(dict, ssid);
4824                 add_network_security_ciphers(dict, ssid);
4825                 add_network_security_proto(dict, ssid);
4826                 break;
4827         case G_SUPPLICANT_SECURITY_IEEE8021X:
4828                 key_mgmt = "WPA-EAP";
4829                 add_network_security_eap(dict, ssid);
4830                 add_network_security_ciphers(dict, ssid);
4831                 add_network_security_proto(dict, ssid);
4832                 break;
4833 #if defined TIZEN_EXT
4834         case G_SUPPLICANT_SECURITY_FT_PSK:
4835                 key_mgmt = "FT-PSK";
4836                 add_network_security_psk(dict, ssid);
4837                 add_network_security_ciphers(dict, ssid);
4838                 add_network_security_proto(dict, ssid);
4839                 break;
4840         case G_SUPPLICANT_SECURITY_FT_IEEE8021X:
4841                 key_mgmt = "FT-EAP";
4842                 add_network_security_eap(dict, ssid);
4843                 add_network_security_ciphers(dict, ssid);
4844                 add_network_security_proto(dict, ssid);
4845                 break;
4846 #endif
4847         }
4848
4849         supplicant_dbus_dict_append_basic(dict, "key_mgmt",
4850                                 DBUS_TYPE_STRING, &key_mgmt);
4851 }
4852
4853 static void add_network_mode(DBusMessageIter *dict, GSupplicantSSID *ssid)
4854 {
4855         dbus_uint32_t mode;
4856
4857         switch (ssid->mode) {
4858         case G_SUPPLICANT_MODE_UNKNOWN:
4859         case G_SUPPLICANT_MODE_INFRA:
4860                 mode = 0;
4861                 break;
4862         case G_SUPPLICANT_MODE_IBSS:
4863                 mode = 1;
4864                 break;
4865         case G_SUPPLICANT_MODE_MASTER:
4866                 mode = 2;
4867                 break;
4868         }
4869
4870         supplicant_dbus_dict_append_basic(dict, "mode",
4871                                 DBUS_TYPE_UINT32, &mode);
4872 }
4873
4874 static void interface_add_network_params(DBusMessageIter *iter, void *user_data)
4875 {
4876         DBusMessageIter dict;
4877         struct interface_connect_data *data = user_data;
4878         GSupplicantSSID *ssid = data->ssid;
4879
4880         supplicant_dbus_dict_open(iter, &dict);
4881
4882         if (ssid->scan_ssid)
4883                 supplicant_dbus_dict_append_basic(&dict, "scan_ssid",
4884                                          DBUS_TYPE_UINT32, &ssid->scan_ssid);
4885
4886         if (ssid->freq)
4887                 supplicant_dbus_dict_append_basic(&dict, "frequency",
4888                                          DBUS_TYPE_UINT32, &ssid->freq);
4889
4890         if (ssid->bgscan)
4891                 supplicant_dbus_dict_append_basic(&dict, "bgscan",
4892                                         DBUS_TYPE_STRING, &ssid->bgscan);
4893
4894         add_network_mode(&dict, ssid);
4895
4896         add_network_security(&dict, ssid);
4897
4898         supplicant_dbus_dict_append_fixed_array(&dict, "ssid",
4899                                         DBUS_TYPE_BYTE, &ssid->ssid,
4900                                                 ssid->ssid_len);
4901
4902         supplicant_dbus_dict_append_basic(&dict, "ignore_broadcast_ssid",
4903                                         DBUS_TYPE_INT32,
4904                                         &ssid->ignore_broadcast_ssid);
4905
4906         supplicant_dbus_dict_close(iter, &dict);
4907 }
4908
4909 static void interface_wps_start_result(const char *error,
4910                                 DBusMessageIter *iter, void *user_data)
4911 {
4912         struct interface_connect_data *data = user_data;
4913         int err;
4914
4915         SUPPLICANT_DBG("");
4916
4917         err = 0;
4918         if (error) {
4919                 SUPPLICANT_DBG("error: %s", error);
4920                 err = parse_supplicant_error(iter);
4921         }
4922
4923         if(data->callback)
4924                 data->callback(err, data->interface, data->user_data);
4925
4926         g_free(data->path);
4927         g_free(data->ssid);
4928         dbus_free(data);
4929 }
4930
4931 static void interface_add_wps_params(DBusMessageIter *iter, void *user_data)
4932 {
4933         struct interface_connect_data *data = user_data;
4934         GSupplicantSSID *ssid = data->ssid;
4935         const char *role = "enrollee", *type;
4936         DBusMessageIter dict;
4937
4938         SUPPLICANT_DBG("");
4939
4940         supplicant_dbus_dict_open(iter, &dict);
4941
4942         supplicant_dbus_dict_append_basic(&dict, "Role",
4943                                                 DBUS_TYPE_STRING, &role);
4944
4945         type = "pbc";
4946         if (ssid->pin_wps) {
4947                 type = "pin";
4948                 supplicant_dbus_dict_append_basic(&dict, "Pin",
4949                                         DBUS_TYPE_STRING, &ssid->pin_wps);
4950         }
4951
4952         supplicant_dbus_dict_append_basic(&dict, "Type",
4953                                         DBUS_TYPE_STRING, &type);
4954
4955 #if defined TIZEN_EXT
4956         if (ssid->bssid)
4957                 supplicant_dbus_dict_append_fixed_array(&dict, "Bssid",
4958                                                 DBUS_TYPE_BYTE, &ssid->bssid, 6);
4959 #endif
4960
4961         supplicant_dbus_dict_close(iter, &dict);
4962 }
4963
4964 static void wps_start(const char *error, DBusMessageIter *iter, void *user_data)
4965 {
4966         struct interface_connect_data *data = user_data;
4967
4968         SUPPLICANT_DBG("");
4969
4970         if (error) {
4971                 SUPPLICANT_DBG("error: %s", error);
4972                 g_free(data->path);
4973                 g_free(data->ssid);
4974                 dbus_free(data);
4975                 return;
4976         }
4977 #if defined TIZEN_EXT
4978         GSupplicantSSID *ssid = data->ssid;
4979         if (ssid->pin_wps != NULL) {
4980                 if (!g_utf8_validate(ssid->pin_wps, 8, NULL)) {
4981                         SUPPLICANT_DBG("Invalid characters in WPS_PIN");
4982                         g_free(data->ssid);
4983                         dbus_free(data);
4984                         return;
4985                 }
4986         }
4987 #endif
4988         supplicant_dbus_method_call(data->interface->path,
4989                         SUPPLICANT_INTERFACE ".Interface.WPS", "Start",
4990                         interface_add_wps_params,
4991                         interface_wps_start_result, data, NULL);
4992 }
4993
4994 static void wps_process_credentials(DBusMessageIter *iter, void *user_data)
4995 {
4996         dbus_bool_t credentials = TRUE;
4997
4998         SUPPLICANT_DBG("");
4999
5000         dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &credentials);
5001 }
5002
5003
5004 int g_supplicant_interface_connect(GSupplicantInterface *interface,
5005                                 GSupplicantSSID *ssid,
5006                                 GSupplicantInterfaceCallback callback,
5007                                                         void *user_data)
5008 {
5009         struct interface_connect_data *data;
5010         int ret;
5011
5012         if (!interface)
5013                 return -EINVAL;
5014
5015         if (!system_available)
5016                 return -EFAULT;
5017
5018         /* TODO: Check if we're already connected and switch */
5019
5020         data = dbus_malloc0(sizeof(*data));
5021         if (!data)
5022                 return -ENOMEM;
5023
5024         data->interface = interface;
5025         data->path = g_strdup(interface->path);
5026         data->callback = callback;
5027         data->ssid = ssid;
5028         data->user_data = user_data;
5029
5030         if (ssid->use_wps) {
5031                 g_free(interface->wps_cred.key);
5032                 memset(&interface->wps_cred, 0,
5033                                 sizeof(struct _GSupplicantWpsCredentials));
5034
5035                 ret = supplicant_dbus_property_set(interface->path,
5036                         SUPPLICANT_INTERFACE ".Interface.WPS",
5037                         "ProcessCredentials", DBUS_TYPE_BOOLEAN_AS_STRING,
5038                         wps_process_credentials, wps_start, data, interface);
5039         } else
5040                 ret = supplicant_dbus_method_call(interface->path,
5041                         SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
5042                         interface_add_network_params,
5043                         interface_add_network_result, data,
5044                         interface);
5045
5046         if (ret < 0) {
5047                 g_free(data->path);
5048                 dbus_free(data);
5049                 return ret;
5050         }
5051
5052         return -EINPROGRESS;
5053 }
5054
5055 static void network_remove_result(const char *error,
5056                                 DBusMessageIter *iter, void *user_data)
5057 {
5058         struct interface_data *data = user_data;
5059         int result = 0;
5060
5061         SUPPLICANT_DBG("");
5062
5063         if (error) {
5064                 result = -EIO;
5065                 if (g_strcmp0("org.freedesktop.DBus.Error.UnknownMethod",
5066                                                 error) == 0)
5067                         result = -ECONNABORTED;
5068         }
5069
5070         g_free(data->path);
5071
5072         if (data->callback)
5073                 data->callback(result, data->interface, data->user_data);
5074
5075         dbus_free(data);
5076 }
5077
5078 static void network_remove_params(DBusMessageIter *iter, void *user_data)
5079 {
5080         struct interface_data *data = user_data;
5081         const char *path = data->interface->network_path;
5082
5083         SUPPLICANT_DBG("path %s", path);
5084
5085         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
5086 }
5087
5088 static int network_remove(struct interface_data *data)
5089 {
5090         GSupplicantInterface *interface = data->interface;
5091
5092         SUPPLICANT_DBG("");
5093
5094 #if defined TIZEN_EXT
5095         GSupplicantInterface *intf = NULL;
5096         /*
5097          * Check if 'interface' is valid
5098          */
5099         intf = g_hash_table_lookup(interface_table, interface->path);
5100         if (intf == NULL)
5101                 return -EINVAL;
5102 #endif
5103
5104         return supplicant_dbus_method_call(interface->path,
5105                         SUPPLICANT_INTERFACE ".Interface", "RemoveNetwork",
5106                         network_remove_params, network_remove_result, data,
5107                         interface);
5108 }
5109
5110 static void interface_disconnect_result(const char *error,
5111                                 DBusMessageIter *iter, void *user_data)
5112 {
5113         struct interface_data *data = user_data;
5114         int result = 0;
5115
5116         SUPPLICANT_DBG("");
5117
5118         if (error) {
5119                 result = -EIO;
5120                 if (g_strcmp0("org.freedesktop.DBus.Error.UnknownMethod",
5121                                                 error) == 0)
5122                         result = -ECONNABORTED;
5123         }
5124
5125         if (result < 0 && data->callback) {
5126                 data->callback(result, data->interface, data->user_data);
5127                 data->callback = NULL;
5128         }
5129
5130         /* If we are disconnecting from previous WPS successful
5131          * association. i.e.: it did not went through AddNetwork,
5132          * and interface->network_path was never set. */
5133         if (!data->interface->network_path) {
5134                 g_free(data->path);
5135                 dbus_free(data);
5136                 return;
5137         }
5138
5139         if (result != -ECONNABORTED) {
5140                 if (network_remove(data) < 0) {
5141                         g_free(data->path);
5142                         dbus_free(data);
5143                 }
5144         } else {
5145                 g_free(data->path);
5146                 dbus_free(data);
5147         }
5148 }
5149
5150 int g_supplicant_interface_disconnect(GSupplicantInterface *interface,
5151                                         GSupplicantInterfaceCallback callback,
5152                                                         void *user_data)
5153 {
5154         struct interface_data *data;
5155         int ret;
5156
5157         SUPPLICANT_DBG("");
5158
5159         if (!interface)
5160                 return -EINVAL;
5161
5162         if (!system_available)
5163                 return -EFAULT;
5164
5165         data = dbus_malloc0(sizeof(*data));
5166         if (!data)
5167                 return -ENOMEM;
5168
5169         data->interface = interface;
5170         data->path = g_strdup(interface->path);
5171         data->callback = callback;
5172         data->user_data = user_data;
5173
5174         ret = supplicant_dbus_method_call(interface->path,
5175                         SUPPLICANT_INTERFACE ".Interface", "Disconnect",
5176                         NULL, interface_disconnect_result, data,
5177                         interface);
5178
5179         if (ret < 0) {
5180                 g_free(data->path);
5181                 dbus_free(data);
5182         }
5183
5184         return ret;
5185 }
5186
5187 static void interface_p2p_find_result(const char *error,
5188                                         DBusMessageIter *iter, void *user_data)
5189 {
5190         struct interface_scan_data *data = user_data;
5191         int err = 0;
5192
5193         SUPPLICANT_DBG("error %s", error);
5194
5195         if (error)
5196                 err = -EIO;
5197
5198         if (interface_exists(data->interface, data->path)) {
5199                 if (!data->interface->ready)
5200                         err = -ENOLINK;
5201                 if (!err)
5202                         data->interface->p2p_finding = true;
5203         }
5204
5205         if (data->callback)
5206                 data->callback(err, data->interface, data->user_data);
5207
5208         g_free(data->path);
5209         dbus_free(data);
5210 }
5211
5212 static void interface_p2p_find_params(DBusMessageIter *iter, void *user_data)
5213 {
5214         DBusMessageIter dict;
5215
5216         supplicant_dbus_dict_open(iter, &dict);
5217         supplicant_dbus_dict_close(iter, &dict);
5218 }
5219
5220 int g_supplicant_interface_p2p_find(GSupplicantInterface *interface,
5221                                         GSupplicantInterfaceCallback callback,
5222                                                         void *user_data)
5223 {
5224         struct interface_scan_data *data;
5225         int ret;
5226
5227         if (!interface->p2p_support)
5228                 return -ENOTSUP;
5229
5230         ret = interface_ready_to_scan(interface);
5231         if (ret && ret != -EALREADY)
5232                 return ret;
5233
5234         data = dbus_malloc0(sizeof(*data));
5235         if (!data)
5236                 return -ENOMEM;
5237
5238         data->interface = interface;
5239         data->path = g_strdup(interface->path);
5240         data->callback = callback;
5241         data->user_data = user_data;
5242
5243         ret = supplicant_dbus_method_call(interface->path,
5244                         SUPPLICANT_INTERFACE ".Interface.P2PDevice", "Find",
5245                         interface_p2p_find_params, interface_p2p_find_result,
5246                         data, interface);
5247         if (ret < 0) {
5248                 g_free(data->path);
5249                 dbus_free(data);
5250         }
5251
5252         return ret;
5253 }
5254
5255 bool g_supplicant_interface_is_p2p_finding(GSupplicantInterface *interface)
5256 {
5257         if (!interface)
5258                 return false;
5259
5260         return interface->p2p_finding;
5261 }
5262
5263 int g_supplicant_interface_p2p_stop_find(GSupplicantInterface *interface)
5264 {
5265         if (!interface->p2p_finding)
5266                 return 0;
5267
5268         SUPPLICANT_DBG("");
5269
5270         interface->p2p_finding = false;
5271
5272         return supplicant_dbus_method_call(interface->path,
5273                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "StopFind",
5274                 NULL, NULL, NULL, NULL);
5275 }
5276
5277 static void interface_p2p_connect_result(const char *error,
5278                                         DBusMessageIter *iter, void *user_data)
5279 {
5280         struct interface_connect_data *data = user_data;
5281         int err = 0;
5282
5283         SUPPLICANT_DBG("");
5284
5285         if (error)
5286                 err = parse_supplicant_error(iter);
5287
5288         if (data->callback)
5289                 data->callback(err, data->interface, data->user_data);
5290
5291         g_free(data->path);
5292         g_free(data->peer->wps_pin);
5293         g_free(data->peer->path);
5294         g_free(data->peer);
5295         g_free(data);
5296 }
5297
5298 static void interface_p2p_connect_params(DBusMessageIter *iter, void *user_data)
5299 {
5300         struct interface_connect_data *data = user_data;
5301         const char *wps = "pbc";
5302         DBusMessageIter dict;
5303         int go_intent = 7;
5304
5305         SUPPLICANT_DBG("");
5306
5307         supplicant_dbus_dict_open(iter, &dict);
5308
5309         if (data->peer->master)
5310                 go_intent = 15;
5311
5312         if (data->peer->wps_pin)
5313                 wps = "pin";
5314
5315         supplicant_dbus_dict_append_basic(&dict, "peer",
5316                                 DBUS_TYPE_OBJECT_PATH, &data->peer->path);
5317         supplicant_dbus_dict_append_basic(&dict, "wps_method",
5318                                 DBUS_TYPE_STRING, &wps);
5319         if (data->peer->wps_pin) {
5320                 supplicant_dbus_dict_append_basic(&dict, "pin",
5321                                 DBUS_TYPE_STRING, &data->peer->wps_pin);
5322         }
5323
5324         supplicant_dbus_dict_append_basic(&dict, "go_intent",
5325                                         DBUS_TYPE_INT32, &go_intent);
5326
5327         supplicant_dbus_dict_close(iter, &dict);
5328 }
5329
5330 int g_supplicant_interface_p2p_connect(GSupplicantInterface *interface,
5331                                         GSupplicantPeerParams *peer_params,
5332                                         GSupplicantInterfaceCallback callback,
5333                                         void *user_data)
5334 {
5335         struct interface_connect_data *data;
5336         int ret;
5337
5338         SUPPLICANT_DBG("");
5339
5340         if (!interface->p2p_support)
5341                 return -ENOTSUP;
5342
5343         data = dbus_malloc0(sizeof(*data));
5344         data->interface = interface;
5345         data->path = g_strdup(interface->path);
5346         data->peer = peer_params;
5347         data->callback = callback;
5348         data->user_data = user_data;
5349
5350         ret = supplicant_dbus_method_call(interface->path,
5351                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "Connect",
5352                 interface_p2p_connect_params, interface_p2p_connect_result,
5353                 data, interface);
5354         if (ret < 0) {
5355                 g_free(data->path);
5356                 dbus_free(data);
5357                 return ret;
5358         }
5359
5360         return -EINPROGRESS;
5361 }
5362
5363 int g_supplicant_interface_p2p_disconnect(GSupplicantInterface *interface,
5364                                         GSupplicantPeerParams *peer_params)
5365 {
5366         GSupplicantPeer *peer;
5367         int count = 0;
5368         GSList *list;
5369
5370         SUPPLICANT_DBG("");
5371
5372         if (!interface->p2p_support)
5373                 return -ENOTSUP;
5374
5375         peer = g_hash_table_lookup(interface->peer_table, peer_params->path);
5376         if (!peer)
5377                 return -ENODEV;
5378
5379         for (list = peer->groups; list; list = list->next, count++) {
5380                 const char *group_obj_path = list->data;
5381                 GSupplicantInterface *g_interface;
5382                 GSupplicantGroup *group;
5383
5384                 group = g_hash_table_lookup(group_mapping, group_obj_path);
5385                 if (!group || !group->interface)
5386                         continue;
5387
5388                 g_interface = group->interface;
5389                 supplicant_dbus_method_call(g_interface->path,
5390                                 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
5391                                 "Disconnect", NULL, NULL, NULL, g_interface);
5392         }
5393
5394         if (count == 0 && peer->current_group_iface) {
5395                 supplicant_dbus_method_call(peer->current_group_iface->path,
5396                                 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
5397                                 "Disconnect", NULL, NULL, NULL,
5398                                 peer->current_group_iface->path);
5399         }
5400
5401         peer->current_group_iface = NULL;
5402
5403         return -EINPROGRESS;
5404 }
5405
5406 struct p2p_service_data {
5407         bool registration;
5408         GSupplicantInterface *interface;
5409         GSupplicantP2PServiceParams *service;
5410         GSupplicantInterfaceCallback callback;
5411         void *user_data;
5412 };
5413
5414 static void interface_p2p_service_result(const char *error,
5415                                         DBusMessageIter *iter, void *user_data)
5416 {
5417         struct p2p_service_data *data = user_data;
5418         int result = 0;
5419
5420         SUPPLICANT_DBG("%s result - %s", data->registration ?
5421                                 "Registration" : "Deletion",
5422                                 error ? error : "Success");
5423         if (error)
5424                 result = -EINVAL;
5425
5426         if (data->callback)
5427                 data->callback(result, data->interface, data->user_data);
5428
5429         g_free(data->service->query);
5430         g_free(data->service->response);
5431         g_free(data->service->service);
5432         g_free(data->service->wfd_ies);
5433         g_free(data->service);
5434         dbus_free(data);
5435 }
5436
5437 static void interface_p2p_service_params(DBusMessageIter *iter,
5438                                                         void *user_data)
5439 {
5440         struct p2p_service_data *data = user_data;
5441         GSupplicantP2PServiceParams *service;
5442         DBusMessageIter dict;
5443         const char *type;
5444
5445         SUPPLICANT_DBG("");
5446
5447         service = data->service;
5448
5449         supplicant_dbus_dict_open(iter, &dict);
5450
5451         if (service->query && service->response) {
5452                 type = "bonjour";
5453                 supplicant_dbus_dict_append_basic(&dict, "service_type",
5454                                                 DBUS_TYPE_STRING, &type);
5455                 supplicant_dbus_dict_append_fixed_array(&dict, "query",
5456                                         DBUS_TYPE_BYTE, &service->query,
5457                                         service->query_length);
5458                 supplicant_dbus_dict_append_fixed_array(&dict, "response",
5459                                         DBUS_TYPE_BYTE, &service->response,
5460                                         service->response_length);
5461         } else if (service->version && service->service) {
5462                 type = "upnp";
5463                 supplicant_dbus_dict_append_basic(&dict, "service_type",
5464                                                 DBUS_TYPE_STRING, &type);
5465                 supplicant_dbus_dict_append_basic(&dict, "version",
5466                                         DBUS_TYPE_INT32, &service->version);
5467                 supplicant_dbus_dict_append_basic(&dict, "service",
5468                                         DBUS_TYPE_STRING, &service->service);
5469         }
5470
5471         supplicant_dbus_dict_close(iter, &dict);
5472 }
5473
5474 int g_supplicant_interface_p2p_add_service(GSupplicantInterface *interface,
5475                                 GSupplicantInterfaceCallback callback,
5476                                 GSupplicantP2PServiceParams *p2p_service_params,
5477                                 void *user_data)
5478 {
5479         struct p2p_service_data *data;
5480         int ret;
5481
5482         SUPPLICANT_DBG("");
5483
5484         if (!interface->p2p_support)
5485                 return -ENOTSUP;
5486
5487         data = dbus_malloc0(sizeof(*data));
5488         data->registration = true;
5489         data->interface = interface;
5490         data->service = p2p_service_params;
5491         data->callback = callback;
5492         data->user_data = user_data;
5493
5494         ret = supplicant_dbus_method_call(interface->path,
5495                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "AddService",
5496                 interface_p2p_service_params, interface_p2p_service_result,
5497                 data, interface);
5498         if (ret < 0) {
5499                 dbus_free(data);
5500                 return ret;
5501         }
5502
5503         return -EINPROGRESS;
5504 }
5505
5506 int g_supplicant_interface_p2p_del_service(GSupplicantInterface *interface,
5507                                 GSupplicantP2PServiceParams *p2p_service_params)
5508 {
5509         struct p2p_service_data *data;
5510         int ret;
5511
5512         SUPPLICANT_DBG("");
5513
5514         if (!interface->p2p_support)
5515                 return -ENOTSUP;
5516
5517         data = dbus_malloc0(sizeof(*data));
5518         data->interface = interface;
5519         data->service = p2p_service_params;
5520
5521         ret = supplicant_dbus_method_call(interface->path,
5522                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeleteService",
5523                 interface_p2p_service_params, interface_p2p_service_result,
5524                 data, interface);
5525         if (ret < 0) {
5526                 dbus_free(data);
5527                 return ret;
5528         }
5529
5530         return -EINPROGRESS;
5531 }
5532
5533 struct p2p_listen_data {
5534         int period;
5535         int interval;
5536 };
5537
5538 static void interface_p2p_listen_params(DBusMessageIter *iter, void *user_data)
5539 {
5540         struct p2p_listen_data *params = user_data;
5541         DBusMessageIter dict;
5542
5543         supplicant_dbus_dict_open(iter, &dict);
5544
5545         supplicant_dbus_dict_append_basic(&dict, "period",
5546                                         DBUS_TYPE_INT32, &params->period);
5547         supplicant_dbus_dict_append_basic(&dict, "interval",
5548                                         DBUS_TYPE_INT32, &params->interval);
5549         supplicant_dbus_dict_close(iter, &dict);
5550 }
5551
5552 int g_supplicant_interface_p2p_listen(GSupplicantInterface *interface,
5553                                                 int period, int interval)
5554 {
5555         struct p2p_listen_data params;
5556
5557         SUPPLICANT_DBG("");
5558
5559         if (!interface->p2p_support)
5560                 return -ENOTSUP;
5561
5562         params.period = period;
5563         params.interval = interval;
5564
5565         return supplicant_dbus_method_call(interface->path,
5566                         SUPPLICANT_INTERFACE ".Interface.P2PDevice",
5567                         "ExtendedListen", interface_p2p_listen_params,
5568                         NULL, &params, NULL);
5569 }
5570
5571 static void widi_ies_params(DBusMessageIter *iter, void *user_data)
5572 {
5573         struct p2p_service_data *data = user_data;
5574         GSupplicantP2PServiceParams *service = data->service;
5575         DBusMessageIter array;
5576
5577         SUPPLICANT_DBG("%p - %d", service->wfd_ies, service->wfd_ies_length);
5578
5579         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
5580                                         DBUS_TYPE_BYTE_AS_STRING, &array);
5581
5582         if (service->wfd_ies && service->wfd_ies_length > 0) {
5583                 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
5584                                 &service->wfd_ies, service->wfd_ies_length);
5585         }
5586
5587         dbus_message_iter_close_container(iter, &array);
5588 }
5589
5590 int g_supplicant_set_widi_ies(GSupplicantP2PServiceParams *p2p_service_params,
5591                                         GSupplicantInterfaceCallback callback,
5592                                         void *user_data)
5593 {
5594         struct p2p_service_data *data;
5595         int ret;
5596
5597         SUPPLICANT_DBG("");
5598
5599         if (!system_available)
5600                 return -EFAULT;
5601
5602         data = dbus_malloc0(sizeof(*data));
5603         data->service = p2p_service_params;
5604         data->callback = callback;
5605         data->user_data = user_data;
5606
5607         if (p2p_service_params->wfd_ies)
5608                 data->registration = true;
5609
5610         ret = supplicant_dbus_property_set(SUPPLICANT_PATH,
5611                                         SUPPLICANT_INTERFACE, "WFDIEs",
5612                                         DBUS_TYPE_ARRAY_AS_STRING
5613                                         DBUS_TYPE_BYTE_AS_STRING,
5614                                         widi_ies_params,
5615                                         interface_p2p_service_result,
5616                                         data, NULL);
5617         if (ret < 0 && ret != -EINPROGRESS) {
5618                 dbus_free(data);
5619                 return ret;
5620         }
5621
5622         return -EINPROGRESS;
5623 }
5624
5625 #if defined TIZEN_EXT
5626 int g_supplicant_interface_remove_network(GSupplicantInterface *interface)
5627 {
5628         struct interface_data *data;
5629
5630         SUPPLICANT_DBG("");
5631
5632         if (interface == NULL)
5633                 return -EINVAL;
5634
5635         if (system_available == FALSE)
5636                 return -EFAULT;
5637
5638         data = dbus_malloc0(sizeof(*data));
5639         if (data == NULL)
5640                 return -ENOMEM;
5641
5642         data->interface = interface;
5643
5644         return network_remove(data);
5645 }
5646 #endif
5647
5648 static const char *g_supplicant_rule0 = "type=signal,"
5649                                         "path=" DBUS_PATH_DBUS ","
5650                                         "sender=" DBUS_SERVICE_DBUS ","
5651                                         "interface=" DBUS_INTERFACE_DBUS ","
5652                                         "member=NameOwnerChanged,"
5653                                         "arg0=" SUPPLICANT_SERVICE;
5654 static const char *g_supplicant_rule1 = "type=signal,"
5655                         "interface=" SUPPLICANT_INTERFACE;
5656 static const char *g_supplicant_rule2 = "type=signal,"
5657                         "interface=" SUPPLICANT_INTERFACE ".Interface";
5658 static const char *g_supplicant_rule3 = "type=signal,"
5659                         "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
5660 static const char *g_supplicant_rule4 = "type=signal,"
5661                         "interface=" SUPPLICANT_INTERFACE ".BSS";
5662 static const char *g_supplicant_rule5 = "type=signal,"
5663                         "interface=" SUPPLICANT_INTERFACE ".Network";
5664 #if !defined TIZEN_EXT
5665 static const char *g_supplicant_rule6 = "type=signal,"
5666                 "interface=" SUPPLICANT_INTERFACE ".Interface.P2PDevice";
5667 static const char *g_supplicant_rule7 = "type=signal,"
5668                 "interface=" SUPPLICANT_INTERFACE ".Peer";
5669 static const char *g_supplicant_rule8 = "type=signal,"
5670                 "interface=" SUPPLICANT_INTERFACE ".Group";
5671 #endif
5672
5673 static void invoke_introspect_method(void)
5674 {
5675         DBusMessage *message;
5676
5677         message = dbus_message_new_method_call(SUPPLICANT_SERVICE,
5678                                         SUPPLICANT_PATH,
5679                                         DBUS_INTERFACE_INTROSPECTABLE,
5680                                         "Introspect");
5681
5682         if (!message)
5683                 return;
5684
5685         dbus_message_set_no_reply(message, TRUE);
5686         dbus_connection_send(connection, message, NULL);
5687         dbus_message_unref(message);
5688 }
5689
5690 int g_supplicant_register(const GSupplicantCallbacks *callbacks)
5691 {
5692         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
5693         if (!connection)
5694                 return -EIO;
5695
5696         if (!dbus_connection_add_filter(connection, g_supplicant_filter,
5697                                                 NULL, NULL)) {
5698                 dbus_connection_unref(connection);
5699                 connection = NULL;
5700                 return -EIO;
5701         }
5702
5703         callbacks_pointer = callbacks;
5704         eap_methods = 0;
5705
5706         interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
5707                                                 NULL, remove_interface);
5708
5709         bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
5710                                                                 NULL, NULL);
5711         peer_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
5712                                                                 NULL, NULL);
5713         group_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
5714                                                                 NULL, NULL);
5715         pending_peer_connection = g_hash_table_new_full(g_str_hash, g_str_equal,
5716                                                                 NULL, NULL);
5717
5718         supplicant_dbus_setup(connection);
5719
5720         dbus_bus_add_match(connection, g_supplicant_rule0, NULL);
5721         dbus_bus_add_match(connection, g_supplicant_rule1, NULL);
5722         dbus_bus_add_match(connection, g_supplicant_rule2, NULL);
5723         dbus_bus_add_match(connection, g_supplicant_rule3, NULL);
5724         dbus_bus_add_match(connection, g_supplicant_rule4, NULL);
5725         dbus_bus_add_match(connection, g_supplicant_rule5, NULL);
5726 #if defined TIZEN_EXT
5727         dbus_bus_add_match(connection,
5728                         "type=signal,interface=org.tizen.system.deviced.PowerOff,"
5729                         "member=ChangeState", NULL);
5730 #endif
5731 #if !defined TIZEN_EXT
5732         dbus_bus_add_match(connection, g_supplicant_rule6, NULL);
5733         dbus_bus_add_match(connection, g_supplicant_rule7, NULL);
5734         dbus_bus_add_match(connection, g_supplicant_rule8, NULL);
5735 #endif
5736         dbus_connection_flush(connection);
5737
5738         if (dbus_bus_name_has_owner(connection,
5739                                         SUPPLICANT_SERVICE, NULL)) {
5740                 system_available = TRUE;
5741                 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
5742                                                 SUPPLICANT_INTERFACE,
5743                                                 service_property, NULL, NULL);
5744         } else
5745                 invoke_introspect_method();
5746
5747         return 0;
5748 }
5749
5750 static void unregister_interface_remove_params(DBusMessageIter *iter,
5751                                                 void *user_data)
5752 {
5753         const char *path = user_data;
5754
5755         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
5756                                                         &path);
5757 }
5758
5759
5760 static void unregister_remove_interface(gpointer key, gpointer value,
5761                                                 gpointer user_data)
5762 {
5763         GSupplicantInterface *interface = value;
5764
5765         supplicant_dbus_method_call(SUPPLICANT_PATH,
5766                                         SUPPLICANT_INTERFACE,
5767                                         "RemoveInterface",
5768                                         unregister_interface_remove_params,
5769                                         NULL, interface->path, NULL);
5770 }
5771
5772 void g_supplicant_unregister(const GSupplicantCallbacks *callbacks)
5773 {
5774         SUPPLICANT_DBG("");
5775
5776         if (connection) {
5777 #if !defined TIZEN_EXT
5778                 dbus_bus_remove_match(connection, g_supplicant_rule8, NULL);
5779                 dbus_bus_remove_match(connection, g_supplicant_rule7, NULL);
5780                 dbus_bus_remove_match(connection, g_supplicant_rule6, NULL);
5781 #endif
5782                 dbus_bus_remove_match(connection, g_supplicant_rule5, NULL);
5783                 dbus_bus_remove_match(connection, g_supplicant_rule4, NULL);
5784                 dbus_bus_remove_match(connection, g_supplicant_rule3, NULL);
5785                 dbus_bus_remove_match(connection, g_supplicant_rule2, NULL);
5786                 dbus_bus_remove_match(connection, g_supplicant_rule1, NULL);
5787                 dbus_bus_remove_match(connection, g_supplicant_rule0, NULL);
5788                 dbus_connection_flush(connection);
5789
5790                 dbus_connection_remove_filter(connection,
5791                                                 g_supplicant_filter, NULL);
5792         }
5793
5794         if (bss_mapping) {
5795                 g_hash_table_destroy(bss_mapping);
5796                 bss_mapping = NULL;
5797         }
5798
5799         if (peer_mapping) {
5800                 g_hash_table_destroy(peer_mapping);
5801                 peer_mapping = NULL;
5802         }
5803
5804         if (group_mapping) {
5805                 g_hash_table_destroy(group_mapping);
5806                 group_mapping = NULL;
5807         }
5808
5809         if (interface_table) {
5810                 g_hash_table_foreach(interface_table,
5811                                         unregister_remove_interface, NULL);
5812                 g_hash_table_destroy(interface_table);
5813                 interface_table = NULL;
5814         }
5815
5816         if (system_available)
5817                 callback_system_killed();
5818
5819         if (connection) {
5820                 dbus_connection_unref(connection);
5821                 connection = NULL;
5822         }
5823
5824         callbacks_pointer = NULL;
5825         eap_methods = 0;
5826 }