SensorService.cpp revision db5b4bce9e65ec9c2c7762b601297e4abadbc22a
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 38fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian#include "SensorService.h" 39f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "GravitySensor.h" 40f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "LinearAccelerationSensor.h" 41f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian#include "RotationVectorSensor.h" 42fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 43fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiannamespace android { 44fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 45fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 46fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorService() 47fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian : Thread(false), 4850df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian mDump("android.permission.DUMP"), 4950df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian mInitCheck(NO_INIT) 50fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 51fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 52fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 53fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianvoid SensorService::onFirstRef() 54fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 5550df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian LOGD("nuSensorService starting..."); 5650df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 57f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorDevice& dev(SensorDevice::getInstance()); 58fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 59f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (dev.initCheck() == NO_ERROR) { 60f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian uint32_t virtualSensorsNeeds = 61f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian (1<<SENSOR_TYPE_GRAVITY) | 62f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian (1<<SENSOR_TYPE_LINEAR_ACCELERATION) | 63f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian (1<<SENSOR_TYPE_ROTATION_VECTOR); 64f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensor_t const* list; 65f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian int count = dev.getSensorList(&list); 663560fb24b668675627934356f210d84d19bf4e56Mathias Agopian mLastEventSeen.setCapacity(count); 6750df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian for (int i=0 ; i<count ; i++) { 68f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian registerSensor( new HardwareSensor(list[i]) ); 69f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian switch (list[i].type) { 70f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian case SENSOR_TYPE_GRAVITY: 71f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian case SENSOR_TYPE_LINEAR_ACCELERATION: 72f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian case SENSOR_TYPE_ROTATION_VECTOR: 73f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian virtualSensorsNeeds &= ~(1<<list[i].type); 74f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian break; 7550df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian } 7650df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian } 77fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 78f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) { 79f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian registerVirtualSensor( new GravitySensor(list, count) ); 80f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 81f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) { 82f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian registerVirtualSensor( new LinearAccelerationSensor(list, count) ); 83f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 84f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) { 85f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian registerVirtualSensor( new RotationVectorSensor(list, count) ); 8650df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian } 87f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 88f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian run("SensorService", PRIORITY_URGENT_DISPLAY); 89f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mInitCheck = NO_ERROR; 90fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 91fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 92fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 93f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::registerSensor(SensorInterface* s) 94f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 95f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t event; 96f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian memset(&event, 0, sizeof(event)); 97f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 98f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const Sensor sensor(s->getSensor()); 99f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // add to the sensor list (returned to clients) 100f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorList.add(sensor); 101f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // add to our handle->SensorInterface mapping 102f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorMap.add(sensor.getHandle(), s); 103f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // create an entry in the mLastEventSeen array 104f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mLastEventSeen.add(sensor.getHandle(), event); 105f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 106f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 107f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::registerVirtualSensor(SensorInterface* s) 108f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 109f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian registerSensor(s); 110f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mVirtualSensorList.add( s ); 111f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 112f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 113fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::~SensorService() 114fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 115f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=0 ; i<mSensorMap.size() ; i++) 116f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian delete mSensorMap.valueAt(i); 117fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 118fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 119fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::dump(int fd, const Vector<String16>& args) 120fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 121fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const size_t SIZE = 1024; 122fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian char buffer[SIZE]; 123fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian String8 result; 124fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (!mDump.checkCalling()) { 125fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian snprintf(buffer, SIZE, "Permission Denial: " 126fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian "can't dump SurfaceFlinger from pid=%d, uid=%d\n", 127fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian IPCThreadState::self()->getCallingPid(), 128fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian IPCThreadState::self()->getCallingUid()); 129fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 130fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } else { 131fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 1323560fb24b668675627934356f210d84d19bf4e56Mathias Agopian snprintf(buffer, SIZE, "Sensor List:\n"); 1333560fb24b668675627934356f210d84d19bf4e56Mathias Agopian result.append(buffer); 1343560fb24b668675627934356f210d84d19bf4e56Mathias Agopian for (size_t i=0 ; i<mSensorList.size() ; i++) { 1353560fb24b668675627934356f210d84d19bf4e56Mathias Agopian const Sensor& s(mSensorList[i]); 1363560fb24b668675627934356f210d84d19bf4e56Mathias Agopian const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle())); 137f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian snprintf(buffer, SIZE, "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | last=<%5.1f,%5.1f,%5.1f>\n", 1383560fb24b668675627934356f210d84d19bf4e56Mathias Agopian s.getName().string(), 1393560fb24b668675627934356f210d84d19bf4e56Mathias Agopian s.getVendor().string(), 1403560fb24b668675627934356f210d84d19bf4e56Mathias Agopian s.getHandle(), 14124d72350f321c17b0bfe1ef3fd52d4070a1c02c3Mathias Agopian s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f, 1423560fb24b668675627934356f210d84d19bf4e56Mathias Agopian e.data[0], e.data[1], e.data[2]); 1433560fb24b668675627934356f210d84d19bf4e56Mathias Agopian result.append(buffer); 1443560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 145f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorDevice::getInstance().dump(result, buffer, SIZE); 1463560fb24b668675627934356f210d84d19bf4e56Mathias Agopian 1477c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian snprintf(buffer, SIZE, "%d active connections\n", 1487c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian mActiveConnections.size()); 149fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 150fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian snprintf(buffer, SIZE, "Active sensors:\n"); 151fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 152fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian for (size_t i=0 ; i<mActiveSensors.size() ; i++) { 1535d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian int handle = mActiveSensors.keyAt(i); 154f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n", 1555d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian getSensorName(handle).string(), 1565d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian handle, 157fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mActiveSensors.valueAt(i)->getNumConnections()); 158fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian result.append(buffer); 159fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 160fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 161fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian write(fd, result.string(), result.size()); 162fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return NO_ERROR; 163fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 164fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 165fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::threadLoop() 166fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 167fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGD("nuSensorService thread starting..."); 168fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 169f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const size_t numEventMax = 16 * (1 + mVirtualSensorList.size()); 170f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t buffer[numEventMax]; 171f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t scratch[numEventMax]; 172f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorDevice& device(SensorDevice::getInstance()); 173f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const size_t vcount = mVirtualSensorList.size(); 174fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 175f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian ssize_t count; 176fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian do { 177f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian count = device.poll(buffer, numEventMax); 178fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (count<0) { 179fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGE("sensor poll failed (%s)", strerror(-count)); 180fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian break; 181fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 182fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 18394e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian recordLastValue(buffer, count); 18494e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian 185f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // handle virtual sensors 186f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (count && vcount) { 187f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const DefaultKeyedVector<int, SensorInterface*> virtualSensors( 188f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian getActiveVirtualSensors()); 189f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian const size_t activeVirtualSensorCount = virtualSensors.size(); 190f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (activeVirtualSensorCount) { 191f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian size_t k = 0; 192f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=0 ; i<size_t(count) ; i++) { 193f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t const * const event = buffer; 194f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t j=0 ; j<activeVirtualSensorCount ; j++) { 195f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t out; 196f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (virtualSensors.valueAt(j)->process(&out, event[i])) { 197f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian buffer[count + k] = out; 198f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian k++; 199f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 200f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 201f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 202f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (k) { 203f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // record the last synthesized values 204f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian recordLastValue(&buffer[count], k); 205f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian count += k; 206f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // sort the buffer by time-stamps 207f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sortEventBuffer(buffer, count); 208f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 209f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 210f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 211f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 212f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // send our events to clients... 213fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const SortedVector< wp<SensorEventConnection> > activeConnections( 214fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian getActiveConnections()); 215fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian size_t numConnections = activeConnections.size(); 216f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian for (size_t i=0 ; i<numConnections ; i++) { 217f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sp<SensorEventConnection> connection( 218f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian activeConnections[i].promote()); 219f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (connection != 0) { 220f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian connection->sendEvents(buffer, count, scratch); 221fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 222fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 223fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } while (count >= 0 || Thread::exitPending()); 224fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 225fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGW("Exiting SensorService::threadLoop!"); 226fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return false; 227fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 228fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 22994e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopianvoid SensorService::recordLastValue( 23094e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian sensors_event_t const * buffer, size_t count) 23194e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian{ 23294e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian Mutex::Autolock _l(mLock); 23394e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian 23494e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian // record the last event for each sensor 23594e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian int32_t prev = buffer[0].sensor; 23694e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian for (size_t i=1 ; i<count ; i++) { 23794e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian // record the last event of each sensor type in this buffer 23894e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian int32_t curr = buffer[i].sensor; 23994e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian if (curr != prev) { 24094e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian mLastEventSeen.editValueFor(prev) = buffer[i-1]; 24194e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian prev = curr; 24294e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian } 24394e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian } 24494e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian mLastEventSeen.editValueFor(prev) = buffer[count-1]; 24594e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian} 24694e8f6813f3fb2beaa9bcbfb1ad9b4ae2eb46949Mathias Agopian 247f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopianvoid SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count) 248f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 249f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian struct compar { 250f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian static int cmp(void const* lhs, void const* rhs) { 251f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs); 252f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs); 253f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return r->timestamp - l->timestamp; 254f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 255f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian }; 256f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian qsort(buffer, count, sizeof(sensors_event_t), compar::cmp); 257f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 258f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 259fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSortedVector< wp<SensorService::SensorEventConnection> > 260fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::getActiveConnections() const 261fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 262fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 263fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return mActiveConnections; 264fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 265fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 266f001c92436b4a66eb7687286325ced7f10c9f917Mathias AgopianDefaultKeyedVector<int, SensorInterface*> 267f001c92436b4a66eb7687286325ced7f10c9f917Mathias AgopianSensorService::getActiveVirtualSensors() const 268f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian{ 269f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian Mutex::Autolock _l(mLock); 270f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return mActiveVirtualSensors; 271f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian} 272f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian 2735d2707214dfb97bd8dfcc6620be36841d3c82420Mathias AgopianString8 SensorService::getSensorName(int handle) const { 2745d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian size_t count = mSensorList.size(); 2755d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian for (size_t i=0 ; i<count ; i++) { 2765d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian const Sensor& sensor(mSensorList[i]); 2775d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian if (sensor.getHandle() == handle) { 2785d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian return sensor.getName(); 2795d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian } 2805d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian } 2815d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian String8 result("unknown"); 2825d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian return result; 2835d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian} 2845d2707214dfb97bd8dfcc6620be36841d3c82420Mathias Agopian 285fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianVector<Sensor> SensorService::getSensorList() 286fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 287fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return mSensorList; 288fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 289fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 290fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiansp<ISensorEventConnection> SensorService::createSensorEventConnection() 291fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 292fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian sp<SensorEventConnection> result(new SensorEventConnection(this)); 293fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return result; 294fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 295fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 296db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopianvoid SensorService::cleanupConnection(SensorEventConnection* c) 297fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 298fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 299db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian const wp<SensorEventConnection> connection(c); 3007c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian size_t size = mActiveSensors.size(); 3017c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian for (size_t i=0 ; i<size ; ) { 302db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian int handle = mActiveSensors.keyAt(i); 303db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian if (c->hasSensor(handle)) { 304f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor( handle ); 305f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (sensor) { 306db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian sensor->activate(c, false); 307f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 308db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian } 309db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian SensorRecord* rec = mActiveSensors.valueAt(i); 310db5b4bce9e65ec9c2c7762b601297e4abadbc22aMathias Agopian if (rec && rec->removeConnection(connection)) { 3117c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian mActiveSensors.removeItemsAt(i, 1); 312f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveVirtualSensors.removeItem(handle); 3137c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian delete rec; 3147c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian size--; 3157c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } else { 3167c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian i++; 317fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 318fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 3197c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian mActiveConnections.remove(connection); 320fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 321fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 322fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::enable(const sp<SensorEventConnection>& connection, 323fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle) 324fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 32550df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian if (mInitCheck != NO_ERROR) 32650df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian return mInitCheck; 32750df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 328fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 329f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor(handle); 330f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE); 331f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (err == NO_ERROR) { 332f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorRecord* rec = mActiveSensors.valueFor(handle); 333f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (rec == 0) { 334f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian rec = new SensorRecord(connection); 335f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveSensors.add(handle, rec); 336f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (sensor->isVirtual()) { 337f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveVirtualSensors.add(handle, sensor); 338f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 339f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } else { 340f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (rec->addConnection(connection)) { 341f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // this sensor is already activated, but we are adding a 342f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // connection that uses it. Immediately send down the last 343f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // known value of the requested sensor. 344f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t scratch; 345f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian sensors_event_t& event(mLastEventSeen.editValueFor(handle)); 346f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (event.version == sizeof(sensors_event_t)) { 347f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian connection->sendEvents(&event, 1); 348f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 3493560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 3503560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 351f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (err == NO_ERROR) { 352f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // connection now active 353f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (connection->addSensor(handle)) { 354f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // the sensor was added (which means it wasn't already there) 355f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian // so, see if this connection becomes active 356f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mActiveConnections.indexOf(connection) < 0) { 357f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveConnections.add(connection); 358f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian } 3597c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } 360fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 361fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 362fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return err; 363fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 364fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 365fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::disable(const sp<SensorEventConnection>& connection, 366fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle) 367fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 36850df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian if (mInitCheck != NO_ERROR) 36950df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian return mInitCheck; 37050df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 371fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian status_t err = NO_ERROR; 372fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian Mutex::Autolock _l(mLock); 373fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian SensorRecord* rec = mActiveSensors.valueFor(handle); 374fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (rec) { 375fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // see if this connection becomes inactive 376fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian connection->removeSensor(handle); 377fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (connection->hasAnySensor() == false) { 378fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mActiveConnections.remove(connection); 379fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 380fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // see if this sensor becomes inactive 381fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (rec->removeConnection(connection)) { 382fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mActiveSensors.removeItem(handle); 383f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mActiveVirtualSensors.removeItem(handle); 384fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian delete rec; 385fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 386f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor(handle); 387f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE); 3887c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } 389fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return err; 390fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 391fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 3927c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianstatus_t SensorService::setEventRate(const sp<SensorEventConnection>& connection, 393fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle, nsecs_t ns) 394fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 39550df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian if (mInitCheck != NO_ERROR) 39650df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian return mInitCheck; 39750df2959e58fc7408f98d11d77c8428397dca445Mathias Agopian 3981cd700015318727d6d42236ab6274f1949fb08baMathias Agopian if (ns < 0) 3991cd700015318727d6d42236ab6274f1949fb08baMathias Agopian return BAD_VALUE; 4001cd700015318727d6d42236ab6274f1949fb08baMathias Agopian 4017c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian if (ns < MINIMUM_EVENTS_PERIOD) 4027c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian ns = MINIMUM_EVENTS_PERIOD; 4031cd700015318727d6d42236ab6274f1949fb08baMathias Agopian 404f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian SensorInterface* sensor = mSensorMap.valueFor(handle); 405f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (!sensor) return BAD_VALUE; 406f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return sensor->setDelay(connection.get(), handle, ns); 407fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 408fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 409fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 410fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 411fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorRecord::SensorRecord( 412fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const sp<SensorEventConnection>& connection) 413fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 414fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mConnections.add(connection); 415fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 416fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 4177c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorRecord::addConnection( 418fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const sp<SensorEventConnection>& connection) 419fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 420fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (mConnections.indexOf(connection) < 0) { 421fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mConnections.add(connection); 4227c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return true; 423fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 4247c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return false; 425fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 426fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 427fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorRecord::removeConnection( 428fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const wp<SensorEventConnection>& connection) 429fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 430fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian ssize_t index = mConnections.indexOf(connection); 431fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (index >= 0) { 432fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mConnections.removeItemsAt(index, 1); 433fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 434fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return mConnections.size() ? false : true; 435fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 436fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 437fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 438fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 439fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorEventConnection::SensorEventConnection( 440fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian const sp<SensorService>& service) 441fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian : mService(service), mChannel(new SensorChannel()) 442fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 443fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 444fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 445fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias AgopianSensorService::SensorEventConnection::~SensorEventConnection() 446fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 447fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian mService->cleanupConnection(this); 448fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 449fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 450fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianvoid SensorService::SensorEventConnection::onFirstRef() 451fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 452fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 453fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 4547c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorEventConnection::addSensor(int32_t handle) { 45571d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 456f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mSensorInfo.indexOf(handle) <= 0) { 457f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian mSensorInfo.add(handle); 4587c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return true; 459fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 4607c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return false; 461fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 462fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 4637c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopianbool SensorService::SensorEventConnection::removeSensor(int32_t handle) { 46471d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 465f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mSensorInfo.remove(handle) >= 0) { 4667c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return true; 4677c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian } 4687c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return false; 469fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 470fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 471fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorEventConnection::hasSensor(int32_t handle) const { 47271d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 473f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian return mSensorInfo.indexOf(handle) >= 0; 474fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 475fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 476fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianbool SensorService::SensorEventConnection::hasAnySensor() const { 47771d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 4787c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return mSensorInfo.size() ? true : false; 4797c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian} 4807c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian 481fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::sendEvents( 482cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian sensors_event_t const* buffer, size_t numEvents, 483cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian sensors_event_t* scratch) 484fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 485cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian // filter out events not for this connection 4863560fb24b668675627934356f210d84d19bf4e56Mathias Agopian size_t count = 0; 4873560fb24b668675627934356f210d84d19bf4e56Mathias Agopian if (scratch) { 48871d7a5c289c6ef6b5fc86dd4784a075ca6470e38Mathias Agopian Mutex::Autolock _l(mConnectionLock); 4893560fb24b668675627934356f210d84d19bf4e56Mathias Agopian size_t i=0; 4903560fb24b668675627934356f210d84d19bf4e56Mathias Agopian while (i<numEvents) { 4913560fb24b668675627934356f210d84d19bf4e56Mathias Agopian const int32_t curr = buffer[i].sensor; 492f001c92436b4a66eb7687286325ced7f10c9f917Mathias Agopian if (mSensorInfo.indexOf(curr) >= 0) { 4933560fb24b668675627934356f210d84d19bf4e56Mathias Agopian do { 4943560fb24b668675627934356f210d84d19bf4e56Mathias Agopian scratch[count++] = buffer[i++]; 4953560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } while ((i<numEvents) && (buffer[i].sensor == curr)); 4963560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } else { 4973560fb24b668675627934356f210d84d19bf4e56Mathias Agopian i++; 4983560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } 499cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian } 5003560fb24b668675627934356f210d84d19bf4e56Mathias Agopian } else { 5013560fb24b668675627934356f210d84d19bf4e56Mathias Agopian scratch = const_cast<sensors_event_t *>(buffer); 5023560fb24b668675627934356f210d84d19bf4e56Mathias Agopian count = numEvents; 503cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian } 504fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 5053560fb24b668675627934356f210d84d19bf4e56Mathias Agopian if (count == 0) 5063560fb24b668675627934356f210d84d19bf4e56Mathias Agopian return 0; 5073560fb24b668675627934356f210d84d19bf4e56Mathias Agopian 508cf51001dbf28e9885fcacd4048902f1c75768fe9Mathias Agopian ssize_t size = mChannel->write(scratch, count*sizeof(sensors_event_t)); 509fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (size == -EAGAIN) { 510fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // the destination doesn't accept events anymore, it's probably 511fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian // full. For now, we just drop the events on the floor. 512fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGW("dropping %d events on the floor", count); 513fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return size; 514fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 515fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 516fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian LOGE_IF(size<0, "dropping %d events on the floor (%s)", 517fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian count, strerror(-size)); 518fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 5191e0b1e8491e5f6dc59faabe70cbfa942853150e0Jeff Brown return size < 0 ? status_t(size) : status_t(NO_ERROR); 520fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 521fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 522fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopiansp<SensorChannel> SensorService::SensorEventConnection::getSensorChannel() const 523fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 524fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return mChannel; 525fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 526fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 527fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::enableDisable( 528fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle, bool enabled) 529fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 530fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian status_t err; 531fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian if (enabled) { 532fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian err = mService->enable(this, handle); 533fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } else { 534fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian err = mService->disable(this, handle); 535fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian } 536fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian return err; 537fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 538fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 539fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopianstatus_t SensorService::SensorEventConnection::setEventRate( 540fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian int handle, nsecs_t ns) 541fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian{ 5427c1c531872a95051cb11ec829e3daf890d9bb58aMathias Agopian return mService->setEventRate(this, handle, ns); 543fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian} 544fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 545fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian// --------------------------------------------------------------------------- 546fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian}; // namespace android 547fc32881fcc68640d008c7515cdd1bcd866f72cd5Mathias Agopian 548