赞
踩
http://blog.csdn.net/myarrow/article/details/9044689
在了解Sensor工作流程以前,一直以为其事件是通过Event Hub来进行输送的,可是研究完Android4.0代码之后,才发现自己错了。
其主要框架如下图所示:
与下层接口功能:
1) 在SensorManager函数中
(1) 调用native sensors_module_init初始化sensor list,即实例化native中的SensorManager
(2) 创建SensorThread线程
2) 在类SensorThread中
(1) 调用native sensors_create_queue创建队列
(2) 在线程中dead loop地调用native sensors_data_poll以从队列sQueue中获取事件(float[] values = new float[3];)
(3) 收到事件之后,报告sensor event给所有注册且关心此事件的listener
与上层的接口功能:
1) 在onPause时取消listener注册
2) 在onResume时注册listener
3) 把收到的事件报告给注册的listener
实现SensorManager.java中的native函数,它主要调用SenrsorManager.cpp和SensorEventQueue.cpp中的类来完成相关的工作。
SensorService作为一个轻量级的system service,它运行于SystemServer内,即在system_init<system_init.cpp>调用SensorService::instantiate();
SensorService主要功能如下:
1) SensorService::instantiate创建实例对象,并增加到ServiceManager中,且创建并启动线程,并执行threadLoop
2) threadLoop从sensor驱动获取原始数据,然后通过SensorEventConnection把事件发送给客户端
3) BnSensorServer的成员函数负责让客户端获取sensor列表和创建SensorEventConnection
SensorService与客户端的接口定义如下:
SensorService定义如下:
SensorDevice封装了对SensorHAL层代码的调用,主要包含以下功能:
1) 获取sensor列表(getSensorList)
2) 获取sensor事件(poll)
3) Enable或Disable sensor (activate)
4) 设置delay时间
定义:/hardware/libhardware/include/hardware/sensors.h
实现:/hardware/mychip/sensor/st/sensors.c
实现代码位于:/hardware/mychip/sensor/st/nusensors.cpp
从上面的代码中可以看出,当调用init_nusensors时,它将返回sensors_poll_device_t,然后就可以调用sensors_poll_device_t 的以下方法进行相关操作:
1) activate
2) setDelay
3) poll
6.1) struct sensors_poll_context_t 定义
6.2) init_nusensors 实现
由以上代码可见,sensors_poll_device_t的activate、setDelay和poll的实现函数分别为:
(1) poll__activate
(2) poll__setDelay
(3) poll__poll
下面讲解以上三个关键函数的实现
6.3) struct sensors_poll_context_t 的实现
下面MmaSensor为例进行分析。
1) SensorBase的实现(SensorBase.cpp)
2) MmaSensor的实现
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。