Skip to content

Commit 798a76a

Browse files
authored
Merge pull request #71 from lumag/cdba-power
cdba-power: add tool to control power-on/-off
2 parents 1da135b + ca5f95d commit 798a76a

30 files changed

+492
-772
lines changed

cdba-power.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2024, Linaro Ltd.
3+
* All rights reserved.
4+
*
5+
* SPDX-License-Identifier: BSD-3-Clause
6+
*/
7+
8+
#include <stdio.h>
9+
#include <stdlib.h>
10+
#include <string.h>
11+
#include <unistd.h>
12+
13+
#include "cdba-server.h"
14+
#include "device.h"
15+
#include "device_parser.h"
16+
#include "watch.h"
17+
18+
void cdba_send_buf(int type, size_t len, const void *buf)
19+
{
20+
/* ignore console messages */
21+
}
22+
23+
static void usage(const char *name)
24+
{
25+
fprintf(stderr, "Usage: %s <board> on|off\n", name);
26+
exit(EXIT_FAILURE);
27+
}
28+
29+
static struct device *selected_device;
30+
31+
bool ready(void)
32+
{
33+
return device_is_running(selected_device);
34+
}
35+
36+
int main(int argc, char **argv)
37+
{
38+
const char *home;
39+
const char *name;
40+
bool on;
41+
int ret;
42+
43+
if (argc != 3)
44+
usage(argv[0]);
45+
46+
if (!strcmp(argv[2], "on"))
47+
on = true;
48+
else if (!strcmp(argv[2], "off"))
49+
on = false;
50+
else
51+
usage(argv[0]);
52+
53+
home = getenv("HOME");
54+
if (home)
55+
chdir(home);
56+
57+
ret = device_parser(".cdba");
58+
if (ret) {
59+
ret = device_parser("/etc/cdba");
60+
if (ret) {
61+
fprintf(stderr, "device parser: unable to open config file\n");
62+
exit(1);
63+
}
64+
}
65+
66+
name = argv[1];
67+
selected_device = device_open(name, "nobody");
68+
if (!selected_device) {
69+
fprintf(stderr, "failed to open %s\n", name);
70+
exit(EXIT_FAILURE);
71+
}
72+
73+
if (on) {
74+
device_power(selected_device, true);
75+
watch_main_loop(ready);
76+
77+
selected_device->usb_always_on = true;
78+
selected_device->power_always_on = true;
79+
} else {
80+
device_usb(selected_device, false);
81+
device_power(selected_device, false);
82+
}
83+
84+
device_close(selected_device);
85+
86+
return 0;
87+
}

cdba-server.c

Lines changed: 8 additions & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,8 @@
22
* Copyright (c) 2016-2018, Linaro Ltd.
33
* All rights reserved.
44
*
5-
* Redistribution and use in source and binary forms, with or without
6-
* modification, are permitted provided that the following conditions are met:
7-
*
8-
* 1. Redistributions of source code must retain the above copyright notice,
9-
* this list of conditions and the following disclaimer.
10-
*
11-
* 2. Redistributions in binary form must reproduce the above copyright notice,
12-
* this list of conditions and the following disclaimer in the documentation
13-
* and/or other materials provided with the distribution.
14-
*
15-
* 3. Neither the name of the copyright holder nor the names of its contributors
16-
* may be used to endorse or promote products derived from this software without
17-
* specific prior written permission.
18-
*
19-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22-
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23-
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24-
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25-
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26-
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27-
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28-
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29-
* POSSIBILITY OF SUCH DAMAGE.
5+
* SPDX-License-Identifier: BSD-3-Clause
306
*/
31-
#include <sys/time.h>
32-
#include <alloca.h>
337
#include <err.h>
348
#include <errno.h>
359
#include <fcntl.h>
@@ -46,40 +20,12 @@
4620
#include "device_parser.h"
4721
#include "fastboot.h"
4822
#include "list.h"
23+
#include "watch.h"
4924

