Add HAL based hardware detection plugin
authorMarcel Holtmann <marcel@holtmann.org>
Sat, 28 Jun 2008 08:32:59 +0000 (10:32 +0200)
committerMarcel Holtmann <marcel@holtmann.org>
Sat, 28 Jun 2008 08:32:59 +0000 (10:32 +0200)
plugins/Makefile.am
plugins/hal.c [new file with mode: 0644]

index 9047739..8e5e858 100644 (file)
@@ -1,8 +1,12 @@
 
 plugindir = $(libdir)/connman/plugins
 
-plugin_LTLIBRARIES = 80203.la 80211.la bluetooth.la \
-                               dhclient.la resolvconf.la
+plugin_LTLIBRARIES = hal.la 80203.la 80211.la bluetooth.la \
+                                       dhclient.la resolvconf.la
+
+hal_la_SOURCES = hal.c
+hal_la_LIBADD = @HAL_LIBS@
+hal_la_CFLAGS = @GLIB_CFLAGS@ @HAL_CFLAGS@
 
 80203_la_SOURCES = 80203.c
 
diff --git a/plugins/hal.c b/plugins/hal.c
new file mode 100644 (file)
index 0000000..bb5ad94
--- /dev/null
@@ -0,0 +1,281 @@
+/*
+ *
+ *  Connection Manager
+ *
+ *  Copyright (C) 2007-2008  Intel Corporation. All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2 as
+ *  published by the Free Software Foundation.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <dbus/dbus.h>
+#include <hal/libhal.h>
+
+#include <connman/plugin.h>
+#include <connman/element.h>
+#include <connman/log.h>
+
+static struct {
+       const char *name;
+       enum connman_element_subtype subtype;
+} capabilities[] = {
+       { "net.80203", CONNMAN_ELEMENT_SUBTYPE_ETHERNET },
+       { "net.80211", CONNMAN_ELEMENT_SUBTYPE_WIFI     },
+       { }
+};
+
+static GStaticMutex element_mutex = G_STATIC_MUTEX_INIT;
+static GSList *element_list = NULL;
+
+static void device_info(LibHalContext *ctx, const char *udi,
+                                       struct connman_element *element)
+{
+       char *parent, *subsys, *value;
+
+       parent = libhal_device_get_property_string(ctx, udi,
+                                               "info.parent", NULL);
+
+       subsys = libhal_device_get_property_string(ctx, udi,
+                                               "linux.subsystem", NULL);
+
+       value = libhal_device_get_property_string(ctx, udi,
+                                               "info.linux.driver", NULL);
+       if (value == NULL) {
+               value = libhal_device_get_property_string(ctx, parent,
+                                               "info.linux.driver", NULL);
+               if (value != NULL)
+                       element->info.driver = g_strdup(value);
+       }
+
+       if (g_str_equal(subsys, "net") == TRUE) {
+               value = libhal_device_get_property_string(ctx, parent,
+                                                       "info.vendor", NULL);
+               if (value != NULL)
+                       element->info.vendor = g_strdup(value);
+
+               value = libhal_device_get_property_string(ctx, parent,
+                                                       "info.product", NULL);
+               if (value != NULL)
+                       element->info.product = g_strdup(value);
+       }
+}
+
+static void device_netdev(LibHalContext *ctx, const char *udi,
+                                       struct connman_element *element)
+{
+       if (element->subtype == CONNMAN_ELEMENT_SUBTYPE_ETHERNET ||
+                       element->subtype == CONNMAN_ELEMENT_SUBTYPE_WIFI) {
+               element->netdev.index = libhal_device_get_property_int(ctx,
+                                               udi, "net.linux.ifindex", NULL);
+
+               element->netdev.name = libhal_device_get_property_string(ctx,
+                                               udi, "net.interface", NULL);
+       }
+}
+
+static void create_element(LibHalContext *ctx, const char *udi,
+                                       enum connman_element_subtype subtype)
+{
+       struct connman_element *element;
+
+       DBG("ctx %p udi %s", ctx, udi);
+
+       element = connman_element_create();
+
+       element->name = g_path_get_basename(udi);
+       element->type = CONNMAN_ELEMENT_TYPE_DEVICE;
+       element->subtype = subtype;
+
+       device_info(ctx, udi, element);
+       device_netdev(ctx, udi, element);
+
+       g_static_mutex_lock(&element_mutex);
+
+       connman_element_register(element, NULL);
+
+       element_list = g_slist_append(element_list, element);
+
+       g_static_mutex_unlock(&element_mutex);
+}
+
+static void device_added(LibHalContext *ctx, const char *udi)
+{
+       int i;
+
+       DBG("ctx %p udi %s", ctx, udi);
+
+       for (i = 0; capabilities[i].name; i++) {
+               if (libhal_device_query_capability(ctx, udi,
+                                       capabilities[i].name, NULL) == TRUE)
+                       create_element(ctx, udi, capabilities[i].subtype);
+       }
+}
+
+static void device_removed(LibHalContext *ctx, const char *udi)
+{
+       GSList *list;
+       gchar *name;
+
+       DBG("ctx %p udi %s", ctx, udi);
+
+       name = g_path_get_basename(udi);
+
+       g_static_mutex_lock(&element_mutex);
+
+       for (list = element_list; list; list = list->next) {
+               struct connman_element *element = list->data;
+
+               if (g_str_equal(element->name, name) == TRUE) {
+                       element_list = g_slist_remove(element_list, element);
+
+                       connman_element_unregister(element);
+                       connman_element_unref(element);
+                       break;
+               }
+       }
+
+       g_static_mutex_unlock(&element_mutex);
+
+       g_free(name);
+}
+
+static void probe_capability(LibHalContext *ctx, const char *capability,
+                                       enum connman_element_subtype subtype)
+{
+       char **list;
+       int num;
+
+       DBG("ctx %p capability %s", ctx, capability);
+
+       list = libhal_find_device_by_capability(ctx, capability, &num, NULL);
+       if (list) {
+               char **tmp = list;
+
+               while (*tmp) {
+                       create_element(ctx, *tmp, subtype);
+                       tmp++;
+               }
+
+               libhal_free_string_array(list);
+       }
+}
+
+static void find_devices(LibHalContext *ctx)
+{
+       int i;
+
+       DBG("ctx %p", ctx);
+
+       for (i = 0; capabilities[i].name; i++)
+               probe_capability(ctx, capabilities[i].name,
+                                               capabilities[i].subtype);
+}
+
+static LibHalContext *hal_ctx = NULL;
+
+static void libhal_init(void *data)
+{
+       DBusConnection *conn = data;
+
+       DBG("conn %p", conn);
+
+       if (hal_ctx != NULL)
+               return;
+
+       hal_ctx = libhal_ctx_new();
+       if (hal_ctx == NULL)
+               return;
+
+       if (libhal_ctx_set_dbus_connection(hal_ctx, conn) == FALSE) {
+               libhal_ctx_free(hal_ctx);
+               return;
+       }
+
+       if (libhal_ctx_init(hal_ctx, NULL) == FALSE) {
+               libhal_ctx_free(hal_ctx);
+               return ;
+       }
+
+       libhal_ctx_set_device_added(hal_ctx, device_added);
+       libhal_ctx_set_device_removed(hal_ctx, device_removed);
+
+       //libhal_ctx_set_device_new_capability(hal_ctx, new_capability);
+       //libhal_ctx_set_device_lost_capability(hal_ctx, lost_capability);
+
+       find_devices(hal_ctx);
+}
+
+static void libhal_cleanup(void *data)
+{
+       DBusConnection *conn = data;
+       GSList *list;
+
+       DBG("conn %p", conn);
+
+       g_static_mutex_lock(&element_mutex);
+
+       for (list = element_list; list; list = list->next) {
+               struct connman_element *element = list->data;
+
+               connman_element_unregister(element);
+               connman_element_unref(element);
+       }
+
+       g_slist_free(element_list);
+       element_list = NULL;
+
+       g_static_mutex_unlock(&element_mutex);
+
+       if (hal_ctx == NULL)
+               return;
+
+       libhal_ctx_shutdown(hal_ctx, NULL);
+
+       libhal_ctx_free(hal_ctx);
+
+       hal_ctx = NULL;
+}
+
+static int hal_init(void)
+{
+       DBusConnection *conn;
+
+       conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
+       if (conn == NULL)
+               return -EIO;
+
+       libhal_init(conn);
+
+       return 0;
+}
+
+static void hal_exit(void)
+{
+       DBusConnection *conn;
+
+       conn = libhal_ctx_get_dbus_connection(hal_ctx);
+       if (conn == NULL)
+               return;
+
+       libhal_cleanup(conn);
+
+       dbus_connection_unref(conn);
+}
+
+CONNMAN_PLUGIN_DEFINE("HAL", "Hardware detection plugin", VERSION,
+                                                       hal_init, hal_exit)