DemoPublic/Android/HAL/HalDriver/demo_hal_driver.c
rick.chan 798d67b956 增加 demo 代码.
Signed-off-by: rick.chan <chenyang@autoai.com>
2020-06-10 14:11:05 +08:00

101 lines
2.7 KiB
C

/**
* @file demo_hal_driver.c
* @author Rich Chan (cy187lion@sina.com)
* @brief
* @version 0.1
* @date 2020-06-10
*
* @copyright Copyright (c) 2020
*
*/
#define LOG_TAG "MiccomStub"
#include <hard/hardware.h>
#include <fcntl.h>
#include <errno.h>
#include <cutils/log.h>
#include <cutils/atomic.h>
#include "miccom.h"
#define DEVICE_NAME "/dev/miccom"
#define MODULE_NAME "Miccom"
#define MODULE_AUTHOR "cy187lion@sina.com"
static int miccom_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device);
static int miccom_device_close(struct hw_device_t* device);
static int miccom_set_reg(struct miccom_device_t* dev, char reg, unsigned char num);
static int miccom_get_reg(struct miccom_device_t* dev, char* reg, unsigned char num);
static struct hw_modult_methods_t miccom_module_methods = {
.open = miccom_device_open
};
struct miccom_module_t HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.version_major: 1,
.version_minor: 0,
.id = MICCOM_HARDWARE_MODULE_ID,
.name = MODULE_NAME,
.author = MODULE_AUTHOR,
.methods = &miccom_module_methods
}
};
static int miccom_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device)
{
struct miccom_device_t* dev;
dev = (struct hw_device_t*)malloc(sizeof(*dev));
if(!dev) {
LOGE("Miccom stub: failed to alloc space.");
return -EFAULT;
}
memset(dev, 0, sizeof(*dev));
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = const_cast<hw_module_t *>(module);
dev->common.close = miccom_device_close;
dev->set_reg = miccom_set_reg;
dev->get_reg = miccom_get_reg;
if(dev->fd = open(DEVICE_NAME, O_RDWR) == -1) {
LOGE("Miccom stub: failed to open "DEVICE_NAME" -- %s.", strerror(errno));
free(dev);
return -EFAULT;
}
*device = &(dev->common);
LOGI("Miccom stub: Open "DEVICE_NAME" successfully.");
return 0;
}
static int miccom_device_close(struct hw_device_t* device)
{
struct miccom_device_t* dev = (struct miccom_device_t*)device;
if(dev) {
close(dev->fd);
free(dev);
}
return 0;
}
static int miccom_set_reg(struct miccom_device_t* dev, char reg, unsigned char num)
{
LOGI("Miccom stub: set value %d to device reg.", reg);
// write(dev->fd, &reg, num);
}
static int miccom_get_reg(struct miccom_device_t* dev, char* reg, unsigned char num)
{
if(!reg) {
LOGE("Miccom stub: error reg pointer.");
}
// read(dev->fd, reg, num);
LOGI("Miccom stub: get value %d from device reg.", *reg);
return 0;
}