Skip to content

Commit 771910e

Browse files
committed
cdba-server: extract message-sending helper
Instad of hand-coding message sending, create a single wrapper that sends messages to the client. Signed-off-by: Dmitry Baryshkov <[email protected]>
1 parent 74e5139 commit 771910e

File tree

6 files changed

+23
-53
lines changed

6 files changed

+23
-53
lines changed

cdb_assist.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,6 @@ unsigned int cdb_vref(struct cdb_assist *cdb)
344344
void cdb_assist_print_status(struct device *dev)
345345
{
346346
struct cdb_assist *cdb = dev->cdb;
347-
struct msg hdr;
348347
char buf[128];
349348
int n;
350349

@@ -358,10 +357,7 @@ void cdb_assist_print_status(struct device *dev)
358357
cdb->btn[2] ? " btn3" : "",
359358
cdb->vref);
360359

361-
hdr.type = MSG_STATUS_UPDATE;
362-
hdr.len = n;
363-
write(STDOUT_FILENO, &hdr, sizeof(hdr));
364-
write(STDOUT_FILENO, buf, n);
360+
cdba_send_buf(MSG_STATUS_UPDATE, n, buf);
365361
}
366362

367363
void cdb_set_voltage(struct cdb_assist *cdb, unsigned mV)

cdba-server.c

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,10 @@ int tty_open(const char *tty, struct termios *old)
8282
static void fastboot_opened(struct fastboot *fb, void *data)
8383
{
8484
const uint8_t one = 1;
85-
struct msg *msg;
8685

8786
warnx("fastboot connection opened");
8887

89-
msg = alloca(sizeof(*msg) + 1);
90-
msg->type = MSG_FASTBOOT_PRESENT;
91-
msg->len = 1;
92-
memcpy(msg->data, &one, 1);
93-
94-
write(STDOUT_FILENO, msg, sizeof(*msg) + 1);
88+
cdba_send_buf(MSG_FASTBOOT_PRESENT, 1, &one);
9589
}
9690

9791
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)
10296
static void fastboot_disconnect(void *data)
10397
{
10498
const uint8_t zero = 0;
105-
struct msg *msg;
10699

107-
msg = alloca(sizeof(*msg) + 1);
108-
msg->type = MSG_FASTBOOT_PRESENT;
109-
msg->len = 1;
110-
memcpy(msg->data, &zero, 1);
111-
112-
write(STDOUT_FILENO, msg, sizeof(*msg) + 1);
100+
cdba_send_buf(MSG_FASTBOOT_PRESENT, 1, &zero);
113101
}
114102

