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