Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions cdba-power.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void cdba_send_buf(int type, size_t len, const void *buf)
static void usage(const char *name)
{
fprintf(stderr, "Usage: %s <board> on|off\n", name);
fprintf(stderr, "Usage: %s all-off\n", name);
exit(EXIT_FAILURE);
}

Expand All @@ -33,22 +34,47 @@ bool ready(void)
return device_is_running(selected_device);
}

static int all_off(void)
{
struct device *next = NULL, *dev;
int ret = 0;

while ((next = device_next(next))) {
dev = device_open(next->board, "nobody");
if (!dev) {
fprintf(stderr, "failed to open %s\n", next->board);
ret = EXIT_FAILURE;
continue;
}

device_usb(dev, false);
device_power_off(dev);
device_close(dev);
}

return ret;
}

int main(int argc, char **argv)
{
const char *home;
const char *name;
bool all = false;
bool on;
int ret;

if (argc != 3)
usage(argv[0]);

if (!strcmp(argv[2], "on"))
on = true;
else if (!strcmp(argv[2], "off"))
on = false;
else
if (argc == 2 && !strcmp(argv[1], "all-off")) {
all = true;
} else if (argc == 3) {
if (!strcmp(argv[2], "on"))
on = true;
else if (!strcmp(argv[2], "off"))
on = false;
else
usage(argv[0]);
} else {
usage(argv[0]);
}

home = getenv("HOME");
if (home)
Expand All @@ -63,6 +89,9 @@ int main(int argc, char **argv)
}
}

if (all)
return all_off();

name = argv[1];
selected_device = device_open(name, "nobody");
if (!selected_device) {
Expand Down
11 changes: 11 additions & 0 deletions device.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ void device_add(struct device *device)
list_append(&devices, &device->node);
}

struct device *device_next(struct device *device)
{
struct list_head *item;

item = device ? device->node.next : devices.next;
if (item == &devices)
return NULL;

return list_entry(item, struct device, node);
}

static void device_lock(struct device *device)
{
char lock[PATH_MAX];
Expand Down
1 change: 1 addition & 0 deletions device.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ struct device_qdl_user {
};

void device_add(struct device *device);
struct device *device_next(struct device *device);

struct device *device_open(const char *board,
const char *username);
Expand Down
Loading