Android HAL

Hardware Abstraction Layer

HAL即Hardware Abstraction Layer,为硬件供应商定义了一系列标准接口来实现,这使得Android底层驱动程序实现是不可知的。使用HAL可以在不影响或修改系统上层的情况下实现功能。HAL实现被打包到模块中,并在适当的时候由Android系统加载。

HAL 的组成

特定的产品硬件需要实现相应的HAL(和驱动)。HAL的实现通常会被编译成共享库(.so文件)的形式,但是由于Android对于HAL的实现和设备驱动程序之间并没有强制的标准交互协议,所以你可以灵活的做适合自己的最好的系统。然而,为了使Android系统能够正确地与硬件交互,您必须遵守在每个硬件特定HAL接口中定义的协议。
为了保证HAL有一个可预测的结构,每个特定于硬件都会在下面的文件中定义一些属性。

hardware/libhardware/include/hardware/hardware.h. 

This interface allows the Android system to load correct versions of your HAL modules in a consistent way. A HAL interface consists of two components: modules and devices.
(这个接口允许Android系统以一致的方式加载您的HAL模块的正确版本。HAL接口由两个组件组成:模块和设备。)

HAL modules

一个模块的HAL实现,其实就是一个共享库.so文件。
hardware/libhardware/include/hardware/hardware.h 头文件定义的结构体(hw_module_t) 代表一个模块,包含数据,如模块的版本、名称和创建者。Android使用这些数据来正确地查找和加载HAL模块。

此外,hw_module_t 结构体包含一个指向另一个结构体的指针,hw_module_methods_t,它包含一个指向对这个模块开放的函数的指针。

这个开放的函数用于初始化HAL与硬件的通信。

每个特定于硬件的HAL通常会扩展通用的hw_module_t 结构体,并为特定的硬件提供额外的信息。例如,在相机的HAL中,相机的结构体camera_module_t 包含一个hw_module_t结构体以及其他特定于相机的功能指针:

typedef struct camera_module {
    hw_module_t common;
    int (*get_number_of_cameras)(void);
    int (*get_camera_info)(int camera_id, struct camera_info *info);
} camera_module_t;

当你想要实现一个模块的HAL并创建该模块的结构体,你必须把它命名为 HAL_MODULE_INFO_SYM.

Nexus 9 audio HAL的例子:

struct audio_module HAL_MODULE_INFO_SYM = {
    .common = {
        .tag = HARDWARE_MODULE_TAG,
        .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
        .hal_api_version = HARDWARE_HAL_API_VERSION,
        .id = AUDIO_HARDWARE_MODULE_ID,
        .name = "NVIDIA Tegra Audio HAL",
        .author = "The Android Open Source Project",
        .methods = &hal_module_methods,
    },
};

HAL devices

设备对产品的硬件进行抽象。例如,一个音频模块可以包含一个primary audio device,一个USB audio device,或者一个Bluetooth A2DP audio device。
结构体hw_device_t表示一个设备。
与模块类似,每种类型的设备都定义了通用的hwdevicet的详细版本,其中包含了针对硬件的特定功能的函数指针。
例如,结构体audio_hw_device_t 包含对音频设备操作的函数指针:

struct audio_hw_device {
    struct hw_device_t common;

    /**
     * used by audio flinger to enumerate what devices are supported by
     * each audio_hw_device implementation.
     *
     * Return value is a bitmask of 1 or more values of audio_devices_t
     */
    uint32_t (*get_supported_devices)(const struct audio_hw_device *dev);
  ...
};
typedef struct audio_hw_device audio_hw_device_t;

除了这些标准属性之外,每个特定硬件的HAL接口都可以定义更多的特性和需求。

编译HAL 模块

HAL的实现被编译为so库,在android系统中以动态链接的方式使用。
创建 Android.mk来编译HAL模块,库的命名形式 :

<module_type>.<device_name>.

HAL Types

Android O 对 Android系统底层进行了重构,为了更好的支持,运行在Android O 上的设备必须支持binderized HALs或者passthrough HALs

Binderized HALs

HALs用HAL接口定义语言(HIDL)表示。
这些HALs取代了早期版本的Android所使用传统的HALs和 legacy HALs;可以以binderized 模式服务于HAL。
所有Android O或之后的设备必须只支持binderized HALs。

Passthrough HALs

A HIDL-wrapped conventional or legacy HAL. These HALs wrap existing HALs and can serve the HAL in binderized and same-process (passthrough) modes.
升级到AndroidO系统的设备可以使用passthrough HALs。

NOTE: Android provides the following HIDL interfaces which will always be in binderized mode: android.frameworks.*, android.system.* , and android.hidl.* (except for android.hidl.memory@1.0 as described below).

Same-Process HALs

Same-Process HALs (SP-HALs) always open in the same process in which they are used. They include all HALs not expressed in HIDL as well as some that are not binderized. Membership in the SP-HAL set is controlled only by Google, with no exceptions.

SP-HALs include the following:

openGL
Vulkan
android.hidl.memory@1.0 (provided by the Android system, always passthrough)
android.hardware.graphics.mapper@1.0.
android.hardware.renderscript@1.0

Conventional & legacy HALs

Conventional HALs (deprecated in Android O ) are interfaces that conform to a specific named and versioned application binary interface (ABI). The bulk of Android system interfaces (camera, audio, sensors, etc.) are in the form of conventional HALs, which are defined under hardware/libhardware/include/hardware.

Legacy HALs (also deprecated in Android O) are interfaces that predate conventional HALs. A few important subsystems (Wi-Fi, Radio Interface Layer, and Bluetooth) are legacy HALs. While there’s no uniform or standardized way to describe a legacy HAL, anything predating Android O that is not a conventional HAL is a legacy HAL. Parts of some legacy HALs are contained in libhardware_legacy, while other parts are interspersed throughout the codebase.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值