Add discover device report
authorGeoff Levand <geoffrey.levand@am.sony.com>
Sun, 12 Apr 2009 15:11:46 +0000 (15:11 +0000)
committerJeremy Kerr <jk@ozlabs.org>
Tue, 30 Jun 2009 07:29:13 +0000 (15:29 +0800)
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>
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/device-handler.c
discover/device-handler.h
discover/discover-server.c

index 29c237131a4a267c25c11ce28e3f55d1b7c3ec95..0f8dc58441701c32312e040c29417d0bf4a0be9f 100644 (file)
@@ -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 device_handler *handler,
        /* 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 device_handler *handler,
 
        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;
index 01179f289667af56147ceee468ed6933a429a11f..8a469e0fe4c5361a46b8c91127514887240afae5 100644 (file)
@@ -24,8 +24,9 @@ struct device_handler *device_handler_init(struct discover_server *server);
 
 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);
index 47ab4945e2df3071b2f35899cdff48b9badd0f50..f4e96c046927bd1e0709d7ea78a621fd3da4f26c 100644 (file)
@@ -128,7 +128,6 @@ static int write_remove_message(struct discover_server *server,
 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 *arg)
        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;
 }