hal stub框架的结构可以总结为321结构, 也就是三个结构体, 两个常量, 一个函数.// path: hardware/libhardware/include/hardware/hardware.h //三个结构体 typedef struct hw_module_t { /** tag must be initialized to HARDWARE_MODULE_TAG */ uint32_t tag; /** Identifier of module */ const char *id; //用于标识module的 /** 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; //一般用于保存所open的库的句柄 } hw_module_t; typedef struct hw_module_methods_t { /** Open a specific device */ int (*open)(const struct hw_module_t* module, const char* id, struct hw_device_t** device); } hw_module_methods_t; typedef struct hw_device_t { /** tag must be initialized to HARDWARE_DEVICE_TAG */ uint32_t tag; uint32_t version; /** reference to the module this device belongs to */ struct hw_module_t* module; /** Close this device */ int (*close)(struct hw_device_t* device); } hw_device_t; //struct hw_module_t用于描述硬件对象的数据结构, 可以通过这个拿到open方法, //然后就能拿到硬件接口struct hw_device_t, 从而来操作硬件
//两个变量
// Name of the hal_module_info
#define HAL_MODULE_INFO_SYM HMI
// Name of the hal_module_info as a string
#define HAL_MODULE_INFO_SYM_AS_STR "HMI"
//一个函数
/* Get the module info associated with a module by id.
* @return: 0 == success, <0 == error and *module == NULL
*/
int hw_get_module(const char *id, const struct hw_module_t **module);
//用户通过id, 调用该函数可以拿到对应的硬件对象
-
以
lights的代码举个例子(关键代码)://path:vendor/mediatek/proprietary/hardware/liblights/light.c /** * module methods */ /** Open a new instance of a lights device using name */ static int open_lights(const struct hw_module_t* module, char const* name, struct hw_device_t** device) { int (*set_light)(struct light_device_t* dev, struct light_state_t const* state); .... set_light = set_light_backlight; ... struct light_device_t *dev = malloc(sizeof(struct light_device_t)); if (!dev) return -ENOMEM; memset(dev, 0, sizeof(*dev)); dev->common.tag = HARDWARE_DEVICE_TAG; dev->common.version = 0; dev->common.module = (struct hw_module_t*)module; dev->common.close = (int (*)(struct hw_device_t*))close_lights; dev->set_light = set_light; *device = (struct hw_device_t*)dev; return 0; } static struct hw_module_methods_t lights_module_methods = { .open = open_lights, }; struct hw_module_t HAL_MODULE_INFO_SYM = { .tag = HARDWARE_MODULE_TAG, //tag必须这样设置 //.version_major = 1, //.version_minor = 0, .id = LIGHTS_HARDWARE_MODULE_ID, .name = "MTK lights Module", .author = "MediaTek", .methods = &lights_module_methods, }; //path: hardware/libhardware/include/hardware/lights.h struct light_device_t { //继承了hw_device_t的结构 struct hw_device_t common; /** * Set the provided lights to the provided values. * Returns: 0 on succes, error code on failure. */ int (*set_light)(struct light_device_t* dev, struct light_state_t const* state); }; //path: vendor/mediatek/proprietary/hardware/liblights/2.0/default/Light.cpp light_device_t* getLightDevice(const char* name) { light_device_t* lightDevice; const hw_module_t* hwModule = NULL; int ret = hw_get_module (LIGHTS_HARDWARE_MODULE_ID, &hwModule); if (ret == 0) { ret = hwModule->methods->open(hwModule, name, reinterpret_cast<hw_device_t**>(&lightDevice)); if (ret != 0) { ALOGE("light_open %s %s failed: %d", LIGHTS_HARDWARE_MODULE_ID, name, ret); } } else { ALOGE("hw_get_module %s %s failed: %d", LIGHTS_HARDWARE_MODULE_ID, name, ret); } if (ret == 0) { return lightDevice; } else { ALOGE("Light passthrough failed to load legacy HAL."); return nullptr; } }通过硬件
id调用hw_get_module函数, 拿到描述该硬件对象的结构体hwModule, 然后再调用这个结构体里的open方法, 也就是open_lights函数, 拿到包含硬件操作set_lights的结构体struct hw_device_t, 这样, 我们就可以访问对象了.open函数功能:- 分配硬件设备结构体
light_device_t, 里面包含硬件操作方法 - 初始化
light_device_t这个结构体成员, 以及扩展的相关方法set_lights - 将
light_device_t中的父结构体struct hw_device_t返回
- 分配硬件设备结构体
set_light方法的具体实现就是通过写节点或ioctl方式去和底层驱动交互了.
- 问题: 为何通过声明结构体
struct hw_module_t HAL_MODULE_INFO_SYM就可以将hal stub注册到系统中, 以及hw_get_module是如何找出这个so的
//path:hardware/libhardware/hardware.c
#define HAL_LIBRARY_PATH1 "/system/lib64/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib64/hw"
#define HAL_LIBRARY_PATH3 "/odm/lib64/hw"
static const char *variant_keys[] = {
"ro.hardware", /* This goes first so that it can pick up a different
file on the emulator. */
"ro.product.board",
"ro.board.platform",
"ro.arch"
};
static const int HAL_VARIANT_KEYS_COUNT =
(sizeof(variant_keys)/sizeof(variant_keys[0]));
/**
* Load the file defined by the variant and if successful
* return the dlopen handle and the hmi.
* @return 0 = success, !0 = failure.
*/
static int load(const char *id,
const char *path,
const struct hw_module_t **pHmi)
{
int status = -EINVAL;
void *handle = NULL;
struct hw_module_t *hmi = NULL;
#ifdef __ANDROID_VNDK__
const bool try_system = false;
#else
const bool try_system = true;
#endif
/*
* load the symbols resolving undefined symbols before
* dlopen returns. Since RTLD_GLOBAL is not or'd in with
* RTLD_NOW the external symbols will not be global
*/
if (try_system &&
strncmp(path, HAL_LIBRARY_PATH1, strlen(HAL_LIBRARY_PATH1)) == 0) {
/* If the library is in system partition, no need to check
* sphal namespace. Open it with dlopen.
*/
handle = dlopen(path, RTLD_NOW);
} else {
handle = android_load_sphal_library(path, RTLD_NOW);
}
if (handle == NULL) {
char const *err_str = dlerror();
ALOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
status = -EINVAL;
goto done;
}
/* Get the address of the struct hal_module_info. */
const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
hmi = (struct hw_module_t *)dlsym(handle, sym);
if (hmi == NULL) {
ALOGE("load: couldn't find symbol %s", sym);
status = -EINVAL;
goto done;
}
/* Check that the id matches */
if (strcmp(id, hmi->id) != 0) {
ALOGE("load: id=%s != hmi->id=%s", id, hmi->id);
status = -EINVAL;
goto done;
}
hmi->dso = handle; //最终将硬件对象返回到了hw_get_module的调用者
/* success */
status = 0;
done:
if (status != 0) {
hmi = NULL;
if (handle != NULL) {
dlclose(handle);
handle = NULL;
}
} else {
ALOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
id, path, *pHmi, handle);
}
*pHmi = hmi;
return status;
}
/*
* Check if a HAL with given name and subname exists, if so return 0, otherwise
* otherwise return negative. On success path will contain the path to the HAL.
*/
static int hw_module_exists(char *path, size_t path_len, const char *name,
const char *subname)
{
snprintf(path, path_len, "%s/%s.%s.so",
HAL_LIBRARY_PATH3, name, subname);
if (access(path, R_OK) == 0)
return 0;
snprintf(path, path_len, "%s/%s.%s.so",
HAL_LIBRARY_PATH2, name, subname);
if (access(path, R_OK) == 0)
return 0;
#ifndef __ANDROID_VNDK__
snprintf(path, path_len, "%s/%s.%s.so",
HAL_LIBRARY_PATH1, name, subname);
if (access(path, R_OK) == 0)
return 0;
#endif
return -ENOENT;
}
int hw_get_module_by_class(const char *class_id, const char *inst,
const struct hw_module_t **module)
{
int i = 0;
char prop[PATH_MAX] = {0};
char path[PATH_MAX] = {0};
char name[PATH_MAX] = {0};
char prop_name[PATH_MAX] = {0};
if (inst)
snprintf(name, PATH_MAX, "%s.%s", class_id, inst);//name= "lights"
else
strlcpy(name, class_id, PATH_MAX);
/*
* Here we rely on the fact that calling dlopen multiple times on
* the same .so will simply increment a refcount (and not load
* a new copy of the library).
* We also assume that dlopen() is thread-safe.
*/
/* First try a property specific to the class and possibly instance */
snprintf(prop_name, sizeof(prop_name), "ro.hardware.%s", name); //prop_name = ro.hardware.lights
if (property_get(prop_name, prop, NULL) > 0) {
if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
goto found;
}
}
/* Loop through the configuration variants looking for a module */
for (i=0 ; i<HAL_VARIANT_KEYS_COUNT; i++) {
if (property_get(variant_keys[i], prop, NULL) == 0) {
continue;
}
if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
goto found;
}
}
/* Nothing found, try the default */
if (hw_module_exists(path, sizeof(path), name, "default") == 0) {
goto found;
}
return -ENOENT;
found:
/* load the module, if this fails, we're doomed, and we should not try
* to load a different variant. */
return load(class_id, path, module);
}
int hw_get_module(const char *id, const struct hw_module_t **module)
{
return hw_get_module_by_class(id, NULL, module);
}
整体框架如下所示:
调用流程:
上层framework通过JNI调用接口hw_get_module(hardware/libhardware/hardware.c)找到传入ID所对应的so, 拿到对应的硬件模块. 然后调用对应的接口, 在内核实现相应接口的功能.
我们查看vendor/mediatek/proprietary/hardware/liblights/Android.mk可知, 前面的light.c文件(也就是硬件描述对象的结构体), 会被编译为lights.mt8168.so, 所以想要找到硬件对象, 需要先找到对象的动态库, 再去拿相应的硬件接口.
声明结构体struct hw_module_t时, 统一命名为HAL_MODULE_INFO_SYM也就是HMI, 为的就是可以通过dlsym去查找模块中HAL Stub源码生成的so里面的HMI符号. 获得符号在库中的地址. 使用这个地址,就可以获得库中特定函数的指针(也就是open方法),并且调用装载库中的相应函数.