android hal sensor,Android sensor hal 详解

Android sensor hal 详解

这里面有关于hal、JNI的详细讲解

代码的路径是在: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;//模块的方法

void*dso;

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;//这个device的tag,必须初始化为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的实现如下:

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(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

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://blog.csdn.net/mr_raptor/article/details/8074549

http://www.cnblogs.com/innost/archive/2011/11/08/2241653.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值