diff --git a/alpaca.c b/alpaca.c index 45c6b34..016a11d 100644 --- a/alpaca.c +++ b/alpaca.c @@ -46,30 +46,10 @@ struct alpaca { int alpaca_fd; - int console_fd; struct termios alpaca_tios; - struct termios console_tios; }; -static int alpaca_console_data(int fd, void *data) -{ - struct msg hdr; - char buf[128]; - ssize_t n; - - n = read(fd, buf, sizeof(buf)); - if (n < 0) - return n; - - hdr.type = MSG_CONSOLE; - hdr.len = n; - write(STDOUT_FILENO, &hdr, sizeof(hdr)); - write(STDOUT_FILENO, buf, n); - - return 0; -} - void *alpaca_open(struct device *dev) { struct alpaca *alpaca; @@ -80,12 +60,6 @@ void *alpaca_open(struct device *dev) if (alpaca->alpaca_fd < 0) err(1, "failed to open %s", dev->alpaca_dev); - alpaca->console_fd = tty_open(dev->console_dev, &alpaca->console_tios); - if (alpaca->console_fd < 0) - err(1, "failed to open %s", dev->console_dev); - - watch_add_readfd(alpaca->console_fd, alpaca_console_data, alpaca); - return alpaca; } @@ -140,10 +114,3 @@ int alpaca_power_off(struct device *dev) return 0; } - -int alpaca_write(struct device *dev, const void *buf, size_t len) -{ - struct alpaca *alpaca = dev->cdb; - - return write(alpaca->console_fd, buf, len);; -} diff --git a/alpaca.h b/alpaca.h index 1a7af65..49fe11c 100644 --- a/alpaca.h +++ b/alpaca.h @@ -8,6 +8,5 @@ struct alpaca; void *alpaca_open(struct device *dev); int alpaca_power_on(struct device *dev); int alpaca_power_off(struct device *dev); -int alpaca_write(struct device *dev, const void *buf, size_t len); #endif diff --git a/device.c b/device.c index 3ea90a6..cb36c59 100644 --- a/device.c +++ b/device.c @@ -39,6 +39,7 @@ #include #include +#include "cdba-server.h" #include "device.h" #include "fastboot.h" #include "list.h" @@ -77,6 +78,33 @@ static void device_lock(struct device *device) err(1, "failed to lock lockfile %s", lock); } +static int device_console_data(int fd, void *data) +{ + struct msg hdr; + char buf[128]; + ssize_t n; + + n = read(fd, buf, sizeof(buf)); + if (n < 0) + return n; + + hdr.type = MSG_CONSOLE; + hdr.len = n; + write(STDOUT_FILENO, &hdr, sizeof(hdr)); + write(STDOUT_FILENO, buf, n); + + return 0; +} + +static void console_open(struct device *device) +{ + device->console_fd = tty_open(device->console_dev, &device->console_tios); + if (device->console_fd < 0) + err(1, "failed to open %s", device->console_dev); + + watch_add_readfd(device->console_fd, device_console_data, device); +} + struct device *device_open(const char *board, struct fastboot_ops *fastboot_ops) { @@ -98,6 +126,9 @@ found: if (!device->cdb) errx(1, "failed to open device controller"); + if (device->console_dev) + console_open(device); + device->fastboot = fastboot_open(device->serial, fastboot_ops, NULL); return device; @@ -144,9 +175,13 @@ int device_write(struct device *device, const void *buf, size_t len) if (!device) return 0; - assert(device->write); + if (device->console_fd) { + return write(device->console_fd, buf, len);; + } else { + assert(device->write); - return device->write(device, buf, len); + return device->write(device, buf, len); + } } void device_fastboot_boot(struct device *device) diff --git a/device.h b/device.h index bbb9f02..8cc4057 100644 --- a/device.h +++ b/device.h @@ -1,6 +1,7 @@ #ifndef __DEVICE_H__ #define __DEVICE_H__ +#include #include "list.h" struct cdb_assist; @@ -33,6 +34,9 @@ struct device { void *cdb; + int console_fd; + struct termios console_tios; + struct list_head node; }; diff --git a/device_parser.c b/device_parser.c index 14d7b95..9c90bda 100644 --- a/device_parser.c +++ b/device_parser.c @@ -117,7 +117,6 @@ static void parse_board(struct device_parser *dp) dev->open = alpaca_open; dev->power_on = alpaca_power_on; dev->power_off = alpaca_power_off; - dev->write = alpaca_write; } else if (!strcmp(key, "console")) { dev->console_dev = strdup(value); } else if (!strcmp(key, "voltage")) {