一 HIDL的结构体关系

以 Light的HIDL为例,class之间的关系如下

HIDL_0525.png

二 client端

if ((sLight == nullptr && !sLightInit) ||

(sLight != nullptr && !sLight->ping().isOk())) {

// will return the hal if it exists the first time.

sLight = ILight::getService();

sLightInit = true;

if (sLight == nullptr) {

ALOGE("Unable to get ILight interface.");

}

}

return sLight;

}

sp hal = LightHal::associate();

Return ret = hal->setLight(type, state);

// static

::android::sp ILight::getService(const std::string &serviceName, const bool getStub) {

return ::android::hardware::details::getServiceInternal(serviceName, true, getStub);

}

getRawServiceInternal {

const sp sm = defaultServiceManager1_1();

if (sm == nullptr) {

ALOGE("getService: defaultServiceManager() is null");

return nullptr;

}

}

sm 通过defaultServiceManager1_1 得到的是BpHwServiceManager,调用get得到ILight的server

sp defaultServiceManager1_1() {

{

AutoMutex _l(details::gDefaultServiceManagerLock);

if (details::gDefaultServiceManager != nullptr) {

return details::gDefaultServiceManager;

}

if (access("/dev/hwbinder", F_OK|R_OK|W_OK) != 0) {

// HwBinder not available on this device or not accessible to

// this process.

return nullptr;

}

waitForHwServiceManager();

while (details::gDefaultServiceManager == nullptr) {

details::gDefaultServiceManager =

fromBinder(

ProcessState::self()->getContextObject(nullptr));

if (details::gDefaultServiceManager == nullptr) {

LOG(ERROR) << "Waited for hwservicemanager, but got nullptr.";

sleep(1);

}

}

}

return details::gDefaultServiceManager;

在get Light的server中,如果server还未启动的话,调用tryStartService启动light server

// Methods from ::android::hidl::manager::V1_0::IServiceManager follow.

Return> ServiceManager::get(const hidl_string& hidlFqName,

const hidl_string& hidlName) {

const std::string fqName = hidlFqName;

const std::string name = hidlName;

pid_t pid = IPCThreadState::self()->getCallingPid();

if (!mAcl.canGet(fqName, pid)) {

return nullptr;

}

auto ifaceIt = mServiceMap.find(fqName);

if (ifaceIt == mServiceMap.end()) {

tryStartService(fqName, hidlName);

return nullptr;

}

const PackageInterfaceMap &ifaceMap = ifaceIt->second;

const HidlService *hidlService = ifaceMap.lookup(name);

if (hidlService == nullptr) {

tryStartService(fqName, hidlName);

return nullptr;

}

sp service = hidlService->getService();

if (service == nullptr) {

tryStartService(fqName, hidlName);

return nullptr;

}

return service;

}

通过getServiceInternal得到的是ILigt注意一点同server端的getService不同的是client端的getStub:false,而在server端是true,这样在 getRawServiceInternal处理流程就不一样,client端是通过hwservice拿到ILitht,而server端是使用openLibs打开-impl.so,得到ILight。client在getServiceInternal完成后拿到ILight,再new出一个BpHwLight

return sp(new BpType(toBinder(base)));

client调用server的接口,比如light的setLight操作,通过HIDL传到server操作

// Methods from ::android::hardware::light::V2_0::ILight follow.

::android::hardware::Return<::android::hardware::light::v2_0::status> BpHwLight::setLight(::android::hardware::light::V2_0::Type type, const ::android::hardware::light::V2_0::LightState& state){

::android::hardware::Return<::android::hardware::light::v2_0::status> _hidl_out = ::android::hardware::light::V2_0::BpHwLight::_hidl_setLight(this, this, type, state);

return _hidl_out;

}

::android::hardware::Return<::android::hardware::light::v2_0::status> BpHwLight::_hidl_setLight(::android::hardware::IInterface *_hidl_this, ::android::hardware::details::HidlInstrumentor *_hidl_this_instrumentor, ::android::hardware::light::V2_0::Type type, const ::android::hardware::light::V2_0::LightState& state) {

_hidl_err = ::android::hardware::IInterface::asBinder(_hidl_this)->transact(1 /* setLight */, _hidl_data, &_hidl_reply);

if (_hidl_err != ::android::OK) { goto _hidl_error; }

}

asBinder最后得到的是IBiner *remote

sp IInterface::asBinder(const sp& iface)

{

if (iface == NULL) return NULL;

return iface->onAsBinder();

}

frombinder是在client get service过程中由

{

::android::sp<::android::hardware::ibinder> _hidl_binder;

_hidl_err = _hidl_reply.readNullableStrongBinder(&_hidl_binder);

if (_hidl_err != ::android::OK) { goto _hidl_error; }

_hidl_out_service = ::android::hardware::fromBinder<::android::hidl::base::v1_0::ibase>(_hidl_binder);

}

readNullableStrongBinder 通过handle创建 BpHwBinder

sp ProcessState::getStrongProxyForHandle(int32_t handle)

b = new BpHwBinder(handle);

最后通过 fromBinder创建BpHwBase(BpHwBinder),bpHwBase同BpHwLight一样继承同一父类

_hidl_out_service = ::android::hardware::fromBinder<::android::hidl::base::v1_0::ibase>(_hidl_binder);

struct BpHwBase : public ::android::hardware::BpInterface, public ::android::hardware::details::HidlInstrumentor {

struct BpHwLight : public ::android::hardware::BpInterface, public ::android::hardware::details::HidlInstrumentor {

IBase是ILight的基类

在getServiceInternal内部得到Ibase也就是BpHwBase之后,在getServiceInternal经过转换

sp base = getRawServiceInternal(IType::descriptor, instance, retry, getStub);

if (base->isRemote()) {

// getRawServiceInternal guarantees we get the proper class

return sp(new BpType(toBinder(base)));

}

这个过程中还有一个toBinder也就是将BpHwBinder

sp toBinder(sp iface) {

IType *ifacePtr = iface.get();

if (ifacePtr == nullptr) {

return nullptr;

}

if (ifacePtr->isRemote()) {

return ::android::hardware::IInterface::asBinder(

static_cast*>(ifacePtr));

asBinder其实就是调用remoteBinder,而remotebinder其实就是BpHwBinder

BpHwBinder* BpHwBinder::remoteBinder()

{

return this;

}

return sp(new BpType(toBinder(base))); == new BpHwLight(BpHwBinder)

当bpHwLight调用HIDL通信是,通过asBinder将hidl_this(指向bpHwBinder)转换成对bpHwBinder->remote的调用,调用bpHwBinder->tranct

_hidl_err = ::android::hardware::IInterface::asBinder(_hidl_this)->transact(1 /* setLight */, _hidl_data, &_hidl_reply);

client过程中有

先fromBinder则是new出一个bpHwbase(bpHwBinder)

通过toBinder 通过bpHwbase得到bpHwBinder给Iligt

在ILigt调用hidl时asBinder 得到bpHwBinder对于client端toBinder是asBinder的封装

三 server端

server端开始的Interface::getService(name, true /* getStub */); ,只不过这里的getStub为true,加载ILight

hardware/interfaces/light/2.0/default/service.cpp

int main() {

return defaultPassthroughServiceImplementation();

}

template

__attribute__((warn_unused_result))

status_t defaultPassthroughServiceImplementation(std::string name,

size_t maxThreads = 1) {

configureRpcThreadpool(maxThreads, true);

status_t result = registerPassthroughServiceImplementation(name);

if (result != OK) {

return result;

}

joinRpcThreadpool();

return 0;

}

template

__attribute__((warn_unused_result))

status_t registerPassthroughServiceImplementation(

std::string name = "default") {

sp service = Interface::getService(name, true /* getStub */); //这部分同client相同,得到的是ILight

if (service == nullptr) {

ALOGE("Could not get passthrough implementation for %s/%s.",

Interface::descriptor, name.c_str());

return EXIT_FAILURE;

}

LOG_FATAL_IF(service->isRemote(), "Implementation of %s/%s is remote!",

Interface::descriptor, name.c_str());

status_t status = service->registerAsService(name);

if (status == OK) {

ALOGI("Registration complete for %s/%s.",

Interface::descriptor, name.c_str());

} else {

ALOGE("Could not register service %s/%s (%d).",

Interface::descriptor, name.c_str(), status);

}

return status;

}

// static

::android::sp ILight::getService(const std::string &serviceName, const bool getStub) {

return ::android::hardware::details::getServiceInternal(serviceName, true, getStub);

}

拿到的这个service就是Light(Light继承ILight),这个ILight也被BnHwLight的hidl_impl所指向。当调用setLight时其实就转到Light的setLight

service->registerAsService(name); 其实调用的是ILight::registerAsService,通过BpHwServiceManager::add 经HIDL到hwservice,在这个过程中ILigt的this被作为输入参数经toBinder转换成BnHwLight

toBinder

if (sBnObj == nullptr) {

auto func = details::getBnConstructorMap().get(myDescriptor, nullptr);

if (!func) {

func = details::gBnConstructorMap.get(myDescriptor, nullptr);

if (!func) {

return nullptr;

}

}

sBnObj = sp(func(static_cast(ifacePtr)));

在ILight中有descriptor和getBnConstructorMap,这个就是在add service中的BpHwServiceManager::_hidl_add 中通过ILight new一个BnHwLight,在service->add时其实添加的是BnHwLight

const char* ILight::descriptor("android.hardware.light@2.0::ILight");

__attribute__((constructor)) static void static_constructor() {

::android::hardware::details::getBnConstructorMap().set(ILight::descriptor,

[](void *iIntf) -> ::android::sp<::android::hardware::ibinder> {

return new BnHwLight(static_cast(iIntf));

});

在HIDL调用hwservice后,BnHwServiceManager::_hidl_add添加service的接口被调用,读出BnHwLight,转变成IBase作为service添加进去

_hidl_err = _hidl_data.readNullableStrongBinder(&_hidl_binder);

::android::hardware::fromBinder<::android::hidl::base::v1_0::ibase>(_hidl_binder);

add调用最后就执行servicemanager的add接口

下面是BnHwLight 操作setLight接口,其实调用的是impl中的HAL实现接口。

::android::status_t BnHwLight::_hidl_setLight(

::android::hidl::base::V1_0::BnHwBase* _hidl_this,

const ::android::hardware::Parcel &_hidl_data,

::android::hardware::Parcel *_hidl_reply,

TransactCallback _hidl_cb) {

::android::hardware::light::V2_0::Status _hidl_out_status = static_cast(_hidl_this->getImpl().get())->setLight(type, *state);

HIDL_getservice.png

::android::status_t ILight::registerAsService(const std::string &serviceName) {

::android::hardware::details::onRegistration("android.hardware.light@2.0", "ILight", serviceName);

const ::android::sp<::android::hidl::manager::v1_0::iservicemanager> sm

= ::android::hardware::defaultServiceManager();

if (sm == nullptr) {

return ::android::INVALID_OPERATION;

}

::android::hardware::Return ret = sm->add(serviceName.c_str(), this);

return ret.isOk() && ret ? ::android::OK : ::android::UNKNOWN_ERROR;

}

joinRpcThreadpool(); 流程如下:

void IPCThreadState::joinThreadPool(bool isMain)

{

result = getAndExecuteCommand();

status_t IPCThreadState::executeCommand(int32_t cmd)

{

error = reinterpret_cast(tr.cookie)->transact(tr.code, buffer,

&reply, tr.flags, reply_callback);

三 hwserice

hwservice负责管理hidl clinet与server之间的管理。

system/hwservicemanager/service.cpp中

int main() {

configureRpcThreadpool(1, true /* callerWillJoin */);

ServiceManager *manager = new ServiceManager();

if (!manager->add(serviceName, manager)) {

ALOGE("Failed to register hwservicemanager with itself.");

}

TokenManager *tokenManager = new TokenManager();

if (!manager->add(serviceName, tokenManager)) {

ALOGE("Failed to register ITokenManager with hwservicemanager.");

}

// Tell IPCThreadState we're the service manager

sp service = new BnHwServiceManager(manager);

IPCThreadState::self()->setTheContextObject(service);

// Then tell the kernel

ProcessState::self()->becomeContextManager(nullptr, nullptr);

int rc = property_set("hwservicemanager.ready", "true");

if (rc) {

ALOGE("Failed to set \"hwservicemanager.ready\" (error %d). "\

"HAL services will not start!\n", rc);

}

joinRpcThreadpool();

return 0;

}

当client或者server需要使用hwservice时,主要有以下get add

::android::status_t BnHwServiceManager::onTransact(

uint32_t _hidl_code,

const ::android::hardware::Parcel &_hidl_data,

::android::hardware::Parcel *_hidl_reply,

uint32_t _hidl_flags,

TransactCallback _hidl_cb) {

::android::status_t _hidl_err = ::android::OK;

switch (_hidl_code) {

case 1 /* get */:

{

bool _hidl_is_oneway = _hidl_flags & 1 /* oneway */;

if (_hidl_is_oneway != false) {

return ::android::UNKNOWN_ERROR;

}

_hidl_err = ::android::hidl::manager::V1_0::BnHwServiceManager::_hidl_get(this, _hidl_data, _hidl_reply, _hidl_cb);

break;

}

case 2 /* add */:

{

bool _hidl_is_oneway = _hidl_flags & 1 /* oneway */;

if (_hidl_is_oneway != false) {

return ::android::UNKNOWN_ERROR;

}

_hidl_err = ::android::hidl::manager::V1_0::BnHwServiceManager::_hidl_add(this, _hidl_data, _hidl_reply, _hidl_cb);

break;

}

case 3 /* getTransport */:

{

bool _hidl_is_oneway = _hidl_flags & 1 /* oneway */;

if (_hidl_is_oneway != false) {

return ::android::UNKNOWN_ERROR;

}

_hidl_err = ::android::hidl::manager::V1_0::BnHwServiceManager::_hidl_getTransport(this, _hidl_data, _hidl_reply, _hidl_cb);

break;

}

case 4 /* list */:

{

bool _hidl_is_oneway = _hidl_flags & 1 /* oneway */;

if (_hidl_is_oneway != false) {

return ::android::UNKNOWN_ERROR;

}

_hidl_err = ::android::hidl::manager::V1_0::BnHwServiceManager::_hidl_list(this, _hidl_data, _hidl_reply, _hidl_cb);

break;

}

case 5 /* listByInterface */:

{

bool _hidl_is_oneway = _hidl_flags & 1 /* oneway */;

if (_hidl_is_oneway != false) {

return ::android::UNKNOWN_ERROR;

}

_hidl_err = ::android::hidl::manager::V1_0::BnHwServiceManager::_hidl_listByInterface(this, _hidl_data, _hidl_reply, _hidl_cb);

break;

}

case 6 /* registerForNotifications */:

{

bool _hidl_is_oneway = _hidl_flags & 1 /* oneway */;

if (_hidl_is_oneway != false) {

return ::android::UNKNOWN_ERROR;

}

_hidl_err = ::android::hidl::manager::V1_0::BnHwServiceManager::_hidl_registerForNotifications(this, _hidl_data, _hidl_reply, _hidl_cb);

break;

}

case 7 /* debugDump */:

{

bool _hidl_is_oneway = _hidl_flags & 1 /* oneway */;

if (_hidl_is_oneway != false) {

return ::android::UNKNOWN_ERROR;

}

_hidl_err = ::android::hidl::manager::V1_0::BnHwServiceManager::_hidl_debugDump(this, _hidl_data, _hidl_reply, _hidl_cb);

break;

}

case 8 /* registerPassthroughClient */:

{

bool _hidl_is_oneway = _hidl_flags & 1 /* oneway */;

if (_hidl_is_oneway != false) {

return ::android::UNKNOWN_ERROR;

}

_hidl_err = ::android::hidl::manager::V1_0::BnHwServiceManager::_hidl_registerPassthroughClient(this, _hidl_data, _hidl_reply, _hidl_cb);

break;

}

case 9 /* unregisterForNotifications */:

{

bool _hidl_is_oneway = _hidl_flags & 1 /* oneway */;

if (_hidl_is_oneway != false) {

return ::android::UNKNOWN_ERROR;

}

_hidl_err = ::android::hidl::manager::V1_1::BnHwServiceManager::_hidl_unregisterForNotifications(this, _hidl_data, _hidl_reply, _hidl_cb);

break;

}

default:

{

return ::android::hidl::base::V1_0::BnHwBase::onTransact(

_hidl_code, _hidl_data, _hidl_reply, _hidl_flags, _hidl_cb);

}

}

if (_hidl_err == ::android::UNEXPECTED_NULL) {

_hidl_err = ::android::hardware::writeToParcel(

::android::hardware::Status::fromExceptionCode(::android::hardware::Status::EX_NULL_POINTER),

_hidl_reply);

}return _hidl_err;

}

hidl 原理分析_HIDL模型分析相关推荐

  1. 基于matlab的电机运行分析,电机模型分析及拖动仿真:基于MATLAB的现代方法

    第1章绪论 1.1MATLAB概述 1.2Simulink平台与MATLAB工具箱 1.2.1Simulink平台 1.2.2MATLAB工具箱 1.2.3SimPowerSystems工具箱 1.3 ...

  2. 软件测试分析——质量模型分析法

    质量模型包含6大特性27个子特性,本次介绍除维护性之外的5大特性. 目录 功能性 适合性 准确性 互操作性 保密安全性 可靠性 成熟性 容错性 易恢复性 易用性 易理解性 易学性 吸引性 效率 时间特 ...

  3. 分析ctr模型效果的一些思路总结

    算法工程师日常的主要工作就是各种调优模型,从特征.模型结构.超参数.建模方向等.评估.分析模型的方法论,往往决定着模型优化的效率和技术选择方向.我尝试总结在模型分析的过程中常用的方法和遇到过的问题以及 ...

  4. 数学建模之层次分析法模型

    目录 问题 问题实例 问题分析 建立层次分析法模型 分析权重的方法 ​编辑 填表指标的权重​编辑 一致性检验 一致性检验的引入 一致性检验的步骤 计算判断矩阵 归一化处理 算术平均法求权重 几何平均法 ...

  5. 用户行为分析的背景以及几种模型分析、实例分析——淘宝用户行为分析

    这里写目录标题 1. 绪论 1.1了解用户行为分析 1.2用户行为分析的目的 2.用户行为分析的具体内容 2.1用户行为分析的指标 2.2用户行为分析模型 2.2.1漏斗模型分析 2.2.2用户留存分 ...

  6. 分析洋葱模型实现原理,在自己项目中接入洋葱模型

    分析洋葱模型实现原理,在自己项目中接入洋葱模型 上一篇文章初识洋葱模型,分析中间件执行过程,浅析koa中间件源码简单的介绍了 基于 koa 的洋葱模型的中间件的运行过程,了解了一下中间件的写法 不过基 ...

  7. hidl 原理分析_AIDL原理分析

    季春初始,天气返暖,新冠渐去,正值学习好时机.在Android系统中,AIDL一直在Framework和应用层上扮演着很重要的角色,今日且将其原理简单分析.(文2020.03.30) 一.开篇介绍 1 ...

  8. [GIS原理] 9 数字地形分析DTA、数字地形模型DTM、数字高程模型DEM、数字地表模型DSM、不规则三角网TIN

    在知识传播途中,向涉及到的相关著作权人谨致谢意! 文章目录 1 数字地形分析(DTA) 1.1 数字地形模型(DTM) 1.1.1 DSM与DEM对比 1.2 数字地形分析研究与应用进展 1.2.1 ...

  9. 朴素、Select、Poll和Epoll网络编程模型实现和分析——Epoll模型

    在阅读完<朴素.Select.Poll和Epoll网络编程模型实现和分析--Select模型>和<朴素.Select.Poll和Epoll网络编程模型实现和分析--Poll模型> ...

最新文章

  1. 两个硬盘和文件相关的小技巧 (C#)
  2. “拒绝在 iPad 上运行 Xcode!”
  3. Windows下安装python的pip
  4. MAT之SA:利用SA算法解决TSP(数据是14个虚拟城市的横纵坐标)问题
  5. python水印 resized_如何改进Python中的水印图像?
  6. 在Ubuntu环境下使用vcpkg安装sqlite_orm包文件
  7. python表格数据的储存和读取_python读取表格存储
  8. 2019年8月1日星期四(数据结构)
  9. 漫谈iOS Crash收集框架
  10. asp.net中Roles和User的异常处理机制的思考
  11. 周杰 清华大学计算机学院,周杰 -清华大学自动化系
  12. 看流畅的python感觉有难度_读《流畅的Python》有感
  13. 马云单挑“叶问”吊打“战狼”,网友:原来天下武功,唯富不破~是贫穷限制了我的想象力
  14. SSD目标检测网络tensorRT推理【附代码】
  15. 计算机组成原理实验心得2000字,计算机组成原理实验一:基础汇编语言程序设计实验...
  16. c语言 输入两个正整数m和n,求其最小公倍数。
  17. 单基因gsea_又是神器!基于单基因批量相关性分析的GSEA
  18. CES2020 | 小牛电动成为科技出行的“另类”标杆
  19. 这个毕业季,让海马体照相馆为简历添“战斗力”
  20. 基于MATLAB的线性规划:linprog用法

热门文章

  1. opencv文字区域的提取(vs2019 c++)
  2. Cocos Creater 学习笔记(一)
  3. iOS NSNumber(数字对象)
  4. c语言中 加法符号如何定义,【 c语言中无符号和有符号的加法运算】【深入理解】--【sky原创】...
  5. html5时间线图片自动轮播,js实现自动播放匀速轮播图
  6. 风火轮PN532 NFC 开发板 串口连接方式 卡片物理号 读取
  7. python练手_邮件定时发送
  8. 【产业互联网周报】财报季:AWS营收108亿美元,增速降至30%以下;Azure增速首次放缓低于50%;IBM云营收达63亿美元...
  9. 【随风丶逆风】2021年终总结
  10. JAVA学习(八):JAVA文件编程