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 const std::string BLUETOOTH_HOST = "BluetoothHost";
35 // It is recommended to ues one of the between bluetoothLoadedfunc and bleTurnOnFunc
36 struct ProfileFunctions {
37 std::function<void(sptr<IRemoteObject>)> bluetoothLoadedfunc {};
38 std::function<void(sptr<IRemoteObject>)> bleTurnOnFunc {};
39 std::function<void(void)> bluetoothTurnOffFunc {};
40 };
41 struct ProfileIdProperty {
42 ProfileFunctions functions;
43 std::string objectName = "";
44 };
45 class BluetoothProfileManager {
46 DECLARE_DELAYED_SINGLETON(BluetoothProfileManager);
47
48 public:
49 /**
50 * @brief Get the Remote of the Profile
51 *
52 * @param objectName the objectName of profile
53 *
54 * @return Returns the Remote of the Profile.
55 */
56 sptr<IRemoteObject> GetProfileRemote(const std::string &objectName);
57 /**
58 * @brief register function for profile to get proxy when profile is init
59 *
60 * @param objectName the objectName of profile
61 * @param func the function for profile to register
62 *
63 * @return Returns the id of the Profile.
64 */
65 int32_t RegisterFunc(const std::string &objectName, std::function<void (sptr<IRemoteObject>)> func);
66 /**
67 * @brief register function for profile to get proxy when profile is init
68 *
69 * @param objectName the objectName of profile
70 * @param ProfileFunctions the function for profile to register
71 *
72 * @return Returns the id of the Profile.
73 */
74 int32_t RegisterFunc(const std::string &objectName, ProfileFunctions profileFunctions);
75 /**
76 * @brief Deregister function for profile, ensure that there is a deregister after register
77 *
78 * @param id the id of profile
79 */
80 void DeregisterFunc(int32_t id);
81 /**
82 * @brief Notify Bluetooth State Change
83 */
84 void NotifyBluetoothStateChange(int32_t transport, int32_t status);
85
86 private:
87 class BluetoothSystemAbility : public SystemAbilityStatusChangeStub {
88 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
89 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
90 };
91
92 void SubScribeBluetoothSystemAbility();
93 void UnSubScribeBluetoothSystemAbility();
94 void RunFuncWhenBluetoothServiceStarted();
95 sptr<IRemoteObject> GetHostRemote();
96 void GetValidId();
97
98 SafeMap<std::string, sptr<IRemoteObject>> profileRemoteMap_;
99 SafeMap<int32_t, ProfileIdProperty> profileIdFuncMap_;
100 std::atomic_bool isBluetoothServiceOn_ = false;
101 sptr<BluetoothSystemAbility> bluetoothSystemAbility_ = nullptr;
102 std::mutex getProfileRemoteMutex_;
103 };
104 template <typename T>
GetRemoteProxy(const std::string & objectName)105 sptr<T> GetRemoteProxy(const std::string &objectName)
106 {
107 return iface_cast<T>(DelayedSingleton<BluetoothProfileManager>::GetInstance()->GetProfileRemote(objectName));
108 };
109 } // namespace bluetooth
110 } // namespace OHOS
111 #endif // BLUETOOTH_PROFILE_MANAGER_H