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 
SupportedCommandsfinal72   inline auto SupportedCommands() const {
73     return pw::bluetooth::emboss::MakeSupportedCommandsView(
74         supported_commands, sizeof(supported_commands));
75   }
76 
77   // HCI version supported by the controller.
78   pw::bluetooth::emboss::CoreSpecificationVersion hci_version;
79 
80   // The Features that are supported by this adapter.
81   hci_spec::LMPFeatureSet features;
82 
83   // Features reported by Controller.
84   pw::bluetooth::Controller::FeaturesBits controller_features{0};
85 
86   // Bitmask list of HCI commands that the controller supports.
87   uint8_t supported_commands[64] = {0};
88 
89   // This returns Bluetooth Controller address. This address has the following
90   // meaning based on the controller capabilities:
91   //   - On BR/EDR this is the Bluetooth Controller Address, or BD_ADDR.
92   //   - On LE this is the Public Device Address. This value can be used as the
93   //     device's identity address. This value can be zero if a Public Device
94   //     Address is not used.
95   //   - On BR/EDR/LE this is the LE Public Device Address AND the BD_ADDR.
96   DeviceAddressBytes controller_address;
97 
98   // The BR/EDR ACL data buffer size. We store this here as it is needed on
99   // dual-mode controllers even if the host stack is compiled for LE-only.
100   hci::DataBufferInfo bredr_data_buffer_info;
101 
102   // The SCO buffer size.
103   hci::DataBufferInfo sco_buffer_info;
104 
105   // BLE-specific state.
106   LowEnergyState low_energy_state;
107 
108   // Android vendor extensions capabilities
109   // NOTE: callers should separately check that the controller actually supports
110   // android vendor extensions first.
111   std::optional<AndroidVendorCapabilities> android_vendor_capabilities;
112 
113   // Local name
114   std::string local_name;
115 };
116 
117 }  // namespace bt::gap
118