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 };
80 
81 struct BluetoothStopVirtualCallHandle {
82     BluetoothRemoteDevice device;
83     bool isWaitingForStoppingVirtualCall;
84 };
85 
86 class HfpBluetoothDeviceManager {
87 public:
88     HfpBluetoothDeviceManager() = default;
89     virtual ~HfpBluetoothDeviceManager() = default;
90     static void SetHfpStack(const BluetoothRemoteDevice &device, int action);
91     static void HandleConnectingDevice(const BluetoothRemoteDevice &device);
92     static void HandleConnectDevice(const BluetoothRemoteDevice &device);
93     static void HandleDisconnectDevice(const BluetoothRemoteDevice &device);
94     static void HandleWearDevice(const BluetoothRemoteDevice &device);
95     static void HandleUnwearDevice(const BluetoothRemoteDevice &device);
96     static void HandleEnableDevice(const BluetoothRemoteDevice &device);
97     static void HandleDisableDevice(const BluetoothRemoteDevice &device);
98     static void HandleWearEnable(const BluetoothRemoteDevice &device);
99     static void HandleWearDisable(const BluetoothRemoteDevice &device);
100     static void HandleUserSelection(const BluetoothRemoteDevice &device);
101     static void HandleStopVirtualCall(const BluetoothRemoteDevice &device);
102     static void HandleVirtualConnectDevice(const BluetoothRemoteDevice &device);
103     static void HandleRemoveVirtualConnectDevice(const BluetoothRemoteDevice &device);
104     static void AddDeviceInConfigVector(const BluetoothRemoteDevice &device,
105         std::vector<BluetoothRemoteDevice> &deviceVector);
106     static void RemoveDeviceInConfigVector(const BluetoothRemoteDevice &device,
107         std::vector<BluetoothRemoteDevice> &deviceVector);
108     static void NotifyToUpdateAudioDevice(const BluetoothRemoteDevice &device,
109         AudioStandard::AudioDeviceDescriptor &desc, DeviceStatus deviceStatus);
110     static void NotifyToUpdateVirtualDevice(const BluetoothRemoteDevice &device,
111         AudioStandard::AudioDeviceDescriptor &desc, DeviceStatus deviceStatus);
112     static bool IsHfpBluetoothDeviceExist(const std::string& macAddress);
113     static bool IsHfpBluetoothDeviceConnecting(const std::string& macAddress);
114     static void UpdateHfpDeviceConfiguration(const BluetoothRemoteDevice &device,
115         const AudioStandard::AudioStreamInfo &streamInfo);
116     static void OnScoStateChanged(const BluetoothRemoteDevice &device, bool isConnected, int reason);
117     static int32_t GetConnectedHfpBluetoothDevice(const std::string& macAddress, BluetoothRemoteDevice &device);
118     static std::vector<BluetoothRemoteDevice> GetAllHfpBluetoothDevice();
119     static void ClearAllHfpBluetoothDevice();
120     static std::vector<BluetoothRemoteDevice> GetHfpVirtualDeviceList();
121 
122 private:
123     static std::map<std::string, BluetoothRemoteDevice> hfpBluetoothDeviceMap_;
124     static std::map<std::string, BluetoothDeviceAction> wearDetectionStateMap_;
125     static std::vector<BluetoothRemoteDevice> privacyDevices_;
126     static std::vector<BluetoothRemoteDevice> commonDevices_;
127     static std::vector<BluetoothRemoteDevice> negativeDevices_;
128     static std::vector<BluetoothRemoteDevice> connectingDevices_;
129     static std::vector<BluetoothRemoteDevice> virtualDevices_;
130     static std::mutex stopVirtualCallHandleLock_;
131     static BluetoothStopVirtualCallHandle stopVirtualCallHandle_;
132 };
133 } // namespace Bluetooth
134 } // namespace OHOS
135 
136 #endif // BLUETOOTH_DEVICE_MANAGER_H