Change baudrate dynamically through dbus

Create a dbus interface so that user can change baudrate dynamically.
With this feature, SOL can still work correctly when BIOS switch uart
from high speed uart to normal uart.

Tested By:
Run busctl introspect xyz.openbmc_project.console /xyz/openbmc_project/console
the property baudrate show the current baudrate.
Run busctl call xyz.openbmc_project.console /xyz/openbmc_project/console
xyz.openbmc_project.console setBaudRate x 9600
The property baudrate show 9600 now. After change BIOS console redirection
to 9600 baudrate and change putty client also to 9600. SOL and serial port can
work correctly.

Change-Id: I2045f47520275a0b5bb9242af78a64e5aac8ea8a
Signed-off-by: Cheng C Yang <cheng.c.yang@linux.intel.com>
diff --git a/Makefile.am b/Makefile.am
index 96fa9fb..f61fddf 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -18,6 +18,9 @@
 	 tty-handler.c \
 	 console-socket.c
 
+obmc_console_server_LDFLAGS = $(SYSTEMD_LIBS)
+obmc_console_server_CFLAGS = $(SYSTEMD_CFLAGS)
+
 obmc_console_client_SOURCES = \
 	 console-client.c \
 	 console-server.h \
diff --git a/config.c b/config.c
index f5653dc..51ab0f3 100644
--- a/config.c
+++ b/config.c
@@ -28,6 +28,7 @@
 
 #include <sys/mman.h>
 #include <sys/stat.h>
+#include "console-server.h"
 
 static const char *config_default_filename = SYSCONFDIR "/obmc-console.conf";
 
@@ -165,47 +166,50 @@
 
 struct terminal_speed_name {
 	speed_t		speed;
+	uint32_t	baud;
 	const char	*name;
 };
 
