blob: 6525c14f1ec07dc33601a28505febbc3153048d0 [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
cba7cdd8e2017-09-28 16:31:59 +080025#include "bt_target.h"
Marie Janssen49a86702015-07-08 11:48:57 -070026#include "bt_types.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070027#include "btcore/include/event_mask.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070028#include "btcore/include/module.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070029#include "btcore/include/version.h"
Marie Janssen49a86702015-07-08 11:48:57 -070030#include "hcimsgs.h"
31#include "osi/include/future.h"
32#include "stack/include/btm_ble_api.h"
Zach Johnson30e58062014-09-26 21:14:34 -070033
Jakub Pawlowski692a0ca2018-04-19 02:32:40 -070034const bt_event_mask_t BLE_EVENT_MASK = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
35#if (BLE_PRIVACY_SPT == TRUE)
36 0x1E,
37#else
38 /* Disable "LE Enhanced Connection
39 Complete" when privacy is off */
40 0x1C,
41#endif
42 0x7f}};
Priti Aghera636d6712014-12-18 13:55:48 -080043
Myles Watson4451b3b2016-11-09 10:20:44 -080044const bt_event_mask_t CLASSIC_EVENT_MASK = {HCI_DUMO_EVENT_MASK_EXT};
Zach Johnson30e58062014-09-26 21:14:34 -070045
46// TODO(zachoverflow): factor out into common module
47const uint8_t SCO_HOST_BUFFER_SIZE = 0xff;
48
49#define HCI_SUPPORTED_COMMANDS_ARRAY_SIZE 64
50#define MAX_FEATURES_CLASSIC_PAGE_COUNT 3
Myles Watson4451b3b2016-11-09 10:20:44 -080051#define BLE_SUPPORTED_STATES_SIZE 8
52#define BLE_SUPPORTED_FEATURES_SIZE 8
53#define MAX_LOCAL_SUPPORTED_CODECS_SIZE 8
Zach Johnsonbf8193b2014-09-08 09:56:35 -070054
Myles Watson4451b3b2016-11-09 10:20:44 -080055static const hci_t* hci;
56static const hci_packet_factory_t* packet_factory;
57static const hci_packet_parser_t* packet_parser;
Zach Johnsonbf8193b2014-09-08 09:56:35 -070058
Jakub Pawlowskia484a882017-06-24 17:30:18 -070059static RawAddress address;
Zach Johnson30e58062014-09-26 21:14:34 -070060static bt_version_t bt_version;
Zach Johnsonbf8193b2014-09-08 09:56:35 -070061
Zach Johnson30e58062014-09-26 21:14:34 -070062static uint8_t supported_commands[HCI_SUPPORTED_COMMANDS_ARRAY_SIZE];
63static bt_device_features_t features_classic[MAX_FEATURES_CLASSIC_PAGE_COUNT];
64static uint8_t last_features_classic_page_index;
Zach Johnsonbf8193b2014-09-08 09:56:35 -070065
Zach Johnson30e58062014-09-26 21:14:34 -070066static uint16_t acl_data_size_classic;
67static uint16_t acl_data_size_ble;
68static uint16_t acl_buffer_count_classic;
69static uint8_t acl_buffer_count_ble;
70
71static uint8_t ble_white_list_size;
Satya Calloji444a8da2015-03-06 10:38:22 -080072static uint8_t ble_resolving_list_max_size;
Zach Johnson30e58062014-09-26 21:14:34 -070073static uint8_t ble_supported_states[BLE_SUPPORTED_STATES_SIZE];
74static bt_device_features_t features_ble;
Priti Aghera636d6712014-12-18 13:55:48 -080075static uint16_t ble_suggested_default_data_length;
Jakub Pawlowski4f0c5b52018-02-02 22:05:11 -080076static uint16_t ble_supported_max_tx_octets;
77static uint16_t ble_supported_max_tx_time;
78static uint16_t ble_supported_max_rx_octets;
79static uint16_t ble_supported_max_rx_time;
80
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -080081static uint16_t ble_maxium_advertising_data_length;
82static uint8_t ble_number_of_supported_advertising_sets;
Srinu Jellaf6dcf792015-07-22 17:30:32 +053083static uint8_t local_supported_codecs[MAX_LOCAL_SUPPORTED_CODECS_SIZE];
84static uint8_t number_of_local_supported_codecs = 0;
Zach Johnson30e58062014-09-26 21:14:34 -070085
86static bool readable;
87static bool ble_supported;
Srinu Jella6e7a3352015-08-24 21:00:28 +053088static bool ble_offload_features_supported;
Zach Johnson30e58062014-09-26 21:14:34 -070089static bool simple_pairing_supported;
Satya Calloji444a8da2015-03-06 10:38:22 -080090static bool secure_connections_supported;
Zach Johnson30e58062014-09-26 21:14:34 -070091
Myles Watson4451b3b2016-11-09 10:20:44 -080092#define AWAIT_COMMAND(command) \
93 static_cast<BT_HDR*>(future_await(hci->transmit_command_futured(command)))
Zach Johnson30e58062014-09-26 21:14:34 -070094
95// Module lifecycle functions
96
Myles Watson4451b3b2016-11-09 10:20:44 -080097static future_t* start_up(void) {
98 BT_HDR* response;
Zach Johnson30e58062014-09-26 21:14:34 -070099
100 // Send the initial reset command
101 response = AWAIT_COMMAND(packet_factory->make_reset());
102 packet_parser->parse_generic_command_complete(response);
103
104 // Request the classic buffer size next
105 response = AWAIT_COMMAND(packet_factory->make_read_buffer_size());
106 packet_parser->parse_read_buffer_size_response(
107 response, &acl_data_size_classic, &acl_buffer_count_classic);
108
109 // Tell the controller about our buffer sizes and buffer counts next
Myles Watson4451b3b2016-11-09 10:20:44 -0800110 // TODO(zachoverflow): factor this out. eww l2cap contamination. And why just
111 // a hardcoded 10?
112 response = AWAIT_COMMAND(packet_factory->make_host_buffer_size(
113 L2CAP_MTU_SIZE, SCO_HOST_BUFFER_SIZE, L2CAP_HOST_FC_ACL_BUFS, 10));
Zach Johnson30e58062014-09-26 21:14:34 -0700114
115 packet_parser->parse_generic_command_complete(response);
116
117 // Read the local version info off the controller next, including
118 // information such as manufacturer and supported HCI version
119 response = AWAIT_COMMAND(packet_factory->make_read_local_version_info());
120 packet_parser->parse_read_local_version_info_response(response, &bt_version);
121
122 // Read the bluetooth address off the controller next
123 response = AWAIT_COMMAND(packet_factory->make_read_bd_addr());
124 packet_parser->parse_read_bd_addr_response(response, &address);
125
126 // Request the controller's supported commands next
Myles Watson4451b3b2016-11-09 10:20:44 -0800127 response =
128 AWAIT_COMMAND(packet_factory->make_read_local_supported_commands());
Zach Johnson30e58062014-09-26 21:14:34 -0700129 packet_parser->parse_read_local_supported_commands_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800130 response, supported_commands, HCI_SUPPORTED_COMMANDS_ARRAY_SIZE);
cba7cdd8e2017-09-28 16:31:59 +0800131#if (BTM_SCO_ENHANCED_SYNC_ENABLED == FALSE)
132 supported_commands[29] &= ~0x08;
133#endif
Zach Johnson30e58062014-09-26 21:14:34 -0700134
135 // Read page 0 of the controller features next
136 uint8_t page_number = 0;
Myles Watson4451b3b2016-11-09 10:20:44 -0800137 response = AWAIT_COMMAND(
138 packet_factory->make_read_local_extended_features(page_number));
Zach Johnson30e58062014-09-26 21:14:34 -0700139 packet_parser->parse_read_local_extended_features_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800140 response, &page_number, &last_features_classic_page_index,
141 features_classic, MAX_FEATURES_CLASSIC_PAGE_COUNT);
Zach Johnson30e58062014-09-26 21:14:34 -0700142
Jack Hef2af1c42016-12-13 01:59:12 -0800143 CHECK(page_number == 0);
Zach Johnson30e58062014-09-26 21:14:34 -0700144 page_number++;
145
146 // Inform the controller what page 0 features we support, based on what
147 // it told us it supports. We need to do this first before we request the
148 // next page, because the controller's response for page 1 may be
149 // dependent on what we configure from page 0
Myles Watson4451b3b2016-11-09 10:20:44 -0800150 simple_pairing_supported =
151 HCI_SIMPLE_PAIRING_SUPPORTED(features_classic[0].as_array);
Zach Johnson30e58062014-09-26 21:14:34 -0700152 if (simple_pairing_supported) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800153 response = AWAIT_COMMAND(
154 packet_factory->make_write_simple_pairing_mode(HCI_SP_MODE_ENABLED));
Zach Johnson30e58062014-09-26 21:14:34 -0700155 packet_parser->parse_generic_command_complete(response);
156 }
157
Zach Johnson30e58062014-09-26 21:14:34 -0700158 if (HCI_LE_SPT_SUPPORTED(features_classic[0].as_array)) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800159 uint8_t simultaneous_le_host =
160 HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array)
161 ? BTM_BLE_SIMULTANEOUS_HOST
162 : 0;
163 response = AWAIT_COMMAND(packet_factory->make_ble_write_host_support(
164 BTM_BLE_HOST_SUPPORT, simultaneous_le_host));
Zach Johnson30e58062014-09-26 21:14:34 -0700165
166 packet_parser->parse_generic_command_complete(response);
Frédéric Dalleauebc367a2015-08-18 12:23:12 +0200167
168 // If we modified the BT_HOST_SUPPORT, we will need ext. feat. page 1
169 if (last_features_classic_page_index < 1)
170 last_features_classic_page_index = 1;
Zach Johnson30e58062014-09-26 21:14:34 -0700171 }
Zach Johnson30e58062014-09-26 21:14:34 -0700172
173 // Done telling the controller about what page 0 features we support
174 // Request the remaining feature pages
175 while (page_number <= last_features_classic_page_index &&
176 page_number < MAX_FEATURES_CLASSIC_PAGE_COUNT) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800177 response = AWAIT_COMMAND(
178 packet_factory->make_read_local_extended_features(page_number));
Zach Johnson30e58062014-09-26 21:14:34 -0700179 packet_parser->parse_read_local_extended_features_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800180 response, &page_number, &last_features_classic_page_index,
181 features_classic, MAX_FEATURES_CLASSIC_PAGE_COUNT);
Zach Johnson30e58062014-09-26 21:14:34 -0700182
183 page_number++;
184 }
185
Srinu Jella6e7a3352015-08-24 21:00:28 +0530186 // read BLE offload features support from controller
187 response = AWAIT_COMMAND(packet_factory->make_ble_read_offload_features_support());
188 packet_parser->parse_ble_read_offload_features_response(response, &ble_offload_features_supported);
Andre Eisenbach67a784b2015-05-26 17:40:12 -0700189#if (SC_MODE_INCLUDED == TRUE)
Srinu Jella6e7a3352015-08-24 21:00:28 +0530190 if(ble_offload_features_supported) {
191 secure_connections_supported =
192 HCI_SC_CTRLR_SUPPORTED(features_classic[2].as_array);
193 if (secure_connections_supported) {
194 response = AWAIT_COMMAND(
195 packet_factory->make_write_secure_connections_host_support(
196 HCI_SC_MODE_ENABLED));
197 packet_parser->parse_generic_command_complete(response);
198 }
Mudumba Ananth899b7712015-01-30 02:33:02 -0800199 }
Andre Eisenbach67a784b2015-05-26 17:40:12 -0700200#endif
Mudumba Ananth899b7712015-01-30 02:33:02 -0800201
Myles Watson4451b3b2016-11-09 10:20:44 -0800202 ble_supported = last_features_classic_page_index >= 1 &&
203 HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
Zach Johnson30e58062014-09-26 21:14:34 -0700204 if (ble_supported) {
205 // Request the ble white list size next
206 response = AWAIT_COMMAND(packet_factory->make_ble_read_white_list_size());
Myles Watson4451b3b2016-11-09 10:20:44 -0800207 packet_parser->parse_ble_read_white_list_size_response(
208 response, &ble_white_list_size);
Zach Johnson30e58062014-09-26 21:14:34 -0700209
210 // Request the ble buffer size next
211 response = AWAIT_COMMAND(packet_factory->make_ble_read_buffer_size());
212 packet_parser->parse_ble_read_buffer_size_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800213 response, &acl_data_size_ble, &acl_buffer_count_ble);
Zach Johnson30e58062014-09-26 21:14:34 -0700214
215 // Response of 0 indicates ble has the same buffer size as classic
Myles Watson4451b3b2016-11-09 10:20:44 -0800216 if (acl_data_size_ble == 0) acl_data_size_ble = acl_data_size_classic;
Zach Johnson30e58062014-09-26 21:14:34 -0700217
218 // Request the ble supported states next
219 response = AWAIT_COMMAND(packet_factory->make_ble_read_supported_states());
220 packet_parser->parse_ble_read_supported_states_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800221 response, ble_supported_states, sizeof(ble_supported_states));
Zach Johnson30e58062014-09-26 21:14:34 -0700222
223 // Request the ble supported features next
Myles Watson4451b3b2016-11-09 10:20:44 -0800224 response =
225 AWAIT_COMMAND(packet_factory->make_ble_read_local_supported_features());
Zach Johnson30e58062014-09-26 21:14:34 -0700226 packet_parser->parse_ble_read_local_supported_features_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800227 response, &features_ble);
Zach Johnson30e58062014-09-26 21:14:34 -0700228
Satya Calloji444a8da2015-03-06 10:38:22 -0800229 if (HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array)) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800230 response =
231 AWAIT_COMMAND(packet_factory->make_ble_read_resolving_list_size());
232 packet_parser->parse_ble_read_resolving_list_size_response(
233 response, &ble_resolving_list_max_size);
Satya Calloji444a8da2015-03-06 10:38:22 -0800234 }
235
Priti Aghera636d6712014-12-18 13:55:48 -0800236 if (HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array)) {
Jakub Pawlowski4f0c5b52018-02-02 22:05:11 -0800237 response =
238 AWAIT_COMMAND(packet_factory->make_ble_read_maximum_data_length());
239 packet_parser->parse_ble_read_maximum_data_length_response(
240 response, &ble_supported_max_tx_octets, &ble_supported_max_tx_time,
241 &ble_supported_max_rx_octets, &ble_supported_max_rx_time);
242
Myles Watson4451b3b2016-11-09 10:20:44 -0800243 response = AWAIT_COMMAND(
244 packet_factory->make_ble_read_suggested_default_data_length());
245 packet_parser->parse_ble_read_suggested_default_data_length_response(
246 response, &ble_suggested_default_data_length);
Priti Aghera636d6712014-12-18 13:55:48 -0800247 }
248
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800249 if (HCI_LE_EXTENDED_ADVERTISING_SUPPORTED(features_ble.as_array)) {
250 response = AWAIT_COMMAND(
251 packet_factory->make_ble_read_maximum_advertising_data_length());
252 packet_parser->parse_ble_read_maximum_advertising_data_length(
253 response, &ble_maxium_advertising_data_length);
254
255 response = AWAIT_COMMAND(
256 packet_factory->make_ble_read_number_of_supported_advertising_sets());
257 packet_parser->parse_ble_read_number_of_supported_advertising_sets(
258 response, &ble_number_of_supported_advertising_sets);
Jakub Pawlowski5a8a1622017-03-16 18:19:20 -0700259 } else {
260 /* If LE Excended Advertising is not supported, use the default value */
261 ble_maxium_advertising_data_length = 31;
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800262 }
263
Zach Johnson30e58062014-09-26 21:14:34 -0700264 // Set the ble event mask next
Myles Watson4451b3b2016-11-09 10:20:44 -0800265 response =
266 AWAIT_COMMAND(packet_factory->make_ble_set_event_mask(&BLE_EVENT_MASK));
Zach Johnson30e58062014-09-26 21:14:34 -0700267 packet_parser->parse_generic_command_complete(response);
268 }
Zach Johnson30e58062014-09-26 21:14:34 -0700269
270 if (simple_pairing_supported) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800271 response =
272 AWAIT_COMMAND(packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK));
Zach Johnson30e58062014-09-26 21:14:34 -0700273 packet_parser->parse_generic_command_complete(response);
274 }
275
Srinu Jellaf6dcf792015-07-22 17:30:32 +0530276 // read local supported codecs
Myles Watson4451b3b2016-11-09 10:20:44 -0800277 if (HCI_READ_LOCAL_CODECS_SUPPORTED(supported_commands)) {
278 response =
279 AWAIT_COMMAND(packet_factory->make_read_local_supported_codecs());
Srinu Jellaf6dcf792015-07-22 17:30:32 +0530280 packet_parser->parse_read_local_supported_codecs_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800281 response, &number_of_local_supported_codecs, local_supported_codecs);
Srinu Jellaf6dcf792015-07-22 17:30:32 +0530282 }
283
Jakub Pawlowski3c03d4f2019-02-14 12:44:06 +0100284 if (!HCI_READ_ENCR_KEY_SIZE_SUPPORTED(supported_commands)) {
285 LOG(FATAL) << " Controller must support Read Encryption Key Size command";
286 }
287
Zach Johnson30e58062014-09-26 21:14:34 -0700288 readable = true;
289 return future_new_immediate(FUTURE_SUCCESS);
290}
291
Myles Watson4451b3b2016-11-09 10:20:44 -0800292static future_t* shut_down(void) {
Zach Johnson30e58062014-09-26 21:14:34 -0700293 readable = false;
294 return future_new_immediate(FUTURE_SUCCESS);
295}
296
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -0700297EXPORT_SYMBOL extern const module_t controller_module = {
Myles Watson4451b3b2016-11-09 10:20:44 -0800298 .name = CONTROLLER_MODULE,
299 .init = NULL,
300 .start_up = start_up,
301 .shut_down = shut_down,
302 .clean_up = NULL,
303 .dependencies = {HCI_MODULE, NULL}};
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700304
305// Interface functions
306
Myles Watson4451b3b2016-11-09 10:20:44 -0800307static bool get_is_ready(void) { return readable; }
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700308
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700309static const RawAddress* get_address(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800310 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700311 return &address;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700312}
313
Myles Watson4451b3b2016-11-09 10:20:44 -0800314static const bt_version_t* get_bt_version(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800315 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700316 return &bt_version;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700317}
318
Zach Johnson30e58062014-09-26 21:14:34 -0700319// TODO(zachoverflow): hide inside, move decoder inside too
Myles Watson4451b3b2016-11-09 10:20:44 -0800320static const bt_device_features_t* get_features_classic(int index) {
Jack Hef2af1c42016-12-13 01:59:12 -0800321 CHECK(readable);
322 CHECK(index < MAX_FEATURES_CLASSIC_PAGE_COUNT);
Zach Johnson30e58062014-09-26 21:14:34 -0700323 return &features_classic[index];
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700324}
325
Zach Johnson30e58062014-09-26 21:14:34 -0700326static uint8_t get_last_features_classic_index(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800327 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700328 return last_features_classic_page_index;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700329}
330
Myles Watson4451b3b2016-11-09 10:20:44 -0800331static uint8_t* get_local_supported_codecs(uint8_t* number_of_codecs) {
Jack Hef2af1c42016-12-13 01:59:12 -0800332 CHECK(readable);
Myles Watson4451b3b2016-11-09 10:20:44 -0800333 if (number_of_local_supported_codecs) {
Srinu Jellaf6dcf792015-07-22 17:30:32 +0530334 *number_of_codecs = number_of_local_supported_codecs;
335 return local_supported_codecs;
336 }
337 return NULL;
338}
339
Myles Watson4451b3b2016-11-09 10:20:44 -0800340static const bt_device_features_t* get_features_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800341 CHECK(readable);
342 CHECK(ble_supported);
Zach Johnson30e58062014-09-26 21:14:34 -0700343 return &features_ble;
344}
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700345
Myles Watson4451b3b2016-11-09 10:20:44 -0800346static const uint8_t* get_ble_supported_states(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800347 CHECK(readable);
348 CHECK(ble_supported);
Zach Johnsona87c8212014-10-28 17:59:13 -0700349 return ble_supported_states;
350}
351
Zach Johnson30e58062014-09-26 21:14:34 -0700352static bool supports_simple_pairing(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800353 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700354 return simple_pairing_supported;
355}
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700356
Satya Calloji444a8da2015-03-06 10:38:22 -0800357static bool supports_secure_connections(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800358 CHECK(readable);
Satya Calloji444a8da2015-03-06 10:38:22 -0800359 return secure_connections_supported;
360}
361
Zach Johnson30e58062014-09-26 21:14:34 -0700362static bool supports_simultaneous_le_bredr(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800363 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700364 return HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array);
365}
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700366
Zach Johnson30e58062014-09-26 21:14:34 -0700367static bool supports_reading_remote_extended_features(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800368 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700369 return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(supported_commands);
370}
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700371
Zach Johnson30e58062014-09-26 21:14:34 -0700372static bool supports_interlaced_inquiry_scan(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800373 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700374 return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(features_classic[0].as_array);
375}
376
377static bool supports_rssi_with_inquiry_results(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800378 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700379 return HCI_LMP_INQ_RSSI_SUPPORTED(features_classic[0].as_array);
380}
381
382static bool supports_extended_inquiry_response(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800383 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700384 return HCI_EXT_INQ_RSP_SUPPORTED(features_classic[0].as_array);
385}
386
387static bool supports_master_slave_role_switch(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800388 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700389 return HCI_SWITCH_SUPPORTED(features_classic[0].as_array);
390}
391
Mudumba Ananth57f65082017-02-09 09:05:48 -0800392static bool supports_enhanced_setup_synchronous_connection(void) {
393 assert(readable);
394 return HCI_ENH_SETUP_SYNCH_CONN_SUPPORTED(supported_commands);
395}
396
397static bool supports_enhanced_accept_synchronous_connection(void) {
398 assert(readable);
399 return HCI_ENH_ACCEPT_SYNCH_CONN_SUPPORTED(supported_commands);
400}
401
Zach Johnson30e58062014-09-26 21:14:34 -0700402static bool supports_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800403 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700404 return ble_supported;
405}
406
Satya Calloji444a8da2015-03-06 10:38:22 -0800407static bool supports_ble_privacy(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800408 CHECK(readable);
409 CHECK(ble_supported);
Satya Calloji444a8da2015-03-06 10:38:22 -0800410 return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array);
411}
412
Jakub Pawlowskiba786012017-06-26 11:26:46 -0700413static bool supports_ble_set_privacy_mode() {
414 CHECK(readable);
415 CHECK(ble_supported);
416 return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array) &&
417 HCI_LE_SET_PRIVACY_MODE_SUPPORTED(supported_commands);
418}
419
Priti Aghera636d6712014-12-18 13:55:48 -0800420static bool supports_ble_packet_extension(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800421 CHECK(readable);
422 CHECK(ble_supported);
Priti Aghera636d6712014-12-18 13:55:48 -0800423 return HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array);
424}
425
Zach Johnson30e58062014-09-26 21:14:34 -0700426static bool supports_ble_connection_parameters_request(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800427 CHECK(readable);
428 CHECK(ble_supported);
Zach Johnson30e58062014-09-26 21:14:34 -0700429 return HCI_LE_CONN_PARAM_REQ_SUPPORTED(features_ble.as_array);
430}
431
Jakub Pawlowski4b1feb62017-03-09 18:11:43 -0800432static bool supports_ble_2m_phy(void) {
433 CHECK(readable);
434 CHECK(ble_supported);
435 return HCI_LE_2M_PHY_SUPPORTED(features_ble.as_array);
436}
437
438static bool supports_ble_coded_phy(void) {
439 CHECK(readable);
440 CHECK(ble_supported);
441 return HCI_LE_CODED_PHY_SUPPORTED(features_ble.as_array);
442}
443
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800444static bool supports_ble_extended_advertising(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800445 CHECK(readable);
446 CHECK(ble_supported);
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800447 return HCI_LE_EXTENDED_ADVERTISING_SUPPORTED(features_ble.as_array);
448}
449
450static bool supports_ble_periodic_advertising(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800451 CHECK(readable);
452 CHECK(ble_supported);
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800453 return HCI_LE_PERIODIC_ADVERTISING_SUPPORTED(features_ble.as_array);
454}
455
Srinu Jella6e7a3352015-08-24 21:00:28 +0530456static bool supports_ble_offload_features(void) {
457 assert(readable);
458 assert(ble_supported);
459 return ble_offload_features_supported;
460}
461
Zach Johnson30e58062014-09-26 21:14:34 -0700462static uint16_t get_acl_data_size_classic(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800463 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700464 return acl_data_size_classic;
465}
466
467static uint16_t get_acl_data_size_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800468 CHECK(readable);
469 CHECK(ble_supported);
Zach Johnson30e58062014-09-26 21:14:34 -0700470 return acl_data_size_ble;
471}
472
473static uint16_t get_acl_packet_size_classic(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800474 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700475 return acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE;
476}
477
478static uint16_t get_acl_packet_size_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800479 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700480 return acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE;
481}
482
Priti Aghera636d6712014-12-18 13:55:48 -0800483static uint16_t get_ble_suggested_default_data_length(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800484 CHECK(readable);
485 CHECK(ble_supported);
Priti Aghera636d6712014-12-18 13:55:48 -0800486 return ble_suggested_default_data_length;
487}
488
Jakub Pawlowski4f0c5b52018-02-02 22:05:11 -0800489static uint16_t get_ble_maximum_tx_data_length(void) {
490 CHECK(readable);
491 CHECK(ble_supported);
492 return ble_supported_max_tx_octets;
493}
494
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800495static uint16_t get_ble_maxium_advertising_data_length(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800496 CHECK(readable);
497 CHECK(ble_supported);
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800498 return ble_maxium_advertising_data_length;
499}
500
501static uint8_t get_ble_number_of_supported_advertising_sets(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800502 CHECK(readable);
503 CHECK(ble_supported);
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800504 return ble_number_of_supported_advertising_sets;
505}
506
Zach Johnson30e58062014-09-26 21:14:34 -0700507static uint16_t get_acl_buffer_count_classic(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800508 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700509 return acl_buffer_count_classic;
510}
511
512static uint8_t get_acl_buffer_count_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800513 CHECK(readable);
514 CHECK(ble_supported);
Zach Johnson30e58062014-09-26 21:14:34 -0700515 return acl_buffer_count_ble;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700516}
517
Priti Agherab8b3e802015-04-17 18:11:24 -0700518static uint8_t get_ble_white_list_size(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800519 CHECK(readable);
520 CHECK(ble_supported);
Priti Agherab8b3e802015-04-17 18:11:24 -0700521 return ble_white_list_size;
522}
523
Satya Calloji444a8da2015-03-06 10:38:22 -0800524static uint8_t get_ble_resolving_list_max_size(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800525 CHECK(readable);
526 CHECK(ble_supported);
Satya Calloji444a8da2015-03-06 10:38:22 -0800527 return ble_resolving_list_max_size;
528}
529
530static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
Pavlin Radoslavov58318f42016-05-05 16:20:15 -0700531 // Setting "resolving_list_max_size" to 0 is done during cleanup,
532 // hence we ignore the "readable" flag already set to false during shutdown.
533 if (resolving_list_max_size != 0) {
Jack Hef2af1c42016-12-13 01:59:12 -0800534 CHECK(readable);
Pavlin Radoslavov58318f42016-05-05 16:20:15 -0700535 }
Jack Hef2af1c42016-12-13 01:59:12 -0800536 CHECK(ble_supported);
Satya Calloji444a8da2015-03-06 10:38:22 -0800537 ble_resolving_list_max_size = resolving_list_max_size;
538}
539
Jakub Pawlowski96fb2732017-03-24 17:52:02 -0700540static uint8_t get_le_all_initiating_phys() {
541 uint8_t phy = PHY_LE_1M;
542 // TODO(jpawlowski): uncomment after next FW udpate
543 // if (supports_ble_2m_phy()) phy |= PHY_LE_2M;
544 // if (supports_ble_coded_phy()) phy |= PHY_LE_CODED;
545 return phy;
546}
547
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700548static const controller_t interface = {
Myles Watson4451b3b2016-11-09 10:20:44 -0800549 get_is_ready,
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700550
Myles Watson4451b3b2016-11-09 10:20:44 -0800551 get_address,
552 get_bt_version,
Zach Johnson30e58062014-09-26 21:14:34 -0700553
Myles Watson4451b3b2016-11-09 10:20:44 -0800554 get_features_classic,
555 get_last_features_classic_index,
Zach Johnson30e58062014-09-26 21:14:34 -0700556
Myles Watson4451b3b2016-11-09 10:20:44 -0800557 get_features_ble,
558 get_ble_supported_states,
Zach Johnson30e58062014-09-26 21:14:34 -0700559
Myles Watson4451b3b2016-11-09 10:20:44 -0800560 supports_simple_pairing,
561 supports_secure_connections,
562 supports_simultaneous_le_bredr,
563 supports_reading_remote_extended_features,
564 supports_interlaced_inquiry_scan,
565 supports_rssi_with_inquiry_results,
566 supports_extended_inquiry_response,
567 supports_master_slave_role_switch,
Mudumba Ananth57f65082017-02-09 09:05:48 -0800568 supports_enhanced_setup_synchronous_connection,
569 supports_enhanced_accept_synchronous_connection,
Zach Johnson30e58062014-09-26 21:14:34 -0700570
Myles Watson4451b3b2016-11-09 10:20:44 -0800571 supports_ble,
572 supports_ble_packet_extension,
573 supports_ble_connection_parameters_request,
574 supports_ble_privacy,
Jakub Pawlowskiba786012017-06-26 11:26:46 -0700575 supports_ble_set_privacy_mode,
Jakub Pawlowski4b1feb62017-03-09 18:11:43 -0800576 supports_ble_2m_phy,
577 supports_ble_coded_phy,
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800578 supports_ble_extended_advertising,
579 supports_ble_periodic_advertising,
Zach Johnson30e58062014-09-26 21:14:34 -0700580
Myles Watson4451b3b2016-11-09 10:20:44 -0800581 get_acl_data_size_classic,
582 get_acl_data_size_ble,
Zach Johnson30e58062014-09-26 21:14:34 -0700583
Myles Watson4451b3b2016-11-09 10:20:44 -0800584 get_acl_packet_size_classic,
585 get_acl_packet_size_ble,
586 get_ble_suggested_default_data_length,
Jakub Pawlowski4f0c5b52018-02-02 22:05:11 -0800587 get_ble_maximum_tx_data_length,
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800588 get_ble_maxium_advertising_data_length,
589 get_ble_number_of_supported_advertising_sets,
Zach Johnson30e58062014-09-26 21:14:34 -0700590
Myles Watson4451b3b2016-11-09 10:20:44 -0800591 get_acl_buffer_count_classic,
592 get_acl_buffer_count_ble,
Satya Calloji444a8da2015-03-06 10:38:22 -0800593
Myles Watson4451b3b2016-11-09 10:20:44 -0800594 get_ble_white_list_size,
Priti Agherab8b3e802015-04-17 18:11:24 -0700595
Myles Watson4451b3b2016-11-09 10:20:44 -0800596 get_ble_resolving_list_max_size,
597 set_ble_resolving_list_max_size,
Jakub Pawlowski96fb2732017-03-24 17:52:02 -0700598 get_local_supported_codecs,
Srinu Jella6e7a3352015-08-24 21:00:28 +0530599 supports_ble_offload_features,
Jakub Pawlowski96fb2732017-03-24 17:52:02 -0700600 get_le_all_initiating_phys};
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700601
Myles Watson4451b3b2016-11-09 10:20:44 -0800602const controller_t* controller_get_interface() {
Zach Johnson30e58062014-09-26 21:14:34 -0700603 static bool loaded = false;
604 if (!loaded) {
605 loaded = true;
606
607 hci = hci_layer_get_interface();
608 packet_factory = hci_packet_factory_get_interface();
609 packet_parser = hci_packet_parser_get_interface();
610 }
611
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700612 return &interface;
613}
614
Myles Watson4451b3b2016-11-09 10:20:44 -0800615const controller_t* controller_get_test_interface(
616 const hci_t* hci_interface,
617 const hci_packet_factory_t* packet_factory_interface,
618 const hci_packet_parser_t* packet_parser_interface) {
Zach Johnson30e58062014-09-26 21:14:34 -0700619 hci = hci_interface;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700620 packet_factory = packet_factory_interface;
621 packet_parser = packet_parser_interface;
622 return &interface;
623}