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 "bluetooth_hfp_ag.h"
17 #include "bluetooth_host.h"
18 #include "bluetooth_log.h"
19 #include "bluetooth_observer_list.h"
20 #include "i_bluetooth_hfp_ag.h"
21 #include "bluetooth_hfp_ag_observer_stub.h"
22 #include "i_bluetooth_host.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace Bluetooth {
28 class AgServiceObserver : public BluetoothHfpAgObserverStub {
29 public:
AgServiceObserver(BluetoothObserverList<HandsFreeAudioGatewayObserver> & observers)30 explicit AgServiceObserver(BluetoothObserverList<HandsFreeAudioGatewayObserver> &observers) : observers_(observers)
31 {
32 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
33 }
~AgServiceObserver()34 ~AgServiceObserver() override
35 {
36 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
37 };
38
OnConnectionStateChanged(const BluetoothRawAddress & device,int32_t state)39 void OnConnectionStateChanged(const BluetoothRawAddress &device, int32_t state) override
40 {
41 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
42 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
43 observers_.ForEach([remoteDevice, state](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
44 observer->OnConnectionStateChanged(remoteDevice, state);
45 });
46 }
47
OnScoStateChanged(const BluetoothRawAddress & device,int32_t state)48 void OnScoStateChanged(const BluetoothRawAddress &device, int32_t state) override
49 {
50 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
51 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
52 observers_.ForEach([remoteDevice, state](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
53 observer->OnScoStateChanged(remoteDevice, state);
54 });
55 }
56
OnActiveDeviceChanged(const BluetoothRawAddress & device)57 void OnActiveDeviceChanged(const BluetoothRawAddress &device) override
58 {
59 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
60 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
61 observers_.ForEach([remoteDevice](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
62 observer->OnActiveDeviceChanged(remoteDevice);
63 });
64 }
65
OnHfEnhancedDriverSafetyChanged(const BluetoothRawAddress & device,int32_t indValue)66 void OnHfEnhancedDriverSafetyChanged(
67 const BluetoothRawAddress &device, int32_t indValue) override
68 {
69 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
70 BluetoothRemoteDevice remoteDevice(device.GetAddress(), 0);
71 observers_.ForEach([remoteDevice, indValue](std::shared_ptr<HandsFreeAudioGatewayObserver> observer) {
72 observer->OnHfEnhancedDriverSafetyChanged(remoteDevice, indValue);
73 });
74 }
75
76 private:
77 BluetoothObserverList<HandsFreeAudioGatewayObserver> &observers_;
78 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(AgServiceObserver);
79 };
80
81 std::string HfpAgServiceName = "bluetooth-hfp-ag-server";
82
83 struct HandsFreeAudioGateway::impl {
84 impl();
85 ~impl();
GetConnectedDevicesOHOS::Bluetooth::HandsFreeAudioGateway::impl86 std::vector<BluetoothRemoteDevice> GetConnectedDevices()
87 {
88 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
89 std::vector<BluetoothRemoteDevice> devices;
90 if (proxy_ != nullptr && IS_BT_ENABLED()) {
91 std::vector<BluetoothRawAddress> ori;
92 proxy_->GetConnectDevices(ori);
93 for (auto it = ori.begin(); it != ori.end(); it++) {
94 devices.push_back(BluetoothRemoteDevice(it->GetAddress(), 0));
95 }
96 }
97 return devices;
98 }
99
GetDevicesByStatesOHOS::Bluetooth::HandsFreeAudioGateway::impl100 std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states)
101 {
102 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
103 std::vector<BluetoothRemoteDevice> remoteDevices;
104 if (proxy_ != nullptr && IS_BT_ENABLED()) {
105 std::vector<BluetoothRawAddress> rawDevices;
106 std::vector<int32_t> tmpstates;
107 for (int state : states) {
108 int32_t value = (int32_t)state;
109 tmpstates.push_back(value);
110 }
111 proxy_->GetDevicesByStates(tmpstates, rawDevices);
112 for (BluetoothRawAddress rawDevice : rawDevices) {
113 BluetoothRemoteDevice remoteDevice(rawDevice.GetAddress(), 0);
114 remoteDevices.push_back(remoteDevice);
115 }
116 }
117 return remoteDevices;
118 }
119
GetDeviceStateOHOS::Bluetooth::HandsFreeAudioGateway::impl120 int GetDeviceState(const BluetoothRemoteDevice &device)
121 {
122 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
123
124 if (proxy_ == nullptr)
125 {
126 HILOGI("proxy_ == nullptr");
127 printf("proxy_ == nullptr");
128 }
129
130 if (!IS_BT_ENABLED())
131 {
132 HILOGI("Not IS_BT_ENABLED");
133 printf("Not IS_BT_ENABLED");
134 }
135
136 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
137 return proxy_->GetDeviceState(BluetoothRawAddress(device.GetDeviceAddr()));
138 }
139 return HFP_AG_SLC_STATE_DISCONNECTED;
140 }
141
ConnectOHOS::Bluetooth::HandsFreeAudioGateway::impl142 bool Connect(const BluetoothRemoteDevice &device)
143 {
144 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
145
146 if (proxy_ == nullptr)
147 {
148 HILOGI("proxy_ == nullptr");
149 printf("proxy_ == nullptr");
150 }
151
152 if (!IS_BT_ENABLED())
153 {
154 HILOGI("Not IS_BT_ENABLED");
155 printf("Not IS_BT_ENABLED");
156 }
157
158 if (proxy_ != nullptr && IS_BT_ENABLED() && !BluetoothHost::GetDefaultHost().IsBtDiscovering() &&
159 device.IsValidBluetoothRemoteDevice()) {
160 if (proxy_->Connect(BluetoothRawAddress(device.GetDeviceAddr())) == NO_ERROR) {
161 return true;
162 }
163 }
164 return false;
165 }
166
DisconnectOHOS::Bluetooth::HandsFreeAudioGateway::impl167 bool Disconnect(const BluetoothRemoteDevice &device)
168 {
169 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
170
171 if (proxy_ == nullptr)
172 {
173 HILOGI("proxy_ == nullptr");
174 printf("proxy_ == nullptr");
175 }
176
177 if (!IS_BT_ENABLED())
178 {
179 HILOGI("Not IS_BT_ENABLED");
180 printf("Not IS_BT_ENABLED");
181 }
182
183 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
184 if (proxy_->Disconnect(BluetoothRawAddress(device.GetDeviceAddr())) == NO_ERROR) {
185 return true;
186 }
187 }
188 return false;
189 }
190
GetScoStateOHOS::Bluetooth::HandsFreeAudioGateway::impl191 int GetScoState(const BluetoothRemoteDevice &device)
192 {
193 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
194 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
195 return proxy_->GetScoState(BluetoothRawAddress(device.GetDeviceAddr()));
196 }
197 return HFP_AG_SCO_STATE_DISCONNECTED;
198 }
199
ConnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl200 bool ConnectSco()
201 {
202 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
203 if (proxy_ != nullptr && IS_BT_ENABLED()) {
204 return proxy_->ConnectSco();
205 }
206 return false;
207 }
208
DisconnectScoOHOS::Bluetooth::HandsFreeAudioGateway::impl209 bool DisconnectSco()
210 {
211 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
212 if (proxy_ != nullptr && IS_BT_ENABLED()) {
213 return proxy_->DisconnectSco();
214 }
215 return false;
216 }
217
PhoneStateChangedOHOS::Bluetooth::HandsFreeAudioGateway::impl218 void PhoneStateChanged(
219 int numActive, int numHeld, int callState, const std::string &number, int type, const std::string &name)
220 {
221 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
222 if (proxy_ != nullptr && IS_BT_ENABLED()) {
223 proxy_->PhoneStateChanged(numActive, numHeld, callState, number, type, name);
224 }
225 }
226
ClccResponseOHOS::Bluetooth::HandsFreeAudioGateway::impl227 void ClccResponse(int index, int direction, int status, int mode, bool mpty, std::string number, int type)
228 {
229 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
230 if (proxy_ != nullptr && IS_BT_ENABLED()) {
231 proxy_->ClccResponse(index, direction, status, mode, mpty, number, type);
232 }
233 }
234
OpenVoiceRecognitionOHOS::Bluetooth::HandsFreeAudioGateway::impl235 bool OpenVoiceRecognition(const BluetoothRemoteDevice &device)
236 {
237 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
238 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
239 return proxy_->OpenVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
240 }
241 return false;
242 }
243
CloseVoiceRecognitionOHOS::Bluetooth::HandsFreeAudioGateway::impl244 bool CloseVoiceRecognition(const BluetoothRemoteDevice &device)
245 {
246 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
247 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
248 return proxy_->CloseVoiceRecognition(BluetoothRawAddress(device.GetDeviceAddr()));
249 }
250 return false;
251 }
252
SetActiveDeviceOHOS::Bluetooth::HandsFreeAudioGateway::impl253 bool SetActiveDevice(const BluetoothRemoteDevice &device)
254 {
255 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
256 if (proxy_ != nullptr && IS_BT_ENABLED() && device.IsValidBluetoothRemoteDevice()) {
257 return proxy_->SetActiveDevice(BluetoothRawAddress(device.GetDeviceAddr()));
258 }
259 return false;
260 }
261
GetActiveDeviceOHOS::Bluetooth::HandsFreeAudioGateway::impl262 BluetoothRemoteDevice GetActiveDevice()
263 {
264 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
265 BluetoothRemoteDevice device;
266 if (proxy_ != nullptr && IS_BT_ENABLED()) {
267 std::string address = proxy_->GetActiveDevice();
268 BluetoothRemoteDevice remoteDevice(address, 0);
269 device = remoteDevice;
270 }
271 return device;
272 }
273
RegisterObserverOHOS::Bluetooth::HandsFreeAudioGateway::impl274 void RegisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
275 {
276 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
277 observers_.Register(observer);
278 }
279
DeregisterObserverOHOS::Bluetooth::HandsFreeAudioGateway::impl280 void DeregisterObserver(std::shared_ptr<HandsFreeAudioGatewayObserver> observer)
281 {
282 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
283 observers_.Deregister(observer);
284 HILOGD("[hfpag]: %{public}s(): end!", __FUNCTION__);
285 }
286
287 private:
288 const static int HFP_AG_SLC_STATE_DISCONNECTED = (int)BTConnectState::DISCONNECTED;
289 const static int HFP_AG_SCO_STATE_DISCONNECTED = 3;
290
291 BluetoothObserverList<HandsFreeAudioGatewayObserver> observers_;
292 sptr<AgServiceObserver> serviceObserver_;
293 sptr<IBluetoothHfpAg> proxy_;
294 class HandsFreeAudioGatewayDeathRecipient;
295 sptr<HandsFreeAudioGatewayDeathRecipient> deathRecipient_;
296 };
297
298 class HandsFreeAudioGateway::impl::HandsFreeAudioGatewayDeathRecipient final : public IRemoteObject::DeathRecipient {
299 public:
HandsFreeAudioGatewayDeathRecipient(HandsFreeAudioGateway::impl & impl)300 HandsFreeAudioGatewayDeathRecipient(HandsFreeAudioGateway::impl &impl) : impl_(impl)
301 {};
302 ~HandsFreeAudioGatewayDeathRecipient() final = default;
303 BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(HandsFreeAudioGatewayDeathRecipient);
304
OnRemoteDied(const wptr<IRemoteObject> & remote)305 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
306 {
307 HILOGI("HandsFreeAudioGateway::impl::HandsFreeAudioGatewayDeathRecipient::OnRemoteDied starts");
308 impl_.proxy_->AsObject()->RemoveDeathRecipient(impl_.deathRecipient_);
309 impl_.proxy_ = nullptr;
310 }
311
312 private:
313 HandsFreeAudioGateway::impl &impl_;
314 };
315
impl()316 HandsFreeAudioGateway::impl::impl()
317 {
318 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
319 sptr<IRemoteObject> hostRemote = samgr->GetSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID);
320
321 if (!hostRemote) {
322 HILOGE("HandsFreeAudioGateway::impl:impl() failed: no hostRemote");
323 return;
324 }
325 sptr<IBluetoothHost> hostProxy = iface_cast<IBluetoothHost>(hostRemote);
326 sptr<IRemoteObject> remote = hostProxy->GetProfile(PROFILE_HFP_AG);
327
328 if (!remote) {
329 HILOGE("HandsFreeAudioGateway::impl:impl() failed: no remote");
330 return;
331 }
332 HILOGI("HandsFreeAudioGateway::impl:impl() remote obtained");
333
334 serviceObserver_ = new AgServiceObserver(observers_);
335
336 proxy_ = iface_cast<IBluetoothHfpAg>(remote);
337 proxy_->RegisterObserver(serviceObserver_);
338 deathRecipient_ = new HandsFreeAudioGatewayDeathRecipient(*this);
339 proxy_->AsObject()->AddDeathRecipient(deathRecipient_);
340 }
341
~impl()342 HandsFreeAudioGateway::impl::~impl()
343 {
344 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
345 if (proxy_ != nullptr) {
346 proxy_->DeregisterObserver(serviceObserver_);
347 }
348 proxy_->AsObject()->RemoveDeathRecipient(deathRecipient_);
349 }
350
GetProfile()351 HandsFreeAudioGateway *HandsFreeAudioGateway::GetProfile()
352 {
353 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
354 static HandsFreeAudioGateway instance;
355 return &instance;
356 }
357
HandsFreeAudioGateway()358 HandsFreeAudioGateway::HandsFreeAudioGateway()
359 {
360 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
361 pimpl = std::make_unique<impl>();
362 }
363
~HandsFreeAudioGateway()364 HandsFreeAudioGateway::~HandsFreeAudioGateway()
365 {}
366
GetConnectedDevices() const367 std::vector<BluetoothRemoteDevice> HandsFreeAudioGateway::GetConnectedDevices() const
368 {
369 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
370 return pimpl->GetConnectedDevices();
371 }
372
GetDevicesByStates(std::vector<int> states)373 std::vector<BluetoothRemoteDevice> HandsFreeAudioGateway::GetDevicesByStates(std::vector<int> states)
374 {
375 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
376 return pimpl->GetDevicesByStates(states);
377 }
378
GetDeviceState(const BluetoothRemoteDevice & device) const379 int HandsFreeAudioGateway::GetDeviceState(const BluetoothRemoteDevice &device) const
380 {
381 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
382 return pimpl->GetDeviceState(device);
383 }
384
Connect(const BluetoothRemoteDevice & device)385 bool HandsFreeAudioGateway::Connect(const BluetoothRemoteDevice &device)
386 {
387 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
388 return pimpl->Connect(device);
389 }
390
Disconnect(const BluetoothRemoteDevice & device)391 bool HandsFreeAudioGateway::Disconnect(const BluetoothRemoteDevice &device)
392 {
393 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
394 return pimpl->Disconnect(device);
395 }
396
GetScoState(const BluetoothRemoteDevice & device) const397 int HandsFreeAudioGateway::GetScoState(const BluetoothRemoteDevice &device) const
398 {
399 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
400 return pimpl->GetScoState(device);
401 }
402
ConnectSco()403 bool HandsFreeAudioGateway::ConnectSco()
404 {
405 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
406 return pimpl->ConnectSco();
407 }
408
DisconnectSco()409 bool HandsFreeAudioGateway::DisconnectSco()
410 {
411 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
412 return pimpl->DisconnectSco();
413 }
414
PhoneStateChanged(int numActive,int numHeld,int callState,const std::string & number,int type,const std::string & name)415 void HandsFreeAudioGateway::PhoneStateChanged(
416 int numActive, int numHeld, int callState, const std::string &number, int type, const std::string &name)
417 {
418 pimpl->PhoneStateChanged(numActive, numHeld, callState, number, type, name);
419 }
420
ClccResponse(int index,int direction,int status,int mode,bool mpty,const std::string & number,int type)421 void HandsFreeAudioGateway::ClccResponse(
422 int index, int direction, int status, int mode, bool mpty, const std::string &number, int type)
423 {
424 pimpl->ClccResponse(index, direction, status, mode, mpty, number, type);
425 }
426
OpenVoiceRecognition(const BluetoothRemoteDevice & device)427 bool HandsFreeAudioGateway::OpenVoiceRecognition(const BluetoothRemoteDevice &device)
428 {
429 return pimpl->OpenVoiceRecognition(device);
430 }
431
CloseVoiceRecognition(const BluetoothRemoteDevice & device)432 bool HandsFreeAudioGateway::CloseVoiceRecognition(const BluetoothRemoteDevice &device)
433 {
434 return pimpl->CloseVoiceRecognition(device);
435 }
436
SetActiveDevice(const BluetoothRemoteDevice & device)437 bool HandsFreeAudioGateway::SetActiveDevice(const BluetoothRemoteDevice &device)
438 {
439 return pimpl->SetActiveDevice(device);
440 }
441
GetActiveDevice() const442 BluetoothRemoteDevice HandsFreeAudioGateway::GetActiveDevice() const
443 {
444 return pimpl->GetActiveDevice();
445 }
446
RegisterObserver(HandsFreeAudioGatewayObserver * observer)447 void HandsFreeAudioGateway::RegisterObserver(HandsFreeAudioGatewayObserver *observer)
448 {
449
450 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
451 std::shared_ptr<HandsFreeAudioGatewayObserver> observerPtr(observer, [](HandsFreeAudioGatewayObserver *) {});
452 }
453
DeregisterObserver(HandsFreeAudioGatewayObserver * observer)454 void HandsFreeAudioGateway::DeregisterObserver(HandsFreeAudioGatewayObserver *observer)
455 {
456 HILOGD("[hfpag]: %{public}s(): Enter!", __FUNCTION__);
457 std::shared_ptr<HandsFreeAudioGatewayObserver> observerPtr(observer, [](HandsFreeAudioGatewayObserver *) {});
458 pimpl->DeregisterObserver(observerPtr);
459 HILOGD("[hfpag]: %{public}s(): end!", __FUNCTION__);
460 }
461 } // namespace Bluetooth
462 } // namespace OHOS