blob: b5312de022cddb4fec3193e58dd2147e57070c0e [file] [log] [blame]
Zach Johnsonbf8193b2014-09-08 09:56:35 -07001/******************************************************************************
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
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"
RAJATH R9d59e3f2017-06-07 14:45:28 +053032#include "osi/include/log.h"
33#include "utils/include/bt_utils.h"
Zach Johnson30e58062014-09-26 21:14:34 -070034
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -070035const bt_event_mask_t BLE_EVENT_MASK = {
Jakub Pawlowskieafd45d2017-03-22 19:00:47 -070036 {0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x1E, 0x7f}};
Priti Aghera636d6712014-12-18 13:55:48 -080037
Myles Watson4451b3b2016-11-09 10:20:44 -080038const bt_event_mask_t CLASSIC_EVENT_MASK = {HCI_DUMO_EVENT_MASK_EXT};
Zach Johnson30e58062014-09-26 21:14:34 -070039
40// TODO(zachoverflow): factor out into common module
41const uint8_t SCO_HOST_BUFFER_SIZE = 0xff;
42
43#define HCI_SUPPORTED_COMMANDS_ARRAY_SIZE 64
44#define MAX_FEATURES_CLASSIC_PAGE_COUNT 3
Myles Watson4451b3b2016-11-09 10:20:44 -080045#define BLE_SUPPORTED_STATES_SIZE 8
46#define BLE_SUPPORTED_FEATURES_SIZE 8
47#define MAX_LOCAL_SUPPORTED_CODECS_SIZE 8
RAJATH R9d59e3f2017-06-07 14:45:28 +053048#define UNUSED(x) (void)(x)
Zach Johnsonbf8193b2014-09-08 09:56:35 -070049
Myles Watson4451b3b2016-11-09 10:20:44 -080050static const hci_t* hci;
51static const hci_packet_factory_t* packet_factory;
52static const hci_packet_parser_t* packet_parser;
Zach Johnsonbf8193b2014-09-08 09:56:35 -070053
Zach Johnson30e58062014-09-26 21:14:34 -070054static bt_bdaddr_t address;
55static bt_version_t bt_version;
Zach Johnsonbf8193b2014-09-08 09:56:35 -070056
Zach Johnson30e58062014-09-26 21:14:34 -070057static uint8_t supported_commands[HCI_SUPPORTED_COMMANDS_ARRAY_SIZE];
58static bt_device_features_t features_classic[MAX_FEATURES_CLASSIC_PAGE_COUNT];
59static uint8_t last_features_classic_page_index;
Zach Johnsonbf8193b2014-09-08 09:56:35 -070060
Zach Johnson30e58062014-09-26 21:14:34 -070061static uint16_t acl_data_size_classic;
62static uint16_t acl_data_size_ble;
63static uint16_t acl_buffer_count_classic;
64static uint8_t acl_buffer_count_ble;
65
66static uint8_t ble_white_list_size;
Satya Calloji444a8da2015-03-06 10:38:22 -080067static uint8_t ble_resolving_list_max_size;
Zach Johnson30e58062014-09-26 21:14:34 -070068static uint8_t ble_supported_states[BLE_SUPPORTED_STATES_SIZE];
69static bt_device_features_t features_ble;
Priti Aghera636d6712014-12-18 13:55:48 -080070static uint16_t ble_suggested_default_data_length;
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -080071static uint16_t ble_maxium_advertising_data_length;
72static uint8_t ble_number_of_supported_advertising_sets;
Srinu Jellaf6dcf792015-07-22 17:30:32 +053073static uint8_t local_supported_codecs[MAX_LOCAL_SUPPORTED_CODECS_SIZE];
74static uint8_t number_of_local_supported_codecs = 0;
Zach Johnson30e58062014-09-26 21:14:34 -070075
76static bool readable;
77static bool ble_supported;
Srinu Jella039b25b2015-08-24 21:00:28 +053078static bool ble_offload_features_supported;
Zach Johnson30e58062014-09-26 21:14:34 -070079static bool simple_pairing_supported;
Satya Calloji444a8da2015-03-06 10:38:22 -080080static bool secure_connections_supported;
Zach Johnson30e58062014-09-26 21:14:34 -070081
Myles Watson4451b3b2016-11-09 10:20:44 -080082#define AWAIT_COMMAND(command) \
83 static_cast<BT_HDR*>(future_await(hci->transmit_command_futured(command)))
Zach Johnson30e58062014-09-26 21:14:34 -070084
85// Module lifecycle functions
86
Myles Watson4451b3b2016-11-09 10:20:44 -080087static future_t* start_up(void) {
88 BT_HDR* response;
Zach Johnson30e58062014-09-26 21:14:34 -070089
90 // Send the initial reset command
91 response = AWAIT_COMMAND(packet_factory->make_reset());
92 packet_parser->parse_generic_command_complete(response);
93
94 // Request the classic buffer size next
95 response = AWAIT_COMMAND(packet_factory->make_read_buffer_size());
96 packet_parser->parse_read_buffer_size_response(
97 response, &acl_data_size_classic, &acl_buffer_count_classic);
98
99 // Tell the controller about our buffer sizes and buffer counts next
Myles Watson4451b3b2016-11-09 10:20:44 -0800100 // TODO(zachoverflow): factor this out. eww l2cap contamination. And why just
101 // a hardcoded 10?
102 response = AWAIT_COMMAND(packet_factory->make_host_buffer_size(
103 L2CAP_MTU_SIZE, SCO_HOST_BUFFER_SIZE, L2CAP_HOST_FC_ACL_BUFS, 10));
Zach Johnson30e58062014-09-26 21:14:34 -0700104
105 packet_parser->parse_generic_command_complete(response);
106
107 // Read the local version info off the controller next, including
108 // information such as manufacturer and supported HCI version
109 response = AWAIT_COMMAND(packet_factory->make_read_local_version_info());
110 packet_parser->parse_read_local_version_info_response(response, &bt_version);
111
112 // Read the bluetooth address off the controller next
113 response = AWAIT_COMMAND(packet_factory->make_read_bd_addr());
114 packet_parser->parse_read_bd_addr_response(response, &address);
115
116 // Request the controller's supported commands next
Myles Watson4451b3b2016-11-09 10:20:44 -0800117 response =
118 AWAIT_COMMAND(packet_factory->make_read_local_supported_commands());
Zach Johnson30e58062014-09-26 21:14:34 -0700119 packet_parser->parse_read_local_supported_commands_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800120 response, supported_commands, HCI_SUPPORTED_COMMANDS_ARRAY_SIZE);
Zach Johnson30e58062014-09-26 21:14:34 -0700121
122 // Read page 0 of the controller features next
123 uint8_t page_number = 0;
Myles Watson4451b3b2016-11-09 10:20:44 -0800124 response = AWAIT_COMMAND(
125 packet_factory->make_read_local_extended_features(page_number));
Zach Johnson30e58062014-09-26 21:14:34 -0700126 packet_parser->parse_read_local_extended_features_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800127 response, &page_number, &last_features_classic_page_index,
128 features_classic, MAX_FEATURES_CLASSIC_PAGE_COUNT);
Zach Johnson30e58062014-09-26 21:14:34 -0700129
Jack Hef2af1c42016-12-13 01:59:12 -0800130 CHECK(page_number == 0);
Zach Johnson30e58062014-09-26 21:14:34 -0700131 page_number++;
132
133 // Inform the controller what page 0 features we support, based on what
134 // it told us it supports. We need to do this first before we request the
135 // next page, because the controller's response for page 1 may be
136 // dependent on what we configure from page 0
Myles Watson4451b3b2016-11-09 10:20:44 -0800137 simple_pairing_supported =
138 HCI_SIMPLE_PAIRING_SUPPORTED(features_classic[0].as_array);
Zach Johnson30e58062014-09-26 21:14:34 -0700139 if (simple_pairing_supported) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800140 response = AWAIT_COMMAND(
141 packet_factory->make_write_simple_pairing_mode(HCI_SP_MODE_ENABLED));
Zach Johnson30e58062014-09-26 21:14:34 -0700142 packet_parser->parse_generic_command_complete(response);
143 }
144
Zach Johnson30e58062014-09-26 21:14:34 -0700145 if (HCI_LE_SPT_SUPPORTED(features_classic[0].as_array)) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800146 uint8_t simultaneous_le_host =
147 HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array)
148 ? BTM_BLE_SIMULTANEOUS_HOST
149 : 0;
150 response = AWAIT_COMMAND(packet_factory->make_ble_write_host_support(
151 BTM_BLE_HOST_SUPPORT, simultaneous_le_host));
Zach Johnson30e58062014-09-26 21:14:34 -0700152
153 packet_parser->parse_generic_command_complete(response);
Frédéric Dalleauebc367a2015-08-18 12:23:12 +0200154
155 // If we modified the BT_HOST_SUPPORT, we will need ext. feat. page 1
156 if (last_features_classic_page_index < 1)
157 last_features_classic_page_index = 1;
Zach Johnson30e58062014-09-26 21:14:34 -0700158 }
Zach Johnson30e58062014-09-26 21:14:34 -0700159
160 // Done telling the controller about what page 0 features we support
161 // Request the remaining feature pages
162 while (page_number <= last_features_classic_page_index &&
163 page_number < MAX_FEATURES_CLASSIC_PAGE_COUNT) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800164 response = AWAIT_COMMAND(
165 packet_factory->make_read_local_extended_features(page_number));
Zach Johnson30e58062014-09-26 21:14:34 -0700166 packet_parser->parse_read_local_extended_features_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800167 response, &page_number, &last_features_classic_page_index,
168 features_classic, MAX_FEATURES_CLASSIC_PAGE_COUNT);
Zach Johnson30e58062014-09-26 21:14:34 -0700169
170 page_number++;
171 }
172
Srinu Jella039b25b2015-08-24 21:00:28 +0530173 // read BLE offload features support from controller
174 response = AWAIT_COMMAND(packet_factory->make_ble_read_offload_features_support());
175 packet_parser->parse_ble_read_offload_features_response(response, &ble_offload_features_supported);
Andre Eisenbach67a784b2015-05-26 17:40:12 -0700176#if (SC_MODE_INCLUDED == TRUE)
Srinu Jella039b25b2015-08-24 21:00:28 +0530177 if(ble_offload_features_supported) {
178 secure_connections_supported =
179 HCI_SC_CTRLR_SUPPORTED(features_classic[2].as_array);
180 if (secure_connections_supported) {
181 response = AWAIT_COMMAND(
182 packet_factory->make_write_secure_connections_host_support(
183 HCI_SC_MODE_ENABLED));
184 packet_parser->parse_generic_command_complete(response);
185 }
Mudumba Ananth899b7712015-01-30 02:33:02 -0800186 }
Andre Eisenbach67a784b2015-05-26 17:40:12 -0700187#endif
Mudumba Ananth899b7712015-01-30 02:33:02 -0800188
Myles Watson4451b3b2016-11-09 10:20:44 -0800189 ble_supported = last_features_classic_page_index >= 1 &&
190 HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
Zach Johnson30e58062014-09-26 21:14:34 -0700191 if (ble_supported) {
192 // Request the ble white list size next
193 response = AWAIT_COMMAND(packet_factory->make_ble_read_white_list_size());
Myles Watson4451b3b2016-11-09 10:20:44 -0800194 packet_parser->parse_ble_read_white_list_size_response(
195 response, &ble_white_list_size);
Zach Johnson30e58062014-09-26 21:14:34 -0700196
197 // Request the ble buffer size next
198 response = AWAIT_COMMAND(packet_factory->make_ble_read_buffer_size());
199 packet_parser->parse_ble_read_buffer_size_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800200 response, &acl_data_size_ble, &acl_buffer_count_ble);
Zach Johnson30e58062014-09-26 21:14:34 -0700201
202 // Response of 0 indicates ble has the same buffer size as classic
Myles Watson4451b3b2016-11-09 10:20:44 -0800203 if (acl_data_size_ble == 0) acl_data_size_ble = acl_data_size_classic;
Zach Johnson30e58062014-09-26 21:14:34 -0700204
205 // Request the ble supported states next
206 response = AWAIT_COMMAND(packet_factory->make_ble_read_supported_states());
207 packet_parser->parse_ble_read_supported_states_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800208 response, ble_supported_states, sizeof(ble_supported_states));
Zach Johnson30e58062014-09-26 21:14:34 -0700209
210 // Request the ble supported features next
Myles Watson4451b3b2016-11-09 10:20:44 -0800211 response =
212 AWAIT_COMMAND(packet_factory->make_ble_read_local_supported_features());
Zach Johnson30e58062014-09-26 21:14:34 -0700213 packet_parser->parse_ble_read_local_supported_features_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800214 response, &features_ble);
Zach Johnson30e58062014-09-26 21:14:34 -0700215
Satya Calloji444a8da2015-03-06 10:38:22 -0800216 if (HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array)) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800217 response =
218 AWAIT_COMMAND(packet_factory->make_ble_read_resolving_list_size());
219 packet_parser->parse_ble_read_resolving_list_size_response(
220 response, &ble_resolving_list_max_size);
Satya Calloji444a8da2015-03-06 10:38:22 -0800221 }
222
Priti Aghera636d6712014-12-18 13:55:48 -0800223 if (HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array)) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800224 response = AWAIT_COMMAND(
225 packet_factory->make_ble_read_suggested_default_data_length());
226 packet_parser->parse_ble_read_suggested_default_data_length_response(
227 response, &ble_suggested_default_data_length);
Priti Aghera636d6712014-12-18 13:55:48 -0800228 }
229
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800230 if (HCI_LE_EXTENDED_ADVERTISING_SUPPORTED(features_ble.as_array)) {
231 response = AWAIT_COMMAND(
232 packet_factory->make_ble_read_maximum_advertising_data_length());
233 packet_parser->parse_ble_read_maximum_advertising_data_length(
234 response, &ble_maxium_advertising_data_length);
235
236 response = AWAIT_COMMAND(
237 packet_factory->make_ble_read_number_of_supported_advertising_sets());
238 packet_parser->parse_ble_read_number_of_supported_advertising_sets(
239 response, &ble_number_of_supported_advertising_sets);
Jakub Pawlowski5a8a1622017-03-16 18:19:20 -0700240 } else {
241 /* If LE Excended Advertising is not supported, use the default value */
242 ble_maxium_advertising_data_length = 31;
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800243 }
244
Zach Johnson30e58062014-09-26 21:14:34 -0700245 // Set the ble event mask next
Myles Watson4451b3b2016-11-09 10:20:44 -0800246 response =
247 AWAIT_COMMAND(packet_factory->make_ble_set_event_mask(&BLE_EVENT_MASK));
Zach Johnson30e58062014-09-26 21:14:34 -0700248 packet_parser->parse_generic_command_complete(response);
249 }
Zach Johnson30e58062014-09-26 21:14:34 -0700250
251 if (simple_pairing_supported) {
Myles Watson4451b3b2016-11-09 10:20:44 -0800252 response =
253 AWAIT_COMMAND(packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK));
Zach Johnson30e58062014-09-26 21:14:34 -0700254 packet_parser->parse_generic_command_complete(response);
255 }
256
Srinu Jellaf6dcf792015-07-22 17:30:32 +0530257 // read local supported codecs
Myles Watson4451b3b2016-11-09 10:20:44 -0800258 if (HCI_READ_LOCAL_CODECS_SUPPORTED(supported_commands)) {
259 response =
260 AWAIT_COMMAND(packet_factory->make_read_local_supported_codecs());
Srinu Jellaf6dcf792015-07-22 17:30:32 +0530261 packet_parser->parse_read_local_supported_codecs_response(
Myles Watson4451b3b2016-11-09 10:20:44 -0800262 response, &number_of_local_supported_codecs, local_supported_codecs);
Srinu Jellaf6dcf792015-07-22 17:30:32 +0530263 }
264
Zach Johnson30e58062014-09-26 21:14:34 -0700265 readable = true;
266 return future_new_immediate(FUTURE_SUCCESS);
267}
268
Myles Watson4451b3b2016-11-09 10:20:44 -0800269static future_t* shut_down(void) {
Zach Johnson30e58062014-09-26 21:14:34 -0700270 readable = false;
271 return future_new_immediate(FUTURE_SUCCESS);
272}
273
Pavlin Radoslavovb2a292b2016-10-14 19:34:48 -0700274EXPORT_SYMBOL extern const module_t controller_module = {
Myles Watson4451b3b2016-11-09 10:20:44 -0800275 .name = CONTROLLER_MODULE,
276 .init = NULL,
277 .start_up = start_up,
278 .shut_down = shut_down,
279 .clean_up = NULL,
280 .dependencies = {HCI_MODULE, NULL}};
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700281
282// Interface functions
283
Myles Watson4451b3b2016-11-09 10:20:44 -0800284static bool get_is_ready(void) { return readable; }
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700285
Myles Watson4451b3b2016-11-09 10:20:44 -0800286static const bt_bdaddr_t* get_address(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800287 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700288 return &address;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700289}
290
Myles Watson4451b3b2016-11-09 10:20:44 -0800291static const bt_version_t* get_bt_version(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800292 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700293 return &bt_version;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700294}
295
Zach Johnson30e58062014-09-26 21:14:34 -0700296// TODO(zachoverflow): hide inside, move decoder inside too
Myles Watson4451b3b2016-11-09 10:20:44 -0800297static const bt_device_features_t* get_features_classic(int index) {
Jack Hef2af1c42016-12-13 01:59:12 -0800298 CHECK(readable);
299 CHECK(index < MAX_FEATURES_CLASSIC_PAGE_COUNT);
Zach Johnson30e58062014-09-26 21:14:34 -0700300 return &features_classic[index];
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700301}
302
Zach Johnson30e58062014-09-26 21:14:34 -0700303static uint8_t get_last_features_classic_index(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800304 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700305 return last_features_classic_page_index;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700306}
307
Myles Watson4451b3b2016-11-09 10:20:44 -0800308static uint8_t* get_local_supported_codecs(uint8_t* number_of_codecs) {
Jack Hef2af1c42016-12-13 01:59:12 -0800309 CHECK(readable);
Myles Watson4451b3b2016-11-09 10:20:44 -0800310 if (number_of_local_supported_codecs) {
Srinu Jellaf6dcf792015-07-22 17:30:32 +0530311 *number_of_codecs = number_of_local_supported_codecs;
312 return local_supported_codecs;
313 }
314 return NULL;
315}
316
Myles Watson4451b3b2016-11-09 10:20:44 -0800317static const bt_device_features_t* get_features_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800318 CHECK(readable);
319 CHECK(ble_supported);
Zach Johnson30e58062014-09-26 21:14:34 -0700320 return &features_ble;
321}
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700322
Myles Watson4451b3b2016-11-09 10:20:44 -0800323static const uint8_t* get_ble_supported_states(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800324 CHECK(readable);
325 CHECK(ble_supported);
Zach Johnsona87c8212014-10-28 17:59:13 -0700326 return ble_supported_states;
327}
328
Zach Johnson30e58062014-09-26 21:14:34 -0700329static bool supports_simple_pairing(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800330 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700331 return simple_pairing_supported;
332}
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700333
Satya Calloji444a8da2015-03-06 10:38:22 -0800334static bool supports_secure_connections(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800335 CHECK(readable);
Satya Calloji444a8da2015-03-06 10:38:22 -0800336 return secure_connections_supported;
337}
338
Zach Johnson30e58062014-09-26 21:14:34 -0700339static bool supports_simultaneous_le_bredr(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800340 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700341 return HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array);
342}
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700343
Zach Johnson30e58062014-09-26 21:14:34 -0700344static bool supports_reading_remote_extended_features(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800345 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700346 return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(supported_commands);
347}
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700348
Zach Johnson30e58062014-09-26 21:14:34 -0700349static bool supports_interlaced_inquiry_scan(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800350 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700351 return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(features_classic[0].as_array);
352}
353
354static bool supports_rssi_with_inquiry_results(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800355 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700356 return HCI_LMP_INQ_RSSI_SUPPORTED(features_classic[0].as_array);
357}
358
359static bool supports_extended_inquiry_response(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800360 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700361 return HCI_EXT_INQ_RSP_SUPPORTED(features_classic[0].as_array);
362}
363
364static bool supports_master_slave_role_switch(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800365 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700366 return HCI_SWITCH_SUPPORTED(features_classic[0].as_array);
367}
368
Mudumba Ananth181863e2017-02-09 09:05:48 -0800369static bool supports_enhanced_setup_synchronous_connection(void) {
370 assert(readable);
371 return HCI_ENH_SETUP_SYNCH_CONN_SUPPORTED(supported_commands);
372}
373
374static bool supports_enhanced_accept_synchronous_connection(void) {
375 assert(readable);
376 return HCI_ENH_ACCEPT_SYNCH_CONN_SUPPORTED(supported_commands);
377}
378
Zach Johnson30e58062014-09-26 21:14:34 -0700379static bool supports_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800380 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700381 return ble_supported;
382}
383
Satya Calloji444a8da2015-03-06 10:38:22 -0800384static bool supports_ble_privacy(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800385 CHECK(readable);
386 CHECK(ble_supported);
Satya Calloji444a8da2015-03-06 10:38:22 -0800387 return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array);
388}
389
Priti Aghera636d6712014-12-18 13:55:48 -0800390static bool supports_ble_packet_extension(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800391 CHECK(readable);
392 CHECK(ble_supported);
Priti Aghera636d6712014-12-18 13:55:48 -0800393 return HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array);
394}
395
Zach Johnson30e58062014-09-26 21:14:34 -0700396static bool supports_ble_connection_parameters_request(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800397 CHECK(readable);
398 CHECK(ble_supported);
Zach Johnson30e58062014-09-26 21:14:34 -0700399 return HCI_LE_CONN_PARAM_REQ_SUPPORTED(features_ble.as_array);
400}
401
Jakub Pawlowski4b1feb62017-03-09 18:11:43 -0800402static bool supports_ble_2m_phy(void) {
403 CHECK(readable);
404 CHECK(ble_supported);
405 return HCI_LE_2M_PHY_SUPPORTED(features_ble.as_array);
406}
407
408static bool supports_ble_coded_phy(void) {
409 CHECK(readable);
410 CHECK(ble_supported);
411 return HCI_LE_CODED_PHY_SUPPORTED(features_ble.as_array);
412}
413
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800414static bool supports_ble_extended_advertising(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800415 CHECK(readable);
416 CHECK(ble_supported);
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800417 return HCI_LE_EXTENDED_ADVERTISING_SUPPORTED(features_ble.as_array);
418}
419
420static bool supports_ble_periodic_advertising(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800421 CHECK(readable);
422 CHECK(ble_supported);
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800423 return HCI_LE_PERIODIC_ADVERTISING_SUPPORTED(features_ble.as_array);
424}
425
Srinu Jella039b25b2015-08-24 21:00:28 +0530426static bool supports_ble_offload_features(void) {
427 assert(readable);
428 assert(ble_supported);
429 return ble_offload_features_supported;
430}
431
Zach Johnson30e58062014-09-26 21:14:34 -0700432static uint16_t get_acl_data_size_classic(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800433 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700434 return acl_data_size_classic;
435}
436
437static uint16_t get_acl_data_size_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800438 CHECK(readable);
439 CHECK(ble_supported);
Zach Johnson30e58062014-09-26 21:14:34 -0700440 return acl_data_size_ble;
441}
442
443static uint16_t get_acl_packet_size_classic(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800444 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700445 return acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE;
446}
447
448static uint16_t get_acl_packet_size_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800449 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700450 return acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE;
451}
452
Priti Aghera636d6712014-12-18 13:55:48 -0800453static uint16_t get_ble_suggested_default_data_length(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800454 CHECK(readable);
455 CHECK(ble_supported);
Priti Aghera636d6712014-12-18 13:55:48 -0800456 return ble_suggested_default_data_length;
457}
458
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800459static uint16_t get_ble_maxium_advertising_data_length(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800460 CHECK(readable);
461 CHECK(ble_supported);
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800462 return ble_maxium_advertising_data_length;
463}
464
465static uint8_t get_ble_number_of_supported_advertising_sets(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800466 CHECK(readable);
467 CHECK(ble_supported);
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800468 return ble_number_of_supported_advertising_sets;
469}
470
Zach Johnson30e58062014-09-26 21:14:34 -0700471static uint16_t get_acl_buffer_count_classic(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800472 CHECK(readable);
Zach Johnson30e58062014-09-26 21:14:34 -0700473 return acl_buffer_count_classic;
474}
475
476static uint8_t get_acl_buffer_count_ble(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800477 CHECK(readable);
478 CHECK(ble_supported);
Zach Johnson30e58062014-09-26 21:14:34 -0700479 return acl_buffer_count_ble;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700480}
481
Priti Agherab8b3e802015-04-17 18:11:24 -0700482static uint8_t get_ble_white_list_size(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800483 CHECK(readable);
484 CHECK(ble_supported);
Priti Agherab8b3e802015-04-17 18:11:24 -0700485 return ble_white_list_size;
486}
487
Satya Calloji444a8da2015-03-06 10:38:22 -0800488static uint8_t get_ble_resolving_list_max_size(void) {
Jack Hef2af1c42016-12-13 01:59:12 -0800489 CHECK(readable);
490 CHECK(ble_supported);
Satya Calloji444a8da2015-03-06 10:38:22 -0800491 return ble_resolving_list_max_size;
492}
493
494static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
Pavlin Radoslavov58318f42016-05-05 16:20:15 -0700495 // Setting "resolving_list_max_size" to 0 is done during cleanup,
496 // hence we ignore the "readable" flag already set to false during shutdown.
497 if (resolving_list_max_size != 0) {
Jack Hef2af1c42016-12-13 01:59:12 -0800498 CHECK(readable);
Pavlin Radoslavov58318f42016-05-05 16:20:15 -0700499 }
Jack Hef2af1c42016-12-13 01:59:12 -0800500 CHECK(ble_supported);
Satya Calloji444a8da2015-03-06 10:38:22 -0800501 ble_resolving_list_max_size = resolving_list_max_size;
502}
503
Jakub Pawlowski96fb2732017-03-24 17:52:02 -0700504static uint8_t get_le_all_initiating_phys() {
505 uint8_t phy = PHY_LE_1M;
506 // TODO(jpawlowski): uncomment after next FW udpate
507 // if (supports_ble_2m_phy()) phy |= PHY_LE_2M;
508 // if (supports_ble_coded_phy()) phy |= PHY_LE_CODED;
509 return phy;
510}
511
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700512static const controller_t interface = {
Myles Watson4451b3b2016-11-09 10:20:44 -0800513 get_is_ready,
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700514
Myles Watson4451b3b2016-11-09 10:20:44 -0800515 get_address,
516 get_bt_version,
Zach Johnson30e58062014-09-26 21:14:34 -0700517
Myles Watson4451b3b2016-11-09 10:20:44 -0800518 get_features_classic,
519 get_last_features_classic_index,
Zach Johnson30e58062014-09-26 21:14:34 -0700520
Myles Watson4451b3b2016-11-09 10:20:44 -0800521 get_features_ble,
522 get_ble_supported_states,
Zach Johnson30e58062014-09-26 21:14:34 -0700523
Myles Watson4451b3b2016-11-09 10:20:44 -0800524 supports_simple_pairing,
525 supports_secure_connections,
526 supports_simultaneous_le_bredr,
527 supports_reading_remote_extended_features,
528 supports_interlaced_inquiry_scan,
529 supports_rssi_with_inquiry_results,
530 supports_extended_inquiry_response,
531 supports_master_slave_role_switch,
Mudumba Ananth181863e2017-02-09 09:05:48 -0800532 supports_enhanced_setup_synchronous_connection,
533 supports_enhanced_accept_synchronous_connection,
Zach Johnson30e58062014-09-26 21:14:34 -0700534
Myles Watson4451b3b2016-11-09 10:20:44 -0800535 supports_ble,
536 supports_ble_packet_extension,
537 supports_ble_connection_parameters_request,
538 supports_ble_privacy,
Jakub Pawlowski4b1feb62017-03-09 18:11:43 -0800539 supports_ble_2m_phy,
540 supports_ble_coded_phy,
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800541 supports_ble_extended_advertising,
542 supports_ble_periodic_advertising,
Zach Johnson30e58062014-09-26 21:14:34 -0700543
Myles Watson4451b3b2016-11-09 10:20:44 -0800544 get_acl_data_size_classic,
545 get_acl_data_size_ble,
Zach Johnson30e58062014-09-26 21:14:34 -0700546
Myles Watson4451b3b2016-11-09 10:20:44 -0800547 get_acl_packet_size_classic,
548 get_acl_packet_size_ble,
549 get_ble_suggested_default_data_length,
Jakub Pawlowski1f4cc6d2016-11-28 11:16:04 -0800550 get_ble_maxium_advertising_data_length,
551 get_ble_number_of_supported_advertising_sets,
Zach Johnson30e58062014-09-26 21:14:34 -0700552
Myles Watson4451b3b2016-11-09 10:20:44 -0800553 get_acl_buffer_count_classic,
554 get_acl_buffer_count_ble,
Satya Calloji444a8da2015-03-06 10:38:22 -0800555
Myles Watson4451b3b2016-11-09 10:20:44 -0800556 get_ble_white_list_size,
Priti Agherab8b3e802015-04-17 18:11:24 -0700557
Myles Watson4451b3b2016-11-09 10:20:44 -0800558 get_ble_resolving_list_max_size,
559 set_ble_resolving_list_max_size,
Jakub Pawlowski96fb2732017-03-24 17:52:02 -0700560 get_local_supported_codecs,
Srinu Jella039b25b2015-08-24 21:00:28 +0530561 supports_ble_offload_features,
Jakub Pawlowski96fb2732017-03-24 17:52:02 -0700562 get_le_all_initiating_phys};
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700563
Myles Watson4451b3b2016-11-09 10:20:44 -0800564const controller_t* controller_get_interface() {
Zach Johnson30e58062014-09-26 21:14:34 -0700565 static bool loaded = false;
566 if (!loaded) {
567 loaded = true;
568
569 hci = hci_layer_get_interface();
570 packet_factory = hci_packet_factory_get_interface();
571 packet_parser = hci_packet_parser_get_interface();
572 }
573
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700574 return &interface;
575}
576
Myles Watson4451b3b2016-11-09 10:20:44 -0800577const controller_t* controller_get_test_interface(
578 const hci_t* hci_interface,
579 const hci_packet_factory_t* packet_factory_interface,
580 const hci_packet_parser_t* packet_parser_interface) {
Zach Johnson30e58062014-09-26 21:14:34 -0700581 hci = hci_interface;
Zach Johnsonbf8193b2014-09-08 09:56:35 -0700582 packet_factory = packet_factory_interface;
583 packet_parser = packet_parser_interface;
584 return &interface;
585}