]> git.ozlabs.org Git - petitboot/blobdiff - discover/discover-server.c
discover: Add configuration methods
[petitboot] / discover / discover-server.c
index bafcb77630ec9379b48eb3c8bb2ddc80c0c50ef1..198793204ef0cd30a68bb7da80858fd3a7791cdd 100644 (file)
@@ -29,6 +29,7 @@ struct discover_server {
 };
 
 struct client {
+       struct discover_server *server;
        struct list_item list;
        int fd;
 };
@@ -90,7 +91,7 @@ static int client_write_message(
        return rc;
 }
 
-static int write_add_message(struct discover_server *server,
+static int write_device_add_message(struct discover_server *server,
                struct client *client, const struct device *dev)
 {
        struct pb_protocol_message *message;
@@ -99,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;
 
@@ -108,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;
@@ -117,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;
 
@@ -129,7 +148,9 @@ static int write_remove_message(struct discover_server *server,
 static int discover_server_process_message(void *arg)
 {
        struct pb_protocol_message *message;
+       struct boot_command *boot_command;
        struct client *client = arg;
+       int rc;
 
        message = pb_protocol_read_message(client, client->fd);
 
@@ -141,7 +162,16 @@ static int discover_server_process_message(void *arg)
                return 0;
        }
 
-       /* todo: process boot message */
+       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;
+       }
+
+       device_handler_boot(client->server->device_handler, boot_command);
+
        return 0;
 }
 
@@ -165,14 +195,21 @@ static int discover_server_process_connection(void *arg)
        talloc_set_destructor(client, client_destructor);
 
        client->fd = fd;
+       client->server = server;
 
        /* send existing devices to client */
        n_devices = device_handler_get_device_count(server->device_handler);
        for (i = 0; i < n_devices; i++) {
-               const struct device *device;
+               const struct discover_boot_option *opt;
+               const struct discover_device *device;
 
                device = device_handler_get_device(server->device_handler, i);
-               write_add_message(server, client, device);
+               write_device_add_message(server, client, device->device);
+
+               list_for_each_entry(&device->boot_options, opt, list)
+                       discover_server_notify_boot_option_add(server,
+                                       opt->option);
+
        }
 
        waiter_register(server->waitset, client->fd, WAIT_IN,
@@ -181,23 +218,32 @@ static int discover_server_process_connection(void *arg)
        return 0;
 }
 
-void discover_server_notify_add(struct discover_server *server,
+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_add_message(server, client, device);
+               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_remove(struct discover_server *server,
+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_remove_message(server, client, device->id);
+               write_device_remove_message(server, client, device->id);
 
 }