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
29 #include <dbus/dbus.h>
30 #include <hal/libhal.h>
32 #include <connman/plugin.h>
33 #include <connman/element.h>
34 #include <connman/log.h>
38 enum connman_element_subtype subtype;
40 { "net.80203", CONNMAN_ELEMENT_SUBTYPE_ETHERNET },
41 { "net.80211", CONNMAN_ELEMENT_SUBTYPE_WIFI },
42 { "net.wimax", CONNMAN_ELEMENT_SUBTYPE_WIMAX },
43 { "modem", CONNMAN_ELEMENT_SUBTYPE_MODEM },
47 static GStaticMutex element_mutex = G_STATIC_MUTEX_INIT;
48 static GSList *element_list = NULL;
50 static void device_info(LibHalContext *ctx, const char *udi,
51 struct connman_element *element)
53 char *parent, *subsys, *value;
55 parent = libhal_device_get_property_string(ctx, udi,
58 subsys = libhal_device_get_property_string(ctx, udi,
59 "linux.subsystem", NULL);
61 value = libhal_device_get_property_string(ctx, udi,
62 "info.linux.driver", NULL);
64 value = libhal_device_get_property_string(ctx, parent,
65 "info.linux.driver", NULL);
67 connman_element_add_static_property(element,
68 "Driver", DBUS_TYPE_STRING, &value);
71 if (g_str_equal(subsys, "net") == TRUE ||
72 g_str_equal(subsys, "tty") == TRUE) {
73 value = libhal_device_get_property_string(ctx, parent,
76 connman_element_add_static_property(element,
77 "Vendor", DBUS_TYPE_STRING, &value);
79 value = libhal_device_get_property_string(ctx, parent,
80 "info.product", NULL);
82 connman_element_add_static_property(element,
83 "Product", DBUS_TYPE_STRING, &value);
87 static void device_netdev(LibHalContext *ctx, const char *udi,
88 struct connman_element *element)
90 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_ETHERNET ||
91 element->subtype == CONNMAN_ELEMENT_SUBTYPE_WIFI ||
92 element->subtype == CONNMAN_ELEMENT_SUBTYPE_WIMAX) {
93 element->index = libhal_device_get_property_int(ctx,
94 udi, "net.linux.ifindex", NULL);
96 element->name = libhal_device_get_property_string(ctx,
97 udi, "net.interface", NULL);
100 if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_MODEM) {
101 element->index = libhal_device_get_property_int(ctx,
102 udi, "serial.port", NULL);
104 element->name = libhal_device_get_property_string(ctx,
105 udi, "serial.device", NULL);
109 static void create_element(LibHalContext *ctx, const char *udi,
110 enum connman_element_subtype subtype)
112 struct connman_element *element;
114 DBG("ctx %p udi %s", ctx, udi);
116 if (subtype == CONNMAN_ELEMENT_SUBTYPE_ETHERNET) {
117 char *sysfs_path, wimax_path[PATH_MAX];
120 sysfs_path = libhal_device_get_property_string(ctx, udi,
121 "linux.sysfs_path", NULL);
122 if (sysfs_path != NULL) {
123 snprintf(wimax_path, PATH_MAX, "%s/wimax", sysfs_path);
125 if (stat(wimax_path, &st) == 0 &&
126 (st.st_mode & S_IFDIR))
127 subtype = CONNMAN_ELEMENT_SUBTYPE_WIMAX;
131 element = connman_element_create(NULL);
133 element->type = CONNMAN_ELEMENT_TYPE_DEVICE;
134 element->subtype = subtype;
136 device_info(ctx, udi, element);
137 device_netdev(ctx, udi, element);
139 if (element->name == NULL) {
140 element->name = g_path_get_basename(udi);
141 if (element->name == NULL) {
142 connman_element_unref(element);
147 g_static_mutex_lock(&element_mutex);
149 connman_element_register(element, NULL);
151 element_list = g_slist_append(element_list, element);
153 g_static_mutex_unlock(&element_mutex);
156 static void device_added(LibHalContext *ctx, const char *udi)
160 DBG("ctx %p udi %s", ctx, udi);
162 for (i = 0; capabilities[i].name; i++) {
163 if (libhal_device_query_capability(ctx, udi,
164 capabilities[i].name, NULL) == TRUE)
165 create_element(ctx, udi, capabilities[i].subtype);
169 static void device_removed(LibHalContext *ctx, const char *udi)
171 struct connman_element *removal = NULL;
175 DBG("ctx %p udi %s", ctx, udi);
177 name = g_path_get_basename(udi);
179 g_static_mutex_lock(&element_mutex);
181 for (list = element_list; list; list = list->next) {
182 struct connman_element *element = list->data;
184 if (g_str_equal(element->name, name) == TRUE) {
190 if (removal != NULL) {
191 element_list = g_slist_remove(element_list, removal);
193 connman_element_unregister(removal);
194 connman_element_unref(removal);
197 g_static_mutex_unlock(&element_mutex);
202 static void probe_capability(LibHalContext *ctx, const char *capability,
203 enum connman_element_subtype subtype)
208 DBG("ctx %p capability %s", ctx, capability);
210 list = libhal_find_device_by_capability(ctx, capability, &num, NULL);
215 create_element(ctx, *tmp, subtype);
219 libhal_free_string_array(list);
223 static void find_devices(LibHalContext *ctx)
229 for (i = 0; capabilities[i].name; i++)
230 probe_capability(ctx, capabilities[i].name,
231 capabilities[i].subtype);
234 static LibHalContext *hal_ctx = NULL;
236 static void libhal_init(void *data)
238 DBusConnection *conn = data;
240 DBG("conn %p", conn);
245 hal_ctx = libhal_ctx_new();
249 if (libhal_ctx_set_dbus_connection(hal_ctx, conn) == FALSE) {
250 libhal_ctx_free(hal_ctx);
254 if (libhal_ctx_init(hal_ctx, NULL) == FALSE) {
255 libhal_ctx_free(hal_ctx);
259 libhal_ctx_set_device_added(hal_ctx, device_added);
260 libhal_ctx_set_device_removed(hal_ctx, device_removed);
262 //libhal_ctx_set_device_new_capability(hal_ctx, new_capability);
263 //libhal_ctx_set_device_lost_capability(hal_ctx, lost_capability);
265 find_devices(hal_ctx);
268 static void libhal_cleanup(void *data)
270 DBusConnection *conn = data;
273 DBG("conn %p", conn);
275 g_static_mutex_lock(&element_mutex);
277 for (list = element_list; list; list = list->next) {
278 struct connman_element *element = list->data;
280 connman_element_unregister(element);
281 connman_element_unref(element);
284 g_slist_free(element_list);
287 g_static_mutex_unlock(&element_mutex);
292 libhal_ctx_shutdown(hal_ctx, NULL);
294 libhal_ctx_free(hal_ctx);
299 static int hal_init(void)
301 DBusConnection *conn;
303 conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
312 static void hal_exit(void)
314 DBusConnection *conn;
316 conn = libhal_ctx_get_dbus_connection(hal_ctx);
320 libhal_cleanup(conn);
322 dbus_connection_unref(conn);
325 CONNMAN_PLUGIN_DEFINE("hal", "Hardware detection plugin", VERSION,