Skip to content

Commit

Permalink
Merge pull request #71 from lumag/cdba-power
Browse files Browse the repository at this point in the history
cdba-power: add tool to control power-on/-off
  • Loading branch information
konradybcio authored Mar 30, 2024
2 parents 1da135b + ca5f95d commit 798a76a
Show file tree
Hide file tree
Showing 30 changed files with 492 additions and 772 deletions.
87 changes: 87 additions & 0 deletions cdba-power.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2024, Linaro Ltd.
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "cdba-server.h"
#include "device.h"
#include "device_parser.h"
#include "watch.h"

void cdba_send_buf(int type, size_t len, const void *buf)
{
/* ignore console messages */
}

static void usage(const char *name)
{
fprintf(stderr, "Usage: %s <board> on|off\n", name);
exit(EXIT_FAILURE);
}

static struct device *selected_device;

bool ready(void)
{
return device_is_running(selected_device);
}

int main(int argc, char **argv)
{
const char *home;
const char *name;
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
usage(argv[0]);

home = getenv("HOME");
if (home)
chdir(home);

ret = device_parser(".cdba");
if (ret) {
ret = device_parser("/etc/cdba");
if (ret) {
fprintf(stderr, "device parser: unable to open config file\n");
exit(1);
}
}

name = argv[1];
selected_device = device_open(name, "nobody");
if (!selected_device) {
fprintf(stderr, "failed to open %s\n", name);
exit(EXIT_FAILURE);
}

if (on) {
device_power(selected_device, true);
watch_main_loop(ready);

selected_device->usb_always_on = true;
selected_device->power_always_on = true;
} else {
device_usb(selected_device, false);
device_power(selected_device, false);
}

device_close(selected_device);

return 0;
}
206 changes: 8 additions & 198 deletions cdba-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,8 @@
* Copyright (c) 2016-2018, Linaro Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <sys/time.h>
#include <alloca.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
Expand All @@ -46,40 +20,12 @@
#include "device_parser.h"
#include "fastboot.h"
#include "list.h"
#include "watch.h"

static bool quit_invoked;
static const char *username;

struct device *selected_device;

int tty_open(const char *tty, struct termios *old)
{
struct termios tios;
int ret;
int fd;

fd = open(tty, O_RDWR | O_NOCTTY | O_EXCL);
if (fd < 0)
err(1, "unable to open \"%s\"", tty);

ret = tcgetattr(fd, old);
if (ret < 0)
err(1, "unable to retrieve \"%s\" tios", tty);

memset(&tios, 0, sizeof(tios));
tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
tios.c_iflag = IGNPAR;
tios.c_oflag = 0;

tcflush(fd, TCIFLUSH);

ret = tcsetattr(fd, TCSANOW, &tios);
if (ret < 0)
err(1, "unable to update \"%s\" tios", tty);

return fd;
}

static void fastboot_opened(struct fastboot *fb, void *data)
{
const uint8_t one = 1;
Expand Down Expand Up @@ -109,12 +55,14 @@ static struct fastboot_ops fastboot_ops = {

static void msg_select_board(const void *param)
{
selected_device = device_open(param, username, &fastboot_ops);
selected_device = device_open(param, username);
if (!selected_device) {
fprintf(stderr, "failed to open %s\n", (const char *)param);
quit_invoked = true;
watch_quit();
}

device_fastboot_open(selected_device, &fastboot_ops);

cdba_send(MSG_SELECT_BOARD);
}

Expand Down Expand Up @@ -248,119 +196,14 @@ static int handle_stdin(int fd, void *buf)
return 0;
}

struct watch {
struct list_head node;

int fd;
int (*cb)(int, void*);
void *data;
};

struct timer {
struct list_head node;
struct timeval tv;

void (*cb)(void *);
void *data;
};

static struct list_head read_watches = LIST_INIT(read_watches);
static struct list_head timer_watches = LIST_INIT(timer_watches);

void watch_add_readfd(int fd, int (*cb)(int, void*), void *data)
{
struct watch *w;

w = calloc(1, sizeof(*w));
w->fd = fd;
w->cb = cb;
w->data = data;

list_add(&read_watches, &w->node);
}

void watch_timer_add(int timeout_ms, void (*cb)(void *), void *data)
{
struct timeval tv_timeout;
struct timeval now;
struct timer *t;

t = calloc(1, sizeof(*t));

gettimeofday(&now, NULL);

tv_timeout.tv_sec = timeout_ms / 1000;
tv_timeout.tv_usec = (timeout_ms % 1000) * 1000;

t->cb = cb;
t->data = data;
timeradd(&now, &tv_timeout, &t->tv);

list_add(&timer_watches, &t->node);
}

static struct timeval *watch_timer_next(void)
{
static struct timeval timeout;
struct timeval now;
struct timer *next;
struct timer *t;

if (list_empty(&timer_watches))
return NULL;

next = list_entry_first(&timer_watches, struct timer, node);

list_for_each_entry(t, &timer_watches, node) {
if (timercmp(&t->tv, &next->tv, <))
next = t;
}

gettimeofday(&now, NULL);
timersub(&next->tv, &now, &timeout);
if (timeout.tv_sec < 0) {
timeout.tv_sec = 0;
timeout.tv_usec = 0;
}

return &timeout;
}

static void watch_timer_invoke(void)
{
struct timeval now;
struct timer *tmp;
struct timer *t;

gettimeofday(&now, NULL);

list_for_each_entry_safe(t, tmp, &timer_watches, node) {
if (timercmp(&t->tv, &now, <)) {
t->cb(t->data);

list_del(&t->node);
free(t);
}
}
}

static void sigpipe_handler(int signo)
{
quit_invoked = true;
}

void watch_quit(void)
{
quit_invoked = true;
watch_quit();
}

int main(int argc, char **argv)
{
struct timeval *timeoutp;
struct watch *w;
fd_set rfds;
int flags;
int nfds;
int ret;

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

while (!quit_invoked) {
nfds = 0;

list_for_each_entry(w, &read_watches, node) {
nfds = MAX(nfds, w->fd);
FD_SET(w->fd, &rfds);
}

if (!FD_ISSET(STDIN_FILENO, &rfds)) {
fprintf(stderr, "rfds is trash!\n");
goto done;
}

timeoutp = watch_timer_next();
ret = select(nfds + 1, &rfds, NULL, NULL, timeoutp);
if (ret < 0 && errno == EINTR)
continue;
else if (ret < 0)
break;

watch_timer_invoke();

list_for_each_entry(w, &read_watches, node) {
if (FD_ISSET(w->fd, &rfds)) {
ret = w->cb(w->fd, w->data);
if (ret < 0) {
fprintf(stderr, "cb returned %d\n", ret);
goto done;
}
}
}
}

done:
watch_run();

/* if we got here, stdin/out/err might be not accessible anymore */
ret = open("/dev/null", O_RDWR);
Expand Down
8 changes: 0 additions & 8 deletions cdba-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@

#include "cdba.h"

void watch_add_readfd(int fd, int (*cb)(int, void*), void *data);
int watch_add_quit(int (*cb)(int, void*), void *data);
void watch_timer_add(int timeout_ms, void (*cb)(void *), void *data);
void watch_quit(void);
int watch_run(void);

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

void cdba_send_buf(int type, size_t len, const void *buf);
#define cdba_send(type) cdba_send_buf(type, 0, NULL)

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

0 comments on commit 798a76a

Please sign in to comment.