1 /*
2 * Copyright (C) 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 #include "bluetooth_proxy_manager.h"
17
18 #include "bluetooth_ble_central_manager.h"
19 #include "bluetooth_def.h"
20 #include "bluetooth_log.h"
21 #include "i_bluetooth_ble_central_manager.h"
22 #include "i_bluetooth_host.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace Bluetooth {
GetInstance()28 BluetoothProxyManager& BluetoothProxyManager::GetInstance()
29 {
30 static BluetoothProxyManager instance;
31 return instance;
32 }
33
ProxyUid(int32_t uid,bool isProxy)34 bool BluetoothProxyManager::ProxyUid(int32_t uid, bool isProxy)
35 {
36 HILOGI("ProxyUid start. uid:%{public}d , isProxy:%{public}d", uid, isProxy);
37 if (!GetBleCentralManagerProxy()) {
38 HILOGE("GetBleCentralManagerProxy failed.");
39 return false;
40 }
41 return proxy_->ProxyUid(uid, isProxy);
42 }
43
ResetAllProxy()44 bool BluetoothProxyManager::ResetAllProxy()
45 {
46 HILOGI("ResetAllProxy start.");
47 if (!GetBleCentralManagerProxy()) {
48 HILOGE("GetBleCentralManagerProxy failed.");
49 return false;
50 }
51 return proxy_->ResetAllProxy();
52 }
53
GetBleCentralManagerProxy()54 bool BluetoothProxyManager::GetBleCentralManagerProxy()
55 {
56 if (proxy_) {
57 return true;
58 }
59
60 std::lock_guard<std::mutex> lock(mutex_);
61 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
62 if (!samgr) {
63 HILOGE("samgr is null");
64 return false;
65 }
66
67 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
68 if (!hostRemote) {
69 HILOGE("hostRemote is null");
70 return false;
71 }
72 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
73 if (!hostProxy) {
74 HILOGE("hostProxy is null");
75 return false;
76 }
77 sptr<IRemoteObject> remote = hostProxy->GetBleRemote(BLE_CENTRAL_MANAGER_SERVER);
78 if (!remote) {
79 HILOGE("remote is null");
80 return false;
81 }
82 proxy_ = iface_cast<IBluetoothBleCentralManager>(remote);
83 if (!proxy_ || !proxy_->AsObject()) {
84 HILOGE("proxy_ is null");
85 return false;
86 }
87 recipient_ = new (std::nothrow) BleCentralManagerDeathRecipient(*this);
88 if (!recipient_) {
89 HILOGE("recipient_ is null");
90 proxy_ = nullptr;
91 return false;
92 }
93 proxy_->AsObject()->AddDeathRecipient(recipient_);
94 return true;
95 }
96
ResetClient()97 void BluetoothProxyManager::ResetClient()
98 {
99 if (proxy_ && proxy_->AsObject()) {
100 proxy_->AsObject()->RemoveDeathRecipient(recipient_);
101 }
102 proxy_ = nullptr;
103 }
104
BleCentralManagerDeathRecipient(BluetoothProxyManager & bleProxyManager)105 BluetoothProxyManager::BleCentralManagerDeathRecipient::BleCentralManagerDeathRecipient(
106 BluetoothProxyManager &bleProxyManager) : bleProxyManager_(bleProxyManager) {}
107
~BleCentralManagerDeathRecipient()108 BluetoothProxyManager::BleCentralManagerDeathRecipient::~BleCentralManagerDeathRecipient() {}
109
OnRemoteDied(const wptr<IRemoteObject> & object)110 void BluetoothProxyManager::BleCentralManagerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
111 {
112 bleProxyManager_.ResetClient();
113 }
114 } // namespace Bluetooth
115 } // namespace OHOS