1 /*
2 * Copyright (c) 2023-2025 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 #ifndef LOG_TAG
16 #define LOG_TAG "BluetoothDeviceManager"
17 #endif
18
19 #include "bluetooth_device_manager.h"
20
21 #include <thread>
22
23 #include "bluetooth_audio_manager.h"
24 #include "audio_bluetooth_manager.h"
25
26 namespace OHOS {
27 namespace Bluetooth {
28 using namespace AudioStandard;
29
30 const int WEAR_ENABLED = 1;
31 const int DEFAULT_COD = -1;
32 const int DEFAULT_MAJOR_CLASS = -1;
33 const int DEFAULT_MAJOR_MINOR_CLASS = -1;
34 const int A2DP_DEFAULT_SELECTION = -1;
35 const int HFP_DEFAULT_SELECTION = -1;
36 const int USER_SELECTION = 1;
37 const std::map<std::pair<int, int>, DeviceCategory> bluetoothDeviceCategoryMap_ = {
38 {std::make_pair(BluetoothDevice::MAJOR_AUDIO_VIDEO, BluetoothDevice::AUDIO_VIDEO_HEADPHONES), BT_HEADPHONE},
39 {std::make_pair(BluetoothDevice::MAJOR_AUDIO_VIDEO, BluetoothDevice::AUDIO_VIDEO_WEARABLE_HEADSET), BT_HEADPHONE},
40 {std::make_pair(BluetoothDevice::MAJOR_AUDIO_VIDEO, BluetoothDevice::AUDIO_VIDEO_LOUDSPEAKER), BT_SOUNDBOX},
41 {std::make_pair(BluetoothDevice::MAJOR_AUDIO_VIDEO, BluetoothDevice::AUDIO_VIDEO_HANDSFREE), BT_CAR},
42 {std::make_pair(BluetoothDevice::MAJOR_AUDIO_VIDEO, BluetoothDevice::AUDIO_VIDEO_CAR_AUDIO), BT_CAR},
43 {std::make_pair(BluetoothDevice::MAJOR_WEARABLE, BluetoothDevice::WEARABLE_GLASSES), BT_GLASSES},
44 {std::make_pair(BluetoothDevice::MAJOR_WEARABLE, BluetoothDevice::WEARABLE_WRIST_WATCH), BT_WATCH},
45 };
46 IDeviceStatusObserver *g_deviceObserver = nullptr;
47 std::mutex g_observerLock;
48 std::mutex g_a2dpDeviceLock;
49 std::mutex g_a2dpDeviceMapLock;
50 std::mutex g_a2dpWearStateMapLock;
51 std::map<std::string, BluetoothRemoteDevice> MediaBluetoothDeviceManager::a2dpBluetoothDeviceMap_;
52 std::map<std::string, BluetoothDeviceAction> MediaBluetoothDeviceManager::wearDetectionStateMap_;
53 std::vector<BluetoothRemoteDevice> MediaBluetoothDeviceManager::privacyDevices_;
54 std::vector<BluetoothRemoteDevice> MediaBluetoothDeviceManager::commonDevices_;
55 std::vector<BluetoothRemoteDevice> MediaBluetoothDeviceManager::negativeDevices_;
56 std::vector<BluetoothRemoteDevice> MediaBluetoothDeviceManager::connectingDevices_;
57 std::vector<BluetoothRemoteDevice> MediaBluetoothDeviceManager::virtualDevices_;
58 std::mutex g_a2dpInDeviceMapLock;
59 std::mutex g_a2dpInStreamInfoMapLock;
60 std::map<std::string, BluetoothRemoteDevice> A2dpInBluetoothDeviceManager::a2dpInBluetoothDeviceMap_;
61 std::map<std::string, AudioStreamInfo> A2dpInBluetoothDeviceManager::a2dpInStreamInfoMap_;
62 std::mutex g_hfpDeviceLock;
63 std::mutex g_hfpDeviceMapLock;
64 std::mutex g_hfpWearStateMapLock;
65 std::map<std::string, BluetoothRemoteDevice> HfpBluetoothDeviceManager::hfpBluetoothDeviceMap_;
66 std::map<std::string, BluetoothDeviceAction> HfpBluetoothDeviceManager::wearDetectionStateMap_;
67 std::vector<BluetoothRemoteDevice> HfpBluetoothDeviceManager::privacyDevices_;
68 std::vector<BluetoothRemoteDevice> HfpBluetoothDeviceManager::commonDevices_;
69 std::vector<BluetoothRemoteDevice> HfpBluetoothDeviceManager::negativeDevices_;
70 std::vector<BluetoothRemoteDevice> HfpBluetoothDeviceManager::connectingDevices_;
71 std::vector<BluetoothRemoteDevice> HfpBluetoothDeviceManager::virtualDevices_;
72 std::mutex HfpBluetoothDeviceManager::stopVirtualCallHandleLock_;
73 HfpBluetoothDeviceManager::DisconnectScoForDevice HfpBluetoothDeviceManager::disconnectScoFun_ = nullptr;
74 BluetoothStopVirtualCallHandle HfpBluetoothDeviceManager::stopVirtualCallHandle_ = { BluetoothRemoteDevice(), false};
75
RegisterDeviceObserver(IDeviceStatusObserver & observer)76 int32_t RegisterDeviceObserver(IDeviceStatusObserver &observer)
77 {
78 std::lock_guard<std::mutex> deviceLock(g_observerLock);
79 g_deviceObserver = &observer;
80 return SUCCESS;
81 }
82
UnregisterDeviceObserver()83 void UnregisterDeviceObserver()
84 {
85 std::lock_guard<std::mutex> deviceLock(g_observerLock);
86 g_deviceObserver = nullptr;
87 }
88
SendUserSelectionEvent(AudioStandard::DeviceType devType,const std::string & macAddress,int32_t eventType)89 void SendUserSelectionEvent(AudioStandard::DeviceType devType, const std::string &macAddress, int32_t eventType)
90 {
91 AUDIO_INFO_LOG("devType is %{public}d, eventType is%{public}d.", devType, eventType);
92 BluetoothRemoteDevice device;
93 if (devType == DEVICE_TYPE_BLUETOOTH_A2DP) {
94 if (MediaBluetoothDeviceManager::GetConnectedA2dpBluetoothDevice(macAddress, device) != SUCCESS) {
95 AUDIO_ERR_LOG("failed for the device is not connected.");
96 return;
97 }
98 BluetoothAudioManager::GetInstance().SendDeviceSelection(device, eventType,
99 HFP_DEFAULT_SELECTION, USER_SELECTION);
100 } else if (devType == DEVICE_TYPE_BLUETOOTH_A2DP_IN) {
101 if (A2dpInBluetoothDeviceManager::IsA2dpInBluetoothDeviceExist(macAddress) != SUCCESS) {
102 AUDIO_ERR_LOG("failed for the device is not connected.");
103 return;
104 }
105 BluetoothAudioManager::GetInstance().SendDeviceSelection(device, eventType,
106 HFP_DEFAULT_SELECTION, USER_SELECTION);
107 } else if (devType == DEVICE_TYPE_BLUETOOTH_SCO) {
108 if (HfpBluetoothDeviceManager::GetConnectedHfpBluetoothDevice(macAddress, device) != SUCCESS) {
109 AUDIO_ERR_LOG("failed for the device is not connected.");
110 return;
111 }
112 BluetoothAudioManager::GetInstance().SendDeviceSelection(device, A2DP_DEFAULT_SELECTION,
113 eventType, USER_SELECTION);
114 } else {
115 AUDIO_ERR_LOG("failed for the devType is not Bluetooth type.");
116 }
117 }
118
IsBTWearDetectionEnable(const BluetoothRemoteDevice & device)119 bool IsBTWearDetectionEnable(const BluetoothRemoteDevice &device)
120 {
121 int32_t wearEnabledAbility = 0;
122 bool isWearSupported = false;
123 BluetoothAudioManager::GetInstance().GetWearDetectionState(device.GetDeviceAddr(), wearEnabledAbility);
124 BluetoothAudioManager::GetInstance().IsWearDetectionSupported(device.GetDeviceAddr(), isWearSupported);
125 AUDIO_INFO_LOG("wear detection on-off state: %{public}d, wear detection support state: %{public}d",
126 wearEnabledAbility, isWearSupported);
127 return (wearEnabledAbility == WEAR_ENABLED && isWearSupported);
128 }
129
GetDeviceCategory(const BluetoothRemoteDevice & device)130 DeviceCategory GetDeviceCategory(const BluetoothRemoteDevice &device)
131 {
132 int cod = DEFAULT_COD;
133 int majorClass = DEFAULT_MAJOR_CLASS;
134 int majorMinorClass = DEFAULT_MAJOR_MINOR_CLASS;
135 device.GetDeviceProductType(cod, majorClass, majorMinorClass);
136 AUDIO_WARNING_LOG("Device type majorClass: %{public}d, majorMinorClass: %{public}d.", majorClass, majorMinorClass);
137 DeviceCategory bluetoothCategory = CATEGORY_DEFAULT;
138 auto pos = bluetoothDeviceCategoryMap_.find(std::make_pair(majorClass, majorMinorClass));
139 if (pos != bluetoothDeviceCategoryMap_.end()) {
140 bluetoothCategory = pos->second;
141 AUDIO_WARNING_LOG("Bluetooth category is: %{public}d", bluetoothCategory);
142 }
143 return bluetoothCategory;
144 }
145
SetMediaStack(const BluetoothRemoteDevice & device,int action)146 void MediaBluetoothDeviceManager::SetMediaStack(const BluetoothRemoteDevice &device, int action)
147 {
148 switch (action) {
149 case BluetoothDeviceAction::CONNECTING_ACTION:
150 HandleConnectingDevice(device);
151 break;
152 case BluetoothDeviceAction::CONNECT_ACTION:
153 HandleConnectDevice(device);
154 break;
155 case BluetoothDeviceAction::DISCONNECT_ACTION:
156 HandleDisconnectDevice(device);
157 break;
158 case BluetoothDeviceAction::WEAR_ACTION:
159 HandleWearDevice(device);
160 break;
161 case BluetoothDeviceAction::UNWEAR_ACTION:
162 HandleUnwearDevice(device);
163 break;
164 case BluetoothDeviceAction::ENABLEFROMREMOTE_ACTION:
165 HandleEnableDevice(device);
166 break;
167 case BluetoothDeviceAction::DISABLEFROMREMOTE_ACTION:
168 HandleDisableDevice(device);
169 break;
170 case BluetoothDeviceAction::ENABLE_WEAR_DETECTION_ACTION:
171 HandleWearEnable(device);
172 break;
173 case BluetoothDeviceAction::DISABLE_WEAR_DETECTION_ACTION:
174 HandleWearDisable(device);
175 break;
176 case BluetoothDeviceAction::USER_SELECTION_ACTION:
177 HandleUserSelection(device);
178 break;
179 case BluetoothDeviceAction::VIRTUAL_DEVICE_ADD_ACTION:
180 HandleVirtualConnectDevice(device);
181 break;
182 case BluetoothDeviceAction::VIRTUAL_DEVICE_REMOVE_ACTION:
183 HandleRemoveVirtualConnectDevice(device);
184 break;
185 case BluetoothDeviceAction::CATEGORY_UPDATE_ACTION:
186 HandleUpdateDeviceCategory(device);
187 break;
188 default:
189 AUDIO_ERR_LOG("SetMediaStack failed due to the unknow action: %{public}d", action);
190 break;
191 }
192 }
193
HandleConnectingDevice(const BluetoothRemoteDevice & device)194 void MediaBluetoothDeviceManager::HandleConnectingDevice(const BluetoothRemoteDevice &device)
195 {
196 if (IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
197 return;
198 }
199 AddDeviceInConfigVector(device, connectingDevices_);
200 }
201
HandleConnectDevice(const BluetoothRemoteDevice & device)202 void MediaBluetoothDeviceManager::HandleConnectDevice(const BluetoothRemoteDevice &device)
203 {
204 if (IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
205 return;
206 }
207 AudioDeviceDescriptor desc = HandleConnectDeviceInner(device);
208 NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD);
209 }
210
HandleConnectDeviceInner(const BluetoothRemoteDevice & device)211 AudioDeviceDescriptor MediaBluetoothDeviceManager::HandleConnectDeviceInner(const BluetoothRemoteDevice &device)
212 {
213 RemoveDeviceInConfigVector(device, connectingDevices_);
214 // If the device was virtual connected, remove it from the negativeDevices_ list.
215 RemoveDeviceInConfigVector(device, negativeDevices_);
216 DeviceCategory bluetoothCategory = GetDeviceCategory(device);
217 AudioDeviceDescriptor desc;
218 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
219 desc.macAddress_ = device.GetDeviceAddr();
220 desc.deviceCategory_ = bluetoothCategory;
221 switch (bluetoothCategory) {
222 case BT_GLASSES:
223 case BT_HEADPHONE:
224 if (IsBTWearDetectionEnable(device)) {
225 AddDeviceInConfigVector(device, negativeDevices_);
226 desc.deviceCategory_ = BT_UNWEAR_HEADPHONE;
227 } else {
228 AddDeviceInConfigVector(device, privacyDevices_);
229 }
230 break;
231 case BT_SOUNDBOX:
232 case BT_CAR:
233 AddDeviceInConfigVector(device, commonDevices_);
234 break;
235 case BT_WATCH:
236 AddDeviceInConfigVector(device, negativeDevices_);
237 break;
238 default:
239 AUDIO_WARNING_LOG("Unknow BT category, regard as bluetooth headset.");
240 AddDeviceInConfigVector(device, privacyDevices_);
241 desc.deviceCategory_ = BT_HEADPHONE;
242 break;
243 }
244 return desc;
245 }
246
HandleDisconnectDevice(const BluetoothRemoteDevice & device)247 void MediaBluetoothDeviceManager::HandleDisconnectDevice(const BluetoothRemoteDevice &device)
248 {
249 RemoveDeviceInConfigVector(device, connectingDevices_);
250 if (!IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
251 AUDIO_INFO_LOG("The device is already disconnected, ignore disconnect action.");
252 return;
253 }
254 RemoveDeviceInConfigVector(device, privacyDevices_);
255 RemoveDeviceInConfigVector(device, commonDevices_);
256 RemoveDeviceInConfigVector(device, negativeDevices_);
257 {
258 std::lock_guard<std::mutex> wearStateMapLock(g_a2dpWearStateMapLock);
259 wearDetectionStateMap_.erase(device.GetDeviceAddr());
260 }
261 AudioDeviceDescriptor desc;
262 desc.deviceCategory_ = CATEGORY_DEFAULT;
263 NotifyToUpdateAudioDevice(device, desc, DeviceStatus::REMOVE);
264 }
265
HandleWearDevice(const BluetoothRemoteDevice & device)266 void MediaBluetoothDeviceManager::HandleWearDevice(const BluetoothRemoteDevice &device)
267 {
268 bool isDeviceExist = IsA2dpBluetoothDeviceExist(device.GetDeviceAddr());
269 CHECK_AND_RETURN_LOG(isDeviceExist,
270 "HandleWearDevice failed for the device has not be reported the connected action.");
271 RemoveDeviceInConfigVector(device, negativeDevices_);
272 RemoveDeviceInConfigVector(device, privacyDevices_);
273 AddDeviceInConfigVector(device, privacyDevices_);
274 {
275 std::lock_guard<std::mutex> wearStateMapLock(g_a2dpWearStateMapLock);
276 wearDetectionStateMap_[device.GetDeviceAddr()] = BluetoothDeviceAction::WEAR_ACTION;
277 }
278 AudioDeviceDescriptor desc;
279 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
280 desc.macAddress_ = device.GetDeviceAddr();
281 desc.deviceCategory_ = BT_HEADPHONE;
282 std::lock_guard<std::mutex> observerLock(g_observerLock);
283 if (g_deviceObserver != nullptr) {
284 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
285 }
286 }
287
HandleUnwearDevice(const BluetoothRemoteDevice & device)288 void MediaBluetoothDeviceManager::HandleUnwearDevice(const BluetoothRemoteDevice &device)
289 {
290 bool isDeviceExist = IsA2dpBluetoothDeviceExist(device.GetDeviceAddr());
291 CHECK_AND_RETURN_LOG(isDeviceExist, "HandleWearDevice failed for the device has not worn.");
292 RemoveDeviceInConfigVector(device, privacyDevices_);
293 RemoveDeviceInConfigVector(device, negativeDevices_);
294 AddDeviceInConfigVector(device, negativeDevices_);
295 {
296 std::lock_guard<std::mutex> wearStateMapLock(g_a2dpWearStateMapLock);
297 wearDetectionStateMap_[device.GetDeviceAddr()] = BluetoothDeviceAction::UNWEAR_ACTION;
298 }
299 AudioDeviceDescriptor desc;
300 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
301 desc.macAddress_ = device.GetDeviceAddr();
302 desc.deviceCategory_ = BT_UNWEAR_HEADPHONE;
303 std::lock_guard<std::mutex> observerLock(g_observerLock);
304 if (g_deviceObserver != nullptr) {
305 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
306 }
307 }
308
HandleEnableDevice(const BluetoothRemoteDevice & device)309 void MediaBluetoothDeviceManager::HandleEnableDevice(const BluetoothRemoteDevice &device)
310 {
311 if (!IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
312 AUDIO_ERR_LOG("HandleEnableDevice failed for the device has not connected.");
313 return;
314 }
315 AudioDeviceDescriptor desc;
316 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
317 desc.macAddress_ = device.GetDeviceAddr();
318 desc.isEnable_ = true;
319 std::lock_guard<std::mutex> observerLock(g_observerLock);
320 if (g_deviceObserver != nullptr) {
321 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::ENABLE_UPDATE);
322 }
323 }
324
HandleDisableDevice(const BluetoothRemoteDevice & device)325 void MediaBluetoothDeviceManager::HandleDisableDevice(const BluetoothRemoteDevice &device)
326 {
327 if (!IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
328 AUDIO_ERR_LOG("HandleDisableDevice failed for the device has not connected.");
329 return;
330 }
331 AudioDeviceDescriptor desc;
332 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
333 desc.macAddress_ = device.GetDeviceAddr();
334 desc.isEnable_ = false;
335 std::lock_guard<std::mutex> observerLock(g_observerLock);
336 if (g_deviceObserver != nullptr) {
337 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::ENABLE_UPDATE);
338 }
339 }
340
HandleWearEnable(const BluetoothRemoteDevice & device)341 void MediaBluetoothDeviceManager::HandleWearEnable(const BluetoothRemoteDevice &device)
342 {
343 if (!IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
344 AUDIO_ERR_LOG("HandleWearEnable failed for the device has not connected.");
345 return;
346 }
347 RemoveDeviceInConfigVector(device, negativeDevices_);
348 RemoveDeviceInConfigVector(device, privacyDevices_);
349 std::lock_guard<std::mutex> wearStateMapLock(g_a2dpWearStateMapLock);
350 AudioDeviceDescriptor desc;
351 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
352 desc.macAddress_ = device.GetDeviceAddr();
353 auto wearStateIter = wearDetectionStateMap_.find(device.GetDeviceAddr());
354 if (wearStateIter != wearDetectionStateMap_.end() &&
355 wearStateIter->second == BluetoothDeviceAction::WEAR_ACTION) {
356 AddDeviceInConfigVector(device, privacyDevices_);
357 desc.deviceCategory_ = BT_HEADPHONE;
358 } else {
359 AddDeviceInConfigVector(device, negativeDevices_);
360 desc.deviceCategory_ = BT_UNWEAR_HEADPHONE;
361 }
362 std::lock_guard<std::mutex> observerLock(g_observerLock);
363 if (g_deviceObserver != nullptr) {
364 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
365 }
366 }
367
HandleWearDisable(const BluetoothRemoteDevice & device)368 void MediaBluetoothDeviceManager::HandleWearDisable(const BluetoothRemoteDevice &device)
369 {
370 if (!IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
371 AUDIO_ERR_LOG("HandleWearDisable failed for the device has not connected.");
372 return;
373 }
374 RemoveDeviceInConfigVector(device, privacyDevices_);
375 RemoveDeviceInConfigVector(device, negativeDevices_);
376 AddDeviceInConfigVector(device, privacyDevices_);
377 AudioDeviceDescriptor desc;
378 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
379 desc.macAddress_ = device.GetDeviceAddr();
380 desc.deviceCategory_ = BT_HEADPHONE;
381 std::lock_guard<std::mutex> observerLock(g_observerLock);
382 if (g_deviceObserver != nullptr) {
383 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
384 }
385 }
386
HandleUserSelection(const BluetoothRemoteDevice & device)387 void MediaBluetoothDeviceManager::HandleUserSelection(const BluetoothRemoteDevice &device)
388 {
389 std::lock_guard<std::mutex> observerLock(g_observerLock);
390 if (g_deviceObserver != nullptr) {
391 g_deviceObserver->OnForcedDeviceSelected(DEVICE_TYPE_BLUETOOTH_A2DP, device.GetDeviceAddr());
392 }
393 }
394
HandleVirtualConnectDevice(const BluetoothRemoteDevice & device)395 void MediaBluetoothDeviceManager::HandleVirtualConnectDevice(const BluetoothRemoteDevice &device)
396 {
397 AddDeviceInConfigVector(device, virtualDevices_);
398 DeviceCategory bluetoothCategory = GetDeviceCategory(device);
399 AudioDeviceDescriptor desc;
400 desc.deviceCategory_ = bluetoothCategory;
401 AddDeviceInConfigVector(device, negativeDevices_);
402 NotifyToUpdateVirtualDevice(device, desc, DeviceStatus::VIRTUAL_ADD);
403 }
404
HandleRemoveVirtualConnectDevice(const BluetoothRemoteDevice & device)405 void MediaBluetoothDeviceManager::HandleRemoveVirtualConnectDevice(const BluetoothRemoteDevice &device)
406 {
407 RemoveDeviceInConfigVector(device, virtualDevices_);
408 RemoveDeviceInConfigVector(device, negativeDevices_);
409 AudioDeviceDescriptor desc;
410 desc.deviceCategory_ = CATEGORY_DEFAULT;
411 NotifyToUpdateVirtualDevice(device, desc, DeviceStatus::VIRTUAL_REMOVE);
412 }
413
AddDeviceInConfigVector(const BluetoothRemoteDevice & device,std::vector<BluetoothRemoteDevice> & deviceVector)414 void MediaBluetoothDeviceManager::AddDeviceInConfigVector(const BluetoothRemoteDevice &device,
415 std::vector<BluetoothRemoteDevice> &deviceVector)
416 {
417 std::lock_guard<std::mutex> a2dpDeviceLock(g_a2dpDeviceLock);
418 auto isPresent = [&device] (BluetoothRemoteDevice &bluetoothRemoteDevice) {
419 return device.GetDeviceAddr() == bluetoothRemoteDevice.GetDeviceAddr();
420 };
421 auto deviceIter = std::find_if(deviceVector.begin(), deviceVector.end(), isPresent);
422 if (deviceIter == deviceVector.end()) {
423 deviceVector.push_back(device);
424 }
425 }
426
RemoveDeviceInConfigVector(const BluetoothRemoteDevice & device,std::vector<BluetoothRemoteDevice> & deviceVector)427 void MediaBluetoothDeviceManager::RemoveDeviceInConfigVector(const BluetoothRemoteDevice &device,
428 std::vector<BluetoothRemoteDevice> &deviceVector)
429 {
430 std::lock_guard<std::mutex> a2dpDeviceLock(g_a2dpDeviceLock);
431 auto isPresent = [&device] (BluetoothRemoteDevice &bluetoothRemoteDevice) {
432 return device.GetDeviceAddr() == bluetoothRemoteDevice.GetDeviceAddr();
433 };
434 auto deviceIter = std::find_if(deviceVector.begin(), deviceVector.end(), isPresent);
435 if (deviceIter != deviceVector.end()) {
436 deviceVector.erase(deviceIter);
437 }
438 }
439
NotifyToUpdateAudioDevice(const BluetoothRemoteDevice & device,AudioDeviceDescriptor & desc,DeviceStatus deviceStatus)440 void MediaBluetoothDeviceManager::NotifyToUpdateAudioDevice(const BluetoothRemoteDevice &device,
441 AudioDeviceDescriptor &desc, DeviceStatus deviceStatus)
442 {
443 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
444 desc.deviceRole_ = DeviceRole::OUTPUT_DEVICE;
445 desc.macAddress_ = device.GetDeviceAddr();
446 desc.deviceName_ = device.GetDeviceName();
447 desc.connectState_ = ConnectState::CONNECTED;
448 AUDIO_WARNING_LOG("a2dpBluetoothDeviceMap_ operation: %{public}d new bluetooth device, device address\
449 is %{public}s, category is %{public}d", deviceStatus,
450 GetEncryptAddr(device.GetDeviceAddr()).c_str(), desc.deviceCategory_);
451 {
452 std::lock_guard<std::mutex> deviceMapLock(g_a2dpDeviceMapLock);
453 if (deviceStatus == DeviceStatus::ADD) {
454 a2dpBluetoothDeviceMap_[device.GetDeviceAddr()] = device;
455 } else if (deviceStatus == DeviceStatus::REMOVE) {
456 if (a2dpBluetoothDeviceMap_.find(device.GetDeviceAddr()) != a2dpBluetoothDeviceMap_.end()) {
457 a2dpBluetoothDeviceMap_.erase(device.GetDeviceAddr());
458 }
459 }
460 }
461 std::lock_guard<std::mutex> observerLock(g_observerLock);
462 CHECK_AND_RETURN_LOG(g_deviceObserver != nullptr, "NotifyToUpdateAudioDevice, device observer is null");
463 bool isConnected = deviceStatus == DeviceStatus::ADD;
464 g_deviceObserver->OnDeviceStatusUpdated(desc, isConnected);
465 }
466
HandleUpdateDeviceCategory(const BluetoothRemoteDevice & device)467 void MediaBluetoothDeviceManager::HandleUpdateDeviceCategory(const BluetoothRemoteDevice &device)
468 {
469 if (!IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
470 AUDIO_WARNING_LOG("HandleUpdateDeviceCategory failed for the device has not be reported the connected action.");
471 return;
472 }
473 AudioDeviceDescriptor desc = HandleConnectDeviceInner(device);
474 int32_t wearState = BluetoothAudioManager::GetInstance().IsDeviceWearing(device);
475 if (wearState == 1 && desc.deviceCategory_ == BT_UNWEAR_HEADPHONE) { // 1 wear state
476 desc.deviceCategory_ = BT_HEADPHONE;
477 }
478 std::lock_guard<std::mutex> observerLock(g_observerLock);
479 if (g_deviceObserver != nullptr) {
480 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
481 }
482 }
483
NotifyToUpdateVirtualDevice(const BluetoothRemoteDevice & device,AudioDeviceDescriptor & desc,DeviceStatus deviceStatus)484 void MediaBluetoothDeviceManager::NotifyToUpdateVirtualDevice(const BluetoothRemoteDevice &device,
485 AudioDeviceDescriptor &desc, DeviceStatus deviceStatus)
486 {
487 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
488 desc.deviceRole_ = DeviceRole::OUTPUT_DEVICE;
489 desc.macAddress_ = device.GetDeviceAddr();
490 desc.deviceName_ = device.GetDeviceName();
491 desc.connectState_ = ConnectState::VIRTUAL_CONNECTED;
492 std::lock_guard<std::mutex> observerLock(g_observerLock);
493 CHECK_AND_RETURN_LOG(g_deviceObserver != nullptr, "NotifyToUpdateVirtualDevice, device observer is null");
494 bool isConnected = deviceStatus == DeviceStatus::VIRTUAL_ADD;
495 g_deviceObserver->OnDeviceStatusUpdated(desc, isConnected);
496 }
497
IsA2dpBluetoothDeviceExist(const std::string & macAddress)498 bool MediaBluetoothDeviceManager::IsA2dpBluetoothDeviceExist(const std::string& macAddress)
499 {
500 std::lock_guard<std::mutex> deviceMapLock(g_a2dpDeviceMapLock);
501 if (a2dpBluetoothDeviceMap_.find(macAddress) != a2dpBluetoothDeviceMap_.end()) {
502 return true;
503 }
504 return false;
505 }
506
IsA2dpBluetoothDeviceConnecting(const std::string & macAddress)507 bool MediaBluetoothDeviceManager::IsA2dpBluetoothDeviceConnecting(const std::string& macAddress)
508 {
509 std::lock_guard<std::mutex> a2dpDeviceLock(g_a2dpDeviceLock);
510 auto deviceIter = std::find_if(connectingDevices_.begin(), connectingDevices_.end(),
511 [&macAddress] (BluetoothRemoteDevice &bluetoothRemoteDevice) {
512 return macAddress == bluetoothRemoteDevice.GetDeviceAddr();
513 });
514 if (deviceIter != connectingDevices_.end()) {
515 return true;
516 }
517 return false;
518 }
519
GetConnectedA2dpBluetoothDevice(const std::string & macAddress,BluetoothRemoteDevice & device)520 int32_t MediaBluetoothDeviceManager::GetConnectedA2dpBluetoothDevice(const std::string& macAddress,
521 BluetoothRemoteDevice &device)
522 {
523 std::lock_guard<std::mutex> deviceMapLock(g_a2dpDeviceMapLock);
524 auto deviceIter = a2dpBluetoothDeviceMap_.find(macAddress);
525 if (deviceIter != a2dpBluetoothDeviceMap_.end()) {
526 device = deviceIter->second;
527 return SUCCESS;
528 }
529 return ERROR;
530 }
531
GetAllA2dpBluetoothDevice()532 std::vector<BluetoothRemoteDevice> MediaBluetoothDeviceManager::GetAllA2dpBluetoothDevice()
533 {
534 std::lock_guard<std::mutex> deviceMapLock(g_a2dpDeviceMapLock);
535 std::vector<BluetoothRemoteDevice> a2dpList = {};
536 a2dpList.reserve(a2dpBluetoothDeviceMap_.size());
537 for (const auto &[macaddr, device] : a2dpBluetoothDeviceMap_) {
538 a2dpList.emplace_back(device);
539 }
540 return a2dpList;
541 }
542
UpdateA2dpDeviceConfiguration(const BluetoothRemoteDevice & device,const AudioStreamInfo & streamInfo)543 void MediaBluetoothDeviceManager::UpdateA2dpDeviceConfiguration(const BluetoothRemoteDevice &device,
544 const AudioStreamInfo &streamInfo)
545 {
546 std::lock_guard<std::mutex> observerLock(g_observerLock);
547 CHECK_AND_RETURN_LOG(g_deviceObserver != nullptr, "UpdateA2dpDeviceConfiguration, device observer is null");
548 g_deviceObserver->OnDeviceConfigurationChanged(DEVICE_TYPE_BLUETOOTH_A2DP, device.GetDeviceAddr(),
549 device.GetDeviceName(), streamInfo);
550 }
551
ClearAllA2dpBluetoothDevice()552 void MediaBluetoothDeviceManager::ClearAllA2dpBluetoothDevice()
553 {
554 AUDIO_INFO_LOG("Bluetooth service crashed and enter the ClearAllA2dpBluetoothDevice.");
555 {
556 std::lock_guard<std::mutex> a2dpDeviceLock(g_a2dpDeviceLock);
557 privacyDevices_.clear();
558 commonDevices_.clear();
559 negativeDevices_.clear();
560 connectingDevices_.clear();
561 virtualDevices_.clear();
562 }
563 std::lock_guard<std::mutex> deviceMapLock(g_a2dpDeviceMapLock);
564 std::lock_guard<std::mutex> wearStateMapLock(g_a2dpWearStateMapLock);
565 a2dpBluetoothDeviceMap_.clear();
566 wearDetectionStateMap_.clear();
567 }
568
GetA2dpVirtualDeviceList()569 std::vector<BluetoothRemoteDevice> MediaBluetoothDeviceManager::GetA2dpVirtualDeviceList()
570 {
571 std::lock_guard<std::mutex> a2dpDeviceLock(g_hfpDeviceLock);
572 return virtualDevices_;
573 }
574
SetA2dpInStack(const BluetoothRemoteDevice & device,const AudioStreamInfo & streamInfo,int32_t action)575 void A2dpInBluetoothDeviceManager::SetA2dpInStack(const BluetoothRemoteDevice &device,
576 const AudioStreamInfo &streamInfo, int32_t action)
577 {
578 switch (action) {
579 case BluetoothDeviceAction::CONNECT_ACTION:
580 HandleConnectDevice(device, streamInfo);
581 break;
582 case BluetoothDeviceAction::DISCONNECT_ACTION:
583 HandleDisconnectDevice(device, streamInfo);
584 break;
585 default:
586 AUDIO_ERR_LOG("SetA2dpInStack failed due to the unknow action: %{public}d", action);
587 break;
588 }
589 }
590
HandleConnectDevice(const BluetoothRemoteDevice & device,const AudioStreamInfo & streamInfo)591 void A2dpInBluetoothDeviceManager::HandleConnectDevice(const BluetoothRemoteDevice &device,
592 const AudioStreamInfo &streamInfo)
593 {
594 if (IsA2dpInBluetoothDeviceExist(device.GetDeviceAddr())) {
595 return;
596 }
597 DeviceCategory bluetoothCategory = GetDeviceCategory(device);
598 AudioDeviceDescriptor desc;
599 desc.deviceCategory_ = bluetoothCategory;
600 switch (bluetoothCategory) {
601 case BT_HEADPHONE:
602 case BT_GLASSES:
603 case BT_SOUNDBOX:
604 case BT_CAR:
605 case BT_WATCH:
606 break;
607 default:
608 AUDIO_INFO_LOG("Unknow BT category, regard as bluetooth headset.");
609 desc.deviceCategory_ = BT_HEADPHONE;
610 break;
611 }
612 NotifyToUpdateAudioDevice(device, streamInfo, desc, DeviceStatus::ADD);
613 }
614
HandleDisconnectDevice(const BluetoothRemoteDevice & device,const AudioStreamInfo & streamInfo)615 void A2dpInBluetoothDeviceManager::HandleDisconnectDevice(const BluetoothRemoteDevice &device,
616 const AudioStreamInfo &streamInfo)
617 {
618 if (!IsA2dpInBluetoothDeviceExist(device.GetDeviceAddr())) {
619 AUDIO_WARNING_LOG("The device is already disconnected, ignore disconnect action.");
620 return;
621 }
622 AudioDeviceDescriptor desc;
623 desc.deviceCategory_ = CATEGORY_DEFAULT;
624 NotifyToUpdateAudioDevice(device, streamInfo, desc, DeviceStatus::REMOVE);
625 }
626
NotifyToUpdateAudioDevice(const BluetoothRemoteDevice & device,const AudioStreamInfo & streamInfo,AudioDeviceDescriptor & desc,DeviceStatus deviceStatus)627 void A2dpInBluetoothDeviceManager::NotifyToUpdateAudioDevice(const BluetoothRemoteDevice &device,
628 const AudioStreamInfo &streamInfo, AudioDeviceDescriptor &desc, DeviceStatus deviceStatus)
629 {
630 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP_IN;
631 desc.deviceRole_ = DeviceRole::INPUT_DEVICE;
632 desc.macAddress_ = device.GetDeviceAddr();
633 desc.deviceName_ = device.GetDeviceName();
634 desc.connectState_ = ConnectState::CONNECTED;
635 AUDIO_INFO_LOG("a2dpInBluetoothDeviceMap_ operation: %{public}d new bluetooth device, device address is %{public}s,\
636 category is %{public}d", deviceStatus, GetEncryptAddr(device.GetDeviceAddr()).c_str(), desc.deviceCategory_);
637 {
638 std::lock_guard<std::mutex> a2dpInDeviceMapLock(g_a2dpInDeviceMapLock);
639 if (deviceStatus == DeviceStatus::ADD) {
640 a2dpInBluetoothDeviceMap_[device.GetDeviceAddr()] = device;
641 } else if (deviceStatus == DeviceStatus::REMOVE) {
642 if (a2dpInBluetoothDeviceMap_.find(device.GetDeviceAddr()) != a2dpInBluetoothDeviceMap_.end()) {
643 a2dpInBluetoothDeviceMap_.erase(device.GetDeviceAddr());
644 }
645 }
646 }
647 {
648 std::lock_guard<std::mutex> a2dpInStreamInfoMapLock(g_a2dpInStreamInfoMapLock);
649 if (deviceStatus == DeviceStatus::ADD) {
650 a2dpInStreamInfoMap_[device.GetDeviceAddr()] = streamInfo;
651 } else if (deviceStatus == DeviceStatus::REMOVE) {
652 if (a2dpInStreamInfoMap_.find(device.GetDeviceAddr()) != a2dpInStreamInfoMap_.end()) {
653 a2dpInStreamInfoMap_.erase(device.GetDeviceAddr());
654 }
655 }
656 }
657 std::lock_guard<std::mutex> observerLock(g_observerLock);
658 CHECK_AND_RETURN_LOG(g_deviceObserver != nullptr, "NotifyToUpdateAudioDevice, device observer is null");
659 bool isConnected = deviceStatus == DeviceStatus::ADD;
660 g_deviceObserver->OnDeviceStatusUpdated(desc, isConnected);
661 }
662
GetA2dpInDeviceStreamInfo(const std::string & macAddress,AudioStreamInfo & streamInfo)663 bool A2dpInBluetoothDeviceManager::GetA2dpInDeviceStreamInfo(const std::string& macAddress, AudioStreamInfo &streamInfo)
664 {
665 std::lock_guard<std::mutex> a2dpInStreamInfoMapLock(g_a2dpInStreamInfoMapLock);
666 auto it = a2dpInStreamInfoMap_.find(macAddress);
667 if (it != a2dpInStreamInfoMap_.end()) {
668 streamInfo = it->second;
669 return true;
670 }
671 return false;
672 }
673
IsA2dpInBluetoothDeviceExist(const std::string & macAddress)674 bool A2dpInBluetoothDeviceManager::IsA2dpInBluetoothDeviceExist(const std::string& macAddress)
675 {
676 std::lock_guard<std::mutex> a2dpInDeviceMapLock(g_a2dpInDeviceMapLock);
677 if (a2dpInBluetoothDeviceMap_.find(macAddress) != a2dpInBluetoothDeviceMap_.end()) {
678 return true;
679 }
680 return false;
681 }
682
GetAllA2dpInBluetoothDevice()683 std::vector<BluetoothRemoteDevice> A2dpInBluetoothDeviceManager::GetAllA2dpInBluetoothDevice()
684 {
685 std::lock_guard<std::mutex> a2dpInDeviceMapLock(g_a2dpInDeviceMapLock);
686 std::vector<BluetoothRemoteDevice> a2dpInList = {};
687 a2dpInList.reserve(a2dpInBluetoothDeviceMap_.size());
688 for (const auto &[macaddr, device] : a2dpInBluetoothDeviceMap_) {
689 a2dpInList.emplace_back(device);
690 }
691 return a2dpInList;
692 }
693
ClearAllA2dpInBluetoothDevice()694 void A2dpInBluetoothDeviceManager::ClearAllA2dpInBluetoothDevice()
695 {
696 AUDIO_INFO_LOG("Bluetooth service crashed and enter the ClearAllA2dpInBluetoothDevice.");
697 std::lock_guard<std::mutex> a2dpInDeviceMapLock(g_a2dpInDeviceMapLock);
698 a2dpInBluetoothDeviceMap_.clear();
699 }
700
ClearAllA2dpInStreamInfo()701 void A2dpInBluetoothDeviceManager::ClearAllA2dpInStreamInfo()
702 {
703 std::lock_guard<std::mutex> a2dpInStreamMapLock(g_a2dpInStreamInfoMapLock);
704 a2dpInStreamInfoMap_.clear();
705 }
706
SetHfpStack(const BluetoothRemoteDevice & device,int action)707 void HfpBluetoothDeviceManager::SetHfpStack(const BluetoothRemoteDevice &device, int action)
708 {
709 switch (action) {
710 case BluetoothDeviceAction::CONNECTING_ACTION:
711 HandleConnectingDevice(device);
712 break;
713 case BluetoothDeviceAction::CONNECT_ACTION:
714 HandleConnectDevice(device);
715 break;
716 case BluetoothDeviceAction::DISCONNECT_ACTION:
717 HandleDisconnectDevice(device);
718 break;
719 case BluetoothDeviceAction::WEAR_ACTION:
720 HandleWearDevice(device);
721 break;
722 case BluetoothDeviceAction::UNWEAR_ACTION:
723 HandleUnwearDevice(device);
724 break;
725 case BluetoothDeviceAction::ENABLEFROMREMOTE_ACTION:
726 HandleEnableDevice(device);
727 break;
728 case BluetoothDeviceAction::DISABLEFROMREMOTE_ACTION:
729 HandleDisableDevice(device);
730 break;
731 case BluetoothDeviceAction::ENABLE_WEAR_DETECTION_ACTION:
732 HandleWearEnable(device);
733 break;
734 case BluetoothDeviceAction::DISABLE_WEAR_DETECTION_ACTION:
735 HandleWearDisable(device);
736 break;
737 case BluetoothDeviceAction::USER_SELECTION_ACTION:
738 HandleUserSelection(device);
739 break;
740 case BluetoothDeviceAction::STOP_VIRTUAL_CALL:
741 HandleStopVirtualCall(device);
742 break;
743 case BluetoothDeviceAction::VIRTUAL_DEVICE_ADD_ACTION:
744 HandleVirtualConnectDevice(device);
745 break;
746 case BluetoothDeviceAction::VIRTUAL_DEVICE_REMOVE_ACTION:
747 HandleRemoveVirtualConnectDevice(device);
748 break;
749 case BluetoothDeviceAction::CATEGORY_UPDATE_ACTION:
750 HandleUpdateDeviceCategory(device);
751 break;
752 default:
753 AUDIO_ERR_LOG("SetHfpStack failed due to the unknow action: %{public}d", action);
754 break;
755 }
756 }
757
HandleConnectingDevice(const BluetoothRemoteDevice & device)758 void HfpBluetoothDeviceManager::HandleConnectingDevice(const BluetoothRemoteDevice &device)
759 {
760 if (IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
761 return;
762 }
763 AddDeviceInConfigVector(device, connectingDevices_);
764 }
765
HandleConnectDevice(const BluetoothRemoteDevice & device)766 void HfpBluetoothDeviceManager::HandleConnectDevice(const BluetoothRemoteDevice &device)
767 {
768 if (IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
769 return;
770 }
771 AudioDeviceDescriptor desc = HandleConnectDeviceInner(device);
772 NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD);
773 }
774
HandleConnectDeviceInner(const BluetoothRemoteDevice & device)775 AudioDeviceDescriptor HfpBluetoothDeviceManager::HandleConnectDeviceInner(const BluetoothRemoteDevice &device)
776 {
777 RemoveDeviceInConfigVector(device, connectingDevices_);
778 // If the device was virtual connected, remove it from the negativeDevices_ list.
779 RemoveDeviceInConfigVector(device, negativeDevices_);
780 DeviceCategory bluetoothCategory = GetDeviceCategory(device);
781 HandsFreeAudioGateway *hfpInstance = HandsFreeAudioGateway::GetProfile();
782 CHECK_AND_RETURN_RET_LOG(hfpInstance != nullptr, ERROR, "HFP AG profile instance unavailable");
783 AudioDeviceDescriptor desc;
784 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
785 desc.macAddress_ = device.GetDeviceAddr();
786 desc.deviceCategory_ = bluetoothCategory;
787 hfpInstance->IsVoiceRecognitionSupported(device, desc.isVrSupported_);
788 AUDIO_INFO_LOG("Device supports recognition = %{public}d", desc.isVrSupported_);
789 switch (bluetoothCategory) {
790 case BT_GLASSES:
791 case BT_HEADPHONE:
792 if (IsBTWearDetectionEnable(device)) {
793 AddDeviceInConfigVector(device, negativeDevices_);
794 desc.deviceCategory_ = BT_UNWEAR_HEADPHONE;
795 } else {
796 AddDeviceInConfigVector(device, privacyDevices_);
797 }
798 break;
799 case BT_SOUNDBOX:
800 case BT_CAR:
801 AddDeviceInConfigVector(device, commonDevices_);
802 break;
803 case BT_WATCH:
804 AddDeviceInConfigVector(device, negativeDevices_);
805 break;
806 default:
807 AUDIO_WARNING_LOG("Unknow BT category, regard as bluetooth headset.");
808 AddDeviceInConfigVector(device, privacyDevices_);
809 desc.deviceCategory_ = BT_HEADPHONE;
810 break;
811 }
812 return desc;
813 }
814
HandleDisconnectDevice(const BluetoothRemoteDevice & device)815 void HfpBluetoothDeviceManager::HandleDisconnectDevice(const BluetoothRemoteDevice &device)
816 {
817 RemoveDeviceInConfigVector(device, connectingDevices_);
818 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
819 AUDIO_INFO_LOG("The device is already disconnected, ignore disconnect action.");
820 return;
821 }
822 RemoveDeviceInConfigVector(device, privacyDevices_);
823 RemoveDeviceInConfigVector(device, commonDevices_);
824 RemoveDeviceInConfigVector(device, negativeDevices_);
825 {
826 std::lock_guard<std::mutex> wearStateMapLock(g_hfpWearStateMapLock);
827 wearDetectionStateMap_.erase(device.GetDeviceAddr());
828 }
829 AudioDeviceDescriptor desc;
830 desc.deviceCategory_ = CATEGORY_DEFAULT;
831 NotifyToUpdateAudioDevice(device, desc, DeviceStatus::REMOVE);
832 }
833
HandleWearDevice(const BluetoothRemoteDevice & device)834 void HfpBluetoothDeviceManager::HandleWearDevice(const BluetoothRemoteDevice &device)
835 {
836 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
837 AUDIO_ERR_LOG("HandleWearDevice failed for the device has not be reported the connected action.");
838 return;
839 }
840 RemoveDeviceInConfigVector(device, negativeDevices_);
841 RemoveDeviceInConfigVector(device, privacyDevices_);
842 AddDeviceInConfigVector(device, privacyDevices_);
843 {
844 std::lock_guard<std::mutex> wearStateMapLock(g_hfpWearStateMapLock);
845 wearDetectionStateMap_[device.GetDeviceAddr()] = BluetoothDeviceAction::WEAR_ACTION;
846 }
847 AudioDeviceDescriptor desc;
848 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
849 desc.macAddress_ = device.GetDeviceAddr();
850 desc.deviceCategory_ = BT_HEADPHONE;
851 OnDeviceCategoryUpdated(device, desc);
852 }
853
HandleUnwearDevice(const BluetoothRemoteDevice & device)854 void HfpBluetoothDeviceManager::HandleUnwearDevice(const BluetoothRemoteDevice &device)
855 {
856 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
857 AUDIO_ERR_LOG("HandleWearDevice failed for the device has not worn.");
858 return;
859 }
860 RemoveDeviceInConfigVector(device, privacyDevices_);
861 RemoveDeviceInConfigVector(device, negativeDevices_);
862 AddDeviceInConfigVector(device, negativeDevices_);
863 {
864 std::lock_guard<std::mutex> wearStateMapLock(g_hfpWearStateMapLock);
865 wearDetectionStateMap_[device.GetDeviceAddr()] = BluetoothDeviceAction::UNWEAR_ACTION;
866 }
867 AudioDeviceDescriptor desc;
868 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
869 desc.macAddress_ = device.GetDeviceAddr();
870 desc.deviceCategory_ = BT_UNWEAR_HEADPHONE;
871 OnDeviceCategoryUpdated(device, desc);
872 }
873
HandleEnableDevice(const BluetoothRemoteDevice & device)874 void HfpBluetoothDeviceManager::HandleEnableDevice(const BluetoothRemoteDevice &device)
875 {
876 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
877 AUDIO_ERR_LOG("HandleEnableDevice failed for the device has not connected.");
878 return;
879 }
880 AudioDeviceDescriptor desc;
881 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
882 desc.macAddress_ = device.GetDeviceAddr();
883 desc.isEnable_ = true;
884 OnDeviceEnableUpdated(device, desc);
885 }
886
HandleDisableDevice(const BluetoothRemoteDevice & device)887 void HfpBluetoothDeviceManager::HandleDisableDevice(const BluetoothRemoteDevice &device)
888 {
889 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
890 AUDIO_ERR_LOG("HandleDisableDevice failed for the device has not connected.");
891 return;
892 }
893 AudioDeviceDescriptor desc;
894 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
895 desc.macAddress_ = device.GetDeviceAddr();
896 desc.isEnable_ = false;
897 OnDeviceEnableUpdated(device, desc);
898 }
899
HandleWearEnable(const BluetoothRemoteDevice & device)900 void HfpBluetoothDeviceManager::HandleWearEnable(const BluetoothRemoteDevice &device)
901 {
902 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
903 AUDIO_ERR_LOG("HandleWearEnable failed for the device has not connected.");
904 return;
905 }
906 RemoveDeviceInConfigVector(device, negativeDevices_);
907 RemoveDeviceInConfigVector(device, privacyDevices_);
908 AudioDeviceDescriptor desc;
909 {
910 std::lock_guard<std::mutex> wearStateMapLock(g_hfpWearStateMapLock);
911 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
912 desc.macAddress_ = device.GetDeviceAddr();
913 auto wearStateIter = wearDetectionStateMap_.find(device.GetDeviceAddr());
914 if (wearStateIter != wearDetectionStateMap_.end() &&
915 wearStateIter->second == BluetoothDeviceAction::WEAR_ACTION) {
916 AddDeviceInConfigVector(device, privacyDevices_);
917 desc.deviceCategory_ = BT_HEADPHONE;
918 } else {
919 AddDeviceInConfigVector(device, negativeDevices_);
920 desc.deviceCategory_ = BT_UNWEAR_HEADPHONE;
921 }
922 }
923 OnDeviceCategoryUpdated(device, desc);
924 }
925
HandleWearDisable(const BluetoothRemoteDevice & device)926 void HfpBluetoothDeviceManager::HandleWearDisable(const BluetoothRemoteDevice &device)
927 {
928 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
929 AUDIO_ERR_LOG("HandleWearDisable failed for the device has not connected.");
930 return;
931 }
932 RemoveDeviceInConfigVector(device, privacyDevices_);
933 RemoveDeviceInConfigVector(device, negativeDevices_);
934 AddDeviceInConfigVector(device, privacyDevices_);
935 AudioDeviceDescriptor desc;
936 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
937 desc.macAddress_ = device.GetDeviceAddr();
938 desc.deviceCategory_ = BT_HEADPHONE;
939 OnDeviceCategoryUpdated(device, desc);
940 }
941
HandleUserSelection(const BluetoothRemoteDevice & device)942 void HfpBluetoothDeviceManager::HandleUserSelection(const BluetoothRemoteDevice &device)
943 {
944 std::string deviceAddr = device.GetDeviceAddr();
945 DeviceCategory bluetoothCategory = GetDeviceCategory(device);
946 if (bluetoothCategory == BT_WATCH) {
947 std::lock_guard<std::mutex> wearStateMapLock(g_hfpWearStateMapLock);
948 std::lock_guard<std::mutex> hfpDeviceLock(g_hfpDeviceLock);
949 auto isPresent = [] (BluetoothRemoteDevice &bluetoothRemoteDevice) {
950 return wearDetectionStateMap_[bluetoothRemoteDevice.GetDeviceAddr()] == WEAR_ACTION;
951 };
952 auto deviceIter = std::find_if(privacyDevices_.rbegin(), privacyDevices_.rend(), isPresent);
953 if (deviceIter != privacyDevices_.rend()) {
954 deviceAddr = deviceIter->GetDeviceAddr();
955 AUDIO_WARNING_LOG("Change user select device from watch %{public}s to wear headphone %{public}s",
956 GetEncryptAddr(device.GetDeviceAddr()).c_str(), GetEncryptAddr(deviceAddr).c_str());
957 }
958 }
959 std::lock_guard<std::mutex> observerLock(g_observerLock);
960 if (g_deviceObserver != nullptr) {
961 g_deviceObserver->OnForcedDeviceSelected(DEVICE_TYPE_BLUETOOTH_SCO, deviceAddr);
962 }
963 }
964
HandleStopVirtualCall(const BluetoothRemoteDevice & device)965 void HfpBluetoothDeviceManager::HandleStopVirtualCall(const BluetoothRemoteDevice &device)
966 {
967 {
968 std::lock_guard<std::mutex> handleLock(stopVirtualCallHandleLock_);
969 stopVirtualCallHandle_.device = device;
970 stopVirtualCallHandle_.isWaitingForStoppingVirtualCall = true;
971 }
972 TryDisconnectScoAsync(device);
973 }
974
HandleVirtualConnectDevice(const BluetoothRemoteDevice & device)975 void HfpBluetoothDeviceManager::HandleVirtualConnectDevice(const BluetoothRemoteDevice &device)
976 {
977 AddDeviceInConfigVector(device, virtualDevices_);
978 DeviceCategory bluetoothCategory = GetDeviceCategory(device);
979 AudioDeviceDescriptor desc;
980 desc.deviceCategory_ = bluetoothCategory;
981 AddDeviceInConfigVector(device, negativeDevices_);
982 NotifyToUpdateVirtualDevice(device, desc, DeviceStatus::VIRTUAL_ADD);
983 }
984
HandleRemoveVirtualConnectDevice(const BluetoothRemoteDevice & device)985 void HfpBluetoothDeviceManager::HandleRemoveVirtualConnectDevice(const BluetoothRemoteDevice &device)
986 {
987 RemoveDeviceInConfigVector(device, virtualDevices_);
988 RemoveDeviceInConfigVector(device, negativeDevices_);
989 AudioDeviceDescriptor desc;
990 desc.deviceCategory_ = CATEGORY_DEFAULT;
991 NotifyToUpdateVirtualDevice(device, desc, DeviceStatus::VIRTUAL_REMOVE);
992 }
993
HandleUpdateDeviceCategory(const BluetoothRemoteDevice & device)994 void HfpBluetoothDeviceManager::HandleUpdateDeviceCategory(const BluetoothRemoteDevice &device)
995 {
996 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
997 AUDIO_WARNING_LOG("HandleUpdateDeviceCategory failed for the device has not be reported the connected action.");
998 return;
999 }
1000 AudioDeviceDescriptor desc = HandleConnectDeviceInner(device);
1001 int32_t wearState = BluetoothAudioManager::GetInstance().IsDeviceWearing(device);
1002 if (wearState == 1 && desc.deviceCategory_ == BT_UNWEAR_HEADPHONE) { // 1 wear state
1003 desc.deviceCategory_ = BT_HEADPHONE;
1004 }
1005 OnDeviceCategoryUpdated(device, desc);
1006 }
1007
AddDeviceInConfigVector(const BluetoothRemoteDevice & device,std::vector<BluetoothRemoteDevice> & deviceVector)1008 void HfpBluetoothDeviceManager::AddDeviceInConfigVector(const BluetoothRemoteDevice &device,
1009 std::vector<BluetoothRemoteDevice> &deviceVector)
1010 {
1011 std::lock_guard<std::mutex> hfpDeviceLock(g_hfpDeviceLock);
1012 auto isPresent = [&device] (BluetoothRemoteDevice &bluetoothRemoteDevice) {
1013 return device.GetDeviceAddr() == bluetoothRemoteDevice.GetDeviceAddr();
1014 };
1015 auto deviceIter = std::find_if(deviceVector.begin(), deviceVector.end(), isPresent);
1016 if (deviceIter == deviceVector.end()) {
1017 deviceVector.push_back(device);
1018 }
1019 }
1020
RemoveDeviceInConfigVector(const BluetoothRemoteDevice & device,std::vector<BluetoothRemoteDevice> & deviceVector)1021 void HfpBluetoothDeviceManager::RemoveDeviceInConfigVector(const BluetoothRemoteDevice &device,
1022 std::vector<BluetoothRemoteDevice> &deviceVector)
1023 {
1024 std::lock_guard<std::mutex> hfpDeviceLock(g_hfpDeviceLock);
1025 auto isPresent = [&device] (BluetoothRemoteDevice &bluetoothRemoteDevice) {
1026 return device.GetDeviceAddr() == bluetoothRemoteDevice.GetDeviceAddr();
1027 };
1028 auto deviceIter = std::find_if(deviceVector.begin(), deviceVector.end(), isPresent);
1029 if (deviceIter != deviceVector.end()) {
1030 deviceVector.erase(deviceIter);
1031 }
1032 }
1033
NotifyToUpdateAudioDevice(const BluetoothRemoteDevice & device,AudioDeviceDescriptor & desc,DeviceStatus deviceStatus)1034 void HfpBluetoothDeviceManager::NotifyToUpdateAudioDevice(const BluetoothRemoteDevice &device,
1035 AudioDeviceDescriptor &desc, DeviceStatus deviceStatus)
1036 {
1037 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
1038 desc.macAddress_ = device.GetDeviceAddr();
1039 desc.deviceName_ = device.GetDeviceName();
1040 desc.connectState_ = ConnectState::DEACTIVE_CONNECTED;
1041 AUDIO_WARNING_LOG("hfpBluetoothDeviceMap_ operation: %{public}d new bluetooth device, device address is %{public}s,\
1042 category is %{public}d", deviceStatus, GetEncryptAddr(device.GetDeviceAddr()).c_str(), desc.deviceCategory_);
1043 {
1044 std::lock_guard<std::mutex> deviceMapLock(g_hfpDeviceMapLock);
1045 if (deviceStatus == DeviceStatus::ADD) {
1046 hfpBluetoothDeviceMap_[device.GetDeviceAddr()] = device;
1047 } else if (deviceStatus == DeviceStatus::REMOVE) {
1048 if (hfpBluetoothDeviceMap_.find(device.GetDeviceAddr()) != hfpBluetoothDeviceMap_.end()) {
1049 hfpBluetoothDeviceMap_.erase(device.GetDeviceAddr());
1050 }
1051 }
1052 }
1053 std::lock_guard<std::mutex> observerLock(g_observerLock);
1054 if (g_deviceObserver == nullptr) {
1055 AUDIO_ERR_LOG("NotifyToUpdateAudioDevice, device observer is null");
1056 return;
1057 }
1058 bool isConnected = deviceStatus == DeviceStatus::ADD;
1059 g_deviceObserver->OnDeviceStatusUpdated(desc, isConnected);
1060 }
1061
NotifyToUpdateVirtualDevice(const BluetoothRemoteDevice & device,AudioDeviceDescriptor & desc,DeviceStatus deviceStatus)1062 void HfpBluetoothDeviceManager::NotifyToUpdateVirtualDevice(const BluetoothRemoteDevice &device,
1063 AudioDeviceDescriptor &desc, DeviceStatus deviceStatus)
1064 {
1065 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
1066 desc.macAddress_ = device.GetDeviceAddr();
1067 desc.deviceName_ = device.GetDeviceName();
1068 desc.connectState_ = ConnectState::VIRTUAL_CONNECTED;
1069 std::lock_guard<std::mutex> observerLock(g_observerLock);
1070 CHECK_AND_RETURN_LOG(g_deviceObserver != nullptr, "NotifyToUpdateVirtualDevice, device observer is null");
1071 bool isConnected = deviceStatus == DeviceStatus::VIRTUAL_ADD;
1072 g_deviceObserver->OnDeviceStatusUpdated(desc, isConnected);
1073 }
1074
IsHfpBluetoothDeviceExist(const std::string & macAddress)1075 bool HfpBluetoothDeviceManager::IsHfpBluetoothDeviceExist(const std::string& macAddress)
1076 {
1077 std::lock_guard<std::mutex> deviceMapLock(g_hfpDeviceMapLock);
1078 if (hfpBluetoothDeviceMap_.find(macAddress) != hfpBluetoothDeviceMap_.end()) {
1079 return true;
1080 }
1081 return false;
1082 }
1083
IsHfpBluetoothDeviceConnecting(const std::string & macAddress)1084 bool HfpBluetoothDeviceManager::IsHfpBluetoothDeviceConnecting(const std::string& macAddress)
1085 {
1086 std::lock_guard<std::mutex> hfpDeviceLock(g_hfpDeviceLock);
1087 auto deviceIter = std::find_if(connectingDevices_.begin(), connectingDevices_.end(),
1088 [&macAddress] (BluetoothRemoteDevice &bluetoothRemoteDevice) {
1089 return macAddress == bluetoothRemoteDevice.GetDeviceAddr();
1090 });
1091 if (deviceIter != connectingDevices_.end()) {
1092 return true;
1093 }
1094 return false;
1095 }
1096
GetConnectedHfpBluetoothDevice(const std::string & macAddress,BluetoothRemoteDevice & device)1097 int32_t HfpBluetoothDeviceManager::GetConnectedHfpBluetoothDevice(const std::string& macAddress,
1098 BluetoothRemoteDevice &device)
1099 {
1100 std::lock_guard<std::mutex> deviceMapLock(g_hfpDeviceMapLock);
1101 auto deviceIter = hfpBluetoothDeviceMap_.find(macAddress);
1102 if (deviceIter != hfpBluetoothDeviceMap_.end()) {
1103 device = deviceIter->second;
1104 return SUCCESS;
1105 }
1106 return ERROR;
1107 }
1108
GetAllHfpBluetoothDevice()1109 std::vector<BluetoothRemoteDevice> HfpBluetoothDeviceManager::GetAllHfpBluetoothDevice()
1110 {
1111 std::lock_guard<std::mutex> deviceMapLock(g_hfpDeviceMapLock);
1112 std::vector<BluetoothRemoteDevice> hfpList = {};
1113 hfpList.reserve(hfpBluetoothDeviceMap_.size());
1114 for (const auto &[macaddr, device] : hfpBluetoothDeviceMap_) {
1115 hfpList.emplace_back(device);
1116 }
1117 return hfpList;
1118 }
1119
ClearAllHfpBluetoothDevice()1120 void HfpBluetoothDeviceManager::ClearAllHfpBluetoothDevice()
1121 {
1122 AUDIO_WARNING_LOG("Bluetooth service crashed and enter the ClearAllhfpBluetoothDevice.");
1123 {
1124 std::lock_guard<std::mutex> hfpDeviceLock(g_hfpDeviceLock);
1125 privacyDevices_.clear();
1126 commonDevices_.clear();
1127 negativeDevices_.clear();
1128 connectingDevices_.clear();
1129 virtualDevices_.clear();
1130 }
1131 std::lock_guard<std::mutex> deviceMapLock(g_hfpDeviceMapLock);
1132 std::lock_guard<std::mutex> wearStateMapLock(g_hfpWearStateMapLock);
1133 hfpBluetoothDeviceMap_.clear();
1134 wearDetectionStateMap_.clear();
1135 }
1136
OnScoStateChanged(const BluetoothRemoteDevice & device,bool isConnected,int reason)1137 void HfpBluetoothDeviceManager::OnScoStateChanged(const BluetoothRemoteDevice &device, bool isConnected, int reason)
1138 {
1139 AudioDeviceDescriptor desc;
1140 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
1141 desc.macAddress_ = device.GetDeviceAddr();
1142 if (isConnected) {
1143 desc.connectState_ = ConnectState::CONNECTED;
1144 } else {
1145 {
1146 std::lock_guard<std::mutex> handleLock(stopVirtualCallHandleLock_);
1147 if (device.GetDeviceAddr() == stopVirtualCallHandle_.device.GetDeviceAddr() &&
1148 stopVirtualCallHandle_.isWaitingForStoppingVirtualCall) {
1149 AUDIO_WARNING_LOG("reason change to %{public}d", HFP_AG_SCO_REMOTE_USER_TERMINATED);
1150 reason = HFP_AG_SCO_REMOTE_USER_TERMINATED;
1151 stopVirtualCallHandle_.device = BluetoothRemoteDevice();
1152 stopVirtualCallHandle_.isWaitingForStoppingVirtualCall = false;
1153 }
1154 }
1155 desc.connectState_ = reason == HFP_AG_SCO_REMOTE_USER_TERMINATED ? ConnectState::SUSPEND_CONNECTED
1156 : ConnectState::DEACTIVE_CONNECTED;
1157 }
1158 std::lock_guard<std::mutex> observerLock(g_observerLock);
1159 if (g_deviceObserver != nullptr) {
1160 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CONNECTSTATE_UPDATE);
1161 }
1162 }
1163
GetHfpVirtualDeviceList()1164 std::vector<BluetoothRemoteDevice> HfpBluetoothDeviceManager::GetHfpVirtualDeviceList()
1165 {
1166 std::lock_guard<std::mutex> hfpDeviceLock(g_hfpDeviceLock);
1167 return virtualDevices_;
1168 }
1169
RegisterDisconnectScoFunc(DisconnectScoForDevice func)1170 void HfpBluetoothDeviceManager::RegisterDisconnectScoFunc(DisconnectScoForDevice func)
1171 {
1172 disconnectScoFun_ = func;
1173 }
1174
TryDisconnectScoAsync(const BluetoothRemoteDevice & device)1175 void HfpBluetoothDeviceManager::TryDisconnectScoAsync(const BluetoothRemoteDevice &device)
1176 {
1177 std::thread disconnectScoThread = std::thread([device]() {
1178 if (HfpBluetoothDeviceManager::disconnectScoFun_ != nullptr) {
1179 AUDIO_INFO_LOG("bluetooth service trigger disconnect sco async");
1180 HfpBluetoothDeviceManager::disconnectScoFun_(device);
1181 }
1182 });
1183 disconnectScoThread.detach();
1184 }
1185
TryDisconnectScoSync(const BluetoothRemoteDevice & device,const std::string & reason)1186 void HfpBluetoothDeviceManager::TryDisconnectScoSync(const BluetoothRemoteDevice &device, const std::string &reason)
1187 {
1188 if (disconnectScoFun_ != nullptr) {
1189 AUDIO_INFO_LOG("bluetooth service trigger disconnect %{public}s sco sync with reason %{public}s",
1190 GetEncryptAddr(device.GetDeviceAddr()).c_str(), reason.c_str());
1191 disconnectScoFun_(device);
1192 }
1193 }
1194
OnDeviceCategoryUpdated(const BluetoothRemoteDevice & device,AudioDeviceDescriptor & desc)1195 void HfpBluetoothDeviceManager::OnDeviceCategoryUpdated(const BluetoothRemoteDevice &device,
1196 AudioDeviceDescriptor &desc)
1197 {
1198 {
1199 std::lock_guard<std::mutex> observerLock(g_observerLock);
1200 if (g_deviceObserver != nullptr) {
1201 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
1202 }
1203 }
1204 if (desc.deviceCategory_ == BT_UNWEAR_HEADPHONE) {
1205 TryDisconnectScoSync(device, "BT_UNWEAR_HEADPHONE");
1206 }
1207 }
1208
OnDeviceEnableUpdated(const BluetoothRemoteDevice & device,AudioDeviceDescriptor & desc)1209 void HfpBluetoothDeviceManager::OnDeviceEnableUpdated(const BluetoothRemoteDevice &device,
1210 AudioDeviceDescriptor &desc)
1211 {
1212 {
1213 std::lock_guard<std::mutex> observerLock(g_observerLock);
1214 if (g_deviceObserver != nullptr) {
1215 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::ENABLE_UPDATE);
1216 }
1217 }
1218 if (!desc.isEnable_) {
1219 TryDisconnectScoSync(device, "Device Disable");
1220 }
1221 }
1222 // LCOV_EXCL_STOP
1223 } // namespace Bluetooth
1224 } // namespace OHOS
1225