从java层到framework到JNI到HAL到kernel的hello 例子
来源:互联网 发布:js 字符串寻找子串 编辑:程序博客网 时间:2024/05/17 22:49
转载出处:http://blog.csdn.net/poltroon/article/details/7978264
在智能手机时代,每个品牌的手机都有自己的个性特点。正是依靠这种与众不同的个性来吸引用户,营造品牌凝聚力和用户忠城度,典型的代表非iphone莫属了。据统计,截止2011年5月,AppStore的应用软件数量达381062个,位居第一,而Android Market的应用软件数量达294738,紧随AppStore后面,并有望在8月份越过AppStore。随着Android系统逐步扩大市场占有率,终端设备的多样性亟需更多的移动开发人员的参与。据业内统计,Android研发人才缺口至少30万。目前,对Android人才需求一类是偏向硬件驱动的Android人才需求,一类是偏向软件应用的Android人才需求。总的来说,对有志于从事Android硬件驱动的开发工程师来说,现在是一个大展拳脚的机会。那么,就让我们一起来看看如何为Android系统编写内核驱动程序吧。
这里,我们不会为真实的硬件设备编写内核驱动程序。为了方便描述为Android系统编写内核驱动程序的过程,我们使用一个虚拟的硬件设备,这个设备只有一个4字节的寄存器,它可读可写。想起我们第一次学习程序语言时,都喜欢用“Hello, World”作为例子,这里,我们就把这个虚拟的设备命名为“hello”,而这个内核驱动程序也命名为hello驱动程序。其实,Android内核驱动程序和一般Linux内核驱动程序的编写方法是一样的,都是以Linux模块的形式实现的,具体可参考前面Android学习启动篇一文中提到的Linux Device Drivers一书。不过,这里我们还是从Android系统的角度来描述Android内核驱动程序的编写和编译过程。
一. 参照前面两篇文章在Ubuntu上下载、编译和安装Android最新源代码和在Ubuntu上下载、编译和安装Android最新内核源代码(Linux Kernel)准备好Android内核驱动程序开发环境。
二. 进入到kernel/common/drivers目录,新建hello目录:
USER-NAME@MACHINE-NAME:~/Android$ cd kernel/common/drivers
USER-NAME@MACHINE-NAME:~/Android/kernel/common/drivers$ mkdir hello
三. 在hello目录中增加hello.h文件:
- #ifndef _HELLO_ANDROID_H_
- #define _HELLO_ANDROID_H_
- #include <linux/cdev.h>
- #include <linux/semaphore.h>
- #define HELLO_DEVICE_NODE_NAME "hello"
- #define HELLO_DEVICE_FILE_NAME "hello"
- #define HELLO_DEVICE_PROC_NAME "hello"
- #define HELLO_DEVICE_CLASS_NAME "hello"
- struct hello_android_dev {
- int val;
- struct semaphore sem;
- struct cdev dev;
- };
- #endif
这个头文件定义了一些字符串常量宏,在后面我们要用到。此外,还定义了一个字符设备结构体hello_android_dev,这个就是我们虚拟的硬件设备了,val成员变量就代表设备里面的寄存器,它的类型为int,sem成员变量是一个信号量,是用同步访问寄存器val的,dev成员变量是一个内嵌的字符设备,这个Linux驱动程序自定义字符设备结构体的标准方法。
四.在hello目录中增加hello.c文件,这是驱动程序的实现部分。驱动程序的功能主要是向上层提供访问设备的寄存器的值,包括读和写。这里,提供了三种访问设备寄存器的方法,一是通过proc文件系统来访问,二是通过传统的设备文件的方法来访问,三是通过devfs文件系统来访问。下面分段描述该驱动程序的实现。
首先是包含必要的头文件和定义三种访问设备的方法:
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/types.h>
- #include <linux/fs.h>
- #include <linux/proc_fs.h>
- #include <linux/device.h>
- #include <asm/uaccess.h>
- #include "hello.h"
- /*主设备和从设备号变量*/
- static int hello_major = 0;
- static int hello_minor = 0;
- /*设备类别和设备变量*/
- static struct class* hello_class = NULL;
- static struct hello_android_dev* hello_dev = NULL;
- /*传统的设备文件操作方法*/
- static int hello_open(struct inode* inode, struct file* filp);
- static int hello_release(struct inode* inode, struct file* filp);
- static ssize_t hello_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos);
- static ssize_t hello_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos);
- /*设备文件操作方法表*/
- static struct file_operations hello_fops = {
- .owner = THIS_MODULE,
- .open = hello_open,
- .release = hello_release,
- .read = hello_read,
- .write = hello_write,
- };
- /*访问设置属性方法*/
- static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr, char* buf);
- static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count);
- /*定义设备属性*/
- static DEVICE_ATTR(val, S_IRUGO | S_IWUSR, hello_val_show, hello_val_store);
定义传统的设备文件访问方法,主要是定义hello_open、hello_release、hello_read和hello_write这四个打开、释放、读和写设备文件的方法:
- /*打开设备方法*/
- static int hello_open(struct inode* inode, struct file* filp) {
- struct hello_android_dev* dev;
- /*将自定义设备结构体保存在文件指针的私有数据域中,以便访问设备时拿来用*/
- dev = container_of(inode->i_cdev, struct hello_android_dev, dev);
- filp->private_data = dev;
- return 0;
- }
- /*设备文件释放时调用,空实现*/
- static int hello_release(struct inode* inode, struct file* filp) {
- return 0;
- }
- /*读取设备的寄存器val的值*/
- static ssize_t hello_read(struct file* filp, char __user *buf, size_t count, loff_t* f_pos) {
- ssize_t err = 0;
- struct hello_android_dev* dev = filp->private_data;
- /*同步访问*/
- if(down_interruptible(&(dev->sem))) {
- return -ERESTARTSYS;
- }
- if(count < sizeof(dev->val)) {
- goto out;
- }
- /*将寄存器val的值拷贝到用户提供的缓冲区*/
- if(copy_to_user(buf, &(dev->val), sizeof(dev->val))) {
- err = -EFAULT;
- goto out;
- }
- err = sizeof(dev->val);
- out:
- up(&(dev->sem));
- return err;
- }
- /*写设备的寄存器值val*/
- static ssize_t hello_write(struct file* filp, const char __user *buf, size_t count, loff_t* f_pos) {
- struct hello_android_dev* dev = filp->private_data;
- ssize_t err = 0;
- /*同步访问*/
- if(down_interruptible(&(dev->sem))) {
- return -ERESTARTSYS;
- }
- if(count != sizeof(dev->val)) {
- goto out;
- }
- /*将用户提供的缓冲区的值写到设备寄存器去*/
- if(copy_from_user(&(dev->val), buf, count)) {
- err = -EFAULT;
- goto out;
- }
- err = sizeof(dev->val);
- out:
- up(&(dev->sem));
- return err;
- }
定义通过devfs文件系统访问方法,这里把设备的寄存器val看成是设备的一个属性,通过读写这个属性来对设备进行访问,主要是实现hello_val_show和hello_val_store两个方法,同时定义了两个内部使用的访问val值的方法__hello_get_val和__hello_set_val:
- /*读取寄存器val的值到缓冲区buf中,内部使用*/
- static ssize_t __hello_get_val(struct hello_android_dev* dev, char* buf) {
- int val = 0;
- /*同步访问*/
- if(down_interruptible(&(dev->sem))) {
- return -ERESTARTSYS;
- }
- val = dev->val;
- up(&(dev->sem));
- return snprintf(buf, PAGE_SIZE, "%d\n", val);
- }
- /*把缓冲区buf的值写到设备寄存器val中去,内部使用*/
- static ssize_t __hello_set_val(struct hello_android_dev* dev, const char* buf, size_t count) {
- int val = 0;
- /*将字符串转换成数字*/
- val = simple_strtol(buf, NULL, 10);
- /*同步访问*/
- if(down_interruptible(&(dev->sem))) {
- return -ERESTARTSYS;
- }
- dev->val = val;
- up(&(dev->sem));
- return count;
- }
- /*读取设备属性val*/
- static ssize_t hello_val_show(struct device* dev, struct device_attribute* attr, char* buf) {
- struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);
- return __hello_get_val(hdev, buf);
- }
- /*写设备属性val*/
- static ssize_t hello_val_store(struct device* dev, struct device_attribute* attr, const char* buf, size_t count) {
- struct hello_android_dev* hdev = (struct hello_android_dev*)dev_get_drvdata(dev);
- return __hello_set_val(hdev, buf, count);
- }
定义通过proc文件系统访问方法,主要实现了hello_proc_read和hello_proc_write两个方法,同时定义了在proc文件系统创建和删除文件的方法hello_create_proc和hello_remove_proc:
- /*读取设备寄存器val的值,保存在page缓冲区中*/
- static ssize_t hello_proc_read(char* page, char** start, off_t off, int count, int* eof, void* data) {
- if(off > 0) {
- *eof = 1;
- return 0;
- }
- return __hello_get_val(hello_dev, page);
- }
- /*把缓冲区的值buff保存到设备寄存器val中去*/
- static ssize_t hello_proc_write(struct file* filp, const char __user *buff, unsigned long len, void* data) {
- int err = 0;
- char* page = NULL;
- if(len > PAGE_SIZE) {
- printk(KERN_ALERT"The buff is too large: %lu.\n", len);
- return -EFAULT;
- }
- page = (char*)__get_free_page(GFP_KERNEL);
- if(!page) {
- printk(KERN_ALERT"Failed to alloc page.\n");
- return -ENOMEM;
- }
- /*先把用户提供的缓冲区值拷贝到内核缓冲区中去*/
- if(copy_from_user(page, buff, len)) {
- printk(KERN_ALERT"Failed to copy buff from user.\n");
- err = -EFAULT;
- goto out;
- }
- err = __hello_set_val(hello_dev, page, len);
- out:
- free_page((unsigned long)page);
- return err;
- }
- /*创建/proc/hello文件*/
- static void hello_create_proc(void) {
- struct proc_dir_entry* entry;
- entry = create_proc_entry(HELLO_DEVICE_PROC_NAME, 0, NULL);
- if(entry) {
- entry->owner = THIS_MODULE;
- entry->read_proc = hello_proc_read;
- entry->write_proc = hello_proc_write;
- }
- }
- /*删除/proc/hello文件*/
- static void hello_remove_proc(void) {
- remove_proc_entry(HELLO_DEVICE_PROC_NAME, NULL);
- }
最后,定义模块加载和卸载方法,这里只要是执行设备注册和初始化操作:
- /*初始化设备*/
- static int __hello_setup_dev(struct hello_android_dev* dev) {
- int err;
- dev_t devno = MKDEV(hello_major, hello_minor);
- memset(dev, 0, sizeof(struct hello_android_dev));
- cdev_init(&(dev->dev), &hello_fops);
- dev->dev.owner = THIS_MODULE;
- dev->dev.ops = &hello_fops;
- /*注册字符设备*/
- err = cdev_add(&(dev->dev),devno, 1);
- if(err) {
- return err;
- }
- /*初始化信号量和寄存器val的值*/
- init_MUTEX(&(dev->sem));
- dev->val = 0;
- return 0;
- }
- /*模块加载方法*/
- static int __init hello_init(void){
- int err = -1;
- dev_t dev = 0;
- struct device* temp = NULL;
- printk(KERN_ALERT"Initializing hello device.\n");
- /*动态分配主设备和从设备号*/
- err = alloc_chrdev_region(&dev, 0, 1, HELLO_DEVICE_NODE_NAME);
- if(err < 0) {
- printk(KERN_ALERT"Failed to alloc char dev region.\n");
- goto fail;
- }
- hello_major = MAJOR(dev);
- hello_minor = MINOR(dev);
- /*分配helo设备结构体变量*/
- hello_dev = kmalloc(sizeof(struct hello_android_dev), GFP_KERNEL);
- if(!hello_dev) {
- err = -ENOMEM;
- printk(KERN_ALERT"Failed to alloc hello_dev.\n");
- goto unregister;
- }
- /*初始化设备*/
- err = __hello_setup_dev(hello_dev);
- if(err) {
- printk(KERN_ALERT"Failed to setup dev: %d.\n", err);
- goto cleanup;
- }
- /*在/sys/class/目录下创建设备类别目录hello*/
- hello_class = class_create(THIS_MODULE, HELLO_DEVICE_CLASS_NAME);
- if(IS_ERR(hello_class)) {
- err = PTR_ERR(hello_class);
- printk(KERN_ALERT"Failed to create hello class.\n");
- goto destroy_cdev;
- }
- /*在/dev/目录和/sys/class/hello目录下分别创建设备文件hello*/
- temp = device_create(hello_class, NULL, dev, "%s", HELLO_DEVICE_FILE_NAME);
- if(IS_ERR(temp)) {
- err = PTR_ERR(temp);
- printk(KERN_ALERT"Failed to create hello device.");
- goto destroy_class;
- }
- /*在/sys/class/hello/hello目录下创建属性文件val*/
- err = device_create_file(temp, &dev_attr_val);
- if(err < 0) {
- printk(KERN_ALERT"Failed to create attribute val.");
- goto destroy_device;
- }
- dev_set_drvdata(temp, hello_dev);
- /*创建/proc/hello文件*/
- hello_create_proc();
- printk(KERN_ALERT"Succedded to initialize hello device.\n");
- return 0;
- destroy_device:
- device_destroy(hello_class, dev);
- destroy_class:
- class_destroy(hello_class);
- destroy_cdev:
- cdev_del(&(hello_dev->dev));
- cleanup:
- kfree(hello_dev);
- unregister:
- unregister_chrdev_region(MKDEV(hello_major, hello_minor), 1);
- fail:
- return err;
- }
- /*模块卸载方法*/
- static void __exit hello_exit(void) {
- dev_t devno = MKDEV(hello_major, hello_minor);
- printk(KERN_ALERT"Destroy hello device.\n");
- /*删除/proc/hello文件*/
- hello_remove_proc();
- /*销毁设备类别和设备*/
- if(hello_class) {
- device_destroy(hello_class, MKDEV(hello_major, hello_minor));
- class_destroy(hello_class);
- }
- /*删除字符设备和释放设备内存*/
- if(hello_dev) {
- cdev_del(&(hello_dev->dev));
- kfree(hello_dev);
- }
- /*释放设备号*/
- unregister_chrdev_region(devno, 1);
- }
- MODULE_LICENSE("GPL");
- MODULE_DESCRIPTION("First Android Driver");
- module_init(hello_init);
- module_exit(hello_exit);
五.在hello目录中新增Kconfig和Makefile两个文件,其中Kconfig是在编译前执行配置命令make menuconfig时用到的,而Makefile是执行编译命令make是用到的:
Kconfig文件的内容
一. 参照在Ubuntu上为Android系统编写Linux内核驱动程序一文,准备好Linux驱动程序。使用Android模拟器加载包含这个Linux驱动程序的内核文件,并且使用adb shell命令连接上模拟,验证在/dev目录中存在设备文件hello。
二. 进入到Android源代码工程的external目录,创建hello目录:
USER-NAME@MACHINE-NAME:~/Android$ cd external
USER-NAME@MACHINE-NAME:~/Android/external$ mkdir hello
三. 在hello目录中新建hello.c文件:
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #define DEVICE_NAME "/dev/hello"
- int main(int argc, char** argv)
- {
- int fd = -1;
- int val = 0;
- fd = open(DEVICE_NAME, O_RDWR);
- if(fd == -1) {
- printf("Failed to open device %s.\n", DEVICE_NAME);
- return -1;
- }
- printf("Read original value:\n");
- read(fd, &val, sizeof(val));
- printf("%d.\n\n", val);
- val = 5;
- printf("Write value %d to %s.\n\n", val, DEVICE_NAME);
- write(fd, &val, sizeof(val));
- printf("Read the value again:\n");
- read(fd, &val, sizeof(val));
- printf("%d.\n\n", val);
- close(fd);
- return 0;
- }
这个程序的作用中,打开/dev/hello文件,然后先读出/dev/hello文件中的值,接着写入值5到/dev/hello中去,最后再次读出/dev/hello文件中的值,看看是否是我们刚才写入的值5。从/dev/hello文件读写的值实际上就是我们虚拟的硬件的寄存器val的值。
四. 在hello目录中新建Android.mk文件:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := hello
LOCAL_SRC_FILES := $(call all-subdir-c-files)
include $(BUILD_EXECUTABLE)
注意,BUILD_EXECUTABLE表示我们要编译的是可执行程序。
五. 参照如何单独编译Android源代码中的模块一文,使用mmm命令进行编译:
USER-NAME@MACHINE-NAME:~/Android$ mmm ./external/hello
编译成功后,就可以在out/target/product/gerneric/system/bin目录下,看到可执行文件hello了。
六. 重新打包Android系统文件system.img:
USER-NAME@MACHINE-NAME:~/Android$ make snod
这样,重新打包后的system.img文件就包含刚才编译好的hello可执行文件了。
七. 运行模拟器,使用/system/bin/hello可执行程序来访问Linux内核驱动程序:
USER-NAME@MACHINE-NAME:~/Android$ emulator -kernel ./kernel/common/arch/arm/boot/zImage &
USER-NAME@MACHINE-NAME:~/Android$ adb shell
root@android:/ # cd system/bin
root@android:/system/bin # ./hello
Read the original value:
0.
Write value 5 to /dev/hello.
Read the value again:
5.
看到这个结果,就说我们编写的C可执行程序可以访问我们编写的Linux内核驱动程序了。
介绍完了如何使用C语言编写的可执行程序来访问我们的Linux内核驱动程序,读者可能会问,能不能在Android的Application Frameworks提供Java接口来访问Linux内核驱动程序呢?可以的,接下来的几篇文章中,我们将介绍如何在Android的Application Frameworks中,增加Java接口来访问Linux内核驱动程序,敬请期待
----------------------------------分割线-----------------------------------------------------
在Android硬件抽象层(HAL)概要介绍和学习计划一文中,我们简要介绍了在Android系统为为硬件编写驱动程序的方法。简单来说,硬件驱动程序一方面分布在Linux内核中,另一方面分布在用户空间的硬件抽象层中。接着,在Ubuntu上为Android系统编写Linux内核驱动程序一文中举例子说明了如何在Linux内核编写驱动程序。在这一篇文章中,我们将继续介绍Android系统硬件驱动程序的另一方面实现,即如何在硬件抽象层中增加硬件模块来和内核驱动程序交互。在这篇文章中,我们还将学习到如何在Android系统创建设备文件时用类似Linux的udev规则修改设备文件模式的方法。
一. 参照在Ubuntu上为Android系统编写Linux内核驱动程序一文所示,准备好示例内核驱动序。完成这个内核驱动程序后,便可以在Android系统中得到三个文件,分别是/dev/hello、/sys/class/hello/hello/val和/proc/hello。在本文中,我们将通过设备文件/dev/hello来连接硬件抽象层模块和Linux内核驱动程序模块。
二. 进入到在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
- /*定义模块ID*/
- #define HELLO_HARDWARE_MODULE_ID "hello"
- /*硬件模块结构体*/
- struct hello_module_t {
- struct hw_module_t common;
- };
- /*硬件接口结构体*/
- struct hello_device_t {
- struct hw_device_t common;
- int fd;
- int (*set_val)(struct hello_device_t* dev, int val);
- int (*get_val)(struct hello_device_t* dev, int* val);
- };
- __END_DECLS
- #endif
这里按照Android硬件抽象层规范的要求,分别定义模块ID、模块结构体以及硬件接口结构体。在硬件接口结构体中,fd表示设备文件描述符,对应我们将要处理的设备文件"/dev/hello",set_val和get_val为该HAL对上提供的函数接口。
三. 进入到hardware/libhardware/modules目录,新建hello目录,并添加hello.c文件。 hello.c的内容较多,我们分段来看。
首先是包含相关头文件和定义相关结构:
- #define LOG_TAG "HelloStub"
- #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);
- /*模块方法表*/
- static struct hw_module_methods_t hello_module_methods = {
- open: hello_device_open
- };
- /*模块实例变量*/
- struct hello_module_t HAL_MODULE_INFO_SYM = {
- common: {
- tag: HARDWARE_MODULE_TAG,
- version_major: 1,
- version_minor: 0,
- id: HELLO_HARDWARE_MODULE_ID,
- name: MODULE_NAME,
- author: MODULE_AUTHOR,
- methods: &hello_module_methods,
- }
- };
这里,实例变量名必须为HAL_MODULE_INFO_SYM,tag也必须为HARDWARE_MODULE_TAG,这是Android硬件抽象层规范规定的。
定义hello_device_open函数:
- static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) {
- struct hello_device_t* dev;dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t));
- 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) {
- 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.");
- 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;
- }
-------------------------------------分割线------------------------------------------
在上两篇文章中,我们介绍了如何为Android系统的硬件编写驱动程序,包括如何在Linux内核空间实现内核驱动程序和在用户空间实现硬件抽象层接口。实现这两者的目的是为了向更上一层提供硬件访问接口,即为Android的Application Frameworks层提供硬件服务。我们知道,Android系统的应用程序是用Java语言编写的,而硬件驱动程序是用C语言来实现的,那么,Java接口如何去访问C接口呢?众所周知,Java提供了JNI方法调用,同样,在Android系统中,Java应用程序通过JNI来调用硬件抽象层接口。在这一篇文章中,我们将介绍如何为Android硬件抽象层接口编写JNI方法,以便使得上层的Java应用程序能够使用下层提供的硬件服务。
一. 参照在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序一文,准备好硬件抽象层模块,确保Android系统镜像文件system.img已经包含hello.default模块。
二. 进入到frameworks/base/services/jni目录,新建com_android_server_HelloService.cpp文件:
USER-NAME@MACHINE-NAME:~/Android$ cd frameworks/base/services/jni
USER-NAME@MACHINE-NAME:~/Android/frameworks/base/services/jni$ vi com_android_server_HelloService.cpp
在com_android_server_HelloService.cpp文件中,实现JNI方法。注意文件的命令方法,com_android_server前缀表示的是包名,表示硬件服务HelloService是放在frameworks/base/services/java目录下的com/android/server目录的,即存在一个命令为com.android.server.HelloService的类。这里,我们暂时略去HelloService类的描述,在下一篇文章中,我们将回到HelloService类来。简单地说,HelloService是一个提供Java接口的硬件访问服务类。
首先是包含相应的头文件:
- #define LOG_TAG "HelloService"
- #include "jni.h"
- #include "JNIHelp.h"
- #include "android_runtime/AndroidRuntime.h"
- #include <utils/misc.h>
- #include <utils/Log.h>
- #include <hardware/hardware.h>
- #include <hardware/hello.h>
- #include <stdio.h>
接着定义hello_init、hello_getVal和hello_setVal三个JNI方法:
- namespace android
- {
- /*在硬件抽象层中定义的硬件访问结构体,参考<hardware/hello.h>*/
- struct hello_device_t* hello_device = NULL;
- /*通过硬件抽象层定义的硬件访问接口设置硬件寄存器val的值*/
- static void hello_setVal(JNIEnv* env, jobject clazz, jint value) {
- int val = value;
- LOGI("Hello JNI: set value %d to device.", val);
- if(!hello_device) {
- LOGI("Hello JNI: device is not open.");
- return;
- }
- hello_device->set_val(hello_device, val);
- }
- /*通过硬件抽象层定义的硬件访问接口读取硬件寄存器val的值*/
- static jint hello_getVal(JNIEnv* env, jobject clazz) {
- int val = 0;
- if(!hello_device) {
- LOGI("Hello JNI: device is not open.");
- return val;
- }
- hello_device->get_val(hello_device, &val);
- LOGI("Hello JNI: get value %d from device.", val);
- return val;
- }
- /*通过硬件抽象层定义的硬件模块打开接口打开硬件设备*/
- static inline int hello_device_open(const hw_module_t* module, struct hello_device_t** device) {
- return module->methods->open(module, HELLO_HARDWARE_MODULE_ID, (struct hw_device_t**)device);
- }
- /*通过硬件模块ID来加载指定的硬件抽象层模块并打开硬件*/
- static jboolean hello_init(JNIEnv* env, jclass clazz) {
- hello_module_t* module;
- LOGI("Hello JNI: initializing......");
- if(hw_get_module(HELLO_HARDWARE_MODULE_ID, (const struct hw_module_t**)&module) == 0) {
- LOGI("Hello JNI: hello Stub found.");
- if(hello_device_open(&(module->common), &hello_device) == 0) {
- LOGI("Hello JNI: hello device is open.");
- return 0;
- }
- LOGE("Hello JNI: failed to open hello device.");
- return -1;
- }
- LOGE("Hello JNI: failed to get hello stub module.");
- return -1;
- }
- /*JNI方法表*/
- static const JNINativeMethod method_table[] = {
- {"init_native", "()Z", (void*)hello_init},
- {"setVal_native", "(I)V", (void*)hello_setVal},
- {"getVal_native", "()I", (void*)hello_getVal},
- };
- /*注册JNI方法*/
- int register_android_server_HelloService(JNIEnv *env) {
- return jniRegisterNativeMethods(env, "com/android/server/HelloService", method_table, NELEM(method_table));
- }
- };
注意,在hello_init函数中,通过Android硬件抽象层提供的hw_get_module方法来加载模块ID为HELLO_HARDWARE_MODULE_ID的硬件抽象层模块,其中,HELLO_HARDWARE_MODULE_ID是在<hardware/hello.h>中定义的。Android硬件抽象层会根据HELLO_HARDWARE_MODULE_ID的值在Android系统的/system/lib/hw目录中找到相应的模块,然后加载起来,并且返回hw_module_t接口给调用者使用。在jniRegisterNativeMethods函数中,第二个参数的值必须对应HelloService所在的包的路径,即com.android.server.HelloService。
三. 修改同目录下的onload.cpp文件,首先在namespace android增加register_android_server_HelloService函数声明:
namespace android {
..............................................................................................
int register_android_server_HelloService(JNIEnv *env);
};
一. 参照在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口一文所示,为硬件抽象层模块准备好JNI方法调用层。
二. 在Android系统中,硬件服务一般是运行在一个独立的进程中为各种应用程序提供服务。因此,调用这些硬件服务的应用程序与这些硬件服务之间的通信需要通过代理来进行。为此,我们要先定义好通信接口。进入到frameworks/base/core/java/android/os目录,新增IHelloService.aidl接口定义文件:
USER-NAME@MACHINE-NAME:~/Android$ cd frameworks/base/core/java/android/os
USER-NAME@MACHINE-NAME:~/Android/frameworks/base/core/java/android/os$ vi IHelloService.aidl
IHelloService.aidl定义了IHelloService接口:
- package android.os;
- interface IHelloService {
- void setVal(int val);
- int getVal();
- }
IHelloService接口主要提供了设备和获取硬件寄存器val的值的功能,分别通过setVal和getVal两个函数来实现。
三.返回到frameworks/base目录,打开Android.mk文件,修改LOCAL_SRC_FILES变量的值,增加IHelloService.aidl源文件:
## READ ME: ########################################################
##
## When updating this list of aidl files, consider if that aidl is
## part of the SDK API. If it is, also add it to the list below that
## is preprocessed and distributed with the SDK. This list should
## not contain any aidl files for parcelables, but the one below should
## if you intend for 3rd parties to be able to send those objects
## across process boundaries.
##
## READ ME: ########################################################
LOCAL_SRC_FILES += /
....................................................................
core/java/android/os/IVibratorService.aidl /
core/java/android/os/IHelloService.aidl /
core/java/android/service/urlrenderer/IUrlRendererService.aidl /
.....................................................................
这样,就会根据IHelloService.aidl生成相应的IHelloService.Stub接口。
五.进入到frameworks/base/services/java/com/android/server目录,新增HelloService.java文件:
- package com.android.server;
- import android.content.Context;
- import android.os.IHelloService;
- import android.util.Slog;
- public class HelloService extends IHelloService.Stub {
- private static final String TAG = "HelloService";
- HelloService() {
- init_native();
- }
- public void setVal(int val) {
- setVal_native(val);
- }
- public int getVal() {
- return getVal_native();
- }
- private static native boolean init_native();
- private static native void setVal_native(int val);
- private static native int getVal_native();
- };
HelloService主要是通过调用JNI方法init_native、setVal_native和getVal_native(见在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口一文)来提供硬件服务。
六. 修改同目录的SystemServer.java文件,在ServerThread::run函数中增加加载HelloService的代码:
@Override
public void run() {
....................................................................................
try {
Slog.i(TAG, "DiskStats Service");
ServiceManager.addService("diskstats", new DiskStatsService(context));
} catch (Throwable e) {
Slog.e(TAG, "Failure starting DiskStats Service", e);
}
try {
Slog.i(TAG, "Hello Service");
ServiceManager.addService("hello", new HelloService());
} catch (Throwable e) {
Slog.e(TAG, "Failure starting Hello Service", e);
}
......................................................................................
}
七. 编译HelloService和重新打包system.img:
USER-NAME@MACHINE-NAME:~/Android$ mmm frameworks/base/services/java
USER-NAME@MACHINE-NAME:~/Android$ make snod
这样,重新打包后的system.img系统镜像文件就在Application Frameworks层中包含了我们自定义的硬件服务HelloService了,并且会在系统启动的时候,自动加载HelloService。这时,应用程序就可以通过Java接口来访问Hello硬件服务了。我们将在下一篇文章中描述如何编写一个Java应用程序来调用这个HelloService接口来访问硬件,敬请期待
------------------------------------分割线-----------------------------------------------
我们在Android系统增加硬件服务的目的是为了让应用层的APP能够通过Java接口来访问硬件服务。那么, APP如何通过Java接口来访问Application Frameworks层提供的硬件服务呢?在这一篇文章中,我们将在Android系统的应用层增加一个内置的应用程序,这个内置的应用程序通过ServiceManager接口获取指定的服务,然后通过这个服务来获得硬件服务。
一. 参照在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务一文,在Application Frameworks层定义好自己的硬件服务HelloService,并提供IHelloService接口提供访问服务。
二. 为了方便开发,我们可以在IDE环境下使用Android SDK来开发Android应用程序。开发完成后,再把程序源代码移植到Android源代码工程目录中。使用Eclipse的Android插件ADT创建Android工程很方便,这里不述,可以参考网上其它资料。工程名称为Hello,下面主例出主要文件:
主程序是src/shy/luo/hello/Hello.java:
- package shy.luo.hello;
- import shy.luo.hello.R;
- import android.app.Activity;
- import android.os.ServiceManager;
- import android.os.Bundle;
- import android.os.IHelloService;
- import android.os.RemoteException;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class Hello extends Activity implements OnClickListener {
- private final static String LOG_TAG = "shy.luo.renju.Hello";
- private IHelloService helloService = null;
- private EditText valueText = null;
- private Button readButton = null;
- private Button writeButton = null;
- private Button clearButton = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- helloService = IHelloService.Stub.asInterface(
- ServiceManager.getService("hello"));
- valueText = (EditText)findViewById(R.id.edit_value);
- readButton = (Button)findViewById(R.id.button_read);
- writeButton = (Button)findViewById(R.id.button_write);
- clearButton = (Button)findViewById(R.id.button_clear);
- readButton.setOnClickListener(this);
- writeButton.setOnClickListener(this);
- clearButton.setOnClickListener(this);
- Log.i(LOG_TAG, "Hello Activity Created");
- }
- @Override
- public void onClick(View v) {
- if(v.equals(readButton)) {
- try {
- int val = helloService.getVal();
- String text = String.valueOf(val);
- valueText.setText(text);
- } catch (RemoteException e) {
- Log.e(LOG_TAG, "Remote Exception while reading value from device.");
- }
- }
- else if(v.equals(writeButton)) {
- try {
- String text = valueText.getText().toString();
- int val = Integer.parseInt(text);
- helloService.setVal(val);
- } catch (RemoteException e) {
- Log.e(LOG_TAG, "Remote Exception while writing value to device.");
- }
- }
- else if(v.equals(clearButton)) {
- String text = "";
- valueText.setText(text);
- }
- }
- }
程序通过ServiceManager.getService("hello")来获得HelloService,接着通过IHelloService.Stub.asInterface函数转换为IHelloService接口。其中,服务名字“hello”是系统启动时加载HelloService时指定的,而IHelloService接口定义在android.os.IHelloService中,具体可以参考在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务一文。这个程序提供了简单的读定自定义硬件有寄存器val的值的功能,通过IHelloService.getVal和IHelloService.setVal两个接口实现。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- android:gravity="center">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/value">
- </TextView>
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:id="@+id/edit_value"
- android:hint="@string/hint">
- </EditText>
- </LinearLayout>
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:gravity="center">
- <Button
- android:id="@+id/button_read"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/read">
- </Button>
- <Button
- android:id="@+id/button_write"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/write">
- </Button>
- <Button
- android:id="@+id/button_clear"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/clear">
- </Button>
- </LinearLayout>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">Hello</string>
- <string name="value">Value</string>
- <string name="hint">Please input a value...</string>
- <string name="read">Read</string>
- <string name="write">Write</string>
- <string name="clear">Clear</string>
- </resources>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="shy.luo.hello"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".Hello"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
USER-NAME@MACHINE-NAME:~/Android/packages/experimental$ vi Android.mk
五. 重新打包系统镜像文件system.img:
重新打包后的system.img文件就内置了Hello.apk文件了。
- 从java层到framework到JNI到HAL到kernel的hello 例子
- 从java层到framework到JNI到HAL到kernel的hello 例子
- 上层使用jni(从java层到framework到JNI的用法过程)
- Android Camera从App层到framework层到HAL层的初始化过程
- Android从Hal到framework层添加自定义Sensor
- Android应用层到Framework到HAL再到驱动层的整个流程分析
- Android 从framework层到HAL层调用实现(蓝牙串口)
- Android Uevent 分析,从kernel到framework
- Android Uevent 分析,从kernel到framework
- Android Uevent 分析,从kernel到framework
- Android Uevent 分析,从kernel到framework
- Android Uevent 分析,从kernel到framework
- Android Uevent 分析,从kernel到framework
- Android Uevent 分析,从kernel到framework
- Android Uevent 分析,从kernel到framework
- Android 关机流程 从kernel到framework
- Android 关机流程 从kernel到framework
- Android Uevent 分析,从kernel到framework
- Android的消息机制——概述
- 2017.3.18【NOIP提高组】模拟赛B组小结
- 连通图-SSL 1758
- POJ1679-The Unique MST
- eclipse上反编译.class文件成java文件(含解决错误)
- 从java层到framework到JNI到HAL到kernel的hello 例子
- 离散题目集合--stl版题解--离散题目3
- iOS 开发中OC 与 JS的交互
- 2017.3.24 MATLAB(三)数组与矩阵
- Java Character 类
- 初学python,爬虫小项目
- session 文件同步
- |算法讨论|二分查找 学习笔记
- 海思3536:交叉编译Qt4.8.4