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