• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/device_address.h"
20 #include "pw_bluetooth_sapphire/internal/host/gap/android_vendor_capabilities.h"
21 #include "pw_bluetooth_sapphire/internal/host/gap/gap.h"
22 #include "pw_bluetooth_sapphire/internal/host/gap/low_energy_state.h"
23 #include "pw_bluetooth_sapphire/internal/host/hci-spec/constants.h"
24 #include "pw_bluetooth_sapphire/internal/host/hci-spec/lmp_feature_set.h"
25 #include "pw_bluetooth_sapphire/internal/host/transport/acl_data_channel.h"
26 
27 namespace bt::gap {
28 
29 // The member variables in this class consist of controller settings that are
30 // shared between LE and BR/EDR controllers. LE and BR/EDR specific state is
31 // stored in corresponding data structures.
32 struct AdapterState final {
typefinal33   TechnologyType type() const {
34     // Note: we don't support BR/EDR only controllers.
35     if (IsBREDRSupported()) {
36       return TechnologyType::kDualMode;
37     }
38     return TechnologyType::kLowEnergy;
39   }
40 
41   // Returns true if the indicated feature is supported by Controller.
IsControllerFeatureSupportedfinal42   bool IsControllerFeatureSupported(
43       pw::bluetooth::Controller::FeaturesBits feature) const {
44     return feature & controller_features;
45   }
46 
47   // Helpers for querying LMP capabilities.
IsBREDRSupportedfinal48   inline bool IsBREDRSupported() const {
49     return !features.HasBit(/*page=*/0u,
50                             hci_spec::LMPFeature::kBREDRNotSupported);
51   }
52 
IsLowEnergySupportedfinal53   inline bool IsLowEnergySupported() const {
54     return features.HasBit(/*page=*/0u, hci_spec::LMPFeature::kLESupportedHost);
55   }
56 
IsLocalSecureConnectionsSupportedfinal57   inline bool IsLocalSecureConnectionsSupported() const {
58     return features.HasBit(
59                /*page=*/1u,
60                hci_spec::LMPFeature::kSecureConnectionsHostSupport) &&
61            features.HasBit(
62                /*page=*/2u,
63                hci_spec::LMPFeature::kSecureConnectionsControllerSupport);
64   }
65 
IsSecureConnectionHostSupportSupportedfinal66   inline bool IsSecureConnectionHostSupportSupported() const {
67     return features.HasBit(/*page=*/1,
68                            hci_spec::LMPFeature::kSecureConnectionsHostSupport);
69   }
70 
SupportedCommandsfinal71   inline auto SupportedCommands() const {
72     return pw::bluetooth::emboss::MakeSupportedCommandsView(
73         supported_commands, sizeof(supported_commands));
74   }
75 
IsControllerRemotePublicKeyValidationSupportedfinal76   inline bool IsControllerRemotePublicKeyValidationSupported() const {
77     // We don't actually need to send the command:  "Note: If this command is
78     // supported, then the Controller must support remote public key validation"
79     // (v6.0, Vol 4, Part E, Sec. 7.4.9).
80     return SupportedCommands().read_local_simple_pairing_options().Read();
81   }
82 
83   // HCI version supported by the controller.
84   pw::bluetooth::emboss::CoreSpecificationVersion hci_version;
85 
86   // The Features that are supported by this adapter.
87   hci_spec::LMPFeatureSet features;
88 
89   // Features reported by Controller.
90   pw::bluetooth::Controller::FeaturesBits controller_features{0};
91 
92   // Bitmask list of HCI commands that the controller supports.
93   uint8_t supported_commands[64] = {0};
94 
95   // This returns Bluetooth Controller address. This address has the following
96   // meaning based on the controller capabilities:
97   //   - On BR/EDR this is the Bluetooth Controller Address, or BD_ADDR.
98   //   - On LE this is the Public Device Address. This value can be used as the
99   //     device's identity address. This value can be zero if a Public Device
100   //     Address is not used.
101   //   - On BR/EDR/LE this is the LE Public Device Address AND the BD_ADDR.
102   DeviceAddressBytes controller_address;
103 
104   // The BR/EDR ACL data buffer size. We store this here as it is needed on
105   // dual-mode controllers even if the host stack is compiled for LE-only.
106   hci::DataBufferInfo bredr_data_buffer_info;
107 
108   // The SCO buffer size.
109   hci::DataBufferInfo sco_buffer_info;
110 
111   // BLE-specific state.
112   LowEnergyState low_energy_state;
113 
114   // Android vendor extensions capabilities
115   // NOTE: callers should separately check that the controller actually supports
116   // android vendor extensions first.
117   std::optional<AndroidVendorCapabilities> android_vendor_capabilities;
118 
119   // Local name
120   std::string local_name;
121 };
122 
123 }  // namespace bt::gap
124