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 #include <list>
16 
17 #include "bluetooth_def.h"
18 #include "bluetooth_errorcode.h"
19 #include "bluetooth_hfp_ag_server.h"
20 #include "bluetooth_hitrace.h"
21 #include "bluetooth_log.h"
22 #include "bluetooth_utils_server.h"
23 #include "hisysevent.h"
24 #include "interface_profile_hfp_ag.h"
25 #include "interface_profile_manager.h"
26 #include "interface_profile.h"
27 #include "interface_adapter_manager.h"
28 #include "remote_observer_list.h"
29 #include "permission_utils.h"
30 
31 
32 namespace OHOS {
33 namespace Bluetooth {
34 using namespace OHOS::bluetooth;
35 
36 class HfpAgServerObserver : public HfpAgServiceObserver {
37 public:
38     HfpAgServerObserver() = default;
39     ~HfpAgServerObserver() override = default;
40 
OnConnectionStateChanged(const RawAddress & device,int state)41     void OnConnectionStateChanged(const RawAddress& device, int state) override
42     {
43         HILOGI("device:%{public}s, state:%{public}d", GET_ENCRYPT_ADDR(device), state);
44         if (state == static_cast<int>(BTConnectState::CONNECTED) ||
45             state == static_cast<int>(BTConnectState::DISCONNECTED)) {
46             HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::BT_SERVICE, "HFP_CONNECTED_STATE",
47                 OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC, "STATE", state);
48         }
49         observers_->ForEach([device, state](IBluetoothHfpAgObserver* observer) {
50             observer->OnConnectionStateChanged(device, state,
51                 static_cast<uint32_t>(ConnChangeCause::CONNECT_CHANGE_COMMON_CAUSE));
52         });
53     }
54 
OnScoStateChanged(const RawAddress & device,int state,int reason)55     void OnScoStateChanged(const RawAddress& device, int state, int reason) override
56     {
57         HILOGI("device:%{public}s, state:%{public}d, reason:%{public}d", GET_ENCRYPT_ADDR(device), state, reason);
58         observers_->ForEach([device, state, reason](IBluetoothHfpAgObserver* observer) {
59             observer->OnScoStateChanged(device, state, reason);
60         });
61     }
62 
OnActiveDeviceChanged(const RawAddress & device)63     void OnActiveDeviceChanged(const RawAddress& device) override
64     {
65         HILOGI("device:%{public}s", GET_ENCRYPT_ADDR(device));
66         observers_->ForEach([device](IBluetoothHfpAgObserver* observer) {
67             observer->OnActiveDeviceChanged(device);
68         });
69     }
70 
OnHfEnhancedDriverSafetyChanged(const RawAddress & device,int indValue)71     void OnHfEnhancedDriverSafetyChanged(const RawAddress& device, int indValue) override
72     {
73         HILOGI("device:%{public}s, indValue:%{public}d", GET_ENCRYPT_ADDR(device), indValue);
74         observers_->ForEach([device, indValue](IBluetoothHfpAgObserver* observer) {
75             observer->OnHfEnhancedDriverSafetyChanged(device, indValue);
76         });
77     }
78 
OnHfpStackChanged(const RawAddress & device,int action)79     void OnHfpStackChanged(const RawAddress &device, int action) override
80     {
81         HILOGI("addr: %{public}s, action: %{public}d", GET_ENCRYPT_ADDR(device), action);
82     }
83 
SetObserver(RemoteObserverList<IBluetoothHfpAgObserver> * observers)84     void SetObserver(RemoteObserverList<IBluetoothHfpAgObserver>* observers)
85     {
86         observers_ = observers;
87     }
88 
89 private:
90     RemoteObserverList<IBluetoothHfpAgObserver>* observers_;
91     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(HfpAgServerObserver);
92 };
93 
94 struct BluetoothHfpAgServer::impl {
95     RemoteObserverList<IBluetoothHfpAgObserver> observers_;
96     std::unique_ptr<HfpAgServerObserver> observerImp_{std::make_unique<HfpAgServerObserver>()};
97     IProfileHfpAg* HfpAgService_ = nullptr;
98 
99     class HfpAgSystemObserver : public ISystemStateObserver {
100     public:
HfpAgSystemObserver(BluetoothHfpAgServer::impl * pimpl)101         explicit HfpAgSystemObserver(BluetoothHfpAgServer::impl* pimpl) : pimpl_(pimpl) {};
OnSystemStateChange(const BTSystemState state)102         void OnSystemStateChange(const BTSystemState state) override
103         {
104             HILOGD("state:%{public}d", state);
105             IProfileManager* serviceMgr = IProfileManager::GetInstance();
106             switch (state) {
107             case BTSystemState::ON:
108                 if (serviceMgr != nullptr) {
109                     pimpl_->HfpAgService_ = (IProfileHfpAg*)serviceMgr->GetProfileService(PROFILE_NAME_HFP_AG);
110                     if (pimpl_->HfpAgService_ != nullptr) {
111                         pimpl_->HfpAgService_->RegisterObserver(*pimpl_->observerImp_);
112                     }
113                 }
114                 break;
115             case BTSystemState::OFF:
116                 if (serviceMgr != nullptr) {
117                     pimpl_->HfpAgService_ = (IProfileHfpAg*)serviceMgr->GetProfileService(PROFILE_NAME_HFP_AG);
118                     if (pimpl_->HfpAgService_ != nullptr) {
119                         pimpl_->HfpAgService_->DeregisterObserver(*pimpl_->observerImp_);
120                     }
121                 }
122                 pimpl_->HfpAgService_ = nullptr;
123                 break;
124             default:
125                 break;
126             }
127         };
128 
129     private:
130         BluetoothHfpAgServer::impl* pimpl_;
131     };
132 
133     std::unique_ptr<HfpAgSystemObserver> HfpAgSystemObserver_;
134 };
135 
136 
BluetoothHfpAgServer()137 BluetoothHfpAgServer::BluetoothHfpAgServer()
138 {
139     HILOGD("Enter!");
140     pimpl = std::make_unique<impl>();
141     pimpl->observerImp_->SetObserver(&(pimpl->observers_));
142     pimpl->HfpAgSystemObserver_ = std::make_unique<impl::HfpAgSystemObserver>(pimpl.get());
143     IAdapterManager::GetInstance()->RegisterSystemStateObserver(*(pimpl->HfpAgSystemObserver_));
144 
145     IProfileManager* serviceMgr = IProfileManager::GetInstance();
146     if (serviceMgr != nullptr) {
147         pimpl->HfpAgService_ = (IProfileHfpAg*)serviceMgr->GetProfileService(PROFILE_NAME_HFP_AG);
148         if (pimpl->HfpAgService_ != nullptr) {
149             pimpl->HfpAgService_->RegisterObserver(*pimpl->observerImp_);
150         }
151     }
152 }
153 
~BluetoothHfpAgServer()154 BluetoothHfpAgServer::~BluetoothHfpAgServer()
155 {
156     HILOGD("Enter!");
157     IAdapterManager::GetInstance()->DeregisterSystemStateObserver(*(pimpl->HfpAgSystemObserver_));
158     if (pimpl->HfpAgService_ != nullptr) {
159         pimpl->HfpAgService_->DeregisterObserver(*pimpl->observerImp_);
160     }
161 }
162 
GetConnectDevices(std::vector<BluetoothRawAddress> & devices)163 int32_t BluetoothHfpAgServer::GetConnectDevices(std::vector<BluetoothRawAddress> &devices)
164 {
165     HILOGI("Enter!");
166     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
167         HILOGE("GetConnectDevices() false, check permission failed");
168         return BT_ERR_PERMISSION_FAILED;
169     }
170     std::list<RawAddress> deviceList;
171     if (pimpl->HfpAgService_  != nullptr) {
172         deviceList = pimpl->HfpAgService_->GetConnectDevices();
173     } else {
174         return BT_ERR_INTERNAL_ERROR;
175     }
176     for (RawAddress device : deviceList) {
177         devices.push_back(BluetoothRawAddress(device));
178     }
179     return NO_ERROR;
180 }
181 
GetDevicesByStates(const std::vector<int> & states,std::vector<BluetoothRawAddress> & devices)182 int BluetoothHfpAgServer::GetDevicesByStates(const std::vector<int> &states, std::vector<BluetoothRawAddress> &devices)
183 {
184     std::vector<int> tmpStates;
185     for (int32_t state : states) {
186         HILOGI("state = %{public}d", state);
187         tmpStates.push_back((int)state);
188     }
189     std::vector<RawAddress> rawDevices;
190     if (pimpl->HfpAgService_ != nullptr) {
191         rawDevices = pimpl->HfpAgService_->GetDevicesByStates(tmpStates);
192     } else {
193         return BT_ERR_INTERNAL_ERROR;
194     }
195     for (RawAddress device : rawDevices) {
196         devices.push_back(BluetoothRawAddress(device));
197     }
198     return NO_ERROR;
199 }
200 
GetDeviceState(const BluetoothRawAddress & device,int32_t & state)201 int32_t BluetoothHfpAgServer::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
202 {
203     if (PermissionUtils::VerifyUseBluetoothPermission() == PERMISSION_DENIED) {
204         HILOGE("GetDeviceState() false, check permission failed");
205         return BT_ERR_PERMISSION_FAILED;
206     }
207     RawAddress addr(device.GetAddress());
208     if (pimpl->HfpAgService_) {
209         state = pimpl->HfpAgService_->GetDeviceState(addr);
210         HILOGI("state:%{public}d", state);
211     } else {
212         return BT_ERR_INTERNAL_ERROR;
213     }
214     return NO_ERROR;
215 }
216 
Connect(const BluetoothRawAddress & device)217 int32_t BluetoothHfpAgServer::Connect(const BluetoothRawAddress &device)
218 {
219     HILOGI("target device:%{public}s()", GET_ENCRYPT_ADDR(device));
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("Connect error, check permission failed");
226         return BT_ERR_PERMISSION_FAILED;
227     }
228     RawAddress addr(device.GetAddress());
229     if (pimpl->HfpAgService_ != nullptr) {
230         OHOS::Bluetooth::BluetoothHiTrace::BluetoothStartAsyncTrace("HFP_AG_CONNECT", 1);
231         int32_t result = pimpl->HfpAgService_->Connect(addr);
232         OHOS::Bluetooth::BluetoothHiTrace::BluetoothFinishAsyncTrace("HFP_AG_CONNECT", 1);
233         return result;
234     }
235     return BT_ERR_INTERNAL_ERROR;
236 }
237 
Disconnect(const BluetoothRawAddress & device)238 int32_t BluetoothHfpAgServer::Disconnect(const BluetoothRawAddress &device)
239 {
240     HILOGI("target device:%{public}s()", GET_ENCRYPT_ADDR(device));
241     if (!PermissionUtils::CheckSystemHapApp()) {
242         HILOGE("check system api failed.");
243         return BT_ERR_SYSTEM_PERMISSION_FAILED;
244     }
245     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
246         HILOGE("Disconnect error, check permission failed");
247         return BT_ERR_PERMISSION_FAILED;
248     }
249     RawAddress addr(device.GetAddress());
250     if (pimpl->HfpAgService_ != nullptr) {
251         return pimpl->HfpAgService_->Disconnect(addr);
252     }
253     return BT_ERR_INTERNAL_ERROR;
254 }
255 
GetScoState(const BluetoothRawAddress & device)256 int BluetoothHfpAgServer::GetScoState(const BluetoothRawAddress &device)
257 {
258     HILOGI("Enter!");
259     RawAddress addr(device.GetAddress());
260     if (pimpl->HfpAgService_ != nullptr) {
261         return pimpl->HfpAgService_->GetScoState(addr);
262     }
263     return BT_FAILURE;
264 }
265 
ConnectSco()266 bool BluetoothHfpAgServer::ConnectSco()
267 {
268     HILOGI("Enter!");
269     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
270         HILOGE("error, check permission failed");
271         return false;
272     }
273     if (pimpl->HfpAgService_ != nullptr) {
274         return pimpl->HfpAgService_->ConnectSco();
275     }
276     return false;
277 }
278 
DisconnectSco()279 bool BluetoothHfpAgServer::DisconnectSco()
280 {
281     HILOGI("Enter!");
282     if (pimpl->HfpAgService_ != nullptr) {
283         return pimpl->HfpAgService_->DisconnectSco();
284     }
285     return false;
286 }
287 
PhoneStateChanged(BluetoothPhoneState & phoneState)288 void BluetoothHfpAgServer::PhoneStateChanged(BluetoothPhoneState &phoneState)
289 {
290     HILOGI("numActive:%{public}d, numHeld:%{public}d, callState:%{public}d, type:%{public}d",
291         phoneState.GetActiveNum(), phoneState.GetHeldNum(), phoneState.GetCallState(), phoneState.GetCallType());
292     if (pimpl->HfpAgService_ != nullptr) {
293         pimpl->HfpAgService_->PhoneStateChanged(phoneState);
294     }
295 }
296 
ClccResponse(int index,int direction,int status,int mode,bool mpty,const std::string & number,int type)297 void BluetoothHfpAgServer::ClccResponse(int index, int direction, int status, int mode, bool mpty,
298     const std::string &number, int type)
299 {
300     HILOGI("index:%{public}d, direction:%{public}d, status:%{public}d, mode:%{public}d, mpty:%{public}d,"
301         "number:%{public}s, type:%{public}d", index, direction, status, mode, mpty, number.c_str(), type);
302     if (pimpl->HfpAgService_ != nullptr) {
303         pimpl->HfpAgService_->ClccResponse(index, direction, status, mode, mpty, number, type);
304     }
305 }
306 
OpenVoiceRecognition(const BluetoothRawAddress & device)307 bool BluetoothHfpAgServer::OpenVoiceRecognition(const BluetoothRawAddress &device)
308 {
309     HILOGI("target device:%{public}s()", GET_ENCRYPT_ADDR(device));
310     if (PermissionUtils::VerifyDiscoverBluetoothPermission() == PERMISSION_DENIED) {
311         HILOGE("error, check permission failed");
312         return false;
313     }
314     RawAddress addr(device.GetAddress());
315     if (pimpl->HfpAgService_ != nullptr) {
316         return pimpl->HfpAgService_->OpenVoiceRecognition(addr);
317     }
318     return false;
319 }
320 
CloseVoiceRecognition(const BluetoothRawAddress & device)321 bool BluetoothHfpAgServer::CloseVoiceRecognition(const BluetoothRawAddress &device)
322 {
323     HILOGI("target device:%{public}s()", GET_ENCRYPT_ADDR(device));
324      RawAddress addr(device.GetAddress());
325     if (pimpl->HfpAgService_ != nullptr) {
326         return pimpl->HfpAgService_->CloseVoiceRecognition(addr);
327     }
328     return false;
329 }
330 
SetActiveDevice(const BluetoothRawAddress & device)331 bool BluetoothHfpAgServer::SetActiveDevice(const BluetoothRawAddress &device)
332 {
333     HILOGI("target device:%{public}s()", GET_ENCRYPT_ADDR(device));
334     RawAddress addr(device.GetAddress());
335     if (pimpl->HfpAgService_ ) {
336         return pimpl->HfpAgService_->SetActiveDevice(addr);
337     }
338     return false;
339 }
340 
IntoMock(const BluetoothRawAddress & device,int state)341 bool BluetoothHfpAgServer::IntoMock(const BluetoothRawAddress &device, int state) {
342     HILOGI("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
343     if (pimpl->HfpAgService_ ) {
344         return pimpl->HfpAgService_ ->IntoMock(state);
345     }
346     return false;
347 }
348 
SendNoCarrier(const BluetoothRawAddress & device)349 bool BluetoothHfpAgServer::SendNoCarrier(const BluetoothRawAddress &device) {
350     HILOGD("[%{public}s]: %{public}s(): Enter!", __FILE__, __FUNCTION__);
351     RawAddress addr(device.GetAddress());
352     if (pimpl->HfpAgService_ ) {
353         return pimpl->HfpAgService_ ->SendNoCarrier(addr);
354     }
355     return false;
356 }
357 
GetActiveDevice()358 std::string BluetoothHfpAgServer::GetActiveDevice()
359 {
360     std::string dev = "";
361     if (pimpl->HfpAgService_ != nullptr) {
362         dev = pimpl->HfpAgService_->GetActiveDevice();
363     }
364     HILOGI("active dev:%{public}s()", GetEncryptAddr(dev).c_str());
365     return dev;
366 }
367 
IsInbandRingingEnabled(bool & isEnabled)368 int BluetoothHfpAgServer::IsInbandRingingEnabled(bool &isEnabled)
369 {
370     return true;
371 }
372 
CallDetailsChanged(int callId,int callState)373 void BluetoothHfpAgServer::CallDetailsChanged(int callId, int callState)
374 {
375     HILOGI("enter");
376 }
377 
IsVgsSupported(const BluetoothRawAddress & device,bool & isSupported)378 int32_t BluetoothHfpAgServer::IsVgsSupported(const BluetoothRawAddress &device, bool &isSupported)
379 {
380     return BT_ERR_API_NOT_SUPPORT;
381 }
382 
RegisterObserver(const sptr<IBluetoothHfpAgObserver> & observer)383 void BluetoothHfpAgServer::RegisterObserver(const sptr<IBluetoothHfpAgObserver> &observer)
384 {
385     HILOGD("Enter!");
386     if (observer == nullptr) {
387         HILOGE("observer is null");
388         return ;
389     }
390     if (pimpl == nullptr) {
391         HILOGE("pimpl is null");
392         return ;
393     }
394     auto func = std::bind(&BluetoothHfpAgServer::DeregisterObserver, this, std::placeholders::_1);
395     pimpl->observers_.Register(observer, func);
396 }
397 
DeregisterObserver(const sptr<IBluetoothHfpAgObserver> & observer)398 void BluetoothHfpAgServer::DeregisterObserver(const sptr<IBluetoothHfpAgObserver> &observer)
399 {
400     HILOGD("Enter!");
401     if (observer == nullptr) {
402         HILOGE("observer is null");
403         return ;
404     }
405     if (pimpl == nullptr) {
406         HILOGE("pimpl is null");
407         return ;
408     }
409     pimpl->observers_.Deregister(observer);
410 }
411 
SetConnectStrategy(const BluetoothRawAddress & device,int strategy)412 int BluetoothHfpAgServer::SetConnectStrategy(const BluetoothRawAddress &device, int strategy)
413 {
414     HILOGI("target device:%{public}s()", GET_ENCRYPT_ADDR(device));
415     if (!PermissionUtils::CheckSystemHapApp()) {
416         HILOGE("check system api failed.");
417         return BT_ERR_SYSTEM_PERMISSION_FAILED;
418     }
419     return NO_ERROR;
420 }
421 
GetConnectStrategy(const BluetoothRawAddress & device,int & strategy)422 int BluetoothHfpAgServer::GetConnectStrategy(const BluetoothRawAddress &device, int &strategy)
423 {
424     return NO_ERROR;
425 }
426 
ConnectSco(uint8_t callType)427 int BluetoothHfpAgServer::ConnectSco(uint8_t callType)
428 {
429     return NO_ERROR;
430 }
431 
DisconnectSco(uint8_t callType)432 int BluetoothHfpAgServer::DisconnectSco(uint8_t callType)
433 {
434     return NO_ERROR;
435 }
436 
EnableBtCallLog(bool state)437 void BluetoothHfpAgServer::EnableBtCallLog(bool state)
438 {
439     HILOGI("enter");
440 }
441 
GetVirtualDeviceList(std::vector<std::string> & devices)442 void BluetoothHfpAgServer::GetVirtualDeviceList(std::vector<std::string> &devices)
443 {
444     return;
445 }
446 
UpdateVirtualDevice(int32_t action,const std::string & address)447 void BluetoothHfpAgServer::UpdateVirtualDevice(int32_t action, const std::string &address)
448 {
449     return;
450 }
451 }  // namespace Bluetooth
452 }  // namespace OHOS
453