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 中。看看它的实现。。。。
-
- 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;
- }
-
- break;
- }
- status = -ENOENT;
- if (i < HAL_VARIANT_KEYS_COUNT+1) {
-
- status = load(id, path, module);//调用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;
-
-
- 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;
- }
-
-
- 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;
- }
-
-
- //找到了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;
-
- 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的代码。
-
- 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 模块中的所有想要外部访问到的方法。