OSDN Git Service

02a3998a8d88d4ace8319c8edf0214239004dad0
[android-x86/system-bt.git] / device / src / controller.cc
1 /******************************************************************************
2  *
3  *  Copyright 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
19 #define LOG_TAG "bt_controller"
20
21 #include "device/include/controller.h"
22
23 #include <base/logging.h>
24
25 #include "bt_types.h"
26 #include "btcore/include/event_mask.h"
27 #include "btcore/include/module.h"
28 #include "btcore/include/version.h"
29 #include "hcimsgs.h"
30 #include "osi/include/future.h"
31 #include "stack/include/btm_ble_api.h"
32
33 const bt_event_mask_t BLE_EVENT_MASK = {
34     {0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x1E, 0x7f}};
35
36 const bt_event_mask_t CLASSIC_EVENT_MASK = {HCI_DUMO_EVENT_MASK_EXT};
37
38 // TODO(zachoverflow): factor out into common module
39 const uint8_t SCO_HOST_BUFFER_SIZE = 0xff;
40
41 #define HCI_SUPPORTED_COMMANDS_ARRAY_SIZE 64
42 #define MAX_FEATURES_CLASSIC_PAGE_COUNT 3
43 #define BLE_SUPPORTED_STATES_SIZE 8
44 #define BLE_SUPPORTED_FEATURES_SIZE 8
45 #define MAX_LOCAL_SUPPORTED_CODECS_SIZE 8
46
47 static const hci_t* hci;
48 static const hci_packet_factory_t* packet_factory;
49 static const hci_packet_parser_t* packet_parser;
50
51 static RawAddress address;
52 static bt_version_t bt_version;
53
54 static uint8_t supported_commands[HCI_SUPPORTED_COMMANDS_ARRAY_SIZE];
55 static bt_device_features_t features_classic[MAX_FEATURES_CLASSIC_PAGE_COUNT];
56 static uint8_t last_features_classic_page_index;
57
58 static uint16_t acl_data_size_classic;
59 static uint16_t acl_data_size_ble;
60 static uint16_t acl_buffer_count_classic;
61 static uint8_t acl_buffer_count_ble;
62
63 static uint8_t ble_white_list_size;
64 static uint8_t ble_resolving_list_max_size;
65 static uint8_t ble_supported_states[BLE_SUPPORTED_STATES_SIZE];
66 static bt_device_features_t features_ble;
67 static uint16_t ble_suggested_default_data_length;
68 static uint16_t ble_supported_max_tx_octets;
69 static uint16_t ble_supported_max_tx_time;
70 static uint16_t ble_supported_max_rx_octets;
71 static uint16_t ble_supported_max_rx_time;
72
73 static uint16_t ble_maxium_advertising_data_length;
74 static uint8_t ble_number_of_supported_advertising_sets;
75 static uint8_t local_supported_codecs[MAX_LOCAL_SUPPORTED_CODECS_SIZE];
76 static uint8_t number_of_local_supported_codecs = 0;
77
78 static bool readable;
79 static bool ble_supported;
80 static bool simple_pairing_supported;
81 static bool secure_connections_supported;
82
83 #define AWAIT_COMMAND(command) \
84   static_cast<BT_HDR*>(future_await(hci->transmit_command_futured(command)))
85
86 // Module lifecycle functions
87
88 static future_t* start_up(void) {
89   BT_HDR* response;
90
91   // Send the initial reset command
92   response = AWAIT_COMMAND(packet_factory->make_reset());
93   packet_parser->parse_generic_command_complete(response);
94
95   // Request the classic buffer size next
96   response = AWAIT_COMMAND(packet_factory->make_read_buffer_size());
97   packet_parser->parse_read_buffer_size_response(
98       response, &acl_data_size_classic, &acl_buffer_count_classic);
99
100   // Tell the controller about our buffer sizes and buffer counts next
101   // TODO(zachoverflow): factor this out. eww l2cap contamination. And why just
102   // a hardcoded 10?
103   response = AWAIT_COMMAND(packet_factory->make_host_buffer_size(
104       L2CAP_MTU_SIZE, SCO_HOST_BUFFER_SIZE, L2CAP_HOST_FC_ACL_BUFS, 10));
105
106   packet_parser->parse_generic_command_complete(response);
107
108   // Read the local version info off the controller next, including
109   // information such as manufacturer and supported HCI version
110   response = AWAIT_COMMAND(packet_factory->make_read_local_version_info());
111   packet_parser->parse_read_local_version_info_response(response, &bt_version);
112
113   // Read the bluetooth address off the controller next
114   response = AWAIT_COMMAND(packet_factory->make_read_bd_addr());
115   packet_parser->parse_read_bd_addr_response(response, &address);
116
117   // Request the controller's supported commands next
118   response =
119       AWAIT_COMMAND(packet_factory->make_read_local_supported_commands());
120   packet_parser->parse_read_local_supported_commands_response(
121       response, supported_commands, HCI_SUPPORTED_COMMANDS_ARRAY_SIZE);
122
123   // Read page 0 of the controller features next
124   uint8_t page_number = 0;
125   response = AWAIT_COMMAND(
126       packet_factory->make_read_local_extended_features(page_number));
127   packet_parser->parse_read_local_extended_features_response(
128       response, &page_number, &last_features_classic_page_index,
129       features_classic, MAX_FEATURES_CLASSIC_PAGE_COUNT);
130
131   CHECK(page_number == 0);
132   page_number++;
133
134   // Inform the controller what page 0 features we support, based on what
135   // it told us it supports. We need to do this first before we request the
136   // next page, because the controller's response for page 1 may be
137   // dependent on what we configure from page 0
138   simple_pairing_supported =
139       HCI_SIMPLE_PAIRING_SUPPORTED(features_classic[0].as_array);
140   if (simple_pairing_supported) {
141     response = AWAIT_COMMAND(
142         packet_factory->make_write_simple_pairing_mode(HCI_SP_MODE_ENABLED));
143     packet_parser->parse_generic_command_complete(response);
144   }
145
146   if (HCI_LE_SPT_SUPPORTED(features_classic[0].as_array)) {
147     uint8_t simultaneous_le_host =
148         HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array)
149             ? BTM_BLE_SIMULTANEOUS_HOST
150             : 0;
151     response = AWAIT_COMMAND(packet_factory->make_ble_write_host_support(
152         BTM_BLE_HOST_SUPPORT, simultaneous_le_host));
153
154     packet_parser->parse_generic_command_complete(response);
155
156     // If we modified the BT_HOST_SUPPORT, we will need ext. feat. page 1
157     if (last_features_classic_page_index < 1)
158       last_features_classic_page_index = 1;
159   }
160
161   // Done telling the controller about what page 0 features we support
162   // Request the remaining feature pages
163   while (page_number <= last_features_classic_page_index &&
164          page_number < MAX_FEATURES_CLASSIC_PAGE_COUNT) {
165     response = AWAIT_COMMAND(
166         packet_factory->make_read_local_extended_features(page_number));
167     packet_parser->parse_read_local_extended_features_response(
168         response, &page_number, &last_features_classic_page_index,
169         features_classic, MAX_FEATURES_CLASSIC_PAGE_COUNT);
170
171     page_number++;
172   }
173
174 #if (SC_MODE_INCLUDED == TRUE)
175   secure_connections_supported =
176       HCI_SC_CTRLR_SUPPORTED(features_classic[2].as_array);
177   if (secure_connections_supported) {
178     response = AWAIT_COMMAND(
179         packet_factory->make_write_secure_connections_host_support(
180             HCI_SC_MODE_ENABLED));
181     packet_parser->parse_generic_command_complete(response);
182   }
183 #endif
184
185   ble_supported = last_features_classic_page_index >= 1 &&
186                   HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
187   if (ble_supported) {
188     // Request the ble white list size next
189     response = AWAIT_COMMAND(packet_factory->make_ble_read_white_list_size());
190     packet_parser->parse_ble_read_white_list_size_response(
191         response, &ble_white_list_size);
192
193     // Request the ble buffer size next
194     response = AWAIT_COMMAND(packet_factory->make_ble_read_buffer_size());
195     packet_parser->parse_ble_read_buffer_size_response(
196         response, &acl_data_size_ble, &acl_buffer_count_ble);
197
198     // Response of 0 indicates ble has the same buffer size as classic
199     if (acl_data_size_ble == 0) acl_data_size_ble = acl_data_size_classic;
200
201     // Request the ble supported states next
202     response = AWAIT_COMMAND(packet_factory->make_ble_read_supported_states());
203     packet_parser->parse_ble_read_supported_states_response(
204         response, ble_supported_states, sizeof(ble_supported_states));
205
206     // Request the ble supported features next
207     response =
208         AWAIT_COMMAND(packet_factory->make_ble_read_local_supported_features());
209     packet_parser->parse_ble_read_local_supported_features_response(
210         response, &features_ble);
211
212     if (HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array)) {
213       response =
214           AWAIT_COMMAND(packet_factory->make_ble_read_resolving_list_size());
215       packet_parser->parse_ble_read_resolving_list_size_response(
216           response, &ble_resolving_list_max_size);
217     }
218
219     if (HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array)) {
220       response =
221           AWAIT_COMMAND(packet_factory->make_ble_read_maximum_data_length());
222       packet_parser->parse_ble_read_maximum_data_length_response(
223           response, &ble_supported_max_tx_octets, &ble_supported_max_tx_time,
224           &ble_supported_max_rx_octets, &ble_supported_max_rx_time);
225
226       response = AWAIT_COMMAND(
227           packet_factory->make_ble_read_suggested_default_data_length());
228       packet_parser->parse_ble_read_suggested_default_data_length_response(
229           response, &ble_suggested_default_data_length);
230     }
231
232     if (HCI_LE_EXTENDED_ADVERTISING_SUPPORTED(features_ble.as_array)) {
233       response = AWAIT_COMMAND(
234           packet_factory->make_ble_read_maximum_advertising_data_length());
235       packet_parser->parse_ble_read_maximum_advertising_data_length(
236           response, &ble_maxium_advertising_data_length);
237
238       response = AWAIT_COMMAND(
239           packet_factory->make_ble_read_number_of_supported_advertising_sets());
240       packet_parser->parse_ble_read_number_of_supported_advertising_sets(
241           response, &ble_number_of_supported_advertising_sets);
242     } else {
243       /* If LE Excended Advertising is not supported, use the default value */
244       ble_maxium_advertising_data_length = 31;
245     }
246
247     // Set the ble event mask next
248     response =
249         AWAIT_COMMAND(packet_factory->make_ble_set_event_mask(&BLE_EVENT_MASK));
250     packet_parser->parse_generic_command_complete(response);
251   }
252
253   if (simple_pairing_supported) {
254     response =
255         AWAIT_COMMAND(packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK));
256     packet_parser->parse_generic_command_complete(response);
257   }
258
259   // read local supported codecs
260   if (HCI_READ_LOCAL_CODECS_SUPPORTED(supported_commands)) {
261     response =
262         AWAIT_COMMAND(packet_factory->make_read_local_supported_codecs());
263     packet_parser->parse_read_local_supported_codecs_response(
264         response, &number_of_local_supported_codecs, local_supported_codecs);
265   }
266
267   if (!HCI_READ_ENCR_KEY_SIZE_SUPPORTED(supported_commands)) {
268     LOG(FATAL) << " Controller must support Read Encryption Key Size command";
269   }
270
271   readable = true;
272   return future_new_immediate(FUTURE_SUCCESS);
273 }
274
275 static future_t* shut_down(void) {
276   readable = false;
277   return future_new_immediate(FUTURE_SUCCESS);
278 }
279
280 EXPORT_SYMBOL extern const module_t controller_module = {
281     .name = CONTROLLER_MODULE,
282     .init = NULL,
283     .start_up = start_up,
284     .shut_down = shut_down,
285     .clean_up = NULL,
286     .dependencies = {HCI_MODULE, NULL}};
287
288 // Interface functions
289
290 static bool get_is_ready(void) { return readable; }
291
292 static const RawAddress* get_address(void) {
293   CHECK(readable);
294   return &address;
295 }
296
297 static const bt_version_t* get_bt_version(void) {
298   CHECK(readable);
299   return &bt_version;
300 }
301
302 // TODO(zachoverflow): hide inside, move decoder inside too
303 static const bt_device_features_t* get_features_classic(int index) {
304   CHECK(readable);
305   CHECK(index < MAX_FEATURES_CLASSIC_PAGE_COUNT);
306   return &features_classic[index];
307 }
308
309 static uint8_t get_last_features_classic_index(void) {
310   CHECK(readable);
311   return last_features_classic_page_index;
312 }
313
314 static uint8_t* get_local_supported_codecs(uint8_t* number_of_codecs) {
315   CHECK(readable);
316   if (number_of_local_supported_codecs) {
317     *number_of_codecs = number_of_local_supported_codecs;
318     return local_supported_codecs;
319   }
320   return NULL;
321 }
322
323 static const bt_device_features_t* get_features_ble(void) {
324   CHECK(readable);
325   CHECK(ble_supported);
326   return &features_ble;
327 }
328
329 static const uint8_t* get_ble_supported_states(void) {
330   CHECK(readable);
331   CHECK(ble_supported);
332   return ble_supported_states;
333 }
334
335 static bool supports_simple_pairing(void) {
336   CHECK(readable);
337   return simple_pairing_supported;
338 }
339
340 static bool supports_secure_connections(void) {
341   CHECK(readable);
342   return secure_connections_supported;
343 }
344
345 static bool supports_simultaneous_le_bredr(void) {
346   CHECK(readable);
347   return HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array);
348 }
349
350 static bool supports_reading_remote_extended_features(void) {
351   CHECK(readable);
352   return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(supported_commands);
353 }
354
355 static bool supports_interlaced_inquiry_scan(void) {
356   CHECK(readable);
357   return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(features_classic[0].as_array);
358 }
359
360 static bool supports_rssi_with_inquiry_results(void) {
361   CHECK(readable);
362   return HCI_LMP_INQ_RSSI_SUPPORTED(features_classic[0].as_array);
363 }
364
365 static bool supports_extended_inquiry_response(void) {
366   CHECK(readable);
367   return HCI_EXT_INQ_RSP_SUPPORTED(features_classic[0].as_array);
368 }
369
370 static bool supports_master_slave_role_switch(void) {
371   CHECK(readable);
372   return HCI_SWITCH_SUPPORTED(features_classic[0].as_array);
373 }
374
375 static bool supports_enhanced_setup_synchronous_connection(void) {
376   assert(readable);
377   return HCI_ENH_SETUP_SYNCH_CONN_SUPPORTED(supported_commands);
378 }
379
380 static bool supports_enhanced_accept_synchronous_connection(void) {
381   assert(readable);
382   return HCI_ENH_ACCEPT_SYNCH_CONN_SUPPORTED(supported_commands);
383 }
384
385 static bool supports_ble(void) {
386   CHECK(readable);
387   return ble_supported;
388 }
389
390 static bool supports_ble_privacy(void) {
391   CHECK(readable);
392   CHECK(ble_supported);
393   return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array);
394 }
395
396 static bool supports_ble_set_privacy_mode() {
397   CHECK(readable);
398   CHECK(ble_supported);
399   return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array) &&
400          HCI_LE_SET_PRIVACY_MODE_SUPPORTED(supported_commands);
401 }
402
403 static bool supports_ble_packet_extension(void) {
404   CHECK(readable);
405   CHECK(ble_supported);
406   return HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array);
407 }
408
409 static bool supports_ble_connection_parameters_request(void) {
410   CHECK(readable);
411   CHECK(ble_supported);
412   return HCI_LE_CONN_PARAM_REQ_SUPPORTED(features_ble.as_array);
413 }
414
415 static bool supports_ble_2m_phy(void) {
416   CHECK(readable);
417   CHECK(ble_supported);
418   return HCI_LE_2M_PHY_SUPPORTED(features_ble.as_array);
419 }
420
421 static bool supports_ble_coded_phy(void) {
422   CHECK(readable);
423   CHECK(ble_supported);
424   return HCI_LE_CODED_PHY_SUPPORTED(features_ble.as_array);
425 }
426
427 static bool supports_ble_extended_advertising(void) {
428   CHECK(readable);
429   CHECK(ble_supported);
430   return HCI_LE_EXTENDED_ADVERTISING_SUPPORTED(features_ble.as_array);
431 }
432
433 static bool supports_ble_periodic_advertising(void) {
434   CHECK(readable);
435   CHECK(ble_supported);
436   return HCI_LE_PERIODIC_ADVERTISING_SUPPORTED(features_ble.as_array);
437 }
438
439 static uint16_t get_acl_data_size_classic(void) {
440   CHECK(readable);
441   return acl_data_size_classic;
442 }
443
444 static uint16_t get_acl_data_size_ble(void) {
445   CHECK(readable);
446   CHECK(ble_supported);
447   return acl_data_size_ble;
448 }
449
450 static uint16_t get_acl_packet_size_classic(void) {
451   CHECK(readable);
452   return acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE;
453 }
454
455 static uint16_t get_acl_packet_size_ble(void) {
456   CHECK(readable);
457   return acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE;
458 }
459
460 static uint16_t get_ble_suggested_default_data_length(void) {
461   CHECK(readable);
462   CHECK(ble_supported);
463   return ble_suggested_default_data_length;
464 }
465
466 static uint16_t get_ble_maximum_tx_data_length(void) {
467   CHECK(readable);
468   CHECK(ble_supported);
469   return ble_supported_max_tx_octets;
470 }
471
472 static uint16_t get_ble_maxium_advertising_data_length(void) {
473   CHECK(readable);
474   CHECK(ble_supported);
475   return ble_maxium_advertising_data_length;
476 }
477
478 static uint8_t get_ble_number_of_supported_advertising_sets(void) {
479   CHECK(readable);
480   CHECK(ble_supported);
481   return ble_number_of_supported_advertising_sets;
482 }
483
484 static uint16_t get_acl_buffer_count_classic(void) {
485   CHECK(readable);
486   return acl_buffer_count_classic;
487 }
488
489 static uint8_t get_acl_buffer_count_ble(void) {
490   CHECK(readable);
491   CHECK(ble_supported);
492   return acl_buffer_count_ble;
493 }
494
495 static uint8_t get_ble_white_list_size(void) {
496   CHECK(readable);
497   CHECK(ble_supported);
498   return ble_white_list_size;
499 }
500
501 static uint8_t get_ble_resolving_list_max_size(void) {
502   CHECK(readable);
503   CHECK(ble_supported);
504   return ble_resolving_list_max_size;
505 }
506
507 static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
508   // Setting "resolving_list_max_size" to 0 is done during cleanup,
509   // hence we ignore the "readable" flag already set to false during shutdown.
510   if (resolving_list_max_size != 0) {
511     CHECK(readable);
512   }
513   CHECK(ble_supported);
514   ble_resolving_list_max_size = resolving_list_max_size;
515 }
516
517 static uint8_t get_le_all_initiating_phys() {
518   uint8_t phy = PHY_LE_1M;
519   // TODO(jpawlowski): uncomment after next FW udpate
520   // if (supports_ble_2m_phy()) phy |= PHY_LE_2M;
521   // if (supports_ble_coded_phy()) phy |= PHY_LE_CODED;
522   return phy;
523 }
524
525 static const controller_t interface = {
526     get_is_ready,
527
528     get_address,
529     get_bt_version,
530
531     get_features_classic,
532     get_last_features_classic_index,
533
534     get_features_ble,
535     get_ble_supported_states,
536
537     supports_simple_pairing,
538     supports_secure_connections,
539     supports_simultaneous_le_bredr,
540     supports_reading_remote_extended_features,
541     supports_interlaced_inquiry_scan,
542     supports_rssi_with_inquiry_results,
543     supports_extended_inquiry_response,
544     supports_master_slave_role_switch,
545     supports_enhanced_setup_synchronous_connection,
546     supports_enhanced_accept_synchronous_connection,
547
548     supports_ble,
549     supports_ble_packet_extension,
550     supports_ble_connection_parameters_request,
551     supports_ble_privacy,
552     supports_ble_set_privacy_mode,
553     supports_ble_2m_phy,
554     supports_ble_coded_phy,
555     supports_ble_extended_advertising,
556     supports_ble_periodic_advertising,
557
558     get_acl_data_size_classic,
559     get_acl_data_size_ble,
560
561     get_acl_packet_size_classic,
562     get_acl_packet_size_ble,
563     get_ble_suggested_default_data_length,
564     get_ble_maximum_tx_data_length,
565     get_ble_maxium_advertising_data_length,
566     get_ble_number_of_supported_advertising_sets,
567
568     get_acl_buffer_count_classic,
569     get_acl_buffer_count_ble,
570
571     get_ble_white_list_size,
572
573     get_ble_resolving_list_max_size,
574     set_ble_resolving_list_max_size,
575     get_local_supported_codecs,
576     get_le_all_initiating_phys};
577
578 const controller_t* controller_get_interface() {
579   static bool loaded = false;
580   if (!loaded) {
581     loaded = true;
582
583     hci = hci_layer_get_interface();
584     packet_factory = hci_packet_factory_get_interface();
585     packet_parser = hci_packet_parser_get_interface();
586   }
587
588   return &interface;
589 }
590
591 const controller_t* controller_get_test_interface(
592     const hci_t* hci_interface,
593     const hci_packet_factory_t* packet_factory_interface,
594     const hci_packet_parser_t* packet_parser_interface) {
595   hci = hci_interface;
596   packet_factory = packet_factory_interface;
597   packet_parser = packet_parser_interface;
598   return &interface;
599 }