-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadapter.c
369 lines (305 loc) · 7.34 KB
/
adapter.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include "adapter.h"
#define HCI_TIMEOUT 1000
static char original_name[HCI_MAX_NAME_LENGTH];
static uint8_t original_class[3];
static uint8_t original_scan_enable;
static uint8_t original_iac[MAX_IAC_LAP][3];
static uint8_t original_iac_num;
static uint8_t original_simple_pairing_mode;
static const char * wiimote_name = "Nintendo RVL-CNT-01";
static const uint32_t wiimote_class = 0x002504;
static const uint8_t wiimote_iac[3] = { 0x00, 0x8B, 0x9E };
int hci_read_scan_enable(int dd, uint8_t * enabled, int to)
{
struct {
uint8_t status;
uint8_t enabled;
} __attribute__ ((packed)) rp;
struct hci_request rq;
memset(&rq, 0, sizeof(rq));
rq.ogf = OGF_HOST_CTL;
rq.ocf = OCF_READ_SCAN_ENABLE;
rq.rparam = &rp;
rq.rlen = sizeof(rp);
if (hci_send_req(dd, &rq, to) < 0)
{
return -1;
}
if (rp.status)
{
errno = EIO;
return -1;
}
*enabled = rp.enabled;
return 0;
}
int hci_write_scan_enable(int dd, uint8_t enabled, int to)
{
struct {
uint8_t enabled;
} __attribute__ ((packed)) cp;
struct {
uint8_t status;
} __attribute__ ((packed)) rp;
struct hci_request rq;
memset(&cp, 0, sizeof(cp));
cp.enabled = enabled;
memset(&rq, 0, sizeof(rq));
rq.ogf = OGF_HOST_CTL;
rq.ocf = OCF_WRITE_SCAN_ENABLE;
rq.cparam = &cp;
rq.clen = sizeof(cp);
rq.rparam = &rp;
rq.rlen = sizeof(rp);
if (hci_send_req(dd, &rq, to) < 0)
{
return -1;
}
if (rp.status)
{
errno = EIO;
return -1;
}
return 0;
}
int set_up_device_name(int dd)
{
int ret;
ret = hci_read_local_name(dd, HCI_MAX_NAME_LENGTH, original_name, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't read device name: %s (%d)\n", strerror(errno), errno);
return -1;
}
ret = hci_write_local_name(dd, wiimote_name, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't write device name: %s (%d)\n", strerror(errno), errno);
return -1;
}
return 0;
}
int restore_device_name(int dd)
{
int ret;
ret = hci_write_local_name(dd, original_name, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't restore device name: %s (%d)\n", strerror(errno), errno);
return -1;
}
return 0;
}
int set_up_device_class(int dd)
{
int ret;
ret = hci_read_class_of_dev(dd, original_class, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't read device class: %s (%d)\n", strerror(errno), errno);
return -1;
}
ret = hci_write_class_of_dev(dd, wiimote_class, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't write device class: %s (%d)\n", strerror(errno), errno);
return -1;
}
return 0;
}
int restore_device_class(int dd)
{
int ret;
uint32_t class_int = 0;
class_int |= original_class[0];
class_int |= original_class[1] << 8;
class_int |= original_class[2] << 16;
ret = hci_write_class_of_dev(dd, class_int, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't restore device class: %s (%d)\n", strerror(errno), errno);
return -1;
}
return 0;
}
int set_up_device_inquiry(int dd)
{
int ret;
ret = hci_read_scan_enable(dd, &original_scan_enable, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't read scan enable: %s (%d)\n", strerror(errno), errno);
return -1;
}
ret = hci_write_scan_enable(dd, SCAN_INQUIRY | SCAN_PAGE, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't write scan enable: %s (%d)\n", strerror(errno), errno);
return -1;
}
ret = hci_read_current_iac_lap(dd, &original_iac_num, (uint8_t *)original_iac, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't read iac: %s (%d)\n", strerror(errno), errno);
return -1;
}
ret = hci_write_current_iac_lap(dd, 1, (uint8_t *)wiimote_iac, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't write iac: %s (%d)\n", strerror(errno), errno);
return -1;
}
return 0;
}
int restore_device_inquiry(int dd)
{
int ret;
ret = hci_write_scan_enable(dd, original_scan_enable, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't restore scan enable: %s (%d)\n", strerror(errno), errno);
return -1;
}
ret = hci_write_current_iac_lap(dd, original_iac_num, (uint8_t *)original_iac, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't restore iac: %s (%d)\n", strerror(errno), errno);
return -1;
}
return 0;
}
int set_up_simple_pairing_mode(int dd)
{
int ret;
ret = hci_read_simple_pairing_mode(dd, &original_simple_pairing_mode, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't read simple pairing mode: %s (%d)\n", strerror(errno), errno);
return -1;
}
if (original_simple_pairing_mode != 0)
{
ret = hci_write_simple_pairing_mode(dd, 0, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't write simple pairing mode: %s (%d)\n", strerror(errno), errno);
return -1;
}
}
return 0;
}
int restore_simple_pairing_mode(int dd)
{
int ret;
if (original_simple_pairing_mode != 0)
{
ret = hci_write_simple_pairing_mode(dd, original_simple_pairing_mode, HCI_TIMEOUT);
if (ret < 0)
{
fprintf(stderr, "Can't restore simple pairing mode: %s (%d)\n", strerror(errno), errno);
return -1;
}
}
return 0;
}
int set_up_device(char * dev_str)
{
int device_id = 0, dd, ret;
dd = hci_open_dev(device_id);
if (dd < 0)
{
fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
device_id, strerror(errno), errno);
return -1;
}
ret = ioctl(dd, HCIDEVDOWN, device_id);
if (ret < 0)
{
fprintf(stderr, "Can't down device hci%d: %s (%d)\n",
device_id, strerror(errno), errno);
}
ret = ioctl(dd, HCIDEVUP, device_id);
if (ret < 0)
{
fprintf(stderr, "Can't init device hci%d: %s (%d)\n",
device_id, strerror(errno), errno);
}
ret = set_up_device_name(dd);
if (ret < 0)
{
printf("Failed to set device name\n");
hci_close_dev(dd);
return -1;
}
ret = set_up_device_class(dd);
if (ret < 0)
{
printf("Failed to set device class\n");
hci_close_dev(dd);
return -1;
}
ret = set_up_device_inquiry(dd);
if (ret < 0)
{
printf("Failed to set device inquiry settings\n");
hci_close_dev(dd);
return -1;
}
ret = set_up_simple_pairing_mode(dd);
if (ret < 0)
{
printf("Failed to set simple pairing mode\n");
printf("Warning: make sure secure simple pairing mode is disabled\n");
}
hci_close_dev(dd);
return 0;
}
int restore_device()
{
int device_id = 0, dd, ret;
dd = hci_open_dev(device_id);
if (dd < 0)
{
fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
device_id, strerror(errno), errno);
return -1;
}
ret = restore_device_name(dd);
if (ret < 0)
{
printf("Failed to restore device name\n");
hci_close_dev(dd);
return -1;
}
ret = restore_device_class(dd);
if (ret < 0)
{
printf("Failed to restore device class\n");
hci_close_dev(dd);
return -1;
}
ret = restore_device_inquiry(dd);
if (ret < 0)
{
printf("Failed to restore device inquiry settings\n");
hci_close_dev(dd);
return -1;
}
ret = restore_simple_pairing_mode(dd);
if (ret < 0)
{
printf("Failed to restore simple pairing mode\n");
hci_close_dev(dd);
return -1;
}
hci_close_dev(dd);
return 0;
}