blob: 20daa1c8737f34b3ccf59c8e26e23db9f2d9a9c7 [file] [log] [blame]
Zach Johnsonbf8193b2014-09-08 09:56:35 -07001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2014 Google, Inc.
Zach Johnsonbf8193b2014-09-08 09:56:35 -07004 *
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
Zach Johnson30e58062014-09-26 21:14:34 -070019#define LOG_TAG "bt_controller"
20
Chris Manton79ecab52014-10-31 14:54:51 -070021#include "device/include/controller.h"
Marie Janssen49a86702015-07-08 11:48:57 -070022
Jack Hef2af1c42016-12-13 01:59:12 -080023#include <base/logging.h>
Marie Janssen49a86702015-07-08 11:48:57 -070024
25#include "bt_types.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070026#include "btcore/include/event_mask.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070027#include "btcore/include/module.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070028#include "btcore/include/version.h"
Marie Janssen49a86702015-07-08 11:48:57 -070029#include "hcimsgs.h"
30#include "osi/include/future.h"
31#include "stack/include/btm_ble_api.h"
Zach Johnson30e58062014-09-26 21:14:34 -070032
Jakub Pawlowski692a0ca2018-04-19 02:32:40 -070033const bt_event_mask_t BLE_EVENT_MASK = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
34#if (BLE_PRIVACY_SPT == TRUE)
35 0x1E,
36#else
37 /* Disable "LE Enhanced Connection
38 Complete" when privacy is off */
39 0x1C,
40#endif
41 0x7f}};
Priti Aghera636d6712014-12-18 13:55:48 -080042
Myles Watson4451b3b2016-11-09 10:20:44 -080043const bt_event_mask_t CLASSIC_EVENT_MASK = {HCI_DUMO_EVENT_MASK_EXT};
Zach Johnson30e58062014-09-26 21:14:34 -070044
45// TODO(zachoverflow): factor out into common module
46const uint8_t SCO_HOST_BUFFER_SIZE = 0xff;
47
48#define HCI_SUPPORTED_COMMANDS_ARRAY_SIZE 64
49#define MAX_FEATURES_CLASSIC_PAGE_COUNT 3
Myles Watson4451b3b2016-11-09 10:20:44 -080050#define BLE_SUPPORTED_STATES_SIZE 8
51#define BLE_SUPPORTED_FEATURES_SIZE 8
52#define MAX_LOCAL_SUPPORTED_CODECS_SIZE 8
Zach Johnsonbf8193b2014-09-08 09:56:35 -070053
Myles Watson4451b3b2016-11-09 10:20:44 -080054static const hci_t* hci;
55static const hci_packet_factory_t* packet_factory;
56static const hci_packet_parser_t* packet_parser;
Zach Johnsonbf8193b2014-09-08 09:56:35 -070057
Jakub Pawlowskia484a882017-06-24 17:30:18 -070058static RawAddress address;
Zach Johnson30e58062014-09-26 21:14:34 -070059static bt_version_t bt_version;
Zach Johnsonbf8193b2014-09-08 09:56:35 -070060
Zach Johnson30e58062014-09-26 21:14:34 -070061static uint8_t supported_commands[HCI_SUPPORTED_COMMANDS_ARRAY_SIZE];
62static bt_device_features_t features_classic[MAX_FEATURES_CLASSIC_PAGE_COUNT];
63static uint8_t last_features_classic_page_index;
Zach Johnsonbf8193b2014-09-08 09:56:35 -070064
Zach Johnson30e58062014-09-26 21:14:34 -070065static uint16_t acl_data_size_classic;
66static uint16_t acl_data_size_ble;
67static uint16_t acl_buffer_count_classic;
68static uint8_t acl_buffer_count_ble;
69
70static uint8_t ble_white_list_size;
Satya Calloji444a8da2015-03-06 10:38:22 -080071static uint8_t ble_resolving_list_max_size;
Zach Johnson30e58062014-09-26 21:14:34 -070072static uint8_t ble_supported_states[BLE_SUPPORTED_STATES_SIZE];
73static bt_device_features_t features_ble;
Priti Aghera636d6712014-12-18 13:55:48 -080074static uint16_t ble_suggested_default_data_length;
Jakub Pawlowski4f0c5b52018-02-02 22:05:11 -080075static uint16_t ble_supported_max_tx_octets;
76static uint16_t ble_supported_max_tx_time;
77static uint16_t ble_supported_max_rx_octets;
78static uint16_t ble_supported_max_rx_time;
79
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -080080static uint16_t ble_maxium_advertising_data_length;
81static uint8_t ble_number_of_supported_advertising_sets;
Srinu Jellaf6dcf792015-07-22 17:30:32 +053082static uint8_t local_supported_codecs[MAX_LOCAL_SUPPORTED_CODECS_SIZE];
83static uint8_t number_of_local_supported_codecs = 0;
Zach Johnson30e58062014-09-26 21:14:34 -070084
85static bool readable;
86static bool ble_supported;
87static bool simple_pairing_supported;
Satya Calloji444a8da2015-03-06 10:38:22 -080088static bool secure_connections_supported;
Zach Johnson30e58062014-09-26 21:14:34 -070089
Myles Watson4451b3b2016-11-09 10:20:44 -080090#define AWAIT_COMMAND(command) \
91 static_cast<BT_HDR*>(future_await(hci->transmit_command_futured(command)))
Zach Johnson30e58062014-09-26 21:14:34 -070092
93// Module lifecycle functions
94
Myles Watson4451b3b2016-11-09 10:20:44 -080095static future_t* start_up(void) {
96 BT_HDR* response;
Zach Johnson30e58062014-09-26 21:14:34 -070097
98 // Send the initial reset command
99 response = AWAIT_COMMAND(packet_factory->make_reset());
100 packet_parser->parse_generic_command_complete(response);
101
102 // Request the classic buffer size next
103 response = AWAIT_COMMAND(packet_factory->make_read_buffer_size());
104 packet_parser->parse_read_buffer_size_response(
105 response, &acl_data_size_classic, &acl_buffer_count_classic);
106
107 // Tell the controller about our buffer sizes and buffer counts next
Myles Watson4451b3b2016-11-09 10:20:44 -0800108 // TODO(zachoverflow): factor this out. eww l2cap contamination. And why just
109 // a hardcoded 10?
110 response = AWAIT_COMMAND(packet_factory->make_host_buffer_size(
111 L2CAP_MTU_SIZE, SCO_HOST_BUFFER_SIZE, L2CAP_HOST_FC_ACL_BUFS, 10));
Zach Johnson30e58062014-09-26 21:14:34 -0700112
113 packet_parser->parse_generic_command_complete(response);
114
115 // Read the local version info off the controller next, including
116 // information such as manufacturer and supported HCI version
117 response = AWAIT_COMMAND(packet_factory->make_read_local_version_info());
118 packet_parser->parse_read_local_version_info_response(response, &bt_version);
119
120 // Read the bluetooth address off the controller next
121 response = AWAIT_COMMAND(packet_factory->make_read_bd_addr());
122 packet_parser->parse_read_bd_addr_response(response, &address);
123
124 // Request the controller's supported commands next
Myles Watson4451b3b2016-11-09 10:20:44 -0800125 response =
126 AWAIT_COMMAND(packet_factory->make_read_local_supported_commands());
Zach Johnson30e58062014-09-26 21:14:34 -0700127 packet_parser->parse_read_local_supported_commands_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800128 response, supported_commands, HCI_SUPPORTED_COMMANDS_ARRAY_SIZE);
Zach Johnson30e58062014-09-26 21:14:34 -0700129
130 // Read page 0 of the controller features next
131 uint8_t page_number = 0;
Myles Watson4451b3b2016-11-09 10:20:44 -0800132 response = AWAIT_COMMAND(
133 packet_factory->make_read_local_extended_features(page_number));
Zach Johnson30e58062014-09-26 21:14:34 -0700134 packet_parser->parse_read_local_extended_features_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800135 response, &page_number, &last_features_classic_page_index,
136 features_classic, MAX_FEATURES_CLASSIC_PAGE_COUNT);
Zach Johnson30e58062014-09-26 21:14:34 -0700137
Jack Hef2af1c42016-12-13 01:59:12 -0800138 CHECK(page_number == 0);
Zach Johnson30e58062014-09-26 21:14:34 -0700139 page_number++;
140
141 // Inform the controller what page 0 features we support, based on what
142 // it told us it supports. We need to do this first before we request the
143 // next page, because the controller's response for page 1 may be
144 // dependent on what we configure from page 0
Myles Watson4451b3b2016-11-09 10:20:44 -0800145 simple_pairing_supported =
146 HCI_SIMPLE_PAIRING_SUPPORTED(features_classic[0].as_array);
Zach Johnson30e58062014-09-26 21:14:34 -0700147 if (simple_pairing_supported) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800148 response = AWAIT_COMMAND(
149 packet_factory->make_write_simple_pairing_mode(HCI_SP_MODE_ENABLED));
Zach Johnson30e58062014-09-26 21:14:34 -0700150 packet_parser->parse_generic_command_complete(response);
151 }
152
Zach Johnson30e58062014-09-26 21:14:34 -0700153 if (HCI_LE_SPT_SUPPORTED(features_classic[0].as_array)) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800154 uint8_t simultaneous_le_host =
155 HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array)
156 ? BTM_BLE_SIMULTANEOUS_HOST
157 : 0;
158 response = AWAIT_COMMAND(packet_factory->make_ble_write_host_support(
159 BTM_BLE_HOST_SUPPORT, simultaneous_le_host));
Zach Johnson30e58062014-09-26 21:14:34 -0700160
161 packet_parser->parse_generic_command_complete(response);
Frédéric Dalleauebc367a2015-08-18 12:23:12 +0200162
163 // If we modified the BT_HOST_SUPPORT, we will need ext. feat. page 1
164 if (last_features_classic_page_index < 1)
165 last_features_classic_page_index = 1;
Zach Johnson30e58062014-09-26 21:14:34 -0700166 }
Zach Johnson30e58062014-09-26 21:14:34 -0700167
168 // Done telling the controller about what page 0 features we support
169 // Request the remaining feature pages
170 while (page_number <= last_features_classic_page_index &&
171 page_number < MAX_FEATURES_CLASSIC_PAGE_COUNT) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800172 response = AWAIT_COMMAND(
173 packet_factory->make_read_local_extended_features(page_number));
Zach Johnson30e58062014-09-26 21:14:34 -0700174 packet_parser->parse_read_local_extended_features_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800175 response, &page_number, &last_features_classic_page_index,
176 features_classic, MAX_FEATURES_CLASSIC_PAGE_COUNT);
Zach Johnson30e58062014-09-26 21:14:34 -0700177
178 page_number++;
179 }
180
Andre Eisenbach67a784b2015-05-26 17:40:12 -0700181#if (SC_MODE_INCLUDED == TRUE)
Myles Watson4451b3b2016-11-09 10:20:44 -0800182 secure_connections_supported =
183 HCI_SC_CTRLR_SUPPORTED(features_classic[2].as_array);
Mudumba Ananth899b7712015-01-30 02:33:02 -0800184 if (secure_connections_supported) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800185 response = AWAIT_COMMAND(
186 packet_factory->make_write_secure_connections_host_support(
187 HCI_SC_MODE_ENABLED));
Mudumba Ananth899b7712015-01-30 02:33:02 -0800188 packet_parser->parse_generic_command_complete(response);
189 }
Andre Eisenbach67a784b2015-05-26 17:40:12 -0700190#endif
Mudumba Ananth899b7712015-01-30 02:33:02 -0800191
Myles Watson4451b3b2016-11-09 10:20:44 -0800192 ble_supported = last_features_classic_page_index >= 1 &&
193 HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
Zach Johnson30e58062014-09-26 21:14:34 -0700194 if (ble_supported) {
195 // Request the ble white list size next
196 response = AWAIT_COMMAND(packet_factory->make_ble_read_white_list_size());
Myles Watson4451b3b2016-11-09 10:20:44 -0800197 packet_parser->parse_ble_read_white_list_size_response(
198 response, &ble_white_list_size);
Zach Johnson30e58062014-09-26 21:14:34 -0700199
200 // Request the ble buffer size next
201 response = AWAIT_COMMAND(packet_factory->make_ble_read_buffer_size());
202 packet_parser->parse_ble_read_buffer_size_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800203 response, &acl_data_size_ble, &acl_buffer_count_ble);
Zach Johnson30e58062014-09-26 21:14:34 -0700204
205 // Response of 0 indicates ble has the same buffer size as classic
Myles Watson4451b3b2016-11-09 10:20:44 -0800206 if (acl_data_size_ble == 0) acl_data_size_ble = acl_data_size_classic;
Zach Johnson30e58062014-09-26 21:14:34 -0700207
208 // Request the ble supported states next
209 response = AWAIT_COMMAND(packet_factory->make_ble_read_supported_states());
210 packet_parser->parse_ble_read_supported_states_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800211 response, ble_supported_states, sizeof(ble_supported_states));
Zach Johnson30e58062014-09-26 21:14:34 -0700212
213 // Request the ble supported features next
Myles Watson4451b3b2016-11-09 10:20:44 -0800214 response =
215 AWAIT_COMMAND(packet_factory->make_ble_read_local_supported_features());
Zach Johnson30e58062014-09-26 21:14:34 -0700216 packet_parser->parse_ble_read_local_supported_features_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800217 response, &features_ble);
Zach Johnson30e58062014-09-26 21:14:34 -0700218
Satya Calloji444a8da2015-03-06 10:38:22 -0800219 if (HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array)) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800220 response =
221 AWAIT_COMMAND(packet_factory->make_ble_read_resolving_list_size());
222 packet_parser->parse_ble_read_resolving_list_size_response(
223 response, &ble_resolving_list_max_size);
Satya Calloji444a8da2015-03-06 10:38:22 -0800224 }
225
Priti Aghera636d6712014-12-18 13:55:48 -0800226 if (HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array)) {
Jakub Pawlowski4f0c5b52018-02-02 22:05:11 -0800227 response =
228 AWAIT_COMMAND(packet_factory->make_ble_read_maximum_data_length());
229 packet_parser->parse_ble_read_maximum_data_length_response(
230 response, &ble_supported_max_tx_octets, &ble_supported_max_tx_time,
231 &ble_supported_max_rx_octets, &ble_supported_max_rx_time);
232
Myles Watson4451b3b2016-11-09 10:20:44 -0800233 response = AWAIT_COMMAND(
234 packet_factory->make_ble_read_suggested_default_data_length());
235 packet_parser->parse_ble_read_suggested_default_data_length_response(
236 response, &ble_suggested_default_data_length);
Priti Aghera636d6712014-12-18 13:55:48 -0800237 }
238
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800239 if (HCI_LE_EXTENDED_ADVERTISING_SUPPORTED(features_ble.as_array)) {
240 response = AWAIT_COMMAND(
241 packet_factory->make_ble_read_maximum_advertising_data_length());
242 packet_parser->parse_ble_read_maximum_advertising_data_length(
243 response, &ble_maxium_advertising_data_length);
244
245 response = AWAIT_COMMAND(
246 packet_factory->make_ble_read_number_of_supported_advertising_sets());
247 packet_parser->parse_ble_read_number_of_supported_advertising_sets(
248 response, &ble_number_of_supported_advertising_sets);
Jakub Pawlowski5a8a1622017-03-16 18:19:20 -0700249 } else {
250 /* If LE Excended Advertising is not supported, use the default value */
251 ble_maxium_advertising_data_length = 31;
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800252 }
253
Zach Johnson30e58062014-09-26 21:14:34 -0700254 // Set the ble event mask next
Myles Watson4451b3b2016-11-09 10:20:44 -0800255 response =
256 AWAIT_COMMAND(packet_factory->make_ble_set_event_mask(&BLE_EVENT_MASK));
Zach Johnson30e58062014-09-26 21:14:34 -0700257 packet_parser->parse_generic_command_complete(response);
258 }
Zach Johnson30e58062014-09-26 21:14:34 -0700259
260 if (simple_pairing_supported) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800261 response =
262 AWAIT_COMMAND(packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK));
Zach Johnson30e58062014-09-26 21:14:34 -0700263 packet_parser->parse_generic_command_complete(response);
264 }
265
Srinu Jellaf6dcf792015-07-22 17:30:32 +0530266 // read local supported codecs
Myles Watson4451b3b2016-11-09 10:20:44 -0800267 if (HCI_READ_LOCAL_CODECS_SUPPORTED(supported_commands)) {
268 response =
269 AWAIT_COMMAND(packet_factory->make_read_local_supported_codecs());
Srinu Jellaf6dcf792015-07-22 17:30:32 +0530270 packet_parser->parse_read_local_supported_codecs_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800271 response, &number_of_local_supported_codecs, local_supported_codecs);
Srinu Jellaf6dcf792015-07-22 17:30:32 +0530272 }
273
Zach Johnson30e58062014-09-26 21:14:34 -0700274 readable = true;
275 return future_new_immediate(FUTURE_SUCCESS);
276}
277
Myles Watson4451b3b2016-11-09 10:20:44 -0800278static future_t* shut_down(void) {
Zach Johnson30e58062014-09-26 21:14:34 -0700279 readable = false;
280 return future_new_immediate(FUTURE_SUCCESS);
281}
282
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -0700283EXPORT_SYMBOL extern const module_t controller_module = {
Myles Watson4451b3b2016-11-09 10:20:44 -0800284 .name = CONTROLLER_MODULE,
285 .init = NULL,
286 .start_up = start_up,
287 .shut_down = shut_down,
288 .clean_up = NULL,
289 .dependencies = {HCI_MODULE, NULL}};
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700290
291// Interface functions
292
Myles Watson4451b3b2016-11-09 10:20:44 -0800293static bool get_is_ready(void) { return readable; }
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700294
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700295static const RawAddress* get_address(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800296 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700297 return &address;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700298}
299
Myles Watson4451b3b2016-11-09 10:20:44 -0800300static const bt_version_t* get_bt_version(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800301 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700302 return &bt_version;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700303}
304
Zach Johnson30e58062014-09-26 21:14:34 -0700305// TODO(zachoverflow): hide inside, move decoder inside too
Myles Watson4451b3b2016-11-09 10:20:44 -0800306static const bt_device_features_t* get_features_classic(int index) {
Jack Hef2af1c42016-12-13 01:59:12 -0800307 CHECK(readable);
308 CHECK(index < MAX_FEATURES_CLASSIC_PAGE_COUNT);
Zach Johnson30e58062014-09-26 21:14:34 -0700309 return &features_classic[index];
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700310}
311
Zach Johnson30e58062014-09-26 21:14:34 -0700312static uint8_t get_last_features_classic_index(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800313 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700314 return last_features_classic_page_index;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700315}
316
Myles Watson4451b3b2016-11-09 10:20:44 -0800317static uint8_t* get_local_supported_codecs(uint8_t* number_of_codecs) {
Jack Hef2af1c42016-12-13 01:59:12 -0800318 CHECK(readable);
Myles Watson4451b3b2016-11-09 10:20:44 -0800319 if (number_of_local_supported_codecs) {
Srinu Jellaf6dcf792015-07-22 17:30:32 +0530320 *number_of_codecs = number_of_local_supported_codecs;
321 return local_supported_codecs;
322 }
323 return NULL;
324}
325
Myles Watson4451b3b2016-11-09 10:20:44 -0800326static const bt_device_features_t* get_features_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800327 CHECK(readable);
328 CHECK(ble_supported);
Zach Johnson30e58062014-09-26 21:14:34 -0700329 return &features_ble;
330}
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700331
Myles Watson4451b3b2016-11-09 10:20:44 -0800332static const uint8_t* get_ble_supported_states(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800333 CHECK(readable);
334 CHECK(ble_supported);
Zach Johnsona87c8212014-10-28 17:59:13 -0700335 return ble_supported_states;
336}
337
Zach Johnson30e58062014-09-26 21:14:34 -0700338static bool supports_simple_pairing(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800339 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700340 return simple_pairing_supported;
341}
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700342
Satya Calloji444a8da2015-03-06 10:38:22 -0800343static bool supports_secure_connections(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800344 CHECK(readable);
Satya Calloji444a8da2015-03-06 10:38:22 -0800345 return secure_connections_supported;
346}
347
Zach Johnson30e58062014-09-26 21:14:34 -0700348static bool supports_simultaneous_le_bredr(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800349 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700350 return HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array);
351}
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700352
Zach Johnson30e58062014-09-26 21:14:34 -0700353static bool supports_reading_remote_extended_features(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800354 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700355 return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(supported_commands);
356}
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700357
Zach Johnson30e58062014-09-26 21:14:34 -0700358static bool supports_interlaced_inquiry_scan(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800359 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700360 return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(features_classic[0].as_array);
361}
362
363static bool supports_rssi_with_inquiry_results(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800364 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700365 return HCI_LMP_INQ_RSSI_SUPPORTED(features_classic[0].as_array);
366}
367
368static bool supports_extended_inquiry_response(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800369 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700370 return HCI_EXT_INQ_RSP_SUPPORTED(features_classic[0].as_array);
371}
372
373static bool supports_master_slave_role_switch(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800374 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700375 return HCI_SWITCH_SUPPORTED(features_classic[0].as_array);
376}
377
Mudumba Ananth57f65082017-02-09 09:05:48 -0800378static bool supports_enhanced_setup_synchronous_connection(void) {
379 assert(readable);
380 return HCI_ENH_SETUP_SYNCH_CONN_SUPPORTED(supported_commands);
381}
382
383static bool supports_enhanced_accept_synchronous_connection(void) {
384 assert(readable);
385 return HCI_ENH_ACCEPT_SYNCH_CONN_SUPPORTED(supported_commands);
386}
387
Zach Johnson30e58062014-09-26 21:14:34 -0700388static bool supports_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800389 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700390 return ble_supported;
391}
392
Satya Calloji444a8da2015-03-06 10:38:22 -0800393static bool supports_ble_privacy(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800394 CHECK(readable);
395 CHECK(ble_supported);
Satya Calloji444a8da2015-03-06 10:38:22 -0800396 return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array);
397}
398
Jakub Pawlowskiba786012017-06-26 11:26:46 -0700399static bool supports_ble_set_privacy_mode() {
400 CHECK(readable);
401 CHECK(ble_supported);
402 return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array) &&
403 HCI_LE_SET_PRIVACY_MODE_SUPPORTED(supported_commands);
404}
405
Priti Aghera636d6712014-12-18 13:55:48 -0800406static bool supports_ble_packet_extension(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800407 CHECK(readable);
408 CHECK(ble_supported);
Priti Aghera636d6712014-12-18 13:55:48 -0800409 return HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array);
410}
411
Zach Johnson30e58062014-09-26 21:14:34 -0700412static bool supports_ble_connection_parameters_request(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800413 CHECK(readable);
414 CHECK(ble_supported);
Zach Johnson30e58062014-09-26 21:14:34 -0700415 return HCI_LE_CONN_PARAM_REQ_SUPPORTED(features_ble.as_array);
416}
417
Jakub Pawlowski4b1feb62017-03-09 18:11:43 -0800418static bool supports_ble_2m_phy(void) {
419 CHECK(readable);
420 CHECK(ble_supported);
421 return HCI_LE_2M_PHY_SUPPORTED(features_ble.as_array);
422}
423
424static bool supports_ble_coded_phy(void) {
425 CHECK(readable);
426 CHECK(ble_supported);
427 return HCI_LE_CODED_PHY_SUPPORTED(features_ble.as_array);
428}
429
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800430static bool supports_ble_extended_advertising(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800431 CHECK(readable);
432 CHECK(ble_supported);
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800433 return HCI_LE_EXTENDED_ADVERTISING_SUPPORTED(features_ble.as_array);
434}
435
436static bool supports_ble_periodic_advertising(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800437 CHECK(readable);
438 CHECK(ble_supported);
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800439 return HCI_LE_PERIODIC_ADVERTISING_SUPPORTED(features_ble.as_array);
440}
441
Zach Johnson30e58062014-09-26 21:14:34 -0700442static uint16_t get_acl_data_size_classic(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800443 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700444 return acl_data_size_classic;
445}
446
447static uint16_t get_acl_data_size_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800448 CHECK(readable);
449 CHECK(ble_supported);
Zach Johnson30e58062014-09-26 21:14:34 -0700450 return acl_data_size_ble;
451}
452
453static uint16_t get_acl_packet_size_classic(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800454 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700455 return acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE;
456}
457
458static uint16_t get_acl_packet_size_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800459 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700460 return acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE;
461}
462
Priti Aghera636d6712014-12-18 13:55:48 -0800463static uint16_t get_ble_suggested_default_data_length(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800464 CHECK(readable);
465 CHECK(ble_supported);
Priti Aghera636d6712014-12-18 13:55:48 -0800466 return ble_suggested_default_data_length;
467}
468
Jakub Pawlowski4f0c5b52018-02-02 22:05:11 -0800469static uint16_t get_ble_maximum_tx_data_length(void) {
470 CHECK(readable);
471 CHECK(ble_supported);
472 return ble_supported_max_tx_octets;
473}
474
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800475static uint16_t get_ble_maxium_advertising_data_length(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800476 CHECK(readable);
477 CHECK(ble_supported);
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800478 return ble_maxium_advertising_data_length;
479}
480
481static uint8_t get_ble_number_of_supported_advertising_sets(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800482 CHECK(readable);
483 CHECK(ble_supported);
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800484 return ble_number_of_supported_advertising_sets;
485}
486
Zach Johnson30e58062014-09-26 21:14:34 -0700487static uint16_t get_acl_buffer_count_classic(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800488 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700489 return acl_buffer_count_classic;
490}
491
492static uint8_t get_acl_buffer_count_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800493 CHECK(readable);
494 CHECK(ble_supported);
Zach Johnson30e58062014-09-26 21:14:34 -0700495 return acl_buffer_count_ble;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700496}
497
Priti Agherab8b3e802015-04-17 18:11:24 -0700498static uint8_t get_ble_white_list_size(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800499 CHECK(readable);
500 CHECK(ble_supported);
Priti Agherab8b3e802015-04-17 18:11:24 -0700501 return ble_white_list_size;
502}
503
Satya Calloji444a8da2015-03-06 10:38:22 -0800504static uint8_t get_ble_resolving_list_max_size(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800505 CHECK(readable);
506 CHECK(ble_supported);
Satya Calloji444a8da2015-03-06 10:38:22 -0800507 return ble_resolving_list_max_size;
508}
509
510static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
Pavlin Radoslavov58318f42016-05-05 16:20:15 -0700511 // Setting "resolving_list_max_size" to 0 is done during cleanup,
512 // hence we ignore the "readable" flag already set to false during shutdown.
513 if (resolving_list_max_size != 0) {
Jack Hef2af1c42016-12-13 01:59:12 -0800514 CHECK(readable);
Pavlin Radoslavov58318f42016-05-05 16:20:15 -0700515 }
Jack Hef2af1c42016-12-13 01:59:12 -0800516 CHECK(ble_supported);
Satya Calloji444a8da2015-03-06 10:38:22 -0800517 ble_resolving_list_max_size = resolving_list_max_size;
518}
519
Jakub Pawlowski96fb2732017-03-24 17:52:02 -0700520static uint8_t get_le_all_initiating_phys() {
521 uint8_t phy = PHY_LE_1M;
522 // TODO(jpawlowski): uncomment after next FW udpate
523 // if (supports_ble_2m_phy()) phy |= PHY_LE_2M;
524 // if (supports_ble_coded_phy()) phy |= PHY_LE_CODED;
525 return phy;
526}
527
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700528static const controller_t interface = {
Myles Watson4451b3b2016-11-09 10:20:44 -0800529 get_is_ready,
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700530
Myles Watson4451b3b2016-11-09 10:20:44 -0800531 get_address,
532 get_bt_version,
Zach Johnson30e58062014-09-26 21:14:34 -0700533
Myles Watson4451b3b2016-11-09 10:20:44 -0800534 get_features_classic,
535 get_last_features_classic_index,
Zach Johnson30e58062014-09-26 21:14:34 -0700536
Myles Watson4451b3b2016-11-09 10:20:44 -0800537 get_features_ble,
538 get_ble_supported_states,
Zach Johnson30e58062014-09-26 21:14:34 -0700539
Myles Watson4451b3b2016-11-09 10:20:44 -0800540 supports_simple_pairing,
541 supports_secure_connections,
542 supports_simultaneous_le_bredr,
543 supports_reading_remote_extended_features,
544 supports_interlaced_inquiry_scan,
545 supports_rssi_with_inquiry_results,
546 supports_extended_inquiry_response,
547 supports_master_slave_role_switch,
Mudumba Ananth57f65082017-02-09 09:05:48 -0800548 supports_enhanced_setup_synchronous_connection,
549 supports_enhanced_accept_synchronous_connection,
Zach Johnson30e58062014-09-26 21:14:34 -0700550
Myles Watson4451b3b2016-11-09 10:20:44 -0800551 supports_ble,
552 supports_ble_packet_extension,
553 supports_ble_connection_parameters_request,
554 supports_ble_privacy,
Jakub Pawlowskiba786012017-06-26 11:26:46 -0700555 supports_ble_set_privacy_mode,
Jakub Pawlowski4b1feb62017-03-09 18:11:43 -0800556 supports_ble_2m_phy,
557 supports_ble_coded_phy,
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800558 supports_ble_extended_advertising,
559 supports_ble_periodic_advertising,
Zach Johnson30e58062014-09-26 21:14:34 -0700560
Myles Watson4451b3b2016-11-09 10:20:44 -0800561 get_acl_data_size_classic,
562 get_acl_data_size_ble,
Zach Johnson30e58062014-09-26 21:14:34 -0700563
Myles Watson4451b3b2016-11-09 10:20:44 -0800564 get_acl_packet_size_classic,
565 get_acl_packet_size_ble,
566 get_ble_suggested_default_data_length,
Jakub Pawlowski4f0c5b52018-02-02 22:05:11 -0800567 get_ble_maximum_tx_data_length,
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800568 get_ble_maxium_advertising_data_length,
569 get_ble_number_of_supported_advertising_sets,
Zach Johnson30e58062014-09-26 21:14:34 -0700570
Myles Watson4451b3b2016-11-09 10:20:44 -0800571 get_acl_buffer_count_classic,
572 get_acl_buffer_count_ble,
Satya Calloji444a8da2015-03-06 10:38:22 -0800573
Myles Watson4451b3b2016-11-09 10:20:44 -0800574 get_ble_white_list_size,
Priti Agherab8b3e802015-04-17 18:11:24 -0700575
Myles Watson4451b3b2016-11-09 10:20:44 -0800576 get_ble_resolving_list_max_size,
577 set_ble_resolving_list_max_size,
Jakub Pawlowski96fb2732017-03-24 17:52:02 -0700578 get_local_supported_codecs,
579 get_le_all_initiating_phys};
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700580
Myles Watson4451b3b2016-11-09 10:20:44 -0800581const controller_t* controller_get_interface() {
Zach Johnson30e58062014-09-26 21:14:34 -0700582 static bool loaded = false;
583 if (!loaded) {
584 loaded = true;
585
586 hci = hci_layer_get_interface();
587 packet_factory = hci_packet_factory_get_interface();
588 packet_parser = hci_packet_parser_get_interface();
589 }
590
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700591 return &interface;
592}
593
Myles Watson4451b3b2016-11-09 10:20:44 -0800594const controller_t* controller_get_test_interface(
595 const hci_t* hci_interface,
596 const hci_packet_factory_t* packet_factory_interface,
597 const hci_packet_parser_t* packet_parser_interface) {
Zach Johnson30e58062014-09-26 21:14:34 -0700598 hci = hci_interface;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700599 packet_factory = packet_factory_interface;
600 packet_parser = packet_parser_interface;
601 return &interface;
602}