Don't set name for Ethernet devices
[framework/connectivity/connman.git] / 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 GDBusMethodTable service_methods[] = {
97         { "GetProperties", "", "a{sv}", get_properties },
98         { },
99 };
100
101 static void free_group(gpointer data)
102 {
103         struct connman_group *group = data;
104
105         DBG("group %p", group);
106
107         g_dbus_unregister_interface(connection, group->path,
108                                                 CONNMAN_SERVICE_INTERFACE);
109
110         g_free(group->security);
111         g_free(group->mode);
112         g_free(group->name);
113         g_free(group->type);
114         g_free(group->path);
115         g_free(group);
116 }
117
118 static struct connman_group *lookup_group(const char *name)
119 {
120         struct connman_group *group;
121
122         DBG("name %s", name);
123
124         if (name == NULL)
125                 return NULL;
126
127         group = g_hash_table_lookup(groups, name);
128         if (group != NULL)
129                 goto done;
130
131         group = g_try_new0(struct connman_group, 1);
132         if (group == NULL)
133                 return NULL;
134
135         group->type = CONNMAN_ELEMENT_TYPE_UNKNOWN;
136         group->path = g_strdup_printf("%s/%s", PROFILE_DEFAULT, name);
137
138         group->favorite = FALSE;
139
140         g_hash_table_insert(groups, g_strdup(name), group);
141
142         g_dbus_register_interface(connection, group->path,
143                                                 CONNMAN_SERVICE_INTERFACE,
144                                                 service_methods,
145                                                 NULL, NULL, group, NULL);
146
147 done:
148         DBG("group %p", group);
149
150         return group;
151 }
152
153 int __connman_profile_add_device(struct connman_device *device)
154 {
155         struct connman_group *group;
156         char *name;
157
158         DBG("device %p", device);
159
160         name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
161                                         connman_device_get_index(device));
162         group = lookup_group(name);
163         g_free(name);
164
165         if (group == NULL)
166                 return -EINVAL;
167
168         group->type = g_strdup(__connman_device_get_type(device));
169
170         return 0;
171 }
172
173 int __connman_profile_remove_device(struct connman_device *device)
174 {
175         struct connman_group *group;
176         char *name;
177
178         DBG("device %p", device);
179
180         name = g_strdup_printf("%s_%d", __connman_device_get_type(device),
181                                         connman_device_get_index(device));
182         group = lookup_group(name);
183         g_free(name);
184
185         if (group == NULL)
186                 return -EINVAL;
187
188         return 0;
189 }
190
191 int __connman_profile_add_network(struct connman_network *network)
192 {
193         struct connman_group *group;
194
195         DBG("network %p", network);
196
197         group = lookup_group(__connman_network_get_group(network));
198         if (group == NULL)
199                 return -EINVAL;
200
201         g_free(group->type);
202         g_free(group->name);
203
204         group->type = g_strdup(__connman_network_get_type(network));
205         group->name = g_strdup(connman_network_get_string(network, "Name"));
206
207         group->strength = connman_network_get_uint8(network, "Strength");
208
209         if (group->network == NULL) {
210                 group->network = network;
211
212                 group->mode = g_strdup(connman_network_get_string(network,
213                                                                 "WiFi.Mode"));
214                 group->security = g_strdup(connman_network_get_string(network,
215                                                         "WiFi.Security"));
216         }
217
218         return 0;
219 }
220
221 int __connman_profile_remove_network(struct connman_network *network)
222 {
223         struct connman_group *group;
224
225         DBG("network %p", network);
226
227         group = lookup_group(__connman_network_get_group(network));
228         if (group == NULL)
229                 return -EINVAL;
230
231         if (group->network == network) {
232                 g_free(group->security);
233                 group->security = NULL;
234
235                 g_free(group->mode);
236                 group->mode = NULL;
237
238                 group->network = NULL;
239         }
240
241         return 0;
242 }
243
244 const char *__connman_profile_active(void)
245 {
246         DBG("");
247
248         return PROFILE_DEFAULT;
249 }
250
251 void __connman_profile_list(DBusMessageIter *iter)
252 {
253         const char *path = __connman_profile_active();
254
255         DBG("");
256
257         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
258 }
259
260 static void append_path(gpointer key, gpointer value, gpointer user_data)
261 {
262         struct connman_group *group = value;
263         DBusMessageIter *iter = user_data;
264
265         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
266                                                         &group->path);
267 }
268
269 void __connman_profile_list_services(DBusMessageIter *iter)
270 {
271         DBG("");
272
273         g_hash_table_foreach(groups, append_path, iter);
274 }
275
276 static void append_services(DBusMessageIter *dict)
277 {
278         DBusMessageIter entry, value, iter;
279         const char *key = "Services";
280
281         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
282                                                                 NULL, &entry);
283
284         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
285
286         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
287                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
288                                                                 &value);
289
290         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
291                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
292         __connman_profile_list_services(&iter);
293         dbus_message_iter_close_container(&value, &iter);
294
295         dbus_message_iter_close_container(&entry, &value);
296
297         dbus_message_iter_close_container(dict, &entry);
298 }
299
300 static DBusMessage *profile_properties(DBusConnection *conn,
301                                         DBusMessage *msg, void *data)
302 {
303         const char *name = "Default";
304         DBusMessage *reply;
305         DBusMessageIter array, dict;
306
307         DBG("conn %p", conn);
308
309         reply = dbus_message_new_method_return(msg);
310         if (reply == NULL)
311                 return NULL;
312
313         dbus_message_iter_init_append(reply, &array);
314
315         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
316                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
317                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
318                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
319
320         connman_dbus_dict_append_variant(&dict, "Name",
321                                                 DBUS_TYPE_STRING, &name);
322
323         append_services(&dict);
324
325         dbus_message_iter_close_container(&array, &dict);
326
327         return reply;
328 }
329
330 static GDBusMethodTable profile_methods[] = {
331         { "GetProperties", "", "a{sv}", profile_properties },
332         { },
333 };
334
335 int __connman_profile_init(DBusConnection *conn)
336 {
337         DBG("conn %p", conn);
338
339         connection = dbus_connection_ref(conn);
340         if (connection == NULL)
341                 return -1;
342
343         groups = g_hash_table_new_full(g_str_hash, g_str_equal,
344                                                         g_free, free_group);
345
346         g_dbus_register_interface(connection, PROFILE_DEFAULT,
347                                                 CONNMAN_PROFILE_INTERFACE,
348                                                 profile_methods,
349                                                 NULL, NULL, NULL, NULL);
350
351         return 0;
352 }
353
354 void __connman_profile_cleanup(void)
355 {
356         DBG("conn %p", connection);
357
358         g_dbus_unregister_interface(connection, PROFILE_DEFAULT,
359                                                 CONNMAN_PROFILE_INTERFACE);
360
361         g_hash_table_destroy(groups);
362         groups = NULL;
363
364         if (connection == NULL)
365                 return;
366
367         dbus_connection_unref(connection);
368 }