1 /* 2 * Copyright (c) 2023-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef BLUETOOTH_DEVICE_MANAGER_H 17 #define BLUETOOTH_DEVICE_MANAGER_H 18 19 #include <mutex> 20 #include "bluetooth_hfp_ag.h" 21 #include "bluetooth_device_utils.h" 22 #include "audio_errors.h" 23 #include "audio_common_log.h" 24 #include "audio_system_manager.h" 25 #include "idevice_status_observer.h" 26 27 namespace OHOS { 28 namespace Bluetooth { 29 30 int32_t RegisterDeviceObserver(AudioStandard::IDeviceStatusObserver &observer); 31 void UnregisterDeviceObserver(); 32 void SendUserSelectionEvent(AudioStandard::DeviceType devType, const std::string &macAddress, int32_t eventType); 33 bool IsBTWearDetectionEnable(const BluetoothRemoteDevice &device); 34 std::string GetEncryptAddr(const std::string &addr); 35 36 class MediaBluetoothDeviceManager { 37 public: 38 MediaBluetoothDeviceManager() = default; 39 virtual ~MediaBluetoothDeviceManager() = default; 40 static void SetMediaStack(const BluetoothRemoteDevice &device, int action); 41 static void HandleConnectingDevice(const BluetoothRemoteDevice &device); 42 static void HandleConnectDevice(const BluetoothRemoteDevice &device); 43 static void HandleDisconnectDevice(const BluetoothRemoteDevice &device); 44 static void HandleWearDevice(const BluetoothRemoteDevice &device); 45 static void HandleUnwearDevice(const BluetoothRemoteDevice &device); 46 static void HandleEnableDevice(const BluetoothRemoteDevice &device); 47 static void HandleDisableDevice(const BluetoothRemoteDevice &device); 48 static void HandleWearEnable(const BluetoothRemoteDevice &device); 49 static void HandleWearDisable(const BluetoothRemoteDevice &device); 50 static void HandleUserSelection(const BluetoothRemoteDevice &device); 51 static void HandleVirtualConnectDevice(const BluetoothRemoteDevice &device); 52 static void HandleRemoveVirtualConnectDevice(const BluetoothRemoteDevice &device); 53 static void AddDeviceInConfigVector(const BluetoothRemoteDevice &device, 54 std::vector<BluetoothRemoteDevice> &deviceVector); 55 static void RemoveDeviceInConfigVector(const BluetoothRemoteDevice &device, 56 std::vector<BluetoothRemoteDevice> &deviceVector); 57 static void NotifyToUpdateAudioDevice(const BluetoothRemoteDevice &device, 58 AudioStandard::AudioDeviceDescriptor &desc, DeviceStatus deviceStatus); 59 static void NotifyToUpdateVirtualDevice(const BluetoothRemoteDevice &device, 60 AudioStandard::AudioDeviceDescriptor &desc, DeviceStatus deviceStatus); 61 static bool IsA2dpBluetoothDeviceExist(const std::string& macAddress); 62 static bool IsA2dpBluetoothDeviceConnecting(const std::string& macAddress); 63 static int32_t GetConnectedA2dpBluetoothDevice(const std::string& macAddress, BluetoothRemoteDevice &device); 64 static void UpdateA2dpDeviceConfiguration(const BluetoothRemoteDevice &device, 65 const AudioStandard::AudioStreamInfo &streamInfo); 66 static std::vector<BluetoothRemoteDevice> GetAllA2dpBluetoothDevice(); 67 static void ClearAllA2dpBluetoothDevice(); 68 static std::vector<BluetoothRemoteDevice> GetA2dpVirtualDeviceList(); 69 70 private: 71 static std::map<std::string, BluetoothRemoteDevice> a2dpBluetoothDeviceMap_; 72 static std::map<std::string, BluetoothDeviceAction> wearDetectionStateMap_; 73 static std::vector<BluetoothRemoteDevice> privacyDevices_; 74 static std::vector<BluetoothRemoteDevice> commonDevices_; 75 static std::vector<BluetoothRemoteDevice> negativeDevices_; 76 static std::vector<BluetoothRemoteDevice> connectingDevices_; 77 static std::vector<BluetoothRemoteDevice> virtualDevices_; 78 static void HandleUpdateDeviceCategory(const BluetoothRemoteDevice &device); 79 static AudioStandard::AudioDeviceDescriptor HandleConnectDeviceInner(const BluetoothRemoteDevice &device); 80 }; 81 82 class A2dpInBluetoothDeviceManager { 83 public: 84 A2dpInBluetoothDeviceManager() = default; 85 virtual ~A2dpInBluetoothDeviceManager() = default; 86 static void SetA2dpInStack(const BluetoothRemoteDevice &device, 87 const AudioStandard::AudioStreamInfo &streamInfo, int32_t action); 88 static void HandleConnectDevice(const BluetoothRemoteDevice &device, 89 const AudioStandard::AudioStreamInfo &streamInfo); 90 static void HandleDisconnectDevice(const BluetoothRemoteDevice &device, 91 const AudioStandard::AudioStreamInfo &streamInfo); 92 static void NotifyToUpdateAudioDevice(const BluetoothRemoteDevice &device, 93 const AudioStandard::AudioStreamInfo &streamInfo, 94 AudioStandard::AudioDeviceDescriptor &desc, 95 DeviceStatus deviceStatus); 96 static bool GetA2dpInDeviceStreamInfo(const std::string& macAddress, 97 AudioStandard::AudioStreamInfo &streamInfo); 98 static bool IsA2dpInBluetoothDeviceExist(const std::string& macAddress); 99 static std::vector<BluetoothRemoteDevice> GetAllA2dpInBluetoothDevice(); 100 static void ClearAllA2dpInBluetoothDevice(); 101 static void ClearAllA2dpInStreamInfo(); 102 103 private: 104 static std::map<std::string, BluetoothRemoteDevice> a2dpInBluetoothDeviceMap_; 105 static std::map<std::string, AudioStandard::AudioStreamInfo> a2dpInStreamInfoMap_; 106 }; 107 108 struct BluetoothStopVirtualCallHandle { 109 BluetoothRemoteDevice device; 110 bool isWaitingForStoppingVirtualCall; 111 }; 112 113 class HfpBluetoothDeviceManager { 114 public: 115 HfpBluetoothDeviceManager() = default; 116 virtual ~HfpBluetoothDeviceManager() = default; 117 static void SetHfpStack(const BluetoothRemoteDevice &device, int action); 118 static void HandleConnectingDevice(const BluetoothRemoteDevice &device); 119 static void HandleConnectDevice(const BluetoothRemoteDevice &device); 120 static void HandleDisconnectDevice(const BluetoothRemoteDevice &device); 121 static void HandleWearDevice(const BluetoothRemoteDevice &device); 122 static void HandleUnwearDevice(const BluetoothRemoteDevice &device); 123 static void HandleEnableDevice(const BluetoothRemoteDevice &device); 124 static void HandleDisableDevice(const BluetoothRemoteDevice &device); 125 static void HandleWearEnable(const BluetoothRemoteDevice &device); 126 static void HandleWearDisable(const BluetoothRemoteDevice &device); 127 static void HandleUserSelection(const BluetoothRemoteDevice &device); 128 static void HandleStopVirtualCall(const BluetoothRemoteDevice &device); 129 static void HandleVirtualConnectDevice(const BluetoothRemoteDevice &device); 130 static void HandleRemoveVirtualConnectDevice(const BluetoothRemoteDevice &device); 131 static void AddDeviceInConfigVector(const BluetoothRemoteDevice &device, 132 std::vector<BluetoothRemoteDevice> &deviceVector); 133 static void RemoveDeviceInConfigVector(const BluetoothRemoteDevice &device, 134 std::vector<BluetoothRemoteDevice> &deviceVector); 135 static void NotifyToUpdateAudioDevice(const BluetoothRemoteDevice &device, 136 AudioStandard::AudioDeviceDescriptor &desc, DeviceStatus deviceStatus); 137 static void NotifyToUpdateVirtualDevice(const BluetoothRemoteDevice &device, 138 AudioStandard::AudioDeviceDescriptor &desc, DeviceStatus deviceStatus); 139 static bool IsHfpBluetoothDeviceExist(const std::string& macAddress); 140 static bool IsHfpBluetoothDeviceConnecting(const std::string& macAddress); 141 static void UpdateHfpDeviceConfiguration(const BluetoothRemoteDevice &device, 142 const AudioStandard::AudioStreamInfo &streamInfo); 143 static void OnScoStateChanged(const BluetoothRemoteDevice &device, bool isConnected, int reason); 144 static int32_t GetConnectedHfpBluetoothDevice(const std::string& macAddress, BluetoothRemoteDevice &device); 145 static std::vector<BluetoothRemoteDevice> GetAllHfpBluetoothDevice(); 146 static void ClearAllHfpBluetoothDevice(); 147 static std::vector<BluetoothRemoteDevice> GetHfpVirtualDeviceList(); 148 149 private: 150 static void HandleUpdateDeviceCategory(const BluetoothRemoteDevice &device); 151 static AudioStandard::AudioDeviceDescriptor HandleConnectDeviceInner(const BluetoothRemoteDevice &device); 152 static std::map<std::string, BluetoothRemoteDevice> hfpBluetoothDeviceMap_; 153 static std::map<std::string, BluetoothDeviceAction> wearDetectionStateMap_; 154 static std::vector<BluetoothRemoteDevice> privacyDevices_; 155 static std::vector<BluetoothRemoteDevice> commonDevices_; 156 static std::vector<BluetoothRemoteDevice> negativeDevices_; 157 static std::vector<BluetoothRemoteDevice> connectingDevices_; 158 static std::vector<BluetoothRemoteDevice> virtualDevices_; 159 static std::mutex stopVirtualCallHandleLock_; 160 static BluetoothStopVirtualCallHandle stopVirtualCallHandle_; 161 }; 162 } // namespace Bluetooth 163 } // namespace OHOS 164 165 #endif // BLUETOOTH_DEVICE_MANAGER_H