這裏,我們不會爲真實的硬件設備編寫內核驅動程序。爲了方便描述爲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 "[email protected]"
- /*設備打開和關閉接口*/
- 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文件了。