Add method and signal for property handling
[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 void append_elements(DBusMessageIter *dict)
31 {
32         DBusMessageIter entry, value, iter;
33         const char *key = "Elements";
34
35         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
36                                                                 NULL, &entry);
37
38         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
39
40         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
41                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
42                                                                 &value);
43
44         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
45                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
46
47         __connman_element_list(CONNMAN_ELEMENT_TYPE_UNKNOWN, &iter);
48
49         dbus_message_iter_close_container(&value, &iter);
50
51         dbus_message_iter_close_container(&entry, &value);
52
53         dbus_message_iter_close_container(dict, &entry);
54 }
55
56 static DBusMessage *get_properties(DBusConnection *conn,
57                                         DBusMessage *msg, void *data)
58 {
59         DBusMessage *reply;
60         DBusMessageIter array, dict;
61
62         DBG("conn %p", conn);
63
64         reply = dbus_message_new_method_return(msg);
65         if (reply == NULL)
66                 return NULL;
67
68         dbus_message_iter_init_append(reply, &array);
69
70         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
71                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
72                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
73                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
74
75         append_elements(&dict);
76
77         dbus_message_iter_close_container(&array, &dict);
78
79         return reply;
80 }
81
82 static DBusMessage *register_agent(DBusConnection *conn,
83                                         DBusMessage *msg, void *data)
84 {
85         DBusMessage *reply;
86         const char *sender, *path;
87
88         DBG("conn %p", conn);
89
90         sender = dbus_message_get_sender(msg);
91
92         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
93                                                         DBUS_TYPE_INVALID);
94
95         reply = dbus_message_new_method_return(msg);
96         if (reply == NULL)
97                 return NULL;
98
99         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
100
101         __connman_agent_register(sender, path);
102
103         return reply;
104 }
105
106 static DBusMessage *unregister_agent(DBusConnection *conn,
107                                         DBusMessage *msg, void *data)
108 {
109         DBusMessage *reply;
110         const char *sender, *path;
111
112         DBG("conn %p", conn);
113
114         sender = dbus_message_get_sender(msg);
115
116         dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
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         __connman_agent_unregister(sender, path);
126
127         return reply;
128 }
129
130 static DBusMessage *list_profiles(DBusConnection *conn,
131                                         DBusMessage *msg, void *data)
132 {
133         DBusMessage *reply;
134         DBusMessageIter array, iter;
135
136         DBG("conn %p", conn);
137
138         reply = dbus_message_new_method_return(msg);
139         if (reply == NULL)
140                 return NULL;
141
142         dbus_message_iter_init_append(reply, &array);
143
144         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
145                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
146
147         __connman_profile_list(&iter);
148
149         dbus_message_iter_close_container(&array, &iter);
150
151         return reply;
152 }
153
154 static DBusMessage *list_elements(DBusConnection *conn,
155                                         DBusMessage *msg, void *data)
156 {
157         DBusMessage *reply;
158         DBusMessageIter array, iter;
159
160         DBG("conn %p", conn);
161
162         reply = dbus_message_new_method_return(msg);
163         if (reply == NULL)
164                 return NULL;
165
166         dbus_message_iter_init_append(reply, &array);
167
168         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
169                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
170
171         __connman_element_list(CONNMAN_ELEMENT_TYPE_UNKNOWN, &iter);
172
173         dbus_message_iter_close_container(&array, &iter);
174
175         return reply;
176 }
177
178 static GDBusMethodTable manager_methods[] = {
179         { "GetProperties",   "",  "a{sv}", get_properties   },
180         { "RegisterAgent",   "o", "",      register_agent   },
181         { "UnregisterAgent", "o", "",      unregister_agent },
182         { "ListProfiles",    "",  "ao",    list_profiles    },
183         { "ListElements",    "",  "ao",    list_elements    },
184         { },
185 };
186
187 static GDBusSignalTable manager_signals[] = {
188         { "PropertyChanged", "sv" },
189         { "ElementAdded",    "o"  },
190         { "ElementUpdated",  "o"  },
191         { "ElementRemoved",  "o"  },
192         { },
193 };
194
195 static DBusMessage *nm_sleep(DBusConnection *conn,
196                                         DBusMessage *msg, void *data)
197 {
198         DBusMessage *reply;
199
200         DBG("conn %p", conn);
201
202         reply = dbus_message_new_method_return(msg);
203         if (reply == NULL)
204                 return NULL;
205
206         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
207
208         return reply;
209 }
210
211 static DBusMessage *nm_wake(DBusConnection *conn,
212                                         DBusMessage *msg, void *data)
213 {
214         DBusMessage *reply;
215
216         DBG("conn %p", conn);
217
218         reply = dbus_message_new_method_return(msg);
219         if (reply == NULL)
220                 return NULL;
221
222         dbus_message_append_args(reply, DBUS_TYPE_INVALID);
223
224         return reply;
225 }
226
227 enum {
228         NM_STATE_UNKNOWN = 0,
229         NM_STATE_ASLEEP,
230         NM_STATE_CONNECTING,
231         NM_STATE_CONNECTED,
232         NM_STATE_DISCONNECTED
233 };
234
235 static DBusMessage *nm_state(DBusConnection *conn,
236                                         DBusMessage *msg, void *data)
237 {
238         DBusMessage *reply;
239         dbus_uint32_t state;
240
241         DBG("conn %p", conn);
242
243         reply = dbus_message_new_method_return(msg);
244         if (reply == NULL)
245                 return NULL;
246
247         state = NM_STATE_DISCONNECTED;
248
249         dbus_message_append_args(reply, DBUS_TYPE_UINT32, &state,
250                                                         DBUS_TYPE_INVALID);
251
252         return reply;
253 }
254
255 static GDBusMethodTable nm_methods[] = {
256         { "sleep", "",  "",   nm_sleep        },
257         { "wake",  "",  "",   nm_wake         },
258         { "state", "",  "u",  nm_state        },
259         { },
260 };
261
262 static DBusConnection *connection = NULL;
263 static gboolean nm_compat = FALSE;
264
265 int __connman_manager_init(DBusConnection *conn, gboolean compat)
266 {
267         DBG("conn %p", conn);
268
269         connection = dbus_connection_ref(conn);
270         if (connection == NULL)
271                 return -1;
272
273         g_dbus_register_interface(connection, CONNMAN_MANAGER_PATH,
274                                         CONNMAN_MANAGER_INTERFACE,
275                                         manager_methods,
276                                         manager_signals, NULL, NULL, NULL);
277
278         if (compat == TRUE) {
279                 g_dbus_register_interface(connection, NM_PATH, NM_INTERFACE,
280                                         nm_methods, NULL, NULL, NULL, NULL);
281
282                 nm_compat = TRUE;
283         }
284
285         return 0;
286 }
287
288 void __connman_manager_cleanup(void)
289 {
290         DBG("conn %p", connection);
291
292         if (nm_compat == TRUE) {
293                 g_dbus_unregister_interface(connection, NM_PATH, NM_INTERFACE);
294         }
295
296         g_dbus_unregister_interface(connection, CONNMAN_MANAGER_PATH,
297                                                 CONNMAN_MANAGER_INTERFACE);
298
299         dbus_connection_unref(connection);
300 }