hal interface

?IDeviceFactory openDevice得到IDevice, IDevice openInput/outputStream得到IStream
?android.hardware.audio.service
存在形式
hardware/interfaces/audio/common/all-versions/default/service/android.hardware.audio.service.rc service vendor.audio-hal /vendor/bin/hw/android.hardware.audio.service ? ? class hal ? ? user audioserver
hardware/interfaces/audio/common/all-versions/default/service/Android.bp cc_binary { ? ? name: "android.hardware.audio.service", ? ? init_rc: ["android.hardware.audio.service.rc"], ? ? vendor: true, ? ? srcs: ["service.cpp"], }
在同一个进程启动了多个service
int main(int /* argc */, char* /* argv */ []) { ? ? ::android::ProcessState::initWithDriver("/dev/vndbinder"); ? ? // start a threadpool for vndbinder interactions ? ? ::android::ProcessState::self()->startThreadPool(); ? ? const std::vector<InterfacesList> mandatoryInterfaces = { ? ? ? ? { ? ? ? ? ? ? "Audio Core API", ? ? ? ? ? ? "android.hardware.audio@6.0::IDevicesFactory", ? ? ? ? ? ? "android.hardware.audio@5.0::IDevicesFactory", ? ? ? ? ? ? "android.hardware.audio@4.0::IDevicesFactory", ? ? ? ? ? ? "android.hardware.audio@2.0::IDevicesFactory" ? ? ? ? }, ? ? ? ? { ? ? ? ? ? ? "Audio Effect API", ? ? ? ? ? ? "android.hardware.audio.effect@6.0::IEffectsFactory", ? ? ? ? ? ? "android.hardware.audio.effect@5.0::IEffectsFactory", ? ? ? ? ? ? "android.hardware.audio.effect@4.0::IEffectsFactory", ? ? ? ? ? ? "android.hardware.audio.effect@2.0::IEffectsFactory", ? ? ? ? } ? ? };
? ? const std::vector<InterfacesList> optionalInterfaces = { ? ? ? ? { ? ? ? ? ? ? "Soundtrigger API", ? ? ? ? ? ? "android.hardware.soundtrigger@2.3::ISoundTriggerHw", ? ? ? ? ? ? "android.hardware.soundtrigger@2.2::ISoundTriggerHw", ? ? ? ? ? ? "android.hardware.soundtrigger@2.1::ISoundTriggerHw", ? ? ? ? ? ? "android.hardware.soundtrigger@2.0::ISoundTriggerHw", ? ? ? ? }, ? ? ? ? { ? ? ? ? ? ? "Bluetooth Audio API", ? ? ? ? ? ? "android.hardware.bluetooth.audio@2.0::IBluetoothAudioProvidersFactory" ? ? ? ? }, ? ? ? ? // remove the old HIDL when Bluetooth Audio Hal V2 has offloading supported ? ? ? ? { ? ? ? ? ? ? "Bluetooth Audio Offload API", ? ? ? ? ? ? "android.hardware.bluetooth.a2dp@1.0::IBluetoothAudioOffload" ? ? ? ? } ? ? };
? ? for (const auto& listIter : mandatoryInterfaces) { ? ? ? ? auto iter = listIter.begin(); ? ? ? ? const std::string& interfaceFamilyName = *iter++; ? ? ? ? LOG_ALWAYS_FATAL_IF(!registerPassthroughServiceImplementations(iter, listIter.end()), ? ? ? ? ? ? ? ? ? ? ? ? ? ? "Could not register %s", interfaceFamilyName.c_str()); ? ? }
? ? for (const auto& listIter : optionalInterfaces) { ? ? ? ? auto iter = listIter.begin(); ? ? ? ? const std::string& interfaceFamilyName = *iter++; ? ? ? ? ALOGW_IF(!registerPassthroughServiceImplementations(iter, listIter.end()), ? ? ? ? ? ? ? ? ?"Could not register %s", interfaceFamilyName.c_str()); ? ? }
? ? joinRpcThreadpool(); } ?
registerPassthroughServiceImplementation的实现
https://www.programminghunter.com/article/7441427492/ https://blog.csdn.net/u013928208/article/details/81667162
HIDL_FETCH_IDevicesFactory是怎样别调到的? registerPassthroughServiceImplementation的实现 system/libhidl/transport/LegacySupport.cpp
registerPassthroughServiceImplementation ->? getRawServiceInternal ->? getPassthroughServiceManager -> (system/libhidl/transport/ServiceManagement.cpp) IServiceManager1_1::openLibs { ? ? ? ? const std::string prefix = packageAndVersion + "-impl"; ? ? ? ? const std::string sym = "HIDL_FETCH_" + ifaceName; }
hardware/interfaces/audio/effect/all-versions/default/Android.bp cc_library_shared { ? ? name: "android.hardware.audio.effect@6.0-impl", ? ? defaults: ["android.hardware.audio.effect-impl_default"], ? ? shared_libs: [ ? ? ? ? "android.hardware.audio.common@6.0", ? ? ? ? "android.hardware.audio.common@6.0-util", ? ? ? ? "android.hardware.audio.effect@6.0", ? ? ], }
hardware/interfaces/audio/core/all-versions/default/DevicesFactory.cpp IDevicesFactory* HIDL_FETCH_IDevicesFactory(const char* name) { ? ? return strcmp(name, "default") == 0 ? new DevicesFactory() : nullptr; }
DevicesFactoryHalHybrid::openDevice 的实现
status_t DevicesFactoryHalHybrid::openDevice(const char *name, sp<DeviceHalInterface> *device) { ? ? if (mHidlFactory != 0 && strcmp(AUDIO_HARDWARE_MODULE_ID_A2DP, name) != 0 && ? ? ? ? strcmp(AUDIO_HARDWARE_MODULE_ID_HEARING_AID, name) != 0) { ? ? ? ? return mHidlFactory->openDevice(name, device); ? ? } ? ? return mLocalFactory->openDevice(name, device); }
/** ?* List of known audio HAL modules. This is the base name of the audio HAL ?* library composed of the "audio." prefix, one of the base names below and ?* a suffix specific to the device. ?* e.g: audio.primary.goldfish.so or audio.a2dp.default.so ?* ?* The same module names are used in audio policy configuration files. ?*/ #define AUDIO_HARDWARE_MODULE_ID_PRIMARY "primary" #define AUDIO_HARDWARE_MODULE_ID_A2DP "a2dp" #define AUDIO_HARDWARE_MODULE_ID_USB "usb" #define AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX "r_submix" #define AUDIO_HARDWARE_MODULE_ID_CODEC_OFFLOAD "codec_offload" #define AUDIO_HARDWARE_MODULE_ID_STUB "stub" #define AUDIO_HARDWARE_MODULE_ID_HEARING_AID "hearing_aid" #define AUDIO_HARDWARE_MODULE_ID_MSD "msd"
mHidlFactory->openDevice(name, device)的实现 status_t DevicesFactoryHalHidl::openDevice(const char *name, sp<DeviceHalInterface> *device) { ? ? auto factories = copyDeviceFactories(); ? ? if (factories.empty()) return NO_INIT; ? ? status_t status; ? ? auto hidlId = idFromHal(name, &status); ? ? if (status != OK) return status; ? ? Result retval = Result::NOT_INITIALIZED; ? ? for (const auto& factory : factories) { ? ? ? ? Return<void> ret = factory->openDevice( ? ? ? ? ? ? ? ? hidlId, ? ? ? ? ? ? ? ? [&](Result r, const sp<IDevice>& result) { ? ? ? ? ? ? ? ? ? ? retval = r; ? ? ? ? ? ? ? ? ? ? if (retval == Result::OK) { //在回调函数里 hidl service 把生成的 IDevice作为参数 ?? ??? ??? ??? ??? ??? ?//调用new DeviceHalHidl(IDevice) ? ? ? ? ? ? ? ? ? ? ? ? *device = new DeviceHalHidl(result); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? }); ? ? } ? ? ALOGW("The specified device name is not recognized: \"%s\"", name); ? ? return BAD_VALUE; }
hardware/interfaces/audio/core/all-versions/default/DevicesFactory.cpp template <class DeviceShim, class Callback> Return<void> DevicesFactory::openDevice(const char* moduleName, Callback _hidl_cb) { ? ? audio_hw_device_t* halDevice;
? ? sp<DeviceShim> result; ? ? int halStatus = loadAudioInterface(moduleName, &halDevice); ? ? if (halStatus == OK) { ? ? ? ? result = new DeviceShim(halDevice); ? ? ? ? retval = Result::OK; ? ? }
? ? _hidl_cb(retval, result); ? ? return Void(); }
hardware device的框架 int DevicesFactory::loadAudioInterface(const char* if_name, audio_hw_device_t** dev) { ? ? const hw_module_t* mod; ? ? int rc;
? ? rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID: "audio", if_name, &mod);
? ? rc = audio_hw_device_open(mod, dev);
? ? return OK; } ?
|