Skip to content

Commit f6bd2d0

Browse files
committed
watch: split implementation to aseparate file
Split CDBA's watching code to a separate file. Signed-off-by: Dmitry Baryshkov <[email protected]>
1 parent bde90b4 commit f6bd2d0

File tree

12 files changed

+195
-151
lines changed

12 files changed

+195
-151
lines changed

cdba-server.c

Lines changed: 4 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
*
55
* SPDX-License-Identifier: BSD-3-Clause
66
*/
7-
#include <sys/time.h>
8-
#include <alloca.h>
97
#include <err.h>
108
#include <errno.h>
119
#include <fcntl.h>
@@ -22,8 +20,8 @@
2220
#include "device_parser.h"
2321
#include "fastboot.h"
2422
#include "list.h"
23+
#include "watch.h"
2524

26-
static bool quit_invoked;
2725
static const char *username;
2826

2927
struct device *selected_device;
@@ -88,7 +86,7 @@ static void msg_select_board(const void *param)
8886
selected_device = device_open(param, username, &fastboot_ops);
8987
if (!selected_device) {
9088
fprintf(stderr, "failed to open %s\n", (const char *)param);
91-
quit_invoked = true;
89+
watch_quit();
9290
}
9391

9492
cdba_send(MSG_SELECT_BOARD);
@@ -224,119 +222,14 @@ static int handle_stdin(int fd, void *buf)
224222
return 0;
225223
}
226224

227-
struct watch {
228-
struct list_head node;
229-
230-
int fd;
231-
int (*cb)(int, void*);
232-
void *data;
233-
};
234-
235-
struct timer {
236-
struct list_head node;
237-
struct timeval tv;
238-
239-
void (*cb)(void *);
240-
void *data;
241-
};
242-
243-
static struct list_head read_watches = LIST_INIT(read_watches);
244-
static struct list_head timer_watches = LIST_INIT(timer_watches);
245-
246-
void watch_add_readfd(int fd, int (*cb)(int, void*), void *data)
247-
{
248-
struct watch *w;
249-
250-
w = calloc(1, sizeof(*w));
251-
w->fd = fd;
252-
w->cb = cb;
253-
w->data = data;
254-
255-
list_add(&read_watches, &w->node);
256-
}
257-
258-
void watch_timer_add(int timeout_ms, void (*cb)(void *), void *data)
259-
{
260-
struct timeval tv_timeout;
261-
struct timeval now;
262-
struct timer *t;
263-
264-
t = calloc(1, sizeof(*t));
265-
266-
gettimeofday(&now, NULL);
267-
268-
tv_timeout.tv_sec = timeout_ms / 1000;
269-
tv_timeout.tv_usec = (timeout_ms % 1000) * 1000;
270-
271-
t->cb = cb;
272-
t->data = data;
273-
timeradd(&now, &tv_timeout, &t->tv);
274-
275-
list_add(&timer_watches, &t->node);
276-
}
277-
278-
static struct timeval *watch_timer_next(void)
279-
{
280-
static struct timeval timeout;
281-
struct timeval now;
282-
struct timer *next;
283-
struct timer *t;
284-
285-
if (list_empty(&timer_watches))
286-
return NULL;
287-
288-
next = list_entry_first(&timer_watches, struct timer, node);
289-
290-
list_for_each_entry(t, &timer_watches, node) {
291-
if (timercmp(&t->tv, &next->tv, <))
292-
next = t;
293-
}
294-
295-
gettimeofday(&now, NULL);
296-
timersub(&next->tv, &now, &timeout);
297-
if (timeout.tv_sec < 0) {
298-
timeout.tv_sec = 0;
299-
timeout.tv_usec = 0;
300-
}
301-
302-
return &timeout;
303-
}
304-
305-
static void watch_timer_invoke(void)
306-
{
307-
struct timeval now;
308-
struct timer *tmp;
309-
struct timer *t;
310-
311-
gettimeofday(&now, NULL);
312-
313-
list_for_each_entry_safe(t, tmp, &timer_watches, node) {
314-
if (timercmp(&t->tv, &now, <)) {
315-
t->cb(t->data);
316-
317-
list_del(&t->node);
318-
free(t);
319-
}
320-
}
321-
}
322-
323225
static void sigpipe_handler(int signo)
324226
{
325-
quit_invoked = true;
326-
}
327-
328-
void watch_quit(void)
329-
{
330-
quit_invoked = true;
227+
watch_quit();
331228
}
332229

