1 // Copyright 2023 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 15 #pragma once 16 #include <cstdint> 17 18 #include "pw_bluetooth/controller.h" 19 #include "pw_bluetooth_sapphire/internal/host/common/assert.h" 20 #include "pw_bluetooth_sapphire/internal/host/common/device_address.h" 21 #include "pw_bluetooth_sapphire/internal/host/gap/android_vendor_capabilities.h" 22 #include "pw_bluetooth_sapphire/internal/host/gap/gap.h" 23 #include "pw_bluetooth_sapphire/internal/host/gap/low_energy_state.h" 24 #include "pw_bluetooth_sapphire/internal/host/hci-spec/constants.h" 25 #include "pw_bluetooth_sapphire/internal/host/hci-spec/lmp_feature_set.h" 26 #include "pw_bluetooth_sapphire/internal/host/transport/acl_data_channel.h" 27 28 namespace bt::gap { 29 30 // The member variables in this class consist of controller settings that are 31 // shared between LE and BR/EDR controllers. LE and BR/EDR specific state is 32 // stored in corresponding data structures. 33 struct AdapterState final { typefinal34 TechnologyType type() const { 35 // Note: we don't support BR/EDR only controllers. 36 if (IsBREDRSupported()) { 37 return TechnologyType::kDualMode; 38 } 39 return TechnologyType::kLowEnergy; 40 } 41 42 // Returns true if the indicated feature is supported by Controller. IsControllerFeatureSupportedfinal43 bool IsControllerFeatureSupported( 44 pw::bluetooth::Controller::FeaturesBits feature) const { 45 return feature & controller_features; 46 } 47 48 // Helpers for querying LMP capabilities. IsBREDRSupportedfinal49 inline bool IsBREDRSupported() const { 50 return !features.HasBit(/*page=*/0u, 51 hci_spec::LMPFeature::kBREDRNotSupported); 52 } 53 IsLowEnergySupportedfinal54 inline bool IsLowEnergySupported() const { 55 return features.HasBit(/*page=*/0u, hci_spec::LMPFeature::kLESupportedHost); 56 } 57 IsLocalSecureConnectionsSupportedfinal58 inline bool IsLocalSecureConnectionsSupported() const { 59 return features.HasBit( 60 /*page=*/1u, 61 hci_spec::LMPFeature::kSecureConnectionsHostSupport) && 62 features.HasBit( 63 /*page=*/2u, 64 hci_spec::LMPFeature::kSecureConnectionsControllerSupport); 65 } 66 IsSecureConnectionHostSupportSupportedfinal67 inline bool IsSecureConnectionHostSupportSupported() const { 68 return features.HasBit(/*page=*/1, 69 hci_spec::LMPFeature::kSecureConnectionsHostSupport); 70 } 71 72 // Returns true if |command_bit| in the given |octet| is set in the supported 73 // command list. IsCommandSupportedfinal74 inline bool IsCommandSupported(size_t octet, 75 hci_spec::SupportedCommand command_bit) const { 76 BT_DEBUG_ASSERT(octet < sizeof(supported_commands)); 77 return supported_commands[octet] & static_cast<uint8_t>(command_bit); 78 } 79 80 // HCI version supported by the controller. 81 pw::bluetooth::emboss::CoreSpecificationVersion hci_version; 82 83 // The Features that are supported by this adapter. 84 hci_spec::LMPFeatureSet features; 85 86 // Features reported by Controller. 87 pw::bluetooth::Controller::FeaturesBits controller_features{0}; 88 89 // Bitmask list of HCI commands that the controller supports. 90 uint8_t supported_commands[64] = {0}; 91 92 // This returns Bluetooth Controller address. This address has the following 93 // meaning based on the controller capabilities: 94 // - On BR/EDR this is the Bluetooth Controller Address, or BD_ADDR. 95 // - On LE this is the Public Device Address. This value can be used as the 96 // device's identity address. This value can be zero if a Public Device 97 // Address is not used. 98 // - On BR/EDR/LE this is the LE Public Device Address AND the BD_ADDR. 99 DeviceAddressBytes controller_address; 100 101 // The BR/EDR ACL data buffer size. We store this here as it is needed on 102 // dual-mode controllers even if the host stack is compiled for LE-only. 103 hci::DataBufferInfo bredr_data_buffer_info; 104 105 // The SCO buffer size. 106 hci::DataBufferInfo sco_buffer_info; 107 108 // BLE-specific state. 109 LowEnergyState low_energy_state; 110 111 // Android vendor extensions capabilities 112 // NOTE: callers should separately check that the controller actually supports 113 // android vendor extensions first. 114 AndroidVendorCapabilities android_vendor_capabilities; 115 116 // Local name 117 std::string local_name; 118 }; 119 120 } // namespace bt::gap 121