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 connman_element_add_static_property(element,
63 "Driver", DBUS_TYPE_STRING, &value);
66 if (g_str_equal(subsys, "net") == TRUE) {
67 value = libhal_device_get_property_string(ctx, parent,
70 connman_element_add_static_property(element,
71 "Vendor", DBUS_TYPE_STRING, &value);
73 value = libhal_device_get_property_string(ctx, parent,
74 "info.product", NULL);
76 connman_element_add_static_property(element,
77 "Product", DBUS_TYPE_STRING, &value);
81 static void device_netdev(LibHalContext *ctx, const char *udi,
82 struct connman_element *element)
84 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_ETHERNET ||
85 element->subtype == CONNMAN_ELEMENT_SUBTYPE_WIFI) {
86 element->netdev.index = libhal_device_get_property_int(ctx,
87 udi, "net.linux.ifindex", NULL);
89 element->netdev.name = libhal_device_get_property_string(ctx,
90 udi, "net.interface", NULL);
94 static void create_element(LibHalContext *ctx, const char *udi,
95 enum connman_element_subtype subtype)
97 struct connman_element *element;
99 DBG("ctx %p udi %s", ctx, udi);
101 element = connman_element_create();
103 element->name = g_path_get_basename(udi);
104 element->type = CONNMAN_ELEMENT_TYPE_DEVICE;
105 element->subtype = subtype;
107 device_info(ctx, udi, element);
108 device_netdev(ctx, udi, element);
110 g_static_mutex_lock(&element_mutex);
112 connman_element_register(element, NULL);
114 element_list = g_slist_append(element_list, element);
116 g_static_mutex_unlock(&element_mutex);
119 static void device_added(LibHalContext *ctx, const char *udi)
123 DBG("ctx %p udi %s", ctx, udi);
125 for (i = 0; capabilities[i].name; i++) {
126 if (libhal_device_query_capability(ctx, udi,
127 capabilities[i].name, NULL) == TRUE)
128 create_element(ctx, udi, capabilities[i].subtype);
132 static void device_removed(LibHalContext *ctx, const char *udi)
137 DBG("ctx %p udi %s", ctx, udi);
139 name = g_path_get_basename(udi);
141 g_static_mutex_lock(&element_mutex);
143 for (list = element_list; list; list = list->next) {
144 struct connman_element *element = list->data;
146 if (g_str_equal(element->name, name) == TRUE) {
147 element_list = g_slist_remove(element_list, element);
149 connman_element_unregister(element);
150 connman_element_unref(element);
155 g_static_mutex_unlock(&element_mutex);
160 static void probe_capability(LibHalContext *ctx, const char *capability,
161 enum connman_element_subtype subtype)
166 DBG("ctx %p capability %s", ctx, capability);
168 list = libhal_find_device_by_capability(ctx, capability, &num, NULL);
173 create_element(ctx, *tmp, subtype);
177 libhal_free_string_array(list);
181 static void find_devices(LibHalContext *ctx)
187 for (i = 0; capabilities[i].name; i++)
188 probe_capability(ctx, capabilities[i].name,
189 capabilities[i].subtype);
192 static LibHalContext *hal_ctx = NULL;
194 static void libhal_init(void *data)
196 DBusConnection *conn = data;
198 DBG("conn %p", conn);
203 hal_ctx = libhal_ctx_new();
207 if (libhal_ctx_set_dbus_connection(hal_ctx, conn) == FALSE) {
208 libhal_ctx_free(hal_ctx);
212 if (libhal_ctx_init(hal_ctx, NULL) == FALSE) {
213 libhal_ctx_free(hal_ctx);
217 libhal_ctx_set_device_added(hal_ctx, device_added);
218 libhal_ctx_set_device_removed(hal_ctx, device_removed);
220 //libhal_ctx_set_device_new_capability(hal_ctx, new_capability);
221 //libhal_ctx_set_device_lost_capability(hal_ctx, lost_capability);
223 find_devices(hal_ctx);
226 static void libhal_cleanup(void *data)
228 DBusConnection *conn = data;
231 DBG("conn %p", conn);
233 g_static_mutex_lock(&element_mutex);
235 for (list = element_list; list; list = list->next) {
236 struct connman_element *element = list->data;
238 connman_element_unregister(element);
239 connman_element_unref(element);
242 g_slist_free(element_list);
245 g_static_mutex_unlock(&element_mutex);
250 libhal_ctx_shutdown(hal_ctx, NULL);
252 libhal_ctx_free(hal_ctx);
257 static int hal_init(void)
259 DBusConnection *conn;
261 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
270 static void hal_exit(void)
272 DBusConnection *conn;
274 conn = libhal_ctx_get_dbus_connection(hal_ctx);
278 libhal_cleanup(conn);
280 dbus_connection_unref(conn);
283 CONNMAN_PLUGIN_DEFINE("HAL", "Hardware detection plugin", VERSION,