Skip to content

Commit ca5f95d

Browse files
committed
cdba-power: add power-on/-off tool
Add simple tool reusing CDBA code to power boards on and off. Signed-off-by: Dmitry Baryshkov <[email protected]>
1 parent 762879f commit ca5f95d

File tree

6 files changed

+133
-13
lines changed

6 files changed

+133
-13
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+
}

device.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ static void device_tick(void *data)
227227
}
228228
}
229229

230+
bool device_is_running(struct device *device)
231+
{
232+
return device->state == DEVICE_STATE_RUNNING;
233+
}
234+
230235
static int device_power_on(struct device *device)
231236
{
232237
if (!device || !device_has_control(device, power))

device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ void device_send_break(struct device *device);
9191
void device_list_devices(const char *username);
9292
void device_info(const char *username, const void *data, size_t dlen);
9393
void device_fastboot_continue(struct device *device);
94+
bool device_is_running(struct device *device);
9495

9596
enum {
9697
DEVICE_KEY_FASTBOOT,

meson.build

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ if not ftdi_dep.found()
5454
endif
5555

5656
gpiod_dep = dependency('libgpiod', required: server_opt)
57-
server_deps = [dependency('libudev', required: server_opt),
57+
cdbalib_deps = [dependency('libudev', required: server_opt),
5858
dependency('yaml-0.1', required: server_opt),
5959
gpiod_dep,
6060
ftdi_dep]
6161

6262
# E.g. Debian reuires -lutil for forkpty
6363
if not compiler.has_function('forkpty')
6464
util_dep = compiler.find_library('util')
65-
server_deps += util_dep
65+
cdbalib_deps += util_dep
6666
endif
6767

6868
drivers_srcs = ['drivers/alpaca.c',
@@ -80,8 +80,7 @@ else
8080
drivers_srcs += ['drivers/local-gpio-v1.c']
8181
endif
8282

83-
server_srcs = ['cdba-server.c',
84-
'circ_buf.c',
83+
cdbalib_srcs = ['circ_buf.c',
8584
'device.c',
8685
'device_parser.c',
8786
'fastboot.c',
@@ -92,17 +91,28 @@ server_srcs = ['cdba-server.c',
9291
'watch.c',
9392
'tty.c']
9493

94+
server_srcs = ['cdba-server.c']
95+
9596
build_server = true
96-
foreach d: server_deps
97+
foreach d: cdbalib_deps
9798
if not d.found()
9899
build_server = false
99100
endif
100101
endforeach
101102

102103
if build_server
104+
libcdba = static_library('cdba',
105+
cdbalib_srcs + drivers_srcs,
106+
dependencies : cdbalib_deps,
107+
)
108+
103109
executable('cdba-server',
104-
server_srcs + drivers_srcs,
105-
dependencies : server_deps,
110+
server_srcs,
111+
link_with : libcdba,
112+
install : true)
113+
executable('cdba-power',
114+
['cdba-power.c'],
115+
link_with : libcdba,
106116
install : true)
107117
elif not server_opt.disabled()
108118
message('Skipping CDBA server build')

watch.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void watch_quit(void)
124124
quit_invoked = true;
125125
}
126126

127-
int watch_run(void)
127+
int watch_main_loop(bool (*quit_cb)(void))
128128
{
129129
struct timeval *timeoutp;
130130
struct watch *w;
@@ -133,18 +133,16 @@ int watch_run(void)
133133
int ret;
134134

135135
while (!quit_invoked) {
136+
if (quit_cb && quit_cb())
137+
break;
138+
136139
nfds = 0;
137140

138141
list_for_each_entry(w, &read_watches, node) {
139142
nfds = MAX(nfds, w->fd);
140143
FD_SET(w->fd, &rfds);
141144
}
142145

143-
if (!FD_ISSET(STDIN_FILENO, &rfds)) {
144-
fprintf(stderr, "rfds is trash!\n");
145-
return -EINVAL;
146-
}
147-
148146
timeoutp = watch_timer_next();
149147
ret = select(nfds + 1, &rfds, NULL, NULL, timeoutp);
150148
if (ret < 0 && errno == EINTR)
@@ -170,3 +168,21 @@ int watch_run(void)
170168

171169
return 0;
172170
}
171+
172+
int watch_run(void)
173+
{
174+
struct watch *w;
175+
bool found = false;
176+
177+
list_for_each_entry(w, &read_watches, node) {
178+
if (w->fd == STDIN_FILENO)
179+
found = true;
180+
}
181+
182+
if (!found) {
183+
fprintf(stderr, "rfds is trash!\n");
184+
return -EINVAL;
185+
}
186+
187+
return watch_main_loop(NULL);
188+
}

watch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ void watch_add_readfd(int fd, int (*cb)(int, void*), void *data);
55
int watch_add_quit(int (*cb)(int, void*), void *data);
66
void watch_timer_add(int timeout_ms, void (*cb)(void *), void *data);
77
void watch_quit(void);
8+
int watch_main_loop(bool (*quit_cb)(void));
89
int watch_run(void);
910

1011
#endif

0 commit comments

Comments
 (0)