blob: 6ab747db32b8df9b6b14cb3a2f099df1793dcaf7 [file] [log] [blame]
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +00001/*
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
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000017#include <cstdlib>
18#include <ctime>
Mikhail Naganovdf5feba2022-12-15 00:11:14 +000019#include <utility>
Mikhail Naganov521fc492023-07-11 17:24:08 -070020#include <vector>
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000021
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000022#define LOG_TAG "AHAL_Main"
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000023#include <android-base/logging.h>
Mikhail Naganov521fc492023-07-11 17:24:08 -070024#include <android-base/properties.h>
Shunkai Yao39bf2c32022-12-06 03:25:59 +000025#include <android/binder_ibinder_platform.h>
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000026#include <android/binder_manager.h>
27#include <android/binder_process.h>
28
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000029#include "core-impl/AudioPolicyConfigXmlConverter.h"
30#include "core-impl/ChildInterface.h"
Lorena Torres-Huertabc585bd2022-10-23 20:41:35 +000031#include "core-impl/Config.h"
32#include "core-impl/Module.h"
33
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000034using aidl::android::hardware::audio::core::ChildInterface;
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000035using aidl::android::hardware::audio::core::Config;
36using aidl::android::hardware::audio::core::Module;
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000037using aidl::android::hardware::audio::core::internal::AudioPolicyConfigXmlConverter;
38
39namespace {
40
41ChildInterface<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
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000064
65int main() {
Mikhail Naganov4f5d3f12022-07-22 23:23:25 +000066 // Random values are used in the implementation.
67 std::srand(std::time(nullptr));
68
Mikhail Naganov16db9b72022-06-17 21:36:18 +000069 // This is a debug implementation, always enable debug logging.
70 android::base::SetMinimumLogSeverity(::android::base::DEBUG);
Mikhail Naganov1f72fd42023-02-02 15:26:04 -080071 // For more logs, use VERBOSE, however this may hinder performance.
72 // android::base::SetMinimumLogSeverity(::android::base::VERBOSE);
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000073 ABinderProcess_setThreadPoolMaxThreadCount(16);
74
Steven Moreland705c39b2023-03-06 14:30:15 +000075 // Guaranteed log for b/210919187 and logd_integration_test
76 LOG(INFO) << "Init for Audio AIDL HAL";
77
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000078 AudioPolicyConfigXmlConverter audioPolicyConverter{
79 ::android::audio_get_audio_policy_config_file()};
80
Mikhail Naganov16db9b72022-06-17 21:36:18 +000081 // Make the default config service
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000082 auto config = ndk::SharedRefBase::make<Config>(audioPolicyConverter);
83 const std::string configFqn = std::string().append(Config::descriptor).append("/default");
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000084 binder_status_t status =
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000085 AServiceManager_addService(config->asBinder().get(), configFqn.c_str());
86 if (status != STATUS_OK) {
87 LOG(ERROR) << "failed to register service for \"" << configFqn << "\"";
88 }
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +000089
Mikhail Naganovc8e43122022-12-09 00:33:47 +000090 // Make modules
Lorena Torres-Huerta394e2522022-12-20 02:21:41 +000091 std::vector<ChildInterface<Module>> moduleInstances;
92 auto configs(audioPolicyConverter.releaseModuleConfigs());
93 for (std::pair<std::string, std::unique_ptr<Module::Configuration>>& configPair : *configs) {
94 std::string name = configPair.first;
95 if (auto instance = createModule(name, std::move(configPair.second)); instance) {
96 moduleInstances.push_back(std::move(instance));
97 }
98 }
Mikhail Naganovc8e43122022-12-09 00:33:47 +000099
Mikhail Naganovdf5adfd2021-11-11 22:09:22 +0000100 ABinderProcess_joinThreadPool();
101 return EXIT_FAILURE; // should not reach
102}