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)
{
diff --git a/console-server.h b/console-server.h
index 9571ff9..0be567e 100644
--- a/console-server.h
+++ b/console-server.h
@@ -15,8 +15,9 @@
*/
#include <poll.h>
-#include <stdint.h>
#include <stdbool.h>
+#include <stdint.h>
+#include <termios.h> /* for speed_t */
struct console;
struct config;
@@ -116,6 +117,8 @@
struct config *config_init(const char *filename);
void config_fini(struct config *config);
+int config_parse_baud(speed_t *speed, const char *baud_string);
+
/* socket paths */
extern const char *console_socket_path;
extern const size_t console_socket_path_len;
diff --git a/tty-handler.c b/tty-handler.c
index bc10fd9..6248054 100644
--- a/tty-handler.c
+++ b/tty-handler.c
@@ -38,11 +38,6 @@
bool blocked;
};
-struct terminal_speed_name {
- speed_t speed;
- const char *name;
-};
-
static struct tty_handler *to_tty_handler(struct handler *handler)
{
return container_of(handler, struct tty_handler, handler);
@@ -185,58 +180,12 @@
return POLLER_REMOVE;
}
-static int baud_string_to_speed(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;
-}
-
static int set_terminal_baud(struct tty_handler *th, const char *tty_name,
const char *desired_baud) {
struct termios term_options;
speed_t speed;
- if (baud_string_to_speed(&speed, desired_baud) != 0) {
+ 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;