control.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570
  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 <stdbool.h>
  17. #include <stddef.h>
  18. #include <errno.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/time.h>
  23. #include <sys/socket.h>
  24. #include <sys/un.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <termios.h>
  28. #include <fcntl.h>
  29. #include <linux/filter.h>
  30. #include "lib/bluetooth.h"
  31. #include "lib/hci.h"
  32. #include "lib/mgmt.h"
  33. #include "src/shared/util.h"
  34. #include "src/shared/btsnoop.h"
  35. #include "src/shared/mainloop.h"
  36. #include "display.h"
  37. #include "packet.h"
  38. #include "hcidump.h"
  39. #include "ellisys.h"
  40. #include "tty.h"
  41. #include "control.h"
  42. #include "jlink.h"
  43. static struct btsnoop *btsnoop_file = NULL;
  44. static bool hcidump_fallback = false;
  45. static bool decode_control = true;
  46. static uint16_t filter_index = HCI_DEV_NONE;
  47. struct control_data {
  48. uint16_t channel;
  49. int fd;
  50. unsigned char buf[BTSNOOP_MAX_PACKET_SIZE];
  51. uint16_t offset;
  52. };
  53. static void free_data(void *user_data)
  54. {
  55. struct control_data *data = user_data;
  56. close(data->fd);
  57. free(data);
  58. }
  59. static void mgmt_index_added(uint16_t len, const void *buf)
  60. {
  61. printf("@ Index Added\n");
  62. packet_hexdump(buf, len);
  63. }
  64. static void mgmt_index_removed(uint16_t len, const void *buf)
  65. {
  66. printf("@ Index Removed\n");
  67. packet_hexdump(buf, len);
  68. }
  69. static void mgmt_unconf_index_added(uint16_t len, const void *buf)
  70. {
  71. printf("@ Unconfigured Index Added\n");
  72. packet_hexdump(buf, len);
  73. }
  74. static void mgmt_unconf_index_removed(uint16_t len, const void *buf)
  75. {
  76. printf("@ Unconfigured Index Removed\n");
  77. packet_hexdump(buf, len);
  78. }
  79. static void mgmt_ext_index_added(uint16_t len, const void *buf)
  80. {
  81. const struct mgmt_ev_ext_index_added *ev = buf;
  82. if (len < sizeof(*ev)) {
  83. printf("* Malformed Extended Index Added control\n");
  84. return;
  85. }
  86. printf("@ Extended Index Added: %u (%u)\n", ev->type, ev->bus);
  87. buf += sizeof(*ev);
  88. len -= sizeof(*ev);
  89. packet_hexdump(buf, len);
  90. }
  91. static void mgmt_ext_index_removed(uint16_t len, const void *buf)
  92. {
  93. const struct mgmt_ev_ext_index_removed *ev = buf;
  94. if (len < sizeof(*ev)) {
  95. printf("* Malformed Extended Index Removed control\n");
  96. return;
  97. }
  98. printf("@ Extended Index Removed: %u (%u)\n", ev->type, ev->bus);
  99. buf += sizeof(*ev);
  100. len -= sizeof(*ev);
  101. packet_hexdump(buf, len);
  102. }
  103. static void mgmt_controller_error(uint16_t len, const void *buf)
  104. {
  105. const struct mgmt_ev_controller_error *ev = buf;
  106. if (len < sizeof(*ev)) {
  107. printf("* Malformed Controller Error control\n");
  108. return;
  109. }
  110. printf("@ Controller Error: 0x%2.2x\n", ev->error_code);
  111. buf += sizeof(*ev);
  112. len -= sizeof(*ev);
  113. packet_hexdump(buf, len);
  114. }
  115. #ifndef NELEM
  116. #define NELEM(x) (sizeof(x) / sizeof((x)[0]))
  117. #endif
  118. static const char *config_options_str[] = {
  119. "external", "public-address",
  120. };
  121. static void mgmt_new_config_options(uint16_t len, const void *buf)
  122. {
  123. uint32_t options;
  124. unsigned int i;
  125. if (len < 4) {
  126. printf("* Malformed New Configuration Options control\n");
  127. return;
  128. }
  129. options = get_le32(buf);
  130. printf("@ New Configuration Options: 0x%4.4x\n", options);
  131. if (options) {
  132. printf("%-12c", ' ');
  133. for (i = 0; i < NELEM(config_options_str); i++) {
  134. if (options & (1 << i))
  135. printf("%s ", config_options_str[i]);
  136. }
  137. printf("\n");
  138. }
  139. buf += 4;
  140. len -= 4;
  141. packet_hexdump(buf, len);
  142. }
  143. static const char *settings_str[] = {
  144. "powered", "connectable", "fast-connectable", "discoverable",
  145. "bondable", "link-security", "ssp", "br/edr", "hs", "le",
  146. "advertising", "secure-conn", "debug-keys", "privacy",
  147. "configuration", "static-addr", "phy", "wbs"
  148. };
  149. static void mgmt_new_settings(uint16_t len, const void *buf)
  150. {
  151. uint32_t settings;
  152. unsigned int i;
  153. if (len < 4) {
  154. printf("* Malformed New Settings control\n");
  155. return;
  156. }
  157. settings = get_le32(buf);
  158. printf("@ New Settings: 0x%4.4x\n", settings);
  159. if (settings) {
  160. printf("%-12c", ' ');
  161. for (i = 0; i < NELEM(settings_str); i++) {
  162. if (settings & (1 << i))
  163. printf("%s ", settings_str[i]);
  164. }
  165. printf("\n");
  166. }
  167. buf += 4;
  168. len -= 4;
  169. packet_hexdump(buf, len);
  170. }
  171. static void mgmt_class_of_dev_changed(uint16_t len, const void *buf)
  172. {
  173. const struct mgmt_ev_class_of_dev_changed *ev = buf;
  174. if (len < sizeof(*ev)) {
  175. printf("* Malformed Class of Device Changed control\n");
  176. return;
  177. }
  178. printf("@ Class of Device Changed: 0x%2.2x%2.2x%2.2x\n",
  179. ev->dev_class[2],
  180. ev->dev_class[1],
  181. ev->dev_class[0]);
  182. buf += sizeof(*ev);
  183. len -= sizeof(*ev);
  184. packet_hexdump(buf, len);
  185. }
  186. static void mgmt_local_name_changed(uint16_t len, const void *buf)
  187. {
  188. const struct mgmt_ev_local_name_changed *ev = buf;
  189. if (len < sizeof(*ev)) {
  190. printf("* Malformed Local Name Changed control\n");
  191. return;
  192. }
  193. printf("@ Local Name Changed: %s (%s)\n", ev->name, ev->short_name);
  194. buf += sizeof(*ev);
  195. len -= sizeof(*ev);
  196. packet_hexdump(buf, len);
  197. }
  198. static void mgmt_new_link_key(uint16_t len, const void *buf)
  199. {
  200. const struct mgmt_ev_new_link_key *ev = buf;
  201. const char *type;
  202. char str[18];
  203. static const char *types[] = {
  204. "Combination key",
  205. "Local Unit key",
  206. "Remote Unit key",
  207. "Debug Combination key",
  208. "Unauthenticated Combination key from P-192",
  209. "Authenticated Combination key from P-192",
  210. "Changed Combination key",
  211. "Unauthenticated Combination key from P-256",
  212. "Authenticated Combination key from P-256",
  213. };
  214. if (len < sizeof(*ev)) {
  215. printf("* Malformed New Link Key control\n");
  216. return;
  217. }
  218. if (ev->key.type < NELEM(types))
  219. type = types[ev->key.type];
  220. else
  221. type = "Reserved";
  222. ba2str(&ev->key.addr.bdaddr, str);
  223. printf("@ New Link Key: %s (%d) %s (%u)\n", str,
  224. ev->key.addr.type, type, ev->key.type);
  225. buf += sizeof(*ev);
  226. len -= sizeof(*ev);
  227. packet_hexdump(buf, len);
  228. }
  229. static void mgmt_new_long_term_key(uint16_t len, const void *buf)
  230. {
  231. const struct mgmt_ev_new_long_term_key *ev = buf;
  232. const char *type;
  233. char str[18];
  234. if (len < sizeof(*ev)) {
  235. printf("* Malformed New Long Term Key control\n");
  236. return;
  237. }
  238. /* LE SC keys are both for central and peripheral */
  239. switch (ev->key.type) {
  240. case 0x00:
  241. if (ev->key.central)
  242. type = "Central (Unauthenticated)";
  243. else
  244. type = "Peripheral (Unauthenticated)";
  245. break;
  246. case 0x01:
  247. if (ev->key.central)
  248. type = "Central (Authenticated)";
  249. else
  250. type = "Peripheral (Authenticated)";
  251. break;
  252. case 0x02:
  253. type = "SC (Unauthenticated)";
  254. break;
  255. case 0x03:
  256. type = "SC (Authenticated)";
  257. break;
  258. case 0x04:
  259. type = "SC (Debug)";
  260. break;
  261. default:
  262. type = "<unknown>";
  263. break;
  264. }
  265. ba2str(&ev->key.addr.bdaddr, str);
  266. printf("@ New Long Term Key: %s (%d) %s 0x%02x\n", str,
  267. ev->key.addr.type, type, ev->key.type);
  268. buf += sizeof(*ev);
  269. len -= sizeof(*ev);
  270. packet_hexdump(buf, len);
  271. }
  272. static void mgmt_device_connected(uint16_t len, const void *buf)
  273. {
  274. const struct mgmt_ev_device_connected *ev = buf;
  275. uint32_t flags;
  276. char str[18];
  277. if (len < sizeof(*ev)) {
  278. printf("* Malformed Device Connected control\n");
  279. return;
  280. }
  281. flags = le32_to_cpu(ev->flags);
  282. ba2str(&ev->addr.bdaddr, str);
  283. printf("@ Device Connected: %s (%d) flags 0x%4.4x\n",
  284. str, ev->addr.type, flags);
  285. buf += sizeof(*ev);
  286. len -= sizeof(*ev);
  287. packet_hexdump(buf, len);
  288. }
  289. static void mgmt_device_disconnected(uint16_t len, const void *buf)
  290. {
  291. const struct mgmt_ev_device_disconnected *ev = buf;
  292. char str[18];
  293. uint8_t reason;
  294. uint16_t consumed_len;
  295. if (len < sizeof(struct mgmt_addr_info)) {
  296. printf("* Malformed Device Disconnected control\n");
  297. return;
  298. }
  299. if (len < sizeof(*ev)) {
  300. reason = MGMT_DEV_DISCONN_UNKNOWN;
  301. consumed_len = len;
  302. } else {
  303. reason = ev->reason;
  304. consumed_len = sizeof(*ev);
  305. }
  306. ba2str(&ev->addr.bdaddr, str);
  307. printf("@ Device Disconnected: %s (%d) reason %u\n", str, ev->addr.type,
  308. reason);
  309. buf += consumed_len;
  310. len -= consumed_len;
  311. packet_hexdump(buf, len);
  312. }
  313. static void mgmt_connect_failed(uint16_t len, const void *buf)
  314. {
  315. const struct mgmt_ev_connect_failed *ev = buf;
  316. char str[18];
  317. if (len < sizeof(*ev)) {
  318. printf("* Malformed Connect Failed control\n");
  319. return;
  320. }
  321. ba2str(&ev->addr.bdaddr, str);
  322. printf("@ Connect Failed: %s (%d) status 0x%2.2x\n",
  323. str, ev->addr.type, ev->status);
  324. buf += sizeof(*ev);
  325. len -= sizeof(*ev);
  326. packet_hexdump(buf, len);
  327. }
  328. static void mgmt_pin_code_request(uint16_t len, const void *buf)
  329. {
  330. const struct mgmt_ev_pin_code_request *ev = buf;
  331. char str[18];
  332. if (len < sizeof(*ev)) {
  333. printf("* Malformed PIN Code Request control\n");
  334. return;
  335. }
  336. ba2str(&ev->addr.bdaddr, str);
  337. printf("@ PIN Code Request: %s (%d) secure 0x%2.2x\n",
  338. str, ev->addr.type, ev->secure);
  339. buf += sizeof(*ev);
  340. len -= sizeof(*ev);
  341. packet_hexdump(buf, len);
  342. }
  343. static void mgmt_user_confirm_request(uint16_t len, const void *buf)
  344. {
  345. const struct mgmt_ev_user_confirm_request *ev = buf;
  346. char str[18];
  347. if (len < sizeof(*ev)) {
  348. printf("* Malformed User Confirmation Request control\n");
  349. return;
  350. }
  351. ba2str(&ev->addr.bdaddr, str);
  352. printf("@ User Confirmation Request: %s (%d) hint %d value %d\n",
  353. str, ev->addr.type, ev->confirm_hint, ev->value);
  354. buf += sizeof(*ev);
  355. len -= sizeof(*ev);
  356. packet_hexdump(buf, len);
  357. }
  358. static void mgmt_user_passkey_request(uint16_t len, const void *buf)
  359. {
  360. const struct mgmt_ev_user_passkey_request *ev = buf;
  361. char str[18];
  362. if (len < sizeof(*ev)) {
  363. printf("* Malformed User Passkey Request control\n");
  364. return;
  365. }
  366. ba2str(&ev->addr.bdaddr, str);
  367. printf("@ User Passkey Request: %s (%d)\n", str, ev->addr.type);
  368. buf += sizeof(*ev);
  369. len -= sizeof(*ev);
  370. packet_hexdump(buf, len);
  371. }
  372. static void mgmt_auth_failed(uint16_t len, const void *buf)
  373. {
  374. const struct mgmt_ev_auth_failed *ev = buf;
  375. char str[18];
  376. if (len < sizeof(*ev)) {
  377. printf("* Malformed Authentication Failed control\n");
  378. return;
  379. }
  380. ba2str(&ev->addr.bdaddr, str);
  381. printf("@ Authentication Failed: %s (%d) status 0x%2.2x\n",
  382. str, ev->addr.type, ev->status);
  383. buf += sizeof(*ev);
  384. len -= sizeof(*ev);
  385. packet_hexdump(buf, len);
  386. }
  387. static void mgmt_device_found(uint16_t len, const void *buf)
  388. {
  389. const struct mgmt_ev_device_found *ev = buf;
  390. uint32_t flags;
  391. char str[18];
  392. if (len < sizeof(*ev)) {
  393. printf("* Malformed Device Found control\n");
  394. return;
  395. }
  396. flags = le32_to_cpu(ev->flags);
  397. ba2str(&ev->addr.bdaddr, str);
  398. printf("@ Device Found: %s (%d) rssi %d flags 0x%4.4x\n",
  399. str, ev->addr.type, ev->rssi, flags);
  400. buf += sizeof(*ev);
  401. len -= sizeof(*ev);
  402. packet_hexdump(buf, len);
  403. }
  404. static void mgmt_discovering(uint16_t len, const void *buf)
  405. {
  406. const struct mgmt_ev_discovering *ev = buf;
  407. if (len < sizeof(*ev)) {
  408. printf("* Malformed Discovering control\n");
  409. return;
  410. }
  411. printf("@ Discovering: 0x%2.2x (%d)\n", ev->discovering, ev->type);
  412. buf += sizeof(*ev);
  413. len -= sizeof(*ev);
  414. packet_hexdump(buf, len);
  415. }
  416. static void mgmt_device_blocked(uint16_t len, const void *buf)
  417. {
  418. const struct mgmt_ev_device_blocked *ev = buf;
  419. char str[18];
  420. if (len < sizeof(*ev)) {
  421. printf("* Malformed Device Blocked control\n");
  422. return;
  423. }
  424. ba2str(&ev->addr.bdaddr, str);
  425. printf("@ Device Blocked: %s (%d)\n", str, ev->addr.type);
  426. buf += sizeof(*ev);
  427. len -= sizeof(*ev);
  428. packet_hexdump(buf, len);
  429. }
  430. static void mgmt_device_unblocked(uint16_t len, const void *buf)
  431. {
  432. const struct mgmt_ev_device_unblocked *ev = buf;
  433. char str[18];
  434. if (len < sizeof(*ev)) {
  435. printf("* Malformed Device Unblocked control\n");
  436. return;
  437. }
  438. ba2str(&ev->addr.bdaddr, str);
  439. printf("@ Device Unblocked: %s (%d)\n", str, ev->addr.type);
  440. buf += sizeof(*ev);
  441. len -= sizeof(*ev);
  442. packet_hexdump(buf, len);
  443. }
  444. static void mgmt_device_unpaired(uint16_t len, const void *buf)
  445. {
  446. const struct mgmt_ev_device_unpaired *ev = buf;
  447. char str[18];
  448. if (len < sizeof(*ev)) {
  449. printf("* Malformed Device Unpaired control\n");
  450. return;
  451. }
  452. ba2str(&ev->addr.bdaddr, str);
  453. printf("@ Device Unpaired: %s (%d)\n", str, ev->addr.type);
  454. buf += sizeof(*ev);
  455. len -= sizeof(*ev);
  456. packet_hexdump(buf, len);
  457. }
  458. static void mgmt_passkey_notify(uint16_t len, const void *buf)
  459. {
  460. const struct mgmt_ev_passkey_notify *ev = buf;
  461. uint32_t passkey;
  462. char str[18];
  463. if (len < sizeof(*ev)) {
  464. printf("* Malformed Passkey Notify control\n");
  465. return;
  466. }
  467. ba2str(&ev->addr.bdaddr, str);
  468. passkey = le32_to_cpu(ev->passkey);
  469. printf("@ Passkey Notify: %s (%d) passkey %06u entered %u\n",
  470. str, ev->addr.type, passkey, ev->entered);
  471. buf += sizeof(*ev);
  472. len -= sizeof(*ev);
  473. packet_hexdump(buf, len);
  474. }
  475. static void mgmt_new_irk(uint16_t len, const void *buf)
  476. {
  477. const struct mgmt_ev_new_irk *ev = buf;
  478. char addr[18], rpa[18];
  479. if (len < sizeof(*ev)) {
  480. printf("* Malformed New IRK control\n");
  481. return;
  482. }
  483. ba2str(&ev->rpa, rpa);
  484. ba2str(&ev->key.addr.bdaddr, addr);
  485. printf("@ New IRK: %s (%d) %s\n", addr, ev->key.addr.type, rpa);
  486. buf += sizeof(*ev);
  487. len -= sizeof(*ev);
  488. packet_hexdump(buf, len);
  489. }
  490. static void mgmt_new_csrk(uint16_t len, const void *buf)
  491. {
  492. const struct mgmt_ev_new_csrk *ev = buf;
  493. const char *type;
  494. char addr[18];
  495. if (len < sizeof(*ev)) {
  496. printf("* Malformed New CSRK control\n");
  497. return;
  498. }
  499. ba2str(&ev->key.addr.bdaddr, addr);
  500. switch (ev->key.type) {
  501. case 0x00:
  502. type = "Local Unauthenticated";
  503. break;
  504. case 0x01:
  505. type = "Remote Unauthenticated";
  506. break;
  507. case 0x02:
  508. type = "Local Authenticated";
  509. break;
  510. case 0x03:
  511. type = "Remote Authenticated";
  512. break;
  513. default:
  514. type = "<unknown>";
  515. break;
  516. }
  517. printf("@ New CSRK: %s (%d) %s (%u)\n", addr, ev->key.addr.type,
  518. type, ev->key.type);
  519. buf += sizeof(*ev);
  520. len -= sizeof(*ev);
  521. packet_hexdump(buf, len);
  522. }
  523. static void mgmt_device_added(uint16_t len, const void *buf)
  524. {
  525. const struct mgmt_ev_device_added *ev = buf;
  526. char str[18];
  527. if (len < sizeof(*ev)) {
  528. printf("* Malformed Device Added control\n");
  529. return;
  530. }
  531. ba2str(&ev->addr.bdaddr, str);
  532. printf("@ Device Added: %s (%d) %d\n", str, ev->addr.type, ev->action);
  533. buf += sizeof(*ev);
  534. len -= sizeof(*ev);
  535. packet_hexdump(buf, len);
  536. }
  537. static void mgmt_device_removed(uint16_t len, const void *buf)
  538. {
  539. const struct mgmt_ev_device_removed *ev = buf;
  540. char str[18];
  541. if (len < sizeof(*ev)) {
  542. printf("* Malformed Device Removed control\n");
  543. return;
  544. }
  545. ba2str(&ev->addr.bdaddr, str);
  546. printf("@ Device Removed: %s (%d)\n", str, ev->addr.type);
  547. buf += sizeof(*ev);
  548. len -= sizeof(*ev);
  549. packet_hexdump(buf, len);
  550. }
  551. static void mgmt_new_conn_param(uint16_t len, const void *buf)
  552. {
  553. const struct mgmt_ev_new_conn_param *ev = buf;
  554. char addr[18];
  555. uint16_t min, max, latency, timeout;
  556. if (len < sizeof(*ev)) {
  557. printf("* Malformed New Connection Parameter control\n");
  558. return;
  559. }
  560. ba2str(&ev->addr.bdaddr, addr);
  561. min = le16_to_cpu(ev->min_interval);
  562. max = le16_to_cpu(ev->max_interval);
  563. latency = le16_to_cpu(ev->latency);
  564. timeout = le16_to_cpu(ev->timeout);
  565. printf("@ New Conn Param: %s (%d) hint %d min 0x%4.4x max 0x%4.4x "
  566. "latency 0x%4.4x timeout 0x%4.4x\n", addr, ev->addr.type,
  567. ev->store_hint, min, max, latency, timeout);
  568. buf += sizeof(*ev);
  569. len -= sizeof(*ev);
  570. packet_hexdump(buf, len);
  571. }
  572. static void mgmt_advertising_added(uint16_t len, const void *buf)
  573. {
  574. const struct mgmt_ev_advertising_added *ev = buf;
  575. if (len < sizeof(*ev)) {
  576. printf("* Malformed Advertising Added control\n");
  577. return;
  578. }
  579. printf("@ Advertising Added: %u\n", ev->instance);
  580. buf += sizeof(*ev);
  581. len -= sizeof(*ev);
  582. packet_hexdump(buf, len);
  583. }
  584. static void mgmt_advertising_removed(uint16_t len, const void *buf)
  585. {
  586. const struct mgmt_ev_advertising_removed *ev = buf;
  587. if (len < sizeof(*ev)) {
  588. printf("* Malformed Advertising Removed control\n");
  589. return;
  590. }
  591. printf("@ Advertising Removed: %u\n", ev->instance);
  592. buf += sizeof(*ev);
  593. len -= sizeof(*ev);
  594. packet_hexdump(buf, len);
  595. }
  596. void control_message(uint16_t opcode, const void *data, uint16_t size)
  597. {
  598. if (!decode_control)
  599. return;
  600. switch (opcode) {
  601. case MGMT_EV_INDEX_ADDED:
  602. mgmt_index_added(size, data);
  603. break;
  604. case MGMT_EV_INDEX_REMOVED:
  605. mgmt_index_removed(size, data);
  606. break;
  607. case MGMT_EV_CONTROLLER_ERROR:
  608. mgmt_controller_error(size, data);
  609. break;
  610. case MGMT_EV_NEW_SETTINGS:
  611. mgmt_new_settings(size, data);
  612. break;
  613. case MGMT_EV_CLASS_OF_DEV_CHANGED:
  614. mgmt_class_of_dev_changed(size, data);
  615. break;
  616. case MGMT_EV_LOCAL_NAME_CHANGED:
  617. mgmt_local_name_changed(size, data);
  618. break;
  619. case MGMT_EV_NEW_LINK_KEY:
  620. mgmt_new_link_key(size, data);
  621. break;
  622. case MGMT_EV_NEW_LONG_TERM_KEY:
  623. mgmt_new_long_term_key(size, data);
  624. break;
  625. case MGMT_EV_DEVICE_CONNECTED:
  626. mgmt_device_connected(size, data);
  627. break;
  628. case MGMT_EV_DEVICE_DISCONNECTED:
  629. mgmt_device_disconnected(size, data);
  630. break;
  631. case MGMT_EV_CONNECT_FAILED:
  632. mgmt_connect_failed(size, data);
  633. break;
  634. case MGMT_EV_PIN_CODE_REQUEST:
  635. mgmt_pin_code_request(size, data);
  636. break;
  637. case MGMT_EV_USER_CONFIRM_REQUEST:
  638. mgmt_user_confirm_request(size, data);
  639. break;
  640. case MGMT_EV_USER_PASSKEY_REQUEST:
  641. mgmt_user_passkey_request(size, data);
  642. break;
  643. case MGMT_EV_AUTH_FAILED:
  644. mgmt_auth_failed(size, data);
  645. break;
  646. case MGMT_EV_DEVICE_FOUND:
  647. mgmt_device_found(size, data);
  648. break;
  649. case MGMT_EV_DISCOVERING:
  650. mgmt_discovering(size, data);
  651. break;
  652. case MGMT_EV_DEVICE_BLOCKED:
  653. mgmt_device_blocked(size, data);
  654. break;
  655. case MGMT_EV_DEVICE_UNBLOCKED:
  656. mgmt_device_unblocked(size, data);
  657. break;
  658. case MGMT_EV_DEVICE_UNPAIRED:
  659. mgmt_device_unpaired(size, data);
  660. break;
  661. case MGMT_EV_PASSKEY_NOTIFY:
  662. mgmt_passkey_notify(size, data);
  663. break;
  664. case MGMT_EV_NEW_IRK:
  665. mgmt_new_irk(size, data);
  666. break;
  667. case MGMT_EV_NEW_CSRK:
  668. mgmt_new_csrk(size, data);
  669. break;
  670. case MGMT_EV_DEVICE_ADDED:
  671. mgmt_device_added(size, data);
  672. break;
  673. case MGMT_EV_DEVICE_REMOVED:
  674. mgmt_device_removed(size, data);
  675. break;
  676. case MGMT_EV_NEW_CONN_PARAM:
  677. mgmt_new_conn_param(size, data);
  678. break;
  679. case MGMT_EV_UNCONF_INDEX_ADDED:
  680. mgmt_unconf_index_added(size, data);
  681. break;
  682. case MGMT_EV_UNCONF_INDEX_REMOVED:
  683. mgmt_unconf_index_removed(size, data);
  684. break;
  685. case MGMT_EV_NEW_CONFIG_OPTIONS:
  686. mgmt_new_config_options(size, data);
  687. break;
  688. case MGMT_EV_EXT_INDEX_ADDED:
  689. mgmt_ext_index_added(size, data);
  690. break;
  691. case MGMT_EV_EXT_INDEX_REMOVED:
  692. mgmt_ext_index_removed(size, data);
  693. break;
  694. case MGMT_EV_ADVERTISING_ADDED:
  695. mgmt_advertising_added(size, data);
  696. break;
  697. case MGMT_EV_ADVERTISING_REMOVED:
  698. mgmt_advertising_removed(size, data);
  699. break;
  700. default:
  701. printf("* Unknown control (code %d len %d)\n", opcode, size);
  702. packet_hexdump(data, size);
  703. break;
  704. }
  705. }
  706. static void data_callback(int fd, uint32_t events, void *user_data)
  707. {
  708. struct control_data *data = user_data;
  709. unsigned char control[64];
  710. struct mgmt_hdr hdr;
  711. struct msghdr msg;
  712. struct iovec iov[2];
  713. if (events & (EPOLLERR | EPOLLHUP)) {
  714. mainloop_remove_fd(data->fd);
  715. return;
  716. }
  717. iov[0].iov_base = &hdr;
  718. iov[0].iov_len = MGMT_HDR_SIZE;
  719. iov[1].iov_base = data->buf;
  720. iov[1].iov_len = sizeof(data->buf);
  721. memset(&msg, 0, sizeof(msg));
  722. msg.msg_iov = iov;
  723. msg.msg_iovlen = 2;
  724. msg.msg_control = control;
  725. msg.msg_controllen = sizeof(control);
  726. while (1) {
  727. struct cmsghdr *cmsg;
  728. struct timeval *tv = NULL;
  729. struct timeval ctv;
  730. struct ucred *cred = NULL;
  731. struct ucred ccred;
  732. uint16_t opcode, index, pktlen;
  733. ssize_t len;
  734. len = recvmsg(data->fd, &msg, MSG_DONTWAIT);
  735. if (len < 0)
  736. break;
  737. if (len < MGMT_HDR_SIZE)
  738. break;
  739. for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
  740. cmsg = CMSG_NXTHDR(&msg, cmsg)) {
  741. if (cmsg->cmsg_level != SOL_SOCKET)
  742. continue;
  743. if (cmsg->cmsg_type == SCM_TIMESTAMP) {
  744. memcpy(&ctv, CMSG_DATA(cmsg), sizeof(ctv));
  745. tv = &ctv;
  746. }
  747. if (cmsg->cmsg_type == SCM_CREDENTIALS) {
  748. memcpy(&ccred, CMSG_DATA(cmsg), sizeof(ccred));
  749. cred = &ccred;
  750. }
  751. }
  752. opcode = le16_to_cpu(hdr.opcode);
  753. index = le16_to_cpu(hdr.index);
  754. pktlen = le16_to_cpu(hdr.len);
  755. switch (data->channel) {
  756. case HCI_CHANNEL_CONTROL:
  757. packet_control(tv, cred, index, opcode,
  758. data->buf, pktlen);
  759. break;
  760. case HCI_CHANNEL_MONITOR:
  761. btsnoop_write_hci(btsnoop_file, tv, index, opcode, 0,
  762. data->buf, pktlen);
  763. ellisys_inject_hci(tv, index, opcode,
  764. data->buf, pktlen);
  765. packet_monitor(tv, cred, index, opcode,
  766. data->buf, pktlen);
  767. break;
  768. }
  769. }
  770. }
  771. static int open_socket(uint16_t channel)
  772. {
  773. struct sockaddr_hci addr;
  774. int fd, opt = 1;
  775. fd = socket(AF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC, BTPROTO_HCI);
  776. if (fd < 0) {
  777. perror("Failed to open channel");
  778. return -1;
  779. }
  780. memset(&addr, 0, sizeof(addr));
  781. addr.hci_family = AF_BLUETOOTH;
  782. addr.hci_dev = HCI_DEV_NONE;
  783. addr.hci_channel = channel;
  784. if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  785. if (errno == EINVAL) {
  786. /* Fallback to hcidump support */
  787. hcidump_fallback = true;
  788. close(fd);
  789. return -1;
  790. }
  791. perror("Failed to bind channel");
  792. close(fd);
  793. return -1;
  794. }
  795. if (setsockopt(fd, SOL_SOCKET, SO_TIMESTAMP, &opt, sizeof(opt)) < 0) {
  796. perror("Failed to enable timestamps");
  797. close(fd);
  798. return -1;
  799. }
  800. if (setsockopt(fd, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(opt)) < 0) {
  801. perror("Failed to enable credentials");
  802. close(fd);
  803. return -1;
  804. }
  805. return fd;
  806. }
  807. static void attach_index_filter(int fd, uint16_t index)
  808. {
  809. struct sock_filter filters[] = {
  810. /* Load MGMT index:
  811. * A <- MGMT index
  812. */
  813. BPF_STMT(BPF_LD + BPF_B + BPF_ABS,
  814. offsetof(struct mgmt_hdr, index)),
  815. /* Accept if index is HCI_DEV_NONE:
  816. * A == HCI_DEV_NONE
  817. */
  818. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, HCI_DEV_NONE, 0, 1),
  819. /* return */
  820. BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* pass */
  821. /* Accept if index match:
  822. * A == index
  823. */
  824. BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, index, 0, 1),
  825. /* returns */
  826. BPF_STMT(BPF_RET|BPF_K, 0x0fffffff), /* pass */
  827. BPF_STMT(BPF_RET|BPF_K, 0), /* reject */
  828. };
  829. struct sock_fprog fprog = {
  830. .len = sizeof(filters) / sizeof(filters[0]),
  831. /* casting const away: */
  832. .filter = filters,
  833. };
  834. setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &fprog, sizeof(fprog));
  835. }
  836. static int open_channel(uint16_t channel)
  837. {
  838. struct control_data *data;
  839. data = malloc(sizeof(*data));
  840. if (!data)
  841. return -1;
  842. memset(data, 0, sizeof(*data));
  843. data->channel = channel;
  844. data->fd = open_socket(channel);
  845. if (data->fd < 0) {
  846. free(data);
  847. return -1;
  848. }
  849. if (filter_index != HCI_DEV_NONE)
  850. attach_index_filter(data->fd, filter_index);
  851. if (mainloop_add_fd(data->fd, EPOLLIN, data_callback,
  852. data, free_data) < 0) {
  853. close(data->fd);
  854. free(data);
  855. return -1;
  856. };
  857. return 0;
  858. }
  859. static void client_callback(int fd, uint32_t events, void *user_data)
  860. {
  861. struct control_data *data = user_data;
  862. ssize_t len;
  863. if (events & (EPOLLERR | EPOLLHUP)) {
  864. mainloop_remove_fd(data->fd);
  865. return;
  866. }
  867. len = recv(data->fd, data->buf + data->offset,
  868. sizeof(data->buf) - data->offset, MSG_DONTWAIT);
  869. if (len < 0)
  870. return;
  871. data->offset += len;
  872. while (data->offset >= MGMT_HDR_SIZE) {
  873. struct mgmt_hdr *hdr = (struct mgmt_hdr *) data->buf;
  874. uint16_t pktlen = le16_to_cpu(hdr->len);
  875. uint16_t opcode, index;
  876. if (data->offset < pktlen + MGMT_HDR_SIZE)
  877. return;
  878. opcode = le16_to_cpu(hdr->opcode);
  879. index = le16_to_cpu(hdr->index);
  880. packet_monitor(NULL, NULL, index, opcode,
  881. data->buf + MGMT_HDR_SIZE, pktlen);
  882. data->offset -= pktlen + MGMT_HDR_SIZE;
  883. if (data->offset > 0)
  884. memmove(data->buf, data->buf + MGMT_HDR_SIZE + pktlen,
  885. data->offset);
  886. }
  887. }
  888. static void server_accept_callback(int fd, uint32_t events, void *user_data)
  889. {
  890. struct control_data *data;
  891. struct sockaddr_un addr;
  892. socklen_t len;
  893. int nfd;
  894. if (events & (EPOLLERR | EPOLLHUP)) {
  895. mainloop_remove_fd(fd);
  896. return;
  897. }
  898. memset(&addr, 0, sizeof(addr));
  899. len = sizeof(addr);
  900. nfd = accept(fd, (struct sockaddr *) &addr, &len);
  901. if (nfd < 0) {
  902. perror("Failed to accept client socket");
  903. return;
  904. }
  905. printf("--- New monitor connection ---\n");
  906. data = malloc(sizeof(*data));
  907. if (!data) {
  908. close(nfd);
  909. return;
  910. }
  911. memset(data, 0, sizeof(*data));
  912. data->channel = HCI_CHANNEL_MONITOR;
  913. data->fd = nfd;
  914. if (mainloop_add_fd(data->fd, EPOLLIN, client_callback,
  915. data, free_data) < 0) {
  916. close(data->fd);
  917. free(data);
  918. }
  919. }
  920. static int server_fd = -1;
  921. void control_server(const char *path)
  922. {
  923. struct sockaddr_un addr;
  924. int fd;
  925. if (server_fd >= 0)
  926. return;
  927. if (strlen(path) > sizeof(addr.sun_path) - 1) {
  928. fprintf(stderr, "Socket name too long\n");
  929. return;
  930. }
  931. unlink(path);
  932. fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
  933. if (fd < 0) {
  934. perror("Failed to open server socket");
  935. return;
  936. }
  937. memset(&addr, 0, sizeof(addr));
  938. addr.sun_family = AF_UNIX;
  939. strcpy(addr.sun_path, path);
  940. if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  941. perror("Failed to bind server socket");
  942. close(fd);
  943. return;
  944. }
  945. if (listen(fd, 5) < 0) {
  946. perror("Failed to listen server socket");
  947. close(fd);
  948. return;
  949. }
  950. if (mainloop_add_fd(fd, EPOLLIN, server_accept_callback,
  951. NULL, NULL) < 0) {
  952. close(fd);
  953. return;
  954. }
  955. server_fd = fd;
  956. }
  957. static bool parse_drops(uint8_t **data, uint8_t *len, uint8_t *drops,
  958. uint32_t *total)
  959. {
  960. if (*len < 1)
  961. return false;
  962. *drops = **data;
  963. *total += *drops;
  964. (*data)++;
  965. (*len)--;
  966. return true;
  967. }
  968. static bool tty_parse_header(uint8_t *hdr, uint8_t len, struct timeval **tv,
  969. struct timeval *ctv, uint32_t *drops)
  970. {
  971. uint8_t cmd = 0;
  972. uint8_t evt = 0;
  973. uint8_t acl_tx = 0;
  974. uint8_t acl_rx = 0;
  975. uint8_t sco_tx = 0;
  976. uint8_t sco_rx = 0;
  977. uint8_t other = 0;
  978. uint32_t total = 0;
  979. uint32_t ts32;
  980. while (len) {
  981. uint8_t type = hdr[0];
  982. hdr++; len--;
  983. switch (type) {
  984. case TTY_EXTHDR_COMMAND_DROPS:
  985. if (!parse_drops(&hdr, &len, &cmd, &total))
  986. return false;
  987. break;
  988. case TTY_EXTHDR_EVENT_DROPS:
  989. if (!parse_drops(&hdr, &len, &evt, &total))
  990. return false;
  991. break;
  992. case TTY_EXTHDR_ACL_TX_DROPS:
  993. if (!parse_drops(&hdr, &len, &acl_tx, &total))
  994. return false;
  995. break;
  996. case TTY_EXTHDR_ACL_RX_DROPS:
  997. if (!parse_drops(&hdr, &len, &acl_rx, &total))
  998. return false;
  999. break;
  1000. case TTY_EXTHDR_SCO_TX_DROPS:
  1001. if (!parse_drops(&hdr, &len, &sco_tx, &total))
  1002. return false;
  1003. break;
  1004. case TTY_EXTHDR_SCO_RX_DROPS:
  1005. if (!parse_drops(&hdr, &len, &sco_rx, &total))
  1006. return false;
  1007. break;
  1008. case TTY_EXTHDR_OTHER_DROPS:
  1009. if (!parse_drops(&hdr, &len, &other, &total))
  1010. return false;
  1011. break;
  1012. case TTY_EXTHDR_TS32:
  1013. if (len < sizeof(ts32))
  1014. return false;
  1015. ts32 = get_le32(hdr);
  1016. hdr += sizeof(ts32); len -= sizeof(ts32);
  1017. /* ts32 is in units of 1/10th of a millisecond */
  1018. ctv->tv_sec = ts32 / 10000;
  1019. ctv->tv_usec = (ts32 % 10000) * 100;
  1020. *tv = ctv;
  1021. break;
  1022. default:
  1023. printf("Unknown extended header type %u\n", type);
  1024. return false;
  1025. }
  1026. }
  1027. if (total) {
  1028. *drops += total;
  1029. printf("* Drops: cmd %u evt %u acl_tx %u acl_rx %u sco_tx %u "
  1030. "sco_rx %u other %u\n", cmd, evt, acl_tx, acl_rx,
  1031. sco_tx, sco_rx, other);
  1032. }
  1033. return true;
  1034. }
  1035. static void process_data(struct control_data *data)
  1036. {
  1037. while (data->offset >= sizeof(struct tty_hdr)) {
  1038. struct tty_hdr *hdr = (struct tty_hdr *) data->buf;
  1039. uint16_t pktlen, opcode, data_len;
  1040. struct timeval *tv = NULL;
  1041. struct timeval ctv;
  1042. uint32_t drops = 0;
  1043. data_len = le16_to_cpu(hdr->data_len);
  1044. if (data->offset < 2 + data_len)
  1045. return;
  1046. if (data->offset < sizeof(*hdr) + hdr->hdr_len) {
  1047. fprintf(stderr, "Received corrupted data from TTY\n");
  1048. memmove(data->buf, data->buf + 2 + data_len,
  1049. data->offset);
  1050. return;
  1051. }
  1052. if (!tty_parse_header(hdr->ext_hdr, hdr->hdr_len,
  1053. &tv, &ctv, &drops))
  1054. fprintf(stderr, "Unable to parse extended header\n");
  1055. opcode = le16_to_cpu(hdr->opcode);
  1056. pktlen = data_len - 4 - hdr->hdr_len;
  1057. btsnoop_write_hci(btsnoop_file, tv, 0, opcode, drops,
  1058. hdr->ext_hdr + hdr->hdr_len, pktlen);
  1059. ellisys_inject_hci(tv, 0, opcode, hdr->ext_hdr + hdr->hdr_len,
  1060. pktlen);
  1061. packet_monitor(tv, NULL, 0, opcode,
  1062. hdr->ext_hdr + hdr->hdr_len, pktlen);
  1063. data->offset -= 2 + data_len;
  1064. if (data->offset > 0)
  1065. memmove(data->buf, data->buf + 2 + data_len,
  1066. data->offset);
  1067. }
  1068. }
  1069. static void tty_callback(int fd, uint32_t events, void *user_data)
  1070. {
  1071. struct control_data *data = user_data;
  1072. ssize_t len;
  1073. if (events & (EPOLLERR | EPOLLHUP)) {
  1074. mainloop_remove_fd(data->fd);
  1075. return;
  1076. }
  1077. len = read(data->fd, data->buf + data->offset,
  1078. sizeof(data->buf) - data->offset);
  1079. if (len < 0)
  1080. return;
  1081. data->offset += len;
  1082. process_data(data);
  1083. }
  1084. int control_tty(const char *path, unsigned int speed)
  1085. {
  1086. struct control_data *data;
  1087. struct termios ti;
  1088. int fd, err;
  1089. fd = open(path, O_RDWR | O_NOCTTY | O_NONBLOCK);
  1090. if (fd < 0) {
  1091. err = -errno;
  1092. perror("Failed to open serial port");
  1093. return err;
  1094. }
  1095. if (tcflush(fd, TCIOFLUSH) < 0) {
  1096. err = -errno;
  1097. perror("Failed to flush serial port");
  1098. close(fd);
  1099. return err;
  1100. }
  1101. memset(&ti, 0, sizeof(ti));
  1102. /* Switch TTY to raw mode */
  1103. cfmakeraw(&ti);
  1104. ti.c_cflag |= (CLOCAL | CREAD);
  1105. ti.c_cflag &= ~CRTSCTS;
  1106. cfsetspeed(&ti, speed);
  1107. if (tcsetattr(fd, TCSANOW, &ti) < 0) {
  1108. err = -errno;
  1109. perror("Failed to set serial port settings");
  1110. close(fd);
  1111. return err;
  1112. }
  1113. printf("--- %s opened ---\n", path);
  1114. data = malloc(sizeof(*data));
  1115. if (!data) {
  1116. close(fd);
  1117. return -ENOMEM;
  1118. }
  1119. memset(data, 0, sizeof(*data));
  1120. data->channel = HCI_CHANNEL_MONITOR;
  1121. data->fd = fd;
  1122. if (mainloop_add_fd(data->fd, EPOLLIN, tty_callback,
  1123. data, free_data) < 0) {
  1124. close(data->fd);
  1125. free(data);
  1126. return -1;
  1127. }
  1128. return 0;
  1129. }
  1130. static void rtt_callback(int id, void *user_data)
  1131. {
  1132. struct control_data *data = user_data;
  1133. ssize_t len;
  1134. do {
  1135. len = jlink_rtt_read(data->buf + data->offset,
  1136. sizeof(data->buf) - data->offset);
  1137. data->offset += len;
  1138. process_data(data);
  1139. } while (len > 0);
  1140. if (mainloop_modify_timeout(id, 1) < 0)
  1141. mainloop_exit_failure();
  1142. }
  1143. int control_rtt(char *jlink, char *rtt)
  1144. {
  1145. struct control_data *data;
  1146. if (jlink_init() < 0) {
  1147. fprintf(stderr, "Failed to initialize J-Link library\n");
  1148. return -EIO;
  1149. }
  1150. if (jlink_connect(jlink) < 0) {
  1151. fprintf(stderr, "Failed to connect to target device\n");
  1152. return -ENODEV;
  1153. }
  1154. if (jlink_start_rtt(rtt) < 0) {
  1155. fprintf(stderr, "Failed to initialize RTT\n");
  1156. return -ENODEV;
  1157. }
  1158. printf("--- RTT opened ---\n");
  1159. data = new0(struct control_data, 1);
  1160. data->channel = HCI_CHANNEL_MONITOR;
  1161. data->fd = -1;
  1162. if (mainloop_add_timeout(1, rtt_callback, data, free_data) < 0) {
  1163. free(data);
  1164. return -EIO;
  1165. }
  1166. return 0;
  1167. }
  1168. bool control_writer(const char *path)
  1169. {
  1170. btsnoop_file = btsnoop_create(path, 0, 0, BTSNOOP_FORMAT_MONITOR);
  1171. return !!btsnoop_file;
  1172. }
  1173. void control_reader(const char *path, bool pager)
  1174. {
  1175. unsigned char buf[BTSNOOP_MAX_PACKET_SIZE];
  1176. uint16_t pktlen;
  1177. uint32_t format;
  1178. struct timeval tv;
  1179. btsnoop_file = btsnoop_open(path, BTSNOOP_FLAG_PKLG_SUPPORT);
  1180. if (!btsnoop_file)
  1181. return;
  1182. format = btsnoop_get_format(btsnoop_file);
  1183. switch (format) {
  1184. case BTSNOOP_FORMAT_HCI:
  1185. case BTSNOOP_FORMAT_UART:
  1186. case BTSNOOP_FORMAT_SIMULATOR:
  1187. packet_del_filter(PACKET_FILTER_SHOW_INDEX);
  1188. break;
  1189. case BTSNOOP_FORMAT_MONITOR:
  1190. packet_add_filter(PACKET_FILTER_SHOW_INDEX);
  1191. break;
  1192. }
  1193. if (pager)
  1194. open_pager();
  1195. switch (format) {
  1196. case BTSNOOP_FORMAT_HCI:
  1197. case BTSNOOP_FORMAT_UART:
  1198. case BTSNOOP_FORMAT_MONITOR:
  1199. while (1) {
  1200. uint16_t index, opcode;
  1201. if (!btsnoop_read_hci(btsnoop_file, &tv, &index,
  1202. &opcode, buf, &pktlen))
  1203. break;
  1204. if (opcode == 0xffff)
  1205. continue;
  1206. packet_monitor(&tv, NULL, index, opcode, buf, pktlen);
  1207. ellisys_inject_hci(&tv, index, opcode, buf, pktlen);
  1208. }
  1209. break;
  1210. case BTSNOOP_FORMAT_SIMULATOR:
  1211. while (1) {
  1212. uint16_t frequency;
  1213. if (!btsnoop_read_phy(btsnoop_file, &tv, &frequency,
  1214. buf, &pktlen))
  1215. break;
  1216. packet_simulator(&tv, frequency, buf, pktlen);
  1217. }
  1218. break;
  1219. }
  1220. if (pager)
  1221. close_pager();
  1222. btsnoop_unref(btsnoop_file);
  1223. }
  1224. int control_tracing(void)
  1225. {
  1226. packet_add_filter(PACKET_FILTER_SHOW_INDEX);
  1227. if (server_fd >= 0)
  1228. return 0;
  1229. if (open_channel(HCI_CHANNEL_MONITOR) < 0) {
  1230. if (!hcidump_fallback)
  1231. return -1;
  1232. if (hcidump_tracing() < 0)
  1233. return -1;
  1234. return 0;
  1235. }
  1236. if (packet_has_filter(PACKET_FILTER_SHOW_MGMT_SOCKET))
  1237. open_channel(HCI_CHANNEL_CONTROL);
  1238. return 0;
  1239. }
  1240. void control_disable_decoding(void)
  1241. {
  1242. decode_control = false;
  1243. }
  1244. void control_filter_index(uint16_t index)
  1245. {
  1246. filter_index = index;
  1247. }