blob: 8f3c907bdef60fd5849fc7d7cc1f9224871086f8 [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
Mikhail Naganov9f57e3c2016-12-05 12:54:36 -080029#include "ConversionHelperHidl.h"
Mikhail Naganovf558e022016-11-14 17:45:17 -080030#include "DeviceHalHidl.h"
31#include "DevicesFactoryHalHidl.h"
32
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
109status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800110 auto factories = copyDeviceFactories();
111 if (factories.empty()) return NO_INIT;
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700112 status_t status;
113 auto hidlId = idFromHal(name, &status);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800114 if (status != OK) return status;
115 Result retval = Result::NOT_INITIALIZED;
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800116 for (const auto& factory : factories) {
Mikhail Naganov6718c392022-01-27 22:17:21 +0000117 Return<void> ret;
118 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
119 // In V7.1 it's not possible to cast IDevice back to IPrimaryDevice,
120 // thus openPrimaryDevice must be used.
121#if MAJOR_VERSION == 7 && MINOR_VERSION == 1
122 ret = factory->openPrimaryDevice_7_1(
123#else
124 ret = factory->openPrimaryDevice(
125#endif
126 [&](Result r,
127 const sp<::android::hardware::audio::CPP_VERSION::IPrimaryDevice>& result) {
128 retval = r;
129 if (retval == Result::OK) {
130 *device = new DeviceHalHidl(result);
131 }
132 });
133 } else {
134#if MAJOR_VERSION == 7 && MINOR_VERSION == 1
135 ret = factory->openDevice_7_1(
136#else
137 ret = factory->openDevice(
138#endif
139 hidlId,
140 [&](Result r,
141 const sp<::android::hardware::audio::CPP_VERSION::IDevice>& result) {
142 retval = r;
143 if (retval == Result::OK) {
144 *device = new DeviceHalHidl(result);
145 }
146 });
147 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700148 if (!ret.isOk()) return FAILED_TRANSACTION;
149 switch (retval) {
150 // Device was found and was initialized successfully.
151 case Result::OK: return OK;
152 // Device was found but failed to initalize.
153 case Result::NOT_INITIALIZED: return NO_INIT;
154 // Otherwise continue iterating.
155 default: ;
156 }
Mikhail Naganovf558e022016-11-14 17:45:17 -0800157 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700158 ALOGW("The specified device name is not recognized: \"%s\"", name);
159 return BAD_VALUE;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800160}
161
Eric Laurentb680e952019-09-27 15:40:33 -0700162status_t DevicesFactoryHalHidl::getHalPids(std::vector<pid_t> *pids) {
163 std::set<pid_t> pidsSet;
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800164 auto factories = copyDeviceFactories();
165 for (const auto& factory : factories) {
Eric Laurentb680e952019-09-27 15:40:33 -0700166 using ::android::hidl::base::V1_0::DebugInfo;
Eric Laurentb680e952019-09-27 15:40:33 -0700167
168 DebugInfo debugInfo;
169 auto ret = factory->getDebugInfo([&] (const auto &info) {
170 debugInfo = info;
171 });
172 if (!ret.isOk()) {
173 return INVALID_OPERATION;
174 }
175 if (debugInfo.pid == (int)IServiceManager::PidConstant::NO_PID) {
176 continue;
177 }
178 pidsSet.insert(debugInfo.pid);
179 }
180
181 *pids = {pidsSet.begin(), pidsSet.end()};
182 return NO_ERROR;
183}
184
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800185status_t DevicesFactoryHalHidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
186 ALOG_ASSERT(callback != nullptr);
187 bool needToCallCallback = false;
188 {
189 std::lock_guard<std::mutex> lock(mLock);
190 if (mCallback.unsafe_get()) return INVALID_OPERATION;
191 mCallback = callback;
192 if (mHaveUndeliveredNotifications) {
193 needToCallCallback = true;
194 mHaveUndeliveredNotifications = false;
195 }
196 }
197 if (needToCallCallback) {
198 callback->onNewDevicesAvailable();
199 }
200 return NO_ERROR;
201}
202
Mikhail Naganov6718c392022-01-27 22:17:21 +0000203void DevicesFactoryHalHidl::addDeviceFactory(
204 sp<::android::hardware::audio::CPP_VERSION::IDevicesFactory> factory, bool needToNotify) {
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800205 // It is assumed that the DevicesFactoryHalInterface instance is owned
206 // by AudioFlinger and thus have the same lifespan.
207 factory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
208 sp<DevicesFactoryHalCallback> callback;
209 {
210 std::lock_guard<std::mutex> lock(mLock);
211 mDeviceFactories.push_back(factory);
212 if (needToNotify) {
213 callback = mCallback.promote();
214 if (!callback) {
215 mHaveUndeliveredNotifications = true;
216 }
217 }
218 }
219 if (callback) {
220 callback->onNewDevicesAvailable();
221 }
222}
223
Mikhail Naganov6718c392022-01-27 22:17:21 +0000224std::vector<sp<::android::hardware::audio::CPP_VERSION::IDevicesFactory>>
225 DevicesFactoryHalHidl::copyDeviceFactories() {
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800226 std::lock_guard<std::mutex> lock(mLock);
227 return mDeviceFactories;
228}
229
Ytai Ben-Tsvi50e93462021-12-30 12:00:04 -0800230// Main entry-point to the shared library.
231extern "C" __attribute__((visibility("default"))) void* createIDevicesFactory() {
232 auto service = hardware::audio::CPP_VERSION::IDevicesFactory::getService();
233 return service ? new DevicesFactoryHalHidl(service) : nullptr;
234}
235
Mikhail Naganovf558e022016-11-14 17:45:17 -0800236} // namespace android