forked from quvox/raw_ble_advertise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbletool.c
288 lines (241 loc) · 5.51 KB
/
bletool.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* BLE advertise and scan tool
* t-kubo @ Zettant Inc.
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <getopt.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#define DEVICE_NAME "hci0"
#define DEFAULT_ADV_HEADER "1F0201060303AAFE1716AAFE80"
static struct hci_filter ofilter;
static volatile int signal_received = 0;
static void sigint_handler(int sig)
{
signal_received = sig;
}
static void hex_dump(char *pref, int width, unsigned char *buf, int len)
{
register int i, n;
for (i = 0, n = 1; i < len; i++, n++) {
if (n == 1)
printf("%s", pref);
printf("%2.2X ", buf[i]);
if (n == width) {
printf("\n");
n = 0;
}
}
if (i && n != 1)
printf("\n");
}
static int open_device(char *dev_name)
{
int dev_id = hci_devid(dev_name);
if (dev_id < 0)
dev_id = hci_get_route(NULL);
int dd = hci_open_dev(dev_id);
if (dd < 0) {
perror("Could not open device");
exit(1);
}
return dd;
}
void ctrl_command(uint8_t ogf, uint16_t ocf, char *data)
{
unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr = buf, tmp[2];
struct hci_filter flt;
int i, len, dd;
dd = open_device(DEVICE_NAME);
len = (int) (strlen(data) / 2);
for (i = 0; i < len; i++) {
memcpy(tmp, &data[i * 2], 2);
*ptr++ = (uint8_t) strtol((const char *) tmp, NULL, 16);
}
/* Setup filter */
hci_filter_clear(&flt);
hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
hci_filter_all_events(&flt);
if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
hci_close_dev(dd);
perror("HCI filter setup failed");
exit(EXIT_FAILURE);
}
if (hci_send_cmd(dd, ogf, ocf, len, buf) < 0) {
hci_close_dev(dd);
perror("Send failed");
exit(EXIT_FAILURE);
}
hci_close_dev(dd);
}
void configure(uint16_t min_interval, uint16_t max_interval)
{
char data[31];
sprintf(data, "%04X%04X0000000000000000000700",
htons(min_interval), htons(max_interval));
ctrl_command(0x08, 0x0006, data);
}
void advertise_on()
{
ctrl_command(0x08, 0x000a, "01");
}
void advertise_off()
{
ctrl_command(0x08, 0x000a, "00");
}
void set_advertisement_data(char *data)
{
int i;
char alldata[64];
sprintf(alldata, "%s%s", DEFAULT_ADV_HEADER, data);
for (i = strlen(alldata); i < 64; i++) {
alldata[i] = '0';
}
ctrl_command(0x08, 0x0008, alldata);
}
static u_char recvbuf[HCI_MAX_EVENT_SIZE];
int read_advertise(int dd, uint8_t * data, int datalen)
{
int len;
evt_le_meta_event *meta;
le_advertising_info *info;
unsigned char *ptr;
while ((len = read(dd, recvbuf, sizeof(recvbuf))) < 0) {
if (errno == EINTR && signal_received == SIGINT) {
return 0;
}
if (errno == EAGAIN || errno == EINTR)
continue;
}
ptr = recvbuf + (1 + HCI_EVENT_HDR_SIZE);
len -= (1 + HCI_EVENT_HDR_SIZE);
meta = (void *) ptr;
info = (le_advertising_info *) (meta->data + 1);
memcpy(data, info->data, datalen);
return len;
}
int print_advertising_devices(int dd)
{
struct sigaction sa;
unsigned char dat[31];
memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_NOCLDSTOP;
sa.sa_handler = sigint_handler;
sigaction(SIGINT, &sa, NULL);
while (1) {
if (read_advertise(dd, dat, 31) == 0)
break;
hex_dump(" ", 40, dat, 31);
}
return 0;
}
void lescan_close(int dd)
{
uint8_t filter_dup = 0;
if (dd == -1) {
dd = open_device(DEVICE_NAME);
} else {
setsockopt(dd, SOL_HCI, HCI_FILTER, &ofilter,
sizeof(ofilter));
}
int err = hci_le_set_scan_enable(dd, 0x00, filter_dup, 1000);
if (err < 0) {
perror("Disable scan failed");
exit(1);
}
hci_close_dev(dd);
}
int lescan_setup()
{
int err, dd;
uint8_t own_type = 0x00;
uint8_t scan_type = 0x00; // passive
uint8_t filter_policy = 0x00;
uint16_t interval = htobs(0x0010);
uint16_t window = htobs(0x0010);
uint8_t filter_dup = 0;
dd = open_device(DEVICE_NAME);
err = hci_le_set_scan_parameters(dd, scan_type, interval, window,
own_type, filter_policy, 1000);
if (err < 0) {
lescan_close(-1);
perror("Set scan parameters failed");
exit(1);
}
err = hci_le_set_scan_enable(dd, 0x01, filter_dup, 1000);
if (err < 0) {
hci_close_dev(dd);
perror("Enable scan failed");
exit(1);
}
struct hci_filter nf;
socklen_t olen;
olen = sizeof(ofilter);
if (getsockopt(dd, SOL_HCI, HCI_FILTER, &ofilter, &olen) < 0) {
hci_close_dev(dd);
printf("Could not get socket options\n");
return -1;
}
hci_filter_clear(&nf);
hci_filter_set_ptype(HCI_EVENT_PKT, &nf);
hci_filter_set_event(EVT_LE_META_EVENT, &nf);
if (setsockopt(dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf)) < 0) {
printf("Could not set socket options\n");
return -1;
}
return dd;
}
static void usage(void)
{
printf("bletool\n");
printf("Usage:\n" "\tbletool [options] [hex_string to send]\n");
printf("Options:\n"
"\t--help\tDisplay help\n"
"\t-r\tReceive mode\n" "\t-s hex_string\tSend data mode\n");
}
static struct option main_options[] = {
{"help", 0, 0, 'h'},
{"read", 1, 0, 'r'},
{"send", 1, 0, 's'},
{0, 0, 0, 0}
};
int main(int argc, char **argv)
{
int mode = 1, opt;
char *send_data;
while ((opt =
getopt_long(argc, argv, "r+s:h", main_options,
NULL)) != -1) {
switch (opt) {
case 'r':
mode = 1; // receive mode
break;
case 's':
mode = 2;
send_data = optarg;
break;
case 'h':
default:
usage();
exit(0);
}
}
if (mode == 1) {
int dd = lescan_setup();
print_advertising_devices(dd);
lescan_close(dd);
} else {
configure(32, 64);
set_advertisement_data(send_data);
advertise_on();
sleep(1);
advertise_off();
}
}