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