1 /*
2 * Copyright (C) 2022 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 <cstdlib>
18 #include <ctime>
19 #include <utility>
20 #include <vector>
21
22 #define LOG_TAG "AHAL_Main"
23 #include <android-base/logging.h>
24 #include <android-base/properties.h>
25 #include <android/binder_ibinder_platform.h>
26 #include <android/binder_manager.h>
27 #include <android/binder_process.h>
28
29 #include "core-impl/AudioPolicyConfigXmlConverter.h"
30 #include "core-impl/ChildInterface.h"
31 #include "core-impl/Config.h"
32 #include "core-impl/Module.h"
33
34 using aidl::android::hardware::audio::core::ChildInterface;
35 using aidl::android::hardware::audio::core::Config;
36 using aidl::android::hardware::audio::core::Module;
37 using aidl::android::hardware::audio::core::internal::AudioPolicyConfigXmlConverter;
38
39 namespace {
40
createModule(const std::string & name,std::unique_ptr<Module::Configuration> && config)41 ChildInterface<Module> createModule(const std::string& name,
42 std::unique_ptr<Module::Configuration>&& config) {
43 ChildInterface<Module> result;
44 {
45 auto moduleType = Module::typeFromString(name);
46 if (!moduleType.has_value()) {
47 LOG(ERROR) << __func__ << ": module type \"" << name << "\" is not supported";
48 return result;
49 }
50 auto module = Module::createInstance(*moduleType, std::move(config));
51 if (module == nullptr) return result;
52 result = std::move(module);
53 }
54 const std::string moduleFqn = std::string().append(Module::descriptor).append("/").append(name);
55 binder_status_t status = AServiceManager_addService(result.getBinder(), moduleFqn.c_str());
56 if (status != STATUS_OK) {
57 LOG(ERROR) << __func__ << ": failed to register service for \"" << moduleFqn << "\"";
58 return ChildInterface<Module>();
59 }
60 return result;
61 };
62
63 } // namespace
64
main()65 int main() {
66 // Random values are used in the implementation.
67 std::srand(std::time(nullptr));
68
69 // This is a debug implementation, always enable debug logging.
70 android::base::SetMinimumLogSeverity(::android::base::DEBUG);
71 // For more logs, use VERBOSE, however this may hinder performance.
72 // android::base::SetMinimumLogSeverity(::android::base::VERBOSE);
73 ABinderProcess_setThreadPoolMaxThreadCount(16);
74 ABinderProcess_startThreadPool();
75
76 // Guaranteed log for b/210919187 and logd_integration_test
77 LOG(INFO) << "Init for Audio AIDL HAL";
78
79 AudioPolicyConfigXmlConverter audioPolicyConverter{
80 ::android::audio_get_audio_policy_config_file()};
81
82 // Make the default config service
83 auto config = ndk::SharedRefBase::make<Config>(audioPolicyConverter);
84 const std::string configFqn = std::string().append(Config::descriptor).append("/default");
85 binder_status_t status =
86 AServiceManager_addService(config->asBinder().get(), configFqn.c_str());
87 if (status != STATUS_OK) {
88 LOG(ERROR) << "failed to register service for \"" << configFqn << "\"";
89 }
90
91 // Make modules
92 std::vector<ChildInterface<Module>> moduleInstances;
93 auto configs(audioPolicyConverter.releaseModuleConfigs());
94 for (std::pair<std::string, std::unique_ptr<Module::Configuration>>& configPair : *configs) {
95 std::string name = configPair.first;
96 if (auto instance = createModule(name, std::move(configPair.second)); instance) {
97 moduleInstances.push_back(std::move(instance));
98 }
99 }
100
101 ABinderProcess_joinThreadPool();
102 return EXIT_FAILURE; // should not reach
103 }
104