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 #include "hal_version_manager.h"
18 
19 #include <android/binder_manager.h>
20 #include <android/hidl/manager/1.2/IServiceManager.h>
21 #include <bluetooth/log.h>
22 #include <hidl/ServiceManagement.h>
23 
24 #include <memory>
25 
26 #include "aidl/audio_aidl_interfaces.h"
27 
28 namespace bluetooth {
29 namespace audio {
30 
31 using ::aidl::android::hardware::bluetooth::audio::IBluetoothAudioProviderFactory;
32 
33 static const std::string kDefaultAudioProviderFactoryInterface =
34         std::string() + IBluetoothAudioProviderFactory::descriptor + "/default";
35 
toString(BluetoothAudioHalTransport transport)36 std::string toString(BluetoothAudioHalTransport transport) {
37   switch (transport) {
38     case BluetoothAudioHalTransport::UNKNOWN:
39       return "UNKNOWN";
40     case BluetoothAudioHalTransport::HIDL:
41       return "HIDL";
42     case BluetoothAudioHalTransport::AIDL:
43       return "AIDL";
44     default:
45       return std::to_string(static_cast<int32_t>(transport));
46   }
47 }
48 
49 const BluetoothAudioHalVersion BluetoothAudioHalVersion::VERSION_UNAVAILABLE =
50         BluetoothAudioHalVersion();
51 const BluetoothAudioHalVersion BluetoothAudioHalVersion::VERSION_2_0 =
52         BluetoothAudioHalVersion(BluetoothAudioHalTransport::HIDL, 2, 0);
53 const BluetoothAudioHalVersion BluetoothAudioHalVersion::VERSION_2_1 =
54         BluetoothAudioHalVersion(BluetoothAudioHalTransport::HIDL, 2, 1);
55 const BluetoothAudioHalVersion BluetoothAudioHalVersion::VERSION_AIDL_V1 =
56         BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 1, 0);
57 const BluetoothAudioHalVersion BluetoothAudioHalVersion::VERSION_AIDL_V2 =
58         BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 2, 0);
59 const BluetoothAudioHalVersion BluetoothAudioHalVersion::VERSION_AIDL_V3 =
60         BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 3, 0);
61 const BluetoothAudioHalVersion BluetoothAudioHalVersion::VERSION_AIDL_V4 =
62         BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, 4, 0);
63 
64 // Ideally HalVersionManager can be a singleton class
65 std::unique_ptr<HalVersionManager> HalVersionManager::instance_ptr =
66         std::make_unique<HalVersionManager>();
67 
68 /**
69  * A singleton implementation to get the AIDL interface version.
70  */
GetAidlInterfaceVersion()71 BluetoothAudioHalVersion GetAidlInterfaceVersion() {
72   static auto aidl_version = []() -> BluetoothAudioHalVersion {
73     int version = 0;
74     auto provider_factory = IBluetoothAudioProviderFactory::fromBinder(::ndk::SpAIBinder(
75             AServiceManager_waitForService(kDefaultAudioProviderFactoryInterface.c_str())));
76 
77     if (provider_factory == nullptr) {
78       log::error("getInterfaceVersion: Can't get aidl version from unknown factory");
79       return BluetoothAudioHalVersion::VERSION_UNAVAILABLE;
80     }
81 
82     auto aidl_retval = provider_factory->getInterfaceVersion(&version);
83     if (!aidl_retval.isOk()) {
84       log::error("BluetoothAudioHal::getInterfaceVersion failure: {}",
85                  aidl_retval.getDescription());
86       return BluetoothAudioHalVersion::VERSION_UNAVAILABLE;
87     }
88 
89     return BluetoothAudioHalVersion(BluetoothAudioHalTransport::AIDL, version, 0);
90   }();
91 
92   return aidl_version;
93 }
94 
GetHalTransport()95 BluetoothAudioHalTransport HalVersionManager::GetHalTransport() {
96   return instance_ptr->hal_version_.getTransport();
97 }
98 
GetHalVersion()99 BluetoothAudioHalVersion HalVersionManager::GetHalVersion() {
100   std::lock_guard<std::mutex> guard(instance_ptr->mutex_);
101   return instance_ptr->hal_version_;
102 }
103 
GetProvidersFactory_2_1()104 android::sp<IBluetoothAudioProvidersFactory_2_1> HalVersionManager::GetProvidersFactory_2_1() {
105   std::lock_guard<std::mutex> guard(instance_ptr->mutex_);
106   if (instance_ptr->hal_version_ != BluetoothAudioHalVersion::VERSION_2_1) {
107     return nullptr;
108   }
109   android::sp<IBluetoothAudioProvidersFactory_2_1> providers_factory =
110           IBluetoothAudioProvidersFactory_2_1::getService();
111   log::assert_that(providers_factory != nullptr,
112                    "V2_1::IBluetoothAudioProvidersFactory::getService() failed");
113 
114   log::info("V2_1::IBluetoothAudioProvidersFactory::getService() returned {}{}",
115             std::format_ptr(providers_factory.get()),
116             (providers_factory->isRemote() ? " (remote)" : " (local)"));
117   return providers_factory;
118 }
119 
GetProvidersFactory_2_0()120 android::sp<IBluetoothAudioProvidersFactory_2_0> HalVersionManager::GetProvidersFactory_2_0() {
121   std::unique_lock<std::mutex> guard(instance_ptr->mutex_);
122   if (instance_ptr->hal_version_ == BluetoothAudioHalVersion::VERSION_2_1) {
123     guard.unlock();
124     return instance_ptr->GetProvidersFactory_2_1();
125   }
126   android::sp<IBluetoothAudioProvidersFactory_2_0> providers_factory =
127           IBluetoothAudioProvidersFactory_2_0::getService();
128   log::assert_that(providers_factory != nullptr,
129                    "V2_0::IBluetoothAudioProvidersFactory::getService() failed");
130 
131   log::info("V2_0::IBluetoothAudioProvidersFactory::getService() returned {}{}",
132             std::format_ptr(providers_factory.get()),
133             (providers_factory->isRemote() ? " (remote)" : " (local)"));
134   guard.unlock();
135   return providers_factory;
136 }
137 
HalVersionManager()138 HalVersionManager::HalVersionManager() {
139   hal_transport_ = BluetoothAudioHalTransport::UNKNOWN;
140   if (AServiceManager_checkService(kDefaultAudioProviderFactoryInterface.c_str()) != nullptr) {
141     hal_version_ = GetAidlInterfaceVersion();
142     hal_transport_ = BluetoothAudioHalTransport::AIDL;
143     return;
144   }
145 
146   auto service_manager = android::hardware::defaultServiceManager1_2();
147   log::assert_that(service_manager != nullptr, "assert failed: service_manager != nullptr");
148   size_t instance_count = 0;
149   auto listManifestByInterface_cb =
150           [&instance_count](const hidl_vec<android::hardware::hidl_string>& instanceNames) {
151             instance_count = instanceNames.size();
152           };
153   auto hidl_retval = service_manager->listManifestByInterface(kFullyQualifiedInterfaceName_2_1,
154                                                               listManifestByInterface_cb);
155   if (!hidl_retval.isOk()) {
156     log::fatal("IServiceManager::listByInterface failure: {}", hidl_retval.description());
157     return;
158   }
159 
160   if (instance_count > 0) {
161     hal_version_ = BluetoothAudioHalVersion::VERSION_2_1;
162     hal_transport_ = BluetoothAudioHalTransport::HIDL;
163     return;
164   }
165 
166   hidl_retval = service_manager->listManifestByInterface(kFullyQualifiedInterfaceName_2_0,
167                                                          listManifestByInterface_cb);
168   if (!hidl_retval.isOk()) {
169     log::fatal("IServiceManager::listByInterface failure: {}", hidl_retval.description());
170     return;
171   }
172 
173   if (instance_count > 0) {
174     hal_version_ = BluetoothAudioHalVersion::VERSION_2_0;
175     hal_transport_ = BluetoothAudioHalTransport::HIDL;
176     return;
177   }
178 
179   hal_version_ = BluetoothAudioHalVersion::VERSION_UNAVAILABLE;
180   log::error("No supported HAL version");
181 }
182 
183 }  // namespace audio
184 }  // namespace bluetooth
185