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
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2011-2012 Intel Corporation
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <endian.h>
#include <stdbool.h>
#include "monitor/bt.h"
#include "bthost.h"
#define le16_to_cpu(val) (val)
#define le32_to_cpu(val) (val)
#define cpu_to_le16(val) (val)
#define cpu_to_le32(val) (val)
struct cmd {
struct cmd *next;
struct cmd *prev;
uint8_t data[256 + sizeof(struct bt_hci_cmd_hdr)];
uint16_t len;
};
struct cmd_queue {
struct cmd *head;
struct cmd *tail;
};
struct btconn {
uint16_t handle;
uint16_t next_cid;
struct l2conn *l2conns;
struct btconn *next;
};
struct l2conn {
uint16_t scid;
uint16_t dcid;
struct l2conn *next;
};
struct bthost {
uint8_t bdaddr[6];
bthost_send_func send_handler;
void *send_data;
struct cmd_queue cmd_q;
uint8_t ncmd;
struct btconn *conns;
bthost_cmd_complete_cb cmd_complete_cb;
void *cmd_complete_data;
bthost_new_conn_cb new_conn_cb;
void *new_conn_data;
uint16_t server_psm;
};
struct bthost *bthost_create(void)
{
struct bthost *bthost;
bthost = malloc(sizeof(*bthost));
if (!bthost)
return NULL;
memset(bthost, 0, sizeof(*bthost));
return bthost;
}
static void l2conn_free(struct l2conn *conn)
{
free(conn);
}
static void btconn_free(struct btconn *conn)
{
while (conn->l2conns) {
struct l2conn *l2conn = conn->l2conns;
conn->l2conns = l2conn->next;
l2conn_free(l2conn);
}
free(conn);
}
static struct btconn *bthost_find_conn(struct bthost *bthost, uint16_t handle)
{
struct btconn *conn;
for (conn = bthost->conns; conn != NULL; conn = conn->next) {
if (conn->handle == handle)
return conn;
}
return NULL;
}
void bthost_destroy(struct bthost *bthost)
{
struct cmd *cmd;
if (!bthost)
return;
for (cmd = bthost->cmd_q.tail; cmd != NULL; cmd = cmd->next)
free(cmd);
while (bthost->conns) {
struct btconn *conn = bthost->conns;
bthost->conns = conn->next;
btconn_free(conn);
}
free(bthost);
}
void bthost_set_send_handler(struct bthost *bthost, bthost_send_func handler,
void *user_data)
{
if (!bthost)
return;
bthost->send_handler = handler;
bthost->send_data = user_data;
}
static void queue_command(struct bthost *bthost, const void *data,
uint16_t len)
{
struct cmd_queue *cmd_q = &bthost->cmd_q;
struct cmd *cmd;
cmd = malloc(sizeof(*cmd));
if (!cmd)
return;
memset(cmd, 0, sizeof(*cmd));
memcpy(cmd->data, data, len);
cmd->len = len;
if (cmd_q->tail)
cmd_q->tail->next = cmd;
cmd->prev = cmd_q->tail;
cmd_q->tail = cmd;
}
static void send_packet(struct bthost *bthost, const void *data, uint16_t len)
{
if (!bthost->send_handler)
return;
bthost->send_handler(data, len, bthost->send_data);
}
static void send_acl(struct bthost *bthost, uint16_t handle, uint16_t cid,
const void *data, uint16_t len)
{
struct bt_hci_acl_hdr *acl_hdr;
struct bt_l2cap_hdr *l2_hdr;
uint16_t pkt_len;
void *pkt_data;
pkt_len = 1 + sizeof(*acl_hdr) + sizeof(*l2_hdr) + len;
pkt_data = malloc(pkt_len);
if (!pkt_data)
return;
((uint8_t *) pkt_data)[0] = BT_H4_ACL_PKT;
acl_hdr = pkt_data + 1;
acl_hdr->handle = cpu_to_le16(handle);
acl_hdr->dlen = cpu_to_le16(len + sizeof(*l2_hdr));
l2_hdr = pkt_data + 1 + sizeof(*acl_hdr);
l2_hdr->cid = cpu_to_le16(cid);
l2_hdr->len = cpu_to_le16(len);
if (len > 0)
memcpy(pkt_data + 1 + sizeof(*acl_hdr) + sizeof(*l2_hdr),
data, len);
send_packet(bthost, pkt_data, pkt_len);
free(pkt_data);
}
void bthost_l2cap_cmd(struct bthost *bthost, uint16_t handle, uint8_t code,
uint8_t ident, const void *data, uint16_t len)
{
static uint8_t next_ident = 1;
struct bt_l2cap_hdr_sig *hdr;
uint16_t pkt_len;
void *pkt_data;
pkt_len = sizeof(*hdr) + len;
pkt_data = malloc(pkt_len);
if (!pkt_data)
return;
if (!ident) {
ident = next_ident++;
if (!ident)
ident = next_ident++;
}
hdr = pkt_data;
hdr->code = code;
hdr->ident = ident;
hdr->len = cpu_to_le16(len);
if (len > 0)
memcpy(pkt_data + sizeof(*hdr), data, len);
send_acl(bthost, handle, 0x0001, pkt_data, pkt_len);
free(pkt_data);
}
static void send_command(struct bthost *bthost, uint16_t opcode,
const void *data, uint8_t len)
{
struct bt_hci_cmd_hdr *hdr;
uint16_t pkt_len;
void *pkt_data;
pkt_len = 1 + sizeof(*hdr) + len;
pkt_data = malloc(pkt_len);
if (!pkt_data)
return;
((uint8_t *) pkt_data)[0] = BT_H4_CMD_PKT;
hdr = pkt_data + 1;
hdr->opcode = cpu_to_le16(opcode);
hdr->plen = len;
if (len > 0)
memcpy(pkt_data + 1 + sizeof(*hdr), data, len);
if (bthost->ncmd) {
send_packet(bthost, pkt_data, pkt_len);
bthost->ncmd--;
} else {
queue_command(bthost, pkt_data, pkt_len);
}
free(pkt_data);
}
static void next_cmd(struct bthost *bthost)
{
struct cmd_queue *cmd_q = &bthost->cmd_q;
struct cmd *cmd = cmd_q->tail;
struct cmd *next;
if (!cmd)
return;
next = cmd->next;
if (!bthost->ncmd)
return;
send_packet(bthost, cmd->data, cmd->len);
bthost->ncmd--;
if (next)
next->prev = NULL;
cmd_q->tail = next;
free(cmd);
}
static void read_bd_addr_complete(struct bthost *bthost, const void *data,
uint8_t len)
{
const struct bt_hci_rsp_read_bd_addr *ev = data;
if (len < sizeof(*ev))
return;
if (ev->status)
return;
memcpy(bthost->bdaddr, ev->bdaddr, 6);
}
static void evt_cmd_complete(struct bthost *bthost, const void *data,
uint8_t len)
{
const struct bt_hci_evt_cmd_complete *ev = data;
const void *param;
uint16_t opcode;
if (len < sizeof(*ev))
return;
param = data + sizeof(*ev);
bthost->ncmd = ev->ncmd;
opcode = le16toh(ev->opcode);
switch (opcode) {
case BT_HCI_CMD_RESET:
break;
case BT_HCI_CMD_READ_BD_ADDR:
read_bd_addr_complete(bthost, param, len - sizeof(*ev));
break;
case BT_HCI_CMD_WRITE_SCAN_ENABLE:
break;
default:
printf("Unhandled cmd_complete opcode 0x%04x\n", opcode);
break;
}
if (bthost->cmd_complete_cb)
bthost->cmd_complete_cb(opcode, 0, param, len - sizeof(*ev),
bthost->cmd_complete_data);
next_cmd(bthost);
}
static void evt_cmd_status(struct bthost *bthost, const void *data,
uint8_t len)
{
const struct bt_hci_evt_cmd_status *ev = data;
uint16_t opcode;
if (len < sizeof(*ev))
return;
bthost->ncmd = ev->ncmd;
opcode = le16toh(ev->opcode);
if (ev->status && bthost->cmd_complete_cb)
bthost->cmd_complete_cb(opcode, ev->status, NULL, 0,
bthost->cmd_complete_data);
next_cmd(bthost);
}
static void evt_conn_request(struct bthost *bthost, const void *data,
uint8_t len)
{
const struct bt_hci_evt_conn_request *ev = data;
struct bt_hci_cmd_accept_conn_request cmd;
if (len < sizeof(*ev))
return;
memset(&cmd, 0, sizeof(cmd));
memcpy(cmd.bdaddr, ev->bdaddr, sizeof(ev->bdaddr));
send_command(bthost, BT_HCI_CMD_ACCEPT_CONN_REQUEST, &cmd,
sizeof(cmd));
}
static void evt_conn_complete(struct bthost *bthost, const void *data,
uint8_t len)
{
const struct bt_hci_evt_conn_complete *ev = data;
struct btconn *conn;
if (len < sizeof(*ev))
return;
if (ev->status)
return;
conn = malloc(sizeof(*conn));
if (!conn)
return;
memset(conn, 0, sizeof(*conn));
conn->handle = le16_to_cpu(ev->handle);
conn->next_cid = 0x0040;
conn->next = bthost->conns;
bthost->conns = conn;
if (bthost->new_conn_cb)
bthost->new_conn_cb(conn->handle, bthost->new_conn_data);
}
static void evt_disconn_complete(struct bthost *bthost, const void *data,
uint8_t len)
{
const struct bt_hci_evt_disconnect_complete *ev = data;
struct btconn **curr;
uint16_t handle;
if (len < sizeof(*ev))
return;
if (ev->status)
return;
handle = le16_to_cpu(ev->handle);
for (curr = &bthost->conns; *curr;) {
struct btconn *conn = *curr;
if (conn->handle == handle) {
*curr = conn->next;
btconn_free(conn);
} else {
curr = &conn->next;
}
}
}
static void evt_num_completed_packets(struct bthost *bthost, const void *data,
uint8_t len)
{
const struct bt_hci_evt_num_completed_packets *ev = data;
if (len < sizeof(*ev))
return;
}
static void process_evt(struct bthost *bthost, const void *data, uint16_t len)
{
const struct bt_hci_evt_hdr *hdr = data;
const void *param;
if (len < sizeof(*hdr))
return;
if (sizeof(*hdr) + hdr->plen != len)
return;
param = data + sizeof(*hdr);
switch (hdr->evt) {
case BT_HCI_EVT_CMD_COMPLETE:
evt_cmd_complete(bthost, param, hdr->plen);
break;
case BT_HCI_EVT_CMD_STATUS:
evt_cmd_status(bthost, param, hdr->plen);
break;
case BT_HCI_EVT_CONN_REQUEST:
evt_conn_request(bthost, param, hdr->plen);
break;
case BT_HCI_EVT_CONN_COMPLETE:
evt_conn_complete(bthost, param, hdr->plen);
break;
case BT_HCI_EVT_DISCONNECT_COMPLETE:
evt_disconn_complete(bthost, param, hdr->plen);
break;
case BT_HCI_EVT_NUM_COMPLETED_PACKETS:
evt_num_completed_packets(bthost, param, hdr->plen);
break;
default:
printf("Unsupported event 0x%2.2x\n", hdr->evt);
break;
}
}
static bool l2cap_conn_req(struct bthost *bthost, uint16_t handle,
uint8_t ident, const void *data, uint16_t len)
{
const struct bt_l2cap_pdu_conn_req *req = data;
struct bt_l2cap_pdu_conn_rsp rsp;
struct btconn *conn;
uint16_t psm;
if (len < sizeof(*req))
return false;
conn = bthost_find_conn(bthost, handle);
if (!conn)
return false;
psm = le16_to_cpu(req->psm);
memset(&rsp, 0, sizeof(rsp));
rsp.scid = req->scid;
if (bthost->server_psm && bthost->server_psm == psm)
rsp.dcid = cpu_to_le16(conn->next_cid++);
else
rsp.result = cpu_to_le16(0x0002); /* PSM Not Supported */
bthost_l2cap_cmd(bthost, handle, BT_L2CAP_PDU_CONN_RSP, ident, &rsp,
sizeof(rsp));
if (!rsp.result) {
struct bt_l2cap_pdu_config_req conf_req;
memset(&conf_req, 0, sizeof(conf_req));
conf_req.dcid = rsp.dcid;
bthost_l2cap_cmd(bthost, handle, BT_L2CAP_PDU_CONFIG_REQ, 0,
&conf_req, sizeof(conf_req));
}
return true;
}
static bool l2cap_conn_rsp(struct bthost *bthost, uint16_t handle,
uint8_t ident, const void *data, uint16_t len)
{
const struct bt_l2cap_pdu_conn_rsp *rsp = data;
if (len < sizeof(*rsp))
return false;
if (le16_to_cpu(rsp->result) == 0x0001) {
struct bt_l2cap_pdu_config_req req;
memset(&req, 0, sizeof(req));
req.dcid = rsp->dcid;
bthost_l2cap_cmd(bthost, handle, BT_L2CAP_PDU_CONFIG_REQ, 0,
&req, sizeof(req));
}
return true;
}
static bool l2cap_config_req(struct bthost *bthost, uint16_t handle,
uint8_t ident, const void *data, uint16_t len)
{
const struct bt_l2cap_pdu_config_req *req = data;
struct bt_l2cap_pdu_config_rsp rsp;
if (len < sizeof(*req))
return false;
memset(&rsp, 0, sizeof(rsp));
rsp.scid = req->dcid;
rsp.flags = req->flags;
bthost_l2cap_cmd(bthost, handle, BT_L2CAP_PDU_CONFIG_RSP, ident, &rsp,
sizeof(rsp));
return true;
}
static bool l2cap_config_rsp(struct bthost *bthost, uint16_t handle,
uint8_t ident, const void *data, uint16_t len)
{
const struct bt_l2cap_pdu_config_rsp *rsp = data;
if (len < sizeof(*rsp))
return false;
return true;
}
static bool l2cap_disconn_req(struct bthost *bthost, uint16_t handle,
uint8_t ident, const void *data, uint16_t len)
{
const struct bt_l2cap_pdu_disconn_req *req = data;
struct bt_l2cap_pdu_disconn_rsp rsp;
if (len < sizeof(*req))
return false;
memset(&rsp, 0, sizeof(rsp));
rsp.dcid = req->dcid;
rsp.scid = req->scid;
bthost_l2cap_cmd(bthost, handle, BT_L2CAP_PDU_DISCONN_RSP, ident, &rsp,
sizeof(rsp));
return true;
}
static bool l2cap_info_req(struct bthost *bthost, uint16_t handle,
uint8_t ident, const void *data, uint16_t len)
{
const struct bt_l2cap_pdu_info_req *req = data;
struct bt_l2cap_pdu_info_rsp rsp;
if (len < sizeof(*req))
return false;
rsp.type = req->type;
rsp.result = cpu_to_le16(0x0001); /* Not Supported */
bthost_l2cap_cmd(bthost, handle, BT_L2CAP_PDU_INFO_RSP, ident, &rsp,
sizeof(rsp));
return true;
}
static void l2cap_sig(struct bthost *bthost, uint16_t handle, const void *data,
uint16_t len)
{
const struct bt_l2cap_hdr_sig *hdr = data;
struct bt_l2cap_pdu_cmd_reject rej;
uint16_t hdr_len;
bool ret;
if (len < sizeof(*hdr))
goto reject;
hdr_len = le16_to_cpu(hdr->len);
if (sizeof(*hdr) + hdr_len != len)
goto reject;
switch (hdr->code) {
case BT_L2CAP_PDU_CONN_REQ:
ret = l2cap_conn_req(bthost, handle, hdr->ident,
data + sizeof(*hdr), hdr_len);
break;
case BT_L2CAP_PDU_CONN_RSP:
ret = l2cap_conn_rsp(bthost, handle, hdr->ident,
data + sizeof(*hdr), hdr_len);
break;
case BT_L2CAP_PDU_CONFIG_REQ:
ret = l2cap_config_req(bthost, handle, hdr->ident,
data + sizeof(*hdr), hdr_len);
break;
case BT_L2CAP_PDU_CONFIG_RSP:
ret = l2cap_config_rsp(bthost, handle, hdr->ident,
data + sizeof(*hdr), hdr_len);
break;
case BT_L2CAP_PDU_DISCONN_REQ:
ret = l2cap_disconn_req(bthost, handle, hdr->ident,
data + sizeof(*hdr), hdr_len);
break;
case BT_L2CAP_PDU_INFO_REQ:
ret = l2cap_info_req(bthost, handle, hdr->ident,
data + sizeof(*hdr), hdr_len);
break;
default:
printf("Unknown L2CAP code 0x%02x\n", hdr->code);
ret = false;
}
if (ret)
return;
reject:
memset(&rej, 0, sizeof(rej));
bthost_l2cap_cmd(bthost, handle, BT_L2CAP_PDU_CMD_REJECT, 0,
&rej, sizeof(rej));
}
static void process_acl(struct bthost *bthost, const void *data, uint16_t len)
{
const struct bt_hci_acl_hdr *acl_hdr = data;
const struct bt_l2cap_hdr *l2_hdr = data + sizeof(*acl_hdr);
uint16_t handle, cid, acl_len, l2_len;
const void *l2_data;
if (len < sizeof(*acl_hdr) + sizeof(*l2_hdr))
return;
acl_len = le16_to_cpu(acl_hdr->dlen);
if (len != sizeof(*acl_hdr) + acl_len)
return;
handle = le16_to_cpu(acl_hdr->handle);
l2_len = le16_to_cpu(l2_hdr->len);
if (len - sizeof(*acl_hdr) != sizeof(*l2_hdr) + l2_len)
return;
l2_data = data + sizeof(*acl_hdr) + sizeof(*l2_hdr);
cid = le16_to_cpu(l2_hdr->cid);
switch (cid) {
case 0x0001:
l2cap_sig(bthost, handle, l2_data, l2_len);
break;
default:
printf("Packet for unknown CID 0x%04x (%u)\n", cid, cid);
break;
}
}
void bthost_receive_h4(struct bthost *bthost, const void *data, uint16_t len)
{
uint8_t pkt_type;
if (!bthost)
return;
if (len < 1)
return;
pkt_type = ((const uint8_t *) data)[0];
switch (pkt_type) {
case BT_H4_EVT_PKT:
process_evt(bthost, data + 1, len - 1);
break;
case BT_H4_ACL_PKT:
process_acl(bthost, data + 1, len - 1);
break;
default:
printf("Unsupported packet 0x%2.2x\n", pkt_type);
break;
}
}
void bthost_set_cmd_complete_cb(struct bthost *bthost,
bthost_cmd_complete_cb cb, void *user_data)
{
bthost->cmd_complete_cb = cb;
bthost->cmd_complete_data = user_data;
}
void bthost_set_connect_cb(struct bthost *bthost, bthost_new_conn_cb cb,
void *user_data)
{
bthost->new_conn_cb = cb;
bthost->new_conn_data = user_data;
}
void bthost_hci_connect(struct bthost *bthost, const uint8_t *bdaddr)
{
struct bt_hci_cmd_create_conn cmd;
memset(&cmd, 0, sizeof(cmd));
memcpy(cmd.bdaddr, bdaddr, sizeof(cmd.bdaddr));
send_command(bthost, BT_HCI_CMD_CREATE_CONN, &cmd, sizeof(cmd));
}
void bthost_write_scan_enable(struct bthost *bthost, uint8_t scan)
{
send_command(bthost, BT_HCI_CMD_WRITE_SCAN_ENABLE, &scan, 1);
}
void bthost_set_server_psm(struct bthost *bthost, uint16_t psm)
{
bthost->server_psm = psm;
}
void bthost_start(struct bthost *bthost)
{
if (!bthost)
return;
bthost->ncmd = 1;
send_command(bthost, BT_HCI_CMD_RESET, NULL, 0);
send_command(bthost, BT_HCI_CMD_READ_BD_ADDR, NULL, 0);
}
void bthost_stop(struct bthost *bthost)
{
}
|