-int config_parse_baud(speed_t *speed, const char *baud_string) {
-	const struct terminal_speed_name terminal_speeds[] = {
-		{ B50, "50" },
-		{ B75, "75" },
-		{ B110, "110" },
-		{ B134, "134" },
-		{ B150, "150" },
-		{ B200, "200" },
-		{ B300, "300" },
-		{ B600, "600" },
-		{ B1200, "1200" },
-		{ B1800, "1800" },
-		{ B2400, "2400" },
-		{ B4800, "4800" },
-		{ B9600, "9600" },
-		{ B19200, "19200" },
-		{ B38400, "38400" },
-		{ B57600, "57600" },
-		{ B115200, "115200" },
-		{ B230400, "230400" },
-		{ B460800, "460800" },
-		{ B500000, "500000" },
-		{ B576000, "576000" },
-		{ B921600, "921600" },
-		{ B1000000, "1000000" },
-		{ B1152000, "1152000" },
-		{ B1500000, "1500000" },
-		{ B2000000, "2000000" },
-		{ B2500000, "2500000" },
-		{ B3000000, "3000000" },
-		{ B3500000, "3500000" },
-		{ B4000000, "4000000" },
-	};
-	const size_t num_terminal_speeds = sizeof(terminal_speeds) /
-		sizeof(struct terminal_speed_name);
+#define TERM_SPEED(x) { B##x, x, #x}
+
+static const struct terminal_speed_name terminal_speeds[] = {
+	TERM_SPEED(50),
+	TERM_SPEED(75),
+	TERM_SPEED(110),
+	TERM_SPEED(134),
+	TERM_SPEED(150),
+	TERM_SPEED(200),
+	TERM_SPEED(300),
+	TERM_SPEED(600),
+	TERM_SPEED(1200),
+	TERM_SPEED(1800),
+	TERM_SPEED(2400),
+	TERM_SPEED(4800),
+	TERM_SPEED(9600),
+	TERM_SPEED(19200),
+	TERM_SPEED(38400),
+	TERM_SPEED(57600),
+	TERM_SPEED(115200),
+	TERM_SPEED(230400),
+	TERM_SPEED(460800),
+	TERM_SPEED(500000),
+	TERM_SPEED(576000),
+	TERM_SPEED(921600),
+	TERM_SPEED(1000000),
+	TERM_SPEED(1152000),
+	TERM_SPEED(1500000),
+	TERM_SPEED(2000000),
+	TERM_SPEED(2500000),
+	TERM_SPEED(3000000),
+	TERM_SPEED(3500000),
+	TERM_SPEED(4000000),
+};
+
+int config_parse_baud(speed_t *speed, const char *baud_string)
+{
 	size_t i;
 
-	for (i = 0; i < num_terminal_speeds; i++) {
+	for (i = 0; i < ARRAY_SIZE(terminal_speeds); i++) {
 		if (strcmp(baud_string, terminal_speeds[i].name) == 0) {
 			*speed = terminal_speeds[i].speed;
 			return 0;
@@ -214,6 +218,30 @@
 	return -1;
 }
 
+uint32_t parse_baud_to_int(speed_t speed)
+{
+	size_t i;
+
+	for (i = 0; i < ARRAY_SIZE(terminal_speeds); i++) {
+		if (terminal_speeds[i].speed == speed) {
+			return terminal_speeds[i].baud;
+		}
+	}
+	return 0;
+}
+
+speed_t parse_int_to_baud(uint32_t baud)
+{
+	size_t i;
+
+	for (i = 0; i < ARRAY_SIZE(terminal_speeds); i++) {
+		if (terminal_speeds[i].baud == baud) {
+			return terminal_speeds[i].speed;
+		}
+	}
+	return 0;
+}
+
 int config_parse_logsize(const char *size_str, size_t *size)
 {
 	struct size_suffix_shift {
diff --git a/configure.ac b/configure.ac
index e705ba1..f720c7c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -12,6 +12,7 @@
 AC_PROG_MAKE_SET
 
 # Checks for libraries.
+PKG_CHECK_MODULES([SYSTEMD], [libsystemd > 221])
 
 # Checks for header files.
 
diff --git a/console-server.c b/console-server.c
index 22e6094..cde4bfc 100644
--- a/console-server.c
+++ b/console-server.c
@@ -38,6 +38,10 @@
 
 #include "console-server.h"
 
+#define DBUS_ERR "org.openbmc.error"
+#define DBUS_NAME "xyz.openbmc_project.console"
+#define OBJ_NAME "/xyz/openbmc_project/console"
+
 struct console {
 	const char	*tty_kname;
 	char		*tty_sysfs_devnode;
@@ -56,6 +60,7 @@
 	int		n_pollers;
 
 	struct pollfd	*pollfds;
+	struct sd_bus	*bus;
 };
 
 struct poller {
@@ -65,8 +70,12 @@
 	bool		remove;
 };
 
-/* we have one extra entry in the pollfds array for the VUART tty */
-static const int n_internal_pollfds = 1;
+/* we have two extra entry in the pollfds array for the VUART tty */
+enum internal_pollfds {
+	POLLFD_HOSTTTY = 0,
+	POLLFD_DBUS = 1,
+	MAX_INTERNAL_POLLFD = 2,
+};
 
 /* size of the shared backlog ringbuffer */
 const size_t buffer_size = 128 * 1024;
@@ -216,6 +225,26 @@
 		warn("Can't set terminal options for %s", console->tty_kname);
 }
 
+
+static void tty_change_baudrate(struct console *console)
+{
+	struct handler *handler;
+	int i, rc;
+
+	tty_init_termios(console);
+
+	for (i = 0; i < console->n_handlers; i++) {
+		handler = console->handlers[i];
+		if (!handler->baudrate)
+			continue;
+
+		rc = handler->baudrate(handler, console->tty_baud);
+		if (rc)
+			warnx("Can't set terminal baudrate for handler %s",
+			     handler->name);
+	}
+}
+
 /**
  * Open and initialise the serial device
  */
@@ -293,6 +322,102 @@
 	return write_buf_to_fd(console->tty_fd, data, len);
 }
 
+static int method_set_baud_rate(sd_bus_message *msg, void *userdata,
+			 sd_bus_error *err)
+{
+	struct console *console = userdata;
+	uint32_t baudrate;
+	speed_t speed;
+	int r;
+
+	if (!console) {
+		sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
+		return sd_bus_reply_method_return(msg, "x", 0);
+	}
+
+	r = sd_bus_message_read(msg, "u", &baudrate);
+	if (r < 0) {
+		sd_bus_error_set_const(err, DBUS_ERR, "Bad message");
+		return sd_bus_reply_method_return(msg, "x", -EINVAL);
+	}
+
+	speed = parse_int_to_baud(baudrate);
+	if (!speed) {
+		warnx("Invalid baud rate: '%u'", baudrate);
+		return sd_bus_reply_method_return(msg, "x", -EINVAL);
+	}
+
+	console->tty_baud = speed;
+	tty_change_baudrate(console);
+
+	return sd_bus_reply_method_return(msg, "x", r);
+}
+
+static int get_handler(sd_bus *bus, const char *path, const char *interface,
+		       const char *property, sd_bus_message *reply, void *userdata,
+		       sd_bus_error *error) {
+	struct console *console = userdata;
+	uint32_t baudrate;
+	int r;
+
+	baudrate = parse_baud_to_int(console->tty_baud);
+	if (!baudrate)
+		warnx("Invalid baud rate: '%d'", console->tty_baud);
+
+	r = sd_bus_message_append(reply, "u", baudrate);
+
+	return r;
+}
+
+static const sd_bus_vtable console_vtable[] = {
+	SD_BUS_VTABLE_START(0),
+	SD_BUS_METHOD("setBaudRate", "u", "x", method_set_baud_rate,
+		      SD_BUS_VTABLE_UNPRIVILEGED),
+	SD_BUS_PROPERTY("baudrate", "u", get_handler, 0, 0),
+	SD_BUS_VTABLE_END,};
+
+static void dbus_init(struct console *console, struct config *config)
+{
+	int dbus_poller = 0;
+	int fd, r;
+
+	if (!console) {
+		warnx("Couldn't get valid console");
+		return;
+	}
+
+	r = sd_bus_default_system(&console->bus);
+	if (r < 0) {
+		warnx("Failed to connect to system bus: %s", strerror(-r));
+		return;
+	}
+
+	r = sd_bus_add_object_vtable(console->bus, NULL, OBJ_NAME, DBUS_NAME,
+				     console_vtable, console);
+	if (r < 0) {
+		warnx("Failed to issue method call: %s", strerror(-r));
+		return;
+	}
+
+	r = sd_bus_request_name(console->bus, DBUS_NAME, SD_BUS_NAME_ALLOW_REPLACEMENT
+				|SD_BUS_NAME_REPLACE_EXISTING);
+	if (r < 0) {
+		warnx("Failed to acquire service name: %s", strerror(-r));
+		return;
+	}
+
+	fd = sd_bus_get_fd(console->bus);
+	if (fd < 0) {
+		warnx("Couldn't get the bus file descriptor");
+		return;
+	}
+
+	dbus_poller = POLLFD_DBUS;
+
+	console->pollfds[dbus_poller].fd = fd;
+	console->pollfds[dbus_poller].events = POLLIN;
+}
+
 static void handlers_init(struct console *console, struct config *config)
 {
 	extern struct handler *__start_handlers, *__stop_handlers;
@@ -361,12 +486,12 @@
 	/* increase pollfds array too  */
 	console->pollfds = realloc(console->pollfds,
 			sizeof(*console->pollfds) *
-				(n_internal_pollfds + console->n_pollers));
+				(MAX_INTERNAL_POLLFD + console->n_pollers));
 
 	/* shift the end pollfds up by one */
-	memcpy(&console->pollfds[n+n_internal_pollfds],
-			&console->pollfds[n],
-			sizeof(*console->pollfds) * n_internal_pollfds);
+	memcpy(&console->pollfds[n+1],
+		&console->pollfds[n],
+		sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
 
 	console->pollfds[n].fd = fd;
 	console->pollfds[n].events = events;
@@ -399,11 +524,11 @@
 	/* ... and the pollfds array */
 	memmove(&console->pollfds[i], &console->pollfds[i+1],
 			sizeof(*console->pollfds) *
-				(n_internal_pollfds + console->n_pollers - i));
+				(MAX_INTERNAL_POLLFD + console->n_pollers - i));
 
 	console->pollfds = realloc(console->pollfds,
 			sizeof(*console->pollfds) *
-				(n_internal_pollfds + console->n_pollers));
+				(MAX_INTERNAL_POLLFD + console->n_pollers));
 
 
 	free(poller);
@@ -498,7 +623,7 @@
 		}
 
 		rc = poll(console->pollfds,
-				console->n_pollers + n_internal_pollfds, -1);
+				console->n_pollers + MAX_INTERNAL_POLLFD, -1);
 		if (rc < 0) {
 			if (errno == EINTR) {
 				continue;
@@ -509,8 +634,6 @@
 		}
 
 		/* process internal fd first */
-		BUILD_ASSERT(n_internal_pollfds == 1);
-
 		if (console->pollfds[console->n_pollers].revents) {
 			rc = read(console->tty_fd, buf, sizeof(buf));
 			if (rc <= 0) {
@@ -523,6 +646,10 @@
 				break;
 		}
 
+		if (console->pollfds[console->n_pollers + 1].revents) {
+			sd_bus_process(console->bus, NULL);
+		}
+
 		/* ... and then the pollers */
 		rc = call_pollers(console);
 		if (rc)
@@ -530,6 +657,7 @@
 	}
 
 	signal(SIGINT, sighandler_save);
+	sd_bus_unref(console->bus);
 
 	return rc ? -1 : 0;
 }
@@ -576,7 +704,7 @@
 
 	console = malloc(sizeof(struct console));
 	memset(console, 0, sizeof(*console));
-	console->pollfds = calloc(n_internal_pollfds,
+	console->pollfds = calloc(MAX_INTERNAL_POLLFD,
 			sizeof(*console->pollfds));
 	console->rb = ringbuffer_init(buffer_size);
 
@@ -592,6 +720,8 @@
 	if (rc)
 		goto out_config_fini;
 
+	dbus_init(console, config);
+
 	handlers_init(console, config);
 
 	rc = run_console(console);
diff --git a/console-server.h b/console-server.h
index d2ac87f..9bb12ba 100644
--- a/console-server.h
+++ b/console-server.h
@@ -18,6 +18,7 @@
 #include <stdbool.h>
 #include <stdint.h>
 #include <termios.h> /* for speed_t */
+#include <systemd/sd-bus.h>
 
 struct console;
 struct config;
@@ -44,6 +45,8 @@
 				struct console *console,
 				struct config *config);
 	void		(*fini)(struct handler *handler);
+	int		(*baudrate)(struct handler *handler,
+				    speed_t baudrate);
 	bool		active;
 };
 
