• 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 
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_info.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 std::string GetEncryptAddr(const std::string &addr);
36 
37 class MediaBluetoothDeviceManager {
38 public:
39     MediaBluetoothDeviceManager() = default;
40     virtual ~MediaBluetoothDeviceManager() = default;
41     static void SetMediaStack(const BluetoothRemoteDevice &device, int action);
42     static void HandleConnectingDevice(const BluetoothRemoteDevice &device);
43     static void HandleConnectDevice(const BluetoothRemoteDevice &device);
44     static void HandleDisconnectDevice(const BluetoothRemoteDevice &device);
45     static void HandleWearDevice(const BluetoothRemoteDevice &device);
46     static void HandleUnwearDevice(const BluetoothRemoteDevice &device);
47     static void HandleEnableDevice(const BluetoothRemoteDevice &device);
48     static void HandleDisableDevice(const BluetoothRemoteDevice &device);
49     static void HandleWearEnable(const BluetoothRemoteDevice &device);
50     static void HandleWearDisable(const BluetoothRemoteDevice &device);
51     static void HandleUserSelection(const BluetoothRemoteDevice &device);
52     static void HandleVirtualConnectDevice(const BluetoothRemoteDevice &device);
53     static void HandleRemoveVirtualConnectDevice(const BluetoothRemoteDevice &device);
54     static void AddDeviceInConfigVector(const BluetoothRemoteDevice &device,
55         std::vector<BluetoothRemoteDevice> &deviceVector);
56     static void RemoveDeviceInConfigVector(const BluetoothRemoteDevice &device,
57         std::vector<BluetoothRemoteDevice> &deviceVector);
58     static void NotifyToUpdateAudioDevice(const BluetoothRemoteDevice &device,
59         AudioStandard::AudioDeviceDescriptor &desc, DeviceStatus deviceStatus);
60     static void NotifyToUpdateVirtualDevice(const BluetoothRemoteDevice &device,
61         AudioStandard::AudioDeviceDescriptor &desc, DeviceStatus deviceStatus);
62     static bool IsA2dpBluetoothDeviceExist(const std::string& macAddress);
63     static bool IsA2dpBluetoothDeviceConnecting(const std::string& macAddress);
64     static int32_t GetConnectedA2dpBluetoothDevice(const std::string& macAddress, BluetoothRemoteDevice &device);
65     static void UpdateA2dpDeviceConfiguration(const BluetoothRemoteDevice &device,
66         const AudioStandard::AudioStreamInfo &streamInfo);
67     static std::vector<BluetoothRemoteDevice> GetAllA2dpBluetoothDevice();
68     static void ClearAllA2dpBluetoothDevice();
69     static std::vector<BluetoothRemoteDevice> GetA2dpVirtualDeviceList();
70 
71 private:
72     static std::map<std::string, BluetoothRemoteDevice> a2dpBluetoothDeviceMap_;
73     static std::map<std::string, BluetoothDeviceAction> wearDetectionStateMap_;
74     static std::vector<BluetoothRemoteDevice> privacyDevices_;
75     static std::vector<BluetoothRemoteDevice> commonDevices_;
76     static std::vector<BluetoothRemoteDevice> negativeDevices_;
77     static std::vector<BluetoothRemoteDevice> connectingDevices_;
78     static std::vector<BluetoothRemoteDevice> virtualDevices_;
79     static void HandleUpdateDeviceCategory(const BluetoothRemoteDevice &device);
80     static AudioStandard::AudioDeviceDescriptor HandleConnectDeviceInner(const BluetoothRemoteDevice &device);
81 };
82 
83 struct BluetoothStopVirtualCallHandle {
84     BluetoothRemoteDevice device;
85     bool isWaitingForStoppingVirtualCall;
86 };
87 
88 class HfpBluetoothDeviceManager {
89 public:
90     HfpBluetoothDeviceManager() = default;
91     virtual ~HfpBluetoothDeviceManager() = default;
92     static void SetHfpStack(const BluetoothRemoteDevice &device, int action);
93     static void HandleConnectingDevice(const BluetoothRemoteDevice &device);
94     static void HandleConnectDevice(const BluetoothRemoteDevice &device);
95     static void HandleDisconnectDevice(const BluetoothRemoteDevice &device);
96     static void HandleWearDevice(const BluetoothRemoteDevice &device);
97     static void HandleUnwearDevice(const BluetoothRemoteDevice &device);
98     static void HandleEnableDevice(const BluetoothRemoteDevice &device);
99     static void HandleDisableDevice(const BluetoothRemoteDevice &device);
100     static void HandleWearEnable(const BluetoothRemoteDevice &device);
101     static void HandleWearDisable(const BluetoothRemoteDevice &device);
102     static void HandleUserSelection(const BluetoothRemoteDevice &device);
103     static void HandleStopVirtualCall(const BluetoothRemoteDevice &device);
104     static void HandleVirtualConnectDevice(const BluetoothRemoteDevice &device);
105     static void HandleRemoveVirtualConnectDevice(const BluetoothRemoteDevice &device);
106     static void AddDeviceInConfigVector(const BluetoothRemoteDevice &device,
107         std::vector<BluetoothRemoteDevice> &deviceVector);
108     static void RemoveDeviceInConfigVector(const BluetoothRemoteDevice &device,
109         std::vector<BluetoothRemoteDevice> &deviceVector);
110     static void NotifyToUpdateAudioDevice(const BluetoothRemoteDevice &device,
111         AudioStandard::AudioDeviceDescriptor &desc, DeviceStatus deviceStatus);
112     static void NotifyToUpdateVirtualDevice(const BluetoothRemoteDevice &device,
113         AudioStandard::AudioDeviceDescriptor &desc, DeviceStatus deviceStatus);
114     static bool IsHfpBluetoothDeviceExist(const std::string& macAddress);
115     static bool IsHfpBluetoothDeviceConnecting(const std::string& macAddress);
116     static void UpdateHfpDeviceConfiguration(const BluetoothRemoteDevice &device,
117         const AudioStandard::AudioStreamInfo &streamInfo);
118     static void OnScoStateChanged(const BluetoothRemoteDevice &device, bool isConnected, int reason);
119     static int32_t GetConnectedHfpBluetoothDevice(const std::string& macAddress, BluetoothRemoteDevice &device);
120     static std::vector<BluetoothRemoteDevice> GetAllHfpBluetoothDevice();
121     static void ClearAllHfpBluetoothDevice();
122     static std::vector<BluetoothRemoteDevice> GetHfpVirtualDeviceList();
123 
124 private:
125     static void HandleUpdateDeviceCategory(const BluetoothRemoteDevice &device);
126     static AudioStandard::AudioDeviceDescriptor HandleConnectDeviceInner(const BluetoothRemoteDevice &device);
127     static std::map<std::string, BluetoothRemoteDevice> hfpBluetoothDeviceMap_;
128     static std::map<std::string, BluetoothDeviceAction> wearDetectionStateMap_;
129     static std::vector<BluetoothRemoteDevice> privacyDevices_;
130     static std::vector<BluetoothRemoteDevice> commonDevices_;
131     static std::vector<BluetoothRemoteDevice> negativeDevices_;
132     static std::vector<BluetoothRemoteDevice> connectingDevices_;
133     static std::vector<BluetoothRemoteDevice> virtualDevices_;
134     static std::mutex stopVirtualCallHandleLock_;
135     static BluetoothStopVirtualCallHandle stopVirtualCallHandle_;
136 };
137 } // namespace Bluetooth
138 } // namespace OHOS
139 
140 #endif // BLUETOOTH_DEVICE_MANAGER_H