-
Notifications
You must be signed in to change notification settings - Fork 11
/
bt-hci.c
2217 lines (1831 loc) · 67.4 KB
/
bt-hci.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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* QEMU Bluetooth HCI logic.
*
* Copyright (C) 2007 OpenMoko, Inc.
* Copyright (C) 2008 Andrzej Zaborowski <[email protected]>
*
* 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 2 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 <http://www.gnu.org/licenses/>.
*/
#include "qemu-common.h"
#include "qemu-timer.h"
#include "usb.h"
#include "net.h"
#include "bt.h"
struct bt_hci_s {
uint8_t *(*evt_packet)(void *opaque);
void (*evt_submit)(void *opaque, int len);
void *opaque;
uint8_t evt_buf[256];
uint8_t acl_buf[4096];
int acl_len;
uint16_t asb_handle;
uint16_t psb_handle;
int last_cmd; /* Note: Always little-endian */
struct bt_device_s *conn_req_host;
struct {
int inquire;
int periodic;
int responses_left;
int responses;
QEMUTimer *inquiry_done;
QEMUTimer *inquiry_next;
int inquiry_length;
int inquiry_period;
int inquiry_mode;
#define HCI_HANDLE_OFFSET 0x20
#define HCI_HANDLES_MAX 0x10
struct bt_hci_master_link_s {
struct bt_link_s *link;
void (*lmp_acl_data)(struct bt_link_s *link,
const uint8_t *data, int start, int len);
QEMUTimer *acl_mode_timer;
} handle[HCI_HANDLES_MAX];
uint32_t role_bmp;
int last_handle;
int connecting;
bdaddr_t awaiting_bdaddr[HCI_HANDLES_MAX];
} lm;
uint8_t event_mask[8];
uint16_t voice_setting; /* Notw: Always little-endian */
uint16_t conn_accept_tout;
QEMUTimer *conn_accept_timer;
struct HCIInfo info;
struct bt_device_s device;
};
#define DEFAULT_RSSI_DBM 20
#define hci_from_info(ptr) container_of((ptr), struct bt_hci_s, info)
#define hci_from_device(ptr) container_of((ptr), struct bt_hci_s, device)
struct bt_hci_link_s {
struct bt_link_s btlink;
uint16_t handle; /* Local */
};
/* LMP layer emulation */
#if 0
static void bt_submit_lmp(struct bt_device_s *bt, int length, uint8_t *data)
{
int resp, resplen, error, op, tr;
uint8_t respdata[17];
if (length < 1)
return;
tr = *data & 1;
op = *(data ++) >> 1;
resp = LMP_ACCEPTED;
resplen = 2;
respdata[1] = op;
error = 0;
length --;
if (op >= 0x7c) { /* Extended opcode */
op |= *(data ++) << 8;
resp = LMP_ACCEPTED_EXT;
resplen = 4;
respdata[0] = op >> 8;
respdata[1] = op & 0xff;
length --;
}
switch (op) {
case LMP_ACCEPTED:
/* data[0] Op code
*/
if (length < 1) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
resp = 0;
break;
case LMP_ACCEPTED_EXT:
/* data[0] Escape op code
* data[1] Extended op code
*/
if (length < 2) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
resp = 0;
break;
case LMP_NOT_ACCEPTED:
/* data[0] Op code
* data[1] Error code
*/
if (length < 2) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
resp = 0;
break;
case LMP_NOT_ACCEPTED_EXT:
/* data[0] Op code
* data[1] Extended op code
* data[2] Error code
*/
if (length < 3) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
resp = 0;
break;
case LMP_HOST_CONNECTION_REQ:
break;
case LMP_SETUP_COMPLETE:
resp = LMP_SETUP_COMPLETE;
resplen = 1;
bt->setup = 1;
break;
case LMP_DETACH:
/* data[0] Error code
*/
if (length < 1) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
bt->setup = 0;
resp = 0;
break;
case LMP_SUPERVISION_TIMEOUT:
/* data[0,1] Supervision timeout
*/
if (length < 2) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
resp = 0;
break;
case LMP_QUALITY_OF_SERVICE:
resp = 0;
/* Fall through */
case LMP_QOS_REQ:
/* data[0,1] Poll interval
* data[2] N(BC)
*/
if (length < 3) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
break;
case LMP_MAX_SLOT:
resp = 0;
/* Fall through */
case LMP_MAX_SLOT_REQ:
/* data[0] Max slots
*/
if (length < 1) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
break;
case LMP_AU_RAND:
case LMP_IN_RAND:
case LMP_COMB_KEY:
/* data[0-15] Random number
*/
if (length < 16) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
if (op == LMP_AU_RAND) {
if (bt->key_present) {
resp = LMP_SRES;
resplen = 5;
/* XXX: [Part H] Section 6.1 on page 801 */
} else {
error = HCI_PIN_OR_KEY_MISSING;
goto not_accepted;
}
} else if (op == LMP_IN_RAND) {
error = HCI_PAIRING_NOT_ALLOWED;
goto not_accepted;
} else {
/* XXX: [Part H] Section 3.2 on page 779 */
resp = LMP_UNIT_KEY;
resplen = 17;
memcpy(respdata + 1, bt->key, 16);
error = HCI_UNIT_LINK_KEY_USED;
goto not_accepted;
}
break;
case LMP_UNIT_KEY:
/* data[0-15] Key
*/
if (length < 16) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
memcpy(bt->key, data, 16);
bt->key_present = 1;
break;
case LMP_SRES:
/* data[0-3] Authentication response
*/
if (length < 4) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
break;
case LMP_CLKOFFSET_REQ:
resp = LMP_CLKOFFSET_RES;
resplen = 3;
respdata[1] = 0x33;
respdata[2] = 0x33;
break;
case LMP_CLKOFFSET_RES:
/* data[0,1] Clock offset
* (Slave to master only)
*/
if (length < 2) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
break;
case LMP_VERSION_REQ:
case LMP_VERSION_RES:
/* data[0] VersNr
* data[1,2] CompId
* data[3,4] SubVersNr
*/
if (length < 5) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
if (op == LMP_VERSION_REQ) {
resp = LMP_VERSION_RES;
resplen = 6;
respdata[1] = 0x20;
respdata[2] = 0xff;
respdata[3] = 0xff;
respdata[4] = 0xff;
respdata[5] = 0xff;
} else
resp = 0;
break;
case LMP_FEATURES_REQ:
case LMP_FEATURES_RES:
/* data[0-7] Features
*/
if (length < 8) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
if (op == LMP_FEATURES_REQ) {
resp = LMP_FEATURES_RES;
resplen = 9;
respdata[1] = (bt->lmp_caps >> 0) & 0xff;
respdata[2] = (bt->lmp_caps >> 8) & 0xff;
respdata[3] = (bt->lmp_caps >> 16) & 0xff;
respdata[4] = (bt->lmp_caps >> 24) & 0xff;
respdata[5] = (bt->lmp_caps >> 32) & 0xff;
respdata[6] = (bt->lmp_caps >> 40) & 0xff;
respdata[7] = (bt->lmp_caps >> 48) & 0xff;
respdata[8] = (bt->lmp_caps >> 56) & 0xff;
} else
resp = 0;
break;
case LMP_NAME_REQ:
/* data[0] Name offset
*/
if (length < 1) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
resp = LMP_NAME_RES;
resplen = 17;
respdata[1] = data[0];
respdata[2] = strlen(bt->lmp_name);
memset(respdata + 3, 0x00, 14);
if (respdata[2] > respdata[1])
memcpy(respdata + 3, bt->lmp_name + respdata[1],
respdata[2] - respdata[1]);
break;
case LMP_NAME_RES:
/* data[0] Name offset
* data[1] Name length
* data[2-15] Name fragment
*/
if (length < 16) {
error = HCI_UNSUPPORTED_LMP_PARAMETER_VALUE;
goto not_accepted;
}
resp = 0;
break;
default:
error = HCI_UNKNOWN_LMP_PDU;
/* Fall through */
not_accepted:
if (op >> 8) {
resp = LMP_NOT_ACCEPTED_EXT;
resplen = 5;
respdata[0] = op >> 8;
respdata[1] = op & 0xff;
respdata[2] = error;
} else {
resp = LMP_NOT_ACCEPTED;
resplen = 3;
respdata[0] = op & 0xff;
respdata[1] = error;
}
}
if (resp == 0)
return;
if (resp >> 8) {
respdata[0] = resp >> 8;
respdata[1] = resp & 0xff;
} else
respdata[0] = resp & 0xff;
respdata[0] <<= 1;
respdata[0] |= tr;
}
static void bt_submit_raw_acl(struct bt_piconet_s *net, int length, uint8_t *data)
{
struct bt_device_s *slave;
if (length < 1)
return;
slave = 0;
#if 0
slave = net->slave;
#endif
switch (data[0] & 3) {
case LLID_ACLC:
bt_submit_lmp(slave, length - 1, data + 1);
break;
case LLID_ACLU_START:
#if 0
bt_sumbit_l2cap(slave, length - 1, data + 1, (data[0] >> 2) & 1);
breka;
#endif
default:
case LLID_ACLU_CONT:
break;
}
}
#endif
/* HCI layer emulation */
/* Note: we could ignore endiannes because unswapped handles will still
* be valid as connection identifiers for the guest - they don't have to
* be continuously allocated. We do it though, to preserve similar
* behaviour between hosts. Some things, like the BD_ADDR cannot be
* preserved though (for example if a real hci is used). */
#ifdef HOST_WORDS_BIGENDIAN
# define HNDL(raw) bswap16(raw)
#else
# define HNDL(raw) (raw)
#endif
static const uint8_t bt_event_reserved_mask[8] = {
0xff, 0x9f, 0xfb, 0xff, 0x07, 0x18, 0x00, 0x00,
};
static inline uint8_t *bt_hci_event_start(struct bt_hci_s *hci,
int evt, int len)
{
uint8_t *packet, mask;
int mask_byte;
if (len > 255) {
fprintf(stderr, "%s: HCI event params too long (%ib)\n",
__FUNCTION__, len);
exit(-1);
}
mask_byte = (evt - 1) >> 3;
mask = 1 << ((evt - 1) & 3);
if (mask & bt_event_reserved_mask[mask_byte] & ~hci->event_mask[mask_byte])
return NULL;
packet = hci->evt_packet(hci->opaque);
packet[0] = evt;
packet[1] = len;
return &packet[2];
}
static inline void bt_hci_event(struct bt_hci_s *hci, int evt,
void *params, int len)
{
uint8_t *packet = bt_hci_event_start(hci, evt, len);
if (!packet)
return;
if (len)
memcpy(packet, params, len);
hci->evt_submit(hci->opaque, len + 2);
}
static inline void bt_hci_event_status(struct bt_hci_s *hci, int status)
{
evt_cmd_status params = {
.status = status,
.ncmd = 1,
.opcode = hci->last_cmd,
};
bt_hci_event(hci, EVT_CMD_STATUS, ¶ms, EVT_CMD_STATUS_SIZE);
}
static inline void bt_hci_event_complete(struct bt_hci_s *hci,
void *ret, int len)
{
uint8_t *packet = bt_hci_event_start(hci, EVT_CMD_COMPLETE,
len + EVT_CMD_COMPLETE_SIZE);
evt_cmd_complete *params = (evt_cmd_complete *) packet;
if (!packet)
return;
params->ncmd = 1;
params->opcode = hci->last_cmd;
if (len)
memcpy(&packet[EVT_CMD_COMPLETE_SIZE], ret, len);
hci->evt_submit(hci->opaque, len + EVT_CMD_COMPLETE_SIZE + 2);
}
static void bt_hci_inquiry_done(void *opaque)
{
struct bt_hci_s *hci = (struct bt_hci_s *) opaque;
uint8_t status = HCI_SUCCESS;
if (!hci->lm.periodic)
hci->lm.inquire = 0;
/* The specification is inconsistent about this one. Page 565 reads
* "The event parameters of Inquiry Complete event will have a summary
* of the result from the Inquiry process, which reports the number of
* nearby Bluetooth devices that responded [so hci->responses].", but
* Event Parameters (see page 729) has only Status. */
bt_hci_event(hci, EVT_INQUIRY_COMPLETE, &status, 1);
}
static void bt_hci_inquiry_result_standard(struct bt_hci_s *hci,
struct bt_device_s *slave)
{
inquiry_info params = {
.num_responses = 1,
.bdaddr = BAINIT(&slave->bd_addr),
.pscan_rep_mode = 0x00, /* R0 */
.pscan_period_mode = 0x00, /* P0 - deprecated */
.pscan_mode = 0x00, /* Standard scan - deprecated */
.dev_class[0] = slave->class[0],
.dev_class[1] = slave->class[1],
.dev_class[2] = slave->class[2],
/* TODO: return the clkoff *differenece* */
.clock_offset = slave->clkoff, /* Note: no swapping */
};
bt_hci_event(hci, EVT_INQUIRY_RESULT, ¶ms, INQUIRY_INFO_SIZE);
}
static void bt_hci_inquiry_result_with_rssi(struct bt_hci_s *hci,
struct bt_device_s *slave)
{
inquiry_info_with_rssi params = {
.num_responses = 1,
.bdaddr = BAINIT(&slave->bd_addr),
.pscan_rep_mode = 0x00, /* R0 */
.pscan_period_mode = 0x00, /* P0 - deprecated */
.dev_class[0] = slave->class[0],
.dev_class[1] = slave->class[1],
.dev_class[2] = slave->class[2],
/* TODO: return the clkoff *differenece* */
.clock_offset = slave->clkoff, /* Note: no swapping */
.rssi = DEFAULT_RSSI_DBM,
};
bt_hci_event(hci, EVT_INQUIRY_RESULT_WITH_RSSI,
¶ms, INQUIRY_INFO_WITH_RSSI_SIZE);
}
static void bt_hci_inquiry_result(struct bt_hci_s *hci,
struct bt_device_s *slave)
{
if (!slave->inquiry_scan || !hci->lm.responses_left)
return;
hci->lm.responses_left --;
hci->lm.responses ++;
switch (hci->lm.inquiry_mode) {
case 0x00:
bt_hci_inquiry_result_standard(hci, slave);
return;
case 0x01:
bt_hci_inquiry_result_with_rssi(hci, slave);
return;
default:
fprintf(stderr, "%s: bad inquiry mode %02x\n", __FUNCTION__,
hci->lm.inquiry_mode);
exit(-1);
}
}
static void bt_hci_mod_timer_1280ms(QEMUTimer *timer, int period)
{
qemu_mod_timer(timer, qemu_get_clock_ns(vm_clock) +
muldiv64(period << 7, get_ticks_per_sec(), 100));
}
static void bt_hci_inquiry_start(struct bt_hci_s *hci, int length)
{
struct bt_device_s *slave;
hci->lm.inquiry_length = length;
for (slave = hci->device.net->slave; slave; slave = slave->next)
/* Don't uncover ourselves. */
if (slave != &hci->device)
bt_hci_inquiry_result(hci, slave);
/* TODO: register for a callback on a new device's addition to the
* scatternet so that if it's added before inquiry_length expires,
* an Inquiry Result is generated immediately. Alternatively re-loop
* through the devices on the inquiry_length expiration and report
* devices not seen before. */
if (hci->lm.responses_left)
bt_hci_mod_timer_1280ms(hci->lm.inquiry_done, hci->lm.inquiry_length);
else
bt_hci_inquiry_done(hci);
if (hci->lm.periodic)
bt_hci_mod_timer_1280ms(hci->lm.inquiry_next, hci->lm.inquiry_period);
}
static void bt_hci_inquiry_next(void *opaque)
{
struct bt_hci_s *hci = (struct bt_hci_s *) opaque;
hci->lm.responses_left += hci->lm.responses;
hci->lm.responses = 0;
bt_hci_inquiry_start(hci, hci->lm.inquiry_length);
}
static inline int bt_hci_handle_bad(struct bt_hci_s *hci, uint16_t handle)
{
return !(handle & HCI_HANDLE_OFFSET) ||
handle >= (HCI_HANDLE_OFFSET | HCI_HANDLES_MAX) ||
!hci->lm.handle[handle & ~HCI_HANDLE_OFFSET].link;
}
static inline int bt_hci_role_master(struct bt_hci_s *hci, uint16_t handle)
{
return !!(hci->lm.role_bmp & (1 << (handle & ~HCI_HANDLE_OFFSET)));
}
static inline struct bt_device_s *bt_hci_remote_dev(struct bt_hci_s *hci,
uint16_t handle)
{
struct bt_link_s *link = hci->lm.handle[handle & ~HCI_HANDLE_OFFSET].link;
return bt_hci_role_master(hci, handle) ? link->slave : link->host;
}
static void bt_hci_mode_tick(void *opaque);
static void bt_hci_lmp_link_establish(struct bt_hci_s *hci,
struct bt_link_s *link, int master)
{
hci->lm.handle[hci->lm.last_handle].link = link;
if (master) {
/* We are the master side of an ACL link */
hci->lm.role_bmp |= 1 << hci->lm.last_handle;
hci->lm.handle[hci->lm.last_handle].lmp_acl_data =
link->slave->lmp_acl_data;
} else {
/* We are the slave side of an ACL link */
hci->lm.role_bmp &= ~(1 << hci->lm.last_handle);
hci->lm.handle[hci->lm.last_handle].lmp_acl_data =
link->host->lmp_acl_resp;
}
/* Mode */
if (master) {
link->acl_mode = acl_active;
hci->lm.handle[hci->lm.last_handle].acl_mode_timer =
qemu_new_timer_ns(vm_clock, bt_hci_mode_tick, link);
}
}
static void bt_hci_lmp_link_teardown(struct bt_hci_s *hci, uint16_t handle)
{
handle &= ~HCI_HANDLE_OFFSET;
hci->lm.handle[handle].link = NULL;
if (bt_hci_role_master(hci, handle)) {
qemu_del_timer(hci->lm.handle[handle].acl_mode_timer);
qemu_free_timer(hci->lm.handle[handle].acl_mode_timer);
}
}
static int bt_hci_connect(struct bt_hci_s *hci, bdaddr_t *bdaddr)
{
struct bt_device_s *slave;
struct bt_link_s link;
for (slave = hci->device.net->slave; slave; slave = slave->next)
if (slave->page_scan && !bacmp(&slave->bd_addr, bdaddr))
break;
if (!slave || slave == &hci->device)
return -ENODEV;
bacpy(&hci->lm.awaiting_bdaddr[hci->lm.connecting ++], &slave->bd_addr);
link.slave = slave;
link.host = &hci->device;
link.slave->lmp_connection_request(&link); /* Always last */
return 0;
}
static void bt_hci_connection_reject(struct bt_hci_s *hci,
struct bt_device_s *host, uint8_t because)
{
struct bt_link_s link = {
.slave = &hci->device,
.host = host,
/* Rest uninitialised */
};
host->reject_reason = because;
host->lmp_connection_complete(&link);
}
static void bt_hci_connection_reject_event(struct bt_hci_s *hci,
bdaddr_t *bdaddr)
{
evt_conn_complete params;
params.status = HCI_NO_CONNECTION;
params.handle = 0;
bacpy(¶ms.bdaddr, bdaddr);
params.link_type = ACL_LINK;
params.encr_mode = 0x00; /* Encryption not required */
bt_hci_event(hci, EVT_CONN_COMPLETE, ¶ms, EVT_CONN_COMPLETE_SIZE);
}
static void bt_hci_connection_accept(struct bt_hci_s *hci,
struct bt_device_s *host)
{
struct bt_hci_link_s *link = g_malloc0(sizeof(struct bt_hci_link_s));
evt_conn_complete params;
uint16_t handle;
uint8_t status = HCI_SUCCESS;
int tries = HCI_HANDLES_MAX;
/* Make a connection handle */
do {
while (hci->lm.handle[++ hci->lm.last_handle].link && -- tries)
hci->lm.last_handle &= HCI_HANDLES_MAX - 1;
handle = hci->lm.last_handle | HCI_HANDLE_OFFSET;
} while ((handle == hci->asb_handle || handle == hci->psb_handle) &&
tries);
if (!tries) {
g_free(link);
bt_hci_connection_reject(hci, host, HCI_REJECTED_LIMITED_RESOURCES);
status = HCI_NO_CONNECTION;
goto complete;
}
link->btlink.slave = &hci->device;
link->btlink.host = host;
link->handle = handle;
/* Link established */
bt_hci_lmp_link_establish(hci, &link->btlink, 0);
complete:
params.status = status;
params.handle = HNDL(handle);
bacpy(¶ms.bdaddr, &host->bd_addr);
params.link_type = ACL_LINK;
params.encr_mode = 0x00; /* Encryption not required */
bt_hci_event(hci, EVT_CONN_COMPLETE, ¶ms, EVT_CONN_COMPLETE_SIZE);
/* Neets to be done at the very end because it can trigger a (nested)
* disconnected, in case the other and had cancelled the request
* locally. */
if (status == HCI_SUCCESS) {
host->reject_reason = 0;
host->lmp_connection_complete(&link->btlink);
}
}
static void bt_hci_lmp_connection_request(struct bt_link_s *link)
{
struct bt_hci_s *hci = hci_from_device(link->slave);
evt_conn_request params;
if (hci->conn_req_host) {
bt_hci_connection_reject(hci, link->host,
HCI_REJECTED_LIMITED_RESOURCES);
return;
}
hci->conn_req_host = link->host;
/* TODO: if masked and auto-accept, then auto-accept,
* if masked and not auto-accept, then auto-reject */
/* TODO: kick the hci->conn_accept_timer, timeout after
* hci->conn_accept_tout * 0.625 msec */
bacpy(¶ms.bdaddr, &link->host->bd_addr);
memcpy(¶ms.dev_class, &link->host->class, sizeof(params.dev_class));
params.link_type = ACL_LINK;
bt_hci_event(hci, EVT_CONN_REQUEST, ¶ms, EVT_CONN_REQUEST_SIZE);
}
static void bt_hci_conn_accept_timeout(void *opaque)
{
struct bt_hci_s *hci = (struct bt_hci_s *) opaque;
if (!hci->conn_req_host)
/* Already accepted or rejected. If the other end cancelled the
* connection request then we still have to reject or accept it
* and then we'll get a disconnect. */
return;
/* TODO */
}
/* Remove from the list of devices which we wanted to connect to and
* are awaiting a response from. If the callback sees a response from
* a device which is not on the list it will assume it's a connection
* that's been cancelled by the host in the meantime and immediately
* try to detach the link and send a Connection Complete. */
static int bt_hci_lmp_connection_ready(struct bt_hci_s *hci,
bdaddr_t *bdaddr)
{
int i;
for (i = 0; i < hci->lm.connecting; i ++)
if (!bacmp(&hci->lm.awaiting_bdaddr[i], bdaddr)) {
if (i < -- hci->lm.connecting)
bacpy(&hci->lm.awaiting_bdaddr[i],
&hci->lm.awaiting_bdaddr[hci->lm.connecting]);
return 0;
}
return 1;
}
static void bt_hci_lmp_connection_complete(struct bt_link_s *link)
{
struct bt_hci_s *hci = hci_from_device(link->host);
evt_conn_complete params;
uint16_t handle;
uint8_t status = HCI_SUCCESS;
int tries = HCI_HANDLES_MAX;
if (bt_hci_lmp_connection_ready(hci, &link->slave->bd_addr)) {
if (!hci->device.reject_reason)
link->slave->lmp_disconnect_slave(link);
handle = 0;
status = HCI_NO_CONNECTION;
goto complete;
}
if (hci->device.reject_reason) {
handle = 0;
status = hci->device.reject_reason;
goto complete;
}
/* Make a connection handle */
do {
while (hci->lm.handle[++ hci->lm.last_handle].link && -- tries)
hci->lm.last_handle &= HCI_HANDLES_MAX - 1;
handle = hci->lm.last_handle | HCI_HANDLE_OFFSET;
} while ((handle == hci->asb_handle || handle == hci->psb_handle) &&
tries);
if (!tries) {
link->slave->lmp_disconnect_slave(link);
status = HCI_NO_CONNECTION;
goto complete;
}
/* Link established */
link->handle = handle;
bt_hci_lmp_link_establish(hci, link, 1);
complete:
params.status = status;
params.handle = HNDL(handle);
params.link_type = ACL_LINK;
bacpy(¶ms.bdaddr, &link->slave->bd_addr);
params.encr_mode = 0x00; /* Encryption not required */
bt_hci_event(hci, EVT_CONN_COMPLETE, ¶ms, EVT_CONN_COMPLETE_SIZE);
}
static void bt_hci_disconnect(struct bt_hci_s *hci,
uint16_t handle, int reason)
{
struct bt_link_s *btlink =
hci->lm.handle[handle & ~HCI_HANDLE_OFFSET].link;
struct bt_hci_link_s *link;
evt_disconn_complete params;
if (bt_hci_role_master(hci, handle)) {
btlink->slave->reject_reason = reason;
btlink->slave->lmp_disconnect_slave(btlink);
/* The link pointer is invalid from now on */
goto complete;
}
btlink->host->reject_reason = reason;
btlink->host->lmp_disconnect_master(btlink);
/* We are the slave, we get to clean this burden */
link = (struct bt_hci_link_s *) btlink;
g_free(link);
complete:
bt_hci_lmp_link_teardown(hci, handle);
params.status = HCI_SUCCESS;
params.handle = HNDL(handle);
params.reason = HCI_CONNECTION_TERMINATED;
bt_hci_event(hci, EVT_DISCONN_COMPLETE,
¶ms, EVT_DISCONN_COMPLETE_SIZE);
}
/* TODO: use only one function */
static void bt_hci_lmp_disconnect_host(struct bt_link_s *link)
{
struct bt_hci_s *hci = hci_from_device(link->host);
uint16_t handle = link->handle;
evt_disconn_complete params;
bt_hci_lmp_link_teardown(hci, handle);
params.status = HCI_SUCCESS;
params.handle = HNDL(handle);
params.reason = hci->device.reject_reason;
bt_hci_event(hci, EVT_DISCONN_COMPLETE,
¶ms, EVT_DISCONN_COMPLETE_SIZE);
}
static void bt_hci_lmp_disconnect_slave(struct bt_link_s *btlink)
{
struct bt_hci_link_s *link = (struct bt_hci_link_s *) btlink;
struct bt_hci_s *hci = hci_from_device(btlink->slave);
uint16_t handle = link->handle;
evt_disconn_complete params;
g_free(link);
bt_hci_lmp_link_teardown(hci, handle);
params.status = HCI_SUCCESS;
params.handle = HNDL(handle);
params.reason = hci->device.reject_reason;
bt_hci_event(hci, EVT_DISCONN_COMPLETE,
¶ms, EVT_DISCONN_COMPLETE_SIZE);
}
static int bt_hci_name_req(struct bt_hci_s *hci, bdaddr_t *bdaddr)
{
struct bt_device_s *slave;
evt_remote_name_req_complete params;
for (slave = hci->device.net->slave; slave; slave = slave->next)
if (slave->page_scan && !bacmp(&slave->bd_addr, bdaddr))
break;
if (!slave)
return -ENODEV;
bt_hci_event_status(hci, HCI_SUCCESS);
params.status = HCI_SUCCESS;
bacpy(¶ms.bdaddr, &slave->bd_addr);
pstrcpy(params.name, sizeof(params.name), slave->lmp_name ?: "");
bt_hci_event(hci, EVT_REMOTE_NAME_REQ_COMPLETE,
¶ms, EVT_REMOTE_NAME_REQ_COMPLETE_SIZE);
return 0;
}
static int bt_hci_features_req(struct bt_hci_s *hci, uint16_t handle)
{
struct bt_device_s *slave;
evt_read_remote_features_complete params;
if (bt_hci_handle_bad(hci, handle))
return -ENODEV;
slave = bt_hci_remote_dev(hci, handle);
bt_hci_event_status(hci, HCI_SUCCESS);
params.status = HCI_SUCCESS;
params.handle = HNDL(handle);
params.features[0] = (slave->lmp_caps >> 0) & 0xff;
params.features[1] = (slave->lmp_caps >> 8) & 0xff;
params.features[2] = (slave->lmp_caps >> 16) & 0xff;
params.features[3] = (slave->lmp_caps >> 24) & 0xff;
params.features[4] = (slave->lmp_caps >> 32) & 0xff;
params.features[5] = (slave->lmp_caps >> 40) & 0xff;
params.features[6] = (slave->lmp_caps >> 48) & 0xff;
params.features[7] = (slave->lmp_caps >> 56) & 0xff;
bt_hci_event(hci, EVT_READ_REMOTE_FEATURES_COMPLETE,
¶ms, EVT_READ_REMOTE_FEATURES_COMPLETE_SIZE);
return 0;
}
static int bt_hci_version_req(struct bt_hci_s *hci, uint16_t handle)
{
evt_read_remote_version_complete params;
if (bt_hci_handle_bad(hci, handle))
return -ENODEV;
bt_hci_remote_dev(hci, handle);
bt_hci_event_status(hci, HCI_SUCCESS);