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 static void free_rfkill(gpointer data)
105 struct connman_rfkill *rfkill = data;
110 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
114 for (list = technology_list; list; list = list->next) {
115 struct connman_technology *technology = list->data;
117 if (technology->path == NULL)
120 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
125 static void technologies_changed(void)
127 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
128 CONNMAN_MANAGER_INTERFACE, "Technologies",
129 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
132 static void device_list(DBusMessageIter *iter, void *user_data)
134 struct connman_technology *technology = user_data;
137 for (list = technology->device_list; list; list = list->next) {
138 struct connman_device *device = list->data;
141 path = connman_device_get_path(device);
145 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
150 static void devices_changed(struct connman_technology *technology)
152 connman_dbus_property_changed_array(technology->path,
153 CONNMAN_TECHNOLOGY_INTERFACE, "Devices",
154 DBUS_TYPE_OBJECT_PATH, device_list, technology);
157 static const char *state2string(enum connman_technology_state state)
160 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
162 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
164 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
166 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
168 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
175 static void state_changed(struct connman_technology *technology)
179 str = state2string(technology->state);
183 connman_dbus_property_changed_basic(technology->path,
184 CONNMAN_TECHNOLOGY_INTERFACE, "State",
185 DBUS_TYPE_STRING, &str);
188 static const char *get_name(enum connman_service_type type)
191 case CONNMAN_SERVICE_TYPE_UNKNOWN:
192 case CONNMAN_SERVICE_TYPE_SYSTEM:
193 case CONNMAN_SERVICE_TYPE_GPS:
194 case CONNMAN_SERVICE_TYPE_VPN:
196 case CONNMAN_SERVICE_TYPE_ETHERNET:
198 case CONNMAN_SERVICE_TYPE_WIFI:
200 case CONNMAN_SERVICE_TYPE_WIMAX:
202 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
204 case CONNMAN_SERVICE_TYPE_CELLULAR:
211 static DBusMessage *get_properties(DBusConnection *conn,
212 DBusMessage *message, void *user_data)
214 struct connman_technology *technology = user_data;
216 DBusMessageIter array, dict;
219 reply = dbus_message_new_method_return(message);
223 dbus_message_iter_init_append(reply, &array);
225 connman_dbus_dict_open(&array, &dict);
227 str = state2string(technology->state);
229 connman_dbus_dict_append_basic(&dict, "State",
230 DBUS_TYPE_STRING, &str);
232 str = get_name(technology->type);
234 connman_dbus_dict_append_basic(&dict, "Name",
235 DBUS_TYPE_STRING, &str);
237 str = __connman_service_type2string(technology->type);
239 connman_dbus_dict_append_basic(&dict, "Type",
240 DBUS_TYPE_STRING, &str);
242 connman_dbus_dict_append_array(&dict, "Devices",
243 DBUS_TYPE_OBJECT_PATH, device_list, technology);
245 connman_dbus_dict_close(&array, &dict);
250 static GDBusMethodTable technology_methods[] = {
251 { "GetProperties", "", "a{sv}", get_properties },
255 static GDBusSignalTable technology_signals[] = {
256 { "PropertyChanged", "sv" },
260 static struct connman_technology *technology_find(enum connman_service_type type)
264 DBG("type %d", type);
266 for (list = technology_list; list; list = list->next) {
267 struct connman_technology *technology = list->data;
269 if (technology->type == type)
276 static struct connman_technology *technology_get(enum connman_service_type type)
278 struct connman_technology *technology;
282 DBG("type %d", type);
284 technology = technology_find(type);
285 if (technology != NULL) {
286 g_atomic_int_inc(&technology->refcount);
290 str = __connman_service_type2string(type);
294 technology = g_try_new0(struct connman_technology, 1);
295 if (technology == NULL)
298 technology->refcount = 1;
300 technology->type = type;
301 technology->path = g_strdup_printf("%s/technology/%s",
304 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
306 technology->device_list = NULL;
308 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
310 if (g_dbus_register_interface(connection, technology->path,
311 CONNMAN_TECHNOLOGY_INTERFACE,
312 technology_methods, technology_signals,
313 NULL, technology, NULL) == FALSE) {
314 connman_error("Failed to register %s", technology->path);
319 technology_list = g_slist_append(technology_list, technology);
321 technologies_changed();
323 if (technology->driver != NULL)
326 for (list = driver_list; list; list = list->next) {
327 struct connman_technology_driver *driver = list->data;
329 DBG("driver %p name %s", driver, driver->name);
331 if (driver->type != technology->type)
334 if (driver->probe(technology) == 0) {
335 technology->driver = driver;
341 DBG("technology %p", technology);
346 static void technology_put(struct connman_technology *technology)
348 DBG("technology %p", technology);
350 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
353 if (technology->driver) {
354 technology->driver->remove(technology);
355 technology->driver = NULL;
358 technology_list = g_slist_remove(technology_list, technology);
360 technologies_changed();
362 g_dbus_unregister_interface(connection, technology->path,
363 CONNMAN_TECHNOLOGY_INTERFACE);
365 g_slist_free(technology->device_list);
366 g_hash_table_destroy(technology->rfkill_list);
368 g_free(technology->path);
372 static void unregister_technology(gpointer data)
374 struct connman_technology *technology = data;
376 technology_put(technology);
379 int __connman_technology_add_device(struct connman_device *device)
381 struct connman_technology *technology;
382 enum connman_service_type type;
384 DBG("device %p", device);
386 type = __connman_device_get_service_type(device);
387 __connman_notifier_register(type);
389 technology = technology_get(type);
390 if (technology == NULL)
393 g_hash_table_insert(device_table, device, technology);
395 if (technology->device_list == NULL) {
396 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
397 state_changed(technology);
400 technology->device_list = g_slist_append(technology->device_list,
402 devices_changed(technology);
407 int __connman_technology_remove_device(struct connman_device *device)
409 struct connman_technology *technology;
410 enum connman_service_type type;
412 DBG("device %p", device);
414 type = __connman_device_get_service_type(device);
415 __connman_notifier_disable(type);
416 __connman_notifier_unregister(type);
418 technology = g_hash_table_lookup(device_table, device);
419 if (technology == NULL)
422 technology->device_list = g_slist_remove(technology->device_list,
424 devices_changed(technology);
426 if (technology->device_list == NULL) {
427 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
428 state_changed(technology);
431 g_hash_table_remove(device_table, device);
436 int __connman_technology_enable_device(struct connman_device *device)
438 struct connman_technology *technology;
439 enum connman_service_type type;
441 DBG("device %p", device);
443 type = __connman_device_get_service_type(device);
444 __connman_notifier_enable(type);
446 technology = g_hash_table_lookup(device_table, device);
447 if (technology == NULL)
450 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
451 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
452 state_changed(technology);
458 int __connman_technology_disable_device(struct connman_device *device)
460 struct connman_technology *technology;
461 enum connman_service_type type;
463 DBG("device %p", device);
465 type = __connman_device_get_service_type(device);
466 __connman_notifier_disable(type);
468 technology = g_hash_table_lookup(device_table, device);
469 if (technology == NULL)
472 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
473 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
474 state_changed(technology);
480 int __connman_technology_add_rfkill(unsigned int index,
481 enum connman_service_type type,
482 connman_bool_t softblock,
483 connman_bool_t hardblock)
485 struct connman_technology *technology;
486 struct connman_rfkill *rfkill;
488 DBG("index %u type %d soft %u hard %u", index, type,
489 softblock, hardblock);
491 rfkill = g_try_new0(struct connman_rfkill, 1);
495 rfkill->index = index;
498 technology = technology_get(type);
499 if (technology == NULL) {
504 g_hash_table_replace(rfkill_table, &index, technology);
506 g_hash_table_replace(technology->rfkill_list, &index, rfkill);
511 int __connman_technology_update_rfkill(unsigned int index,
512 connman_bool_t softblock,
513 connman_bool_t hardblock)
515 struct connman_technology *technology;
517 DBG("index %u soft %u hard %u", index, softblock, hardblock);
519 technology = g_hash_table_lookup(rfkill_table, &index);
520 if (technology == NULL)
526 int __connman_technology_remove_rfkill(unsigned int index)
528 struct connman_technology *technology;
530 DBG("index %u", index);
532 technology = g_hash_table_lookup(rfkill_table, &index);
533 if (technology == NULL)
536 g_hash_table_remove(technology->rfkill_list, &index);
538 g_hash_table_remove(rfkill_table, &index);
543 int __connman_technology_init(void)
547 connection = connman_dbus_get_connection();
549 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
550 NULL, unregister_technology);
551 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
552 NULL, unregister_technology);
557 void __connman_technology_cleanup(void)
561 g_hash_table_destroy(device_table);
562 g_hash_table_destroy(rfkill_table);
564 dbus_connection_unref(connection);