controller.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2014 Google, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. #include <stdbool.h>
  19. #include "common/bt_target.h"
  20. #include "common/bt_trace.h"
  21. #include "device/bdaddr.h"
  22. #include "stack/bt_types.h"
  23. #include "device/controller.h"
  24. #include "device/event_mask.h"
  25. #include "stack/hcimsgs.h"
  26. #include "hci/hci_layer.h"
  27. #include "hci/hci_packet_factory.h"
  28. #include "hci/hci_packet_parser.h"
  29. #include "stack/btm_ble_api.h"
  30. #include "device/version.h"
  31. #include "osi/future.h"
  32. const bt_event_mask_t BLE_EVENT_MASK = { "\x00\x00\x00\x00\x00\x00\x06\x7f" };
  33. #if (BLE_INCLUDED)
  34. const bt_event_mask_t CLASSIC_EVENT_MASK = { HCI_DUMO_EVENT_MASK_EXT };
  35. #else
  36. const bt_event_mask_t CLASSIC_EVENT_MASK = { HCI_LISBON_EVENT_MASK_EXT };
  37. #endif
  38. // TODO(zachoverflow): factor out into common module
  39. const uint8_t SCO_HOST_BUFFER_SIZE = 0xff;
  40. #define HCI_SUPPORTED_COMMANDS_ARRAY_SIZE 64
  41. #define MAX_FEATURES_CLASSIC_PAGE_COUNT 3
  42. #define BLE_SUPPORTED_STATES_SIZE 8
  43. #define BLE_SUPPORTED_FEATURES_SIZE 8
  44. typedef struct {
  45. const hci_t *hci;
  46. const hci_packet_factory_t *packet_factory;
  47. const hci_packet_parser_t *packet_parser;
  48. bt_version_t bt_version;
  49. bt_bdaddr_t address;
  50. uint8_t supported_commands[HCI_SUPPORTED_COMMANDS_ARRAY_SIZE];
  51. uint8_t last_features_classic_page_index;
  52. bt_device_features_t features_classic[MAX_FEATURES_CLASSIC_PAGE_COUNT];
  53. uint16_t acl_data_size_classic;
  54. uint16_t acl_data_size_ble;
  55. uint16_t acl_buffer_count_classic;
  56. uint8_t acl_buffer_count_ble;
  57. uint8_t sco_data_size;
  58. uint16_t sco_buffer_count;
  59. uint8_t ble_white_list_size;
  60. uint8_t ble_resolving_list_max_size;
  61. uint8_t ble_supported_states[BLE_SUPPORTED_STATES_SIZE];
  62. bt_device_features_t features_ble;
  63. uint16_t ble_suggested_default_data_length;
  64. uint16_t ble_suggested_default_data_txtime;
  65. bool readable;
  66. bool ble_supported;
  67. bool simple_pairing_supported;
  68. bool secure_connections_supported;
  69. } controller_local_param_t;
  70. #if BT_BLE_DYNAMIC_ENV_MEMORY == FALSE
  71. static controller_local_param_t controller_param;
  72. #else
  73. static controller_local_param_t *controller_param_ptr;
  74. #define controller_param (*controller_param_ptr)
  75. #endif
  76. #define AWAIT_COMMAND(command) future_await(controller_param.hci->transmit_command_futured(command))
  77. // Module lifecycle functions
  78. static void start_up(void)
  79. {
  80. BT_HDR *response;
  81. // Send the initial reset command
  82. response = AWAIT_COMMAND(controller_param.packet_factory->make_reset());
  83. controller_param.packet_parser->parse_generic_command_complete(response);
  84. // Request the classic buffer size next
  85. response = AWAIT_COMMAND(controller_param.packet_factory->make_read_buffer_size());
  86. controller_param.packet_parser->parse_read_buffer_size_response(
  87. response, &controller_param.acl_data_size_classic, &controller_param.acl_buffer_count_classic,
  88. &controller_param.sco_data_size, &controller_param.sco_buffer_count);
  89. #if (C2H_FLOW_CONTROL_INCLUDED == TRUE)
  90. // Enable controller to host flow control
  91. response = AWAIT_COMMAND(controller_param.packet_factory->make_set_c2h_flow_control(HCI_HOST_FLOW_CTRL_ACL_ON));
  92. controller_param.packet_parser->parse_generic_command_complete(response);
  93. #endif ///C2H_FLOW_CONTROL_INCLUDED == TRUE
  94. #if (BLE_ADV_REPORT_FLOW_CONTROL == TRUE)
  95. // Enable adv flow control
  96. response = AWAIT_COMMAND(controller_param.packet_factory->make_set_adv_report_flow_control(HCI_HOST_FLOW_CTRL_ADV_REPORT_ON, (uint16_t)BLE_ADV_REPORT_FLOW_CONTROL_NUM, (uint16_t)BLE_ADV_REPORT_DISCARD_THRSHOLD));
  97. controller_param.packet_parser->parse_generic_command_complete(response);
  98. #endif
  99. // Tell the controller about our buffer sizes and buffer counts next
  100. // TODO(zachoverflow): factor this out. eww l2cap contamination. And why just a hardcoded 10?
  101. response = AWAIT_COMMAND(
  102. controller_param.packet_factory->make_host_buffer_size(
  103. L2CAP_MTU_SIZE,
  104. SCO_HOST_BUFFER_SIZE,
  105. L2CAP_HOST_FC_ACL_BUFS,
  106. 10
  107. )
  108. );
  109. controller_param.packet_parser->parse_generic_command_complete(response);
  110. // Read the local version info off the controller next, including
  111. // information such as manufacturer and supported HCI version
  112. response = AWAIT_COMMAND(controller_param.packet_factory->make_read_local_version_info());
  113. controller_param.packet_parser->parse_read_local_version_info_response(response, &controller_param.bt_version);
  114. // Read the bluetooth address off the controller next
  115. response = AWAIT_COMMAND(controller_param.packet_factory->make_read_bd_addr());
  116. controller_param.packet_parser->parse_read_bd_addr_response(response, &controller_param.address);
  117. // Request the controller's supported commands next
  118. response = AWAIT_COMMAND(controller_param.packet_factory->make_read_local_supported_commands());
  119. controller_param.packet_parser->parse_read_local_supported_commands_response(
  120. response,
  121. controller_param.supported_commands,
  122. HCI_SUPPORTED_COMMANDS_ARRAY_SIZE
  123. );
  124. // Read page 0 of the controller features next
  125. uint8_t page_number = 0;
  126. response = AWAIT_COMMAND(controller_param.packet_factory->make_read_local_extended_features(page_number));
  127. controller_param.packet_parser->parse_read_local_extended_features_response(
  128. response,
  129. &page_number,
  130. &controller_param.last_features_classic_page_index,
  131. controller_param.features_classic,
  132. MAX_FEATURES_CLASSIC_PAGE_COUNT
  133. );
  134. assert(page_number == 0);
  135. page_number++;
  136. // Inform the controller what page 0 features we support, based on what
  137. // it told us it supports. We need to do this first before we request the
  138. // next page, because the controller's response for page 1 may be
  139. // dependent on what we configure from page 0
  140. #if (BT_SSP_INCLUDED == TRUE)
  141. controller_param.simple_pairing_supported = HCI_SIMPLE_PAIRING_SUPPORTED(controller_param.features_classic[0].as_array);
  142. #else
  143. controller_param.simple_pairing_supported = false;
  144. #endif
  145. if (controller_param.simple_pairing_supported) {
  146. response = AWAIT_COMMAND(controller_param.packet_factory->make_write_simple_pairing_mode(HCI_SP_MODE_ENABLED));
  147. controller_param.packet_parser->parse_generic_command_complete(response);
  148. }
  149. #if (BLE_INCLUDED == TRUE)
  150. if (HCI_LE_SPT_SUPPORTED(controller_param.features_classic[0].as_array)) {
  151. uint8_t simultaneous_le_host = HCI_SIMUL_LE_BREDR_SUPPORTED(controller_param.features_classic[0].as_array) ? BTM_BLE_SIMULTANEOUS_HOST : 0;
  152. response = AWAIT_COMMAND(
  153. controller_param.packet_factory->make_ble_write_host_support(BTM_BLE_HOST_SUPPORT, simultaneous_le_host)
  154. );
  155. controller_param.packet_parser->parse_generic_command_complete(response);
  156. }
  157. #endif
  158. // Done telling the controller about what page 0 features we support
  159. // Request the remaining feature pages
  160. while (page_number <= controller_param.last_features_classic_page_index &&
  161. page_number < MAX_FEATURES_CLASSIC_PAGE_COUNT) {
  162. response = AWAIT_COMMAND(controller_param.packet_factory->make_read_local_extended_features(page_number));
  163. controller_param.packet_parser->parse_read_local_extended_features_response(
  164. response,
  165. &page_number,
  166. &controller_param.last_features_classic_page_index,
  167. controller_param.features_classic,
  168. MAX_FEATURES_CLASSIC_PAGE_COUNT
  169. );
  170. page_number++;
  171. }
  172. #if (SC_MODE_INCLUDED == TRUE)
  173. controller_param.secure_connections_supported = HCI_SC_CTRLR_SUPPORTED(controller_param.features_classic[2].as_array);
  174. if (controller_param.secure_connections_supported) {
  175. response = AWAIT_COMMAND(controller_param.packet_factory->make_write_secure_connections_host_support(HCI_SC_MODE_ENABLED));
  176. controller_param.packet_parser->parse_generic_command_complete(response);
  177. }
  178. #endif
  179. #if (BLE_INCLUDED == TRUE)
  180. controller_param.ble_supported = controller_param.last_features_classic_page_index >= 1 && HCI_LE_HOST_SUPPORTED(controller_param.features_classic[1].as_array);
  181. if (controller_param.ble_supported) {
  182. // Request the ble white list size next
  183. response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_read_white_list_size());
  184. controller_param.packet_parser->parse_ble_read_white_list_size_response(response, &controller_param.ble_white_list_size);
  185. // Request the ble buffer size next
  186. response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_read_buffer_size());
  187. controller_param.packet_parser->parse_ble_read_buffer_size_response(
  188. response,
  189. &controller_param.acl_data_size_ble,
  190. &controller_param.acl_buffer_count_ble
  191. );
  192. // Response of 0 indicates ble has the same buffer size as classic
  193. if (controller_param.acl_data_size_ble == 0) {
  194. controller_param.acl_data_size_ble = controller_param.acl_data_size_classic;
  195. }
  196. // Request the ble supported states next
  197. response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_read_supported_states());
  198. controller_param.packet_parser->parse_ble_read_supported_states_response(
  199. response,
  200. controller_param.ble_supported_states,
  201. sizeof(controller_param.ble_supported_states)
  202. );
  203. // Request the ble supported features next
  204. response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_read_local_supported_features());
  205. controller_param.packet_parser->parse_ble_read_local_supported_features_response(
  206. response,
  207. &controller_param.features_ble
  208. );
  209. if (HCI_LE_ENHANCED_PRIVACY_SUPPORTED(controller_param.features_ble.as_array)) {
  210. response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_read_resolving_list_size());
  211. controller_param.packet_parser->parse_ble_read_resolving_list_size_response(
  212. response,
  213. &controller_param.ble_resolving_list_max_size);
  214. }
  215. if (HCI_LE_DATA_LEN_EXT_SUPPORTED(controller_param.features_ble.as_array)) {
  216. /* set default tx data length to MAX 251 */
  217. response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_write_suggested_default_data_length(BTM_BLE_DATA_SIZE_MAX, BTM_BLE_DATA_TX_TIME_MAX));
  218. controller_param.packet_parser->parse_generic_command_complete(response);
  219. response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_read_suggested_default_data_length());
  220. controller_param.packet_parser->parse_ble_read_suggested_default_data_length_response(
  221. response,
  222. &controller_param.ble_suggested_default_data_length,
  223. &controller_param.ble_suggested_default_data_txtime);
  224. }
  225. // Set the ble event mask next
  226. response = AWAIT_COMMAND(controller_param.packet_factory->make_ble_set_event_mask(&BLE_EVENT_MASK));
  227. controller_param.packet_parser->parse_generic_command_complete(response);
  228. }
  229. #endif
  230. response = AWAIT_COMMAND(controller_param.packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK));
  231. controller_param.packet_parser->parse_generic_command_complete(response);
  232. #if (BTM_SCO_HCI_INCLUDED == TRUE)
  233. response = AWAIT_COMMAND(controller_param.packet_factory->make_write_sync_flow_control_enable(1));
  234. controller_param.packet_parser->parse_generic_command_complete(response);
  235. response = AWAIT_COMMAND(controller_param.packet_factory->make_write_default_erroneous_data_report(1));
  236. controller_param.packet_parser->parse_generic_command_complete(response);
  237. #endif
  238. controller_param.readable = true;
  239. // return future_new_immediate(FUTURE_SUCCESS);
  240. return;
  241. }
  242. static void shut_down(void)
  243. {
  244. controller_param.readable = false;
  245. }
  246. static bool get_is_ready(void)
  247. {
  248. return controller_param.readable;
  249. }
  250. static const bt_bdaddr_t *get_address(void)
  251. {
  252. assert(controller_param.readable);
  253. return &controller_param.address;
  254. }
  255. static const bt_version_t *get_bt_version(void)
  256. {
  257. assert(controller_param.readable);
  258. return &controller_param.bt_version;
  259. }
  260. // TODO(zachoverflow): hide inside, move decoder inside too
  261. static const bt_device_features_t *get_features_classic(int index)
  262. {
  263. assert(controller_param.readable);
  264. assert(index < MAX_FEATURES_CLASSIC_PAGE_COUNT);
  265. return &controller_param.features_classic[index];
  266. }
  267. static uint8_t get_last_features_classic_index(void)
  268. {
  269. assert(controller_param.readable);
  270. return controller_param.last_features_classic_page_index;
  271. }
  272. static const bt_device_features_t *get_features_ble(void)
  273. {
  274. assert(controller_param.readable);
  275. assert(controller_param.ble_supported);
  276. return &controller_param.features_ble;
  277. }
  278. static const uint8_t *get_ble_supported_states(void)
  279. {
  280. assert(controller_param.readable);
  281. assert(controller_param.ble_supported);
  282. return controller_param.ble_supported_states;
  283. }
  284. static bool supports_simple_pairing(void)
  285. {
  286. assert(controller_param.readable);
  287. return controller_param.simple_pairing_supported;
  288. }
  289. static bool supports_secure_connections(void)
  290. {
  291. assert(controller_param.readable);
  292. return controller_param.secure_connections_supported;
  293. }
  294. static bool supports_simultaneous_le_bredr(void)
  295. {
  296. assert(controller_param.readable);
  297. return HCI_SIMUL_LE_BREDR_SUPPORTED(controller_param.features_classic[0].as_array);
  298. }
  299. static bool supports_reading_remote_extended_features(void)
  300. {
  301. assert(controller_param.readable);
  302. return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(controller_param.supported_commands);
  303. }
  304. static bool supports_interlaced_inquiry_scan(void)
  305. {
  306. assert(controller_param.readable);
  307. return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(controller_param.features_classic[0].as_array);
  308. }
  309. static bool supports_rssi_with_inquiry_results(void)
  310. {
  311. assert(controller_param.readable);
  312. return HCI_LMP_INQ_RSSI_SUPPORTED(controller_param.features_classic[0].as_array);
  313. }
  314. static bool supports_extended_inquiry_response(void)
  315. {
  316. assert(controller_param.readable);
  317. return HCI_EXT_INQ_RSP_SUPPORTED(controller_param.features_classic[0].as_array);
  318. }
  319. static bool supports_master_slave_role_switch(void)
  320. {
  321. assert(controller_param.readable);
  322. return HCI_SWITCH_SUPPORTED(controller_param.features_classic[0].as_array);
  323. }
  324. static bool supports_ble(void)
  325. {
  326. assert(controller_param.readable);
  327. return controller_param.ble_supported;
  328. }
  329. static bool supports_ble_privacy(void)
  330. {
  331. assert(controller_param.readable);
  332. assert(controller_param.ble_supported);
  333. return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(controller_param.features_ble.as_array);
  334. }
  335. static bool supports_ble_packet_extension(void)
  336. {
  337. assert(controller_param.readable);
  338. assert(controller_param.ble_supported);
  339. return HCI_LE_DATA_LEN_EXT_SUPPORTED(controller_param.features_ble.as_array);
  340. }
  341. static bool supports_ble_connection_parameters_request(void)
  342. {
  343. assert(controller_param.readable);
  344. assert(controller_param.ble_supported);
  345. return HCI_LE_CONN_PARAM_REQ_SUPPORTED(controller_param.features_ble.as_array);
  346. }
  347. static uint16_t get_acl_data_size_classic(void)
  348. {
  349. assert(controller_param.readable);
  350. return controller_param.acl_data_size_classic;
  351. }
  352. static uint16_t get_acl_data_size_ble(void)
  353. {
  354. assert(controller_param.readable);
  355. assert(controller_param.ble_supported);
  356. return controller_param.acl_data_size_ble;
  357. }
  358. static uint16_t get_acl_packet_size_classic(void)
  359. {
  360. assert(controller_param.readable);
  361. return controller_param.acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE;
  362. }
  363. static uint16_t get_acl_packet_size_ble(void)
  364. {
  365. assert(controller_param.readable);
  366. return controller_param.acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE;
  367. }
  368. static uint16_t get_ble_suggested_default_data_length(void)
  369. {
  370. assert(controller_param.readable);
  371. assert(controller_param.ble_supported);
  372. return controller_param.ble_suggested_default_data_length;
  373. }
  374. static uint16_t get_ble_suggested_default_data_txtime(void)
  375. {
  376. assert(controller_param.readable);
  377. assert(controller_param.ble_supported);
  378. return controller_param.ble_suggested_default_data_txtime;
  379. }
  380. static uint16_t get_acl_buffer_count_classic(void)
  381. {
  382. assert(controller_param.readable);
  383. return controller_param.acl_buffer_count_classic;
  384. }
  385. static uint8_t get_acl_buffer_count_ble(void)
  386. {
  387. assert(controller_param.readable);
  388. assert(controller_param.ble_supported);
  389. return controller_param.acl_buffer_count_ble;
  390. }
  391. static uint8_t get_ble_white_list_size(void)
  392. {
  393. assert(controller_param.readable);
  394. assert(controller_param.ble_supported);
  395. return controller_param.ble_white_list_size;
  396. }
  397. static uint8_t get_ble_resolving_list_max_size(void)
  398. {
  399. assert(controller_param.readable);
  400. assert(controller_param.ble_supported);
  401. return controller_param.ble_resolving_list_max_size;
  402. }
  403. static void set_ble_resolving_list_max_size(int resolving_list_max_size)
  404. {
  405. assert(controller_param.readable);
  406. assert(controller_param.ble_supported);
  407. controller_param.ble_resolving_list_max_size = resolving_list_max_size;
  408. }
  409. #if (BTM_SCO_HCI_INCLUDED == TRUE)
  410. static uint8_t get_sco_data_size(void)
  411. {
  412. assert(controller_param.readable);
  413. return controller_param.sco_data_size;
  414. }
  415. static uint8_t get_sco_buffer_count(void)
  416. {
  417. assert(controller_param.readable);
  418. return controller_param.sco_buffer_count;
  419. }
  420. #endif /* (BTM_SCO_HCI_INCLUDED == TRUE) */
  421. static const controller_t interface = {
  422. start_up,
  423. shut_down,
  424. get_is_ready,
  425. get_address,
  426. get_bt_version,
  427. get_features_classic,
  428. get_last_features_classic_index,
  429. get_features_ble,
  430. get_ble_supported_states,
  431. supports_simple_pairing,
  432. supports_secure_connections,
  433. supports_simultaneous_le_bredr,
  434. supports_reading_remote_extended_features,
  435. supports_interlaced_inquiry_scan,
  436. supports_rssi_with_inquiry_results,
  437. supports_extended_inquiry_response,
  438. supports_master_slave_role_switch,
  439. supports_ble,
  440. supports_ble_packet_extension,
  441. supports_ble_connection_parameters_request,
  442. supports_ble_privacy,
  443. get_acl_data_size_classic,
  444. get_acl_data_size_ble,
  445. get_acl_packet_size_classic,
  446. get_acl_packet_size_ble,
  447. get_ble_suggested_default_data_length,
  448. get_ble_suggested_default_data_txtime,
  449. get_acl_buffer_count_classic,
  450. get_acl_buffer_count_ble,
  451. get_ble_white_list_size,
  452. get_ble_resolving_list_max_size,
  453. set_ble_resolving_list_max_size,
  454. #if (BTM_SCO_HCI_INCLUDED == TRUE)
  455. get_sco_data_size,
  456. get_sco_buffer_count,
  457. #endif /* (BTM_SCO_HCI_INCLUDED == TRUE) */
  458. };
  459. const controller_t *controller_get_interface(void)
  460. {
  461. static bool loaded = false;
  462. if (!loaded) {
  463. loaded = true;
  464. #if (BT_BLE_DYNAMIC_ENV_MEMORY == TRUE)
  465. controller_param_ptr = (controller_local_param_t *)osi_calloc(sizeof(controller_local_param_t));
  466. assert(controller_param_ptr);
  467. #endif
  468. controller_param.hci = hci_layer_get_interface();
  469. controller_param.packet_factory = hci_packet_factory_get_interface();
  470. controller_param.packet_parser = hci_packet_parser_get_interface();
  471. }
  472. return &interface;
  473. }