Android硬件抽象层(HAL)概要介绍和学习计划一文中,我们简要介绍了在Android系统为为硬件编写驱动程序的方法。简单来说,硬件驱动程序一方面分布在Linux内核中,另一方面分布在用户空间的硬件抽象层中。接着,在Ubuntu上为Android系统编写Linux内核驱动程序一文中举例子说明了如何在Linux内核编写驱动程序。在这一篇文章中,我们将继续介绍Android系统硬件驱动程序的另一方面实现,即如何在硬件抽象层中增加硬件模块来和内核驱动程序交互。在这篇文章中,我们还将学习到如何在Android系统创建设备文件时用类似Linux的udev规则修改设备文件模式的方法。

一. 参照在Ubuntu上为Android系统编写Linux内核驱动程序一文所示,准备好示例内核驱动序。完成这个内核驱动程序后,便可以在Android系统中得到三个文件,分别是/dev/hello、/sys/class/hello/hello/val和/proc/hello。在本文中,我们将通过设备文件/dev/hello来连接硬件抽象层模块和Linux内核驱动程序模块。

二. 进入到在hardware/libhardware/include/hardware目录,新建hello.h文件:

      [email protected]:~/Android$ cd hardware/libhardware/include/hardware      [email protected]:~/Android/hardware/libhardware/include/hardware$ vi hello.h


hello.h文件的内容如下:

[html] view plain copy
  1. #ifndefANDROID_HELLO_INTERFACE_H
  2. #defineANDROID_HELLO_INTERFACE_H
  3. #include<hardware/hardware.h>
  4. __BEGIN_DECLS
  5. /*定义模块ID*/
  6. #defineHELLO_HARDWARE_MODULE_ID"hello"
  7. /*硬件模块结构体*/
  8. structhello_module_t{
  9. structhw_module_tcommon;
  10. };
  11. /*硬件接口结构体*/
  12. structhello_device_t{
  13. structhw_device_tcommon;
  14. intfd;
  15. int(*set_val)(structhello_device_t*dev,intval);
  16. int(*get_val)(structhello_device_t*dev,int*val);
  17. };
  18. __END_DECLS
  19. #endif

这里按照Android硬件抽象层规范的要求,分别定义模块ID、模块结构体以及硬件接口结构体。在硬件接口结构体中,fd表示设备文件描述符,对应我们将要处理的设备文件"/dev/hello",set_val和get_val为该HAL对上提供的函数接口。


三. 进入到hardware/libhardware/modules目录,新建hello目录,并添加hello.c文件。 hello.c的内容较多,我们分段来看。

首先是包含相关头文件和定义相关结构:

[html] view plain copy
  1. #defineLOG_TAG"HelloStub"
  2. #include<hardware/hardware.h>
  3. #include<hardware/hello.h>
  4. #include<fcntl.h>
  5. #include<errno.h>
  6. #include<cutils/log.h>
  7. #include<cutils/atomic.h>
  8. #defineDEVICE_NAME"/dev/hello"
  9. #defineMODULE_NAME"Hello"
  10. #defineMODULE_AUTHOR"[email protected]"
  11. /*设备打开和关闭接口*/
  12. staticinthello_device_open(conststructhw_module_t*module,constchar*name,structhw_device_t**device);
  13. staticinthello_device_close(structhw_device_t*device);
  14. /*设备访问接口*/
  15. staticinthello_set_val(structhello_device_t*dev,intval);
  16. staticinthello_get_val(structhello_device_t*dev,int*val);
  17. /*模块方法表*/
  18. staticstructhw_module_methods_thello_module_methods={
  19. open:hello_device_open
  20. };
  21. /*模块实例变量*/
  22. structhello_module_tHAL_MODULE_INFO_SYM={
  23. common:{
  24. tag:HARDWARE_MODULE_TAG,
  25. version_major:1,
  26. version_minor:0,
  27. id:HELLO_HARDWARE_MODULE_ID,
  28. name:MODULE_NAME,
  29. author:MODULE_AUTHOR,
  30. methods:&hello_module_methods,
  31. }
  32. };

这里,实例变量名必须为HAL_MODULE_INFO_SYM,tag也必须为HARDWARE_MODULE_TAG,这是Android硬件抽象层规范规定的。

定义hello_device_open函数:

