2 * Copyright (C) 2013 IBM Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <pb-config/pb-config.h>
25 #include <talloc/talloc.h>
26 #include <types/types.h>
31 #include "nc-config.h"
32 #include "nc-widgets.h"
37 NET_CONF_TYPE_DHCP_ALL,
38 NET_CONF_TYPE_DHCP_ONE,
42 struct config_screen {
45 struct nc_widgetset *widgetset;
47 void (*on_exit)(struct cui *);
53 enum net_conf_type net_conf_type;
56 struct nc_widget_checkbox *autoboot_f;
57 struct nc_widget_label *autoboot_l;
58 struct nc_widget_textbox *timeout_f;
59 struct nc_widget_label *timeout_l;
61 struct nc_widget_label *network_l;
62 struct nc_widget_select *network_f;
64 struct nc_widget_label *iface_l;
65 struct nc_widget_select *iface_f;
66 struct nc_widget_label *ip_addr_l;
67 struct nc_widget_textbox *ip_addr_f;
68 struct nc_widget_label *ip_mask_l;
69 struct nc_widget_textbox *ip_mask_f;
70 struct nc_widget_label *gateway_l;
71 struct nc_widget_textbox *gateway_f;
72 struct nc_widget_label *dns_l;
73 struct nc_widget_textbox *dns_f;
75 struct nc_widget_button *ok_b;
76 struct nc_widget_button *cancel_b;
80 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
82 struct config_screen *config_screen;
84 assert(scr->sig == pb_config_screen_sig);
85 config_screen = (struct config_screen *)
86 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
87 assert(config_screen->scr.sig == pb_config_screen_sig);
91 static void config_screen_process_key(struct nc_scr *scr, int key)
93 struct config_screen *screen = config_screen_from_scr(scr);
96 handled = widgetset_process_key(screen->widgetset, key);
98 screen->on_exit(screen->cui);
100 wrefresh(screen->scr.main_ncw);
103 static void config_screen_resize(struct nc_scr *scr)
105 struct config_screen *screen = config_screen_from_scr(scr);
109 static int config_screen_post(struct nc_scr *scr)
111 struct config_screen *screen = config_screen_from_scr(scr);
112 widgetset_post(screen->widgetset);
113 nc_scr_frame_draw(scr);
114 wrefresh(scr->main_ncw);
118 static int config_screen_unpost(struct nc_scr *scr)
120 struct config_screen *screen = config_screen_from_scr(scr);
121 widgetset_unpost(screen->widgetset);
125 struct nc_scr *config_screen_scr(struct config_screen *screen)
130 static int screen_process_form(struct config_screen *screen)
132 const struct system_info *sysinfo = screen->cui->sysinfo;
133 struct config *config = talloc_zero(screen, struct config);
134 enum net_conf_type net_conf_type;
135 struct interface_config *iface;
139 config_set_defaults(config);
141 config->autoboot_enabled =
142 widget_checkbox_get_value(screen->widgets.autoboot_f);
145 str = widget_textbox_get_value(screen->widgets.timeout_f);
149 x = strtoul(str, &end, 10);
150 if (!errno && end != str)
151 config->autoboot_timeout_sec = x;
154 net_conf_type = widget_select_get_value(screen->widgets.network_f);
156 /* if we don't have any network interfaces, prevent per-interface
158 if (sysinfo->n_interfaces == 0)
159 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
161 if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
162 config->network.n_interfaces = 0;
167 iface = talloc_zero(config, struct interface_config);
168 config->network.n_interfaces = 1;
169 config->network.interfaces = talloc_array(config,
170 struct interface_config *, 1);
171 config->network.interfaces[0] = iface;
173 /* copy hwaddr (from the sysinfo interface data) to
174 * the configuration */
175 idx = widget_select_get_value(screen->widgets.iface_f);
176 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
177 sizeof(iface->hwaddr));
180 if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
181 iface->method = CONFIG_METHOD_DHCP;
184 if (net_conf_type == NET_CONF_TYPE_STATIC) {
185 iface->method = CONFIG_METHOD_STATIC;
186 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
187 widget_textbox_get_value(
188 screen->widgets.ip_addr_f),
189 widget_textbox_get_value(
190 screen->widgets.ip_mask_f));
191 iface->static_config.gateway = talloc_strdup(iface,
192 widget_textbox_get_value(
193 screen->widgets.gateway_f));
196 str = widget_textbox_get_value(screen->widgets.dns_f);
197 if (str && strlen(str)) {
202 dns = strtok_r(str, " \t", &tmp);
207 i = config->network.n_dns_servers++;
208 config->network.dns_servers = talloc_realloc(config,
209 config->network.dns_servers,
211 config->network.n_dns_servers);
212 config->network.dns_servers[i] =
213 talloc_strdup(config, dns);
219 rc = cui_send_config(screen->cui, config);
223 pb_log("cui_send_config failed!\n");
225 pb_debug("config sent!\n");
230 static void ok_click(void *arg)
232 struct config_screen *screen = arg;
233 screen_process_form(screen);
237 static void cancel_click(void *arg)
239 struct config_screen *screen = arg;
243 static int layout_pair(struct config_screen *screen, int y,
244 struct nc_widget_label *label,
245 struct nc_widget *field)
247 struct nc_widget *label_w = widget_label_base(label);
248 widget_move(label_w, y, screen->label_x);
249 widget_move(field, y, screen->field_x);
250 return max(widget_height(label_w), widget_height(field));
253 static void config_screen_layout_widgets(struct config_screen *screen,
254 enum net_conf_type net_conf)
256 struct nc_widget *wl, *wf;
262 y += layout_pair(screen, y, screen->widgets.autoboot_l,
263 widget_checkbox_base(screen->widgets.autoboot_f));
265 y += layout_pair(screen, y, screen->widgets.timeout_l,
266 widget_textbox_base(screen->widgets.timeout_f));
270 y += layout_pair(screen, y, screen->widgets.network_l,
271 widget_select_base(screen->widgets.network_f));
275 /* conditionally show iface select */
276 wl = widget_label_base(screen->widgets.iface_l);
277 wf = widget_select_base(screen->widgets.iface_f);
279 show = net_conf == NET_CONF_TYPE_DHCP_ONE ||
280 net_conf == NET_CONF_TYPE_STATIC;
282 widget_set_visible(wl, show);
283 widget_set_visible(wf, show);
286 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
288 /* conditionally show static IP params */
289 show = net_conf == NET_CONF_TYPE_STATIC;
291 wl = widget_label_base(screen->widgets.ip_addr_l);
292 wf = widget_textbox_base(screen->widgets.ip_addr_f);
293 widget_set_visible(wl, show);
294 widget_set_visible(wf, show);
295 x = screen->field_x + widget_width(wf) + 1;
298 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
300 wl = widget_label_base(screen->widgets.ip_mask_l);
301 wf = widget_textbox_base(screen->widgets.ip_mask_f);
302 widget_set_visible(wl, show);
303 widget_set_visible(wf, show);
306 widget_move(wl, y, x);
307 widget_move(wf, y, x + 2);
311 wl = widget_label_base(screen->widgets.gateway_l);
312 wf = widget_textbox_base(screen->widgets.gateway_f);
313 widget_set_visible(wl, show);
314 widget_set_visible(wf, show);
317 y += layout_pair(screen, y, screen->widgets.gateway_l, wf) + 1;
319 y += layout_pair(screen, y, screen->widgets.dns_l,
320 widget_textbox_base(screen->widgets.dns_f));
324 widget_move(widget_button_base(screen->widgets.ok_b),
326 widget_move(widget_button_base(screen->widgets.cancel_b),
327 y, screen->field_x + 10);
330 static void config_screen_network_change(void *arg, int value)
332 struct config_screen *screen = arg;
333 screen->net_conf_type = value;
334 widgetset_unpost(screen->widgetset);
335 config_screen_layout_widgets(screen, value);
336 widgetset_post(screen->widgetset);
339 static struct interface_config *first_active_interface(
340 const struct config *config)
344 for (i = 0; i < config->network.n_interfaces; i++) {
345 if (config->network.interfaces[i]->ignore)
347 return config->network.interfaces[i];
352 static enum net_conf_type find_net_conf_type(const struct config *config)
354 struct interface_config *ifcfg;
356 ifcfg = first_active_interface(config);
359 return NET_CONF_TYPE_DHCP_ALL;
361 else if (ifcfg->method == CONFIG_METHOD_DHCP)
362 return NET_CONF_TYPE_DHCP_ONE;
364 else if (ifcfg->method == CONFIG_METHOD_STATIC)
365 return NET_CONF_TYPE_STATIC;
368 return NET_CONF_TYPE_DHCP_ALL;
371 static void config_screen_setup_widgets(struct config_screen *screen,
372 const struct config *config,
373 const struct system_info *sysinfo)
375 struct nc_widgetset *set = screen->widgetset;
376 struct interface_config *ifcfg;
377 char *str, *ip, *mask, *gw;
378 enum net_conf_type type;
381 build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
384 type = screen->net_conf_type;
385 ifcfg = first_active_interface(config);
387 screen->widgets.autoboot_l = widget_new_label(set, 0, 0, "Autoboot:");
388 screen->widgets.autoboot_f = widget_new_checkbox(set, 0, 0,
389 config->autoboot_enabled);
391 str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
392 screen->widgets.timeout_l = widget_new_label(set, 0, 0, "Timeout:");
393 screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
395 screen->widgets.network_l = widget_new_label(set, 0, 0, "Network");
396 screen->widgets.network_f = widget_new_select(set, 0, 0, 50);
398 widget_select_add_option(screen->widgets.network_f,
399 NET_CONF_TYPE_DHCP_ALL,
400 "DHCP on all active interfaces",
401 type == NET_CONF_TYPE_DHCP_ALL);
402 widget_select_add_option(screen->widgets.network_f,
403 NET_CONF_TYPE_DHCP_ONE,
404 "DHCP on a specific interface",
405 type == NET_CONF_TYPE_DHCP_ONE);
406 widget_select_add_option(screen->widgets.network_f,
407 NET_CONF_TYPE_STATIC,
408 "Static IP configuration",
409 type == NET_CONF_TYPE_STATIC);
411 widget_select_on_change(screen->widgets.network_f,
412 config_screen_network_change, screen);
414 screen->widgets.iface_l = widget_new_label(set, 0, 0, "Device:");
415 screen->widgets.iface_f = widget_new_select(set, 0, 0, 20);
417 for (i = 0; i < sysinfo->n_interfaces; i++) {
418 struct interface_info *info = sysinfo->interfaces[i];
421 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
422 sizeof(ifcfg->hwaddr));
424 widget_select_add_option(screen->widgets.iface_f,
425 i, info->name, is_default);
428 gw = ip = mask = NULL;
429 if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
432 str = talloc_strdup(screen, ifcfg->static_config.address);
433 sep = strchr(str, '/');
440 gw = ifcfg->static_config.gateway;
443 screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, "IP/mask:");
444 screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
445 screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
446 screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
448 screen->widgets.gateway_l = widget_new_label(set, 0, 0, "Gateway:");
449 screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
451 str = talloc_strdup(screen, "");
452 for (i = 0; i < config->network.n_dns_servers; i++) {
453 str = talloc_asprintf_append(str, "%s%s",
455 config->network.dns_servers[i]);
458 screen->widgets.dns_l = widget_new_label(set, 0, 0, "DNS Server(s):");
459 screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
461 screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, "OK",
463 screen->widgets.cancel_b = widget_new_button(set, 0, 0, 6, "Cancel",
464 cancel_click, screen);
467 struct config_screen *config_screen_init(struct cui *cui,
468 const struct config *config,
469 const struct system_info *sysinfo,
470 void (*on_exit)(struct cui *))
472 struct config_screen *screen;
474 screen = talloc_zero(cui, struct config_screen);
475 nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
476 cui, config_screen_process_key,
477 config_screen_post, config_screen_unpost,
478 config_screen_resize);
481 screen->on_exit = on_exit;
483 screen->field_x = 17;
485 screen->scr.frame.ltitle = talloc_strdup(screen,
486 "Petitboot System Configuration");
487 screen->scr.frame.rtitle = NULL;
488 screen->scr.frame.help = talloc_strdup(screen,
489 "tab=next, shift+tab=previous");
490 nc_scr_frame_draw(&screen->scr);
492 screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
493 screen->scr.sub_ncw);
494 screen->net_conf_type = find_net_conf_type(config);
496 config_screen_setup_widgets(screen, config, sysinfo);
497 config_screen_layout_widgets(screen, screen->net_conf_type);
499 wrefresh(screen->scr.main_ncw);
500 scrollok(screen->scr.sub_ncw, true);