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