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;
59 static void free_rfkill(gpointer data)
61 struct connman_rfkill *rfkill = data;
66 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
70 for (list = technology_list; list; list = list->next) {
71 struct connman_technology *technology = list->data;
73 if (technology->path == NULL)
76 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
81 static void technologies_changed(void)
83 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
84 CONNMAN_MANAGER_INTERFACE, "Technologies",
85 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
88 static void device_list(DBusMessageIter *iter, void *user_data)
90 struct connman_technology *technology = user_data;
93 for (list = technology->device_list; list; list = list->next) {
94 struct connman_device *device = list->data;
97 path = connman_device_get_path(device);
101 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
106 static void devices_changed(struct connman_technology *technology)
108 connman_dbus_property_changed_array(technology->path,
109 CONNMAN_TECHNOLOGY_INTERFACE, "Devices",
110 DBUS_TYPE_OBJECT_PATH, device_list, technology);
113 static const char *state2string(enum connman_technology_state state)
116 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
118 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
120 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
122 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
124 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
131 static void state_changed(struct connman_technology *technology)
135 str = state2string(technology->state);
139 connman_dbus_property_changed_basic(technology->path,
140 CONNMAN_TECHNOLOGY_INTERFACE, "State",
141 DBUS_TYPE_STRING, &str);
144 static const char *get_name(enum connman_service_type type)
147 case CONNMAN_SERVICE_TYPE_UNKNOWN:
148 case CONNMAN_SERVICE_TYPE_SYSTEM:
149 case CONNMAN_SERVICE_TYPE_GPS:
150 case CONNMAN_SERVICE_TYPE_VPN:
152 case CONNMAN_SERVICE_TYPE_ETHERNET:
154 case CONNMAN_SERVICE_TYPE_WIFI:
156 case CONNMAN_SERVICE_TYPE_WIMAX:
158 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
160 case CONNMAN_SERVICE_TYPE_CELLULAR:
167 static DBusMessage *get_properties(DBusConnection *conn,
168 DBusMessage *message, void *user_data)
170 struct connman_technology *technology = user_data;
172 DBusMessageIter array, dict;
175 reply = dbus_message_new_method_return(message);
179 dbus_message_iter_init_append(reply, &array);
181 connman_dbus_dict_open(&array, &dict);
183 str = state2string(technology->state);
185 connman_dbus_dict_append_basic(&dict, "State",
186 DBUS_TYPE_STRING, &str);
188 str = get_name(technology->type);
190 connman_dbus_dict_append_basic(&dict, "Name",
191 DBUS_TYPE_STRING, &str);
193 str = __connman_service_type2string(technology->type);
195 connman_dbus_dict_append_basic(&dict, "Type",
196 DBUS_TYPE_STRING, &str);
198 connman_dbus_dict_append_array(&dict, "Devices",
199 DBUS_TYPE_OBJECT_PATH, device_list, technology);
201 connman_dbus_dict_close(&array, &dict);
206 static GDBusMethodTable technology_methods[] = {
207 { "GetProperties", "", "a{sv}", get_properties },
211 static GDBusSignalTable technology_signals[] = {
212 { "PropertyChanged", "sv" },
216 static struct connman_technology *technology_find(enum connman_service_type type)
220 DBG("type %d", type);
222 for (list = technology_list; list; list = list->next) {
223 struct connman_technology *technology = list->data;
225 if (technology->type == type)
232 static struct connman_technology *technology_get(enum connman_service_type type)
234 struct connman_technology *technology;
237 DBG("type %d", type);
239 technology = technology_find(type);
240 if (technology != NULL) {
241 g_atomic_int_inc(&technology->refcount);
245 str = __connman_service_type2string(type);
249 technology = g_try_new0(struct connman_technology, 1);
250 if (technology == NULL)
253 technology->refcount = 1;
255 technology->type = type;
256 technology->path = g_strdup_printf("%s/technology/%s",
259 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
261 technology->device_list = NULL;
263 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
265 if (g_dbus_register_interface(connection, technology->path,
266 CONNMAN_TECHNOLOGY_INTERFACE,
267 technology_methods, technology_signals,
268 NULL, technology, NULL) == FALSE) {
269 connman_error("Failed to register %s", technology->path);
274 technology_list = g_slist_append(technology_list, technology);
276 technologies_changed();
279 DBG("technology %p", technology);
284 static void technology_put(struct connman_technology *technology)
286 DBG("technology %p", technology);
288 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
291 technology_list = g_slist_remove(technology_list, technology);
293 technologies_changed();
295 g_dbus_unregister_interface(connection, technology->path,
296 CONNMAN_TECHNOLOGY_INTERFACE);
298 g_slist_free(technology->device_list);
299 g_hash_table_destroy(technology->rfkill_list);
301 g_free(technology->path);
305 static void unregister_technology(gpointer data)
307 struct connman_technology *technology = data;
309 technology_put(technology);
312 int __connman_technology_add_device(struct connman_device *device)
314 struct connman_technology *technology;
315 enum connman_service_type type;
317 DBG("device %p", device);
319 type = __connman_device_get_service_type(device);
320 __connman_notifier_register(type);
322 technology = technology_get(type);
323 if (technology == NULL)
326 g_hash_table_insert(device_table, device, technology);
328 if (technology->device_list == NULL) {
329 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
330 state_changed(technology);
333 technology->device_list = g_slist_append(technology->device_list,
335 devices_changed(technology);
340 int __connman_technology_remove_device(struct connman_device *device)
342 struct connman_technology *technology;
343 enum connman_service_type type;
345 DBG("device %p", device);
347 type = __connman_device_get_service_type(device);
348 __connman_notifier_disable(type);
349 __connman_notifier_unregister(type);
351 technology = g_hash_table_lookup(device_table, device);
352 if (technology == NULL)
355 technology->device_list = g_slist_remove(technology->device_list,
357 devices_changed(technology);
359 if (technology->device_list == NULL) {
360 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
361 state_changed(technology);
364 g_hash_table_remove(device_table, device);
369 int __connman_technology_enable_device(struct connman_device *device)
371 struct connman_technology *technology;
372 enum connman_service_type type;
374 DBG("device %p", device);
376 type = __connman_device_get_service_type(device);
377 __connman_notifier_enable(type);
379 technology = g_hash_table_lookup(device_table, device);
380 if (technology == NULL)
383 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
384 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
385 state_changed(technology);
391 int __connman_technology_disable_device(struct connman_device *device)
393 struct connman_technology *technology;
394 enum connman_service_type type;
396 DBG("device %p", device);
398 type = __connman_device_get_service_type(device);
399 __connman_notifier_disable(type);
401 technology = g_hash_table_lookup(device_table, device);
402 if (technology == NULL)
405 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
406 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
407 state_changed(technology);
413 int __connman_technology_add_rfkill(unsigned int index,
414 enum connman_service_type type,
415 connman_bool_t softblock,
416 connman_bool_t hardblock)
418 struct connman_technology *technology;
419 struct connman_rfkill *rfkill;
421 DBG("index %u type %d soft %u hard %u", index, type,
422 softblock, hardblock);
424 rfkill = g_try_new0(struct connman_rfkill, 1);
428 rfkill->index = index;
431 technology = technology_get(type);
432 if (technology == NULL) {
437 g_hash_table_replace(rfkill_table, &index, technology);
439 g_hash_table_replace(technology->rfkill_list, &index, rfkill);
444 int __connman_technology_update_rfkill(unsigned int index,
445 connman_bool_t softblock,
446 connman_bool_t hardblock)
448 struct connman_technology *technology;
450 DBG("index %u soft %u hard %u", index, softblock, hardblock);
452 technology = g_hash_table_lookup(rfkill_table, &index);
453 if (technology == NULL)
459 int __connman_technology_remove_rfkill(unsigned int index)
461 struct connman_technology *technology;
463 DBG("index %u", index);
465 technology = g_hash_table_lookup(rfkill_table, &index);
466 if (technology == NULL)
469 g_hash_table_remove(technology->rfkill_list, &index);
471 g_hash_table_remove(rfkill_table, &index);
476 int __connman_technology_init(void)
480 connection = connman_dbus_get_connection();
482 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
483 NULL, unregister_technology);
484 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
485 NULL, unregister_technology);
490 void __connman_technology_cleanup(void)
494 g_hash_table_destroy(device_table);
495 g_hash_table_destroy(rfkill_table);
497 dbus_connection_unref(connection);