5 * Copyright (C) 2007 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
31 #include <hal/libhal.h>
35 static GSList *drivers = NULL;
37 int connman_iface_register(struct connman_iface_driver *driver)
39 DBG("driver %p", driver);
41 drivers = g_slist_append(drivers, driver);
46 void connman_iface_unregister(struct connman_iface_driver *driver)
48 DBG("driver %p", driver);
50 drivers = g_slist_remove(drivers, driver);
53 static GSList *interfaces = NULL;
55 static void device_free(void *data)
57 struct connman_iface *iface = data;
59 DBG("iface %p", iface);
61 if (iface->driver && iface->driver->remove)
62 iface->driver->remove(iface);
70 static int probe_device(LibHalContext *ctx,
71 struct connman_iface_driver *driver, const char *udi)
74 struct connman_iface *iface;
78 DBG("ctx %p driver %p udi %s", ctx, driver, udi);
83 iface = g_try_new0(struct connman_iface, 1);
87 temp = g_path_get_basename(udi);
88 iface->path = g_strdup_printf("%s/%s", CONNMAN_IFACE_BASEPATH, temp);
91 iface->udi = g_strdup(udi);
93 DBG("path %s", iface->path);
95 sysfs = libhal_device_get_property_string(ctx, udi,
96 "linux.sysfs_path", NULL);
98 iface->sysfs = g_strdup(sysfs);
100 iface->type = CONNMAN_IFACE_TYPE_UNKNOWN;
103 DBG("iface %p", iface);
105 err = driver->probe(iface);
111 iface->driver = driver;
113 conn = libhal_ctx_get_dbus_connection(ctx);
115 g_dbus_register_object(conn, iface->path, iface, device_free);
117 interfaces = g_slist_append(interfaces, iface);
122 static void device_added(LibHalContext *ctx, const char *udi)
126 DBG("ctx %p udi %s", ctx, udi);
128 for (list = drivers; list; list = list->next) {
129 struct connman_iface_driver *driver = list->data;
131 if (driver->capability == NULL)
134 if (libhal_device_query_capability(ctx, udi,
135 driver->capability, NULL) == TRUE) {
136 if (probe_device(ctx, driver, udi) == 0)
142 static void device_removed(LibHalContext *ctx, const char *udi)
144 DBusConnection *conn;
147 DBG("ctx %p udi %s", ctx, udi);
149 conn = libhal_ctx_get_dbus_connection(ctx);
151 for (list = interfaces; list; list = list->next) {
152 struct connman_iface *iface = list->data;
154 if (strcmp(udi, iface->udi) == 0) {
155 interfaces = g_slist_remove(interfaces, iface);
156 g_dbus_unregister_object(conn, iface->path);
162 static void probe_driver(LibHalContext *ctx,
163 struct connman_iface_driver *driver)
168 DBG("ctx %p driver %p", ctx, driver);
170 list = libhal_find_device_by_capability(ctx,
171 driver->capability, &num, NULL);
176 probe_device(ctx, driver, *tmp);
180 libhal_free_string_array(list);
184 static void find_devices(LibHalContext *ctx)
190 for (list = drivers; list; list = list->next) {
191 struct connman_iface_driver *driver = list->data;
193 DBG("driver %p", driver);
195 if (driver->capability == NULL)
198 probe_driver(ctx, driver);
202 static LibHalContext *hal_ctx = NULL;
204 static void hal_init(void *data)
206 DBusConnection *conn = data;
208 DBG("conn %p", conn);
213 hal_ctx = libhal_ctx_new();
217 if (libhal_ctx_set_dbus_connection(hal_ctx, conn) == FALSE) {
218 libhal_ctx_free(hal_ctx);
222 if (libhal_ctx_init(hal_ctx, NULL) == FALSE) {
223 libhal_ctx_free(hal_ctx);
227 libhal_ctx_set_device_added(hal_ctx, device_added);
228 libhal_ctx_set_device_removed(hal_ctx, device_removed);
230 //libhal_ctx_set_device_new_capability(hal_ctx, new_capability);
231 //libhal_ctx_set_device_lost_capability(hal_ctx, lost_capability);
233 find_devices(hal_ctx);
236 static void hal_cleanup(void *data)
238 DBusConnection *conn = data;
241 DBG("conn %p", conn);
246 for (list = interfaces; list; list = list->next) {
247 struct connman_iface *iface = list->data;
249 DBG("path %s", iface->path);
251 g_dbus_unregister_object(conn, iface->path);
254 g_slist_free(interfaces);
258 libhal_ctx_shutdown(hal_ctx, NULL);
260 libhal_ctx_free(hal_ctx);
265 static DBusConnection *connection = NULL;
266 static guint hal_watch = 0;
268 int __connman_iface_init(DBusConnection *conn)
270 DBG("conn %p", conn);
272 connection = dbus_connection_ref(conn);
273 if (connection == NULL)
276 hal_init(connection);
278 hal_watch = g_dbus_add_watch(connection, "org.freedesktop.Hal",
279 hal_init, hal_cleanup, connection, NULL);
284 void __connman_iface_cleanup(void)
286 DBG("conn %p", connection);
288 g_dbus_remove_watch(connection, hal_watch);
290 hal_cleanup(connection);
292 dbus_connection_unref(connection);