Add first draft of new D-Bus API
[platform/upstream/connman.git] / src / manager.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  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 DBusMessage *register_agent(DBusConnection *conn,
31                                         DBusMessage *msg, void *data)
32 {
33         DBusMessage *reply;
34         const char *sender, *path;
35
36         DBG("conn %p", conn);
37
38         sender = dbus_message_get_sender(msg);
39
40         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
41                                                         DBUS_TYPE_INVALID);
42
43         reply = dbus_message_new_method_return(msg);
44         if (reply == NULL)
45                 return NULL;
46
47         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
48
49         __connman_agent_register(sender, path);
50
51         return reply;
52 }
53
54 static DBusMessage *unregister_agent(DBusConnection *conn,
55                                         DBusMessage *msg, void *data)
56 {
57         DBusMessage *reply;
58         const char *sender, *path;
59
60         DBG("conn %p", conn);
61
62         sender = dbus_message_get_sender(msg);
63
64         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
65                                                         DBUS_TYPE_INVALID);
66
67         reply = dbus_message_new_method_return(msg);
68         if (reply == NULL)
69                 return NULL;
70
71         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
72
73         __connman_agent_unregister(sender, path);
74
75         return reply;
76 }
77
78 static DBusMessage *list_elements(DBusConnection *conn,
79                                         DBusMessage *msg, void *data)
80 {
81         DBusMessage *reply;
82         DBusMessageIter array, iter;
83
84         DBG("conn %p", conn);
85
86         reply = dbus_message_new_method_return(msg);
87         if (reply == NULL)
88                 return NULL;
89
90         dbus_message_iter_init_append(reply, &array);
91
92         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
93                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
94
95         __connman_element_list(CONNMAN_ELEMENT_TYPE_UNKNOWN, &iter);
96
97         dbus_message_iter_close_container(&array, &iter);
98
99         return reply;
100 }
101
102 static DBusMessage *list_devices(DBusConnection *conn,
103                                         DBusMessage *msg, void *data)
104 {
105         DBusMessage *reply;
106         DBusMessageIter array, iter;
107
108         DBG("conn %p", conn);
109
110         reply = dbus_message_new_method_return(msg);
111         if (reply == NULL)
112                 return NULL;
113
114         dbus_message_iter_init_append(reply, &array);
115
116         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
117                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
118
119         __connman_element_list(CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
120
121         dbus_message_iter_close_container(&array, &iter);
122
123         return reply;
124 }
125
126 static GDBusMethodTable manager_methods[] = {
127         { "RegisterAgent",   "o", "", register_agent   },
128         { "UnregisterAgent", "o", "", unregister_agent },
129
130         { "ListElements", "", "ao", list_elements },
131         { "ListDevices",  "", "ao", list_devices  },
132         { },
133 };
134
135 static GDBusSignalTable manager_signals[] = {
136         { "ElementAdded",   "o" },
137         { "ElementUpdated", "o" },
138         { "ElementRemoved", "o" },
139         { "DeviceAdded",    "o" },
140         { "DeviceRemoved",  "o" },
141         { },
142 };
143
144 static DBusMessage *nm_sleep(DBusConnection *conn,
145                                         DBusMessage *msg, void *data)
146 {
147         DBusMessage *reply;
148
149         DBG("conn %p", conn);
150
151         reply = dbus_message_new_method_return(msg);
152         if (reply == NULL)
153                 return NULL;
154
155         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
156
157         return reply;
158 }
159
160 static DBusMessage *nm_wake(DBusConnection *conn,
161                                         DBusMessage *msg, void *data)
162 {
163         DBusMessage *reply;
164
165         DBG("conn %p", conn);
166
167         reply = dbus_message_new_method_return(msg);
168         if (reply == NULL)
169                 return NULL;
170
171         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
172
173         return reply;
174 }
175
176 enum {
177         NM_STATE_UNKNOWN = 0,
178         NM_STATE_ASLEEP,
179         NM_STATE_CONNECTING,
180         NM_STATE_CONNECTED,
181         NM_STATE_DISCONNECTED
182 };
183
184 static DBusMessage *nm_state(DBusConnection *conn,
185                                         DBusMessage *msg, void *data)
186 {
187         DBusMessage *reply;
188         dbus_uint32_t state;
189
190         DBG("conn %p", conn);
191
192         reply = dbus_message_new_method_return(msg);
193         if (reply == NULL)
194                 return NULL;
195
196         state = NM_STATE_DISCONNECTED;
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         { "sleep", "",  "",   nm_sleep        },
206         { "wake",  "",  "",   nm_wake         },
207         { "state", "",  "u",  nm_state        },
208         { },
209 };
210
211 static DBusConnection *connection = NULL;
212 static gboolean nm_compat = FALSE;
213
214 int __connman_manager_init(DBusConnection *conn, gboolean compat)
215 {
216         DBG("conn %p", conn);
217
218         connection = dbus_connection_ref(conn);
219         if (connection == NULL)
220                 return -1;
221
222         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
223                                         CONNMAN_MANAGER_INTERFACE,
224                                         manager_methods,
225                                         manager_signals, NULL, NULL, NULL);
226
227         if (compat == TRUE) {
228                 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
229                                         nm_methods, NULL, NULL, NULL, NULL);
230
231                 nm_compat = TRUE;
232         }
233
234         return 0;
235 }
236
237 void __connman_manager_cleanup(void)
238 {
239         DBG("conn %p", connection);
240
241         if (nm_compat == TRUE) {
242                 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
243         }
244
245         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
246                                                 CONNMAN_MANAGER_INTERFACE);
247
248         dbus_connection_unref(connection);
249 }