discover: extend URL in UI to support auto-discovery
[petitboot] / discover / discover-server.c
index 41a4fde130a69567020f96bbfe477264a5041e1a..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,7 @@
 
 #include "device-handler.h"
 #include "discover-server.h"
+#include "platform.h"
 #include "sysinfo.h"
 
 struct discover_server {
@@ -190,11 +192,31 @@ static int write_system_info_message(struct discover_server *server,
        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);
@@ -224,6 +246,30 @@ static int discover_server_process_message(void *arg)
                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;
@@ -263,6 +309,11 @@ static int discover_server_process_connection(void *arg)
        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);
        for (i = 0; i < n_devices; i++) {
@@ -332,6 +383,15 @@ void discover_server_notify_system_info(struct discover_server *server,
                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)
 {