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 static void free_rfkill(gpointer data)
60 struct connman_rfkill *rfkill = data;
65 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
69 for (list = technology_list; list; list = list->next) {
70 struct connman_technology *technology = list->data;
72 if (technology->path == NULL)
75 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
80 static void technologies_changed(void)
82 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
83 CONNMAN_MANAGER_INTERFACE, "Technologies",
84 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
87 static void device_list(DBusMessageIter *iter, void *user_data)
89 struct connman_technology *technology = user_data;
92 for (list = technology->device_list; list; list = list->next) {
93 struct connman_device *device = list->data;
96 path = connman_device_get_path(device);
100 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
105 static void devices_changed(struct connman_technology *technology)
107 connman_dbus_property_changed_array(technology->path,
108 CONNMAN_TECHNOLOGY_INTERFACE, "Devices",
109 DBUS_TYPE_OBJECT_PATH, device_list, technology);
112 static const char *state2string(enum connman_technology_state state)
115 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
117 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
119 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
121 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
123 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
130 static void state_changed(struct connman_technology *technology)
134 str = state2string(technology->state);
138 connman_dbus_property_changed_basic(technology->path,
139 CONNMAN_TECHNOLOGY_INTERFACE, "State",
140 DBUS_TYPE_STRING, &str);
143 static const char *get_name(enum connman_service_type type)
146 case CONNMAN_SERVICE_TYPE_UNKNOWN:
147 case CONNMAN_SERVICE_TYPE_SYSTEM:
148 case CONNMAN_SERVICE_TYPE_GPS:
149 case CONNMAN_SERVICE_TYPE_VPN:
151 case CONNMAN_SERVICE_TYPE_ETHERNET:
153 case CONNMAN_SERVICE_TYPE_WIFI:
155 case CONNMAN_SERVICE_TYPE_WIMAX:
157 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
159 case CONNMAN_SERVICE_TYPE_CELLULAR:
166 static DBusMessage *get_properties(DBusConnection *conn,
167 DBusMessage *message, void *user_data)
169 struct connman_technology *technology = user_data;
171 DBusMessageIter array, dict;
174 reply = dbus_message_new_method_return(message);
178 dbus_message_iter_init_append(reply, &array);
180 connman_dbus_dict_open(&array, &dict);
182 str = state2string(technology->state);
184 connman_dbus_dict_append_basic(&dict, "State",
185 DBUS_TYPE_STRING, &str);
187 str = get_name(technology->type);
189 connman_dbus_dict_append_basic(&dict, "Name",
190 DBUS_TYPE_STRING, &str);
192 str = __connman_service_type2string(technology->type);
194 connman_dbus_dict_append_basic(&dict, "Type",
195 DBUS_TYPE_STRING, &str);
197 connman_dbus_dict_append_array(&dict, "Devices",
198 DBUS_TYPE_OBJECT_PATH, device_list, technology);
200 connman_dbus_dict_close(&array, &dict);
205 static GDBusMethodTable technology_methods[] = {
206 { "GetProperties", "", "a{sv}", get_properties },
210 static GDBusSignalTable technology_signals[] = {
211 { "PropertyChanged", "sv" },
215 static struct connman_technology *technology_find(enum connman_service_type type)
219 DBG("type %d", type);
221 for (list = technology_list; list; list = list->next) {
222 struct connman_technology *technology = list->data;
224 if (technology->type == type)
231 static struct connman_technology *technology_get(enum connman_service_type type)
233 struct connman_technology *technology;
236 DBG("type %d", type);
238 technology = technology_find(type);
239 if (technology != NULL) {
240 g_atomic_int_inc(&technology->refcount);
244 str = __connman_service_type2string(type);
248 technology = g_try_new0(struct connman_technology, 1);
249 if (technology == NULL)
252 technology->refcount = 1;
254 technology->type = type;
255 technology->path = g_strdup_printf("%s/technology/%s",
258 technology->rfkill_list = g_hash_table_new_full(g_int_hash, g_int_equal,
260 technology->device_list = NULL;
262 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
264 if (g_dbus_register_interface(connection, technology->path,
265 CONNMAN_TECHNOLOGY_INTERFACE,
266 technology_methods, technology_signals,
267 NULL, technology, NULL) == FALSE) {
268 connman_error("Failed to register %s", technology->path);
273 technology_list = g_slist_append(technology_list, technology);
275 technologies_changed();
278 DBG("technology %p", technology);
283 static void technology_put(struct connman_technology *technology)
285 DBG("technology %p", technology);
287 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
290 technology_list = g_slist_remove(technology_list, technology);
292 technologies_changed();
294 g_dbus_unregister_interface(connection, technology->path,
295 CONNMAN_TECHNOLOGY_INTERFACE);
297 g_slist_free(technology->device_list);
298 g_hash_table_destroy(technology->rfkill_list);
300 g_free(technology->path);
304 static void unregister_technology(gpointer data)
306 struct connman_technology *technology = data;
308 technology_put(technology);
311 int __connman_technology_add_device(struct connman_device *device)
313 struct connman_technology *technology;
314 enum connman_service_type type;
316 DBG("device %p", device);
318 type = __connman_device_get_service_type(device);
320 technology = technology_get(type);
321 if (technology == NULL)
324 g_hash_table_insert(device_table, device, technology);
326 if (technology->device_list == NULL) {
327 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
328 state_changed(technology);
331 technology->device_list = g_slist_append(technology->device_list,
333 devices_changed(technology);
338 int __connman_technology_remove_device(struct connman_device *device)
340 struct connman_technology *technology;
342 DBG("device %p", device);
344 technology = g_hash_table_lookup(device_table, device);
345 if (technology == NULL)
348 technology->device_list = g_slist_remove(technology->device_list,
350 devices_changed(technology);
352 if (technology->device_list == NULL) {
353 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
354 state_changed(technology);
357 g_hash_table_remove(device_table, device);
362 int __connman_technology_add_rfkill(unsigned int index,
363 enum connman_service_type type,
364 connman_bool_t softblock,
365 connman_bool_t hardblock)
367 struct connman_technology *technology;
368 struct connman_rfkill *rfkill;
370 DBG("index %u type %d soft %u hard %u", index, type,
371 softblock, hardblock);
373 rfkill = g_try_new0(struct connman_rfkill, 1);
377 rfkill->index = index;
380 technology = technology_get(type);
381 if (technology == NULL) {
386 g_hash_table_replace(rfkill_table, &index, technology);
388 g_hash_table_replace(technology->rfkill_list, &index, rfkill);
393 int __connman_technology_update_rfkill(unsigned int index,
394 connman_bool_t softblock,
395 connman_bool_t hardblock)
397 struct connman_technology *technology;
399 DBG("index %u soft %u hard %u", index, softblock, hardblock);
401 technology = g_hash_table_lookup(rfkill_table, &index);
402 if (technology == NULL)
408 int __connman_technology_remove_rfkill(unsigned int index)
410 struct connman_technology *technology;
412 DBG("index %u", index);
414 technology = g_hash_table_lookup(rfkill_table, &index);
415 if (technology == NULL)
418 g_hash_table_remove(technology->rfkill_list, &index);
420 g_hash_table_remove(rfkill_table, &index);
425 int __connman_technology_init(void)
429 connection = connman_dbus_get_connection();
431 rfkill_table = g_hash_table_new_full(g_int_hash, g_int_equal,
432 NULL, unregister_technology);
433 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
434 NULL, unregister_technology);
439 void __connman_technology_cleanup(void)
443 g_hash_table_destroy(device_table);
444 g_hash_table_destroy(rfkill_table);
446 dbus_connection_unref(connection);