-
Notifications
You must be signed in to change notification settings - Fork 0
/
hci_test.c
204 lines (163 loc) · 4 KB
/
hci_test.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* ========================================================================== */
/* */
/* Filename.c */
/* (c) 2001 Author */
/* */
/* Description */
/* */
/* ========================================================================== */
#include <stdio.h>
#include <getopt.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#ifdef ANDROID
#include <termios.h>
#else
#include <sys/termios.h>
#endif
#include <string.h>
#include <signal.h>
int uart_fd = -1;
int debug = 0;
struct termios termios;
unsigned char buffer[1024];
unsigned char hci_reset[] = { 0x01, 0x03, 0x0c, 0x00 };
#define BLUETOOTH_POWER_PATH "/sys/module/bluetooth_power/parameters/power"
static int set_bluetooth_power(int on) {
int ret = -1;
int sz;
const char buffer = (on ? 'Y' : 'N');
int fd = open(BLUETOOTH_POWER_PATH, O_WRONLY);
if (fd == -1) {
printf("Can't open %s for write: %s (%d)\n", BLUETOOTH_POWER_PATH,
strerror(errno), errno);
goto out;
}
sz = write(fd, &buffer, 1);
if (sz != 1) {
printf("Can't write to %s: %s (%d)\n", BLUETOOTH_POWER_PATH,
strerror(errno), errno);
goto out;
}
ret = 0;
out:
if (fd >= 0) close(fd);
return ret;
}
void
dump(unsigned char *out, int len)
{
int i;
for (i = 0; i < len; i++) {
if (i && !(i % 16)) {
fprintf(stderr, "\n");
}
fprintf(stderr, "%02x ", out[i]);
}
fprintf(stderr, "\n");
}
void
hci_send_cmd(unsigned char *buf, int len)
{
if (debug) {
fprintf(stderr, "writing\n");
dump(buf, len);
}
write(uart_fd, buf, len);
}
void
expired(int sig)
{
hci_send_cmd(hci_reset, sizeof(hci_reset));
alarm(4);
}
void read_event(int fd, unsigned char *buffer)
{
int i = 0;
int len = 3;
int count;
while ((count = read(fd, &buffer[i], len)) < len) {
i += count;
len -= count;
}
i += count;
len = buffer[2];
while ((count = read(fd, &buffer[i], len)) < len) {
i += count;
len -= count;
}
if (debug) {
count += i;
fprintf(stderr, "received %d\n", count);
dump(buffer, count);
}
}
typedef struct {
int baud_rate;
int termios_value;
} tBaudRates;
tBaudRates baud_rates[] = {
{ 115200, B115200 },
{ 230400, B230400 },
{ 460800, B460800 },
{ 500000, B500000 },
{ 576000, B576000 },
{ 921600, B921600 },
{ 1000000, B1000000 },
{ 1152000, B1152000 },
{ 1500000, B1500000 },
{ 2000000, B2000000 },
{ 2500000, B2500000 },
{ 3000000, B3000000 },
#ifndef __CYGWIN__
{ 3500000, B3500000 },
{ 4000000, B4000000 }
#endif
};
void proc_reset()
{
signal(SIGALRM, expired);
hci_send_cmd(hci_reset, sizeof(hci_reset));
alarm(4);
read_event(uart_fd, buffer);
alarm(0);
}
void init_uart()
{
tcflush(uart_fd, TCIOFLUSH);
tcgetattr(uart_fd, &termios);
#ifndef __CYGWIN__
cfmakeraw(&termios);
#else
termios.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
| INLCR | IGNCR | ICRNL | IXON);
termios.c_oflag &= ~OPOST;
termios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
termios.c_cflag &= ~(CSIZE | PARENB);
termios.c_cflag |= CS8;
#endif
termios.c_cflag |= CRTSCTS;
tcsetattr(uart_fd, TCSANOW, &termios);
tcflush(uart_fd, TCIOFLUSH);
tcsetattr(uart_fd, TCSANOW, &termios);
tcflush(uart_fd, TCIOFLUSH);
tcflush(uart_fd, TCIOFLUSH);
cfsetospeed(&termios, B115200);
cfsetispeed(&termios, B115200);
tcsetattr(uart_fd, TCSANOW, &termios);
}
int
main (int argc, char **argv)
{
if ((uart_fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY)) <0) {
fprintf(stderr, "port /dev/ttyS1 could not be opened, error %d\n", errno);
exit(1);
}
//set_bluetooth_power(1);
init_uart();
proc_reset();
exit(0);
}