1/* 2 * Copyright (C) 2023 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 BLUETOOTH_DIALOG_H 17#define BLUETOOTH_DIALOG_H 18 19#include <string> 20#include "safe_map.h" 21#include "singleton.h" 22 23#include "i_bluetooth_host.h" 24#include "bluetooth_errorcode.h" 25#include "bluetooth_log.h" 26#include "iremote_broker.h" 27#include "iservice_registry.h" 28#include "system_ability_definition.h" 29#include "system_ability_status_change_stub.h" 30#include <mutex> 31 32namespace OHOS { 33namespace Bluetooth { 34constexpr const char *BLUETOOTH_HOST = "BluetoothHost"; 35const int32_t BEGIN_ID = 0; 36// It is recommended to ues one of the between bluetoothLoadedfunc and bleTurnOnFunc 37struct ProfileFunctions { 38 std::function<void(sptr<IRemoteObject>)> bluetoothLoadedfunc {}; 39 std::function<void(sptr<IRemoteObject>)> bleTurnOnFunc {}; 40 std::function<void(void)> bluetoothTurnOffFunc {}; 41}; 42struct ProfileIdProperty { 43 ProfileFunctions functions; 44 std::string objectName = ""; 45}; 46class BluetoothProfileManager { 47public: 48 BluetoothProfileManager(); 49 ~BluetoothProfileManager(); 50 /** 51 * @brief Get the Remote of the Profile 52 * 53 * @param objectName the objectName of profile 54 * 55 * @return Returns the Remote of the Profile. 56 */ 57 sptr<IRemoteObject> GetProfileRemote(const std::string &objectName); 58 /** 59 * @brief register function for profile to get proxy when profile is init 60 * 61 * @param objectName the objectName of profile 62 * @param func the function for profile to register 63 * 64 * @return Returns the id of the Profile. 65 */ 66 int32_t RegisterFunc(const std::string &objectName, std::function<void (sptr<IRemoteObject>)> func); 67 /** 68 * @brief register function for profile to get proxy when profile is init 69 * 70 * @param objectName the objectName of profile 71 * @param ProfileFunctions the function for profile to register 72 * 73 * @return Returns the id of the Profile. 74 */ 75 int32_t RegisterFunc(const std::string &objectName, ProfileFunctions profileFunctions); 76 /** 77 * @brief Deregister function for profile, ensure that there is a deregister after register 78 * 79 * @param id the id of profile 80 */ 81 void DeregisterFunc(int32_t id); 82 /** 83 * @brief Notify Bluetooth State Change 84 */ 85 void NotifyBluetoothStateChange(int32_t transport, int32_t status); 86 /** 87 * @brief check bluetooth service is on or not 88 */ 89 bool IsBluetoothServiceOn(); 90 91 static BluetoothProfileManager &GetInstance(); 92 93private: 94 class BluetoothSystemAbility : public SystemAbilityStatusChangeStub { 95 public: 96 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 97 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 98 }; 99 100 void SubScribeBluetoothSystemAbility(); 101 void UnSubScribeBluetoothSystemAbility(); 102 void RunFuncWhenBluetoothServiceStarted(); 103 sptr<IRemoteObject> GetHostRemote(); 104 int32_t GetValidId(); 105 106 SafeMap<std::string, sptr<IRemoteObject>> profileRemoteMap_; 107 SafeMap<int32_t, ProfileIdProperty> profileIdFuncMap_; 108 std::atomic_bool isBluetoothServiceOn_ = false; 109 std::atomic_bool isNeedCheckBluetoothServiceOn_ = true; 110 sptr<BluetoothSystemAbility> bluetoothSystemAbility_ = nullptr; 111 int32_t registerValidId_ = BEGIN_ID; 112 std::mutex idMutex_; 113 std::mutex getProfileRemoteMutex_; 114 std::mutex needCheckBluetoothServiceOnMutex_; 115}; 116template <typename T> 117sptr<T> GetRemoteProxy(const std::string &objectName) 118{ 119 return iface_cast<T>(BluetoothProfileManager::GetInstance().GetProfileRemote(objectName)); 120}; 121} // namespace bluetooth 122} // namespace OHOS 123#endif // BLUETOOTH_PROFILE_MANAGER_H