blob: b9ca164875b5368ff40b76c3a8c9c023d41ba72a [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
38void DevicesFactoryHalAidl::onFirstRef() {
39 ALOGE("%s not implemented yet", __func__);
40}
41
42// Opens a device with the specified name. To close the device, it is
43// necessary to release references to the returned object.
44status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
45 if (name == nullptr || device == nullptr) {
46 return BAD_VALUE;
47 }
Shunkai Yao51202502022-12-12 06:11:46 +000048 ALOGE("%s not implemented yet %s", __func__, name);
Shunkai Yaodca65ce2022-12-02 05:35:41 +000049 return INVALID_OPERATION;
Shunkai Yao51202502022-12-12 06:11:46 +000050
51 // TODO: only support primary now ("default" means "primary")
52 if (strcmp(name, "primary") != 0) {
53 auto serviceName = std::string() + IModule::descriptor + "/default";
54 auto service = IModule::fromBinder(
55 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
56 ALOGW("%s fromBinder %s %s", __func__, IModule::descriptor, service ? "succ" : "fail");
57 *device = new DeviceHalAidl(service);
58 }
59 return OK;
Shunkai Yaodca65ce2022-12-02 05:35:41 +000060}
61
62status_t DevicesFactoryHalAidl::getHalPids(std::vector<pid_t> *pids) {
63 if (pids == nullptr) {
64 return BAD_VALUE;
65 }
66 ALOGE("%s not implemented yet", __func__);
67 return INVALID_OPERATION;
68}
69
70status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
71 if (callback == nullptr) {
72 return BAD_VALUE;
73 }
74 ALOGE("%s not implemented yet", __func__);
75 return INVALID_OPERATION;
76}
77
78AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const {
79 int32_t versionNumber = 0;
80 if (mIConfig) {
81 if (!mIConfig->getInterfaceVersion(&versionNumber).isOk()) {
82 ALOGE("%s getInterfaceVersion failed", __func__);
83 } else {
84 ALOGI("%s getInterfaceVersion %d", __func__, versionNumber);
85 }
86 }
87 // AIDL does not have minor version, fill 0 for all versions
88 return AudioHalVersionInfo(AudioHalVersionInfo::Type::AIDL, versionNumber);
89}
90
91// Main entry-point to the shared library.
92extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() {
Shunkai Yao51202502022-12-12 06:11:46 +000093 auto serviceName = std::string(IConfig::descriptor) + "/default";
Shunkai Yaodca65ce2022-12-02 05:35:41 +000094 auto service = IConfig::fromBinder(
Shunkai Yao51202502022-12-12 06:11:46 +000095 ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str())));
96 if (!service) {
97 ALOGE("%s binder service %s not exist", __func__, serviceName.c_str());
98 return nullptr;
99 }
100 return new DevicesFactoryHalAidl(service);
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000101}
102
103} // namespace android