blob: 1c0eacb88b292b745d974eada37317b66c572da6 [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;
34using ::android::hardware::audio::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 {
Kevin Rocard070e7512018-05-22 09:29:13 -070041namespace CPP_VERSION {
Mikhail Naganovf558e022016-11-14 17:45:17 -080042
Mikhail Naganov88de22f2020-03-06 10:55:34 -080043class ServiceNotificationListener : public IServiceNotification {
44 public:
45 explicit ServiceNotificationListener(sp<DevicesFactoryHalHidl> factory)
46 : mFactory(factory) {}
Kevin Rocarda8f13932019-06-25 16:08:29 -070047
Mikhail Naganov88de22f2020-03-06 10:55:34 -080048 Return<void> onRegistration(const hidl_string& /*fully_qualified_name*/,
49 const hidl_string& instance_name,
50 bool /*pre_existing*/) override {
51 if (static_cast<std::string>(instance_name) == "default") return Void();
52 sp<DevicesFactoryHalHidl> factory = mFactory.promote();
53 if (!factory) return Void();
54 sp<IDevicesFactory> halFactory = IDevicesFactory::getService(instance_name);
55 if (halFactory) {
56 factory->addDeviceFactory(halFactory, true /*needToNotify*/);
Kevin Rocard070e7512018-05-22 09:29:13 -070057 }
Mikhail Naganov88de22f2020-03-06 10:55:34 -080058 return Void();
Kevin Rocard070e7512018-05-22 09:29:13 -070059 }
Mikhail Naganov88de22f2020-03-06 10:55:34 -080060
61 private:
62 wp<DevicesFactoryHalHidl> mFactory;
63};
64
65DevicesFactoryHalHidl::DevicesFactoryHalHidl(sp<IDevicesFactory> devicesFactory) {
66 ALOG_ASSERT(devicesFactory != nullptr, "Provided default IDevicesFactory service is NULL");
67 addDeviceFactory(devicesFactory, false /*needToNotify*/);
Mikhail Naganovf558e022016-11-14 17:45:17 -080068}
69
Mikhail Naganov88de22f2020-03-06 10:55:34 -080070void DevicesFactoryHalHidl::onFirstRef() {
71 sp<IServiceManager> sm = IServiceManager::getService();
72 ALOG_ASSERT(sm != nullptr, "Hardware service manager is not running");
73 sp<ServiceNotificationListener> listener = new ServiceNotificationListener(this);
74 Return<bool> result = sm->registerForNotifications(
75 IDevicesFactory::descriptor, "", listener);
76 if (result.isOk()) {
77 ALOGE_IF(!static_cast<bool>(result),
78 "Hardware service manager refused to register listener");
79 } else {
80 ALOGE("Failed to register for hardware service manager notifications: %s",
81 result.description().c_str());
82 }
83}
Mikhail Naganovf558e022016-11-14 17:45:17 -080084
Kevin Rocard070e7512018-05-22 09:29:13 -070085#if MAJOR_VERSION == 2
Kevin Rocarddf9b4202018-05-10 19:56:08 -070086static IDevicesFactory::Device idFromHal(const char *name, status_t* status) {
87 *status = OK;
Mikhail Naganovf558e022016-11-14 17:45:17 -080088 if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070089 return IDevicesFactory::Device::PRIMARY;
Mikhail Naganovf558e022016-11-14 17:45:17 -080090 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070091 return IDevicesFactory::Device::A2DP;
Mikhail Naganovf558e022016-11-14 17:45:17 -080092 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070093 return IDevicesFactory::Device::USB;
Mikhail Naganovf558e022016-11-14 17:45:17 -080094 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070095 return IDevicesFactory::Device::R_SUBMIX;
Eric Laurentcb9d2eb2017-01-18 17:00:39 -080096 } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_STUB) == 0) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -070097 return IDevicesFactory::Device::STUB;
Mikhail Naganovf558e022016-11-14 17:45:17 -080098 }
99 ALOGE("Invalid device name %s", name);
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700100 *status = BAD_VALUE;
101 return {};
Mikhail Naganovf558e022016-11-14 17:45:17 -0800102}
Kevin Rocard3d48dce2018-11-08 17:16:57 -0800103#elif MAJOR_VERSION >= 4
Kevin Rocard070e7512018-05-22 09:29:13 -0700104static const char* idFromHal(const char *name, status_t* status) {
105 *status = OK;
106 return name;
107}
108#endif
Mikhail Naganovf558e022016-11-14 17:45:17 -0800109
110status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800111 auto factories = copyDeviceFactories();
112 if (factories.empty()) return NO_INIT;
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700113 status_t status;
114 auto hidlId = idFromHal(name, &status);
Mikhail Naganovf558e022016-11-14 17:45:17 -0800115 if (status != OK) return status;
116 Result retval = Result::NOT_INITIALIZED;
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800117 for (const auto& factory : factories) {
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700118 Return<void> ret = factory->openDevice(
119 hidlId,
120 [&](Result r, const sp<IDevice>& result) {
121 retval = r;
122 if (retval == Result::OK) {
123 *device = new DeviceHalHidl(result);
124 }
125 });
126 if (!ret.isOk()) return FAILED_TRANSACTION;
127 switch (retval) {
128 // Device was found and was initialized successfully.
129 case Result::OK: return OK;
130 // Device was found but failed to initalize.
131 case Result::NOT_INITIALIZED: return NO_INIT;
132 // Otherwise continue iterating.
133 default: ;
134 }
Mikhail Naganovf558e022016-11-14 17:45:17 -0800135 }
Kevin Rocarddf9b4202018-05-10 19:56:08 -0700136 ALOGW("The specified device name is not recognized: \"%s\"", name);
137 return BAD_VALUE;
Mikhail Naganovf558e022016-11-14 17:45:17 -0800138}
139
Eric Laurentb680e952019-09-27 15:40:33 -0700140status_t DevicesFactoryHalHidl::getHalPids(std::vector<pid_t> *pids) {
141 std::set<pid_t> pidsSet;
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800142 auto factories = copyDeviceFactories();
143 for (const auto& factory : factories) {
Eric Laurentb680e952019-09-27 15:40:33 -0700144 using ::android::hidl::base::V1_0::DebugInfo;
Eric Laurentb680e952019-09-27 15:40:33 -0700145
146 DebugInfo debugInfo;
147 auto ret = factory->getDebugInfo([&] (const auto &info) {
148 debugInfo = info;
149 });
150 if (!ret.isOk()) {
151 return INVALID_OPERATION;
152 }
153 if (debugInfo.pid == (int)IServiceManager::PidConstant::NO_PID) {
154 continue;
155 }
156 pidsSet.insert(debugInfo.pid);
157 }
158
159 *pids = {pidsSet.begin(), pidsSet.end()};
160 return NO_ERROR;
161}
162
Mikhail Naganov88de22f2020-03-06 10:55:34 -0800163status_t DevicesFactoryHalHidl::setCallbackOnce(sp<DevicesFactoryHalCallback> callback) {
164 ALOG_ASSERT(callback != nullptr);
165 bool needToCallCallback = false;
166 {
167 std::lock_guard<std::mutex> lock(mLock);
168 if (mCallback.unsafe_get()) return INVALID_OPERATION;
169 mCallback = callback;
170 if (mHaveUndeliveredNotifications) {
171 needToCallCallback = true;
172 mHaveUndeliveredNotifications = false;
173 }
174 }
175 if (needToCallCallback) {
176 callback->onNewDevicesAvailable();
177 }
178 return NO_ERROR;
179}
180
181void DevicesFactoryHalHidl::addDeviceFactory(sp<IDevicesFactory> factory, bool needToNotify) {
182 // It is assumed that the DevicesFactoryHalInterface instance is owned
183 // by AudioFlinger and thus have the same lifespan.
184 factory->linkToDeath(HalDeathHandler::getInstance(), 0 /*cookie*/);
185 sp<DevicesFactoryHalCallback> callback;
186 {
187 std::lock_guard<std::mutex> lock(mLock);
188 mDeviceFactories.push_back(factory);
189 if (needToNotify) {
190 callback = mCallback.promote();
191 if (!callback) {
192 mHaveUndeliveredNotifications = true;
193 }
194 }
195 }
196 if (callback) {
197 callback->onNewDevicesAvailable();
198 }
199}
200
201std::vector<sp<IDevicesFactory>> DevicesFactoryHalHidl::copyDeviceFactories() {
202 std::lock_guard<std::mutex> lock(mLock);
203 return mDeviceFactories;
204}
205
Kevin Rocard070e7512018-05-22 09:29:13 -0700206} // namespace CPP_VERSION
Mikhail Naganovf558e022016-11-14 17:45:17 -0800207} // namespace android