Add support for technology drivers
[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 *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         gint enabled;
57
58         struct connman_technology_driver *driver;
59         void *driver_data;
60 };
61
62 static GSList *driver_list = NULL;
63
64 static gint compare_priority(gconstpointer a, gconstpointer b)
65 {
66         const struct connman_technology_driver *driver1 = a;
67         const struct connman_technology_driver *driver2 = b;
68
69         return driver2->priority - driver1->priority;
70 }
71
72 /**
73  * connman_technology_driver_register:
74  * @driver: Technology driver definition
75  *
76  * Register a new technology driver
77  *
78  * Returns: %0 on success
79  */
80 int connman_technology_driver_register(struct connman_technology_driver *driver)
81 {
82         DBG("driver %p name %s", driver, driver->name);
83
84         driver_list = g_slist_insert_sorted(driver_list, driver,
85                                                         compare_priority);
86
87         return 0;
88 }
89
90 /**
91  * connman_technology_driver_unregister:
92  * @driver: Technology driver definition
93  *
94  * Remove a previously registered technology driver
95  */
96 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
97 {
98         DBG("driver %p name %s", driver, driver->name);
99
100         driver_list = g_slist_remove(driver_list, driver);
101 }
102
103 static void free_rfkill(gpointer data)
104 {
105         struct connman_rfkill *rfkill = data;
106
107         g_free(rfkill);
108 }
109
110 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
111 {
112         GSList *list;
113
114         for (list = technology_list; list; list = list->next) {
115                 struct connman_technology *technology = list->data;
116
117                 if (technology->path == NULL)
118                         continue;
119
120                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
121                                                         &technology->path);
122         }
123 }
124
125 static void technologies_changed(void)
126 {
127         connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
128                         CONNMAN_MANAGER_INTERFACE, "Technologies",
129                         DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
130 }
131
132 static void device_list(DBusMessageIter *iter, void *user_data)
133 {
134         struct connman_technology *technology = user_data;
135         GSList *list;
136
137         for (list = technology->device_list; list; list = list->next) {
138                 struct connman_device *device = list->data;
139                 const char *path;
140
141                 path = connman_device_get_path(device);
142                 if (path == NULL)
143                         continue;
144
145                 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
146                                                                         &path);
147         }
148 }
149
150 static void devices_changed(struct connman_technology *technology)
151 {
152         connman_dbus_property_changed_array(technology->path,
153                         CONNMAN_TECHNOLOGY_INTERFACE, "Devices",
154                         DBUS_TYPE_OBJECT_PATH, device_list, technology);
155 }
156
157 static const char *state2string(enum connman_technology_state state)
158 {
159         switch (state) {
160         case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
161                 break;
162         case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
163                 return "offline";
164         case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
165                 return "available";
166         case CONNMAN_TECHNOLOGY_STATE_ENABLED:
167                 return "enabled";
168         case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
169                 return "connected";
170         }
171
172         return NULL;
173 }
174
175 static void state_changed(struct connman_technology *technology)
176 {
177         const char *str;
178
179         str = state2string(technology->state);
180         if (str == NULL)
181                 return;
182
183         connman_dbus_property_changed_basic(technology->path,
184                                 CONNMAN_TECHNOLOGY_INTERFACE, "State",
185                                                 DBUS_TYPE_STRING, &str);
186 }
187
188 static const char *get_name(enum connman_service_type type)
189 {
190         switch (type) {
191         case CONNMAN_SERVICE_TYPE_UNKNOWN:
192         case CONNMAN_SERVICE_TYPE_SYSTEM:
193         case CONNMAN_SERVICE_TYPE_GPS:
194         case CONNMAN_SERVICE_TYPE_VPN:
195                 break;
196         case CONNMAN_SERVICE_TYPE_ETHERNET:
197                 return "Wired";
198         case CONNMAN_SERVICE_TYPE_WIFI:
199                 return "WiFi";
200         case CONNMAN_SERVICE_TYPE_WIMAX:
201                 return "WiMAX";
202         case CONNMAN_SERVICE_TYPE_BLUETOOTH:
203                 return "Bluetooth";
204         case CONNMAN_SERVICE_TYPE_CELLULAR:
205                 return "3G";
206         }
207
208         return NULL;
209 }
210
211 static DBusMessage *get_properties(DBusConnection *conn,
212                                         DBusMessage *message, void *user_data)
213 {
214         struct connman_technology *technology = user_data;
215         DBusMessage *reply;
216         DBusMessageIter array, dict;
217         const char *str;
218
219         reply = dbus_message_new_method_return(message);
220         if (reply == NULL)
221                 return NULL;
222
223         dbus_message_iter_init_append(reply, &array);
224
225         connman_dbus_dict_open(&array, &dict);
226
227         str = state2string(technology->state);
228         if (str != NULL)
229                 connman_dbus_dict_append_basic(&dict, "State",
230                                                 DBUS_TYPE_STRING, &str);
231
232         str = get_name(technology->type);
233         if (str != NULL)
234                 connman_dbus_dict_append_basic(&dict, "Name",
235                                                 DBUS_TYPE_STRING, &str);
236
237         str = __connman_service_type2string(technology->type);
238         if (str != NULL)
239                 connman_dbus_dict_append_basic(&dict, "Type",
240                                                 DBUS_TYPE_STRING, &str);
241
242         connman_dbus_dict_append_array(&dict, "Devices",
243                         DBUS_TYPE_OBJECT_PATH, device_list, technology);
244
245         connman_dbus_dict_close(&array, &dict);
246
247         return reply;
248 }
249
250 static GDBusMethodTable technology_methods[] = {
251         { "GetProperties", "", "a{sv}", get_properties },
252         { },
253 };
254
255 static GDBusSignalTable technology_signals[] = {
256         { "PropertyChanged", "sv" },
257         { },
258 };
259
260 static struct connman_technology *technology_find(enum connman_service_type type)
261 {
262         GSList *list;
263
264         DBG("type %d", type);
265
266         for (list = technology_list; list; list = list->next) {
267                 struct connman_technology *technology = list->data;
268
269                 if (technology->type == type)
270                         return technology;
271         }
272
273         return NULL;
274 }
275
276 static struct connman_technology *technology_get(enum connman_service_type type)
277 {
278         struct connman_technology *technology;
279         const char *str;
280         GSList *list;
281
282         DBG("type %d", type);
283
284         technology = technology_find(type);
285         if (technology != NULL) {
286                 g_atomic_int_inc(&technology->refcount);
287                 goto done;
288         }
289
290         str = __connman_service_type2string(type);
291         if (str == NULL)
292                 return NULL;
293
294         technology = g_try_new0(struct connman_technology, 1);
295         if (technology == NULL)
296                 return NULL;
297
298         technology->refcount = 1;
299
300         technology->type = type;
301         technology->path = g_strdup_printf("%s/technology/%s",
302                                                         CONNMAN_PATH, str);
303
304         technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
305                                                         NULL, free_rfkill);
306         technology->device_list = NULL;
307
308         technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
309
310         if (g_dbus_register_interface(connection, technology->path,
311                                         CONNMAN_TECHNOLOGY_INTERFACE,
312                                         technology_methods, technology_signals,
313                                         NULL, technology, NULL) == FALSE) {
314                 connman_error("Failed to register %s", technology->path);
315                 g_free(technology);
316                 return NULL;
317         }
318
319         technology_list = g_slist_append(technology_list, technology);
320
321         technologies_changed();
322
323         if (technology->driver != NULL)
324                 goto done;
325
326         for (list = driver_list; list; list = list->next) {
327                 struct connman_technology_driver *driver = list->data;
328
329                 DBG("driver %p name %s", driver, driver->name);
330
331                 if (driver->type != technology->type)
332                         continue;
333
334                 if (driver->probe(technology) == 0) {
335                         technology->driver = driver;
336                         break;
337                 }
338         }
339
340 done:
341         DBG("technology %p", technology);
342
343         return technology;
344 }
345
346 static void technology_put(struct connman_technology *technology)
347 {
348         DBG("technology %p", technology);
349
350         if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
351                 return;
352
353         if (technology->driver) {
354                 technology->driver->remove(technology);
355                 technology->driver = NULL;
356         }
357
358         technology_list = g_slist_remove(technology_list, technology);
359
360         technologies_changed();
361
362         g_dbus_unregister_interface(connection, technology->path,
363                                                 CONNMAN_TECHNOLOGY_INTERFACE);
364
365         g_slist_free(technology->device_list);
366         g_hash_table_destroy(technology->rfkill_list);
367
368         g_free(technology->path);
369         g_free(technology);
370 }
371
372 static void unregister_technology(gpointer data)
373 {
374         struct connman_technology *technology = data;
375
376         technology_put(technology);
377 }
378
379 int __connman_technology_add_device(struct connman_device *device)
380 {
381         struct connman_technology *technology;
382         enum connman_service_type type;
383
384         DBG("device %p", device);
385
386         type = __connman_device_get_service_type(device);
387         __connman_notifier_register(type);
388
389         technology = technology_get(type);
390         if (technology == NULL)
391                 return -ENXIO;
392
393         g_hash_table_insert(device_table, device, technology);
394
395         if (technology->device_list == NULL) {
396                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
397                 state_changed(technology);
398         }
399
400         technology->device_list = g_slist_append(technology->device_list,
401                                                                 device);
402         devices_changed(technology);
403
404         return 0;
405 }
406
407 int __connman_technology_remove_device(struct connman_device *device)
408 {
409         struct connman_technology *technology;
410         enum connman_service_type type;
411
412         DBG("device %p", device);
413
414         type = __connman_device_get_service_type(device);
415         __connman_notifier_disable(type);
416         __connman_notifier_unregister(type);
417
418         technology = g_hash_table_lookup(device_table, device);
419         if (technology == NULL)
420                 return -ENXIO;
421
422         technology->device_list = g_slist_remove(technology->device_list,
423                                                                 device);
424         devices_changed(technology);
425
426         if (technology->device_list == NULL) {
427                 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
428                 state_changed(technology);
429         }
430
431         g_hash_table_remove(device_table, device);
432
433         return 0;
434 }
435
436 int __connman_technology_enable_device(struct connman_device *device)
437 {
438         struct connman_technology *technology;
439         enum connman_service_type type;
440
441         DBG("device %p", device);
442
443         type = __connman_device_get_service_type(device);
444         __connman_notifier_enable(type);
445
446         technology = g_hash_table_lookup(device_table, device);
447         if (technology == NULL)
448                 return -ENXIO;
449
450         if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
451                 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
452                 state_changed(technology);
453         }
454
455         return 0;
456 }
457
458 int __connman_technology_disable_device(struct connman_device *device)
459 {
460         struct connman_technology *technology;
461         enum connman_service_type type;
462
463         DBG("device %p", device);
464
465         type = __connman_device_get_service_type(device);
466         __connman_notifier_disable(type);
467
468         technology = g_hash_table_lookup(device_table, device);
469         if (technology == NULL)
470                 return -ENXIO;
471
472         if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
473                 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
474                 state_changed(technology);
475         }
476
477         return 0;
478 }
479
480 int __connman_technology_add_rfkill(unsigned int index,
481                                         enum connman_service_type type,
482                                                 connman_bool_t softblock,
483                                                 connman_bool_t hardblock)
484 {
485         struct connman_technology *technology;
486         struct connman_rfkill *rfkill;
487
488         DBG("index %u type %d soft %u hard %u", index, type,
489                                                         softblock, hardblock);
490
491         rfkill = g_try_new0(struct connman_rfkill, 1);
492         if (rfkill == NULL)
493                 return -ENOMEM;
494
495         rfkill->index = index;
496         rfkill->type = type;
497
498         technology = technology_get(type);
499         if (technology == NULL) {
500                 g_free(rfkill);
501                 return -ENXIO;
502         }
503
504         g_hash_table_replace(rfkill_table, &index, technology);
505
506         g_hash_table_replace(technology->rfkill_list, &index, rfkill);
507
508         return 0;
509 }
510
511 int __connman_technology_update_rfkill(unsigned int index,
512                                                 connman_bool_t softblock,
513                                                 connman_bool_t hardblock)
514 {
515         struct connman_technology *technology;
516
517         DBG("index %u soft %u hard %u", index, softblock, hardblock);
518
519         technology = g_hash_table_lookup(rfkill_table, &index);
520         if (technology == NULL)
521                 return -ENXIO;
522
523         return 0;
524 }
525
526 int __connman_technology_remove_rfkill(unsigned int index)
527 {
528         struct connman_technology *technology;
529
530         DBG("index %u", index);
531
532         technology = g_hash_table_lookup(rfkill_table, &index);
533         if (technology == NULL)
534                 return -ENXIO;
535
536         g_hash_table_remove(technology->rfkill_list, &index);
537
538         g_hash_table_remove(rfkill_table, &index);
539
540         return 0;
541 }
542
543 int __connman_technology_init(void)
544 {
545         DBG("");
546
547         connection = connman_dbus_get_connection();
548
549         rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
550                                                 NULL, unregister_technology);
551         device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
552                                                 NULL, unregister_technology);
553
554         return 0;
555 }
556
557 void __connman_technology_cleanup(void)
558 {
559         DBG("");
560
561         g_hash_table_destroy(device_table);
562         g_hash_table_destroy(rfkill_table);
563
564         dbus_connection_unref(connection);
565 }