50-
static bool quit_invoked;
5125
static const char *username;
5226

5327
struct device *selected_device;
5428

55-
int tty_open(const char *tty, struct termios *old)
56-
{
57-
struct termios tios;
58-
int ret;
59-
int fd;
60-
61-
fd = open(tty, O_RDWR | O_NOCTTY | O_EXCL);
62-
if (fd < 0)
63-
err(1, "unable to open \"%s\"", tty);
64-
65-
ret = tcgetattr(fd, old);
66-
if (ret < 0)
67-
err(1, "unable to retrieve \"%s\" tios", tty);
68-
69-
memset(&tios, 0, sizeof(tios));
70-
tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
71-
tios.c_iflag = IGNPAR;
72-
tios.c_oflag = 0;
73-
74-
tcflush(fd, TCIFLUSH);
75-
76-
ret = tcsetattr(fd, TCSANOW, &tios);
77-
if (ret < 0)
78-
err(1, "unable to update \"%s\" tios", tty);
79-
80-
return fd;
81-
}
82-
8329
static void fastboot_opened(struct fastboot *fb, void *data)
8430
{
8531
const uint8_t one = 1;
@@ -109,12 +55,14 @@ static struct fastboot_ops fastboot_ops = {
10955

11056
static void msg_select_board(const void *param)
11157
{
112-
selected_device = device_open(param, username, &fastboot_ops);
58+
selected_device = device_open(param, username);
11359
if (!selected_device) {
11460
fprintf(stderr, "failed to open %s\n", (const char *)param);
115-
quit_invoked = true;
61+
watch_quit();
11662
}
11763

64+
device_fastboot_open(selected_device, &fastboot_ops);
65+
11866
cdba_send(MSG_SELECT_BOARD);
11967
}
12068

@@ -248,119 +196,14 @@ static int handle_stdin(int fd, void *buf)
248196
return 0;
249197
}
250198

251-
struct watch {
252-
struct list_head node;
253-
254-
int fd;
255-
int (*cb)(int, void*);
256-
void *data;
257-
};
258-
259-
struct timer {
260-
struct list_head node;
261-
struct timeval tv;
262-
263-
void (*cb)(void *);
264-
void *data;
265-
};
266-
267-
static struct list_head read_watches = LIST_INIT(read_watches);
268-
static struct list_head timer_watches = LIST_INIT(timer_watches);
269-
270-
void watch_add_readfd(int fd, int (*cb)(int, void*), void *data)
271-
{
272-
struct watch *w;
273-
274-
w = calloc(1, sizeof(*w));
275-
w->fd = fd;
276-
w->cb = cb;
277-
w->data = data;
278-
279-
list_add(&read_watches, &w->node);
280-
}
281-
282-
void watch_timer_add(int timeout_ms, void (*cb)(void *), void *data)
283-
{
284-
struct timeval tv_timeout;
285-
struct timeval now;
286-
struct timer *t;
287-
288-
t = calloc(1, sizeof(*t));
289-
290-
gettimeofday(&now, NULL);
291-
292-
tv_timeout.tv_sec = timeout_ms / 1000;
293-
tv_timeout.tv_usec = (timeout_ms % 1000) * 1000;
294-
295-
t->cb = cb;
296-
t->data = data;
297-
timeradd(&now, &tv_timeout, &t->tv);
298-
299-
list_add(&timer_watches, &t->node);
300-
}
301-
302-
static struct timeval *watch_timer_next(void)
303-
{
304-
static struct timeval timeout;
305-
struct timeval now;
306-
struct timer *next;
307-
struct timer *t;
308-
309-
if (list_empty(&timer_watches))
310-
return NULL;
311-
312-
next = list_entry_first(&timer_watches, struct timer, node);
313-
314-
list_for_each_entry(t, &timer_watches, node) {
315-
if (timercmp(&t->tv, &next->tv, <))
316-
next = t;
317-
}
318-
319-
gettimeofday(&now, NULL);
320-
timersub(&next->tv, &now, &timeout);
321-
if (timeout.tv_sec < 0) {
322-
timeout.tv_sec = 0;
323-
timeout.tv_usec = 0;
324-
}
325-
326-
return &timeout;
327-
}
328-
329-
static void watch_timer_invoke(void)
330-
{
331-
struct timeval now;
332-
struct timer *tmp;
333-
struct timer *t;
334-
335-
gettimeofday(&now, NULL);
336-
337-
list_for_each_entry_safe(t, tmp, &timer_watches, node) {
338-
if (timercmp(&t->tv, &now, <)) {
339-
t->cb(t->data);
340-
341-
list_del(&t->node);
342-
free(t);
343-
}
344-
}
345-
}
346-
347199
static void sigpipe_handler(int signo)
348200
{
349-
quit_invoked = true;
350-
}
351-
352-
void watch_quit(void)
353-
{
354-
quit_invoked = true;
201+
watch_quit();
355202
}
356203

357204
int main(int argc, char **argv)
358205
{
359-
struct timeval *timeoutp;
360-
struct watch *w;
361-
fd_set rfds;
362206
int flags;
363-
int nfds;
364207
int ret;
365208

366209
signal(SIGPIPE, sigpipe_handler);
@@ -389,40 +232,7 @@ int main(int argc, char **argv)
389232
flags = fcntl(STDIN_FILENO, F_GETFL, 0);
390233
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
391234

392-
while (!quit_invoked) {
393-
nfds = 0;
394-
395-
list_for_each_entry(w, &read_watches, node) {
396-
nfds = MAX(nfds, w->fd);
397-
FD_SET(w->fd, &rfds);
398-
}
399-
400-
if (!FD_ISSET(STDIN_FILENO, &rfds)) {
401-
fprintf(stderr, "rfds is trash!\n");
402-
goto done;
403-
}
404-
405-
timeoutp = watch_timer_next();
406-
ret = select(nfds + 1, &rfds, NULL, NULL, timeoutp);
407-
if (ret < 0 && errno == EINTR)
408-
continue;
409-
else if (ret < 0)
410-
break;
411-
412-
watch_timer_invoke();
413-
414-
list_for_each_entry(w, &read_watches, node) {
415-
if (FD_ISSET(w->fd, &rfds)) {
416-
ret = w->cb(w->fd, w->data);
417-
if (ret < 0) {
418-
fprintf(stderr, "cb returned %d\n", ret);
419-
goto done;
420-
}
421-
}
422-
}
423-
}
424-
425-
done:
235+
watch_run();
426236

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

cdba-server.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +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-
15-
int tty_open(const char *tty, struct termios *old);
16-
179
void cdba_send_buf(int type, size_t len, const void *buf);
1810
#define cdba_send(type) cdba_send_buf(type, 0, NULL)
1911

cdba.c

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,7 @@
22
* Copyright (c) 2016-2018, Linaro Ltd.
33
* All rights reserved.
44
*
5-
* Redistribution and use in source and binary forms, with or without
6-
* modification, are permitted provided that the following conditions are met:
7-
*
8-
* 1. Redistributions of source code must retain the above copyright notice,
9-
* this list of conditions and the following disclaimer.
10-
*
11-
* 2. Redistributions in binary form must reproduce the above copyright notice,
12-
* this list of conditions and the following disclaimer in the documentation
13-
* and/or other materials provided with the distribution.
14-
*
15-
* 3. Neither the name of the copyright holder nor the names of its contributors
16-
* may be used to endorse or promote products derived from this software without
17-
* specific prior written permission.
18-
*
19-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22-
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23-
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24-
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25-
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26-
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27-
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28-
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29-
* POSSIBILITY OF SUCH DAMAGE.
5+
* SPDX-License-Identifier: BSD-3-Clause
306
*/
317
#include <sys/select.h>
328
#include <sys/stat.h>

0 commit comments

Comments
 (0)