Add method and signal for master state
[connman] / src / manager.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007  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 <gdbus.h>
27
28 #include "connman.h"
29
30 static const char *master_state = "unknown";
31
32 static DBusMessage *list_interfaces(DBusConnection *conn,
33                                         DBusMessage *msg, void *data)
34 {
35         DBusMessage *reply;
36         DBusMessageIter array, iter;
37
38         DBG("conn %p", conn);
39
40         reply = dbus_message_new_method_return(msg);
41         if (reply == NULL)
42                 return NULL;
43
44         dbus_message_iter_init_append(reply, &array);
45
46         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
47                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
48
49         __connman_iface_list(&iter);
50
51         dbus_message_iter_close_container(&array, &iter);
52
53         return reply;
54 }
55
56 static DBusMessage *get_state(DBusConnection *conn,
57                                         DBusMessage *msg, void *data)
58 {
59         DBusMessage *reply;
60
61         DBG("conn %p", conn);
62
63         reply = dbus_message_new_method_return(msg);
64         if (reply == NULL)
65                 return NULL;
66
67         dbus_message_append_args(reply, DBUS_TYPE_STRING, &master_state,
68                                                         DBUS_TYPE_INVALID);
69
70         return reply;
71 }
72
73 static GDBusMethodTable manager_methods[] = {
74         { "ListInterfaces", "", "ao", list_interfaces },
75         { "GetState",       "", "s",  get_state       },
76         { },
77 };
78
79 static GDBusSignalTable manager_signals[] = {
80         { "InterfaceAdded",   "o" },
81         { "InterfaceRemoved", "o" },
82         { "StateChanged",     "s" },
83         { },
84 };
85
86 static DBusMessage *activate_device(DBusConnection *conn,
87                                         DBusMessage *msg, void *data)
88 {
89         DBusMessage *reply;
90         const char *path;
91
92         DBG("conn %p", conn);
93
94         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
95                                                         DBUS_TYPE_INVALID);
96
97         DBG("device %s", path);
98
99         reply = dbus_message_new_method_return(msg);
100         if (reply == NULL)
101                 return NULL;
102
103         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
104
105         return reply;
106 }
107
108 static DBusMessage *set_wireless(DBusConnection *conn,
109                                         DBusMessage *msg, void *data)
110 {
111         DBusMessage *reply;
112         dbus_bool_t enabled;
113
114         DBG("conn %p", conn);
115
116         dbus_message_get_args(msg, NULL, DBUS_TYPE_BOOLEAN, &enabled,
117                                                         DBUS_TYPE_INVALID);
118
119         reply = dbus_message_new_method_return(msg);
120         if (reply == NULL)
121                 return NULL;
122
123         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
124
125         return reply;
126 }
127
128 static DBusMessage *get_wireless(DBusConnection *conn,
129                                         DBusMessage *msg, void *data)
130 {
131         DBusMessage *reply;
132         dbus_bool_t enabled = TRUE;
133
134         DBG("conn %p", conn);
135
136         reply = dbus_message_new_method_return(msg);
137         if (reply == NULL)
138                 return NULL;
139
140         dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &enabled,
141                                                         DBUS_TYPE_INVALID);
142
143         return reply;
144 }
145
146 static DBusMessage *do_sleep(DBusConnection *conn,
147                                         DBusMessage *msg, void *data)
148 {
149         DBusMessage *reply;
150
151         DBG("conn %p", conn);
152
153         reply = dbus_message_new_method_return(msg);
154         if (reply == NULL)
155                 return NULL;
156
157         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
158
159         return reply;
160 }
161
162 static DBusMessage *do_wake(DBusConnection *conn,
163                                         DBusMessage *msg, void *data)
164 {
165         DBusMessage *reply;
166
167         DBG("conn %p", conn);
168
169         reply = dbus_message_new_method_return(msg);
170         if (reply == NULL)
171                 return NULL;
172
173         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
174
175         return reply;
176 }
177
178 enum {
179         NM_STATE_UNKNOWN = 0,
180         NM_STATE_ASLEEP,
181         NM_STATE_CONNECTING,
182         NM_STATE_CONNECTED,
183         NM_STATE_DISCONNECTED
184 };
185
186 static DBusMessage *do_state(DBusConnection *conn,
187                                         DBusMessage *msg, void *data)
188 {
189         DBusMessage *reply;
190         dbus_uint32_t state = NM_STATE_DISCONNECTED;
191
192         DBG("conn %p", conn);
193
194         reply = dbus_message_new_method_return(msg);
195         if (reply == NULL)
196                 return NULL;
197
198         dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
199                                                         DBUS_TYPE_INVALID);
200
201         return reply;
202 }
203
204 static GDBusMethodTable nm_methods[] = {
205         { "getDevices",            "",  "ao", list_interfaces },
206         { "setActiveDevice",       "o", "",   activate_device },
207         { "setWirelessEnabled",    "b", "",   set_wireless    },
208         { "getWirelessEnabled",    "",  "b",  get_wireless    },
209         { "sleep",                 "",  "",   do_sleep        },
210         { "wake",                  "",  "",   do_wake         },
211         { "state",                 "",  "u",  do_state        },
212         { },
213 };
214
215 static DBusConnection *connection = NULL;
216 static int nm_compat = 0;
217
218 int __connman_manager_init(DBusConnection *conn, int compat)
219 {
220         DBG("conn %p", conn);
221
222         connection = dbus_connection_ref(conn);
223         if (connection == NULL)
224                 return -1;
225
226         g_dbus_register_object(connection, CONNMAN_MANAGER_PATH, NULL, NULL);
227
228         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
229                                                 CONNMAN_MANAGER_INTERFACE,
230                                                 manager_methods,
231                                                 manager_signals, NULL);
232
233         if (compat) {
234                 g_dbus_register_object(connection, NM_PATH, NULL, NULL);
235
236                 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
237                                                 nm_methods, NULL, NULL);
238
239                 nm_compat = 1;
240         }
241
242         return 0;
243 }
244
245 void __connman_manager_cleanup(void)
246 {
247         DBG("conn %p", connection);
248
249         if (nm_compat) {
250                 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
251
252                 g_dbus_unregister_object(connection, NM_PATH);
253         }
254
255         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
256                                                 CONNMAN_MANAGER_INTERFACE);
257
258         g_dbus_unregister_object(connection, CONNMAN_MANAGER_PATH);
259
260         dbus_connection_unref(connection);
261 }