[html] view plain copy
  1. staticinthello_device_open(conststructhw_module_t*module,constchar*name,structhw_device_t**device){
  2. structhello_device_t*dev;dev=(structhello_device_t*)malloc(sizeof(structhello_device_t));
  3. if(!dev){
  4. LOGE("HelloStub:failedtoallocspace");
  5. return-EFAULT;
  6. }
  7. memset(dev,0,sizeof(structhello_device_t));
  8. dev->common.tag=HARDWARE_DEVICE_TAG;
  9. dev->common.version=0;
  10. dev->common.module=(hw_module_t*)module;
  11. dev->common.close=hello_device_close;
  12. dev->set_val=hello_set_val;dev->get_val=hello_get_val;
  13. if((dev->fd=open(DEVICE_NAME,O_RDWR))==-1){
  14. LOGE("HelloStub:failedtoopen/dev/hello--%s.",strerror(errno));free(dev);
  15. return-EFAULT;
  16. }
  17. *device=&(dev->common);
  18. LOGI("HelloStub:open/dev/hellosuccessfully.");
  19. return0;
  20. }

DEVICE_NAME定义为"/dev/hello"。由于设备文件是在内核驱动里面通过device_create创建的,而device_create创建的设备文件默认只有root用户可读写,而hello_device_open一般是由上层APP来调用的,这些APP一般不具有root权限,这时候就导致打开设备文件失败:

Hello Stub: failed to open /dev/hello -- Permission denied.

解决办法是类似于Linux的udev规则,打开Android源代码工程目录下,进入到system/core/rootdir目录,里面有一个名为ueventd.rc文件,往里面添加一行:
/dev/hello 0666 root root

定义hello_device_close、hello_set_val和hello_get_val这三个函数: [html] view plain copy
  1. staticinthello_device_close(structhw_device_t*device){
  2. structhello_device_t*hello_device=(structhello_device_t*)device;
  3. if(hello_device){
  4. close(hello_device->fd);
  5. free(hello_device);
  6. }
  7. return0;
  8. }
  9. staticinthello_set_val(structhello_device_t*dev,intval){
  10. LOGI("HelloStub:setvalue%dtodevice.",val);
  11. write(dev->fd,&val,sizeof(val));
  12. return0;
  13. }
  14. staticinthello_get_val(structhello_device_t*dev,int*val){
  15. if(!val){
  16. LOGE("HelloStub:errorvalpointer");
  17. return-EFAULT;
  18. }
  19. read(dev->fd,val,sizeof(*val));
  20. LOGI("HelloStub:getvalue%dfromdevice",*val);
  21. return0;
  22. }

四. 继续在hello目录下新建Android.mk文件:
      LOCAL_PATH := $(call my-dir)      include $(CLEAR_VARS)      LOCAL_MODULE_TAGS := optional      LOCAL_PRELINK_MODULE := false      LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw      LOCAL_SHARED_LIBRARIES := liblog      LOCAL_SRC_FILES := hello.c      LOCAL_MODULE := hello.default      include $(BUILD_SHARED_LIBRARY)


注意,LOCAL_MODULE的定义规则,hello后面跟有default,hello.default能够保证我们的模块总能被硬象抽象层加载到。
五. 编译:
      [email protected]:~/Android$ mmm hardware/libhardware/modules/hello


编译成功后,就可以在out/target/product/generic/system/lib/hw目录下看到hello.default.so文件了。
六. 把hello.default.so文件通过adb push 到开发板的系统文件的/system/lib/文件里面 就可以了 虽然我们在Android系统为我们自己的硬件增加了一个硬件抽象层模块,但是现在Java应用程序还不能访问到我们的硬件。我们还必须编写JNI方法和在Android的Application Frameworks层增加API接口,才能让上层Application访问我们的硬件。在接下来的文章中,我们还将完成这一系统过程,使得我们能够在Java应用程序中访问我们自己定制的硬件。

更多相关文章

  1. 分析你的第一个 Android(安卓)程序
  2. 初次修改 android app 代码
  3. android 文件下载(一)
  4. Android(安卓)离线播放 (HLS)m3u8文件
  5. Android文件方式建立和读取(properties)方法
  6. 分享一个遍历当前文件夹下所以子目录,并在子目录中执行其他操作的
  7. Android开源中国客户端学习 截屏模块
  8. Android实现夜间模式的方法(一)
  9. Android学习系列(27)--App缓存管理

随机推荐

  1. Shell脚本编写简明教程
  2. 手把手教你在ubuntu下创建桌面快捷方式
  3. 在Linux里设置用户环境变量的方法
  4. 来点基础的--诡异的极客们的符号--流、管
  5. Linux Box上运行哪个SQL服务器?
  6. linux中创建公私钥
  7. 报告节选3:Linux比例近半 操作系统混战虚
  8. 继续问linux下c问题
  9. 嵌入式Linux系统工程师系列之ARM920T的MM
  10. find . -type f ! -name "*.o" 排除某类