Add support for technology interface
[platform/upstream/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         static unsigned int counter = 0;
175         struct connman_technology *technology;
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         technology = g_try_new0(struct connman_technology, 1);
186         if (technology == NULL)
187                 return NULL;
188
189         technology->refcount = 1;
190
191         technology->type = type;
192         technology->path = g_strdup_printf("%s/technology%d",
193                                                 CONNMAN_PATH, counter++);
194
195         if (g_dbus_register_interface(connection, technology->path,
196                                         CONNMAN_TECHNOLOGY_INTERFACE,
197                                         technology_methods, technology_signals,
198                                         NULL, technology, NULL) == FALSE) {
199                 connman_error("Failed to register %s", technology->path);
200                 g_free(technology);
201                 return NULL;
202         }
203
204         technology_list = g_slist_append(technology_list, technology);
205
206         technologies_changed();
207
208 done:
209         DBG("technology %p", technology);
210
211         return technology;
212 }
213
214 static void technology_put(struct connman_technology *technology)
215 {
216         DBG("technology %p", technology);
217
218         if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
219                 return;
220
221         technology_list = g_slist_remove(technology_list, technology);
222
223         technologies_changed();
224
225         g_dbus_unregister_interface(connection, technology->path,
226                                                 CONNMAN_TECHNOLOGY_INTERFACE);
227
228         g_free(technology->path);
229         g_free(technology);
230 }
231
232 static void unregister_device(gpointer data)
233 {
234         struct connman_technology *technology = data;
235
236         DBG("technology %p", technology);
237
238         technology_put(technology);
239 }
240
241 int __connman_technology_add_device(struct connman_device *device)
242 {
243         struct connman_technology *technology;
244         enum connman_service_type type;
245
246         DBG("device %p", device);
247
248         type = __connman_device_get_service_type(device);
249
250         technology = technology_get(type);
251         if (technology == NULL)
252                 return -ENXIO;
253
254         g_hash_table_insert(device_table, device, technology);
255
256         technology->device_list = g_slist_append(technology->device_list,
257                                                                 device);
258
259         devices_changed(technology);
260
261         return 0;
262 }
263
264 int __connman_technology_remove_device(struct connman_device *device)
265 {
266         struct connman_technology *technology;
267
268         DBG("device %p", device);
269
270         technology = g_hash_table_lookup(device_table, device);
271         if (technology == NULL)
272                 return -ENXIO;
273
274         technology->device_list = g_slist_remove(technology->device_list,
275                                                                 device);
276
277         devices_changed(technology);
278
279         g_hash_table_remove(device_table, device);
280
281         return 0;
282 }
283
284 int __connman_technology_init(void)
285 {
286         DBG("");
287
288         connection = connman_dbus_get_connection();
289
290         device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
291                                                 NULL, unregister_device);
292
293         return 0;
294 }
295
296 void __connman_technology_cleanup(void)
297 {
298         DBG("");
299
300         g_hash_table_destroy(device_table);
301
302         dbus_connection_unref(connection);
303 }