Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2116742
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: Android平台

2014-03-28 10:29:14

1.HAL层
首先在hal层定义结构体sensors_module_t
#define SENSORS_HARDWARE_MODULE_ID "sensors"
  1. struct sensors_module_t HAL_MODULE_INFO_SYM = {
  2. common:
  3.     {
  4. tag:
  5.         HARDWARE_MODULE_TAG,
  6.         version_major: 1,
  7.         version_minor: 0,
  8. id:
  9.         SENSORS_HARDWARE_MODULE_ID,   //每一个hal层都有一个唯一的id
  10. name: "MEMSIC Sensors Module"
  11.         ,
  12. author: "MEMSIC Inc."
  13.         ,
  14. methods:
  15.         &__module_methods,
  16. dso:    NULL,
  17.         reserved: {0},
  18.     },
  19. get_sensors_list:
  20.     __get_sensors_list
  21. };
在__module_methods中定义了open方法
  1. static struct hw_module_methods_t __module_methods = {
  2. open:
  3.     __module_methods_open
  4. };




  1. static int __module_methods_open(const struct hw_module_t *module,
  2.                                  const char *id, struct hw_device_t **device)
  3. {
  4.     struct sensors_poll_context_t *dev;
  5.     dev = (struct sensors_poll_context_t *)malloc(sizeof(*dev));
  6.     //初始化dev
  7.     dev->device.common.tag = HARDWARE_DEVICE_TAG;
  8.     dev->device.common.version = 0;
  9.     dev->device.common.module = (struct hw_module_t *)module;
  10.     dev->device.common.close = __common_close;
  11.     dev->device.activate = __control_activate;
  12.     dev->device.setDelay = __control_set_delay;
  13.     dev->device.poll = __data_poll;
  14.     //赋值给device 
  15.     //这儿有个问题: common是一个 struct hw_device_t(小) common;
  16.     //一会在framework中调用的需要的是 sensor_poll_t(大)
  17.     *device = &dev->device.common;
  18.     return res;
  19. }

2. framework层
framework/native/services/sensorservice/SensorDevice.cpp
  1. SensorDevice::SensorDevice()
  2.     : mSensorDevice(0),
  3.        mSensorModule(0)
  4. {
  5.     //fw层会根据唯一的id=SENSORS_HARDWARE_MODULE_ID来找到hal层的so,然后调用dlopen加载到内存中
  6.     //同时定义为 struct sensors_module_t* 的mSensorModule会指向找到的so中的sensor_module_t结构体
  7.     //这样就可以利用hal层定义的函数了
  8.     hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&mSensorModule);
  9.     //struct sensors_poll_device_t* mSensorDevice;
  10.     sensors_open(&mSensorModule->common, &mSensorDevice);
  11.     mSensorDevice->activate(mSensorDevice, list[i].handle, 0);
  12. }

  13. static inline int sensors_open(const struct hw_module_t* module,
  14.         struct sensors_poll_device_t** device) {
  15.     return module->methods->open(module,
  16.             SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);
  17. }

sensors_poll_device_t
                        --> hw_device_t
                        --> 加一些函数指针
在hal层的open中:
      *device = &dev->device.common;
在framework层的sensor_open中
    module->methods->open(..., (struct hw_device_t**)device);
为什么这儿是将sensors_poll_device_t转为了hw_device_t,而不是直接传sensor_poll_device_t呢?
答: 我感觉这儿是为了屏蔽,即让hal层屏蔽fw层的无关代码.
      当然也有可能是为了接口的统一.


阅读(4609) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~