Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +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 | |
| 17 | #define LOG_TAG "DevicesFactoryHalAidl" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 20 | #include <aidl/android/hardware/audio/core/IModule.h> |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 21 | #include <android/binder_manager.h> |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 22 | #include <memory> |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 23 | #include <utils/Log.h> |
| 24 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 25 | #include "DeviceHalAidl.h" |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 26 | #include "DevicesFactoryHalAidl.h" |
| 27 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 28 | using namespace ::aidl::android::hardware::audio::core; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 29 | using ::android::detail::AudioHalVersionInfo; |
| 30 | |
| 31 | namespace android { |
| 32 | |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 33 | DevicesFactoryHalAidl::DevicesFactoryHalAidl(std::shared_ptr<IConfig> iconfig) |
| 34 | : mIConfig(std::move(iconfig)) { |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 35 | ALOG_ASSERT(iconfig != nullptr, "Provided default IConfig service is NULL"); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void 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. |
| 44 | status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) { |
| 45 | if (name == nullptr || device == nullptr) { |
| 46 | return BAD_VALUE; |
| 47 | } |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 48 | ALOGE("%s not implemented yet %s", __func__, name); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 49 | return INVALID_OPERATION; |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 50 | |
| 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 Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | status_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 | |
| 70 | status_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 | |
| 78 | AudioHalVersionInfo 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. |
| 92 | extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 93 | auto serviceName = std::string(IConfig::descriptor) + "/default"; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 94 | auto service = IConfig::fromBinder( |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 95 | 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 Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | } // namespace android |