Create device-handler for managing registered devices
authorJeremy Kerr <jk@ozlabs.org>
Mon, 15 Dec 2008 06:43:04 +0000 (17:43 +1100)
committerJeremy Kerr <jk@ozlabs.org>
Mon, 15 Dec 2008 06:43:04 +0000 (17:43 +1100)
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/device-handler.c [new file with mode: 0644]
discover/device-handler.h [new file with mode: 0644]
discover/discover-server.c
discover/discover-server.h
discover/pb-discover.c
rules.mk

diff --git a/discover/device-handler.c b/discover/device-handler.c
new file mode 100644 (file)
index 0000000..0e902df
--- /dev/null
@@ -0,0 +1,62 @@
+
+#include <talloc/talloc.h>
+#include <pb-protocol/pb-protocol.h>
+
+#include "device-handler.h"
+
+struct device_handler {
+       struct discover_server *server;
+
+       struct device *devices;
+       int n_devices;
+};
+
+struct device_handler *device_handler_init(struct discover_server *server)
+{
+       struct device_handler *handler;
+
+       handler = talloc(NULL, struct device_handler);
+       handler->devices = NULL;
+       handler->n_devices = 0;
+
+       return handler;
+}
+
+void device_handler_destroy(struct device_handler *devices)
+{
+       talloc_free(devices);
+}
+
+static struct boot_option options[] = {
+       {
+               .id = "1.1",
+               .name = "meep one",
+               .description = "meep description one",
+               .icon_file = "meep.one.png",
+               .boot_args = "root=/dev/sda1",
+       },
+};
+
+static struct device device = {
+       .id = "1",
+       .name = "meep",
+       .description = "meep description",
+       .icon_file = "meep.png",
+       .n_options = 1,
+       .options = options,
+};
+
+int device_handler_get_current_devices(struct device_handler *handler,
+               struct device **devices)
+
+{
+       *devices = &device;
+       return 1;
+}
+
+
+int device_handler_event(struct device_handler *handler,
+               struct udev_event *event)
+{
+       return 0;
+}
diff --git a/discover/device-handler.h b/discover/device-handler.h
new file mode 100644 (file)
index 0000000..ddcb201
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef _DEVICE_HANDLER_H
+#define _DEVICE_HANDLER_H
+
+struct device_handler;
+struct discover_server;
+struct udev_event;
+struct device;
+
+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,
+               struct device **devices);
+
+int device_handler_event(struct device_handler *handler,
+               struct udev_event *event);
+
+#endif /* _DEVICE_HANDLER_H */
index 8358f06a320a9e716e10318740326de52f6fdd94..c35d27dddde4a64cfa8040eacc58481253e82417 100644 (file)
 
 #include "log.h"
 #include "waiter.h"
+#include "device-handler.h"
 
 struct discover_server {
        int socket;
        struct waiter *waiter;
        struct list clients;
+       struct device_handler *device_handler;
 };
 
 struct client {
@@ -36,7 +38,7 @@ static int server_destructor(void *arg)
        struct discover_server *server = arg;
 
        if (server->waiter)
-               waiter_unregister(server->waiter);
+               waiter_remove(server->waiter);
 
        if (server->socket >= 0)
                close(server->socket);
@@ -74,25 +76,6 @@ static void print_clients(struct discover_server *server)
                                client->fd);
 }
 
-static struct boot_option options[] = {
-       {
-               .id = "1.1",
-               .name = "meep one",
-               .description = "meep description one",
-               .icon_file = "meep.one.png",
-               .boot_args = "root=/dev/sda1",
-       },
-};
-
-static struct device device = {
-       .id = "1",
-       .name = "meep",
-       .description = "meep description",
-       .icon_file = "meep.png",
-       .n_options = 1,
-       .options = options,
-};
-
 static int client_write_message(struct discover_server *server,
                struct client *client, struct pb_protocol_message *message)
 {
@@ -144,11 +127,9 @@ static int write_remove_message(struct discover_server *server,
 static int discover_server_process(void *arg)
 {
        struct discover_server *server = arg;
+       struct device *devices;
        struct client *client;
-       int fd;
-
-
-       len = sizeof(addr);
+       int fd, i, n_devices;
 
        /* accept the incoming connection */
        fd = accept(server->socket, NULL, 0);
@@ -166,15 +147,20 @@ static int discover_server_process(void *arg)
        client->fd = fd;
 
        /* send existing devices to client */
-       write_add_message(server, client, &device);
-
-       sleep(2);
-
-       write_remove_message(server, client, "1");
+       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]);
 
        return 0;
 }
 
+void discover_server_set_device_source(struct discover_server *server,
+               struct device_handler *handler)
+{
+       server->device_handler = handler;
+}
+
 struct discover_server *discover_server_init(void)
 {
        struct discover_server *server;
index ff27f157c108530b9868f45a50148b15d0b10fdd..e5d7881905ffbcf9cd805242355ed67e9a697021 100644 (file)
@@ -2,9 +2,13 @@
 #define _DISCOVER_SERVER_H
 
 struct discover_server;
+struct device_handler;
 
 struct discover_server *discover_server_init(void);
 
 void discover_server_destroy(struct discover_server *server);
 
+void discover_server_set_device_source(struct discover_server *server,
+               struct device_handler *handler);
+
 #endif /* _DISCOVER_SERVER_H */
index 45b6ba1c7207c62a870a73fe3d22c317bf418326..63aaf0089543965a228849dcdccadceaa16c02fe 100644 (file)
@@ -4,26 +4,33 @@
 
 #include "udev.h"
 #include "discover-server.h"
+#include "device-handler.h"
 #include "waiter.h"
 #include "log.h"
 
-
 int main(void)
 {
+       struct device_handler *handler;
        struct discover_server *server;
        struct udev *udev;
 
        /* we look for closed sockets when we write, so ignore SIGPIPE */
        signal(SIGPIPE, SIG_IGN);
 
-       udev = udev_init();
-       if (!udev)
-               return EXIT_FAILURE;
-
        server = discover_server_init();
        if (!server)
                return EXIT_FAILURE;
 
+       handler = device_handler_init(server);
+       if (!handler)
+               return EXIT_FAILURE;
+
+       discover_server_set_device_source(server, handler);
+
+       udev = udev_init();
+       if (!udev)
+               return EXIT_FAILURE;
+
        for (;;) {
                if (waiter_poll())
                        return EXIT_FAILURE;
index f8d64c61752b3d0c505740c33dea4e8d5850a50f..00241c30279a8645d52ea7055c4a00e8b8d909d4 100644 (file)
--- a/rules.mk
+++ b/rules.mk
@@ -43,6 +43,7 @@ ui/test/pb-test: $(pb_test_objs)
 
 pb_discover_objs = discover/pb-discover.o discover/udev.o discover/log.o \
                   discover/waiter.o discover/discover-server.o \
+                  discover/device-handler.o \
                   $(talloc_objs) $(server_objs) $(list_objs)
 
 discover/pb-discover: $(pb_discover_objs)