SensorService.cpp revision 1cb13461a8cf62e3ba634e5965332f2d284f6d42
1fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian/* 2fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * Copyright (C) 2010 The Android Open Source Project 3fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * 4fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License"); 5fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * you may not use this file except in compliance with the License. 6fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * You may obtain a copy of the License at 7fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * 8fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * http://www.apache.org/licenses/LICENSE-2.0 9fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * 10fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * Unless required by applicable law or agreed to in writing, software 11fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS, 12fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * See the License for the specific language governing permissions and 14fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian * limitations under the License. 15fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian */ 16fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 17fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <stdint.h> 18f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include <math.h> 19fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <sys/types.h> 20fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 213301542828febc768e1df42892cfac4992c35474Mathias Agopian#include <cutils/properties.h> 223301542828febc768e1df42892cfac4992c35474Mathias Agopian 23fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/SortedVector.h> 24fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/KeyedVector.h> 25fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/threads.h> 26fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/Atomic.h> 27fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/Errors.h> 28fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/RefBase.h> 29451beee076cac09f817abae78a990dea108a9482Mathias Agopian#include <utils/Singleton.h> 30c4a930d1d5a432a1f302763ac55460d6e83fe7e0Mathias Agopian#include <utils/String16.h> 31fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 32fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <binder/BinderService.h> 33451beee076cac09f817abae78a990dea108a9482Mathias Agopian#include <binder/IServiceManager.h> 341cb13461a8cf62e3ba634e5965332f2d284f6d42Mathias Agopian#include <binder/PermissionCache.h> 35fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 36fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <gui/ISensorServer.h> 37fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <gui/ISensorEventConnection.h> 38fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 39fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <hardware/sensors.h> 40fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 41984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "CorrectedGyroSensor.h" 42f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "GravitySensor.h" 43f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "LinearAccelerationSensor.h" 44984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "OrientationSensor.h" 45f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "RotationVectorSensor.h" 46984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "SensorFusion.h" 47984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "SensorService.h" 48fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 49fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiannamespace android { 50fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 51fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 523301542828febc768e1df42892cfac4992c35474Mathias Agopian/* 533301542828febc768e1df42892cfac4992c35474Mathias Agopian * Notes: 543301542828febc768e1df42892cfac4992c35474Mathias Agopian * 553301542828febc768e1df42892cfac4992c35474Mathias Agopian * - what about a gyro-corrected magnetic-field sensor? 563301542828febc768e1df42892cfac4992c35474Mathias Agopian * - run mag sensor from time to time to force calibration 573301542828febc768e1df42892cfac4992c35474Mathias Agopian * - gravity sensor length is wrong (=> drift in linear-acc sensor) 583301542828febc768e1df42892cfac4992c35474Mathias Agopian * 593301542828febc768e1df42892cfac4992c35474Mathias Agopian */ 603301542828febc768e1df42892cfac4992c35474Mathias Agopian 61fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorService() 621cb13461a8cf62e3ba634e5965332f2d284f6d42Mathias Agopian : mInitCheck(NO_INIT) 63fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 64fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 65fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 66fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianvoid SensorService::onFirstRef() 67fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 6850df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian LOGD("nuSensorService starting..."); 6950df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 70f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorDevice& dev(SensorDevice::getInstance()); 71fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 72f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (dev.initCheck() == NO_ERROR) { 73010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian ssize_t orientationIndex = -1; 743301542828febc768e1df42892cfac4992c35474Mathias Agopian bool hasGyro = false; 75f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian uint32_t virtualSensorsNeeds = 76f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian (1<<SENSOR_TYPE_GRAVITY) | 77f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian (1<<SENSOR_TYPE_LINEAR_ACCELERATION) | 78f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian (1<<SENSOR_TYPE_ROTATION_VECTOR); 79f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensor_t const* list; 80f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian int count = dev.getSensorList(&list); 813560fb24b668675627934356f210d84d19bf4e56Mathias Agopian mLastEventSeen.setCapacity(count); 8250df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian for (int i=0 ; i<count ; i++) { 83f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian registerSensor( new HardwareSensor(list[i]) ); 84f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian switch (list[i].type) { 85010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian case SENSOR_TYPE_ORIENTATION: 86010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian orientationIndex = i; 87010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian break; 883301542828febc768e1df42892cfac4992c35474Mathias Agopian case SENSOR_TYPE_GYROSCOPE: 893301542828febc768e1df42892cfac4992c35474Mathias Agopian hasGyro = true; 903301542828febc768e1df42892cfac4992c35474Mathias Agopian break; 91f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian case SENSOR_TYPE_GRAVITY: 92f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian case SENSOR_TYPE_LINEAR_ACCELERATION: 93f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian case SENSOR_TYPE_ROTATION_VECTOR: 94f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian virtualSensorsNeeds &= ~(1<<list[i].type); 95f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian break; 9650df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian } 9750df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian } 98fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 99984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // it's safe to instantiate the SensorFusion object here 100984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // (it wants to be instantiated after h/w sensors have been 101984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // registered) 102984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const SensorFusion& fusion(SensorFusion::getInstance()); 103984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 1043301542828febc768e1df42892cfac4992c35474Mathias Agopian if (hasGyro) { 1053301542828febc768e1df42892cfac4992c35474Mathias Agopian // Always instantiate Android's virtual sensors. Since they are 1063301542828febc768e1df42892cfac4992c35474Mathias Agopian // instantiated behind sensors from the HAL, they won't 1073301542828febc768e1df42892cfac4992c35474Mathias Agopian // interfere with applications, unless they looks specifically 1083301542828febc768e1df42892cfac4992c35474Mathias Agopian // for them (by name). 109984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 1103301542828febc768e1df42892cfac4992c35474Mathias Agopian registerVirtualSensor( new RotationVectorSensor() ); 1113301542828febc768e1df42892cfac4992c35474Mathias Agopian registerVirtualSensor( new GravitySensor(list, count) ); 1123301542828febc768e1df42892cfac4992c35474Mathias Agopian registerVirtualSensor( new LinearAccelerationSensor(list, count) ); 113984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 1143301542828febc768e1df42892cfac4992c35474Mathias Agopian // these are optional 115984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian registerVirtualSensor( new OrientationSensor() ); 116984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian registerVirtualSensor( new CorrectedGyroSensor(list, count) ); 1173301542828febc768e1df42892cfac4992c35474Mathias Agopian 1183301542828febc768e1df42892cfac4992c35474Mathias Agopian // virtual debugging sensors... 1193301542828febc768e1df42892cfac4992c35474Mathias Agopian char value[PROPERTY_VALUE_MAX]; 1203301542828febc768e1df42892cfac4992c35474Mathias Agopian property_get("debug.sensors", value, "0"); 1213301542828febc768e1df42892cfac4992c35474Mathias Agopian if (atoi(value)) { 1223301542828febc768e1df42892cfac4992c35474Mathias Agopian registerVirtualSensor( new GyroDriftSensor() ); 1233301542828febc768e1df42892cfac4992c35474Mathias Agopian } 12450df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian } 125f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 126010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian // build the sensor list returned to users 127010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian mUserSensorList = mSensorList; 128010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian if (hasGyro && 129010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR))) { 130010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian // if we have the fancy sensor fusion, and it's not provided by the 131010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian // HAL, use our own (fused) orientation sensor by removing the 132010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian // HAL supplied one form the user list. 133010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian if (orientationIndex >= 0) { 134010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian mUserSensorList.removeItemsAt(orientationIndex); 135010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian } 136010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian } 137010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian 138f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian run("SensorService", PRIORITY_URGENT_DISPLAY); 139f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mInitCheck = NO_ERROR; 140fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 141fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 142fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 143f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::registerSensor(SensorInterface* s) 144f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 145f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t event; 146f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian memset(&event, 0, sizeof(event)); 147f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 148f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const Sensor sensor(s->getSensor()); 149f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // add to the sensor list (returned to clients) 150f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorList.add(sensor); 151f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // add to our handle->SensorInterface mapping 152f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorMap.add(sensor.getHandle(), s); 153f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // create an entry in the mLastEventSeen array 154f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mLastEventSeen.add(sensor.getHandle(), event); 155f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 156f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 157f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::registerVirtualSensor(SensorInterface* s) 158f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 159f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian registerSensor(s); 160f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mVirtualSensorList.add( s ); 161f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 162f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 163fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::~SensorService() 164fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 165f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=0 ; i<mSensorMap.size() ; i++) 166f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian delete mSensorMap.valueAt(i); 167fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 168fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 1691cb13461a8cf62e3ba634e5965332f2d284f6d42Mathias Agopianstatic const String16 sDump("android.permission.DUMP"); 1701cb13461a8cf62e3ba634e5965332f2d284f6d42Mathias Agopian 171fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::dump(int fd, const Vector<String16>& args) 172fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 173fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const size_t SIZE = 1024; 174fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian char buffer[SIZE]; 175fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian String8 result; 1761cb13461a8cf62e3ba634e5965332f2d284f6d42Mathias Agopian if (!PermissionCache::checkCallingPermission(sDump)) { 177fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian snprintf(buffer, SIZE, "Permission Denial: " 178fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian "can't dump SurfaceFlinger from pid=%d, uid=%d\n", 179fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian IPCThreadState::self()->getCallingPid(), 180fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian IPCThreadState::self()->getCallingUid()); 181fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 182fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } else { 183fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 1843560fb24b668675627934356f210d84d19bf4e56Mathias Agopian snprintf(buffer, SIZE, "Sensor List:\n"); 1853560fb24b668675627934356f210d84d19bf4e56Mathias Agopian result.append(buffer); 1863560fb24b668675627934356f210d84d19bf4e56Mathias Agopian for (size_t i=0 ; i<mSensorList.size() ; i++) { 1873560fb24b668675627934356f210d84d19bf4e56Mathias Agopian const Sensor& s(mSensorList[i]); 1883560fb24b668675627934356f210d84d19bf4e56Mathias Agopian const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle())); 189984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian snprintf(buffer, SIZE, 190984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | " 191984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian "last=<%5.1f,%5.1f,%5.1f>\n", 1923560fb24b668675627934356f210d84d19bf4e56Mathias Agopian s.getName().string(), 1933560fb24b668675627934356f210d84d19bf4e56Mathias Agopian s.getVendor().string(), 1943560fb24b668675627934356f210d84d19bf4e56Mathias Agopian s.getHandle(), 19524d72350f321c17b0bfe1ef3fd52d4070a1c02c3Mathias Agopian s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f, 1963560fb24b668675627934356f210d84d19bf4e56Mathias Agopian e.data[0], e.data[1], e.data[2]); 1973560fb24b668675627934356f210d84d19bf4e56Mathias Agopian result.append(buffer); 1983560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 199984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian SensorFusion::getInstance().dump(result, buffer, SIZE); 200f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorDevice::getInstance().dump(result, buffer, SIZE); 2013560fb24b668675627934356f210d84d19bf4e56Mathias Agopian 2027c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian snprintf(buffer, SIZE, "%d active connections\n", 2037c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian mActiveConnections.size()); 204fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 205fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian snprintf(buffer, SIZE, "Active sensors:\n"); 206fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 207fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian for (size_t i=0 ; i<mActiveSensors.size() ; i++) { 2085d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian int handle = mActiveSensors.keyAt(i); 209f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n", 2105d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian getSensorName(handle).string(), 2115d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian handle, 212fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mActiveSensors.valueAt(i)->getNumConnections()); 213fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 214fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 215fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 216fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian write(fd, result.string(), result.size()); 217fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return NO_ERROR; 218fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 219fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 220fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::threadLoop() 221fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 222fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGD("nuSensorService thread starting..."); 223fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 224f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const size_t numEventMax = 16 * (1 + mVirtualSensorList.size()); 225f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t buffer[numEventMax]; 226f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t scratch[numEventMax]; 227f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorDevice& device(SensorDevice::getInstance()); 228f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const size_t vcount = mVirtualSensorList.size(); 229fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 230f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian ssize_t count; 231fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian do { 232f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian count = device.poll(buffer, numEventMax); 233fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (count<0) { 234fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGE("sensor poll failed (%s)", strerror(-count)); 235fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian break; 236fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 237fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 23894e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian recordLastValue(buffer, count); 23994e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian 240f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // handle virtual sensors 241f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (count && vcount) { 242984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian sensors_event_t const * const event = buffer; 243f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const DefaultKeyedVector<int, SensorInterface*> virtualSensors( 244f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian getActiveVirtualSensors()); 245f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const size_t activeVirtualSensorCount = virtualSensors.size(); 246f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (activeVirtualSensorCount) { 247f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian size_t k = 0; 248984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian SensorFusion& fusion(SensorFusion::getInstance()); 249984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (fusion.isEnabled()) { 250984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian for (size_t i=0 ; i<size_t(count) ; i++) { 251984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.process(event[i]); 252984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 253984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 254f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=0 ; i<size_t(count) ; i++) { 255f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t j=0 ; j<activeVirtualSensorCount ; j++) { 256f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t out; 257f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (virtualSensors.valueAt(j)->process(&out, event[i])) { 258f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian buffer[count + k] = out; 259f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian k++; 260f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 261f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 262f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 263f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (k) { 264f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // record the last synthesized values 265f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian recordLastValue(&buffer[count], k); 266f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian count += k; 267f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // sort the buffer by time-stamps 268f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sortEventBuffer(buffer, count); 269f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 270f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 271f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 272f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 273f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // send our events to clients... 274fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const SortedVector< wp<SensorEventConnection> > activeConnections( 275fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian getActiveConnections()); 276fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian size_t numConnections = activeConnections.size(); 277f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=0 ; i<numConnections ; i++) { 278f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sp<SensorEventConnection> connection( 279f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian activeConnections[i].promote()); 280f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (connection != 0) { 281f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian connection->sendEvents(buffer, count, scratch); 282fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 283fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 284fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } while (count >= 0 || Thread::exitPending()); 285fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 286fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGW("Exiting SensorService::threadLoop!"); 287fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return false; 288fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 289fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 29094e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopianvoid SensorService::recordLastValue( 29194e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian sensors_event_t const * buffer, size_t count) 29294e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian{ 29394e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian Mutex::Autolock _l(mLock); 29494e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian 29594e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian // record the last event for each sensor 29694e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian int32_t prev = buffer[0].sensor; 29794e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian for (size_t i=1 ; i<count ; i++) { 29894e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian // record the last event of each sensor type in this buffer 29994e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian int32_t curr = buffer[i].sensor; 30094e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian if (curr != prev) { 30194e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian mLastEventSeen.editValueFor(prev) = buffer[i-1]; 30294e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian prev = curr; 30394e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian } 30494e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian } 30594e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian mLastEventSeen.editValueFor(prev) = buffer[count-1]; 30694e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian} 30794e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian 308f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count) 309f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 310f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian struct compar { 311f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian static int cmp(void const* lhs, void const* rhs) { 312f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs); 313f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs); 314f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return r->timestamp - l->timestamp; 315f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 316f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian }; 317f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian qsort(buffer, count, sizeof(sensors_event_t), compar::cmp); 318f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 319f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 320fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSortedVector< wp<SensorService::SensorEventConnection> > 321fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::getActiveConnections() const 322fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 323fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 324fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return mActiveConnections; 325fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 326fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 327f001c92436b4a66eb7687286325ced7f10c9f917Mathias AgopianDefaultKeyedVector<int, SensorInterface*> 328f001c92436b4a66eb7687286325ced7f10c9f917Mathias AgopianSensorService::getActiveVirtualSensors() const 329f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 330f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Mutex::Autolock _l(mLock); 331f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return mActiveVirtualSensors; 332f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 333f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 3345d2707214dfb97bd8dfcc6620be36841d3c82420Mathias AgopianString8 SensorService::getSensorName(int handle) const { 335010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian size_t count = mUserSensorList.size(); 3365d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian for (size_t i=0 ; i<count ; i++) { 337010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian const Sensor& sensor(mUserSensorList[i]); 3385d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian if (sensor.getHandle() == handle) { 3395d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian return sensor.getName(); 3405d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian } 3415d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian } 3425d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian String8 result("unknown"); 3435d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian return result; 3445d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian} 3455d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian 346fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianVector<Sensor> SensorService::getSensorList() 347fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 348010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian return mUserSensorList; 349fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 350fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 351fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiansp<ISensorEventConnection> SensorService::createSensorEventConnection() 352fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 353fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian sp<SensorEventConnection> result(new SensorEventConnection(this)); 354fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return result; 355fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 356fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 357db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopianvoid SensorService::cleanupConnection(SensorEventConnection* c) 358fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 359fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 360db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian const wp<SensorEventConnection> connection(c); 3617c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian size_t size = mActiveSensors.size(); 362a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size); 3637c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian for (size_t i=0 ; i<size ; ) { 364db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian int handle = mActiveSensors.keyAt(i); 365db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian if (c->hasSensor(handle)) { 366a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle); 367f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor( handle ); 368a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle); 369f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (sensor) { 370db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian sensor->activate(c, false); 371f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 372db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian } 373db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian SensorRecord* rec = mActiveSensors.valueAt(i); 374a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle); 375a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGD_IF(DEBUG_CONNECTIONS, 376a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian "removing connection %p for sensor[%d].handle=0x%08x", 377a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian c, i, handle); 378a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian 379db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian if (rec && rec->removeConnection(connection)) { 380a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection"); 3817c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian mActiveSensors.removeItemsAt(i, 1); 382f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveVirtualSensors.removeItem(handle); 3837c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian delete rec; 3847c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian size--; 3857c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } else { 3867c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian i++; 387fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 388fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 3897c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian mActiveConnections.remove(connection); 390fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 391fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 392fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::enable(const sp<SensorEventConnection>& connection, 393fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle) 394fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 39550df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian if (mInitCheck != NO_ERROR) 39650df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian return mInitCheck; 39750df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 398fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 399f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor(handle); 400f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE); 401f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (err == NO_ERROR) { 402f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorRecord* rec = mActiveSensors.valueFor(handle); 403f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (rec == 0) { 404f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian rec = new SensorRecord(connection); 405f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveSensors.add(handle, rec); 406f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (sensor->isVirtual()) { 407f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveVirtualSensors.add(handle, sensor); 408f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 409f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } else { 410f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (rec->addConnection(connection)) { 411f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // this sensor is already activated, but we are adding a 412f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // connection that uses it. Immediately send down the last 4133f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian // known value of the requested sensor if it's not a 4143f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian // "continuous" sensor. 4153f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian if (sensor->getSensor().getMinDelay() == 0) { 4163f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian sensors_event_t scratch; 4173f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian sensors_event_t& event(mLastEventSeen.editValueFor(handle)); 4183f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian if (event.version == sizeof(sensors_event_t)) { 4193f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian connection->sendEvents(&event, 1); 4203f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian } 421f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 4223560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 4233560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 424f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (err == NO_ERROR) { 425f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // connection now active 426f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (connection->addSensor(handle)) { 427f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // the sensor was added (which means it wasn't already there) 428f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // so, see if this connection becomes active 429f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mActiveConnections.indexOf(connection) < 0) { 430f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveConnections.add(connection); 431f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 4327c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } 433fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 434fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 435fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return err; 436fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 437fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 438fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::disable(const sp<SensorEventConnection>& connection, 439fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle) 440fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 44150df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian if (mInitCheck != NO_ERROR) 44250df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian return mInitCheck; 44350df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 444fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian status_t err = NO_ERROR; 445fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 446fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian SensorRecord* rec = mActiveSensors.valueFor(handle); 447fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (rec) { 448fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // see if this connection becomes inactive 449fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian connection->removeSensor(handle); 450fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (connection->hasAnySensor() == false) { 451fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mActiveConnections.remove(connection); 452fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 453fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // see if this sensor becomes inactive 454fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (rec->removeConnection(connection)) { 455fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mActiveSensors.removeItem(handle); 456f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveVirtualSensors.removeItem(handle); 457fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian delete rec; 458fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 459f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor(handle); 460f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE); 4617c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } 462fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return err; 463fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 464fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 4657c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianstatus_t SensorService::setEventRate(const sp<SensorEventConnection>& connection, 466fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle, nsecs_t ns) 467fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 46850df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian if (mInitCheck != NO_ERROR) 46950df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian return mInitCheck; 47050df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 4711cd700015318727d6d42236ab6274f1949fb08baMathias Agopian if (ns < 0) 4721cd700015318727d6d42236ab6274f1949fb08baMathias Agopian return BAD_VALUE; 4731cd700015318727d6d42236ab6274f1949fb08baMathias Agopian 4747c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian if (ns < MINIMUM_EVENTS_PERIOD) 4757c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian ns = MINIMUM_EVENTS_PERIOD; 4761cd700015318727d6d42236ab6274f1949fb08baMathias Agopian 477f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor(handle); 478f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (!sensor) return BAD_VALUE; 479f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return sensor->setDelay(connection.get(), handle, ns); 480fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 481fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 482fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 483fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 484fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorRecord::SensorRecord( 485fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const sp<SensorEventConnection>& connection) 486fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 487fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mConnections.add(connection); 488fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 489fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 4907c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorRecord::addConnection( 491fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const sp<SensorEventConnection>& connection) 492fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 493fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (mConnections.indexOf(connection) < 0) { 494fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mConnections.add(connection); 4957c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return true; 496fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 4977c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return false; 498fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 499fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 500fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorRecord::removeConnection( 501fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const wp<SensorEventConnection>& connection) 502fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 503fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian ssize_t index = mConnections.indexOf(connection); 504fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (index >= 0) { 505fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mConnections.removeItemsAt(index, 1); 506fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 507fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return mConnections.size() ? false : true; 508fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 509fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 510fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 511fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 512fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorEventConnection::SensorEventConnection( 513fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const sp<SensorService>& service) 514fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian : mService(service), mChannel(new SensorChannel()) 515fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 516fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 517fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 518fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorEventConnection::~SensorEventConnection() 519fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 520a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this); 521fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mService->cleanupConnection(this); 522fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 523fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 524fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianvoid SensorService::SensorEventConnection::onFirstRef() 525fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 526fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 527fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 5287c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorEventConnection::addSensor(int32_t handle) { 52971d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 530f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mSensorInfo.indexOf(handle) <= 0) { 531f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorInfo.add(handle); 5327c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return true; 533fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 5347c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return false; 535fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 536fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 5377c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorEventConnection::removeSensor(int32_t handle) { 53871d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 539f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mSensorInfo.remove(handle) >= 0) { 5407c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return true; 5417c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } 5427c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return false; 543fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 544fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 545fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorEventConnection::hasSensor(int32_t handle) const { 54671d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 547f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return mSensorInfo.indexOf(handle) >= 0; 548fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 549fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 550fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorEventConnection::hasAnySensor() const { 55171d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 5527c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return mSensorInfo.size() ? true : false; 5537c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian} 5547c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian 555fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::sendEvents( 556cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian sensors_event_t const* buffer, size_t numEvents, 557cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian sensors_event_t* scratch) 558fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 559cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian // filter out events not for this connection 5603560fb24b668675627934356f210d84d19bf4e56Mathias Agopian size_t count = 0; 5613560fb24b668675627934356f210d84d19bf4e56Mathias Agopian if (scratch) { 56271d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 5633560fb24b668675627934356f210d84d19bf4e56Mathias Agopian size_t i=0; 5643560fb24b668675627934356f210d84d19bf4e56Mathias Agopian while (i<numEvents) { 5653560fb24b668675627934356f210d84d19bf4e56Mathias Agopian const int32_t curr = buffer[i].sensor; 566f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mSensorInfo.indexOf(curr) >= 0) { 5673560fb24b668675627934356f210d84d19bf4e56Mathias Agopian do { 5683560fb24b668675627934356f210d84d19bf4e56Mathias Agopian scratch[count++] = buffer[i++]; 5693560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } while ((i<numEvents) && (buffer[i].sensor == curr)); 5703560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } else { 5713560fb24b668675627934356f210d84d19bf4e56Mathias Agopian i++; 5723560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 573cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian } 5743560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } else { 5753560fb24b668675627934356f210d84d19bf4e56Mathias Agopian scratch = const_cast<sensors_event_t *>(buffer); 5763560fb24b668675627934356f210d84d19bf4e56Mathias Agopian count = numEvents; 577cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian } 578fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 5793560fb24b668675627934356f210d84d19bf4e56Mathias Agopian if (count == 0) 5803560fb24b668675627934356f210d84d19bf4e56Mathias Agopian return 0; 5813560fb24b668675627934356f210d84d19bf4e56Mathias Agopian 582cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian ssize_t size = mChannel->write(scratch, count*sizeof(sensors_event_t)); 583fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (size == -EAGAIN) { 584fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // the destination doesn't accept events anymore, it's probably 585fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // full. For now, we just drop the events on the floor. 586fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGW("dropping %d events on the floor", count); 587fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return size; 588fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 589fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 590fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGE_IF(size<0, "dropping %d events on the floor (%s)", 591fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian count, strerror(-size)); 592fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 5931e0b1e8491e5f6dc59faabe70cbfa942853150e0Jeff Brown return size < 0 ? status_t(size) : status_t(NO_ERROR); 594fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 595fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 596fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiansp<SensorChannel> SensorService::SensorEventConnection::getSensorChannel() const 597fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 598fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return mChannel; 599fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 600fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 601fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::enableDisable( 602fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle, bool enabled) 603fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 604fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian status_t err; 605fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (enabled) { 606fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian err = mService->enable(this, handle); 607fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } else { 608fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian err = mService->disable(this, handle); 609fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 610fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return err; 611fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 612fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 613fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::setEventRate( 614fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle, nsecs_t ns) 615fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 6167c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return mService->setEventRate(this, handle, ns); 617fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 618fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 619fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 620fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}; // namespace android 621fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 622