• 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 "ConversionHelperHidl.h"
30 #include "DeviceHalHidl.h"
31 #include "DevicesFactoryHalHidl.h"
32 
33 using ::android::hardware::audio::CPP_VERSION::IDevice;
34 using ::android::hardware::audio::CPP_VERSION::Result;
35 using ::android::hardware::Return;
36 using ::android::hardware::Void;
37 using ::android::hidl::manager::V1_0::IServiceManager;
38 using ::android::hidl::manager::V1_0::IServiceNotification;
39 
40 namespace android {
41 namespace CPP_VERSION {
42 
43 class ServiceNotificationListener : public IServiceNotification {
44   public:
ServiceNotificationListener(sp<DevicesFactoryHalHidl> factory)45     explicit ServiceNotificationListener(sp<DevicesFactoryHalHidl> factory)
46             : mFactory(factory) {}
47 
onRegistration(const hidl_string &,const hidl_string & instance_name,bool)48     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*/);
57         }
58         return Void();
59     }
60 
61   private:
62     wp<DevicesFactoryHalHidl> mFactory;
63 };
64 
DevicesFactoryHalHidl(sp<IDevicesFactory> devicesFactory)65 DevicesFactoryHalHidl::DevicesFactoryHalHidl(sp<IDevicesFactory> devicesFactory) {
66     ALOG_ASSERT(devicesFactory != nullptr, "Provided default IDevicesFactory service is NULL");
67     addDeviceFactory(devicesFactory, false /*needToNotify*/);
68 }
69 
onFirstRef()70 void 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 }
84 
85 #if MAJOR_VERSION == 2
idFromHal(const char * name,status_t * status)86 static IDevicesFactory::Device idFromHal(const char *name, status_t* status) {
87     *status = OK;
88     if (strcmp(name, AUDIO_HARDWARE_MODULE_ID_PRIMARY) == 0) {
89         return IDevicesFactory::Device::PRIMARY;
90     } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
91         return IDevicesFactory::Device::A2DP;
92     } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
93         return IDevicesFactory::Device::USB;
94     } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
95         return IDevicesFactory::Device::R_SUBMIX;
96     } else if(strcmp(name, AUDIO_HARDWARE_MODULE_ID_STUB) == 0) {
97         return IDevicesFactory::Device::STUB;
98     }
99     ALOGE("Invalid device name %s", name);
100     *status = BAD_VALUE;
101     return {};
102 }
103 #elif MAJOR_VERSION >= 4
idFromHal(const char * name,status_t * status)104 static const char* idFromHal(const char *name, status_t* status) {
105     *status = OK;
106     return name;
107 }
108 #endif
109 
openDevice(const char * name,sp<DeviceHalInterface> * device)110 status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) {
111     auto factories = copyDeviceFactories();
112     if (factories.empty()) return NO_INIT;
113     status_t status;
114     auto hidlId = idFromHal(name, &status);
115     if (status != OK) return status;
116     Result retval = Result::NOT_INITIALIZED;
117     for (const auto& factory : factories) {
118         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         }
135     }
136     ALOGW("The specified device name is not recognized: \"%s\"", name);
137     return BAD_VALUE;
138 }
139 
getHalPids(std::vector<pid_t> * pids)140 status_t DevicesFactoryHalHidl::getHalPids(std::vector<pid_t> *pids) {
141     std::set<pid_t> pidsSet;
142     auto factories = copyDeviceFactories();
143     for (const auto& factory : factories) {
144         using ::android::hidl::base::V1_0::DebugInfo;
145 
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 
setCallbackOnce(sp<DevicesFactoryHalCallback> callback)163 status_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 
addDeviceFactory(sp<IDevicesFactory> factory,bool needToNotify)181 void 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 
copyDeviceFactories()201 std::vector<sp<IDevicesFactory>> DevicesFactoryHalHidl::copyDeviceFactories() {
202     std::lock_guard<std::mutex> lock(mLock);
203     return mDeviceFactories;
204 }
205 
206 } // namespace CPP_VERSION
207 } // namespace android
208