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 void connman_technology_tethering_notify(struct connman_technology *technology,
181 connman_bool_t enabled)
183 DBG("technology %p enabled %u", technology, enabled);
186 __connman_tethering_set_enabled();
188 __connman_tethering_set_disabled();
191 static int set_tethering(const char *bridge, connman_bool_t enabled)
195 for (list = technology_list; list; list = list->next) {
196 struct connman_technology *technology = list->data;
198 if (technology->driver == NULL)
201 if (technology->driver->set_tethering)
202 technology->driver->set_tethering(technology,
209 int __connman_technology_enable_tethering(const char *bridge)
211 return set_tethering(bridge, TRUE);
214 int __connman_technology_disable_tethering(const char *bridge)
216 return set_tethering(bridge, FALSE);
219 static void free_rfkill(gpointer data)
221 struct connman_rfkill *rfkill = data;
226 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
230 for (list = technology_list; list; list = list->next) {
231 struct connman_technology *technology = list->data;
233 if (technology->path == NULL)
236 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
241 static void technologies_changed(void)
243 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
244 CONNMAN_MANAGER_INTERFACE, "Technologies",
245 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
248 static const char *state2string(enum connman_technology_state state)
251 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
253 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
255 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
257 case CONNMAN_TECHNOLOGY_STATE_BLOCKED:
259 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
261 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
268 static void state_changed(struct connman_technology *technology)
272 str = state2string(technology->state);
276 connman_dbus_property_changed_basic(technology->path,
277 CONNMAN_TECHNOLOGY_INTERFACE, "State",
278 DBUS_TYPE_STRING, &str);
281 static const char *get_name(enum connman_service_type type)
284 case CONNMAN_SERVICE_TYPE_UNKNOWN:
285 case CONNMAN_SERVICE_TYPE_SYSTEM:
286 case CONNMAN_SERVICE_TYPE_GPS:
287 case CONNMAN_SERVICE_TYPE_VPN:
289 case CONNMAN_SERVICE_TYPE_ETHERNET:
291 case CONNMAN_SERVICE_TYPE_WIFI:
293 case CONNMAN_SERVICE_TYPE_WIMAX:
295 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
297 case CONNMAN_SERVICE_TYPE_CELLULAR:
304 static DBusMessage *get_properties(DBusConnection *conn,
305 DBusMessage *message, void *user_data)
307 struct connman_technology *technology = user_data;
309 DBusMessageIter array, dict;
312 reply = dbus_message_new_method_return(message);
316 dbus_message_iter_init_append(reply, &array);
318 connman_dbus_dict_open(&array, &dict);
320 str = state2string(technology->state);
322 connman_dbus_dict_append_basic(&dict, "State",
323 DBUS_TYPE_STRING, &str);
325 str = get_name(technology->type);
327 connman_dbus_dict_append_basic(&dict, "Name",
328 DBUS_TYPE_STRING, &str);
330 str = __connman_service_type2string(technology->type);
332 connman_dbus_dict_append_basic(&dict, "Type",
333 DBUS_TYPE_STRING, &str);
335 connman_dbus_dict_close(&array, &dict);
340 static GDBusMethodTable technology_methods[] = {
341 { "GetProperties", "", "a{sv}", get_properties },
345 static GDBusSignalTable technology_signals[] = {
346 { "PropertyChanged", "sv" },
350 static struct connman_technology *technology_find(enum connman_service_type type)
354 DBG("type %d", type);
356 for (list = technology_list; list; list = list->next) {
357 struct connman_technology *technology = list->data;
359 if (technology->type == type)
366 static struct connman_technology *technology_get(enum connman_service_type type)
368 struct connman_technology *technology;
372 DBG("type %d", type);
374 technology = technology_find(type);
375 if (technology != NULL) {
376 g_atomic_int_inc(&technology->refcount);
380 str = __connman_service_type2string(type);
384 technology = g_try_new0(struct connman_technology, 1);
385 if (technology == NULL)
388 technology->refcount = 1;
390 technology->type = type;
391 technology->path = g_strdup_printf("%s/technology/%s",
394 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
396 technology->device_list = NULL;
398 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
400 if (g_dbus_register_interface(connection, technology->path,
401 CONNMAN_TECHNOLOGY_INTERFACE,
402 technology_methods, technology_signals,
403 NULL, technology, NULL) == FALSE) {
404 connman_error("Failed to register %s", technology->path);
409 technology_list = g_slist_append(technology_list, technology);
411 technologies_changed();
413 if (technology->driver != NULL)
416 for (list = driver_list; list; list = list->next) {
417 struct connman_technology_driver *driver = list->data;
419 DBG("driver %p name %s", driver, driver->name);
421 if (driver->type != technology->type)
424 if (driver->probe(technology) == 0) {
425 technology->driver = driver;
431 DBG("technology %p", technology);
436 static void technology_put(struct connman_technology *technology)
438 DBG("technology %p", technology);
440 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
443 if (technology->driver) {
444 technology->driver->remove(technology);
445 technology->driver = NULL;
448 technology_list = g_slist_remove(technology_list, technology);
450 technologies_changed();
452 g_dbus_unregister_interface(connection, technology->path,
453 CONNMAN_TECHNOLOGY_INTERFACE);
455 g_slist_free(technology->device_list);
456 g_hash_table_destroy(technology->rfkill_list);
458 g_free(technology->path);
462 static void unregister_technology(gpointer data)
464 struct connman_technology *technology = data;
466 technology_put(technology);
469 int __connman_technology_add_device(struct connman_device *device)
471 struct connman_technology *technology;
472 enum connman_service_type type;
474 DBG("device %p", device);
476 type = __connman_device_get_service_type(device);
477 __connman_notifier_register(type);
479 technology = technology_get(type);
480 if (technology == NULL)
483 g_hash_table_insert(device_table, device, technology);
485 if (g_atomic_int_get(&technology->blocked))
488 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
490 state_changed(technology);
494 technology->device_list = g_slist_append(technology->device_list,
500 int __connman_technology_remove_device(struct connman_device *device)
502 struct connman_technology *technology;
503 enum connman_service_type type;
505 DBG("device %p", device);
507 type = __connman_device_get_service_type(device);
508 __connman_notifier_unregister(type);
510 technology = g_hash_table_lookup(device_table, device);
511 if (technology == NULL)
514 technology->device_list = g_slist_remove(technology->device_list,
516 if (technology->device_list == NULL) {
517 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
518 state_changed(technology);
521 g_hash_table_remove(device_table, device);
526 int __connman_technology_enable_device(struct connman_device *device)
528 struct connman_technology *technology;
529 enum connman_service_type type;
531 DBG("device %p", device);
533 technology = g_hash_table_lookup(device_table, device);
534 if (technology == NULL)
537 if (g_atomic_int_get(&technology->blocked))
540 type = __connman_device_get_service_type(device);
541 __connman_notifier_enable(type);
543 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
544 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
545 state_changed(technology);
551 int __connman_technology_disable_device(struct connman_device *device)
553 struct connman_technology *technology;
554 enum connman_service_type type;
557 DBG("device %p", device);
559 type = __connman_device_get_service_type(device);
560 __connman_notifier_disable(type);
562 technology = g_hash_table_lookup(device_table, device);
563 if (technology == NULL)
566 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
567 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
568 state_changed(technology);
571 for (list = technology->device_list; list; list = list->next) {
572 struct connman_device *device = list->data;
574 if (__connman_device_get_blocked(device) == FALSE)
578 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
579 state_changed(technology);
584 static void technology_blocked(struct connman_technology *technology,
585 connman_bool_t blocked)
589 for (list = technology->device_list; list; list = list->next) {
590 struct connman_device *device = list->data;
592 __connman_device_set_blocked(device, blocked);
596 int __connman_technology_add_rfkill(unsigned int index,
597 enum connman_service_type type,
598 connman_bool_t softblock,
599 connman_bool_t hardblock)
601 struct connman_technology *technology;
602 struct connman_rfkill *rfkill;
603 connman_bool_t blocked;
605 DBG("index %u type %d soft %u hard %u", index, type,
606 softblock, hardblock);
608 technology = technology_get(type);
609 if (technology == NULL)
612 rfkill = g_try_new0(struct connman_rfkill, 1);
616 rfkill->index = index;
618 rfkill->softblock = softblock;
619 rfkill->hardblock = hardblock;
621 g_hash_table_replace(rfkill_table, &rfkill->index, technology);
623 g_hash_table_replace(technology->rfkill_list, &rfkill->index, rfkill);
625 blocked = (softblock || hardblock) ? TRUE : FALSE;
626 if (blocked == FALSE)
629 if (g_atomic_int_exchange_and_add(&technology->blocked, 1) == 0) {
630 technology_blocked(technology, TRUE);
632 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
633 state_changed(technology);
639 int __connman_technology_update_rfkill(unsigned int index,
640 connman_bool_t softblock,
641 connman_bool_t hardblock)
643 struct connman_technology *technology;
644 struct connman_rfkill *rfkill;
645 connman_bool_t blocked, old_blocked;
647 DBG("index %u soft %u hard %u", index, softblock, hardblock);
649 technology = g_hash_table_lookup(rfkill_table, &index);
650 if (technology == NULL)
653 rfkill = g_hash_table_lookup(technology->rfkill_list, &index);
657 old_blocked = (rfkill->softblock || rfkill->hardblock) ? TRUE : FALSE;
658 blocked = (softblock || hardblock) ? TRUE : FALSE;
660 rfkill->softblock = softblock;
661 rfkill->hardblock = hardblock;
663 if (blocked == old_blocked)
670 g_atomic_int_exchange_and_add(&technology->blocked, 1);
671 if (n_blocked != g_hash_table_size(technology->rfkill_list) - 1)
674 technology_blocked(technology, blocked);
675 technology->state = CONNMAN_TECHNOLOGY_STATE_BLOCKED;
676 state_changed(technology);
678 if (g_atomic_int_dec_and_test(&technology->blocked) == FALSE)
681 technology_blocked(technology, blocked);
682 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
683 state_changed(technology);
689 int __connman_technology_remove_rfkill(unsigned int index)
691 struct connman_technology *technology;
693 DBG("index %u", index);
695 technology = g_hash_table_lookup(rfkill_table, &index);
696 if (technology == NULL)
699 g_hash_table_remove(technology->rfkill_list, &index);
701 g_hash_table_remove(rfkill_table, &index);
703 if (g_atomic_int_dec_and_test(&technology->blocked) == TRUE) {
704 technology_blocked(technology, FALSE);
705 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
706 state_changed(technology);
712 connman_bool_t __connman_technology_get_blocked(enum connman_service_type type)
714 struct connman_technology *technology;
716 technology = technology_get(type);
717 if (technology == NULL)
720 if (g_atomic_int_get(&technology->blocked))
726 int __connman_technology_init(void)
730 connection = connman_dbus_get_connection();
732 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
733 NULL, unregister_technology);
734 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
735 NULL, unregister_technology);
740 void __connman_technology_cleanup(void)
744 g_hash_table_destroy(device_table);
745 g_hash_table_destroy(rfkill_table);
747 dbus_connection_unref(connection);