android hal 驱动,自己动手 从android硬件驱动到APP---(2)hal驱动层

本文详细介绍了Android硬件抽象层(HAL)的一个示例模块`hello`的实现,包括`hello.h`头文件和`hello.c`源代码。展示了如何定义硬件设备接口,以及设备的打开、关闭、设置值和获取值等操作。通过这个例子,读者可以理解Android HAL模块的基本工作原理和结构。
摘要由CSDN通过智能技术生成

1:aosp/hardware/libhardware/include/hardware/hello.h

#ifndef ANDROID_HELLO_INTERFACE_H

#define ANDROID_HELLO_INTERFACE_H

#include

__BEGIN_DECLS

#define HELLO_HARDWARE_MODULE_ID "hello"//ID

struct hello_module_t {

struct hw_module_t common;

};//hw_module_t的继承者

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);

};//hw_device_t的继承者

__END_DECLS

#endif

2:aosp/hardware/libhardware/modules/hello/hello.c

#define LOG_TAG "HelloStub"

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define MODULE_NAME "Hello"

char const * const device_name = "/dev/hello" ;

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,

};

static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device)

{

struct hello_device_t* dev;

char name_[64];

//pthread_mutex_t lock;

dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t));

if(!dev) {

ALOGE("Hello Stub: failed to alloc space");

return -EFAULT;

}

ALOGE("Hello Stub: hello_device_open eliot_shao");

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;

//pthread_mutex_lock(&lock);

dev->fd = -1 ;

snprintf(name_, 64, device_name, 0);

dev->fd = open(name_, O_RDWR);

if(dev->fd == -1) {

ALOGE("Hello Stub:eliot failed to open %s !-- %s.", name_,strerror(errno));

free(dev);

return -EFAULT;

}

//pthread_mutex_unlock(&lock);

*device = &(dev->common);

ALOGI("Hello Stub: open HAL 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) {

ALOGI("Hello Stub: set value to device.");

write(dev->fd, &val, sizeof(val));

return 0;

}

static int hello_get_val(struct hello_device_t* dev, int* val) {

if(!val) {

ALOGE("Hello Stub: error val pointer");

return -EFAULT;

}

read(dev->fd, val, sizeof(*val));

ALOGI("Hello Stub: get value from device");

return 0;

}

struct hello_module_t HAL_MODULE_INFO_SYM = {

.common = {

.tag = HARDWARE_MODULE_TAG,

//.module_api_version = FINGERPRINT_MODULE_API_VERSION_2_0,

.hal_api_version = HARDWARE_HAL_API_VERSION,

.id = HELLO_HARDWARE_MODULE_ID,

.name = "Demo shaomingliang hello HAL",

.author = "The Android Open Source Project",

.methods = &hello_module_methods,

},

};

来源:oschina

链接:https://my.oschina.net/u/2963604/blog/1784974

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值