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