xref: /aosp_15_r20/hardware/interfaces/audio/common/all-versions/default/service/service.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1 /*
2  * Copyright (C) 2016 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 #define LOG_TAG "audiohalservice"
18 
19 #include <signal.h>
20 #include <string>
21 #include <vector>
22 
23 #include <android/binder_process.h>
24 #include <binder/ProcessState.h>
25 #include <cutils/properties.h>
26 #include <dlfcn.h>
27 #include <hidl/HidlTransportSupport.h>
28 #include <hidl/LegacySupport.h>
29 #include <hwbinder/ProcessState.h>
30 
31 using namespace android::hardware;
32 using android::OK;
33 
34 using InterfacesList = std::vector<std::string>;
35 
36 /** Try to register the provided factories in the provided order.
37  *  If any registers successfully, do not register any other and return true.
38  *  If all fail, return false.
39  */
40 template <class Iter>
registerPassthroughServiceImplementations(Iter first,Iter last)41 static bool registerPassthroughServiceImplementations(Iter first, Iter last) {
42     for (; first != last; ++first) {
43         if (registerPassthroughServiceImplementation(*first) == OK) {
44             return true;
45         }
46     }
47     return false;
48 }
49 
registerExternalServiceImplementation(const std::string & libName,const std::string & funcName)50 static bool registerExternalServiceImplementation(const std::string& libName,
51                                                   const std::string& funcName) {
52     constexpr int dlMode = RTLD_LAZY;
53     void* handle = nullptr;
54     dlerror();  // clear
55     auto libPath = libName + ".so";
56     handle = dlopen(libPath.c_str(), dlMode);
57     if (handle == nullptr) {
58         const char* error = dlerror();
59         ALOGE("Failed to dlopen %s: %s", libPath.c_str(),
60               error != nullptr ? error : "unknown error");
61         return false;
62     }
63     binder_status_t (*factoryFunction)();
64     *(void**)(&factoryFunction) = dlsym(handle, funcName.c_str());
65     if (!factoryFunction) {
66         const char* error = dlerror();
67         ALOGE("Factory function %s not found in libName %s: %s", funcName.c_str(), libPath.c_str(),
68               error != nullptr ? error : "unknown error");
69         dlclose(handle);
70         return false;
71     }
72     return ((*factoryFunction)() == STATUS_OK);
73 }
74 
main(int,char * [])75 int main(int /* argc */, char* /* argv */ []) {
76     signal(SIGPIPE, SIG_IGN);
77 
78     if (::android::ProcessState::isVndservicemanagerEnabled()) {
79         ::android::ProcessState::initWithDriver("/dev/vndbinder");
80         ::android::ProcessState::self()->startThreadPool();
81     }
82 
83     ABinderProcess_setThreadPoolMaxThreadCount(1);
84     ABinderProcess_startThreadPool();
85 
86     const int32_t defaultValue = -1;
87     int32_t value =
88         property_get_int32("persist.vendor.audio.service.hwbinder.size_kbyte", defaultValue);
89     if (value != defaultValue) {
90         ALOGD("Configuring hwbinder with mmap size %d KBytes", value);
91         ProcessState::initWithMmapSize(static_cast<size_t>(value) * 1024);
92     }
93     configureRpcThreadpool(16, true /*callerWillJoin*/);
94 
95     // Automatic formatting tries to compact the lines, making them less readable
96     // clang-format off
97     const std::vector<InterfacesList> mandatoryInterfaces = {
98         {
99             "Audio Core API",
100             "[email protected]::IDevicesFactory",
101             "[email protected]::IDevicesFactory",
102             "[email protected]::IDevicesFactory",
103             "[email protected]::IDevicesFactory",
104             "[email protected]::IDevicesFactory",
105         },
106         {
107             "Audio Effect API",
108             "[email protected]::IEffectsFactory",
109             "[email protected]::IEffectsFactory",
110             "[email protected]::IEffectsFactory",
111             "[email protected]::IEffectsFactory",
112         }
113     };
114 
115     const std::vector<InterfacesList> optionalInterfaces = {
116         {
117             "Soundtrigger API",
118             "[email protected]::ISoundTriggerHw",
119             "[email protected]::ISoundTriggerHw",
120             "[email protected]::ISoundTriggerHw",
121             "[email protected]::ISoundTriggerHw",
122         },
123         {
124             "Bluetooth Audio API",
125             "[email protected]::IBluetoothAudioProvidersFactory",
126             "[email protected]::IBluetoothAudioProvidersFactory",
127             "[email protected]::IBluetoothAudioProvidersFactory",
128         },
129         // remove the old HIDL when Bluetooth Audio Hal V2 has offloading supported
130         {
131             "Bluetooth Audio Offload API",
132             "[email protected]::IBluetoothAudioOffload"
133         }
134     };
135 
136     const std::vector<std::pair<std::string,std::string>> optionalInterfaceSharedLibs = {
137         {
138             "android.hardware.bluetooth.audio-impl",
139             "createIBluetoothAudioProviderFactory",
140         },
141         {
142             "android.hardware.audio.sounddose-vendor-impl",
143             "createISoundDoseFactory",
144         },
145     };
146     // clang-format on
147 
148     for (const auto& listIter : mandatoryInterfaces) {
149         auto iter = listIter.begin();
150         const std::string& interfaceFamilyName = *iter++;
151         LOG_ALWAYS_FATAL_IF(!registerPassthroughServiceImplementations(iter, listIter.end()),
152                             "Could not register %s", interfaceFamilyName.c_str());
153     }
154 
155     for (const auto& listIter : optionalInterfaces) {
156         auto iter = listIter.begin();
157         const std::string& interfaceFamilyName = *iter++;
158         ALOGW_IF(!registerPassthroughServiceImplementations(iter, listIter.end()),
159                  "Could not register %s", interfaceFamilyName.c_str());
160     }
161 
162     for (const auto& interfacePair : optionalInterfaceSharedLibs) {
163         const std::string& libraryName = interfacePair.first;
164         const std::string& interfaceLoaderFuncName = interfacePair.second;
165         if (registerExternalServiceImplementation(libraryName, interfaceLoaderFuncName)) {
166             ALOGI("%s() from %s success", interfaceLoaderFuncName.c_str(), libraryName.c_str());
167         } else {
168             ALOGW("%s() from %s failed", interfaceLoaderFuncName.c_str(), libraryName.c_str());
169         }
170     }
171 
172     joinRpcThreadpool();
173 }
174