blob: ee29f09b0d7eed812e5f4951a6fb91bfb7fecd92 [file] [log] [blame]
Shunkai Yaodca65ce2022-12-02 05:35:41 +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
17#define LOG_TAG "DevicesFactoryHalAidl"
18//#define LOG_NDEBUG 0
19
Shunkai Yao51202502022-12-12 06:11:46 +000020#include <aidl/android/hardware/audio/core/IModule.h>
Shunkai Yaodca65ce2022-12-02 05:35:41 +000021#include <android/binder_manager.h>
Shunkai Yao51202502022-12-12 06:11:46 +000022#include <memory>
Shunkai Yaodca65ce2022-12-02 05:35:41 +000023#include <utils/Log.h>
24
Shunkai Yao51202502022-12-12 06:11:46 +000025#include "DeviceHalAidl.h"
Shunkai Yaodca65ce2022-12-02 05:35:41 +000026#include "DevicesFactoryHalAidl.h"
27
Shunkai Yao51202502022-12-12 06:11:46 +000028using namespace ::aidl::android::hardware::audio::core;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000029using ::android::detail::AudioHalVersionInfo;
30
31namespace android {
32
Shunkai Yao51202502022-12-12 06:11:46 +000033DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> iconfig)
34 : mIConfig(std::move(iconfig)) {
Shunkai Yaodca65ce2022-12-02 05:35:41 +000035 ALOG_ASSERT(iconfig != nullptr, "Provided default IConfig service is NULL");
Shunkai Yaodca65ce2022-12-02 05:35:41 +000036}
37
Shunkai Yaodca65ce2022-12-02 05:35:41 +000038// Opens a device with the specified name. To close the device, it is
39// necessary to release references to the returned object.
40status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
41 if (name == nullptr || device == nullptr) {
42 return BAD_VALUE;
43 }
Shunkai Yao51202502022-12-12 06:11:46 +000044
Mikhail Naganov31d46652023-01-10 18:29:25 +000045 std::shared_ptr<IModule> service;
46 // FIXME: Normally we will list available HAL modules and connect to them,
47 // however currently we still get the list of module names from the config.
48 // Since the example service does not have all modules, the SM will wait
49 // for the missing ones forever.
50 if (strcmp(name, "primary") == 0 || strcmp(name, "r_submix") == 0) {
51 if (strcmp(name, "primary") == 0) name = "default";
52 auto serviceName = std::string(IModule::descriptor) + "/" + name;
53 service = IModule::fromBinder(
Shunkai Yao51202502022-12-12 06:11:46 +000054 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
Mikhail Naganov31d46652023-01-10 18:29:25 +000055 ALOGE_IF(service == nullptr, "%s fromBinder %s failed", __func__, serviceName.c_str());
Shunkai Yao51202502022-12-12 06:11:46 +000056 }
Mikhail Naganov31d46652023-01-10 18:29:25 +000057 // If the service is a nullptr, the device will not be really functional,
58 // but will not crash either.
59 *device = sp<DeviceHalAidl>::make(service);
Shunkai Yao51202502022-12-12 06:11:46 +000060 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000061}
62
63status_t DevicesFactoryHalAidl::getHalPids(std::vector<pid_t> *pids) {
64 if (pids == nullptr) {
65 return BAD_VALUE;
66 }
67 ALOGE("%s not implemented yet", __func__);
68 return INVALID_OPERATION;
69}
70
71status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
Mikhail Naganov31d46652023-01-10 18:29:25 +000072 // Dynamic registration of module instances is not supported. The functionality
73 // in the audio server which is related to this callback can be removed together
74 // with HIDL support.
75 ALOG_ASSERT(callback != nullptr);
76 if (callback != nullptr) {
77 callback->onNewDevicesAvailable();
Shunkai Yaodca65ce2022-12-02 05:35:41 +000078 }
Mikhail Naganov31d46652023-01-10 18:29:25 +000079 return NO_ERROR;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000080}
81
82AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const {
83 int32_t versionNumber = 0;
Mikhail Naganov31d46652023-01-10 18:29:25 +000084 if (mIConfig != 0) {
85 if (ndk::ScopedAStatus status = mIConfig->getInterfaceVersion(&versionNumber);
86 !status.isOk()) {
87 ALOGE("%s getInterfaceVersion failed: %s", __func__, status.getDescription().c_str());
Shunkai Yaodca65ce2022-12-02 05:35:41 +000088 }
Mikhail Naganov31d46652023-01-10 18:29:25 +000089 } else {
90 ALOGW("%s no IConfig instance", __func__);
Shunkai Yaodca65ce2022-12-02 05:35:41 +000091 }
92 // AIDL does not have minor version, fill 0 for all versions
93 return AudioHalVersionInfo(AudioHalVersionInfo::Type::AIDL, versionNumber);
94}
95
96// Main entry-point to the shared library.
97extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() {
Shunkai Yao51202502022-12-12 06:11:46 +000098 auto serviceName = std::string(IConfig::descriptor) + "/default";
Shunkai Yaodca65ce2022-12-02 05:35:41 +000099 auto service = IConfig::fromBinder(
Shunkai Yao51202502022-12-12 06:11:46 +0000100 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
101 if (!service) {
102 ALOGE("%s binder service %s not exist", __func__, serviceName.c_str());
103 return nullptr;
104 }
105 return new DevicesFactoryHalAidl(service);
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000106}
107
108} // namespace android