5 * Copyright (C) 2007-2008 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
26 #include <dbus/dbus.h>
27 #include <hal/libhal.h>
29 #include <connman/plugin.h>
30 #include <connman/element.h>
31 #include <connman/log.h>
35 enum connman_element_subtype subtype;
37 { "net.80203", CONNMAN_ELEMENT_SUBTYPE_ETHERNET },
38 { "net.80211", CONNMAN_ELEMENT_SUBTYPE_WIFI },
42 static GStaticMutex element_mutex = G_STATIC_MUTEX_INIT;
43 static GSList *element_list = NULL;
45 static void device_info(LibHalContext *ctx, const char *udi,
46 struct connman_element *element)
48 char *parent, *subsys, *value;
50 parent = libhal_device_get_property_string(ctx, udi,
53 subsys = libhal_device_get_property_string(ctx, udi,
54 "linux.subsystem", NULL);
56 value = libhal_device_get_property_string(ctx, udi,
57 "info.linux.driver", NULL);
59 value = libhal_device_get_property_string(ctx, parent,
60 "info.linux.driver", NULL);
62 element->info.driver = g_strdup(value);
65 if (g_str_equal(subsys, "net") == TRUE) {
66 value = libhal_device_get_property_string(ctx, parent,
69 element->info.vendor = g_strdup(value);
71 value = libhal_device_get_property_string(ctx, parent,
72 "info.product", NULL);
74 element->info.product = g_strdup(value);
78 static void device_netdev(LibHalContext *ctx, const char *udi,
79 struct connman_element *element)
81 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_ETHERNET ||
82 element->subtype == CONNMAN_ELEMENT_SUBTYPE_WIFI) {
83 element->netdev.index = libhal_device_get_property_int(ctx,
84 udi, "net.linux.ifindex", NULL);
86 element->netdev.name = libhal_device_get_property_string(ctx,
87 udi, "net.interface", NULL);
91 static void create_element(LibHalContext *ctx, const char *udi,
92 enum connman_element_subtype subtype)
94 struct connman_element *element;
96 DBG("ctx %p udi %s", ctx, udi);
98 element = connman_element_create();
100 element->name = g_path_get_basename(udi);
101 element->type = CONNMAN_ELEMENT_TYPE_DEVICE;
102 element->subtype = subtype;
104 device_info(ctx, udi, element);
105 device_netdev(ctx, udi, element);
107 g_static_mutex_lock(&element_mutex);
109 connman_element_register(element, NULL);
111 element_list = g_slist_append(element_list, element);
113 g_static_mutex_unlock(&element_mutex);
116 static void device_added(LibHalContext *ctx, const char *udi)
120 DBG("ctx %p udi %s", ctx, udi);
122 for (i = 0; capabilities[i].name; i++) {
123 if (libhal_device_query_capability(ctx, udi,
124 capabilities[i].name, NULL) == TRUE)
125 create_element(ctx, udi, capabilities[i].subtype);
129 static void device_removed(LibHalContext *ctx, const char *udi)
134 DBG("ctx %p udi %s", ctx, udi);
136 name = g_path_get_basename(udi);
138 g_static_mutex_lock(&element_mutex);
140 for (list = element_list; list; list = list->next) {
141 struct connman_element *element = list->data;
143 if (g_str_equal(element->name, name) == TRUE) {
144 element_list = g_slist_remove(element_list, element);
146 connman_element_unregister(element);
147 connman_element_unref(element);
152 g_static_mutex_unlock(&element_mutex);
157 static void probe_capability(LibHalContext *ctx, const char *capability,
158 enum connman_element_subtype subtype)
163 DBG("ctx %p capability %s", ctx, capability);
165 list = libhal_find_device_by_capability(ctx, capability, &num, NULL);
170 create_element(ctx, *tmp, subtype);
174 libhal_free_string_array(list);
178 static void find_devices(LibHalContext *ctx)
184 for (i = 0; capabilities[i].name; i++)
185 probe_capability(ctx, capabilities[i].name,
186 capabilities[i].subtype);
189 static LibHalContext *hal_ctx = NULL;
191 static void libhal_init(void *data)
193 DBusConnection *conn = data;
195 DBG("conn %p", conn);
200 hal_ctx = libhal_ctx_new();
204 if (libhal_ctx_set_dbus_connection(hal_ctx, conn) == FALSE) {
205 libhal_ctx_free(hal_ctx);
209 if (libhal_ctx_init(hal_ctx, NULL) == FALSE) {
210 libhal_ctx_free(hal_ctx);
214 libhal_ctx_set_device_added(hal_ctx, device_added);
215 libhal_ctx_set_device_removed(hal_ctx, device_removed);
217 //libhal_ctx_set_device_new_capability(hal_ctx, new_capability);
218 //libhal_ctx_set_device_lost_capability(hal_ctx, lost_capability);
220 find_devices(hal_ctx);
223 static void libhal_cleanup(void *data)
225 DBusConnection *conn = data;
228 DBG("conn %p", conn);
230 g_static_mutex_lock(&element_mutex);
232 for (list = element_list; list; list = list->next) {
233 struct connman_element *element = list->data;
235 connman_element_unregister(element);
236 connman_element_unref(element);
239 g_slist_free(element_list);
242 g_static_mutex_unlock(&element_mutex);
247 libhal_ctx_shutdown(hal_ctx, NULL);
249 libhal_ctx_free(hal_ctx);
254 static int hal_init(void)
256 DBusConnection *conn;
258 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
267 static void hal_exit(void)
269 DBusConnection *conn;
271 conn = libhal_ctx_get_dbus_connection(hal_ctx);
275 libhal_cleanup(conn);
277 dbus_connection_unref(conn);
280 CONNMAN_PLUGIN_DEFINE("HAL", "Hardware detection plugin", VERSION,