1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <android/hardware/bluetooth/audio/2.1/IBluetoothAudioProvidersFactory.h>
20 #include <android/hardware/bluetooth/audio/2.1/types.h>
21 
22 namespace bluetooth {
23 namespace audio {
24 
25 using ::android::hardware::hidl_vec;
26 
27 using IBluetoothAudioProvidersFactory_2_0 = ::android::hardware::bluetooth::
28     audio::V2_0::IBluetoothAudioProvidersFactory;
29 using IBluetoothAudioProvidersFactory_2_1 = ::android::hardware::bluetooth::
30     audio::V2_1::IBluetoothAudioProvidersFactory;
31 
32 constexpr char kFullyQualifiedInterfaceName_2_0[] =
33     "android.hardware.bluetooth.audio@2.0::IBluetoothAudioProvidersFactory";
34 constexpr char kFullyQualifiedInterfaceName_2_1[] =
35     "android.hardware.bluetooth.audio@2.1::IBluetoothAudioProvidersFactory";
36 
37 /**
38  * The type of HAL transport, it's important to have
39  * BluetoothAudioHalTransport::HIDL value defined smaller than
40  * BluetoothAudioHalTransport::AIDL.
41  */
42 enum class BluetoothAudioHalTransport : uint8_t {
43   // Uninit, default value
44   UNKNOWN,
45   HIDL,
46   AIDL,
47 };
48 
49 std::string toString(BluetoothAudioHalTransport transport);
50 
51 /**
52  * A hal version class with built-in comparison operators.
53  */
54 class BluetoothAudioHalVersion {
55  public:
56   BluetoothAudioHalVersion(BluetoothAudioHalTransport transport =
57                                BluetoothAudioHalTransport::UNKNOWN,
58                            uint16_t major = 0, uint16_t minor = 0)
mTransport(transport)59       : mTransport(transport), mMajor(major), mMinor(minor) {}
60 
isHIDL()61   bool isHIDL() const { return mTransport == BluetoothAudioHalTransport::HIDL; }
isAIDL()62   bool isAIDL() const { return mTransport == BluetoothAudioHalTransport::AIDL; }
63 
getTransport()64   BluetoothAudioHalTransport getTransport() const { return mTransport; }
65 
66   inline bool operator!=(const BluetoothAudioHalVersion& rhs) const {
67     return std::tie(mTransport, mMajor, mMinor) !=
68            std::tie(rhs.mTransport, rhs.mMajor, rhs.mMinor);
69   }
70   inline bool operator<(const BluetoothAudioHalVersion& rhs) const {
71     return std::tie(mTransport, mMajor, mMinor) <
72            std::tie(rhs.mTransport, rhs.mMajor, rhs.mMinor);
73   }
74   inline bool operator<=(const BluetoothAudioHalVersion& rhs) const {
75     return std::tie(mTransport, mMajor, mMinor) <=
76            std::tie(rhs.mTransport, rhs.mMajor, rhs.mMinor);
77   }
78   inline bool operator==(const BluetoothAudioHalVersion& rhs) const {
79     return std::tie(mTransport, mMajor, mMinor) ==
80            std::tie(rhs.mTransport, rhs.mMajor, rhs.mMinor);
81   }
82   inline bool operator>(const BluetoothAudioHalVersion& rhs) const {
83     return std::tie(mTransport, mMajor, mMinor) >
84            std::tie(rhs.mTransport, rhs.mMajor, rhs.mMinor);
85   }
86   inline bool operator>=(const BluetoothAudioHalVersion& rhs) const {
87     return std::tie(mTransport, mMajor, mMinor) >=
88            std::tie(rhs.mTransport, rhs.mMajor, rhs.mMinor);
89   }
90 
toString()91   inline std::string toString() const {
92     std::ostringstream os;
93     os << "BluetoothAudioHalVersion: {";
94     os << "transport: " << bluetooth::audio::toString(mTransport);
95     os << ", major: " << std::to_string(mMajor);
96     os << ", minor: " << std::to_string(mMinor);
97     os << "}";
98     return os.str();
99   }
100 
101   /* Known HalVersion definitions */
102   static const BluetoothAudioHalVersion VERSION_UNAVAILABLE;
103   static const BluetoothAudioHalVersion VERSION_2_0;
104   static const BluetoothAudioHalVersion VERSION_2_1;
105   static const BluetoothAudioHalVersion VERSION_AIDL_V1;
106   static const BluetoothAudioHalVersion VERSION_AIDL_V2;
107   static const BluetoothAudioHalVersion VERSION_AIDL_V3;
108   static const BluetoothAudioHalVersion VERSION_AIDL_V4;
109 
110  private:
111   BluetoothAudioHalTransport mTransport = BluetoothAudioHalTransport::UNKNOWN;
112   uint16_t mMajor = 0;
113   uint16_t mMinor = 0;
114 };
115 
116 class HalVersionManager {
117  public:
118   static BluetoothAudioHalVersion GetHalVersion();
119 
120   static BluetoothAudioHalTransport GetHalTransport();
121 
122   static android::sp<IBluetoothAudioProvidersFactory_2_1>
123   GetProvidersFactory_2_1();
124 
125   static android::sp<IBluetoothAudioProvidersFactory_2_0>
126   GetProvidersFactory_2_0();
127 
128   HalVersionManager();
129 
130  private:
131   static std::unique_ptr<HalVersionManager> instance_ptr;
132   std::mutex mutex_;
133 
134   BluetoothAudioHalVersion hal_version_;
135   BluetoothAudioHalTransport hal_transport_;
136 };
137 
138 }  // namespace audio
139 }  // namespace bluetooth
140