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 <vector>
17 #include "bluetooth_def.h"
18 #include "bluetooth_hfp_hf_server.h"
19 #include "bluetooth_log.h"
20 #include "bluetooth_utils_server.h"
21 #include "interface_profile_hfp_hf.h"
22 #include "interface_profile_manager.h"
23 #include "interface_profile.h"
24 #include "interface_adapter_manager.h"
25 #include "permission_utils.h"
26 #include "remote_observer_list.h"
27
28 using namespace OHOS::bluetooth;
29
30 namespace OHOS {
31 namespace Bluetooth {
32 class HfpHfServerObserver : public HfpHfServiceObserver {
33 public:
34 HfpHfServerObserver() = default;
35 ~HfpHfServerObserver() override = default;
36
OnConnectionStateChanged(const RawAddress & device,int state)37 void OnConnectionStateChanged(const RawAddress& device, int state) override
38 {
39 HILOGI("addr: %{public}s, state: %{public}d", GET_ENCRYPT_ADDR(device), state);
40 observers_->ForEach([device, state](IBluetoothHfpHfObserver* observer) {
41 observer->OnConnectionStateChanged(device, state);
42 });
43 }
44
OnScoStateChanged(const RawAddress & device,int state)45 void OnScoStateChanged(const RawAddress& device, int state) override
46 {
47 HILOGI("addr: %{public}s, state: %{public}d", GET_ENCRYPT_ADDR(device), state);
48 observers_->ForEach([device, state](IBluetoothHfpHfObserver* observer) {
49 observer->OnScoStateChanged(device, state);
50 });
51 }
52
OnCallChanged(const RawAddress & device,const HandsFreeUnitCalls & call)53 void OnCallChanged(const RawAddress& device, const HandsFreeUnitCalls& call) override
54 {
55 HILOGI("addr: %{public}s", GET_ENCRYPT_ADDR(device));
56 observers_->ForEach([device, call](IBluetoothHfpHfObserver* observer) {
57 observer->OnCallChanged(device, call);
58 });
59 }
60
OnSignalStrengthChanged(const RawAddress & device,int signal)61 void OnSignalStrengthChanged(const RawAddress& device, int signal) override
62 {
63 HILOGI("addr: %{public}s, signal: %{public}d", GET_ENCRYPT_ADDR(device), signal);
64 observers_->ForEach([device, signal](IBluetoothHfpHfObserver* observer) {
65 observer->OnSignalStrengthChanged(device, signal);
66 });
67 }
68
OnRegistrationStatusChanged(const RawAddress & device,int status)69 void OnRegistrationStatusChanged(const RawAddress& device, int status) override
70 {
71 HILOGI("addr: %{public}s, status: %{public}d", GET_ENCRYPT_ADDR(device), status);
72 observers_->ForEach([device, status](IBluetoothHfpHfObserver* observer) {
73 observer->OnRegistrationStatusChanged(device, status);
74 });
75 }
76
OnRoamingStatusChanged(const RawAddress & device,int status)77 void OnRoamingStatusChanged(const RawAddress& device, int status) override
78 {
79 HILOGI("addr: %{public}s, status: %{public}d", GET_ENCRYPT_ADDR(device), status);
80 observers_->ForEach([device, status](IBluetoothHfpHfObserver* observer) {
81 observer->OnRoamingStatusChanged(device, status);
82 });
83 }
84
OnOperatorSelectionChanged(const RawAddress & device,const std::string & name)85 void OnOperatorSelectionChanged(const RawAddress& device, const std::string& name) override
86 {
87 HILOGI("addr: %{public}s, name: %{public}s", GET_ENCRYPT_ADDR(device), name.c_str());
88 observers_->ForEach([device, name](IBluetoothHfpHfObserver* observer) {
89 observer->OnOperatorSelectionChanged(device, name);
90 });
91 }
92
OnSubscriberNumberChanged(const RawAddress & device,const std::string & number)93 void OnSubscriberNumberChanged(const RawAddress& device, const std::string& number) override
94 {
95 HILOGI("addr: %{public}s, number: %{public}s", GET_ENCRYPT_ADDR(device), number.c_str());
96 observers_->ForEach([device, number](IBluetoothHfpHfObserver* observer) {
97 observer->OnSubscriberNumberChanged(device, number);
98 });
99 }
100
OnVoiceRecognitionStatusChanged(const RawAddress & device,int status)101 void OnVoiceRecognitionStatusChanged(const RawAddress& device, int status) override
102 {
103 HILOGI("addr: %{public}s, status: %{public}d", GET_ENCRYPT_ADDR(device), status);
104 observers_->ForEach([device, status](IBluetoothHfpHfObserver* observer) {
105 observer->OnVoiceRecognitionStatusChanged(device, status);
106 });
107 }
108
OnInBandRingToneChanged(const RawAddress & device,int status)109 void OnInBandRingToneChanged(const RawAddress& device, int status) override
110 {
111 HILOGI("addr: %{public}s, status: %{public}d", GET_ENCRYPT_ADDR(device), status);
112 }
113
SetObserver(RemoteObserverList<IBluetoothHfpHfObserver> * observers)114 void SetObserver(RemoteObserverList<IBluetoothHfpHfObserver>* observers)
115 {
116 observers_ = observers;
117 }
118
119 private:
120 RemoteObserverList<IBluetoothHfpHfObserver>* observers_;
121 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(HfpHfServerObserver);
122 };
123
124 struct BluetoothHfpHfServer::impl {
125 RemoteObserverList<IBluetoothHfpHfObserver> observers_;
126 std::unique_ptr<HfpHfServerObserver> observerImp_{std::make_unique<HfpHfServerObserver>()};
127 IProfileHfpHf* HfpHfService_ = nullptr;
128
129 class HfpHfSystemObserver : public ISystemStateObserver {
130 public:
HfpHfSystemObserver(BluetoothHfpHfServer::impl * pimpl)131 explicit HfpHfSystemObserver(BluetoothHfpHfServer::impl* pimpl) : pimpl_(pimpl) {};
OnSystemStateChange(const BTSystemState state)132 void OnSystemStateChange(const BTSystemState state) override
133 {
134 HILOGI("state: %{public}d", state);
135 IProfileManager* serviceMgr = IProfileManager::GetInstance();
136 switch (state) {
137 case BTSystemState::ON:
138 if (serviceMgr != nullptr) {
139 pimpl_->HfpHfService_ = (IProfileHfpHf*)serviceMgr->GetProfileService(PROFILE_NAME_HFP_HF);
140 if (pimpl_->HfpHfService_ != nullptr) {
141 pimpl_->HfpHfService_->RegisterObserver(*pimpl_->observerImp_);
142 }
143 }
144 break;
145 case BTSystemState::OFF:
146 if (serviceMgr != nullptr) {
147 pimpl_->HfpHfService_ = (IProfileHfpHf*)serviceMgr->GetProfileService(PROFILE_NAME_HFP_HF);
148 if (pimpl_->HfpHfService_ != nullptr) {
149 pimpl_->HfpHfService_->DeregisterObserver(*pimpl_->observerImp_);
150 }
151 }
152 pimpl_->HfpHfService_ = nullptr;
153 break;
154 default:
155 break;
156 }
157 };
158
159 private:
160 BluetoothHfpHfServer::impl* pimpl_;
161 };
162
163 std::unique_ptr<HfpHfSystemObserver> HfpHfSystemObserver_;
164 };
165
BluetoothHfpHfServer()166 BluetoothHfpHfServer::BluetoothHfpHfServer()
167 {
168 HILOGI("Enter!");
169 pimpl = std::make_unique<impl>();
170 pimpl->observerImp_->SetObserver(&(pimpl->observers_));
171 pimpl->HfpHfSystemObserver_ = std::make_unique<impl::HfpHfSystemObserver>(pimpl.get());
172 IAdapterManager::GetInstance()->RegisterSystemStateObserver(*(pimpl->HfpHfSystemObserver_));
173
174 IProfileManager* serviceMgr = IProfileManager::GetInstance();
175 if (serviceMgr != nullptr) {
176 pimpl->HfpHfService_ = (IProfileHfpHf*)serviceMgr->GetProfileService(PROFILE_NAME_HFP_HF);
177 if (pimpl->HfpHfService_ != nullptr) {
178 pimpl->HfpHfService_->RegisterObserver(*pimpl->observerImp_);
179 }
180 }
181 }
182
~BluetoothHfpHfServer()183 BluetoothHfpHfServer::~BluetoothHfpHfServer()
184 {
185 HILOGI("Enter!");
186 IAdapterManager::GetInstance()->DeregisterSystemStateObserver(*(pimpl->HfpHfSystemObserver_));
187 if (pimpl->HfpHfService_ != nullptr) {
188 pimpl->HfpHfService_->DeregisterObserver(*pimpl->observerImp_);
189 }
190 }
191
ConnectSco(const BluetoothRawAddress & device)192 bool BluetoothHfpHfServer::ConnectSco(const BluetoothRawAddress &device) {
193 HILOGI("addr: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
194 RawAddress addr(device.GetAddress());
195 if (pimpl->HfpHfService_ != nullptr) {
196 return pimpl->HfpHfService_->ConnectSco(addr);
197 }
198 return false;
199 }
200
DisconnectSco(const BluetoothRawAddress & device)201 bool BluetoothHfpHfServer::DisconnectSco(const BluetoothRawAddress &device) {
202 HILOGI("addr: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
203 RawAddress addr(device.GetAddress());
204 if (pimpl->HfpHfService_ != nullptr) {
205 return pimpl->HfpHfService_->DisconnectSco(addr);
206 }
207 return false;
208 }
209
GetDevicesByStates(const std::vector<int> & states,std::vector<BluetoothRawAddress> & devices)210 int BluetoothHfpHfServer::GetDevicesByStates(const std::vector<int> &states,
211 std::vector<BluetoothRawAddress> &devices) {
212 HILOGI("Enter!");
213 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
214 HILOGE("false, check permission failed");
215 return BT_FAILURE;
216 }
217 std::vector<int> tmpStates;
218 for (int32_t state : states) {
219 HILOGI("state = %{public}d", state);
220 tmpStates.push_back((int)state);
221 }
222 std::vector<RawAddress> rawDevices;
223
224 if (pimpl->HfpHfService_ != nullptr) {
225 rawDevices = pimpl->HfpHfService_->GetDevicesByStates(tmpStates);
226 } else {
227 return BT_FAILURE;
228 }
229 for (RawAddress device : rawDevices) {
230 devices.push_back(BluetoothRawAddress(device));
231 }
232 return BT_SUCCESS;
233 }
234
GetDeviceState(const BluetoothRawAddress & device)235 int BluetoothHfpHfServer::GetDeviceState(const BluetoothRawAddress &device) {
236 HILOGI("addr: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
237 if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
238 HILOGE("false, check permission failed");
239 return BT_FAILURE;
240 }
241 RawAddress addr(device.GetAddress());
242 if (pimpl->HfpHfService_ != nullptr) {
243 return pimpl->HfpHfService_->GetDeviceState(addr);
244 }
245 return BT_FAILURE;
246 }
247
GetScoState(const BluetoothRawAddress & device)248 int BluetoothHfpHfServer::GetScoState(const BluetoothRawAddress &device) {
249 HILOGI("addr: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
250 RawAddress addr(device.GetAddress());
251 if (pimpl->HfpHfService_ != nullptr) {
252 return pimpl->HfpHfService_->GetScoState(addr);
253 }
254 return BT_FAILURE;
255 }
256
SendDTMFTone(const BluetoothRawAddress & device,uint8_t code)257 bool BluetoothHfpHfServer::SendDTMFTone(const BluetoothRawAddress &device, uint8_t code) {
258 HILOGI("addr: %{public}s, code: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), code);
259 RawAddress addr(device.GetAddress());
260 if (pimpl->HfpHfService_ != nullptr) {
261 return pimpl->HfpHfService_->SendDTMFTone(addr, code);
262 }
263 return false;
264 }
265
Connect(const BluetoothRawAddress & device)266 int BluetoothHfpHfServer::Connect(const BluetoothRawAddress &device) {
267 HILOGI("addr: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
268 RawAddress addr(device.GetAddress());
269 if (pimpl->HfpHfService_ != nullptr) {
270 return pimpl->HfpHfService_->Connect(addr);
271 }
272 return BT_FAILURE;
273 }
274
Disconnect(const BluetoothRawAddress & device)275 int BluetoothHfpHfServer::Disconnect(const BluetoothRawAddress &device) {
276 HILOGI("addr: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
277 RawAddress addr(device.GetAddress());
278 if (pimpl->HfpHfService_ != nullptr) {
279 return pimpl->HfpHfService_->Disconnect(addr);
280 }
281 return BT_FAILURE;
282 }
283
OpenVoiceRecognition(const BluetoothRawAddress & device)284 bool BluetoothHfpHfServer::OpenVoiceRecognition(const BluetoothRawAddress &device) {
285 HILOGI("addr: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
286 RawAddress addr(device.GetAddress());
287 if (pimpl->HfpHfService_ != nullptr) {
288 return pimpl->HfpHfService_->OpenVoiceRecognition(addr);
289 }
290 return false;
291 }
292
CloseVoiceRecognition(const BluetoothRawAddress & device)293 bool BluetoothHfpHfServer::CloseVoiceRecognition(const BluetoothRawAddress &device) {
294 HILOGI("addr: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
295 RawAddress addr(device.GetAddress());
296 if (pimpl->HfpHfService_ != nullptr) {
297 return pimpl->HfpHfService_->CloseVoiceRecognition(addr);
298 }
299 return false;
300 }
301
GetCurrentCallList(const BluetoothRawAddress & device,std::vector<BluetoothHfpHfCall> & calls)302 int BluetoothHfpHfServer::GetCurrentCallList(const BluetoothRawAddress &device,
303 std::vector<BluetoothHfpHfCall> &calls) {
304 HILOGI("addr: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
305 std::vector<HandsFreeUnitCalls> callVector;
306 RawAddress addr(device.GetAddress());
307 if (pimpl->HfpHfService_ != nullptr) {
308 callVector = pimpl->HfpHfService_->GetCurrentCallList(addr);
309 }
310 for (HandsFreeUnitCalls call : callVector) {
311 calls.push_back(BluetoothHfpHfCall(call));
312 }
313 return BT_FAILURE;
314 }
315
AcceptIncomingCall(const BluetoothRawAddress & device,int flag)316 bool BluetoothHfpHfServer::AcceptIncomingCall(const BluetoothRawAddress &device, int flag) {
317 HILOGI("addr: %{public}s, flag: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), flag);
318 RawAddress addr(device.GetAddress());
319 if (pimpl->HfpHfService_ != nullptr) {
320 return pimpl->HfpHfService_->AcceptIncomingCall(addr, (int)flag);
321 }
322 return false;
323 }
324
HoldActiveCall(const BluetoothRawAddress & device)325 bool BluetoothHfpHfServer::HoldActiveCall(const BluetoothRawAddress &device) {
326 HILOGI("addr: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
327 RawAddress addr(device.GetAddress());
328 if (pimpl->HfpHfService_ != nullptr) {
329 return pimpl->HfpHfService_->HoldActiveCall(addr);
330 }
331 return false;
332 }
333
RejectIncomingCall(const BluetoothRawAddress & device)334 bool BluetoothHfpHfServer::RejectIncomingCall(const BluetoothRawAddress &device) {
335 HILOGI("addr: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
336 RawAddress addr(device.GetAddress());
337 if (pimpl->HfpHfService_ != nullptr) {
338 return pimpl->HfpHfService_->RejectIncomingCall(addr);
339 }
340 return false;
341 }
342
SendKeyPressed(const BluetoothRawAddress & device)343 bool BluetoothHfpHfServer::SendKeyPressed(const BluetoothRawAddress &device) {
344 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
345 RawAddress addr(device.GetAddress());
346 if (pimpl->HfpHfService_ != nullptr) {
347 return pimpl->HfpHfService_->SendKeyPressed(addr);
348 }
349 return false;
350 }
351
HandleIncomingCall(const BluetoothRawAddress & device,int flag)352 bool BluetoothHfpHfServer::HandleIncomingCall(const BluetoothRawAddress &device, int flag)
353 {
354 HILOGI("addr: %{public}s, flag: %{public}d", GetEncryptAddr((device).GetAddress()).c_str(), flag);
355 RawAddress addr(device.GetAddress());
356 if (pimpl->HfpHfService_ != nullptr) {
357 return pimpl->HfpHfService_->HandleIncomingCall(addr, flag);
358 }
359 return false;
360 }
361
DialLastNumber(const BluetoothRawAddress & device)362 bool BluetoothHfpHfServer::DialLastNumber(const BluetoothRawAddress &device)
363 {
364 HILOGI("addr: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
365 RawAddress addr(device.GetAddress());
366 if (pimpl->HfpHfService_ != nullptr) {
367 return pimpl->HfpHfService_->DialLastNumber(addr);
368 }
369 return false;
370 }
371
DialMemory(const BluetoothRawAddress & device,int index)372 bool BluetoothHfpHfServer::DialMemory(const BluetoothRawAddress &device, int index)
373 {
374 HILOGI("addr: %{public}s, index: %{public}d",
375 GetEncryptAddr((device).GetAddress()).c_str(), index);
376 RawAddress addr(device.GetAddress());
377 if (pimpl->HfpHfService_ != nullptr) {
378 return pimpl->HfpHfService_->DialMemory(addr, index);
379 }
380 return false;
381 }
382
HandleMultiCall(const BluetoothRawAddress & device,int flag,int index)383 bool BluetoothHfpHfServer::HandleMultiCall(const BluetoothRawAddress &device, int flag, int index)
384 {
385 HILOGI("addr: %{public}s, flag: %{public}d, index: %{public}d",
386 GetEncryptAddr((device).GetAddress()).c_str(), flag, index);
387 RawAddress addr(device.GetAddress());
388 if (pimpl->HfpHfService_ != nullptr) {
389 return pimpl->HfpHfService_->HandleMultiCall(addr, flag, index);
390 }
391 return false;
392 }
393
SendVoiceTag(const BluetoothRawAddress & device,int index)394 bool BluetoothHfpHfServer::SendVoiceTag(const BluetoothRawAddress &device, int index)
395 {
396 HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
397 RawAddress addr(device.GetAddress());
398 if (pimpl->HfpHfService_ != nullptr) {
399 return pimpl->HfpHfService_->SendVoiceTag(addr, index);
400 }
401 return false;
402 }
403
FinishActiveCall(const BluetoothRawAddress & device,const BluetoothHfpHfCall & call)404 bool BluetoothHfpHfServer::FinishActiveCall(const BluetoothRawAddress &device,
405 const BluetoothHfpHfCall &call)
406 {
407 HILOGI("addr: %{public}s", GetEncryptAddr((device).GetAddress()).c_str());
408 RawAddress addr(device.GetAddress());
409 if (pimpl->HfpHfService_ != nullptr) {
410 return pimpl->HfpHfService_->FinishActiveCall(addr, call);
411 }
412 return false;
413 }
414
StartDial(const BluetoothRawAddress & device,const std::string & number,BluetoothHfpHfCall & call)415 int BluetoothHfpHfServer::StartDial(const BluetoothRawAddress &device, const std::string &number,
416 BluetoothHfpHfCall &call)
417 {
418 HILOGI("addr: %{public}s, number: %{public}s",
419 GetEncryptAddr((device).GetAddress()).c_str(), number.c_str());
420 std::optional<HandsFreeUnitCalls> ret;
421 HandsFreeUnitCalls calls;
422 RawAddress addr(device.GetAddress());
423 if (pimpl->HfpHfService_ != nullptr) {
424 ret = pimpl->HfpHfService_->StartDial(addr, number);
425 }
426 if (ret == std::nullopt) {
427 call = calls;
428 return BT_FAILURE;
429 } else {
430 call = *ret;
431 return BT_SUCCESS;
432 }
433 }
434
RegisterObserver(const sptr<IBluetoothHfpHfObserver> & observer)435 void BluetoothHfpHfServer::RegisterObserver(const sptr<IBluetoothHfpHfObserver> &observer)
436 {
437 HILOGI("Enter!");
438 auto func = std::bind(&BluetoothHfpHfServer::DeregisterObserver, this, std::placeholders::_1);
439 pimpl->observers_.Register(observer, func);
440 }
441
DeregisterObserver(const sptr<IBluetoothHfpHfObserver> & observer)442 void BluetoothHfpHfServer::DeregisterObserver(const sptr<IBluetoothHfpHfObserver> &observer)
443 {
444 HILOGI("Enter!");
445 pimpl->observers_.Deregister(observer);
446 }
447 } // namespace Bluetooth
448 } // namespace OHOS
449