@@ -118,6 +121,8 @@
 void config_fini(struct config *config);
 
 int config_parse_baud(speed_t *speed, const char *baud_string);
+uint32_t parse_baud_to_int(speed_t speed);
+speed_t parse_int_to_baud(uint32_t baud);
 int config_parse_logsize(const char *size_str, size_t *size);
 
 /* socket paths */
diff --git a/tty-handler.c b/tty-handler.c
index 6248054..a6e622d 100644
--- a/tty-handler.c
+++ b/tty-handler.c
@@ -181,15 +181,8 @@
 }
 
 static int set_terminal_baud(struct tty_handler *th, const char *tty_name,
-		const char *desired_baud) {
+		speed_t speed) {
 	struct termios term_options;
-	speed_t speed;
-
-	if (config_parse_baud(&speed, desired_baud) != 0) {
-		fprintf(stderr, "%s is not a valid baud rate for terminal %s\n",
-				desired_baud, tty_name);
-		return -1;
-	}
 
 	if (tcgetattr(th->fd, &term_options) < 0) {
 		warn("Can't get config for %s", tty_name);
@@ -205,7 +198,6 @@
 		warn("Couldn't commit terminal options for %s", tty_name);
 		return -1;
 	}
-	printf("Set %s terminal baud rate to %s\n", tty_name, desired_baud);
 
 	return 0;
 }
