1 /* 2 * Copyright (C) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://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, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef BLE_ADAPTER_H 17 #define BLE_ADAPTER_H 18 19 #include <map> 20 #include <memory> 21 22 #include "context.h" 23 #include "interface_adapter_ble.h" 24 #include "raw_address.h" 25 26 #include "base_observer_list.h" 27 #include "ble_advertiser_impl.h" 28 #include "ble_central_manager_impl.h" 29 #include "ble_properties.h" 30 #include "ble_security.h" 31 #include "bt_uuid.h" 32 #include "btm.h" 33 34 /* 35 * @brief The Bluetooth subsystem. 36 */ 37 namespace OHOS { 38 namespace bluetooth { 39 /** 40 * @brief BLE Adpter implementation class 41 */ 42 class BleAdapter : public IAdapterBle, public utility::Context { 43 public: 44 /** 45 * @brief Constructor. 46 */ 47 BleAdapter(); 48 49 /** 50 * @brief Destructor. 51 */ 52 ~BleAdapter(); 53 54 utility::Context *GetContext() override; 55 56 /** 57 * @brief Turn on the BLE Bluetooth adapter 58 * 59 * @return @c true Turn on BLE Bluetooth successfully 60 * @c false Failed to turn on BLE Bluetooth 61 */ 62 void Enable() override; 63 64 /** 65 * @brief Turn off the BLE Bluetooth adapter 66 * 67 * @return @c true Turn off BLE Bluetooth successfully 68 * @c false Failed to turn off BLE Bluetooth 69 */ 70 void Disable() override; 71 72 /** 73 * @brief Processing after Bluetooth startup 74 * 75 * @return @c true success 76 * @c false failure 77 */ 78 void PostEnable() override; 79 80 /** 81 * @brief Get local host bluetooth address 82 * 83 * @return @c Local host bluetooth address 84 */ 85 std::string GetLocalAddress() const override; 86 87 /** 88 * @brief Get local host bluetooth name 89 * 90 * @return @c Local host bluetooth name 91 */ 92 std::string GetLocalName() const override; 93 94 /** 95 * @brief Set local host bluetooth name 96 * 97 * @param [in] name Device name. 98 * @return @c true success 99 * @c false failure 100 */ 101 bool SetLocalName(const std::string &name) const override; 102 103 /// add adapter manager common api 104 std::string GetDeviceName(const RawAddress &device) const override; 105 std::vector<Uuid> GetDeviceUuids(const RawAddress &device) const override; 106 std::vector<RawAddress> GetPairedDevices() const override; 107 std::vector<RawAddress> GetConnectedDevices() const; 108 bool StartPair(const RawAddress &device) override; 109 bool CancelPairing(const RawAddress &device) override; 110 bool RemovePair(const RawAddress &device) override; 111 bool RemoveAllPairs() override; 112 bool IsBondedFromLocal(const RawAddress &device) const override; 113 bool SetDevicePasskey(const RawAddress &device, int passkey, bool accept) const override; 114 bool PairRequestReply(const RawAddress &device, bool accept) const override; 115 bool IsAclConnected(const RawAddress &device) const override; 116 bool IsAclEncrypted(const RawAddress &device) const override; 117 int GetPairState(const RawAddress &device) const override; 118 int GetBondableMode() const override; 119 bool SetBondableMode(int mode) const override; 120 bool SetDevicePairingConfirmation(const RawAddress &device, bool accept) const override; 121 int GetDeviceType(const RawAddress &device) const override; 122 int GetBleMaxAdvertisingDataLength() const override; 123 int GetIoCapability() const override; 124 bool SetIoCapability(int ioCapability) const override; 125 bool IsBleEnabled() const; 126 bool IsBtDiscovering() const override; 127 128 /// FW api passthrough from service 129 void StartAdvertising(const BleAdvertiserSettingsImpl &settings, const BleAdvertiserDataImpl &advData, 130 const BleAdvertiserDataImpl &scanResponse, uint8_t advHandle) const override; 131 void StopAdvertising(uint8_t advHandle) const override; 132 void Close(uint8_t advHandle) const override; 133 void StartScan(const BleScanSettingsImpl &setting) const override; 134 void StopScan() const override; 135 int ConfigScanFilter(int32_t scannerId, const std::vector<BleScanFilterImpl> &filters) override; 136 void RemoveScanFilter(int32_t scannerId) override; 137 int GetAdvertisingStatus() const override; 138 bool IsLlPrivacySupported() const override; 139 void AddCharacteristicValue(uint8_t adtype, const std::string &data) const override; 140 141 void RegisterBleAdvertiserCallback(IBleAdvertiserCallback &callback) override; 142 void DeregisterBleAdvertiserCallback() const override; 143 void RegisterBleCentralManagerCallback(IBleCentralManagerCallback &callback) override; 144 void DeregisterBleCentralManagerCallback() const override; 145 void RegisterBlePeripheralCallback(IBlePeripheralCallback &callback) const override; 146 void DeregisterBlePeripheralCallback(IBlePeripheralCallback &callback) const override; 147 void RegisterBleSecurityCallback(BaseObserverList<IAdapterBleObserver> &callback); 148 void DeregisterBleSecurityCallback() const; 149 150 bool RegisterBleAdapterObserver(IAdapterBleObserver &observer) const override; 151 bool DeregisterBleAdapterObserver(IAdapterBleObserver &observer) const override; 152 int GetPeerDeviceAddrType(const RawAddress &device) const override; 153 154 /// pair 155 void LePairComplete(const RawAddress &device, const int status) const; 156 void LePairingStatus(const RawAddress &device) const; 157 void EncryptionComplete(const RawAddress &device) const; 158 void NotifyAllWaitContinue() const; 159 bool IsRemovePairedDevice(const RawAddress &device) const; 160 161 void OnStartAdvertisingEvt() const; 162 void OnStopAdvertisingEvt() const; 163 164 /** 165 * @brief Read Remote Rssi Value. 166 * 167 * @return @c true sucessfull otherwise false. 168 */ 169 bool ReadRemoteRssiValue(const RawAddress &device) const override; 170 uint8_t GetAdvertiserHandle() const override; 171 bool RemovePairWithDisConnect(const RawAddress &device, bool isDisconnect = true) const; 172 173 int32_t AllocScannerId() override; 174 void RemoveScannerId(int32_t scannerId) override; 175 176 private: 177 int RegisterCallbackToBtm(); 178 int DeregisterCallbackToBtm() const; 179 int DeregisterAllCallback() const; 180 bool EnableTask(); 181 bool DisableTask(); 182 bool PostEnableTask() const; 183 void StartOrStopAdvAndScan( 184 const STOP_ALL_ADV_TYPE &stopAllAdvType, const STOP_SCAN_TYPE &scanType, bool isStartAdvAndScan = false) const; 185 void LoadConfig() const; 186 void LeConnectionCompleteTask(uint8_t status, uint16_t connectionHandle, const BtAddr &addr, uint8_t role) const; 187 void LeDisconnectionCompleteTask(uint8_t status, uint16_t connectionHandle, uint8_t reason) const; 188 void OnReadRemoteRssiEventTask(uint8_t status, const BtAddr &addr, int8_t rssi) const; 189 // pair 190 void ReadPeerDeviceInfoFromConf(const std::vector<std::string> &pairedAddrList) const; 191 bool SavePeerDeviceInfoToConf(const std::map<std::string, BlePeripheralDevice> &peerConnDeviceList) const; 192 void SavePeerDevices2BTM(const std::map<std::string, BlePeripheralDevice> &peerConnDeviceList) const; 193 void ClearPeerDeviceInfo() const; 194 void ClearScanResultInfo() const; 195 int SetRpaAddrAndTypeToBtm(); 196 int SetLocalIrkAndIdentityAddrToBtm() const; 197 int InitBtmAndGap(); 198 int SetBleRoles() const; 199 void ExAdvClearHandle() const; 200 std::string ReadRemoteDeviceNameByGatt(const RawAddress &addr, int appID) const; 201 int RegisterGattClientApplication(const RawAddress &addr) const; 202 void DeregisterGattClientApplication(int appID) const; 203 204 /// BTM ACL Change status callback. 205 static void LeConnectionComplete( 206 uint8_t status, uint16_t connectionHandle, const BtAddr *addr, uint8_t role, void *context); 207 static void LeDisconnectionComplete(uint8_t status, uint16_t connectionHandle, uint8_t reason, void *context); 208 static void OnReadRemoteRssiEvent(uint8_t status, const BtAddr *addr, int8_t rssi, void *context); 209 static void GenResPriAddrResult(uint8_t result, const uint8_t addr[BT_ADDRESS_SIZE], void *context); 210 void ClearScannerIdInfo() const; 211 212 BT_DISALLOW_COPY_AND_ASSIGN(BleAdapter); 213 DECLARE_IMPL(); 214 }; 215 } // namespace bluetooth 216 } // namespace OHOS 217 218 #endif /// BLE_ADAPTER_H 219