Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 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 | |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 17 | #include <cstdlib> |
| 18 | #include <ctime> |
Mikhail Naganov | df5feba | 2022-12-15 00:11:14 +0000 | [diff] [blame] | 19 | #include <utility> |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 20 | #include <vector> |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 21 | |
Lorena Torres-Huerta | 394e252 | 2022-12-20 02:21:41 +0000 | [diff] [blame] | 22 | #define LOG_TAG "AHAL_Main" |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 23 | #include <android-base/logging.h> |
Mikhail Naganov | 521fc49 | 2023-07-11 17:24:08 -0700 | [diff] [blame] | 24 | #include <android-base/properties.h> |
Shunkai Yao | 39bf2c3 | 2022-12-06 03:25:59 +0000 | [diff] [blame] | 25 | #include <android/binder_ibinder_platform.h> |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 26 | #include <android/binder_manager.h> |
| 27 | #include <android/binder_process.h> |
| 28 | |
Lorena Torres-Huerta | 394e252 | 2022-12-20 02:21:41 +0000 | [diff] [blame] | 29 | #include "core-impl/AudioPolicyConfigXmlConverter.h" |
| 30 | #include "core-impl/ChildInterface.h" |
Lorena Torres-Huerta | bc585bd | 2022-10-23 20:41:35 +0000 | [diff] [blame] | 31 | #include "core-impl/Config.h" |
| 32 | #include "core-impl/Module.h" |
| 33 | |
Lorena Torres-Huerta | 394e252 | 2022-12-20 02:21:41 +0000 | [diff] [blame] | 34 | using aidl::android::hardware::audio::core::ChildInterface; |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 35 | using aidl::android::hardware::audio::core::Config; |
| 36 | using aidl::android::hardware::audio::core::Module; |
Lorena Torres-Huerta | 394e252 | 2022-12-20 02:21:41 +0000 | [diff] [blame] | 37 | using aidl::android::hardware::audio::core::internal::AudioPolicyConfigXmlConverter; |
| 38 | |
| 39 | namespace { |
| 40 | |
| 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 |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 64 | |
| 65 | int main() { |
Mikhail Naganov | 4f5d3f1 | 2022-07-22 23:23:25 +0000 | [diff] [blame] | 66 | // Random values are used in the implementation. |
| 67 | std::srand(std::time(nullptr)); |
| 68 | |
Mikhail Naganov | 16db9b7 | 2022-06-17 21:36:18 +0000 | [diff] [blame] | 69 | // This is a debug implementation, always enable debug logging. |
| 70 | android::base::SetMinimumLogSeverity(::android::base::DEBUG); |
Mikhail Naganov | 1f72fd4 | 2023-02-02 15:26:04 -0800 | [diff] [blame] | 71 | // For more logs, use VERBOSE, however this may hinder performance. |
| 72 | // android::base::SetMinimumLogSeverity(::android::base::VERBOSE); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 73 | ABinderProcess_setThreadPoolMaxThreadCount(16); |
| 74 | |
Steven Moreland | 705c39b | 2023-03-06 14:30:15 +0000 | [diff] [blame] | 75 | // Guaranteed log for b/210919187 and logd_integration_test |
| 76 | LOG(INFO) << "Init for Audio AIDL HAL"; |
| 77 | |
Lorena Torres-Huerta | 394e252 | 2022-12-20 02:21:41 +0000 | [diff] [blame] | 78 | AudioPolicyConfigXmlConverter audioPolicyConverter{ |
| 79 | ::android::audio_get_audio_policy_config_file()}; |
| 80 | |
Mikhail Naganov | 16db9b7 | 2022-06-17 21:36:18 +0000 | [diff] [blame] | 81 | // Make the default config service |
Lorena Torres-Huerta | 394e252 | 2022-12-20 02:21:41 +0000 | [diff] [blame] | 82 | auto config = ndk::SharedRefBase::make<Config>(audioPolicyConverter); |
| 83 | const std::string configFqn = std::string().append(Config::descriptor).append("/default"); |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 84 | binder_status_t status = |
Lorena Torres-Huerta | 394e252 | 2022-12-20 02:21:41 +0000 | [diff] [blame] | 85 | AServiceManager_addService(config->asBinder().get(), configFqn.c_str()); |
| 86 | if (status != STATUS_OK) { |
| 87 | LOG(ERROR) << "failed to register service for \"" << configFqn << "\""; |
| 88 | } |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 89 | |
Mikhail Naganov | c8e4312 | 2022-12-09 00:33:47 +0000 | [diff] [blame] | 90 | // Make modules |
Lorena Torres-Huerta | 394e252 | 2022-12-20 02:21:41 +0000 | [diff] [blame] | 91 | 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 Naganov | c8e4312 | 2022-12-09 00:33:47 +0000 | [diff] [blame] | 99 | |
Mikhail Naganov | df5adfd | 2021-11-11 22:09:22 +0000 | [diff] [blame] | 100 | ABinderProcess_joinThreadPool(); |
| 101 | return EXIT_FAILURE; // should not reach |
| 102 | } |