• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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_pbap_pse_server.h"
17 #include "i_bluetooth_pbap_pse.h"
18 #include "interface_adapter_manager.h"
19 #include "interface_profile_manager.h"
20 #include "interface_profile_pbap_pse.h"
21 #include "bluetooth_log.h"
22 #include "bluetooth_utils_server.h"
23 #include "permission_utils.h"
24 #include "remote_observer_list.h"
25 #include "bluetooth_def.h"
26 
27 namespace OHOS {
28 namespace Bluetooth {
29 using namespace OHOS::bluetooth;
30 
31 class PbapPseObserver : public bluetooth::IPbapPseObserver {
32 public:
33     PbapPseObserver() = default;
34     ~PbapPseObserver() = default;
OnServiceConnectionStateChanged(const bluetooth::RawAddress & remoteAddr,int state)35     void OnServiceConnectionStateChanged(const bluetooth::RawAddress& remoteAddr, int state) override
36     {
37         HILOGI("state: %{public}d", state);
38         observers_->ForEach([remoteAddr, state](IBluetoothPbapPseObserver* observer) {
39             observer->OnServiceConnectionStateChanged(remoteAddr, state);
40         });
41     }
OnServicePermission(const bluetooth::RawAddress & remoteAddr)42     void OnServicePermission(const bluetooth::RawAddress& remoteAddr) override
43     {
44         HILOGI("Enter!");
45         observers_->ForEach(
46             [remoteAddr](IBluetoothPbapPseObserver* observer) { observer->OnServicePermission(remoteAddr); });
47     }
OnServicePasswordRequired(const bluetooth::RawAddress & remoteAddr,const std::vector<uint8_t> & description,uint8_t charset,bool fullAccess)48     void OnServicePasswordRequired(const bluetooth::RawAddress& remoteAddr, const std::vector<uint8_t>& description,
49                                    uint8_t charset, bool fullAccess) override
50     {
51         HILOGI("charset: %{public}d, fullAccess: %{public}d", charset, fullAccess);
52         observers_->ForEach([remoteAddr, description, charset, fullAccess](IBluetoothPbapPseObserver* observer) {
53             observer->OnServicePasswordRequired(remoteAddr, description, charset, fullAccess);
54         });
55     }
SetObserver(RemoteObserverList<IBluetoothPbapPseObserver> * observers)56     void SetObserver(RemoteObserverList<IBluetoothPbapPseObserver>* observers)
57     {
58         observers_ = observers;
59     }
60 
61 private:
62     RemoteObserverList<IBluetoothPbapPseObserver>* observers_;
63     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(PbapPseObserver);
64 };
65 
66 struct BluetoothPbapPseServer::impl {
67     impl();
68     RemoteObserverList<IBluetoothPbapPseObserver> observers_;
69     std::unique_ptr<PbapPseObserver> observerImp_ = {std::make_unique<PbapPseObserver>()};
70     bluetooth::IProfilePbapPse* pbapPseService_{nullptr};
71     class SystemStateObserver;
72     std::unique_ptr<SystemStateObserver> systemStateObserver_{nullptr};
73 };
74 
75 class BluetoothPbapPseServer::impl::SystemStateObserver : public bluetooth::ISystemStateObserver {
76 public:
SystemStateObserver(BluetoothPbapPseServer::impl * impl)77     SystemStateObserver(BluetoothPbapPseServer::impl* impl) : impl_(impl) {};
78     ~SystemStateObserver() = default;
79 
OnSystemStateChange(const bluetooth::BTSystemState state)80     void OnSystemStateChange(const bluetooth::BTSystemState state) override
81     {
82         HILOGI("state=%{public}d", (int)state);
83         switch (state) {
84         case bluetooth::BTSystemState::ON: {
85             bluetooth::IProfileManager* serviceManager = bluetooth::IProfileManager::GetInstance();
86             if (serviceManager != nullptr) {
87                 bluetooth::IProfile* profileService =
88                     serviceManager->GetProfileService(bluetooth::PROFILE_NAME_PBAP_PSE);
89                 if (profileService) {
90                     impl_->pbapPseService_ = (bluetooth::IProfilePbapPse*)profileService;
91                     impl_->pbapPseService_->RegisterObserver(*impl_->observerImp_);
92                 } else {
93                     HILOGI("can't find PbapPseService from ProfileServiceManager!");
94                 }
95             } else {
96                 HILOGI("can't find ProfileServiceManager!");
97             }
98         } break;
99         case bluetooth::BTSystemState::OFF:
100             impl_->pbapPseService_ = nullptr;
101             break;
102         default:
103             break;
104         }
105     }
106 
107 private:
108     BluetoothPbapPseServer::impl* impl_;
109 };
110 
impl()111 BluetoothPbapPseServer::impl::impl()
112 {
113     systemStateObserver_ = std::make_unique<SystemStateObserver>(this);
114     bluetooth::IAdapterManager::GetInstance()->RegisterSystemStateObserver(*systemStateObserver_);
115 }
116 
BluetoothPbapPseServer()117 BluetoothPbapPseServer::BluetoothPbapPseServer()
118 {
119     pimpl = std::make_unique<impl>();
120     pimpl->observerImp_->SetObserver(&(pimpl->observers_));
121 
122     bluetooth::IProfileManager* serviceManager = bluetooth::IProfileManager::GetInstance();
123     if (serviceManager != nullptr) {
124         bluetooth::IProfile* profileService = serviceManager->GetProfileService(bluetooth::PROFILE_NAME_PBAP_PSE);
125         if (profileService) {
126             pimpl->pbapPseService_ = (bluetooth::IProfilePbapPse*)profileService;
127             pimpl->pbapPseService_->RegisterObserver(*pimpl->observerImp_);
128         } else {
129             HILOGI("can't find PbapPseService from ProfileServiceManager!");
130         }
131     } else {
132         HILOGI("can't find ProfileServiceManager!");
133     }
134 }
135 
~BluetoothPbapPseServer()136 BluetoothPbapPseServer::~BluetoothPbapPseServer() {}
137 
GetDeviceState(const BluetoothRawAddress & device)138 int BluetoothPbapPseServer::GetDeviceState(const BluetoothRawAddress &device)
139 {
140     HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
141     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
142         HILOGE("false, check permission failed");
143         return BT_FAILURE;
144     }
145     if (pimpl->pbapPseService_) {
146         return pimpl->pbapPseService_->GetDeviceState(device);
147     } else {
148         return -1;
149     }
150 }
151 
GetDevicesByStates(const::std::vector<int32_t> tmpStates,std::vector<BluetoothRawAddress> & rawDevices)152 void BluetoothPbapPseServer::GetDevicesByStates(
153     const ::std::vector<int32_t> tmpStates, std::vector<BluetoothRawAddress> &rawDevices)
154 {
155     HILOGI("Enter!");
156     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
157         HILOGE("false, check permission failed");
158     }
159     if (pimpl->pbapPseService_) {
160         auto devices = pimpl->pbapPseService_->GetDevicesByStates(tmpStates);
161         for (auto& device : devices) {
162             rawDevices.push_back(device);
163         }
164     }
165 }
166 
Disconnect(const BluetoothRawAddress & device)167 int BluetoothPbapPseServer::Disconnect(const BluetoothRawAddress& device)
168 {
169     HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
170     if (pimpl->pbapPseService_) {
171         return pimpl->pbapPseService_->Disconnect(device);
172     } else {
173         return -1;
174     }
175 }
176 
SetConnectionStrategy(const BluetoothRawAddress & device,int32_t strategy)177 int BluetoothPbapPseServer::SetConnectionStrategy(const BluetoothRawAddress& device, int32_t strategy)
178 {
179     HILOGI("device: %{public}s, strategy: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), strategy);
180     std::string addString = device.GetAddress();
181     bluetooth::RawAddress addr(addString);
182     if (pimpl->pbapPseService_ != nullptr) {
183         return pimpl->pbapPseService_->SetConnectionStrategy(addr, strategy);
184     } else {
185         return -1;
186     }
187 }
188 
GetConnectionStrategy(const BluetoothRawAddress & device)189 int BluetoothPbapPseServer::GetConnectionStrategy(const BluetoothRawAddress& device)
190 {
191     HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
192     std::string addString = device.GetAddress();
193     bluetooth::RawAddress addr(addString);
194     if (pimpl->pbapPseService_ != nullptr) {
195         return pimpl->pbapPseService_->GetConnectionStrategy(addr);
196     } else {
197         return -1;
198     }
199 }
200 
GrantPermission(const BluetoothRawAddress & device,bool allow,bool save)201 void BluetoothPbapPseServer::GrantPermission(const BluetoothRawAddress& device, bool allow, bool save)
202 {
203     HILOGI("addr: %{public}s, allow: %{public}d, save: %{public}d",
204         GetEncryptAddr((device).GetAddress()).c_str(), allow, save);
205     if (pimpl->pbapPseService_) {
206         pimpl->pbapPseService_->GrantPermission(device, allow, save);
207     }
208 }
209 
SetDevicePassword(const BluetoothRawAddress & device,const std::string & password,const std::string & userId)210 int BluetoothPbapPseServer::SetDevicePassword(const BluetoothRawAddress& device, const std::string &password,
211     const std::string &userId)
212 {
213     HILOGI("device: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
214     if (pimpl->pbapPseService_) {
215         std::string pwdTmp = password;
216         std::string usrIdTmp = userId;
217         return pimpl->pbapPseService_->SetDevicePassword(device, pwdTmp, usrIdTmp);
218     } else {
219         return -1;
220     }
221 }
222 
RegisterObserver(const sptr<IBluetoothPbapPseObserver> & observer)223 void BluetoothPbapPseServer::RegisterObserver(const sptr<IBluetoothPbapPseObserver>& observer)
224 {
225     HILOGI("Enter!");
226     if (!observer) {
227         HILOGE("observer is NULL.");
228     }
229     pimpl->observers_.Register(observer);
230 }
231 
DeregisterObserver(const sptr<IBluetoothPbapPseObserver> & observer)232 void BluetoothPbapPseServer::DeregisterObserver(const sptr<IBluetoothPbapPseObserver>& observer)
233 {
234     HILOGI("Enter!");
235     if (!observer) {
236         HILOGE("observer is NULL.");
237     }
238     pimpl->observers_.Deregister(observer);
239 }
240 
GetConnectedDevices()241 std::vector<BluetoothRawAddress> BluetoothPbapPseServer::GetConnectedDevices()
242 {
243     HILOGI("Enter!");
244     ::std::vector<int32_t> states{static_cast<int32_t>(BTConnectState::CONNECTED)};
245     std::vector<BluetoothRawAddress> rawDevices;
246     GetDevicesByStates(states, rawDevices);
247 
248     return rawDevices;
249 }
250 }  // namespace Bluetooth
251 }  // namespace OHOS