• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 override;
134     void StartScan(const BleScanSettingsImpl &setting) const override;
135     void StopScan() const override;
136     int ConfigScanFilter(int32_t scannerId, const std::vector<BleScanFilterImpl> &filters) override;
137     void RemoveScanFilter(int32_t scannerId) override;
138     int GetAdvertisingStatus() const override;
139     bool IsLlPrivacySupported() const override;
140     void AddCharacteristicValue(uint8_t adtype, const std::string &data) const override;
141 
142     void RegisterBleAdvertiserCallback(IBleAdvertiserCallback &callback) override;
143     void DeregisterBleAdvertiserCallback() const override;
144     void RegisterBleCentralManagerCallback(IBleCentralManagerCallback &callback) override;
145     void DeregisterBleCentralManagerCallback() const override;
146     void RegisterBlePeripheralCallback(IBlePeripheralCallback &callback) const override;
147     void DeregisterBlePeripheralCallback(IBlePeripheralCallback &callback) const override;
148     void RegisterBleSecurityCallback(BaseObserverList<IAdapterBleObserver> &callback);
149     void DeregisterBleSecurityCallback() const;
150 
151     bool RegisterBleAdapterObserver(IAdapterBleObserver &observer) const override;
152     bool DeregisterBleAdapterObserver(IAdapterBleObserver &observer) const override;
153     int GetPeerDeviceAddrType(const RawAddress &device) const override;
154 
155     /// pair
156     void LePairComplete(const RawAddress &device, const int status) const;
157     void LePairingStatus(const RawAddress &device) const;
158     void EncryptionComplete(const RawAddress &device) const;
159     void NotifyAllWaitContinue() const;
160     bool IsRemovePairedDevice(const RawAddress &device) const;
161 
162     void OnStartAdvertisingEvt() const;
163     void OnStopAdvertisingEvt() const;
164 
165     /**
166      * @brief Read Remote Rssi Value.
167      *
168      * @return @c true sucessfull otherwise false.
169      */
170     bool ReadRemoteRssiValue(const RawAddress &device) const override;
171     uint8_t GetAdvertiserHandle() const override;
172     bool RemovePairWithDisConnect(const RawAddress &device, bool isDisconnect = true) const;
173 
174     int32_t AllocScannerId() override;
175     void RemoveScannerId(int32_t scannerId) override;
176 
177 private:
178     int RegisterCallbackToBtm();
179     int DeregisterCallbackToBtm() const;
180     int DeregisterAllCallback() const;
181     bool EnableTask();
182     bool DisableTask();
183     bool PostEnableTask() const;
184     void StartOrStopAdvAndScan(
185         const STOP_ALL_ADV_TYPE &stopAllAdvType, const STOP_SCAN_TYPE &scanType, bool isStartAdvAndScan = false) const;
186     void LoadConfig() const;
187     void LeConnectionCompleteTask(uint8_t status, uint16_t connectionHandle, const BtAddr &addr, uint8_t role) const;
188     void LeDisconnectionCompleteTask(uint8_t status, uint16_t connectionHandle, uint8_t reason) const;
189     void OnReadRemoteRssiEventTask(uint8_t status, const BtAddr &addr, int8_t rssi) const;
190     // pair
191     void ReadPeerDeviceInfoFromConf(const std::vector<std::string> &pairedAddrList) const;
192     bool SavePeerDeviceInfoToConf(const std::map<std::string, BlePeripheralDevice> &peerConnDeviceList) const;
193     void SavePeerDevices2BTM(const std::map<std::string, BlePeripheralDevice> &peerConnDeviceList) const;
194     void ClearPeerDeviceInfo() const;
195     void ClearScanResultInfo() const;
196     int SetRpaAddrAndTypeToBtm();
197     int SetLocalIrkAndIdentityAddrToBtm() const;
198     int InitBtmAndGap();
199     int SetBleRoles() const;
200     void ExAdvClearHandle() const;
201     std::string ReadRemoteDeviceNameByGatt(const RawAddress &addr, int appID) const;
202     int RegisterGattClientApplication(const RawAddress &addr) const;
203     void DeregisterGattClientApplication(int appID) const;
204 
205     /// BTM ACL Change status callback.
206     static void LeConnectionComplete(
207         uint8_t status, uint16_t connectionHandle, const BtAddr *addr, uint8_t role, void *context);
208     static void LeDisconnectionComplete(uint8_t status, uint16_t connectionHandle, uint8_t reason, void *context);
209     static void OnReadRemoteRssiEvent(uint8_t status, const BtAddr *addr, int8_t rssi, void *context);
210     static void GenResPriAddrResult(uint8_t result, const uint8_t addr[BT_ADDRESS_SIZE], void *context);
211     void ClearScannerIdInfo() const;
212 
213     BT_DISALLOW_COPY_AND_ASSIGN(BleAdapter);
214     DECLARE_IMPL();
215 };
216 }  // namespace bluetooth
217 }  // namespace OHOS
218 
219 #endif  /// BLE_ADAPTER_H
220