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 *device_table;
33 static GSList *technology_list = NULL;
35 enum connman_technology_state {
36 CONNMAN_TECHNOLOGY_STATE_UNKNOWN = 0,
37 CONNMAN_TECHNOLOGY_STATE_OFFLINE = 1,
38 CONNMAN_TECHNOLOGY_STATE_AVAILABLE = 2,
39 CONNMAN_TECHNOLOGY_STATE_ENABLED = 3,
40 CONNMAN_TECHNOLOGY_STATE_CONNECTED = 4,
43 struct connman_technology {
45 enum connman_service_type type;
46 enum connman_technology_state state;
51 void __connman_technology_list(DBusMessageIter *iter, void *user_data)
55 for (list = technology_list; list; list = list->next) {
56 struct connman_technology *technology = list->data;
58 if (technology->path == NULL)
61 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
66 static void technologies_changed(void)
68 connman_dbus_property_changed_array(CONNMAN_MANAGER_PATH,
69 CONNMAN_MANAGER_INTERFACE, "Technologies",
70 DBUS_TYPE_OBJECT_PATH, __connman_technology_list, NULL);
73 static void device_list(DBusMessageIter *iter, void *user_data)
75 struct connman_technology *technology = user_data;
78 for (list = technology->device_list; list; list = list->next) {
79 struct connman_device *device = list->data;
82 path = connman_device_get_path(device);
86 dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
91 static void devices_changed(struct connman_technology *technology)
93 connman_dbus_property_changed_array(technology->path,
94 CONNMAN_TECHNOLOGY_INTERFACE, "Devices",
95 DBUS_TYPE_OBJECT_PATH, device_list, technology);
98 static const char *state2string(enum connman_technology_state state)
101 case CONNMAN_TECHNOLOGY_STATE_UNKNOWN:
103 case CONNMAN_TECHNOLOGY_STATE_OFFLINE:
105 case CONNMAN_TECHNOLOGY_STATE_AVAILABLE:
107 case CONNMAN_TECHNOLOGY_STATE_ENABLED:
109 case CONNMAN_TECHNOLOGY_STATE_CONNECTED:
116 static void state_changed(struct connman_technology *technology)
120 str = state2string(technology->state);
124 connman_dbus_property_changed_basic(technology->path,
125 CONNMAN_TECHNOLOGY_INTERFACE, "State",
126 DBUS_TYPE_STRING, &str);
129 static const char *get_name(enum connman_service_type type)
132 case CONNMAN_SERVICE_TYPE_UNKNOWN:
133 case CONNMAN_SERVICE_TYPE_SYSTEM:
134 case CONNMAN_SERVICE_TYPE_GPS:
135 case CONNMAN_SERVICE_TYPE_VPN:
137 case CONNMAN_SERVICE_TYPE_ETHERNET:
139 case CONNMAN_SERVICE_TYPE_WIFI:
141 case CONNMAN_SERVICE_TYPE_WIMAX:
143 case CONNMAN_SERVICE_TYPE_BLUETOOTH:
145 case CONNMAN_SERVICE_TYPE_CELLULAR:
152 static DBusMessage *get_properties(DBusConnection *conn,
153 DBusMessage *message, void *user_data)
155 struct connman_technology *technology = user_data;
157 DBusMessageIter array, dict;
160 reply = dbus_message_new_method_return(message);
164 dbus_message_iter_init_append(reply, &array);
166 connman_dbus_dict_open(&array, &dict);
168 str = state2string(technology->state);
170 connman_dbus_dict_append_basic(&dict, "State",
171 DBUS_TYPE_STRING, &str);
173 str = get_name(technology->type);
175 connman_dbus_dict_append_basic(&dict, "Name",
176 DBUS_TYPE_STRING, &str);
178 str = __connman_service_type2string(technology->type);
180 connman_dbus_dict_append_basic(&dict, "Type",
181 DBUS_TYPE_STRING, &str);
183 connman_dbus_dict_append_array(&dict, "Devices",
184 DBUS_TYPE_OBJECT_PATH, device_list, technology);
186 connman_dbus_dict_close(&array, &dict);
191 static GDBusMethodTable technology_methods[] = {
192 { "GetProperties", "", "a{sv}", get_properties },
196 static GDBusSignalTable technology_signals[] = {
197 { "PropertyChanged", "sv" },
201 static struct connman_technology *technology_find(enum connman_service_type type)
205 DBG("type %d", type);
207 for (list = technology_list; list; list = list->next) {
208 struct connman_technology *technology = list->data;
210 if (technology->type == type)
217 static struct connman_technology *technology_get(enum connman_service_type type)
219 struct connman_technology *technology;
222 DBG("type %d", type);
224 technology = technology_find(type);
225 if (technology != NULL) {
226 g_atomic_int_inc(&technology->refcount);
230 str = __connman_service_type2string(type);
234 technology = g_try_new0(struct connman_technology, 1);
235 if (technology == NULL)
238 technology->refcount = 1;
240 technology->type = type;
241 technology->path = g_strdup_printf("%s/technology/%s",
244 if (g_dbus_register_interface(connection, technology->path,
245 CONNMAN_TECHNOLOGY_INTERFACE,
246 technology_methods, technology_signals,
247 NULL, technology, NULL) == FALSE) {
248 connman_error("Failed to register %s", technology->path);
253 technology_list = g_slist_append(technology_list, technology);
255 technologies_changed();
258 DBG("technology %p", technology);
263 static void technology_put(struct connman_technology *technology)
265 DBG("technology %p", technology);
267 if (g_atomic_int_dec_and_test(&technology->refcount) == FALSE)
270 technology_list = g_slist_remove(technology_list, technology);
272 technologies_changed();
274 g_dbus_unregister_interface(connection, technology->path,
275 CONNMAN_TECHNOLOGY_INTERFACE);
277 g_free(technology->path);
281 static void unregister_device(gpointer data)
283 struct connman_technology *technology = data;
285 DBG("technology %p", technology);
287 technology_put(technology);
290 int __connman_technology_add_device(struct connman_device *device)
292 struct connman_technology *technology;
293 enum connman_service_type type;
295 DBG("device %p", device);
297 type = __connman_device_get_service_type(device);
299 technology = technology_get(type);
300 if (technology == NULL)
303 g_hash_table_insert(device_table, device, technology);
305 if (technology->device_list == NULL) {
306 technology->state = CONNMAN_TECHNOLOGY_STATE_AVAILABLE;
307 state_changed(technology);
310 technology->device_list = g_slist_append(technology->device_list,
312 devices_changed(technology);
317 int __connman_technology_remove_device(struct connman_device *device)
319 struct connman_technology *technology;
321 DBG("device %p", device);
323 technology = g_hash_table_lookup(device_table, device);
324 if (technology == NULL)
327 technology->device_list = g_slist_remove(technology->device_list,
329 devices_changed(technology);
331 if (technology->device_list == NULL) {
332 technology->state = CONNMAN_TECHNOLOGY_STATE_OFFLINE;
333 state_changed(technology);
336 g_hash_table_remove(device_table, device);
341 int __connman_technology_init(void)
345 connection = connman_dbus_get_connection();
347 device_table = g_hash_table_new_full(g_direct_hash, g_direct_equal,
348 NULL, unregister_device);
353 void __connman_technology_cleanup(void)
357 g_hash_table_destroy(device_table);
359 dbus_connection_unref(connection);