CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/external/cache/sources/hcitools/monitor/control.c
Views: 3959
1
/*
2
*
3
* BlueZ - Bluetooth protocol stack for Linux
4
*
5
* Copyright (C) 2011-2012 Intel Corporation
6
* Copyright (C) 2004-2010 Marcel Holtmann <[email protected]>
7
*
8
*
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version.
13
*
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
18
*
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
*
23
*/
24
25
#ifdef HAVE_CONFIG_H
26
#include <config.h>
27
#endif
28
29
#include <stdio.h>
30
#include <stdbool.h>
31
#include <errno.h>
32
#include <unistd.h>
33
#include <stdlib.h>
34
#include <string.h>
35
#include <fcntl.h>
36
#include <sys/time.h>
37
#include <sys/socket.h>
38
#include <sys/un.h>
39
40
#include <lib/bluetooth/bluetooth.h>
41
#include <lib/bluetooth/hci.h>
42
#include <lib/mgmt.h>
43
44
#include "mainloop.h"
45
#include "display.h"
46
#include "packet.h"
47
#include "btsnoop.h"
48
#include "hcidump.h"
49
#include "control.h"
50
51
static bool hcidump_fallback = false;
52
53
#define MAX_PACKET_SIZE (1486 + 4)
54
55
struct control_data {
56
uint16_t channel;
57
int fd;
58
unsigned char buf[MAX_PACKET_SIZE];
59
uint16_t offset;
60
};
61
62
static void free_data(void *user_data)
63
{
64
struct control_data *data = user_data;
65
66
close(data->fd);
67
68
free(data);
69
}
70
71
static void mgmt_index_added(uint16_t len, const void *buf)
72
{
73
printf("@ Index Added\n");
74
75
packet_hexdump(buf, len);
76
}
77
78
static void mgmt_index_removed(uint16_t len, const void *buf)
79
{
80
printf("@ Index Removed\n");
81
82
packet_hexdump(buf, len);
83
}
84
85
static void mgmt_controller_error(uint16_t len, const void *buf)
86
{
87
const struct mgmt_ev_controller_error *ev = buf;
88
89
if (len < sizeof(*ev)) {
90
printf("* Malformed Controller Error control\n");
91
return;
92
}
93
94
printf("@ Controller Error: 0x%2.2x\n", ev->error_code);
95
96
buf += sizeof(*ev);
97
len -= sizeof(*ev);
98
99
packet_hexdump(buf, len);
100
}
101
102
#ifndef NELEM
103
#define NELEM(x) (sizeof(x) / sizeof((x)[0]))
104
#endif
105
106
static const char *settings_str[] = {
107
"powered", "connectable", "fast-connectable", "discoverable",
108
"pairable", "link-security", "ssp", "br/edr", "hs", "le",
109
"advertising",
110
};
111
112
static void mgmt_new_settings(uint16_t len, const void *buf)
113
{
114
uint32_t settings;
115
unsigned int i;
116
117
if (len < 4) {
118
printf("* Malformed New Settings control\n");
119
return;
120
}
121
122
settings = bt_get_le32(buf);
123
124
printf("@ New Settings: 0x%4.4x\n", settings);
125
126
printf("%-12c", ' ');
127
for (i = 0; i < NELEM(settings_str); i++) {
128
if (settings & (1 << i))
129
printf("%s ", settings_str[i]);
130
}
131
printf("\n");
132
133
buf += 4;
134
len -= 4;
135
136
packet_hexdump(buf, len);
137
}
138
139
static void mgmt_class_of_dev_changed(uint16_t len, const void *buf)
140
{
141
const struct mgmt_ev_class_of_dev_changed *ev = buf;
142
143
if (len < sizeof(*ev)) {
144
printf("* Malformed Class of Device Changed control\n");
145
return;
146
}
147
148
printf("@ Class of Device Changed: 0x%2.2x%2.2x%2.2x\n",
149
ev->class_of_dev[2],
150
ev->class_of_dev[1],
151
ev->class_of_dev[0]);
152
153
buf += sizeof(*ev);
154
len -= sizeof(*ev);
155
156
packet_hexdump(buf, len);
157
}
158
159
static void mgmt_local_name_changed(uint16_t len, const void *buf)
160
{
161
const struct mgmt_ev_local_name_changed *ev = buf;
162
163
if (len < sizeof(*ev)) {
164
printf("* Malformed Local Name Changed control\n");
165
return;
166
}
167
168
printf("@ Local Name Changed: %s (%s)\n", ev->name, ev->short_name);
169
170
buf += sizeof(*ev);
171
len -= sizeof(*ev);
172
173
packet_hexdump(buf, len);
174
}
175
176
static void mgmt_new_link_key(uint16_t len, const void *buf)
177
{
178
const struct mgmt_ev_new_link_key *ev = buf;
179
char str[18];
180
181
if (len < sizeof(*ev)) {
182
printf("* Malformed New Link Key control\n");
183
return;
184
}
185
186
ba2str(&ev->key.addr.bdaddr, str);
187
188
printf("@ New Link Key: %s (%d)\n", str, ev->key.addr.type);
189
190
buf += sizeof(*ev);
191
len -= sizeof(*ev);
192
193
packet_hexdump(buf, len);
194
}
195
196
static void mgmt_new_long_term_key(uint16_t len, const void *buf)
197
{
198
const struct mgmt_ev_new_long_term_key *ev = buf;
199
char str[18];
200
201
if (len < sizeof(*ev)) {
202
printf("* Malformed New Long Term Key control\n");
203
return;
204
}
205
206
ba2str(&ev->key.addr.bdaddr, str);
207
208
printf("@ New Long Term Key: %s (%d)\n", str, ev->key.addr.type);
209
210
buf += sizeof(*ev);
211
len -= sizeof(*ev);
212
213
packet_hexdump(buf, len);
214
}
215
216
static void mgmt_device_connected(uint16_t len, const void *buf)
217
{
218
const struct mgmt_ev_device_connected *ev = buf;
219
uint32_t flags;
220
char str[18];
221
222
if (len < sizeof(*ev)) {
223
printf("* Malformed Device Connected control\n");
224
return;
225
}
226
227
flags = btohl(ev->flags);
228
ba2str(&ev->addr.bdaddr, str);
229
230
printf("@ Device Connected: %s (%d) flags 0x%4.4x\n",
231
str, ev->addr.type, flags);
232
233
buf += sizeof(*ev);
234
len -= sizeof(*ev);
235
236
packet_hexdump(buf, len);
237
}
238
239
static void mgmt_device_disconnected(uint16_t len, const void *buf)
240
{
241
const struct mgmt_ev_device_disconnected *ev = buf;
242
char str[18];
243
uint8_t reason;
244
uint16_t consumed_len;
245
246
if (len < sizeof(struct mgmt_addr_info)) {
247
printf("* Malformed Device Disconnected control\n");
248
return;
249
}
250
251
if (len < sizeof(*ev)) {
252
reason = MGMT_DEV_DISCONN_UNKNOWN;
253
consumed_len = len;
254
} else {
255
reason = ev->reason;
256
consumed_len = sizeof(*ev);
257
}
258
259
ba2str(&ev->addr.bdaddr, str);
260
261
printf("@ Device Disconnected: %s (%d) reason %u\n", str, ev->addr.type,
262
reason);
263
264
buf += consumed_len;
265
len -= consumed_len;
266
267
packet_hexdump(buf, len);
268
}
269
270
static void mgmt_connect_failed(uint16_t len, const void *buf)
271
{
272
const struct mgmt_ev_connect_failed *ev = buf;
273
char str[18];
274
275
if (len < sizeof(*ev)) {
276
printf("* Malformed Connect Failed control\n");
277
return;
278
}
279
280
ba2str(&ev->addr.bdaddr, str);
281
282
printf("@ Connect Failed: %s (%d) status 0x%2.2x\n",
283
str, ev->addr.type, ev->status);
284
285
buf += sizeof(*ev);
286
len -= sizeof(*ev);
287
288
packet_hexdump(buf, len);
289
}
290
291
static void mgmt_pin_code_request(uint16_t len, const void *buf)
292
{
293
const struct mgmt_ev_pin_code_request *ev = buf;
294
char str[18];
295
296
if (len < sizeof(*ev)) {
297
printf("* Malformed PIN Code Request control\n");
298
return;
299
}
300
301
ba2str(&ev->addr.bdaddr, str);
302
303
printf("@ PIN Code Request: %s (%d) secure 0x%2.2x\n",
304
str, ev->addr.type, ev->secure);
305
306
buf += sizeof(*ev);
307
len -= sizeof(*ev);
308
309
packet_hexdump(buf, len);
310
}
311
312
static void mgmt_user_confirm_request(uint16_t len, const void *buf)
313
{
314
const struct mgmt_ev_user_confirm_request *ev = buf;
315
char str[18];
316
317
if (len < sizeof(*ev)) {
318
printf("* Malformed User Confirmation Request control\n");
319
return;
320
}
321
322
ba2str(&ev->addr.bdaddr, str);
323
324
printf("@ User Confirmation Request: %s (%d) hint %d value %d\n",
325
str, ev->addr.type, ev->confirm_hint, ev->value);
326
327
buf += sizeof(*ev);
328
len -= sizeof(*ev);
329
330
packet_hexdump(buf, len);
331
}
332
333
static void mgmt_user_passkey_request(uint16_t len, const void *buf)
334
{
335
const struct mgmt_ev_user_passkey_request *ev = buf;
336
char str[18];
337
338
if (len < sizeof(*ev)) {
339
printf("* Malformed User Passkey Request control\n");
340
return;
341
}
342
343
ba2str(&ev->addr.bdaddr, str);
344
345
printf("@ PIN User Passkey Request: %s (%d)\n", str, ev->addr.type);
346
347
buf += sizeof(*ev);
348
len -= sizeof(*ev);
349
350
packet_hexdump(buf, len);
351
}
352
353
static void mgmt_auth_failed(uint16_t len, const void *buf)
354
{
355
const struct mgmt_ev_auth_failed *ev = buf;
356
char str[18];
357
358
if (len < sizeof(*ev)) {
359
printf("* Malformed Authentication Failed control\n");
360
return;
361
}
362
363
ba2str(&ev->addr.bdaddr, str);
364
365
printf("@ Authentication Failed: %s (%d) status 0x%2.2x\n",
366
str, ev->addr.type, ev->status);
367
368
buf += sizeof(*ev);
369
len -= sizeof(*ev);
370
371
packet_hexdump(buf, len);
372
}
373
374
static void mgmt_device_found(uint16_t len, const void *buf)
375
{
376
const struct mgmt_ev_device_found *ev = buf;
377
uint32_t flags;
378
char str[18];
379
380
if (len < sizeof(*ev)) {
381
printf("* Malformed Device Found control\n");
382
return;
383
}
384
385
flags = btohl(ev->flags);
386
ba2str(&ev->addr.bdaddr, str);
387
388
printf("@ Device Found: %s (%d) rssi %d flags 0x%4.4x\n",
389
str, ev->addr.type, ev->rssi, flags);
390
391
buf += sizeof(*ev);
392
len -= sizeof(*ev);
393
394
packet_hexdump(buf, len);
395
}
396
397
static void mgmt_discovering(uint16_t len, const void *buf)
398
{
399
const struct mgmt_ev_discovering *ev = buf;
400
401
if (len < sizeof(*ev)) {
402
printf("* Malformed Discovering control\n");
403
return;
404
}
405
406
printf("@ Discovering: 0x%2.2x (%d)\n", ev->discovering, ev->type);
407
408
buf += sizeof(*ev);
409
len -= sizeof(*ev);
410
411
packet_hexdump(buf, len);
412
}
413
414
static void mgmt_device_blocked(uint16_t len, const void *buf)
415
{
416
const struct mgmt_ev_device_blocked *ev = buf;
417
char str[18];
418
419
if (len < sizeof(*ev)) {
420
printf("* Malformed Device Blocked control\n");
421
return;
422
}
423
424
ba2str(&ev->addr.bdaddr, str);
425
426
printf("@ Device Blocked: %s (%d)\n", str, ev->addr.type);
427
428
buf += sizeof(*ev);
429
len -= sizeof(*ev);
430
431
packet_hexdump(buf, len);
432
}
433
434
static void mgmt_device_unblocked(uint16_t len, const void *buf)
435
{
436
const struct mgmt_ev_device_unblocked *ev = buf;
437
char str[18];
438
439
if (len < sizeof(*ev)) {
440
printf("* Malformed Device Unblocked control\n");
441
return;
442
}
443
444
ba2str(&ev->addr.bdaddr, str);
445
446
printf("@ Device Unblocked: %s (%d)\n", str, ev->addr.type);
447
448
buf += sizeof(*ev);
449
len -= sizeof(*ev);
450
451
packet_hexdump(buf, len);
452
}
453
454
static void mgmt_device_unpaired(uint16_t len, const void *buf)
455
{
456
const struct mgmt_ev_device_unpaired *ev = buf;
457
char str[18];
458
459
if (len < sizeof(*ev)) {
460
printf("* Malformed Device Unpaired control\n");
461
return;
462
}
463
464
ba2str(&ev->addr.bdaddr, str);
465
466
printf("@ Device Unpaired: %s (%d)\n", str, ev->addr.type);
467
468
buf += sizeof(*ev);
469
len -= sizeof(*ev);
470
471
packet_hexdump(buf, len);
472
}
473
474
static void mgmt_passkey_notify(uint16_t len, const void *buf)
475
{
476
const struct mgmt_ev_passkey_notify *ev = buf;
477
uint32_t passkey;
478
char str[18];
479
480
if (len < sizeof(*ev)) {
481
printf("* Malformed Passkey Notify control\n");
482
return;
483
}
484
485
ba2str(&ev->addr.bdaddr, str);
486
487
passkey = btohl(ev->passkey);
488
489
printf("@ Passkey Notify: %s (%d) passkey %06u entered %u\n",
490
str, ev->addr.type, passkey, ev->entered);
491
492
buf += sizeof(*ev);
493
len -= sizeof(*ev);
494
495
packet_hexdump(buf, len);
496
}
497
498
void control_message(uint16_t opcode, const void *data, uint16_t size)
499
{
500
switch (opcode) {
501
case MGMT_EV_INDEX_ADDED:
502
mgmt_index_added(size, data);
503
break;
504
case MGMT_EV_INDEX_REMOVED:
505
mgmt_index_removed(size, data);
506
break;
507
case MGMT_EV_CONTROLLER_ERROR:
508
mgmt_controller_error(size, data);
509
break;
510
case MGMT_EV_NEW_SETTINGS:
511
mgmt_new_settings(size, data);
512
break;
513
case MGMT_EV_CLASS_OF_DEV_CHANGED:
514
mgmt_class_of_dev_changed(size, data);
515
break;
516
case MGMT_EV_LOCAL_NAME_CHANGED:
517
mgmt_local_name_changed(size, data);
518
break;
519
case MGMT_EV_NEW_LINK_KEY:
520
mgmt_new_link_key(size, data);
521
break;
522
case MGMT_EV_NEW_LONG_TERM_KEY:
523
mgmt_new_long_term_key(size, data);
524
break;
525
case MGMT_EV_DEVICE_CONNECTED:
526
mgmt_device_connected(size, data);
527
break;
528
case MGMT_EV_DEVICE_DISCONNECTED:
529
mgmt_device_disconnected(size, data);
530
break;
531
case MGMT_EV_CONNECT_FAILED:
532
mgmt_connect_failed(size, data);
533
break;
534
case MGMT_EV_PIN_CODE_REQUEST:
535
mgmt_pin_code_request(size, data);
536
break;
537
case MGMT_EV_USER_CONFIRM_REQUEST:
538
mgmt_user_confirm_request(size, data);
539
break;
540
case MGMT_EV_USER_PASSKEY_REQUEST:
541
mgmt_user_passkey_request(size, data);
542
break;
543
case MGMT_EV_AUTH_FAILED:
544
mgmt_auth_failed(size, data);
545
break;
546
case MGMT_EV_DEVICE_FOUND:
547
mgmt_device_found(size, data);
548
break;
549
case MGMT_EV_DISCOVERING:
550
mgmt_discovering(size, data);
551
break;
552
case MGMT_EV_DEVICE_BLOCKED:
553
mgmt_device_blocked(size, data);
554
break;
555
case MGMT_EV_DEVICE_UNBLOCKED:
556
mgmt_device_unblocked(size, data);
557
break;
558
case MGMT_EV_DEVICE_UNPAIRED:
559
mgmt_device_unpaired(size, data);
560
break;
561
case MGMT_EV_PASSKEY_NOTIFY:
562
mgmt_passkey_notify(size, data);
563
break;
564
default:
565
printf("* Unknown control (code %d len %d)\n", opcode, size);
566
packet_hexdump(data, size);
567
break;
568
}
569
}
570
571
static void data_callback(int fd, uint32_t events, void *user_data)
572
{
573
struct control_data *data = user_data;
574
unsigned char control[32];
575
struct mgmt_hdr hdr;
576
struct msghdr msg;
577
struct iovec iov[2];
578
579
if (events & (EPOLLERR | EPOLLHUP)) {
580
mainloop_remove_fd(data->fd);
581
return;
582
}
583
584
iov[0].iov_base = &hdr;
585
iov[0].iov_len = MGMT_HDR_SIZE;
586
iov[1].iov_base = data->buf;
587
iov[1].iov_len = sizeof(data->buf);
588
589
memset(&msg, 0, sizeof(msg));
590
msg.msg_iov = iov;
591
msg.msg_iovlen = 2;
592
msg.msg_control = control;
593
msg.msg_controllen = sizeof(control);
594
595
while (1) {
596
struct cmsghdr *cmsg;
597
struct timeval *tv = NULL;
598
struct timeval ctv;
599
uint16_t opcode, index, pktlen;
600
ssize_t len;
601
602
len = recvmsg(data->fd, &msg, MSG_DONTWAIT);
603
if (len < 0)
604
break;
605
606
if (len < MGMT_HDR_SIZE)
607
break;
608
609
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
610
cmsg = CMSG_NXTHDR(&msg, cmsg)) {
611
if (cmsg->cmsg_level != SOL_SOCKET)
612
continue;
613
614
if (cmsg->cmsg_type == SCM_TIMESTAMP) {
615
memcpy(&ctv, CMSG_DATA(cmsg), sizeof(ctv));
616
tv = &ctv;
617
}
618
}
619
620
opcode = btohs(hdr.opcode);
621
index = btohs(hdr.index);
622
pktlen = btohs(hdr.len);
623
624
switch (data->channel) {
625
case HCI_CHANNEL_CONTROL:
626
packet_control(tv, index, opcode, data->buf, pktlen);
627
break;
628
case HCI_CHANNEL_MONITOR:
629
packet_monitor(tv, index, opcode, data->buf, pktlen);
630
btsnoop_write_hci(tv, index, opcode, data->buf, pktlen);
631
break;
632
}
633
}
634
}
635
636
static int open_socket(uint16_t channel)
637
{
638
struct sockaddr_hci addr;
639
int fd, opt = 1;
640
641
fd = socket(AF_BLUETOOTH, SOCK_RAW | O_CLOEXEC, BTPROTO_HCI);
642
if (fd < 0) {
643
perror("Failed to open channel");
644
return -1;
645
}
646
647
memset(&addr, 0, sizeof(addr));
648
addr.hci_family = AF_BLUETOOTH;
649
addr.hci_dev = HCI_DEV_NONE;
650
addr.hci_channel = channel;
651
652
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
653
if (errno == EINVAL) {
654
/* Fallback to hcidump support */
655
hcidump_fallback = true;
656
close(fd);
657
return -1;
658
}
659
perror("Failed to bind channel");
660
close(fd);
661
return -1;
662
}
663
664
if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) {
665
perror("Failed to enable timestamps");
666
close(fd);
667
return -1;
668
}
669
670
return fd;
671
}
672
673
static int open_channel(uint16_t channel)
674
{
675
struct control_data *data;
676
677
data = malloc(sizeof(*data));
678
if (!data)
679
return -1;
680
681
memset(data, 0, sizeof(*data));
682
data->channel = channel;
683
684
data->fd = open_socket(channel);
685
if (data->fd < 0) {
686
free(data);
687
return -1;
688
}
689
690
mainloop_add_fd(data->fd, EPOLLIN, data_callback, data, free_data);
691
692
return 0;
693
}
694
695
static void client_callback(int fd, uint32_t events, void *user_data)
696
{
697
struct control_data *data = user_data;
698
ssize_t len;
699
700
if (events & (EPOLLERR | EPOLLHUP)) {
701
mainloop_remove_fd(data->fd);
702
return;
703
}
704
705
len = recv(data->fd, data->buf + data->offset,
706
sizeof(data->buf) - data->offset, MSG_DONTWAIT);
707
if (len < 0)
708
return;
709
710
data->offset += len;
711
712
if (data->offset > MGMT_HDR_SIZE) {
713
struct mgmt_hdr *hdr = (struct mgmt_hdr *) data->buf;
714
uint16_t pktlen = btohs(hdr->len);
715
716
if (data->offset > pktlen + MGMT_HDR_SIZE) {
717
uint16_t opcode = btohs(hdr->opcode);
718
uint16_t index = btohs(hdr->index);
719
720
packet_monitor(NULL, index, opcode,
721
data->buf + MGMT_HDR_SIZE, pktlen);
722
723
data->offset -= pktlen + MGMT_HDR_SIZE;
724
725
if (data->offset > 0)
726
memmove(data->buf, data->buf +
727
MGMT_HDR_SIZE + pktlen, data->offset);
728
}
729
}
730
}
731
732
static void server_accept_callback(int fd, uint32_t events, void *user_data)
733
{
734
struct control_data *data;
735
struct sockaddr_un addr;
736
socklen_t len;
737
int nfd;
738
739
if (events & (EPOLLERR | EPOLLHUP)) {
740
mainloop_remove_fd(fd);
741
return;
742
}
743
744
memset(&addr, 0, sizeof(addr));
745
len = sizeof(addr);
746
747
nfd = accept(fd, (struct sockaddr *) &addr, &len);
748
if (nfd < 0) {
749
perror("Failed to accept client socket");
750
return;
751
}
752
753
printf("--- New monitor connection ---\n");
754
755
data = malloc(sizeof(*data));
756
if (!data) {
757
close(nfd);
758
return;
759
}
760
761
memset(data, 0, sizeof(*data));
762
data->channel = HCI_CHANNEL_MONITOR;
763
data->fd = nfd;
764
765
mainloop_add_fd(data->fd, EPOLLIN, client_callback, data, free_data);
766
}
767
768
static int server_fd = -1;
769
770
void control_server(const char *path)
771
{
772
struct sockaddr_un addr;
773
int fd;
774
775
if (server_fd >= 0)
776
return;
777
778
unlink(path);
779
780
fd = socket(PF_UNIX, SOCK_STREAM | O_CLOEXEC, 0);
781
if (fd < 0) {
782
perror("Failed to open server socket");
783
return;
784
}
785
786
memset(&addr, 0, sizeof(addr));
787
addr.sun_family = AF_UNIX;
788
strcpy(addr.sun_path, path);
789
790
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
791
perror("Failed to bind server socket");
792
close(fd);
793
return;
794
}
795
796
if (listen(fd, 5) < 0) {
797
perror("Failed to listen server socket");
798
close(fd);
799
return;
800
}
801
802
if (mainloop_add_fd(fd, EPOLLIN, server_accept_callback,
803
NULL, NULL) < 0) {
804
close(fd);
805
return;
806
}
807
808
server_fd = fd;
809
}
810
811
void control_writer(const char *path)
812
{
813
btsnoop_create(path, BTSNOOP_TYPE_EXTENDED_HCI);
814
}
815
816
void control_reader(const char *path)
817
{
818
unsigned char buf[MAX_PACKET_SIZE];
819
uint16_t pktlen;
820
uint32_t type;
821
struct timeval tv;
822
823
if (btsnoop_open(path, &type) < 0)
824
return;
825
826
switch (type) {
827
case BTSNOOP_TYPE_HCI:
828
case BTSNOOP_TYPE_UART:
829
case BTSNOOP_TYPE_EXTENDED_PHY:
830
packet_del_filter(PACKET_FILTER_SHOW_INDEX);
831
break;
832
833
case BTSNOOP_TYPE_EXTENDED_HCI:
834
packet_add_filter(PACKET_FILTER_SHOW_INDEX);
835
break;
836
}
837
838
open_pager();
839
840
switch (type) {
841
case BTSNOOP_TYPE_HCI:
842
case BTSNOOP_TYPE_UART:
843
case BTSNOOP_TYPE_EXTENDED_HCI:
844
while (1) {
845
uint16_t index, opcode;
846
847
if (btsnoop_read_hci(&tv, &index, &opcode,
848
buf, &pktlen) < 0)
849
break;
850
851
packet_monitor(&tv, index, opcode, buf, pktlen);
852
}
853
break;
854
855
case BTSNOOP_TYPE_EXTENDED_PHY:
856
while (1) {
857
uint16_t frequency;
858
859
if (btsnoop_read_phy(&tv, &frequency,
860
buf, &pktlen) < 0)
861
break;
862
863
packet_simulator(&tv, frequency, buf, pktlen);
864
}
865
break;
866
}
867
868
close_pager();
869
870
btsnoop_close();
871
}
872
873
int control_tracing(void)
874
{
875
packet_add_filter(PACKET_FILTER_SHOW_INDEX);
876
877
if (server_fd >= 0)
878
return 0;
879
880
if (open_channel(HCI_CHANNEL_MONITOR) < 0) {
881
if (!hcidump_fallback)
882
return -1;
883
if (hcidump_tracing() < 0)
884
return -1;
885
return 0;
886
}
887
888
open_channel(HCI_CHANNEL_CONTROL);
889
890
return 0;
891
}
892
893