探秘SensorHAL(五)
() );
registerVirtualSensor( new GravitySensor(list, count) );
registerVirtualSensor( new LinearAccelerationSensor(list, count) );
// these are optional
registerVirtualSensor( new OrientationSensor() );
registerVirtualSensor( new CorrectedGyroSensor(list, count) );
// virtual debugging sensors...
char value[PROPERTY_VALUE_MAX];
property_get("debug.sensors", value, "0");
if (atoi(value)) {
registerVirtualSensor( new GyroDriftSensor() );
}
} else if (orientationIndex != -1) {
// If we don't have a gyro but have a orientation sensor from
// elsewhere, we can compute rotation vector from that.
// (Google Maps expects rotation vector sensor to exist.)
registerVirtualSensor( &RotationVectorSensor2::getInstance() );
}
// build the sensor list returned to users
mUserSensorList = mSensorList;
if (hasGyro &&
(virtualSensorsNeeds & (1<
[cpp]
// if we have the fancy sensor fusion, and it's not provided by the
// HAL, use our own (fused) orientation sensor by removing the
// HAL supplied one form the user list.
if (orientationIndex >= 0) {
mUserSensorList.removeItemsAt(orientationIndex);
}
}
run("SensorService", PRIORITY_URGENT_DISPLAY);
mInitCheck = NO_ERROR;
}
}
}
从上面可以看出,如果传感器中有SENSOR_TYPE_GYROSCOPE类型的话,也就是陀螺仪(高端传感器),会定义hasGyro,从而注册多个虚拟传感器,blablabla……
最后会触发一个重要动作:run。
回头查看SensorService类的定义:
class SensorService :
public BinderService,
public BnSensorServer,
protected Thread
发现SensorService是继承了Thread的。
那么run的工作实际是启动了线程,threadLoop:
[cpp]
bool SensorService::threadLoop()
{
ALOGD("nuSensorService thread starting...");
const size_t numEventMax = 16;
const size_t minBufferSize = numEventMax + numEventMax * mVirtualSensorList.size();
sensors_event_t buffer[minBufferSize];
sensors_event_t scratch[minBufferSize];
SensorDevice& device(SensorDevice::getInstance());
const size_t
vcount = mVirtualSensorList.size();
ssize_t count;
do {
count = device.poll(buffer, numEventMax);
if (count<0) {
ALOGE("sensor poll failed (%s)", strerror(-count));
break;
}
...
}
发现这个线程会一直执行poll操作,这个操作是什么呢?还记得“服务开启之路(1)”中提到的吗?
[cpp]
mSensorDevice->poll = sensors_module_poll;
是在HAL中的函数,最终指向sensors_module_poll ( DASH/sensors_module.c )
[cpp]
static int sensors_module_poll(struct sensors_poll_device_t *dev,
sensors_event_t* data, int count)
{
int ret;
while ((ret = sensors_fifo_get_all(data, count)) == 0)
;
return ret;
}
( DASH/sensors_fifo.c )
[cpp]
void sensors_fifo_put(sensors_event_t *data)
{
pthread_mutex_lock(&sensors_fifo.mutex);
if (sensors_fifo.fifo_i < FIFO_LEN)
sensors_fifo.fifo[sensors_fifo.fifo_i++] = *data;
pthread_cond_broadcast(&sensors_fifo.data_cond);