discover: extend URL in UI to support auto-discovery
[petitboot] / discover / discover-server.c
index 198793204ef0cd30a68bb7da80858fd3a7791cdd..68065895f4dc52b85ccb04633f87e7201398c950 100644 (file)
@@ -10,6 +10,7 @@
 #include <sys/un.h>
 #include <asm/byteorder.h>
 
+#include <pb-config/pb-config.h>
 #include <talloc/talloc.h>
 #include <waiter/waiter.h>
 #include <log/log.h>
@@ -19,6 +20,8 @@
 
 #include "device-handler.h"
 #include "discover-server.h"
+#include "platform.h"
+#include "sysinfo.h"
 
 struct discover_server {
        int socket;
@@ -31,7 +34,9 @@ struct discover_server {
 struct client {
        struct discover_server *server;
        struct list_item list;
+       struct waiter *waiter;
        int fd;
+       bool remote_closed;
 };
 
 
@@ -55,6 +60,9 @@ static int client_destructor(void *arg)
        if (client->fd >= 0)
                close(client->fd);
 
+       if (client->waiter)
+               waiter_remove(client->waiter);
+
        list_remove(&client->list);
 
        return 0;
@@ -68,12 +76,12 @@ static void print_clients(struct discover_server *server)
 {
        struct client *client;
 
-       pb_log("current clients [%p,%p,%p]:\n",
+       pb_debug("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)
-               pb_log("\t[%p,%p,%p] client: %d\n", &client->list,
+               pb_debug("\t[%p,%p,%p] client: %d\n", &client->list,
                                client->list.prev, client->list.next,
                                client->fd);
 }
@@ -84,9 +92,12 @@ static int client_write_message(
 {
        int rc;
 
+       if (client->remote_closed)
+               return -1;
+
        rc = pb_protocol_write_message(client->fd, message);
        if (rc)
-               talloc_free(client);
+               client->remote_closed = true;
 
        return rc;
 }
@@ -145,32 +156,125 @@ static int write_device_remove_message(struct discover_server *server,
        return client_write_message(server, client, message);
 }
 
+static int write_boot_status_message(struct discover_server *server,
+               struct client *client, const struct boot_status *status)
+{
+       struct pb_protocol_message *message;
+       int len;
+
+       len = pb_protocol_boot_status_len(status);
+
+       message = pb_protocol_create_message(client,
+                       PB_PROTOCOL_ACTION_STATUS, len);
+       if (!message)
+               return -1;
+
+       pb_protocol_serialise_boot_status(status, message->payload, len);
+
+       return client_write_message(server, client, message);
+}
+
+static int write_system_info_message(struct discover_server *server,
+               struct client *client, const struct system_info *sysinfo)
+{
+       struct pb_protocol_message *message;
+       int len;
+
+       len = pb_protocol_system_info_len(sysinfo);
+
+       message = pb_protocol_create_message(client,
+                       PB_PROTOCOL_ACTION_SYSTEM_INFO, len);
+       if (!message)
+               return -1;
+
+       pb_protocol_serialise_system_info(sysinfo, message->payload, len);
+
+       return client_write_message(server, client, message);
+}
+
+static int write_config_message(struct discover_server *server,
+               struct client *client, const struct config *config)
+{
+       struct pb_protocol_message *message;
+       int len;
+
+       len = pb_protocol_config_len(config);
+
+       message = pb_protocol_create_message(client,
+                       PB_PROTOCOL_ACTION_CONFIG, len);
+       if (!message)
+               return -1;
+
+       pb_protocol_serialise_config(config, message->payload, len);
+
+       return client_write_message(server, client, message);
+}
+
 static int discover_server_process_message(void *arg)
 {
        struct pb_protocol_message *message;
        struct boot_command *boot_command;
        struct client *client = arg;
+       struct config *config;
+       char *url;
        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);
+       if (!message) {
+               talloc_free(client);
                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__);
+       switch (message->action) {
+       case PB_PROTOCOL_ACTION_BOOT:
+               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);
+               break;
+
+       case PB_PROTOCOL_ACTION_CANCEL_DEFAULT:
+               device_handler_cancel_default(client->server->device_handler);
+               break;
+
+       case PB_PROTOCOL_ACTION_REINIT:
+               device_handler_reinit(client->server->device_handler);
+               break;
+
+       case PB_PROTOCOL_ACTION_CONFIG:
+               config = talloc_zero(client, struct config);
+
+               rc = pb_protocol_deserialise_config(config, message);
+               if (rc) {
+                       pb_log("%s: no config?", __func__);
+                       return 0;
+               }
+
+               device_handler_update_config(client->server->device_handler,
+                               config);
+               break;
+
+       case PB_PROTOCOL_ACTION_ADD_URL:
+               url = pb_protocol_deserialise_string((void *) client, message);
+
+               device_handler_process_url(client->server->device_handler,
+                               url, NULL, NULL);
+               break;
+
+       default:
+               pb_log("%s: invalid action %d\n", __func__, message->action);
                return 0;
        }
 
-       device_handler_boot(client->server->device_handler, boot_command);
 
        return 0;
 }
@@ -178,24 +282,37 @@ static int discover_server_process_message(void *arg)
 static int discover_server_process_connection(void *arg)
 {
        struct discover_server *server = arg;
+       int fd, rc, i, n_devices;
        struct client *client;
-       int fd, i, n_devices;
 
        /* accept the incoming connection */
        fd = accept(server->socket, NULL, 0);
-       if (!fd) {
+       if (fd < 0) {
                pb_log("accept: %s\n", strerror(errno));
                return 0;
        }
 
        /* add to our list of clients */
-       client = talloc(server, struct client);
+       client = talloc_zero(server, struct client);
        list_add(&server->clients, &client->list);
 
        talloc_set_destructor(client, client_destructor);
 
        client->fd = fd;
        client->server = server;
+       client->waiter = waiter_register_io(server->waitset, client->fd,
+                               WAIT_IN, discover_server_process_message,
+                               client);
+
+       /* send sysinfo to client */
+       rc = write_system_info_message(server, client, system_info_get());
+       if (rc)
+               return 0;
+
+       /* send config to client */
+       rc = write_config_message(server, client, config_get());
+       if (rc)
+               return 0;
 
        /* send existing devices to client */
        n_devices = device_handler_get_device_count(server->device_handler);
@@ -204,17 +321,18 @@ static int discover_server_process_connection(void *arg)
                const struct discover_device *device;
 
                device = device_handler_get_device(server->device_handler, i);
-               write_device_add_message(server, client, device->device);
+               rc = write_device_add_message(server, client, device->device);
+               if (rc)
+                       return 0;
 
-               list_for_each_entry(&device->boot_options, opt, list)
-                       discover_server_notify_boot_option_add(server,
+               list_for_each_entry(&device->boot_options, opt, list) {
+                       rc = write_boot_option_add_message(server, client,
                                        opt->option);
-
+                       if (rc)
+                               return 0;
+               }
        }
 
-       waiter_register(server->waitset, client->fd, WAIT_IN,
-                       discover_server_process_message, client);
-
        return 0;
 }
 
@@ -247,6 +365,33 @@ void discover_server_notify_device_remove(struct discover_server *server,
 
 }
 
+void discover_server_notify_boot_status(struct discover_server *server,
+               struct boot_status *status)
+{
+       struct client *client;
+
+       list_for_each_entry(&server->clients, client, list)
+               write_boot_status_message(server, client, status);
+}
+
+void discover_server_notify_system_info(struct discover_server *server,
+               const struct system_info *sysinfo)
+{
+       struct client *client;
+
+       list_for_each_entry(&server->clients, client, list)
+               write_system_info_message(server, client, sysinfo);
+}
+
+void discover_server_notify_config(struct discover_server *server,
+               const struct config *config)
+{
+       struct client *client;
+
+       list_for_each_entry(&server->clients, client, list)
+               write_config_message(server, client, config);
+}
+
 void discover_server_set_device_source(struct discover_server *server,
                struct device_handler *handler)
 {
@@ -289,7 +434,7 @@ struct discover_server *discover_server_init(struct waitset *waitset)
                goto out_err;
        }
 
-       server->waiter = waiter_register(server->waitset, server->socket,
+       server->waiter = waiter_register_io(server->waitset, server->socket,
                        WAIT_IN, discover_server_process_connection, server);
 
        return server;