1.HAL层
首先在hal层定义结构体sensors_module_t
#define SENSORS_HARDWARE_MODULE_ID "sensors"
-
struct sensors_module_t HAL_MODULE_INFO_SYM = {
-
common:
-
{
-
tag:
-
HARDWARE_MODULE_TAG,
-
version_major: 1,
-
version_minor: 0,
-
id:
-
SENSORS_HARDWARE_MODULE_ID, //每一个hal层都有一个唯一的id
-
name: "MEMSIC Sensors Module"
-
,
-
author: "MEMSIC Inc."
-
,
-
methods:
-
&__module_methods,
-
dso: NULL,
-
reserved: {0},
-
},
-
get_sensors_list:
-
__get_sensors_list
-
};
在__module_methods中定义了open方法
-
static struct hw_module_methods_t __module_methods = {
-
open:
-
__module_methods_open
-
};
-
static int __module_methods_open(const struct hw_module_t *module,
-
const char *id, struct hw_device_t **device)
-
{
-
struct sensors_poll_context_t *dev;
-
dev = (struct sensors_poll_context_t *)malloc(sizeof(*dev));
-
//初始化dev
-
dev->device.common.tag = HARDWARE_DEVICE_TAG;
-
dev->device.common.version = 0;
-
dev->device.common.module = (struct hw_module_t *)module;
-
dev->device.common.close = __common_close;
-
dev->device.activate = __control_activate;
-
dev->device.setDelay = __control_set_delay;
-
dev->device.poll = __data_poll;
-
//赋值给device
-
//这儿有个问题: common是一个 struct hw_device_t(小) common;
-
//一会在framework中调用的需要的是 sensor_poll_t(大)
-
*device = &dev->device.common;
-
return res;
-
}
2. framework层
framework/native/services/sensorservice/SensorDevice.cpp
-
SensorDevice::SensorDevice()
-
: mSensorDevice(0),
-
mSensorModule(0)
-
{
-
//fw层会根据唯一的id=SENSORS_HARDWARE_MODULE_ID来找到hal层的so,然后调用dlopen加载到内存中
-
//同时定义为 struct sensors_module_t* 的mSensorModule会指向找到的so中的sensor_module_t结构体
-
//这样就可以利用hal层定义的函数了
-
hw_get_module(SENSORS_HARDWARE_MODULE_ID, (hw_module_t const**)&mSensorModule);
-
//struct sensors_poll_device_t* mSensorDevice;
-
sensors_open(&mSensorModule->common, &mSensorDevice);
-
mSensorDevice->activate(mSensorDevice, list[i].handle, 0);
-
}
-
-
static inline int sensors_open(const struct hw_module_t* module,
-
struct sensors_poll_device_t** device) {
-
return module->methods->open(module,
-
SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);
-
}
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层的无关代码.
当然也有可能是为了接口的统一.
阅读(1839) | 评论(0) | 转发(0) |