1、HAL层的hardware module主要实现文件为:

hardware/libhardware/hareware.c
hardware/libhardware/include/hardware/hardware.h

Hareware.c中的内容在此就不做解释,可自行学习。

2、hardware.c中三个重要的结构体:

struct hw_module_t; //描述一个硬件模块
struct hw_module_methods_t;//定义了操作设备的方法,只有一个open函数
struct hw_device_t;//表示一个硬件设备,存储了各种硬件设备的公共属性和方法

3、三个结构的关系图:

4、三个结构体的描述:

typedef struct hw_device_t {uint32_t tag; /** tag must be initialized to HARDWARE_DEVICE_TAG */ 标识符uint32_t version; /** version number for hw_device_t */             版本号struct hw_module_t* module; /** reference to the module this device belongs to */   该硬件属于哪个moduleuint32_t reserved[12]; /** padding reserved for future use */int (*close)(struct hw_device_t* device); /** Close this device */  关闭设备操作
} hw_device_t;
typedef struct hw_module_t {uint32_t tag; /** tag must be initialized to HARDWARE_MODULE_TAG */ 标识符uint16_t version_major; /** major version number for the module */ 主版本号uint16_t version_minor; /** minor version number of the module */ 次版本号const char *id; /** Identifier of module */                    id,决定了库的文件名const char *name; /** Name of this module */                    模块的名字const char *author; /** Author/owner/implementor of the module */   模块作者struct hw_module_methods_t* methods; /** Modules methods */         模块的操作方法void* dso; /** module's dso */uint32_t reserved[32-7]; /** padding to 128 bytes, reserved for future use */
} hw_module_t;
typedef struct hw_module_methods_t {int (*open)(const struct hw_module_t* module, const char* id,struct hw_device_t** device);
} hw_module_methods_t;

5、

举个栗子:

JNI层文件(frameworks/base/services/jni)需包含一下头文件

hello.h文件

#ifndef ANDROID_HELLO_INTERFACE_H
#define ANDROID_HELLO_INTERFACE_H
#include <hardware/hardware.h>__BEGIN_DECLS/*定义模块ID,即名字*/
#define HELLO_LED_HARDWARE_MODULE_ID "hello_led"/*硬件模块结构体*/
struct hello_module_t {struct hw_module_t common;
};/*硬件接口结构体,此提供给JNI层使用*/
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

hello.c文件

#define LOG_TAG "HelloLedStub"#include <hardware/hardware.h>
#include <hardware/hello_led.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 "yanlei.liu@roiland.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_LED_HARDWARE_MODULE_ID,name: MODULE_NAME,author: MODULE_AUTHOR,methods: &hello_module_methods,}
};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) {ALOGE("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) {ALOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev);return -EFAULT;}*device = &(dev->common);ALOGE("Hello Stub: open /dev/hello successfully.");return 0;
}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) {ALOGE("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) {ALOGE("Hello Stub: error val pointer");return -EFAULT;}read(dev->fd, val, sizeof(*val));ALOGE("Hello Stub: get value %d from device", *val);return 0;
}
/*
static int hello_hw_ioctl(struct file *file, unsigned int cmd, unsigned long arg){ALOGE("hello_hw_ioctl: set cmd %d to device.", cmd);hello_ioctl(file, cmd, arg);return 0;
}
*/

参考:

http://blog.chinaunix.net/uid-22030783-id-3056757.html

http://blog.csdn.net/luoshengyang/article/details/6575988

http://blog.csdn.net/luoshengyang/article/details/6573809

android hardware解析相关推荐

  1. Android Json 解析

    方法一 使用API解析 json: {"beaconid":"2397","state":"01","user ...

  2. android.hardware.camera2详解

    看到有些读者对博客提出的疑问,在此真诚说一句实在抱歉,本人是一名在校非科班出身大学生,之前 由于是期末季,所以非常忙,没有空更新,一月二十几号才放假,非常抱歉,评论所诉内容应该都已解决,只是我没有更新 ...

  3. Android混淆解析

    此文章转载来源https://www.jianshu.com/p/84114b7feb38点击打开链接 Android混淆解析 一.混淆的目的 一款发布到市场的软件原则上都应该做代码混淆. 通过代码混 ...

  4. Android中解析XML

    Android中解析XML 转载于:https://www.cnblogs.com/zhujiabin/p/5868993.html

  5. android 如何实现无限列表,在Android中解析和创建无限/无限级别的List /子列表中的XML...

    在我的Android Application的服务器端应用程序也由我开发.在这个应用程序Android应用程序从服务器请求一些XML并解析它. XML包含描述应用程序中应该有多少标签的信息,并且每个标 ...

  6. 在linux kernel或android中解析cmdline参数

    文章目录 ★★★ 友情链接 : 个人博客导读首页-点击此处 ★★★ Kernel command line: earlycon androidboot.selinux=permissive uart_ ...

  7. android最大json,Android:解析大型JSON文件

    我正在创建一个Android应用程序,该应用程序应该将Json从文件或网址解析为jsonarray和jsonobjects. 问题是,我的JSON是3.3 MB,当我使用一个简单的代码,如下所示:(现 ...

  8. android XMl 解析神奇xstream 六: 把集合list 转化为 XML文档

    前言:对xstream不理解的请看: android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件 android XMl 解析神奇xs ...

  9. android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入SD卡中的xml文件

    前言:对xstream不理解的请看: android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件 android XMl 解析神奇xs ...

  10. android XMl 解析神奇xstream 四: 将复杂的xml文件解析为对象

    前言:对xstream不理解的请看: android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件 android XMl 解析神奇xs ...

最新文章

  1. 每日一九度之 题目1030:毕业bg
  2. caffe各种依赖包配置
  3. 生成器 python0 1 8 27 64_python 生成式和生成器
  4. 创建完美SDK的10个技巧
  5. 华为补助武汉员工,最高每日 2000 元;iPhone SE 2 量产或推迟;PowerShell 7.0 发布 | 极客头条...
  6. 2022-04-25 安装PostgreSQL的发现小bug
  7. cmd批处理常用符号详解
  8. 前端CSS - 相对定位,绝对定位,固定定位
  9. wxWindows 2
  10. 大学生网课查题公众号搭建使用
  11. 最新上架 App Store 全流程
  12. java 支付宝预下单失败,系统异常,预下单状态未知!!! connect timed out
  13. Table ‘./zy@002diot/zy_sys_logs‘ is marked as crashed and should be repaired 报错
  14. UVA815 洪水Flooded
  15. 记一次godaddy上同一共享主机上部署多站
  16. 绝地求生登录计算机需要授权,绝地求生计算机授权收不到怎么办 | 手游网游页游攻略大全...
  17. CorelDRAWX4的C++插件开发(四十)纯C++插件开发(4)继承插件结构体IVGAppPlugin和自动化接口IDispatch
  18. 提取文件内容需要什么工具?
  19. 微信多开生成器PC端源码
  20. Redis实现附近的人

热门文章

  1. C语言中字符串转数字的方法
  2. 已测试:网上大神写的快手极速版脚本,autojs版快手极速版自动脚本下载
  3. FlashCS6安装步骤
  4. [OpenDrive] OpenDrive学习笔记
  5. 如何在家优雅地使用 Sci-Hub 免费下载外文文献
  6. linux超出频率限制黑屏,linux suse 超出频率限制 问题
  7. 2021-10-22 学习笔记:和弦对照信息表
  8. 数据结构-------图(最通俗易懂的文章)(一)图的数据结构及其遍历
  9. mysql索引的数据结构
  10. 天线罩结构的基础知识