summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2008-10-14 18:05:16 +0200
committerMarcel Holtmann <marcel@holtmann.org>2008-10-14 18:05:16 +0200
commit4fe64c1ea26c8d0d5c2d51c495a83824613cf2e4 (patch)
tree3d03b520b48814db2099fd0fd09c1c3cecdc7a90 /src
parent0af74f53a3dfd5d4aa93326d5900b8562b019687 (diff)
downloadconnman-4fe64c1ea26c8d0d5c2d51c495a83824613cf2e4.tar.gz
connman-4fe64c1ea26c8d0d5c2d51c495a83824613cf2e4.tar.bz2
connman-4fe64c1ea26c8d0d5c2d51c495a83824613cf2e4.zip
Add first step towards providing device abstraction
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am3
-rw-r--r--src/device.c152
2 files changed, 154 insertions, 1 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 52082cd1..afa1ce91 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -12,7 +12,8 @@ DISTCLEANFILES = $(service_DATA)
sbin_PROGRAMS = connmand
connmand_SOURCES = main.c connman.h log.c error.c plugin.c profile.c \
- element.c security.c storage.c manager.c agent.c rtnl.c
+ element.c device.c security.c storage.c \
+ manager.c agent.c rtnl.c
connmand_LDADD = @GDBUS_LIBS@ @GLIB_LIBS@ @GTHREAD_LIBS@ -ldl
diff --git a/src/device.c b/src/device.c
new file mode 100644
index 00000000..6b73438a
--- /dev/null
+++ b/src/device.c
@@ -0,0 +1,152 @@
+/*
+ *
+ * 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 <errno.h>
+
+#include "connman.h"
+
+static GSList *driver_list = NULL;
+
+static gboolean match_driver(struct connman_device *device,
+ struct connman_device_driver *driver)
+{
+ if (device->element->subtype == driver->type ||
+ driver->type == CONNMAN_DEVICE_TYPE_UNKNOWN)
+ return TRUE;
+
+ return FALSE;
+}
+
+static int device_probe(struct connman_element *element)
+{
+ struct connman_device *device;
+ GSList *list;
+
+ DBG("element %p name %s", element, element->name);
+
+ device = g_try_new0(struct connman_device, 1);
+ if (device == NULL)
+ return -ENOMEM;
+
+ device->element = element;
+
+ for (list = driver_list; list; list = list->next) {
+ struct connman_device_driver *driver = list->data;
+
+ if (match_driver(device, driver) == FALSE)
+ continue;
+
+ DBG("driver %p name %s", driver, driver->name);
+
+ if (driver->probe(device) == 0) {
+ device->driver = driver;
+ connman_element_set_data(element, device);
+ return 0;
+ }
+ }
+
+ g_free(device);
+
+ return -ENODEV;
+}
+
+static int device_remove(struct connman_element *element)
+{
+ struct connman_device *device = connman_element_get_data(element);
+
+ DBG("element %p name %s", element, element->name);
+
+ if (device->driver && device->driver->remove)
+ device->driver->remove(device);
+
+ connman_element_set_data(element, NULL);
+
+ g_free(device);
+
+ return 0;
+}
+
+static struct connman_driver device_driver = {
+ .name = "device",
+ .type = CONNMAN_ELEMENT_TYPE_DEVICE,
+ .priority = CONNMAN_DRIVER_PRIORITY_LOW,
+ .probe = device_probe,
+ .remove = device_remove,
+};
+
+int __connman_device_init(void)
+{
+ DBG("");
+
+ return connman_driver_register(&device_driver);
+}
+
+void __connman_device_cleanup(void)
+{
+ DBG("");
+
+ connman_driver_unregister(&device_driver);
+}
+
+static gint compare_priority(gconstpointer a, gconstpointer b)
+{
+ const struct connman_device_driver *driver1 = a;
+ const struct connman_device_driver *driver2 = b;
+
+ return driver2->priority - driver1->priority;
+}
+
+/**
+ * connman_device_driver_register:
+ * @driver: device driver definition
+ *
+ * Register a new device driver
+ *
+ * Returns: %0 on success
+ */
+int connman_device_driver_register(struct connman_device_driver *driver)
+{
+ DBG("driver %p name %s", driver, driver->name);
+
+ driver_list = g_slist_insert_sorted(driver_list, driver,
+ compare_priority);
+
+ __connman_driver_rescan(&device_driver);
+
+ return 0;
+}
+
+/**
+ * connman_device_driver_unregister:
+ * @driver: device driver definition
+ *
+ * Remove a previously registered device driver
+ */
+void connman_device_driver_unregister(struct connman_device_driver *driver)
+{
+ DBG("driver %p name %s", driver, driver->name);
+
+ driver_list = g_slist_remove(driver_list, driver);
+}