Add basic automatic clustering support for networks
[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         GSList *networks;
36 };
37
38 static GHashTable *groups = NULL;
39
40 static void free_group(gpointer data)
41 {
42         struct connman_group *group = data;
43
44         DBG("group %p", group);
45
46         g_free(group->path);
47         g_free(group);
48 }
49
50 static struct connman_group *lookup_group(const char *name)
51 {
52         struct connman_group *group;
53
54         DBG("name %s", name);
55
56         if (name == NULL)
57                 return NULL;
58
59         group = g_hash_table_lookup(groups, name);
60         if (group != NULL)
61                 goto done;
62
63         group = g_try_new0(struct connman_group, 1);
64         if (group == NULL)
65                 return NULL;
66
67         group->path = g_strdup_printf("%s/%s", PROFILE_DEFAULT, name);
68
69         g_hash_table_insert(groups, g_strdup(name), group);
70
71 done:
72         DBG("group %p", group);
73
74         return group;
75 }
76
77 int __connman_profile_add_device(struct connman_device *device)
78 {
79         struct connman_group *group;
80         char *name;
81
82         DBG("device %p", device);
83
84         name = g_strdup_printf("device%d", connman_device_get_index(device));
85         group = lookup_group(name);
86         g_free(name);
87
88         if (group == NULL)
89                 return -EINVAL;
90
91         return 0;
92 }
93
94 int __connman_profile_remove_device(struct connman_device *device)
95 {
96         struct connman_group *group;
97         char *name;
98
99         DBG("device %p", device);
100
101         name = g_strdup_printf("device%d", connman_device_get_index(device));
102         group = lookup_group(name);
103         g_free(name);
104
105         if (group == NULL)
106                 return -EINVAL;
107
108         return 0;
109 }
110
111 int __connman_profile_add_network(struct connman_network *network)
112 {
113         struct connman_group *group;
114
115         DBG("network %p", network);
116
117         group = lookup_group(__connman_network_get_group(network));
118         if (group == NULL)
119                 return -EINVAL;
120
121         return 0;
122 }
123
124 int __connman_profile_remove_network(struct connman_network *network)
125 {
126         struct connman_group *group;
127
128         DBG("network %p", network);
129
130         group = lookup_group(__connman_network_get_group(network));
131         if (group == NULL)
132                 return -EINVAL;
133
134         return 0;
135 }
136
137 const char *__connman_profile_active(void)
138 {
139         DBG("");
140
141         return PROFILE_DEFAULT;
142 }
143
144 void __connman_profile_list(DBusMessageIter *iter)
145 {
146         const char *path = __connman_profile_active();
147
148         DBG("");
149
150         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
151 }
152
153 static void append_path(gpointer key, gpointer value, gpointer user_data)
154 {
155         struct connman_group *group = value;
156         DBusMessageIter *iter = user_data;
157
158         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
159                                                         &group->path);
160 }
161
162 void __connman_profile_list_services(DBusMessageIter *iter)
163 {
164         DBG("");
165
166         g_hash_table_foreach(groups, append_path, iter);
167 }
168
169 static void append_services(DBusMessageIter *dict)
170 {
171         DBusMessageIter entry, value, iter;
172         const char *key = "Services";
173
174         dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
175                                                                 NULL, &entry);
176
177         dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
178
179         dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
180                 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_OBJECT_PATH_AS_STRING,
181                                                                 &value);
182
183         dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
184                                 DBUS_TYPE_OBJECT_PATH_AS_STRING, &iter);
185         __connman_profile_list_services(&iter);
186         dbus_message_iter_close_container(&value, &iter);
187
188         dbus_message_iter_close_container(&entry, &value);
189
190         dbus_message_iter_close_container(dict, &entry);
191 }
192
193 static DBusMessage *get_properties(DBusConnection *conn,
194                                         DBusMessage *msg, void *data)
195 {
196         const char *name = "Default";
197         DBusMessage *reply;
198         DBusMessageIter array, dict;
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_iter_init_append(reply, &array);
207
208         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
209                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
210                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
211                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
212
213         connman_dbus_dict_append_variant(&dict, "Name",
214                                                 DBUS_TYPE_STRING, &name);
215
216         append_services(&dict);
217
218         dbus_message_iter_close_container(&array, &dict);
219
220         return reply;
221 }
222
223 static GDBusMethodTable profile_methods[] = {
224         { "GetProperties", "", "a{sv}", get_properties },
225         { },
226 };
227
228 static DBusConnection *connection = NULL;
229
230 int __connman_profile_init(DBusConnection *conn)
231 {
232         DBG("conn %p", conn);
233
234         connection = dbus_connection_ref(conn);
235         if (connection == NULL)
236                 return -1;
237
238         groups = g_hash_table_new_full(g_str_hash, g_str_equal,
239                                                         g_free, free_group);
240
241         g_dbus_register_interface(connection, PROFILE_DEFAULT,
242                                                 CONNMAN_PROFILE_INTERFACE,
243                                                 profile_methods,
244                                                 NULL, NULL, NULL, NULL);
245
246         return 0;
247 }
248
249 void __connman_profile_cleanup(void)
250 {
251         DBG("conn %p", connection);
252
253         g_dbus_unregister_interface(connection, PROFILE_DEFAULT,
254                                                 CONNMAN_PROFILE_INTERFACE);
255
256         g_hash_table_destroy(groups);
257         groups = NULL;
258
259         if (connection == NULL)
260                 return;
261
262         dbus_connection_unref(connection);
263 }