Add interface creation function to supplicant test program
[framework/connectivity/connman.git] / tools / supplicant-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  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
50 static void system_ready(void)
51 {
52         DBG("*");
53
54         supplicant_interface_create("wlan0", "nl80211",
55                                                 create_callback, NULL);
56 }
57
58 static void system_killed(void)
59 {
60         DBG("*");
61 }
62
63 static void interface_added(struct supplicant_interface *interface)
64 {
65         const char *ifname = supplicant_interface_get_ifname(interface);
66
67         DBG("* ifname %s", ifname);
68 }
69
70 static void interface_removed(struct supplicant_interface *interface)
71 {
72         const char *ifname = supplicant_interface_get_ifname(interface);
73
74         DBG("* ifname %s", ifname);
75 }
76
77 static void network_added(struct supplicant_network *network)
78 {
79         const char *name = supplicant_network_get_name(network);
80
81         DBG("* name %s", name);
82
83         DBG("* %s", supplicant_network_get_identifier(network));
84 }
85
86 static void network_removed(struct supplicant_network *network)
87 {
88         const char *name = supplicant_network_get_name(network);
89
90         DBG("* name %s", name);
91 }
92
93 static const struct supplicant_callbacks callbacks = {
94         .system_ready           = system_ready,
95         .system_killed          = system_killed,
96         .interface_added        = interface_added,
97         .interface_removed      = interface_removed,
98         .network_added          = network_added,
99         .network_removed        = network_removed,
100 };
101
102 static GMainLoop *main_loop = NULL;
103
104 static void sig_term(int sig)
105 {
106         syslog(LOG_INFO, "Terminating");
107
108         g_main_loop_quit(main_loop);
109 }
110
111 static void disconnect_callback(DBusConnection *conn, void *user_data)
112 {
113         syslog(LOG_ERR, "D-Bus disconnect");
114
115         g_main_loop_quit(main_loop);
116 }
117
118 int main(int argc, char *argv[])
119 {
120         DBusConnection *conn;
121         DBusError err;
122         struct sigaction sa;
123
124         main_loop = g_main_loop_new(NULL, FALSE);
125
126         dbus_error_init(&err);
127
128         conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, &err);
129         if (conn == NULL) {
130                 if (dbus_error_is_set(&err) == TRUE) {
131                         fprintf(stderr, "%s\n", err.message);
132                         dbus_error_free(&err);
133                 } else
134                         fprintf(stderr, "Can't register with system bus\n");
135                 exit(1);
136         }
137
138         openlog("supplicant", LOG_NDELAY | LOG_PERROR, LOG_USER);
139
140         g_dbus_set_disconnect_function(conn, disconnect_callback, NULL, NULL);
141
142         memset(&sa, 0, sizeof(sa));
143         sa.sa_handler = sig_term;
144         sigaction(SIGINT, &sa, NULL);
145         sigaction(SIGTERM, &sa, NULL);
146
147         syslog(LOG_INFO, "Startup");
148
149         if (supplicant_register(&callbacks) < 0) {
150                 syslog(LOG_ERR, "Failed to init supplicant");
151                 goto done;
152         }
153
154         g_main_loop_run(main_loop);
155
156         supplicant_unregister(&callbacks);
157
158 done:
159         syslog(LOG_INFO, "Exit");
160
161         dbus_connection_unref(conn);
162
163         g_main_loop_unref(main_loop);
164
165         closelog();
166
167         return 0;
168 }