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 28 namespace OHOS { 29 namespace Bluetooth { 30 enum class ServiceRunningState { STATE_IDLE, STATE_RUNNING }; 31 32 class BluetoothHostServer : public SystemAbility, public BluetoothHostStub { 33 DECLARE_SYSTEM_ABILITY(BluetoothHostServer); 34 35 public: 36 explicit BluetoothHostServer(); 37 ~BluetoothHostServer() override; 38 39 static sptr<BluetoothHostServer> GetInstance(); 40 41 void OnStart() override; 42 void OnStop() override; 43 44 void RegisterObserver(const sptr<IBluetoothHostObserver> &observer) override; 45 void DeregisterObserver(const sptr<IBluetoothHostObserver> &observer) override; 46 int32_t EnableBt() override; 47 int32_t DisableBt() override; 48 int32_t GetBtState(int32_t &state) override; 49 bool BluetoothFactoryReset() override; 50 int32_t GetDeviceType(int32_t transport, const std::string &address) override; 51 52 std::string GetLocalAddress() override; 53 sptr<IRemoteObject> GetProfile(const std::string &name) override; 54 sptr<IRemoteObject> GetBleRemote(const std::string &name) override; 55 QueryServiceState()56 ServiceRunningState QueryServiceState() const 57 { 58 return state_; 59 } 60 61 bool Start(); 62 void Stop(); 63 int32_t DisableBle() override; 64 int32_t EnableBle() override; 65 bool IsBrEnabled() override; 66 bool IsBleEnabled() override; 67 std::vector<uint32_t> GetProfileList() override; 68 int32_t GetMaxNumConnectedAudioDevices() override; 69 int32_t GetBtConnectionState(int32_t &state) override; 70 int32_t GetBtProfileConnState(uint32_t profileId, int &state) override; 71 int32_t GetLocalDeviceClass() override; 72 bool SetLocalDeviceClass(const int32_t &deviceClass) override; 73 int32_t GetLocalName(std::string &name) override; 74 int32_t SetLocalName(const std::string &name) override; 75 int32_t GetBtScanMode(int32_t &scanMode) override; 76 int32_t SetBtScanMode(int32_t mode, int32_t duration) override; 77 int32_t GetBondableMode(const int32_t transport) override; 78 bool SetBondableMode(int32_t transport, int32_t mode) override; 79 int32_t StartBtDiscovery() override; 80 int32_t CancelBtDiscovery() override; 81 bool IsBtDiscovering(const int32_t transport) override; 82 long GetBtDiscoveryEndMillis() override; 83 int32_t GetPairedDevices(const int32_t transport, std::vector<BluetoothRawAddress> &pairedAddr) override; 84 int32_t RemovePair(const int32_t transport, const sptr<BluetoothRawAddress> &device) override; 85 bool RemoveAllPairs() override; 86 void RegisterRemoteDeviceObserver(const sptr<IBluetoothRemoteDeviceObserver> &observer) override; 87 void DeregisterRemoteDeviceObserver(const sptr<IBluetoothRemoteDeviceObserver> &observer) override; 88 int32_t GetBleMaxAdvertisingDataLength() override; 89 int32_t GetPhonebookPermission(const std::string &address) override; 90 bool SetPhonebookPermission(const std::string &address, int32_t permission) override; 91 int32_t GetMessagePermission(const std::string &address) override; 92 bool SetMessagePermission(const std::string &address, int32_t permission) override; 93 int32_t GetPowerMode(const std::string &address) override; 94 int32_t GetDeviceName(int32_t transport, const std::string &address, std::string &name) override; 95 std::string GetDeviceAlias(const std::string &address) override; 96 bool SetDeviceAlias(const std::string &address, const std::string &aliasName) override; 97 int32_t GetDeviceBatteryLevel(const std::string &address) override; 98 int32_t GetPairState(int32_t transport, const std::string &address) override; 99 int32_t StartPair(int32_t transport, const std::string &address) override; 100 bool CancelPairing(int32_t transport, const std::string &address) override; 101 bool IsBondedFromLocal(int32_t transport, const std::string &address) override; 102 bool IsAclConnected(int32_t transport, const std::string &address) override; 103 bool IsAclEncrypted(int32_t transport, const std::string &address) override; 104 int32_t GetDeviceClass(const std::string &address, int32_t &cod) override; 105 int32_t SetDevicePin(const std::string &address, const std::string &pin) override; 106 int32_t SetDevicePairingConfirmation(int32_t transport, const std::string &address, bool accept) override; 107 bool SetDevicePasskey(int32_t transport, const std::string &address, int32_t passkey, bool accept) override; 108 bool PairRequestReply(int32_t transport, const std::string &address, bool accept) override; 109 bool ReadRemoteRssiValue(const std::string &address) override; 110 void RegisterBleAdapterObserver(const sptr<IBluetoothHostObserver> &observer) override; 111 void DeregisterBleAdapterObserver(const sptr<IBluetoothHostObserver> &observer) override; 112 void RegisterBlePeripheralCallback(const sptr<IBluetoothBlePeripheralObserver> &observer) override; 113 void DeregisterBlePeripheralCallback(const sptr<IBluetoothBlePeripheralObserver> &observer) override; 114 void GetLocalSupportedUuids(std::vector<std::string> &uuids) override; 115 int32_t GetDeviceUuids(const std::string &address, std::vector<std::string> &uuids) override; 116 int32_t GetLocalProfileUuids(std::vector<std::string> &uuids) override; 117 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 118 int32_t SetFastScan(bool isEnable) override; 119 int32_t GetRandomAddress(const std::string &realAddr, std::string &randomAddr) override; 120 int32_t SyncRandomAddress(const std::string &realAddr, const std::string &randomAddr) override; 121 122 private: 123 static sptr<BluetoothHostServer> instance; 124 static std::mutex instanceLock; 125 int32_t transport_ = bluetooth::BT_TRANSPORT_BREDR; 126 bool Init(); 127 128 bool registeredToService_ = false; 129 ServiceRunningState state_ = ServiceRunningState::STATE_IDLE; 130 131 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(BluetoothHostServer); 132 BLUETOOTH_DECLARE_IMPL(); 133 bool IsBtEnabled(); 134 std::mutex oblock_ {}; 135 }; 136 } // namespace Bluetooth 137 } // namespace OHOS 138 #endif // OHOS_BLUETOOTH_STANDARD_HOST_SERVER_H 139