Android应用程序访问linux驱动第二步:实现并测试hardware层
来源:互联网 发布:js.type 编辑:程序博客网 时间:2024/05/17 20:44
不管是出于什么样地考虑,android系统终究是提供了hardware层来封装了对Linux的驱动的访问,同时为上层提供了一个统一的硬件接口和硬件形态。
一.Hardware概述
在Hardware层中的一个模块中,主要设计一下三个结构:
- struct hw_module_t
- struct hw_module_methods_t
- 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 := optional LOCAL_PRELINK_MODULE := false LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw LOCAL_SHARED_LIBRARIES += \ libcutils libutils liblog LOCAL_LDLIBS:= -L$(SYSROOT)/usr/lib -llog LOCAL_SRC_FILES := hellotest.c LOCAL_MODULE := hellotest.default include $(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 := optional LOCAL_MODULE := my_test LOCAL_LDLIBS:= -lhardware LOCAL_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层
- Android应用程序访问linux驱动第四步:实现android应用程序
- Android应用程序访问linux驱动第三步:实现并向系统注册Service
- Android应用程序访问linux驱动第三步:实现并向系统注册Service
- Android应用程序访问linux驱动第一步:实现并测试Linux驱动
- Android应用程序访问linux驱动第五步:回顾hw_get_module
- Android底层开发(二)之HAL层 第二步
- 【嵌入式linux】(第六步):使用eclipse集成开发环境开发第一个嵌入式Linux程序,并测试LED驱动
- Android从驱动层到应用程序层的通信
- Android从驱动层到应用程序层的通信
- Android从驱动层到应用程序层的通信
- Kali Linux渗透测试第二步:漏洞评估
- Linux 第二步
- Linux 第二步
- 为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程
- Android HAL层hardware module的设计
- ADO.NET实现应用程序数据访问层1
- ADO.NET实现应用程序数据访问层2
- React实战-深入源码了解Redux用法之Reducers
- Baxter学习笔记2-Kinect摄像头驱动安装篇
- 背景建模之vibe
- 论提高班的“五一”
- 动态显示倒计时
- Android应用程序访问linux驱动第二步:实现并测试hardware层
- 找到一个hosts,稳定性一般般吧,能用就行
- 双重循环打印十位比个位少的数字
- 小话设计模式(零)设计模式可以满足你对编程的所有幻想(误)
- solr 6.0 没有schema.xml未自动创建schema文件
- Android的布局(二)
- 第十三章 人机猜拳
- Poj 3617 Best Cow Line【贪心】
- 第八章在ARM7上移植uc/os-2