hardware\libhardware\include\hardware\Freg.h
hardware\libhardware\modules\freg\Freg.cpp
hardware\libhardware\modules\freg\Android.mk
hardware\libhardware\include\hardware\Freg.h
#ifndef ANDROID_FREG_INTERFACE_H
#define ANDROID_FREG_INTERFACE_H
#include <hardware/hardware.h>
__BEGIN_DECLS
//定义模块ID
#define FREG_HARDWARE_MODULE_ID "freg"
//定义设备ID
#define FREG_HARDWARE_DEVICE_ID "freg"
//自定义模块结构体
struct freg_module_t {
struct hw_module_t common;
};
//自定义设备结构体
struct freg_device_t {
struct hw_device_t common;
int fd;
int (*set_val)(struct freg_device_t* dev, int val);
int (*get_val)(struct freg_device_t* dev, int * val);
};
__END_DECLS
#endif
hardware\libhardware\modules\freg\Freg.cpp
#define LOG_TAG "FregHALStub"
#include <hardware/hardware.h>
#include <hardware/freg.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <malloc.h>
#include <cutils/log.h>
#include <cutils/atomic.h>
#define DEVICE_NAME "/dev/freg"
#define MODULE_NAME "Freg"
#define MODULE_AUTHOR "liudongming"
//#undef LOG_TAG
//#define LOG_TAG "FREG"
#define LOGD ALOGD
#define LOGE ALOGE
#define LOGI ALOGI
#define LOGW ALOGW
//设备打开和关闭接口
static int freg_device_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device);
static int freg_device_close(struct hw_device_t* device);
//设备寄存器读写接口
static int freg_get_val(struct freg_device_t* dev, int* val);
static int freg_set_val(struct freg_device_t* dev, int val);
//定义模块操作方法结构体变量
static struct hw_module_methods_t freg_module_methods = {
open: freg_device_open
};
//定义模块结构体变量
//在hardware.h中有定义#define HAL_MODULE_INFO_SYM_AS_STR "HMI"
//每个硬件抽象层模块必须导出一个名称为HAL_MODULE_INFO_SYM的符号
struct freg_module_t HAL_MODULE_INFO_SYM = {
common: {
tag: HARDWARE_MODULE_TAG,
version_major: 1,
version_minor: 0,
id: FREG_HARDWARE_MODULE_ID,
name: MODULE_NAME,
author: MODULE_AUTHOR,
methods: &freg_module_methods,
}
};
static int freg_device_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device) {
if(!strcmp(id, FREG_HARDWARE_DEVICE_ID)){
struct freg_device_t* dev;
dev = (struct freg_device_t*)malloc(sizeof(struct freg_device_t));
if(!dev){
LOGE("Failed to alloc space for freg_device_t.");
return -EFAULT;
}
memset(dev, 0, sizeof(struct freg_device_t));
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (hw_module_t*)module;
dev->common.close = freg_device_close;
dev->set_val = freg_set_val;
dev->get_val = freg_get_val;
if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {
LOGE("Failed to open device file /dev/freg -- %s.", strerror(errno));
free(dev);
return -EFAULT;
}
*device = &(dev->common);
LOGI("Open device file /dev/freg successfully.");
return 0;
}
return -EFAULT;
}
static int freg_device_close(struct hw_device_t* device) {
struct freg_device_t* freg_device = (struct freg_device_t*)device;
if(freg_device) {
close(freg_device->fd);
free(freg_device);
}
return 0;
}
static int freg_get_val(struct freg_device_t* dev, int* val){
if(!dev){
LOGE("Null dev pointer.");
return -EFAULT;
}
if(!val){
LOGE("Null val pointer.");
return -EFAULT;
}
read(dev->fd, val, sizeof(*val));
LOGI("Get value %d from device file /dev/freg.", *val);
return 0;
}
static int freg_set_val(struct freg_device_t* dev, int val){
if(!dev){
LOGE("Null dev pointer.");
return -EFAULT;
}
LOGI("Set value %d to device file /dev/freg.", val);
write(dev->fd, &val, sizeof(val));
return 0;
}
hardware\libhardware\modules\freg\Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := false
#LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_SRC_FILES := freg.cpp
LOCAL_MODULE := freg.default
include $(BUILD_SHARED_LIBRARY)
无法初始化服务
SELinux问题
01-01 00:01:58.287 1120 1120 D FregService: FregService()
01-01 00:01:58.287 1120 1120 I FregServiceJNI: [Freg] Initializing HAL stub freg......
01-01 00:01:58.289 298 584 D SurfaceFlinger: postEvent(298, v/c=2140)
01-01 00:01:58.294 1120 1989 I InputReader: Device added: id=-1, name='Virtual', sources=0x00000301
01-01 00:01:58.295 1120 1989 I InputReader: Device added: id=8, name='mtk-kpd', sources=0x00000101
01-01 00:01:58.295 1120 1989 I InputReader: Device added: id=0, name='qwerty', sources=0x00000101
01-01 00:01:58.297 1120 1989 I InputReader: Touch device 'mtk-tpd' could not query the properties of its associated display. The device will be inoperable until the display size becomes available.
01-01 00:01:58.297 1120 1989 I InputReader: Device added: id=2, name='mtk-tpd', sources=0x00001103
01-01 00:01:58.298 1120 1989 I InputReader: Device added: id=1, name='mtk-tpd-kpd', sources=0x00000101
01-01 00:01:58.299 1120 1989 D InputManager: Notification manager is not ready, set message pending
01-01 00:01:58.301 1120 1120 I FregServiceJNI: [Freg] Device freg found.
01-01 00:01:58.302 1120 1120 D FregServiceJNI: [Freg] freg_device_open
01-01 00:01:58.290 1120 1120 W system_server: type=1400 audit(0.0:9): avc: denied { read write } for name="freg" dev="tmpfs" ino=4313 scontext=u:r:system_server:s0 tcontext=u:object_r:device:s0 tclass=chr_file permissive=0
01-01 00:01:58.306 1120 1120 E FregHALStub: Failed to open device file /dev/freg -- Permission denied.
01-01 00:01:58.306 1120 1120 E FregServiceJNI: [Freg] Faied to open device freg.
01-01 00:01:58.306 1120 1120 E FregService: Failed to initialize freg service.
解决办法:
device\mediatek\common\sepolicy\service_contexts
添加
freg u:object_r:freg_service:s0
device\mediatek\common\sepolicy\service.te
添加
type freg_service, app_api_service, system_server_service, service_manager_type;
device\mediatek/common/sepolicy/device.te
添加
type freg_device, dev_type;
device\mediatek/common/sepolicy/file_contexts
##########################
# Devices
#
在Devices最后一行添加
/dev/freg(/.*)? u:object_r:freg_device:s0
device\mediatek/common/sepolicy/system_server.te
添加
allow system_server freg_device:chr_file { read write getattr open ioctl };
修改完SELinux,重新编译一下bootimage
so64位无法加载问题
解决办法:
修改hardware\libhardware\modules\freg\Android.mk
#LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_MODULE_RELATIVE_PATH := hw
分别在lib和lib64下分别生成32位和64位的so文件