-
Notifications
You must be signed in to change notification settings - Fork 2
/
novena-bluetooth-reset.c
71 lines (55 loc) · 1.61 KB
/
novena-bluetooth-reset.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <stdint.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/input.h>
static void kick_bluetooth(void) {
static int event_count = 0;
fprintf(stderr, "%d: Resetting bluetooth\n", event_count++);
system("/bin/hciconfig hci0 down");
system("/bin/hciconfig hci0 up");
}
static int open_by_name(const char *name) {
DIR *devinput = opendir("/dev/input");
char devname[256];
struct dirent *fil;
int fd = -1;
while ((fil = readdir(devinput)) != NULL) {
char fullname[256];
snprintf(fullname, sizeof(fullname)-1, "%s/%s",
"/dev/input",fil->d_name);
fd = open(fullname, O_RDONLY);
if (fd == -1)
continue;
ioctl(fd, EVIOCGNAME(sizeof(devname)), devname);
if (!strcmp(name, devname))
break;
close(fd);
}
closedir(devinput);
return fd;
}
int main(int argc, char **argv) {
int fd = open_by_name("20b8000.kpp");
struct input_event event;
if (-1 == fd) {
perror("Unable to open 20b8000.kpp");
return 1;
}
while (read(fd, &event, sizeof(event)) == sizeof(event)) {
if ((event.type == EV_KEY)
&& (event.code == KEY_CONFIG)
&& (event.value)) {
kick_bluetooth();
}
}
close(fd);
return 0;
}