115103
static struct fastboot_ops fastboot_ops = {
@@ -120,23 +108,20 @@ static struct fastboot_ops fastboot_ops = {
120108

121109
static void msg_select_board(const void *param)
122110
{
123-
struct msg reply = { MSG_SELECT_BOARD, 0 };
124-
125111
selected_device = device_open(param, username, &fastboot_ops);
126112
if (!selected_device) {
127113
fprintf(stderr, "failed to open %s\n", (const char *)param);
128114
quit_invoked = true;
129115
}
130116

131-
write(STDOUT_FILENO, &reply, sizeof(reply));
117+
cdba_send(MSG_SELECT_BOARD);
132118
}
133119

134120
static void *fastboot_payload;
135121
static size_t fastboot_size;
136122

137123
static void msg_fastboot_download(const void *data, size_t len)
138124
{
139-
struct msg reply = { MSG_FASTBOOT_DOWNLOAD, };
140125
size_t new_size = fastboot_size + len;
141126
char *newp;
142127

@@ -152,18 +137,23 @@ static void msg_fastboot_download(const void *data, size_t len)
152137
if (!len) {
153138
device_boot(selected_device, fastboot_payload, fastboot_size);
154139

155-
write(STDOUT_FILENO, &reply, sizeof(reply));
140+
cdba_send(MSG_FASTBOOT_DOWNLOAD);
156141
free(fastboot_payload);
157142
fastboot_payload = NULL;
158143
fastboot_size = 0;
159144
}
160145
}
161146

162-
static void invoke_reply(int reply)
147+
void cdba_send_buf(int type, size_t len, const void *buf)
163148
{
164-
struct msg msg = { reply, };
149+
struct msg msg = {
150+
.type = type,
151+
.len = len
152+
};
165153

166154
write(STDOUT_FILENO, &msg, sizeof(msg));
155+
if (len)
156+
write(STDOUT_FILENO, buf, len);
167157
}
168158

169159
static int handle_stdin(int fd, void *buf)
@@ -206,12 +196,12 @@ static int handle_stdin(int fd, void *buf)
206196
case MSG_POWER_ON:
207197
device_power(selected_device, true);
208198

209-
invoke_reply(MSG_POWER_ON);
199+
cdba_send(MSG_POWER_ON);
210200
break;
211201
case MSG_POWER_OFF:
212202
device_power(selected_device, false);
213203

214-
invoke_reply(MSG_POWER_OFF);
204+
cdba_send(MSG_POWER_OFF);
215205
break;
216206
case MSG_FASTBOOT_DOWNLOAD:
217207
msg_fastboot_download(msg->data, msg->len);

cdba-server.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ int watch_run(void);
1414

1515
int tty_open(const char *tty, struct termios *old);
1616

17+
void cdba_send_buf(int type, size_t len, const void *buf);
18+
#define cdba_send(type) cdba_send_buf(type, 0, NULL)
19+
1720
#endif

conmux.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,6 @@ static int registry_lookup(const char *service, struct conmux_lookup *result)
207207

208208
static int conmux_data(int fd, void *data)
209209
{
210-
struct msg hdr;
211210
char buf[128];
212211
ssize_t n;
213212

@@ -219,10 +218,7 @@ static int conmux_data(int fd, void *data)
219218
fprintf(stderr, "Received EOF from conmux\n");
220219
watch_quit();
221220
} else {
222-
hdr.type = MSG_CONSOLE;
223-
hdr.len = n;
224-
write(STDOUT_FILENO, &hdr, sizeof(hdr));
225-
write(STDOUT_FILENO, buf, n);
221+
cdba_send_buf(MSG_CONSOLE, n, buf);
226222
}
227223

228224
return 0;

console.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,14 @@
3939

4040
static int console_data(int fd, void *data)
4141
{
42-
struct msg hdr;
4342
char buf[128];
4443
ssize_t n;
4544

4645
n = read(fd, buf, sizeof(buf));
4746
if (n < 0)
4847
return n;
4948

50-
hdr.type = MSG_CONSOLE;
51-
hdr.len = n;
52-
write(STDOUT_FILENO, &hdr, sizeof(hdr));
53-
write(STDOUT_FILENO, buf, n);
49+
cdba_send_buf(MSG_CONSOLE, n, buf);
5450

5551
return 0;
5652
}

device.c

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ void device_send_break(struct device *device)
301301
void device_list_devices(const char *username)
302302
{
303303
struct device *device;
304-
struct msg hdr;
305304
size_t len;
306305
char buf[80];
307306

@@ -314,22 +313,16 @@ void device_list_devices(const char *username)
314313
else
315314
len = snprintf(buf, sizeof(buf), "%s", device->board);
316315

317-
hdr.type = MSG_LIST_DEVICES;
318-
hdr.len = len;
319-
write(STDOUT_FILENO, &hdr, sizeof(hdr));
320-
write(STDOUT_FILENO, buf, len);
316+
cdba_send_buf(MSG_LIST_DEVICES, len, buf);
321317
}
322318

323-
hdr.type = MSG_LIST_DEVICES;
324-
hdr.len = 0;
325-
write(STDOUT_FILENO, &hdr, sizeof(hdr));
319+
cdba_send_buf(MSG_LIST_DEVICES, 0, NULL);
326320
}
327321

328322
void device_info(const char *username, const void *data, size_t dlen)
329323
{
324+
char *description = NULL;
330325
struct device *device;
331-
struct msg hdr;
332-
char *description;
333326
size_t len = 0;
334327

335328
list_for_each_entry(device, &devices, node) {
@@ -346,11 +339,7 @@ void device_info(const char *username, const void *data, size_t dlen)
346339
}
347340
}
348341

349-
hdr.type = MSG_BOARD_INFO;
350-
hdr.len = len;
351-
write(STDOUT_FILENO, &hdr, sizeof(hdr));
352-
if (len)
353-
write(STDOUT_FILENO, description, len);
342+
cdba_send_buf(MSG_BOARD_INFO, len, description);
354343
}
355344

356345
void device_close(struct device *dev)

0 commit comments

Comments
 (0)