android 编译hal报错,Android系统HAL层开发,编译过程(hello)

在android2.3.1下进行HAL层的开发,先参照网上弄了个hello的demo,首先看下HAL层在android系统中的位置:

0818b9ca8b590ca3270a3433284dd417.png

硬件驱动程序可以看做是在keinel层,HAL封装了硬件驱动,然后再经过JNI接口的封装才能给Java应用程序调用。

HAL层接口封装的具体流程如下:

1)在../Android-2.3.1/hardware/libhardware/include/hardware这个目录下添加hello.h头文件,具体可以参开当前目录下的overlay.h,

/***************************************************

*android_hal_hello_demo

*hello.h

***************************************************/

#ifndef ANDROID_HELLO_INTERFACE_H

#define ANDROID_HELLO_INTERFACE_H

#include 

__BEGIN_DECLS

#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 (*get_val)(struct hello_device_t *dev,int val);

int (*set_val)(struct hello_device_t *dev,int val);

};

__END_DECLS;

#endif

2)在../Android-2.3.1/hardware/libhardware/modules这个目录下新建hello文件夹,并在此文件夹中添加hello.c和Android.mk文件,具体可以参考modules目录下overlay文件夹中的内容。

/******************************************************************************

*android_hal_hello_demo

*hello.c

*****************************************************************************/

#include 

#include 

#include 

#include 

#include 

#include 

#define LOG_TAG "hello_stub"

#define DEVICE_NAME "/dev/hello"

#define MODULE_NAME "Hello"

#define MODULE_AUTHOR "pwzhgx@163.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_hal_hello_demo

*Android.mk

*****************************************************************************/

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_PRELINK_MODULE := false

LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw

LOCAL_SHARED_LIBRARIES := liblog

LOCAL_SRC_FILES := hello.c

LOCAL_MODULE := hello.default

include $(BUILD_SHARED_LIBRARY)

到此HAL层的代码基本已经弄好,接下来就是编译,将其编译成模块.so库文件。

3)如何编译,这是一个新增的模块,比如你已经make过一次android的源码了,此时就不要要重新make clean;make了,你只需要将模块编译好,然后再make snod即可将新的模块编译到镜像中。在源码的build目录下有一个配置环境的脚本envsetup.sh,此文件包含了一下编译工具m,mm,mmm,具体的功能你可以直接查看。这里编译使用到mmm。

在android源码包中执行

[root@localhost Android-2.3.1]# sh build/envsetup.sh

[root@localhost Android-2.3.1]#croot

此时mmm工具已经在当前的环境中,当然就可以使用mmm来编译模块了

[root@localhost Android-2.3.1]#mmm hardware/libhardware/modules/hello

然而出现了一些意想不到的错误

找不到liblog.so库文件,只能编译一下生成liblog.so这个库文件才可以。

0818b9ca8b590ca3270a3433284dd417.png

4)编译生成liblog.so

[root@localhost Android-2.3.1]#make liblog

0818b9ca8b590ca3270a3433284dd417.png

5)现在重新编译

[root@localhost Android-2.3.1]#mmm hardware/libhardware/modules/hello

终于看到生成hello.default.so库文件了,接下来重新打包

0818b9ca8b590ca3270a3433284dd417.png

6)重新打包镜像

[root@localhost Android-2.3.1]make snod

7)接下来就是JNI封装

待续。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值