Skip to content

Commit fbdc677

Browse files
authored
Merge pull request #9 from lumag/external
Rework cdba to support controlling device via external program
2 parents f09b3e1 + a0de781 commit fbdc677

18 files changed

+344
-201
lines changed

alpaca.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#include <unistd.h>
4343

4444
#include "cdba-server.h"
45-
#include "alpaca.h"
45+
#include "device.h"
4646

4747
struct alpaca {
4848
int alpaca_fd;
@@ -53,7 +53,7 @@ struct alpaca {
5353
static int alpaca_device_power(struct alpaca *alpaca, int on);
5454
static int alpaca_usb_device_power(struct alpaca *alpaca, int on);
5555

56-
void *alpaca_open(struct device *dev)
56+
static void *alpaca_open(struct device *dev)
5757
{
5858
struct alpaca *alpaca;
5959

@@ -121,22 +121,22 @@ static int alpaca_power_off(struct device *dev)
121121
return 0;
122122
}
123123

124-
int alpaca_power(struct device *dev, bool on)
124+
static int alpaca_power(struct device *dev, bool on)
125125
{
126126
if (on)
127127
return alpaca_power_on(dev);
128128
else
129129
return alpaca_power_off(dev);
130130
}
131131

132-
void alpaca_usb(struct device *dev, bool on)
132+
static void alpaca_usb(struct device *dev, bool on)
133133
{
134134
struct alpaca *alpaca = dev->cdb;
135135

136136
alpaca_usb_device_power(alpaca, on);
137137
}
138138

139-
void alpaca_key(struct device *dev, int key, bool asserted)
139+
static void alpaca_key(struct device *dev, int key, bool asserted)
140140
{
141141
switch (key) {
142142
case DEVICE_KEY_FASTBOOT:
@@ -147,3 +147,10 @@ void alpaca_key(struct device *dev, int key, bool asserted)
147147
break;
148148
}
149149
}
150+
151+
const struct control_ops alpaca_ops = {
152+
.open = alpaca_open,
153+
.power = alpaca_power,
154+
.usb = alpaca_usb,
155+
.key = alpaca_key,
156+
};

alpaca.h

Lines changed: 0 additions & 13 deletions
This file was deleted.

cdb_assist.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
#include <unistd.h>
4343

4444
#include "cdba-server.h"
45-
#include "cdb_assist.h"
45+
#include "device.h"
4646

4747
struct cdb_assist {
4848
char serial[9];
@@ -89,6 +89,8 @@ enum {
8989
STATE_num_num_m,
9090
};
9191

92+
static void cdb_set_voltage(struct cdb_assist *cdb, unsigned mV);
93+
9294
static void cdb_parser_bool(struct cdb_assist *cdb, const char *key, bool set)
9395
{
9496
static const char *sz_keys[] = { "vbat", "btn1", "btn2", "btn3", "vbus" };
@@ -271,7 +273,7 @@ static int cdb_ctrl_write(struct cdb_assist *cdb, const char *buf, size_t len)
271273
return write(cdb->control_tty, buf, len);
272274
}
273275

274-
void *cdb_assist_open(struct device *dev)
276+
static void *cdb_assist_open(struct device *dev)
275277
{
276278
struct cdb_assist *cdb;
277279
int ret;
@@ -293,7 +295,7 @@ void *cdb_assist_open(struct device *dev)
293295
return cdb;
294296
}
295297

296-
void cdb_assist_close(struct device *dev)
298+
static void cdb_assist_close(struct device *dev)
297299
{
298300
struct cdb_assist *cdb = dev->cdb;
299301

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

312-
void cdb_vbus(struct cdb_assist *cdb, bool on)
314+
static void cdb_vbus(struct cdb_assist *cdb, bool on)
313315
{
314316
const char cmd[] = "vV";
315317

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

319-
int cdb_assist_power(struct device *dev, bool on)
321+
static int cdb_assist_power(struct device *dev, bool on)
320322
{
321323
struct cdb_assist *cdb = dev->cdb;
322324

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

328-
void cdb_assist_usb(struct device *dev, bool on)
330+
static void cdb_assist_usb(struct device *dev, bool on)
329331
{
330332
cdb_vbus(dev->cdb, on);
331333
}
332334

333-
void cdb_gpio(struct cdb_assist *cdb, int gpio, bool on)
335+
static void cdb_gpio(struct cdb_assist *cdb, int gpio, bool on)
334336
{
335337
const char *cmd[] = { "aA", "bB", "cC" };
336338
cdb_ctrl_write(cdb, &cmd[gpio][on], 1);
337339
}
338340

339-
unsigned int cdb_vref(struct cdb_assist *cdb)
340-
{
341-
return cdb->vref;
342-
}
343-
344-
void cdb_assist_print_status(struct device *dev)
341+
static void cdb_assist_print_status(struct device *dev)
345342
{
346343
struct cdb_assist *cdb = dev->cdb;
347344
char buf[128];
@@ -360,7 +357,7 @@ void cdb_assist_print_status(struct device *dev)
360357
cdba_send_buf(MSG_STATUS_UPDATE, n, buf);
361358
}
362359

363-
void cdb_set_voltage(struct cdb_assist *cdb, unsigned mV)
360+
static void cdb_set_voltage(struct cdb_assist *cdb, unsigned mV)
364361
{
365362
char buf[20];
366363
int n;
@@ -369,7 +366,7 @@ void cdb_set_voltage(struct cdb_assist *cdb, unsigned mV)
369366
cdb_ctrl_write(cdb, buf, n);
370367
}
371368

372-
void cdb_assist_key(struct device *dev, int key, bool asserted)
369+
static void cdb_assist_key(struct device *dev, int key, bool asserted)
373370
{
374371
struct cdb_assist *cdb = dev->cdb;
375372

@@ -382,3 +379,12 @@ void cdb_assist_key(struct device *dev, int key, bool asserted)
382379
break;
383380
}
384381
}
382+
383+
const struct control_ops cdb_assist_ops = {
384+
.open = cdb_assist_open,
385+
.close = cdb_assist_close,
386+
.power = cdb_assist_power,
387+
.print_status = cdb_assist_print_status,
388+
.usb = cdb_assist_usb,
389+
.key = cdb_assist_key,
390+
};

cdb_assist.h

Lines changed: 0 additions & 23 deletions
This file was deleted.

cdba-server.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,17 @@ int main(int argc, char **argv)
406406

407407
done:
408408

409+
/* if we got here, stdin/out/err might be not accessible anymore */
410+
ret = open("/dev/null", O_RDWR);
411+
if (ret >= 0) {
412+
close(STDIN_FILENO);
413+
dup2(ret, STDIN_FILENO);
414+
close(STDOUT_FILENO);
415+
dup2(ret, STDOUT_FILENO);
416+
close(STDERR_FILENO);
417+
dup2(ret, STDERR_FILENO);
418+
}
419+
409420
if (selected_device)
410421
device_close(selected_device);
411422

conmux.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include <unistd.h>
4242

4343
#include "cdba-server.h"
44-
#include "conmux.h"
44+
#include "device.h"
4545

4646
extern int h_errno;
4747

@@ -224,7 +224,7 @@ static int conmux_data(int fd, void *data)
224224
return 0;
225225
}
226226

227-
void *conmux_open(struct device *dev)
227+
static void *conmux_open(struct device *dev)
228228
{
229229
struct addrinfo hints = {0}, *addrs, *addr;
230230
struct conmux_response resp = {};
@@ -297,7 +297,7 @@ void *conmux_open(struct device *dev)
297297
return conmux;
298298
}
299299

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

320-
int conmux_power(struct device *dev, bool on)
320+
static int conmux_power(struct device *dev, bool on)
321321
{
322322
if (on)
323323
return conmux_power_on(dev);
324324
else
325325
return conmux_power_off(dev);
326326
}
327327

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

332332
return write(conmux->fd, buf, len);
333333
}
334+
335+
static void *conmux_console_open(struct device *dev)
336+
{
337+
return dev->cdb;
338+
}
339+
340+
const struct control_ops conmux_ops = {
341+
.open = conmux_open,
342+
.power = conmux_power,
343+
};
344+
345+
const struct console_ops conmux_console_ops = {
346+
.open = conmux_console_open,
347+
.write = conmux_write,
348+
};

conmux.h

Lines changed: 0 additions & 12 deletions
This file was deleted.

console.c

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,17 @@
3232
#include <sys/stat.h>
3333

3434
#include <err.h>
35+
#include <stdlib.h>
3536
#include <unistd.h>
3637

3738
#include "cdba-server.h"
3839
#include "device.h"
3940

41+
struct console {
42+
int console_fd;
43+
struct termios console_tios;
44+
};
45+
4046
static int console_data(int fd, void *data)
4147
{
4248
char buf[128];
@@ -51,21 +57,36 @@ static int console_data(int fd, void *data)
5157
return 0;
5258
}
5359

54-
void console_open(struct device *device)
60+
static void *console_open(struct device *device)
5561
{
56-
device->console_fd = tty_open(device->console_dev, &device->console_tios);
57-
if (device->console_fd < 0)
62+
struct console *console;
63+
64+
console = calloc(1, sizeof(*console));
65+
console->console_fd = tty_open(device->console_dev, &console->console_tios);
66+
if (console->console_fd < 0)
5867
err(1, "failed to open %s", device->console_dev);
5968

60-
watch_add_readfd(device->console_fd, console_data, device);
69+
watch_add_readfd(console->console_fd, console_data, device);
70+
71+
return console;
6172
}
6273

63-
int console_write(struct device *device, const void *buf, size_t len)
74+
static int console_write(struct device *device, const void *buf, size_t len)
6475
{
65-
return write(device->console_fd, buf, len);;
76+
struct console *console = device->console;
77+
78+
return write(console->console_fd, buf, len);;
6679
}
6780

68-
void console_send_break(struct device *device)
81+
static void console_send_break(struct device *device)
6982
{
70-
tcsendbreak(device->console_fd, 0);
83+
struct console *console = device->console;
84+
85+
tcsendbreak(console->console_fd, 0);
7186
}
87+
88+
const struct console_ops console_ops = {
89+
.open = console_open,
90+
.write = console_write,
91+
.send_break = console_send_break,
92+
};

console.h

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)