不管是出于什么样地考虑,android系统终究是提供了hardware层来封装了对Linux的驱动的访问,同时为上层提供了一个统一的硬件接口和硬件形态。

一.Hardware概述

在Hardware层中的一个模块中,主要设计一下三个结构:

  1. struct hw_module_t
  2. struct hw_module_methods_t
  3. struct hw_device_t
    这三个结构体的关系是这样的:我们在上层访问linux驱动时,需要首先获得hardware层的对应的module,使用hw_get_module()方法实现,这个函数通过给定模块的ID来寻找硬件模块的动态链接库,找到后使用load()函数打开这个库,并通过一个固定的符号:HAL_MODULE_INFO_SYM寻找hw_module_t结构体,我们会在hw_module_t结构体中会有一个methods属性,指向hw_module_methods_t结构体,这个结构体中会提供一个open方法用来打开模块,在open的参数中会传入一个hw_device_t**的数据结构,这样我们就可以把对模块的操作函数等数据保存在这个hw_device_t结构中,这样,这样用户可以通过hw_device_t来和模块交互。
    具体的说,我们在上层可以这样调用HAL层的module:
    xxx_module_t *module;
    hw_get_module(XXXID,(struct hw_module_t **)&module);
    以上两步我们就获得了对应ID的 hw_module_t结构体。
    struct xxx_device_t *device;
    module->methods->open(module,XXXID,(struct hw_device_t **)&device);
    这样我们就获得了hw_device_t结构体,通过hw_device_t结构体我们就可以访问HAL层对应的module了。
    这个思路很重要,后面我们测试我们的HAL层module的时候,就需要上面的代码。
    整个过程可用一张图展示:

二.使用Android HAL规范封装对Linux驱动的访问

在hardware/libhardware/modules新建hellotest目录,添加两个文件:hellotest.c 和 Android.mk

1.hellotest.c

这个文件把对linux驱动的访问封装成了Android要求的格式。

#include <hardware/hardware.h>
#include <hardware/hellotest.h>
#include <fcntl.h>
#include <errno.h>
#include <cutils/log.h>
#include <cutils/atomic.h>  #define DEVICE_NAME "/dev/hello"
#define MODULE_NAME "HelloTest"
#define MODULE_AUTHOR "jinwei" #define LOG_TAG "HelloTest"  /* 定义LOG */
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG , LOG_TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO  , LOG_TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN  , LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR  , LOG_TAG, __VA_ARGS__)/*打开和关闭设备的方法*/
static int hellotest_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device);
static int hellotest_device_close(struct hw_device_t* device);  /*读写linux驱动的接口*/
static int hellotest_write_string(struct hellotest_device_t* dev, char * str);
static int hellotest_read_string(struct hellotest_device_t* dev, char **str);  /*模块方法结构体*/
static struct hw_module_methods_t hellotest_module_methods = {  open: hellotest_device_open
};  /*模块实例变量*/
struct hellotest_module_t HAL_MODULE_INFO_SYM = {  common: {  tag: HARDWARE_MODULE_TAG,  version_major: 1,  version_minor: 0,  id: HELLOTEST_HARDWARE_MODULE_ID,  name: MODULE_NAME,  author: MODULE_AUTHOR,  methods: &hellotest_module_methods,  }
};  static int hellotest_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) {  struct hellotest_device_t* dev;dev = (struct hellotest_device_t*)malloc(sizeof(struct hellotest_device_t));  if(!dev) {  LOGE("HelloTest: failed to alloc space");  return -EFAULT;  }  memset(dev, 0, sizeof(struct hellotest_device_t));  dev->common.tag = HARDWARE_DEVICE_TAG;  dev->common.version = 0;  dev->common.module = (hw_module_t*)module;  dev->common.close = hellotest_device_close;  dev->write_string = hellotest_write_string;dev->read_string = hellotest_read_string;  if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {  LOGE("HelloTest: open /dev/hello fail-- %s.", strerror(errno));free(dev);  return -EFAULT;  }  *device = &(dev->common);  LOGI("HelloTest: open /dev/hello successfully.");  return 0;
}
static int hellotest_device_close(struct hw_device_t* device) {  struct hellotest_device_t* hello_device = (struct hellotest_device_t*)device;  if(hello_device) {  close(hello_device->fd);  free(hello_device);  }  return 0;
}  static int hellotest_write_string(struct hellotest_device_t* dev,char * str) {  LOGI("HelloTest:write string: %s", str);  write(dev->fd, str, sizeof(str));  return 0;
}  static int hellotest_read_string(struct hellotest_device_t* dev, char ** str) {  LOGI("HelloTest:read string: %s", *str);  read(dev->fd, *str, sizeof(*str));  return 0;
}

