• 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_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 
133     if (observer == nullptr) {
134         HILOGE("observer is null");
135         return ERR_INVALID_VALUE;
136     }
137     if (pimpl == nullptr) {
138         HILOGE("pimpl is null");
139         return ERR_NO_INIT;
140     }
141     auto func = std::bind(&BluetoothPanServer::DeregisterObserver, this, std::placeholders::_1);
142     pimpl->observers_.Register(observer, func);
143     pimpl->advCallBack_.push_back(observer);
144     return ERR_OK;
145 }
146 
DeregisterObserver(const sptr<IBluetoothPanObserver> observer)147 ErrCode BluetoothPanServer::DeregisterObserver(const sptr<IBluetoothPanObserver> observer)
148 {
149     HILOGI("enter");
150     if (observer == nullptr) {
151         HILOGE("observer is null");
152         return ERR_INVALID_VALUE;
153     }
154     if (pimpl == nullptr) {
155         HILOGE("pimpl is null");
156         return ERR_NO_INIT;
157     }
158     for (auto iter = pimpl->advCallBack_.begin(); iter != pimpl->advCallBack_.end(); ++iter) {
159         if ((*iter)->AsObject() == observer->AsObject()) {
160             if (pimpl != nullptr) {
161                 pimpl->observers_.Deregister(*iter);
162                 pimpl->advCallBack_.erase(iter);
163                 break;
164             }
165         }
166     }
167     pimpl->panService_->DeregisterObserver(*pimpl->observerImp_.get());
168     return ERR_OK;
169 }
170 
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & result)171 int32_t BluetoothPanServer::GetDevicesByStates(const std::vector<int32_t> &states,
172     std::vector<BluetoothRawAddress>& result)
173 {
174     HILOGI("enter");
175     if (pimpl == nullptr || pimpl->panService_ == nullptr) {
176         HILOGI("not init.");
177         return BT_ERR_INTERNAL_ERROR;
178     }
179 
180     std::vector<bluetooth::RawAddress> serviceDeviceList = pimpl->panService_->GetDevicesByStates(states);
181     for (auto &device : serviceDeviceList) {
182         BluetoothRawAddress bluetoothDevice(device.GetAddress());
183         result.push_back(bluetoothDevice);
184     }
185     return NO_ERROR;
186 }
187 
GetDeviceState(const BluetoothRawAddress & device,int32_t & state)188 int32_t BluetoothPanServer::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
189 {
190     if (pimpl == nullptr || pimpl->panService_ == nullptr) {
191         HILOGI("not init.");
192         return BT_ERR_INTERNAL_ERROR;
193     }
194     state = pimpl->panService_->GetDeviceState(device);
195     HILOGI("addr:%{public}s, res:%{public}d", GET_ENCRYPT_ADDR(device), state);
196     return NO_ERROR;
197 }
198 
Disconnect(const BluetoothRawAddress & device)199 int32_t BluetoothPanServer::Disconnect(const BluetoothRawAddress &device)
200 {
201     HILOGI("addr:%{public}s", GET_ENCRYPT_ADDR(device));
202     if (!PermissionUtils::CheckSystemHapApp()) {
203         HILOGE("check system api failed.");
204         return BT_ERR_SYSTEM_PERMISSION_FAILED;
205     }
206     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
207         HILOGE("check permission failed");
208         return BT_ERR_PERMISSION_FAILED;
209     }
210     if (pimpl == nullptr || pimpl->panService_ == nullptr) {
211         HILOGI("not init.");
212         return BT_ERR_INTERNAL_ERROR;
213     }
214     return pimpl->panService_->Disconnect(device);
215 }
216 
SetTethering(const bool enable)217 int32_t BluetoothPanServer::SetTethering(const bool enable)
218 {
219     HILOGI("enable:%{public}d", enable);
220     if (!PermissionUtils::CheckSystemHapApp()) {
221         HILOGE("check system api failed.");
222         return BT_ERR_SYSTEM_PERMISSION_FAILED;
223     }
224     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
225         HILOGE("check permission failed");
226         return BT_ERR_PERMISSION_FAILED;
227     }
228     if (pimpl == nullptr || pimpl->panService_ == nullptr) {
229         HILOGI("not init.");
230         return BT_ERR_INTERNAL_ERROR;
231     }
232     return pimpl->panService_->SetTethering(enable);
233 }
234 
IsTetheringOn(bool & result)235 int32_t BluetoothPanServer::IsTetheringOn(bool& result)
236 {
237     if (!PermissionUtils::CheckSystemHapApp()) {
238         HILOGE("check system api failed.");
239         return BT_ERR_SYSTEM_PERMISSION_FAILED;
240     }
241     if (pimpl == nullptr || pimpl->panService_ == nullptr) {
242         HILOGI("not init.");
243         return BT_ERR_INTERNAL_ERROR;
244     }
245     result = pimpl->panService_->IsTetheringOn();
246     HILOGI("IsTetheringOn:%{public}d", result);
247     return NO_ERROR;
248 }
249 }  // namespace Bluetooth
250 }  // namespace OHOS
251