4 硬件抽象层代码分析
LightsService的HAL(硬件抽象层)物理逻辑代码主要位于hardware\qcom\display\liblight目录下的lights.c以及
hardware\libhardware\include\hardware目录下的hardware.h和lights.h。
4.1 hardware定义
hardware.h定义了Android系统中通用的硬件设备信息,主要包括三个结构体以及用于确定软件版本信息相关的宏定义。以下是三个结构体:
struct hw_module_t;
struct hw_module_methods_t;
struct hw_device_t;
其中hw_module_t的定义如下:
typedef struct hw_module_t {
uint32_t tag;
uint16_t module_api_version;
uint16_t hal_api_version;
const char *id;
const char *name;
const char *author;
/** Modules methods */
struct hw_module_methods_t* methods;
...
} hw_module_t;
这个定义中包含了设备的id号码,设备名称,设备所属用户。比较重要的是还要包含一个函数指针结构体,
这个函数指针用来传递一个open函数,用于打开指定的设备。定义如下:
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;
open函数在JNI方法初始化的时候会调用,参见JNI中的get_device函数。
在使用hw_module_t结构体定义设备时要求每个设备定义都必须包含一个名为HAL_MODULE_INFO_SYM的该结构体,它负责为各属性赋值。
hw_device_t的定义如下:
typedef struct hw_device_t {
uint32_t tag;
uint32_t version;
/** reference to the module this device belongs to */
struct hw_module_t* module;
/** Close this device */
int (*close)(struct hw_device_t* device);
} hw_device_t;
hw_device_t引用了一个hw_module_t指针,并且定义了一个close方法,用于关闭设备Android系统中要求每一个设备信息
都必须首先引用hw_device_t结构体信息,然后才定义自己的公共方法和属性。这类似于面向对象中的继承关系,需要首先继承
自父类的特性,然后在扩展自己的特性。这三个结构体如果以面向对象的关系来看,它们之间的关系如图4-1所示