Hardware概述

不管是出于什么样地考虑,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要求的格式。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#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
</cutils></cutils></errno.h></fcntl.h></hardware></hardware>

这个程序就是把我们读写/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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
   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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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, int val); 
     int (*read_string)(struct hellotest_device_t* dev, int * val); 
}; 
 
__END_DECLS 
 
#endif  </hardware>

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

三.写测试代码

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

1.test.c

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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 ;
}</stdio.h></fcntl.h></hardware></hardware>

测试的流程正如文章开头描述的那样,首先使用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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
   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: hell
write data: nihao
read data: niha
—-end main——
可见,测试成功。
如果有问题,可以使用logcat看下HAL层的打印,看看问题出在什么地方。

猜你喜欢

转载自blog.csdn.net/hubinbin595959/article/details/80004618