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