Add initial support for service properties
[connman] / src / profile.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 <glib.h>
27 #include <gdbus.h>
28
29 #include "connman.h"
30
31 #define PROFILE_DEFAULT  "/profile/default"
32
33 struct connman_group {
34         char *type;
35         char *path;
36         GSList *networks;
37 };
38
39 static GHashTable *groups = NULL;
40
41 static DBusConnection *connection = NULL;
42
43 static DBusMessage *get_properties(DBusConnection *conn,
44                                         DBusMessage *msg, void *data)
45 {
46         struct connman_group *group = data;
47         DBusMessage *reply;
48         DBusMessageIter array, dict;
49
50         DBG("conn %p", conn);
51
52         reply = dbus_message_new_method_return(msg);
53         if (reply == NULL)
54                 return NULL;
55
56         dbus_message_iter_init_append(reply, &array);
57
58         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
59                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
60                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
61                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
62
63         if (group->type != NULL)
64                 connman_dbus_dict_append_variant(&dict, "Type",
65                                         DBUS_TYPE_STRING, &group->type);
66
67         dbus_message_iter_close_container(&array, &dict);
68
69         return reply;
70 }
71
72 static GDBusMethodTable service_methods[] = {
73         { "GetProperties", "", "a{sv}", get_properties },
74         { },
75 };
76
77 static void free_group(gpointer data)
78 {
79         struct connman_group *group = data;
80
81         DBG("group %p", group);
82
83         g_dbus_unregister_interface(connection, group->path,
84                                                 CONNMAN_SERVICE_INTERFACE);
85
86         g_free(group->type);
87         g_free(group->path);
88         g_free(group);
89 }
90
91 static struct connman_group *lookup_group(const char *name)
92 {
93         struct connman_group *group;
94
95         DBG("name %s", name);
96
97         if (name == NULL)
98                 return NULL;
99
100         group = g_hash_table_lookup(groups, name);
101         if (group != NULL)
102                 goto done;
103
104         group = g_try_new0(struct connman_group, 1);
105         if (group == NULL)
106                 return NULL;
107
108         group->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
109         group->path = g_strdup_printf("%s/%s", PROFILE_DEFAULT, name);
110
111         g_hash_table_insert(groups, g_strdup(name), group);
112
113         g_dbus_register_interface(connection, group->path,
114                                                 CONNMAN_SERVICE_INTERFACE,
115                                                 service_methods,
116                                                 NULL, NULL, group, NULL);
117
118 done:
119         DBG("group %p", group);
120
121         return group;
122 }
123
124 int __connman_profile_add_device(struct connman_device *device)
125 {
126         struct connman_group *group;
127         char *name;
128
129         DBG("device %p", device);
130
131         name = g_strdup_printf("device%d", connman_device_get_index(device));
132         group = lookup_group(name);
133         g_free(name);
134
135         if (group == NULL)
136                 return -EINVAL;
137
138         group->type = g_strdup(__connman_device_get_type(device));
139
140         return 0;
141 }
142
143 int __connman_profile_remove_device(struct connman_device *device)
144 {
145         struct connman_group *group;
146         char *name;
147
148         DBG("device %p", device);
149
150         name = g_strdup_printf("device%d", connman_device_get_index(device));
151         group = lookup_group(name);
152         g_free(name);
153
154         if (group == NULL)
155                 return -EINVAL;
156
157         return 0;
158 }
159
160 int __connman_profile_add_network(struct connman_network *network)
161 {
162         struct connman_group *group;
163
164         DBG("network %p", network);
165
166         group = lookup_group(__connman_network_get_group(network));
167         if (group == NULL)
168                 return -EINVAL;
169
170         g_free(group->type);
171
172         group->type = g_strdup(__connman_network_get_type(network));
173
174         return 0;
175 }
176
177 int __connman_profile_remove_network(struct connman_network *network)
178 {
179         struct connman_group *group;
180
181         DBG("network %p", network);
182
183         group = lookup_group(__connman_network_get_group(network));
184         if (group == NULL)
185                 return -EINVAL;
186
187         return 0;
188 }
189
190 const char *__connman_profile_active(void)
191 {
192         DBG("");
193
194         return PROFILE_DEFAULT;
195 }
196
197 void __connman_profile_list(DBusMessageIter *iter)
198 {
199         const char *path = __connman_profile_active();
200
201         DBG("");
202
203         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
204 }
205
206 static void append_path(gpointer key, gpointer value, gpointer user_data)
207 {
208         struct connman_group *group = value;
209         DBusMessageIter *iter = user_data;
210
211         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
212                                                         &group->path);
213 }
214
215 void __connman_profile_list_services(DBusMessageIter *iter)
216 {
217         DBG("");
218
219         g_hash_table_foreach(groups, append_path, iter);
220 }
221
222 static void append_services(DBusMessageIter *dict)
223 {
224         DBusMessageIter entry, value, iter;
225         const char *key = "Services";
226
227         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
228                                                                 NULL, &entry);
229
230         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
231
232         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
233                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
234                                                                 &value);
235
236         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
237                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
238         __connman_profile_list_services(&iter);
239         dbus_message_iter_close_container(&value, &iter);
240
241         dbus_message_iter_close_container(&entry, &value);
242
243         dbus_message_iter_close_container(dict, &entry);
244 }
245
246 static DBusMessage *profile_properties(DBusConnection *conn,
247                                         DBusMessage *msg, void *data)
248 {
249         const char *name = "Default";
250         DBusMessage *reply;
251         DBusMessageIter array, dict;
252
253         DBG("conn %p", conn);
254
255         reply = dbus_message_new_method_return(msg);
256         if (reply == NULL)
257                 return NULL;
258
259         dbus_message_iter_init_append(reply, &array);
260
261         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
262                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
263                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
264                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
265
266         connman_dbus_dict_append_variant(&dict, "Name",
267                                                 DBUS_TYPE_STRING, &name);
268
269         append_services(&dict);
270
271         dbus_message_iter_close_container(&array, &dict);
272
273         return reply;
274 }
275
276 static GDBusMethodTable profile_methods[] = {
277         { "GetProperties", "", "a{sv}", profile_properties },
278         { },
279 };
280
281 int __connman_profile_init(DBusConnection *conn)
282 {
283         DBG("conn %p", conn);
284
285         connection = dbus_connection_ref(conn);
286         if (connection == NULL)
287                 return -1;
288
289         groups = g_hash_table_new_full(g_str_hash, g_str_equal,
290                                                         g_free, free_group);
291
292         g_dbus_register_interface(connection, PROFILE_DEFAULT,
293                                                 CONNMAN_PROFILE_INTERFACE,
294                                                 profile_methods,
295                                                 NULL, NULL, NULL, NULL);
296
297         return 0;
298 }
299
300 void __connman_profile_cleanup(void)
301 {
302         DBG("conn %p", connection);
303
304         g_dbus_unregister_interface(connection, PROFILE_DEFAULT,
305                                                 CONNMAN_PROFILE_INTERFACE);
306
307         g_hash_table_destroy(groups);
308         groups = NULL;
309
310         if (connection == NULL)
311                 return;
312
313         dbus_connection_unref(connection);
314 }