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_unregister(type);
350 technology = g_hash_table_lookup(device_table, device);
351 if (technology == NULL)
354 technology->device_list = g_slist_remove(technology->device_list,
356 devices_changed(technology);
358 if (technology->device_list == NULL) {
359 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
360 state_changed(technology);
363 g_hash_table_remove(device_table, device);
368 int __connman_technology_enable_device(struct connman_device *device)
370 struct connman_technology *technology;
371 enum connman_service_type type;
373 DBG("device %p", device);
375 type = __connman_device_get_service_type(device);
376 __connman_notifier_enable(type);
378 technology = g_hash_table_lookup(device_table, device);
379 if (technology == NULL)
382 if (g_atomic_int_exchange_and_add(&technology->enabled, 1) == 0) {
383 technology->state = CONNMAN_TECHNOLOGY_STATE_ENABLED;
384 state_changed(technology);
390 int __connman_technology_disable_device(struct connman_device *device)
392 struct connman_technology *technology;
393 enum connman_service_type type;
395 DBG("device %p", device);
397 type = __connman_device_get_service_type(device);
398 __connman_notifier_disable(type);
400 technology = g_hash_table_lookup(device_table, device);
401 if (technology == NULL)
404 if (g_atomic_int_dec_and_test(&technology->enabled) == TRUE) {
405 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
406 state_changed(technology);
412 int __connman_technology_add_rfkill(unsigned int index,
413 enum connman_service_type type,
414 connman_bool_t softblock,
415 connman_bool_t hardblock)
417 struct connman_technology *technology;
418 struct connman_rfkill *rfkill;
420 DBG("index %u type %d soft %u hard %u", index, type,
421 softblock, hardblock);
423 rfkill = g_try_new0(struct connman_rfkill, 1);
427 rfkill->index = index;
430 technology = technology_get(type);
431 if (technology == NULL) {
436 g_hash_table_replace(rfkill_table, &index, technology);
438 g_hash_table_replace(technology->rfkill_list, &index, rfkill);
443 int __connman_technology_update_rfkill(unsigned int index,
444 connman_bool_t softblock,
445 connman_bool_t hardblock)
447 struct connman_technology *technology;
449 DBG("index %u soft %u hard %u", index, softblock, hardblock);
451 technology = g_hash_table_lookup(rfkill_table, &index);
452 if (technology == NULL)
458 int __connman_technology_remove_rfkill(unsigned int index)
460 struct connman_technology *technology;
462 DBG("index %u", index);
464 technology = g_hash_table_lookup(rfkill_table, &index);
465 if (technology == NULL)
468 g_hash_table_remove(technology->rfkill_list, &index);
470 g_hash_table_remove(rfkill_table, &index);
475 int __connman_technology_init(void)
479 connection = connman_dbus_get_connection();
481 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
482 NULL, unregister_technology);
483 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
484 NULL, unregister_technology);
489 void __connman_technology_cleanup(void)
493 g_hash_table_destroy(device_table);
494 g_hash_table_destroy(rfkill_table);
496 dbus_connection_unref(connection);