博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android sensor hal 详解
阅读量:4159 次
发布时间:2019-05-26

本文共 4198 字,大约阅读时间需要 13 分钟。

                                              Android sensor hal 详解

这里面有关于halJNI的详细讲解

代码的路径是在:ics_ventana/frameworks/base/services/sensorservice/SensorDevice.cpp里面

Androidhal层有三个重要的结构:

structhw_module_t:

structhw_device_t

structhw_module_methods_t:

structhw_module_t结构如下:

typedefstructhw_module_t{

uint32_ttag;//这个tag必须初始化为HARDWARE_MODULE_TAG

uint16_tversion_major;//模块的主版本号

uint16_tversion_minor;//模块的次版本号

constchar*id;//模块的一个标识id

constchar*name;//模块的名字

constchar*author;//实现这个模块的作者

structhw_module_methods_t*methods;//模块的方法

/**module'sdso*/

void*dso;

/**padding to 128 bytes, reserved for future use */

uint32_treserved[32-7];

}hw_module_t;

每一个hardwaremodule必须有一个数据结构,并且命名为HAL_MODULE_INFO_SYM,并且这个结构开始必须以structhw_module_t,下面才是是这个模块的具体定义的信息。

就比如我们的sensormodule的定义如下:

structsensors_module_t {

structhw_module_tcommon;//必须放在第一位

*/

int(*get_sensors_list)(structsensors_module_t*module,

structsensor_tconst**list);//下面是具体的自己实现的方法

};

我们具体的sensor的实现如下:

structsensors_module_t HAL_MODULE_INFO_SYM = {//每一个模块的定义的变量的名字必须是HAL_MODULE_INFO_SYM

common: {

tag:HARDWARE_MODULE_TAG,//这个tag也必须是HARDWARE_MODULE_TAG

version_major: 1,

version_minor: 0,

id:SENSORS_HARDWARE_MODULE_ID,

name:"Ventanasensors module",

author:"厂商名字",

methods:&sensors_module_methods,

dso: NULL,

reserved: {0}

},

get_sensors_list:sensors__get_sensors_list,

};

structhw_device_t

structhw_device_t{

uint32_ttag;//这个devicetag,必须初始化为HARDWARE_DEVICE_TAG和上面的moduletag是一个意思

uint32_tversion;//这个设备的版本号

structhw_module_t*module;//这个设备所属于的模块

uint32_treserved[12];//保留使用的

int(*close)(structhw_device_t*device);//关闭设备的方法

}

每一个设备必须有一个数据结构,这个结构必须以structhw_device_t开始,下面才是这个设备的公共的方法和属性

structsensors_poll_device_t {

structhw_device_tcommon;

...

..

}

我们一般是在structhw_module_methods_t结构里面的open函数里面来进行对上面的成员进行赋值。

在下面的讲解structhw_module_methods_t的时候会叙述

structhw_module_methods_t:

typedefstructhw_module_methods_t{

int(*open)(conststructhw_module_t*module,constchar*id,

structhw_device_t**device);//用这个函数打开一个具体的设备

}

我们的sensor代码的实现:

staticstruct hw_module_methods_tsensors_module_methods = {

open: open_sensors

};

structsensors_poll_context_t {

structsensors_poll_device_tdevice;//must be first

.....

.....

}

open_sensor的实现如下:

/**Open a new instance of a sensor device using name */

staticintopen_sensors(conststructhw_module_t*module,constchar*id,

structhw_device_t**device)//发现没有,我们传递进来的是structhw_device_t这个结构

{

FUNC_LOG;

intstatus = -EINVAL;

sensors_poll_context_t*dev =newsensors_poll_context_t();//structsensors_poll_context_t的第一个成员的地址和structsensors_poll_device_t以及structhw_device_t的地址是一样的,就是因为这个原因,下面才可以进行取值,进行赋值

memset(&dev->device,0,sizeof(sensors_poll_device_t));

dev->device.common.tag= HARDWARE_DEVICE_TAG;

dev->device.common.version = 0;

dev->device.common.module =const_cast<hw_module_t*>(module);

dev->device.common.close = poll__close;

dev->device.activate = poll__activate;

dev->device.setDelay = poll__setDelay;

dev->device.poll = poll__poll;

*device =&dev->device.common;//经赋值好的device里面的成员,赋值给structsensors_poll_device_t里面的每一个成员

status = 0;

returnstatus;

}

上面是Hal层的一些基础知识,我们上层是如何通过hal层一直访问到底层提供的结构呢?

我们是通过hw_get_module这个函数进行获得对应的hal层的module,获得这个module之后,再通过module里面的interface操作底层驱动。

代码的路径是在:frameworks/base/services/sensorservice/SensorDevice.cpp

SensorDevice::SensorDevice()

:mSensorDevice(0),

mSensorModule(0)

{

status_terr = hw_get_module(SENSORS_HARDWARE_MODULE_ID,

(hw_module_tconst**)&mSensorModule);//获得这个id的模块,

LOGE_IF(err,"couldn'tload %s module (%s)",

SENSORS_HARDWARE_MODULE_ID,strerror(-err));

if(mSensorModule){

err =sensors_open(&mSensorModule->common,&mSensorDevice);//

LOGE_IF(err,"couldn'topen device for module %s (%s)",

SENSORS_HARDWARE_MODULE_ID,strerror(-err));

if(mSensorDevice){

sensor_tconst*list;

ssize_t count =mSensorModule->get_sensors_list(mSensorModule,&list);

mActivationCount.setCapacity(count);

Infomodel;

for(size_t i=0 ; i<size_t(count) ; i++) {

mActivationCount.add(list[i].handle,model);

mSensorDevice->activate(mSensorDevice,list[i].handle, 0);

}

}

}

}

上面只要知道hw_get_module函数通过id的值获得模块。至于hw_get_module函数的实现我会在另外一个文档里面进行讲解。

看下上面的sensors_open函数的实现:

staticinlineintsensors_open(conststructhw_module_t* module,

structsensors_poll_device_t**device) {

returnmodule->methods->open(module,

SENSORS_HARDWARE_POLL,(structhw_device_t**)device);

}

觉得有个参考的网站:

转载地址:http://bsbxi.baihongyu.com/

你可能感兴趣的文章
Python模块(Module)
查看>>
程序员的学习和积累
查看>>
你做过的最有效的提高你的编程水平的一件事情是什么
查看>>
你必看的一个老程序员积累的20条编程经验
查看>>
接口测试总结
查看>>
Python 基础语法(一)
查看>>
Python 基础语法(二)
查看>>
Python 基础语法(三)
查看>>
MXBean already registered报错解决
查看>>
feign接口调用使用header
查看>>
mac 根目录权限问题处理
查看>>
Spring AOP 详解
查看>>
Double数值过大时,默认使用科学计数法的解决办法
查看>>
Spring项目搭建(一)
查看>>
spring项目搭建二(整合springmvc)
查看>>
Spring项目搭建三(整合mybaties)
查看>>
git 提交文件撤销操作
查看>>
SpringBoot入门-helloworld
查看>>
SpringBoot入门-配置文件
查看>>
springboot-定时任务
查看>>