Android HAL层简要分析

本文深入探讨了Android硬件抽象层(HAL)的新框架,重点关注Hal Stub的结构体,包括hw_module_methods_t和hw_device_t。通过分析,揭示了如何通过dlopen和dlsym从SO库中获取硬件对象,以及HAL Stub代码生成的so库命名和位置规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新的HAL框架

新的框架使用的是module stub方式,stub在Binder机制中表示对象引用。应用层加载so库(so库代码我们称之为module),在HAL层注册了每个硬件对象的引用stub,当上层需要访问硬件的时候,就从当前注册的硬件引用对象stub里查找,找到之后stub会向上层module提供该硬件对象的操作接口,该操作接口就保存在module中,应用层再通过这个module操作接口来访问硬件。框架图如下:


Hal Stub框架分析

Hal Stub的框架比较简单,主要记住三个结构体、两个常量、一个函数,简称321架构,定义在:
/hardware/libhardware/include/hardware/hardware.h;
/hardware/libhardware/hardware.c。

三个结构体

struct hw_module_t;
struct hw_module_methods_t;
struct hw_device_t;

先看下hw_modult_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.每个硬件模块必须有一个以HAL_MODULE_INFO_SYM命名的数据结构;数据结构必须以hw_module_t开始,用来指定module信息。
 */
typedef struct hw_module_t {
    /** tag must be initialized to HARDWARE_MODULE_TAG */
    uint32_t tag;//标签必须初始化为HARDWARE_MODULT_TAG

    /**
     * The API version of the implemented module. The module owner is
     * responsible for updating the version when a module interface has
     * changed.
     *
     * The derived modules such as gralloc and audio own and manage this field.
     * The module user must interpret the version field to decide whether or
     * not to inter-operate with the supplied module implementation.
     * For example, SurfaceFlinger is responsible for making sure that
     * it knows how to manage different versions of the gralloc-module API,
     * and AudioFlinger must know how to do the same for audio-module API.
     *
     * The module API version should include a major and a minor component.
     * For example, version 1.0 could be represented as 0x0100. This format
     * implies that versions 0x0100-0x01ff are all API-compatible.
     *
     * In the future, libhardware will expose a hw_get_module_version()
     * (or equivalent) function that will take minimum/maximum supported
     * versions as arguments and would be able to reject modules with
     *
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值