硬件厂商处于保护核心代码,会将核心实现以so库的形式出现在HAL层,当需要时HAL会自动调用相关的共享库。
共享库的格式
<MODULE_ID>.variant.so
- id: 为硬件模块的唯一编号
- variant:为变种名称。这个值从系统属性中获取。获取顺序保存在variant_keys数组中。
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"
};
流程
app通过jni调用hal层hw_get_module函数获取硬件模块,hw_get_module通过模块ID查询对应的模块的共享库,调用load打开共享库,获取硬件结构地址,根据固定符号HAL_MODULE_INFO_SYM查找结构体hw_module_t,调用hw_module_methods_t中的open方法打开硬件。在open时传入hw_device_t二级指针,将模块的操作函数保存在hw_device_t中,实现与硬件的交互。
hw_get_module
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);
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);
if (property_get(prop_name, prop, NULL) > 0) { //??获取属相
if (hw_module_exists(path, sizeof(path), name, prop) == 0) { //检测模块是否存在
goto found;
}
}
//在hal层搜索动态共享库的方式
/* 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); 装载库,得到module
}
//根据硬件id,获取id对应的硬件模块结构体
int hw_get_module(const char *id, const struct hw_module_t **module)
{
//id将作为路径一部分,下面代码的name
return hw_get_module_by_class(id, NULL, module);
}
- hw_get_module 调用hw_get_module_by_class完成加载过程
- hw_get_module_by_class根据传入的变量class_id,查询ro.hardware.<id>获取属相值,如果存在作为variant值,调用hw_module_exit检查目标共享库是否存在,存在调用load加载
- 不存在,循环遍历variant_keys数组定义的key获取对应的属性值,并判断是否存在对应的共享库,存在调用load加载,否则返回错误
hw_module_exists方法根据拼接的路径path,去查询是否存在共享库。共享库的路径由HAL_LIBRARY_PATH1(系统存放路径) ,id(moudle ID),variant(属性)组成。
共享库存放的路径
#if defined(__LP64__)
#define HAL_LIBRARY_PATH1 "/system/lib64/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib64/hw"
#else
#define HAL_LIBRARY_PATH1 "/system/lib/hw"
#define HAL_LIBRARY_PATH2 "/vendor/lib/hw"
#endif
- 共享库存放的位置位于 /system/lib64/hw,/system/lib/hw 和/vendor/lib64/hw ,/vendor/lib/hw 路径下
- 共享库以<MODULE_ID>.variant.so命名,id为模块名称,variant为变种名称,随系统平台变化。
load
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;
/*
* 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
*/
//调用dlopen打开path定义的目标共享库,得到库文件的句柄handle
handle = dlopen(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;
//调用dlsym获取符号HAL_MODULE_INFO_SYM_AS_STR的地址,赋值给hmi
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;
/* 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);
}
//返回得到的hw_module_t结构体的指针
*pHmi = hmi;
return status;
}
- load 函数调用dlopen打开目标模块共享库,使用dlsym获取HMI地址(HMI为模块hw_module_t结构体的名字),得到对应模块hw_module_t指针;通过hw_module_t指针,可以对硬件进行操作。
通过dlopen和dlsym获取模块hw_module_t指针,操作硬件。
蓝牙HAL加载
硬件抽象层中每个模块必须自定义一个硬件抽象层模块结构,第一个成员必须是hw_module_t,其次才是模块的一此相关信息;还必须包含HAL_MODULE_INFO_SYM 。
蓝牙HAL加载在 com_android_bluetooth_btservice_AdapterService.cpp的classInitNative方法中,
static void classInitNative(JNIEnv* env, jclass clazz) {
int err;
hw_module_t* module;
// ........中间省略.....
char value[PROPERTY_VALUE_MAX];
property_get("bluetooth.mock_stack", value, "");
const char *id = (strcmp(value, "1")? BT_STACK_MODULE_ID : BT_STACK_TEST_MODULE_ID);
//获取蓝牙模块hw_module_t指针
err = hw_get_module(id, (hw_module_t const**)&module);
if (err == 0) {
hw_device_t* abstraction;
//调用open方法,获取蓝牙设备hw_device_t指针
err = module->methods->open(module, id, &abstraction);
if (err == 0) {
//蓝牙实现中将蓝牙设备bluetooth_device_t 和 bluetooth_module_t 定义成同一个值
bluetooth_module_t* btStack = (bluetooth_module_t *)abstraction;
//获取蓝牙模块interface接口,通过sBluetoothInterface操作蓝牙设备
sBluetoothInterface = btStack->get_bluetooth_interface();
} else {
ALOGE("Error while opening Bluetooth library");
}
} else {
ALOGE("No Bluetooth Library found");
}
}
BT_STACK_MODULE_ID 定义在bluetooth.h文件中,
#define BT_HARDWARE_MODULE_ID "bluetooth"
#define BT_STACK_MODULE_ID "bluetooth"
hw_get_module获取蓝牙模块hw_module_t指针,
蓝牙自定义硬件模块hw_module_t定义
struct hw_module_t HAL_MODULE_INFO_SYM = {
.tag = HARDWARE_MODULE_TAG,
.version_major = 1,
.version_minor = 0,
.id = BT_HARDWARE_MODULE_ID,
.name = "Bluetooth Stack",
.author = "The Android Open Source Project",
.methods = &bt_stack_module_methods
};
蓝牙调用自定义模块hw_module_t中open方法,获取hw_device_t指针,
static struct hw_module_methods_t bt_stack_module_methods = {
.open = open_bluetooth_stack,
};
static int open_bluetooth_stack(const struct hw_module_t *module, UNUSED_ATTR char const *name, struct hw_device_t **abstraction) {
//给bluetooth_device_t赋值
static bluetooth_device_t device = {
//common变量赋值
.common = {
.tag = HARDWARE_DEVICE_TAG,
.version = 0,
.close = close_bluetooth_stack,
},
//获取蓝牙模块接口
.get_bluetooth_interface = bluetooth__get_bluetooth_interface
};
device.common.module = (struct hw_module_t *)module;
//将bluetooth_device_t指针强制转换为hw_device_t指针赋值给abstraction
*abstraction = (struct hw_device_t *)&device;
return 0;
}
open_bluetooth_stack创建bluetooth_device_t 结构体,初始化,转换成hw_device_t。module->methods->open得到bluetooth_device_t 结构体的指针,也是bluetooth_module_t结构体的指针。
typedef struct {
struct hw_device_t common;
const bt_interface_t* (*get_bluetooth_interface)();
} bluetooth_device_t;
typedef bluetooth_device_t bluetooth_module_t;
bluetooth_device_t 包含hw_device_t;bluetooth_device_t 重命名为bluetooth_module_t。
open方法(open_bluetooth_stack)获取蓝牙设备hw_device_t指针,转化为bluetooth_module_t 指针,在open_bluetooth_stack中将bluetooth_get_bluetooth_interface赋值给get_bluetooth_interface,再赋值给sBluetoothInterface。
bluetooth_get_bluetooth_interface定义了蓝牙模块的基本接口,bluetooth_get_bluetooth_interface最后赋值给sBluetoothInterface,调用sBluetoothInterface可以操作蓝牙模块,与硬件交互。具体接口实现和平台相关。