Add initial skeleton for profile support
[connman] / 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_profiles(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_profile_list(&iter);
96
97         dbus_message_iter_close_container(&array, &iter);
98
99         return reply;
100 }
101
102 static DBusMessage *list_elements(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_UNKNOWN, &iter);
120
121         dbus_message_iter_close_container(&array, &iter);
122
123         return reply;
124 }
125
126 static DBusMessage *list_devices(DBusConnection *conn,
127                                         DBusMessage *msg, void *data)
128 {
129         DBusMessage *reply;
130         DBusMessageIter array, iter;
131
132         DBG("conn %p", conn);
133
134         reply = dbus_message_new_method_return(msg);
135         if (reply == NULL)
136                 return NULL;
137
138         dbus_message_iter_init_append(reply, &array);
139
140         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
141                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
142
143         __connman_element_list(CONNMAN_ELEMENT_TYPE_DEVICE, &iter);
144
145         dbus_message_iter_close_container(&array, &iter);
146
147         return reply;
148 }
149
150 static GDBusMethodTable manager_methods[] = {
151         { "RegisterAgent",   "o", "", register_agent   },
152         { "UnregisterAgent", "o", "", unregister_agent },
153
154         { "ListProfiles", "", "ao", list_profiles },
155
156         { "ListElements", "", "ao", list_elements },
157         { "ListDevices",  "", "ao", list_devices  },
158         { },
159 };
160
161 static GDBusSignalTable manager_signals[] = {
162         { "ElementAdded",   "o" },
163         { "ElementUpdated", "o" },
164         { "ElementRemoved", "o" },
165         { "DeviceAdded",    "o" },
166         { "DeviceRemoved",  "o" },
167         { },
168 };
169
170 static DBusMessage *nm_sleep(DBusConnection *conn,
171                                         DBusMessage *msg, void *data)
172 {
173         DBusMessage *reply;
174
175         DBG("conn %p", conn);
176
177         reply = dbus_message_new_method_return(msg);
178         if (reply == NULL)
179                 return NULL;
180
181         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
182
183         return reply;
184 }
185
186 static DBusMessage *nm_wake(DBusConnection *conn,
187                                         DBusMessage *msg, void *data)
188 {
189         DBusMessage *reply;
190
191         DBG("conn %p", conn);
192
193         reply = dbus_message_new_method_return(msg);
194         if (reply == NULL)
195                 return NULL;
196
197         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
198
199         return reply;
200 }
201
202 enum {
203         NM_STATE_UNKNOWN = 0,
204         NM_STATE_ASLEEP,
205         NM_STATE_CONNECTING,
206         NM_STATE_CONNECTED,
207         NM_STATE_DISCONNECTED
208 };
209
210 static DBusMessage *nm_state(DBusConnection *conn,
211                                         DBusMessage *msg, void *data)
212 {
213         DBusMessage *reply;
214         dbus_uint32_t state;
215
216         DBG("conn %p", conn);
217
218         reply = dbus_message_new_method_return(msg);
219         if (reply == NULL)
220                 return NULL;
221
222         state = NM_STATE_DISCONNECTED;
223
224         dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
225                                                         DBUS_TYPE_INVALID);
226
227         return reply;
228 }
229
230 static GDBusMethodTable nm_methods[] = {
231         { "sleep", "",  "",   nm_sleep        },
232         { "wake",  "",  "",   nm_wake         },
233         { "state", "",  "u",  nm_state        },
234         { },
235 };
236
237 static DBusConnection *connection = NULL;
238 static gboolean nm_compat = FALSE;
239
240 int __connman_manager_init(DBusConnection *conn, gboolean compat)
241 {
242         DBG("conn %p", conn);
243
244         connection = dbus_connection_ref(conn);
245         if (connection == NULL)
246                 return -1;
247
248         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
249                                         CONNMAN_MANAGER_INTERFACE,
250                                         manager_methods,
251                                         manager_signals, NULL, NULL, NULL);
252
253         if (compat == TRUE) {
254                 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
255                                         nm_methods, NULL, NULL, NULL, NULL);
256
257                 nm_compat = TRUE;
258         }
259
260         return 0;
261 }
262
263 void __connman_manager_cleanup(void)
264 {
265         DBG("conn %p", connection);
266
267         if (nm_compat == TRUE) {
268                 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
269         }
270
271         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
272                                                 CONNMAN_MANAGER_INTERFACE);
273
274         dbus_connection_unref(connection);
275 }