333230
int main(int argc, char **argv)
334231
{
335-
struct timeval *timeoutp;
336-
struct watch *w;
337-
fd_set rfds;
338232
int flags;
339-
int nfds;
340233
int ret;
341234

342235
signal(SIGPIPE, sigpipe_handler);
@@ -365,40 +258,7 @@ int main(int argc, char **argv)
365258
flags = fcntl(STDIN_FILENO, F_GETFL, 0);
366259
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
367260

368-
while (!quit_invoked) {
369-
nfds = 0;
370-
371-
list_for_each_entry(w, &read_watches, node) {
372-
nfds = MAX(nfds, w->fd);
373-
FD_SET(w->fd, &rfds);
374-
}
375-
376-
if (!FD_ISSET(STDIN_FILENO, &rfds)) {
377-
fprintf(stderr, "rfds is trash!\n");
378-
goto done;
379-
}
380-
381-
timeoutp = watch_timer_next();
382-
ret = select(nfds + 1, &rfds, NULL, NULL, timeoutp);
383-
if (ret < 0 && errno == EINTR)
384-
continue;
385-
else if (ret < 0)
386-
break;
387-
388-
watch_timer_invoke();
389-
390-
list_for_each_entry(w, &read_watches, node) {
391-
if (FD_ISSET(w->fd, &rfds)) {
392-
ret = w->cb(w->fd, w->data);
393-
if (ret < 0) {
394-
fprintf(stderr, "cb returned %d\n", ret);
395-
goto done;
396-
}
397-
}
398-
}
399-
}
400-
401-
done:
261+
watch_run();
402262

403263
/* if we got here, stdin/out/err might be not accessible anymore */
404264
ret = open("/dev/null", O_RDWR);

cdba-server.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66

77
#include "cdba.h"
88

9-
void watch_add_readfd(int fd, int (*cb)(int, void*), void *data);
10-
int watch_add_quit(int (*cb)(int, void*), void *data);
11-
void watch_timer_add(int timeout_ms, void (*cb)(void *), void *data);
12-
void watch_quit(void);
13-
int watch_run(void);
14-
159
int tty_open(const char *tty, struct termios *old);
1610

1711
void cdba_send_buf(int type, size_t len, const void *buf);

console.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "cdba-server.h"
1515
#include "device.h"
16+
#include "watch.h"
1617

1718
struct console {
1819
int console_fd;

device.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "list.h"
2424
#include "ppps.h"
2525
#include "status-cmd.h"
26+
#include "watch.h"
2627

2728
#define ARRAY_SIZE(x) ((sizeof(x)/sizeof((x)[0])))
2829

drivers/cdb_assist.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "cdba-server.h"
2121
#include "device.h"
2222
#include "status.h"
23+
#include "watch.h"
2324

2425
struct cdb_assist {
2526
char serial[9];

drivers/conmux.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "cdba-server.h"
2020
#include "device.h"
21+
#include "watch.h"
2122

2223
extern int h_errno;
2324

drivers/qcomlt_dbg.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "cdba-server.h"
2222
#include "device.h"
2323
#include "status.h"
24+
#include "watch.h"
2425

2526
enum qcomlt_parse_state {
2627
STATE_,

fastboot.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include "cdba-server.h"
2424
#include "fastboot.h"
25+
#include "watch.h"
2526

2627
#define MAX_USBFS_BULK_SIZE (16*1024)
2728

meson.build

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ server_srcs = ['cdba-server.c',
8888
'console.c',
8989
'ppps.c',
9090
'status.c',
91-
'status-cmd.c']
91+
'status-cmd.c',
92+
'watch.c']
9293

9394
build_server = true
9495
foreach d: server_deps

status-cmd.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "device.h"
1818
#include "status.h"
1919
#include "status-cmd.h"
20+
#include "watch.h"
2021

2122
static void launch_status_cmd(struct device *dev)
2223
{

0 commit comments

Comments
 (0)