传一个Android HAL helloworld例程,这个例程参考“老罗的android之旅”的(http://blog.csdn.net/luoshengyang/article/details/6573809)。

hello.h

#ifndef ANDROID_HELLO_INTERFACE_H#define ANDROID_HELLO_INTERFACE_H#include __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

hello.c

#define LOG_TAG "HelloStub"#include //#include     //commented by vincent.wuyuwei#include "hello.h"               //added by vincent.wuyuwei#include #include #include #include #define DEVICE_NAME "/dev/hello"#define MODULE_NAME "Hello"#define MODULE_AUTHOR "shyluo@gmail.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_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) {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;}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.mk

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_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)

在验证以上HAL例程中,笔者的过程是:

1.在 hardware/libhardware/modules中新建一个hello文件夹,然后将以上三个文件放置在hello文件夹中。

2.在android源码根目录下运行:source build/envsetup.sh 激活mmm命令。

3.运行命令:mmm hardware/libhardware/modules/hello

如果不出错则编译提示如下:

target thumb C: hello.default <= hardware/libhardware/modules/hello/hello.ctarget SharedLib: hello.default (out/target/product/generic/obj/SHARED_LIBRARIES/hello.default_intermediates/LINKED/hello.default.so)target Symbolic: hello.default (out/target/product/generic/symbols/system/lib/hw/hello.default.so)target Strip: hello.default (out/target/product/generic/obj/lib/hello.default.so)Install: out/target/product/generic/system/lib/hw/hello.default.so

说明已经生成了.so文件在out/target/product/generic/system/lib/hw/

4.运行命令make snod .重新打包system.img,新打包的system.img包含hello.default.so 库。

如果提示“make: *** No rule to make target `out/target/product/generic/obj/lib/liblog.so'”

参考上一篇博文解决:http://blog.csdn.net/wuyuwei45/article/details/8824893


关于HAL编译后的库命名

从上面编译知道,这个HAL例程编译出来的库命名为hello.default.so,而这个命名是由Android.mk的LOCAL_MODULE定义的,那么在可不可以随意定义编译后库的名字呢?答案是否定的。

Native部分会通过函数int hw_get_module(...)来得到我们的HAL模块,而这个函数根据模块id和路径,并结合android的product和board等信息查找模块,最终会通过dlopen该so库,所以上面库文件的命名定制最好是xxx.default.so或xxx.yyboard.so,关于命名的注意部分可以参考android的hardware.c文件。

如果随意命名HAL so名字,会容易导致没法通过hw_get_module找到该so!!


更多相关文章

  1. Android(安卓)so库编译错误 java.lang.UnsatisfiedLinkError: da
  2. 解决jdk1.6已经安装,编译android源码报错问题
  3. Android(安卓)NDK开发(一)环境搭建及运行示例
  4. android下mm模块出现编译错误问题
  5. android 开发 @override 编译错误 解决办法
  6. Android(安卓)build失败 原因总结:
  7. android 获取通讯模块制式类型
  8. android string.xml %问题。
  9. Android-8.1.0编译问题汇总

随机推荐

  1. android 浏览器问题
  2. android 安卓 开发 图片库获得图片的绝对
  3. Android Studio Emulator: Process finis
  4. 黑客黑科技?Python代码让android手机实现
  5. android运行模拟器脚本(批处理)
  6. 在Eclipse中进行Android单元测试
  7. android电池信息简介
  8. Android开发之拖动条/滑动条控件、星级评
  9. 笔记!
  10. Android 左右滑屏效果