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层的打印,看看问题出在什么地方。
Hardware概述相关推荐
- 人工操作阶段计算机是如何工作的,第一章计算机基础概述全解.ppt
第一章计算机基础概述全解 1.2.3 汉字编码 汉字的编码 国标码:中文内码之一,汉字信息交换的标准编码.国标码是不可能在计算机内部直接采用.于是, 汉字的机内码采用变形国标码 . 国标码:作为转换为 ...
- arduino nano 蓝牙_用Arduino玩转掌控板(ESP32):ESP32概述与Arduino软件准备
前言 近年来,掌控板(英文名:mPython)在创客教育中应用越来越广泛.掌控板集成了 ESP32 高性能双核芯片,具备 WiFi 和蓝牙连接功能,同时在板子上又配置了丰富的传感器,最重要的是它是专门 ...
- linux shell概述,Linux学习 -- Shell基础 -- 概述
Shell是什么? 命令解释器 编程语言 Linux支持的Shell类型 cat /etc/shells 主要学习 bash 脚本执行方式 echo echo -e 单引号 -- 原始字符串 双引号 ...
- 大数据入门第五天——离线计算之hadoop(上)概述与集群安装
一.概述 根据之前的凡技术必登其官网的原则,我们当然先得找到它的官网:http://hadoop.apache.org/ 1.什么是hadoop 先看官网介绍: The Apache™ Hadoop® ...
- 【Android应用开发】Android 蓝牙低功耗 (BLE) ( 第一篇 . 概述 . 蓝牙低功耗文档 翻译)
转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/50515359 参考 : -- 官方文档 : https://develope ...
- Linux graphic subsytem(1)_概述
1. 前言 图形子系统是linux系统中比较复杂的子系统之一:对下,它要管理形态各异的.性能各异的显示相关的器件:对上,它要向应用程序提供易用的.友好的.功能强大的图形用户界面(GUI).因此,它是l ...
- Linux下进程间通信概述
1. Linux下进程间通信概述 P83-P84 将第一页和第二页合并起来讲了 引言:前面我们学习了一下进程,我们知道多,进程间的地址空间相对独立.进程与进程间不能像线程间通过全局变量通信. 如果想进 ...
- Android 硬件 OpenGL ES 模拟设计概述
简介 Android 平台的 OpenGL ES 模拟由多个组件实现,它们是: 一些宿主机的 "翻译器" 库.它们实现了由 Khronos 定义的 EGL,GLES 1.1 和 G ...
- HoloLens开发手记 - HoloLens shell概述 HoloLens shell overview
使用HoloLens时,shell是由你周围的世界和来自系统的全息图像构成.我们将这种空间成为混合世界(mixed world). shell包含了一个可以让你将全息图像和应用放置在世界中的开始菜单( ...
- [EDA] 第1章 EDA技术概述-潘松版
第1章 EDA技术概述 知识点: 1.1 EDA技术及其发展 名称概念: EDA:Electronic Design Automation,即电子设计自动化,是指是以计算机为平台,使用通用软件包,开展 ...
最新文章
- 类项目中的配置文件app.config在打包安装后的信息获取的问题
- 前端开发知识总结思维导图
- 数据源配置和自动管理
- 高数第七章知识点框架
- 微软发布紧急更新:修复Flash高危漏洞
- djangoORM数据类型及基本操作
- php入门公开课,【PHP公开课|送你一篇有关laravel入门教程的php菜鸟笔记】- 环球网校...
- Elasticsearch使用REST API实现全文检索
- 每天一道剑指offer-对称的二叉树
- Qt——菜单栏、工具栏、状态栏
- 令牌环网概念_令牌环网工作原理_令牌环网为什么没人用
- java生成xps文件_Java 将 Excel 转为PDF、图片、html、XPS、XML、CSV
- 全年腾飞计划笔记(腾飞笔记)
- https://acs.jxnu.edu.cn/problem/GYM103495E
- 平面设计分析之图形创意解析
- ijkplayer 录像 截图功能,支持rtsp rtmp http 流媒体
- 论如何进行培养独立解决问题的能力
- NB-IoT通信模组读取IMEI、ICCID、IMSI
- Linux系统安装Anaconda3保姆级教程
- 编写简单的六轴机械臂
热门文章
- 关于DLL注入的理解
- SSM框架整合(参考尚硅谷视频和文档
- 智哪儿观察:苹果的智能家居为什么没做起来?
- 关于Eclipse优化记录
- cad页面布局快捷键_cad布局窗口快捷键
- 点云配准(一)— ICP方法
- 机器视觉检测:电阻电容的二次筛选提高效率及达成环保目的
- k2p拆机ttl刷breed_【1.10】k2p A版 22.10.3.42;22.10.3.38;拆机TTL刷BREED;B版 21.6.25.20刷机 图文教程...
- 超详细linux部署ecshop流程
- BPM 与 SOA的演进与展望