新的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;
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
*