From d094208e7cab74a288a0bc008c2f92c471525dad Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Sun, 28 Jun 2020 23:45:59 -0700 Subject: [PATCH] cdba: Add support for listing boards Add a new argument '-l' which in combination with '-h' will connect to said server and list the attached devices, then exit. Signed-off-by: Bjorn Andersson --- cdba-server.c | 3 +++ cdba.c | 75 +++++++++++++++++++++++++++++++++++++++++++++------ cdba.h | 1 + device.c | 21 +++++++++++++++ device.h | 1 + 5 files changed, 93 insertions(+), 8 deletions(-) diff --git a/cdba-server.c b/cdba-server.c index db738bd..91bb375 100644 --- a/cdba-server.c +++ b/cdba-server.c @@ -231,6 +231,9 @@ static int handle_stdin(int fd, void *buf) case MSG_SEND_BREAK: device_send_break(selected_device); break; + case MSG_LIST_DEVICES: + device_list_devices(); + break; default: fprintf(stderr, "unk %d len %d\n", msg->type, msg->len); exit(1); diff --git a/cdba.c b/cdba.c index fa87a38..92fde8e 100644 --- a/cdba.c +++ b/cdba.c @@ -224,6 +224,35 @@ struct work { static struct list_head work_items = LIST_INIT(work_items); +struct board_list_request { + struct work work; +}; + +static void list_boards_fn(struct work *work, int ssh_stdin) +{ + struct msg msg; + ssize_t n; + + msg.type = MSG_LIST_DEVICES; + msg.len = 0; + + n = write(ssh_stdin, &msg, sizeof(msg)); + if (n < 0) + err(1, "failed to send board list request"); + + free(work); +} + +static void request_board_list(void) +{ + struct work *work; + + work = malloc(sizeof(*work)); + work->fn = list_boards_fn; + + list_add(&work_items, &work->node); +} + struct select_board { struct work work; @@ -366,6 +395,21 @@ static void handle_status_update(const void *data, size_t len) write(STDOUT_FILENO, str, len + 1); } +static void handle_list_devices(const void *data, size_t len) +{ + char *board; + + if (!len) { + quit = true; + return; + } + + board = alloca(len + 1); + memcpy(board, data, len); + board[len] = '\n'; + write(STDOUT_FILENO, board, len + 1); +} + static bool received_power_off; static bool reached_timeout; @@ -451,6 +495,9 @@ static int handle_message(struct circ_buf *buf) case MSG_STATUS_UPDATE: handle_status_update(msg->data, msg->len); break; + case MSG_LIST_DEVICES: + handle_list_devices(msg->data, msg->len); + break; default: fprintf(stderr, "unk %d len %d\n", msg->type, msg->len); return -1; @@ -481,6 +528,8 @@ static void usage(void) fprintf(stderr, "usage: %s -b -h [-t ] " "[-T ] boot.img\n", __progname); + fprintf(stderr, "usage: %s -l -h \n", + __progname); exit(1); } @@ -501,6 +550,7 @@ int main(int argc, char **argv) struct timeval tv; int power_cycles = 0; struct stat sb; + bool list_only = false; int ssh_fds[3]; char buf[128]; fd_set rfds; @@ -510,7 +560,7 @@ int main(int argc, char **argv) int opt; int ret; - while ((opt = getopt(argc, argv, "b:c:C:h:Rt:T:")) != -1) { + while ((opt = getopt(argc, argv, "b:c:C:h:lRt:T:")) != -1) { switch (opt) { case 'b': board = optarg; @@ -524,6 +574,9 @@ int main(int argc, char **argv) case 'h': host = optarg; break; + case 'l': + list_only = true; + break; case 'R': fastboot_repeat = true; break; @@ -538,16 +591,22 @@ int main(int argc, char **argv) } } - if (optind >= argc || !board || !host) + if (!host) + usage(); + else if (!list_only && (optind >= argc || !board)) usage(); - fastboot_file = argv[optind]; - if (lstat(fastboot_file, &sb)) - err(1, "unable to read \"%s\"", fastboot_file); - if (!S_ISREG(sb.st_mode)) - errx(1, "\"%s\" is not a regular file", fastboot_file); + if (list_only) { + request_board_list(); + } else { + fastboot_file = argv[optind]; + if (lstat(fastboot_file, &sb)) + err(1, "unable to read \"%s\"", fastboot_file); + if (!S_ISREG(sb.st_mode)) + errx(1, "\"%s\" is not a regular file", fastboot_file); - request_select_board(board); + request_select_board(board); + } ret = fork_ssh(host, "sandbox/cdba/cdba-server", ssh_fds); if (ret) diff --git a/cdba.h b/cdba.h index 24582f6..217c15e 100644 --- a/cdba.h +++ b/cdba.h @@ -28,6 +28,7 @@ enum { MSG_VBUS_OFF, MSG_FASTBOOT_REBOOT, MSG_SEND_BREAK, + MSG_LIST_DEVICES, }; #endif diff --git a/device.c b/device.c index cb36c59..cdac6b7 100644 --- a/device.c +++ b/device.c @@ -214,3 +214,24 @@ void device_send_break(struct device *device) if (device->send_break) device->send_break(device); } + +void device_list_devices(void) +{ + struct device *device; + struct msg hdr; + size_t len; + char buf[80]; + + list_for_each_entry(device, &devices, node) { + len = snprintf(buf, sizeof(buf), "%-20s %s", device->board, device->name); + + hdr.type = MSG_LIST_DEVICES; + hdr.len = len; + write(STDOUT_FILENO, &hdr, sizeof(hdr)); + write(STDOUT_FILENO, buf, len); + } + + hdr.type = MSG_LIST_DEVICES; + hdr.len = 0; + write(STDOUT_FILENO, &hdr, sizeof(hdr)); +} diff --git a/device.h b/device.h index 8cc4057..7f36ea4 100644 --- a/device.h +++ b/device.h @@ -56,5 +56,6 @@ void device_fastboot_boot(struct device *device); void device_fastboot_flash_reboot(struct device *device); void device_fastboot_key(struct device *device, bool on); void device_send_break(struct device *device); +void device_list_devices(void); #endif