blob: eef60b506c9f1e5cf59176c5acf3d67ef82919b7 [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
Eric Laurentb680e952019-09-27 15:40:33 -0700166status_t DevicesFactoryHalHidl::getHalPids(std::vector<pid_t> *pids) {
167 std::set<pid_t> pidsSet;
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800168 auto factories = copyDeviceFactories();
169 for (const auto& factory : factories) {
Eric Laurentb680e952019-09-27 15:40:33 -0700170 using ::android::hidl::base::V1_0::DebugInfo;
Eric Laurentb680e952019-09-27 15:40:33 -0700171
172 DebugInfo debugInfo;
173 auto ret = factory->getDebugInfo([&] (const auto &info) {
174 debugInfo = info;
175 });
176 if (!ret.isOk()) {
177 return INVALID_OPERATION;
178 }
179 if (debugInfo.pid == (int)IServiceManager::PidConstant::NO_PID) {
180 continue;
181 }
182 pidsSet.insert(debugInfo.pid);
183 }
184
185 *pids = {pidsSet.begin(), pidsSet.end()};
186 return NO_ERROR;
187}
188
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800189status_t DevicesFactoryHalHidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
190 ALOG_ASSERT(callback != nullptr);
191 bool needToCallCallback = false;
192 {
193 std::lock_guard<std::mutex> lock(mLock);
194 if (mCallback.unsafe_get()) return INVALID_OPERATION;
195 mCallback = callback;
196 if (mHaveUndeliveredNotifications) {
197 needToCallCallback = true;
198 mHaveUndeliveredNotifications = false;
199 }
200 }
201 if (needToCallCallback) {
202 callback->onNewDevicesAvailable();
203 }
204 return NO_ERROR;
205}
206
Mikhail Naganov6718c392022-01-27 22:17:21 +0000207void DevicesFactoryHalHidl::addDeviceFactory(
208 sp<::android::hardware::audio::CPP_VERSION::IDevicesFactory> factory, bool needToNotify) {
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800209 // It is assumed that the DevicesFactoryHalInterface instance is owned
210 // by AudioFlinger and thus have the same lifespan.
211 factory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
212 sp<DevicesFactoryHalCallback> callback;
213 {
214 std::lock_guard<std::mutex> lock(mLock);
215 mDeviceFactories.push_back(factory);
216 if (needToNotify) {
217 callback = mCallback.promote();
218 if (!callback) {
219 mHaveUndeliveredNotifications = true;
220 }
221 }
222 }
223 if (callback) {
224 callback->onNewDevicesAvailable();
225 }
226}
227
Mikhail Naganov6718c392022-01-27 22:17:21 +0000228std::vector<sp<::android::hardware::audio::CPP_VERSION::IDevicesFactory>>
229 DevicesFactoryHalHidl::copyDeviceFactories() {
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800230 std::lock_guard<std::mutex> lock(mLock);
231 return mDeviceFactories;
232}
233
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000234
235AudioHalVersionInfo DevicesFactoryHalHidl::getHalVersion() const {
236 return AudioHalVersionInfo(AudioHalVersionInfo::Type::HIDL, MAJOR_VERSION, MINOR_VERSION);
237}
238
Mikhail Naganovf83b9742023-04-24 13:06:04 -0700239status_t DevicesFactoryHalHidl::getSurroundSoundConfig(
240 media::SurroundSoundConfig *config __unused) {
241 return INVALID_OPERATION;
242}
243
244status_t DevicesFactoryHalHidl::getEngineConfig(
245 media::audio::common::AudioHalEngineConfig *config __unused) {
246 return INVALID_OPERATION;
247}
248
Ytai Ben-Tsvi50e93462021-12-30 12:00:04 -0800249// Main entry-point to the shared library.
Shunkai Yaodca65ce2022-12-02 05:35:41 +0000250extern "C" __attribute__((visibility("default"))) void* createIDevicesFactoryImpl() {
Ytai Ben-Tsvi50e93462021-12-30 12:00:04 -0800251 auto service = hardware::audio::CPP_VERSION::IDevicesFactory::getService();
252 return service ? new DevicesFactoryHalHidl(service) : nullptr;
253}
254
Mikhail Naganovf558e022016-11-14 17:45:17 -0800255} // namespace android