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 实现的。
int hw_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 a module */ for (i=0 ; i
获取了动态链接库的路径之后,就会调用 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(); 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=%p handle=%p", id, path, *pHmi, handle); } //凯旋而归 *pHmi = hmi; return status;}
从上面的代码中,会发现一个很奇怪的宏 HAL_MODULE_INFO_SYM_AS_STR ,它直接被定义为了
#define HAL_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 LOCAL DEFAULT 7 2: 00001104 0 SECTION LOCAL DEFAULT 13 3: 00000000 0 FUNC GLOBAL DEFAULT UND ioctl 4: 00000000 0 FUNC GLOBAL DEFAULT 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 DEFAULT UND __aeabi_unwind_cpp_pr0 8: 00000000 0 FUNC GLOBAL DEFAULT UND __errno 9: 00001188 0 NOTYPE GLOBAL DEFAULT ABS _bss_end__ 10: 00000000 0 FUNC GLOBAL DEFAULT UND malloc 11: 00001188 0 NOTYPE GLOBAL DEFAULT ABS __bss_start__ 12: 00000000 0 FUNC GLOBAL DEFAULT UND __android_log_print 13: 00000b3a 0 NOTYPE GLOBAL DEFAULT ABS __exidx_start 14: 00000000 0 FUNC GLOBAL DEFAULT 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 GLOBAL DEFAULT UND memset 18: 00000000 0 FUNC GLOBAL DEFAULT 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 GLOBAL DEFAULT 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 GLOBAL DEFAULT UND close 27: 00000000 0 FUNC GLOBAL DEFAULT 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: ©bit_module_methods }};
这里定义了一个名为 HAL_MODULE_INFO_SYM 的 copybit_module_t 的结构体, common 成员为 hw_module_t 类型。注意这里的 HAL_MODULE_INFO_SYM 变量必须为这个名字,这样编译器才会将这个结构体的导出符号变为“ HMI ”,这样这个结构体才能被 dlsym 函数找到!
综上,我们知道了 andriod HAL 模块也有一个通用的入口地址,这个入口地址就是 HAL_MODULE_INFO_SYM 变量,通过它,我们可以访问到 HAL 模块中的所有想要外部访问到的方法。
更多相关文章
- Android 优化布局层次结构
- Android之路——走进Android(工程结构剖析)
- 利用BeautifulSoup的find_all()函数查找某个标签且该标签某属性
- 自定义android用户控件,使用回调函数实现自定义事件
- 在Android里添加自己的log函数
- [置顶] Android硬件抽象层模块编写规范
- LibGDX输入模块之加速度计
- android 基于树结构的任意层级列表