b2d9bedb9310a9698b0d47e0bcbcc7ef86187207
[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         char *mode;
38         char *security;
39         connman_uint8_t strength;
40         connman_bool_t favorite;
41         struct connman_network *network;
42 };
43
44 static GHashTable *groups = NULL;
45
46 static DBusConnection *connection = NULL;
47
48 static DBusMessage *get_properties(DBusConnection *conn,
49                                         DBusMessage *msg, void *data)
50 {
51         struct connman_group *group = data;
52         DBusMessage *reply;
53         DBusMessageIter array, dict;
54
55         DBG("conn %p", conn);
56
57         reply = dbus_message_new_method_return(msg);
58         if (reply == NULL)
59                 return NULL;
60
61         dbus_message_iter_init_append(reply, &array);
62
63         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
64                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
65                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
66                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
67
68         if (group->type != NULL)
69                 connman_dbus_dict_append_variant(&dict, "Type",
70                                         DBUS_TYPE_STRING, &group->type);
71
72         if (group->name != NULL)
73                 connman_dbus_dict_append_variant(&dict, "Name",
74                                         DBUS_TYPE_STRING, &group->name);
75
76         if (group->mode != NULL)
77                 connman_dbus_dict_append_variant(&dict, "Mode",
78                                         DBUS_TYPE_STRING, &group->mode);
79
80         if (group->security != NULL)
81                 connman_dbus_dict_append_variant(&dict, "Security",
82                                         DBUS_TYPE_STRING, &group->security);
83
84         if (group->strength > 0)
85                 connman_dbus_dict_append_variant(&dict, "Strength",
86                                         DBUS_TYPE_BYTE, &group->strength);
87
88         connman_dbus_dict_append_variant(&dict, "Favorite",
89                                         DBUS_TYPE_BOOLEAN, &group->favorite);
90
91         dbus_message_iter_close_container(&array, &dict);
92
93         return reply;
94 }
95
96 static DBusMessage *connect_service(DBusConnection *conn,
97                                         DBusMessage *msg, void *data)
98 {
99         return __connman_error_not_implemented(msg);
100 }
101
102 static DBusMessage *disconnect_service(DBusConnection *conn,
103                                         DBusMessage *msg, void *data)
104 {
105         return __connman_error_not_implemented(msg);
106 }
107
108 static DBusMessage *remove_service(DBusConnection *conn,
109                                         DBusMessage *msg, void *data)
110 {
111         return __connman_error_not_implemented(msg);
112 }
113
114 static DBusMessage *move_before(DBusConnection *conn,
115                                         DBusMessage *msg, void *data)
116 {
117         return __connman_error_not_implemented(msg);
118 }
119
120 static DBusMessage *move_after(DBusConnection *conn,
121                                         DBusMessage *msg, void *data)
122 {
123         return __connman_error_not_implemented(msg);
124 }
125
126 static GDBusMethodTable service_methods[] = {
127         { "GetProperties", "",  "a{sv}", get_properties     },
128         { "Connect",       "",  "",      connect_service    },
129         { "Disconnect",    "",  "",      disconnect_service },
130         { "Remove",        "",  "",      remove_service     },
131         { "MoveBefore",    "o", "",      move_before        },
132         { "MoveAfter",     "o", "",      move_after         },
133         { },
134 };
135
136 static GDBusSignalTable service_signals[] = {
137         { "PropertyChanged", "sv" },
138         { },
139 };
140
141 static void free_group(gpointer data)
142 {
143         struct connman_group *group = data;
144
145         DBG("group %p", group);
146
147         g_dbus_unregister_interface(connection, group->path,
148                                                 CONNMAN_SERVICE_INTERFACE);
149
150         g_free(group->security);
151         g_free(group->mode);
152         g_free(group->name);
153         g_free(group->type);
154         g_free(group->path);
155         g_free(group);
156 }
157
158 static struct connman_group *lookup_group(const char *name)
159 {
160         struct connman_group *group;
161
162         DBG("name %s", name);
163
164         if (name == NULL)
165                 return NULL;
166
167         group = g_hash_table_lookup(groups, name);
168         if (group != NULL)
169                 goto done;
170
171         group = g_try_new0(struct connman_group, 1);
172         if (group == NULL)
173                 return NULL;
174
175         group->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
176         group->path = g_strdup_printf("%s/%s", PROFILE_DEFAULT, name);
177
178         group->favorite = FALSE;
179
180         g_hash_table_insert(groups, g_strdup(name), group);
181
182         g_dbus_register_interface(connection, group->path,
183                                         CONNMAN_SERVICE_INTERFACE,
184                                         service_methods, service_signals,
185                                                         NULL, group, NULL);
186
187 done:
188         DBG("group %p", group);
189
190         return group;
191 }
192
193 int __connman_profile_add_device(struct connman_device *device)
194 {
195         struct connman_group *group;
196         char *name;
197
198         DBG("device %p", device);
199
200         name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
201                                         connman_device_get_index(device));
202         group = lookup_group(name);
203         g_free(name);
204
205         if (group == NULL)
206                 return -EINVAL;
207
208         group->type = g_strdup(__connman_device_get_type(device));
209
210         return 0;
211 }
212
213 int __connman_profile_remove_device(struct connman_device *device)
214 {
215         struct connman_group *group;
216         char *name;
217
218         DBG("device %p", device);
219
220         name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
221                                         connman_device_get_index(device));
222         group = lookup_group(name);
223         g_free(name);
224
225         if (group == NULL)
226                 return -EINVAL;
227
228         return 0;
229 }
230
231 int __connman_profile_add_network(struct connman_network *network)
232 {
233         struct connman_group *group;
234
235         DBG("network %p", network);
236
237         group = lookup_group(__connman_network_get_group(network));
238         if (group == NULL)
239                 return -EINVAL;
240
241         g_free(group->type);
242         g_free(group->name);
243
244         group->type = g_strdup(__connman_network_get_type(network));
245         group->name = g_strdup(connman_network_get_string(network, "Name"));
246
247         group->strength = connman_network_get_uint8(network, "Strength");
248
249         if (group->network == NULL) {
250                 group->network = network;
251
252                 group->mode = g_strdup(connman_network_get_string(network,
253                                                                 "WiFi.Mode"));
254                 group->security = g_strdup(connman_network_get_string(network,
255                                                         "WiFi.Security"));
256         }
257
258         return 0;
259 }
260
261 int __connman_profile_remove_network(struct connman_network *network)
262 {
263         struct connman_group *group;
264
265         DBG("network %p", network);
266
267         group = lookup_group(__connman_network_get_group(network));
268         if (group == NULL)
269                 return -EINVAL;
270
271         if (group->network == network) {
272                 g_free(group->security);
273                 group->security = NULL;
274
275                 g_free(group->mode);
276                 group->mode = NULL;
277
278                 group->network = NULL;
279         }
280
281         return 0;
282 }
283
284 const char *__connman_profile_active(void)
285 {
286         DBG("");
287
288         return PROFILE_DEFAULT;
289 }
290
291 void __connman_profile_list(DBusMessageIter *iter)
292 {
293         const char *path = __connman_profile_active();
294
295         DBG("");
296
297         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
298 }
299
300 static void append_path(gpointer key, gpointer value, gpointer user_data)
301 {
302         struct connman_group *group = value;
303         DBusMessageIter *iter = user_data;
304
305         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
306                                                         &group->path);
307 }
308
309 void __connman_profile_list_services(DBusMessageIter *iter)
310 {
311         DBG("");
312
313         g_hash_table_foreach(groups, append_path, iter);
314 }
315
316 static void append_services(DBusMessageIter *dict)
317 {
318         DBusMessageIter entry, value, iter;
319         const char *key = "Services";
320
321         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
322                                                                 NULL, &entry);
323
324         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
325
326         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
327                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
328                                                                 &value);
329
330         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
331                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
332         __connman_profile_list_services(&iter);
333         dbus_message_iter_close_container(&value, &iter);
334
335         dbus_message_iter_close_container(&entry, &value);
336
337         dbus_message_iter_close_container(dict, &entry);
338 }
339
340 static DBusMessage *profile_properties(DBusConnection *conn,
341                                         DBusMessage *msg, void *data)
342 {
343         const char *name = "Default";
344         DBusMessage *reply;
345         DBusMessageIter array, dict;
346
347         DBG("conn %p", conn);
348
349         reply = dbus_message_new_method_return(msg);
350         if (reply == NULL)
351                 return NULL;
352
353         dbus_message_iter_init_append(reply, &array);
354
355         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
356                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
357                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
358                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
359
360         connman_dbus_dict_append_variant(&dict, "Name",
361                                                 DBUS_TYPE_STRING, &name);
362
363         append_services(&dict);
364
365         dbus_message_iter_close_container(&array, &dict);
366
367         return reply;
368 }
369
370 static GDBusMethodTable profile_methods[] = {
371         { "GetProperties", "", "a{sv}", profile_properties },
372         { },
373 };
374
375 int __connman_profile_init(DBusConnection *conn)
376 {
377         DBG("conn %p", conn);
378
379         connection = dbus_connection_ref(conn);
380         if (connection == NULL)
381                 return -1;
382
383         groups = g_hash_table_new_full(g_str_hash, g_str_equal,
384                                                         g_free, free_group);
385
386         g_dbus_register_interface(connection, PROFILE_DEFAULT,
387                                                 CONNMAN_PROFILE_INTERFACE,
388                                                 profile_methods,
389                                                 NULL, NULL, NULL, NULL);
390
391         return 0;
392 }
393
394 void __connman_profile_cleanup(void)
395 {
396         DBG("conn %p", connection);
397
398         g_dbus_unregister_interface(connection, PROFILE_DEFAULT,
399                                                 CONNMAN_PROFILE_INTERFACE);
400
401         g_hash_table_destroy(groups);
402         groups = NULL;
403
404         if (connection == NULL)
405                 return;
406
407         dbus_connection_unref(connection);
408 }