• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <android/hidl/manager/1.2/IServiceManager.h>
22 #include <base/logging.h>
23 #include <hidl/ServiceManagement.h>
24 
25 namespace bluetooth {
26 namespace audio {
27 
28 using ::android::hardware::hidl_vec;
29 
30 using IBluetoothAudioProvidersFactory_2_0 = ::android::hardware::bluetooth::
31     audio::V2_0::IBluetoothAudioProvidersFactory;
32 using IBluetoothAudioProvidersFactory_2_1 = ::android::hardware::bluetooth::
33     audio::V2_1::IBluetoothAudioProvidersFactory;
34 
35 constexpr char kFullyQualifiedInterfaceName_2_0[] =
36     "android.hardware.bluetooth.audio@2.0::IBluetoothAudioProvidersFactory";
37 constexpr char kFullyQualifiedInterfaceName_2_1[] =
38     "android.hardware.bluetooth.audio@2.1::IBluetoothAudioProvidersFactory";
39 
40 enum class BluetoothAudioHalVersion : uint8_t {
41   VERSION_2_0 = 0,
42   VERSION_2_1,
43   VERSION_UNAVAILABLE,
44 };
45 
46 class HalVersionManager {
47  public:
GetHalVersion()48   static BluetoothAudioHalVersion GetHalVersion() {
49     std::lock_guard<std::mutex> guard(instance_ptr->mutex_);
50     return instance_ptr->hal_version_;
51   }
52 
53   static android::sp<IBluetoothAudioProvidersFactory_2_1>
GetProvidersFactory_2_1()54   GetProvidersFactory_2_1() {
55     std::lock_guard<std::mutex> guard(instance_ptr->mutex_);
56     if (instance_ptr->hal_version_ != BluetoothAudioHalVersion::VERSION_2_1) {
57       return nullptr;
58     }
59     android::sp<IBluetoothAudioProvidersFactory_2_1> providers_factory =
60         IBluetoothAudioProvidersFactory_2_1::getService();
61     CHECK(providers_factory)
62         << "V2_1::IBluetoothAudioProvidersFactory::getService() failed";
63 
64     LOG(INFO) << "V2_1::IBluetoothAudioProvidersFactory::getService() returned "
65               << providers_factory.get()
66               << (providers_factory->isRemote() ? " (remote)" : " (local)");
67     return providers_factory;
68   }
69 
70   static android::sp<IBluetoothAudioProvidersFactory_2_0>
GetProvidersFactory_2_0()71   GetProvidersFactory_2_0() {
72     std::unique_lock<std::mutex> guard(instance_ptr->mutex_);
73     if (instance_ptr->hal_version_ == BluetoothAudioHalVersion::VERSION_2_1) {
74       guard.unlock();
75       return instance_ptr->GetProvidersFactory_2_1();
76     }
77     android::sp<IBluetoothAudioProvidersFactory_2_0> providers_factory =
78         IBluetoothAudioProvidersFactory_2_0::getService();
79     CHECK(providers_factory)
80         << "V2_0::IBluetoothAudioProvidersFactory::getService() failed";
81 
82     LOG(INFO) << "V2_0::IBluetoothAudioProvidersFactory::getService() returned "
83               << providers_factory.get()
84               << (providers_factory->isRemote() ? " (remote)" : " (local)");
85     guard.unlock();
86     return providers_factory;
87   }
88 
HalVersionManager()89   HalVersionManager() {
90     auto service_manager = android::hardware::defaultServiceManager1_2();
91     CHECK(service_manager != nullptr);
92     size_t instance_count = 0;
93     auto listManifestByInterface_cb =
94         [&instance_count](
95             const hidl_vec<android::hardware::hidl_string>& instanceNames) {
96           instance_count = instanceNames.size();
97         };
98     auto hidl_retval = service_manager->listManifestByInterface(
99         kFullyQualifiedInterfaceName_2_1, listManifestByInterface_cb);
100     if (!hidl_retval.isOk()) {
101       LOG(FATAL) << __func__ << ": IServiceManager::listByInterface failure: "
102                  << hidl_retval.description();
103       return;
104     }
105 
106     if (instance_count > 0) {
107       hal_version_ = BluetoothAudioHalVersion::VERSION_2_1;
108       return;
109     }
110 
111     hidl_retval = service_manager->listManifestByInterface(
112         kFullyQualifiedInterfaceName_2_0, listManifestByInterface_cb);
113     if (!hidl_retval.isOk()) {
114       LOG(FATAL) << __func__ << ": IServiceManager::listByInterface failure: "
115                  << hidl_retval.description();
116       return;
117     }
118 
119     if (instance_count > 0) {
120       hal_version_ = BluetoothAudioHalVersion::VERSION_2_0;
121       return;
122     }
123 
124     hal_version_ = BluetoothAudioHalVersion::VERSION_UNAVAILABLE;
125     LOG(ERROR) << __func__ << " No supported HAL version";
126   }
127 
128  private:
129   static std::unique_ptr<HalVersionManager> instance_ptr;
130   std::mutex mutex_;
131 
132   BluetoothAudioHalVersion hal_version_;
133 };
134 
135 }  // namespace audio
136 }  // namespace bluetooth
137