Android中HAL如何向上层提供接口总结-hw_device_t
转载自:http://blog.csdn.net/myarrow/article/details/7175204
参考文献:
http://blog.csdn.net/luoshengyang/article/details/6573809
http://blog.csdn.net/hongtao_liu/article/details/6060734
建议阅读本文时先浏览以上两篇文章,本文是对上两篇文章在HAL对上层接口话题的一个总结.
1 什么是HAL
HAL的全称是Hardware Abstraction Layer,即硬件抽象层.其架构图如下:
Android的HAL是为了保护一些硬件提供商的知识产权而提出的,是为了避开linux的GPL束缚。思路是把控制硬件的动作都放到了Android HAL中,而linux driver仅仅完成一些简单的数据交互作用,甚至把硬件寄存器空间直接映射到user space。而Android是基于Aparch的license,因此硬件厂商可以只提供二进制代码,所以说Android只是一个开放的平台,并不是一个开源的平台。也许也正是因为Android不遵从GPL,所以Greg Kroah-Hartman才在2.6.33内核将Andorid驱动从linux中删除。GPL和硬件厂商目前还是有着无法弥合的裂痕。Android想要把这个问题处理好也是不容易的。
总结下来,Android HAL存在的原因主要有:
1. 并不是所有的硬件设备都有标准的linux kernel的接口
2. KERNEL DRIVER涉及到GPL的版权。某些设备制造商并不原因公开硬件驱动,所以才去用HAL方式绕过GPL。
3. 针对某些硬件,Android有一些特殊的需求.
2 与接口相关的几个结构体
首先来看三个与HAL对上层接口有关的几个结构体:
view plain copy to clipboard
- structhw_module_t;//模块类型
- structhw_module_methods_t;//模块方法
- structhw_device_t;//设备类型,向上提供接口
3 解释
一般来说,在写HAL相关代码时都得包含这个hardware.h头文件,所以有必要先了解一下这个头文件中的内容.
view plain copy to clipboard
- /*
- *Copyright(C)2008TheAndroidOpenSourceProject
- *
- *LicensedundertheApacheLicense,Version2.0(the"License");
- *youmaynotusethisfileexceptincompliancewiththeLicense.
- *YoumayobtainacopyoftheLicenseat
- *
- *http://www.apache.org/licenses/LICENSE-2.0
- *
- *Unlessrequiredbyapplicablelaworagreedtoinwriting,software
- *distributedundertheLicenseisdistributedonan"ASIS"BASIS,
- *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
- *SeetheLicenseforthespecificlanguagegoverningpermissionsand
- *limitationsundertheLicense.
- */
- #ifndefANDROID_INCLUDE_HARDWARE_HARDWARE_H
- #defineANDROID_INCLUDE_HARDWARE_HARDWARE_H
- #include<stdint.h>
- #include<sys/cdefs.h>
- #include<cutils/native_handle.h>
- #include<system/graphics.h>
- __BEGIN_DECLS
- /*
- *Valueforthehw_module_t.tagfield
- */
- #defineMAKE_TAG_CONSTANT(A,B,C,D)(((A)<<24)|((B)<<16)|((C)<<8)|(D))
- #defineHARDWARE_MODULE_TAGMAKE_TAG_CONSTANT('H','W','M','T')
- #defineHARDWARE_DEVICE_TAGMAKE_TAG_CONSTANT('H','W','D','T')
- structhw_module_t;
- structhw_module_methods_t;
- structhw_device_t;
- /**
- *EveryhardwaremodulemusthaveadatastructurenamedHAL_MODULE_INFO_SYM
- *andthefieldsofthisdatastructuremustbeginwithhw_module_t
- *followedbymodulespecificinformation.
- */
- //每一个硬件模块都每必须有一个名为HAL_MODULE_INFO_SYM的数据结构变量,它的第一个成员的类型必须为hw_module_t
- typedefstructhw_module_t{
- /**tagmustbeinitializedtoHARDWARE_MODULE_TAG*/
- uint32_ttag;
- /**majorversionnumberforthemodule*/
- uint16_tversion_major;
- /**minorversionnumberofthemodule*/
- uint16_tversion_minor;
- /**Identifierofmodule*/
- constchar*id;
- /**Nameofthismodule*/
- constchar*name;
- /**Author/owner/implementorofthemodule*/
- constchar*author;
- /**Modulesmethods*/
- //模块方法列表,指向hw_module_methods_t*
- structhw_module_methods_t*methods;
- /**module'sdso*/
- void*dso;
- /**paddingto128bytes,reservedforfutureuse*/
- uint32_treserved[32-7];
- }hw_module_t;
- typedefstructhw_module_methods_t{//硬件模块方法列表的定义,这里只定义了一个open函数
- /**Openaspecificdevice*/
- int(*open)(conststructhw_module_t*module,constchar*id,//注意这个open函数明确指出第三个参数的类型为structhw_device_t**
- structhw_device_t**device);
- }hw_module_methods_t;
- /**
- *Everydevicedatastructuremustbeginwithhw_device_t
- *followedbymodulespecificpublicmethodsandattributes.
- */
- //每一个设备数据结构的第一个成员函数必须是hw_device_t类型,其次才是各个公共方法和属性
- typedefstructhw_device_t{
- /**tagmustbeinitializedtoHARDWARE_DEVICE_TAG*/
- uint32_ttag;
- /**versionnumberforhw_device_t*/
- uint32_tversion;
- /**referencetothemodulethisdevicebelongsto*/
- structhw_module_t*module;
- /**paddingreservedforfutureuse*/
- uint32_treserved[12];
- /**Closethisdevice*/
- int(*close)(structhw_device_t*device);
- }hw_device_t;
- /**
- *Nameofthehal_module_info
- */
- #defineHAL_MODULE_INFO_SYMHMI
- /**
- *Nameofthehal_module_infoasastring
- */
- #defineHAL_MODULE_INFO_SYM_AS_STR"HMI"
- /**
- *Getthemoduleinfoassociatedwithamodulebyid.
- *
- *@return:0==success,<0==errorand*module==NULL
- */
- inthw_get_module(constchar*id,conststructhw_module_t**module);
- /**
- *Getthemoduleinfoassociatedwithamoduleinstancebyclass'class_id'
- *andinstance'inst'.
- *
- *Somemodulestypesnecessitatemultipleinstances.Forexampleaudiosupports
- *multipleconcurrentinterfacesandthus'audio'isthemoduleclass
- *and'primary'or'a2dp'aremoduleinterfaces.Thisimpliesthatthefiles
- *providingthesemoduleswouldbenamedaudio.primary.<variant>.soand
- *audio.a2dp.<variant>.so
- *
- *@return:0==success,<0==errorand*module==NULL
- */
- inthw_get_module_by_class(constchar*class_id,constchar*inst,
- conststructhw_module_t**module);
- __END_DECLS
- #endif/*ANDROID_INCLUDE_HARDWARE_HARDWARE_H*/
由以上内容可以看出(typedef struct hw_module_t ,typedef struct hw_device_t),如果我们要写一个自定义设备的驱动的HAL层时,我们得首先自定义两个数据结构:
假设我们要做的设备名为XXX:
在头文件中定义:XXX.h
view plain copy to clipboard
- /*定义模块ID*/
- #defineXXX_HARDWARE_MODULE_ID"XXX"
- /*硬件模块结构体*/
- //见hardware.h中的hw_module_t定义的说明,xxx_module_t的第一个成员必须是hw_module_t类型,其次才是模块的一此相关信息,当然也可以不定义,
- //这里就没有定义模块相关信息
- structxxx_module_t{
- structhw_module_tcommon;
- };
- /*硬件接口结构体*/
- //见hardware.h中的hw_device_t的说明,要求自定义xxx_device_t的第一个成员必须是hw_device_t类型,其次才是其它的一些接口信息.
- structxxx_device_t{
- structhw_device_tcommon;
- //以下成员是HAL对上层提供的接口或一些属性
- intfd;
- int(*set_val)(structxxx_device_t*dev,intval);
- int(*get_val)(structxxx_device_t*dev,int*val);
- };
接下来我们在实现文件XXX.c文件中定义一个xxx_module_t的变量:
view plain copy to clipboard
- /*模块实例变量*/
- structxxx_module_tHAL_MODULE_INFO_SYM={//变量名必须为HAL_MODULE_INFO_SYM,这是强制要求的,你要写Android的HAL就得遵循这个游戏规则,
- //见hardware.h中的hw_module_t的类型信息说明.它是一个宏定义:#define HAL_MODULE_INFO_SYM HMI
- common:{
- tag:HARDWARE_MODULE_TAG,
- version_major:1,
- version_minor:0,
- id:XXX_HARDWARE_MODULE_ID,//头文件中有定义
- name:MODULE_NAME,
- author:MODULE_AUTHOR,
- methods:&xxx_module_methods,//模块方法列表,在本地定义
- }
- };
注意到上面有HAL_MODULE_INFO_SYM变量的成员common中包含一个函数列表xxx_module_methods,而这个成员函数列表是在本地自定义的。那么这个成员函数列是不是就是HAL向上层提供函数的地方呢?很失望,不是在这里,前面我们已经说过了,是在xxx_device_t中定义的,这个xxx_module_methods实际上只提供了一个open函数,就相当于只提供了一个模块初始化函数.其定义如下:
view plain copy to clipboard
- /*模块方法表*/
- staticstructhw_module_methods_txxx_module_methods={
- open:xxx_device_open
- };
那么HAL又到底是怎么将xxx_device_t中定义的接口提供到上层去的呢?
且看上面这个函数列表中唯一的一个xxx_device_open的定义:
view plain copy to clipboard
- staticintxxx_device_open(conststructhw_module_t*module,constchar*name,structhw_device_t**device){
- structxxx_device_t*dev;
- dev=(structhello_device_t*)malloc(sizeof(structxxx_device_t));//动态分配空间
- if(!dev){
- LOGE("HelloStub:failedtoallocspace");
- return-EFAULT;
- }
- memset(dev,0,sizeof(structxxx_device_t));
- //对dev->common的内容赋值,
- dev->common.tag=HARDWARE_DEVICE_TAG;
- dev->common.version=0;
- dev->common.module=(hw_module_t*)module;
- dev->common.close=xxx_device_close;
- //对dev其它成员赋值
- dev->set_val=xxx_set_val;
- dev->get_val=xxx_get_val;
- if((dev->fd=open(DEVICE_NAME,O_RDWR))==-1){
- LOGE("HelloStub:failedtoopen/dev/hello--%s.",strerror(errno));
- free(dev);
- return-EFAULT;
- }
- //输出&(dev->common),输出的并不是dev,而是&(dev->common)!(common内不是只包含了一个close接口吗?)
- *device=&(dev->common);
- LOGI("HelloStub:open/dev/hellosuccessfully.");
- return0;
- }
在回答上述问题之前,让我们先看一下这xxx_device_open函数原型,还是在hardware.h头文件中,找到下面几行代码:
view plain copy to clipboard
- typedefstructhw_module_methods_t{
- /**Openaspecificdevice*/
- int(*open)(conststructhw_module_t*module,constchar*id,
- structhw_device_t**device);
- }hw_module_methods_t;
可是,dev->common不是只包含close接口吗?做为HAL的上层,它又是怎么"看得到"HAL提供的全部接口的呢?
接下来,让我们来看看做为HAL上层,它又是怎么使用由HAL返回的dev->common的:
参考:在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口这篇文章,从中可以看到这么几行代码:
view plain copy to clipboard
- /*通过硬件抽象层定义的硬件模块打开接口打开硬件设备*/
- staticinlineinthello_device_open(consthw_module_t*module,structhello_device_t**device){
- returnmodule->methods->open(module,HELLO_HARDWARE_MODULE_ID,(structhw_device_t**)device);
- }
此外,在hardware.h头文件中,还有明确要求定义xxx_module_t类型时,明确要求第一个成员变量类型必须为hw_module_t,这也是为了方便找到其第一个成员变量common,进而找到本地定义的方法列表,从而调用open函数进行模块初始化.
综上所述,HAL是通过structxxx_device_t这个结构体向上层提供接口的.
即:接口包含在structxxx_device_t这个结构体内。
而具体执行是通过struct xxx_module_t HAL_MODULE_INFO_SYM这个结构体变量的函数列表成员下的open函数来返回给上层的.
更多相关文章
- Android开发学习之TypedArray类
- Android(安卓)多点触摸接口
- Android(安卓)SipDemo项目实现SIP协议
- Android应用程序安装与Launcher启动机制
- AIDL接口描述语言的使用
- Android实现QQ分享功能
- android备份服务流程
- Android(安卓)编译大全
- Android部分功能模块实现