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