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 <bjorn.andersson@linaro.org>
This commit is contained in:
Bjorn Andersson
2020-06-28 23:45:59 -07:00
parent 72c373451c
commit d094208e7c
5 changed files with 93 additions and 8 deletions

View File

@@ -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);

75
cdba.c
View File

@@ -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 <board> -h <host> [-t <timeout>] "
"[-T <inactivity-timeout>] boot.img\n",
__progname);
fprintf(stderr, "usage: %s -l -h <host>\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)

1
cdba.h
View File

@@ -28,6 +28,7 @@ enum {
MSG_VBUS_OFF,
MSG_FASTBOOT_REBOOT,
MSG_SEND_BREAK,
MSG_LIST_DEVICES,
};
#endif

View File

@@ -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));
}

View File

@@ -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