amptest.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011-2012 Intel Corporation
  7. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <alloca.h>
  18. #include <stdlib.h>
  19. #include <stdbool.h>
  20. #include <poll.h>
  21. #include <sys/ioctl.h>
  22. #include <sys/socket.h>
  23. #include "lib/bluetooth.h"
  24. #include "lib/hci.h"
  25. #include "lib/hci_lib.h"
  26. static int activate_amp_controller(int dev_id)
  27. {
  28. struct hci_dev_info di;
  29. struct hci_filter flt;
  30. int fd;
  31. printf("hci%d: Activating controller\n", dev_id);
  32. fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
  33. if (fd < 0) {
  34. perror("Failed to open raw HCI socket");
  35. return -1;
  36. }
  37. di.dev_id = dev_id;
  38. if (ioctl(fd, HCIGETDEVINFO, (void *) &di) < 0) {
  39. perror("Failed to get HCI device info");
  40. close(fd);
  41. return -1;
  42. }
  43. if (!hci_test_bit(HCI_UP, &di.flags)) {
  44. if (ioctl(fd, HCIDEVUP, dev_id) < 0) {
  45. if (errno != EALREADY) {
  46. perror("Failed to bring up HCI device");
  47. close(fd);
  48. return -1;
  49. }
  50. }
  51. }
  52. close(fd);
  53. fd = hci_open_dev(dev_id);
  54. if (fd < 0) {
  55. perror("Failed to open HCI device");
  56. return -1;
  57. }
  58. hci_filter_clear(&flt);
  59. hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
  60. hci_filter_set_event(EVT_CHANNEL_SELECTED, &flt);
  61. hci_filter_set_event(EVT_PHYSICAL_LINK_COMPLETE, &flt);
  62. hci_filter_set_event(EVT_DISCONNECT_PHYSICAL_LINK_COMPLETE, &flt);
  63. if (setsockopt(fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
  64. perror("Failed to setup HCI device filter");
  65. close(fd);
  66. return -1;
  67. }
  68. return fd;
  69. }
  70. static bool read_local_amp_info(int dev_id, uint16_t *max_assoc_len)
  71. {
  72. read_local_amp_info_rp rp;
  73. struct hci_request rq;
  74. int fd;
  75. printf("hci%d: Reading local AMP information\n", dev_id);
  76. fd = hci_open_dev(dev_id);
  77. if (fd < 0) {
  78. perror("Failed to open HCI device");
  79. return false;
  80. }
  81. memset(&rp, 0, sizeof(rp));
  82. memset(&rq, 0, sizeof(rq));
  83. rq.ogf = OGF_STATUS_PARAM;
  84. rq.ocf = OCF_READ_LOCAL_AMP_INFO;
  85. rq.rparam = &rp;
  86. rq.rlen = READ_LOCAL_AMP_INFO_RP_SIZE;
  87. if (hci_send_req(fd, &rq, 1000) < 0) {
  88. perror("Failed sending HCI request");
  89. hci_close_dev(fd);
  90. return false;
  91. }
  92. if (rp.status) {
  93. fprintf(stderr, "Failed HCI command: 0x%02x\n", rp.status);
  94. hci_close_dev(fd);
  95. return false;
  96. }
  97. printf("\tAMP status: 0x%02x\n", rp.amp_status);
  98. printf("\tController type: 0x%02x\n", rp.controller_type);
  99. printf("\tMax ASSOC length: %d\n", btohs(rp.max_amp_assoc_length));
  100. *max_assoc_len = btohs(rp.max_amp_assoc_length);
  101. hci_close_dev(fd);
  102. return true;
  103. }
  104. static bool read_local_amp_assoc(int dev_id, uint8_t phy_handle,
  105. uint16_t max_assoc_len,
  106. uint8_t *assoc_data,
  107. uint16_t *assoc_len)
  108. {
  109. read_local_amp_assoc_cp cp;
  110. read_local_amp_assoc_rp rp;
  111. struct hci_request rq;
  112. int fd;
  113. printf("hci%d: Reading local AMP association\n", dev_id);
  114. fd = hci_open_dev(dev_id);
  115. if (fd < 0) {
  116. perror("Failed to open HCI device");
  117. return false;
  118. }
  119. memset(&cp, 0, sizeof(cp));
  120. cp.handle = phy_handle;
  121. cp.length_so_far = htobs(0);
  122. cp.assoc_length = htobs(max_assoc_len);
  123. memset(&rp, 0, sizeof(rp));
  124. memset(&rq, 0, sizeof(rq));
  125. rq.ogf = OGF_STATUS_PARAM;
  126. rq.ocf = OCF_READ_LOCAL_AMP_ASSOC;
  127. rq.cparam = &cp;
  128. rq.clen = READ_LOCAL_AMP_ASSOC_CP_SIZE;
  129. rq.rparam = &rp;
  130. rq.rlen = READ_LOCAL_AMP_ASSOC_RP_SIZE;
  131. if (hci_send_req(fd, &rq, 1000) < 0) {
  132. perror("Failed sending HCI request");
  133. hci_close_dev(fd);
  134. return false;
  135. }
  136. if (rp.status) {
  137. fprintf(stderr, "Failed HCI command: 0x%02x\n", rp.status);
  138. hci_close_dev(fd);
  139. return false;
  140. }
  141. printf("\tRemain ASSOC length: %d\n", btohs(rp.length));
  142. *assoc_len = btohs(rp.length);
  143. memcpy(assoc_data, rp.fragment, *assoc_len);
  144. hci_close_dev(fd);
  145. return true;
  146. }
  147. static bool write_remote_amp_assoc(int dev_id, uint8_t phy_handle,
  148. uint8_t *assoc_data,
  149. uint16_t assoc_len)
  150. {
  151. write_remote_amp_assoc_cp cp;
  152. write_remote_amp_assoc_rp rp;
  153. struct hci_request rq;
  154. int fd;
  155. printf("hci%d: Writing remote AMP association\n", dev_id);
  156. fd = hci_open_dev(dev_id);
  157. if (fd < 0) {
  158. perror("Failed to open HCI device");
  159. return false;
  160. }
  161. memset(&cp, 0, sizeof(cp));
  162. cp.handle = phy_handle;
  163. cp.length_so_far = htobs(0);
  164. cp.remaining_length = htobs(assoc_len);
  165. memcpy(cp.fragment, assoc_data, assoc_len);
  166. memset(&rp, 0, sizeof(rp));
  167. memset(&rq, 0, sizeof(rq));
  168. rq.ogf = OGF_STATUS_PARAM;
  169. rq.ocf = OCF_WRITE_REMOTE_AMP_ASSOC;
  170. rq.cparam = &cp;
  171. rq.clen = 5 + assoc_len;
  172. rq.rparam = &rp;
  173. rq.rlen = WRITE_REMOTE_AMP_ASSOC_RP_SIZE;
  174. if (hci_send_req(fd, &rq, 1000) < 0) {
  175. perror("Failed sending HCI request");
  176. hci_close_dev(fd);
  177. return false;
  178. }
  179. if (rp.status) {
  180. fprintf(stderr, "Failed HCI command: 0x%02x\n", rp.status);
  181. hci_close_dev(fd);
  182. return false;
  183. }
  184. hci_close_dev(fd);
  185. return true;
  186. }
  187. static bool channel_selected_event(int dev_id, int fd, uint8_t phy_handle)
  188. {
  189. printf("hci%d: Waiting for channel selected event\n", dev_id);
  190. while (1) {
  191. uint8_t buf[HCI_MAX_EVENT_SIZE];
  192. hci_event_hdr *hdr;
  193. struct pollfd p;
  194. int n, len;
  195. p.fd = fd;
  196. p.events = POLLIN;
  197. n = poll(&p, 1, 10000);
  198. if (n < 0) {
  199. if (errno == EAGAIN || errno == EINTR)
  200. continue;
  201. perror("Failed to poll HCI device");
  202. return false;
  203. }
  204. if (n == 0) {
  205. fprintf(stderr, "Failure to receive event\n");
  206. return false;
  207. }
  208. len = read(fd, buf, sizeof(buf));
  209. if (len < 0) {
  210. if (errno == EAGAIN || errno == EINTR)
  211. continue;
  212. perror("Failed to read from HCI device");
  213. return false;
  214. }
  215. hdr = (void *) (buf + 1);
  216. if (hdr->evt == EVT_CHANNEL_SELECTED)
  217. break;
  218. }
  219. return true;
  220. }
  221. static bool create_physical_link(int dev_id, uint8_t phy_handle)
  222. {
  223. create_physical_link_cp cp;
  224. evt_cmd_status evt;
  225. struct hci_request rq;
  226. int i, fd;
  227. printf("hci%d: Creating physical link\n", dev_id);
  228. fd = hci_open_dev(dev_id);
  229. if (fd < 0) {
  230. perror("Failed to open HCI device");
  231. return false;
  232. }
  233. memset(&cp, 0, sizeof(cp));
  234. cp.handle = phy_handle;
  235. cp.key_length = 32;
  236. cp.key_type = 0x03;
  237. for (i = 0; i < cp.key_length; i++)
  238. cp.key[i] = 0x23;
  239. memset(&evt, 0, sizeof(evt));
  240. memset(&rq, 0, sizeof(rq));
  241. rq.ogf = OGF_LINK_CTL;
  242. rq.ocf = OCF_CREATE_PHYSICAL_LINK;
  243. rq.event = EVT_CMD_STATUS;
  244. rq.cparam = &cp;
  245. rq.clen = CREATE_PHYSICAL_LINK_CP_SIZE;
  246. rq.rparam = &evt;
  247. rq.rlen = EVT_CMD_STATUS_SIZE;
  248. if (hci_send_req(fd, &rq, 1000) < 0) {
  249. perror("Failed sending HCI request");
  250. hci_close_dev(fd);
  251. return false;
  252. }
  253. if (evt.status) {
  254. fprintf(stderr, "Failed HCI command: 0x%02x\n", evt.status);
  255. hci_close_dev(fd);
  256. return false;
  257. }
  258. hci_close_dev(fd);
  259. return true;
  260. }
  261. static bool accept_physical_link(int dev_id, uint8_t phy_handle)
  262. {
  263. accept_physical_link_cp cp;
  264. evt_cmd_status evt;
  265. struct hci_request rq;
  266. int i, fd;
  267. printf("hci%d: Accepting physical link\n", dev_id);
  268. fd = hci_open_dev(dev_id);
  269. if (fd < 0) {
  270. perror("Failed to open HCI device");
  271. return false;
  272. }
  273. memset(&cp, 0, sizeof(cp));
  274. cp.handle = phy_handle;
  275. cp.key_length = 32;
  276. cp.key_type = 0x03;
  277. for (i = 0; i < cp.key_length; i++)
  278. cp.key[i] = 0x23;
  279. memset(&evt, 0, sizeof(evt));
  280. memset(&rq, 0, sizeof(rq));
  281. rq.ogf = OGF_LINK_CTL;
  282. rq.ocf = OCF_ACCEPT_PHYSICAL_LINK;
  283. rq.event = EVT_CMD_STATUS;
  284. rq.cparam = &cp;
  285. rq.clen = ACCEPT_PHYSICAL_LINK_CP_SIZE;
  286. rq.rparam = &evt;
  287. rq.rlen = EVT_CMD_STATUS_SIZE;
  288. if (hci_send_req(fd, &rq, 1000) < 0) {
  289. perror("Failed sending HCI request");
  290. hci_close_dev(fd);
  291. return false;
  292. }
  293. if (evt.status) {
  294. fprintf(stderr, "Failed HCI command: 0x%02x\n", evt.status);
  295. hci_close_dev(fd);
  296. return false;
  297. }
  298. hci_close_dev(fd);
  299. return true;
  300. }
  301. static bool disconnect_physical_link(int dev_id, uint8_t phy_handle,
  302. uint8_t reason)
  303. {
  304. disconnect_physical_link_cp cp;
  305. evt_cmd_status evt;
  306. struct hci_request rq;
  307. int fd;
  308. printf("hci%d: Disconnecting physical link\n", dev_id);
  309. fd = hci_open_dev(dev_id);
  310. if (fd < 0) {
  311. perror("Failed to open HCI device");
  312. return false;
  313. }
  314. memset(&cp, 0, sizeof(cp));
  315. cp.handle = phy_handle;
  316. cp.reason = reason;
  317. memset(&rq, 0, sizeof(rq));
  318. rq.ogf = OGF_LINK_CTL;
  319. rq.ocf = OCF_DISCONNECT_PHYSICAL_LINK;
  320. rq.event = EVT_CMD_STATUS;
  321. rq.cparam = &cp;
  322. rq.clen = DISCONNECT_PHYSICAL_LINK_CP_SIZE;
  323. rq.rparam = &evt;
  324. rq.rlen = EVT_CMD_STATUS_SIZE;
  325. if (hci_send_req(fd, &rq, 1000) < 0) {
  326. perror("Failed sending HCI request");
  327. hci_close_dev(fd);
  328. return false;
  329. }
  330. if (evt.status) {
  331. fprintf(stderr, "Failed HCI command: 0x%02x\n", evt.status);
  332. hci_close_dev(fd);
  333. return false;
  334. }
  335. hci_close_dev(fd);
  336. return true;
  337. }
  338. static bool physical_link_complete_event(int dev_id, int fd,
  339. uint8_t phy_handle)
  340. {
  341. printf("hci%d: Waiting for physical link complete event\n", dev_id);
  342. while (1) {
  343. uint8_t buf[HCI_MAX_EVENT_SIZE];
  344. hci_event_hdr *hdr;
  345. int len;
  346. len = read(fd, buf, sizeof(buf));
  347. if (len < 0) {
  348. if (errno == EAGAIN || errno == EINTR)
  349. continue;
  350. perror("Failed to read from HCI device");
  351. return false;
  352. }
  353. hdr = (void *) (buf + 1);
  354. if (hdr->evt == EVT_PHYSICAL_LINK_COMPLETE)
  355. break;
  356. }
  357. return true;
  358. }
  359. static bool disconnect_physical_link_complete_event(int dev_id, int fd,
  360. uint8_t phy_handle)
  361. {
  362. printf("hci%d: Waiting for physical link disconnect event\n", dev_id);
  363. while (1) {
  364. uint8_t buf[HCI_MAX_EVENT_SIZE];
  365. hci_event_hdr *hdr;
  366. int len;
  367. len = read(fd, buf, sizeof(buf));
  368. if (len < 0) {
  369. if (errno == EAGAIN || errno == EINTR)
  370. continue;
  371. perror("Failed to read from HCI device");
  372. return false;
  373. }
  374. hdr = (void *) (buf + 1);
  375. if (hdr->evt == EVT_DISCONNECT_PHYSICAL_LINK_COMPLETE)
  376. break;
  377. }
  378. return true;
  379. }
  380. static int amp1_dev_id = -1;
  381. static int amp2_dev_id = -1;
  382. static bool find_amp_controller(void)
  383. {
  384. struct hci_dev_list_req *dl;
  385. struct hci_dev_req *dr;
  386. int fd, i;
  387. bool result;
  388. fd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
  389. if (fd < 0) {
  390. perror("Failed to open raw HCI socket");
  391. return false;
  392. }
  393. dl = malloc(HCI_MAX_DEV * sizeof(struct hci_dev_req) + sizeof(uint16_t));
  394. if (!dl) {
  395. perror("Failed allocate HCI device request memory");
  396. close(fd);
  397. return false;
  398. }
  399. dl->dev_num = HCI_MAX_DEV;
  400. dr = dl->dev_req;
  401. if (ioctl(fd, HCIGETDEVLIST, (void *) dl) < 0) {
  402. perror("Failed to get HCI device list");
  403. result = false;
  404. goto done;
  405. }
  406. for (i = 0; i< dl->dev_num; i++) {
  407. struct hci_dev_info di;
  408. di.dev_id = (dr + i)->dev_id;
  409. if (ioctl(fd, HCIGETDEVINFO, (void *) &di) < 0)
  410. continue;
  411. if (((di.type & 0x30) >> 4) != HCI_AMP)
  412. continue;
  413. if (amp1_dev_id < 0)
  414. amp1_dev_id = di.dev_id;
  415. else if (amp2_dev_id < 0) {
  416. if (di.dev_id < amp1_dev_id) {
  417. amp2_dev_id = amp1_dev_id;
  418. amp1_dev_id = di.dev_id;
  419. } else
  420. amp2_dev_id = di.dev_id;
  421. }
  422. }
  423. result = true;
  424. done:
  425. free(dl);
  426. close(fd);
  427. return result;
  428. }
  429. int main(int argc ,char *argv[])
  430. {
  431. int amp1_event_fd, amp2_event_fd;
  432. uint16_t amp1_max_assoc_len, amp2_max_assoc_len;
  433. uint8_t *amp1_assoc_data, *amp2_assoc_data;
  434. uint16_t amp1_assoc_len, amp2_assoc_len;
  435. uint8_t amp1_phy_handle, amp2_phy_handle;
  436. if (!find_amp_controller())
  437. return EXIT_FAILURE;
  438. if (amp1_dev_id < 0 || amp2_dev_id < 0) {
  439. fprintf(stderr, "Two AMP controllers are required\n");
  440. return EXIT_FAILURE;
  441. }
  442. printf("hci%d: AMP initiator\n", amp1_dev_id);
  443. printf("hci%d: AMP acceptor\n", amp2_dev_id);
  444. amp1_event_fd = activate_amp_controller(amp1_dev_id);
  445. if (amp1_event_fd < 0)
  446. return EXIT_FAILURE;
  447. amp2_event_fd = activate_amp_controller(amp2_dev_id);
  448. if (amp2_event_fd < 0) {
  449. hci_close_dev(amp1_event_fd);
  450. return EXIT_FAILURE;
  451. }
  452. if (!read_local_amp_info(amp1_dev_id, &amp1_max_assoc_len))
  453. return EXIT_FAILURE;
  454. amp1_assoc_data = alloca(amp1_max_assoc_len);
  455. printf("--> AMP_Get_Info_Request (Amp_ID B)\n");
  456. if (!read_local_amp_info(amp2_dev_id, &amp2_max_assoc_len))
  457. return EXIT_FAILURE;
  458. amp2_assoc_data = alloca(amp2_max_assoc_len);
  459. printf("<-- AMP_Get_Info_Response (Amp_ID B, Status)\n");
  460. printf("--> AMP_Get_AMP_Assoc_Request (Amp_ID B)\n");
  461. if (!read_local_amp_assoc(amp2_dev_id, 0x00, amp2_max_assoc_len,
  462. amp2_assoc_data, &amp2_assoc_len))
  463. return EXIT_FAILURE;
  464. printf("<-- AMP_Get_AMP_Assoc_Response (Amp_ID B, AMP_Assoc B)\n");
  465. amp1_phy_handle = 0x04;
  466. if (!create_physical_link(amp1_dev_id, amp1_phy_handle))
  467. return EXIT_FAILURE;
  468. if (!write_remote_amp_assoc(amp1_dev_id, amp1_phy_handle,
  469. amp2_assoc_data, amp2_assoc_len))
  470. return EXIT_FAILURE;
  471. printf("hci%d: Signal MAC to scan\n", amp1_dev_id);
  472. printf("hci%d: Signal MAC to start\n", amp1_dev_id);
  473. if (!channel_selected_event(amp1_dev_id, amp1_event_fd,
  474. amp1_phy_handle))
  475. return EXIT_FAILURE;
  476. if (!read_local_amp_assoc(amp1_dev_id, amp1_phy_handle,
  477. amp1_max_assoc_len,
  478. amp1_assoc_data, &amp1_assoc_len))
  479. return EXIT_FAILURE;
  480. printf("--> AMP_Create_Physical_Link_Request (Remote-Amp-ID B, AMP_Assoc A)\n");
  481. amp2_phy_handle = 0x05;
  482. if (!accept_physical_link(amp2_dev_id, amp2_phy_handle))
  483. return EXIT_FAILURE;
  484. if (!write_remote_amp_assoc(amp2_dev_id, amp2_phy_handle,
  485. amp1_assoc_data, amp1_assoc_len))
  486. return EXIT_FAILURE;
  487. printf("hci%d: Signal MAC to start\n", amp2_dev_id);
  488. printf("<-- AMP_Create_Physical_Link_Response (Local-Amp-ID B, Status)\n");
  489. if (!physical_link_complete_event(amp2_dev_id, amp2_event_fd,
  490. amp2_phy_handle))
  491. return EXIT_FAILURE;
  492. if (!physical_link_complete_event(amp1_dev_id, amp1_event_fd,
  493. amp1_phy_handle))
  494. return EXIT_FAILURE;
  495. /* physical link established */
  496. if (!disconnect_physical_link(amp1_dev_id, amp1_phy_handle, 0x13))
  497. return EXIT_FAILURE;
  498. if (!disconnect_physical_link_complete_event(amp1_dev_id,
  499. amp1_event_fd,
  500. amp1_phy_handle))
  501. return EXIT_FAILURE;
  502. if (!disconnect_physical_link_complete_event(amp2_dev_id,
  503. amp2_event_fd,
  504. amp2_phy_handle))
  505. return EXIT_FAILURE;
  506. hci_close_dev(amp2_event_fd);
  507. hci_close_dev(amp1_event_fd);
  508. return EXIT_SUCCESS;
  509. }