-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlibminibt.c
836 lines (711 loc) · 17.8 KB
/
libminibt.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
/* Copyright 2020 Dexter Gerig <dexgerig@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <endian.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <unistd.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/l2cap.h>
#include "libminibt.h"
#include "stream_macros.h"
static inline void hci_filter_clear(struct hci_filter *f)
{
memset(f, 0, sizeof(*f));
}
static inline void hci_filter_all_ptypes(struct hci_filter *f)
{
memset((void *) &f->type_mask, 0xff, sizeof(f->type_mask));
}
static inline void hci_filter_all_events(struct hci_filter *f)
{
memset((void *) f->event_mask, 0xff, sizeof(f->event_mask));
}
int hci_open_raw_dev(int dev_id)
{
struct sockaddr_hci a;
int dd, err;
/* Check for valid device id */
if (dev_id < 0) {
errno = ENODEV;
return -1;
}
/* Create HCI socket */
dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
if (dd < 0)
return dd;
/* Set filters */
struct hci_filter flt;
hci_filter_clear(&flt);
hci_filter_all_ptypes(&flt);
hci_filter_all_events(&flt);
if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
printf("Can't set filter\n");
goto failed;
}
/* Bind socket to the HCI device */
memset(&a, 0, sizeof(a));
a.hci_family = AF_BLUETOOTH;
a.hci_dev = dev_id;
if (bind(dd, (struct sockaddr *) &a, sizeof(a)) < 0) {
printf("Can't bind to socket\n");
goto failed;
}
return dd;
failed:
err = errno;
close(dd);
errno = err;
return -1;
}
int hci_open_control_dev(int dev_id)
{
struct sockaddr_hci a;
int dd, err;
/* Check for valid device id */
if (dev_id < 0) {
errno = ENODEV;
return -1;
}
/* Create HCI socket */
dd = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
if (dd < 0)
return dd;
/* Bind socket to the HCI device */
memset(&a, 0, sizeof(a));
a.hci_family = AF_BLUETOOTH;
a.hci_dev = HCI_DEV_NONE;
a.hci_channel = HCI_CHANNEL_CONTROL;
if (bind(dd, (struct sockaddr *) &a, sizeof(a)) < 0) {
err = errno;
printf("Can't bind to socket\n");
errno = err;
goto failed;
}
return dd;
failed:
err = errno;
close(dd);
errno = err;
return -1;
}
int get_device_handle(int raw_sock)
{
uint8_t *buf = malloc(HCI_MAX_FRAME_SIZE);
ssize_t err = 0;
while (1) {
while ((err = recv(raw_sock, buf, HCI_MAX_FRAME_SIZE, 0)) < 0 && (errno == EINTR || errno == EAGAIN))
;
if (err < 0) {
err = errno;
printf("recv failed: %s\n", strerror(errno));
free(buf);
errno = err;
return -1;
}
if (buf[0] != HCI_EVENT_PKT)
continue;
if (buf[1] != 0x03) // HCI_CONNECTION_COMPLETE_EVENT
continue;
int handle = (buf[5] * 0x100) | buf[4];
free(buf);
return handle;
}
}
int hci_send_mgmt_cmd(int ctrl_sock, uint16_t cmd, uint16_t hci_dev, void *param, uint16_t len)
{
struct mgmt_cmd a;
struct iovec iv[2];
int ivn;
a.cmd = htole16(cmd);
a.hci_dev = htole16(hci_dev);
a.len = htole16(len);
iv[0].iov_base = &a;
iv[0].iov_len = sizeof(a);
ivn = 1;
if (len) {
iv[1].iov_base = param;
iv[1].iov_len = len;
ivn = 2;
}
while (writev(ctrl_sock, iv, ivn) < 0) {
if (errno == EAGAIN || errno == EINTR)
continue;
return -1;
}
return 0;
}
int hci_send_cmd(int dd, uint16_t ogf, uint16_t ocf, void *param, uint8_t plen)
{
uint8_t type = HCI_COMMAND_PKT;
hci_command_hdr hc;
struct iovec iv[3];
int ivn;
hc.opcode = htobs(cmd_opcode_pack(ogf, ocf));
hc.plen = plen;
iv[0].iov_base = &type;
iv[0].iov_len = 1;
iv[1].iov_base = &hc;
iv[1].iov_len = HCI_COMMAND_HDR_SIZE;
ivn = 2;
if (plen) {
iv[2].iov_base = param;
iv[2].iov_len = plen;
ivn = 3;
}
while (writev(dd, iv, ivn) < 0) {
if (errno == EAGAIN || errno == EINTR)
continue;
return -1;
}
return 0;
}
#define MTU 0x00F0 // Just to be on the safe side
int hci_send_acl(int dd, uint16_t handle, uint8_t pb_bc, void *param, uint16_t dlen)
{
if (dlen > MTU) {
printf("hci_send_acl: dlen bigger than MTU\n");
return -1;
}
uint8_t type = HCI_ACLDATA_PKT;
hci_acl_hdr ha;
struct iovec iv[3];
int ivn;
ha.handle = htobs(acl_handle_pack(handle, pb_bc));
ha.dlen = htobs(dlen);
iv[0].iov_base = &type;
iv[0].iov_len = 1;
iv[1].iov_base = &ha;
iv[1].iov_len = HCI_ACL_HDR_SIZE;
ivn = 2;
if (dlen) {
iv[2].iov_base = param;
iv[2].iov_len = dlen;
ivn = 3;
}
while (writev(dd, iv, ivn) < 0) {
if (errno == EAGAIN || errno == EINTR)
continue;
return -1;
}
return 0;
}
int send_acl_packet(int dd, uint16_t handle, void* param, int len)
{
int ret = 0;
if (dd < 0) {
printf("send_acl_packet: Invalid socket\n");
return -1;
}
if (param == NULL || len == 0) {
printf("send_acl_packet: No data to send\n");
return -1;
}
if (len < 0 || len > 0xFFFF) {
printf("send_acl_packet: Invalid length\n");
return -1;
}
if (len <= MTU) {
return hci_send_acl(dd, handle, ACL_START, param, len);
}
ret = hci_send_acl(dd, handle, ACL_START, param, MTU);
param += MTU;
if (ret)
return ret;
int rem = len - MTU;
while (rem > MTU) {
int ret = hci_send_acl(dd, handle, ACL_CONT, param, MTU);
if (ret)
return ret;
rem -= MTU;
param += MTU;
}
if (rem)
ret = hci_send_acl(dd, handle, ACL_CONT, param, rem);
return ret;
}
int await_mgmt_response(int ctrl_sock, int hci_dev, void** ret_buf, int* ret_size)
{
struct mgmt_evt* evt = malloc(sizeof(struct mgmt_evt) + 0x1000);
memset(evt, 0x00, sizeof(struct mgmt_evt) + 0x1000);
ssize_t err = 0;
if (ret_buf)
*ret_buf = NULL;
if (ret_size)
*ret_size = 0;
while (1) {
while ((err = recv(ctrl_sock, evt, sizeof(struct mgmt_evt) + 0x1000, 0)) < 0 && (errno == EINTR || errno == EAGAIN))
;
if (err < 0) {
err = errno;
printf("recv failed: %s\n", strerror(errno));
free(evt);
errno = err;
return -1;
}
if (htole16(evt->hci_dev) == hci_dev)
break;
}
int evt_code = htole16(evt->evt);
if ((ret_buf != NULL) && ret_size && htole16(evt->len)) {
*ret_size = htole16(evt->len);
*ret_buf = malloc(*ret_size);
memcpy(*ret_buf, evt->param, *ret_size);
}
free(evt);
return evt_code;
}
#define HCI_WRITE_SCAN_ENABLE_COMMAND 0x0C1A
#define HCI_WRITE_CURRENT_IAC_LAP_COMMAND 0x0C3A
int await_command_complete(int raw_sock, uint16_t ogf, uint16_t ocf, void** ret_buf, int* ret_size)
{
uint8_t *buf = malloc(HCI_MAX_FRAME_SIZE);
ssize_t err = 0;
uint16_t opcode = htobs(cmd_opcode_pack(ogf, ocf));
if (ret_buf)
*ret_buf = NULL;
if (ret_size)
*ret_size = 0;
while (1) {
while ((err = recv(raw_sock, buf, HCI_MAX_FRAME_SIZE, 0)) < 0 && (errno == EINTR || errno == EAGAIN))
;
if (err < 0) {
err = errno;
printf("recv failed: %s\n", strerror(errno));
free(buf);
errno = err;
return -1;
}
if (buf[0] != HCI_EVENT_PKT || buf[1] != 0x0E)
continue;
if (buf[2] != 0x04) //TODO: Remove later
continue;
if (((buf[5] * 0x100) | buf[4]) != opcode)
continue;
switch (opcode) {
case HCI_WRITE_SCAN_ENABLE_COMMAND:
printf("HCI_WRITE_SCAN_ENABLE_COMMAND received\n");
if (buf[6] == 0) {
free(buf);
return 0;
} else {
err = buf[6];
free(buf);
return err;
}
break;
case HCI_WRITE_CURRENT_IAC_LAP_COMMAND:
printf("HCI_WRITE_CURRENT_IAC_LAP_COMMAND received\n");
if (buf[6] == 0) {
free(buf);
return 0;
} else {
err = buf[6];
free(buf);
return err;
}
break;
default:
printf("Unsupported opcode 0x%04X\n", opcode);
free(buf);
return -1;
}
free(buf);
return 0;
}
}
int await_response(int raw_sock, uint16_t await_msg)
{
uint8_t *buf = malloc(HCI_MAX_FRAME_SIZE);
ssize_t err = 0;
while (1) {
while ((err = recv(raw_sock, buf, HCI_MAX_FRAME_SIZE, 0)) < 0 && (errno == EINTR || errno == EAGAIN))
;
if (err < 0) {
err = errno;
printf("recv failed: %s\n", strerror(errno));
free(buf);
errno = err;
return -1;
}
if (buf[0] != HCI_ACLDATA_PKT)
continue;
//TODO: These blindly trust packet lengths, refactor to not do that.
hci_acl_hdr *hdr = (hci_acl_hdr*)&buf[1];
int size_left = btohs(hdr->dlen);
uint8_t *l2_head = &buf[1 + sizeof(hci_acl_hdr)];
while (size_left > 0) {
struct l2cap_packet *pkt = (struct l2cap_packet*)l2_head;
int pkt_len = L2CAP_HEADER_LENGTH + le16toh(pkt->length);
size_left -= pkt_len;
l2_head += pkt_len;
uint8_t *payload_head = (uint8_t*)&pkt->payload;
while (pkt_len > 0) {
struct l2cap_payload *payload = (struct l2cap_payload*)payload_head;
int payload_len = L2CAP_PAYLOAD_LENGTH + le16toh(payload->length);
pkt_len -= payload_len;
payload_head += payload_len;
if (payload->opcode != 0x03) // L2CAP_CONNECTION_RESPONSE
continue;
uint8_t *conn_response = payload->data;
uint16_t dest_cid;
uint16_t src_cid;
uint16_t result;
uint16_t status;
STREAM_TO_UINT16(dest_cid, conn_response);
STREAM_TO_UINT16(src_cid, conn_response);
STREAM_TO_UINT16(result, conn_response);
STREAM_TO_UINT16(status, conn_response);
// shutup gcc
(void)dest_cid;
(void)src_cid;
(void)result;
(void)status;
if (result != await_msg)
continue;
free(buf);
return 0;
}
}
}
}
// Get ready for copy-pasta
int mgmt_power_device(int ctrl_sock, int hci_dev, int power) {
if (ctrl_sock < 0) {
printf("Invalid file handle\n");
return -1;
}
if (power != 0 && power != 1) {
printf("Invalid power setting: %d\n", power);
return -1;
}
uint8_t pwr = power;
if (hci_send_mgmt_cmd(ctrl_sock, 0x0005, hci_dev, &pwr, 1) < 0) {
printf("Failed to send mgmt command\n");
return -1;
}
uint8_t* ret_buf = NULL;
int ret_size = 0;
int evt_code = await_mgmt_response(ctrl_sock, hci_dev, (void**)&ret_buf, &ret_size);
int err = errno;
if (ret_size < 3) {
printf("Got bad event size: %d\n", ret_size);
free(ret_buf);
errno = err;
return -1;
}
if (evt_code != 1) {
printf("Got bad event: %d\n", evt_code);
printf("Status: 0x%X\n", ret_buf[2]);
free(ret_buf);
errno = err;
return -1;
}
if (htole16(*(uint16_t*)&ret_buf[0]) != 0x0005) {
printf("Got event for wrong opcode: 0x%04X\n", htole16(*(uint16_t*)&ret_buf[0]));
free(ret_buf);
errno = err;
return -1;
}
if (ret_size != 3 + 4) {
printf("Got bad event size: %d\n", ret_size);
free(ret_buf);
errno = err;
return -1;
}
if (ret_buf[2] != 0x00) {
printf("Got bad event status: %d\n", ret_buf[2]);
free(ret_buf);
errno = err;
return -1;
}
free(ret_buf);
return 0;
}
int mgmt_set_connectable(int ctrl_sock, int hci_dev, int connectable)
{
if (ctrl_sock < 0) {
printf("Invalid file handle\n");
return -1;
}
if (connectable != 0 && connectable != 1) {
printf("Invalid connectable setting: %d\n", connectable);
return -1;
}
uint8_t cnt = connectable;
if (hci_send_mgmt_cmd(ctrl_sock, 0x0007, hci_dev, &cnt, 1) < 0) {
printf("Failed to send mgmt command\n");
return -1;
}
uint8_t* ret_buf = NULL;
int ret_size = 0;
int evt_code = await_mgmt_response(ctrl_sock, hci_dev, (void**)&ret_buf, &ret_size);
int err = errno;
if (ret_size < 3) {
printf("Got bad event size: %d\n", ret_size);
free(ret_buf);
errno = err;
return -1;
}
if (evt_code != 1) {
printf("Got bad event: %d\n", evt_code);
printf("Status: 0x%X\n", ret_buf[2]);
free(ret_buf);
errno = err;
return -1;
}
if (htole16(*(uint16_t*)&ret_buf[0]) != 0x0007) {
printf("Got event for wrong opcode: 0x%04X\n", htole16(*(uint16_t*)&ret_buf[0]));
free(ret_buf);
errno = err;
return -1;
}
if (ret_size != 3 + 4) {
printf("Got bad event size: %d\n", ret_size);
free(ret_buf);
errno = err;
return -1;
}
if (ret_buf[2] != 0x00) {
printf("Got bad event status: %d\n", ret_buf[2]);
free(ret_buf);
errno = err;
return -1;
}
free(ret_buf);
return 0;
}
int mgmt_set_bondable(int ctrl_sock, int hci_dev, int bondable)
{
if (ctrl_sock < 0) {
printf("Invalid file handle\n");
return -1;
}
if (bondable != 0 && bondable != 1) {
printf("Invalid bondable setting: %d\n", bondable);
return -1;
}
uint8_t bnd = bondable;
if (hci_send_mgmt_cmd(ctrl_sock, 0x0009, hci_dev, &bnd, 1) < 0) {
printf("Failed to send mgmt command\n");
return -1;
}
uint8_t* ret_buf = NULL;
int ret_size = 0;
int evt_code = await_mgmt_response(ctrl_sock, hci_dev, (void**)&ret_buf, &ret_size);
int err = errno;
if (ret_size < 3) {
printf("Got bad event size: %d\n", ret_size);
free(ret_buf);
errno = err;
return -1;
}
if (evt_code != 1) {
printf("Got bad event: %d\n", evt_code);
printf("Status: 0x%X\n", ret_buf[2]);
free(ret_buf);
errno = err;
return -1;
}
if (htole16(*(uint16_t*)&ret_buf[0]) != 0x0009) {
printf("Got event for wrong opcode: 0x%04X\n", htole16(*(uint16_t*)&ret_buf[0]));
free(ret_buf);
errno = err;
return -1;
}
if (ret_size != 3 + 4) {
printf("Got bad event size: %d\n", ret_size);
free(ret_buf);
errno = err;
return -1;
}
if (ret_buf[2] != 0x00) {
printf("Got bad event status: %d\n", ret_buf[2]);
free(ret_buf);
errno = err;
return -1;
}
free(ret_buf);
return 0;
}
int mgmt_set_discoverable(int ctrl_sock, int hci_dev, int discoverable, uint16_t timeout)
{
if (ctrl_sock < 0) {
printf("Invalid file handle\n");
return -1;
}
if (discoverable != 0 && discoverable != 1 && discoverable != 2) {
printf("Invalid discoverable setting: %d\n", discoverable);
return -1;
}
struct {
uint8_t discoverable;
uint16_t timeout;
} __attribute__((packed)) dsc;
dsc.discoverable = discoverable;
dsc.timeout = htole32(timeout);
if (hci_send_mgmt_cmd(ctrl_sock, 0x0006, hci_dev, &dsc, sizeof(dsc)) < 0) {
printf("Failed to send mgmt command\n");
return -1;
}
uint8_t* ret_buf = NULL;
int ret_size = 0;
int evt_code = await_mgmt_response(ctrl_sock, hci_dev, (void**)&ret_buf, &ret_size);
int err = errno;
if (ret_size < 3) {
printf("Got bad event size: %d\n", ret_size);
free(ret_buf);
errno = err;
return -1;
}
if (evt_code != 1) {
printf("Got bad event: %d\n", evt_code);
printf("Status: 0x%X\n", ret_buf[2]);
free(ret_buf);
errno = err;
return -1;
}
if (htole16(*(uint16_t*)&ret_buf[0]) != 0x0006) {
printf("Got event for wrong opcode: 0x%04X\n", htole16(*(uint16_t*)&ret_buf[0]));
free(ret_buf);
errno = err;
return -1;
}
if (ret_size != 3 + 4) {
printf("Got bad event size: %d\n", ret_size);
free(ret_buf);
errno = err;
return -1;
}
if (ret_buf[2] != 0x00) {
printf("Got bad event status: %d\n", ret_buf[2]);
free(ret_buf);
errno = err;
return -1;
}
free(ret_buf);
return 0;
}
int mgmt_set_local_name(int ctrl_sock, int hci_dev, char* name)
{
if (ctrl_sock < 0) {
printf("Invalid file handle\n");
return -1;
}
int name_len = strlen(name) + 1;
if (name_len > 248) {
printf("Device name too long\n");
return -1;
}
char padded_local_name[249 + 11] = {0}; // Plus 11 for the short name, we don't even use it.
memcpy(padded_local_name, name, name_len);
if (hci_send_mgmt_cmd(ctrl_sock, 0x000F, hci_dev, padded_local_name, 249 + 11) < 0) {
printf("Failed to send mgmt command\n");
return -1;
}
uint8_t* ret_buf = NULL;
int ret_size = 0;
int evt_code = await_mgmt_response(ctrl_sock, hci_dev, (void**)&ret_buf, &ret_size);
int err = errno;
if (ret_size < 3) {
printf("Got bad event size: %d\n", ret_size);
free(ret_buf);
errno = err;
return -1;
}
if (evt_code != 1) {
printf("Got bad event: %d\n", evt_code);
printf("Status: 0x%X\n", ret_buf[2]);
free(ret_buf);
errno = err;
return -1;
}
if (htole16(*(uint16_t*)&ret_buf[0]) != 0x000F) {
printf("Got event for wrong opcode: 0x%04X\n", htole16(*(uint16_t*)&ret_buf[0]));
free(ret_buf);
errno = err;
return -1;
}
if (ret_size != 3 + 249 + 11) {
printf("Got bad event size: %d\n", ret_size);
free(ret_buf);
errno = err;
return -1;
}
if (ret_buf[2] != 0x00) {
printf("Got bad event status: %d\n", ret_buf[2]);
free(ret_buf);
errno = err;
return -1;
}
free(ret_buf);
return 0;
}
int bt_set_device_scan(int fd, int status)
{
if (fd < 0) {
printf("Invalid file handle\n");
return -1;
}
if (status != 0 && status != 1 && status != 2 && status != 3) {
printf("Invalid scan status\n");
return -1;
}
uint8_t stat = status;
if (hci_send_cmd(fd, 0x0003, 0x001a, &stat, 1) < 0) { // HCI_Write_Scan_Enable
printf("Failed to send command\n");
return -1;
}
return await_command_complete(fd, 0x0003, 0x001a, NULL, 0);
}
int bt_set_iac_lap(int fd, uint8_t Num_Current_IAC, uint32_t* IAC_LAP)
{
int err = 0;
if (fd < 0) {
printf("Invalid file handle\n");
return -1;
}
if (Num_Current_IAC == 0) {
printf("Can't have zero IACs\n");
return -1;
}
if (IAC_LAP == NULL) {
printf("No IAC_LAP buffer passed\n");
return -1;
}
for (int i = 0; i < Num_Current_IAC; i++) {
uint32_t a = IAC_LAP[i] - 0x9E8B00;
if (a > 0x3f) {
printf("Invalid IAC_LAP: 0x%X at index %d\n", IAC_LAP[i], i);
return -1;
}
}
uint8_t* buf = malloc(1 + (Num_Current_IAC * 3));
buf[0] = Num_Current_IAC;
for (int i = 0; i < Num_Current_IAC; i++) {
uint32_t converted_lap = htole32(IAC_LAP[i]);
memcpy(buf + (i * 3) + 1, ((uint8_t*)&converted_lap), 3);
}
if (hci_send_cmd(fd, 0x0003, 0x003a, buf, 1 + (Num_Current_IAC * 3)) < 0) { // HCI_Write_Current_IAC_LAP
err = errno;
printf("Failed to send command\n");
free(buf);
errno = err;
return -1;
}
free(buf);
return await_command_complete(fd, 0x0003, 0x003a, NULL, 0);
}