]> git.ozlabs.org Git - petitboot/blobdiff - discover/discover-server.c
protocol: Separate device add from boot-option add messages
[petitboot] / discover / discover-server.c
index 8358f06a320a9e716e10318740326de52f6fdd94..9f6e7daaa6793b40f8389ef69ee22edb35498d5d 100644 (file)
 #include <asm/byteorder.h>
 
 #include <talloc/talloc.h>
+#include <waiter/waiter.h>
+#include <log/log.h>
 
-#include "ui/common/device.h"
 #include "pb-protocol/pb-protocol.h"
 #include "list/list.h"
 
-#include "log.h"
-#include "waiter.h"
+#include "device-handler.h"
+#include "discover-server.h"
 
 struct discover_server {
        int socket;
+       struct waitset *waitset;
        struct waiter *waiter;
        struct list clients;
+       struct device_handler *device_handler;
 };
 
 struct client {
+       struct discover_server *server;
        struct list_item list;
        int fd;
 };
@@ -36,7 +40,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);
@@ -64,36 +68,18 @@ static void print_clients(struct discover_server *server)
 {
        struct client *client;
 
-       printf("current clients [%p,%p,%p]:\n",
+       pb_log("current clients [%p,%p,%p]:\n",
                        &server->clients.head,
                        server->clients.head.prev,
                        server->clients.head.next);
        list_for_each_entry(&server->clients, client, list)
-               printf("\t[%p,%p,%p] client: %d\n", &client->list,
+               pb_log("\t[%p,%p,%p] client: %d\n", &client->list,
                                client->list.prev, client->list.next,
                                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,
+static int client_write_message(
+               struct discover_server *server __attribute__((unused)),
                struct client *client, struct pb_protocol_message *message)
 {
        int rc;
@@ -105,8 +91,8 @@ static int client_write_message(struct discover_server *server,
        return rc;
 }
 
-static int write_add_message(struct discover_server *server,
-               struct client *client, struct device *dev)
+static int write_device_add_message(struct discover_server *server,
+               struct client *client, const struct device *dev)
 {
        struct pb_protocol_message *message;
        int len;
@@ -114,7 +100,7 @@ static int write_add_message(struct discover_server *server,
        len = pb_protocol_device_len(dev);
 
        message = pb_protocol_create_message(client,
-                       PB_PROTOCOL_ACTION_ADD, len);
+                       PB_PROTOCOL_ACTION_DEVICE_ADD, len);
        if (!message)
                return -1;
 
@@ -123,7 +109,25 @@ static int write_add_message(struct discover_server *server,
        return client_write_message(server, client, message);
 }
 
-static int write_remove_message(struct discover_server *server,
+static int write_boot_option_add_message(struct discover_server *server,
+               struct client *client, const struct boot_option *opt)
+{
+       struct pb_protocol_message *message;
+       int len;
+
+       len = pb_protocol_boot_option_len(opt);
+
+       message = pb_protocol_create_message(client,
+                       PB_PROTOCOL_ACTION_BOOT_OPTION_ADD, len);
+       if (!message)
+               return -1;
+
+       pb_protocol_serialise_boot_option(opt, message->payload, len);
+
+       return client_write_message(server, client, message);
+}
+
+static int write_device_remove_message(struct discover_server *server,
                struct client *client, char *dev_id)
 {
        struct pb_protocol_message *message;
@@ -132,7 +136,7 @@ static int write_remove_message(struct discover_server *server,
        len = strlen(dev_id) + sizeof(uint32_t);
 
        message = pb_protocol_create_message(client,
-                       PB_PROTOCOL_ACTION_REMOVE, len);
+                       PB_PROTOCOL_ACTION_DEVICE_REMOVE, len);
        if (!message)
                return -1;
 
@@ -141,14 +145,41 @@ static int write_remove_message(struct discover_server *server,
        return client_write_message(server, client, message);
 }
 
-static int discover_server_process(void *arg)
+static int discover_server_process_message(void *arg)
 {
-       struct discover_server *server = arg;
-       struct client *client;
-       int fd;
+       struct pb_protocol_message *message;
+       struct boot_command *boot_command;
+       struct client *client = arg;
+       int rc;
 
+       message = pb_protocol_read_message(client, client->fd);
+
+       if (!message)
+               return 0;
+
+       if (message->action != PB_PROTOCOL_ACTION_BOOT) {
+               pb_log("%s: invalid action %d\n", __func__, message->action);
+               return 0;
+       }
+
+       boot_command = talloc(client, struct boot_command);
+
+       rc = pb_protocol_deserialise_boot_command(boot_command, message);
+       if (rc) {
+               pb_log("%s: no boot command?", __func__);
+               return 0;
+       }
 
-       len = sizeof(addr);
+       device_handler_boot(client->server->device_handler, boot_command);
+
+       return 0;
+}
+
+static int discover_server_process_connection(void *arg)
+{
+       struct discover_server *server = arg;
+       struct client *client;
+       int fd, i, n_devices;
 
        /* accept the incoming connection */
        fd = accept(server->socket, NULL, 0);
@@ -164,18 +195,64 @@ static int discover_server_process(void *arg)
        talloc_set_destructor(client, client_destructor);
 
        client->fd = fd;
+       client->server = server;
 
        /* send existing devices to client */
-       write_add_message(server, client, &device);
+       n_devices = device_handler_get_device_count(server->device_handler);
+       for (i = 0; i < n_devices; i++) {
+               const struct device *device;
+               struct boot_option *opt;
 
-       sleep(2);
+               device = device_handler_get_device(server->device_handler, i);
+               write_device_add_message(server, client, device);
 
-       write_remove_message(server, client, "1");
+               list_for_each_entry(&device->boot_options, opt, list)
+                       discover_server_notify_boot_option_add(server, opt);
+
+       }
+
+       waiter_register(server->waitset, client->fd, WAIT_IN,
+                       discover_server_process_message, client);
 
        return 0;
 }
 
-struct discover_server *discover_server_init(void)
+void discover_server_notify_device_add(struct discover_server *server,
+               struct device *device)
+{
+       struct client *client;
+
+       list_for_each_entry(&server->clients, client, list)
+               write_device_add_message(server, client, device);
+
+}
+
+void discover_server_notify_boot_option_add(struct discover_server *server,
+               struct boot_option *boot_option)
+{
+       struct client *client;
+
+       list_for_each_entry(&server->clients, client, list)
+               write_boot_option_add_message(server, client, boot_option);
+}
+
+void discover_server_notify_device_remove(struct discover_server *server,
+               struct device *device)
+{
+       struct client *client;
+
+       list_for_each_entry(&server->clients, client, list)
+               write_device_remove_message(server, client, device->id);
+
+}
+
+void discover_server_set_device_source(struct discover_server *server,
+               struct device_handler *handler)
+{
+       server->device_handler = handler;
+}
+
+struct discover_server *discover_server_init(struct waitset *waitset)
 {
        struct discover_server *server;
        struct sockaddr_un addr;
@@ -185,6 +262,7 @@ struct discover_server *discover_server_init(void)
                return NULL;
 
        server->waiter = NULL;
+       server->waitset = waitset;
        list_init(&server->clients);
 
        unlink(PB_SOCKET_PATH);
@@ -210,8 +288,8 @@ struct discover_server *discover_server_init(void)
                goto out_err;
        }
 
-       server->waiter = waiter_register(server->socket, WAIT_IN,
-                       discover_server_process, server);
+       server->waiter = waiter_register(server->waitset, server->socket,
+                       WAIT_IN, discover_server_process_connection, server);
 
        return server;