探秘SensorHAL(四)

2014-11-24 07:30:28 · 作者: · 浏览: 4
or (size_t i=0 ; i
mActivationCount.add(list[i].handle, model);
mSensorDevice->activate(mSensorDevice, list[i].handle, 0);
}
OK,这下,引出了三大数据处理函数之一:activate。
sensors_module.c
[cpp]
/* mSensorDevice->activate = sensors_module_activate; */
static int sensors_module_activate(struct sensors_poll_device_t *dev,
int handle, int enabled)
{
struct sensor_api_t* api = sensors_list_get_api_from_handle(handle);
if (!api) {
ALOGE("%s: unable to find handle!", __func__);
return -1;
}
if (api->activate(api, enabled) < 0)
return -1;
return 0;
}
sensors_list.c
[cpp]
struct sensor_api_t* sensors_list_get_api_from_handle(int handle)
{
int i;
for (i = 0; i < number_of_sensors; i++)
if (sensors[i].handle == handle)
return sensor_apis[i];
return NULL;
}
比如BMA150的handle就是SENSOR_ACCELEROMETER_HANDLE,通过简单的匹配后,就能找到对应的sensors_api[x],然后执行active也就是bma150_input_activate函数,有兴趣就简单看一下代码,大体意思就是打开驱动获取句柄,然后组装给相关参数。
至此SensorDevice的大体工作也就完成了,总结就是成功获取HAL后,对每个传感器执行activate操作。
3.服务开启之路(2)
之所分(1)(2)是觉得SensorDevice部分可以单独成一部分,毕竟多数是在跟HAL在交互,这部分将分析完服务部分。
回头看SensorService::onFirstRef()
[cpp]
void SensorService::onFirstRef()
{
ALOGD("nuSensorService starting...");
SensorDevice& dev(SensorDevice::getInstance());
if (dev.initCheck() == NO_ERROR) {
/**********************************
检测SensorDevice执行是否成功,所获取的数据是否有效。
>status_t SensorDevice::initCheck() const {
return mSensorDevice && mSensorModule NO_ERROR : NO_INIT;
}
**********************************/
sensor_t const* list;
ssize_t count = dev.getSensorList(&list);
/* 执行与(1)中sensors_list_get一样的工作 */
if (count > 0) {
ssize_t orientationIndex = -1;
bool hasGyro = false;
uint32_t virtualSensorsNeeds =
(1<
(1<
(1<
mLastEventSeen.setCapacity(count);
for (ssize_t i=0 ; i
registerSensor( new HardwareSensor(list[i]) );
switch (list[i].type) {
case SENSOR_TYPE_ORIENTATION:
orientationIndex = i;
break;
case SENSOR_TYPE_GYROSCOPE:
hasGyro = true;
break;
case SENSOR_TYPE_GRAVITY:
case SENSOR_TYPE_LINEAR_ACCELERATION:
case SENSOR_TYPE_ROTATION_VECTOR:
virtualSensorsNeeds &= ~(1<
break;
}
}
/* 遍历查看SENSOR HAL都支持哪些类型的传感器 */
// it's safe to instantiate the SensorFusion object here
// (it wants to be instantiated after h/w sensors have been
// registered)
const SensorFusion& fusion(SensorFusion::getInstance());
if (hasGyro) {
// Always instantiate Android's virtual sensors. Since they are
// instantiated behind sensors from the HAL, they won't
// interfere with applications, unless they looks specifically
// for them (by name).
registerVirtualSensor( new RotationVectorSensor