1 两个重要的结构体
a hw_module_t (用于动态库名,jni通过hw_get_module函数来获取到该结构体)
b hw_device_t (初始化在hw_get_module 的open方法中,jni通过调用open来调用它)
2 在 myhal.h中封装着两个结构体,并可扩展其它属性和方法
#ifndef __LED_HAL_H__
#define __LED_HAL_H__
#include <hardware/hardware.h>
#define LED_MODULE_ID "s5p_led_hal"
//设计两个对象--分别继承hw_module_t, hw_device_t
//module对象
struct led_hw_module_t{
struct hw_module_t common; // 必须是在一个位置,必须叫做common
//以下部分可以进行扩展--只是用于演示
int (*close)(struct hw_module_t *module);
};
//device对象
struct led_hw_device_t{
struct hw_device_t common; // 必须是在一个位置,必须叫做common
//以下部分可以进行扩展
int (*open)(void);
int (*control_dev)(int on);
};
#endif
3 初始化两个结构体,并实现其它方法
有三必须 :
1 定义struct led_hw_module_t 名必须为 HMI
2 定义struct hw_module_t 名必须为 common
3 定义struct hw_device_t 名必须为 common
//需要的头文件
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define LOG_TAG "LED_HAL_LOG" //logcat调试信息的定义
#include <utils/Log.h>
#include "led_hal.h"
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define LOG_TAG "LED_HAL_LOG"
#include <utils/Log.h>
#include "led_hal.h"
static int fd;
//3 方法的实现
int led_hal_device_close(struct hw_device_t* device)
{
LOGD("---------%s-----------\n", __FUNCTION__);
if(device)
{
free(device);
}
close(fd);
return 0;
}
int led_hal_device_open(void)
{
LOGD("---------%s-----------\n", __FUNCTION__);
fd = open("/dev/led1", O_RDWR);
if(fd < 0)
{
LOGE("open : %s\n", strerror(errno));
return -1;
}
return 0;
}
int led_control_dev(int on)
{
LOGD("---------%s-----------\n", __FUNCTION__);
int ret;
ret = write(fd, &on, 4);
if(ret < 0)
{
LOGE("write on : %s\n", strerror(errno));
return -1;
}
return 0;
}
//2 如果open方法调用的时候传递了一个一级指针,该一级指针等同于mydev
// open(,, &p1);
int led_hal_module_open(const struct hw_module_t* module, const char* id,
struct hw_device_t** device)
{
LOGD("---------%s-----------\n", __FUNCTION__);
//构建device对象--动态构建
struct led_hw_device_t *mydev = NULL;
mydev = (struct led_hw_device_t *)malloc(sizeof(struct led_hw_device_t));
if(mydev == NULL)
{
LOGE("malloc error\n");
return -1;
}
//初始化device对象
mydev->common.tag = HARDWARE_DEVICE_TAG; //标签必须为 HARDWARE_DEVICE_TAG
mydev->common.version = 1; //不重要
mydev->common.module = module; //继承父类 struct hw_module_t
mydev->common.close = led_hal_device_close; //关闭资源
mydev->open = led_hal_device_open; //自定义函数
mydev->control_dev = led_control_dev; //自定义函数
//把初始化好的结构体通过open函数返回给jni层
*device = (struct hw_device_t *)mydev;
return 0;
}
struct hw_module_methods_t led_methods = {
open : led_hal_module_open,
};
//1 首先构建module对象
//p->common.methods->open();
//被ret = hw_get_module(LED_MODULE_ID,
(const struct hw_module_t * * )&pModule); 进行获取
struct led_hw_module_t HMI = { //变量名必须HMI
common : {
tag : HARDWARE_MODULE_TAG, //标签,必须为HARDWARE_MODULE_TAG
version_major : 1, //不重要
version_minor : 0, //不重要
id : LED_MODULE_ID, //编译为动态库的名字
name : "this is a simple hal demo", //不重要
author : "bajie@163.com", //不重要
methods : &led_methods, //里面有个open方法,jni通过调用该方法来获取到struct hw_device_t 结构体的属性和方法
},
};