Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdint.h> |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 18 | #include <math.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 21 | #include <cutils/properties.h> |
| 22 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 23 | #include <utils/SortedVector.h> |
| 24 | #include <utils/KeyedVector.h> |
| 25 | #include <utils/threads.h> |
| 26 | #include <utils/Atomic.h> |
| 27 | #include <utils/Errors.h> |
| 28 | #include <utils/RefBase.h> |
Mathias Agopian | 451beee | 2010-07-19 15:03:55 -0700 | [diff] [blame] | 29 | #include <utils/Singleton.h> |
Mathias Agopian | c4a930d | 2010-07-22 22:19:43 -0700 | [diff] [blame] | 30 | #include <utils/String16.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 31 | |
| 32 | #include <binder/BinderService.h> |
Mathias Agopian | 451beee | 2010-07-19 15:03:55 -0700 | [diff] [blame] | 33 | #include <binder/IServiceManager.h> |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 34 | #include <binder/PermissionCache.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 35 | |
| 36 | #include <gui/ISensorServer.h> |
| 37 | #include <gui/ISensorEventConnection.h> |
Mathias Agopian | 907103b | 2012-04-02 18:38:02 -0700 | [diff] [blame] | 38 | #include <gui/SensorEventQueue.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 39 | |
| 40 | #include <hardware/sensors.h> |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 41 | #include <hardware_legacy/power.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 42 | |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 43 | #include "BatteryService.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 44 | #include "CorrectedGyroSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 45 | #include "GravitySensor.h" |
| 46 | #include "LinearAccelerationSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 47 | #include "OrientationSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 48 | #include "RotationVectorSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 49 | #include "SensorFusion.h" |
| 50 | #include "SensorService.h" |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 51 | |
| 52 | namespace android { |
| 53 | // --------------------------------------------------------------------------- |
| 54 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 55 | /* |
| 56 | * Notes: |
| 57 | * |
| 58 | * - what about a gyro-corrected magnetic-field sensor? |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 59 | * - run mag sensor from time to time to force calibration |
| 60 | * - gravity sensor length is wrong (=> drift in linear-acc sensor) |
| 61 | * |
| 62 | */ |
| 63 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 64 | const char* SensorService::WAKE_LOCK_NAME = "SensorService"; |
| 65 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 66 | SensorService::SensorService() |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 67 | : mInitCheck(NO_INIT) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 68 | { |
| 69 | } |
| 70 | |
| 71 | void SensorService::onFirstRef() |
| 72 | { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 73 | ALOGD("nuSensorService starting..."); |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 74 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 75 | SensorDevice& dev(SensorDevice::getInstance()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 76 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 77 | if (dev.initCheck() == NO_ERROR) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 78 | sensor_t const* list; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 79 | ssize_t count = dev.getSensorList(&list); |
| 80 | if (count > 0) { |
| 81 | ssize_t orientationIndex = -1; |
| 82 | bool hasGyro = false; |
| 83 | uint32_t virtualSensorsNeeds = |
| 84 | (1<<SENSOR_TYPE_GRAVITY) | |
| 85 | (1<<SENSOR_TYPE_LINEAR_ACCELERATION) | |
| 86 | (1<<SENSOR_TYPE_ROTATION_VECTOR); |
| 87 | |
| 88 | mLastEventSeen.setCapacity(count); |
| 89 | for (ssize_t i=0 ; i<count ; i++) { |
| 90 | registerSensor( new HardwareSensor(list[i]) ); |
| 91 | switch (list[i].type) { |
| 92 | case SENSOR_TYPE_ORIENTATION: |
| 93 | orientationIndex = i; |
| 94 | break; |
| 95 | case SENSOR_TYPE_GYROSCOPE: |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame^] | 96 | case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 97 | hasGyro = true; |
| 98 | break; |
| 99 | case SENSOR_TYPE_GRAVITY: |
| 100 | case SENSOR_TYPE_LINEAR_ACCELERATION: |
| 101 | case SENSOR_TYPE_ROTATION_VECTOR: |
| 102 | virtualSensorsNeeds &= ~(1<<list[i].type); |
| 103 | break; |
| 104 | } |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 105 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 106 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 107 | // it's safe to instantiate the SensorFusion object here |
| 108 | // (it wants to be instantiated after h/w sensors have been |
| 109 | // registered) |
| 110 | const SensorFusion& fusion(SensorFusion::getInstance()); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 111 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 112 | // build the sensor list returned to users |
| 113 | mUserSensorList = mSensorList; |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 114 | |
| 115 | if (hasGyro) { |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame^] | 116 | Sensor aSensor; |
| 117 | |
| 118 | // Add Android virtual sensors if they're not already |
| 119 | // available in the HAL |
| 120 | |
| 121 | aSensor = registerVirtualSensor( new RotationVectorSensor() ); |
| 122 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) { |
| 123 | mUserSensorList.add(aSensor); |
| 124 | } |
| 125 | |
| 126 | aSensor = registerVirtualSensor( new GravitySensor(list, count) ); |
| 127 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) { |
| 128 | mUserSensorList.add(aSensor); |
| 129 | } |
| 130 | |
| 131 | aSensor = registerVirtualSensor( new LinearAccelerationSensor(list, count) ); |
| 132 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) { |
| 133 | mUserSensorList.add(aSensor); |
| 134 | } |
| 135 | |
| 136 | aSensor = registerVirtualSensor( new OrientationSensor() ); |
| 137 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) { |
| 138 | // if we are doing our own rotation-vector, also add |
| 139 | // the orientation sensor and remove the HAL provided one. |
| 140 | mUserSensorList.replaceAt(aSensor, orientationIndex); |
| 141 | } |
| 142 | |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 143 | // virtual debugging sensors are not added to mUserSensorList |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame^] | 144 | registerVirtualSensor( new CorrectedGyroSensor(list, count) ); |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 145 | registerVirtualSensor( new GyroDriftSensor() ); |
| 146 | } |
| 147 | |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 148 | // debugging sensor list |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame^] | 149 | mUserSensorListDebug = mSensorList; |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 150 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 151 | run("SensorService", PRIORITY_URGENT_DISPLAY); |
| 152 | mInitCheck = NO_ERROR; |
| 153 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 154 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame^] | 157 | Sensor SensorService::registerSensor(SensorInterface* s) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 158 | { |
| 159 | sensors_event_t event; |
| 160 | memset(&event, 0, sizeof(event)); |
| 161 | |
| 162 | const Sensor sensor(s->getSensor()); |
| 163 | // add to the sensor list (returned to clients) |
| 164 | mSensorList.add(sensor); |
| 165 | // add to our handle->SensorInterface mapping |
| 166 | mSensorMap.add(sensor.getHandle(), s); |
| 167 | // create an entry in the mLastEventSeen array |
| 168 | mLastEventSeen.add(sensor.getHandle(), event); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame^] | 169 | |
| 170 | return sensor; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame^] | 173 | Sensor SensorService::registerVirtualSensor(SensorInterface* s) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 174 | { |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame^] | 175 | Sensor sensor = registerSensor(s); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 176 | mVirtualSensorList.add( s ); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame^] | 177 | return sensor; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 180 | SensorService::~SensorService() |
| 181 | { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 182 | for (size_t i=0 ; i<mSensorMap.size() ; i++) |
| 183 | delete mSensorMap.valueAt(i); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 186 | static const String16 sDump("android.permission.DUMP"); |
| 187 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 188 | status_t SensorService::dump(int fd, const Vector<String16>& args) |
| 189 | { |
| 190 | const size_t SIZE = 1024; |
| 191 | char buffer[SIZE]; |
| 192 | String8 result; |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 193 | if (!PermissionCache::checkCallingPermission(sDump)) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 194 | snprintf(buffer, SIZE, "Permission Denial: " |
| 195 | "can't dump SurfaceFlinger from pid=%d, uid=%d\n", |
| 196 | IPCThreadState::self()->getCallingPid(), |
| 197 | IPCThreadState::self()->getCallingUid()); |
| 198 | result.append(buffer); |
| 199 | } else { |
| 200 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 201 | snprintf(buffer, SIZE, "Sensor List:\n"); |
| 202 | result.append(buffer); |
| 203 | for (size_t i=0 ; i<mSensorList.size() ; i++) { |
| 204 | const Sensor& s(mSensorList[i]); |
| 205 | const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle())); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 206 | snprintf(buffer, SIZE, |
| 207 | "%-48s| %-32s | 0x%08x | maxRate=%7.2fHz | " |
| 208 | "last=<%5.1f,%5.1f,%5.1f>\n", |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 209 | s.getName().string(), |
| 210 | s.getVendor().string(), |
| 211 | s.getHandle(), |
Mathias Agopian | 24d7235 | 2010-11-05 19:12:58 -0700 | [diff] [blame] | 212 | s.getMinDelay() ? (1000000.0f / s.getMinDelay()) : 0.0f, |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 213 | e.data[0], e.data[1], e.data[2]); |
| 214 | result.append(buffer); |
| 215 | } |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 216 | SensorFusion::getInstance().dump(result, buffer, SIZE); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 217 | SensorDevice::getInstance().dump(result, buffer, SIZE); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 218 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 219 | snprintf(buffer, SIZE, "%d active connections\n", |
| 220 | mActiveConnections.size()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 221 | result.append(buffer); |
| 222 | snprintf(buffer, SIZE, "Active sensors:\n"); |
| 223 | result.append(buffer); |
| 224 | for (size_t i=0 ; i<mActiveSensors.size() ; i++) { |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 225 | int handle = mActiveSensors.keyAt(i); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 226 | snprintf(buffer, SIZE, "%s (handle=0x%08x, connections=%d)\n", |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 227 | getSensorName(handle).string(), |
| 228 | handle, |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 229 | mActiveSensors.valueAt(i)->getNumConnections()); |
| 230 | result.append(buffer); |
| 231 | } |
| 232 | } |
| 233 | write(fd, result.string(), result.size()); |
| 234 | return NO_ERROR; |
| 235 | } |
| 236 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 237 | void SensorService::cleanupAutoDisabledSensor(const sp<SensorEventConnection>& connection, |
| 238 | sensors_event_t const* buffer, const int count) { |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 239 | SensorInterface* sensor; |
| 240 | status_t err = NO_ERROR; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 241 | for (int i=0 ; i<count ; i++) { |
| 242 | int handle = buffer[i].sensor; |
| 243 | if (getSensorType(handle) == SENSOR_TYPE_SIGNIFICANT_MOTION) { |
| 244 | if (connection->hasSensor(handle)) { |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 245 | sensor = mSensorMap.valueFor(handle); |
| 246 | err = sensor ?sensor->resetStateWithoutActuatingHardware(connection.get(), handle) |
| 247 | : status_t(BAD_VALUE); |
| 248 | if (err != NO_ERROR) { |
| 249 | ALOGE("Sensor Inteface: Resetting state failed with err: %d", err); |
| 250 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 251 | cleanupWithoutDisable(connection, handle); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 257 | bool SensorService::threadLoop() |
| 258 | { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 259 | ALOGD("nuSensorService thread starting..."); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 260 | |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 261 | const size_t numEventMax = 16; |
Mathias Agopian | 8dd4fe8 | 2012-05-30 18:08:30 -0700 | [diff] [blame] | 262 | const size_t minBufferSize = numEventMax + numEventMax * mVirtualSensorList.size(); |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 263 | sensors_event_t buffer[minBufferSize]; |
| 264 | sensors_event_t scratch[minBufferSize]; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 265 | SensorDevice& device(SensorDevice::getInstance()); |
| 266 | const size_t vcount = mVirtualSensorList.size(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 267 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 268 | ssize_t count; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 269 | bool wakeLockAcquired = false; |
| 270 | const int halVersion = device.getHalDeviceVersion(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 271 | do { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 272 | count = device.poll(buffer, numEventMax); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 273 | if (count<0) { |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 274 | ALOGE("sensor poll failed (%s)", strerror(-count)); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 275 | break; |
| 276 | } |
| 277 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 278 | // Poll has returned. Hold a wakelock. |
| 279 | // Todo(): add a flag to the sensors definitions to indicate |
| 280 | // the sensors which can wake up the AP |
| 281 | for (int i = 0; i < count; i++) { |
| 282 | if (getSensorType(buffer[i].sensor) == SENSOR_TYPE_SIGNIFICANT_MOTION) { |
| 283 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME); |
| 284 | wakeLockAcquired = true; |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 289 | recordLastValue(buffer, count); |
| 290 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 291 | // handle virtual sensors |
| 292 | if (count && vcount) { |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 293 | sensors_event_t const * const event = buffer; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 294 | const DefaultKeyedVector<int, SensorInterface*> virtualSensors( |
| 295 | getActiveVirtualSensors()); |
| 296 | const size_t activeVirtualSensorCount = virtualSensors.size(); |
| 297 | if (activeVirtualSensorCount) { |
| 298 | size_t k = 0; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 299 | SensorFusion& fusion(SensorFusion::getInstance()); |
| 300 | if (fusion.isEnabled()) { |
| 301 | for (size_t i=0 ; i<size_t(count) ; i++) { |
| 302 | fusion.process(event[i]); |
| 303 | } |
| 304 | } |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 305 | for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 306 | for (size_t j=0 ; j<activeVirtualSensorCount ; j++) { |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 307 | if (count + k >= minBufferSize) { |
| 308 | ALOGE("buffer too small to hold all events: " |
| 309 | "count=%u, k=%u, size=%u", |
| 310 | count, k, minBufferSize); |
| 311 | break; |
| 312 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 313 | sensors_event_t out; |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 314 | SensorInterface* si = virtualSensors.valueAt(j); |
| 315 | if (si->process(&out, event[i])) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 316 | buffer[count + k] = out; |
| 317 | k++; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | if (k) { |
| 322 | // record the last synthesized values |
| 323 | recordLastValue(&buffer[count], k); |
| 324 | count += k; |
| 325 | // sort the buffer by time-stamps |
| 326 | sortEventBuffer(buffer, count); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 331 | // handle backward compatibility for RotationVector sensor |
| 332 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) { |
| 333 | for (int i = 0; i < count; i++) { |
| 334 | if (getSensorType(buffer[i].sensor) == SENSOR_TYPE_ROTATION_VECTOR) { |
| 335 | // All the 4 components of the quaternion should be available |
| 336 | // No heading accuracy. Set it to -1 |
| 337 | buffer[i].data[4] = -1; |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 342 | // send our events to clients... |
| 343 | const SortedVector< wp<SensorEventConnection> > activeConnections( |
| 344 | getActiveConnections()); |
| 345 | size_t numConnections = activeConnections.size(); |
| 346 | for (size_t i=0 ; i<numConnections ; i++) { |
| 347 | sp<SensorEventConnection> connection( |
| 348 | activeConnections[i].promote()); |
| 349 | if (connection != 0) { |
| 350 | connection->sendEvents(buffer, count, scratch); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 351 | // Some sensors need to be auto disabled after the trigger |
| 352 | cleanupAutoDisabledSensor(connection, buffer, count); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 353 | } |
| 354 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 355 | |
| 356 | // We have read the data, upper layers should hold the wakelock. |
| 357 | if (wakeLockAcquired) release_wake_lock(WAKE_LOCK_NAME); |
| 358 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 359 | } while (count >= 0 || Thread::exitPending()); |
| 360 | |
Steve Block | 3c20fbe | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 361 | ALOGW("Exiting SensorService::threadLoop => aborting..."); |
Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 362 | abort(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 363 | return false; |
| 364 | } |
| 365 | |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 366 | void SensorService::recordLastValue( |
| 367 | sensors_event_t const * buffer, size_t count) |
| 368 | { |
| 369 | Mutex::Autolock _l(mLock); |
| 370 | |
| 371 | // record the last event for each sensor |
| 372 | int32_t prev = buffer[0].sensor; |
| 373 | for (size_t i=1 ; i<count ; i++) { |
| 374 | // record the last event of each sensor type in this buffer |
| 375 | int32_t curr = buffer[i].sensor; |
| 376 | if (curr != prev) { |
| 377 | mLastEventSeen.editValueFor(prev) = buffer[i-1]; |
| 378 | prev = curr; |
| 379 | } |
| 380 | } |
| 381 | mLastEventSeen.editValueFor(prev) = buffer[count-1]; |
| 382 | } |
| 383 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 384 | void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count) |
| 385 | { |
| 386 | struct compar { |
| 387 | static int cmp(void const* lhs, void const* rhs) { |
| 388 | sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs); |
| 389 | sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs); |
Mathias Agopian | a5c106a | 2012-04-19 18:18:24 -0700 | [diff] [blame] | 390 | return l->timestamp - r->timestamp; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 391 | } |
| 392 | }; |
| 393 | qsort(buffer, count, sizeof(sensors_event_t), compar::cmp); |
| 394 | } |
| 395 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 396 | SortedVector< wp<SensorService::SensorEventConnection> > |
| 397 | SensorService::getActiveConnections() const |
| 398 | { |
| 399 | Mutex::Autolock _l(mLock); |
| 400 | return mActiveConnections; |
| 401 | } |
| 402 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 403 | DefaultKeyedVector<int, SensorInterface*> |
| 404 | SensorService::getActiveVirtualSensors() const |
| 405 | { |
| 406 | Mutex::Autolock _l(mLock); |
| 407 | return mActiveVirtualSensors; |
| 408 | } |
| 409 | |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 410 | String8 SensorService::getSensorName(int handle) const { |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 411 | size_t count = mUserSensorList.size(); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 412 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 413 | const Sensor& sensor(mUserSensorList[i]); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 414 | if (sensor.getHandle() == handle) { |
| 415 | return sensor.getName(); |
| 416 | } |
| 417 | } |
| 418 | String8 result("unknown"); |
| 419 | return result; |
| 420 | } |
| 421 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 422 | int SensorService::getSensorType(int handle) const { |
| 423 | size_t count = mUserSensorList.size(); |
| 424 | for (size_t i=0 ; i<count ; i++) { |
| 425 | const Sensor& sensor(mUserSensorList[i]); |
| 426 | if (sensor.getHandle() == handle) { |
| 427 | return sensor.getType(); |
| 428 | } |
| 429 | } |
| 430 | return -1; |
| 431 | } |
| 432 | |
| 433 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 434 | Vector<Sensor> SensorService::getSensorList() |
| 435 | { |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 436 | char value[PROPERTY_VALUE_MAX]; |
| 437 | property_get("debug.sensors", value, "0"); |
| 438 | if (atoi(value)) { |
| 439 | return mUserSensorListDebug; |
| 440 | } |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 441 | return mUserSensorList; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | sp<ISensorEventConnection> SensorService::createSensorEventConnection() |
| 445 | { |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 446 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 447 | sp<SensorEventConnection> result(new SensorEventConnection(this, uid)); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 448 | return result; |
| 449 | } |
| 450 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 451 | void SensorService::cleanupConnection(SensorEventConnection* c) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 452 | { |
| 453 | Mutex::Autolock _l(mLock); |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 454 | const wp<SensorEventConnection> connection(c); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 455 | size_t size = mActiveSensors.size(); |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 456 | ALOGD_IF(DEBUG_CONNECTIONS, "%d active sensors", size); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 457 | for (size_t i=0 ; i<size ; ) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 458 | int handle = mActiveSensors.keyAt(i); |
| 459 | if (c->hasSensor(handle)) { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 460 | ALOGD_IF(DEBUG_CONNECTIONS, "%i: disabling handle=0x%08x", i, handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 461 | SensorInterface* sensor = mSensorMap.valueFor( handle ); |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 462 | ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 463 | if (sensor) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 464 | sensor->activate(c, false); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 465 | } |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 466 | } |
| 467 | SensorRecord* rec = mActiveSensors.valueAt(i); |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 468 | ALOGE_IF(!rec, "mActiveSensors[%d] is null (handle=0x%08x)!", i, handle); |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 469 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 470 | "removing connection %p for sensor[%d].handle=0x%08x", |
| 471 | c, i, handle); |
| 472 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 473 | if (rec && rec->removeConnection(connection)) { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 474 | ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection"); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 475 | mActiveSensors.removeItemsAt(i, 1); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 476 | mActiveVirtualSensors.removeItem(handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 477 | delete rec; |
| 478 | size--; |
| 479 | } else { |
| 480 | i++; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 481 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 482 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 483 | mActiveConnections.remove(connection); |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 484 | BatteryService::cleanup(c->getUid()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | status_t SensorService::enable(const sp<SensorEventConnection>& connection, |
| 488 | int handle) |
| 489 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 490 | if (mInitCheck != NO_ERROR) |
| 491 | return mInitCheck; |
| 492 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 493 | Mutex::Autolock _l(mLock); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 494 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 495 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
| 496 | if (rec == 0) { |
| 497 | rec = new SensorRecord(connection); |
| 498 | mActiveSensors.add(handle, rec); |
| 499 | if (sensor->isVirtual()) { |
| 500 | mActiveVirtualSensors.add(handle, sensor); |
| 501 | } |
| 502 | } else { |
| 503 | if (rec->addConnection(connection)) { |
| 504 | // this sensor is already activated, but we are adding a |
| 505 | // connection that uses it. Immediately send down the last |
| 506 | // known value of the requested sensor if it's not a |
| 507 | // "continuous" sensor. |
| 508 | if (sensor->getSensor().getMinDelay() == 0) { |
| 509 | sensors_event_t scratch; |
| 510 | sensors_event_t& event(mLastEventSeen.editValueFor(handle)); |
| 511 | if (event.version == sizeof(sensors_event_t)) { |
| 512 | connection->sendEvents(&event, 1); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | if (connection->addSensor(handle)) { |
| 519 | BatteryService::enableSensor(connection->getUid(), handle); |
| 520 | // the sensor was added (which means it wasn't already there) |
| 521 | // so, see if this connection becomes active |
| 522 | if (mActiveConnections.indexOf(connection) < 0) { |
| 523 | mActiveConnections.add(connection); |
| 524 | } |
| 525 | } else { |
| 526 | ALOGW("sensor %08x already enabled in connection %p (ignoring)", |
| 527 | handle, connection.get()); |
| 528 | } |
| 529 | |
| 530 | |
| 531 | // we are setup, now enable the sensor. |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 532 | status_t err = sensor ? sensor->activate(connection.get(), true) : status_t(BAD_VALUE); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 533 | |
| 534 | if (err != NO_ERROR) { |
Jaikumar Ganesh | 4c01b1a | 2013-04-16 15:52:23 -0700 | [diff] [blame] | 535 | // enable has failed, reset state in SensorDevice. |
| 536 | status_t resetErr = sensor ? sensor->resetStateWithoutActuatingHardware(connection.get(), |
| 537 | handle) : status_t(BAD_VALUE); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 538 | // enable has failed, reset our state. |
| 539 | cleanupWithoutDisable(connection, handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 540 | } |
| 541 | return err; |
| 542 | } |
| 543 | |
| 544 | status_t SensorService::disable(const sp<SensorEventConnection>& connection, |
| 545 | int handle) |
| 546 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 547 | if (mInitCheck != NO_ERROR) |
| 548 | return mInitCheck; |
| 549 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 550 | status_t err = cleanupWithoutDisable(connection, handle); |
| 551 | if (err == NO_ERROR) { |
| 552 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 553 | err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE); |
| 554 | } |
| 555 | return err; |
| 556 | } |
| 557 | |
| 558 | status_t SensorService::cleanupWithoutDisable(const sp<SensorEventConnection>& connection, |
| 559 | int handle) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 560 | Mutex::Autolock _l(mLock); |
| 561 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 562 | if (rec) { |
| 563 | // see if this connection becomes inactive |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 564 | if (connection->removeSensor(handle)) { |
| 565 | BatteryService::disableSensor(connection->getUid(), handle); |
| 566 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 567 | if (connection->hasAnySensor() == false) { |
| 568 | mActiveConnections.remove(connection); |
| 569 | } |
| 570 | // see if this sensor becomes inactive |
| 571 | if (rec->removeConnection(connection)) { |
| 572 | mActiveSensors.removeItem(handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 573 | mActiveVirtualSensors.removeItem(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 574 | delete rec; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 575 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 576 | return NO_ERROR; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 577 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 578 | return BAD_VALUE; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 581 | status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection, |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 582 | int handle, nsecs_t ns) |
| 583 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 584 | if (mInitCheck != NO_ERROR) |
| 585 | return mInitCheck; |
| 586 | |
Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 587 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 588 | if (!sensor) |
| 589 | return BAD_VALUE; |
| 590 | |
Mathias Agopian | 1cd7000 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 591 | if (ns < 0) |
| 592 | return BAD_VALUE; |
| 593 | |
Mathias Agopian | 62569ec | 2011-11-07 21:21:47 -0800 | [diff] [blame] | 594 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 595 | if (ns < minDelayNs) { |
| 596 | ns = minDelayNs; |
Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 599 | if (ns < MINIMUM_EVENTS_PERIOD) |
| 600 | ns = MINIMUM_EVENTS_PERIOD; |
Mathias Agopian | 1cd7000 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 601 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 602 | return sensor->setDelay(connection.get(), handle, ns); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | // --------------------------------------------------------------------------- |
| 606 | |
| 607 | SensorService::SensorRecord::SensorRecord( |
| 608 | const sp<SensorEventConnection>& connection) |
| 609 | { |
| 610 | mConnections.add(connection); |
| 611 | } |
| 612 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 613 | bool SensorService::SensorRecord::addConnection( |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 614 | const sp<SensorEventConnection>& connection) |
| 615 | { |
| 616 | if (mConnections.indexOf(connection) < 0) { |
| 617 | mConnections.add(connection); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 618 | return true; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 619 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 620 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | bool SensorService::SensorRecord::removeConnection( |
| 624 | const wp<SensorEventConnection>& connection) |
| 625 | { |
| 626 | ssize_t index = mConnections.indexOf(connection); |
| 627 | if (index >= 0) { |
| 628 | mConnections.removeItemsAt(index, 1); |
| 629 | } |
| 630 | return mConnections.size() ? false : true; |
| 631 | } |
| 632 | |
| 633 | // --------------------------------------------------------------------------- |
| 634 | |
| 635 | SensorService::SensorEventConnection::SensorEventConnection( |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 636 | const sp<SensorService>& service, uid_t uid) |
| 637 | : mService(service), mChannel(new BitTube()), mUid(uid) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 638 | { |
| 639 | } |
| 640 | |
| 641 | SensorService::SensorEventConnection::~SensorEventConnection() |
| 642 | { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 643 | ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 644 | mService->cleanupConnection(this); |
| 645 | } |
| 646 | |
| 647 | void SensorService::SensorEventConnection::onFirstRef() |
| 648 | { |
| 649 | } |
| 650 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 651 | bool SensorService::SensorEventConnection::addSensor(int32_t handle) { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 652 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | a5b8e8b | 2012-09-18 17:18:54 -0700 | [diff] [blame] | 653 | if (mSensorInfo.indexOf(handle) < 0) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 654 | mSensorInfo.add(handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 655 | return true; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 656 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 657 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 660 | bool SensorService::SensorEventConnection::removeSensor(int32_t handle) { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 661 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 662 | if (mSensorInfo.remove(handle) >= 0) { |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 663 | return true; |
| 664 | } |
| 665 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 669 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 670 | return mSensorInfo.indexOf(handle) >= 0; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | bool SensorService::SensorEventConnection::hasAnySensor() const { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 674 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 675 | return mSensorInfo.size() ? true : false; |
| 676 | } |
| 677 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 678 | status_t SensorService::SensorEventConnection::sendEvents( |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 679 | sensors_event_t const* buffer, size_t numEvents, |
| 680 | sensors_event_t* scratch) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 681 | { |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 682 | // filter out events not for this connection |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 683 | size_t count = 0; |
| 684 | if (scratch) { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 685 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 686 | size_t i=0; |
| 687 | while (i<numEvents) { |
| 688 | const int32_t curr = buffer[i].sensor; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 689 | if (mSensorInfo.indexOf(curr) >= 0) { |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 690 | do { |
| 691 | scratch[count++] = buffer[i++]; |
| 692 | } while ((i<numEvents) && (buffer[i].sensor == curr)); |
| 693 | } else { |
| 694 | i++; |
| 695 | } |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 696 | } |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 697 | } else { |
| 698 | scratch = const_cast<sensors_event_t *>(buffer); |
| 699 | count = numEvents; |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 700 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 701 | |
Mathias Agopian | 907103b | 2012-04-02 18:38:02 -0700 | [diff] [blame] | 702 | // NOTE: ASensorEvent and sensors_event_t are the same type |
| 703 | ssize_t size = SensorEventQueue::write(mChannel, |
| 704 | reinterpret_cast<ASensorEvent const*>(scratch), count); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 705 | if (size == -EAGAIN) { |
| 706 | // the destination doesn't accept events anymore, it's probably |
| 707 | // full. For now, we just drop the events on the floor. |
Steve Block | 3c20fbe | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 708 | //ALOGW("dropping %d events on the floor", count); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 709 | return size; |
| 710 | } |
| 711 | |
Jeff Brown | 1e0b1e8 | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 712 | return size < 0 ? status_t(size) : status_t(NO_ERROR); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Mathias Agopian | b398927 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 715 | sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 716 | { |
| 717 | return mChannel; |
| 718 | } |
| 719 | |
| 720 | status_t SensorService::SensorEventConnection::enableDisable( |
| 721 | int handle, bool enabled) |
| 722 | { |
| 723 | status_t err; |
| 724 | if (enabled) { |
| 725 | err = mService->enable(this, handle); |
| 726 | } else { |
| 727 | err = mService->disable(this, handle); |
| 728 | } |
| 729 | return err; |
| 730 | } |
| 731 | |
| 732 | status_t SensorService::SensorEventConnection::setEventRate( |
| 733 | int handle, nsecs_t ns) |
| 734 | { |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 735 | return mService->setEventRate(this, handle, ns); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | // --------------------------------------------------------------------------- |
| 739 | }; // namespace android |
| 740 | |