Android HAL是如何被调用的

Android对硬件的调用,google推荐使用HAL的方式进行调用,对于Andriod HAL的写法,可以参考android源码里的hardware目录下几个模块的模版。

在看HAL的编写方法的过程中,会发现整个模块貌似没有一个入口。一般说来模块都要有个入口,比如应用程序有main函数,可以为加载器进行加载执行,dll文件有dllmain,而对于我们自己写的动态链接库,我们可以对库中导出的任何符号进行调用。问题来了,Android中的HAL是比较具有通用性的,需要上层的函数对其进行加载调用,Android的HAL加载器是如何实现对不同的Hardware Module进行通用性的调用的呢?

带着这个疑问查看Android源码,会发现Android中实现调用HAL是通过hw_get_module实现的。inthw_get_module(const char *id, const struct hw_module_t **module);这是其函数原型,id会指定Hardware的id,这是一个字符串,比如sensor的id是#define SENSORS_HARDWARE_MODULE_ID "sensors",如果找到了对应的hw_module_t结构体,会将其指针放入*module中。看看它的实现。。。。  

/* Loop through the configuration variants looking for amodule */

for (i=0 ; i<HAL_VARIANT_KEYS_COUNT+1 ; i++) {

if (i < HAL_VARIANT_KEYS_COUNT) {

//获取ro.hardware/ro.product.board/ro.board.platform/ro.arch等key的值。

 if(property_get(variant_keys[i], prop, NULL) == 0) {

  continue;

   }

snprintf(path, sizeof(path), "%s/%s.%s.so",HAL_LIBRARY_PATH,id, prop);

 //如果开发板mmdroid,那么这里的path是 system/lib/hw/sensor.mmdroid.so

 } else {

 snprintf(path,sizeof(path), "%s/%s.default.so",

 HAL_LIBRARY_PATH,id);//默认会加载/system/lib/hw/sensor.default.so

}

if (access(path, R_OK)) {

 continue;

}

/* we found a library matching this id/variant */

 break; }    

status = -ENOENT;

 if (i <HAL_VARIANT_KEYS_COUNT+1) {

 /* load themodule, if this fails, we're doomed, and we should not try

 * to load a different variant. */

status = load(id, path, module);//调用load函数打开动态链接库

 }    

 获取了动态链接库的路径之后,就会调用load函数打开它,下面会打开它。

奥秘在load中

static int load(const char *id,    constchar *path,

 const structhw_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 theexternal symbols will not be global

  */

handle = dlopen(path, RTLD_NOW);//打开动态库

if (handle == NULL) {

char const *err_str = dlerror();

LOGE("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);

//查找“HMI”这个导出符号,获取其地址

if (hmi == NULL) {

LOGE("load: couldn't find symbol %s", sym);

status = -EINVAL;

goto done;

}

/* Check that the id matches */

//找到了hw_module_t结构!!!

if (strcmp(id, hmi->id) != 0) {

 LOGE("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 {

LOGV("loaded HAL id=%s path=%s hmi=%phandle=%p",  id, path, *pHmi,

handle);

 }

//凯旋而归

*pHmi = hmi;

return status;

从上面的代码中,会发现一个很奇怪的宏HAL_MODULE_INFO_SYM_AS_STR,它直接被定义为了#defineHAL_MODULE_INFO_SYM_AS_STR "HMI",为何根据它就能从动态链接库中找到这个hw_module_t结构体呢?我们查看一下我们用到的hal对应的so就可以了,在linux中可以使用readelf XX.so –s查看。

Symbol table '.dynsym' contains 28 entries:

Num:    Value Size Type    Bind  Vis      Ndx Name

0: 00000000     0 NOTYPE LOCAL DEFAULT UND 

1: 00000594     0 SECTION LOCALDEFAULT    7 

2: 00001104     0 SECTION LOCAL DEFAULT   13 

3: 00000000     0 FUNC    GLOBALDEFAULT UND ioctl

4: 00000000     0 FUNC    GLOBALDEFAULT UND strerror

5: 00000b84     0 NOTYPE GLOBAL DEFAULT ABS__exidx_end

6: 00000000     0 OBJECT GLOBAL DEFAULT UND__stack_chk_guard

7: 00000000     0 FUNC    GLOBAL DEFAULTUND __aeabi_unwind_cpp_pr0

8: 00000000     0 FUNC    GLOBALDEFAULT UND __errno

9: 00001188     0 NOTYPE GLOBAL DEFAULT ABS _bss_end__

10: 00000000     0 FUNC    GLOBALDEFAULT UND malloc

11: 00001188     0 NOTYPE GLOBAL DEFAULT ABS__bss_start__

12: 00000000     0 FUNC    GLOBAL DEFAULTUND __android_log_print

13: 00000b3a     0 NOTYPE GLOBAL DEFAULT ABS__exidx_start

14: 00000000     0 FUNC    GLOBALDEFAULT UND __stack_chk_fail

15: 00001188     0 NOTYPE GLOBAL DEFAULT ABS__bss_end__

16: 00001188     0 NOTYPE GLOBAL DEFAULT ABS__bss_start

17: 00000000     0 FUNC    GLOBALDEFAULT UND memset

18: 00000000     0 FUNC    GLOBALDEFAULT UND __aeabi_uidiv

19: 00001188     0 NOTYPE GLOBAL DEFAULT ABS __end__

20: 00001188     0 NOTYPE GLOBAL DEFAULT ABS _edata

21: 00001188     0 NOTYPE GLOBAL DEFAULT ABS _end

22: 00000000     0 FUNC    GLOBALDEFAULT UND open

23: 00080000     0 NOTYPE GLOBAL DEFAULT ABS _stack

24: 00001104   128 OBJECT GLOBAL DEFAULT   13 HMI

25: 00001104     0 NOTYPE GLOBAL DEFAULT  13 __data_start

26: 00000000     0 FUNC    GLOBALDEFAULT UND close

27: 00000000     0 FUNC    GLOBALDEFAULT UND free 

从上面中,第24个符号,名字就是“HMI”,对应于hw_module_t结构体。再去对照一下HAL的代码。

/*

* The COPYBIT Module

*/

struct copybit_module_t HAL_MODULE_INFO_SYM = {

common: {

tag: HARDWARE_MODULE_TAG,

version_major: 1,

version_minor: 0,

id: COPYBIT_HARDWARE_MODULE_ID,

name: "QCT MSM7K COPYBIT Module",

author: "Google, Inc.",

methods: &copybit_module_methods
    }

}; 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值