blob: 4069a6bdbdfec3b72b605a269ddde65e33e54b36 [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
Kevin Rocard070e7512018-05-22 09:29:13 -070032using ::android::hardware::audio::CPP_VERSION::IDevice;
Mikhail Naganov6718c392022-01-27 22:17:21 +000033using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::Result;
Mikhail Naganovf558e022016-11-14 17:45:17 -080034using ::android::hardware::Return;
Mikhail Naganov88de22f2020-03-06 10:55:34 -080035using ::android::hardware::Void;
36using ::android::hidl::manager::V1_0::IServiceManager;
37using ::android::hidl::manager::V1_0::IServiceNotification;
Mikhail Naganovf558e022016-11-14 17:45:17 -080038
39namespace android {
40
Mikhail Naganov88de22f2020-03-06 10:55:34 -080041class ServiceNotificationListener : public IServiceNotification {
42 public:
43 explicit ServiceNotificationListener(sp<DevicesFactoryHalHidl> factory)
44 : mFactory(factory) {}
Kevin Rocarda8f13932019-06-25 16:08:29 -070045
Mikhail Naganov88de22f2020-03-06 10:55:34 -080046 Return<void> onRegistration(const hidl_string& /*fully_qualified_name*/,
47 const hidl_string& instance_name,
48 bool /*pre_existing*/) override {
49 if (static_cast<std::string>(instance_name) == "default") return Void();
50 sp<DevicesFactoryHalHidl> factory = mFactory.promote();
51 if (!factory) return Void();
52 sp<IDevicesFactory> halFactory = IDevicesFactory::getService(instance_name);
53 if (halFactory) {
54 factory->addDeviceFactory(halFactory, true /*needToNotify*/);
Kevin Rocard070e7512018-05-22 09:29:13 -070055 }
Mikhail Naganov88de22f2020-03-06 10:55:34 -080056 return Void();
Kevin Rocard070e7512018-05-22 09:29:13 -070057 }
Mikhail Naganov88de22f2020-03-06 10:55:34 -080058
59 private:
60 wp<DevicesFactoryHalHidl> mFactory;
61};
62
63DevicesFactoryHalHidl::DevicesFactoryHalHidl(sp<IDevicesFactory> devicesFactory) {
64 ALOG_ASSERT(devicesFactory != nullptr, "Provided default IDevicesFactory service is NULL");
65 addDeviceFactory(devicesFactory, false /*needToNotify*/);
Mikhail Naganovf558e022016-11-14 17:45:17 -080066}
67
Mikhail Naganov88de22f2020-03-06 10:55:34 -080068void DevicesFactoryHalHidl::onFirstRef() {
69 sp<IServiceManager> sm = IServiceManager::getService();
70 ALOG_ASSERT(sm != nullptr, "Hardware service manager is not running");
71 sp<ServiceNotificationListener> listener = new ServiceNotificationListener(this);
72 Return<bool> result = sm->registerForNotifications(
73 IDevicesFactory::descriptor, "", listener);
74 if (result.isOk()) {
75 ALOGE_IF(!static_cast<bool>(result),
76 "Hardware service manager refused to register listener");
77 } else {
78 ALOGE("Failed to register for hardware service manager notifications: %s",
79 result.description().c_str());
80 }
81}
Mikhail Naganovf558e022016-11-14 17:45:17 -080082
Kevin Rocard070e7512018-05-22 09:29:13 -070083#if MAJOR_VERSION == 2
Kevin Rocarddf9b4202018-05-10 19:56:08 -070084static IDevicesFactory::Device idFromHal(const char *name, status_t* status) {
85 *status = OK;
Mikhail Naganovf558e022016-11-14 17:45:17 -080086 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070087 return IDevicesFactory::Device::PRIMARY;
Mikhail Naganovf558e022016-11-14 17:45:17 -080088 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070089 return IDevicesFactory::Device::A2DP;
Mikhail Naganovf558e022016-11-14 17:45:17 -080090 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070091 return IDevicesFactory::Device::USB;
Mikhail Naganovf558e022016-11-14 17:45:17 -080092 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070093 return IDevicesFactory::Device::R_SUBMIX;
Eric Laurentcb9d2eb2017-01-18 17:00:39 -080094 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_STUB) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070095 return IDevicesFactory::Device::STUB;
Mikhail Naganovf558e022016-11-14 17:45:17 -080096 }
97 ALOGE("Invalid device name %s", name);
Kevin Rocarddf9b4202018-05-10 19:56:08 -070098 *status = BAD_VALUE;
99 return {};
Mikhail Naganovf558e022016-11-14 17:45:17 -0800100}
Kevin Rocard3d48dce2018-11-08 17:16:57 -0800101#elif MAJOR_VERSION >= 4
Kevin Rocard070e7512018-05-22 09:29:13 -0700102static const char* idFromHal(const char *name, status_t* status) {
103 *status = OK;
104 return name;
105}
106#endif
Mikhail Naganovf558e022016-11-14 17:45:17 -0800107
108status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800109 auto factories = copyDeviceFactories();
110 if (factories.empty()) return NO_INIT;
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700111 status_t status;
112 auto hidlId = idFromHal(name, &status);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800113 if (status != OK) return status;
114 Result retval = Result::NOT_INITIALIZED;
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800115 for (const auto& factory : factories) {
Mikhail Naganov6718c392022-01-27 22:17:21 +0000116 Return<void> ret;
117 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
118 // In V7.1 it's not possible to cast IDevice back to IPrimaryDevice,
119 // thus openPrimaryDevice must be used.
120#if MAJOR_VERSION == 7 && MINOR_VERSION == 1
121 ret = factory->openPrimaryDevice_7_1(
122#else
123 ret = factory->openPrimaryDevice(
124#endif
125 [&](Result r,
126 const sp<::android::hardware::audio::CPP_VERSION::IPrimaryDevice>& result) {
127 retval = r;
128 if (retval == Result::OK) {
129 *device = new DeviceHalHidl(result);
130 }
131 });
132 } else {
133#if MAJOR_VERSION == 7 && MINOR_VERSION == 1
134 ret = factory->openDevice_7_1(
135#else
136 ret = factory->openDevice(
137#endif
138 hidlId,
139 [&](Result r,
140 const sp<::android::hardware::audio::CPP_VERSION::IDevice>& result) {
141 retval = r;
142 if (retval == Result::OK) {
143 *device = new DeviceHalHidl(result);
144 }
145 });
146 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700147 if (!ret.isOk()) return FAILED_TRANSACTION;
148 switch (retval) {
149 // Device was found and was initialized successfully.
150 case Result::OK: return OK;
151 // Device was found but failed to initalize.
152 case Result::NOT_INITIALIZED: return NO_INIT;
153 // Otherwise continue iterating.
154 default: ;
155 }
Mikhail Naganovf558e022016-11-14 17:45:17 -0800156 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700157 ALOGW("The specified device name is not recognized: \"%s\"", name);
158 return BAD_VALUE;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800159}
160
Eric Laurentb680e952019-09-27 15:40:33 -0700161status_t DevicesFactoryHalHidl::getHalPids(std::vector<pid_t> *pids) {
162 std::set<pid_t> pidsSet;
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800163 auto factories = copyDeviceFactories();
164 for (const auto& factory : factories) {
Eric Laurentb680e952019-09-27 15:40:33 -0700165 using ::android::hidl::base::V1_0::DebugInfo;
Eric Laurentb680e952019-09-27 15:40:33 -0700166
167 DebugInfo debugInfo;
168 auto ret = factory->getDebugInfo([&] (const auto &info) {
169 debugInfo = info;
170 });
171 if (!ret.isOk()) {
172 return INVALID_OPERATION;
173 }
174 if (debugInfo.pid == (int)IServiceManager::PidConstant::NO_PID) {
175 continue;
176 }
177 pidsSet.insert(debugInfo.pid);
178 }
179
180 *pids = {pidsSet.begin(), pidsSet.end()};
181 return NO_ERROR;
182}
183
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800184status_t DevicesFactoryHalHidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
185 ALOG_ASSERT(callback != nullptr);
186 bool needToCallCallback = false;
187 {
188 std::lock_guard<std::mutex> lock(mLock);
189 if (mCallback.unsafe_get()) return INVALID_OPERATION;
190 mCallback = callback;
191 if (mHaveUndeliveredNotifications) {
192 needToCallCallback = true;
193 mHaveUndeliveredNotifications = false;
194 }
195 }
196 if (needToCallCallback) {
197 callback->onNewDevicesAvailable();
198 }
199 return NO_ERROR;
200}
201
Mikhail Naganov6718c392022-01-27 22:17:21 +0000202void DevicesFactoryHalHidl::addDeviceFactory(
203 sp<::android::hardware::audio::CPP_VERSION::IDevicesFactory> factory, bool needToNotify) {
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800204 // It is assumed that the DevicesFactoryHalInterface instance is owned
205 // by AudioFlinger and thus have the same lifespan.
206 factory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
207 sp<DevicesFactoryHalCallback> callback;
208 {
209 std::lock_guard<std::mutex> lock(mLock);
210 mDeviceFactories.push_back(factory);
211 if (needToNotify) {
212 callback = mCallback.promote();
213 if (!callback) {
214 mHaveUndeliveredNotifications = true;
215 }
216 }
217 }
218 if (callback) {
219 callback->onNewDevicesAvailable();
220 }
221}
222
Mikhail Naganov6718c392022-01-27 22:17:21 +0000223std::vector<sp<::android::hardware::audio::CPP_VERSION::IDevicesFactory>>
224 DevicesFactoryHalHidl::copyDeviceFactories() {
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800225 std::lock_guard<std::mutex> lock(mLock);
226 return mDeviceFactories;
227}
228
Ytai Ben-Tsvi50e93462021-12-30 12:00:04 -0800229// Main entry-point to the shared library.
230extern "C" __attribute__((visibility("default"))) void* createIDevicesFactory() {
231 auto service = hardware::audio::CPP_VERSION::IDevicesFactory::getService();
232 return service ? new DevicesFactoryHalHidl(service) : nullptr;
233}
234
Mikhail Naganovf558e022016-11-14 17:45:17 -0800235} // namespace android