当前位置:操作系统 > 安卓/Android >>

Android架构分析之硬件抽象层(HAL)

Android版本:2.3.7_r1

Linux内核版本:android-goldfish-2.6.29

 

一、硬件抽象层核心数据结构

Android硬件抽象层有三个核心数据结构,分别是hw_module_t , hw_module_methods_t, hw_device_t。定义在hardware/libhardware/include/hardware/hardware.h文件中:

[cpp]
40/**
 41 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
 42 * and the fields of this data structure must begin with hw_module_t
 43 * followed by module specific information.
 44 */ 
 45typedef struct hw_module_t { 
 46    /** tag must be initialized to HARDWARE_MODULE_TAG */ 
 47    uint32_t tag; 
 48 
 49    /** major version number for the module */ 
 50    uint16_t version_major; 
 51 
 52    /** minor version number of the module */ 
 53    uint16_t version_minor; 
 54 
 55    /** Identifier of module */ 
 56    const char *id; 
 57 
 58    /** Name of this module */ 
 59    const char *name; 
 60 
 61    /** Author/owner/implementor of the module */ 
 62    const char *author; 
 63 
 64    /** Modules methods */ 
 65    struct hw_module_methods_t* methods; 
 66 
 67    /** module's dso */ 
 68    void* dso; 
 69 
 70    /** padding to 128 bytes, reserved for future use */ 
 71    uint32_t reserved[32-7]; 
 72 
 73} hw_module_t; 
 74 
 75typedef struct hw_module_methods_t { 
 76    /** Open a specific device */ 
 77    int (*open)(const struct hw_module_t* module, const char* id, 
 78            struct hw_device_t** device); 
 79 
 80} hw_module_methods_t; 
 81 
 82/**
 83 * Every device data structure must begin with hw_device_t
 84 * followed by module specific public methods and attributes.
 85 */ 
 86typedef struct hw_device_t { 
 87    /** tag must be initialized to HARDWARE_DEVICE_TAG */ 
 88    uint32_t tag; 
 89 
 90    /** version number for hw_device_t */ 
 91    uint32_t version; 
 92 
 93    /** reference to the module this device belongs to */ 
 94    struct hw_module_t* module; 
 95 
 96    /** padding reserved for future use */ 
 97    uint32_t reserved[12]; 
 98 
 99    /** Close this device */ 
100    int (*close)(struct hw_device_t* device); 
101 
102} hw_device_t; 

 40/**
 41 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
 42 * and the fields of this data structure must begin with hw_module_t
 43 * followed by module specific information.
 44 */
 45typedef struct hw_module_t {
 46    /** tag must be initialized to HARDWARE_MODULE_TAG */
 47    uint32_t tag;
 48
 49    /** major version number for the module */
 50    uint16_t version_major;
 51
 52    /** minor version number of the module */
 53    uint16_t version_minor;
 54
 55    /** Identifier of module */
 56    const char *id;
 57
 58    /** Name of this module */
 59    const char *name;
 60
 61    /** Author/owner/implementor of the module */
 62    const char *author;
 63
 64    /** Modules methods */
 65    struct hw_module_methods_t* methods;
 66
 67    /** module's dso */
 68    void* dso;
 69
 70    /** padding to 128 bytes, reserved for future use */
 71    uint32_t reserved[32-7];
 72
 73} hw_module_t;
 74
 75typedef struct hw_module_methods_t {
 76    /** Open a specific device */
 77    int (*open)(const struct hw_module_t* module, const char* id,
 78            struct hw_device_t** device);
 79
 80} hw_module_methods_t;
 81
 82/**
 83 * Every device data structure must begin with hw_device_t
 84 * followed by module specific public methods and attributes.
 85 */
 86typedef struct hw_device_t {
 87    /** tag must be initialized to HARDWARE_DEVICE_TAG */
 88    uint32_t tag;
 89
 90    /** version number for hw_device_t */
 91    uint32_t version;
 92
 93    /** reference to the module this device belongs to */
 94    struct hw_module_t* module;
 95
 96    /** padding reserved for future use */
 97    uint32_t reserved[12];
 98
 99    /** Close this device */
100    int (*close)(struct hw_device_t* device);
101
102} hw_device_t;
40-44行,注意这段说明文字,硬件抽象层HAL由一个一个的模块组成,Android规定,每一个模块都是一个命名为HAL_MODULE_INFO_SYM的自定义结构体,并且该结构体的第一个成员必须为hw_module_t类型的变量,其它成员变量根据需要由开发者设置。

82-85行,注意这段说明文字,每个设备对应一个自定义结构体,该结构体的第一个成员必须为hw_device_t,其它成员根据需要由开发者设置。

例如,sensor模块对应的结构体定义在hardware/libhardware/include/hardware/sensors.h文件中:

[cpp]
?344/**
345 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
346 * and the fields of this data structure must begin with hw_module_t
347 * followed by module specific information.
348 */ 
349struct sensors_module_t { 
350    struct hw_module_t common; 
351 
352    /**
353     * Enumerate all available sensors. The list is returned in "list".
354     * @return number of sensors in the list
355     */ 
356    int (*get_sensors_list)(struct sensors_module_t* module, 
357            struct sensor_t const** list); 
358}; 
sensor设备对应的结构体如下: 
392/**
393 * Every device data s

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,