博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android hal学习——编写hal代码【转】
阅读量:2194 次
发布时间:2019-05-02

本文共 5765 字,大约阅读时间需要 19 分钟。

from:http://blog.csdn.net/brightming/article/details/49869805

一、参考说明 

下面这个是关于的mk文件的变量的说明。 

代码是拷贝来的。 
修改的地方是: 
将example.cpp中的LOGE修改为ALOGE,LOGI修改为ALOGI. 
在Android.mk文件中修改: 
1、增加:LOCAL_LDLIBS+=-llog 
2、注释:#LOCAL_MODULE_PATH

二、代码 
1、在hardware/libhardware/include/hardware/增加: 
example.h

#ifndef ANDROID_EXAMPLE_INTERFACE_H#define ANDROID_EXAMPLE_INTERFACE_H#include 
__BEGIN_DECLS/** * The id of this module */#define EXAMPLE_HARDWARE_MODULE_ID "example"/** * The id of this device */#define EXAMPLE_HARDWARE_DEVICE_ID "example"struct example_module_t { struct hw_module_t common;};struct example_device_t { struct hw_device_t common; int fd; int (*set_val)(struct example_device_t* dev, int val); int (*get_val)(struct example_device_t* dev, int* val);};__END_DECLS#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

2、在hardware/libhardware/modules/新增文件夹:example 
进入该文件夹,编写2个文件: 
1)example.cpp

#define LOG_TAG "ExampleHALStub"#include 
#include
#include
#include
#include
#include
#define DEVICE_NAME "/dev/example"#define MODULE_NAME "Example"#define MODULE_AUTHOR "haoyu"#include
#include
static int example_device_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device); static int example_device_close(struct hw_device_t* device); static int example_set_val(struct example_device_t* dev, int val); static int example_get_val(struct example_device_t* dev, int* val); static struct hw_module_methods_t example_module_methods = { .open= example_device_open }; struct example_module_t HAL_MODULE_INFO_SYM = { .common= { .tag= HARDWARE_MODULE_TAG, .version_major= 1, .version_minor= 0, .id= EXAMPLE_HARDWARE_MODULE_ID, .name= MODULE_NAME, .author= MODULE_AUTHOR, .methods=&example_module_methods, } }; static int example_device_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device) { if(!strcmp(id, EXAMPLE_HARDWARE_DEVICE_ID)) { struct example_device_t* dev; dev = (struct example_device_t*)malloc(sizeof(struct example_device_t)); if(!dev) { ALOGE("Failed to alloc space for example_device_t."); return -EFAULT; } memset(dev, 0, sizeof(struct example_device_t)); dev->common.tag = HARDWARE_DEVICE_TAG; dev->common.version = 0; dev->common.module = (hw_module_t*)module; dev->common.close = example_device_close; dev->set_val = example_set_val; dev->get_val = example_get_val; if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) { ALOGE("Failed to open device file /dev/example -- %s.", strerror(errno)); free(dev); return -EFAULT; } *device = &(dev->common); ALOGI("Open device file /dev/example successfully."); return 0; } return -EFAULT; } static int example_device_close(struct hw_device_t* device) { struct example_device_t* example_device = (struct example_device_t*)device; if(example_device) { close(example_device->fd); free(example_device); } return 0; }static int example_set_val(struct example_device_t* dev, int val) { if(!dev) { ALOGE("Null dev pointer."); return -EFAULT; } ALOGI("Set value %d to device file /dev/example.", val); write(dev->fd, &val, sizeof(val)); return 0; } static int example_get_val(struct example_device_t* dev, int* val) { if(!dev) { ALOGE("Null dev pointer."); return -EFAULT; } if(!val) { ALOGE("Null val pointer."); return -EFAULT; } read(dev->fd, val, sizeof(*val)); ALOGI("Get value %d from device file /dev/example.", *val); return 0;} 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

2)Android.mk

LOCAL_PATH :=$(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE_TAGS :=optionalLOCAL_PRELINK_MODULE:=false#LOCAL_MODULE_PATH :=$(TARGET_OUT_SHARED_LIBRARYS)/hwLOCAL_SHARED_LIBRARYS :=liblogLOCAL_LDLIBS +=-llogLOCAL_SRC_FILES :=example.cppLOCAL_MODULE :=example.defaultinclude $(BUILD_SHARED_LIBRARY) 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

三、执行 
[gumh@localhost android-src]$ mmm hardware/libhardware/modules/example/ 
… 
Starting build with ninja 
ninja: Entering directory .' 
[100% 3/3] Install: out/target/product/generic/system/lib/example.default.so 
make: Leaving directory
/home/zzz/opensource/android-src’

[gumh@localhost android-src]$ make snod 

-l 1610612736 -a system out/target/product/generic/system.img out/target/product/generic/system out/target/product/generic/system 
Creating filesystem with parameters: 
Size: 1610612736 
Block size: 4096 
Blocks per group: 32768 
Inodes per group: 8192 
Inode size: 256 
Journal blocks: 6144 
Label: system 
Blocks: 393216 
Block groups: 12 
Reserved block group size: 95 
Created filesystem with 1751/98304 inodes and 146360/393216 blocks 
out/target/product/generic/system.img maxsize=1644333504 blocksize=2112 total=1610612736 reserve=16610880

make completed successfully (21 seconds)

system.img就是新的了。

你可能感兴趣的文章
Intellij IDEA使用(十三)—— 在Intellij IDEA中配置Maven
查看>>
面试题 —— 关于main方法的十个面试题
查看>>
集成测试(一)—— 使用PHP页面请求Spring项目的Java接口数据
查看>>
使用Maven构建的简单的单模块SSM项目
查看>>
Intellij IDEA使用(十四)—— 在IDEA中创建包(package)的问题
查看>>
FastDFS集群架构配置搭建(转载)
查看>>
HTM+CSS实现立方体图片旋转展示效果
查看>>
FFmpeg 命令操作音视频
查看>>
问题:Opencv(3.1.0/3.4)找不到 /opencv2/gpu/gpu.hpp 问题
查看>>
目的:使用CUDA环境变量CUDA_VISIBLE_DEVICES来限定CUDA程序所能使用的GPU设备
查看>>
问题:Mysql中字段类型为text的值, java使用selectByExample查询为null
查看>>
程序员--学习之路--技巧
查看>>
解决问题之 MySQL慢查询日志设置
查看>>
contOS6 部署 lnmp、FTP、composer、ThinkPHP5、docker详细步骤
查看>>
TP5.1模板布局中遇到的坑,配置完不生效解决办法
查看>>
PHPstudy中遇到的坑No input file specified,以及传到linux环境下遇到的坑,模板文件不存在
查看>>
TP5.1事务操作和TP5事务回滚操作多表
查看>>
composer install或composer update 或 composer require phpoffice/phpexcel 失败解决办法
查看>>
TP5.1项目从windows的Apache服务迁移到linux的Nginx服务需要注意几点。
查看>>
win10安装软件 打开时报错 找不到 msvcp120.dll
查看>>