Chinaunix首页 | 论坛 | 博客
  • 博客访问: 53540
  • 博文数量: 13
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 231
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-26 10:22
文章分类

全部博文(13)

文章存档

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;
}

 

阅读(1043) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Android系统中 *.img作用

给主人留下些什么吧!~~