• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_fwk_profile_manager"
17 #endif
18 
19 #include "bluetooth_profile_manager.h"
20 
21 #include <atomic>
22 #include <mutex>
23 
24 #include "i_bluetooth_host.h"
25 #include "bluetooth_def.h"
26 #include "bluetooth_host.h"
27 #include "bluetooth_log.h"
28 #include "iservice_registry.h"
29 #include "system_ability_definition.h"
30 #include "bluetooth_no_destructor.h"
31 #include "ohos_bt_gatt.h"
32 
33 namespace OHOS {
34 namespace Bluetooth {
35 
36 sptr<BluetoothProfileManager::BluetoothSystemAbility> BluetoothProfileManager::bluetoothSystemAbility_ = nullptr;
37 
BluetoothProfileManager()38 BluetoothProfileManager::BluetoothProfileManager()
39 {
40     bluetoothSystemAbility_ = new BluetoothSystemAbility();
41     SubScribeBluetoothSystemAbility();
42 }
43 
~BluetoothProfileManager()44 BluetoothProfileManager::~BluetoothProfileManager()
45 {
46     UnSubScribeBluetoothSystemAbility();
47 }
48 
GetInstance()49 BluetoothProfileManager &BluetoothProfileManager::GetInstance()
50 {
51     static BluetoothNoDestructor<BluetoothProfileManager> instance;
52     return *instance;
53 }
54 
ClearSystemAbility()55 extern "C" __attribute__((destructor)) void BluetoothProfileManager::ClearSystemAbility()
56 {
57     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
58     CHECK_AND_RETURN_LOG(samgrProxy != nullptr, "[BLUETOOTH_PROFILE_MANAGER] failed to get samgrProxy");
59     int32_t ret = samgrProxy->UnSubscribeSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID, bluetoothSystemAbility_);
60     CHECK_AND_RETURN_LOG(ret == ERR_OK,
61         "[BLUETOOTH_PROFILE_MANAGER] Unsubscribe systemAbilityId: bluetooth service failed!");
62     bluetoothSystemAbility_ = nullptr;
63 }
64 
SubScribeBluetoothSystemAbility()65 void BluetoothProfileManager::SubScribeBluetoothSystemAbility()
66 {
67     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
68     CHECK_AND_RETURN_LOG(samgrProxy != nullptr, "[BLUETOOTH_PROFILE_MANAGER] failed to get samgrProxy");
69     int32_t ret = samgrProxy->SubscribeSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID, bluetoothSystemAbility_);
70     CHECK_AND_RETURN_LOG(ret == ERR_OK,
71         "[BLUETOOTH_PROFILE_MANAGER] subscribe systemAbilityId: bluetooth service failed!");
72 }
73 
UnSubScribeBluetoothSystemAbility()74 void BluetoothProfileManager::UnSubScribeBluetoothSystemAbility()
75 {
76     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
77     CHECK_AND_RETURN_LOG(samgrProxy != nullptr, "[BLUETOOTH_PROFILE_MANAGER] failed to get samgrProxy");
78     int32_t ret = samgrProxy->UnSubscribeSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID, bluetoothSystemAbility_);
79     CHECK_AND_RETURN_LOG(ret == ERR_OK,
80         "[BLUETOOTH_PROFILE_MANAGER] Unsubscribe systemAbilityId: bluetooth service failed!");
81 }
82 
GetHostRemote()83 sptr<IRemoteObject> BluetoothProfileManager::GetHostRemote()
84 {
85     sptr<IRemoteObject> value = nullptr;
86     if (profileRemoteMap_.Find(BLUETOOTH_HOST, value)) {
87         return value;
88     }
89     std::lock_guard<std::mutex> lock(needCheckBluetoothServiceOnMutex_);
90     if (!isNeedCheckBluetoothServiceOn_.load()) {
91         HILOGD("Bluetooth service is not start");
92         return value;
93     }
94     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
95     CHECK_AND_RETURN_LOG_RET(samgrProxy != nullptr, nullptr, "samgrProxy is nullptr");
96     auto object = samgrProxy->CheckSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
97     if (object == nullptr) {
98         HILOGE("object is nullptr");
99         isNeedCheckBluetoothServiceOn_ = false;
100         return nullptr;
101     }
102     CHECK_AND_RETURN_LOG_RET(object != nullptr, nullptr, "object is nullptr");
103     return object;
104 }
105 
GetProfileRemote(const std::string & objectName)106 sptr<IRemoteObject> BluetoothProfileManager::GetProfileRemote(const std::string &objectName)
107 {
108     std::lock_guard<std::mutex> lock(getProfileRemoteMutex_);
109     sptr<IRemoteObject> remote = nullptr;
110     if (profileRemoteMap_.Find(objectName, remote)) {
111         return remote;
112     } // SafeMap
113     auto hostRemote = GetHostRemote();
114     if (hostRemote == nullptr) {
115         HILOGD("hostRemote is nullptr");
116         return nullptr;
117     }
118     if (objectName == BLUETOOTH_HOST) {
119         remote = hostRemote;
120     } else {
121         sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
122         CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, nullptr, "hostProxy is nullptr");
123         if (objectName == BLE_ADVERTISER_SERVER || objectName == BLE_CENTRAL_MANAGER_SERVER) {
124             remote = hostProxy->GetBleRemote(objectName);
125         } else {
126             remote = hostProxy->GetProfile(objectName);
127         }
128     }
129     if (remote == nullptr) {
130         HILOGD("remote is nullptr");
131         return nullptr;
132     }
133     profileRemoteMap_.Insert(objectName, remote);
134     return remote;
135 }
136 
NotifyBluetoothStateChange(int32_t transport,int32_t status)137 void BluetoothProfileManager::NotifyBluetoothStateChange(int32_t transport, int32_t status)
138 {
139     if (transport == ADAPTER_BLE && status == STATE_TURN_OFF) {
140         profileIdFuncMap_.Iterate([this](const int32_t id, ProfileIdProperty &property) {
141             // true if *this stores a callcack function target.flase otherwise
142             if (property.functions.bluetoothTurnOffFunc) {
143                 property.functions.bluetoothTurnOffFunc();
144             }
145         });
146     }
147     if (transport == ADAPTER_BLE && status == STATE_TURN_ON) {
148         profileIdFuncMap_.Iterate([this](const int32_t id, ProfileIdProperty &property) {
149             if (property.functions.bleTurnOnFunc) {
150                 auto remote = GetProfileRemote(property.objectName);
151                 property.functions.bleTurnOnFunc(remote);
152             }
153         });
154     }
155     return;
156 }
157 
RunFuncWhenBluetoothServiceStarted()158 void BluetoothProfileManager::RunFuncWhenBluetoothServiceStarted()
159 {
160     profileIdFuncMap_.Iterate([this](const int32_t id, ProfileIdProperty &property) {
161         auto remote = GetProfileRemote(property.objectName);
162         if (property.functions.bluetoothLoadedfunc) {
163             property.functions.bluetoothLoadedfunc(remote);
164         }
165     });
166 }
167 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)168 void BluetoothProfileManager::BluetoothSystemAbility::OnAddSystemAbility(int32_t systemAbilityId,
169     const std::string &deviceId)
170 {
171     HILOGI("systemAbilityId:%{public}d", systemAbilityId);
172     switch (systemAbilityId) {
173         case BLUETOOTH_HOST_SYS_ABILITY_ID: {
174             BluetoothProfileManager::GetInstance().isBluetoothServiceOn_ = true;
175             {
176                 std::lock_guard<std::mutex> lock(
177                     BluetoothProfileManager::GetInstance().needCheckBluetoothServiceOnMutex_);
178                 BluetoothProfileManager::GetInstance().isNeedCheckBluetoothServiceOn_ = true;
179             }
180             BluetoothHost::GetDefaultHost().LoadSystemAbilitySuccess(nullptr);
181             BluetoothProfileManager::GetInstance().RunFuncWhenBluetoothServiceStarted();
182             break;
183         }
184         default:
185             HILOGE("unhandled sysabilityId:%{public}d", systemAbilityId);
186             break;
187     }
188     return;
189 }
190 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)191 void BluetoothProfileManager::BluetoothSystemAbility::OnRemoveSystemAbility(int32_t systemAbilityId,
192     const std::string &deviceId)
193 {
194     HILOGI("systemAbilityId:%{public}d", systemAbilityId);
195     switch (systemAbilityId) {
196         case BLUETOOTH_HOST_SYS_ABILITY_ID: {
197             HILOGD("Clear global variables first");
198             ClearGlobalResource();
199             BluetoothProfileManager::GetInstance().profileRemoteMap_.Clear();
200             BluetoothProfileManager::GetInstance().isBluetoothServiceOn_ = false;
201             {
202                 std::lock_guard<std::mutex> lock(
203                     BluetoothProfileManager::GetInstance().needCheckBluetoothServiceOnMutex_);
204                 BluetoothProfileManager::GetInstance().isNeedCheckBluetoothServiceOn_ = true;
205             }
206             BluetoothHost::GetDefaultHost().OnRemoveBluetoothSystemAbility();
207             break;
208         }
209         default:
210             HILOGE("unhandled sysabilityId:%{public}d", systemAbilityId);
211             break;
212     }
213     return;
214 }
215 
GetValidId()216 int32_t BluetoothProfileManager::GetValidId()
217 {
218     std::lock_guard<std::mutex> lock(idMutex_);
219     registerValidId_++;
220     return registerValidId_;
221 }
222 
RegisterFunc(const std::string & objectName,std::function<void (sptr<IRemoteObject>)> func)223 int32_t BluetoothProfileManager::RegisterFunc(const std::string &objectName,
224     std::function<void (sptr<IRemoteObject>)> func)
225 {
226     int32_t id = GetValidId();
227     ProfileIdProperty value;
228     ProfileIdProperty idProperties;
229     idProperties.objectName = objectName;
230     idProperties.functions.bluetoothLoadedfunc = func;
231     HILOGD("objectname: %{public}s, id: %{public}d", objectName.c_str(), id);
232     profileIdFuncMap_.Insert(id, idProperties);
233     if (isBluetoothServiceOn_) {
234         sptr<IRemoteObject> remote = GetProfileRemote(objectName);
235         CHECK_AND_RETURN_LOG_RET(remote != nullptr, id, "remote is nullptr"); // 蓝牙已开启,但getremote失败。
236         func(remote);
237     }
238     return id;
239 }
240 
RegisterFunc(const std::string & objectName,ProfileFunctions profileFunctions)241 int32_t BluetoothProfileManager::RegisterFunc(const std::string &objectName, ProfileFunctions profileFunctions)
242 {
243     int32_t id = GetValidId();
244     ProfileIdProperty value;
245     ProfileIdProperty idProperties;
246     idProperties.objectName = objectName;
247     idProperties.functions = profileFunctions;
248     HILOGI("objectname: %{public}s, id: %{public}d", objectName.c_str(), id);
249     profileIdFuncMap_.Insert(id, idProperties);
250     if (isBluetoothServiceOn_) {
251         sptr<IRemoteObject> remote = GetProfileRemote(objectName);
252         CHECK_AND_RETURN_LOG_RET(remote != nullptr, id, "remote is nullptr"); // 蓝牙已开启,但getremote失败。
253         if (profileFunctions.bluetoothLoadedfunc) {
254             profileFunctions.bluetoothLoadedfunc(remote);
255         }
256         if (profileFunctions.bleTurnOnFunc && IS_BLE_ENABLED()) {
257             profileFunctions.bleTurnOnFunc(remote);
258         }
259     }
260     return id;
261 }
262 
DeregisterFunc(int32_t id)263 void BluetoothProfileManager::DeregisterFunc(int32_t id)
264 {
265     HILOGI("id: %{public}d", id);
266     ProfileIdProperty value;
267     CHECK_AND_RETURN_LOG(profileIdFuncMap_.Find(id, value), "id is not exist");
268     profileIdFuncMap_.Erase(id);
269 }
270 
IsBluetoothServiceOn()271 bool BluetoothProfileManager::IsBluetoothServiceOn()
272 {
273     return isBluetoothServiceOn_.load();
274 }
275 } // namespace bluetooth
276 } // namespace OHOS