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 | |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 38 | // Opens a device with the specified name. To close the device, it is |
| 39 | // necessary to release references to the returned object. |
| 40 | status_t DevicesFactoryHalAidl::openDevice(const char *name, sp<DeviceHalInterface> *device) { |
| 41 | if (name == nullptr || device == nullptr) { |
| 42 | return BAD_VALUE; |
| 43 | } |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 44 | |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 45 | 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 Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 54 | ndk::SpAIBinder(AServiceManager_waitForService(serviceName.c_str()))); |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 55 | ALOGE_IF(service == nullptr, "%s fromBinder %s failed", __func__, serviceName.c_str()); |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 56 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 57 | // 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 Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 60 | return OK; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | status_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 | |
| 71 | status_t DevicesFactoryHalAidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) { |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 72 | // 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 Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 78 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 79 | return NO_ERROR; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | AudioHalVersionInfo DevicesFactoryHalAidl::getHalVersion() const { |
| 83 | int32_t versionNumber = 0; |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 84 | 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 Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 88 | } |
Mikhail Naganov | 31d4665 | 2023-01-10 18:29:25 +0000 | [diff] [blame] | 89 | } else { |
| 90 | ALOGW("%s no IConfig instance", __func__); |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 91 | } |
| 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. |
| 97 | extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() { |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 98 | auto serviceName = std::string(IConfig::descriptor) + "/default"; |
Shunkai Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 99 | auto service = IConfig::fromBinder( |
Shunkai Yao | 5120250 | 2022-12-12 06:11:46 +0000 | [diff] [blame] | 100 | 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 Yao | dca65ce | 2022-12-02 05:35:41 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } // namespace android |