mtk java模块_mtk平台hw_get_module分析

google为了保护硬件厂商的信息,在android中添加了一层HAL层。查看HAL的编写方法的过程中,发现整个模块没有一个入口。一般应用程序有main函数,可以让加载器进行加载执行,而对于动态链接库,我们可以对库中导出的任何符号进行调用。

android中的HAL需要上层的函数对其进行加载调用,android的HAL加载器是如何实现对不同的Hardware Module进行通用性的调用的呢?

查看源码的过程中发现android中实现调用HAL是通过hw_get_module实现的。

int hw_get_module(const char *id, const struct hw_module_t **module)

{

return hw_get_module_by_class(id, NULL, module);

}

这是其函数原型,id会指定Hardware的id,这是一个字符串,比如我们比较熟悉的sensor的id是#define SENSORS_HARDWARE_MODULE_ID "sensors",如果找到了对应的hw_module_t结构体,会将其指针放入*module中。看看它的实现。

int hw_get_module_by_class(const char *class_id, const char *inst,

const struct hw_module_t **module)

{

int i;

char prop[PATH_MAX];

char path[PATH_MAX];

char name[PATH_MAX];

char prop_name[PATH_MAX];

if (inst)

snprintf(name, PATH_MAX, "%s.%s", class_id, inst);

else

strlcpy(name, class_id, PATH_MAX);//拷贝hardware id,name为sensor

/*

* 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);//先尝试获取ro.hardware.sensor的属性值

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

if (property_get(variant_keys[i], prop, NULL) == 0) {//获取数组variant_keys里面的属性值

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) {//这里默认加载sensor.default.so

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);//打开动态链接库

}

hw_module_exists如下:

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",//查找动态链接库的两个路径   vendor/lib/hw/   system/lib/hw

HAL_LIBRARY_PATH2, name, subname);

if (access(path, R_OK) == 0)

return 0;

snprintf(path, path_len, "%s/%s.%s.so",

HAL_LIBRARY_PATH1, name, subname);

if (access(path, R_OK) == 0)

return 0;

return -ENOENT;

}

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",

"ro.btstack"

};

上述代码主要是获取动态链接库的路径,并调用load函数去打开指定路径下的库文件,load函数是关键所在。好,那我们就来解开load函数的神秘面纱!!!

static int load(const char *id,

const char *path,

const struct hw_module_t **pHmi)

{

int status;

void *handle;

struct hw_module_t *hmi;

/*

* 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

*/

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;//这个变量很重要,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);

}

*pHmi = hmi;//返回找到的模块结构,赋值给module

return status;

}

HAL_MODULE_INFO_SYM_AS_STR这个宏很重要:

/**

* Name of the hal_module_info as a string

*/

#define HAL_MODULE_INFO_SYM_AS_STR  "HMI"

其中 hmi = (struct hw_module_t *)dlsym(handle, sym);这里是查找“HMI”这个导出符号,并获取其地址。为什么根据“HMI”这个导出符号,就可以从动态链接库中找到结构体hw_module_t呢?

其实,我们看一下动态链接库的格式:

ELF头在文件的开始,保存了路线图,描述了该文件的组织情况。sections保存着object 文件的信息,从连接角度看:包括指令,数据,符号表,重定位信息等等

所以说,我们可以使用readelf命令去查看相应的符号信息,就一目了然了!

365行我们发现,名字就是“HMI”,对应于hw_module_t结构体。再去对照一下HAL的代码。

struct sensors_module_t HAL_MODULE_INFO_SYM = {

.common = {

.tag = HARDWARE_MODULE_TAG,

.version_major = 1,

.version_minor = 0,

.id = SENSORS_HARDWARE_MODULE_ID,

.name = "MTK SENSORS Module",

.author = "Mediatek",

.methods = &sensors_module_methods,

},

.get_sensors_list = sensors__get_sensors_list,

};

这里定义了一个名为HAL_MODULE_INFO_SYM的sensors_module_t的结构体,common成员为hw_module_t类型。注意这里的HAL_MODULE_INFO_SYM变量必须为这个名字,这样编译器才会将这个结构体的导出符号变为“HMI”,这样这个结构体才能被dlsym函数找到!

如此,我们知道了andriod HAL模块也有一个通用的入口地址,这个入口地址就是HAL_MODULE_INFO_SYM变量,通过它,我们可以访问到HAL模块中的所有想要外部访问到的方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值