这个程序就是把我们读写/dev/hello的代码做了一次封装而已,经过封装以后,我们有了hw_module_t,hw_module_methods_t,hw_device_t这三个结构体。正因为它如此规范,所以上层才可以按照这种规范的格式获取的hw_module_t结构体,进而使用hw_module_methods_t中的open函数获取hw_device_t结构体,然后使用hw_device_t结构体中的方法操作Linux驱动。

2.Android.mk

      LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_PRELINK_MODULE := falseLOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hwLOCAL_SHARED_LIBRARIES += \libcutils libutils liblogLOCAL_LDLIBS:=  -L$(SYSROOT)/usr/lib -llog LOCAL_SRC_FILES := hellotest.cLOCAL_MODULE := hellotest.defaultinclude $(BUILD_SHARED_LIBRARY)

LOCAL_MODULE 的值为hellotest.default,注意,一定要加上default,不然使用hw_get_module函数找不到我们的HAL模块。
然后在hardware/libhardware/include/hardware新建对应的头文件hellotest.h

3.hellotest.h

#ifndef ANDROID_HELLO_TEST_H
#define ANDROID_HELLO_TEST_H
#include <hardware/hardware.h>  __BEGIN_DECLS  /*定义模块ID,必须的,用与上层程序获取该模块*/
#define HELLOTEST_HARDWARE_MODULE_ID "hellotest"/*硬件模块结构体*/
struct hellotest_module_t {  struct hw_module_t common;
};  /*硬件接口结构体*/
struct hellotest_device_t {  struct hw_device_t common;  int fd;  int (*write_string)(struct hellotest_device_t* dev, char *str);  int (*read_string)(struct hellotest_device_t* dev, char ** str);
};  __END_DECLS  #endif  

做好这三个文件以后,我们就可以编译该模块了。进入到hardware/libhardware/modules目录,执行mm命令进行编译,编译后的文件如图所示:

三.写测试代码

封装好了我们的HAL层module以后,我希望立刻测试它是否正确运行,下面我们将写一个简单的C程序,用来测试module是否正确工作。
首先在hardware/libhardware/tests/目录下新建一个testhellotest目录,新增test.c和Android.mk文件:

1.test.c

#include <hardware/hardware.h>
#include <hardware/hellotest.h>
#include <fcntl.h>
#include <stdio.h>struct hw_module_t * module;
struct hw_device_t * device;int main(){char *read_str;char *write_str="nihao";read_str = malloc(100);printf("----begin main------\n");if(hw_get_module(HELLOTEST_HARDWARE_MODULE_ID,(struct hw_module_t const **)&module)==0){printf("get module sucess\n");}else{printf("get module fail\n");return -1;}if(module->methods->open(module,HELLOTEST_HARDWARE_MODULE_ID,(struct hw_device_t const**)&device)==0){printf("open module sucess\n");}else{printf("open module error\n");return -2;}struct hellotest_device_t* dev = (struct hellotest_device_t *)device;dev->read_string(dev,&read_str);if(read_str == NULL){printf("read error");}else{printf("read data: %s\n",read_str);}dev->write_string(dev,write_str);printf("write data: %s\n",write_str);dev->read_string(dev,&read_str);if(read_str == NULL){printf("read error");}else{printf("read data: %s\n",read_str);}printf("----end main------\n");
return 0;
}

测试的流程正如文章开头描述的那样,首先使用hw_get_module函数获得hw_module_t结构体,然后调用hw_module_methods_t结构体中的open方法过得hw_device_t结构体,然后使用hw_device_t结构体中的read_string和write_string方法与linux驱动交互。注意,驱动的打开是在hw_module_methods_t结构体中的open方法中做的。

2.Android.mk

      LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_MODULE := my_testLOCAL_LDLIBS:=  -lhardwareLOCAL_SRC_FILES := $(call all-subdir-c-files)include $(BUILD_EXECUTABLE)

然后进入到该目录执行mm命令进行编译,编译后生成文件如图所示:

四.测试

1.把生成的hellotest.default.so文件拷贝到android设备的/system/lib/hw目录下,这需要root权限,没有root权限请自行解决。
2.获得root权限后会提示/system是一个只读文件系统,所以需要重新挂载为可读可写的文件系统,执行mount -o remount,rw /system 即可。
3.修改hellotest.default.so文件的的权限为644,执行chmod 644 /system/lib/dw/hellotest.default.so
4.装载上一节实现的hello.ko驱动:insmod hello.ko
5.把生成的my_test可执行文件拷贝的android设备上,建议push my_test /data
6.给my_test文件添加可执行权限. chmod +x my_test
7.执行my_test。 ./my_test
打印如下:
—-begin main——
get module sucess
open module sucess
read data: hello
write data: nihao
read data: nihao
—-end main——
可见,测试成功。
如果有问题,可以使用logcat看下HAL层的打印,看看问题出在什么地方。

