在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序(老罗学习笔记3)
来源:互联网 发布:淘宝休闲运动服 编辑:程序博客网 时间:2024/06/04 17:52
在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序(老罗学习笔记3)
简单来说,硬件驱动程序一方面分布在Linux内核中,另一方面分布在用户空间的硬件抽象层中。接着,在Ubuntu上为Android系统编写Linux内核驱动程序(老罗学习笔记1)一文中举例子说明了如何在Linux内核编写驱动程序。在这一篇文章中,我们将继续介绍Android系统硬件驱动程序的另一方面实现,即如何在硬件抽象层中增加硬件模块来和内核驱动程序交互。在这篇文章中,我们还将学习到如何在Android系统创建设备文件时用类似Linux的udev规则修改设备文件模式的方法。
一. 参照在Ubuntu上为Android系统编写Linux内核驱动程序(老罗学习笔记1)一文所示,准备好示例内核驱动序。完成这个内核驱动程序后,便可以在Android系统中得到三个文件,分别是/dev/hello、/sys/class/hello/hello/val和/proc/hello。在本文中,我们将通过设备文件/dev/hello来连接硬件抽象层模块和Linux内核驱动程序模块。
HAL层总体思想请看下图:
(核心思维:在Android系统创建设备文件时用类似Linux的udev规则修改设备文件模式的方法)
二. 进入到在hardware/libhardware/include/hardware目录,新建hello.h文件:
USER-NAME@MACHINE-NAME:~/Android$ cd hardware/libhardware/include/hardware
USER-NAME@MACHINE-NAME:~/Android/hardware/libhardware/include/hardware$ vi hello.h
hello.h文件的内容如下:
#ifndef ANDROID_HELLO_INTERFACE_H #define ANDROID_HELLO_INTERFACE_H #include <hardware/hardware.h> __BEGIN_DECLS /*--扩充C语言在编译的时候按照C++编译器进行统一处理,使得C++代码能够调用C编译生成的中间代码*/ /* 以下的部分只是定义而已,起作用的是下面调用的过程 *//*定义模块ID*/ #define HELLO_HARDWARE_MODULE_ID "hello" /*对上层 1 模块ID号 */ /*硬件模块结构体*/ /*对上层 2 模块信息*/struct hello_module_t { struct hw_module_t common; }; /*硬件接口结构体*/ struct hello_device_t { /*--(四)总包 出口 --*/ /*对上层 3 接口信息*/ struct hw_device_t common; //这个玩意是主要的,可以说是地址通告的一个标志 int fd; //设备文件描述符 int (*set_val)(struct hello_device_t* dev, int val); /*--set_val什么类型?什么作用?以及后面这样复用结构体 何用??--*/ int (*get_val)(struct hello_device_t* dev, int* val); //set_reg和get_reg为该HAL对上提供的函数接口。 /*这个可以添加自己想要的数据类型*/}; __END_DECLS #endif
/*--
hw_module_t:
/** * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM * and the fields of this data structure must begin with hw_module_t * followed by module specific information. */ //每一个硬件模块都每必须有一个名为HAL_MODULE_INFO_SYM的数据结构变量,它的第一个成员的类型必须为hw_module_t typedef struct hw_module_t { /** tag must be initialized to HARDWARE_MODULE_TAG */ uint32_t tag; // 初始化硬件模块标签 /** major version number for the module */ uint16_t version_major; //主模块版本号 /** minor version number of the module */ uint16_t version_minor; //副模块版本号 /** Identifier of module */ const char *id; //模块标识符 /** Name of this module */ const char *name; //模块名字 /** Author/owner/implementor of the module */ const char *author; //编写作者 /** Modules methods */ //模块方法列表,指向hw_module_methods_t* struct hw_module_methods_t* methods; //硬件模块的方法 /** module's dso */ void* dso; //DSO: Device Software Optimization,设备软件优化“安全维和组织”(DSO) /** padding to 128 bytes, reserved for future use */ uint32_t reserved[32-7]; //填充到128字节,保留以供将来使用 } hw_module_t;
hw_device_t :
/** * Every device data structure must begin with hw_device_t * followed by module specific public methods and attributes. */ //每一个设备数据结构的第一个成员函数必须是hw_device_t类型,其次才是各个公共方法和属性 typedef struct hw_device_t { /** tag must be initialized to HARDWARE_DEVICE_TAG */ uint32_t tag; /** version number for hw_device_t */ uint32_t version; /** reference to the module this device belongs to */ struct hw_module_t* module; /** padding reserved for future use */ uint32_t reserved[12]; /** Close this device */ int (*close)(struct hw_device_t* device); } hw_device_t;
--*/
这里按照Android硬件抽象层规范的要求,分别定义①模块ID、②模块结构体以及③硬件接口结构体。在硬件接口结构体中,fd表示设备文件描述符,对应我们将要处理的设备文件"/dev/hello",set_val和get_val为该HAL对上(JNI)提供的函数接口。
三. 进入到hardware/libhardware/modules目录,新建hello目录,并添加hello.c文件。 hello.c的内容较多,我们分段来看。
首先是包含相关头文件和定义相关结构:
#define LOG_TAG "HelloStub" /*--HelloStub:什么作用?答:
HAL stub的框架比较简单,三个结构体、两个常量、一个函数,简称321架构,它的定义在:
@hardware/libhardware/include/hardware/hardware.h
@hardware/libhardware/hardware.c
--*/ #include <hardware/hardware.h> #include <hardware/hello.h> #include <fcntl.h> //文件控制#include <errno.h> //出错码#include <cutils/log.h> //?#include <cutils/atomic.h> //? #define DEVICE_NAME "/dev/hello" //需要是路径 ! #define MODULE_NAME "Hello" #define MODULE_AUTHOR "shyluo@gmail.com" //模块作者 /*设备打开和关闭接口*/ static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device); static int hello_device_close(struct hw_device_t* device); /*设备访问接口*/ static int hello_set_val(struct hello_device_t* dev, int val); static int hello_get_val(struct hello_device_t* dev, int* val); /*模块方法表,下面模块实例最后一个变量*/ //硬件模块方法列表的定义,这里只定义了一个open函数 这个结构体说白了是安排了接口信息文件static struct hw_module_methods_t hello_module_methods【流向3】 = { open: hello_device_open 【流向2】}; /*模块实例变量*/ //模块中包含了接口 struct hello_module_t HAL_MODULE_INFO_SYM 【流向4】= { //HAL_MODULE_INFO_SYM: 上层调用时的入口(相当于main)!! /*----(三,模块放包中)----*/ common: { tag: HARDWARE_MODULE_TAG, version_major: 1, version_minor: 0, id: HELLO_HARDWARE_MODULE_ID, //模块ID号 这里包含了ID name: MODULE_NAME, author: MODULE_AUTHOR, methods: &hello_module_methods, //解释看上【流向3】 这个就包含了接口 } };
这里,实例变量名必须为HAL_MODULE_INFO_SYM,tag也必须为HARDWARE_MODULE_TAG,这是Android硬件抽象层规范规定的。
-----
定义hello_device_open函数:
/*
hw_module_t* module : 模块名(在JNI使用中该变量为自身模块名)const char* name :模块IDstruct hw_device_t** device :接口信息文件
*/static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device)【流向2】 { struct hello_device_t* dev; dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t)); /*--将一个分配域的起始地址转换成struct hello_device_t* 类型 如果此函数未能成功地执行(例如内存不足),则返回空指针(NULL,'0')--*/ if(!dev) { LOGE("Hello Stub: failed to alloc space"); return -EFAULT; } memset(dev, 0, sizeof(struct hello_device_t)); //清空该段内存,然后该内存段重新赋值 dev->common.tag = HARDWARE_DEVICE_TAG; /*------(三)接口信息放包中--------*/ dev->common.version = 0; dev->common.module = (hw_module_t*)module; dev->common.close = hello_device_close; dev->set_val = hello_set_val;dev->get_val = hello_get_val; if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) { 【流向1】 //返回 -1 时,模块打开失败☆☆☆ 与硬件驱动层的入口 /*----(一)打开对应文件系统,底层hello驱动文件包地址入口 入口---*/ LOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev); return -EFAULT; } *device = &(dev->common); LOGI("Hello Stub: open /dev/hello successfully."); //LOGI是打印信息吗? 在哪定义的? return 0; }
/*--
--*/
DEVICE_NAME定义为"/dev/hello"。由于设备文件是在内核驱动里面通过device_create创建的,而device_create创建的设备文件默认只有root用户可读写,而hello_device_open一般是由上层APP来调用的,这些APP一般不具有root权限,这时候就导致打开设备文件失败:
static int hello_device_close(struct hw_device_t* device) { struct hello_device_t* hello_device = (struct hello_device_t*)device; if(hello_device) { close(hello_device->fd); free(hello_device); } return 0; } static int hello_set_val(struct hello_device_t* dev, int val) { LOGI("Hello Stub: set value %d to device.", val); write(dev->fd, &val, sizeof(val)); return 0; } static int hello_get_val(struct hello_device_t* dev, int* val) { if(!val) { LOGE("Hello Stub: error val pointer"); return -EFAULT; } read(dev->fd, val, sizeof(*val)); LOGI("Hello Stub: get value %d from device", *val); return 0; }
1
make:进入目录'/opt/FriendlyARM/tiny4412/android/android-4.1.2'
make: *** 没有规则可以创建“out/target/product/generic/obj/SHARED_LIBRARIES/audio.usb.default_intermediates/import_includes”需要的目标“out/target/product/generic/obj/SHARED_LIBRARIES/libc_intermediates/export_includes”。 停止。
make:离开目录“/opt/FriendlyARM/tiny4412/android/android-4.1.2”
切记:. setenv
2 局部程序修改
http://blog.csdn.net/oldmtn/article/details/9213869
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序(老罗学习笔记3)
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序(老罗学习笔记3)
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序 (学习老罗的)
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序 .
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- HDU 2897 邂逅明下(博弈)
- linux编程里有这么一句if (lseek(STDIN_FILENO, 0, SEEK_CUR) == -1 ),请问是de意思?
- 无线充电各种原理方案的比较
- 【bzoj3786】【星系探索】【dfs序+splay】
- jQuery中ready和load的区别
- 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序(老罗学习笔记3)
- Javascript模块化编程(三):require.js的用法
- ubuntu下vpn服务器搭建
- libpcap详解
- flume学习(九):自定义拦截器
- STDIN_FILENO与STDIN的区别
- sqlserver中select造成死锁
- List的普通for loop delete 连续元素有遗漏的解决
- Android实时获取当前下载速度