diff options
-rw-r--r-- | discover/device-handler.c | 62 | ||||
-rw-r--r-- | discover/device-handler.h | 19 | ||||
-rw-r--r-- | discover/discover-server.c | 44 | ||||
-rw-r--r-- | discover/discover-server.h | 4 | ||||
-rw-r--r-- | discover/pb-discover.c | 17 | ||||
-rw-r--r-- | rules.mk | 1 |
6 files changed, 113 insertions, 34 deletions
diff --git a/discover/device-handler.c b/discover/device-handler.c new file mode 100644 index 0000000..0e902df --- /dev/null +++ b/discover/device-handler.c @@ -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 index 0000000..ddcb201 --- /dev/null +++ b/discover/device-handler.h @@ -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 */ diff --git a/discover/discover-server.c b/discover/discover-server.c index 8358f06..c35d27d 100644 --- a/discover/discover-server.c +++ b/discover/discover-server.c @@ -18,11 +18,13 @@ #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; diff --git a/discover/discover-server.h b/discover/discover-server.h index ff27f15..e5d7881 100644 --- a/discover/discover-server.h +++ b/discover/discover-server.h @@ -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 */ diff --git a/discover/pb-discover.c b/discover/pb-discover.c index 45b6ba1..63aaf00 100644 --- a/discover/pb-discover.c +++ b/discover/pb-discover.c @@ -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; @@ -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) |