@@ -236,6 +228,7 @@
 		struct config *config __attribute__((unused)))
 {
 	struct tty_handler *th = to_tty_handler(handler);
+	speed_t desired_speed;
 	const char *tty_name;
 	const char *tty_baud;
 	char *tty_path;
@@ -260,10 +253,18 @@
 	th->fd_flags = fcntl(th->fd, F_GETFL, 0);
 
 	tty_baud = config_get_value(config, "local-tty-baud");
-	if (tty_baud != NULL)
-		if (set_terminal_baud(th, tty_name, tty_baud) != 0)
-			fprintf(stderr, "Couldn't set baud rate for %s to %s\n",
+	if (tty_baud != NULL) {
+		rc = config_parse_baud(&desired_speed, tty_baud);
+		if (rc) {
+			fprintf(stderr, "%s is not a valid baud rate\n",
+				tty_baud);
+		} else {
+			rc = set_terminal_baud(th, tty_name, desired_speed);
+			if (rc)
+				fprintf(stderr, "Couldn't set baud rate for %s to %s\n",
 					tty_name, tty_baud);
+		}
+	}
 
 	if (make_terminal_raw(th, tty_name) != 0)
 		fprintf(stderr, "Couldn't make %s a raw terminal\n", tty_name);
@@ -285,11 +286,29 @@
 	close(th->fd);
 }
 
+static int tty_baudrate(struct handler *handler, speed_t baudrate)
+{
+	const char *tty_name = "local-tty";
+	struct tty_handler *th = to_tty_handler(handler);
+
+	if (baudrate == 0) {
+		return -1;
+	}
+
+	if (set_terminal_baud(th, tty_name, baudrate) != 0) {
+		fprintf(stderr, "Couldn't set baud rate for %s to %d\n",
+			tty_name, baudrate);
+		return -1;
+	}
+	return 0;
+}
+
 static struct tty_handler tty_handler = {
 	.handler = {
 		.name		= "tty",
 		.init		= tty_init,
 		.fini		= tty_fini,
+		.baudrate	= tty_baudrate,
 	},
 };