Android应用程序访问linux驱动第二步:实现并测试hardware层相关推荐

  1. Android应用程序访问linux驱动第三步:实现并向系统注册Service

    在学习Android应用程序访问linux驱动时,原博主在第一.二步写得具体详细,但我学到第三步实现并向系统注册Service时,发觉内迷惑和发现几处错误,这里我将我的理解和修改记录下来和大家分享.希 ...

  2. linux按键检测程序,Tiny4412 Linux驱动之按键(使用查询方式) | 技术部落

    前几天在TIny4412开发板上做了LED点灯的Linux驱动,其实挺简单,GPIO驱动,今天再看一下按键的驱动,毕竟按键用的还是比较广泛的,本文使用查询的方式获取按键值,后面会有文章使用中断的方式进 ...

  3. nginx程序访问linux任意目录,通过nginx访问linux目录

    http { ...... autoindex on; autoindex_exact_size off; autoindex_localtime on; server { listen 80; .. ...

  4. 从Android应用程序访问Internet需要什么权限?

    我在运行我的应用程序时遇到以下异常: java.net.SocketException: Permission denied (maybe missing INTERNET permission) 如 ...

  5. 04.在android模拟器中运行linux驱动

    驱动编译 makefile obj-m += word_count.o ​ all:make -C /root/goldfish/ M=$(PWD) modules # -C 到 linux kern ...

  6. 一文带你深入了解linux驱动

    很多程序员在学习技能时,盲目追求技术实现,而忽略了整个生态环境的观察和基础理论铺垫,导致学完后似是而非,不能举一反三,遇到项目依然拿不出合理的解决方案. 作为一个技术开发者,大家要明白"技术 ...

  7. Android 应用程序消息处理机制(Looper、Handler)分析

    Android应用程序是通过消息来驱动的,系统为每一个应用程序维护一个消息队例,应用程序的主线程不断地从这个消息队例中获取消息(Looper),然后对这些消息进行处理(Handler),这样就实现了通 ...

  8. Android应用程序消息处理机制(Looper、Handler)分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6817933 Android应用程序是通过消息来 ...

  9. linux添加hello驱动,Linux驱动之建立一个hello模块

    目标:在开发板上执行insmod hello.ko能在控制台打印出hello init:接着执行rmmod会在控制台打印出hello exit 建立一个hello模块的步骤如下: 1.建立一个hell ...

  10. linux驱动开发(转载自正点原子)

    一.Linux驱动开发思维 1.Linux下驱动开发直接操作寄存器不现实. 2.根据Linux下的各种驱动框架进行开发.一定要满足框架,也就是Linux下各种驱动框架的掌握. 3.驱动最终表现就是/d ...

最新文章

  1. 深蓝学院的深度学习理论与实践课程:第四章
  2. 抓紧!抓紧!CSDN年终重榜福利来了~人手一份,快来投稿!!
  3. [转载]极速狂飚 Windows 2003系统25招加速大法
  4. 计算机具有很强的记忆力记忆能力的基础是,基于学习能力的记忆力计算机测评研究...
  5. WordPress搬家全攻略
  6. ios 获取html的高度,iOS Webview自适应实际内容高度的4种方法详解
  7. 10976 - Fractions Again?!
  8. [Unity脚本运行时更新]C#7.3新特性
  9. 毕业论文图像快速画出
  10. 飞鱼星行为管理路由器【限制视频】方法(网页+客户端)
  11. Python智能机械助理
  12. WiFi的单频和双频
  13. playbook中的block rescue always
  14. python画魄罗代码_LOL:灵魂画师在这里!玩家手绘冰雪节魄罗
  15. 审计溯源 | IP-guard终端操作审计,助力高效防控泄密风险
  16. 企业如何选择合适的精益生产方案?
  17. 走近古人的生活 衣食住行
  18. 敷完面膜后要擦水乳吗_面膜敷完后要擦水乳吗 面膜使用后如何正确护肤
  19. 怎么在通达信上设置连板次数以及所属行业
  20. Vue组件库实现按需加载功能

热门文章

  1. 在 mysql数据库怎么知道的ip_用户名_密码_数据库_数据库ip怎么查
  2. html作业本,连作业本都不用买了!Word做作业本竟这么简单
  3. vscode中文乱码
  4. 什么是线性同余法c语言,C语言线性同余法产生随机数
  5. java字符串中的数据排序
  6. Linux内核编译 —— 配置文件
  7. [转]防火墙、防病毒网关、IDS以及该类安全产品开发(文章汇总)
  8. Java学习视频教程
  9. MegaRAID Storage Manager RAID管理工具基本操作
  10. 计算机页面的工具,网页智能填写工具