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