fcfd9d8fbf59f9600bf7321fc096bd53815dffb1
[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 *rfkill_table;
33 static GHashTable *device_table;
34 static GSList *technology_list = NULL;
35
36 struct connman_rfkill {
37         unsigned int index;
38         enum connman_service_type type;
39 };
40
41 enum connman_technology_state {
42         CONNMAN_TECHNOLOGY_STATE_UNKNOWN   = 0,
43         CONNMAN_TECHNOLOGY_STATE_OFFLINE   = 1,
44         CONNMAN_TECHNOLOGY_STATE_AVAILABLE = 2,
45         CONNMAN_TECHNOLOGY_STATE_ENABLED   = 3,
46         CONNMAN_TECHNOLOGY_STATE_CONNECTED = 4,
47 };
48
49 struct connman_technology {
50         gint refcount;
51         enum connman_service_type type;
52         enum connman_technology_state state;
53         char *path;
54         GHashTable *rfkill_list;
55         GSList *device_list;
56 };
57
58 static void free_rfkill(gpointer data)
59 {
60         struct connman_rfkill *rfkill = data;
61
62         g_free(rfkill);
63 }
64
65 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
66 {
67         GSList *list;
68
69         for (list = technology_list; list; list = list->next) {
70                 struct connman_technology *technology = list->data;
71
72                 if (technology->path == NULL)
73                         continue;
74
75                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
76                                                         &technology->path);
77         }
78 }
79
80 static void technologies_changed(void)
81 {
82         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
83                         CONNMAN_MANAGER_INTERFACE, "Technologies",
84                         DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
85 }
86
87 static void device_list(DBusMessageIter *iter, void *user_data)
88 {
89         struct connman_technology *technology = user_data;
90         GSList *list;
91
92         for (list = technology->device_list; list; list = list->next) {
93                 struct connman_device *device = list->data;
94                 const char *path;
95
96                 path = connman_device_get_path(device);
97                 if (path == NULL)
98                         continue;
99
100                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
101                                                                         &path);
102         }
103 }
104
105 static void devices_changed(struct connman_technology *technology)
106 {
107         connman_dbus_property_changed_array(technology->path,
108                         CONNMAN_TECHNOLOGY_INTERFACE, "Devices",
109                         DBUS_TYPE_OBJECT_PATH, device_list, technology);
110 }
111
112 static const char *state2string(enum connman_technology_state state)
113 {
114         switch (state) {
115         case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
116                 break;
117         case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
118                 return "offline";
119         case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
120                 return "available";
121         case CONNMAN_TECHNOLOGY_STATE_ENABLED:
122                 return "enabled";
123         case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
124                 return "connected";
125         }
126
127         return NULL;
128 }
129
130 static void state_changed(struct connman_technology *technology)
131 {
132         const char *str;
133
134         str = state2string(technology->state);
135         if (str == NULL)
136                 return;
137
138         connman_dbus_property_changed_basic(technology->path,
139                                 CONNMAN_TECHNOLOGY_INTERFACE, "State",
140                                                 DBUS_TYPE_STRING, &str);
141 }
142
143 static const char *get_name(enum connman_service_type type)
144 {
145         switch (type) {
146         case CONNMAN_SERVICE_TYPE_UNKNOWN:
147         case CONNMAN_SERVICE_TYPE_SYSTEM:
148         case CONNMAN_SERVICE_TYPE_GPS:
149         case CONNMAN_SERVICE_TYPE_VPN:
150                 break;
151         case CONNMAN_SERVICE_TYPE_ETHERNET:
152                 return "Wired";
153         case CONNMAN_SERVICE_TYPE_WIFI:
154                 return "WiFi";
155         case CONNMAN_SERVICE_TYPE_WIMAX:
156                 return "WiMAX";
157         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
158                 return "Bluetooth";
159         case CONNMAN_SERVICE_TYPE_CELLULAR:
160                 return "3G";
161         }
162
163         return NULL;
164 }
165
166 static DBusMessage *get_properties(DBusConnection *conn,
167                                         DBusMessage *message, void *user_data)
168 {
169         struct connman_technology *technology = user_data;
170         DBusMessage *reply;
171         DBusMessageIter array, dict;
172         const char *str;
173
174         reply = dbus_message_new_method_return(message);
175         if (reply == NULL)
176                 return NULL;
177
178         dbus_message_iter_init_append(reply, &array);
179
180         connman_dbus_dict_open(&array, &dict);
181
182         str = state2string(technology->state);
183         if (str != NULL)
184                 connman_dbus_dict_append_basic(&dict, "State",
185                                                 DBUS_TYPE_STRING, &str);
186
187         str = get_name(technology->type);
188         if (str != NULL)
189                 connman_dbus_dict_append_basic(&dict, "Name",
190                                                 DBUS_TYPE_STRING, &str);
191
192         str = __connman_service_type2string(technology->type);
193         if (str != NULL)
194                 connman_dbus_dict_append_basic(&dict, "Type",
195                                                 DBUS_TYPE_STRING, &str);
196
197         connman_dbus_dict_append_array(&dict, "Devices",
198                         DBUS_TYPE_OBJECT_PATH, device_list, technology);
199
200         connman_dbus_dict_close(&array, &dict);
201
202         return reply;
203 }
204
205 static GDBusMethodTable technology_methods[] = {
206         { "GetProperties", "", "a{sv}", get_properties },
207         { },
208 };
209
210 static GDBusSignalTable technology_signals[] = {
211         { "PropertyChanged", "sv" },
212         { },
213 };
214
215 static struct connman_technology *technology_find(enum connman_service_type type)
216 {
217         GSList *list;
218
219         DBG("type %d", type);
220
221         for (list = technology_list; list; list = list->next) {
222                 struct connman_technology *technology = list->data;
223
224                 if (technology->type == type)
225                         return technology;
226         }
227
228         return NULL;
229 }
230
231 static struct connman_technology *technology_get(enum connman_service_type type)
232 {
233         struct connman_technology *technology;
234         const char *str;
235
236         DBG("type %d", type);
237
238         technology = technology_find(type);
239         if (technology != NULL) {
240                 g_atomic_int_inc(&technology->refcount);
241                 goto done;
242         }
243
244         str = __connman_service_type2string(type);
245         if (str == NULL)
246                 return NULL;
247
248         technology = g_try_new0(struct connman_technology, 1);
249         if (technology == NULL)
250                 return NULL;
251
252         technology->refcount = 1;
253
254         technology->type = type;
255         technology->path = g_strdup_printf("%s/technology/%s",
256                                                         CONNMAN_PATH, str);
257
258         technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
259                                                         NULL, free_rfkill);
260         technology->device_list = NULL;
261
262         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
263
264         if (g_dbus_register_interface(connection, technology->path,
265                                         CONNMAN_TECHNOLOGY_INTERFACE,
266                                         technology_methods, technology_signals,
267                                         NULL, technology, NULL) == FALSE) {
268                 connman_error("Failed to register %s", technology->path);
269                 g_free(technology);
270                 return NULL;
271         }
272
273         technology_list = g_slist_append(technology_list, technology);
274
275         technologies_changed();
276
277 done:
278         DBG("technology %p", technology);
279
280         return technology;
281 }
282
283 static void technology_put(struct connman_technology *technology)
284 {
285         DBG("technology %p", technology);
286
287         if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
288                 return;
289
290         technology_list = g_slist_remove(technology_list, technology);
291
292         technologies_changed();
293
294         g_dbus_unregister_interface(connection, technology->path,
295                                                 CONNMAN_TECHNOLOGY_INTERFACE);
296
297         g_slist_free(technology->device_list);
298         g_hash_table_destroy(technology->rfkill_list);
299
300         g_free(technology->path);
301         g_free(technology);
302 }
303
304 static void unregister_technology(gpointer data)
305 {
306         struct connman_technology *technology = data;
307
308         technology_put(technology);
309 }
310
311 int __connman_technology_add_device(struct connman_device *device)
312 {
313         struct connman_technology *technology;
314         enum connman_service_type type;
315
316         DBG("device %p", device);
317
318         type = __connman_device_get_service_type(device);
319
320         technology = technology_get(type);
321         if (technology == NULL)
322                 return -ENXIO;
323
324         g_hash_table_insert(device_table, device, technology);
325
326         if (technology->device_list == NULL) {
327                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
328                 state_changed(technology);
329         }
330
331         technology->device_list = g_slist_append(technology->device_list,
332                                                                 device);
333         devices_changed(technology);
334
335         return 0;
336 }
337
338 int __connman_technology_remove_device(struct connman_device *device)
339 {
340         struct connman_technology *technology;
341
342         DBG("device %p", device);
343
344         technology = g_hash_table_lookup(device_table, device);
345         if (technology == NULL)
346                 return -ENXIO;
347
348         technology->device_list = g_slist_remove(technology->device_list,
349                                                                 device);
350         devices_changed(technology);
351
352         if (technology->device_list == NULL) {
353                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
354                 state_changed(technology);
355         }
356
357         g_hash_table_remove(device_table, device);
358
359         return 0;
360 }
361
362 int __connman_technology_add_rfkill(unsigned int index,
363                                         enum connman_service_type type,
364                                                 connman_bool_t softblock,
365                                                 connman_bool_t hardblock)
366 {
367         struct connman_technology *technology;
368         struct connman_rfkill *rfkill;
369
370         DBG("index %u type %d soft %u hard %u", index, type,
371                                                         softblock, hardblock);
372
373         rfkill = g_try_new0(struct connman_rfkill, 1);
374         if (rfkill == NULL)
375                 return -ENOMEM;
376
377         rfkill->index = index;
378         rfkill->type = type;
379
380         technology = technology_get(type);
381         if (technology == NULL) {
382                 g_free(rfkill);
383                 return -ENXIO;
384         }
385
386         g_hash_table_replace(rfkill_table, &index, technology);
387
388         g_hash_table_replace(technology->rfkill_list, &index, rfkill);
389
390         return 0;
391 }
392
393 int __connman_technology_update_rfkill(unsigned int index,
394                                                 connman_bool_t softblock,
395                                                 connman_bool_t hardblock)
396 {
397         struct connman_technology *technology;
398
399         DBG("index %u soft %u hard %u", index, softblock, hardblock);
400
401         technology = g_hash_table_lookup(rfkill_table, &index);
402         if (technology == NULL)
403                 return -ENXIO;
404
405         return 0;
406 }
407
408 int __connman_technology_remove_rfkill(unsigned int index)
409 {
410         struct connman_technology *technology;
411
412         DBG("index %u", index);
413
414         technology = g_hash_table_lookup(rfkill_table, &index);
415         if (technology == NULL)
416                 return -ENXIO;
417
418         g_hash_table_remove(technology->rfkill_list, &index);
419
420         g_hash_table_remove(rfkill_table, &index);
421
422         return 0;
423 }
424
425 int __connman_technology_init(void)
426 {
427         DBG("");
428
429         connection = connman_dbus_get_connection();
430
431         rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
432                                                 NULL, unregister_technology);
433         device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
434                                                 NULL, unregister_technology);
435
436         return 0;
437 }
438
439 void __connman_technology_cleanup(void)
440 {
441         DBG("");
442
443         g_hash_table_destroy(device_table);
444         g_hash_table_destroy(rfkill_table);
445
446         dbus_connection_unref(connection);
447 }