Skip to content

Commit cdee00d

Browse files
authored
Merge pull request #83 from stephan-gh/keys
cdba: Add keys to remotely press power or fastboot key
2 parents 5bade34 + 3922dc7 commit cdee00d

File tree

5 files changed

+75
-6
lines changed

5 files changed

+75
-6
lines changed

cdba-server.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,28 @@ static void msg_fastboot_continue(void)
9999
cdba_send(MSG_FASTBOOT_CONTINUE);
100100
}
101101

102+
static void msg_key_release(void *data)
103+
{
104+
int key = (int)(uintptr_t)data;
105+
106+
device_key(selected_device, key, false);
107+
}
108+
109+
static void msg_key_press(const void *data, size_t len)
110+
{
111+
const struct key_press *press = data;
112+
113+
if (len != sizeof(*press))
114+
return;
115+
116+
if (press->state == KEY_PRESS_PULSE) {
117+
device_key(selected_device, press->key, true);
118+
watch_timer_add(100, msg_key_release, (void*)(uintptr_t)press->key);
119+
} else {
120+
device_key(selected_device, press->key, !!press->state);
121+
}
122+
}
123+
102124
void cdba_send_buf(int type, size_t len, const void *buf)
103125
{
104126
struct msg msg = {
@@ -185,6 +207,9 @@ static int handle_stdin(int fd, void *buf)
185207
case MSG_FASTBOOT_CONTINUE:
186208
msg_fastboot_continue();
187209
break;
210+
case MSG_KEY_PRESS:
211+
msg_key_press(msg->data, msg->len);
212+
break;
188213
default:
189214
fprintf(stderr, "unk %d len %d\n", msg->type, msg->len);
190215
exit(1);

cdba.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,25 @@ static int cdba_send_buf(int fd, int type, size_t len, const void *buf)
148148
return ret < 0 ? ret : 0;
149149
}
150150

151+
static int cdba_send_key(int fd, int key, uint8_t state)
152+
{
153+
struct key_press press = {
154+
.key = key,
155+
.state = state,
156+
};
157+
158+
return cdba_send_buf(fd, MSG_KEY_PRESS, sizeof(press), &press);
159+
}
160+
161+
static int cdba_toggle_key(int fd, int key, bool key_state[DEVICE_KEY_COUNT])
162+
{
163+
key_state[key] = !key_state[key];
164+
return cdba_send_key(fd, key, key_state[key]);
165+
}
166+
151167
static int tty_callback(int *ssh_fds)
152168
{
169+
static bool key_state[DEVICE_KEY_COUNT];
153170
static const char ctrl_a = 0x1;
154171
static bool special;
155172
char buf[32];
@@ -189,6 +206,18 @@ static int tty_callback(int *ssh_fds)
189206
case 'B':
190207
cdba_send(ssh_fds[0], MSG_SEND_BREAK);
191208
break;
209+
case 'o':
210+
cdba_send_key(ssh_fds[0], DEVICE_KEY_POWER, KEY_PRESS_PULSE);
211+
break;
212+
case 'O':
213+
cdba_toggle_key(ssh_fds[0], DEVICE_KEY_POWER, key_state);
214+
break;
215+
case 'f':
216+
cdba_send_key(ssh_fds[0], DEVICE_KEY_FASTBOOT, KEY_PRESS_PULSE);
217+
break;
218+
case 'F':
219+
cdba_toggle_key(ssh_fds[0], DEVICE_KEY_FASTBOOT, key_state);
220+
break;
192221
}
193222

194223
special = false;

cdba.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,24 @@ enum {
3131
MSG_LIST_DEVICES,
3232
MSG_BOARD_INFO,
3333
MSG_FASTBOOT_CONTINUE,
34+
MSG_KEY_PRESS,
35+
};
36+
37+
struct key_press {
38+
uint8_t key;
39+
uint8_t state;
40+
} __packed;
41+
42+
enum {
43+
DEVICE_KEY_FASTBOOT,
44+
DEVICE_KEY_POWER,
45+
DEVICE_KEY_COUNT
46+
};
47+
48+
enum {
49+
KEY_PRESS_RELEASE,
50+
KEY_PRESS_PRESS,
51+
KEY_PRESS_PULSE,
3452
};
3553

3654
#endif

device.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static void device_impl_power(struct device *device, bool on)
163163
device_control(device, power, on);
164164
}
165165

166-
static void device_key(struct device *device, int key, bool asserted)
166+
void device_key(struct device *device, int key, bool asserted)
167167
{
168168
if (device_has_control(device, key))
169169
device_control(device, key, key, asserted);

device.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define __DEVICE_H__
33

44
#include <termios.h>
5+
#include "cdba.h"
56
#include "list.h"
67

78
struct cdb_assist;
@@ -76,6 +77,7 @@ struct device *device_open(const char *board,
7677
const char *username);
7778
void device_close(struct device *dev);
7879
int device_power(struct device *device, bool on);
80+
void device_key(struct device *device, int key, bool asserted);
7981

8082
void device_status_enable(struct device *device);
8183
void device_usb(struct device *device, bool on);
@@ -93,11 +95,6 @@ void device_info(const char *username, const void *data, size_t dlen);
9395
void device_fastboot_continue(struct device *device);
9496
bool device_is_running(struct device *device);
9597

96-
enum {
97-
DEVICE_KEY_FASTBOOT,
98-
DEVICE_KEY_POWER,
99-
};
100-
10198
extern const struct control_ops alpaca_ops;
10299
extern const struct control_ops cdb_assist_ops;
103100
extern const struct control_ops conmux_ops;

0 commit comments

Comments
 (0)