探秘SensorHAL

本文详细探讨了Android SensorHAL在4.1.2版本中的实现,包括sensors.h中的结构体定义、传感器服务的启动过程、SensorDevice如何与HAL交互以及SensorService如何通过线程循环读取硬件数据。通过对源码的分析,揭示了SensorHAL如何激活传感器并持续从硬件驱动中获取数据。
摘要由CSDN通过智能技术生成

/************************************************************/

本文仅供学习交流,如用于商业用途,请征得作者同意。

欢迎转载,转载请注明出处。

http://blog.csdn.net/airk000/article/details/8659453

E-mail: airk908@gmail.com

/************************************************************/


分析背景:

Android4.1.2(Based on CyanogenMod 10)

DASH(https://github.com/sonyxperiadev/DASH)


1.接口头文件sensors.h(hardware/libhardware/include/sensors.h)

    刚一打开就看到SENSOR HAL的ID 为"SENSORS_HARDWARE_MODULE_ID"。

/**
 * Handles must be higher than SENSORS_HANDLE_BASE and must be unique.
 * A Handle identifies a given sensors. The handle is used to activate
 * and/or deactivate sensors.
 * In this version of the API there can only be 256 handles.
 */
#define SENSORS_HANDLE_BASE             0
#define SENSORS_HANDLE_BITS             8
#define SENSORS_HANDLE_COUNT            (1<<SENSORS_HANDLE_BITS)
/* 8位能够支持256个handle */

/**
 * Sensor types
 */
#define SENSOR_TYPE_ACCELEROMETER       1  //加速度传感器
#define SENSOR_TYPE_MAGNETIC_FIELD      2  //磁力传感器
#define SENSOR_TYPE_ORIENTATION         3  //方向传感器
#define SENSOR_TYPE_GYROSCOPE           4  //陀螺仪
#define SENSOR_TYPE_LIGHT               5  //亮度传感器
#define SENSOR_TYPE_PRESSURE            6  //压力传感器
#define SENSOR_TYPE_TEMPERATURE         7   // deprecated  温度传感器,以后的版本中将看不到任何身影
#define SENSOR_TYPE_PROXIMITY           8  //距离传感器
#define SENSOR_TYPE_GRAVITY             9  //重力传感器
#define SENSOR_TYPE_LINEAR_ACCELERATION 10  //速度传感器
#define SENSOR_TYPE_ROTATION_VECTOR     11  //旋转矢量传感器
#define SENSOR_TYPE_RELATIVE_HUMIDITY   12  //相对湿度传感器
#define SENSOR_TYPE_AMBIENT_TEMPERATURE 13  //环境温度传感器

    支持的传感器还很多,但是 一般设备并不会用到这么多传感器,也就是加速度传感器、重力传感器、方向传感器、距离传感器、光线传感器这些,陀螺仪都很少见。

/**
 * Definition of the axis
 * ----------------------
 *
 * This API is relative to the screen of the device in its default orientation,
 * that is, if the device can be used in portrait or landscape, this API
 * is only relative to the NATURAL orientation of the screen. In other words,
 * the axis are not swapped when the device's screen orientation changes.
 * Higher level services /may/ perform this transformation.
 *
 *   x<0         x>0
 *                ^
 *                |
 *    +-----------+-->  y>0
 *    |           |
 *    |           |
 *    |    设备    |
 *    |           |   / z<0
 *    |  面朝天空  |  /
 *    |           | /
 *    O-----------+/
 *    |[]  [ ]  []/
 *    +----------/+     y<0
 *              /
 *             /
 *           |/ z>0 (toward the sky)
 *
 *    O: Origin (x=0,y=0,z=0)

还用有趣的符号图形形象的描述了“轴”的概念。然后就是一坨坨的传感器知识,blablabla......


定义了几个结构体:

1) sensors_vec_t 对单个传感器的泛用性封装,包含坐标、角度、状态等信息;

2) sensors_event_t 对传感器细致的数据再封装,包含版本号、传感器类型、数据、加速度、磁力、角度、重力等等等等的信息;

3) sensor_t 对应每个传感器,是都会有的数据,包括传感器名称、版本、handle句柄、类型、最大范围、解析度、耗能、最小延迟等信息;

4) sensors_module_t 对hw_module_t的扩展,不仅有common为hw_module_t,还定义了一个函数get_sensors_list用来获取所支持的传感器,返回值为传感器总数;

5) sensors_poll_device_t 每个传感器所私有的数据操作,包括(反)激活、设置延时、提取数据等动作。


最后定义了两个API函数用来打开/关闭一个传感器。

static inline int sensors_open(const struct hw_module_t* module,
        struct sensors_poll_device_t** device) {
    return module->methods->open(module,
            SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);
}

static inline int sensors_close(struct sensors_poll_device_t* device) {
    return device->common.close(&device->common);
}

2. 服务开启之路(1)

sensor服务由system_server开启:

frameworks/base/cmds/system_server/system_init.cpp

extern "C" status_t system_init()
{
    ALOGI("Entered system_init()");

    sp<ProcessState> proc(ProcessState::self());

    sp<IServiceManager> sm = defaultServiceManager();
    ALOGI("ServiceManager: %p\n", sm.get());

 ...
    }

    property_get("system_init.startsensorservice", propBuf, "1");
    if (strcmp(propBuf, "1") == 0) {
        // Start the sensor service
        SensorService::instantiate();
    }
...
}
调用到frameworks/base/services/sensorservice/SensorService.cpp
void SensorService::onFirstRef()
{
    ALOGD("nuSensorService starting...");

    SensorDevice& dev(SensorDevice::getInstance());
之后又调用到了SensorDevice.cpp(同目录下)

SensorDevice::SensorDevice()
    :  mSensorDevice(0),
       mSensorModule(0)
{
    status_t err = hw_get_module(SENSORS_HARDWARE_MODULE_ID,
            (hw_module_t const**)&mSensorModule);

    ALOGE_IF(err, "couldn't load %s module (%s)",
            SENSORS_HARDWARE_MODULE_ID, strerror(-err));

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

        ALOGE_IF(err, "couldn't open device for module %s (%s)",
                SENSORS_HARDWARE_MODULE_ID, strerror(-err));

        if (mSensorDevice) {
            sensor_t const* list;
            ssize_t count = mSensorModule->get_sensors_list(mSensorModule, &list);

            mActivationCount.setCapacity(count);
            Info model;
            for (size_t i=0 ; i<size_t(count) ; i++) {
                mActivationCount.add(list[i].handle, model);
                mSensorDevice->activate(mSensorDevice, list[
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值