DemoPublic/Android/HAL/HalDriver/demo_hal_driver.c

108 lines
2.8 KiB
C

/**
* @file demo_hal_driver.c
* @author Rick Chan (cy187lion@sina.com)
* @brief
* @version 0.1
* @date 2020-06-10
*
* @copyright Copyright (c) 2020
*
*/
#define LOG_TAG "MiccomStub"
#include <hardware/hardware.h>
#include <utils/Log.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "miccom.h"
#define DEVICE_NAME "/dev/miccom"
#define MODULE_NAME "Miccom"
#define MODULE_AUTHOR "chenyang@auotai.com"
#define UNUSED(x) ((void)(x))
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_module_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;
UNUSED(name);
dev = (struct miccom_device_t*)malloc(sizeof(*dev));
if(!dev) {
ALOGE("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 = (hw_module_t*)(module);
dev->common.close = miccom_device_close;
dev->set_reg = miccom_set_reg;
dev->get_reg = miccom_get_reg;
dev->fd = open(DEVICE_NAME, O_RDWR);
if(dev->fd == -1) {
ALOGE("Miccom stub: failed to open "DEVICE_NAME" -- %s.", strerror(errno));
free(dev);
return -EFAULT;
}
*device = &(dev->common);
ALOGI("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)
{
ALOGI("Miccom stub: set value %d to device reg.", reg);
write(dev->fd, &reg, num);
return 0;
}
static int miccom_get_reg(struct miccom_device_t* dev, char* reg, unsigned char num)
{
if(!reg) {
ALOGE("Miccom stub: error reg pointer.");
}
read(dev->fd, reg, num);
ALOGI("Miccom stub: get value %d from device reg.", *reg);
return 0;
}