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 },
39 { "modem", CONNMAN_ELEMENT_SUBTYPE_MODEM },
43 static GStaticMutex element_mutex = G_STATIC_MUTEX_INIT;
44 static GSList *element_list = NULL;
46 static void device_info(LibHalContext *ctx, const char *udi,
47 struct connman_element *element)
49 char *parent, *subsys, *value;
51 parent = libhal_device_get_property_string(ctx, udi,
54 subsys = libhal_device_get_property_string(ctx, udi,
55 "linux.subsystem", NULL);
57 value = libhal_device_get_property_string(ctx, udi,
58 "info.linux.driver", NULL);
60 value = libhal_device_get_property_string(ctx, parent,
61 "info.linux.driver", NULL);
63 connman_element_add_static_property(element,
64 "Driver", DBUS_TYPE_STRING, &value);
67 if (g_str_equal(subsys, "net") == TRUE ||
68 g_str_equal(subsys, "tty") == TRUE) {
69 value = libhal_device_get_property_string(ctx, parent,
72 connman_element_add_static_property(element,
73 "Vendor", DBUS_TYPE_STRING, &value);
75 value = libhal_device_get_property_string(ctx, parent,
76 "info.product", NULL);
78 connman_element_add_static_property(element,
79 "Product", DBUS_TYPE_STRING, &value);
83 static void device_netdev(LibHalContext *ctx, const char *udi,
84 struct connman_element *element)
86 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_ETHERNET ||
87 element->subtype == CONNMAN_ELEMENT_SUBTYPE_WIFI) {
88 element->index = libhal_device_get_property_int(ctx,
89 udi, "net.linux.ifindex", NULL);
91 element->name = libhal_device_get_property_string(ctx,
92 udi, "net.interface", NULL);
95 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_MODEM) {
96 element->index = libhal_device_get_property_int(ctx,
97 udi, "serial.port", NULL);
99 element->name = libhal_device_get_property_string(ctx,
100 udi, "serial.device", NULL);
104 static void create_element(LibHalContext *ctx, const char *udi,
105 enum connman_element_subtype subtype)
107 struct connman_element *element;
109 DBG("ctx %p udi %s", ctx, udi);
111 element = connman_element_create(NULL);
113 element->type = CONNMAN_ELEMENT_TYPE_DEVICE;
114 element->subtype = subtype;
116 device_info(ctx, udi, element);
117 device_netdev(ctx, udi, element);
119 if (element->name == NULL) {
120 element->name = g_path_get_basename(udi);
121 if (element->name == NULL) {
122 connman_element_unref(element);
127 g_static_mutex_lock(&element_mutex);
129 connman_element_register(element, NULL);
131 element_list = g_slist_append(element_list, element);
133 g_static_mutex_unlock(&element_mutex);
136 static void device_added(LibHalContext *ctx, const char *udi)
140 DBG("ctx %p udi %s", ctx, udi);
142 for (i = 0; capabilities[i].name; i++) {
143 if (libhal_device_query_capability(ctx, udi,
144 capabilities[i].name, NULL) == TRUE)
145 create_element(ctx, udi, capabilities[i].subtype);
149 static void device_removed(LibHalContext *ctx, const char *udi)
154 DBG("ctx %p udi %s", ctx, udi);
156 name = g_path_get_basename(udi);
158 g_static_mutex_lock(&element_mutex);
160 for (list = element_list; list; list = list->next) {
161 struct connman_element *element = list->data;
163 if (g_str_equal(element->name, name) == TRUE) {
164 element_list = g_slist_remove(element_list, element);
166 connman_element_unregister(element);
167 connman_element_unref(element);
172 g_static_mutex_unlock(&element_mutex);
177 static void probe_capability(LibHalContext *ctx, const char *capability,
178 enum connman_element_subtype subtype)
183 DBG("ctx %p capability %s", ctx, capability);
185 list = libhal_find_device_by_capability(ctx, capability, &num, NULL);
190 create_element(ctx, *tmp, subtype);
194 libhal_free_string_array(list);
198 static void find_devices(LibHalContext *ctx)
204 for (i = 0; capabilities[i].name; i++)
205 probe_capability(ctx, capabilities[i].name,
206 capabilities[i].subtype);
209 static LibHalContext *hal_ctx = NULL;
211 static void libhal_init(void *data)
213 DBusConnection *conn = data;
215 DBG("conn %p", conn);
220 hal_ctx = libhal_ctx_new();
224 if (libhal_ctx_set_dbus_connection(hal_ctx, conn) == FALSE) {
225 libhal_ctx_free(hal_ctx);
229 if (libhal_ctx_init(hal_ctx, NULL) == FALSE) {
230 libhal_ctx_free(hal_ctx);
234 libhal_ctx_set_device_added(hal_ctx, device_added);
235 libhal_ctx_set_device_removed(hal_ctx, device_removed);
237 //libhal_ctx_set_device_new_capability(hal_ctx, new_capability);
238 //libhal_ctx_set_device_lost_capability(hal_ctx, lost_capability);
240 find_devices(hal_ctx);
243 static void libhal_cleanup(void *data)
245 DBusConnection *conn = data;
248 DBG("conn %p", conn);
250 g_static_mutex_lock(&element_mutex);
252 for (list = element_list; list; list = list->next) {
253 struct connman_element *element = list->data;
255 connman_element_unregister(element);
256 connman_element_unref(element);
259 g_slist_free(element_list);
262 g_static_mutex_unlock(&element_mutex);
267 libhal_ctx_shutdown(hal_ctx, NULL);
269 libhal_ctx_free(hal_ctx);
274 static int hal_init(void)
276 DBusConnection *conn;
278 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
287 static void hal_exit(void)
289 DBusConnection *conn;
291 conn = libhal_ctx_get_dbus_connection(hal_ctx);
295 libhal_cleanup(conn);
297 dbus_connection_unref(conn);
300 CONNMAN_PLUGIN_DEFINE("hal", "Hardware detection plugin", VERSION,