Skip to content

Commit

Permalink
Merge pull request #9 from lumag/external
Browse files Browse the repository at this point in the history
Rework cdba to support controlling device via external program
  • Loading branch information
konradybcio authored Oct 23, 2023
2 parents f09b3e1 + a0de781 commit fbdc677
Show file tree
Hide file tree
Showing 18 changed files with 344 additions and 201 deletions.
17 changes: 12 additions & 5 deletions alpaca.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#include <unistd.h>

#include "cdba-server.h"
#include "alpaca.h"
#include "device.h"

struct alpaca {
int alpaca_fd;
Expand All @@ -53,7 +53,7 @@ struct alpaca {
static int alpaca_device_power(struct alpaca *alpaca, int on);
static int alpaca_usb_device_power(struct alpaca *alpaca, int on);

void *alpaca_open(struct device *dev)
static void *alpaca_open(struct device *dev)
{
struct alpaca *alpaca;

Expand Down Expand Up @@ -121,22 +121,22 @@ static int alpaca_power_off(struct device *dev)
return 0;
}

int alpaca_power(struct device *dev, bool on)
static int alpaca_power(struct device *dev, bool on)
{
if (on)
return alpaca_power_on(dev);
else
return alpaca_power_off(dev);
}

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

alpaca_usb_device_power(alpaca, on);
}

void alpaca_key(struct device *dev, int key, bool asserted)
static void alpaca_key(struct device *dev, int key, bool asserted)
{
switch (key) {
case DEVICE_KEY_FASTBOOT:
Expand All @@ -147,3 +147,10 @@ void alpaca_key(struct device *dev, int key, bool asserted)
break;
}
}

const struct control_ops alpaca_ops = {
.open = alpaca_open,
.power = alpaca_power,
.usb = alpaca_usb,
.key = alpaca_key,
};
13 changes: 0 additions & 13 deletions alpaca.h

This file was deleted.

36 changes: 21 additions & 15 deletions cdb_assist.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#include <unistd.h>

#include "cdba-server.h"
#include "cdb_assist.h"
#include "device.h"

struct cdb_assist {
char serial[9];
Expand Down Expand Up @@ -89,6 +89,8 @@ enum {
STATE_num_num_m,
};

static void cdb_set_voltage(struct cdb_assist *cdb, unsigned mV);

static void cdb_parser_bool(struct cdb_assist *cdb, const char *key, bool set)
{
static const char *sz_keys[] = { "vbat", "btn1", "btn2", "btn3", "vbus" };
Expand Down Expand Up @@ -271,7 +273,7 @@ static int cdb_ctrl_write(struct cdb_assist *cdb, const char *buf, size_t len)
return write(cdb->control_tty, buf, len);
}

void *cdb_assist_open(struct device *dev)
static void *cdb_assist_open(struct device *dev)
{
struct cdb_assist *cdb;
int ret;
Expand All @@ -293,7 +295,7 @@ void *cdb_assist_open(struct device *dev)
return cdb;
}

void cdb_assist_close(struct device *dev)
static void cdb_assist_close(struct device *dev)
{
struct cdb_assist *cdb = dev->cdb;

Expand All @@ -309,14 +311,14 @@ static void cdb_power(struct cdb_assist *cdb, bool on)
cdb_ctrl_write(cdb, &cmd[on], 1);
}

void cdb_vbus(struct cdb_assist *cdb, bool on)
static void cdb_vbus(struct cdb_assist *cdb, bool on)
{
const char cmd[] = "vV";

cdb_ctrl_write(cdb, &cmd[on], 1);
}

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

Expand All @@ -325,23 +327,18 @@ int cdb_assist_power(struct device *dev, bool on)
return 0;
}

void cdb_assist_usb(struct device *dev, bool on)
static void cdb_assist_usb(struct device *dev, bool on)
{
cdb_vbus(dev->cdb, on);
}

void cdb_gpio(struct cdb_assist *cdb, int gpio, bool on)
static void cdb_gpio(struct cdb_assist *cdb, int gpio, bool on)
{
const char *cmd[] = { "aA", "bB", "cC" };
cdb_ctrl_write(cdb, &cmd[gpio][on], 1);
}

unsigned int cdb_vref(struct cdb_assist *cdb)
{
return cdb->vref;
}

void cdb_assist_print_status(struct device *dev)
static void cdb_assist_print_status(struct device *dev)
{
struct cdb_assist *cdb = dev->cdb;
char buf[128];
Expand All @@ -360,7 +357,7 @@ void cdb_assist_print_status(struct device *dev)
cdba_send_buf(MSG_STATUS_UPDATE, n, buf);
}

void cdb_set_voltage(struct cdb_assist *cdb, unsigned mV)
static void cdb_set_voltage(struct cdb_assist *cdb, unsigned mV)
{
char buf[20];
int n;
Expand All @@ -369,7 +366,7 @@ void cdb_set_voltage(struct cdb_assist *cdb, unsigned mV)
cdb_ctrl_write(cdb, buf, n);
}

void cdb_assist_key(struct device *dev, int key, bool asserted)
static void cdb_assist_key(struct device *dev, int key, bool asserted)
{
struct cdb_assist *cdb = dev->cdb;

Expand All @@ -382,3 +379,12 @@ void cdb_assist_key(struct device *dev, int key, bool asserted)
break;
}
}

