Android HAL是如何被调用的
2011-07-14 20:47
281 查看
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 Name0: 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 ioctl4: 00000000 0 FUNC GLOBALDEFAULT UND strerror5: 00000b84 0 NOTYPE GLOBAL DEFAULT ABS__exidx_end6: 00000000 0 OBJECT GLOBAL DEFAULT UND__stack_chk_guard7: 00000000 0 FUNC GLOBAL DEFAULTUND __aeabi_unwind_cpp_pr08: 00000000 0 FUNC GLOBALDEFAULT UND __errno9: 00001188 0 NOTYPE GLOBAL DEFAULT ABS _bss_end__10: 00000000 0 FUNC GLOBALDEFAULT UND malloc11: 00001188 0 NOTYPE GLOBAL DEFAULT ABS__bss_start__12: 00000000 0 FUNC GLOBAL DEFAULTUND __android_log_print13: 00000b3a 0 NOTYPE GLOBAL DEFAULT ABS__exidx_start14: 00000000 0 FUNC GLOBALDEFAULT UND __stack_chk_fail15: 00001188 0 NOTYPE GLOBAL DEFAULT ABS__bss_end__16: 00001188 0 NOTYPE GLOBAL DEFAULT ABS__bss_start17: 00000000 0 FUNC GLOBALDEFAULT UND memset18: 00000000 0 FUNC GLOBALDEFAULT UND __aeabi_uidiv19: 00001188 0 NOTYPE GLOBAL DEFAULT ABS __end__20: 00001188 0 NOTYPE GLOBAL DEFAULT ABS _edata21: 00001188 0 NOTYPE GLOBAL DEFAULT ABS _end22: 00000000 0 FUNC GLOBALDEFAULT UND open23: 00080000 0 NOTYPE GLOBAL DEFAULT ABS _stack24: 00001104 128 OBJECT GLOBAL DEFAULT 13 HMI25: 00001104 0 NOTYPE GLOBAL DEFAULT 13 __data_start26: 00000000 0 FUNC GLOBALDEFAULT UND close27: 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: ©bit_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模块中的所有想要外部访问到的方法。
}}; 这里定义了一个名为HAL_MODULE_INFO_SYM的copybit_module_t的结构体,common成员为hw_module_t类型。注意这里的HAL_MODULE_INFO_SYM变量必须为这个名字,这样编译器才会将这个结构体的导出符号变为“HMI”,这样这个结构体才能被dlsym函数找到!综上,我们知道了andriodHAL模块也有一个通用的入口地址,这个入口地址就是HAL_MODULE_INFO_SYM变量,通过它,我们可以访问到HAL模块中的所有想要外部访问到的方法。
相关文章推荐
- Android HAL 是如何被调用的
- Android HAL 是如何被调用的
- Android HAL 是如何被调用的
- Android HAL是如何被调用的
- Android HAL 是如何被调用的
- Android HAL 是如何被调用的
- Android HAL 是如何被调用的
- Android HAL是如何被调用的
- android如何调用显示和隐藏系统默认的输入法
- android的HAL第二种调用hal方法中的APP 测试程序(属于APP层)代码的实现:
- android 如何调用(打开)其他程序
- Android开发中如何调用摄像头的功能
- Android中HAL如何向上层提供接口总结
- 高版本Android如何利用反射调用系统隐藏的远程服务拦截来电
- Android中HAL如何向上层提供接口总结
- Android开发中如何调用摄像头的功…
- Android平台,如何调用javascript操作网页和js调用系统功能
- 如何调用Android隐藏API
- 如何制作Jar包并在android中调用jar包
- 如何在Android使用Rest服务从客户端调用webservice