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 =
28         ::android::hardware::bluetooth::audio::V2_0::IBluetoothAudioProvidersFactory;
29 using IBluetoothAudioProvidersFactory_2_1 =
30         ::android::hardware::bluetooth::audio::V2_1::IBluetoothAudioProvidersFactory;
31 
32 constexpr char kFullyQualifiedInterfaceName_2_0[] =
33         "[email protected]::IBluetoothAudioProvidersFactory";
34 constexpr char kFullyQualifiedInterfaceName_2_1[] =
35         "[email protected]::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(
57           BluetoothAudioHalTransport transport = 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) != std::tie(rhs.mTransport, rhs.mMajor, rhs.mMinor);
68   }
69   inline bool operator<(const BluetoothAudioHalVersion& rhs) const {
70     return std::tie(mTransport, mMajor, mMinor) < std::tie(rhs.mTransport, rhs.mMajor, rhs.mMinor);
71   }
72   inline bool operator<=(const BluetoothAudioHalVersion& rhs) const {
73     return std::tie(mTransport, mMajor, mMinor) <= std::tie(rhs.mTransport, rhs.mMajor, rhs.mMinor);
74   }
75   inline bool operator==(const BluetoothAudioHalVersion& rhs) const {
76     return std::tie(mTransport, mMajor, mMinor) == std::tie(rhs.mTransport, rhs.mMajor, rhs.mMinor);
77   }
78   inline bool operator>(const BluetoothAudioHalVersion& rhs) const {
79     return std::tie(mTransport, mMajor, mMinor) > std::tie(rhs.mTransport, rhs.mMajor, rhs.mMinor);
80   }
81   inline bool operator>=(const BluetoothAudioHalVersion& rhs) const {
82     return std::tie(mTransport, mMajor, mMinor) >= std::tie(rhs.mTransport, rhs.mMajor, rhs.mMinor);
83   }
84 
toString()85   inline std::string toString() const {
86     std::ostringstream os;
87     os << "BluetoothAudioHalVersion: {";
88     os << "transport: " << bluetooth::audio::toString(mTransport);
89     os << ", major: " << std::to_string(mMajor);
90     os << ", minor: " << std::to_string(mMinor);
91     os << "}";
92     return os.str();
93   }
94 
95   /* Known HalVersion definitions */
96   static const BluetoothAudioHalVersion VERSION_UNAVAILABLE;
97   static const BluetoothAudioHalVersion VERSION_2_0;
98   static const BluetoothAudioHalVersion VERSION_2_1;
99   static const BluetoothAudioHalVersion VERSION_AIDL_V1;
100   static const BluetoothAudioHalVersion VERSION_AIDL_V2;
101   static const BluetoothAudioHalVersion VERSION_AIDL_V3;
102   static const BluetoothAudioHalVersion VERSION_AIDL_V4;
103 
104 private:
105   BluetoothAudioHalTransport mTransport = BluetoothAudioHalTransport::UNKNOWN;
106   uint16_t mMajor = 0;
107   uint16_t mMinor = 0;
108 };
109 
110 class HalVersionManager {
111 public:
112   static BluetoothAudioHalVersion GetHalVersion();
113 
114   static BluetoothAudioHalTransport GetHalTransport();
115 
116   static android::sp<IBluetoothAudioProvidersFactory_2_1> GetProvidersFactory_2_1();
117 
118   static android::sp<IBluetoothAudioProvidersFactory_2_0> GetProvidersFactory_2_0();
119 
120   HalVersionManager();
121 
122 private:
123   static std::unique_ptr<HalVersionManager> instance_ptr;
124   std::mutex mutex_;
125 
126   BluetoothAudioHalVersion hal_version_;
127   BluetoothAudioHalTransport hal_transport_;
128 };
129 
130 // Return the supported AIDL version.
131 BluetoothAudioHalVersion GetAidlInterfaceVersion();
132 
133 }  // namespace audio
134 }  // namespace bluetooth
135