tty-handler: move baud rate parsing into config.c

This is in preparation for being able to set the baud rate on the host
console tty.

Change-Id: Id279530bdfc1240649fcdd606fbc928437fc38dc
Signed-off-by: Benjamin Fair <benjaminfair@google.com>
diff --git a/config.c b/config.c
index 750f8c9..b68cfcd 100644
--- a/config.c
+++ b/config.c
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <strings.h>
+#include <termios.h> /* for speed_t */
 #include <unistd.h>
 
 #include <sys/mman.h>
@@ -160,6 +161,57 @@
 	free(config);
 }
 
+struct terminal_speed_name {
+	speed_t		speed;
+	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);
+	size_t i;
+
+	for (i = 0; i < num_terminal_speeds; i++) {
+		if (strcmp(baud_string, terminal_speeds[i].name) == 0) {
+			*speed = terminal_speeds[i].speed;
+			return 0;
+		}
+	}
+	return -1;
+}
+
 #ifdef CONFIG_TEST
 int main(void)
 {