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 int set_tethering(connman_bool_t enabled)
107 for (list = technology_list; list; list = list->next) {
108 struct connman_technology *technology = list->data;
110 if (technology->driver == NULL)
113 if (technology->driver->set_tethering)
114 technology->driver->set_tethering(technology, enabled);
120 int __connman_technology_enable_tethering(void)
122 return set_tethering(TRUE);
125 int __connman_technology_disable_tethering(void)
127 return set_tethering(FALSE);
130 static void free_rfkill(gpointer data)
132 struct connman_rfkill *rfkill = data;
137 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
141 for (list = technology_list; list; list = list->next) {
142 struct connman_technology *technology = list->data;
144 if (technology->path == NULL)
147 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
152 static void technologies_changed(void)
154 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
155 CONNMAN_MANAGER_INTERFACE, "Technologies",
156 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
159 static void device_list(DBusMessageIter *iter, void *user_data)
161 struct connman_technology *technology = user_data;
164 for (list = technology->device_list; list; list = list->next) {
165 struct connman_device *device = list->data;
168 path = connman_device_get_path(device);
172 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
177 static void devices_changed(struct connman_technology *technology)
179 connman_dbus_property_changed_array(technology->path,
180 CONNMAN_TECHNOLOGY_INTERFACE, "Devices",
181 DBUS_TYPE_OBJECT_PATH, device_list, technology);
184 static const char *state2string(enum connman_technology_state state)
187 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
189 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
191 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
193 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
195 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
202 static void state_changed(struct connman_technology *technology)
206 str = state2string(technology->state);
210 connman_dbus_property_changed_basic(technology->path,
211 CONNMAN_TECHNOLOGY_INTERFACE, "State",
212 DBUS_TYPE_STRING, &str);
215 static const char *get_name(enum connman_service_type type)
218 case CONNMAN_SERVICE_TYPE_UNKNOWN:
219 case CONNMAN_SERVICE_TYPE_SYSTEM:
220 case CONNMAN_SERVICE_TYPE_GPS:
221 case CONNMAN_SERVICE_TYPE_VPN:
223 case CONNMAN_SERVICE_TYPE_ETHERNET:
225 case CONNMAN_SERVICE_TYPE_WIFI:
227 case CONNMAN_SERVICE_TYPE_WIMAX:
229 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
231 case CONNMAN_SERVICE_TYPE_CELLULAR:
238 static DBusMessage *get_properties(DBusConnection *conn,
239 DBusMessage *message, void *user_data)
241 struct connman_technology *technology = user_data;
243 DBusMessageIter array, dict;
246 reply = dbus_message_new_method_return(message);
250 dbus_message_iter_init_append(reply, &array);
252 connman_dbus_dict_open(&array, &dict);
254 str = state2string(technology->state);
256 connman_dbus_dict_append_basic(&dict, "State",
257 DBUS_TYPE_STRING, &str);
259 str = get_name(technology->type);
261 connman_dbus_dict_append_basic(&dict, "Name",
262 DBUS_TYPE_STRING, &str);
264 str = __connman_service_type2string(technology->type);
266 connman_dbus_dict_append_basic(&dict, "Type",
267 DBUS_TYPE_STRING, &str);
269 connman_dbus_dict_append_array(&dict, "Devices",
270 DBUS_TYPE_OBJECT_PATH, device_list, technology);
272 connman_dbus_dict_close(&array, &dict);
277 static GDBusMethodTable technology_methods[] = {
278 { "GetProperties", "", "a{sv}", get_properties },
282 static GDBusSignalTable technology_signals[] = {
283 { "PropertyChanged", "sv" },
287 static struct connman_technology *technology_find(enum connman_service_type type)
291 DBG("type %d", type);
293 for (list = technology_list; list; list = list->next) {
294 struct connman_technology *technology = list->data;
296 if (technology->type == type)
303 static struct connman_technology *technology_get(enum connman_service_type type)
305 struct connman_technology *technology;
309 DBG("type %d", type);
311 technology = technology_find(type);
312 if (technology != NULL) {
313 g_atomic_int_inc(&technology->refcount);
317 str = __connman_service_type2string(type);
321 technology = g_try_new0(struct connman_technology, 1);
322 if (technology == NULL)
325 technology->refcount = 1;
327 technology->type = type;
328 technology->path = g_strdup_printf("%s/technology/%s",
331 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
333 technology->device_list = NULL;
335 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
337 if (g_dbus_register_interface(connection, technology->path,
338 CONNMAN_TECHNOLOGY_INTERFACE,
339 technology_methods, technology_signals,
340 NULL, technology, NULL) == FALSE) {
341 connman_error("Failed to register %s", technology->path);
346 technology_list = g_slist_append(technology_list, technology);
348 technologies_changed();
350 if (technology->driver != NULL)
353 for (list = driver_list; list; list = list->next) {
354 struct connman_technology_driver *driver = list->data;
356 DBG("driver %p name %s", driver, driver->name);
358 if (driver->type != technology->type)
361 if (driver->probe(technology) == 0) {
362 technology->driver = driver;
368 DBG("technology %p", technology);
373 static void technology_put(struct connman_technology *technology)
375 DBG("technology %p", technology);
377 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
380 if (technology->driver) {
381 technology->driver->remove(technology);
382 technology->driver = NULL;
385 technology_list = g_slist_remove(technology_list, technology);
387 technologies_changed();
389 g_dbus_unregister_interface(connection, technology->path,
390 CONNMAN_TECHNOLOGY_INTERFACE);
392 g_slist_free(technology->device_list);
393 g_hash_table_destroy(technology->rfkill_list);
395 g_free(technology->path);
399 static void unregister_technology(gpointer data)
401 struct connman_technology *technology = data;
403 technology_put(technology);
406 int __connman_technology_add_device(struct connman_device *device)
408 struct connman_technology *technology;
409 enum connman_service_type type;
411 DBG("device %p", device);
413 type = __connman_device_get_service_type(device);
414 __connman_notifier_register(type);
416 technology = technology_get(type);
417 if (technology == NULL)
420 g_hash_table_insert(device_table, device, technology);
422 if (technology->device_list == NULL) {
423 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
424 state_changed(technology);
427 technology->device_list = g_slist_append(technology->device_list,
429 devices_changed(technology);
434 int __connman_technology_remove_device(struct connman_device *device)
436 struct connman_technology *technology;
437 enum connman_service_type type;
439 DBG("device %p", device);
441 type = __connman_device_get_service_type(device);
442 __connman_notifier_disable(type);
443 __connman_notifier_unregister(type);
445 technology = g_hash_table_lookup(device_table, device);
446 if (technology == NULL)
449 technology->device_list = g_slist_remove(technology->device_list,
451 devices_changed(technology);
453 if (technology->device_list == NULL) {
454 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
455 state_changed(technology);
458 g_hash_table_remove(device_table, device);
463 int __connman_technology_enable_device(struct connman_device *device)
465 struct connman_technology *technology;
466 enum connman_service_type type;
468 DBG("device %p", device);
470 type = __connman_device_get_service_type(device);
471 __connman_notifier_enable(type);
473 technology = g_hash_table_lookup(device_table, device);
474 if (technology == NULL)
477 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
478 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
479 state_changed(technology);
485 int __connman_technology_disable_device(struct connman_device *device)
487 struct connman_technology *technology;
488 enum connman_service_type type;
490 DBG("device %p", device);
492 type = __connman_device_get_service_type(device);
493 __connman_notifier_disable(type);
495 technology = g_hash_table_lookup(device_table, device);
496 if (technology == NULL)
499 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
500 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
501 state_changed(technology);
507 int __connman_technology_add_rfkill(unsigned int index,
508 enum connman_service_type type,
509 connman_bool_t softblock,
510 connman_bool_t hardblock)
512 struct connman_technology *technology;
513 struct connman_rfkill *rfkill;
515 DBG("index %u type %d soft %u hard %u", index, type,
516 softblock, hardblock);
518 rfkill = g_try_new0(struct connman_rfkill, 1);
522 rfkill->index = index;
525 technology = technology_get(type);
526 if (technology == NULL) {
531 g_hash_table_replace(rfkill_table, &index, technology);
533 g_hash_table_replace(technology->rfkill_list, &index, rfkill);
538 int __connman_technology_update_rfkill(unsigned int index,
539 connman_bool_t softblock,
540 connman_bool_t hardblock)
542 struct connman_technology *technology;
544 DBG("index %u soft %u hard %u", index, softblock, hardblock);
546 technology = g_hash_table_lookup(rfkill_table, &index);
547 if (technology == NULL)
553 int __connman_technology_remove_rfkill(unsigned int index)
555 struct connman_technology *technology;
557 DBG("index %u", index);
559 technology = g_hash_table_lookup(rfkill_table, &index);
560 if (technology == NULL)
563 g_hash_table_remove(technology->rfkill_list, &index);
565 g_hash_table_remove(rfkill_table, &index);
570 int __connman_technology_init(void)
574 connection = connman_dbus_get_connection();
576 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
577 NULL, unregister_technology);
578 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
579 NULL, unregister_technology);
584 void __connman_technology_cleanup(void)
588 g_hash_table_destroy(device_table);
589 g_hash_table_destroy(rfkill_table);
591 dbus_connection_unref(connection);