2013年(13)
分类: Android平台
2013-04-26 10:44:12
#define LOG_TAG "SdcardStub"
#include
#include
#include
#include
#include
#include
#define DEVICE_NAME "/dev/mtd/mmcblk0"
#define MODULE_NAME "Sdcard"
#define MODULE_AUTHOR "wuyq"
//打开、关闭设备; 读、写数据。
static int sdcard_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t ** device);
static int sdcard_device_close(struct hw_device_t* device);
static int sdcard_set_data(struct sdcard_device_t* dev, int val);
static int sdcard_get_data(struct sdcard_device_t* dev, int* val);
static struct hw_module_methods_t sdcard_module_methods = {
open: sdcard_device_open
};
struct sdcard_module_t HAL_MODULE_INFO_SYM = { /*android硬件抽象层规范规定*/
common: {
tag: HARDWARE_MODULE_TAG,
version_major: 1,
version_minor: 0,
id: SDCARD_HARDWARE_MODULE_ID,
name: MODULE_NAME,
author: MODULE_AUTHOR,
methods: &sdcard_module_methods,
}
};
static int sdcard_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device){
struct sdcard_device_t *dev;
dev = (struct sdcard_device_t*)malloc(sizeof(struct sdcard_device_t));
if(!dev){
LOGE("Sdcard Stub: faile to alloc space");
return -EFUTLT;
}
memset(dev, 0, sizeof(struct sdcard_device_t));
//结构体成员变量赋值
dev->common.tag = HARDWARE_DEVICE_TAG;
dev->common.version = 0;
dev->common.module = (hw_module_t*)module;
dev->common.close = sdcard_device_close;
dev->set_data = sdcard_set_val;
dev->get_data = sdcard_get_val;
if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1){
LOGE("Sdcard Stub: failed to open /dev/mtd/mmcblk0 -- %s", strerror(errno));
free(dev);
return -EFAULT;
}
*device = &(dev->common);
LOGI("Sdcard Stub: open /dev/mtd/mmcblk0 successfully!");
return 0;
}
static int sdcard_device_close(struct hw_device_t* device){
struct sdcard_device_t* sdcard_device = (struct sdcard_device_t*)device;
if(sdcard_device){
close(sdcard_device->fd);
free(sdcard_device);
}
return 0;
}
static int sdcard_set_data(struct sdcard_device_t* dev, int val){
LOGI("Sdcard Stub: set data %d to device.", val);
write(dev->fd, &val, sizeof(val));
return 0;
}
static int sdcard_get_data(struct sdcard_device_t* dev, int* val){
if(!val){
LOGE("Sdcard Stub: error val pointer");
return -EFAULT;
}
read(dev->fd, val, sizeof(*val));
LOGI("Sdcard Stub: get data %d from devie", *val);
return 0;
}