SensorService.cpp revision a5c106a4f0afcf061728a1cb7c8c3b908728575d
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> 38907103bf186cfdd2ed9eb3b6c36de53ade7b16f6Mathias Agopian#include <gui/SensorEventQueue.h> 39fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 40fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <hardware/sensors.h> 41fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 42984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "CorrectedGyroSensor.h" 43f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "GravitySensor.h" 44f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "LinearAccelerationSensor.h" 45984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "OrientationSensor.h" 46f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "RotationVectorSensor.h" 47984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "SensorFusion.h" 48984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "SensorService.h" 49fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 50fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiannamespace android { 51fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 52fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 533301542828febc768e1df42892cfac4992c35474Mathias Agopian/* 543301542828febc768e1df42892cfac4992c35474Mathias Agopian * Notes: 553301542828febc768e1df42892cfac4992c35474Mathias Agopian * 563301542828febc768e1df42892cfac4992c35474Mathias Agopian * - what about a gyro-corrected magnetic-field sensor? 573301542828febc768e1df42892cfac4992c35474Mathias Agopian * - run mag sensor from time to time to force calibration 583301542828febc768e1df42892cfac4992c35474Mathias Agopian * - gravity sensor length is wrong (=> drift in linear-acc sensor) 593301542828febc768e1df42892cfac4992c35474Mathias Agopian * 603301542828febc768e1df42892cfac4992c35474Mathias Agopian */ 613301542828febc768e1df42892cfac4992c35474Mathias Agopian 62fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorService() 631cb13461a8cf62e3ba634e5965332f2d284f6d42Mathias Agopian : mInitCheck(NO_INIT) 64fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 65fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 66fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 67fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianvoid SensorService::onFirstRef() 68fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 69a551237de142549fb8a6608ee9d2fbf4b7ca2ebfSteve Block ALOGD("nuSensorService starting..."); 7050df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 71f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorDevice& dev(SensorDevice::getInstance()); 72fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 73f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (dev.initCheck() == NO_ERROR) { 74f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensor_t const* list; 757b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian ssize_t count = dev.getSensorList(&list); 767b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian if (count > 0) { 777b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian ssize_t orientationIndex = -1; 787b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian bool hasGyro = false; 797b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian uint32_t virtualSensorsNeeds = 807b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian (1<<SENSOR_TYPE_GRAVITY) | 817b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian (1<<SENSOR_TYPE_LINEAR_ACCELERATION) | 827b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian (1<<SENSOR_TYPE_ROTATION_VECTOR); 837b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian 847b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian mLastEventSeen.setCapacity(count); 857b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian for (ssize_t i=0 ; i<count ; i++) { 867b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian registerSensor( new HardwareSensor(list[i]) ); 877b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian switch (list[i].type) { 887b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian case SENSOR_TYPE_ORIENTATION: 897b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian orientationIndex = i; 907b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian break; 917b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian case SENSOR_TYPE_GYROSCOPE: 927b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian hasGyro = true; 937b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian break; 947b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian case SENSOR_TYPE_GRAVITY: 957b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian case SENSOR_TYPE_LINEAR_ACCELERATION: 967b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian case SENSOR_TYPE_ROTATION_VECTOR: 977b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian virtualSensorsNeeds &= ~(1<<list[i].type); 987b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian break; 997b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian } 10050df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian } 101fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 1027b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // it's safe to instantiate the SensorFusion object here 1037b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // (it wants to be instantiated after h/w sensors have been 1047b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // registered) 1057b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian const SensorFusion& fusion(SensorFusion::getInstance()); 1067b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian 1077b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian if (hasGyro) { 1087b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // Always instantiate Android's virtual sensors. Since they are 1097b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // instantiated behind sensors from the HAL, they won't 1107b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // interfere with applications, unless they looks specifically 1117b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // for them (by name). 1127b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian 1137b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian registerVirtualSensor( new RotationVectorSensor() ); 1147b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian registerVirtualSensor( new GravitySensor(list, count) ); 1157b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian registerVirtualSensor( new LinearAccelerationSensor(list, count) ); 1167b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian 1177b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // these are optional 1187b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian registerVirtualSensor( new OrientationSensor() ); 1197b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian registerVirtualSensor( new CorrectedGyroSensor(list, count) ); 1207b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian 1217b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // virtual debugging sensors... 1227b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian char value[PROPERTY_VALUE_MAX]; 1237b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian property_get("debug.sensors", value, "0"); 1247b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian if (atoi(value)) { 1257b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian registerVirtualSensor( new GyroDriftSensor() ); 1267b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian } 1273301542828febc768e1df42892cfac4992c35474Mathias Agopian } 128f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 1297b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // build the sensor list returned to users 1307b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian mUserSensorList = mSensorList; 1317b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian if (hasGyro && 1327b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR))) { 1337b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // if we have the fancy sensor fusion, and it's not provided by the 1347b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // HAL, use our own (fused) orientation sensor by removing the 1357b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian // HAL supplied one form the user list. 1367b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian if (orientationIndex >= 0) { 1377b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian mUserSensorList.removeItemsAt(orientationIndex); 1387b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian } 139010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian } 140010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian 1417b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian run("SensorService", PRIORITY_URGENT_DISPLAY); 1427b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian mInitCheck = NO_ERROR; 1437b2b32f2e761a919deb6f82d978b379429f77b05Mathias Agopian } 144fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 145fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 146fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 147f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::registerSensor(SensorInterface* s) 148f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 149f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t event; 150f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian memset(&event, 0, sizeof(event)); 151f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 152f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const Sensor sensor(s->getSensor()); 153f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // add to the sensor list (returned to clients) 154f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorList.add(sensor); 155f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // add to our handle->SensorInterface mapping 156f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorMap.add(sensor.getHandle(), s); 157f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // create an entry in the mLastEventSeen array 158f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mLastEventSeen.add(sensor.getHandle(), event); 159f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 160f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 161f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::registerVirtualSensor(SensorInterface* s) 162f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 163f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian registerSensor(s); 164f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mVirtualSensorList.add( s ); 165f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 166f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 167fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::~SensorService() 168fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 169f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=0 ; i<mSensorMap.size() ; i++) 170f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian delete mSensorMap.valueAt(i); 171fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 172fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 1731cb13461a8cf62e3ba634e5965332f2d284f6d42Mathias Agopianstatic const String16 sDump("android.permission.DUMP"); 1741cb13461a8cf62e3ba634e5965332f2d284f6d42Mathias Agopian 175fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::dump(int fd, const Vector<String16>& args) 176fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 177fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const size_t SIZE = 1024; 178fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian char buffer[SIZE]; 179fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian String8 result; 1801cb13461a8cf62e3ba634e5965332f2d284f6d42Mathias Agopian if (!PermissionCache::checkCallingPermission(sDump)) { 181fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian snprintf(buffer, SIZE, "Permission Denial: " 182fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian "can't dump SurfaceFlinger from pid=%d, uid=%d\n", 183fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian IPCThreadState::self()->getCallingPid(), 184fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian IPCThreadState::self()->getCallingUid()); 185fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 186fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } else { 187fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 1883560fb24b668675627934356f210d84d19bf4e56Mathias Agopian snprintf(buffer, SIZE, "Sensor List:\n"); 1893560fb24b668675627934356f210d84d19bf4e56Mathias Agopian result.append(buffer); 1903560fb24b668675627934356f210d84d19bf4e56Mathias Agopian for (size_t i=0 ; i<mSensorList.size() ; i++) { 1913560fb24b668675627934356f210d84d19bf4e56Mathias Agopian const Sensor& s(mSensorList[i]); 1923560fb24b668675627934356f210d84d19bf4e56Mathias Agopian const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle())); 193984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian snprintf(buffer, SIZE, 194984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | " 195984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian "last=<%5.1f,%5.1f,%5.1f>\n", 1963560fb24b668675627934356f210d84d19bf4e56Mathias Agopian s.getName().string(), 1973560fb24b668675627934356f210d84d19bf4e56Mathias Agopian s.getVendor().string(), 1983560fb24b668675627934356f210d84d19bf4e56Mathias Agopian s.getHandle(), 19924d72350f321c17b0bfe1ef3fd52d4070a1c02c3Mathias Agopian s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f, 2003560fb24b668675627934356f210d84d19bf4e56Mathias Agopian e.data[0], e.data[1], e.data[2]); 2013560fb24b668675627934356f210d84d19bf4e56Mathias Agopian result.append(buffer); 2023560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 203984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian SensorFusion::getInstance().dump(result, buffer, SIZE); 204f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorDevice::getInstance().dump(result, buffer, SIZE); 2053560fb24b668675627934356f210d84d19bf4e56Mathias Agopian 2067c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian snprintf(buffer, SIZE, "%d active connections\n", 2077c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian mActiveConnections.size()); 208fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 209fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian snprintf(buffer, SIZE, "Active sensors:\n"); 210fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 211fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian for (size_t i=0 ; i<mActiveSensors.size() ; i++) { 2125d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian int handle = mActiveSensors.keyAt(i); 213f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n", 2145d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian getSensorName(handle).string(), 2155d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian handle, 216fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mActiveSensors.valueAt(i)->getNumConnections()); 217fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 218fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 219fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 220fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian write(fd, result.string(), result.size()); 221fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return NO_ERROR; 222fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 223fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 224fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::threadLoop() 225fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 226a551237de142549fb8a6608ee9d2fbf4b7ca2ebfSteve Block ALOGD("nuSensorService thread starting..."); 227fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 228f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const size_t numEventMax = 16 * (1 + mVirtualSensorList.size()); 229f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t buffer[numEventMax]; 230f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t scratch[numEventMax]; 231f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorDevice& device(SensorDevice::getInstance()); 232f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const size_t vcount = mVirtualSensorList.size(); 233fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 234f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian ssize_t count; 235fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian do { 236f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian count = device.poll(buffer, numEventMax); 237fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (count<0) { 238f5a1230d322c14c42331d0a1536b50c87742973bSteve Block ALOGE("sensor poll failed (%s)", strerror(-count)); 239fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian break; 240fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 241fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 24294e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian recordLastValue(buffer, count); 24394e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian 244f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // handle virtual sensors 245f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (count && vcount) { 246984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian sensors_event_t const * const event = buffer; 247f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const DefaultKeyedVector<int, SensorInterface*> virtualSensors( 248f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian getActiveVirtualSensors()); 249f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const size_t activeVirtualSensorCount = virtualSensors.size(); 250f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (activeVirtualSensorCount) { 251f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian size_t k = 0; 252984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian SensorFusion& fusion(SensorFusion::getInstance()); 253984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (fusion.isEnabled()) { 254984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian for (size_t i=0 ; i<size_t(count) ; i++) { 255984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.process(event[i]); 256984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 257984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 258f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=0 ; i<size_t(count) ; i++) { 259f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t j=0 ; j<activeVirtualSensorCount ; j++) { 260f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t out; 261f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (virtualSensors.valueAt(j)->process(&out, event[i])) { 262f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian buffer[count + k] = out; 263f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian k++; 264f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 265f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 266f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 267f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (k) { 268f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // record the last synthesized values 269f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian recordLastValue(&buffer[count], k); 270f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian count += k; 271f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // sort the buffer by time-stamps 272f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sortEventBuffer(buffer, count); 273f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 274f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 275f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 276f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 277f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // send our events to clients... 278fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const SortedVector< wp<SensorEventConnection> > activeConnections( 279fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian getActiveConnections()); 280fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian size_t numConnections = activeConnections.size(); 281f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=0 ; i<numConnections ; i++) { 282f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sp<SensorEventConnection> connection( 283f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian activeConnections[i].promote()); 284f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (connection != 0) { 285f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian connection->sendEvents(buffer, count, scratch); 286fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 287fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 288fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } while (count >= 0 || Thread::exitPending()); 289fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 2903c20fbed7f3a916ced10f2ed5a272271b7d81edeSteve Block ALOGW("Exiting SensorService::threadLoop => aborting..."); 2911a62301fc58cd2af18239b0415813461bf5fc41bMathias Agopian abort(); 292fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return false; 293fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 294fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 29594e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopianvoid SensorService::recordLastValue( 29694e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian sensors_event_t const * buffer, size_t count) 29794e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian{ 29894e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian Mutex::Autolock _l(mLock); 29994e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian 30094e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian // record the last event for each sensor 30194e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian int32_t prev = buffer[0].sensor; 30294e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian for (size_t i=1 ; i<count ; i++) { 30394e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian // record the last event of each sensor type in this buffer 30494e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian int32_t curr = buffer[i].sensor; 30594e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian if (curr != prev) { 30694e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian mLastEventSeen.editValueFor(prev) = buffer[i-1]; 30794e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian prev = curr; 30894e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian } 30994e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian } 31094e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian mLastEventSeen.editValueFor(prev) = buffer[count-1]; 31194e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian} 31294e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian 313f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count) 314f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 315f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian struct compar { 316f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian static int cmp(void const* lhs, void const* rhs) { 317f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs); 318f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs); 319a5c106a4f0afcf061728a1cb7c8c3b908728575dMathias Agopian return l->timestamp - r->timestamp; 320f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 321f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian }; 322f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian qsort(buffer, count, sizeof(sensors_event_t), compar::cmp); 323f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 324f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 325fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSortedVector< wp<SensorService::SensorEventConnection> > 326fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::getActiveConnections() const 327fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 328fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 329fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return mActiveConnections; 330fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 331fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 332f001c92436b4a66eb7687286325ced7f10c9f917Mathias AgopianDefaultKeyedVector<int, SensorInterface*> 333f001c92436b4a66eb7687286325ced7f10c9f917Mathias AgopianSensorService::getActiveVirtualSensors() const 334f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 335f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Mutex::Autolock _l(mLock); 336f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return mActiveVirtualSensors; 337f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 338f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 3395d2707214dfb97bd8dfcc6620be36841d3c82420Mathias AgopianString8 SensorService::getSensorName(int handle) const { 340010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian size_t count = mUserSensorList.size(); 3415d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian for (size_t i=0 ; i<count ; i++) { 342010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian const Sensor& sensor(mUserSensorList[i]); 3435d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian if (sensor.getHandle() == handle) { 3445d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian return sensor.getName(); 3455d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian } 3465d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian } 3475d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian String8 result("unknown"); 3485d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian return result; 3495d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian} 3505d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian 351fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianVector<Sensor> SensorService::getSensorList() 352fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 353010e42230135815907e76e5d7e5f30edf9e1799dMathias Agopian return mUserSensorList; 354fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 355fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 356fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiansp<ISensorEventConnection> SensorService::createSensorEventConnection() 357fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 358fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian sp<SensorEventConnection> result(new SensorEventConnection(this)); 359fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return result; 360fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 361fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 362db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopianvoid SensorService::cleanupConnection(SensorEventConnection* c) 363fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 364fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 365db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian const wp<SensorEventConnection> connection(c); 3667c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian size_t size = mActiveSensors.size(); 367a551237de142549fb8a6608ee9d2fbf4b7ca2ebfSteve Block ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size); 3687c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian for (size_t i=0 ; i<size ; ) { 369db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian int handle = mActiveSensors.keyAt(i); 370db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian if (c->hasSensor(handle)) { 371a551237de142549fb8a6608ee9d2fbf4b7ca2ebfSteve Block ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle); 372f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor( handle ); 373f5a1230d322c14c42331d0a1536b50c87742973bSteve Block ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle); 374f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (sensor) { 375db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian sensor->activate(c, false); 376f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 377db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian } 378db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian SensorRecord* rec = mActiveSensors.valueAt(i); 379f5a1230d322c14c42331d0a1536b50c87742973bSteve Block ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle); 380a551237de142549fb8a6608ee9d2fbf4b7ca2ebfSteve Block ALOGD_IF(DEBUG_CONNECTIONS, 381a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian "removing connection %p for sensor[%d].handle=0x%08x", 382a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian c, i, handle); 383a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian 384db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian if (rec && rec->removeConnection(connection)) { 385a551237de142549fb8a6608ee9d2fbf4b7ca2ebfSteve Block ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection"); 3867c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian mActiveSensors.removeItemsAt(i, 1); 387f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveVirtualSensors.removeItem(handle); 3887c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian delete rec; 3897c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian size--; 3907c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } else { 3917c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian i++; 392fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 393fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 3947c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian mActiveConnections.remove(connection); 395fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 396fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 397fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::enable(const sp<SensorEventConnection>& connection, 398fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle) 399fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 40050df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian if (mInitCheck != NO_ERROR) 40150df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian return mInitCheck; 40250df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 403fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 404f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor(handle); 405f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE); 406f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (err == NO_ERROR) { 407f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorRecord* rec = mActiveSensors.valueFor(handle); 408f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (rec == 0) { 409f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian rec = new SensorRecord(connection); 410f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveSensors.add(handle, rec); 411f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (sensor->isVirtual()) { 412f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveVirtualSensors.add(handle, sensor); 413f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 414f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } else { 415f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (rec->addConnection(connection)) { 416f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // this sensor is already activated, but we are adding a 417f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // connection that uses it. Immediately send down the last 4183f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian // known value of the requested sensor if it's not a 4193f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian // "continuous" sensor. 4203f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian if (sensor->getSensor().getMinDelay() == 0) { 4213f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian sensors_event_t scratch; 4223f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian sensors_event_t& event(mLastEventSeen.editValueFor(handle)); 4233f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian if (event.version == sizeof(sensors_event_t)) { 4243f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian connection->sendEvents(&event, 1); 4253f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian } 426f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 4273560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 4283560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 429f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (err == NO_ERROR) { 430f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // connection now active 431f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (connection->addSensor(handle)) { 432f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // the sensor was added (which means it wasn't already there) 433f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // so, see if this connection becomes active 434f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mActiveConnections.indexOf(connection) < 0) { 435f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveConnections.add(connection); 436f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 4377c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } 438fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 439fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 440fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return err; 441fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 442fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 443fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::disable(const sp<SensorEventConnection>& connection, 444fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle) 445fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 44650df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian if (mInitCheck != NO_ERROR) 44750df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian return mInitCheck; 44850df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 449fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian status_t err = NO_ERROR; 450fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 451fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian SensorRecord* rec = mActiveSensors.valueFor(handle); 452fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (rec) { 453fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // see if this connection becomes inactive 454fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian connection->removeSensor(handle); 455fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (connection->hasAnySensor() == false) { 456fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mActiveConnections.remove(connection); 457fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 458fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // see if this sensor becomes inactive 459fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (rec->removeConnection(connection)) { 460fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mActiveSensors.removeItem(handle); 461f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveVirtualSensors.removeItem(handle); 462fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian delete rec; 463fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 464f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor(handle); 465f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE); 4667c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } 467fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return err; 468fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 469fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 4707c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianstatus_t SensorService::setEventRate(const sp<SensorEventConnection>& connection, 471fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle, nsecs_t ns) 472fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 47350df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian if (mInitCheck != NO_ERROR) 47450df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian return mInitCheck; 47550df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 476ae09d65f5b35cb51da2e1386a6dd7a52085f1325Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor(handle); 477ae09d65f5b35cb51da2e1386a6dd7a52085f1325Mathias Agopian if (!sensor) 478ae09d65f5b35cb51da2e1386a6dd7a52085f1325Mathias Agopian return BAD_VALUE; 479ae09d65f5b35cb51da2e1386a6dd7a52085f1325Mathias Agopian 4801cd700015318727d6d42236ab6274f1949fb08baMathias Agopian if (ns < 0) 4811cd700015318727d6d42236ab6274f1949fb08baMathias Agopian return BAD_VALUE; 4821cd700015318727d6d42236ab6274f1949fb08baMathias Agopian 48362569ecf526c7c01cb68ea461c6bbd3cb26057d2Mathias Agopian nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); 48462569ecf526c7c01cb68ea461c6bbd3cb26057d2Mathias Agopian if (ns < minDelayNs) { 48562569ecf526c7c01cb68ea461c6bbd3cb26057d2Mathias Agopian ns = minDelayNs; 486ae09d65f5b35cb51da2e1386a6dd7a52085f1325Mathias Agopian } 487ae09d65f5b35cb51da2e1386a6dd7a52085f1325Mathias Agopian 4887c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian if (ns < MINIMUM_EVENTS_PERIOD) 4897c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian ns = MINIMUM_EVENTS_PERIOD; 4901cd700015318727d6d42236ab6274f1949fb08baMathias Agopian 491f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return sensor->setDelay(connection.get(), handle, ns); 492fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 493fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 494fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 495fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 496fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorRecord::SensorRecord( 497fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const sp<SensorEventConnection>& connection) 498fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 499fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mConnections.add(connection); 500fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 501fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 5027c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorRecord::addConnection( 503fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const sp<SensorEventConnection>& connection) 504fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 505fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (mConnections.indexOf(connection) < 0) { 506fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mConnections.add(connection); 5077c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return true; 508fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 5097c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return false; 510fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 511fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 512fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorRecord::removeConnection( 513fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const wp<SensorEventConnection>& connection) 514fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 515fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian ssize_t index = mConnections.indexOf(connection); 516fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (index >= 0) { 517fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mConnections.removeItemsAt(index, 1); 518fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 519fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return mConnections.size() ? false : true; 520fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 521fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 522fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 523fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 524fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorEventConnection::SensorEventConnection( 525fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const sp<SensorService>& service) 526b3989276d17f2b083bec67b695d1078fb86c6c53Mathias Agopian : mService(service), mChannel(new BitTube()) 527fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 528fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 529fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 530fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorEventConnection::~SensorEventConnection() 531fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 532a551237de142549fb8a6608ee9d2fbf4b7ca2ebfSteve Block ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this); 533fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mService->cleanupConnection(this); 534fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 535fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 536fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianvoid SensorService::SensorEventConnection::onFirstRef() 537fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 538fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 539fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 5407c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorEventConnection::addSensor(int32_t handle) { 54171d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 542f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mSensorInfo.indexOf(handle) <= 0) { 543f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorInfo.add(handle); 5447c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return true; 545fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 5467c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return false; 547fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 548fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 5497c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorEventConnection::removeSensor(int32_t handle) { 55071d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 551f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mSensorInfo.remove(handle) >= 0) { 5527c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return true; 5537c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } 5547c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return false; 555fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 556fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 557fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorEventConnection::hasSensor(int32_t handle) const { 55871d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 559f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return mSensorInfo.indexOf(handle) >= 0; 560fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 561fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 562fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorEventConnection::hasAnySensor() const { 56371d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 5647c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return mSensorInfo.size() ? true : false; 5657c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian} 5667c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian 567fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::sendEvents( 568cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian sensors_event_t const* buffer, size_t numEvents, 569cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian sensors_event_t* scratch) 570fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 571cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian // filter out events not for this connection 5723560fb24b668675627934356f210d84d19bf4e56Mathias Agopian size_t count = 0; 5733560fb24b668675627934356f210d84d19bf4e56Mathias Agopian if (scratch) { 57471d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 5753560fb24b668675627934356f210d84d19bf4e56Mathias Agopian size_t i=0; 5763560fb24b668675627934356f210d84d19bf4e56Mathias Agopian while (i<numEvents) { 5773560fb24b668675627934356f210d84d19bf4e56Mathias Agopian const int32_t curr = buffer[i].sensor; 578f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mSensorInfo.indexOf(curr) >= 0) { 5793560fb24b668675627934356f210d84d19bf4e56Mathias Agopian do { 5803560fb24b668675627934356f210d84d19bf4e56Mathias Agopian scratch[count++] = buffer[i++]; 5813560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } while ((i<numEvents) && (buffer[i].sensor == curr)); 5823560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } else { 5833560fb24b668675627934356f210d84d19bf4e56Mathias Agopian i++; 5843560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 585cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian } 5863560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } else { 5873560fb24b668675627934356f210d84d19bf4e56Mathias Agopian scratch = const_cast<sensors_event_t *>(buffer); 5883560fb24b668675627934356f210d84d19bf4e56Mathias Agopian count = numEvents; 589cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian } 590fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 591907103bf186cfdd2ed9eb3b6c36de53ade7b16f6Mathias Agopian // NOTE: ASensorEvent and sensors_event_t are the same type 592907103bf186cfdd2ed9eb3b6c36de53ade7b16f6Mathias Agopian ssize_t size = SensorEventQueue::write(mChannel, 593907103bf186cfdd2ed9eb3b6c36de53ade7b16f6Mathias Agopian reinterpret_cast<ASensorEvent const*>(scratch), count); 594fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (size == -EAGAIN) { 595fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // the destination doesn't accept events anymore, it's probably 596fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // full. For now, we just drop the events on the floor. 5973c20fbed7f3a916ced10f2ed5a272271b7d81edeSteve Block //ALOGW("dropping %d events on the floor", count); 598fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return size; 599fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 600fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 6011e0b1e8491e5f6dc59faabe70cbfa942853150e0Jeff Brown return size < 0 ? status_t(size) : status_t(NO_ERROR); 602fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 603fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 604b3989276d17f2b083bec67b695d1078fb86c6c53Mathias Agopiansp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const 605fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 606fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return mChannel; 607fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 608fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 609fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::enableDisable( 610fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle, bool enabled) 611fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 612fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian status_t err; 613fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (enabled) { 614fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian err = mService->enable(this, handle); 615fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } else { 616fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian err = mService->disable(this, handle); 617fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 618fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return err; 619fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 620fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 621fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::setEventRate( 622fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle, nsecs_t ns) 623fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 6247c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return mService->setEventRate(this, handle, ns); 625fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 626fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 627fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 628fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}; // namespace android 629fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 630