1 /* 2 * Copyright 2019 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 <cstddef> 20 #include <cstdint> 21 #include <functional> 22 #include <mutex> 23 #include <unordered_map> 24 #include <vector> 25 26 #include "gd/common/callback.h" 27 #include "gd/hci/le_advertising_manager.h" 28 #include "gd/hci/le_scanning_manager.h" 29 #include "gd/neighbor/inquiry.h" 30 #include "gd/os/alarm.h" 31 #include "hci/hci_packets.h" 32 #include "stack/btm/neighbor_inquiry.h" 33 #include "stack/include/btm_api_types.h" 34 #include "types/bluetooth/uuid.h" 35 #include "types/raw_address.h" 36 37 // 38 // NOTE: limited and general constants for inquiry and discoverable are swapped 39 // 40 41 /* Discoverable modes */ 42 static constexpr int kDiscoverableModeOff = 0; // BTM_NON_DISCOVERABLE 43 static constexpr int kLimitedDiscoverableMode = 1; // BTM_LIMITED_DISCOVERABLE 44 static constexpr int kGeneralDiscoverableMode = 2; // BTM_GENERAL_DISCOVERABLE 45 46 /* Inquiry modes */ 47 static constexpr uint8_t kInquiryModeOff = 0; // BTM_INQUIRY_NONE 48 static constexpr uint8_t kGeneralInquiryMode = 1; // BTM_GENERAL_INQUIRY 49 static constexpr uint8_t kLimitedInquiryMode = 2; // BTM_LIMITED_INQUIRY 50 51 /* Connectable modes */ 52 static constexpr int kConnectibleModeOff = 0; // BTM_NON_CONNECTABLE 53 static constexpr int kConnectibleModeOn = 1; // BTM_CONNECTABLE 54 55 /* Inquiry and page scan modes */ 56 static constexpr int kStandardScanType = 0; 57 static constexpr int kInterlacedScanType = 1; 58 59 /* Inquiry result modes */ 60 static constexpr int kStandardInquiryResult = 0; 61 static constexpr int kInquiryResultWithRssi = 1; 62 static constexpr int kExtendedInquiryResult = 2; 63 64 static constexpr uint8_t kPhyConnectionNone = 0x00; 65 static constexpr uint8_t kPhyConnectionLe1M = 0x01; 66 static constexpr uint8_t kPhyConnectionLe2M = 0x02; 67 static constexpr uint8_t kPhyConnectionLeCoded = 0x03; 68 69 using LegacyInquiryCompleteCallback = 70 std::function<void(tBTM_STATUS status, uint8_t inquiry_mode)>; 71 72 using DiscoverabilityState = struct { 73 int mode; 74 uint16_t interval; 75 uint16_t window; 76 }; 77 using ConnectabilityState = DiscoverabilityState; 78 79 using HACK_NonAclDisconnectCallback = std::function<void(uint16_t, uint8_t)>; 80 81 using BtmStatus = tBTM_STATUS; 82 83 namespace bluetooth { 84 namespace shim { 85 86 class Btm { 87 public: 88 // |handler| is used to run timer tasks and scan callbacks 89 Btm(os::Handler* handler, neighbor::InquiryModule* inquiry); 90 ~Btm() = default; 91 92 // Inquiry result callbacks 93 void OnInquiryResult(bluetooth::hci::InquiryResultView view); 94 void OnInquiryResultWithRssi(bluetooth::hci::InquiryResultWithRssiView view); 95 void OnExtendedInquiryResult(bluetooth::hci::ExtendedInquiryResultView view); 96 void OnInquiryComplete(bluetooth::hci::ErrorCode status); 97 98 void SetStandardInquiryResultMode(); 99 void SetInquiryWithRssiResultMode(); 100 void SetExtendedInquiryResultMode(); 101 102 void SetInterlacedInquiryScan(); 103 void SetStandardInquiryScan(); 104 bool IsInterlacedScanSupported() const; 105 106 bool StartInquiry(uint8_t mode, uint8_t duration, uint8_t max_responses, 107 LegacyInquiryCompleteCallback inquiry_complete_callback); 108 void CancelInquiry(); 109 bool IsInquiryActive() const; 110 bool IsGeneralInquiryActive() const; 111 bool IsLimitedInquiryActive() const; 112 113 bool StartPeriodicInquiry(uint8_t mode, uint8_t duration, 114 uint8_t max_responses, uint16_t max_delay, 115 uint16_t min_delay, 116 tBTM_INQ_RESULTS_CB* p_results_cb); 117 void CancelPeriodicInquiry(); 118 bool IsGeneralPeriodicInquiryActive() const; 119 bool IsLimitedPeriodicInquiryActive() const; 120 121 // Discoverability API 122 bool general_inquiry_active_{false}; 123 bool limited_inquiry_active_{false}; 124 bool general_periodic_inquiry_active_{false}; 125 bool limited_periodic_inquiry_active_{false}; 126 void SetClassicGeneralDiscoverability(uint16_t window, uint16_t interval); 127 void SetClassicLimitedDiscoverability(uint16_t window, uint16_t interval); 128 void SetClassicDiscoverabilityOff(); 129 DiscoverabilityState GetClassicDiscoverabilityState() const; 130 131 void SetLeGeneralDiscoverability(); 132 void SetLeLimitedDiscoverability(); 133 void SetLeDiscoverabilityOff(); 134 DiscoverabilityState GetLeDiscoverabilityState() const; 135 136 void SetClassicConnectibleOn(); 137 void SetClassicConnectibleOff(); 138 ConnectabilityState GetClassicConnectabilityState() const; 139 void SetInterlacedPageScan(); 140 void SetStandardPageScan(); 141 142 void SetLeConnectibleOn(); 143 void SetLeConnectibleOff(); 144 ConnectabilityState GetLeConnectabilityState() const; 145 146 bool UseLeLink(const RawAddress& raw_address) const; 147 148 // Remote device name API 149 BtmStatus ReadClassicRemoteDeviceName(const RawAddress& raw_address, 150 tBTM_CMPL_CB* callback); 151 BtmStatus ReadLeRemoteDeviceName(const RawAddress& raw_address, 152 tBTM_CMPL_CB* callback); 153 BtmStatus CancelAllReadRemoteDeviceName(); 154 155 // Le neighbor interaction API 156 bluetooth::hci::AdvertiserId advertiser_id_{ 157 hci::LeAdvertisingManager::kInvalidId}; 158 void StartAdvertising(); 159 void StopAdvertising(); 160 void StartConnectability(); 161 void StopConnectability(); 162 163 void StartActiveScanning(); 164 void StopActiveScanning(); 165 166 void StartObserving(); 167 void StopObserving(); 168 169 size_t GetNumberOfAdvertisingInstances() const; 170 171 void SetObservingTimer(uint64_t duration_ms, 172 common::OnceCallback<void()> callback); 173 void CancelObservingTimer(); 174 void SetScanningTimer(uint64_t duration_ms, 175 common::OnceCallback<void()> callback); 176 void CancelScanningTimer(); 177 178 tBTM_STATUS CreateBond(const RawAddress& bd_addr, tBLE_ADDR_TYPE addr_type, 179 tBT_TRANSPORT transport, int device_type); 180 bool CancelBond(const RawAddress& bd_addr); 181 bool RemoveBond(const RawAddress& bd_addr); 182 183 uint16_t GetAclHandle(const RawAddress& remote_bda, tBT_TRANSPORT transport); 184 185 static hci::AddressWithType GetAddressAndType(const RawAddress& bd_addr); 186 187 private: 188 os::Alarm scanning_timer_; 189 os::Alarm observing_timer_; 190 191 LegacyInquiryCompleteCallback legacy_inquiry_complete_callback_{}; 192 uint8_t active_inquiry_mode_ = 0; 193 194 class ReadRemoteName { 195 public: 196 ReadRemoteName() = default; 197 bool Start(RawAddress raw_address); 198 void Stop(); 199 bool IsInProgress() const; 200 std::string AddressString() const; 201 202 private: 203 std::mutex mutex_; 204 bool in_progress_ = false; 205 RawAddress raw_address_ = RawAddress::kEmpty; 206 }; 207 ReadRemoteName le_read_remote_name_; 208 ReadRemoteName classic_read_remote_name_; 209 210 class ScanningCallbacks : public hci::ScanningCallback { 211 void OnScannerRegistered(const bluetooth::hci::Uuid app_uuid, 212 bluetooth::hci::ScannerId scanner_id, 213 ScanningStatus status) override; 214 void OnSetScannerParameterComplete(bluetooth::hci::ScannerId scanner_id, 215 ScanningStatus status) override; 216 void OnScanResult(uint16_t event_type, uint8_t address_type, 217 bluetooth::hci::Address address, uint8_t primary_phy, 218 uint8_t secondary_phy, uint8_t advertising_sid, 219 int8_t tx_power, int8_t rssi, 220 uint16_t periodic_advertising_interval, 221 std::vector<uint8_t> advertising_data) override; 222 void OnTrackAdvFoundLost(bluetooth::hci::AdvertisingFilterOnFoundOnLostInfo 223 on_found_on_lost_info) override; 224 void OnBatchScanReports(int client_if, int status, int report_format, 225 int num_records, 226 std::vector<uint8_t> data) override; 227 void OnBatchScanThresholdCrossed(int client_if) override; 228 void OnTimeout() override; 229 void OnFilterEnable(bluetooth::hci::Enable enable, uint8_t status) override; 230 void OnFilterParamSetup(uint8_t available_spaces, 231 bluetooth::hci::ApcfAction action, 232 uint8_t status) override; 233 void OnFilterConfigCallback(bluetooth::hci::ApcfFilterType filter_type, 234 uint8_t available_spaces, 235 bluetooth::hci::ApcfAction action, 236 uint8_t status) override; 237 void OnPeriodicSyncStarted( 238 int reg_id, uint8_t status, uint16_t sync_handle, 239 uint8_t advertising_sid, 240 bluetooth::hci::AddressWithType address_with_type, uint8_t phy, 241 uint16_t interval) override; 242 void OnPeriodicSyncReport(uint16_t sync_handle, int8_t tx_power, 243 int8_t rssi, uint8_t status, 244 std::vector<uint8_t> data) override; 245 void OnPeriodicSyncLost(uint16_t sync_handle) override; 246 void OnPeriodicSyncTransferred(int pa_source, uint8_t status, 247 bluetooth::hci::Address address) override; 248 }; 249 ScanningCallbacks scanning_callbacks_; 250 251 // TODO(cmanton) abort if there is no classic acl link up CheckClassicAclLink(const RawAddress & raw_address)252 bool CheckClassicAclLink(const RawAddress& raw_address) { return true; } CheckLeAclLink(const RawAddress & raw_address)253 bool CheckLeAclLink(const RawAddress& raw_address) { return true; } 254 void StartScanning(bool use_active_scanning); 255 }; 256 257 } // namespace shim 258 } // namespace bluetooth 259