5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
30 static DBusConnection *connection;
32 static GHashTable *rfkill_table;
33 static GHashTable *device_table;
34 static GSList *technology_list = NULL;
36 struct connman_rfkill {
38 enum connman_service_type type;
39 connman_bool_t softblock;
40 connman_bool_t hardblock;
43 enum connman_technology_state {
44 CONNMAN_TECHNOLOGY_STATE_UNKNOWN = 0,
45 CONNMAN_TECHNOLOGY_STATE_OFFLINE = 1,
46 CONNMAN_TECHNOLOGY_STATE_AVAILABLE = 2,
47 CONNMAN_TECHNOLOGY_STATE_BLOCKED = 3,
48 CONNMAN_TECHNOLOGY_STATE_ENABLED = 4,
49 CONNMAN_TECHNOLOGY_STATE_CONNECTED = 5,
52 struct connman_technology {
54 enum connman_service_type type;
55 enum connman_technology_state state;
57 GHashTable *rfkill_list;
62 struct connman_technology_driver *driver;
66 static GSList *driver_list = NULL;
68 static gint compare_priority(gconstpointer a, gconstpointer b)
70 const struct connman_technology_driver *driver1 = a;
71 const struct connman_technology_driver *driver2 = b;
73 return driver2->priority - driver1->priority;
77 * connman_technology_driver_register:
78 * @driver: Technology driver definition
80 * Register a new technology driver
82 * Returns: %0 on success
84 int connman_technology_driver_register(struct connman_technology_driver *driver)
86 DBG("driver %p name %s", driver, driver->name);
88 driver_list = g_slist_insert_sorted(driver_list, driver,
95 * connman_technology_driver_unregister:
96 * @driver: Technology driver definition
98 * Remove a previously registered technology driver
100 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
102 DBG("driver %p name %s", driver, driver->name);
104 driver_list = g_slist_remove(driver_list, driver);
107 void __connman_technology_add_interface(enum connman_service_type type,
108 int index, const char *name, const char *ident)
113 case CONNMAN_SERVICE_TYPE_UNKNOWN:
114 case CONNMAN_SERVICE_TYPE_SYSTEM:
116 case CONNMAN_SERVICE_TYPE_ETHERNET:
117 case CONNMAN_SERVICE_TYPE_WIFI:
118 case CONNMAN_SERVICE_TYPE_WIMAX:
119 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
120 case CONNMAN_SERVICE_TYPE_CELLULAR:
121 case CONNMAN_SERVICE_TYPE_GPS:
122 case CONNMAN_SERVICE_TYPE_VPN:
126 connman_info("Create interface %s [ %s ]", name,
127 __connman_service_type2string(type));
129 for (list = technology_list; list; list = list->next) {
130 struct connman_technology *technology = list->data;
132 if (technology->type != type)
135 if (technology->driver == NULL)
138 if (technology->driver->add_interface)
139 technology->driver->add_interface(technology,
144 void __connman_technology_remove_interface(enum connman_service_type type,
145 int index, const char *name, const char *ident)
150 case CONNMAN_SERVICE_TYPE_UNKNOWN:
151 case CONNMAN_SERVICE_TYPE_SYSTEM:
153 case CONNMAN_SERVICE_TYPE_ETHERNET:
154 case CONNMAN_SERVICE_TYPE_WIFI:
155 case CONNMAN_SERVICE_TYPE_WIMAX:
156 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
157 case CONNMAN_SERVICE_TYPE_CELLULAR:
158 case CONNMAN_SERVICE_TYPE_GPS:
159 case CONNMAN_SERVICE_TYPE_VPN:
163 connman_info("Remove interface %s [ %s ]", name,
164 __connman_service_type2string(type));
166 for (list = technology_list; list; list = list->next) {
167 struct connman_technology *technology = list->data;
169 if (technology->type != type)
172 if (technology->driver == NULL)
175 if (technology->driver->remove_interface)
176 technology->driver->remove_interface(technology, index);
180 static int set_tethering(const char *bridge, connman_bool_t enabled)
184 for (list = technology_list; list; list = list->next) {
185 struct connman_technology *technology = list->data;
187 if (technology->driver == NULL)
190 if (technology->driver->set_tethering)
191 technology->driver->set_tethering(technology,
198 int __connman_technology_enable_tethering(const char *bridge)
200 return set_tethering(bridge, TRUE);
203 int __connman_technology_disable_tethering(const char *bridge)
205 return set_tethering(bridge, FALSE);
208 static void free_rfkill(gpointer data)
210 struct connman_rfkill *rfkill = data;
215 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
219 for (list = technology_list; list; list = list->next) {
220 struct connman_technology *technology = list->data;
222 if (technology->path == NULL)
225 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
230 static void technologies_changed(void)
232 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
233 CONNMAN_MANAGER_INTERFACE, "Technologies",
234 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
237 static const char *state2string(enum connman_technology_state state)
240 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
242 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
244 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
246 case CONNMAN_TECHNOLOGY_STATE_BLOCKED:
248 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
250 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
257 static void state_changed(struct connman_technology *technology)
261 str = state2string(technology->state);
265 connman_dbus_property_changed_basic(technology->path,
266 CONNMAN_TECHNOLOGY_INTERFACE, "State",
267 DBUS_TYPE_STRING, &str);
270 static const char *get_name(enum connman_service_type type)
273 case CONNMAN_SERVICE_TYPE_UNKNOWN:
274 case CONNMAN_SERVICE_TYPE_SYSTEM:
275 case CONNMAN_SERVICE_TYPE_GPS:
276 case CONNMAN_SERVICE_TYPE_VPN:
278 case CONNMAN_SERVICE_TYPE_ETHERNET:
280 case CONNMAN_SERVICE_TYPE_WIFI:
282 case CONNMAN_SERVICE_TYPE_WIMAX:
284 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
286 case CONNMAN_SERVICE_TYPE_CELLULAR:
293 static DBusMessage *get_properties(DBusConnection *conn,
294 DBusMessage *message, void *user_data)
296 struct connman_technology *technology = user_data;
298 DBusMessageIter array, dict;
301 reply = dbus_message_new_method_return(message);
305 dbus_message_iter_init_append(reply, &array);
307 connman_dbus_dict_open(&array, &dict);
309 str = state2string(technology->state);
311 connman_dbus_dict_append_basic(&dict, "State",
312 DBUS_TYPE_STRING, &str);
314 str = get_name(technology->type);
316 connman_dbus_dict_append_basic(&dict, "Name",
317 DBUS_TYPE_STRING, &str);
319 str = __connman_service_type2string(technology->type);
321 connman_dbus_dict_append_basic(&dict, "Type",
322 DBUS_TYPE_STRING, &str);
324 connman_dbus_dict_close(&array, &dict);
329 static GDBusMethodTable technology_methods[] = {
330 { "GetProperties", "", "a{sv}", get_properties },
334 static GDBusSignalTable technology_signals[] = {
335 { "PropertyChanged", "sv" },
339 static struct connman_technology *technology_find(enum connman_service_type type)
343 DBG("type %d", type);
345 for (list = technology_list; list; list = list->next) {
346 struct connman_technology *technology = list->data;
348 if (technology->type == type)
355 static struct connman_technology *technology_get(enum connman_service_type type)
357 struct connman_technology *technology;
361 DBG("type %d", type);
363 technology = technology_find(type);
364 if (technology != NULL) {
365 g_atomic_int_inc(&technology->refcount);
369 str = __connman_service_type2string(type);
373 technology = g_try_new0(struct connman_technology, 1);
374 if (technology == NULL)
377 technology->refcount = 1;
379 technology->type = type;
380 technology->path = g_strdup_printf("%s/technology/%s",
383 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
385 technology->device_list = NULL;
387 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
389 if (g_dbus_register_interface(connection, technology->path,
390 CONNMAN_TECHNOLOGY_INTERFACE,
391 technology_methods, technology_signals,
392 NULL, technology, NULL) == FALSE) {
393 connman_error("Failed to register %s", technology->path);
398 technology_list = g_slist_append(technology_list, technology);
400 technologies_changed();
402 if (technology->driver != NULL)
405 for (list = driver_list; list; list = list->next) {
406 struct connman_technology_driver *driver = list->data;
408 DBG("driver %p name %s", driver, driver->name);
410 if (driver->type != technology->type)
413 if (driver->probe(technology) == 0) {
414 technology->driver = driver;
420 DBG("technology %p", technology);
425 static void technology_put(struct connman_technology *technology)
427 DBG("technology %p", technology);
429 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
432 if (technology->driver) {
433 technology->driver->remove(technology);
434 technology->driver = NULL;
437 technology_list = g_slist_remove(technology_list, technology);
439 technologies_changed();
441 g_dbus_unregister_interface(connection, technology->path,
442 CONNMAN_TECHNOLOGY_INTERFACE);
444 g_slist_free(technology->device_list);
445 g_hash_table_destroy(technology->rfkill_list);
447 g_free(technology->path);
451 static void unregister_technology(gpointer data)
453 struct connman_technology *technology = data;
455 technology_put(technology);
458 int __connman_technology_add_device(struct connman_device *device)
460 struct connman_technology *technology;
461 enum connman_service_type type;
463 DBG("device %p", device);
465 type = __connman_device_get_service_type(device);
466 __connman_notifier_register(type);
468 technology = technology_get(type);
469 if (technology == NULL)
472 g_hash_table_insert(device_table, device, technology);
474 if (g_atomic_int_get(&technology->blocked))
477 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
479 state_changed(technology);
483 technology->device_list = g_slist_append(technology->device_list,
489 int __connman_technology_remove_device(struct connman_device *device)
491 struct connman_technology *technology;
492 enum connman_service_type type;
494 DBG("device %p", device);
496 type = __connman_device_get_service_type(device);
497 __connman_notifier_unregister(type);
499 technology = g_hash_table_lookup(device_table, device);
500 if (technology == NULL)
503 technology->device_list = g_slist_remove(technology->device_list,
505 if (technology->device_list == NULL) {
506 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
507 state_changed(technology);
510 g_hash_table_remove(device_table, device);
515 int __connman_technology_enable_device(struct connman_device *device)
517 struct connman_technology *technology;
518 enum connman_service_type type;
520 DBG("device %p", device);
522 technology = g_hash_table_lookup(device_table, device);
523 if (technology == NULL)
526 if (g_atomic_int_get(&technology->blocked))
529 type = __connman_device_get_service_type(device);
530 __connman_notifier_enable(type);
532 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
533 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
534 state_changed(technology);
540 int __connman_technology_disable_device(struct connman_device *device)
542 struct connman_technology *technology;
543 enum connman_service_type type;
546 DBG("device %p", device);
548 type = __connman_device_get_service_type(device);
549 __connman_notifier_disable(type);
551 technology = g_hash_table_lookup(device_table, device);
552 if (technology == NULL)
555 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
556 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
557 state_changed(technology);
560 for (list = technology->device_list; list; list = list->next) {
561 struct connman_device *device = list->data;
563 if (__connman_device_get_blocked(device) == FALSE)
567 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
568 state_changed(technology);
573 static void technology_blocked(struct connman_technology *technology,
574 connman_bool_t blocked)
578 for (list = technology->device_list; list; list = list->next) {
579 struct connman_device *device = list->data;
581 __connman_device_set_blocked(device, blocked);
585 int __connman_technology_add_rfkill(unsigned int index,
586 enum connman_service_type type,
587 connman_bool_t softblock,
588 connman_bool_t hardblock)
590 struct connman_technology *technology;
591 struct connman_rfkill *rfkill;
592 connman_bool_t blocked;
594 DBG("index %u type %d soft %u hard %u", index, type,
595 softblock, hardblock);
597 technology = technology_get(type);
598 if (technology == NULL)
601 rfkill = g_try_new0(struct connman_rfkill, 1);
605 rfkill->index = index;
607 rfkill->softblock = softblock;
608 rfkill->hardblock = hardblock;
610 g_hash_table_replace(rfkill_table, &rfkill->index, technology);
612 g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
614 blocked = (softblock || hardblock) ? TRUE : FALSE;
615 if (blocked == FALSE)
618 if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
619 technology_blocked(technology, TRUE);
621 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
622 state_changed(technology);
628 int __connman_technology_update_rfkill(unsigned int index,
629 connman_bool_t softblock,
630 connman_bool_t hardblock)
632 struct connman_technology *technology;
633 struct connman_rfkill *rfkill;
634 connman_bool_t blocked, old_blocked;
636 DBG("index %u soft %u hard %u", index, softblock, hardblock);
638 technology = g_hash_table_lookup(rfkill_table, &index);
639 if (technology == NULL)
642 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
646 old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
647 blocked = (softblock || hardblock) ? TRUE : FALSE;
649 rfkill->softblock = softblock;
650 rfkill->hardblock = hardblock;
652 if (blocked == old_blocked)
659 g_atomic_int_exchange_and_add(&technology->blocked, 1);
660 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
663 technology_blocked(technology, blocked);
664 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
665 state_changed(technology);
667 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
670 technology_blocked(technology, blocked);
671 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
672 state_changed(technology);
678 int __connman_technology_remove_rfkill(unsigned int index)
680 struct connman_technology *technology;
682 DBG("index %u", index);
684 technology = g_hash_table_lookup(rfkill_table, &index);
685 if (technology == NULL)
688 g_hash_table_remove(technology->rfkill_list, &index);
690 g_hash_table_remove(rfkill_table, &index);
692 if (g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
693 technology_blocked(technology, FALSE);
694 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
695 state_changed(technology);
701 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
703 struct connman_technology *technology;
705 technology = technology_get(type);
706 if (technology == NULL)
709 if (g_atomic_int_get(&technology->blocked))
715 int __connman_technology_init(void)
719 connection = connman_dbus_get_connection();
721 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
722 NULL, unregister_technology);
723 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
724 NULL, unregister_technology);
729 void __connman_technology_cleanup(void)
733 g_hash_table_destroy(device_table);
734 g_hash_table_destroy(rfkill_table);
736 dbus_connection_unref(connection);