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

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




更多相关文章

  1. C语言函数的递归(上)
  2. Android(安卓)面试--请描述一下Activity的生命周期?
  3. [置顶] android ndk空指针导致CRASH的问题
  4. Android(安卓)单选之史上最简单的ListView实现单选效果
  5. Android之SQLite数据库的使用
  6. Android应用程序安装过程源代码分析(2)
  7. android短信的接收和发送处理
  8. Android通过URL获取网络数据(2)
  9. Android(安卓)Studio开发之ndk报错--error: no template named

随机推荐

  1. 世界国家 的数据库sql
  2. mysql下载+安装+启动过程及问题解决
  3. Recordset记录集对象的属性
  4. SQL - 仅选择distinct一列
  5. Excel 复制Sql查询结果错位
  6. 新手求助一条sql语句~~帮忙看看
  7. SQL 日志太大,脚本清除
  8. 使用python抓取csdn博客访问量并保存在sq
  9. 编译安装mysql-server5.6.32手记
  10. 将非常简单的Expression >转换为SQL wher