探秘SensorHAL(三)

2014-11-24 07:30:28 · 作者: · 浏览: 6
*device = (struct hw_device_t*) dev;
sensors_config_read(NULL);
sensors_fifo_init();
sensors_list_foreach_api(sensors_init_iterator, NULL);
return 0;
}
struct hw_module_methods_t sensors_module_methods = {
open: sensors_module_open
};
实际调用到了sensors_module_open,此函数返回0代表成功,-1代表打开失败,主要工作是组装device,最终将句柄交给mSensorDevice,即:
[cpp]
mSensorDevice->common.tag = HARDWARE_DEVICE_TAG;
mSensorDevice->common.version = 0;
mSensorDevice->common.module = (struct hw_module_t*)module;
mSensorDevice->common.close = sensors_module_close;
mSensorDevice->activate = sensors_module_activate;
mSensorDevice->setDelay = sensors_module_set_delay;
mSensorDevice->poll = sensors_module_poll;
之前在说明接口头文件时,说明了,对于传感器的数据操作有三个重要函数就是avtivate, setDelay, poll。记住这几个函数,稍后做分析。
在mSensorDevice成功被组装后,调用sensors_module_t自创的另一重要接口:get_sensors_list。瞧一瞧此函数在本实例中的具体实现(DASH/sensors_list.c):
[cpp]
static struct sensor_t sensors[DASH_MAX_SENSORS];
static struct sensor_api_t* sensor_apis[DASH_MAX_SENSORS];
static int number_of_sensors = 0;
[cpp] view plaincopyprint
int sensors_list_get(struct sensors_module_t* module, struct sensor_t const** plist)
{
*plist = sensors;
return number_of_sensors;
}
此函数完成两个工作,一将传感器列表赋给指针plist,二返回传感器数量。这里sensors列表和number_of_sensors是如何运作的:
[cpp]
int sensors_list_register(struct sensor_t* sensor, struct sensor_api_t* api)
{
if (!sensor || !api)
return -1;
if (number_of_sensors > DASH_MAX_SENSORS-1)
return -1;
sensor_apis[number_of_sensors] = api;
/* We have to copy due to sensor API */
memcpy(&sensors[number_of_sensors++], sensor, sizeof(*sensor));
return 0;
}
每个传感器都会调用sensors_list_register将自己注册进sensors列表中,并且会将自己的API也注册进sensors_apis列表中,来一个例子吧(DASH/sensors/bma150_input.c):
[cpp]
struct sensor_desc {
struct sensors_select_t select_worker;
struct sensor_t sensor;
struct sensor_api_t api;
int input_fd;
float current_data[3];
char *rate_path;
/* config options */
int axis_x;
int axis_y;
int axis_z;
int neg_x;
int neg_y;
int neg_z;
int64_t delay_requests[MAX_CLIENTS];
};
static struct sensor_desc bma150_input = {
.sensor = {
name: "BMA150 accelerometer",
vendor: "Bosch Sensortec GmbH",
version: sizeof(sensors_event_t),
handle: SENSOR_ACCELEROMETER_HANDLE,
type: SENSOR_TYPE_ACCELEROMETER,
maxRange: 9.81,
resolution: 20,
power: 0.13,
minDelay: 5000
},
.api = {
init: bma150_input_init,
activate: bma150_input_activate,
set_delay: bma150_input_fw_delay,
close: bma150_input_close
},
.input_fd = -1,
.axis_x = 0,
.axis_y = 1,
.axis_z = 2,
.neg_x = 0,
.neg_y = 0,
.neg_z = 0
};
以上是BMA150注册了属于自己的初始化、激活、延迟、关闭等操作。
每次注册完成后自会对number_of_sensors进行+1操作。这样所有的传感器注册完成后,就会形成两个列表和一个传感器总数。sensors_list_get也就理解了。
在获取到总数后,会根据总数进行循环遍历,挨个激活传感器。
[cpp]
f