当前位置:操作系统 > 安卓/Android >>
Android模拟器学framework和driver之传感器篇3(Android HAL)
前面,我带你一起写了一个temperature sensor驱动已经测试过了tool测试此驱动程序的基本功能ok,如果有问题,可以参考前两篇文章,这里我们要在这里HAL将我们的设备添加到层中,跟随framework连接中间的代码。
在开始摆代码之前,我觉得有必要啰嗦几句。HAL我个人认为这是一件更重要的事情,虽然这里不是很成熟,不是很标准,但是google我还是做了很大的努力HAL的。
先介绍一下android HAL 层,
Android简单来说,硬件抽象层是对的Linux包装内核驱动程序,向上提供界面,屏蔽低层实现细节。也就是说,硬件支持分为两层,一层放在用户空间(User Space),一层放在核心空间(Kernel Space),硬件抽象层在用户空间中运行,Linux内核驱动程序运行在内核空间。
下图为整个android 架构:
这个是google定义的android你可以看到整个系统的架构HAL层起承上启下的作用,HAL层主要存在于一些传感器和一些硬件中,也可以绕过HAL层直接使用JNI实现从驱动到framework但我们这里主要讲的是这个过程android中的sensor,针对标准的android而言,sensor都是在HAL添加,个人理解或呼应硬件制造商的建议,毕竟,这部分代码不能开源。
嗯,我不在乎开源不开源,对我影响不大。我们继续看HAL。
Android从下到上涉及硬件抽象层Android硬件驱动层、硬件抽象层、运行时库、应用程序框架层等。下图描述了硬件抽象层Android系统中的位置及其与其他层的关系:
现在的libhardware 使用了方法stub的概念。stub 虽然仍是以*.so 但是HAL 已经将*.so 文件隐藏起来。Stub 向HAL提供操作函数(operations),而runtime 则是向HAL 获取特定模块(stub)的operations,再callback 这些操作函数。
HAL实现主要在hardware.c和hardware.h在文件中。本质也是通过加载的*.so 从而呼叫*.so 里的符号(symbol)实现。
--------------------------------------------------------------------------------------------------------------------------------------------------------------
到目前为止,都是理论上的东西。我真的不想谈论这些问题。我从小就不擅长中文。我最讨厌的是这些理论上的东西。实践是真理。
好了接下来轮到我们的sensor的HAL层,这里首先列出主要使用的代码:
。。。
漏了,这边sensor的HAL我参考的是层代码freescale的BSP,因为个人感觉更容易理解,哈哈。
先下载链接,否则你看不到完整的代码。
http://download.csdn.net/detail/zhangjie201412/4039312
代码目录如下:
sensor/
├──AccelSensor.cpp
├──AccelSensor.h
├──Android.mk
├──InputEventReader.cpp
├──InputEventReader.h
├──LightSensor.cpp
├──LightSensor.h
├──SensorBase.cpp
├──SensorBase.h
├──sensors.cpp
├──sensors.h
├──TemperatureSensor.cpp
└──TemperatureSensor.h
在这里,我们需要修改和添加三个文件:
sensor.cpp TemperatureSensor.cpp TemperatureSensor.h
好吧,接下来分析一下。android sensor的HAL层!!!
先看一下/hardware/libhareware/hardware.c和/hardware/libhardware/include/hardware/hardware.h
这2个文件是android hal定义了两个层最重要的文件hal层主要实现三个结构体,
struct hw_module_t;
struct hw_module_methods_t;
struct hw_device_t;
/**
* Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
* and the fields of this data structure must begin with hw_module_t
* followed by module specific information.
*/
typedef struct hw_module_t {
/** tag must be initialized to HARDWARE_MODULE_TAG */
uint32_t tag;
/** major version number for the module */
uint16_t version_major;
/** minor version number of the module */
uint16_t version_minor;
/** Identifier of module */
const char *id;
/** Name of this module */
const char *name;
/** Author/owner/implementor of the module */
const char *author;
/** Modules methods */
struct hw_module_methods_t* methods;
/** module's dso */
void* dso;
/** padding to 128 bytes, reserved for future use */
uint32_t reserved[32-7];
} hw_module_t;
typedef struct hw_module_methods_t {
/** Open a specific device */
int (*open)(const struct hw_module_t* module, const char* id,
struct hw_device_t** device);
} hw_module_methods_t;
/**
* Every device data structure must begin with hw_device_t
* followed by module specific public methods and attributes.
*/
typedef struct hw_device_t {
/** tag must be initialized to HARDWARE_DEVICE_TAG */
uint32_t tag;
/** version number for hw_device_t */
uint32_t version;
/** reference to the module this device belongs to */
struct hw_module_t* module;
/** padding reserved for future use */
uint32_t reserved[12];
/** Close this device */
int (*close)(struct hw_device_t* device);
} hw_device_t;
struct hw_module_t;
struct hw_module_methods_t;
struct hw_device_t;
/**
* Every hardware module must have a data structure named HAL_MODUE_INFO_SYM
* and the fields of this data structure must begin with hw_module_t
* followed by module specific information.
*/
typedef struct hw_module_t {
/** tag must be initialized to HARDWARE_MODULE_TAG */
uint32_t tag;
/** major version number for the module */
uint16_t version_major;
/** minor version number of the module */
uint16_t version_minor;
/** Identifier of module */
const char *id;
/** Name of this module */
const char *name;
/** Author/owner/implementor of the module */
const char *author;
/** Modules methods */
struct hw_module_methods_t* methods;
/** module's dso */
void* dso;
/** padding to 128 bytes, reserved for futu补充:移动开发 , Android ,