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;
41 enum connman_technology_state {
42 CONNMAN_TECHNOLOGY_STATE_UNKNOWN = 0,
43 CONNMAN_TECHNOLOGY_STATE_OFFLINE = 1,
44 CONNMAN_TECHNOLOGY_STATE_AVAILABLE = 2,
45 CONNMAN_TECHNOLOGY_STATE_ENABLED = 3,
46 CONNMAN_TECHNOLOGY_STATE_CONNECTED = 4,
49 struct connman_technology {
51 enum connman_service_type type;
52 enum connman_technology_state state;
54 GHashTable *rfkill_list;
58 struct connman_technology_driver *driver;
62 static GSList *driver_list = NULL;
64 static gint compare_priority(gconstpointer a, gconstpointer b)
66 const struct connman_technology_driver *driver1 = a;
67 const struct connman_technology_driver *driver2 = b;
69 return driver2->priority - driver1->priority;
73 * connman_technology_driver_register:
74 * @driver: Technology driver definition
76 * Register a new technology driver
78 * Returns: %0 on success
80 int connman_technology_driver_register(struct connman_technology_driver *driver)
82 DBG("driver %p name %s", driver, driver->name);
84 driver_list = g_slist_insert_sorted(driver_list, driver,
91 * connman_technology_driver_unregister:
92 * @driver: Technology driver definition
94 * Remove a previously registered technology driver
96 void connman_technology_driver_unregister(struct connman_technology_driver *driver)
98 DBG("driver %p name %s", driver, driver->name);
100 driver_list = g_slist_remove(driver_list, driver);
103 void __connman_technology_add_interface(enum connman_service_type type,
104 int index, const char *name, const char *ident)
109 case CONNMAN_SERVICE_TYPE_UNKNOWN:
110 case CONNMAN_SERVICE_TYPE_SYSTEM:
112 case CONNMAN_SERVICE_TYPE_ETHERNET:
113 case CONNMAN_SERVICE_TYPE_WIFI:
114 case CONNMAN_SERVICE_TYPE_WIMAX:
115 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
116 case CONNMAN_SERVICE_TYPE_CELLULAR:
117 case CONNMAN_SERVICE_TYPE_GPS:
118 case CONNMAN_SERVICE_TYPE_VPN:
122 connman_info("Create interface %s [ %s ]", name,
123 __connman_service_type2string(type));
125 for (list = technology_list; list; list = list->next) {
126 struct connman_technology *technology = list->data;
128 if (technology->type != type)
131 if (technology->driver == NULL)
134 if (technology->driver->add_interface)
135 technology->driver->add_interface(technology,
140 void __connman_technology_remove_interface(enum connman_service_type type,
141 int index, const char *name, const char *ident)
146 case CONNMAN_SERVICE_TYPE_UNKNOWN:
147 case CONNMAN_SERVICE_TYPE_SYSTEM:
149 case CONNMAN_SERVICE_TYPE_ETHERNET:
150 case CONNMAN_SERVICE_TYPE_WIFI:
151 case CONNMAN_SERVICE_TYPE_WIMAX:
152 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
153 case CONNMAN_SERVICE_TYPE_CELLULAR:
154 case CONNMAN_SERVICE_TYPE_GPS:
155 case CONNMAN_SERVICE_TYPE_VPN:
159 connman_info("Remove interface %s [ %s ]", name,
160 __connman_service_type2string(type));
162 for (list = technology_list; list; list = list->next) {
163 struct connman_technology *technology = list->data;
165 if (technology->type != type)
168 if (technology->driver == NULL)
171 if (technology->driver->remove_interface)
172 technology->driver->remove_interface(technology, index);
176 static int set_tethering(connman_bool_t enabled)
180 for (list = technology_list; list; list = list->next) {
181 struct connman_technology *technology = list->data;
183 if (technology->driver == NULL)
186 if (technology->driver->set_tethering)
187 technology->driver->set_tethering(technology, enabled);
193 int __connman_technology_enable_tethering(void)
195 return set_tethering(TRUE);
198 int __connman_technology_disable_tethering(void)
200 return set_tethering(FALSE);
203 static void free_rfkill(gpointer data)
205 struct connman_rfkill *rfkill = data;
210 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
214 for (list = technology_list; list; list = list->next) {
215 struct connman_technology *technology = list->data;
217 if (technology->path == NULL)
220 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
225 static void technologies_changed(void)
227 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
228 CONNMAN_MANAGER_INTERFACE, "Technologies",
229 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
232 static void device_list(DBusMessageIter *iter, void *user_data)
234 struct connman_technology *technology = user_data;
237 for (list = technology->device_list; list; list = list->next) {
238 struct connman_device *device = list->data;
241 path = connman_device_get_path(device);
245 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
250 static void devices_changed(struct connman_technology *technology)
252 connman_dbus_property_changed_array(technology->path,
253 CONNMAN_TECHNOLOGY_INTERFACE, "Devices",
254 DBUS_TYPE_OBJECT_PATH, device_list, technology);
257 static const char *state2string(enum connman_technology_state state)
260 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
262 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
264 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
266 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
268 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
275 static void state_changed(struct connman_technology *technology)
279 str = state2string(technology->state);
283 connman_dbus_property_changed_basic(technology->path,
284 CONNMAN_TECHNOLOGY_INTERFACE, "State",
285 DBUS_TYPE_STRING, &str);
288 static const char *get_name(enum connman_service_type type)
291 case CONNMAN_SERVICE_TYPE_UNKNOWN:
292 case CONNMAN_SERVICE_TYPE_SYSTEM:
293 case CONNMAN_SERVICE_TYPE_GPS:
294 case CONNMAN_SERVICE_TYPE_VPN:
296 case CONNMAN_SERVICE_TYPE_ETHERNET:
298 case CONNMAN_SERVICE_TYPE_WIFI:
300 case CONNMAN_SERVICE_TYPE_WIMAX:
302 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
304 case CONNMAN_SERVICE_TYPE_CELLULAR:
311 static DBusMessage *get_properties(DBusConnection *conn,
312 DBusMessage *message, void *user_data)
314 struct connman_technology *technology = user_data;
316 DBusMessageIter array, dict;
319 reply = dbus_message_new_method_return(message);
323 dbus_message_iter_init_append(reply, &array);
325 connman_dbus_dict_open(&array, &dict);
327 str = state2string(technology->state);
329 connman_dbus_dict_append_basic(&dict, "State",
330 DBUS_TYPE_STRING, &str);
332 str = get_name(technology->type);
334 connman_dbus_dict_append_basic(&dict, "Name",
335 DBUS_TYPE_STRING, &str);
337 str = __connman_service_type2string(technology->type);
339 connman_dbus_dict_append_basic(&dict, "Type",
340 DBUS_TYPE_STRING, &str);
342 connman_dbus_dict_append_array(&dict, "Devices",
343 DBUS_TYPE_OBJECT_PATH, device_list, technology);
345 connman_dbus_dict_close(&array, &dict);
350 static GDBusMethodTable technology_methods[] = {
351 { "GetProperties", "", "a{sv}", get_properties },
355 static GDBusSignalTable technology_signals[] = {
356 { "PropertyChanged", "sv" },
360 static struct connman_technology *technology_find(enum connman_service_type type)
364 DBG("type %d", type);
366 for (list = technology_list; list; list = list->next) {
367 struct connman_technology *technology = list->data;
369 if (technology->type == type)
376 static struct connman_technology *technology_get(enum connman_service_type type)
378 struct connman_technology *technology;
382 DBG("type %d", type);
384 technology = technology_find(type);
385 if (technology != NULL) {
386 g_atomic_int_inc(&technology->refcount);
390 str = __connman_service_type2string(type);
394 technology = g_try_new0(struct connman_technology, 1);
395 if (technology == NULL)
398 technology->refcount = 1;
400 technology->type = type;
401 technology->path = g_strdup_printf("%s/technology/%s",
404 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
406 technology->device_list = NULL;
408 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
410 if (g_dbus_register_interface(connection, technology->path,
411 CONNMAN_TECHNOLOGY_INTERFACE,
412 technology_methods, technology_signals,
413 NULL, technology, NULL) == FALSE) {
414 connman_error("Failed to register %s", technology->path);
419 technology_list = g_slist_append(technology_list, technology);
421 technologies_changed();
423 if (technology->driver != NULL)
426 for (list = driver_list; list; list = list->next) {
427 struct connman_technology_driver *driver = list->data;
429 DBG("driver %p name %s", driver, driver->name);
431 if (driver->type != technology->type)
434 if (driver->probe(technology) == 0) {
435 technology->driver = driver;
441 DBG("technology %p", technology);
446 static void technology_put(struct connman_technology *technology)
448 DBG("technology %p", technology);
450 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
453 if (technology->driver) {
454 technology->driver->remove(technology);
455 technology->driver = NULL;
458 technology_list = g_slist_remove(technology_list, technology);
460 technologies_changed();
462 g_dbus_unregister_interface(connection, technology->path,
463 CONNMAN_TECHNOLOGY_INTERFACE);
465 g_slist_free(technology->device_list);
466 g_hash_table_destroy(technology->rfkill_list);
468 g_free(technology->path);
472 static void unregister_technology(gpointer data)
474 struct connman_technology *technology = data;
476 technology_put(technology);
479 int __connman_technology_add_device(struct connman_device *device)
481 struct connman_technology *technology;
482 enum connman_service_type type;
484 DBG("device %p", device);
486 type = __connman_device_get_service_type(device);
487 __connman_notifier_register(type);
489 technology = technology_get(type);
490 if (technology == NULL)
493 g_hash_table_insert(device_table, device, technology);
495 if (technology->device_list == NULL) {
496 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
497 state_changed(technology);
500 technology->device_list = g_slist_append(technology->device_list,
502 devices_changed(technology);
507 int __connman_technology_remove_device(struct connman_device *device)
509 struct connman_technology *technology;
510 enum connman_service_type type;
512 DBG("device %p", device);
514 type = __connman_device_get_service_type(device);
515 __connman_notifier_disable(type);
516 __connman_notifier_unregister(type);
518 technology = g_hash_table_lookup(device_table, device);
519 if (technology == NULL)
522 technology->device_list = g_slist_remove(technology->device_list,
524 devices_changed(technology);
526 if (technology->device_list == NULL) {
527 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
528 state_changed(technology);
531 g_hash_table_remove(device_table, device);
536 int __connman_technology_enable_device(struct connman_device *device)
538 struct connman_technology *technology;
539 enum connman_service_type type;
541 DBG("device %p", device);
543 type = __connman_device_get_service_type(device);
544 __connman_notifier_enable(type);
546 technology = g_hash_table_lookup(device_table, device);
547 if (technology == NULL)
550 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
551 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
552 state_changed(technology);
558 int __connman_technology_disable_device(struct connman_device *device)
560 struct connman_technology *technology;
561 enum connman_service_type type;
563 DBG("device %p", device);
565 type = __connman_device_get_service_type(device);
566 __connman_notifier_disable(type);
568 technology = g_hash_table_lookup(device_table, device);
569 if (technology == NULL)
572 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
573 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
574 state_changed(technology);
580 int __connman_technology_add_rfkill(unsigned int index,
581 enum connman_service_type type,
582 connman_bool_t softblock,
583 connman_bool_t hardblock)
585 struct connman_technology *technology;
586 struct connman_rfkill *rfkill;
588 DBG("index %u type %d soft %u hard %u", index, type,
589 softblock, hardblock);
591 rfkill = g_try_new0(struct connman_rfkill, 1);
595 rfkill->index = index;
598 technology = technology_get(type);
599 if (technology == NULL) {
604 g_hash_table_replace(rfkill_table, &index, technology);
606 g_hash_table_replace(technology->rfkill_list, &index, rfkill);
611 int __connman_technology_update_rfkill(unsigned int index,
612 connman_bool_t softblock,
613 connman_bool_t hardblock)
615 struct connman_technology *technology;
617 DBG("index %u soft %u hard %u", index, softblock, hardblock);
619 technology = g_hash_table_lookup(rfkill_table, &index);
620 if (technology == NULL)
626 int __connman_technology_remove_rfkill(unsigned int index)
628 struct connman_technology *technology;
630 DBG("index %u", index);
632 technology = g_hash_table_lookup(rfkill_table, &index);
633 if (technology == NULL)
636 g_hash_table_remove(technology->rfkill_list, &index);
638 g_hash_table_remove(rfkill_table, &index);
643 int __connman_technology_init(void)
647 connection = connman_dbus_get_connection();
649 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
650 NULL, unregister_technology);
651 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
652 NULL, unregister_technology);
657 void __connman_technology_cleanup(void)
661 g_hash_table_destroy(device_table);
662 g_hash_table_destroy(rfkill_table);
664 dbus_connection_unref(connection);