const struct control_ops cdb_assist_ops = {
.open = cdb_assist_open,
.close = cdb_assist_close,
.power = cdb_assist_power,
.print_status = cdb_assist_print_status,
.usb = cdb_assist_usb,
.key = cdb_assist_key,
};
23 changes: 0 additions & 23 deletions cdb_assist.h

This file was deleted.

11 changes: 11 additions & 0 deletions cdba-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,17 @@ int main(int argc, char **argv)

done:

/* if we got here, stdin/out/err might be not accessible anymore */
ret = open("/dev/null", O_RDWR);
if (ret >= 0) {
close(STDIN_FILENO);
dup2(ret, STDIN_FILENO);
close(STDOUT_FILENO);
dup2(ret, STDOUT_FILENO);
close(STDERR_FILENO);
dup2(ret, STDERR_FILENO);
}

if (selected_device)
device_close(selected_device);

Expand Down
25 changes: 20 additions & 5 deletions conmux.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#include <unistd.h>

#include "cdba-server.h"
#include "conmux.h"
#include "device.h"

extern int h_errno;

Expand Down Expand Up @@ -224,7 +224,7 @@ static int conmux_data(int fd, void *data)
return 0;
}

void *conmux_open(struct device *dev)
static void *conmux_open(struct device *dev)
{
struct addrinfo hints = {0}, *addrs, *addr;
struct conmux_response resp = {};
Expand Down Expand Up @@ -297,7 +297,7 @@ void *conmux_open(struct device *dev)
return conmux;
}

int conmux_power_on(struct device *dev)
static int conmux_power_on(struct device *dev)
{
struct conmux *conmux = dev->cdb;
char sz[] = "~$hardreset\n";
Expand All @@ -317,17 +317,32 @@ static int conmux_power_off(struct device *dev)
return write(conmux->fd, sz, sizeof(sz));
}

int conmux_power(struct device *dev, bool on)
static int conmux_power(struct device *dev, bool on)
{
if (on)
return conmux_power_on(dev);
else
return conmux_power_off(dev);
}

int conmux_write(struct device *dev, const void *buf, size_t len)
static int conmux_write(struct device *dev, const void *buf, size_t len)
{
struct conmux *conmux = dev->cdb;

return write(conmux->fd, buf, len);
}

static void *conmux_console_open(struct device *dev)
{
return dev->cdb;
}

const struct control_ops conmux_ops = {
.open = conmux_open,
.power = conmux_power,
};

const struct console_ops conmux_console_ops = {
.open = conmux_console_open,
.write = conmux_write,
};
12 changes: 0 additions & 12 deletions conmux.h

This file was deleted.

37 changes: 29 additions & 8 deletions console.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@
#include <sys/stat.h>

#include <err.h>
#include <stdlib.h>
#include <unistd.h>

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

struct console {
int console_fd;
struct termios console_tios;
};

static int console_data(int fd, void *data)
{
char buf[128];
Expand All @@ -51,21 +57,36 @@ static int console_data(int fd, void *data)
return 0;
}

void console_open(struct device *device)
static void *console_open(struct device *device)
{
device->console_fd = tty_open(device->console_dev, &device->console_tios);
if (device->console_fd < 0)
struct console *console;

console = calloc(1, sizeof(*console));
console->console_fd = tty_open(device->console_dev, &console->console_tios);
if (console->console_fd < 0)
err(1, "failed to open %s", device->console_dev);

watch_add_readfd(device->console_fd, console_data, device);
watch_add_readfd(console->console_fd, console_data, device);

return console;
}

int console_write(struct device *device, const void *buf, size_t len)
static int console_write(struct device *device, const void *buf, size_t len)
{
return write(device->console_fd, buf, len);;
struct console *console = device->console;

return write(console->console_fd, buf, len);;
}

void console_send_break(struct device *device)
static void console_send_break(struct device *device)
{
tcsendbreak(device->console_fd, 0);
struct console *console = device->console;

tcsendbreak(console->console_fd, 0);
}

const struct console_ops console_ops = {
.open = console_open,
.write = console_write,
.send_break = console_send_break,
};
10 changes: 0 additions & 10 deletions console.h

This file was deleted.

Loading

0 comments on commit fbdc677

Please sign in to comment.