void SensorService::onFirstRef() {
ALOGD( "nuSensorService starting..." ); SensorDevice& dev(SensorDevice::getInstance()); if (dev.initCheck() == NO_ERROR) {
sensor_t const * list; ssize_t count = dev.getSensorList(&list); if (count > 0) {
ssize_t orientationIndex = -1; bool hasGyro = false ; uint32_t virtualSensorsNeeds = (1<<SENSOR_TYPE_GRAVITY) | (1<<SENSOR_TYPE_LINEAR_ACCELERATION) | (1<<SENSOR_TYPE_ROTATION_VECTOR); mLastEventSeen.setCapacity(count); for (ssize_t i=0 ; i<count ; i++) {
registerSensor( new HardwareSensor(list[i]) ); switch (list[i].type) {
case SENSOR_TYPE_ORIENTATION: orientationIndex = i; break ; case SENSOR_TYPE_GYROSCOPE: case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: hasGyro = true ; break ; case SENSOR_TYPE_GRAVITY: case SENSOR_TYPE_LINEAR_ACCELERATION: case SENSOR_TYPE_ROTATION_VECTOR: virtualSensorsNeeds &= ~(1<<list[i].type); break ; } } // 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()); // build the sensor list returned to users mUserSensorList = mSensorList; if (hasGyro) {
Sensor aSensor; // Add Android virtual sensors if they're not already // available in the HAL aSensor = registerVirtualSensor( new RotationVectorSensor() ); if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
mUserSensorList.add(aSensor); } aSensor = registerVirtualSensor( new GravitySensor(list, count) ); if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) {
mUserSensorList.add(aSensor); } aSensor = registerVirtualSensor( new LinearAccelerationSensor(list, count) ); if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) {
mUserSensorList.add(aSensor); } aSensor = registerVirtualSensor( new OrientationSensor() ); if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) {
// if we are doing our own rotation-vector, also add // the orientation sensor and remove the HAL provided one. mUserSensorList.replaceAt(aSensor, orientationIndex); } // virtual debugging sensors are not added to mUserSensorList registerVirtualSensor( new CorrectedGyroSensor(list, count) ); registerVirtualSensor( new GyroDriftSensor() ); } // debugging sensor list mUserSensorListDebug = mSensorList; // Check if the device really supports batching by looking at the FIFO event // counts for each sensor. bool batchingSupported = false ; for ( int i = 0; i < mSensorList.size(); ++i) {
if (mSensorList[i].getFifoMaxEventCount() > 0) {
batchingSupported = true ; break ; } } if (batchingSupported) {
// Increase socket buffer size to a max of 100 KB for batching capabilities. mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED; } else {
mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED; } // Compare the socketBufferSize value against the system limits and limit // it to maxSystemSocketBufferSize if necessary. FILE *fp = fopen ( "/proc/sys/net/core/wmem_max" , "r" ); char line[128]; if (fp != NULL && fgets (line, sizeof (line), fp) != NULL) {
line[ sizeof (line) - 1] = '\0' ; size_t maxSystemSocketBufferSize; sscanf (line, "%zu" , &maxSystemSocketBufferSize); if (mSocketBufferSize > maxSystemSocketBufferSize) {
mSocketBufferSize = maxSystemSocketBufferSize; } } if (fp) {
fclose (fp); } mWakeLockAcquired = false ; mLooper = new Looper( false ); const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT; mSensorEventBuffer = new sensors_event_t[minBufferSize]; mSensorEventScratch = new sensors_event_t[minBufferSize]; mMapFlushEventsToConnections = new SensorEventConnection const * [minBufferSize]; mAckReceiver = new SensorEventAckReceiver( this ); mAckReceiver->run( "SensorEventAckReceiver" , PRIORITY_URGENT_DISPLAY); mInitCheck = NO_ERROR; run( "SensorService" , PRIORITY_URGENT_DISPLAY); } } } |