blob: 1cac9dab7628ac5dd42694987714090dc57ee72e [file] [log] [blame]
Mikhail Naganovf558e022016-11-14 17:45:17 -08001/*
2 * Copyright (C) 2016 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#include <string.h>
Mikhail Naganov88de22f2020-03-06 10:55:34 -080018#include <set>
Mikhail Naganovf558e022016-11-14 17:45:17 -080019
20#define LOG_TAG "DevicesFactoryHalHidl"
21//#define LOG_NDEBUG 0
22
Mikhail Naganovd7b2ff02020-02-07 13:51:04 -080023#include <android/hidl/manager/1.0/IServiceManager.h>
Mikhail Naganov88de22f2020-03-06 10:55:34 -080024#include <android/hidl/manager/1.0/IServiceNotification.h>
Kevin Rocard95213bf2018-11-08 17:16:57 -080025#include PATH(android/hardware/audio/FILE_VERSION/IDevice.h)
Mikhail Naganovd621ac82017-01-12 17:17:45 -080026#include <media/audiohal/hidl/HalDeathHandler.h>
Mikhail Naganovf558e022016-11-14 17:45:17 -080027#include <utils/Log.h>
28
29#include "DeviceHalHidl.h"
30#include "DevicesFactoryHalHidl.h"
31
Shunkai Yaodca65ce2022-12-02 05:35:41 +000032using ::android::detail::AudioHalVersionInfo;
Kevin Rocard070e7512018-05-22 09:29:13 -070033using ::android::hardware::audio::CPP_VERSION::IDevice;
Mikhail Naganov6718c392022-01-27 22:17:21 +000034using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::Result;
Mikhail Naganovf558e022016-11-14 17:45:17 -080035using ::android::hardware::Return;
Mikhail Naganov88de22f2020-03-06 10:55:34 -080036using ::android::hardware::Void;
37using ::android::hidl::manager::V1_0::IServiceManager;
38using ::android::hidl::manager::V1_0::IServiceNotification;
Mikhail Naganovf558e022016-11-14 17:45:17 -080039
40namespace android {
41
Mikhail Naganov88de22f2020-03-06 10:55:34 -080042class ServiceNotificationListener : public IServiceNotification {
43 public:
44 explicit ServiceNotificationListener(sp<DevicesFactoryHalHidl> factory)
45 : mFactory(factory) {}
Kevin Rocarda8f13932019-06-25 16:08:29 -070046
Mikhail Naganov88de22f2020-03-06 10:55:34 -080047 Return<void> onRegistration(const hidl_string& /*fully_qualified_name*/,
48 const hidl_string& instance_name,
49 bool /*pre_existing*/) override {
50 if (static_cast<std::string>(instance_name) == "default") return Void();
51 sp<DevicesFactoryHalHidl> factory = mFactory.promote();
52 if (!factory) return Void();
53 sp<IDevicesFactory> halFactory = IDevicesFactory::getService(instance_name);
54 if (halFactory) {
55 factory->addDeviceFactory(halFactory, true /*needToNotify*/);
Kevin Rocard070e7512018-05-22 09:29:13 -070056 }
Mikhail Naganov88de22f2020-03-06 10:55:34 -080057 return Void();
Kevin Rocard070e7512018-05-22 09:29:13 -070058 }
Mikhail Naganov88de22f2020-03-06 10:55:34 -080059
60 private:
61 wp<DevicesFactoryHalHidl> mFactory;
62};
63
64DevicesFactoryHalHidl::DevicesFactoryHalHidl(sp<IDevicesFactory> devicesFactory) {
65 ALOG_ASSERT(devicesFactory != nullptr, "Provided default IDevicesFactory service is NULL");
66 addDeviceFactory(devicesFactory, false /*needToNotify*/);
Mikhail Naganovf558e022016-11-14 17:45:17 -080067}
68
Mikhail Naganov88de22f2020-03-06 10:55:34 -080069void DevicesFactoryHalHidl::onFirstRef() {
70 sp<IServiceManager> sm = IServiceManager::getService();
71 ALOG_ASSERT(sm != nullptr, "Hardware service manager is not running");
72 sp<ServiceNotificationListener> listener = new ServiceNotificationListener(this);
73 Return<bool> result = sm->registerForNotifications(
74 IDevicesFactory::descriptor, "", listener);
75 if (result.isOk()) {
76 ALOGE_IF(!static_cast<bool>(result),
77 "Hardware service manager refused to register listener");
78 } else {
79 ALOGE("Failed to register for hardware service manager notifications: %s",
80 result.description().c_str());
81 }
82}
Mikhail Naganovf558e022016-11-14 17:45:17 -080083
Kevin Rocard070e7512018-05-22 09:29:13 -070084#if MAJOR_VERSION == 2
Kevin Rocarddf9b4202018-05-10 19:56:08 -070085static IDevicesFactory::Device idFromHal(const char *name, status_t* status) {
86 *status = OK;
Mikhail Naganovf558e022016-11-14 17:45:17 -080087 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070088 return IDevicesFactory::Device::PRIMARY;
Mikhail Naganovf558e022016-11-14 17:45:17 -080089 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070090 return IDevicesFactory::Device::A2DP;
Mikhail Naganovf558e022016-11-14 17:45:17 -080091 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070092 return IDevicesFactory::Device::USB;
Mikhail Naganovf558e022016-11-14 17:45:17 -080093 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070094 return IDevicesFactory::Device::R_SUBMIX;
Eric Laurentcb9d2eb2017-01-18 17:00:39 -080095 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_STUB) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070096 return IDevicesFactory::Device::STUB;
Mikhail Naganovf558e022016-11-14 17:45:17 -080097 }
98 ALOGE("Invalid device name %s", name);
Kevin Rocarddf9b4202018-05-10 19:56:08 -070099 *status = BAD_VALUE;
100 return {};
Mikhail Naganovf558e022016-11-14 17:45:17 -0800101}
Kevin Rocard3d48dce2018-11-08 17:16:57 -0800102#elif MAJOR_VERSION >= 4
Kevin Rocard070e7512018-05-22 09:29:13 -0700103static const char* idFromHal(const char *name, status_t* status) {
104 *status = OK;
105 return name;
106}
107#endif
Mikhail Naganovf558e022016-11-14 17:45:17 -0800108
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700109status_t DevicesFactoryHalHidl::getDeviceNames(std::vector<std::string> *names __unused) {
110 return INVALID_OPERATION;
111}
112
Mikhail Naganovf558e022016-11-14 17:45:17 -0800113status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800114 auto factories = copyDeviceFactories();
115 if (factories.empty()) return NO_INIT;
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700116 status_t status;
117 auto hidlId = idFromHal(name, &status);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800118 if (status != OK) return status;
119 Result retval = Result::NOT_INITIALIZED;
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800120 for (const auto& factory : factories) {
Mikhail Naganov6718c392022-01-27 22:17:21 +0000121 Return<void> ret;
122 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
123 // In V7.1 it's not possible to cast IDevice back to IPrimaryDevice,
124 // thus openPrimaryDevice must be used.
125#if MAJOR_VERSION == 7 && MINOR_VERSION == 1
126 ret = factory->openPrimaryDevice_7_1(
127#else
128 ret = factory->openPrimaryDevice(
129#endif
130 [&](Result r,
131 const sp<::android::hardware::audio::CPP_VERSION::IPrimaryDevice>& result) {
132 retval = r;
133 if (retval == Result::OK) {
134 *device = new DeviceHalHidl(result);
135 }
136 });
137 } else {
138#if MAJOR_VERSION == 7 && MINOR_VERSION == 1
139 ret = factory->openDevice_7_1(
140#else
141 ret = factory->openDevice(
142#endif
143 hidlId,
144 [&](Result r,
145 const sp<::android::hardware::audio::CPP_VERSION::IDevice>& result) {
146 retval = r;
147 if (retval == Result::OK) {
148 *device = new DeviceHalHidl(result);
149 }
150 });
151 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700152 if (!ret.isOk()) return FAILED_TRANSACTION;
153 switch (retval) {
154 // Device was found and was initialized successfully.
155 case Result::OK: return OK;
156 // Device was found but failed to initalize.
157 case Result::NOT_INITIALIZED: return NO_INIT;
158 // Otherwise continue iterating.
159 default: ;
160 }
Mikhail Naganovf558e022016-11-14 17:45:17 -0800161 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700162 ALOGW("The specified device name is not recognized: \"%s\"", name);
163 return BAD_VALUE;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800164}
165
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800166status_t DevicesFactoryHalHidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
167 ALOG_ASSERT(callback != nullptr);
168 bool needToCallCallback = false;
169 {
170 std::lock_guard<std::mutex> lock(mLock);
171 if (mCallback.unsafe_get()) return INVALID_OPERATION;
172 mCallback = callback;
173 if (mHaveUndeliveredNotifications) {
174 needToCallCallback = true;
175 mHaveUndeliveredNotifications = false;
176 }
177 }
178 if (needToCallCallback) {
179 callback->onNewDevicesAvailable();
180 }
181 return NO_ERROR;
182}
183
Mikhail Naganov6718c392022-01-27 22:17:21 +0000184void DevicesFactoryHalHidl::addDeviceFactory(
185 sp<::android::hardware::audio::CPP_VERSION::IDevicesFactory> factory, bool needToNotify) {
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800186 // It is assumed that the DevicesFactoryHalInterface instance is owned
187 // by AudioFlinger and thus have the same lifespan.
188 factory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
189 sp<DevicesFactoryHalCallback> callback;
190 {
191 std::lock_guard<std::mutex> lock(mLock);
192 mDeviceFactories.push_back(factory);
193 if (needToNotify) {
194 callback = mCallback.promote();
195 if (!callback) {
196 mHaveUndeliveredNotifications = true;
197 }
198 }
199 }
200 if (callback) {
201 callback->onNewDevicesAvailable();
202 }
203}
204
Mikhail Naganov6718c392022-01-27 22:17:21 +0000205std::vector<sp<::android::hardware::audio::CPP_VERSION::IDevicesFactory>>
206 DevicesFactoryHalHidl::copyDeviceFactories() {
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800207 std::lock_guard<std::mutex> lock(mLock);
208 return mDeviceFactories;
209}
210
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000211
212AudioHalVersionInfo DevicesFactoryHalHidl::getHalVersion() const {
213 return AudioHalVersionInfo(AudioHalVersionInfo::Type::HIDL, MAJOR_VERSION, MINOR_VERSION);
214}
215
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700216status_t DevicesFactoryHalHidl::getSurroundSoundConfig(
217 media::SurroundSoundConfig *config __unused) {
218 return INVALID_OPERATION;
219}
220
221status_t DevicesFactoryHalHidl::getEngineConfig(
222 media::audio::common::AudioHalEngineConfig *config __unused) {
223 return INVALID_OPERATION;
224}
225
Ytai Ben-Tsvi50e93462021-12-30 12:00:04 -0800226// Main entry-point to the shared library.
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000227extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() {
Ytai Ben-Tsvi50e93462021-12-30 12:00:04 -0800228 auto service = hardware::audio::CPP_VERSION::IDevicesFactory::getService();
229 return service ? new DevicesFactoryHalHidl(service) : nullptr;
230}
231
Mikhail Naganovf558e022016-11-14 17:45:17 -0800232} // namespace android