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
32 namespace OHOS {
33 namespace Bluetooth {
34 constexpr const char *BLUETOOTH_HOST = "BluetoothHost";
35 const int32_t BEGIN_ID = 0;
36 // It is recommended to ues one of the between bluetoothLoadedfunc and bleTurnOnFunc
37 struct ProfileFunctions {
38 std::function<void(sptr<IRemoteObject>)> bluetoothLoadedfunc {};
39 std::function<void(sptr<IRemoteObject>)> bleTurnOnFunc {};
40 std::function<void(void)> bluetoothTurnOffFunc {};
41 };
42 struct ProfileIdProperty {
43 ProfileFunctions functions;
44 std::string objectName = "";
45 };
46 class BluetoothProfileManager {
47 public:
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 * @brief clear resource when dlclose
92 */
93 static void ClearSystemAbility();
94
95 static BluetoothProfileManager &GetInstance();
96
97 private:
98 class BluetoothSystemAbility : public SystemAbilityStatusChangeStub {
99 public:
100 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
101 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
102 };
103
104 void SubScribeBluetoothSystemAbility();
105 void UnSubScribeBluetoothSystemAbility();
106 void RunFuncWhenBluetoothServiceStarted();
107 sptr<IRemoteObject> GetHostRemote();
108 int32_t GetValidId();
109
110 SafeMap<std::string, sptr<IRemoteObject>> profileRemoteMap_;
111 SafeMap<int32_t, ProfileIdProperty> profileIdFuncMap_;
112 std::atomic_bool isBluetoothServiceOn_ = false;
113 std::atomic_bool isNeedCheckBluetoothServiceOn_ = true;
114 static sptr<BluetoothSystemAbility> bluetoothSystemAbility_;
115 int32_t registerValidId_ = BEGIN_ID;
116 std::mutex idMutex_;
117 std::mutex getProfileRemoteMutex_;
118 std::mutex needCheckBluetoothServiceOnMutex_;
119 };
120 template <typename T>
GetRemoteProxy(const std::string & objectName)121 sptr<T> GetRemoteProxy(const std::string &objectName)
122 {
123 return iface_cast<T>(BluetoothProfileManager::GetInstance().GetProfileRemote(objectName));
124 };
125 } // namespace bluetooth
126 } // namespace OHOS
127 #endif // BLUETOOTH_PROFILE_MANAGER_H