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_errorcode.h"
17 #include "bluetooth_pan_server.h"
18 #include "bluetooth_log.h"
19 #include "bluetooth_utils_server.h"
20 #include "interface_profile.h"
21 #include "interface_profile_pan.h"
22 #include "i_bluetooth_host_observer.h"
23 #include "permission_utils.h"
24 #include "remote_observer_list.h"
25 #include "hilog/log.h"
26
27 namespace OHOS {
28 namespace Bluetooth {
29 class BluetoothPanCallback : public bluetooth::IPanObserver {
30 public:
31 BluetoothPanCallback() = default;
32 ~BluetoothPanCallback() override = default;
33
OnConnectionStateChanged(const RawAddress & device,int state)34 void OnConnectionStateChanged(const RawAddress &device, int state) override
35 {
36 HILOGI("addr:%{public}s, state:%{public}d", GET_ENCRYPT_ADDR(device), state);
37 observers_->ForEach([device, state](sptr<IBluetoothPanObserver> observer) {
38 observer->OnConnectionStateChanged(device, state);
39 });
40 }
41
SetObserver(RemoteObserverList<IBluetoothPanObserver> * observers)42 void SetObserver(RemoteObserverList<IBluetoothPanObserver> *observers)
43 {
44 observers_ = observers;
45 }
46
47 private:
48 RemoteObserverList<IBluetoothPanObserver> *observers_;
49 };
50
51 struct BluetoothPanServer::impl {
52 impl();
53 ~impl();
54
55 /// sys state observer
56 class SystemStateObserver;
57 std::unique_ptr<SystemStateObserver> systemStateObserver_ = nullptr;
58
59 RemoteObserverList<IBluetoothPanObserver> observers_;
60 std::unique_ptr<BluetoothPanCallback> observerImp_ = std::make_unique<BluetoothPanCallback>();
61 IProfilePan *panService_ = nullptr;
62 std::vector<sptr<IBluetoothPanObserver>> advCallBack_;
63
GetServicePtrOHOS::Bluetooth::BluetoothPanServer::impl64 IProfilePan *GetServicePtr()
65 {
66 if (IProfileManager::GetInstance() == nullptr) {
67 return nullptr;
68 }
69 return static_cast<IProfilePan *>(
70 IProfileManager::GetInstance()->GetProfileService(PROFILE_NAME_PAN));
71 }
72 };
73
74 class BluetoothPanServer::impl::SystemStateObserver : public ISystemStateObserver {
75 public:
SystemStateObserver(BluetoothPanServer::impl * pimpl)76 explicit SystemStateObserver(BluetoothPanServer::impl *pimpl) : pimpl_(pimpl) {}
OnSystemStateChange(const BTSystemState state)77 void OnSystemStateChange(const BTSystemState state) override
78 {
79 switch (state) {
80 case BTSystemState::ON:
81 pimpl_->panService_ = pimpl_->GetServicePtr();
82 if (pimpl_->panService_ != nullptr) {
83 pimpl_->panService_->RegisterObserver(*pimpl_->observerImp_.get());
84 }
85 break;
86 case BTSystemState::OFF:
87 pimpl_->panService_ = nullptr;
88 break;
89 default:
90 break;
91 }
92 };
93
94 private:
95 BluetoothPanServer::impl *pimpl_ = nullptr;
96 };
97
impl()98 BluetoothPanServer::impl::impl()
99 {
100 HILOGI("starts");
101 }
102
~impl()103 BluetoothPanServer::impl::~impl()
104 {
105 HILOGI("starts");
106 }
107
BluetoothPanServer()108 BluetoothPanServer::BluetoothPanServer()
109 {
110 pimpl = std::make_unique<impl>();
111 pimpl->observerImp_->SetObserver(&(pimpl->observers_));
112 pimpl->systemStateObserver_ = std::make_unique<impl::SystemStateObserver>(pimpl.get());
113 IAdapterManager::GetInstance()->RegisterSystemStateObserver(*(pimpl->systemStateObserver_));
114
115 pimpl->panService_ = pimpl->GetServicePtr();
116 if (pimpl->panService_ != nullptr) {
117 pimpl->panService_->RegisterObserver(*pimpl->observerImp_.get());
118 }
119 }
120
~BluetoothPanServer()121 BluetoothPanServer::~BluetoothPanServer()
122 {
123 IAdapterManager::GetInstance()->DeregisterSystemStateObserver(*(pimpl->systemStateObserver_));
124 if (pimpl->panService_ != nullptr) {
125 pimpl->panService_->DeregisterObserver(*pimpl->observerImp_.get());
126 }
127 }
128
RegisterObserver(const sptr<IBluetoothPanObserver> observer)129 ErrCode BluetoothPanServer::RegisterObserver(const sptr<IBluetoothPanObserver> observer)
130 {
131 HILOGI("enter");
132 std::lock_guard<std::mutex> lock(oblock_);
133
134 if (observer == nullptr) {
135 HILOGE("observer is null");
136 return ERR_INVALID_VALUE;
137 }
138 if (pimpl == nullptr) {
139 HILOGE("pimpl is null");
140 return ERR_NO_INIT;
141 }
142 auto func = std::bind(&BluetoothPanServer::DeregisterObserver, this, std::placeholders::_1);
143 pimpl->observers_.Register(observer, func);
144 pimpl->advCallBack_.push_back(observer);
145 return ERR_OK;
146 }
147
DeregisterObserver(const sptr<IBluetoothPanObserver> observer)148 ErrCode BluetoothPanServer::DeregisterObserver(const sptr<IBluetoothPanObserver> observer)
149 {
150 HILOGI("enter");
151 std::lock_guard<std::mutex> lock(oblock_);
152 if (observer == nullptr) {
153 HILOGE("observer is null");
154 return ERR_INVALID_VALUE;
155 }
156 if (pimpl == nullptr) {
157 HILOGE("pimpl is null");
158 return ERR_NO_INIT;
159 }
160 for (auto iter = pimpl->advCallBack_.begin(); iter != pimpl->advCallBack_.end(); ++iter) {
161 if ((*iter)->AsObject() != nullptr && (*iter)->AsObject() == observer->AsObject()) {
162 if (pimpl != nullptr) {
163 pimpl->observers_.Deregister(*iter);
164 pimpl->advCallBack_.erase(iter);
165 break;
166 }
167 }
168 }
169 pimpl->panService_->DeregisterObserver(*pimpl->observerImp_.get());
170 return ERR_OK;
171 }
172
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & result)173 int32_t BluetoothPanServer::GetDevicesByStates(const std::vector<int32_t> &states,
174 std::vector<BluetoothRawAddress>& result)
175 {
176 HILOGI("enter");
177 if (pimpl == nullptr || pimpl->panService_ == nullptr) {
178 HILOGI("not init.");
179 return BT_ERR_INTERNAL_ERROR;
180 }
181
182 std::vector<bluetooth::RawAddress> serviceDeviceList = pimpl->panService_->GetDevicesByStates(states);
183 for (auto &device : serviceDeviceList) {
184 BluetoothRawAddress bluetoothDevice(device.GetAddress());
185 result.push_back(bluetoothDevice);
186 }
187 return NO_ERROR;
188 }
189
GetDeviceState(const BluetoothRawAddress & device,int32_t & state)190 int32_t BluetoothPanServer::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
191 {
192 if (pimpl == nullptr || pimpl->panService_ == nullptr) {
193 HILOGI("not init.");
194 return BT_ERR_INTERNAL_ERROR;
195 }
196 state = pimpl->panService_->GetDeviceState(device);
197 HILOGI("addr:%{public}s, res:%{public}d", GET_ENCRYPT_ADDR(device), state);
198 return NO_ERROR;
199 }
200
Disconnect(const BluetoothRawAddress & device)201 int32_t BluetoothPanServer::Disconnect(const BluetoothRawAddress &device)
202 {
203 HILOGI("addr:%{public}s", GET_ENCRYPT_ADDR(device));
204 if (!PermissionUtils::CheckSystemHapApp()) {
205 HILOGE("check system api failed.");
206 return BT_ERR_SYSTEM_PERMISSION_FAILED;
207 }
208 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
209 HILOGE("check permission failed");
210 return BT_ERR_PERMISSION_FAILED;
211 }
212 if (pimpl == nullptr || pimpl->panService_ == nullptr) {
213 HILOGI("not init.");
214 return BT_ERR_INTERNAL_ERROR;
215 }
216 return pimpl->panService_->Disconnect(device);
217 }
218
SetTethering(const bool enable)219 int32_t BluetoothPanServer::SetTethering(const bool enable)
220 {
221 HILOGI("enable:%{public}d", enable);
222 if (!PermissionUtils::CheckSystemHapApp()) {
223 HILOGE("check system api failed.");
224 return BT_ERR_SYSTEM_PERMISSION_FAILED;
225 }
226 if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
227 HILOGE("check permission failed");
228 return BT_ERR_PERMISSION_FAILED;
229 }
230 if (pimpl == nullptr || pimpl->panService_ == nullptr) {
231 HILOGI("not init.");
232 return BT_ERR_INTERNAL_ERROR;
233 }
234 return pimpl->panService_->SetTethering(enable);
235 }
236
IsTetheringOn(bool & result)237 int32_t BluetoothPanServer::IsTetheringOn(bool& result)
238 {
239 if (!PermissionUtils::CheckSystemHapApp()) {
240 HILOGE("check system api failed.");
241 return BT_ERR_SYSTEM_PERMISSION_FAILED;
242 }
243 if (pimpl == nullptr || pimpl->panService_ == nullptr) {
244 HILOGI("not init.");
245 return BT_ERR_INTERNAL_ERROR;
246 }
247 result = pimpl->panService_->IsTetheringOn();
248 HILOGI("IsTetheringOn:%{public}d", result);
249 return NO_ERROR;
250 }
251 } // namespace Bluetooth
252 } // namespace OHOS
253