1 /* 2 * Copyright 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <array> 20 #include <cstdint> 21 #include <optional> 22 #include <string> 23 #include <vector> 24 25 #include "config.pb.h" 26 #include "hci/address.h" 27 #include "hci/hci_packets.h" 28 29 namespace rootcanal { 30 using bluetooth::hci::HciVersion; 31 using bluetooth::hci::LmpVersion; 32 33 // Local controller quirks. 34 struct ControllerQuirks { 35 // The specification states that the Random Address is invalid until 36 // explicitly set by the command LE Set Random Address. Certain HCI commands 37 // check for this condition. 38 // 39 // This quirk configures a default value for the LE random address in order 40 // to bypass this validation. The default random address will 41 // be ba:db:ad:ba:db:ad. 42 bool has_default_random_address{false}; 43 44 // This quirks configures the controller to send an Hardware Error event 45 // in case a command is received before the HCI Reset command. 46 // 47 // Receiving a different command is indicative of the emulator being 48 // started from a snapshot. In this case the controller state is lost 49 // but the Host stack is loaded post-initialization. This quirk 50 // ensures that the stack will reset itself after reloading. 51 bool hardware_error_before_reset{false}; 52 }; 53 54 // Local controller information. 55 // 56 // Provide the Informational Parameters returned by HCI commands 57 // in the range of the same name (cf. [4] E.7.4). 58 // The informational parameters are fixed by the manufacturer of the Bluetooth 59 // hardware. These parameters provide information about the BR/EDR Controller 60 // and the capabilities of the Link Manager and Baseband in the BR/EDR 61 // Controller. The Host device cannot modify any of these parameters. 62 struct ControllerProperties { 63 public: 64 ControllerProperties(); 65 ControllerProperties(rootcanal::configuration::Controller const&); 66 ControllerProperties(ControllerProperties const&) = default; 67 ControllerProperties(ControllerProperties&&) = default; 68 ~ControllerProperties() = default; 69 70 // Perform a bitwise and operation on the supported commands mask; 71 // the default bit setting is either loaded from the configuration 72 // file or all 1s. 73 void SetSupportedCommands(std::array<uint8_t, 64> supported_commands); 74 75 // Check if the feature masks are valid according to the specification. 76 bool CheckSupportedFeatures() const; 77 78 // Check if the supported command mask is valid according to the 79 // specification. If fixup is true, then the mask is updated instead of 80 // returning an error. 81 bool CheckSupportedCommands() const; 82 83 // Enabled quirks. 84 ControllerQuirks quirks{}; 85 86 // Local Version Information (Vol 4, Part E § 7.4.1). 87 HciVersion hci_version{HciVersion::V_5_3}; 88 LmpVersion lmp_version{LmpVersion::V_5_3}; 89 uint16_t hci_subversion{0}; 90 uint16_t lmp_subversion{0}; 91 uint16_t company_identifier{0x00E0}; // Google 92 93 // Transports. 94 bool br_supported{true}; 95 bool le_supported{true}; 96 97 // Local Supported Commands (Vol 4, Part E § 7.4.2). 98 std::array<uint8_t, 64> supported_commands{}; 99 100 // Vendor Supported Commands. 101 bool supports_le_get_vendor_capabilities_command{true}; 102 103 // Local Supported Features (Vol 4, Part E § 7.4.3) and 104 // Local Extended Features (Vol 4, Part E § 7.4.3). 105 std::array<uint64_t, 3> lmp_features{}; 106 107 // LE Local Supported Features (Vol 4, Part E § 7.8.3). 108 uint64_t le_features{0}; 109 110 // Buffer Size (Vol 4, Part E § 7.4.5). 111 uint16_t acl_data_packet_length{1024}; 112 uint8_t sco_data_packet_length{255}; 113 uint16_t total_num_acl_data_packets{10}; 114 uint16_t total_num_sco_data_packets{10}; 115 116 // LE Buffer Size (Vol 4, Part E § 7.8.2). 117 uint16_t le_acl_data_packet_length{27}; 118 uint16_t iso_data_packet_length{1021}; 119 uint8_t total_num_le_acl_data_packets{20}; 120 uint8_t total_num_iso_data_packets{12}; 121 122 // Number of Supported IAC (Vol 4, Part E § 7.3.43). 123 uint8_t num_supported_iac{4}; 124 125 // LE Advertising Physical Channel TX Power (Vol 4, Part E § 7.8.6). 126 uint8_t le_advertising_physical_channel_tx_power{static_cast<uint8_t>(-10)}; 127 128 // Supported Codecs (Vol 4, Part E § 7.4.8). 129 // Implements the [v1] version only. 130 std::vector<uint8_t> supported_standard_codecs{0}; 131 std::vector<uint32_t> supported_vendor_specific_codecs{}; 132 133 // LE Filter Accept List Size (Vol 4, Part E § 7.8.14). 134 uint8_t le_filter_accept_list_size{16}; 135 136 // LE Resolving List Size (Vol 4, Part E § 7.8.41). 137 uint8_t le_resolving_list_size{16}; 138 139 // LE Supported States (Vol 4, Part E § 7.8.27). 140 uint64_t le_supported_states{0x3ffffffffff}; 141 142 // LE Maximum Advertising Data Length (Vol 4, Part E § 7.8.57). 143 // Note: valid range 0x001F to 0x0672. 144 uint16_t le_max_advertising_data_length{512}; 145 146 // LE Number of Supported Advertising Sets (Vol 4, Part E § 7.8.58) 147 // Note: the controller can change the number of advertising sets 148 // at any time. This behaviour is not emulated here. 149 uint8_t le_num_supported_advertising_sets{16}; 150 151 // LE Periodic Advertiser List Size (Vol 4, Part E § 7.8.73). 152 uint8_t le_periodic_advertiser_list_size{8}; 153 154 // Vendor Information. 155 // Provide parameters returned by vendor specific commands. 156 std::vector<uint8_t> le_vendor_capabilities{}; 157 SupportsLMPFeatureControllerProperties158 bool SupportsLMPFeature(bluetooth::hci::LMPFeaturesPage0Bits bit) const { 159 return (lmp_features[0] & static_cast<uint64_t>(bit)) != 0; 160 } 161 SupportsLMPFeatureControllerProperties162 bool SupportsLMPFeature(bluetooth::hci::LMPFeaturesPage2Bits bit) const { 163 return (lmp_features[2] & static_cast<uint64_t>(bit)) != 0; 164 } 165 SupportsLLFeatureControllerProperties166 bool SupportsLLFeature(bluetooth::hci::LLFeaturesBits bit) const { 167 return (le_features & static_cast<uint64_t>(bit)) != 0; 168 } 169 SupportsCommandControllerProperties170 bool SupportsCommand(bluetooth::hci::OpCodeIndex op_code) const { 171 int index = static_cast<int>(op_code); 172 return (supported_commands[index / 10] & (UINT64_C(1) << (index % 10))) != 173 0; 174 } 175 176 /// Return a bit mask with all supported PHYs 177 /// (0b001 = LE_1M, 0b010 = LE_2M, 0b100 = LE_CODED). LeSupportedPhysControllerProperties178 uint8_t LeSupportedPhys() const { 179 uint8_t supported_phys = 0x1; // LE_1M is always supported. 180 if (SupportsLLFeature(bluetooth::hci::LLFeaturesBits::LE_2M_PHY)) { 181 supported_phys |= 0x2; 182 } 183 if (SupportsLLFeature(bluetooth::hci::LLFeaturesBits::LE_CODED_PHY)) { 184 supported_phys |= 0x4; 185 } 186 return supported_phys; 187 } 188 }; 189 190 } // namespace rootcanal 191