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