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 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 17 | #include <inttypes.h> |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 18 | #include <math.h> |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 19 | #include <stdint.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 21 | #include <sys/socket.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 22 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 23 | #include <cutils/properties.h> |
| 24 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 25 | #include <utils/SortedVector.h> |
| 26 | #include <utils/KeyedVector.h> |
| 27 | #include <utils/threads.h> |
| 28 | #include <utils/Atomic.h> |
| 29 | #include <utils/Errors.h> |
| 30 | #include <utils/RefBase.h> |
Mathias Agopian | 451beee | 2010-07-19 15:03:55 -0700 | [diff] [blame] | 31 | #include <utils/Singleton.h> |
Mathias Agopian | c4a930d | 2010-07-22 22:19:43 -0700 | [diff] [blame] | 32 | #include <utils/String16.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 33 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 34 | #include <binder/AppOpsManager.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 35 | #include <binder/BinderService.h> |
Mathias Agopian | 451beee | 2010-07-19 15:03:55 -0700 | [diff] [blame] | 36 | #include <binder/IServiceManager.h> |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 37 | #include <binder/PermissionCache.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 38 | |
| 39 | #include <gui/ISensorServer.h> |
| 40 | #include <gui/ISensorEventConnection.h> |
Mathias Agopian | 907103b | 2012-04-02 18:38:02 -0700 | [diff] [blame] | 41 | #include <gui/SensorEventQueue.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 42 | |
| 43 | #include <hardware/sensors.h> |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 44 | #include <hardware_legacy/power.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 45 | |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 46 | #include "BatteryService.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 47 | #include "CorrectedGyroSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 48 | #include "GravitySensor.h" |
| 49 | #include "LinearAccelerationSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 50 | #include "OrientationSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 51 | #include "RotationVectorSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 52 | #include "SensorFusion.h" |
| 53 | #include "SensorService.h" |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 54 | |
| 55 | namespace android { |
| 56 | // --------------------------------------------------------------------------- |
| 57 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 58 | /* |
| 59 | * Notes: |
| 60 | * |
| 61 | * - what about a gyro-corrected magnetic-field sensor? |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 62 | * - run mag sensor from time to time to force calibration |
| 63 | * - gravity sensor length is wrong (=> drift in linear-acc sensor) |
| 64 | * |
| 65 | */ |
| 66 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 67 | const char* SensorService::WAKE_LOCK_NAME = "SensorService"; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 68 | // Permissions. |
Aravind Akella | a8814ce | 2015-06-10 15:15:57 -0700 | [diff] [blame] | 69 | static const String16 sDataInjectionPermission("android.permission.LOCATION_HARDWARE"); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 70 | static const String16 sDump("android.permission.DUMP"); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 71 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 72 | SensorService::SensorService() |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 73 | : mInitCheck(NO_INIT), mSocketBufferSize(SOCKET_BUFFER_SIZE_NON_BATCHED), |
| 74 | mWakeLockAcquired(false) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 75 | { |
| 76 | } |
| 77 | |
| 78 | void SensorService::onFirstRef() |
| 79 | { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 80 | ALOGD("nuSensorService starting..."); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 81 | SensorDevice& dev(SensorDevice::getInstance()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 82 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 83 | if (dev.initCheck() == NO_ERROR) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 84 | sensor_t const* list; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 85 | ssize_t count = dev.getSensorList(&list); |
| 86 | if (count > 0) { |
| 87 | ssize_t orientationIndex = -1; |
| 88 | bool hasGyro = false; |
| 89 | uint32_t virtualSensorsNeeds = |
| 90 | (1<<SENSOR_TYPE_GRAVITY) | |
| 91 | (1<<SENSOR_TYPE_LINEAR_ACCELERATION) | |
| 92 | (1<<SENSOR_TYPE_ROTATION_VECTOR); |
| 93 | |
| 94 | mLastEventSeen.setCapacity(count); |
| 95 | for (ssize_t i=0 ; i<count ; i++) { |
| 96 | registerSensor( new HardwareSensor(list[i]) ); |
| 97 | switch (list[i].type) { |
| 98 | case SENSOR_TYPE_ORIENTATION: |
| 99 | orientationIndex = i; |
| 100 | break; |
| 101 | case SENSOR_TYPE_GYROSCOPE: |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 102 | case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 103 | hasGyro = true; |
| 104 | break; |
| 105 | case SENSOR_TYPE_GRAVITY: |
| 106 | case SENSOR_TYPE_LINEAR_ACCELERATION: |
| 107 | case SENSOR_TYPE_ROTATION_VECTOR: |
| 108 | virtualSensorsNeeds &= ~(1<<list[i].type); |
| 109 | break; |
| 110 | } |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 111 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 112 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 113 | // it's safe to instantiate the SensorFusion object here |
| 114 | // (it wants to be instantiated after h/w sensors have been |
| 115 | // registered) |
| 116 | const SensorFusion& fusion(SensorFusion::getInstance()); |
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 | // build the sensor list returned to users |
| 119 | mUserSensorList = mSensorList; |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 120 | |
| 121 | if (hasGyro) { |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 122 | Sensor aSensor; |
| 123 | |
| 124 | // Add Android virtual sensors if they're not already |
| 125 | // available in the HAL |
| 126 | |
| 127 | aSensor = registerVirtualSensor( new RotationVectorSensor() ); |
| 128 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) { |
| 129 | mUserSensorList.add(aSensor); |
| 130 | } |
| 131 | |
| 132 | aSensor = registerVirtualSensor( new GravitySensor(list, count) ); |
| 133 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) { |
| 134 | mUserSensorList.add(aSensor); |
| 135 | } |
| 136 | |
| 137 | aSensor = registerVirtualSensor( new LinearAccelerationSensor(list, count) ); |
| 138 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) { |
| 139 | mUserSensorList.add(aSensor); |
| 140 | } |
| 141 | |
| 142 | aSensor = registerVirtualSensor( new OrientationSensor() ); |
| 143 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) { |
| 144 | // if we are doing our own rotation-vector, also add |
| 145 | // the orientation sensor and remove the HAL provided one. |
| 146 | mUserSensorList.replaceAt(aSensor, orientationIndex); |
| 147 | } |
| 148 | |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 149 | // virtual debugging sensors are not added to mUserSensorList |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 150 | registerVirtualSensor( new CorrectedGyroSensor(list, count) ); |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 151 | registerVirtualSensor( new GyroDriftSensor() ); |
| 152 | } |
| 153 | |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 154 | // debugging sensor list |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 155 | mUserSensorListDebug = mSensorList; |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 156 | |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 157 | // Check if the device really supports batching by looking at the FIFO event |
| 158 | // counts for each sensor. |
| 159 | bool batchingSupported = false; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 160 | for (size_t i = 0; i < mSensorList.size(); ++i) { |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 161 | if (mSensorList[i].getFifoMaxEventCount() > 0) { |
| 162 | batchingSupported = true; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (batchingSupported) { |
| 168 | // Increase socket buffer size to a max of 100 KB for batching capabilities. |
| 169 | mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED; |
| 170 | } else { |
| 171 | mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED; |
| 172 | } |
| 173 | |
| 174 | // Compare the socketBufferSize value against the system limits and limit |
| 175 | // it to maxSystemSocketBufferSize if necessary. |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 176 | FILE *fp = fopen("/proc/sys/net/core/wmem_max", "r"); |
| 177 | char line[128]; |
| 178 | if (fp != NULL && fgets(line, sizeof(line), fp) != NULL) { |
| 179 | line[sizeof(line) - 1] = '\0'; |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 180 | size_t maxSystemSocketBufferSize; |
| 181 | sscanf(line, "%zu", &maxSystemSocketBufferSize); |
| 182 | if (mSocketBufferSize > maxSystemSocketBufferSize) { |
| 183 | mSocketBufferSize = maxSystemSocketBufferSize; |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 186 | if (fp) { |
| 187 | fclose(fp); |
| 188 | } |
| 189 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 190 | mWakeLockAcquired = false; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 191 | mLooper = new Looper(false); |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 192 | const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT; |
| 193 | mSensorEventBuffer = new sensors_event_t[minBufferSize]; |
| 194 | mSensorEventScratch = new sensors_event_t[minBufferSize]; |
| 195 | mMapFlushEventsToConnections = new SensorEventConnection const * [minBufferSize]; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 196 | mCurrentOperatingMode = NORMAL; |
Aravind Akella | 7830ef3 | 2014-10-07 14:13:12 -0700 | [diff] [blame] | 197 | |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 198 | mNextSensorRegIndex = 0; |
| 199 | for (int i = 0; i < SENSOR_REGISTRATIONS_BUF_SIZE; ++i) { |
| 200 | mLastNSensorRegistrations.push(); |
| 201 | } |
| 202 | |
| 203 | mInitCheck = NO_ERROR; |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 204 | mAckReceiver = new SensorEventAckReceiver(this); |
| 205 | mAckReceiver->run("SensorEventAckReceiver", PRIORITY_URGENT_DISPLAY); |
Aravind Akella | 7830ef3 | 2014-10-07 14:13:12 -0700 | [diff] [blame] | 206 | run("SensorService", PRIORITY_URGENT_DISPLAY); |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 207 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 208 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 211 | Sensor SensorService::registerSensor(SensorInterface* s) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 212 | { |
| 213 | sensors_event_t event; |
| 214 | memset(&event, 0, sizeof(event)); |
| 215 | |
| 216 | const Sensor sensor(s->getSensor()); |
| 217 | // add to the sensor list (returned to clients) |
| 218 | mSensorList.add(sensor); |
| 219 | // add to our handle->SensorInterface mapping |
| 220 | mSensorMap.add(sensor.getHandle(), s); |
| 221 | // create an entry in the mLastEventSeen array |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 222 | mLastEventSeen.add(sensor.getHandle(), NULL); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 223 | |
| 224 | return sensor; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 227 | Sensor SensorService::registerVirtualSensor(SensorInterface* s) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 228 | { |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 229 | Sensor sensor = registerSensor(s); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 230 | mVirtualSensorList.add( s ); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 231 | return sensor; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 234 | SensorService::~SensorService() |
| 235 | { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 236 | for (size_t i=0 ; i<mSensorMap.size() ; i++) |
| 237 | delete mSensorMap.valueAt(i); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 240 | status_t SensorService::dump(int fd, const Vector<String16>& args) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 241 | { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 242 | String8 result; |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 243 | if (!PermissionCache::checkCallingPermission(sDump)) { |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 244 | result.appendFormat("Permission Denial: " |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 245 | "can't dump SensorService from pid=%d, uid=%d\n", |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 246 | IPCThreadState::self()->getCallingPid(), |
| 247 | IPCThreadState::self()->getCallingUid()); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 248 | } else { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 249 | if (args.size() > 1) { |
| 250 | return INVALID_OPERATION; |
| 251 | } |
| 252 | Mutex::Autolock _l(mLock); |
| 253 | SensorDevice& dev(SensorDevice::getInstance()); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 254 | if (args.size() == 1 && args[0] == String16("restrict")) { |
| 255 | // If already in restricted mode. Ignore. |
| 256 | if (mCurrentOperatingMode == RESTRICTED) { |
| 257 | return status_t(NO_ERROR); |
| 258 | } |
| 259 | // If in any mode other than normal, ignore. |
| 260 | if (mCurrentOperatingMode != NORMAL) { |
| 261 | return INVALID_OPERATION; |
| 262 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 263 | mCurrentOperatingMode = RESTRICTED; |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 264 | dev.disableAllSensors(); |
| 265 | // Clear all pending flush connections for all active sensors. If one of the active |
| 266 | // connections has called flush() and the underlying sensor has been disabled before a |
| 267 | // flush complete event is returned, we need to remove the connection from this queue. |
| 268 | for (size_t i=0 ; i< mActiveSensors.size(); ++i) { |
| 269 | mActiveSensors.valueAt(i)->clearAllPendingFlushConnections(); |
| 270 | } |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 271 | return status_t(NO_ERROR); |
| 272 | } else if (args.size() == 1 && args[0] == String16("enable")) { |
| 273 | // If currently in restricted mode, reset back to NORMAL mode else ignore. |
| 274 | if (mCurrentOperatingMode == RESTRICTED) { |
| 275 | mCurrentOperatingMode = NORMAL; |
| 276 | dev.enableAllSensors(); |
| 277 | } |
| 278 | return status_t(NO_ERROR); |
Aravind Akella | ee155ca | 2015-06-24 08:31:32 -0700 | [diff] [blame] | 279 | } else if (mSensorList.size() == 0) { |
| 280 | result.append("No Sensors on the device\n"); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 281 | } else { |
| 282 | // Default dump the sensor list and debugging information. |
| 283 | result.append("Sensor List:\n"); |
| 284 | for (size_t i=0 ; i<mSensorList.size() ; i++) { |
| 285 | const Sensor& s(mSensorList[i]); |
| 286 | result.appendFormat( |
| 287 | "%-15s| %-10s| version=%d |%-20s| 0x%08x | \"%s\" | type=%d |", |
| 288 | s.getName().string(), |
| 289 | s.getVendor().string(), |
| 290 | s.getVersion(), |
| 291 | s.getStringType().string(), |
| 292 | s.getHandle(), |
| 293 | s.getRequiredPermission().string(), |
| 294 | s.getType()); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 295 | |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 296 | const int reportingMode = s.getReportingMode(); |
| 297 | if (reportingMode == AREPORTING_MODE_CONTINUOUS) { |
| 298 | result.append(" continuous | "); |
| 299 | } else if (reportingMode == AREPORTING_MODE_ON_CHANGE) { |
| 300 | result.append(" on-change | "); |
| 301 | } else if (reportingMode == AREPORTING_MODE_ONE_SHOT) { |
| 302 | result.append(" one-shot | "); |
| 303 | } else { |
| 304 | result.append(" special-trigger | "); |
| 305 | } |
| 306 | |
| 307 | if (s.getMaxDelay() > 0) { |
| 308 | result.appendFormat("minRate=%.2fHz | ", 1e6f / s.getMaxDelay()); |
| 309 | } else { |
| 310 | result.appendFormat("maxDelay=%dus |", s.getMaxDelay()); |
| 311 | } |
| 312 | |
| 313 | if (s.getMinDelay() > 0) { |
| 314 | result.appendFormat("maxRate=%.2fHz | ", 1e6f / s.getMinDelay()); |
| 315 | } else { |
| 316 | result.appendFormat("minDelay=%dus |", s.getMinDelay()); |
| 317 | } |
| 318 | |
| 319 | if (s.getFifoMaxEventCount() > 0) { |
| 320 | result.appendFormat("FifoMax=%d events | ", |
| 321 | s.getFifoMaxEventCount()); |
| 322 | } else { |
| 323 | result.append("no batching | "); |
| 324 | } |
| 325 | |
| 326 | if (s.isWakeUpSensor()) { |
| 327 | result.appendFormat("wakeUp | "); |
| 328 | } else { |
| 329 | result.appendFormat("non-wakeUp | "); |
| 330 | } |
| 331 | |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 332 | int bufIndex = mLastEventSeen.indexOfKey(s.getHandle()); |
| 333 | if (bufIndex >= 0) { |
| 334 | const CircularBuffer* buf = mLastEventSeen.valueAt(bufIndex); |
| 335 | if (buf != NULL && s.getRequiredPermission().isEmpty()) { |
| 336 | buf->printBuffer(result); |
| 337 | } else { |
| 338 | result.append("last=<> \n"); |
| 339 | } |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 340 | } |
| 341 | result.append("\n"); |
| 342 | } |
| 343 | SensorFusion::getInstance().dump(result); |
| 344 | SensorDevice::getInstance().dump(result); |
| 345 | |
| 346 | result.append("Active sensors:\n"); |
| 347 | for (size_t i=0 ; i<mActiveSensors.size() ; i++) { |
| 348 | int handle = mActiveSensors.keyAt(i); |
| 349 | result.appendFormat("%s (handle=0x%08x, connections=%zu)\n", |
| 350 | getSensorName(handle).string(), |
| 351 | handle, |
| 352 | mActiveSensors.valueAt(i)->getNumConnections()); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 355 | result.appendFormat("Socket Buffer size = %d events\n", |
| 356 | mSocketBufferSize/sizeof(sensors_event_t)); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 357 | result.appendFormat("WakeLock Status: %s \n", mWakeLockAcquired ? "acquired" : |
| 358 | "not held"); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 359 | result.appendFormat("Mode :"); |
| 360 | switch(mCurrentOperatingMode) { |
| 361 | case NORMAL: |
| 362 | result.appendFormat(" NORMAL\n"); |
| 363 | break; |
| 364 | case RESTRICTED: |
| 365 | result.appendFormat(" RESTRICTED\n"); |
| 366 | break; |
| 367 | case DATA_INJECTION: |
| 368 | result.appendFormat(" DATA_INJECTION\n"); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 369 | } |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 370 | result.appendFormat("%zd active connections\n", mActiveConnections.size()); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 371 | |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 372 | for (size_t i=0 ; i < mActiveConnections.size() ; i++) { |
| 373 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
| 374 | if (connection != 0) { |
| 375 | result.appendFormat("Connection Number: %zu \n", i); |
| 376 | connection->dump(result); |
| 377 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 378 | } |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 379 | |
| 380 | result.appendFormat("Previous Registrations:\n"); |
| 381 | // Log in the reverse chronological order. |
| 382 | int currentIndex = (mNextSensorRegIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % |
| 383 | SENSOR_REGISTRATIONS_BUF_SIZE; |
| 384 | const int startIndex = currentIndex; |
| 385 | do { |
| 386 | const SensorRegistrationInfo& reg_info = mLastNSensorRegistrations[currentIndex]; |
| 387 | if (SensorRegistrationInfo::isSentinel(reg_info)) { |
| 388 | // Ignore sentinel, proceed to next item. |
| 389 | currentIndex = (currentIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % |
| 390 | SENSOR_REGISTRATIONS_BUF_SIZE; |
| 391 | continue; |
| 392 | } |
| 393 | if (reg_info.mActivated) { |
| 394 | result.appendFormat("%02d:%02d:%02d activated package=%s handle=0x%08x " |
| 395 | "samplingRate=%dus maxReportLatency=%dus\n", |
| 396 | reg_info.mHour, reg_info.mMin, reg_info.mSec, |
| 397 | reg_info.mPackageName.string(), reg_info.mSensorHandle, |
| 398 | reg_info.mSamplingRateUs, reg_info.mMaxReportLatencyUs); |
| 399 | } else { |
| 400 | result.appendFormat("%02d:%02d:%02d de-activated package=%s handle=0x%08x\n", |
| 401 | reg_info.mHour, reg_info.mMin, reg_info.mSec, |
| 402 | reg_info.mPackageName.string(), reg_info.mSensorHandle); |
| 403 | } |
| 404 | currentIndex = (currentIndex - 1 + SENSOR_REGISTRATIONS_BUF_SIZE) % |
| 405 | SENSOR_REGISTRATIONS_BUF_SIZE; |
| 406 | } while(startIndex != currentIndex); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 407 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 408 | } |
| 409 | write(fd, result.string(), result.size()); |
| 410 | return NO_ERROR; |
| 411 | } |
| 412 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 413 | void SensorService::cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection, |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 414 | sensors_event_t const* buffer, const int count) { |
| 415 | for (int i=0 ; i<count ; i++) { |
| 416 | int handle = buffer[i].sensor; |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 417 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { |
| 418 | handle = buffer[i].meta_data.sensor; |
| 419 | } |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 420 | if (connection->hasSensor(handle)) { |
| 421 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 422 | // If this buffer has an event from a one_shot sensor and this connection is registered |
| 423 | // for this particular one_shot sensor, try cleaning up the connection. |
| 424 | if (sensor != NULL && |
| 425 | sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 426 | sensor->autoDisable(connection.get(), handle); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 427 | cleanupWithoutDisableLocked(connection, handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 428 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 429 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 430 | } |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 431 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 434 | bool SensorService::threadLoop() |
| 435 | { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 436 | ALOGD("nuSensorService thread starting..."); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 437 | |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 438 | // each virtual sensor could generate an event per "real" event, that's why we need |
| 439 | // to size numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT. |
| 440 | // in practice, this is too aggressive, but guaranteed to be enough. |
| 441 | const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT; |
| 442 | const size_t numEventMax = minBufferSize / (1 + mVirtualSensorList.size()); |
| 443 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 444 | SensorDevice& device(SensorDevice::getInstance()); |
| 445 | const size_t vcount = mVirtualSensorList.size(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 446 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 447 | const int halVersion = device.getHalDeviceVersion(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 448 | do { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 449 | ssize_t count = device.poll(mSensorEventBuffer, numEventMax); |
| 450 | if (count < 0) { |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 451 | ALOGE("sensor poll failed (%s)", strerror(-count)); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 452 | break; |
| 453 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 454 | |
| 455 | // Reset sensors_event_t.flags to zero for all events in the buffer. |
| 456 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 457 | mSensorEventBuffer[i].flags = 0; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 458 | } |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 459 | |
| 460 | // Make a copy of the connection vector as some connections may be removed during the |
| 461 | // course of this loop (especially when one-shot sensor events are present in the |
| 462 | // sensor_event buffer). Promote all connections to StrongPointers before the lock is |
| 463 | // acquired. If the destructor of the sp gets called when the lock is acquired, it may |
| 464 | // result in a deadlock as ~SensorEventConnection() needs to acquire mLock again for |
| 465 | // cleanup. So copy all the strongPointers to a vector before the lock is acquired. |
| 466 | SortedVector< sp<SensorEventConnection> > activeConnections; |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 467 | populateActiveConnections(&activeConnections); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 468 | Mutex::Autolock _l(mLock); |
| 469 | // Poll has returned. Hold a wakelock if one of the events is from a wake up sensor. The |
| 470 | // rest of this loop is under a critical section protected by mLock. Acquiring a wakeLock, |
| 471 | // sending events to clients (incrementing SensorEventConnection::mWakeLockRefCount) should |
| 472 | // not be interleaved with decrementing SensorEventConnection::mWakeLockRefCount and |
| 473 | // releasing the wakelock. |
| 474 | bool bufferHasWakeUpEvent = false; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 475 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 476 | if (isWakeUpSensorEvent(mSensorEventBuffer[i])) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 477 | bufferHasWakeUpEvent = true; |
| 478 | break; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 482 | if (bufferHasWakeUpEvent && !mWakeLockAcquired) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 483 | setWakeLockAcquiredLocked(true); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 484 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 485 | recordLastValueLocked(mSensorEventBuffer, count); |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 486 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 487 | // handle virtual sensors |
| 488 | if (count && vcount) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 489 | sensors_event_t const * const event = mSensorEventBuffer; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 490 | const size_t activeVirtualSensorCount = mActiveVirtualSensors.size(); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 491 | if (activeVirtualSensorCount) { |
| 492 | size_t k = 0; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 493 | SensorFusion& fusion(SensorFusion::getInstance()); |
| 494 | if (fusion.isEnabled()) { |
| 495 | for (size_t i=0 ; i<size_t(count) ; i++) { |
| 496 | fusion.process(event[i]); |
| 497 | } |
| 498 | } |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 499 | for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 500 | for (size_t j=0 ; j<activeVirtualSensorCount ; j++) { |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 501 | if (count + k >= minBufferSize) { |
| 502 | ALOGE("buffer too small to hold all events: " |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 503 | "count=%zd, k=%zu, size=%zu", |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 504 | count, k, minBufferSize); |
| 505 | break; |
| 506 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 507 | sensors_event_t out; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 508 | SensorInterface* si = mActiveVirtualSensors.valueAt(j); |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 509 | if (si->process(&out, event[i])) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 510 | mSensorEventBuffer[count + k] = out; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 511 | k++; |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | if (k) { |
| 516 | // record the last synthesized values |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 517 | recordLastValueLocked(&mSensorEventBuffer[count], k); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 518 | count += k; |
| 519 | // sort the buffer by time-stamps |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 520 | sortEventBuffer(mSensorEventBuffer, count); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 525 | // handle backward compatibility for RotationVector sensor |
| 526 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) { |
| 527 | for (int i = 0; i < count; i++) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 528 | if (mSensorEventBuffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) { |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 529 | // All the 4 components of the quaternion should be available |
| 530 | // No heading accuracy. Set it to -1 |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 531 | mSensorEventBuffer[i].data[4] = -1; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | // Map flush_complete_events in the buffer to SensorEventConnections which called |
| 537 | // flush on the hardware sensor. mapFlushEventsToConnections[i] will be the |
| 538 | // SensorEventConnection mapped to the corresponding flush_complete_event in |
| 539 | // mSensorEventBuffer[i] if such a mapping exists (NULL otherwise). |
| 540 | for (int i = 0; i < count; ++i) { |
| 541 | mMapFlushEventsToConnections[i] = NULL; |
| 542 | if (mSensorEventBuffer[i].type == SENSOR_TYPE_META_DATA) { |
| 543 | const int sensor_handle = mSensorEventBuffer[i].meta_data.sensor; |
| 544 | SensorRecord* rec = mActiveSensors.valueFor(sensor_handle); |
| 545 | if (rec != NULL) { |
| 546 | mMapFlushEventsToConnections[i] = rec->getFirstPendingFlushConnection(); |
| 547 | rec->removeFirstPendingFlushConnection(); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 552 | // Send our events to clients. Check the state of wake lock for each client and release the |
| 553 | // lock if none of the clients need it. |
| 554 | bool needsWakeLock = false; |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 555 | size_t numConnections = activeConnections.size(); |
| 556 | for (size_t i=0 ; i < numConnections; ++i) { |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 557 | if (activeConnections[i] != 0) { |
| 558 | activeConnections[i]->sendEvents(mSensorEventBuffer, count, mSensorEventScratch, |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 559 | mMapFlushEventsToConnections); |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 560 | needsWakeLock |= activeConnections[i]->needsWakeLock(); |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 561 | // If the connection has one-shot sensors, it may be cleaned up after first trigger. |
| 562 | // Early check for one-shot sensors. |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 563 | if (activeConnections[i]->hasOneShotSensors()) { |
| 564 | cleanupAutoDisabledSensorLocked(activeConnections[i], mSensorEventBuffer, |
| 565 | count); |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 566 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 567 | } |
| 568 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 569 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 570 | if (mWakeLockAcquired && !needsWakeLock) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 571 | setWakeLockAcquiredLocked(false); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 572 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 573 | } while (!Thread::exitPending()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 574 | |
Steve Block | 3c20fbe | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 575 | ALOGW("Exiting SensorService::threadLoop => aborting..."); |
Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 576 | abort(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 577 | return false; |
| 578 | } |
| 579 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 580 | sp<Looper> SensorService::getLooper() const { |
| 581 | return mLooper; |
| 582 | } |
| 583 | |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 584 | void SensorService::resetAllWakeLockRefCounts() { |
| 585 | SortedVector< sp<SensorEventConnection> > activeConnections; |
| 586 | populateActiveConnections(&activeConnections); |
| 587 | { |
| 588 | Mutex::Autolock _l(mLock); |
| 589 | for (size_t i=0 ; i < activeConnections.size(); ++i) { |
| 590 | if (activeConnections[i] != 0) { |
| 591 | activeConnections[i]->resetWakeLockRefCount(); |
| 592 | } |
| 593 | } |
| 594 | setWakeLockAcquiredLocked(false); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | void SensorService::setWakeLockAcquiredLocked(bool acquire) { |
| 599 | if (acquire) { |
| 600 | if (!mWakeLockAcquired) { |
| 601 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME); |
| 602 | mWakeLockAcquired = true; |
| 603 | } |
| 604 | mLooper->wake(); |
| 605 | } else { |
| 606 | if (mWakeLockAcquired) { |
| 607 | release_wake_lock(WAKE_LOCK_NAME); |
| 608 | mWakeLockAcquired = false; |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 613 | bool SensorService::isWakeLockAcquired() { |
| 614 | Mutex::Autolock _l(mLock); |
| 615 | return mWakeLockAcquired; |
| 616 | } |
| 617 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 618 | bool SensorService::SensorEventAckReceiver::threadLoop() { |
| 619 | ALOGD("new thread SensorEventAckReceiver"); |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 620 | sp<Looper> looper = mService->getLooper(); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 621 | do { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 622 | bool wakeLockAcquired = mService->isWakeLockAcquired(); |
| 623 | int timeout = -1; |
| 624 | if (wakeLockAcquired) timeout = 5000; |
| 625 | int ret = looper->pollOnce(timeout); |
| 626 | if (ret == ALOOPER_POLL_TIMEOUT) { |
| 627 | mService->resetAllWakeLockRefCounts(); |
| 628 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 629 | } while(!Thread::exitPending()); |
| 630 | return false; |
| 631 | } |
| 632 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 633 | void SensorService::recordLastValueLocked( |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 634 | const sensors_event_t* buffer, size_t count) { |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 635 | for (size_t i = 0; i < count; i++) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 636 | if (buffer[i].type != SENSOR_TYPE_META_DATA) { |
| 637 | CircularBuffer* &circular_buf = mLastEventSeen.editValueFor(buffer[i].sensor); |
| 638 | if (circular_buf == NULL) { |
| 639 | circular_buf = new CircularBuffer(buffer[i].type); |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 640 | } |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 641 | circular_buf->addEvent(buffer[i]); |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 642 | } |
| 643 | } |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 644 | } |
| 645 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 646 | void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count) |
| 647 | { |
| 648 | struct compar { |
| 649 | static int cmp(void const* lhs, void const* rhs) { |
| 650 | sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs); |
| 651 | sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs); |
Mathias Agopian | a5c106a | 2012-04-19 18:18:24 -0700 | [diff] [blame] | 652 | return l->timestamp - r->timestamp; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 653 | } |
| 654 | }; |
| 655 | qsort(buffer, count, sizeof(sensors_event_t), compar::cmp); |
| 656 | } |
| 657 | |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 658 | String8 SensorService::getSensorName(int handle) const { |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 659 | size_t count = mUserSensorList.size(); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 660 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 661 | const Sensor& sensor(mUserSensorList[i]); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 662 | if (sensor.getHandle() == handle) { |
| 663 | return sensor.getName(); |
| 664 | } |
| 665 | } |
| 666 | String8 result("unknown"); |
| 667 | return result; |
| 668 | } |
| 669 | |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 670 | bool SensorService::isVirtualSensor(int handle) const { |
| 671 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 672 | return sensor->isVirtual(); |
| 673 | } |
| 674 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 675 | bool SensorService::isWakeUpSensorEvent(const sensors_event_t& event) const { |
Sean Wan | 7869e22 | 2014-07-14 17:07:33 -0700 | [diff] [blame] | 676 | int handle = event.sensor; |
| 677 | if (event.type == SENSOR_TYPE_META_DATA) { |
| 678 | handle = event.meta_data.sensor; |
| 679 | } |
| 680 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 681 | return sensor != NULL && sensor->getSensor().isWakeUpSensor(); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 684 | SensorService::SensorRecord * SensorService::getSensorRecord(int handle) { |
| 685 | return mActiveSensors.valueFor(handle); |
| 686 | } |
| 687 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 688 | Vector<Sensor> SensorService::getSensorList(const String16& opPackageName) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 689 | { |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 690 | char value[PROPERTY_VALUE_MAX]; |
| 691 | property_get("debug.sensors", value, "0"); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 692 | const Vector<Sensor>& initialSensorList = (atoi(value)) ? |
| 693 | mUserSensorListDebug : mUserSensorList; |
| 694 | Vector<Sensor> accessibleSensorList; |
| 695 | for (size_t i = 0; i < initialSensorList.size(); i++) { |
| 696 | Sensor sensor = initialSensorList[i]; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 697 | if (canAccessSensor(sensor, "getSensorList", opPackageName)) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 698 | accessibleSensorList.add(sensor); |
| 699 | } else { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 700 | ALOGI("Skipped sensor %s because it requires permission %s and app op %d", |
Bernhard Rosenkränzer | 5f61993 | 2014-11-17 21:06:20 +0100 | [diff] [blame] | 701 | sensor.getName().string(), |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 702 | sensor.getRequiredPermission().string(), |
| 703 | sensor.getRequiredAppOp()); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 704 | } |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 705 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 706 | return accessibleSensorList; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 709 | sp<ISensorEventConnection> SensorService::createSensorEventConnection(const String8& packageName, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 710 | int requestedMode, const String16& opPackageName) { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 711 | // Only 2 modes supported for a SensorEventConnection ... NORMAL and DATA_INJECTION. |
| 712 | if (requestedMode != NORMAL && requestedMode != DATA_INJECTION) { |
| 713 | return NULL; |
| 714 | } |
| 715 | // DATA_INJECTION mode needs to have the required permissions set. |
| 716 | if (requestedMode == DATA_INJECTION && !hasDataInjectionPermissions()) { |
| 717 | return NULL; |
| 718 | } |
| 719 | |
| 720 | Mutex::Autolock _l(mLock); |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 721 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 722 | sp<SensorEventConnection> result(new SensorEventConnection(this, uid, packageName, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 723 | requestedMode == DATA_INJECTION, opPackageName)); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 724 | if (requestedMode == DATA_INJECTION) { |
| 725 | if (mActiveConnections.indexOf(result) < 0) { |
| 726 | mActiveConnections.add(result); |
| 727 | } |
| 728 | // Add the associated file descriptor to the Looper for polling whenever there is data to |
| 729 | // be injected. |
| 730 | result->updateLooperRegistration(mLooper); |
| 731 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 732 | return result; |
| 733 | } |
| 734 | |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 735 | status_t SensorService::enableDataInjection(int requestedMode) { |
| 736 | if (!hasDataInjectionPermissions()) { |
| 737 | return INVALID_OPERATION; |
| 738 | } |
| 739 | Mutex::Autolock _l(mLock); |
| 740 | ALOGD_IF(DEBUG_CONNECTIONS, "SensorService::enableDataInjection %d", requestedMode); |
| 741 | SensorDevice& dev(SensorDevice::getInstance()); |
| 742 | status_t err(NO_ERROR); |
| 743 | if (requestedMode == DATA_INJECTION) { |
| 744 | if (mCurrentOperatingMode == NORMAL) { |
| 745 | dev.disableAllSensors(); |
| 746 | err = dev.setMode(requestedMode); |
| 747 | if (err == NO_ERROR) { |
| 748 | mCurrentOperatingMode = DATA_INJECTION; |
| 749 | } else { |
| 750 | // Re-enable sensors. |
| 751 | dev.enableAllSensors(); |
| 752 | } |
| 753 | } else if (mCurrentOperatingMode == DATA_INJECTION) { |
| 754 | // Already in DATA_INJECTION mode. Treat this as a no_op. |
| 755 | return NO_ERROR; |
| 756 | } else { |
| 757 | // Transition to data injection mode supported only from NORMAL mode. |
| 758 | return INVALID_OPERATION; |
| 759 | } |
| 760 | } else if (requestedMode == NORMAL && mCurrentOperatingMode != NORMAL) { |
| 761 | err = resetToNormalModeLocked(); |
| 762 | } |
| 763 | return err; |
| 764 | } |
| 765 | |
| 766 | status_t SensorService::resetToNormalMode() { |
| 767 | Mutex::Autolock _l(mLock); |
| 768 | return resetToNormalModeLocked(); |
| 769 | } |
| 770 | |
| 771 | status_t SensorService::resetToNormalModeLocked() { |
| 772 | SensorDevice& dev(SensorDevice::getInstance()); |
| 773 | dev.enableAllSensors(); |
| 774 | status_t err = dev.setMode(NORMAL); |
| 775 | mCurrentOperatingMode = NORMAL; |
| 776 | return err; |
| 777 | } |
| 778 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 779 | void SensorService::cleanupConnection(SensorEventConnection* c) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 780 | { |
| 781 | Mutex::Autolock _l(mLock); |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 782 | const wp<SensorEventConnection> connection(c); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 783 | size_t size = mActiveSensors.size(); |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 784 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu active sensors", size); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 785 | for (size_t i=0 ; i<size ; ) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 786 | int handle = mActiveSensors.keyAt(i); |
| 787 | if (c->hasSensor(handle)) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 788 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu: disabling handle=0x%08x", i, handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 789 | SensorInterface* sensor = mSensorMap.valueFor( handle ); |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 790 | ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 791 | if (sensor) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 792 | sensor->activate(c, false); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 793 | } |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 794 | c->removeSensor(handle); |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 795 | } |
| 796 | SensorRecord* rec = mActiveSensors.valueAt(i); |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 797 | ALOGE_IF(!rec, "mActiveSensors[%zu] is null (handle=0x%08x)!", i, handle); |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 798 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 799 | "removing connection %p for sensor[%zu].handle=0x%08x", |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 800 | c, i, handle); |
| 801 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 802 | if (rec && rec->removeConnection(connection)) { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 803 | ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection"); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 804 | mActiveSensors.removeItemsAt(i, 1); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 805 | mActiveVirtualSensors.removeItem(handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 806 | delete rec; |
| 807 | size--; |
| 808 | } else { |
| 809 | i++; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 810 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 811 | } |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 812 | c->updateLooperRegistration(mLooper); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 813 | mActiveConnections.remove(connection); |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 814 | BatteryService::cleanup(c->getUid()); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 815 | if (c->needsWakeLock()) { |
| 816 | checkWakeLockStateLocked(); |
| 817 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 820 | Sensor SensorService::getSensorFromHandle(int handle) const { |
| 821 | return mSensorMap.valueFor(handle)->getSensor(); |
| 822 | } |
| 823 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 824 | status_t SensorService::enable(const sp<SensorEventConnection>& connection, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 825 | int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags, |
| 826 | const String16& opPackageName) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 827 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 828 | if (mInitCheck != NO_ERROR) |
| 829 | return mInitCheck; |
| 830 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 831 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 832 | if (sensor == NULL) { |
| 833 | return BAD_VALUE; |
| 834 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 835 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 836 | if (!canAccessSensor(sensor->getSensor(), "Tried enabling", opPackageName)) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 837 | return BAD_VALUE; |
| 838 | } |
| 839 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 840 | Mutex::Autolock _l(mLock); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 841 | if (mCurrentOperatingMode == RESTRICTED && !isWhiteListedPackage(connection->getPackageName())) { |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 842 | return INVALID_OPERATION; |
| 843 | } |
| 844 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 845 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
| 846 | if (rec == 0) { |
| 847 | rec = new SensorRecord(connection); |
| 848 | mActiveSensors.add(handle, rec); |
| 849 | if (sensor->isVirtual()) { |
| 850 | mActiveVirtualSensors.add(handle, sensor); |
| 851 | } |
| 852 | } else { |
| 853 | if (rec->addConnection(connection)) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 854 | // this sensor is already activated, but we are adding a connection that uses it. |
| 855 | // Immediately send down the last known value of the requested sensor if it's not a |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 856 | // "continuous" sensor. |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 857 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ON_CHANGE) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 858 | // NOTE: The wake_up flag of this event may get set to |
| 859 | // WAKE_UP_SENSOR_EVENT_NEEDS_ACK if this is a wake_up event. |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 860 | CircularBuffer *circular_buf = mLastEventSeen.valueFor(handle); |
| 861 | if (circular_buf) { |
| 862 | sensors_event_t event; |
| 863 | memset(&event, 0, sizeof(event)); |
| 864 | // It is unlikely that this buffer is empty as the sensor is already active. |
| 865 | // One possible corner case may be two applications activating an on-change |
| 866 | // sensor at the same time. |
| 867 | if(circular_buf->populateLastEvent(&event)) { |
| 868 | event.sensor = handle; |
| 869 | if (event.version == sizeof(sensors_event_t)) { |
| 870 | if (isWakeUpSensorEvent(event) && !mWakeLockAcquired) { |
| 871 | setWakeLockAcquiredLocked(true); |
| 872 | } |
| 873 | connection->sendEvents(&event, 1, NULL); |
| 874 | if (!connection->needsWakeLock() && mWakeLockAcquired) { |
| 875 | checkWakeLockStateLocked(); |
| 876 | } |
| 877 | } |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 878 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | if (connection->addSensor(handle)) { |
| 885 | BatteryService::enableSensor(connection->getUid(), handle); |
| 886 | // the sensor was added (which means it wasn't already there) |
| 887 | // so, see if this connection becomes active |
| 888 | if (mActiveConnections.indexOf(connection) < 0) { |
| 889 | mActiveConnections.add(connection); |
| 890 | } |
| 891 | } else { |
| 892 | ALOGW("sensor %08x already enabled in connection %p (ignoring)", |
| 893 | handle, connection.get()); |
| 894 | } |
| 895 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 896 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 897 | if (samplingPeriodNs < minDelayNs) { |
| 898 | samplingPeriodNs = minDelayNs; |
| 899 | } |
| 900 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 901 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d" |
| 902 | "rate=%" PRId64 " timeout== %" PRId64"", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 903 | handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 904 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 905 | status_t err = sensor->batch(connection.get(), handle, 0, samplingPeriodNs, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 906 | maxBatchReportLatencyNs); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 907 | |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 908 | // Call flush() before calling activate() on the sensor. Wait for a first flush complete |
| 909 | // event before sending events on this connection. Ignore one-shot sensors which don't |
| 910 | // support flush(). Also if this sensor isn't already active, don't call flush(). |
| 911 | if (err == NO_ERROR && sensor->getSensor().getReportingMode() != AREPORTING_MODE_ONE_SHOT && |
| 912 | rec->getNumConnections() > 1) { |
| 913 | connection->setFirstFlushPending(handle, true); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 914 | status_t err_flush = sensor->flush(connection.get(), handle); |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 915 | // Flush may return error if the underlying h/w sensor uses an older HAL. |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 916 | if (err_flush == NO_ERROR) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 917 | rec->addPendingFlushConnection(connection.get()); |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 918 | } else { |
| 919 | connection->setFirstFlushPending(handle, false); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 920 | } |
| 921 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 922 | |
| 923 | if (err == NO_ERROR) { |
| 924 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle); |
| 925 | err = sensor->activate(connection.get(), true); |
| 926 | } |
| 927 | |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 928 | if (err == NO_ERROR) { |
| 929 | connection->updateLooperRegistration(mLooper); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 930 | SensorRegistrationInfo ®_info = |
| 931 | mLastNSensorRegistrations.editItemAt(mNextSensorRegIndex); |
| 932 | reg_info.mSensorHandle = handle; |
| 933 | reg_info.mSamplingRateUs = samplingPeriodNs/1000; |
| 934 | reg_info.mMaxReportLatencyUs = maxBatchReportLatencyNs/1000; |
| 935 | reg_info.mActivated = true; |
| 936 | reg_info.mPackageName = connection->getPackageName(); |
| 937 | time_t rawtime = time(NULL); |
| 938 | struct tm * timeinfo = localtime(&rawtime); |
| 939 | reg_info.mHour = timeinfo->tm_hour; |
| 940 | reg_info.mMin = timeinfo->tm_min; |
| 941 | reg_info.mSec = timeinfo->tm_sec; |
| 942 | mNextSensorRegIndex = (mNextSensorRegIndex + 1) % SENSOR_REGISTRATIONS_BUF_SIZE; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 943 | } |
| 944 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 945 | if (err != NO_ERROR) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 946 | // batch/activate has failed, reset our state. |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 947 | cleanupWithoutDisableLocked(connection, handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 948 | } |
| 949 | return err; |
| 950 | } |
| 951 | |
| 952 | status_t SensorService::disable(const sp<SensorEventConnection>& connection, |
| 953 | int handle) |
| 954 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 955 | if (mInitCheck != NO_ERROR) |
| 956 | return mInitCheck; |
| 957 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 958 | Mutex::Autolock _l(mLock); |
| 959 | status_t err = cleanupWithoutDisableLocked(connection, handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 960 | if (err == NO_ERROR) { |
| 961 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 962 | err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 963 | |
| 964 | } |
| 965 | if (err == NO_ERROR) { |
| 966 | SensorRegistrationInfo ®_info = |
| 967 | mLastNSensorRegistrations.editItemAt(mNextSensorRegIndex); |
| 968 | reg_info.mActivated = false; |
| 969 | reg_info.mPackageName= connection->getPackageName(); |
| 970 | reg_info.mSensorHandle = handle; |
| 971 | time_t rawtime = time(NULL); |
| 972 | struct tm * timeinfo = localtime(&rawtime); |
| 973 | reg_info.mHour = timeinfo->tm_hour; |
| 974 | reg_info.mMin = timeinfo->tm_min; |
| 975 | reg_info.mSec = timeinfo->tm_sec; |
| 976 | mNextSensorRegIndex = (mNextSensorRegIndex + 1) % SENSOR_REGISTRATIONS_BUF_SIZE; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 977 | } |
| 978 | return err; |
| 979 | } |
| 980 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 981 | status_t SensorService::cleanupWithoutDisable( |
| 982 | const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 983 | Mutex::Autolock _l(mLock); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 984 | return cleanupWithoutDisableLocked(connection, handle); |
| 985 | } |
| 986 | |
| 987 | status_t SensorService::cleanupWithoutDisableLocked( |
| 988 | const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 989 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 990 | if (rec) { |
| 991 | // see if this connection becomes inactive |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 992 | if (connection->removeSensor(handle)) { |
| 993 | BatteryService::disableSensor(connection->getUid(), handle); |
| 994 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 995 | if (connection->hasAnySensor() == false) { |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 996 | connection->updateLooperRegistration(mLooper); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 997 | mActiveConnections.remove(connection); |
| 998 | } |
| 999 | // see if this sensor becomes inactive |
| 1000 | if (rec->removeConnection(connection)) { |
| 1001 | mActiveSensors.removeItem(handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1002 | mActiveVirtualSensors.removeItem(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1003 | delete rec; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1004 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1005 | return NO_ERROR; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1006 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 1007 | return BAD_VALUE; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1010 | status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1011 | int handle, nsecs_t ns, const String16& opPackageName) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1012 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 1013 | if (mInitCheck != NO_ERROR) |
| 1014 | return mInitCheck; |
| 1015 | |
Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 1016 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 1017 | if (!sensor) |
| 1018 | return BAD_VALUE; |
| 1019 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1020 | if (!canAccessSensor(sensor->getSensor(), "Tried configuring", opPackageName)) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1021 | return BAD_VALUE; |
| 1022 | } |
| 1023 | |
Mathias Agopian | 1cd7000 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1024 | if (ns < 0) |
| 1025 | return BAD_VALUE; |
| 1026 | |
Mathias Agopian | 62569ec | 2011-11-07 21:21:47 -0800 | [diff] [blame] | 1027 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 1028 | if (ns < minDelayNs) { |
| 1029 | ns = minDelayNs; |
Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 1032 | return sensor->setDelay(connection.get(), handle, ns); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1035 | status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection, |
| 1036 | const String16& opPackageName) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1037 | if (mInitCheck != NO_ERROR) return mInitCheck; |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1038 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1039 | const int halVersion = dev.getHalDeviceVersion(); |
| 1040 | status_t err(NO_ERROR); |
| 1041 | Mutex::Autolock _l(mLock); |
| 1042 | // Loop through all sensors for this connection and call flush on each of them. |
| 1043 | for (size_t i = 0; i < connection->mSensorInfo.size(); ++i) { |
| 1044 | const int handle = connection->mSensorInfo.keyAt(i); |
| 1045 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 1046 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 1047 | ALOGE("flush called on a one-shot sensor"); |
| 1048 | err = INVALID_OPERATION; |
| 1049 | continue; |
| 1050 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1051 | if (halVersion <= SENSORS_DEVICE_API_VERSION_1_0 || isVirtualSensor(handle)) { |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1052 | // For older devices just increment pending flush count which will send a trivial |
| 1053 | // flush complete event. |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1054 | connection->incrementPendingFlushCount(handle); |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1055 | } else { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1056 | if (!canAccessSensor(sensor->getSensor(), "Tried flushing", opPackageName)) { |
| 1057 | err = INVALID_OPERATION; |
| 1058 | continue; |
| 1059 | } |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1060 | status_t err_flush = sensor->flush(connection.get(), handle); |
| 1061 | if (err_flush == NO_ERROR) { |
| 1062 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
| 1063 | if (rec != NULL) rec->addPendingFlushConnection(connection); |
| 1064 | } |
| 1065 | err = (err_flush != NO_ERROR) ? err_flush : err; |
| 1066 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1067 | } |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1068 | return err; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1069 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1070 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1071 | bool SensorService::canAccessSensor(const Sensor& sensor, const char* operation, |
| 1072 | const String16& opPackageName) { |
| 1073 | const String8& requiredPermission = sensor.getRequiredPermission(); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1074 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1075 | if (requiredPermission.length() <= 0) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1076 | return true; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | bool hasPermission = false; |
| 1080 | |
| 1081 | // Runtime permissions can't use the cache as they may change. |
| 1082 | if (sensor.isRequiredPermissionRuntime()) { |
| 1083 | hasPermission = checkPermission(String16(requiredPermission), |
| 1084 | IPCThreadState::self()->getCallingPid(), IPCThreadState::self()->getCallingUid()); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1085 | } else { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1086 | hasPermission = PermissionCache::checkCallingPermission(String16(requiredPermission)); |
| 1087 | } |
| 1088 | |
| 1089 | if (!hasPermission) { |
| 1090 | ALOGE("%s a sensor (%s) without holding its required permission: %s", |
| 1091 | operation, sensor.getName().string(), sensor.getRequiredPermission().string()); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1092 | return false; |
| 1093 | } |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1094 | |
| 1095 | const int32_t opCode = sensor.getRequiredAppOp(); |
| 1096 | if (opCode >= 0) { |
| 1097 | AppOpsManager appOps; |
| 1098 | if (appOps.noteOp(opCode, IPCThreadState::self()->getCallingUid(), opPackageName) |
| 1099 | != AppOpsManager::MODE_ALLOWED) { |
| 1100 | ALOGE("%s a sensor (%s) without enabled required app op: %D", |
| 1101 | operation, sensor.getName().string(), opCode); |
| 1102 | return false; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | return true; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1109 | bool SensorService::hasDataInjectionPermissions() { |
| 1110 | if (!PermissionCache::checkCallingPermission(sDataInjectionPermission)) { |
| 1111 | ALOGE("Permission Denial trying to activate data injection without" |
| 1112 | " the required permission"); |
| 1113 | return false; |
| 1114 | } |
| 1115 | return true; |
| 1116 | } |
| 1117 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1118 | void SensorService::checkWakeLockState() { |
| 1119 | Mutex::Autolock _l(mLock); |
| 1120 | checkWakeLockStateLocked(); |
| 1121 | } |
| 1122 | |
| 1123 | void SensorService::checkWakeLockStateLocked() { |
| 1124 | if (!mWakeLockAcquired) { |
| 1125 | return; |
| 1126 | } |
| 1127 | bool releaseLock = true; |
| 1128 | for (size_t i=0 ; i<mActiveConnections.size() ; i++) { |
| 1129 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
| 1130 | if (connection != 0) { |
| 1131 | if (connection->needsWakeLock()) { |
| 1132 | releaseLock = false; |
| 1133 | break; |
| 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | if (releaseLock) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1138 | setWakeLockAcquiredLocked(false); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | void SensorService::sendEventsFromCache(const sp<SensorEventConnection>& connection) { |
| 1143 | Mutex::Autolock _l(mLock); |
| 1144 | connection->writeToSocketFromCache(); |
| 1145 | if (connection->needsWakeLock()) { |
| 1146 | setWakeLockAcquiredLocked(true); |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | void SensorService::populateActiveConnections( |
| 1151 | SortedVector< sp<SensorEventConnection> >* activeConnections) { |
| 1152 | Mutex::Autolock _l(mLock); |
| 1153 | for (size_t i=0 ; i < mActiveConnections.size(); ++i) { |
| 1154 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
| 1155 | if (connection != 0) { |
| 1156 | activeConnections->add(connection); |
| 1157 | } |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1158 | } |
| 1159 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1160 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1161 | bool SensorService::isWhiteListedPackage(const String8& packageName) { |
| 1162 | // TODO: Come up with a list of packages. |
| 1163 | return (packageName.find(".cts.") != -1); |
| 1164 | } |
| 1165 | |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1166 | int SensorService::getNumEventsForSensorType(int sensor_event_type) { |
| 1167 | switch (sensor_event_type) { |
| 1168 | case SENSOR_TYPE_ROTATION_VECTOR: |
| 1169 | case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR: |
| 1170 | return 5; |
| 1171 | |
| 1172 | case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED: |
| 1173 | case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: |
| 1174 | return 6; |
| 1175 | |
| 1176 | case SENSOR_TYPE_GAME_ROTATION_VECTOR: |
| 1177 | return 4; |
| 1178 | |
| 1179 | case SENSOR_TYPE_SIGNIFICANT_MOTION: |
| 1180 | case SENSOR_TYPE_STEP_DETECTOR: |
| 1181 | case SENSOR_TYPE_STEP_COUNTER: |
| 1182 | return 1; |
| 1183 | |
| 1184 | default: |
| 1185 | return 3; |
| 1186 | } |
| 1187 | } |
| 1188 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1189 | // --------------------------------------------------------------------------- |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1190 | SensorService::SensorRecord::SensorRecord( |
| 1191 | const sp<SensorEventConnection>& connection) |
| 1192 | { |
| 1193 | mConnections.add(connection); |
| 1194 | } |
| 1195 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1196 | bool SensorService::SensorRecord::addConnection( |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1197 | const sp<SensorEventConnection>& connection) |
| 1198 | { |
| 1199 | if (mConnections.indexOf(connection) < 0) { |
| 1200 | mConnections.add(connection); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1201 | return true; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1202 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1203 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | bool SensorService::SensorRecord::removeConnection( |
| 1207 | const wp<SensorEventConnection>& connection) |
| 1208 | { |
| 1209 | ssize_t index = mConnections.indexOf(connection); |
| 1210 | if (index >= 0) { |
| 1211 | mConnections.removeItemsAt(index, 1); |
| 1212 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1213 | // Remove this connections from the queue of flush() calls made on this sensor. |
| 1214 | for (Vector< wp<SensorEventConnection> >::iterator it = |
| 1215 | mPendingFlushConnections.begin(); it != mPendingFlushConnections.end();) { |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1216 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1217 | if (it->unsafe_get() == connection.unsafe_get()) { |
| 1218 | it = mPendingFlushConnections.erase(it); |
| 1219 | } else { |
| 1220 | ++it; |
| 1221 | } |
| 1222 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1223 | return mConnections.size() ? false : true; |
| 1224 | } |
| 1225 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1226 | void SensorService::SensorRecord::addPendingFlushConnection( |
| 1227 | const sp<SensorEventConnection>& connection) { |
| 1228 | mPendingFlushConnections.add(connection); |
| 1229 | } |
| 1230 | |
| 1231 | void SensorService::SensorRecord::removeFirstPendingFlushConnection() { |
| 1232 | if (mPendingFlushConnections.size() > 0) { |
| 1233 | mPendingFlushConnections.removeAt(0); |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | SensorService::SensorEventConnection * |
| 1238 | SensorService::SensorRecord::getFirstPendingFlushConnection() { |
| 1239 | if (mPendingFlushConnections.size() > 0) { |
| 1240 | return mPendingFlushConnections[0].unsafe_get(); |
| 1241 | } |
| 1242 | return NULL; |
| 1243 | } |
| 1244 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1245 | void SensorService::SensorRecord::clearAllPendingFlushConnections() { |
| 1246 | mPendingFlushConnections.clear(); |
| 1247 | } |
| 1248 | |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1249 | |
| 1250 | // --------------------------------------------------------------------------- |
| 1251 | SensorService::TrimmedSensorEvent::TrimmedSensorEvent(int sensorType) { |
| 1252 | mTimestamp = -1; |
| 1253 | const int numData = SensorService::getNumEventsForSensorType(sensorType); |
| 1254 | if (sensorType == SENSOR_TYPE_STEP_COUNTER) { |
| 1255 | mStepCounter = 0; |
| 1256 | } else { |
| 1257 | mData = new float[numData]; |
| 1258 | for (int i = 0; i < numData; ++i) { |
| 1259 | mData[i] = -1.0; |
| 1260 | } |
| 1261 | } |
| 1262 | mHour = mMin = mSec = INT32_MIN; |
| 1263 | } |
| 1264 | |
| 1265 | bool SensorService::TrimmedSensorEvent::isSentinel(const TrimmedSensorEvent& event) { |
| 1266 | return (event.mHour == INT32_MIN && event.mMin == INT32_MIN && event.mSec == INT32_MIN); |
| 1267 | } |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1268 | // -------------------------------------------------------------------------- |
| 1269 | SensorService::CircularBuffer::CircularBuffer(int sensor_event_type) { |
| 1270 | mNextInd = 0; |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1271 | mBufSize = CIRCULAR_BUF_SIZE; |
| 1272 | if (sensor_event_type == SENSOR_TYPE_STEP_COUNTER || |
| 1273 | sensor_event_type == SENSOR_TYPE_SIGNIFICANT_MOTION || |
| 1274 | sensor_event_type == SENSOR_TYPE_ACCELEROMETER) { |
| 1275 | mBufSize = CIRCULAR_BUF_SIZE * 5; |
| 1276 | } |
| 1277 | mTrimmedSensorEventArr = new TrimmedSensorEvent *[mBufSize]; |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1278 | mSensorType = sensor_event_type; |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1279 | for (int i = 0; i < mBufSize; ++i) { |
| 1280 | mTrimmedSensorEventArr[i] = new TrimmedSensorEvent(mSensorType); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | void SensorService::CircularBuffer::addEvent(const sensors_event_t& sensor_event) { |
| 1285 | TrimmedSensorEvent *curr_event = mTrimmedSensorEventArr[mNextInd]; |
| 1286 | curr_event->mTimestamp = sensor_event.timestamp; |
| 1287 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { |
| 1288 | curr_event->mStepCounter = sensor_event.u64.step_counter; |
| 1289 | } else { |
| 1290 | memcpy(curr_event->mData, sensor_event.data, |
| 1291 | sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType)); |
| 1292 | } |
| 1293 | time_t rawtime = time(NULL); |
| 1294 | struct tm * timeinfo = localtime(&rawtime); |
| 1295 | curr_event->mHour = timeinfo->tm_hour; |
| 1296 | curr_event->mMin = timeinfo->tm_min; |
| 1297 | curr_event->mSec = timeinfo->tm_sec; |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1298 | mNextInd = (mNextInd + 1) % mBufSize; |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | void SensorService::CircularBuffer::printBuffer(String8& result) const { |
| 1302 | const int numData = SensorService::getNumEventsForSensorType(mSensorType); |
| 1303 | int i = mNextInd, eventNum = 1; |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1304 | result.appendFormat("last %d events = < ", mBufSize); |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1305 | do { |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1306 | if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[i])) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1307 | // Sentinel, ignore. |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1308 | i = (i + 1) % mBufSize; |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1309 | continue; |
| 1310 | } |
| 1311 | result.appendFormat("%d) ", eventNum++); |
| 1312 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { |
| 1313 | result.appendFormat("%llu,", mTrimmedSensorEventArr[i]->mStepCounter); |
| 1314 | } else { |
| 1315 | for (int j = 0; j < numData; ++j) { |
| 1316 | result.appendFormat("%5.1f,", mTrimmedSensorEventArr[i]->mData[j]); |
| 1317 | } |
| 1318 | } |
| 1319 | result.appendFormat("%lld %02d:%02d:%02d ", mTrimmedSensorEventArr[i]->mTimestamp, |
| 1320 | mTrimmedSensorEventArr[i]->mHour, mTrimmedSensorEventArr[i]->mMin, |
| 1321 | mTrimmedSensorEventArr[i]->mSec); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1322 | i = (i + 1) % mBufSize; |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1323 | } while (i != mNextInd); |
| 1324 | result.appendFormat(">\n"); |
| 1325 | } |
| 1326 | |
| 1327 | bool SensorService::CircularBuffer::populateLastEvent(sensors_event_t *event) { |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1328 | int lastEventInd = (mNextInd - 1 + mBufSize) % mBufSize; |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1329 | // Check if the buffer is empty. |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1330 | if (TrimmedSensorEvent::isSentinel(*mTrimmedSensorEventArr[lastEventInd])) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1331 | return false; |
| 1332 | } |
| 1333 | event->version = sizeof(sensors_event_t); |
| 1334 | event->type = mSensorType; |
| 1335 | event->timestamp = mTrimmedSensorEventArr[lastEventInd]->mTimestamp; |
| 1336 | if (mSensorType == SENSOR_TYPE_STEP_COUNTER) { |
| 1337 | event->u64.step_counter = mTrimmedSensorEventArr[lastEventInd]->mStepCounter; |
| 1338 | } else { |
| 1339 | memcpy(event->data, mTrimmedSensorEventArr[lastEventInd]->mData, |
| 1340 | sizeof(float) * SensorService::getNumEventsForSensorType(mSensorType)); |
| 1341 | } |
| 1342 | return true; |
| 1343 | } |
| 1344 | |
| 1345 | SensorService::CircularBuffer::~CircularBuffer() { |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1346 | for (int i = 0; i < mBufSize; ++i) { |
Aravind Akella | 444f267 | 2015-05-07 12:40:52 -0700 | [diff] [blame] | 1347 | delete mTrimmedSensorEventArr[i]; |
| 1348 | } |
| 1349 | delete [] mTrimmedSensorEventArr; |
| 1350 | } |
| 1351 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1352 | // --------------------------------------------------------------------------- |
| 1353 | |
| 1354 | SensorService::SensorEventConnection::SensorEventConnection( |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1355 | const sp<SensorService>& service, uid_t uid, String8 packageName, bool isDataInjectionMode, |
| 1356 | const String16& opPackageName) |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1357 | : mService(service), mUid(uid), mWakeLockRefCount(0), mHasLooperCallbacks(false), |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1358 | mDead(false), mDataInjectionMode(isDataInjectionMode), mEventCache(NULL), |
| 1359 | mCacheSize(0), mMaxCacheSize(0), mPackageName(packageName), mOpPackageName(opPackageName) { |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1360 | mChannel = new BitTube(mService->mSocketBufferSize); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1361 | #if DEBUG_CONNECTIONS |
| 1362 | mEventsReceived = mEventsSentFromCache = mEventsSent = 0; |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1363 | mTotalAcksNeeded = mTotalAcksReceived = 0; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1364 | #endif |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1367 | SensorService::SensorEventConnection::~SensorEventConnection() { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1368 | ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this); |
Aravind Akella | e148bc2 | 2014-09-24 22:12:58 -0700 | [diff] [blame] | 1369 | mService->cleanupConnection(this); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1370 | if (mEventCache != NULL) { |
| 1371 | delete mEventCache; |
| 1372 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1375 | void SensorService::SensorEventConnection::onFirstRef() { |
| 1376 | LooperCallback::onFirstRef(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1379 | bool SensorService::SensorEventConnection::needsWakeLock() { |
| 1380 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1381 | return !mDead && mWakeLockRefCount > 0; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1382 | } |
| 1383 | |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1384 | void SensorService::SensorEventConnection::resetWakeLockRefCount() { |
| 1385 | Mutex::Autolock _l(mConnectionLock); |
| 1386 | mWakeLockRefCount = 0; |
| 1387 | } |
| 1388 | |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1389 | void SensorService::SensorEventConnection::dump(String8& result) { |
| 1390 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1391 | result.appendFormat("Operating Mode: %s\n", mDataInjectionMode ? "DATA_INJECTION" : "NORMAL"); |
Aravind Akella | 18d6d51 | 2015-06-18 14:18:28 -0700 | [diff] [blame] | 1392 | result.appendFormat("\t %s | WakeLockRefCount %d | uid %d | cache size %d | " |
| 1393 | "max cache size %d\n", mPackageName.string(), mWakeLockRefCount, mUid, mCacheSize, |
| 1394 | mMaxCacheSize); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1395 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 1396 | const FlushInfo& flushInfo = mSensorInfo.valueAt(i); |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1397 | result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d \n", |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1398 | mService->getSensorName(mSensorInfo.keyAt(i)).string(), |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1399 | mSensorInfo.keyAt(i), |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1400 | flushInfo.mFirstFlushPending ? "First flush pending" : |
| 1401 | "active", |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1402 | flushInfo.mPendingFlushEventsToSend); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1403 | } |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1404 | #if DEBUG_CONNECTIONS |
| 1405 | result.appendFormat("\t events recvd: %d | sent %d | cache %d | dropped %d |" |
| 1406 | " total_acks_needed %d | total_acks_recvd %d\n", |
| 1407 | mEventsReceived, |
| 1408 | mEventsSent, |
| 1409 | mEventsSentFromCache, |
| 1410 | mEventsReceived - (mEventsSentFromCache + mEventsSent + mCacheSize), |
| 1411 | mTotalAcksNeeded, |
| 1412 | mTotalAcksReceived); |
| 1413 | #endif |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1416 | bool SensorService::SensorEventConnection::addSensor(int32_t handle) { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 1417 | Mutex::Autolock _l(mConnectionLock); |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1418 | if (!canAccessSensor(mService->getSensorFromHandle(handle), |
| 1419 | "Tried adding", mOpPackageName)) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 1420 | return false; |
| 1421 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1422 | if (mSensorInfo.indexOfKey(handle) < 0) { |
| 1423 | mSensorInfo.add(handle, FlushInfo()); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1424 | return true; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1425 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1426 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1427 | } |
| 1428 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1429 | bool SensorService::SensorEventConnection::removeSensor(int32_t handle) { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 1430 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1431 | if (mSensorInfo.removeItem(handle) >= 0) { |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1432 | return true; |
| 1433 | } |
| 1434 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 1438 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1439 | return mSensorInfo.indexOfKey(handle) >= 0; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | bool SensorService::SensorEventConnection::hasAnySensor() const { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 1443 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1444 | return mSensorInfo.size() ? true : false; |
| 1445 | } |
| 1446 | |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1447 | bool SensorService::SensorEventConnection::hasOneShotSensors() const { |
| 1448 | Mutex::Autolock _l(mConnectionLock); |
| 1449 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 1450 | const int handle = mSensorInfo.keyAt(i); |
| 1451 | if (mService->getSensorFromHandle(handle).getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 1452 | return true; |
| 1453 | } |
| 1454 | } |
| 1455 | return false; |
| 1456 | } |
| 1457 | |
Aravind Akella | 4949c50 | 2015-02-11 15:54:35 -0800 | [diff] [blame] | 1458 | String8 SensorService::SensorEventConnection::getPackageName() const { |
| 1459 | return mPackageName; |
| 1460 | } |
| 1461 | |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1462 | void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle, |
| 1463 | bool value) { |
| 1464 | Mutex::Autolock _l(mConnectionLock); |
| 1465 | ssize_t index = mSensorInfo.indexOfKey(handle); |
| 1466 | if (index >= 0) { |
| 1467 | FlushInfo& flushInfo = mSensorInfo.editValueAt(index); |
| 1468 | flushInfo.mFirstFlushPending = value; |
| 1469 | } |
| 1470 | } |
| 1471 | |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1472 | void SensorService::SensorEventConnection::updateLooperRegistration(const sp<Looper>& looper) { |
| 1473 | Mutex::Autolock _l(mConnectionLock); |
| 1474 | updateLooperRegistrationLocked(looper); |
| 1475 | } |
| 1476 | |
| 1477 | void SensorService::SensorEventConnection::updateLooperRegistrationLocked( |
| 1478 | const sp<Looper>& looper) { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1479 | bool isConnectionActive = (mSensorInfo.size() > 0 && !mDataInjectionMode) || |
| 1480 | mDataInjectionMode; |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1481 | // If all sensors are unregistered OR Looper has encountered an error, we |
| 1482 | // can remove the Fd from the Looper if it has been previously added. |
| 1483 | if (!isConnectionActive || mDead) { |
| 1484 | if (mHasLooperCallbacks) { |
| 1485 | ALOGD_IF(DEBUG_CONNECTIONS, "%p removeFd fd=%d", this, mChannel->getSendFd()); |
| 1486 | looper->removeFd(mChannel->getSendFd()); |
| 1487 | mHasLooperCallbacks = false; |
| 1488 | } |
| 1489 | return; |
| 1490 | } |
| 1491 | |
| 1492 | int looper_flags = 0; |
| 1493 | if (mCacheSize > 0) looper_flags |= ALOOPER_EVENT_OUTPUT; |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1494 | if (mDataInjectionMode) looper_flags |= ALOOPER_EVENT_INPUT; |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1495 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 1496 | const int handle = mSensorInfo.keyAt(i); |
| 1497 | if (mService->getSensorFromHandle(handle).isWakeUpSensor()) { |
| 1498 | looper_flags |= ALOOPER_EVENT_INPUT; |
| 1499 | break; |
| 1500 | } |
| 1501 | } |
| 1502 | // If flags is still set to zero, we don't need to add this fd to the Looper, if |
| 1503 | // the fd has already been added, remove it. This is likely to happen when ALL the |
| 1504 | // events stored in the cache have been sent to the corresponding app. |
| 1505 | if (looper_flags == 0) { |
| 1506 | if (mHasLooperCallbacks) { |
| 1507 | ALOGD_IF(DEBUG_CONNECTIONS, "removeFd fd=%d", mChannel->getSendFd()); |
| 1508 | looper->removeFd(mChannel->getSendFd()); |
| 1509 | mHasLooperCallbacks = false; |
| 1510 | } |
| 1511 | return; |
| 1512 | } |
| 1513 | // Add the file descriptor to the Looper for receiving acknowledegments if the app has |
| 1514 | // registered for wake-up sensors OR for sending events in the cache. |
| 1515 | int ret = looper->addFd(mChannel->getSendFd(), 0, looper_flags, this, NULL); |
| 1516 | if (ret == 1) { |
| 1517 | ALOGD_IF(DEBUG_CONNECTIONS, "%p addFd fd=%d", this, mChannel->getSendFd()); |
| 1518 | mHasLooperCallbacks = true; |
| 1519 | } else { |
| 1520 | ALOGE("Looper::addFd failed ret=%d fd=%d", ret, mChannel->getSendFd()); |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | void SensorService::SensorEventConnection::incrementPendingFlushCount(int32_t handle) { |
| 1525 | Mutex::Autolock _l(mConnectionLock); |
| 1526 | ssize_t index = mSensorInfo.indexOfKey(handle); |
| 1527 | if (index >= 0) { |
| 1528 | FlushInfo& flushInfo = mSensorInfo.editValueAt(index); |
| 1529 | flushInfo.mPendingFlushEventsToSend++; |
| 1530 | } |
| 1531 | } |
| 1532 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1533 | status_t SensorService::SensorEventConnection::sendEvents( |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1534 | sensors_event_t const* buffer, size_t numEvents, |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1535 | sensors_event_t* scratch, |
| 1536 | SensorEventConnection const * const * mapFlushEventsToConnections) { |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 1537 | // filter out events not for this connection |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1538 | int count = 0; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1539 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 1540 | if (scratch) { |
| 1541 | size_t i=0; |
| 1542 | while (i<numEvents) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1543 | int32_t sensor_handle = buffer[i].sensor; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1544 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { |
| 1545 | ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ", |
Aravind Akella | 9e3adfc | 2014-09-03 15:48:05 -0700 | [diff] [blame] | 1546 | buffer[i].meta_data.sensor); |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1547 | // Setting sensor_handle to the correct sensor to ensure the sensor events per |
| 1548 | // connection are filtered correctly. buffer[i].sensor is zero for meta_data |
| 1549 | // events. |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1550 | sensor_handle = buffer[i].meta_data.sensor; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1551 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1552 | ssize_t index = mSensorInfo.indexOfKey(sensor_handle); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1553 | // Check if this connection has registered for this sensor. If not continue to the |
| 1554 | // next sensor_event. |
| 1555 | if (index < 0) { |
| 1556 | ++i; |
| 1557 | continue; |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1558 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1559 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1560 | FlushInfo& flushInfo = mSensorInfo.editValueAt(index); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1561 | // Check if there is a pending flush_complete event for this sensor on this connection. |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1562 | if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true && |
| 1563 | this == mapFlushEventsToConnections[i]) { |
| 1564 | flushInfo.mFirstFlushPending = false; |
| 1565 | ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ", |
| 1566 | buffer[i].meta_data.sensor); |
| 1567 | ++i; |
| 1568 | continue; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | // If there is a pending flush complete event for this sensor on this connection, |
| 1572 | // ignore the event and proceed to the next. |
| 1573 | if (flushInfo.mFirstFlushPending) { |
| 1574 | ++i; |
| 1575 | continue; |
| 1576 | } |
| 1577 | |
| 1578 | do { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1579 | // Keep copying events into the scratch buffer as long as they are regular |
| 1580 | // sensor_events are from the same sensor_handle OR they are flush_complete_events |
| 1581 | // from the same sensor_handle AND the current connection is mapped to the |
| 1582 | // corresponding flush_complete_event. |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1583 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1584 | if (this == mapFlushEventsToConnections[i]) { |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1585 | scratch[count++] = buffer[i]; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1586 | } |
| 1587 | ++i; |
| 1588 | } else { |
| 1589 | // Regular sensor event, just copy it to the scratch buffer. |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1590 | scratch[count++] = buffer[i++]; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1591 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 1592 | } while ((i<numEvents) && ((buffer[i].sensor == sensor_handle && |
| 1593 | buffer[i].type != SENSOR_TYPE_META_DATA) || |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1594 | (buffer[i].type == SENSOR_TYPE_META_DATA && |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1595 | buffer[i].meta_data.sensor == sensor_handle))); |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 1596 | } |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 1597 | } else { |
| 1598 | scratch = const_cast<sensors_event_t *>(buffer); |
| 1599 | count = numEvents; |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 1600 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1601 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1602 | sendPendingFlushEventsLocked(); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1603 | // Early return if there are no events for this connection. |
| 1604 | if (count == 0) { |
| 1605 | return status_t(NO_ERROR); |
| 1606 | } |
| 1607 | |
| 1608 | #if DEBUG_CONNECTIONS |
| 1609 | mEventsReceived += count; |
| 1610 | #endif |
| 1611 | if (mCacheSize != 0) { |
| 1612 | // There are some events in the cache which need to be sent first. Copy this buffer to |
| 1613 | // the end of cache. |
| 1614 | if (mCacheSize + count <= mMaxCacheSize) { |
| 1615 | memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t)); |
| 1616 | mCacheSize += count; |
| 1617 | } else { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1618 | // Check if any new sensors have registered on this connection which may have increased |
| 1619 | // the max cache size that is desired. |
| 1620 | if (mCacheSize + count < computeMaxCacheSizeLocked()) { |
| 1621 | reAllocateCacheLocked(scratch, count); |
| 1622 | return status_t(NO_ERROR); |
| 1623 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1624 | // Some events need to be dropped. |
| 1625 | int remaningCacheSize = mMaxCacheSize - mCacheSize; |
| 1626 | if (remaningCacheSize != 0) { |
| 1627 | memcpy(&mEventCache[mCacheSize], scratch, |
| 1628 | remaningCacheSize * sizeof(sensors_event_t)); |
| 1629 | } |
| 1630 | int numEventsDropped = count - remaningCacheSize; |
| 1631 | countFlushCompleteEventsLocked(mEventCache, numEventsDropped); |
| 1632 | // Drop the first "numEventsDropped" in the cache. |
| 1633 | memmove(mEventCache, &mEventCache[numEventsDropped], |
| 1634 | (mCacheSize - numEventsDropped) * sizeof(sensors_event_t)); |
| 1635 | |
| 1636 | // Copy the remainingEvents in scratch buffer to the end of cache. |
| 1637 | memcpy(&mEventCache[mCacheSize - numEventsDropped], scratch + remaningCacheSize, |
| 1638 | numEventsDropped * sizeof(sensors_event_t)); |
| 1639 | } |
| 1640 | return status_t(NO_ERROR); |
| 1641 | } |
| 1642 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1643 | int index_wake_up_event = findWakeUpSensorEventLocked(scratch, count); |
| 1644 | if (index_wake_up_event >= 0) { |
| 1645 | scratch[index_wake_up_event].flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 1646 | ++mWakeLockRefCount; |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1647 | #if DEBUG_CONNECTIONS |
| 1648 | ++mTotalAcksNeeded; |
| 1649 | #endif |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1650 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1651 | |
| 1652 | // NOTE: ASensorEvent and sensors_event_t are the same type. |
| 1653 | ssize_t size = SensorEventQueue::write(mChannel, |
| 1654 | reinterpret_cast<ASensorEvent const*>(scratch), count); |
| 1655 | if (size < 0) { |
| 1656 | // Write error, copy events to local cache. |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1657 | if (index_wake_up_event >= 0) { |
| 1658 | // If there was a wake_up sensor_event, reset the flag. |
| 1659 | scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1660 | if (mWakeLockRefCount > 0) { |
| 1661 | --mWakeLockRefCount; |
| 1662 | } |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1663 | #if DEBUG_CONNECTIONS |
| 1664 | --mTotalAcksNeeded; |
| 1665 | #endif |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1666 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1667 | if (mEventCache == NULL) { |
| 1668 | mMaxCacheSize = computeMaxCacheSizeLocked(); |
| 1669 | mEventCache = new sensors_event_t[mMaxCacheSize]; |
| 1670 | mCacheSize = 0; |
| 1671 | } |
| 1672 | memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t)); |
| 1673 | mCacheSize += count; |
| 1674 | |
| 1675 | // Add this file descriptor to the looper to get a callback when this fd is available for |
| 1676 | // writing. |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1677 | updateLooperRegistrationLocked(mService->getLooper()); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1678 | return size; |
| 1679 | } |
| 1680 | |
| 1681 | #if DEBUG_CONNECTIONS |
| 1682 | if (size > 0) { |
| 1683 | mEventsSent += count; |
| 1684 | } |
| 1685 | #endif |
| 1686 | |
| 1687 | return size < 0 ? status_t(size) : status_t(NO_ERROR); |
| 1688 | } |
| 1689 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1690 | void SensorService::SensorEventConnection::reAllocateCacheLocked(sensors_event_t const* scratch, |
| 1691 | int count) { |
| 1692 | sensors_event_t *eventCache_new; |
| 1693 | const int new_cache_size = computeMaxCacheSizeLocked(); |
| 1694 | // Allocate new cache, copy over events from the old cache & scratch, free up memory. |
| 1695 | eventCache_new = new sensors_event_t[new_cache_size]; |
| 1696 | memcpy(eventCache_new, mEventCache, mCacheSize * sizeof(sensors_event_t)); |
| 1697 | memcpy(&eventCache_new[mCacheSize], scratch, count * sizeof(sensors_event_t)); |
| 1698 | |
| 1699 | ALOGD_IF(DEBUG_CONNECTIONS, "reAllocateCacheLocked maxCacheSize=%d %d", mMaxCacheSize, |
| 1700 | new_cache_size); |
| 1701 | |
| 1702 | delete mEventCache; |
| 1703 | mEventCache = eventCache_new; |
| 1704 | mCacheSize += count; |
| 1705 | mMaxCacheSize = new_cache_size; |
| 1706 | } |
| 1707 | |
| 1708 | void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() { |
| 1709 | ASensorEvent flushCompleteEvent; |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1710 | memset(&flushCompleteEvent, 0, sizeof(flushCompleteEvent)); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1711 | flushCompleteEvent.type = SENSOR_TYPE_META_DATA; |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1712 | // Loop through all the sensors for this connection and check if there are any pending |
| 1713 | // flush complete events to be sent. |
| 1714 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 1715 | FlushInfo& flushInfo = mSensorInfo.editValueAt(i); |
| 1716 | while (flushInfo.mPendingFlushEventsToSend > 0) { |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1717 | const int sensor_handle = mSensorInfo.keyAt(i); |
| 1718 | flushCompleteEvent.meta_data.sensor = sensor_handle; |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1719 | bool wakeUpSensor = mService->getSensorFromHandle(sensor_handle).isWakeUpSensor(); |
| 1720 | if (wakeUpSensor) { |
| 1721 | ++mWakeLockRefCount; |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1722 | flushCompleteEvent.flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 1723 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1724 | ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1); |
| 1725 | if (size < 0) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1726 | if (wakeUpSensor) --mWakeLockRefCount; |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1727 | return; |
| 1728 | } |
| 1729 | ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ", |
| 1730 | flushCompleteEvent.meta_data.sensor); |
| 1731 | flushInfo.mPendingFlushEventsToSend--; |
| 1732 | } |
| 1733 | } |
| 1734 | } |
| 1735 | |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1736 | void SensorService::SensorEventConnection::writeToSocketFromCache() { |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1737 | // At a time write at most half the size of the receiver buffer in SensorEventQueue OR |
| 1738 | // half the size of the socket buffer allocated in BitTube whichever is smaller. |
| 1739 | const int maxWriteSize = helpers::min(SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2, |
| 1740 | int(mService->mSocketBufferSize/(sizeof(sensors_event_t)*2))); |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1741 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1742 | // Send pending flush complete events (if any) |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1743 | sendPendingFlushEventsLocked(); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1744 | for (int numEventsSent = 0; numEventsSent < mCacheSize;) { |
Aravind Akella | 5466c3d | 2014-08-22 16:11:10 -0700 | [diff] [blame] | 1745 | const int numEventsToWrite = helpers::min(mCacheSize - numEventsSent, maxWriteSize); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1746 | int index_wake_up_event = |
| 1747 | findWakeUpSensorEventLocked(mEventCache + numEventsSent, numEventsToWrite); |
| 1748 | if (index_wake_up_event >= 0) { |
| 1749 | mEventCache[index_wake_up_event + numEventsSent].flags |= |
| 1750 | WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 1751 | ++mWakeLockRefCount; |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1752 | #if DEBUG_CONNECTIONS |
| 1753 | ++mTotalAcksNeeded; |
| 1754 | #endif |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1755 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1756 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1757 | ssize_t size = SensorEventQueue::write(mChannel, |
| 1758 | reinterpret_cast<ASensorEvent const*>(mEventCache + numEventsSent), |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1759 | numEventsToWrite); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1760 | if (size < 0) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1761 | if (index_wake_up_event >= 0) { |
| 1762 | // If there was a wake_up sensor_event, reset the flag. |
| 1763 | mEventCache[index_wake_up_event + numEventsSent].flags &= |
| 1764 | ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1765 | if (mWakeLockRefCount > 0) { |
| 1766 | --mWakeLockRefCount; |
| 1767 | } |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1768 | #if DEBUG_CONNECTIONS |
| 1769 | --mTotalAcksNeeded; |
| 1770 | #endif |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1771 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1772 | memmove(mEventCache, &mEventCache[numEventsSent], |
| 1773 | (mCacheSize - numEventsSent) * sizeof(sensors_event_t)); |
| 1774 | ALOGD_IF(DEBUG_CONNECTIONS, "wrote %d events from cache size==%d ", |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1775 | numEventsSent, mCacheSize); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1776 | mCacheSize -= numEventsSent; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1777 | return; |
| 1778 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1779 | numEventsSent += numEventsToWrite; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1780 | #if DEBUG_CONNECTIONS |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1781 | mEventsSentFromCache += numEventsToWrite; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1782 | #endif |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 1783 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1784 | ALOGD_IF(DEBUG_CONNECTIONS, "wrote all events from cache size=%d ", mCacheSize); |
| 1785 | // All events from the cache have been sent. Reset cache size to zero. |
| 1786 | mCacheSize = 0; |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1787 | // There are no more events in the cache. We don't need to poll for write on the fd. |
| 1788 | // Update Looper registration. |
| 1789 | updateLooperRegistrationLocked(mService->getLooper()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 1792 | void SensorService::SensorEventConnection::countFlushCompleteEventsLocked( |
Aravind Akella | 0ec2066 | 2014-09-14 17:29:48 -0700 | [diff] [blame] | 1793 | sensors_event_t const* scratch, const int numEventsDropped) { |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1794 | ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1795 | // Count flushComplete events in the events that are about to the dropped. These will be sent |
| 1796 | // separately before the next batch of events. |
| 1797 | for (int j = 0; j < numEventsDropped; ++j) { |
| 1798 | if (scratch[j].type == SENSOR_TYPE_META_DATA) { |
| 1799 | FlushInfo& flushInfo = mSensorInfo.editValueFor(scratch[j].meta_data.sensor); |
| 1800 | flushInfo.mPendingFlushEventsToSend++; |
| 1801 | ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d", |
| 1802 | flushInfo.mPendingFlushEventsToSend); |
| 1803 | } |
| 1804 | } |
| 1805 | return; |
| 1806 | } |
| 1807 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1808 | int SensorService::SensorEventConnection::findWakeUpSensorEventLocked( |
| 1809 | sensors_event_t const* scratch, const int count) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1810 | for (int i = 0; i < count; ++i) { |
| 1811 | if (mService->isWakeUpSensorEvent(scratch[i])) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1812 | return i; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1813 | } |
| 1814 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1815 | return -1; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1816 | } |
| 1817 | |
Mathias Agopian | b398927 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 1818 | sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1819 | { |
| 1820 | return mChannel; |
| 1821 | } |
| 1822 | |
| 1823 | status_t SensorService::SensorEventConnection::enableDisable( |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1824 | int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, |
| 1825 | int reservedFlags) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1826 | { |
| 1827 | status_t err; |
| 1828 | if (enabled) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1829 | err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs, |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1830 | reservedFlags, mOpPackageName); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1831 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1832 | } else { |
| 1833 | err = mService->disable(this, handle); |
| 1834 | } |
| 1835 | return err; |
| 1836 | } |
| 1837 | |
| 1838 | status_t SensorService::SensorEventConnection::setEventRate( |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1839 | int handle, nsecs_t samplingPeriodNs) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1840 | { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1841 | return mService->setEventRate(this, handle, samplingPeriodNs, mOpPackageName); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1842 | } |
| 1843 | |
Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 1844 | status_t SensorService::SensorEventConnection::flush() { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1845 | return mService->flushSensor(this, mOpPackageName); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1846 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1847 | |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1848 | int SensorService::SensorEventConnection::handleEvent(int fd, int events, void* /*data*/) { |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1849 | if (events & ALOOPER_EVENT_HANGUP || events & ALOOPER_EVENT_ERROR) { |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1850 | { |
| 1851 | // If the Looper encounters some error, set the flag mDead, reset mWakeLockRefCount, |
| 1852 | // and remove the fd from Looper. Call checkWakeLockState to know if SensorService |
| 1853 | // can release the wake-lock. |
| 1854 | ALOGD_IF(DEBUG_CONNECTIONS, "%p Looper error %d", this, fd); |
| 1855 | Mutex::Autolock _l(mConnectionLock); |
| 1856 | mDead = true; |
| 1857 | mWakeLockRefCount = 0; |
| 1858 | updateLooperRegistrationLocked(mService->getLooper()); |
| 1859 | } |
| 1860 | mService->checkWakeLockState(); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1861 | if (mDataInjectionMode) { |
| 1862 | // If the Looper has encountered some error in data injection mode, reset SensorService |
| 1863 | // back to normal mode. |
| 1864 | mService->resetToNormalMode(); |
| 1865 | mDataInjectionMode = false; |
| 1866 | } |
Aravind Akella | 8a96955 | 2014-09-28 17:52:41 -0700 | [diff] [blame] | 1867 | return 1; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | if (events & ALOOPER_EVENT_INPUT) { |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1871 | unsigned char buf[sizeof(sensors_event_t)]; |
| 1872 | ssize_t numBytesRead = ::recv(fd, buf, sizeof(buf), MSG_DONTWAIT); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1873 | { |
| 1874 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1875 | if (numBytesRead == sizeof(sensors_event_t)) { |
| 1876 | if (!mDataInjectionMode) { |
| 1877 | ALOGE("Data injected in normal mode, dropping event" |
| 1878 | "package=%s uid=%d", mPackageName.string(), mUid); |
| 1879 | // Unregister call backs. |
| 1880 | return 0; |
| 1881 | } |
| 1882 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1883 | sensors_event_t sensor_event; |
| 1884 | memset(&sensor_event, 0, sizeof(sensor_event)); |
| 1885 | memcpy(&sensor_event, buf, sizeof(sensors_event_t)); |
| 1886 | Sensor sensor = mService->getSensorFromHandle(sensor_event.sensor); |
| 1887 | sensor_event.type = sensor.getType(); |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1888 | dev.injectSensorData(&sensor_event); |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1889 | #if DEBUG_CONNECTIONS |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1890 | ++mEventsReceived; |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1891 | #endif |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1892 | } else if (numBytesRead == sizeof(uint32_t)) { |
| 1893 | uint32_t numAcks = 0; |
Aravind Akella | 08f04bf | 2015-05-08 15:59:23 -0700 | [diff] [blame] | 1894 | memcpy(&numAcks, buf, numBytesRead); |
Aravind Akella | a9e6cc3 | 2015-04-16 18:57:31 -0700 | [diff] [blame] | 1895 | // Sanity check to ensure there are no read errors in recv, numAcks is always |
| 1896 | // within the range and not zero. If any of the above don't hold reset |
| 1897 | // mWakeLockRefCount to zero. |
| 1898 | if (numAcks > 0 && numAcks < mWakeLockRefCount) { |
| 1899 | mWakeLockRefCount -= numAcks; |
| 1900 | } else { |
| 1901 | mWakeLockRefCount = 0; |
| 1902 | } |
| 1903 | #if DEBUG_CONNECTIONS |
| 1904 | mTotalAcksReceived += numAcks; |
| 1905 | #endif |
| 1906 | } else { |
| 1907 | // Read error, reset wakelock refcount. |
| 1908 | mWakeLockRefCount = 0; |
| 1909 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1910 | } |
| 1911 | // Check if wakelock can be released by sensorservice. mConnectionLock needs to be released |
| 1912 | // here as checkWakeLockState() will need it. |
| 1913 | if (mWakeLockRefCount == 0) { |
| 1914 | mService->checkWakeLockState(); |
| 1915 | } |
| 1916 | // continue getting callbacks. |
| 1917 | return 1; |
| 1918 | } |
| 1919 | |
| 1920 | if (events & ALOOPER_EVENT_OUTPUT) { |
Aravind Akella | b4373ac | 2014-10-29 17:55:20 -0700 | [diff] [blame] | 1921 | // send sensor data that is stored in mEventCache for this connection. |
| 1922 | mService->sendEventsFromCache(this); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1923 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1924 | return 1; |
| 1925 | } |
| 1926 | |
| 1927 | int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const { |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 1928 | size_t fifoWakeUpSensors = 0; |
| 1929 | size_t fifoNonWakeUpSensors = 0; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1930 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 1931 | const Sensor& sensor = mService->getSensorFromHandle(mSensorInfo.keyAt(i)); |
| 1932 | if (sensor.getFifoReservedEventCount() == sensor.getFifoMaxEventCount()) { |
| 1933 | // Each sensor has a reserved fifo. Sum up the fifo sizes for all wake up sensors and |
| 1934 | // non wake_up sensors. |
| 1935 | if (sensor.isWakeUpSensor()) { |
| 1936 | fifoWakeUpSensors += sensor.getFifoReservedEventCount(); |
| 1937 | } else { |
| 1938 | fifoNonWakeUpSensors += sensor.getFifoReservedEventCount(); |
| 1939 | } |
| 1940 | } else { |
| 1941 | // Shared fifo. Compute the max of the fifo sizes for wake_up and non_wake up sensors. |
| 1942 | if (sensor.isWakeUpSensor()) { |
| 1943 | fifoWakeUpSensors = fifoWakeUpSensors > sensor.getFifoMaxEventCount() ? |
| 1944 | fifoWakeUpSensors : sensor.getFifoMaxEventCount(); |
| 1945 | |
| 1946 | } else { |
| 1947 | fifoNonWakeUpSensors = fifoNonWakeUpSensors > sensor.getFifoMaxEventCount() ? |
| 1948 | fifoNonWakeUpSensors : sensor.getFifoMaxEventCount(); |
| 1949 | |
| 1950 | } |
| 1951 | } |
| 1952 | } |
| 1953 | if (fifoWakeUpSensors + fifoNonWakeUpSensors == 0) { |
| 1954 | // It is extremely unlikely that there is a write failure in non batch mode. Return a cache |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1955 | // size that is equal to that of the batch mode. |
Aravind Akella | e74baf6 | 2014-08-21 12:28:35 -0700 | [diff] [blame] | 1956 | // ALOGW("Write failure in non-batch mode"); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame] | 1957 | return MAX_SOCKET_BUFFER_SIZE_BATCHED/sizeof(sensors_event_t); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1958 | } |
| 1959 | return fifoWakeUpSensors + fifoNonWakeUpSensors; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1960 | } |
| 1961 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1962 | // --------------------------------------------------------------------------- |
| 1963 | }; // namespace android |
| 1964 | |