Skip to content

laurent: add support for controlling USB power #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 18, 2025
Merged
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
4 changes: 2 additions & 2 deletions cdba-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ static void msg_select_board(const void *param)
if (!selected_device) {
fprintf(stderr, "failed to open %s\n", (const char *)param);
watch_quit();
} else {
device_fastboot_open(selected_device, &fastboot_ops);
}

device_fastboot_open(selected_device, &fastboot_ops);

cdba_send(MSG_SELECT_BOARD);
}

Expand Down
2 changes: 1 addition & 1 deletion device.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ void device_usb(struct device *device, bool on)
{
if (device->ppps_path)
ppps_power(device, on);
else if (device_has_control(device, usb))
if (device_has_control(device, usb))
device_control(device, usb, on);
}

Expand Down
30 changes: 28 additions & 2 deletions drivers/laurent.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct laurent_options {
const char *server;
const char *password;
unsigned int relay;
int usb_relay;
};

struct laurent {
Expand All @@ -42,6 +43,7 @@ void *laurent_parse_options(struct device_parser *dp)

options = calloc(1, sizeof(*options));
options->password = DEFAULT_PASSWORD;
options->usb_relay = -1;

device_parser_accept(dp, YAML_MAPPING_START_EVENT, NULL, 0);
while (device_parser_accept(dp, YAML_SCALAR_EVENT, key, TOKEN_LENGTH)) {
Expand All @@ -54,6 +56,8 @@ void *laurent_parse_options(struct device_parser *dp)
options->password = strdup(value);
else if (!strcmp(key, "relay"))
options->relay = strtoul(value, NULL, 0);
else if (!strcmp(key, "usb_relay"))
options->usb_relay = strtoul(value, NULL, 0);
else
errx(1, "%s: unknown option \"%s\"", __func__, key);
}
Expand Down Expand Up @@ -124,7 +128,7 @@ static void *laurent_open(struct device *dev)
return laurent;
}

static int laurent_power(struct device *dev, bool on)
static int laurent_control(struct device *dev, unsigned int relay, bool on)
{
struct laurent *laurent = dev->cdb;
char buf[BUFSIZ];
Expand All @@ -145,7 +149,7 @@ static int laurent_power(struct device *dev, bool on)

len = snprintf(buf, sizeof(buf), "GET /cmd.cgi?psw=%s&cmd=REL,%u,%d HTTP/1.0\r\n\r\n",
laurent->options->password,
laurent->options->relay,
relay,
on);
if (len < 0) {
warn("asprintf failed\n");
Expand Down Expand Up @@ -190,8 +194,30 @@ static int laurent_power(struct device *dev, bool on)
return -1;
}

static int laurent_power(struct device *dev, bool on)
{
struct laurent *laurent = dev->cdb;

return laurent_control(dev,
laurent->options->relay,
on);
}

static void laurent_usb(struct device *dev, bool on)
{
struct laurent *laurent = dev->cdb;

if (laurent->options->usb_relay < 0)
return;

laurent_control(dev,
laurent->options->usb_relay,
on);
}

const struct control_ops laurent_ops = {
.parse_options = laurent_parse_options,
.open = laurent_open,
.power = laurent_power,
.usb = laurent_usb,
};
2 changes: 2 additions & 0 deletions schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ properties:
type: string
relay:
type: integer
usb_relay:
type: integer
password:
description: password to access the relays, defaults to 'Laurent'
type: string
Expand Down
Loading