• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <functional>
21 #include "bluetooth_hfp_ag.h"
22 #include "bluetooth_device_utils.h"
23 #include "audio_errors.h"
24 #include "audio_common_log.h"
25 #include "audio_system_manager.h"
26 #include "idevice_status_observer.h"
27 
28 namespace OHOS {
29 namespace Bluetooth {
30 
31 int32_t RegisterDeviceObserver(AudioStandard::IDeviceStatusObserver &observer);
32 void UnregisterDeviceObserver();
33 void SendUserSelectionEvent(AudioStandard::DeviceType devType, const std::string &macAddress, int32_t eventType);
34 bool IsBTWearDetectionEnable(const BluetoothRemoteDevice &device);
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     using DisconnectScoForDevice = std::function<void(const BluetoothRemoteDevice &)>;
116 
117     HfpBluetoothDeviceManager() = default;
118     virtual ~HfpBluetoothDeviceManager() = default;
119     static void SetHfpStack(const BluetoothRemoteDevice &device, int action);
120     static void HandleConnectingDevice(const BluetoothRemoteDevice &device);
121     static void HandleConnectDevice(const BluetoothRemoteDevice &device);
122     static void HandleDisconnectDevice(const BluetoothRemoteDevice &device);
123     static void HandleWearDevice(const BluetoothRemoteDevice &device);
124     static void HandleUnwearDevice(const BluetoothRemoteDevice &device);
125     static void HandleEnableDevice(const BluetoothRemoteDevice &device);
126     static void HandleDisableDevice(const BluetoothRemoteDevice &device);
127     static void HandleWearEnable(const BluetoothRemoteDevice &device);
128     static void HandleWearDisable(const BluetoothRemoteDevice &device);
129     static void HandleUserSelection(const BluetoothRemoteDevice &device);
130     static void HandleStopVirtualCall(const BluetoothRemoteDevice &device);
131     static void HandleVirtualConnectDevice(const BluetoothRemoteDevice &device);
132     static void HandleRemoveVirtualConnectDevice(const BluetoothRemoteDevice &device);
133     static void AddDeviceInConfigVector(const BluetoothRemoteDevice &device,
134         std::vector<BluetoothRemoteDevice> &deviceVector);
135     static void RemoveDeviceInConfigVector(const BluetoothRemoteDevice &device,
136         std::vector<BluetoothRemoteDevice> &deviceVector);
137     static void NotifyToUpdateAudioDevice(const BluetoothRemoteDevice &device,
138         AudioStandard::AudioDeviceDescriptor &desc, DeviceStatus deviceStatus);
139     static void NotifyToUpdateVirtualDevice(const BluetoothRemoteDevice &device,
140         AudioStandard::AudioDeviceDescriptor &desc, DeviceStatus deviceStatus);
141     static bool IsHfpBluetoothDeviceExist(const std::string& macAddress);
142     static bool IsHfpBluetoothDeviceConnecting(const std::string& macAddress);
143     static void UpdateHfpDeviceConfiguration(const BluetoothRemoteDevice &device,
144         const AudioStandard::AudioStreamInfo &streamInfo);
145     static void OnScoStateChanged(const BluetoothRemoteDevice &device, bool isConnected, int reason);
146     static int32_t GetConnectedHfpBluetoothDevice(const std::string& macAddress, BluetoothRemoteDevice &device);
147     static std::vector<BluetoothRemoteDevice> GetAllHfpBluetoothDevice();
148     static void ClearAllHfpBluetoothDevice();
149     static std::vector<BluetoothRemoteDevice> GetHfpVirtualDeviceList();
150     static void RegisterDisconnectScoFunc(DisconnectScoForDevice func);
151 
152 private:
153     static void HandleUpdateDeviceCategory(const BluetoothRemoteDevice &device);
154     static AudioStandard::AudioDeviceDescriptor HandleConnectDeviceInner(const BluetoothRemoteDevice &device);
155     static void TryDisconnectScoAsync(const BluetoothRemoteDevice &device);
156     static void TryDisconnectScoSync(const BluetoothRemoteDevice &device, const std::string &reason);
157     static void OnDeviceCategoryUpdated(const BluetoothRemoteDevice &device,
158         AudioStandard::AudioDeviceDescriptor &desc);
159     static void OnDeviceEnableUpdated(const BluetoothRemoteDevice &device,
160         AudioStandard::AudioDeviceDescriptor &desc);
161 
162     static std::map<std::string, BluetoothRemoteDevice> hfpBluetoothDeviceMap_;
163     static std::map<std::string, BluetoothDeviceAction> wearDetectionStateMap_;
164     static std::vector<BluetoothRemoteDevice> privacyDevices_;
165     static std::vector<BluetoothRemoteDevice> commonDevices_;
166     static std::vector<BluetoothRemoteDevice> negativeDevices_;
167     static std::vector<BluetoothRemoteDevice> connectingDevices_;
168     static std::vector<BluetoothRemoteDevice> virtualDevices_;
169     static std::mutex stopVirtualCallHandleLock_;
170     static BluetoothStopVirtualCallHandle stopVirtualCallHandle_;
171     static DisconnectScoForDevice disconnectScoFun_;
172 };
173 } // namespace Bluetooth
174 } // namespace OHOS
175 
176 #endif // BLUETOOTH_DEVICE_MANAGER_H