ui/ncurses: Parse configuration form & send to server
authorJeremy Kerr <jk@ozlabs.org>
Thu, 24 Oct 2013 06:02:49 +0000 (14:02 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 14 Nov 2013 02:23:52 +0000 (13:23 +1100)
This change implements the configuration save action, when the user
submits the nc-config form.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
ui/common/discover-client.c
ui/common/discover-client.h
ui/ncurses/nc-config.c
ui/ncurses/nc-cui.c
ui/ncurses/nc-cui.h

index b48d541050613af3b865c60fe4f684e0a2145852..0e7da9d2cca4a6a9535d9dd601601307f771e67f 100644 (file)
@@ -332,3 +332,21 @@ int discover_client_cancel_default(struct discover_client *client)
 
        return pb_protocol_write_message(client->fd, message);
 }
+
+int discover_client_send_config(struct discover_client *client,
+               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 pb_protocol_write_message(client->fd, message);
+}
index fa97fa52b782a2c95f37c2c27763d38a098e9fe3..68be8a52c3be81e5d8adf4a2177eb50d7602cb16 100644 (file)
@@ -75,4 +75,8 @@ int discover_client_boot(struct discover_client *client,
 /* Tell the discover server to cancel the default boot option, if any
  */
 int discover_client_cancel_default(struct discover_client *client);
+
+/* Send new configuration data to the discover server */
+int discover_client_send_config(struct discover_client *client,
+               struct config *config);
 #endif
index 0b2a8d7b06738f60185b70c1b94615b1a460ad81..0141ba070ab5ab29fbbc7d8fcb8f3aca92b9e009 100644 (file)
 
 #define _GNU_SOURCE
 
-
+#include <errno.h>
+#include <stdlib.h>
 #include <string.h>
 
+#include <pb-config/pb-config.h>
 #include <talloc/talloc.h>
 #include <types/types.h>
 #include <log/log.h>
@@ -123,11 +125,111 @@ struct nc_scr *config_screen_scr(struct config_screen *screen)
        return &screen->scr;
 }
 
+static int screen_process_form(struct config_screen *screen)
+{
+       const struct system_info *sysinfo = screen->cui->sysinfo;
+       struct config *config = talloc_zero(screen, struct config);
+       enum net_conf_type net_conf_type;
+       struct interface_config *iface;
+       char *str, *end;
+       int rc;
+
+       config_set_defaults(config);
+
+       config->autoboot_enabled =
+               widget_checkbox_get_value(screen->widgets.autoboot_f);
+
+
+       str = widget_textbox_get_value(screen->widgets.timeout_f);
+       if (str) {
+               unsigned long x;
+               errno = 0;
+               x = strtoul(str, &end, 10);
+               if (!errno && end != str)
+                       config->autoboot_timeout_sec = x;
+       }
+
+       net_conf_type = widget_select_get_value(screen->widgets.network_f);
+
+       /* if we don't have any network interfaces, prevent per-interface
+        * configuration */
+       if (sysinfo->n_interfaces == 0)
+               net_conf_type = NET_CONF_TYPE_DHCP_ALL;
+
+       if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
+               config->network.n_interfaces = 0;
+
+       } else {
+               int idx;
+
+               iface = talloc_zero(config, struct interface_config);
+               config->network.n_interfaces = 1;
+               config->network.interfaces = talloc_array(config,
+                               struct interface_config *, 1);
+               config->network.interfaces[0] = iface;
+
+               /* copy hwaddr (from the sysinfo interface data) to
+                * the configuration */
+               idx = widget_select_get_value(screen->widgets.iface_f);
+               memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
+                               sizeof(iface->hwaddr));
+       }
+
+       if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
+               iface->method = CONFIG_METHOD_DHCP;
+       }
+
+       if (net_conf_type == NET_CONF_TYPE_STATIC) {
+               iface->method = CONFIG_METHOD_STATIC;
+               iface->static_config.address = talloc_asprintf(iface, "%s/%s",
+                               widget_textbox_get_value(
+                                       screen->widgets.ip_addr_f),
+                               widget_textbox_get_value(
+                                       screen->widgets.ip_mask_f));
+               iface->static_config.gateway = talloc_strdup(iface,
+                               widget_textbox_get_value(
+                                       screen->widgets.gateway_f));
+       }
+
+       str = widget_textbox_get_value(screen->widgets.dns_f);
+       if (str && strlen(str)) {
+               char *dns, *tmp;
+               int i;
+
+               for (;;) {
+                       dns = strtok_r(str, " \t", &tmp);
+
+                       if (!dns)
+                               break;
+
+                       i = config->network.n_dns_servers++;
+                       config->network.dns_servers = talloc_realloc(config,
+                                       config->network.dns_servers,
+                                       const char *,
+                                       config->network.n_dns_servers);
+                       config->network.dns_servers[i] =
+                               talloc_strdup(config, dns);
+
+                       str = NULL;
+               }
+       }
+
+       rc = cui_send_config(screen->cui, config);
+       talloc_free(config);
+
+       if (rc)
+               pb_log("cui_send_config failed!\n");
+       else
+               pb_debug("config sent!\n");
+
+       return 0;
+}
+
 static void ok_click(void *arg)
 {
        struct config_screen *screen = arg;
-       /* todo: save config */
-       screen->on_exit(screen->cui);
+       screen_process_form(screen);
+       screen->exit = true;
 }
 
 static void cancel_click(void *arg)
@@ -212,13 +314,8 @@ static void config_screen_layout_widgets(struct config_screen *screen,
        if (show)
                y += layout_pair(screen, y, screen->widgets.gateway_l, wf);
 
-       wl = widget_label_base(screen->widgets.dns_l);
-       wf = widget_textbox_base(screen->widgets.dns_f);
-       widget_set_visible(wl, show);
-       widget_set_visible(wf, show);
-
-       if (show)
-               y += 1 + layout_pair(screen, y, screen->widgets.dns_l, wf);
+       y += 1 + layout_pair(screen, y, screen->widgets.dns_l,
+                       widget_textbox_base(screen->widgets.dns_f));
 
        widget_move(widget_button_base(screen->widgets.ok_b),
                        y, screen->field_x);
index d74c1a46ece00804e4049835a907c0ad2e20e4e2..93377bb56f94584d88e732999fedaed908b86f5d 100644 (file)
@@ -551,6 +551,11 @@ static void cui_update_config(struct config *config, void *arg)
        cui->config = talloc_steal(cui, config);
 }
 
+int cui_send_config(struct cui *cui, struct config *config)
+{
+       return discover_client_send_config(cui->client, config);
+}
+
 static struct discover_client_ops cui_client_ops = {
        .device_add = NULL,
        .boot_option_add = cui_boot_option_add,
index 752f2db38021a4ec67f751ec89d33190a1fbb745..aba682a3c43d90633831a7c24ffa3ce6d4c44cc3 100644 (file)
@@ -74,6 +74,7 @@ void cui_item_edit(struct pmenu_item *item);
 void cui_item_new(struct pmenu *menu);
 void cui_show_sysinfo(struct cui *cui);
 void cui_show_config(struct cui *cui);
+int cui_send_config(struct cui *cui, struct config *config);
 
 /* convenience routines */