Patchwork [10/24] petitboot: Add discover device report

login
register
mail settings
Submitter Geoff Levand
Date April 13, 2009, 1:11 a.m.
Message ID <20090413011137.254063505@am.sony.com>
Download mbox | patch
Permalink /patch/25879/
State Accepted
Headers show

Comments

Geoff Levand - April 13, 2009, 1:11 a.m.
Fillout the missing discover code that reports current devices.
Replaces device_handler_get_current_devices() with two new
accessor routines device_handler_get_device_count() and
device_handler_get_device().

Signed-off-by: Geoff Levand <geoffrey.levand@am.sony.com>
---
 discover/device-handler.c  |   92 ++++++++++++++++++++++++++++++++++++++++++---
 discover/device-handler.h  |    5 +-
 discover/discover-server.c |   12 +++--
 3 files changed, 97 insertions(+), 12 deletions(-)

Patch

--- a/discover/device-handler.c
+++ b/discover/device-handler.c
@@ -1,4 +1,5 @@ 
 
+#include <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
@@ -24,8 +25,8 @@ 
 struct device_handler {
 	struct discover_server *server;
 
-	struct device *devices;
-	int n_devices;
+	struct device **devices;
+	unsigned int n_devices;
 
 	struct list contexts;
 };
@@ -35,13 +36,88 @@  struct mount_map {
 	char *mount_point;
 };
 
-int device_handler_get_current_devices(struct device_handler *handler,
-	const struct device **devices)
+/**
+ * device_handler_add - Add a device to the handler device array.
+ */
+
+static void device_handler_add(struct device_handler *handler,
+	struct device *device)
+{
+	handler->n_devices++;
+	handler->devices = talloc_realloc(handler, handler->devices,
+		struct device *, handler->n_devices);
+	handler->devices[handler->n_devices - 1] = device;
+}
+
+/**
+ * device_handler_remove - Remove a device from the handler device array.
+ */
+
+static void device_handler_remove(struct device_handler *handler,
+	struct device *device)
+{
+	unsigned int i;
+
+	for (i = 0; i < handler->n_devices; i++)
+		if (handler->devices[i] == device)
+			break;
+
+	if (i < handler->n_devices) {
+		assert(0 && "unknown device");
+		return;
+	}
+
+	handler->n_devices--;
+	memmove(&handler->devices[i], &handler->devices[i + 1],
+		(handler->n_devices - i) * sizeof(handler->devices[0]));
+	handler->devices = talloc_realloc(handler, handler->devices,
+		struct device *, handler->n_devices);
+}
+
+/**
+ * device_handler_find - Find a handler device by id.
+ */
+
+static struct device *device_handler_find(struct device_handler *handler,
+	const char *id)
+{
+	unsigned int i;
+
+	assert(id);
+
+	for (i = 0; i < handler->n_devices; i++)
+		if (handler->devices[i]->id
+			&& streq(handler->devices[i]->id, id))
+			return handler->devices[i];
+
+	assert(0 && "unknown device");
+	return NULL;
+}
+
+/**
+ * device_handler_get_device_count - Get the count of current handler devices.
+ */
+
+int device_handler_get_device_count(const struct device_handler *handler)
 {
-	*devices = handler->devices;
 	return handler->n_devices;
 }
 
+/**
+ * device_handler_get_device - Get a handler device by index.
+ */
+
+const struct device *device_handler_get_device(
+	const struct device_handler *handler, unsigned int index)
+{
+	if (index >= handler->n_devices) {
+		assert(0 && "bad index");
+		return NULL;
+	}
+
+	return handler->devices[index];
+}
+
 static int mkdir_recursive(const char *dir)
 {
 	struct stat statbuf;
@@ -317,6 +393,9 @@  static int handle_add_event(struct devic
 	/* run the parsers */
 	iterate_parsers(ctx);
 
+	/* add device to handler device array */
+	device_handler_add(handler, ctx->device);
+
 	discover_server_notify_add(handler->server, ctx->device);
 
 	return 0;
@@ -333,6 +412,9 @@  static int handle_remove_event(struct de
 
 	discover_server_notify_remove(handler->server, ctx->device);
 
+	/* remove device from handler device array */
+	device_handler_remove(handler, ctx->device);
+
 	talloc_free(ctx);
 
 	return 0;
--- a/discover/device-handler.h
+++ b/discover/device-handler.h
@@ -24,8 +24,9 @@  struct device_handler *device_handler_in
 
 void device_handler_destroy(struct device_handler *devices);
 
-int device_handler_get_current_devices(struct device_handler *handler,
-		const struct device **devices);
+int device_handler_get_device_count(const struct device_handler *handler);
+const struct device *device_handler_get_device(
+	const struct device_handler *handler, unsigned int index);
 
 int device_handler_event(struct device_handler *handler,
 		struct udev_event *event);
--- a/discover/discover-server.c
+++ b/discover/discover-server.c
@@ -128,7 +128,6 @@  static int write_remove_message(struct d
 static int discover_server_process(void *arg)
 {
 	struct discover_server *server = arg;
-	const struct device *devices;
 	struct client *client;
 	int fd, i, n_devices;
 
@@ -148,10 +147,13 @@  static int discover_server_process(void 
 	client->fd = fd;
 
 	/* send existing devices to client */
-	n_devices = device_handler_get_current_devices(server->device_handler,
-			&devices);
-	for (i = 0; i < n_devices; i++)
-		write_add_message(server, client, &devices[i]);
+	n_devices = device_handler_get_device_count(server->device_handler);
+	for (i = 0; i < n_devices; i++) {
+		const struct device *device;
+
+		device = device_handler_get_device(server->device_handler, i);
+		write_add_message(server, client, device);
+	}
 
 	return 0;
 }