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 OHOS_BLUETOOTH_STANDARD_HOST_SERVER_H
17 #define OHOS_BLUETOOTH_STANDARD_HOST_SERVER_H
18 
19 #include <map>
20 
21 #include "bt_def.h"
22 #include "bluetooth_host_stub.h"
23 #include "bluetooth_types.h"
24 #include "if_system_ability_manager.h"
25 #include "iservice_registry.h"
26 #include "system_ability.h"
27 #include "bluetooth_remote_device_info.h"
28 
29 namespace OHOS {
30 namespace Bluetooth {
31 enum class ServiceRunningState { STATE_IDLE, STATE_RUNNING };
32 
33 class BluetoothHostServer : public SystemAbility, public BluetoothHostStub {
34     DECLARE_SYSTEM_ABILITY(BluetoothHostServer);
35 
36 public:
37     explicit BluetoothHostServer();
38     ~BluetoothHostServer() override;
39 
40     static sptr<BluetoothHostServer> GetInstance();
41 
42     void OnStart() override;
43     void OnStop() override;
44 
45     void RegisterObserver(const sptr<IBluetoothHostObserver> &observer) override;
46     void DeregisterObserver(const sptr<IBluetoothHostObserver> &observer) override;
47     int32_t EnableBt() override;
48     int32_t DisableBt() override;
49     int32_t GetBtState(int32_t &state) override;
50     int32_t BluetoothFactoryReset() override;
51     int32_t GetDeviceType(int32_t transport, const std::string &address) override;
52 
53     int32_t GetLocalAddress(std::string &addr) override;
54     sptr<IRemoteObject> GetProfile(const std::string &name) override;
55     sptr<IRemoteObject> GetBleRemote(const std::string &name) override;
56 
QueryServiceState()57     ServiceRunningState QueryServiceState() const
58     {
59         return state_;
60     }
61 
62     bool Start();
63     void Stop();
64     int32_t DisableBle() override;
65     int32_t EnableBle() override;
66     int32_t SatelliteControl(int type, int state) override;
67     bool IsBrEnabled();
68     bool IsBleEnabled();
69     std::vector<uint32_t> GetProfileList() override;
70     int32_t GetMaxNumConnectedAudioDevices() override;
71     int32_t GetBtConnectionState(int32_t &state) override;
72     int32_t GetBtProfileConnState(uint32_t profileId, int &state) override;
73     int32_t GetLocalDeviceClass() override;
74     bool SetLocalDeviceClass(const int32_t &deviceClass) override;
75     int32_t GetLocalName(std::string &name) override;
76     int32_t SetLocalName(const std::string &name) override;
77     int32_t GetBtScanMode(int32_t &scanMode) override;
78     int32_t SetBtScanMode(int32_t mode, int32_t duration) override;
79     int32_t GetBondableMode(const int32_t transport) override;
80     bool SetBondableMode(int32_t transport, int32_t mode) override;
81     int32_t StartBtDiscovery() override;
82     int32_t CancelBtDiscovery() override;
83     int32_t IsBtDiscovering(bool &isDiscovering, const int32_t transport) override;
84     long GetBtDiscoveryEndMillis() override;
85     int32_t GetPairedDevices(std::vector<BluetoothRawAddress> &pairedAddr) override;
86     int32_t RemovePair(const int32_t transport, const sptr<BluetoothRawAddress> &device) override;
87     bool RemoveAllPairs() override;
88     void RegisterRemoteDeviceObserver(const sptr<IBluetoothRemoteDeviceObserver> &observer) override;
89     void DeregisterRemoteDeviceObserver(const sptr<IBluetoothRemoteDeviceObserver> &observer) override;
90     int32_t GetBleMaxAdvertisingDataLength() override;
91     int32_t GetPhonebookPermission(const std::string &address) override;
92     bool SetPhonebookPermission(const std::string &address, int32_t permission) override;
93     int32_t GetMessagePermission(const std::string &address) override;
94     bool SetMessagePermission(const std::string &address, int32_t permission) override;
95     int32_t GetPowerMode(const std::string &address) override;
96     int32_t GetDeviceName(int32_t transport, const std::string &address, std::string &name) override;
97     std::string GetDeviceAlias(const std::string &address) override;
98     int32_t SetDeviceAlias(const std::string &address, const std::string &aliasName) override;
99     int32_t GetRemoteDeviceBatteryInfo(const std::string &address, BluetoothBatteryInfo &batteryInfo) override;
100     int32_t GetPairState(int32_t transport, const std::string &address, int32_t &pairState) override;
101     int32_t StartPair(int32_t transport, const std::string &address) override;
102     bool CancelPairing(int32_t transport, const std::string &address) override;
103     bool IsBondedFromLocal(int32_t transport, const std::string &address) override;
104     bool IsAclConnected(int32_t transport, const std::string &address) override;
105     bool IsAclEncrypted(int32_t transport, const std::string &address) override;
106     int32_t GetDeviceClass(const std::string &address, int32_t &cod) override;
107     int32_t SetDevicePin(const std::string &address, const std::string &pin) override;
108     int32_t SetDevicePairingConfirmation(int32_t transport, const std::string &address, bool accept) override;
109     bool SetDevicePasskey(int32_t transport, const std::string &address, int32_t passkey, bool accept) override;
110     bool PairRequestReply(int32_t transport, const std::string &address, bool accept) override;
111     bool ReadRemoteRssiValue(const std::string &address) override;
112     void RegisterBleAdapterObserver(const sptr<IBluetoothHostObserver> &observer) override;
113     void DeregisterBleAdapterObserver(const sptr<IBluetoothHostObserver> &observer) override;
114     void RegisterBlePeripheralCallback(const sptr<IBluetoothBlePeripheralObserver> &observer) override;
115     void DeregisterBlePeripheralCallback(const sptr<IBluetoothBlePeripheralObserver> &observer) override;
116     void GetLocalSupportedUuids(std::vector<std::string> &uuids) override;
117     int32_t GetDeviceUuids(const std::string &address, std::vector<std::string> &uuids) override;
118     int32_t GetLocalProfileUuids(std::vector<std::string> &uuids) override;
119     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
120     int32_t SetFastScan(bool isEnable) override;
121     int32_t GetRandomAddress(const std::string &realAddr, std::string &randomAddr) override;
122     int32_t SyncRandomAddress(const std::string &realAddr, const std::string &randomAddr) override;
123     int32_t StartCrediblePair(int32_t transport, const std::string &address) override;
124     int32_t ConnectAllowedProfiles(const std::string &address) override;
125     int32_t DisconnectAllowedProfiles(const std::string &address) override;
126     int32_t SetDeviceCustomType(const std::string &address, int32_t deviceType) override;
127     int32_t GetRemoteDeviceInfo(const std::string &address,
128         std::shared_ptr<BluetoothRemoteDeviceInfo> &deviceInfo, int type) override;
129     void UpdateVirtualDevice(int32_t action, const std::string &address) override;
130     int32_t IsSupportVirtualAutoConnect(const std::string &address, bool &outSupport) override;
131     int32_t SetVirtualAutoConnectType(const std::string &address, int connType, int businessType) override;
132     int32_t SetFastScanLevel(int level) override;
133     void RegisterBtResourceManagerObserver(const sptr<IBluetoothResourceManagerObserver> &observer) override;
134     void DeregisterBtResourceManagerObserver(const sptr<IBluetoothResourceManagerObserver> &observer) override;
135     int32_t EnableBluetoothToRestrictMode(void) override;
136 
137 private:
138     static sptr<BluetoothHostServer> instance;
139     static std::mutex instanceLock;
140     int32_t transport_ = bluetooth::BT_TRANSPORT_BREDR;
141     bool Init();
142 
143     bool registeredToService_ = false;
144     ServiceRunningState state_ = ServiceRunningState::STATE_IDLE;
145 
146     int GetTransportByDeviceType(int32_t transport, const std::string &address);
147 
148     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothHostServer);
149     BLUETOOTH_DECLARE_IMPL();
150     bool IsBtEnabled();
151     std::mutex oblock_ {};
152 };
153 }  // namespace Bluetooth
154 }  // namespace OHOS
155 #endif  // OHOS_BLUETOOTH_STANDARD_HOST_SERVER_H
156