Use the technology type for an unqiue object path
[framework/connectivity/connman.git] / src / technology.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 <gdbus.h>
27
28 #include "connman.h"
29
30 static DBusConnection *connection;
31
32 static GHashTable *device_table;
33 static GSList *technology_list = NULL;
34
35 struct connman_technology {
36         gint refcount;
37         enum connman_service_type type;
38         char *path;
39         GSList *device_list;
40 };
41
42 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
43 {
44         GSList *list;
45
46         for (list = technology_list; list; list = list->next) {
47                 struct connman_technology *technology = list->data;
48
49                 if (technology->path == NULL)
50                         continue;
51
52                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
53                                                         &technology->path);
54         }
55 }
56
57 static void technologies_changed(void)
58 {
59         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
60                         CONNMAN_MANAGER_INTERFACE, "Technologies",
61                         DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
62 }
63
64 static void device_list(DBusMessageIter *iter, void *user_data)
65 {
66         struct connman_technology *technology = user_data;
67         GSList *list;
68
69         for (list = technology->device_list; list; list = list->next) {
70                 struct connman_device *device = list->data;
71                 const char *path;
72
73                 path = connman_device_get_path(device);
74                 if (path == NULL)
75                         continue;
76
77                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
78                                                                         &path);
79         }
80 }
81
82 static void devices_changed(struct connman_technology *technology)
83 {
84         connman_dbus_property_changed_array(technology->path,
85                         CONNMAN_TECHNOLOGY_INTERFACE, "Devices",
86                         DBUS_TYPE_OBJECT_PATH, device_list, technology);
87 }
88
89 static const char *get_name(enum connman_service_type type)
90 {
91         switch (type) {
92         case CONNMAN_SERVICE_TYPE_UNKNOWN:
93         case CONNMAN_SERVICE_TYPE_SYSTEM:
94         case CONNMAN_SERVICE_TYPE_GPS:
95         case CONNMAN_SERVICE_TYPE_VPN:
96                 break;
97         case CONNMAN_SERVICE_TYPE_ETHERNET:
98                 return "Wired";
99         case CONNMAN_SERVICE_TYPE_WIFI:
100                 return "WiFi";
101         case CONNMAN_SERVICE_TYPE_WIMAX:
102                 return "WiMAX";
103         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
104                 return "Bluetooth";
105         case CONNMAN_SERVICE_TYPE_CELLULAR:
106                 return "3G";
107         }
108
109         return NULL;
110 }
111
112 static DBusMessage *get_properties(DBusConnection *conn,
113                                         DBusMessage *message, void *user_data)
114 {
115         struct connman_technology *technology = user_data;
116         DBusMessage *reply;
117         DBusMessageIter array, dict;
118         const char *str;
119
120         reply = dbus_message_new_method_return(message);
121         if (reply == NULL)
122                 return NULL;
123
124         dbus_message_iter_init_append(reply, &array);
125
126         connman_dbus_dict_open(&array, &dict);
127
128         str = get_name(technology->type);
129         if (str != NULL)
130                 connman_dbus_dict_append_basic(&dict, "Name",
131                                                 DBUS_TYPE_STRING, &str);
132
133         str = __connman_service_type2string(technology->type);
134         if (str != NULL)
135                 connman_dbus_dict_append_basic(&dict, "Type",
136                                                 DBUS_TYPE_STRING, &str);
137
138         connman_dbus_dict_append_array(&dict, "Devices",
139                         DBUS_TYPE_OBJECT_PATH, device_list, technology);
140
141         connman_dbus_dict_close(&array, &dict);
142
143         return reply;
144 }
145
146 static GDBusMethodTable technology_methods[] = {
147         { "GetProperties", "", "a{sv}", get_properties },
148         { },
149 };
150
151 static GDBusSignalTable technology_signals[] = {
152         { "PropertyChanged", "sv" },
153         { },
154 };
155
156 static struct connman_technology *technology_find(enum connman_service_type type)
157 {
158         GSList *list;
159
160         DBG("type %d", type);
161
162         for (list = technology_list; list; list = list->next) {
163                 struct connman_technology *technology = list->data;
164
165                 if (technology->type == type)
166                         return technology;
167         }
168
169         return NULL;
170 }
171
172 static struct connman_technology *technology_get(enum connman_service_type type)
173 {
174         struct connman_technology *technology;
175         const char *str;
176
177         DBG("type %d", type);
178
179         technology = technology_find(type);
180         if (technology != NULL) {
181                 g_atomic_int_inc(&technology->refcount);
182                 goto done;
183         }
184
185         str = __connman_service_type2string(type);
186         if (str == NULL)
187                 return NULL;
188
189         technology = g_try_new0(struct connman_technology, 1);
190         if (technology == NULL)
191                 return NULL;
192
193         technology->refcount = 1;
194
195         technology->type = type;
196         technology->path = g_strdup_printf("%s/technology/%s",
197                                                         CONNMAN_PATH, str);
198
199         if (g_dbus_register_interface(connection, technology->path,
200                                         CONNMAN_TECHNOLOGY_INTERFACE,
201                                         technology_methods, technology_signals,
202                                         NULL, technology, NULL) == FALSE) {
203                 connman_error("Failed to register %s", technology->path);
204                 g_free(technology);
205                 return NULL;
206         }
207
208         technology_list = g_slist_append(technology_list, technology);
209
210         technologies_changed();
211
212 done:
213         DBG("technology %p", technology);
214
215         return technology;
216 }
217
218 static void technology_put(struct connman_technology *technology)
219 {
220         DBG("technology %p", technology);
221
222         if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
223                 return;
224
225         technology_list = g_slist_remove(technology_list, technology);
226
227         technologies_changed();
228
229         g_dbus_unregister_interface(connection, technology->path,
230                                                 CONNMAN_TECHNOLOGY_INTERFACE);
231
232         g_free(technology->path);
233         g_free(technology);
234 }
235
236 static void unregister_device(gpointer data)
237 {
238         struct connman_technology *technology = data;
239
240         DBG("technology %p", technology);
241
242         technology_put(technology);
243 }
244
245 int __connman_technology_add_device(struct connman_device *device)
246 {
247         struct connman_technology *technology;
248         enum connman_service_type type;
249
250         DBG("device %p", device);
251
252         type = __connman_device_get_service_type(device);
253
254         technology = technology_get(type);
255         if (technology == NULL)
256                 return -ENXIO;
257
258         g_hash_table_insert(device_table, device, technology);
259
260         technology->device_list = g_slist_append(technology->device_list,
261                                                                 device);
262
263         devices_changed(technology);
264
265         return 0;
266 }
267
268 int __connman_technology_remove_device(struct connman_device *device)
269 {
270         struct connman_technology *technology;
271
272         DBG("device %p", device);
273
274         technology = g_hash_table_lookup(device_table, device);
275         if (technology == NULL)
276                 return -ENXIO;
277
278         technology->device_list = g_slist_remove(technology->device_list,
279                                                                 device);
280
281         devices_changed(technology);
282
283         g_hash_table_remove(device_table, device);
284
285         return 0;
286 }
287
288 int __connman_technology_init(void)
289 {
290         DBG("");
291
292         connection = connman_dbus_get_connection();
293
294         device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
295                                                 NULL, unregister_device);
296
297         return 0;
298 }
299
300 void __connman_technology_cleanup(void)
301 {
302         DBG("");
303
304         g_hash_table_destroy(device_table);
305
306         dbus_connection_unref(connection);
307 }