1) libhardware_legacy
是将 .so 文件当作shared library来使用,在runtime(JNI 部份)以 direct function call 使用 HAL module。通过直接函数调用的方式,来操作驱动程序。当然,应用程序也可以不需要通过 JNI 的方式进行,直接加载 .so (dlopen)的做法调用*.so 里的符号(symbol)也是一种方式。总而言之是没有经过封装,上层可以直接操作硬件。
2) libhardware
HAL stub 是一种代理人(proxy)的概念,stub 虽然仍是以 .so檔的形式存在,但HAL已经将 .so 档隐藏起来了。
这些硬件模块都编译成xxx.xxx.so,目标位置为/system/lib/hw目录
上层仅仅链接libhardware.so, 调用hw_get_modules 获取相应的模块,加载相应的库。
1. HAL 硬件抽象层规范
HAL_MODULE_INFO_SYM
hw_module_t
HARDWARE_MODULE_TAG
1) 所有的hal模块都要有一个以HAL_MODULE_INFO_SYM命名的结构,而且这个结构要以hw_module_t开始,即要继承hw_module_t这个结构,比如sensor:
struct hw_module_t HAL_MODULE_INFO_SYM={};
2) 实例变量名必须为HAL_MODULE_INFO_SYM,tag也必须为HARDWARE_MODULE_TAG.
struct sensors_module_t {
struct hw_module_t common;
int (*get_sensors_list)(struct sensors_module_t* module,
struct sensor_t const** list);
};
typedef struct hw_module_t {
/** tag must be initialized to HARDWARE_MODULE_TAG */
uint32_t tag;
/** major version number for the module */
uint16_t version_major;
/** minor version number of the module */
uint16_t version_minor;
/** Identifier of module */
const char *id;
/** Name of this module */
const char *name;
/** Author/owner/implementor of the module */
const char *author;
/** Modules methods */
struct hw_module_methods_t* methods; //硬件模块的方法
/** module's dso */
void* dso;
/** padding to 128 bytes, reserved for future use */
uint32_t reserved[