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:
123 case CONNMAN_SERVICE_TYPE_GADGET:
127 connman_info("Create interface %s [ %s ]", name,
128 __connman_service_type2string(type));
130 for (list = technology_list; list; list = list->next) {
131 struct connman_technology *technology = list->data;
133 if (technology->type != type)
136 if (technology->driver == NULL)
139 if (technology->driver->add_interface)
140 technology->driver->add_interface(technology,
145 void __connman_technology_remove_interface(enum connman_service_type type,
146 int index, const char *name, const char *ident)
151 case CONNMAN_SERVICE_TYPE_UNKNOWN:
152 case CONNMAN_SERVICE_TYPE_SYSTEM:
154 case CONNMAN_SERVICE_TYPE_ETHERNET:
155 case CONNMAN_SERVICE_TYPE_WIFI:
156 case CONNMAN_SERVICE_TYPE_WIMAX:
157 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
158 case CONNMAN_SERVICE_TYPE_CELLULAR:
159 case CONNMAN_SERVICE_TYPE_GPS:
160 case CONNMAN_SERVICE_TYPE_VPN:
161 case CONNMAN_SERVICE_TYPE_GADGET:
165 connman_info("Remove interface %s [ %s ]", name,
166 __connman_service_type2string(type));
168 for (list = technology_list; list; list = list->next) {
169 struct connman_technology *technology = list->data;
171 if (technology->type != type)
174 if (technology->driver == NULL)
177 if (technology->driver->remove_interface)
178 technology->driver->remove_interface(technology, index);
182 void connman_technology_tethering_notify(struct connman_technology *technology,
183 connman_bool_t enabled)
185 DBG("technology %p enabled %u", technology, enabled);
188 __connman_tethering_set_enabled();
190 __connman_tethering_set_disabled();
193 static int set_tethering(const char *bridge, connman_bool_t enabled)
197 for (list = technology_list; list; list = list->next) {
198 struct connman_technology *technology = list->data;
200 if (technology->driver == NULL)
203 if (technology->driver->set_tethering)
204 technology->driver->set_tethering(technology,
211 int __connman_technology_enable_tethering(const char *bridge)
213 return set_tethering(bridge, TRUE);
216 int __connman_technology_disable_tethering(const char *bridge)
218 return set_tethering(bridge, FALSE);
221 int __connman_technology_set_regdom(const char *alpha2)
225 for (list = technology_list; list; list = list->next) {
226 struct connman_technology *technology = list->data;
228 if (technology->driver == NULL)
231 if (technology->driver->set_regdom)
232 technology->driver->set_regdom(technology, alpha2);
238 static void free_rfkill(gpointer data)
240 struct connman_rfkill *rfkill = data;
245 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
249 for (list = technology_list; list; list = list->next) {
250 struct connman_technology *technology = list->data;
252 if (technology->path == NULL)
255 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
260 static void technologies_changed(void)
262 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
263 CONNMAN_MANAGER_INTERFACE, "Technologies",
264 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
267 static const char *state2string(enum connman_technology_state state)
270 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
272 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
274 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
276 case CONNMAN_TECHNOLOGY_STATE_BLOCKED:
278 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
280 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
287 static void state_changed(struct connman_technology *technology)
291 str = state2string(technology->state);
295 connman_dbus_property_changed_basic(technology->path,
296 CONNMAN_TECHNOLOGY_INTERFACE, "State",
297 DBUS_TYPE_STRING, &str);
300 static const char *get_name(enum connman_service_type type)
303 case CONNMAN_SERVICE_TYPE_UNKNOWN:
304 case CONNMAN_SERVICE_TYPE_SYSTEM:
305 case CONNMAN_SERVICE_TYPE_GPS:
306 case CONNMAN_SERVICE_TYPE_VPN:
307 case CONNMAN_SERVICE_TYPE_GADGET:
309 case CONNMAN_SERVICE_TYPE_ETHERNET:
311 case CONNMAN_SERVICE_TYPE_WIFI:
313 case CONNMAN_SERVICE_TYPE_WIMAX:
315 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
317 case CONNMAN_SERVICE_TYPE_CELLULAR:
324 static DBusMessage *get_properties(DBusConnection *conn,
325 DBusMessage *message, void *user_data)
327 struct connman_technology *technology = user_data;
329 DBusMessageIter array, dict;
332 reply = dbus_message_new_method_return(message);
336 dbus_message_iter_init_append(reply, &array);
338 connman_dbus_dict_open(&array, &dict);
340 str = state2string(technology->state);
342 connman_dbus_dict_append_basic(&dict, "State",
343 DBUS_TYPE_STRING, &str);
345 str = get_name(technology->type);
347 connman_dbus_dict_append_basic(&dict, "Name",
348 DBUS_TYPE_STRING, &str);
350 str = __connman_service_type2string(technology->type);
352 connman_dbus_dict_append_basic(&dict, "Type",
353 DBUS_TYPE_STRING, &str);
355 connman_dbus_dict_close(&array, &dict);
360 static GDBusMethodTable technology_methods[] = {
361 { "GetProperties", "", "a{sv}", get_properties },
365 static GDBusSignalTable technology_signals[] = {
366 { "PropertyChanged", "sv" },
370 static struct connman_technology *technology_find(enum connman_service_type type)
374 DBG("type %d", type);
376 for (list = technology_list; list; list = list->next) {
377 struct connman_technology *technology = list->data;
379 if (technology->type == type)
386 static struct connman_technology *technology_get(enum connman_service_type type)
388 struct connman_technology *technology;
392 DBG("type %d", type);
394 technology = technology_find(type);
395 if (technology != NULL) {
396 g_atomic_int_inc(&technology->refcount);
400 str = __connman_service_type2string(type);
404 technology = g_try_new0(struct connman_technology, 1);
405 if (technology == NULL)
408 technology->refcount = 1;
410 technology->type = type;
411 technology->path = g_strdup_printf("%s/technology/%s",
414 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
416 technology->device_list = NULL;
418 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
420 if (g_dbus_register_interface(connection, technology->path,
421 CONNMAN_TECHNOLOGY_INTERFACE,
422 technology_methods, technology_signals,
423 NULL, technology, NULL) == FALSE) {
424 connman_error("Failed to register %s", technology->path);
429 technology_list = g_slist_append(technology_list, technology);
431 technologies_changed();
433 if (technology->driver != NULL)
436 for (list = driver_list; list; list = list->next) {
437 struct connman_technology_driver *driver = list->data;
439 DBG("driver %p name %s", driver, driver->name);
441 if (driver->type != technology->type)
444 if (driver->probe(technology) == 0) {
445 technology->driver = driver;
451 DBG("technology %p", technology);
456 static void technology_put(struct connman_technology *technology)
458 DBG("technology %p", technology);
460 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
463 if (technology->driver) {
464 technology->driver->remove(technology);
465 technology->driver = NULL;
468 technology_list = g_slist_remove(technology_list, technology);
470 technologies_changed();
472 g_dbus_unregister_interface(connection, technology->path,
473 CONNMAN_TECHNOLOGY_INTERFACE);
475 g_slist_free(technology->device_list);
476 g_hash_table_destroy(technology->rfkill_list);
478 g_free(technology->path);
482 static void unregister_technology(gpointer data)
484 struct connman_technology *technology = data;
486 technology_put(technology);
489 int __connman_technology_add_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_register(type);
499 technology = technology_get(type);
500 if (technology == NULL)
503 g_hash_table_insert(device_table, device, technology);
505 if (g_atomic_int_get(&technology->blocked))
508 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
510 state_changed(technology);
514 technology->device_list = g_slist_append(technology->device_list,
520 int __connman_technology_remove_device(struct connman_device *device)
522 struct connman_technology *technology;
523 enum connman_service_type type;
525 DBG("device %p", device);
527 type = __connman_device_get_service_type(device);
528 __connman_notifier_unregister(type);
530 technology = g_hash_table_lookup(device_table, device);
531 if (technology == NULL)
534 technology->device_list = g_slist_remove(technology->device_list,
536 if (technology->device_list == NULL) {
537 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
538 state_changed(technology);
541 g_hash_table_remove(device_table, device);
546 int __connman_technology_enable_device(struct connman_device *device)
548 struct connman_technology *technology;
549 enum connman_service_type type;
551 DBG("device %p", device);
553 technology = g_hash_table_lookup(device_table, device);
554 if (technology == NULL)
557 if (g_atomic_int_get(&technology->blocked))
560 type = __connman_device_get_service_type(device);
561 __connman_notifier_enable(type);
563 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
564 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
565 state_changed(technology);
571 int __connman_technology_disable_device(struct connman_device *device)
573 struct connman_technology *technology;
574 enum connman_service_type type;
577 DBG("device %p", device);
579 type = __connman_device_get_service_type(device);
580 __connman_notifier_disable(type);
582 technology = g_hash_table_lookup(device_table, device);
583 if (technology == NULL)
586 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
587 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
588 state_changed(technology);
591 for (list = technology->device_list; list; list = list->next) {
592 struct connman_device *device = list->data;
594 if (__connman_device_get_blocked(device) == FALSE)
598 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
599 state_changed(technology);
604 static void technology_blocked(struct connman_technology *technology,
605 connman_bool_t blocked)
609 for (list = technology->device_list; list; list = list->next) {
610 struct connman_device *device = list->data;
612 __connman_device_set_blocked(device, blocked);
616 int __connman_technology_add_rfkill(unsigned int index,
617 enum connman_service_type type,
618 connman_bool_t softblock,
619 connman_bool_t hardblock)
621 struct connman_technology *technology;
622 struct connman_rfkill *rfkill;
623 connman_bool_t blocked;
625 DBG("index %u type %d soft %u hard %u", index, type,
626 softblock, hardblock);
628 technology = technology_get(type);
629 if (technology == NULL)
632 rfkill = g_try_new0(struct connman_rfkill, 1);
636 rfkill->index = index;
638 rfkill->softblock = softblock;
639 rfkill->hardblock = hardblock;
641 g_hash_table_replace(rfkill_table, &rfkill->index, technology);
643 g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
645 blocked = (softblock || hardblock) ? TRUE : FALSE;
646 if (blocked == FALSE)
649 if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
650 technology_blocked(technology, TRUE);
652 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
653 state_changed(technology);
659 int __connman_technology_update_rfkill(unsigned int index,
660 connman_bool_t softblock,
661 connman_bool_t hardblock)
663 struct connman_technology *technology;
664 struct connman_rfkill *rfkill;
665 connman_bool_t blocked, old_blocked;
667 DBG("index %u soft %u hard %u", index, softblock, hardblock);
669 technology = g_hash_table_lookup(rfkill_table, &index);
670 if (technology == NULL)
673 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
677 old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
678 blocked = (softblock || hardblock) ? TRUE : FALSE;
680 rfkill->softblock = softblock;
681 rfkill->hardblock = hardblock;
683 if (blocked == old_blocked)
690 g_atomic_int_exchange_and_add(&technology->blocked, 1);
691 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
694 technology_blocked(technology, blocked);
695 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
696 state_changed(technology);
698 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
701 technology_blocked(technology, blocked);
702 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
703 state_changed(technology);
709 int __connman_technology_remove_rfkill(unsigned int index)
711 struct connman_technology *technology;
712 struct connman_rfkill *rfkill;
713 connman_bool_t blocked;
715 DBG("index %u", index);
717 technology = g_hash_table_lookup(rfkill_table, &index);
718 if (technology == NULL)
721 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
725 blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
727 g_hash_table_remove(technology->rfkill_list, &index);
729 g_hash_table_remove(rfkill_table, &index);
732 g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
733 technology_blocked(technology, FALSE);
734 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
735 state_changed(technology);
741 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
743 struct connman_technology *technology;
745 technology = technology_find(type);
746 if (technology == NULL)
749 if (g_atomic_int_get(&technology->blocked))
755 int __connman_technology_init(void)
759 connection = connman_dbus_get_connection();
761 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
762 NULL, unregister_technology);
763 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
764 NULL, unregister_technology);
769 void __connman_technology_cleanup(void)
773 g_hash_table_destroy(device_table);
774 g_hash_table_destroy(rfkill_table);
776 dbus_connection_unref(connection);