analyze.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011-2014 Intel Corporation
  7. * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #define _GNU_SOURCE
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <sys/time.h>
  18. #include "lib/bluetooth.h"
  19. #include "src/shared/util.h"
  20. #include "src/shared/queue.h"
  21. #include "src/shared/btsnoop.h"
  22. #include "monitor/bt.h"
  23. #include "monitor/display.h"
  24. #include "monitor/packet.h"
  25. #include "monitor/analyze.h"
  26. struct hci_dev {
  27. uint16_t index;
  28. uint8_t type;
  29. uint8_t bdaddr[6];
  30. struct timeval time_added;
  31. struct timeval time_removed;
  32. unsigned long num_hci;
  33. unsigned long num_cmd;
  34. unsigned long num_evt;
  35. unsigned long num_acl;
  36. unsigned long num_sco;
  37. unsigned long num_iso;
  38. unsigned long vendor_diag;
  39. unsigned long system_note;
  40. unsigned long user_log;
  41. unsigned long ctrl_msg;
  42. unsigned long unknown;
  43. uint16_t manufacturer;
  44. struct queue *conn_list;
  45. };
  46. #define CONN_BR_ACL 0x01
  47. #define CONN_BR_SCO 0x02
  48. #define CONN_BR_ESCO 0x03
  49. #define CONN_LE_ACL 0x04
  50. #define CONN_LE_ISO 0x05
  51. struct hci_conn {
  52. uint16_t handle;
  53. uint8_t type;
  54. uint8_t bdaddr[6];
  55. bool setup_seen;
  56. bool terminated;
  57. unsigned long rx_num;
  58. unsigned long tx_num;
  59. unsigned long tx_num_comp;
  60. size_t tx_bytes;
  61. struct queue *tx_queue;
  62. struct timeval tx_lat_min;
  63. struct timeval tx_lat_max;
  64. struct timeval tx_lat_med;
  65. uint16_t tx_pkt_min;
  66. uint16_t tx_pkt_max;
  67. uint16_t tx_pkt_med;
  68. struct queue *chan_list;
  69. };
  70. struct l2cap_chan {
  71. uint16_t cid;
  72. uint16_t psm;
  73. bool out;
  74. unsigned long num;
  75. };
  76. static struct queue *dev_list;
  77. static void chan_destroy(void *data)
  78. {
  79. struct l2cap_chan *chan = data;
  80. printf(" Found %s L2CAP channel with CID %u\n",
  81. chan->out ? "TX" : "RX", chan->cid);
  82. if (chan->psm)
  83. printf(" PSM %u\n", chan->psm);
  84. printf(" %lu packets\n", chan->num);
  85. free(chan);
  86. }
  87. static struct l2cap_chan *chan_alloc(struct hci_conn *conn, uint16_t cid,
  88. bool out)
  89. {
  90. struct l2cap_chan *chan;
  91. chan = new0(struct l2cap_chan, 1);
  92. chan->cid = cid;
  93. chan->out = out;
  94. return chan;
  95. }
  96. static bool chan_match_cid(const void *a, const void *b)
  97. {
  98. const struct l2cap_chan *chan = a;
  99. uint32_t val = PTR_TO_UINT(b);
  100. uint16_t cid = val & 0xffff;
  101. bool out = val & 0x10000;
  102. return chan->cid == cid && chan->out == out;
  103. }
  104. static struct l2cap_chan *chan_lookup(struct hci_conn *conn, uint16_t cid,
  105. bool out)
  106. {
  107. struct l2cap_chan *chan;
  108. uint32_t val = cid | (out ? 0x10000 : 0);
  109. chan = queue_find(conn->chan_list, chan_match_cid, UINT_TO_PTR(val));
  110. if (!chan) {
  111. chan = chan_alloc(conn, cid, out);
  112. queue_push_tail(conn->chan_list, chan);
  113. }
  114. return chan;
  115. }
  116. static void conn_destroy(void *data)
  117. {
  118. struct hci_conn *conn = data;
  119. const char *str;
  120. switch (conn->type) {
  121. case CONN_BR_ACL:
  122. str = "BR-ACL";
  123. break;
  124. case CONN_BR_SCO:
  125. str = "BR-SCO";
  126. break;
  127. case CONN_BR_ESCO:
  128. str = "BR-ESCO";
  129. break;
  130. case CONN_LE_ACL:
  131. str = "LE-ACL";
  132. break;
  133. case CONN_LE_ISO:
  134. str = "LE-ISO";
  135. break;
  136. default:
  137. str = "unknown";
  138. break;
  139. }
  140. conn->tx_pkt_med = conn->tx_bytes / conn->tx_num;
  141. printf(" Found %s connection with handle %u\n", str, conn->handle);
  142. /* TODO: Store address type */
  143. packet_print_addr("Address", conn->bdaddr, 0x00);
  144. if (!conn->setup_seen)
  145. print_field("Connection setup missing");
  146. print_field("%lu RX packets", conn->rx_num);
  147. print_field("%lu TX packets", conn->tx_num);
  148. print_field("%lu TX completed packets", conn->tx_num_comp);
  149. print_field("%ld msec min latency",
  150. conn->tx_lat_min.tv_sec * 1000 +
  151. conn->tx_lat_min.tv_usec / 1000);
  152. print_field("%ld msec max latency",
  153. conn->tx_lat_max.tv_sec * 1000 +
  154. conn->tx_lat_max.tv_usec / 1000);
  155. print_field("%ld msec median latency",
  156. conn->tx_lat_med.tv_sec * 1000 +
  157. conn->tx_lat_med.tv_usec / 1000);
  158. print_field("%u octets TX min packet size", conn->tx_pkt_min);
  159. print_field("%u octets TX max packet size", conn->tx_pkt_max);
  160. print_field("%u octets TX median packet size", conn->tx_pkt_med);
  161. queue_destroy(conn->chan_list, chan_destroy);
  162. queue_destroy(conn->tx_queue, free);
  163. free(conn);
  164. }
  165. static struct hci_conn *conn_alloc(struct hci_dev *dev, uint16_t handle,
  166. uint8_t type)
  167. {
  168. struct hci_conn *conn;
  169. conn = new0(struct hci_conn, 1);
  170. conn->handle = handle;
  171. conn->type = type;
  172. conn->tx_queue = queue_new();
  173. conn->chan_list = queue_new();
  174. return conn;
  175. }
  176. static bool conn_match_handle(const void *a, const void *b)
  177. {
  178. const struct hci_conn *conn = a;
  179. uint16_t handle = PTR_TO_UINT(b);
  180. return (conn->handle == handle && !conn->terminated);
  181. }
  182. static struct hci_conn *conn_lookup(struct hci_dev *dev, uint16_t handle)
  183. {
  184. return queue_find(dev->conn_list, conn_match_handle,
  185. UINT_TO_PTR(handle));
  186. }
  187. static struct hci_conn *conn_lookup_type(struct hci_dev *dev, uint16_t handle,
  188. uint8_t type)
  189. {
  190. struct hci_conn *conn;
  191. conn = queue_find(dev->conn_list, conn_match_handle,
  192. UINT_TO_PTR(handle));
  193. if (!conn || conn->type != type) {
  194. conn = conn_alloc(dev, handle, type);
  195. queue_push_tail(dev->conn_list, conn);
  196. }
  197. return conn;
  198. }
  199. static void dev_destroy(void *data)
  200. {
  201. struct hci_dev *dev = data;
  202. const char *str;
  203. switch (dev->type) {
  204. case 0x00:
  205. str = "BR/EDR";
  206. break;
  207. case 0x01:
  208. str = "AMP";
  209. break;
  210. default:
  211. str = "unknown";
  212. break;
  213. }
  214. printf("Found %s controller with index %u\n", str, dev->index);
  215. printf(" BD_ADDR %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
  216. dev->bdaddr[5], dev->bdaddr[4], dev->bdaddr[3],
  217. dev->bdaddr[2], dev->bdaddr[1], dev->bdaddr[0]);
  218. if (dev->manufacturer != 0xffff)
  219. printf(" (%s)", bt_compidtostr(dev->manufacturer));
  220. printf("\n");
  221. printf(" %lu commands\n", dev->num_cmd);
  222. printf(" %lu events\n", dev->num_evt);
  223. printf(" %lu ACL packets\n", dev->num_acl);
  224. printf(" %lu SCO packets\n", dev->num_sco);
  225. printf(" %lu ISO packets\n", dev->num_iso);
  226. printf(" %lu vendor diagnostics\n", dev->vendor_diag);
  227. printf(" %lu system notes\n", dev->system_note);
  228. printf(" %lu user logs\n", dev->user_log);
  229. printf(" %lu control messages \n", dev->ctrl_msg);
  230. printf(" %lu unknown opcodes\n", dev->unknown);
  231. queue_destroy(dev->conn_list, conn_destroy);
  232. printf("\n");
  233. free(dev);
  234. }
  235. static struct hci_dev *dev_alloc(uint16_t index)
  236. {
  237. struct hci_dev *dev;
  238. dev = new0(struct hci_dev, 1);
  239. dev->index = index;
  240. dev->manufacturer = 0xffff;
  241. dev->conn_list = queue_new();
  242. return dev;
  243. }
  244. static bool dev_match_index(const void *a, const void *b)
  245. {
  246. const struct hci_dev *dev = a;
  247. uint16_t index = PTR_TO_UINT(b);
  248. return dev->index == index;
  249. }
  250. static struct hci_dev *dev_lookup(uint16_t index)
  251. {
  252. struct hci_dev *dev;
  253. dev = queue_find(dev_list, dev_match_index, UINT_TO_PTR(index));
  254. if (!dev) {
  255. dev = dev_alloc(index);
  256. queue_push_tail(dev_list, dev);
  257. }
  258. return dev;
  259. }
  260. static void l2cap_sig(struct hci_conn *conn, bool out,
  261. const void *data, uint16_t size)
  262. {
  263. const struct bt_l2cap_hdr_sig *hdr = data;
  264. struct l2cap_chan *chan;
  265. uint16_t psm, scid, dcid;
  266. switch (hdr->code) {
  267. case BT_L2CAP_PDU_CONN_REQ:
  268. psm = get_le16(data + 4);
  269. scid = get_le16(data + 6);
  270. chan = chan_lookup(conn, scid, out);
  271. if (chan)
  272. chan->psm = psm;
  273. break;
  274. case BT_L2CAP_PDU_CONN_RSP:
  275. dcid = get_le16(data + 4);
  276. scid = get_le16(data + 6);
  277. chan = chan_lookup(conn, scid, !out);
  278. if (chan) {
  279. psm = chan->psm;
  280. chan = chan_lookup(conn, dcid, out);
  281. if (chan)
  282. chan->psm = psm;
  283. }
  284. break;
  285. }
  286. }
  287. static void new_index(struct timeval *tv, uint16_t index,
  288. const void *data, uint16_t size)
  289. {
  290. const struct btsnoop_opcode_new_index *ni = data;
  291. struct hci_dev *dev;
  292. dev = dev_alloc(index);
  293. dev->type = ni->type;
  294. memcpy(dev->bdaddr, ni->bdaddr, 6);
  295. queue_push_tail(dev_list, dev);
  296. }
  297. static void del_index(struct timeval *tv, uint16_t index,
  298. const void *data, uint16_t size)
  299. {
  300. struct hci_dev *dev;
  301. dev = queue_remove_if(dev_list, dev_match_index, UINT_TO_PTR(index));
  302. if (!dev) {
  303. fprintf(stderr, "Remove for an unexisting device\n");
  304. return;
  305. }
  306. dev_destroy(dev);
  307. }
  308. static void command_pkt(struct timeval *tv, uint16_t index,
  309. const void *data, uint16_t size)
  310. {
  311. const struct bt_hci_cmd_hdr *hdr = data;
  312. struct hci_dev *dev;
  313. data += sizeof(*hdr);
  314. size -= sizeof(*hdr);
  315. dev = dev_lookup(index);
  316. if (!dev)
  317. return;
  318. dev->num_hci++;
  319. dev->num_cmd++;
  320. }
  321. static void evt_conn_complete(struct hci_dev *dev, struct timeval *tv,
  322. const void *data, uint16_t size)
  323. {
  324. const struct bt_hci_evt_conn_complete *evt = data;
  325. struct hci_conn *conn;
  326. data += sizeof(*evt);
  327. size -= sizeof(*evt);
  328. if (evt->status)
  329. return;
  330. conn = conn_lookup_type(dev, le16_to_cpu(evt->handle), CONN_BR_ACL);
  331. if (!conn)
  332. return;
  333. memcpy(conn->bdaddr, evt->bdaddr, 6);
  334. conn->setup_seen = true;
  335. }
  336. static void evt_disconnect_complete(struct hci_dev *dev, struct timeval *tv,
  337. const void *data, uint16_t size)
  338. {
  339. const struct bt_hci_evt_disconnect_complete *evt = data;
  340. struct hci_conn *conn;
  341. data += sizeof(*evt);
  342. size -= sizeof(*evt);
  343. if (evt->status)
  344. return;
  345. conn = conn_lookup(dev, le16_to_cpu(evt->handle));
  346. if (!conn)
  347. return;
  348. conn->terminated = true;
  349. }
  350. static void rsp_read_bd_addr(struct hci_dev *dev, struct timeval *tv,
  351. const void *data, uint16_t size)
  352. {
  353. const struct bt_hci_rsp_read_bd_addr *rsp = data;
  354. if (rsp->status)
  355. return;
  356. memcpy(dev->bdaddr, rsp->bdaddr, 6);
  357. }
  358. static void evt_cmd_complete(struct hci_dev *dev, struct timeval *tv,
  359. const void *data, uint16_t size)
  360. {
  361. const struct bt_hci_evt_cmd_complete *evt = data;
  362. uint16_t opcode;
  363. data += sizeof(*evt);
  364. size -= sizeof(*evt);
  365. opcode = le16_to_cpu(evt->opcode);
  366. switch (opcode) {
  367. case BT_HCI_CMD_READ_BD_ADDR:
  368. rsp_read_bd_addr(dev, tv, data, size);
  369. break;
  370. }
  371. }
  372. static void evt_num_completed_packets(struct hci_dev *dev, struct timeval *tv,
  373. const void *data, uint16_t size)
  374. {
  375. uint8_t num_handles = get_u8(data);
  376. int i;
  377. data += sizeof(num_handles);
  378. size -= sizeof(num_handles);
  379. for (i = 0; i < num_handles; i++) {
  380. uint16_t handle = get_le16(data);
  381. uint16_t count = get_le16(data + 2);
  382. struct hci_conn *conn;
  383. struct timeval res;
  384. struct timeval *last_tx;
  385. data += 4;
  386. size -= 4;
  387. conn = conn_lookup(dev, handle);
  388. if (!conn)
  389. continue;
  390. conn->tx_num_comp += count;
  391. last_tx = queue_pop_head(conn->tx_queue);
  392. if (last_tx) {
  393. timersub(tv, last_tx, &res);
  394. if ((!timerisset(&conn->tx_lat_min) ||
  395. timercmp(&res, &conn->tx_lat_min, <)) &&
  396. res.tv_sec >= 0 && res.tv_usec >= 0)
  397. conn->tx_lat_min = res;
  398. if (!timerisset(&conn->tx_lat_max) ||
  399. timercmp(&res, &conn->tx_lat_max, >))
  400. conn->tx_lat_max = res;
  401. if (timerisset(&conn->tx_lat_med)) {
  402. struct timeval tmp;
  403. timeradd(&conn->tx_lat_med, &res, &tmp);
  404. tmp.tv_sec /= 2;
  405. tmp.tv_usec /= 2;
  406. if (tmp.tv_sec % 2) {
  407. tmp.tv_usec += 500000;
  408. if (tmp.tv_usec >= 1000000) {
  409. tmp.tv_sec++;
  410. tmp.tv_usec -= 1000000;
  411. }
  412. }
  413. conn->tx_lat_med = tmp;
  414. } else
  415. conn->tx_lat_med = res;
  416. free(last_tx);
  417. }
  418. }
  419. }
  420. static void evt_le_meta_event(struct hci_dev *dev, struct timeval *tv,
  421. const void *data, uint16_t size)
  422. {
  423. uint8_t subtype = get_u8(data);
  424. data += sizeof(subtype);
  425. size -= sizeof(subtype);
  426. switch (subtype) {
  427. }
  428. }
  429. static void event_pkt(struct timeval *tv, uint16_t index,
  430. const void *data, uint16_t size)
  431. {
  432. const struct bt_hci_evt_hdr *hdr = data;
  433. struct hci_dev *dev;
  434. data += sizeof(*hdr);
  435. size -= sizeof(*hdr);
  436. dev = dev_lookup(index);
  437. if (!dev)
  438. return;
  439. dev->num_hci++;
  440. dev->num_evt++;
  441. switch (hdr->evt) {
  442. case BT_HCI_EVT_CONN_COMPLETE:
  443. evt_conn_complete(dev, tv, data, size);
  444. break;
  445. case BT_HCI_EVT_DISCONNECT_COMPLETE:
  446. evt_disconnect_complete(dev, tv, data, size);
  447. break;
  448. case BT_HCI_EVT_CMD_COMPLETE:
  449. evt_cmd_complete(dev, tv, data, size);
  450. break;
  451. case BT_HCI_EVT_NUM_COMPLETED_PACKETS:
  452. evt_num_completed_packets(dev, tv, data, size);
  453. break;
  454. case BT_HCI_EVT_LE_META_EVENT:
  455. evt_le_meta_event(dev, tv, data, size);
  456. break;
  457. }
  458. }
  459. static void acl_pkt(struct timeval *tv, uint16_t index, bool out,
  460. const void *data, uint16_t size)
  461. {
  462. const struct bt_hci_acl_hdr *hdr = data;
  463. struct hci_dev *dev;
  464. struct hci_conn *conn;
  465. struct l2cap_chan *chan;
  466. uint16_t cid;
  467. data += sizeof(*hdr);
  468. size -= sizeof(*hdr);
  469. dev = dev_lookup(index);
  470. if (!dev)
  471. return;
  472. dev->num_hci++;
  473. dev->num_acl++;
  474. conn = conn_lookup_type(dev, le16_to_cpu(hdr->handle) & 0x0fff,
  475. CONN_BR_ACL);
  476. if (!conn)
  477. return;
  478. switch (le16_to_cpu(hdr->handle) >> 12) {
  479. case 0x00:
  480. case 0x02:
  481. cid = get_le16(data + 2);
  482. chan = chan_lookup(conn, cid, out);
  483. if (chan)
  484. chan->num++;
  485. if (cid == 1)
  486. l2cap_sig(conn, out, data + 4, size - 4);
  487. break;
  488. }
  489. if (out) {
  490. struct timeval *last_tx;
  491. conn->tx_num++;
  492. last_tx = new0(struct timeval, 1);
  493. memcpy(last_tx, tv, sizeof(*tv));
  494. queue_push_tail(conn->tx_queue, last_tx);
  495. conn->tx_bytes += size;
  496. if (!conn->tx_pkt_min || size < conn->tx_pkt_min)
  497. conn->tx_pkt_min = size;
  498. if (!conn->tx_pkt_max || size > conn->tx_pkt_max)
  499. conn->tx_pkt_max = size;
  500. } else {
  501. conn->rx_num++;
  502. }
  503. }
  504. static void sco_pkt(struct timeval *tv, uint16_t index,
  505. const void *data, uint16_t size)
  506. {
  507. const struct bt_hci_sco_hdr *hdr = data;
  508. struct hci_dev *dev;
  509. data += sizeof(*hdr);
  510. size -= sizeof(*hdr);
  511. dev = dev_lookup(index);
  512. if (!dev)
  513. return;
  514. dev->num_hci++;
  515. dev->num_sco++;
  516. }
  517. static void info_index(struct timeval *tv, uint16_t index,
  518. const void *data, uint16_t size)
  519. {
  520. const struct btsnoop_opcode_index_info *hdr = data;
  521. struct hci_dev *dev;
  522. data += sizeof(*hdr);
  523. size -= sizeof(*hdr);
  524. dev = dev_lookup(index);
  525. if (!dev)
  526. return;
  527. dev->manufacturer = hdr->manufacturer;
  528. }
  529. static void vendor_diag(struct timeval *tv, uint16_t index,
  530. const void *data, uint16_t size)
  531. {
  532. struct hci_dev *dev;
  533. dev = dev_lookup(index);
  534. if (!dev)
  535. return;
  536. dev->vendor_diag++;
  537. }
  538. static void system_note(struct timeval *tv, uint16_t index,
  539. const void *data, uint16_t size)
  540. {
  541. struct hci_dev *dev;
  542. dev = dev_lookup(index);
  543. if (!dev)
  544. return;
  545. dev->system_note++;
  546. }
  547. static void user_log(struct timeval *tv, uint16_t index,
  548. const void *data, uint16_t size)
  549. {
  550. struct hci_dev *dev;
  551. dev = dev_lookup(index);
  552. if (!dev)
  553. return;
  554. dev->user_log++;
  555. }
  556. static void ctrl_msg(struct timeval *tv, uint16_t index,
  557. const void *data, uint16_t size)
  558. {
  559. struct hci_dev *dev;
  560. dev = dev_lookup(index);
  561. if (!dev)
  562. return;
  563. dev->ctrl_msg++;
  564. }
  565. static void iso_pkt(struct timeval *tv, uint16_t index,
  566. const void *data, uint16_t size)
  567. {
  568. const struct bt_hci_iso_hdr *hdr = data;
  569. struct hci_dev *dev;
  570. data += sizeof(*hdr);
  571. size -= sizeof(*hdr);
  572. dev = dev_lookup(index);
  573. if (!dev)
  574. return;
  575. dev->num_hci++;
  576. dev->num_iso++;
  577. }
  578. static void unknown_opcode(struct timeval *tv, uint16_t index,
  579. const void *data, uint16_t size)
  580. {
  581. struct hci_dev *dev;
  582. dev = dev_lookup(index);
  583. if (!dev)
  584. return;
  585. dev->unknown++;
  586. }
  587. void analyze_trace(const char *path)
  588. {
  589. struct btsnoop *btsnoop_file;
  590. unsigned long num_packets = 0;
  591. uint32_t format;
  592. btsnoop_file = btsnoop_open(path, BTSNOOP_FLAG_PKLG_SUPPORT);
  593. if (!btsnoop_file)
  594. return;
  595. format = btsnoop_get_format(btsnoop_file);
  596. switch (format) {
  597. case BTSNOOP_FORMAT_HCI:
  598. case BTSNOOP_FORMAT_UART:
  599. case BTSNOOP_FORMAT_MONITOR:
  600. break;
  601. default:
  602. fprintf(stderr, "Unsupported packet format\n");
  603. goto done;
  604. }
  605. dev_list = queue_new();
  606. while (1) {
  607. unsigned char buf[BTSNOOP_MAX_PACKET_SIZE];
  608. struct timeval tv;
  609. uint16_t index, opcode, pktlen;
  610. if (!btsnoop_read_hci(btsnoop_file, &tv, &index, &opcode,
  611. buf, &pktlen))
  612. break;
  613. switch (opcode) {
  614. case BTSNOOP_OPCODE_NEW_INDEX:
  615. new_index(&tv, index, buf, pktlen);
  616. break;
  617. case BTSNOOP_OPCODE_DEL_INDEX:
  618. del_index(&tv, index, buf, pktlen);
  619. break;
  620. case BTSNOOP_OPCODE_COMMAND_PKT:
  621. command_pkt(&tv, index, buf, pktlen);
  622. break;
  623. case BTSNOOP_OPCODE_EVENT_PKT:
  624. event_pkt(&tv, index, buf, pktlen);
  625. break;
  626. case BTSNOOP_OPCODE_ACL_TX_PKT:
  627. acl_pkt(&tv, index, true, buf, pktlen);
  628. break;
  629. case BTSNOOP_OPCODE_ACL_RX_PKT:
  630. acl_pkt(&tv, index, false, buf, pktlen);
  631. break;
  632. case BTSNOOP_OPCODE_SCO_TX_PKT:
  633. case BTSNOOP_OPCODE_SCO_RX_PKT:
  634. sco_pkt(&tv, index, buf, pktlen);
  635. break;
  636. case BTSNOOP_OPCODE_OPEN_INDEX:
  637. case BTSNOOP_OPCODE_CLOSE_INDEX:
  638. break;
  639. case BTSNOOP_OPCODE_INDEX_INFO:
  640. info_index(&tv, index, buf, pktlen);
  641. break;
  642. case BTSNOOP_OPCODE_VENDOR_DIAG:
  643. vendor_diag(&tv, index, buf, pktlen);
  644. break;
  645. case BTSNOOP_OPCODE_SYSTEM_NOTE:
  646. system_note(&tv, index, buf, pktlen);
  647. break;
  648. case BTSNOOP_OPCODE_USER_LOGGING:
  649. user_log(&tv, index, buf, pktlen);
  650. break;
  651. case BTSNOOP_OPCODE_CTRL_OPEN:
  652. case BTSNOOP_OPCODE_CTRL_CLOSE:
  653. case BTSNOOP_OPCODE_CTRL_COMMAND:
  654. case BTSNOOP_OPCODE_CTRL_EVENT:
  655. ctrl_msg(&tv, index, buf, pktlen);
  656. break;
  657. case BTSNOOP_OPCODE_ISO_TX_PKT:
  658. case BTSNOOP_OPCODE_ISO_RX_PKT:
  659. iso_pkt(&tv, index, buf, pktlen);
  660. break;
  661. default:
  662. unknown_opcode(&tv, index, buf, pktlen);
  663. break;
  664. }
  665. num_packets++;
  666. }
  667. printf("Trace contains %lu packets\n\n", num_packets);
  668. queue_destroy(dev_list, dev_destroy);
  669. done:
  670. btsnoop_unref(btsnoop_file);
  671. }