[Android13|12] 如何写自己的 hal 代码以及测试代码? 如何写访问驱动demo代码

本文展示了如何创建一个名为hello的硬件抽象层(HAL)模块,包括模块头文件、源代码、Android构建脚本以及测试程序。模块提供了加法运算和写入sysfs的功能,并在HelloTest.cpp中进行了测试,成功执行了设备操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码总体结构: 

hardware/libhardware/include/hardware/hello.h

hardware/libhardware/modules/hello/Android.bp

hardware/libhardware/modules/hello/hello.c

hardware/libhardware/modules/hello/HelloTest.cpp

hello.h

#include <sys/cdefs.h>
#include <sys/types.h>
#include <hardware/hardware.h>
//HAL模块名
#define HELLO_HARDWARE_MODULE_ID "hello"
//HAL版本号
#define HELLO_MODULE_API_VERSION_1_0 HARDWARE_MODULE_API_VERSION(0, 1)
//设备名
#define HARDWARE_HELLO "hello"

#define SPRD_BACKLIGHT_PATH "/sys/class/backlight/sprd_backlight/brightness"
#define ERR_BUF_MAX 64

//自定义HAL模块结构体
typedef struct hello_module {
    struct hw_module_t common;
} hello_module_t;

//自定义HAL设备结构体
typedef struct hello_device {
    struct hw_device_t common;
    //加法函数
    int (*additionTest)(const struct hello_device *dev,int a,int b,int* total);
	int (*sysfsWrite)(const char * path,char * buf);
} hello_device_t;

//给外部调用提供打开设备的函数
static inline int _hello_open(const struct hw_module_t *module,
        hello_device_t **device) {
    return module->methods->open(module, HARDWARE_HELLO,
            (struct hw_device_t **) device);
}

hardware/libhardware/modules/hello/Android.bp

​
cc_library_shared {
    name: "hello.default",
    relative_install_path: "hw",
    proprietary: true,
    srcs: ["hello.c"],
    header_libs: ["libhardware_headers"],
    shared_libs: ["liblog"],
}

cc_binary {
    name: "hardware_HelloTest",
    srcs: ["HelloTest.cpp"],
    shared_libs: [
        "liblog",
        "libhardware",
    ],
}

​

hardware/libhardware/modules/hello/hello.c

#define LOG_TAG "HelloHal"

#include <malloc.h>
#include <stdint.h>
#include <string.h>

#include <log/log.h>

#include <hardware/hello.h>
#include <hardware/hardware.h>


static int sysfsWrite(const char * path,char * buf)
{
	char err_buf[ERR_BUF_MAX];
    int size;
    int fd;
	//int errno;

    fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
    if (fd < 0) {
        //strerror_r(errno, err_buf, sizeof(err_buf));
        ALOGE("Error open %s: %s\n", path, err_buf);
        return -1 ;
    }

    //LOGE(" %s -> buf = %s ",__FUNCTION__,buf);

    size = write(fd, buf, strlen(buf));
    if (size < 0) {
        //strerror_r(errno, err_buf, sizeof(err_buf));
        ALOGE("Error write %s: %s\n", path, err_buf);
    }
    close(fd);
    return size;
}

//加法函数实现
static int additionTest(const struct hello_device *dev,int a,int b,int *total)
{
	if(!dev){
		return -1;
	}
    *total = a + b;
    return 0;
}

//关闭设备函数
static int hello_close(hw_device_t *dev)
{
    if (dev) {
        free(dev);
        return 0;
    } else {
        return -1;
    }
}
//打开设备函数
static int hello_open(const hw_module_t* module,const char __unused *id,
                            hw_device_t** device)
{
    if (device == NULL) {
        ALOGE("NULL hello device on open");
        return -1;
    }

    hello_device_t *dev = malloc(sizeof(hello_device_t));
    memset(dev, 0, sizeof(hello_device_t));

    dev->common.tag = HARDWARE_DEVICE_TAG;
    dev->common.version = HELLO_MODULE_API_VERSION_1_0;
    dev->common.module = (struct hw_module_t*) module;
    dev->common.close = hello_close;
    dev->additionTest = additionTest;
	dev->sysfsWrite   = sysfsWrite;

    *device = &(dev->common);
    return 0;
}

static struct hw_module_methods_t hello_module_methods = {
    .open = hello_open,
};
//导出符号HAL_MODULE_INFO_SYM,指向自定义模块
hello_module_t HAL_MODULE_INFO_SYM = {
    .common = {
        .tag                = HARDWARE_MODULE_TAG,
        .module_api_version = HELLO_MODULE_API_VERSION_1_0,
        .hal_api_version    = HARDWARE_HAL_API_VERSION,
        .id                 = HELLO_HARDWARE_MODULE_ID,
        .name               = "Hello HAL",
        .author             = "Hello",
        .methods            = &hello_module_methods,
    },
};

hardware/libhardware/modules/hello/HelloTest.cpp

#include <hardware/hardware.h>
#include <hardware/hello.h>
#include <log/log.h>
#define TAG "HelloHal"
int main(){
    hello_device_t* hello_device = NULL;

    const hello_module_t * module = NULL;

    int ret = hw_get_module(HELLO_HARDWARE_MODULE_ID,(const struct hw_module_t**)&module);
    if (!ret) {
		ALOGD("get hello-hal failed....... 1");
        ret = _hello_open((const struct hw_module_t*)module,&hello_device);
    }

   
    if (ret < 0) {
          ALOGD("get hello-hal failed....... 2");
          return -1;
    }

    int total = 0;
	char val[3] = {'2','5','5'};
    hello_device->additionTest(hello_device,3,5,&total);
	hello_device->sysfsWrite(SPRD_BACKLIGHT_PATH,val);
    ALOGD("success start hello-hal....total = %d",total);
    return 0;
}

成果演示:

/system/bin # ./hardware_HelloTest

07-19 22:02:52.073  5265  5265 D hardware_HelloTest: get hello-hal failed....... 1
07-19 22:02:52.074  5265  5265 D hardware_HelloTest: success start hello-hal....total = 8
07-19 22:03:09.656  5328  5328 D hardware_HelloTest: get hello-hal failed....... 1
07-19 22:03:09.657  5328  5328 D hardware_HelloTest: success start hello-hal....total = 8
07-19 22:03:11.853  5329  5329 D hardware_HelloTest: get hello-hal failed....... 1
07-19 22:03:11.853  5329  5329 D hardware_HelloTest: success start hello-hal....total = 8
07-19 22:03:37.192  5397  5397 D hardware_HelloTest: get hello-hal failed....... 1
07-19 22:03:37.193  5397  5397 D hardware_HelloTest: success start hello-hal....total = 8

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值