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, bridge, enabled);
197 int __connman_technology_enable_tethering(const char *bridge)
199 return set_tethering(bridge, TRUE);
202 int __connman_technology_disable_tethering(const char *bridge)
204 return set_tethering(bridge, 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 const char *state2string(enum connman_technology_state state)
239 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
241 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
243 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
245 case CONNMAN_TECHNOLOGY_STATE_BLOCKED:
247 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
249 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
256 static void state_changed(struct connman_technology *technology)
260 str = state2string(technology->state);
264 connman_dbus_property_changed_basic(technology->path,
265 CONNMAN_TECHNOLOGY_INTERFACE, "State",
266 DBUS_TYPE_STRING, &str);
269 static const char *get_name(enum connman_service_type type)
272 case CONNMAN_SERVICE_TYPE_UNKNOWN:
273 case CONNMAN_SERVICE_TYPE_SYSTEM:
274 case CONNMAN_SERVICE_TYPE_GPS:
275 case CONNMAN_SERVICE_TYPE_VPN:
277 case CONNMAN_SERVICE_TYPE_ETHERNET:
279 case CONNMAN_SERVICE_TYPE_WIFI:
281 case CONNMAN_SERVICE_TYPE_WIMAX:
283 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
285 case CONNMAN_SERVICE_TYPE_CELLULAR:
292 static DBusMessage *get_properties(DBusConnection *conn,
293 DBusMessage *message, void *user_data)
295 struct connman_technology *technology = user_data;
297 DBusMessageIter array, dict;
300 reply = dbus_message_new_method_return(message);
304 dbus_message_iter_init_append(reply, &array);
306 connman_dbus_dict_open(&array, &dict);
308 str = state2string(technology->state);
310 connman_dbus_dict_append_basic(&dict, "State",
311 DBUS_TYPE_STRING, &str);
313 str = get_name(technology->type);
315 connman_dbus_dict_append_basic(&dict, "Name",
316 DBUS_TYPE_STRING, &str);
318 str = __connman_service_type2string(technology->type);
320 connman_dbus_dict_append_basic(&dict, "Type",
321 DBUS_TYPE_STRING, &str);
323 connman_dbus_dict_close(&array, &dict);
328 static GDBusMethodTable technology_methods[] = {
329 { "GetProperties", "", "a{sv}", get_properties },
333 static GDBusSignalTable technology_signals[] = {
334 { "PropertyChanged", "sv" },
338 static struct connman_technology *technology_find(enum connman_service_type type)
342 DBG("type %d", type);
344 for (list = technology_list; list; list = list->next) {
345 struct connman_technology *technology = list->data;
347 if (technology->type == type)
354 static struct connman_technology *technology_get(enum connman_service_type type)
356 struct connman_technology *technology;
360 DBG("type %d", type);
362 technology = technology_find(type);
363 if (technology != NULL) {
364 g_atomic_int_inc(&technology->refcount);
368 str = __connman_service_type2string(type);
372 technology = g_try_new0(struct connman_technology, 1);
373 if (technology == NULL)
376 technology->refcount = 1;
378 technology->type = type;
379 technology->path = g_strdup_printf("%s/technology/%s",
382 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
384 technology->device_list = NULL;
386 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
388 if (g_dbus_register_interface(connection, technology->path,
389 CONNMAN_TECHNOLOGY_INTERFACE,
390 technology_methods, technology_signals,
391 NULL, technology, NULL) == FALSE) {
392 connman_error("Failed to register %s", technology->path);
397 technology_list = g_slist_append(technology_list, technology);
399 technologies_changed();
401 if (technology->driver != NULL)
404 for (list = driver_list; list; list = list->next) {
405 struct connman_technology_driver *driver = list->data;
407 DBG("driver %p name %s", driver, driver->name);
409 if (driver->type != technology->type)
412 if (driver->probe(technology) == 0) {
413 technology->driver = driver;
419 DBG("technology %p", technology);
424 static void technology_put(struct connman_technology *technology)
426 DBG("technology %p", technology);
428 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
431 if (technology->driver) {
432 technology->driver->remove(technology);
433 technology->driver = NULL;
436 technology_list = g_slist_remove(technology_list, technology);
438 technologies_changed();
440 g_dbus_unregister_interface(connection, technology->path,
441 CONNMAN_TECHNOLOGY_INTERFACE);
443 g_slist_free(technology->device_list);
444 g_hash_table_destroy(technology->rfkill_list);
446 g_free(technology->path);
450 static void unregister_technology(gpointer data)
452 struct connman_technology *technology = data;
454 technology_put(technology);
457 int __connman_technology_add_device(struct connman_device *device)
459 struct connman_technology *technology;
460 enum connman_service_type type;
462 DBG("device %p", device);
464 type = __connman_device_get_service_type(device);
465 __connman_notifier_register(type);
467 technology = technology_get(type);
468 if (technology == NULL)
471 g_hash_table_insert(device_table, device, technology);
473 if (g_atomic_int_get(&technology->blocked))
476 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
478 state_changed(technology);
482 technology->device_list = g_slist_append(technology->device_list,
488 int __connman_technology_remove_device(struct connman_device *device)
490 struct connman_technology *technology;
491 enum connman_service_type type;
493 DBG("device %p", device);
495 type = __connman_device_get_service_type(device);
496 __connman_notifier_unregister(type);
498 technology = g_hash_table_lookup(device_table, device);
499 if (technology == NULL)
502 technology->device_list = g_slist_remove(technology->device_list,
504 if (technology->device_list == NULL) {
505 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
506 state_changed(technology);
509 g_hash_table_remove(device_table, device);
514 int __connman_technology_enable_device(struct connman_device *device)
516 struct connman_technology *technology;
517 enum connman_service_type type;
519 DBG("device %p", device);
521 technology = g_hash_table_lookup(device_table, device);
522 if (technology == NULL)
525 if (g_atomic_int_get(&technology->blocked))
528 type = __connman_device_get_service_type(device);
529 __connman_notifier_enable(type);
531 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
532 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
533 state_changed(technology);
539 int __connman_technology_disable_device(struct connman_device *device)
541 struct connman_technology *technology;
542 enum connman_service_type type;
545 DBG("device %p", device);
547 type = __connman_device_get_service_type(device);
548 __connman_notifier_disable(type);
550 technology = g_hash_table_lookup(device_table, device);
551 if (technology == NULL)
554 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
555 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
556 state_changed(technology);
559 for (list = technology->device_list; list; list = list->next) {
560 struct connman_device *device = list->data;
562 if (__connman_device_get_blocked(device) == FALSE)
566 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
567 state_changed(technology);
572 static void technology_blocked(struct connman_technology *technology,
573 connman_bool_t blocked)
577 for (list = technology->device_list; list; list = list->next) {
578 struct connman_device *device = list->data;
580 __connman_device_set_blocked(device, blocked);
584 int __connman_technology_add_rfkill(unsigned int index,
585 enum connman_service_type type,
586 connman_bool_t softblock,
587 connman_bool_t hardblock)
589 struct connman_technology *technology;
590 struct connman_rfkill *rfkill;
591 connman_bool_t blocked;
593 DBG("index %u type %d soft %u hard %u", index, type,
594 softblock, hardblock);
596 technology = technology_get(type);
597 if (technology == NULL)
600 rfkill = g_try_new0(struct connman_rfkill, 1);
604 rfkill->index = index;
606 rfkill->softblock = softblock;
607 rfkill->hardblock = hardblock;
609 g_hash_table_replace(rfkill_table, &rfkill->index, technology);
611 g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
613 blocked = (softblock || hardblock) ? TRUE : FALSE;
614 if (blocked == FALSE)
617 if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
618 technology_blocked(technology, TRUE);
620 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
621 state_changed(technology);
627 int __connman_technology_update_rfkill(unsigned int index,
628 connman_bool_t softblock,
629 connman_bool_t hardblock)
631 struct connman_technology *technology;
632 struct connman_rfkill *rfkill;
633 connman_bool_t blocked, old_blocked;
635 DBG("index %u soft %u hard %u", index, softblock, hardblock);
637 technology = g_hash_table_lookup(rfkill_table, &index);
638 if (technology == NULL)
641 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
645 old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
646 blocked = (softblock || hardblock) ? TRUE : FALSE;
648 rfkill->softblock = softblock;
649 rfkill->hardblock = hardblock;
651 if (blocked == old_blocked)
658 g_atomic_int_exchange_and_add(&technology->blocked, 1);
659 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
662 technology_blocked(technology, blocked);
663 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
664 state_changed(technology);
666 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
669 technology_blocked(technology, blocked);
670 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
671 state_changed(technology);
677 int __connman_technology_remove_rfkill(unsigned int index)
679 struct connman_technology *technology;
681 DBG("index %u", index);
683 technology = g_hash_table_lookup(rfkill_table, &index);
684 if (technology == NULL)
687 g_hash_table_remove(technology->rfkill_list, &index);
689 g_hash_table_remove(rfkill_table, &index);
691 if (g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
692 technology_blocked(technology, FALSE);
693 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
694 state_changed(technology);
700 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
702 struct connman_technology *technology;
704 technology = technology_get(type);
705 if (technology == NULL)
708 if (g_atomic_int_get(&technology->blocked))
714 int __connman_technology_init(void)
718 connection = connman_dbus_get_connection();
720 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
721 NULL, unregister_technology);
722 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
723 NULL, unregister_technology);
728 void __connman_technology_cleanup(void)
732 g_hash_table_destroy(device_table);
733 g_hash_table_destroy(rfkill_table);
735 dbus_connection_unref(connection);