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 case BluetoothDeviceAction::CATEGORY_UPDATE_ACTION:
190 HandleUpdateDeviceCategory(device);
191 break;
192 default:
193 AUDIO_ERR_LOG("SetMediaStack failed due to the unknow action: %{public}d", action);
194 break;
195 }
196 }
197
HandleConnectingDevice(const BluetoothRemoteDevice & device)198 void MediaBluetoothDeviceManager::HandleConnectingDevice(const BluetoothRemoteDevice &device)
199 {
200 if (IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
201 return;
202 }
203 AddDeviceInConfigVector(device, connectingDevices_);
204 }
205
HandleConnectDevice(const BluetoothRemoteDevice & device)206 void MediaBluetoothDeviceManager::HandleConnectDevice(const BluetoothRemoteDevice &device)
207 {
208 if (IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
209 return;
210 }
211 AudioDeviceDescriptor desc = HandleConnectDeviceInner(device);
212 NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD);
213 }
214
HandleConnectDeviceInner(const BluetoothRemoteDevice & device)215 AudioDeviceDescriptor MediaBluetoothDeviceManager::HandleConnectDeviceInner(const BluetoothRemoteDevice &device)
216 {
217 RemoveDeviceInConfigVector(device, connectingDevices_);
218 // If the device was virtual connected, remove it from the negativeDevices_ list.
219 RemoveDeviceInConfigVector(device, negativeDevices_);
220 RemoveDeviceInConfigVector(device, virtualDevices_);
221 DeviceCategory bluetoothCategory = GetDeviceCategory(device);
222 AudioDeviceDescriptor desc;
223 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
224 desc.macAddress_ = device.GetDeviceAddr();
225 desc.deviceCategory_ = bluetoothCategory;
226 switch (bluetoothCategory) {
227 case BT_GLASSES:
228 case BT_HEADPHONE:
229 if (IsBTWearDetectionEnable(device)) {
230 AddDeviceInConfigVector(device, negativeDevices_);
231 desc.deviceCategory_ = BT_UNWEAR_HEADPHONE;
232 } else {
233 AddDeviceInConfigVector(device, privacyDevices_);
234 }
235 break;
236 case BT_SOUNDBOX:
237 case BT_CAR:
238 AddDeviceInConfigVector(device, commonDevices_);
239 break;
240 case BT_WATCH:
241 AddDeviceInConfigVector(device, negativeDevices_);
242 break;
243 default:
244 AUDIO_WARNING_LOG("Unknow BT category, regard as bluetooth headset.");
245 AddDeviceInConfigVector(device, privacyDevices_);
246 desc.deviceCategory_ = BT_HEADPHONE;
247 break;
248 }
249 return desc;
250 }
251
HandleDisconnectDevice(const BluetoothRemoteDevice & device)252 void MediaBluetoothDeviceManager::HandleDisconnectDevice(const BluetoothRemoteDevice &device)
253 {
254 RemoveDeviceInConfigVector(device, connectingDevices_);
255 if (!IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
256 AUDIO_INFO_LOG("The device is already disconnected, ignore disconnect action.");
257 return;
258 }
259 RemoveDeviceInConfigVector(device, privacyDevices_);
260 RemoveDeviceInConfigVector(device, commonDevices_);
261 RemoveDeviceInConfigVector(device, negativeDevices_);
262 {
263 std::lock_guard<std::mutex> wearStateMapLock(g_a2dpWearStateMapLock);
264 wearDetectionStateMap_.erase(device.GetDeviceAddr());
265 }
266 AudioDeviceDescriptor desc;
267 desc.deviceCategory_ = CATEGORY_DEFAULT;
268 NotifyToUpdateAudioDevice(device, desc, DeviceStatus::REMOVE);
269 }
270
HandleWearDevice(const BluetoothRemoteDevice & device)271 void MediaBluetoothDeviceManager::HandleWearDevice(const BluetoothRemoteDevice &device)
272 {
273 bool isDeviceExist = IsA2dpBluetoothDeviceExist(device.GetDeviceAddr());
274 CHECK_AND_RETURN_LOG(isDeviceExist,
275 "HandleWearDevice failed for the device has not be reported the connected action.");
276 RemoveDeviceInConfigVector(device, negativeDevices_);
277 RemoveDeviceInConfigVector(device, privacyDevices_);
278 AddDeviceInConfigVector(device, privacyDevices_);
279 {
280 std::lock_guard<std::mutex> wearStateMapLock(g_a2dpWearStateMapLock);
281 wearDetectionStateMap_[device.GetDeviceAddr()] = BluetoothDeviceAction::WEAR_ACTION;
282 }
283 AudioDeviceDescriptor desc;
284 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
285 desc.macAddress_ = device.GetDeviceAddr();
286 desc.deviceCategory_ = BT_HEADPHONE;
287 std::lock_guard<std::mutex> observerLock(g_observerLock);
288 if (g_deviceObserver != nullptr) {
289 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
290 }
291 }
292
HandleUnwearDevice(const BluetoothRemoteDevice & device)293 void MediaBluetoothDeviceManager::HandleUnwearDevice(const BluetoothRemoteDevice &device)
294 {
295 bool isDeviceExist = IsA2dpBluetoothDeviceExist(device.GetDeviceAddr());
296 CHECK_AND_RETURN_LOG(isDeviceExist, "HandleWearDevice failed for the device has not worn.");
297 RemoveDeviceInConfigVector(device, privacyDevices_);
298 RemoveDeviceInConfigVector(device, negativeDevices_);
299 AddDeviceInConfigVector(device, negativeDevices_);
300 {
301 std::lock_guard<std::mutex> wearStateMapLock(g_a2dpWearStateMapLock);
302 wearDetectionStateMap_[device.GetDeviceAddr()] = BluetoothDeviceAction::UNWEAR_ACTION;
303 }
304 AudioDeviceDescriptor desc;
305 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
306 desc.macAddress_ = device.GetDeviceAddr();
307 desc.deviceCategory_ = BT_UNWEAR_HEADPHONE;
308 std::lock_guard<std::mutex> observerLock(g_observerLock);
309 if (g_deviceObserver != nullptr) {
310 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
311 }
312 }
313
HandleEnableDevice(const BluetoothRemoteDevice & device)314 void MediaBluetoothDeviceManager::HandleEnableDevice(const BluetoothRemoteDevice &device)
315 {
316 if (!IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
317 AUDIO_ERR_LOG("HandleEnableDevice failed for the device has not connected.");
318 return;
319 }
320 AudioDeviceDescriptor desc;
321 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
322 desc.macAddress_ = device.GetDeviceAddr();
323 desc.isEnable_ = true;
324 std::lock_guard<std::mutex> observerLock(g_observerLock);
325 if (g_deviceObserver != nullptr) {
326 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::ENABLE_UPDATE);
327 }
328 }
329
HandleDisableDevice(const BluetoothRemoteDevice & device)330 void MediaBluetoothDeviceManager::HandleDisableDevice(const BluetoothRemoteDevice &device)
331 {
332 if (!IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
333 AUDIO_ERR_LOG("HandleDisableDevice failed for the device has not connected.");
334 return;
335 }
336 AudioDeviceDescriptor desc;
337 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
338 desc.macAddress_ = device.GetDeviceAddr();
339 desc.isEnable_ = false;
340 std::lock_guard<std::mutex> observerLock(g_observerLock);
341 if (g_deviceObserver != nullptr) {
342 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::ENABLE_UPDATE);
343 }
344 }
345
HandleWearEnable(const BluetoothRemoteDevice & device)346 void MediaBluetoothDeviceManager::HandleWearEnable(const BluetoothRemoteDevice &device)
347 {
348 if (!IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
349 AUDIO_ERR_LOG("HandleWearEnable failed for the device has not connected.");
350 return;
351 }
352 RemoveDeviceInConfigVector(device, negativeDevices_);
353 RemoveDeviceInConfigVector(device, privacyDevices_);
354 std::lock_guard<std::mutex> wearStateMapLock(g_a2dpWearStateMapLock);
355 AudioDeviceDescriptor desc;
356 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
357 desc.macAddress_ = device.GetDeviceAddr();
358 auto wearStateIter = wearDetectionStateMap_.find(device.GetDeviceAddr());
359 if (wearStateIter != wearDetectionStateMap_.end() &&
360 wearStateIter->second == BluetoothDeviceAction::WEAR_ACTION) {
361 AddDeviceInConfigVector(device, privacyDevices_);
362 desc.deviceCategory_ = BT_HEADPHONE;
363 } else {
364 AddDeviceInConfigVector(device, negativeDevices_);
365 desc.deviceCategory_ = BT_UNWEAR_HEADPHONE;
366 }
367 std::lock_guard<std::mutex> observerLock(g_observerLock);
368 if (g_deviceObserver != nullptr) {
369 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
370 }
371 }
372
HandleWearDisable(const BluetoothRemoteDevice & device)373 void MediaBluetoothDeviceManager::HandleWearDisable(const BluetoothRemoteDevice &device)
374 {
375 if (!IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
376 AUDIO_ERR_LOG("HandleWearDisable failed for the device has not connected.");
377 return;
378 }
379 RemoveDeviceInConfigVector(device, privacyDevices_);
380 RemoveDeviceInConfigVector(device, negativeDevices_);
381 AddDeviceInConfigVector(device, privacyDevices_);
382 AudioDeviceDescriptor desc;
383 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
384 desc.macAddress_ = device.GetDeviceAddr();
385 desc.deviceCategory_ = BT_HEADPHONE;
386 std::lock_guard<std::mutex> observerLock(g_observerLock);
387 if (g_deviceObserver != nullptr) {
388 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
389 }
390 }
391
HandleUserSelection(const BluetoothRemoteDevice & device)392 void MediaBluetoothDeviceManager::HandleUserSelection(const BluetoothRemoteDevice &device)
393 {
394 std::lock_guard<std::mutex> observerLock(g_observerLock);
395 if (g_deviceObserver != nullptr) {
396 g_deviceObserver->OnForcedDeviceSelected(DEVICE_TYPE_BLUETOOTH_A2DP, device.GetDeviceAddr());
397 }
398 }
399
HandleVirtualConnectDevice(const BluetoothRemoteDevice & device)400 void MediaBluetoothDeviceManager::HandleVirtualConnectDevice(const BluetoothRemoteDevice &device)
401 {
402 if (IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
403 AUDIO_WARNING_LOG("The device is already removed as virtual Devices, ignore remove action.");
404 return;
405 }
406 AddDeviceInConfigVector(device, virtualDevices_);
407 DeviceCategory bluetoothCategory = GetDeviceCategory(device);
408 AudioDeviceDescriptor desc;
409 desc.deviceCategory_ = bluetoothCategory;
410 AddDeviceInConfigVector(device, negativeDevices_);
411 NotifyToUpdateVirtualDevice(device, desc, DeviceStatus::VIRTUAL_ADD);
412 }
413
HandleRemoveVirtualConnectDevice(const BluetoothRemoteDevice & device)414 void MediaBluetoothDeviceManager::HandleRemoveVirtualConnectDevice(const BluetoothRemoteDevice &device)
415 {
416 if (IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
417 AUDIO_WARNING_LOG("The device is already removed as virtual Devices, ignore remove action.");
418 return;
419 }
420 RemoveDeviceInConfigVector(device, virtualDevices_);
421 RemoveDeviceInConfigVector(device, negativeDevices_);
422 AudioDeviceDescriptor desc;
423 desc.deviceCategory_ = CATEGORY_DEFAULT;
424 NotifyToUpdateVirtualDevice(device, desc, DeviceStatus::VIRTUAL_REMOVE);
425 }
426
AddDeviceInConfigVector(const BluetoothRemoteDevice & device,std::vector<BluetoothRemoteDevice> & deviceVector)427 void MediaBluetoothDeviceManager::AddDeviceInConfigVector(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.push_back(device);
437 }
438 }
439
RemoveDeviceInConfigVector(const BluetoothRemoteDevice & device,std::vector<BluetoothRemoteDevice> & deviceVector)440 void MediaBluetoothDeviceManager::RemoveDeviceInConfigVector(const BluetoothRemoteDevice &device,
441 std::vector<BluetoothRemoteDevice> &deviceVector)
442 {
443 std::lock_guard<std::mutex> a2dpDeviceLock(g_a2dpDeviceLock);
444 auto isPresent = [&device] (BluetoothRemoteDevice &bluetoothRemoteDevice) {
445 return device.GetDeviceAddr() == bluetoothRemoteDevice.GetDeviceAddr();
446 };
447 auto deviceIter = std::find_if(deviceVector.begin(), deviceVector.end(), isPresent);
448 if (deviceIter != deviceVector.end()) {
449 deviceVector.erase(deviceIter);
450 }
451 }
452
NotifyToUpdateAudioDevice(const BluetoothRemoteDevice & device,AudioDeviceDescriptor & desc,DeviceStatus deviceStatus)453 void MediaBluetoothDeviceManager::NotifyToUpdateAudioDevice(const BluetoothRemoteDevice &device,
454 AudioDeviceDescriptor &desc, DeviceStatus deviceStatus)
455 {
456 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
457 desc.deviceRole_ = DeviceRole::OUTPUT_DEVICE;
458 desc.macAddress_ = device.GetDeviceAddr();
459 desc.deviceName_ = device.GetDeviceName();
460 desc.connectState_ = ConnectState::CONNECTED;
461 AUDIO_WARNING_LOG("a2dpBluetoothDeviceMap_ operation: %{public}d new bluetooth device, \
462 device address is %{public}s, category is %{public}d", deviceStatus,
463 GetEncryptAddr(device.GetDeviceAddr()).c_str(), desc.deviceCategory_);
464 {
465 std::lock_guard<std::mutex> deviceMapLock(g_a2dpDeviceMapLock);
466 if (deviceStatus == DeviceStatus::ADD) {
467 a2dpBluetoothDeviceMap_[device.GetDeviceAddr()] = device;
468 } else if (deviceStatus == DeviceStatus::REMOVE) {
469 if (a2dpBluetoothDeviceMap_.find(device.GetDeviceAddr()) != a2dpBluetoothDeviceMap_.end()) {
470 a2dpBluetoothDeviceMap_.erase(device.GetDeviceAddr());
471 }
472 }
473 }
474 std::lock_guard<std::mutex> observerLock(g_observerLock);
475 CHECK_AND_RETURN_LOG(g_deviceObserver != nullptr, "NotifyToUpdateAudioDevice, device observer is null");
476 bool isConnected = deviceStatus == DeviceStatus::ADD;
477 g_deviceObserver->OnDeviceStatusUpdated(desc, isConnected);
478 }
479
HandleUpdateDeviceCategory(const BluetoothRemoteDevice & device)480 void MediaBluetoothDeviceManager::HandleUpdateDeviceCategory(const BluetoothRemoteDevice &device)
481 {
482 if (!IsA2dpBluetoothDeviceExist(device.GetDeviceAddr())) {
483 AUDIO_WARNING_LOG("HandleUpdateDeviceCategory failed for the device has not be reported the connected action.");
484 return;
485 }
486 AudioDeviceDescriptor desc = HandleConnectDeviceInner(device);
487 std::lock_guard<std::mutex> observerLock(g_observerLock);
488 if (g_deviceObserver != nullptr) {
489 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
490 }
491 }
492
NotifyToUpdateVirtualDevice(const BluetoothRemoteDevice & device,AudioDeviceDescriptor & desc,DeviceStatus deviceStatus)493 void MediaBluetoothDeviceManager::NotifyToUpdateVirtualDevice(const BluetoothRemoteDevice &device,
494 AudioDeviceDescriptor &desc, DeviceStatus deviceStatus)
495 {
496 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_A2DP;
497 desc.deviceRole_ = DeviceRole::OUTPUT_DEVICE;
498 desc.macAddress_ = device.GetDeviceAddr();
499 desc.deviceName_ = device.GetDeviceName();
500 desc.connectState_ = ConnectState::VIRTUAL_CONNECTED;
501 std::lock_guard<std::mutex> observerLock(g_observerLock);
502 CHECK_AND_RETURN_LOG(g_deviceObserver != nullptr, "NotifyToUpdateVirtualDevice, device observer is null");
503 bool isConnected = deviceStatus == DeviceStatus::VIRTUAL_ADD;
504 g_deviceObserver->OnDeviceStatusUpdated(desc, isConnected);
505 }
506
IsA2dpBluetoothDeviceExist(const std::string & macAddress)507 bool MediaBluetoothDeviceManager::IsA2dpBluetoothDeviceExist(const std::string& macAddress)
508 {
509 std::lock_guard<std::mutex> deviceMapLock(g_a2dpDeviceMapLock);
510 if (a2dpBluetoothDeviceMap_.find(macAddress) != a2dpBluetoothDeviceMap_.end()) {
511 return true;
512 }
513 return false;
514 }
515
IsA2dpBluetoothDeviceConnecting(const std::string & macAddress)516 bool MediaBluetoothDeviceManager::IsA2dpBluetoothDeviceConnecting(const std::string& macAddress)
517 {
518 std::lock_guard<std::mutex> a2dpDeviceLock(g_a2dpDeviceLock);
519 auto deviceIter = std::find_if(connectingDevices_.begin(), connectingDevices_.end(),
520 [&macAddress] (BluetoothRemoteDevice &bluetoothRemoteDevice) {
521 return macAddress == bluetoothRemoteDevice.GetDeviceAddr();
522 });
523 if (deviceIter != connectingDevices_.end()) {
524 return true;
525 }
526 return false;
527 }
528
GetConnectedA2dpBluetoothDevice(const std::string & macAddress,BluetoothRemoteDevice & device)529 int32_t MediaBluetoothDeviceManager::GetConnectedA2dpBluetoothDevice(const std::string& macAddress,
530 BluetoothRemoteDevice &device)
531 {
532 std::lock_guard<std::mutex> deviceMapLock(g_a2dpDeviceMapLock);
533 auto deviceIter = a2dpBluetoothDeviceMap_.find(macAddress);
534 if (deviceIter != a2dpBluetoothDeviceMap_.end()) {
535 device = deviceIter->second;
536 return SUCCESS;
537 }
538 return ERROR;
539 }
540
GetAllA2dpBluetoothDevice()541 std::vector<BluetoothRemoteDevice> MediaBluetoothDeviceManager::GetAllA2dpBluetoothDevice()
542 {
543 std::lock_guard<std::mutex> deviceMapLock(g_a2dpDeviceMapLock);
544 std::vector<BluetoothRemoteDevice> a2dpList = {};
545 a2dpList.reserve(a2dpBluetoothDeviceMap_.size());
546 for (const auto &[macaddr, device] : a2dpBluetoothDeviceMap_) {
547 a2dpList.emplace_back(device);
548 }
549 return a2dpList;
550 }
551
UpdateA2dpDeviceConfiguration(const BluetoothRemoteDevice & device,const AudioStreamInfo & streamInfo)552 void MediaBluetoothDeviceManager::UpdateA2dpDeviceConfiguration(const BluetoothRemoteDevice &device,
553 const AudioStreamInfo &streamInfo)
554 {
555 std::lock_guard<std::mutex> observerLock(g_observerLock);
556 CHECK_AND_RETURN_LOG(g_deviceObserver != nullptr, "UpdateA2dpDeviceConfiguration, device observer is null");
557 g_deviceObserver->OnDeviceConfigurationChanged(DEVICE_TYPE_BLUETOOTH_A2DP, device.GetDeviceAddr(),
558 device.GetDeviceName(), streamInfo);
559 }
560
ClearAllA2dpBluetoothDevice()561 void MediaBluetoothDeviceManager::ClearAllA2dpBluetoothDevice()
562 {
563 AUDIO_INFO_LOG("Bluetooth service crashed and enter the ClearAllA2dpBluetoothDevice.");
564 {
565 std::lock_guard<std::mutex> a2dpDeviceLock(g_a2dpDeviceLock);
566 privacyDevices_.clear();
567 commonDevices_.clear();
568 negativeDevices_.clear();
569 connectingDevices_.clear();
570 virtualDevices_.clear();
571 }
572 std::lock_guard<std::mutex> deviceMapLock(g_a2dpDeviceMapLock);
573 std::lock_guard<std::mutex> wearStateMapLock(g_a2dpWearStateMapLock);
574 a2dpBluetoothDeviceMap_.clear();
575 wearDetectionStateMap_.clear();
576 }
577
GetA2dpVirtualDeviceList()578 std::vector<BluetoothRemoteDevice> MediaBluetoothDeviceManager::GetA2dpVirtualDeviceList()
579 {
580 std::lock_guard<std::mutex> a2dpDeviceLock(g_hfpDeviceLock);
581 return virtualDevices_;
582 }
583
SetHfpStack(const BluetoothRemoteDevice & device,int action)584 void HfpBluetoothDeviceManager::SetHfpStack(const BluetoothRemoteDevice &device, int action)
585 {
586 switch (action) {
587 case BluetoothDeviceAction::CONNECTING_ACTION:
588 HandleConnectingDevice(device);
589 break;
590 case BluetoothDeviceAction::CONNECT_ACTION:
591 HandleConnectDevice(device);
592 break;
593 case BluetoothDeviceAction::DISCONNECT_ACTION:
594 HandleDisconnectDevice(device);
595 break;
596 case BluetoothDeviceAction::WEAR_ACTION:
597 HandleWearDevice(device);
598 break;
599 case BluetoothDeviceAction::UNWEAR_ACTION:
600 HandleUnwearDevice(device);
601 break;
602 case BluetoothDeviceAction::ENABLEFROMREMOTE_ACTION:
603 HandleEnableDevice(device);
604 break;
605 case BluetoothDeviceAction::DISABLEFROMREMOTE_ACTION:
606 HandleDisableDevice(device);
607 break;
608 case BluetoothDeviceAction::ENABLE_WEAR_DETECTION_ACTION:
609 HandleWearEnable(device);
610 break;
611 case BluetoothDeviceAction::DISABLE_WEAR_DETECTION_ACTION:
612 HandleWearDisable(device);
613 break;
614 case BluetoothDeviceAction::USER_SELECTION_ACTION:
615 HandleUserSelection(device);
616 break;
617 case BluetoothDeviceAction::STOP_VIRTUAL_CALL:
618 HandleStopVirtualCall(device);
619 break;
620 case BluetoothDeviceAction::VIRTUAL_DEVICE_ADD_ACTION:
621 HandleVirtualConnectDevice(device);
622 break;
623 case BluetoothDeviceAction::VIRTUAL_DEVICE_REMOVE_ACTION:
624 HandleRemoveVirtualConnectDevice(device);
625 break;
626 case BluetoothDeviceAction::CATEGORY_UPDATE_ACTION:
627 HandleUpdateDeviceCategory(device);
628 break;
629 default:
630 AUDIO_ERR_LOG("SetHfpStack failed due to the unknow action: %{public}d", action);
631 break;
632 }
633 }
634
HandleConnectingDevice(const BluetoothRemoteDevice & device)635 void HfpBluetoothDeviceManager::HandleConnectingDevice(const BluetoothRemoteDevice &device)
636 {
637 if (IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
638 return;
639 }
640 AddDeviceInConfigVector(device, connectingDevices_);
641 }
642
HandleConnectDevice(const BluetoothRemoteDevice & device)643 void HfpBluetoothDeviceManager::HandleConnectDevice(const BluetoothRemoteDevice &device)
644 {
645 if (IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
646 return;
647 }
648 AudioDeviceDescriptor desc = HandleConnectDeviceInner(device);
649 NotifyToUpdateAudioDevice(device, desc, DeviceStatus::ADD);
650 }
651
HandleConnectDeviceInner(const BluetoothRemoteDevice & device)652 AudioDeviceDescriptor HfpBluetoothDeviceManager::HandleConnectDeviceInner(const BluetoothRemoteDevice &device)
653 {
654 RemoveDeviceInConfigVector(device, connectingDevices_);
655 // If the device was virtual connected, remove it from the negativeDevices_ list.
656 RemoveDeviceInConfigVector(device, negativeDevices_);
657 RemoveDeviceInConfigVector(device, virtualDevices_);
658 DeviceCategory bluetoothCategory = GetDeviceCategory(device);
659 AudioDeviceDescriptor desc;
660 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
661 desc.macAddress_ = device.GetDeviceAddr();
662 desc.deviceCategory_ = bluetoothCategory;
663 switch (bluetoothCategory) {
664 case BT_GLASSES:
665 case BT_HEADPHONE:
666 if (IsBTWearDetectionEnable(device)) {
667 AddDeviceInConfigVector(device, negativeDevices_);
668 desc.deviceCategory_ = BT_UNWEAR_HEADPHONE;
669 } else {
670 AddDeviceInConfigVector(device, privacyDevices_);
671 }
672 break;
673 case BT_SOUNDBOX:
674 case BT_CAR:
675 AddDeviceInConfigVector(device, commonDevices_);
676 break;
677 case BT_WATCH:
678 AddDeviceInConfigVector(device, negativeDevices_);
679 break;
680 default:
681 AUDIO_WARNING_LOG("Unknow BT category, regard as bluetooth headset.");
682 AddDeviceInConfigVector(device, privacyDevices_);
683 desc.deviceCategory_ = BT_HEADPHONE;
684 break;
685 }
686 return desc;
687 }
688
HandleDisconnectDevice(const BluetoothRemoteDevice & device)689 void HfpBluetoothDeviceManager::HandleDisconnectDevice(const BluetoothRemoteDevice &device)
690 {
691 RemoveDeviceInConfigVector(device, connectingDevices_);
692 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
693 AUDIO_WARNING_LOG("The device is already disconnected, ignore disconnect action.");
694 return;
695 }
696 RemoveDeviceInConfigVector(device, privacyDevices_);
697 RemoveDeviceInConfigVector(device, commonDevices_);
698 RemoveDeviceInConfigVector(device, negativeDevices_);
699 {
700 std::lock_guard<std::mutex> wearStateMapLock(g_hfpWearStateMapLock);
701 wearDetectionStateMap_.erase(device.GetDeviceAddr());
702 }
703 AudioDeviceDescriptor desc;
704 desc.deviceCategory_ = CATEGORY_DEFAULT;
705 NotifyToUpdateAudioDevice(device, desc, DeviceStatus::REMOVE);
706 }
707
HandleWearDevice(const BluetoothRemoteDevice & device)708 void HfpBluetoothDeviceManager::HandleWearDevice(const BluetoothRemoteDevice &device)
709 {
710 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
711 AUDIO_ERR_LOG("HandleWearDevice failed for the device has not be reported the connected action.");
712 return;
713 }
714 RemoveDeviceInConfigVector(device, negativeDevices_);
715 RemoveDeviceInConfigVector(device, privacyDevices_);
716 AddDeviceInConfigVector(device, privacyDevices_);
717 {
718 std::lock_guard<std::mutex> wearStateMapLock(g_hfpWearStateMapLock);
719 wearDetectionStateMap_[device.GetDeviceAddr()] = BluetoothDeviceAction::WEAR_ACTION;
720 }
721 AudioDeviceDescriptor desc;
722 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
723 desc.macAddress_ = device.GetDeviceAddr();
724 desc.deviceCategory_ = BT_HEADPHONE;
725 std::lock_guard<std::mutex> observerLock(g_observerLock);
726 if (g_deviceObserver != nullptr) {
727 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
728 }
729 }
730
HandleUnwearDevice(const BluetoothRemoteDevice & device)731 void HfpBluetoothDeviceManager::HandleUnwearDevice(const BluetoothRemoteDevice &device)
732 {
733 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
734 AUDIO_ERR_LOG("HandleWearDevice failed for the device has not worn.");
735 return;
736 }
737 RemoveDeviceInConfigVector(device, privacyDevices_);
738 RemoveDeviceInConfigVector(device, negativeDevices_);
739 AddDeviceInConfigVector(device, negativeDevices_);
740 {
741 std::lock_guard<std::mutex> wearStateMapLock(g_hfpWearStateMapLock);
742 wearDetectionStateMap_[device.GetDeviceAddr()] = BluetoothDeviceAction::UNWEAR_ACTION;
743 }
744 AudioDeviceDescriptor desc;
745 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
746 desc.macAddress_ = device.GetDeviceAddr();
747 desc.deviceCategory_ = BT_UNWEAR_HEADPHONE;
748 std::lock_guard<std::mutex> observerLock(g_observerLock);
749 if (g_deviceObserver != nullptr) {
750 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
751 }
752 }
753
HandleEnableDevice(const BluetoothRemoteDevice & device)754 void HfpBluetoothDeviceManager::HandleEnableDevice(const BluetoothRemoteDevice &device)
755 {
756 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
757 AUDIO_ERR_LOG("HandleEnableDevice failed for the device has not connected.");
758 return;
759 }
760 AudioDeviceDescriptor desc;
761 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
762 desc.macAddress_ = device.GetDeviceAddr();
763 desc.isEnable_ = true;
764 std::lock_guard<std::mutex> observerLock(g_observerLock);
765 if (g_deviceObserver != nullptr) {
766 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::ENABLE_UPDATE);
767 }
768 }
769
HandleDisableDevice(const BluetoothRemoteDevice & device)770 void HfpBluetoothDeviceManager::HandleDisableDevice(const BluetoothRemoteDevice &device)
771 {
772 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
773 AUDIO_ERR_LOG("HandleDisableDevice failed for the device has not connected.");
774 return;
775 }
776 AudioDeviceDescriptor desc;
777 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
778 desc.macAddress_ = device.GetDeviceAddr();
779 desc.isEnable_ = false;
780 std::lock_guard<std::mutex> observerLock(g_observerLock);
781 if (g_deviceObserver != nullptr) {
782 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::ENABLE_UPDATE);
783 }
784 }
785
HandleWearEnable(const BluetoothRemoteDevice & device)786 void HfpBluetoothDeviceManager::HandleWearEnable(const BluetoothRemoteDevice &device)
787 {
788 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
789 AUDIO_ERR_LOG("HandleWearEnable failed for the device has not connected.");
790 return;
791 }
792 RemoveDeviceInConfigVector(device, negativeDevices_);
793 RemoveDeviceInConfigVector(device, privacyDevices_);
794 std::lock_guard<std::mutex> wearStateMapLock(g_hfpWearStateMapLock);
795 AudioDeviceDescriptor desc;
796 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
797 desc.macAddress_ = device.GetDeviceAddr();
798 auto wearStateIter = wearDetectionStateMap_.find(device.GetDeviceAddr());
799 if (wearStateIter != wearDetectionStateMap_.end() &&
800 wearStateIter->second == BluetoothDeviceAction::WEAR_ACTION) {
801 AddDeviceInConfigVector(device, privacyDevices_);
802 desc.deviceCategory_ = BT_HEADPHONE;
803 } else {
804 AddDeviceInConfigVector(device, negativeDevices_);
805 desc.deviceCategory_ = BT_UNWEAR_HEADPHONE;
806 }
807 std::lock_guard<std::mutex> observerLock(g_observerLock);
808 if (g_deviceObserver != nullptr) {
809 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
810 }
811 }
812
HandleWearDisable(const BluetoothRemoteDevice & device)813 void HfpBluetoothDeviceManager::HandleWearDisable(const BluetoothRemoteDevice &device)
814 {
815 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
816 AUDIO_ERR_LOG("HandleWearDisable failed for the device has not connected.");
817 return;
818 }
819 RemoveDeviceInConfigVector(device, privacyDevices_);
820 RemoveDeviceInConfigVector(device, negativeDevices_);
821 AddDeviceInConfigVector(device, privacyDevices_);
822 AudioDeviceDescriptor desc;
823 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
824 desc.macAddress_ = device.GetDeviceAddr();
825 desc.deviceCategory_ = BT_HEADPHONE;
826 std::lock_guard<std::mutex> observerLock(g_observerLock);
827 if (g_deviceObserver != nullptr) {
828 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
829 }
830 }
831
HandleUserSelection(const BluetoothRemoteDevice & device)832 void HfpBluetoothDeviceManager::HandleUserSelection(const BluetoothRemoteDevice &device)
833 {
834 std::string deviceAddr = device.GetDeviceAddr();
835 DeviceCategory bluetoothCategory = GetDeviceCategory(device);
836 if (bluetoothCategory == BT_WATCH) {
837 std::lock_guard<std::mutex> wearStateMapLock(g_hfpWearStateMapLock);
838 std::lock_guard<std::mutex> hfpDeviceLock(g_hfpDeviceLock);
839 auto isPresent = [] (BluetoothRemoteDevice &bluetoothRemoteDevice) {
840 return wearDetectionStateMap_[bluetoothRemoteDevice.GetDeviceAddr()] == WEAR_ACTION;
841 };
842 auto deviceIter = std::find_if(privacyDevices_.rbegin(), privacyDevices_.rend(), isPresent);
843 if (deviceIter != privacyDevices_.rend()) {
844 deviceAddr = deviceIter->GetDeviceAddr();
845 AUDIO_WARNING_LOG("Change user select device from watch %{public}s to wear headphone %{public}s",
846 GetEncryptAddr(device.GetDeviceAddr()).c_str(), GetEncryptAddr(deviceAddr).c_str());
847 }
848 }
849 std::lock_guard<std::mutex> observerLock(g_observerLock);
850 if (g_deviceObserver != nullptr) {
851 g_deviceObserver->OnForcedDeviceSelected(DEVICE_TYPE_BLUETOOTH_SCO, deviceAddr);
852 }
853 }
854
HandleStopVirtualCall(const BluetoothRemoteDevice & device)855 void HfpBluetoothDeviceManager::HandleStopVirtualCall(const BluetoothRemoteDevice &device)
856 {
857 {
858 std::lock_guard<std::mutex> handleLock(stopVirtualCallHandleLock_);
859 stopVirtualCallHandle_.device = device;
860 stopVirtualCallHandle_.isWaitingForStoppingVirtualCall = true;
861 }
862 AUDIO_INFO_LOG("bluetooth service trigger disconnect sco");
863 std::thread disconnectScoThread = std::thread(&Bluetooth::AudioHfpManager::DisconnectSco);
864 disconnectScoThread.detach();
865 }
866
HandleVirtualConnectDevice(const BluetoothRemoteDevice & device)867 void HfpBluetoothDeviceManager::HandleVirtualConnectDevice(const BluetoothRemoteDevice &device)
868 {
869 if (IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
870 return;
871 }
872 AddDeviceInConfigVector(device, virtualDevices_);
873 DeviceCategory bluetoothCategory = GetDeviceCategory(device);
874 AudioDeviceDescriptor desc;
875 desc.deviceCategory_ = bluetoothCategory;
876 AddDeviceInConfigVector(device, negativeDevices_);
877 NotifyToUpdateVirtualDevice(device, desc, DeviceStatus::VIRTUAL_ADD);
878 }
879
HandleRemoveVirtualConnectDevice(const BluetoothRemoteDevice & device)880 void HfpBluetoothDeviceManager::HandleRemoveVirtualConnectDevice(const BluetoothRemoteDevice &device)
881 {
882 if (IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
883 AUDIO_INFO_LOG("The device is already removed as virtual Devices, ignore remove action.");
884 return;
885 }
886 RemoveDeviceInConfigVector(device, virtualDevices_);
887 RemoveDeviceInConfigVector(device, negativeDevices_);
888 AudioDeviceDescriptor desc;
889 desc.deviceCategory_ = CATEGORY_DEFAULT;
890 NotifyToUpdateVirtualDevice(device, desc, DeviceStatus::VIRTUAL_REMOVE);
891 }
892
HandleUpdateDeviceCategory(const BluetoothRemoteDevice & device)893 void HfpBluetoothDeviceManager::HandleUpdateDeviceCategory(const BluetoothRemoteDevice &device)
894 {
895 if (!IsHfpBluetoothDeviceExist(device.GetDeviceAddr())) {
896 AUDIO_WARNING_LOG("HandleUpdateDeviceCategory failed for the device has not be reported the connected action.");
897 return;
898 }
899 AudioDeviceDescriptor desc = HandleConnectDeviceInner(device);
900 std::lock_guard<std::mutex> observerLock(g_observerLock);
901 if (g_deviceObserver != nullptr) {
902 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CATEGORY_UPDATE);
903 }
904 }
905
AddDeviceInConfigVector(const BluetoothRemoteDevice & device,std::vector<BluetoothRemoteDevice> & deviceVector)906 void HfpBluetoothDeviceManager::AddDeviceInConfigVector(const BluetoothRemoteDevice &device,
907 std::vector<BluetoothRemoteDevice> &deviceVector)
908 {
909 std::lock_guard<std::mutex> hfpDeviceLock(g_hfpDeviceLock);
910 auto isPresent = [&device] (BluetoothRemoteDevice &bluetoothRemoteDevice) {
911 return device.GetDeviceAddr() == bluetoothRemoteDevice.GetDeviceAddr();
912 };
913 auto deviceIter = std::find_if(deviceVector.begin(), deviceVector.end(), isPresent);
914 if (deviceIter == deviceVector.end()) {
915 deviceVector.push_back(device);
916 }
917 }
918
RemoveDeviceInConfigVector(const BluetoothRemoteDevice & device,std::vector<BluetoothRemoteDevice> & deviceVector)919 void HfpBluetoothDeviceManager::RemoveDeviceInConfigVector(const BluetoothRemoteDevice &device,
920 std::vector<BluetoothRemoteDevice> &deviceVector)
921 {
922 std::lock_guard<std::mutex> hfpDeviceLock(g_hfpDeviceLock);
923 auto isPresent = [&device] (BluetoothRemoteDevice &bluetoothRemoteDevice) {
924 return device.GetDeviceAddr() == bluetoothRemoteDevice.GetDeviceAddr();
925 };
926 auto deviceIter = std::find_if(deviceVector.begin(), deviceVector.end(), isPresent);
927 if (deviceIter != deviceVector.end()) {
928 deviceVector.erase(deviceIter);
929 }
930 }
931
NotifyToUpdateAudioDevice(const BluetoothRemoteDevice & device,AudioDeviceDescriptor & desc,DeviceStatus deviceStatus)932 void HfpBluetoothDeviceManager::NotifyToUpdateAudioDevice(const BluetoothRemoteDevice &device,
933 AudioDeviceDescriptor &desc, DeviceStatus deviceStatus)
934 {
935 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
936 desc.macAddress_ = device.GetDeviceAddr();
937 desc.deviceName_ = device.GetDeviceName();
938 desc.connectState_ = ConnectState::DEACTIVE_CONNECTED;
939 AUDIO_WARNING_LOG("hfpBluetoothDeviceMap_ operation: %{public}d new bluetooth device, device address is %{public}s,\
940 category is %{public}d", deviceStatus, GetEncryptAddr(device.GetDeviceAddr()).c_str(), desc.deviceCategory_);
941 {
942 std::lock_guard<std::mutex> deviceMapLock(g_hfpDeviceMapLock);
943 if (deviceStatus == DeviceStatus::ADD) {
944 hfpBluetoothDeviceMap_[device.GetDeviceAddr()] = device;
945 } else if (deviceStatus == DeviceStatus::REMOVE) {
946 if (hfpBluetoothDeviceMap_.find(device.GetDeviceAddr()) != hfpBluetoothDeviceMap_.end()) {
947 hfpBluetoothDeviceMap_.erase(device.GetDeviceAddr());
948 }
949 }
950 }
951 std::lock_guard<std::mutex> observerLock(g_observerLock);
952 if (g_deviceObserver == nullptr) {
953 AUDIO_ERR_LOG("NotifyToUpdateAudioDevice, device observer is null");
954 return;
955 }
956 bool isConnected = deviceStatus == DeviceStatus::ADD;
957 g_deviceObserver->OnDeviceStatusUpdated(desc, isConnected);
958 }
959
NotifyToUpdateVirtualDevice(const BluetoothRemoteDevice & device,AudioDeviceDescriptor & desc,DeviceStatus deviceStatus)960 void HfpBluetoothDeviceManager::NotifyToUpdateVirtualDevice(const BluetoothRemoteDevice &device,
961 AudioDeviceDescriptor &desc, DeviceStatus deviceStatus)
962 {
963 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
964 desc.macAddress_ = device.GetDeviceAddr();
965 desc.deviceName_ = device.GetDeviceName();
966 desc.connectState_ = ConnectState::VIRTUAL_CONNECTED;
967 std::lock_guard<std::mutex> observerLock(g_observerLock);
968 CHECK_AND_RETURN_LOG(g_deviceObserver != nullptr, "NotifyToUpdateVirtualDevice, device observer is null");
969 bool isConnected = deviceStatus == DeviceStatus::VIRTUAL_ADD;
970 g_deviceObserver->OnDeviceStatusUpdated(desc, isConnected);
971 }
972
IsHfpBluetoothDeviceExist(const std::string & macAddress)973 bool HfpBluetoothDeviceManager::IsHfpBluetoothDeviceExist(const std::string& macAddress)
974 {
975 std::lock_guard<std::mutex> deviceMapLock(g_hfpDeviceMapLock);
976 if (hfpBluetoothDeviceMap_.find(macAddress) != hfpBluetoothDeviceMap_.end()) {
977 return true;
978 }
979 return false;
980 }
981
IsHfpBluetoothDeviceConnecting(const std::string & macAddress)982 bool HfpBluetoothDeviceManager::IsHfpBluetoothDeviceConnecting(const std::string& macAddress)
983 {
984 std::lock_guard<std::mutex> hfpDeviceLock(g_hfpDeviceLock);
985 auto deviceIter = std::find_if(connectingDevices_.begin(), connectingDevices_.end(),
986 [&macAddress] (BluetoothRemoteDevice &bluetoothRemoteDevice) {
987 return macAddress == bluetoothRemoteDevice.GetDeviceAddr();
988 });
989 if (deviceIter != connectingDevices_.end()) {
990 return true;
991 }
992 return false;
993 }
994
GetConnectedHfpBluetoothDevice(const std::string & macAddress,BluetoothRemoteDevice & device)995 int32_t HfpBluetoothDeviceManager::GetConnectedHfpBluetoothDevice(const std::string& macAddress,
996 BluetoothRemoteDevice &device)
997 {
998 std::lock_guard<std::mutex> deviceMapLock(g_hfpDeviceMapLock);
999 auto deviceIter = hfpBluetoothDeviceMap_.find(macAddress);
1000 if (deviceIter != hfpBluetoothDeviceMap_.end()) {
1001 device = deviceIter->second;
1002 return SUCCESS;
1003 }
1004 return ERROR;
1005 }
1006
GetAllHfpBluetoothDevice()1007 std::vector<BluetoothRemoteDevice> HfpBluetoothDeviceManager::GetAllHfpBluetoothDevice()
1008 {
1009 std::lock_guard<std::mutex> deviceMapLock(g_hfpDeviceMapLock);
1010 std::vector<BluetoothRemoteDevice> hfpList = {};
1011 hfpList.reserve(hfpBluetoothDeviceMap_.size());
1012 for (const auto &[macaddr, device] : hfpBluetoothDeviceMap_) {
1013 hfpList.emplace_back(device);
1014 }
1015 return hfpList;
1016 }
1017
ClearAllHfpBluetoothDevice()1018 void HfpBluetoothDeviceManager::ClearAllHfpBluetoothDevice()
1019 {
1020 AUDIO_WARNING_LOG("Bluetooth service crashed and enter the ClearAllhfpBluetoothDevice.");
1021 {
1022 std::lock_guard<std::mutex> hfpDeviceLock(g_hfpDeviceLock);
1023 privacyDevices_.clear();
1024 commonDevices_.clear();
1025 negativeDevices_.clear();
1026 connectingDevices_.clear();
1027 virtualDevices_.clear();
1028 }
1029 std::lock_guard<std::mutex> deviceMapLock(g_hfpDeviceMapLock);
1030 std::lock_guard<std::mutex> wearStateMapLock(g_hfpWearStateMapLock);
1031 hfpBluetoothDeviceMap_.clear();
1032 wearDetectionStateMap_.clear();
1033 }
1034
OnScoStateChanged(const BluetoothRemoteDevice & device,bool isConnected,int reason)1035 void HfpBluetoothDeviceManager::OnScoStateChanged(const BluetoothRemoteDevice &device, bool isConnected, int reason)
1036 {
1037 AudioDeviceDescriptor desc;
1038 desc.deviceType_ = DEVICE_TYPE_BLUETOOTH_SCO;
1039 desc.macAddress_ = device.GetDeviceAddr();
1040 if (isConnected) {
1041 desc.connectState_ = ConnectState::CONNECTED;
1042 } else {
1043 {
1044 std::lock_guard<std::mutex> handleLock(stopVirtualCallHandleLock_);
1045 if (device.GetDeviceAddr() == stopVirtualCallHandle_.device.GetDeviceAddr() &&
1046 stopVirtualCallHandle_.isWaitingForStoppingVirtualCall) {
1047 AUDIO_WARNING_LOG("reason change to %{public}d", HFP_AG_SCO_REMOTE_USER_TERMINATED);
1048 reason = HFP_AG_SCO_REMOTE_USER_TERMINATED;
1049 stopVirtualCallHandle_.device = BluetoothRemoteDevice();
1050 stopVirtualCallHandle_.isWaitingForStoppingVirtualCall = false;
1051 }
1052 }
1053 desc.connectState_ = reason == HFP_AG_SCO_REMOTE_USER_TERMINATED ? ConnectState::SUSPEND_CONNECTED
1054 : ConnectState::DEACTIVE_CONNECTED;
1055 }
1056
1057 // VGS feature
1058 desc.isVgsSupported_ = false;
1059 if (desc.connectState_ == ConnectState::CONNECTED) {
1060 desc.isVgsSupported_ = isConnected;
1061 AUDIO_INFO_LOG("desc.isVgsSupported_: %{public}d", desc.isVgsSupported_);
1062 }
1063
1064 std::lock_guard<std::mutex> observerLock(g_observerLock);
1065 if (g_deviceObserver != nullptr) {
1066 g_deviceObserver->OnDeviceInfoUpdated(desc, DeviceInfoUpdateCommand::CONNECTSTATE_UPDATE);
1067 }
1068 }
1069
GetHfpVirtualDeviceList()1070 std::vector<BluetoothRemoteDevice> HfpBluetoothDeviceManager::GetHfpVirtualDeviceList()
1071 {
1072 std::lock_guard<std::mutex> hfpDeviceLock(g_hfpDeviceLock);
1073 return virtualDevices_;
1074 }
1075 // LCOV_EXCL_STOP
1076 } // namespace Bluetooth
1077 } // namespace OHOS
1078