简单来说,硬件驱动程序一方面分布在Linux 内核中,另一方面分布在用户空间的硬件抽象层中。接着,在Ubuntu 上为Android系统编写Linux 内核驱动程序一文中举例子说明了如何在Linux 内核编写驱动程序。在这一篇文章中,我们将继续介绍Android 系统硬件驱动程序的另一方面实现,即如何在硬件抽象层中增加硬件模块来和内核驱动程序交互。在这篇文章中,我们还将学习到如何在Android 系统创建设备文件时用类似Linux 的udev 规则修改设备文件模式的方法。

step1: 进入到在hardware/libhardware/include/hardware 目录,新建hello.h 文件

hello.h

#ifndef Android_HELLO_INTERFACE_H#define ANDROID_HELLO_INTERFACE_H#include <hardware/hardware.h>__BEGIN_DECLS#define HELLO_HARDWARE_MODULE_ID "hello_hal"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

step2: 进入到hardware/libhardware/modules 目录,新建hello 目录,并添加hello.c 文件。

hello.c

#include <hardware/hardware.h>#include <hardware/hello.h>#include <fcntl.h>#include <errno.h>#include <cutils/log.h>#include <cutils/atomic.h>#include <android/log.h>#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,"hello_stub",__VA_ARGS__)#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,"hello_stub",__VA_ARGS__)#define LOG_TAG "HelloStub"#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 硬件抽象层规范规定的 */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 权限,这时候就导致打开设备文件失败:Hello Stub: failed to open /dev/hello -- Permission denied.解决办法是类似于Linux 的udev 规则,打开Android 源代码工程目录下,进入到system/core/rootdir 目录,里面有一个名为uevent.rc 文件,往里面添加一行:/dev/hello 0666 root 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;}

step3: 继续在hello 目录下新建Android.mk 文件:

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_LDLIBS := -llogLOCAL_MODULE_TAGS := optionalLOCAL_PRELINK_MODULE := falseLOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hwLOCAL_SHARED_LIBRARIES := liblogLOCAL_SRC_FILES := hello.cLOCAL_MODULE := hello.defaultinclude $(BUILD_SHARED_LIBRARY)

注意,LOCAL_MODULE 的定义规则,hello 后面跟有default,hello.default 能够保证我们的模块总能被硬象抽象层加载到。

step4: 编译hello.default.so

cd hardware/libhardware/moudles/hello

mm

编译成功后,就可以在out/target/product/maguro/system/lib/hw 目录下看到hello.default.so 文件了。

虽然我们在Android 系统为我们自己的硬件增加了一个硬件抽象层模块,但是现在Java 应用程序还不能访问到我们的硬件。我们还必须编写JNI 方法和在Android 的Application Frameworks 层增加API 接口,才能让上层Application 访问我们的硬件。在接下来的文章中,我们还将完成这一系统过程,使得我们能够在Java 应用程序中访问我们自己定制的硬件。

更多相关文章

  1. android 扫描文件(sdcard添加新的音乐文件时候后,可扫描到)
  2. Android stutio 中怎么将XML中文件快速findById——Android Layo
  3. init进程 && 解析Android启动脚本init.rc && 修改它使不启动andr
  4. Android Developers:保存文件
  5. Android中读取assets目录下的文件详细介绍
  6. Android串口的兼收 Android上执行硬件指令
  7. android studio导入gbk编码的工程文件导致的乱码问题
  8. gen文件无法生成
  9. Android 数据缓存-文件存储

随机推荐

  1. android下lcd显示的两个问题
  2. 解决 Linux/Ubuntu: adb shell insufficie
  3. Official Note of Android (important pa
  4. 不同Activity之间传递数据
  5. android蓝牙开发入门到精通3---服务端客
  6. android中的Sqlite数据库操作
  7. Android(安卓)修改默认按键不震动
  8. android spinner下拉选择框
  9. android 存储一些app配置信息
  10. android 使用md5加密