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