• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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>
18 #include <set>
19 
20 #define LOG_TAG "DevicesFactoryHalHidl"
21 //#define LOG_NDEBUG 0
22 
23 #include <android/hidl/manager/1.0/IServiceManager.h>
24 #include <android/hidl/manager/1.0/IServiceNotification.h>
25 #include PATH(android/hardware/audio/FILE_VERSION/IDevice.h)
26 #include <media/audiohal/hidl/HalDeathHandler.h>
27 #include <utils/Log.h>
28 
29 #include "DeviceHalHidl.h"
30 #include "DevicesFactoryHalHidl.h"
31 
32 using ::android::hardware::audio::CPP_VERSION::IDevice;
33 using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::Result;
34 using ::android::hardware::Return;
35 using ::android::hardware::Void;
36 using ::android::hidl::manager::V1_0::IServiceManager;
37 using ::android::hidl::manager::V1_0::IServiceNotification;
38 
39 namespace android {
40 
41 class ServiceNotificationListener : public IServiceNotification {
42   public:
ServiceNotificationListener(sp<DevicesFactoryHalHidl> factory)43     explicit ServiceNotificationListener(sp<DevicesFactoryHalHidl> factory)
44             : mFactory(factory) {}
45 
onRegistration(const hidl_string &,const hidl_string & instance_name,bool)46     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*/);
55         }
56         return Void();
57     }
58 
59   private:
60     wp<DevicesFactoryHalHidl> mFactory;
61 };
62 
DevicesFactoryHalHidl(sp<IDevicesFactory> devicesFactory)63 DevicesFactoryHalHidl::DevicesFactoryHalHidl(sp<IDevicesFactory> devicesFactory) {
64     ALOG_ASSERT(devicesFactory != nullptr, "Provided default IDevicesFactory service is NULL");
65     addDeviceFactory(devicesFactory, false /*needToNotify*/);
66 }
67 
onFirstRef()68 void 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 }
82 
83 #if MAJOR_VERSION == 2
idFromHal(const char * name,status_t * status)84 static IDevicesFactory::Device idFromHal(const char *name, status_t* status) {
85     *status = OK;
86     if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
87         return IDevicesFactory::Device::PRIMARY;
88     } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
89         return IDevicesFactory::Device::A2DP;
90     } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
91         return IDevicesFactory::Device::USB;
92     } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
93         return IDevicesFactory::Device::R_SUBMIX;
94     } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_STUB) == 0) {
95         return IDevicesFactory::Device::STUB;
96     }
97     ALOGE("Invalid device name %s", name);
98     *status = BAD_VALUE;
99     return {};
100 }
101 #elif MAJOR_VERSION >= 4
idFromHal(const char * name,status_t * status)102 static const char* idFromHal(const char *name, status_t* status) {
103     *status = OK;
104     return name;
105 }
106 #endif
107 
openDevice(const char * name,sp<DeviceHalInterface> * device)108 status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
109     auto factories = copyDeviceFactories();
110     if (factories.empty()) return NO_INIT;
111     status_t status;
112     auto hidlId = idFromHal(name, &status);
113     if (status != OK) return status;
114     Result retval = Result::NOT_INITIALIZED;
115     for (const auto& factory : factories) {
116         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         }
147         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         }
156     }
157     ALOGW("The specified device name is not recognized: \"%s\"", name);
158     return BAD_VALUE;
159 }
160 
getHalPids(std::vector<pid_t> * pids)161 status_t DevicesFactoryHalHidl::getHalPids(std::vector<pid_t> *pids) {
162     std::set<pid_t> pidsSet;
163     auto factories = copyDeviceFactories();
164     for (const auto& factory : factories) {
165         using ::android::hidl::base::V1_0::DebugInfo;
166 
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 
setCallbackOnce(sp<DevicesFactoryHalCallback> callback)184 status_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 
addDeviceFactory(sp<::android::hardware::audio::CPP_VERSION::IDevicesFactory> factory,bool needToNotify)202 void DevicesFactoryHalHidl::addDeviceFactory(
203         sp<::android::hardware::audio::CPP_VERSION::IDevicesFactory> factory, bool needToNotify) {
204     // 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 
223 std::vector<sp<::android::hardware::audio::CPP_VERSION::IDevicesFactory>>
copyDeviceFactories()224         DevicesFactoryHalHidl::copyDeviceFactories() {
225     std::lock_guard<std::mutex> lock(mLock);
226     return mDeviceFactories;
227 }
228 
229 // Main entry-point to the shared library.
createIDevicesFactory()230 extern "C" __attribute__((visibility("default"))) void* createIDevicesFactory() {
231     auto service = hardware::audio::CPP_VERSION::IDevicesFactory::getService();
232     return service ? new DevicesFactoryHalHidl(service) : nullptr;
233 }
234 
235 } // namespace android
236