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(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, enabled);
197 int __connman_technology_enable_tethering(void)
199 return set_tethering(TRUE);
202 int __connman_technology_disable_tethering(void)
204 return set_tethering(FALSE);
207 static void free_rfkill(gpointer data)
209 struct connman_rfkill *rfkill = data;
214 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
218 for (list = technology_list; list; list = list->next) {
219 struct connman_technology *technology = list->data;
221 if (technology->path == NULL)
224 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
229 static void technologies_changed(void)
231 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
232 CONNMAN_MANAGER_INTERFACE, "Technologies",
233 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
236 static void device_list(DBusMessageIter *iter, void *user_data)
238 struct connman_technology *technology = user_data;
241 for (list = technology->device_list; list; list = list->next) {
242 struct connman_device *device = list->data;
245 path = connman_device_get_path(device);
249 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
254 static void devices_changed(struct connman_technology *technology)
256 connman_dbus_property_changed_array(technology->path,
257 CONNMAN_TECHNOLOGY_INTERFACE, "Devices",
258 DBUS_TYPE_OBJECT_PATH, device_list, technology);
261 static const char *state2string(enum connman_technology_state state)
264 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
266 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
268 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
270 case CONNMAN_TECHNOLOGY_STATE_BLOCKED:
272 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
274 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
281 static void state_changed(struct connman_technology *technology)
285 str = state2string(technology->state);
289 connman_dbus_property_changed_basic(technology->path,
290 CONNMAN_TECHNOLOGY_INTERFACE, "State",
291 DBUS_TYPE_STRING, &str);
294 static const char *get_name(enum connman_service_type type)
297 case CONNMAN_SERVICE_TYPE_UNKNOWN:
298 case CONNMAN_SERVICE_TYPE_SYSTEM:
299 case CONNMAN_SERVICE_TYPE_GPS:
300 case CONNMAN_SERVICE_TYPE_VPN:
302 case CONNMAN_SERVICE_TYPE_ETHERNET:
304 case CONNMAN_SERVICE_TYPE_WIFI:
306 case CONNMAN_SERVICE_TYPE_WIMAX:
308 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
310 case CONNMAN_SERVICE_TYPE_CELLULAR:
317 static DBusMessage *get_properties(DBusConnection *conn,
318 DBusMessage *message, void *user_data)
320 struct connman_technology *technology = user_data;
322 DBusMessageIter array, dict;
325 reply = dbus_message_new_method_return(message);
329 dbus_message_iter_init_append(reply, &array);
331 connman_dbus_dict_open(&array, &dict);
333 str = state2string(technology->state);
335 connman_dbus_dict_append_basic(&dict, "State",
336 DBUS_TYPE_STRING, &str);
338 str = get_name(technology->type);
340 connman_dbus_dict_append_basic(&dict, "Name",
341 DBUS_TYPE_STRING, &str);
343 str = __connman_service_type2string(technology->type);
345 connman_dbus_dict_append_basic(&dict, "Type",
346 DBUS_TYPE_STRING, &str);
348 connman_dbus_dict_append_array(&dict, "Devices",
349 DBUS_TYPE_OBJECT_PATH, device_list, technology);
351 connman_dbus_dict_close(&array, &dict);
356 static GDBusMethodTable technology_methods[] = {
357 { "GetProperties", "", "a{sv}", get_properties },
361 static GDBusSignalTable technology_signals[] = {
362 { "PropertyChanged", "sv" },
366 static struct connman_technology *technology_find(enum connman_service_type type)
370 DBG("type %d", type);
372 for (list = technology_list; list; list = list->next) {
373 struct connman_technology *technology = list->data;
375 if (technology->type == type)
382 static struct connman_technology *technology_get(enum connman_service_type type)
384 struct connman_technology *technology;
388 DBG("type %d", type);
390 technology = technology_find(type);
391 if (technology != NULL) {
392 g_atomic_int_inc(&technology->refcount);
396 str = __connman_service_type2string(type);
400 technology = g_try_new0(struct connman_technology, 1);
401 if (technology == NULL)
404 technology->refcount = 1;
406 technology->type = type;
407 technology->path = g_strdup_printf("%s/technology/%s",
410 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
412 technology->device_list = NULL;
414 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
416 if (g_dbus_register_interface(connection, technology->path,
417 CONNMAN_TECHNOLOGY_INTERFACE,
418 technology_methods, technology_signals,
419 NULL, technology, NULL) == FALSE) {
420 connman_error("Failed to register %s", technology->path);
425 technology_list = g_slist_append(technology_list, technology);
427 technologies_changed();
429 if (technology->driver != NULL)
432 for (list = driver_list; list; list = list->next) {
433 struct connman_technology_driver *driver = list->data;
435 DBG("driver %p name %s", driver, driver->name);
437 if (driver->type != technology->type)
440 if (driver->probe(technology) == 0) {
441 technology->driver = driver;
447 DBG("technology %p", technology);
452 static void technology_put(struct connman_technology *technology)
454 DBG("technology %p", technology);
456 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
459 if (technology->driver) {
460 technology->driver->remove(technology);
461 technology->driver = NULL;
464 technology_list = g_slist_remove(technology_list, technology);
466 technologies_changed();
468 g_dbus_unregister_interface(connection, technology->path,
469 CONNMAN_TECHNOLOGY_INTERFACE);
471 g_slist_free(technology->device_list);
472 g_hash_table_destroy(technology->rfkill_list);
474 g_free(technology->path);
478 static void unregister_technology(gpointer data)
480 struct connman_technology *technology = data;
482 technology_put(technology);
485 int __connman_technology_add_device(struct connman_device *device)
487 struct connman_technology *technology;
488 enum connman_service_type type;
490 DBG("device %p", device);
492 type = __connman_device_get_service_type(device);
493 __connman_notifier_register(type);
495 technology = technology_get(type);
496 if (technology == NULL)
499 g_hash_table_insert(device_table, device, technology);
501 if (g_atomic_int_get(&technology->blocked))
504 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
506 state_changed(technology);
510 technology->device_list = g_slist_append(technology->device_list,
512 devices_changed(technology);
517 int __connman_technology_remove_device(struct connman_device *device)
519 struct connman_technology *technology;
520 enum connman_service_type type;
522 DBG("device %p", device);
524 type = __connman_device_get_service_type(device);
525 __connman_notifier_unregister(type);
527 technology = g_hash_table_lookup(device_table, device);
528 if (technology == NULL)
531 technology->device_list = g_slist_remove(technology->device_list,
533 devices_changed(technology);
535 if (technology->device_list == NULL) {
536 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
537 state_changed(technology);
540 g_hash_table_remove(device_table, device);
545 int __connman_technology_enable_device(struct connman_device *device)
547 struct connman_technology *technology;
548 enum connman_service_type type;
550 DBG("device %p", device);
552 technology = g_hash_table_lookup(device_table, device);
553 if (technology == NULL)
556 if (g_atomic_int_get(&technology->blocked))
559 type = __connman_device_get_service_type(device);
560 __connman_notifier_enable(type);
562 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
563 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
564 state_changed(technology);
570 int __connman_technology_disable_device(struct connman_device *device)
572 struct connman_technology *technology;
573 enum connman_service_type type;
576 DBG("device %p", device);
578 type = __connman_device_get_service_type(device);
579 __connman_notifier_disable(type);
581 technology = g_hash_table_lookup(device_table, device);
582 if (technology == NULL)
585 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
586 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
587 state_changed(technology);
590 for (list = technology->device_list; list; list = list->next) {
591 struct connman_device *device = list->data;
593 if (__connman_device_get_blocked(device) == FALSE)
597 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
598 state_changed(technology);
603 static void technology_blocked(struct connman_technology *technology,
604 connman_bool_t blocked)
608 for (list = technology->device_list; list; list = list->next) {
609 struct connman_device *device = list->data;
611 __connman_device_set_blocked(device, blocked);
615 int __connman_technology_add_rfkill(unsigned int index,
616 enum connman_service_type type,
617 connman_bool_t softblock,
618 connman_bool_t hardblock)
620 struct connman_technology *technology;
621 struct connman_rfkill *rfkill;
622 connman_bool_t blocked;
624 DBG("index %u type %d soft %u hard %u", index, type,
625 softblock, hardblock);
627 technology = technology_get(type);
628 if (technology == NULL)
631 rfkill = g_try_new0(struct connman_rfkill, 1);
635 rfkill->index = index;
637 rfkill->softblock = softblock;
638 rfkill->hardblock = hardblock;
640 g_hash_table_replace(rfkill_table, &rfkill->index, technology);
642 g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
644 blocked = (softblock || hardblock) ? TRUE : FALSE;
645 if (blocked == FALSE)
648 if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
649 technology_blocked(technology, TRUE);
651 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
652 state_changed(technology);
658 int __connman_technology_update_rfkill(unsigned int index,
659 connman_bool_t softblock,
660 connman_bool_t hardblock)
662 struct connman_technology *technology;
663 struct connman_rfkill *rfkill;
664 connman_bool_t blocked;
666 DBG("index %u soft %u hard %u", index, softblock, hardblock);
668 technology = g_hash_table_lookup(rfkill_table, &index);
669 if (technology == NULL)
672 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
676 rfkill->softblock = softblock;
677 rfkill->hardblock = hardblock;
679 blocked = (softblock || hardblock) ? TRUE : FALSE;
685 g_atomic_int_exchange_and_add(&technology->blocked, 1);
686 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
689 technology_blocked(technology, blocked);
690 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
691 state_changed(technology);
693 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
696 technology_blocked(technology, blocked);
697 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
698 state_changed(technology);
704 int __connman_technology_remove_rfkill(unsigned int index)
706 struct connman_technology *technology;
708 DBG("index %u", index);
710 technology = g_hash_table_lookup(rfkill_table, &index);
711 if (technology == NULL)
714 g_hash_table_remove(technology->rfkill_list, &index);
716 g_hash_table_remove(rfkill_table, &index);
718 if (g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
719 technology_blocked(technology, FALSE);
720 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
721 state_changed(technology);
727 int __connman_technology_init(void)
731 connection = connman_dbus_get_connection();
733 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
734 NULL, unregister_technology);
735 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
736 NULL, unregister_technology);
741 void __connman_technology_cleanup(void)
745 g_hash_table_destroy(device_table);
746 g_hash_table_destroy(rfkill_table);
748 dbus_connection_unref(connection);