DA: Skip initializing failed_bssids list when eapol failure case
[platform/upstream/connman.git] / tools / supplicant-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  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 <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <signal.h>
30 #include <syslog.h>
31
32 #include <gdbus.h>
33
34 #include "supplicant.h"
35
36 #define DBG(fmt, arg...) do { \
37         syslog(LOG_DEBUG, "%s() " fmt, __FUNCTION__ , ## arg); \
38 } while (0)
39
40 static void create_callback(int result, struct supplicant_interface *interface,
41                                                         void *user_data)
42 {
43         DBG("* result %d ifname %s", result,
44                                 supplicant_interface_get_ifname(interface));
45
46         if (result < 0)
47                 return;
48
49         //supplicant_set_debug_level(1);
50 }
51
52 static void system_ready(void)
53 {
54         DBG("*");
55
56         supplicant_interface_create("wlan0", "nl80211,wext",
57                                                 create_callback, NULL);
58 }
59
60 static void system_killed(void)
61 {
62         DBG("*");
63 }
64
65 static void scan_callback(int result, void *user_data)
66 {
67         DBG("* result %d", result);
68
69         if (result < 0)
70                 return;
71 }
72
73 static void interface_added(struct supplicant_interface *interface)
74 {
75         const char *ifname = supplicant_interface_get_ifname(interface);
76         const char *driver = supplicant_interface_get_driver(interface);
77
78         DBG("* ifname %s driver %s", ifname, driver);
79
80         if (supplicant_interface_scan(interface, scan_callback, NULL) < 0)
81                 DBG("scan failed");
82 }
83
84 static void interface_removed(struct supplicant_interface *interface)
85 {
86         const char *ifname = supplicant_interface_get_ifname(interface);
87
88         DBG("* ifname %s", ifname);
89 }
90
91 static void scan_started(struct supplicant_interface *interface)
92 {
93         const char *ifname = supplicant_interface_get_ifname(interface);
94
95         DBG("* ifname %s", ifname);
96 }
97
98 static void scan_finished(struct supplicant_interface *interface)
99 {
100         const char *ifname = supplicant_interface_get_ifname(interface);
101
102         DBG("* ifname %s", ifname);
103 }
104
105 static void network_added(struct supplicant_network *network)
106 {
107         const char *name = supplicant_network_get_name(network);
108
109         DBG("* name %s", name);
110
111         DBG("* %s", supplicant_network_get_identifier(network));
112 }
113
114 static void network_removed(struct supplicant_network *network)
115 {
116         const char *name = supplicant_network_get_name(network);
117
118         DBG("* name %s", name);
119 }
120
121 static const struct supplicant_callbacks callbacks = {
122         .system_ready           = system_ready,
123         .system_killed          = system_killed,
124         .interface_added        = interface_added,
125         .interface_removed      = interface_removed,
126         .scan_started           = scan_started,
127         .scan_finished          = scan_finished,
128         .network_added          = network_added,
129         .network_removed        = network_removed,
130 };
131
132 static GMainLoop *main_loop = NULL;
133
134 static void sig_term(int sig)
135 {
136         syslog(LOG_INFO, "Terminating");
137
138         g_main_loop_quit(main_loop);
139 }
140
141 static void disconnect_callback(DBusConnection *conn, void *user_data)
142 {
143         syslog(LOG_ERR, "D-Bus disconnect");
144
145         g_main_loop_quit(main_loop);
146 }
147
148 int main(int argc, char *argv[])
149 {
150         DBusConnection *conn;
151         DBusError err;
152         struct sigaction sa;
153
154         main_loop = g_main_loop_new(NULL, FALSE);
155
156         dbus_error_init(&err);
157
158         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
159         if (!conn) {
160                 if (dbus_error_is_set(&err)) {
161                         fprintf(stderr, "%s\n", err.message);
162                         dbus_error_free(&err);
163                 } else
164                         fprintf(stderr, "Can't register with system bus\n");
165                 exit(1);
166         }
167
168         openlog("supplicant", LOG_NDELAY | LOG_PERROR, LOG_USER);
169
170         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
171
172         memset(&sa, 0, sizeof(sa));
173         sa.sa_handler = sig_term;
174         sigaction(SIGINT, &sa, NULL);
175         sigaction(SIGTERM, &sa, NULL);
176
177         syslog(LOG_INFO, "Startup");
178
179         if (supplicant_register(&callbacks) < 0) {
180                 syslog(LOG_ERR, "Failed to init supplicant");
181                 goto done;
182         }
183
184         g_main_loop_run(main_loop);
185
186         supplicant_unregister(&callbacks);
187
188 done:
189         syslog(LOG_INFO, "Exit");
190
191         dbus_connection_unref(conn);
192
193         g_main_loop_unref(main_loop);
194
195         closelog();
196
197         return 0;
198 }