设为首页 加入收藏

TOP

Android系统HAL层开发,编译过程(hello)(一)
2014-11-24 07:34:57 来源: 作者: 【 】 浏览:1
Tags:Android 系统 HAL 开发 编译 过程 hello


硬件驱动程序可以看做是在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", *v

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android Gallery 3D效果 下一篇Android开发工控软件--蓝牙控制

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis 分布式锁全解 (2025-12-25 17:19:51)
·SpringBoot 整合 Red (2025-12-25 17:19:48)
·MongoDB 索引 - 菜鸟 (2025-12-25 17:19:45)
·What Is Linux (2025-12-25 16:57:17)
·Linux小白必备:超全 (2025-12-25 16:57:14)