本文共 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就是新的了。