SensorService.cpp revision 984826cc158193e61e3a00359ef4f6699c7d748a
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 21fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/SortedVector.h> 22fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/KeyedVector.h> 23fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/threads.h> 24fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/Atomic.h> 25fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/Errors.h> 26fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <utils/RefBase.h> 27451beee076cac09f817abae78a990dea108a9482Mathias Agopian#include <utils/Singleton.h> 28c4a930d1d5a432a1f302763ac55460d6e83fe7e0Mathias Agopian#include <utils/String16.h> 29fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 30fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <binder/BinderService.h> 31451beee076cac09f817abae78a990dea108a9482Mathias Agopian#include <binder/IServiceManager.h> 32fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 33fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <gui/ISensorServer.h> 34fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <gui/ISensorEventConnection.h> 35fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 36fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include <hardware/sensors.h> 37fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 38984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "CorrectedGyroSensor.h" 39f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "GravitySensor.h" 40f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "LinearAccelerationSensor.h" 41984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "OrientationSensor.h" 42f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "RotationVectorSensor.h" 43984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "SensorFusion.h" 44984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian#include "SensorService.h" 45fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 46fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiannamespace android { 47fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 48fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 49fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorService() 50e04a63b3053270d64890f156869e7cf75c436fbbMathias Agopian : mDump("android.permission.DUMP"), 5150df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian mInitCheck(NO_INIT) 52fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 53fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 54fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 55fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianvoid SensorService::onFirstRef() 56fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 5750df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian LOGD("nuSensorService starting..."); 5850df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 59f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorDevice& dev(SensorDevice::getInstance()); 60fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 61f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (dev.initCheck() == NO_ERROR) { 62f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian uint32_t virtualSensorsNeeds = 63f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian (1<<SENSOR_TYPE_GRAVITY) | 64f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian (1<<SENSOR_TYPE_LINEAR_ACCELERATION) | 65f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian (1<<SENSOR_TYPE_ROTATION_VECTOR); 66f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensor_t const* list; 67f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian int count = dev.getSensorList(&list); 683560fb24b668675627934356f210d84d19bf4e56Mathias Agopian mLastEventSeen.setCapacity(count); 6950df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian for (int i=0 ; i<count ; i++) { 70f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian registerSensor( new HardwareSensor(list[i]) ); 71f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian switch (list[i].type) { 72f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian case SENSOR_TYPE_GRAVITY: 73f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian case SENSOR_TYPE_LINEAR_ACCELERATION: 74f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian case SENSOR_TYPE_ROTATION_VECTOR: 75f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian virtualSensorsNeeds &= ~(1<<list[i].type); 76f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian break; 7750df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian } 7850df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian } 79fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 80984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // it's safe to instantiate the SensorFusion object here 81984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // (it wants to be instantiated after h/w sensors have been 82984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // registered) 83984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian const SensorFusion& fusion(SensorFusion::getInstance()); 84984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 85984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // Always instantiate Android's virtual sensors. Since they are 86984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // instantiated behind sensors from the HAL, they won't 87984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // interfere with applications, unless they looks specifically 88984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // for them (by name). 89984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 90984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian registerVirtualSensor( new RotationVectorSensor() ); 91984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian registerVirtualSensor( new GravitySensor(list, count) ); 92984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian registerVirtualSensor( new LinearAccelerationSensor(list, count) ); 93984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian 94984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // if we have a gyro, we have the option of enabling these 95984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // "better" orientation and gyro sensors 96984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (fusion.hasGyro()) { 97984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian // FIXME: OrientationSensor buggy when not pointing north 98984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian registerVirtualSensor( new OrientationSensor() ); 99984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian registerVirtualSensor( new CorrectedGyroSensor(list, count) ); 10050df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian } 101f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 102f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian run("SensorService", PRIORITY_URGENT_DISPLAY); 103f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mInitCheck = NO_ERROR; 104fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 105fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 106fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 107f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::registerSensor(SensorInterface* s) 108f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 109f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t event; 110f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian memset(&event, 0, sizeof(event)); 111f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 112f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const Sensor sensor(s->getSensor()); 113f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // add to the sensor list (returned to clients) 114f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorList.add(sensor); 115f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // add to our handle->SensorInterface mapping 116f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorMap.add(sensor.getHandle(), s); 117f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // create an entry in the mLastEventSeen array 118f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mLastEventSeen.add(sensor.getHandle(), event); 119f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 120f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 121f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::registerVirtualSensor(SensorInterface* s) 122f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 123f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian registerSensor(s); 124f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mVirtualSensorList.add( s ); 125f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 126f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 127fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::~SensorService() 128fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 129f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=0 ; i<mSensorMap.size() ; i++) 130f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian delete mSensorMap.valueAt(i); 131fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 132fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 133fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::dump(int fd, const Vector<String16>& args) 134fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 135fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const size_t SIZE = 1024; 136fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian char buffer[SIZE]; 137fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian String8 result; 138fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (!mDump.checkCalling()) { 139fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian snprintf(buffer, SIZE, "Permission Denial: " 140fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian "can't dump SurfaceFlinger from pid=%d, uid=%d\n", 141fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian IPCThreadState::self()->getCallingPid(), 142fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian IPCThreadState::self()->getCallingUid()); 143fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 144fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } else { 145fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 1463560fb24b668675627934356f210d84d19bf4e56Mathias Agopian snprintf(buffer, SIZE, "Sensor List:\n"); 1473560fb24b668675627934356f210d84d19bf4e56Mathias Agopian result.append(buffer); 1483560fb24b668675627934356f210d84d19bf4e56Mathias Agopian for (size_t i=0 ; i<mSensorList.size() ; i++) { 1493560fb24b668675627934356f210d84d19bf4e56Mathias Agopian const Sensor& s(mSensorList[i]); 1503560fb24b668675627934356f210d84d19bf4e56Mathias Agopian const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle())); 151984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian snprintf(buffer, SIZE, 152984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | " 153984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian "last=<%5.1f,%5.1f,%5.1f>\n", 1543560fb24b668675627934356f210d84d19bf4e56Mathias Agopian s.getName().string(), 1553560fb24b668675627934356f210d84d19bf4e56Mathias Agopian s.getVendor().string(), 1563560fb24b668675627934356f210d84d19bf4e56Mathias Agopian s.getHandle(), 15724d72350f321c17b0bfe1ef3fd52d4070a1c02c3Mathias Agopian s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f, 1583560fb24b668675627934356f210d84d19bf4e56Mathias Agopian e.data[0], e.data[1], e.data[2]); 1593560fb24b668675627934356f210d84d19bf4e56Mathias Agopian result.append(buffer); 1603560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 161984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian SensorFusion::getInstance().dump(result, buffer, SIZE); 162f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorDevice::getInstance().dump(result, buffer, SIZE); 1633560fb24b668675627934356f210d84d19bf4e56Mathias Agopian 1647c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian snprintf(buffer, SIZE, "%d active connections\n", 1657c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian mActiveConnections.size()); 166fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 167fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian snprintf(buffer, SIZE, "Active sensors:\n"); 168fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 169fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian for (size_t i=0 ; i<mActiveSensors.size() ; i++) { 1705d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian int handle = mActiveSensors.keyAt(i); 171f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n", 1725d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian getSensorName(handle).string(), 1735d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian handle, 174fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mActiveSensors.valueAt(i)->getNumConnections()); 175fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 176fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 177fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 178fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian write(fd, result.string(), result.size()); 179fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return NO_ERROR; 180fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 181fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 182fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::threadLoop() 183fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 184fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGD("nuSensorService thread starting..."); 185fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 186f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const size_t numEventMax = 16 * (1 + mVirtualSensorList.size()); 187f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t buffer[numEventMax]; 188f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t scratch[numEventMax]; 189f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorDevice& device(SensorDevice::getInstance()); 190f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const size_t vcount = mVirtualSensorList.size(); 191fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 192f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian ssize_t count; 193fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian do { 194f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian count = device.poll(buffer, numEventMax); 195fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (count<0) { 196fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGE("sensor poll failed (%s)", strerror(-count)); 197fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian break; 198fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 199fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 20094e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian recordLastValue(buffer, count); 20194e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian 202f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // handle virtual sensors 203f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (count && vcount) { 204984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian sensors_event_t const * const event = buffer; 205f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const DefaultKeyedVector<int, SensorInterface*> virtualSensors( 206f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian getActiveVirtualSensors()); 207f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const size_t activeVirtualSensorCount = virtualSensors.size(); 208f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (activeVirtualSensorCount) { 209f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian size_t k = 0; 210984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian SensorFusion& fusion(SensorFusion::getInstance()); 211984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian if (fusion.isEnabled()) { 212984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian for (size_t i=0 ; i<size_t(count) ; i++) { 213984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian fusion.process(event[i]); 214984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 215984826cc158193e61e3a00359ef4f6699c7d748aMathias Agopian } 216f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=0 ; i<size_t(count) ; i++) { 217f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t j=0 ; j<activeVirtualSensorCount ; j++) { 218f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t out; 219f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (virtualSensors.valueAt(j)->process(&out, event[i])) { 220f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian buffer[count + k] = out; 221f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian k++; 222f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 223f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 224f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 225f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (k) { 226f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // record the last synthesized values 227f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian recordLastValue(&buffer[count], k); 228f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian count += k; 229f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // sort the buffer by time-stamps 230f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sortEventBuffer(buffer, count); 231f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 232f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 233f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 234f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 235f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // send our events to clients... 236fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const SortedVector< wp<SensorEventConnection> > activeConnections( 237fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian getActiveConnections()); 238fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian size_t numConnections = activeConnections.size(); 239f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=0 ; i<numConnections ; i++) { 240f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sp<SensorEventConnection> connection( 241f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian activeConnections[i].promote()); 242f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (connection != 0) { 243f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian connection->sendEvents(buffer, count, scratch); 244fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 245fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 246fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } while (count >= 0 || Thread::exitPending()); 247fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 248fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGW("Exiting SensorService::threadLoop!"); 249fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return false; 250fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 251fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 25294e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopianvoid SensorService::recordLastValue( 25394e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian sensors_event_t const * buffer, size_t count) 25494e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian{ 25594e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian Mutex::Autolock _l(mLock); 25694e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian 25794e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian // record the last event for each sensor 25894e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian int32_t prev = buffer[0].sensor; 25994e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian for (size_t i=1 ; i<count ; i++) { 26094e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian // record the last event of each sensor type in this buffer 26194e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian int32_t curr = buffer[i].sensor; 26294e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian if (curr != prev) { 26394e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian mLastEventSeen.editValueFor(prev) = buffer[i-1]; 26494e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian prev = curr; 26594e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian } 26694e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian } 26794e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian mLastEventSeen.editValueFor(prev) = buffer[count-1]; 26894e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian} 26994e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian 270f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count) 271f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 272f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian struct compar { 273f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian static int cmp(void const* lhs, void const* rhs) { 274f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs); 275f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs); 276f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return r->timestamp - l->timestamp; 277f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 278f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian }; 279f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian qsort(buffer, count, sizeof(sensors_event_t), compar::cmp); 280f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 281f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 282fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSortedVector< wp<SensorService::SensorEventConnection> > 283fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::getActiveConnections() const 284fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 285fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 286fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return mActiveConnections; 287fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 288fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 289f001c92436b4a66eb7687286325ced7f10c9f917Mathias AgopianDefaultKeyedVector<int, SensorInterface*> 290f001c92436b4a66eb7687286325ced7f10c9f917Mathias AgopianSensorService::getActiveVirtualSensors() const 291f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 292f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Mutex::Autolock _l(mLock); 293f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return mActiveVirtualSensors; 294f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 295f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 2965d2707214dfb97bd8dfcc6620be36841d3c82420Mathias AgopianString8 SensorService::getSensorName(int handle) const { 2975d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian size_t count = mSensorList.size(); 2985d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian for (size_t i=0 ; i<count ; i++) { 2995d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian const Sensor& sensor(mSensorList[i]); 3005d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian if (sensor.getHandle() == handle) { 3015d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian return sensor.getName(); 3025d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian } 3035d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian } 3045d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian String8 result("unknown"); 3055d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian return result; 3065d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian} 3075d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian 308fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianVector<Sensor> SensorService::getSensorList() 309fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 310fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return mSensorList; 311fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 312fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 313fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiansp<ISensorEventConnection> SensorService::createSensorEventConnection() 314fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 315fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian sp<SensorEventConnection> result(new SensorEventConnection(this)); 316fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return result; 317fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 318fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 319db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopianvoid SensorService::cleanupConnection(SensorEventConnection* c) 320fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 321fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 322db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian const wp<SensorEventConnection> connection(c); 3237c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian size_t size = mActiveSensors.size(); 324a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size); 3257c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian for (size_t i=0 ; i<size ; ) { 326db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian int handle = mActiveSensors.keyAt(i); 327db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian if (c->hasSensor(handle)) { 328a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle); 329f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor( handle ); 330a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle); 331f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (sensor) { 332db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian sensor->activate(c, false); 333f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 334db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian } 335db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian SensorRecord* rec = mActiveSensors.valueAt(i); 336a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle); 337a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGD_IF(DEBUG_CONNECTIONS, 338a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian "removing connection %p for sensor[%d].handle=0x%08x", 339a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian c, i, handle); 340a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian 341db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian if (rec && rec->removeConnection(connection)) { 342a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection"); 3437c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian mActiveSensors.removeItemsAt(i, 1); 344f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveVirtualSensors.removeItem(handle); 3457c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian delete rec; 3467c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian size--; 3477c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } else { 3487c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian i++; 349fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 350fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 3517c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian mActiveConnections.remove(connection); 352fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 353fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 354fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::enable(const sp<SensorEventConnection>& connection, 355fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle) 356fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 35750df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian if (mInitCheck != NO_ERROR) 35850df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian return mInitCheck; 35950df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 360fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 361f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor(handle); 362f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE); 363f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (err == NO_ERROR) { 364f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorRecord* rec = mActiveSensors.valueFor(handle); 365f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (rec == 0) { 366f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian rec = new SensorRecord(connection); 367f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveSensors.add(handle, rec); 368f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (sensor->isVirtual()) { 369f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveVirtualSensors.add(handle, sensor); 370f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 371f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } else { 372f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (rec->addConnection(connection)) { 373f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // this sensor is already activated, but we are adding a 374f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // connection that uses it. Immediately send down the last 3753f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian // known value of the requested sensor if it's not a 3763f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian // "continuous" sensor. 3773f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian if (sensor->getSensor().getMinDelay() == 0) { 3783f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian sensors_event_t scratch; 3793f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian sensors_event_t& event(mLastEventSeen.editValueFor(handle)); 3803f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian if (event.version == sizeof(sensors_event_t)) { 3813f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian connection->sendEvents(&event, 1); 3823f2f8916112126fb1a93e4a4b4e2d4e01570aeeeMathias Agopian } 383f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 3843560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 3853560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 386f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (err == NO_ERROR) { 387f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // connection now active 388f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (connection->addSensor(handle)) { 389f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // the sensor was added (which means it wasn't already there) 390f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // so, see if this connection becomes active 391f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mActiveConnections.indexOf(connection) < 0) { 392f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveConnections.add(connection); 393f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 3947c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } 395fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 396fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 397fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return err; 398fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 399fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 400fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::disable(const sp<SensorEventConnection>& connection, 401fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle) 402fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 40350df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian if (mInitCheck != NO_ERROR) 40450df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian return mInitCheck; 40550df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 406fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian status_t err = NO_ERROR; 407fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 408fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian SensorRecord* rec = mActiveSensors.valueFor(handle); 409fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (rec) { 410fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // see if this connection becomes inactive 411fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian connection->removeSensor(handle); 412fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (connection->hasAnySensor() == false) { 413fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mActiveConnections.remove(connection); 414fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 415fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // see if this sensor becomes inactive 416fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (rec->removeConnection(connection)) { 417fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mActiveSensors.removeItem(handle); 418f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveVirtualSensors.removeItem(handle); 419fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian delete rec; 420fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 421f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor(handle); 422f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE); 4237c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } 424fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return err; 425fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 426fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 4277c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianstatus_t SensorService::setEventRate(const sp<SensorEventConnection>& connection, 428fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle, nsecs_t ns) 429fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 43050df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian if (mInitCheck != NO_ERROR) 43150df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian return mInitCheck; 43250df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 4331cd700015318727d6d42236ab6274f1949fb08baMathias Agopian if (ns < 0) 4341cd700015318727d6d42236ab6274f1949fb08baMathias Agopian return BAD_VALUE; 4351cd700015318727d6d42236ab6274f1949fb08baMathias Agopian 4367c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian if (ns < MINIMUM_EVENTS_PERIOD) 4377c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian ns = MINIMUM_EVENTS_PERIOD; 4381cd700015318727d6d42236ab6274f1949fb08baMathias Agopian 439f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor(handle); 440f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (!sensor) return BAD_VALUE; 441f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return sensor->setDelay(connection.get(), handle, ns); 442fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 443fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 444fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 445fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 446fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorRecord::SensorRecord( 447fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const sp<SensorEventConnection>& connection) 448fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 449fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mConnections.add(connection); 450fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 451fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 4527c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorRecord::addConnection( 453fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const sp<SensorEventConnection>& connection) 454fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 455fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (mConnections.indexOf(connection) < 0) { 456fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mConnections.add(connection); 4577c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return true; 458fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 4597c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return false; 460fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 461fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 462fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorRecord::removeConnection( 463fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const wp<SensorEventConnection>& connection) 464fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 465fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian ssize_t index = mConnections.indexOf(connection); 466fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (index >= 0) { 467fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mConnections.removeItemsAt(index, 1); 468fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 469fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return mConnections.size() ? false : true; 470fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 471fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 472fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 473fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 474fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorEventConnection::SensorEventConnection( 475fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const sp<SensorService>& service) 476fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian : mService(service), mChannel(new SensorChannel()) 477fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 478fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 479fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 480fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorEventConnection::~SensorEventConnection() 481fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 482a1b7db95b6ccf5be9d8dfaac1b8f45494813edc0Mathias Agopian LOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this); 483fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mService->cleanupConnection(this); 484fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 485fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 486fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianvoid SensorService::SensorEventConnection::onFirstRef() 487fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 488fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 489fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 4907c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorEventConnection::addSensor(int32_t handle) { 49171d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 492f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mSensorInfo.indexOf(handle) <= 0) { 493f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorInfo.add(handle); 4947c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return true; 495fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 4967c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return false; 497fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 498fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 4997c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorEventConnection::removeSensor(int32_t handle) { 50071d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 501f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mSensorInfo.remove(handle) >= 0) { 5027c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return true; 5037c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } 5047c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return false; 505fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 506fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 507fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorEventConnection::hasSensor(int32_t handle) const { 50871d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 509f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return mSensorInfo.indexOf(handle) >= 0; 510fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 511fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 512fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorEventConnection::hasAnySensor() const { 51371d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 5147c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return mSensorInfo.size() ? true : false; 5157c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian} 5167c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian 517fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::sendEvents( 518cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian sensors_event_t const* buffer, size_t numEvents, 519cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian sensors_event_t* scratch) 520fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 521cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian // filter out events not for this connection 5223560fb24b668675627934356f210d84d19bf4e56Mathias Agopian size_t count = 0; 5233560fb24b668675627934356f210d84d19bf4e56Mathias Agopian if (scratch) { 52471d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 5253560fb24b668675627934356f210d84d19bf4e56Mathias Agopian size_t i=0; 5263560fb24b668675627934356f210d84d19bf4e56Mathias Agopian while (i<numEvents) { 5273560fb24b668675627934356f210d84d19bf4e56Mathias Agopian const int32_t curr = buffer[i].sensor; 528f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mSensorInfo.indexOf(curr) >= 0) { 5293560fb24b668675627934356f210d84d19bf4e56Mathias Agopian do { 5303560fb24b668675627934356f210d84d19bf4e56Mathias Agopian scratch[count++] = buffer[i++]; 5313560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } while ((i<numEvents) && (buffer[i].sensor == curr)); 5323560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } else { 5333560fb24b668675627934356f210d84d19bf4e56Mathias Agopian i++; 5343560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 535cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian } 5363560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } else { 5373560fb24b668675627934356f210d84d19bf4e56Mathias Agopian scratch = const_cast<sensors_event_t *>(buffer); 5383560fb24b668675627934356f210d84d19bf4e56Mathias Agopian count = numEvents; 539cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian } 540fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 5413560fb24b668675627934356f210d84d19bf4e56Mathias Agopian if (count == 0) 5423560fb24b668675627934356f210d84d19bf4e56Mathias Agopian return 0; 5433560fb24b668675627934356f210d84d19bf4e56Mathias Agopian 544cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian ssize_t size = mChannel->write(scratch, count*sizeof(sensors_event_t)); 545fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (size == -EAGAIN) { 546fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // the destination doesn't accept events anymore, it's probably 547fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // full. For now, we just drop the events on the floor. 548fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGW("dropping %d events on the floor", count); 549fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return size; 550fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 551fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 552fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGE_IF(size<0, "dropping %d events on the floor (%s)", 553fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian count, strerror(-size)); 554fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 5551e0b1e8491e5f6dc59faabe70cbfa942853150e0Jeff Brown return size < 0 ? status_t(size) : status_t(NO_ERROR); 556fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 557fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 558fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiansp<SensorChannel> SensorService::SensorEventConnection::getSensorChannel() const 559fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 560fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return mChannel; 561fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 562fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 563fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::enableDisable( 564fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle, bool enabled) 565fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 566fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian status_t err; 567fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (enabled) { 568fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian err = mService->enable(this, handle); 569fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } else { 570fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian err = mService->disable(this, handle); 571fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 572fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return err; 573fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 574fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 575fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::setEventRate( 576fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle, nsecs_t ns) 577fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 5787c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return mService->setEventRate(this, handle, ns); 579fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 580fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 581fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 582fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}; // namespace android 583fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 584