diff --git a/cdb_assist.c b/cdb_assist.c index 0ad1b1b..549afbd 100644 --- a/cdb_assist.c +++ b/cdb_assist.c @@ -344,7 +344,6 @@ unsigned int cdb_vref(struct cdb_assist *cdb) void cdb_assist_print_status(struct device *dev) { struct cdb_assist *cdb = dev->cdb; - struct msg hdr; char buf[128]; int n; @@ -358,10 +357,7 @@ void cdb_assist_print_status(struct device *dev) cdb->btn[2] ? " btn3" : "", cdb->vref); - hdr.type = MSG_STATUS_UPDATE; - hdr.len = n; - write(STDOUT_FILENO, &hdr, sizeof(hdr)); - write(STDOUT_FILENO, buf, n); + cdba_send_buf(MSG_STATUS_UPDATE, n, buf); } void cdb_set_voltage(struct cdb_assist *cdb, unsigned mV) diff --git a/cdba-server.c b/cdba-server.c index ceec6a0..5bd7a25 100644 --- a/cdba-server.c +++ b/cdba-server.c @@ -82,16 +82,10 @@ int tty_open(const char *tty, struct termios *old) static void fastboot_opened(struct fastboot *fb, void *data) { const uint8_t one = 1; - struct msg *msg; warnx("fastboot connection opened"); - msg = alloca(sizeof(*msg) + 1); - msg->type = MSG_FASTBOOT_PRESENT; - msg->len = 1; - memcpy(msg->data, &one, 1); - - write(STDOUT_FILENO, msg, sizeof(*msg) + 1); + cdba_send_buf(MSG_FASTBOOT_PRESENT, 1, &one); } static void fastboot_info(struct fastboot *fb, const void *buf, size_t len) @@ -102,14 +96,8 @@ static void fastboot_info(struct fastboot *fb, const void *buf, size_t len) static void fastboot_disconnect(void *data) { const uint8_t zero = 0; - struct msg *msg; - msg = alloca(sizeof(*msg) + 1); - msg->type = MSG_FASTBOOT_PRESENT; - msg->len = 1; - memcpy(msg->data, &zero, 1); - - write(STDOUT_FILENO, msg, sizeof(*msg) + 1); + cdba_send_buf(MSG_FASTBOOT_PRESENT, 1, &zero); } static struct fastboot_ops fastboot_ops = { @@ -120,15 +108,13 @@ static struct fastboot_ops fastboot_ops = { static void msg_select_board(const void *param) { - struct msg reply = { MSG_SELECT_BOARD, 0 }; - selected_device = device_open(param, username, &fastboot_ops); if (!selected_device) { fprintf(stderr, "failed to open %s\n", (const char *)param); quit_invoked = true; } - write(STDOUT_FILENO, &reply, sizeof(reply)); + cdba_send(MSG_SELECT_BOARD); } static void *fastboot_payload; @@ -136,7 +122,6 @@ static size_t fastboot_size; static void msg_fastboot_download(const void *data, size_t len) { - struct msg reply = { MSG_FASTBOOT_DOWNLOAD, }; size_t new_size = fastboot_size + len; char *newp; @@ -152,18 +137,23 @@ static void msg_fastboot_download(const void *data, size_t len) if (!len) { device_boot(selected_device, fastboot_payload, fastboot_size); - write(STDOUT_FILENO, &reply, sizeof(reply)); + cdba_send(MSG_FASTBOOT_DOWNLOAD); free(fastboot_payload); fastboot_payload = NULL; fastboot_size = 0; } } -static void invoke_reply(int reply) +void cdba_send_buf(int type, size_t len, const void *buf) { - struct msg msg = { reply, }; + struct msg msg = { + .type = type, + .len = len + }; write(STDOUT_FILENO, &msg, sizeof(msg)); + if (len) + write(STDOUT_FILENO, buf, len); } static int handle_stdin(int fd, void *buf) @@ -206,12 +196,12 @@ static int handle_stdin(int fd, void *buf) case MSG_POWER_ON: device_power(selected_device, true); - invoke_reply(MSG_POWER_ON); + cdba_send(MSG_POWER_ON); break; case MSG_POWER_OFF: device_power(selected_device, false); - invoke_reply(MSG_POWER_OFF); + cdba_send(MSG_POWER_OFF); break; case MSG_FASTBOOT_DOWNLOAD: msg_fastboot_download(msg->data, msg->len); diff --git a/cdba-server.h b/cdba-server.h index 5c870d7..0dd6f26 100644 --- a/cdba-server.h +++ b/cdba-server.h @@ -14,4 +14,7 @@ int watch_run(void); int tty_open(const char *tty, struct termios *old); +void cdba_send_buf(int type, size_t len, const void *buf); +#define cdba_send(type) cdba_send_buf(type, 0, NULL) + #endif diff --git a/conmux.c b/conmux.c index 3d71e62..4b0b670 100644 --- a/conmux.c +++ b/conmux.c @@ -207,7 +207,6 @@ out: static int conmux_data(int fd, void *data) { - struct msg hdr; char buf[128]; ssize_t n; @@ -219,10 +218,7 @@ static int conmux_data(int fd, void *data) fprintf(stderr, "Received EOF from conmux\n"); watch_quit(); } else { - hdr.type = MSG_CONSOLE; - hdr.len = n; - write(STDOUT_FILENO, &hdr, sizeof(hdr)); - write(STDOUT_FILENO, buf, n); + cdba_send_buf(MSG_CONSOLE, n, buf); } return 0; diff --git a/console.c b/console.c index 2cab31a..0a21523 100644 --- a/console.c +++ b/console.c @@ -39,7 +39,6 @@ static int console_data(int fd, void *data) { - struct msg hdr; char buf[128]; ssize_t n; @@ -47,10 +46,7 @@ static int console_data(int fd, void *data) if (n < 0) return n; - hdr.type = MSG_CONSOLE; - hdr.len = n; - write(STDOUT_FILENO, &hdr, sizeof(hdr)); - write(STDOUT_FILENO, buf, n); + cdba_send_buf(MSG_CONSOLE, n, buf); return 0; } diff --git a/device.c b/device.c index 1b0e3b9..826b038 100644 --- a/device.c +++ b/device.c @@ -301,7 +301,6 @@ void device_send_break(struct device *device) void device_list_devices(const char *username) { struct device *device; - struct msg hdr; size_t len; char buf[80]; @@ -314,22 +313,16 @@ void device_list_devices(const char *username) else len = snprintf(buf, sizeof(buf), "%s", device->board); - hdr.type = MSG_LIST_DEVICES; - hdr.len = len; - write(STDOUT_FILENO, &hdr, sizeof(hdr)); - write(STDOUT_FILENO, buf, len); + cdba_send_buf(MSG_LIST_DEVICES, len, buf); } - hdr.type = MSG_LIST_DEVICES; - hdr.len = 0; - write(STDOUT_FILENO, &hdr, sizeof(hdr)); + cdba_send_buf(MSG_LIST_DEVICES, 0, NULL); } void device_info(const char *username, const void *data, size_t dlen) { + char *description = NULL; struct device *device; - struct msg hdr; - char *description; size_t len = 0; list_for_each_entry(device, &devices, node) { @@ -346,11 +339,7 @@ void device_info(const char *username, const void *data, size_t dlen) } } - hdr.type = MSG_BOARD_INFO; - hdr.len = len; - write(STDOUT_FILENO, &hdr, sizeof(hdr)); - if (len) - write(STDOUT_FILENO, description, len); + cdba_send_buf(MSG_BOARD_INFO, len, description); } void device_close(struct device *dev)