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 | |
| 34 | #include <binder/BinderService.h> |
Mathias Agopian | 451beee | 2010-07-19 15:03:55 -0700 | [diff] [blame] | 35 | #include <binder/IServiceManager.h> |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 36 | #include <binder/PermissionCache.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 37 | |
| 38 | #include <gui/ISensorServer.h> |
| 39 | #include <gui/ISensorEventConnection.h> |
Mathias Agopian | 907103b | 2012-04-02 18:38:02 -0700 | [diff] [blame] | 40 | #include <gui/SensorEventQueue.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 41 | |
| 42 | #include <hardware/sensors.h> |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 43 | #include <hardware_legacy/power.h> |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 44 | |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 45 | #include "BatteryService.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 46 | #include "CorrectedGyroSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 47 | #include "GravitySensor.h" |
| 48 | #include "LinearAccelerationSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 49 | #include "OrientationSensor.h" |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 50 | #include "RotationVectorSensor.h" |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 51 | #include "SensorFusion.h" |
| 52 | #include "SensorService.h" |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 53 | |
| 54 | namespace android { |
| 55 | // --------------------------------------------------------------------------- |
| 56 | |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Notes: |
| 59 | * |
| 60 | * - what about a gyro-corrected magnetic-field sensor? |
Mathias Agopian | 3301542 | 2011-05-27 18:18:13 -0700 | [diff] [blame] | 61 | * - run mag sensor from time to time to force calibration |
| 62 | * - gravity sensor length is wrong (=> drift in linear-acc sensor) |
| 63 | * |
| 64 | */ |
| 65 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 66 | const char* SensorService::WAKE_LOCK_NAME = "SensorService"; |
| 67 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 68 | SensorService::SensorService() |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 69 | : mInitCheck(NO_INIT) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 70 | { |
| 71 | } |
| 72 | |
| 73 | void SensorService::onFirstRef() |
| 74 | { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 75 | ALOGD("nuSensorService starting..."); |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 76 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 77 | SensorDevice& dev(SensorDevice::getInstance()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 78 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 79 | if (dev.initCheck() == NO_ERROR) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 80 | sensor_t const* list; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 81 | ssize_t count = dev.getSensorList(&list); |
| 82 | if (count > 0) { |
| 83 | ssize_t orientationIndex = -1; |
| 84 | bool hasGyro = false; |
| 85 | uint32_t virtualSensorsNeeds = |
| 86 | (1<<SENSOR_TYPE_GRAVITY) | |
| 87 | (1<<SENSOR_TYPE_LINEAR_ACCELERATION) | |
| 88 | (1<<SENSOR_TYPE_ROTATION_VECTOR); |
| 89 | |
| 90 | mLastEventSeen.setCapacity(count); |
| 91 | for (ssize_t i=0 ; i<count ; i++) { |
| 92 | registerSensor( new HardwareSensor(list[i]) ); |
| 93 | switch (list[i].type) { |
| 94 | case SENSOR_TYPE_ORIENTATION: |
| 95 | orientationIndex = i; |
| 96 | break; |
| 97 | case SENSOR_TYPE_GYROSCOPE: |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 98 | case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 99 | hasGyro = true; |
| 100 | break; |
| 101 | case SENSOR_TYPE_GRAVITY: |
| 102 | case SENSOR_TYPE_LINEAR_ACCELERATION: |
| 103 | case SENSOR_TYPE_ROTATION_VECTOR: |
| 104 | virtualSensorsNeeds &= ~(1<<list[i].type); |
| 105 | break; |
| 106 | } |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 107 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 108 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 109 | // it's safe to instantiate the SensorFusion object here |
| 110 | // (it wants to be instantiated after h/w sensors have been |
| 111 | // registered) |
| 112 | const SensorFusion& fusion(SensorFusion::getInstance()); |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 113 | |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 114 | // build the sensor list returned to users |
| 115 | mUserSensorList = mSensorList; |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 116 | |
| 117 | if (hasGyro) { |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 118 | Sensor aSensor; |
| 119 | |
| 120 | // Add Android virtual sensors if they're not already |
| 121 | // available in the HAL |
| 122 | |
| 123 | aSensor = registerVirtualSensor( new RotationVectorSensor() ); |
| 124 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) { |
| 125 | mUserSensorList.add(aSensor); |
| 126 | } |
| 127 | |
| 128 | aSensor = registerVirtualSensor( new GravitySensor(list, count) ); |
| 129 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_GRAVITY)) { |
| 130 | mUserSensorList.add(aSensor); |
| 131 | } |
| 132 | |
| 133 | aSensor = registerVirtualSensor( new LinearAccelerationSensor(list, count) ); |
| 134 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_LINEAR_ACCELERATION)) { |
| 135 | mUserSensorList.add(aSensor); |
| 136 | } |
| 137 | |
| 138 | aSensor = registerVirtualSensor( new OrientationSensor() ); |
| 139 | if (virtualSensorsNeeds & (1<<SENSOR_TYPE_ROTATION_VECTOR)) { |
| 140 | // if we are doing our own rotation-vector, also add |
| 141 | // the orientation sensor and remove the HAL provided one. |
| 142 | mUserSensorList.replaceAt(aSensor, orientationIndex); |
| 143 | } |
| 144 | |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 145 | // virtual debugging sensors are not added to mUserSensorList |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 146 | registerVirtualSensor( new CorrectedGyroSensor(list, count) ); |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 147 | registerVirtualSensor( new GyroDriftSensor() ); |
| 148 | } |
| 149 | |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 150 | // debugging sensor list |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 151 | mUserSensorListDebug = mSensorList; |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 152 | |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 153 | mSocketBufferSize = SOCKET_BUFFER_SIZE_NON_BATCHED; |
| 154 | FILE *fp = fopen("/proc/sys/net/core/wmem_max", "r"); |
| 155 | char line[128]; |
| 156 | if (fp != NULL && fgets(line, sizeof(line), fp) != NULL) { |
| 157 | line[sizeof(line) - 1] = '\0'; |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 158 | sscanf(line, "%zu", &mSocketBufferSize); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 159 | if (mSocketBufferSize > MAX_SOCKET_BUFFER_SIZE_BATCHED) { |
| 160 | mSocketBufferSize = MAX_SOCKET_BUFFER_SIZE_BATCHED; |
| 161 | } |
| 162 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 163 | if (fp) { |
| 164 | fclose(fp); |
| 165 | } |
| 166 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 167 | mWakeLockAcquired = false; |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 168 | run("SensorService", PRIORITY_URGENT_DISPLAY); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 169 | mLooper = new Looper(false); |
Mathias Agopian | 7b2b32f | 2011-07-14 16:39:46 -0700 | [diff] [blame] | 170 | mInitCheck = NO_ERROR; |
| 171 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 172 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 175 | Sensor SensorService::registerSensor(SensorInterface* s) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 176 | { |
| 177 | sensors_event_t event; |
| 178 | memset(&event, 0, sizeof(event)); |
| 179 | |
| 180 | const Sensor sensor(s->getSensor()); |
| 181 | // add to the sensor list (returned to clients) |
| 182 | mSensorList.add(sensor); |
| 183 | // add to our handle->SensorInterface mapping |
| 184 | mSensorMap.add(sensor.getHandle(), s); |
| 185 | // create an entry in the mLastEventSeen array |
| 186 | mLastEventSeen.add(sensor.getHandle(), event); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 187 | |
| 188 | return sensor; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 191 | Sensor SensorService::registerVirtualSensor(SensorInterface* s) |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 192 | { |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 193 | Sensor sensor = registerSensor(s); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 194 | mVirtualSensorList.add( s ); |
Mathias Agopian | 0319306 | 2013-05-10 19:32:39 -0700 | [diff] [blame] | 195 | return sensor; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 198 | SensorService::~SensorService() |
| 199 | { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 200 | for (size_t i=0 ; i<mSensorMap.size() ; i++) |
| 201 | delete mSensorMap.valueAt(i); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 204 | static const String16 sDump("android.permission.DUMP"); |
| 205 | |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 206 | status_t SensorService::dump(int fd, const Vector<String16>& /*args*/) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 207 | { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 208 | String8 result; |
Mathias Agopian | 1cb1346 | 2011-06-27 16:05:52 -0700 | [diff] [blame] | 209 | if (!PermissionCache::checkCallingPermission(sDump)) { |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 210 | result.appendFormat("Permission Denial: " |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 211 | "can't dump SensorService from pid=%d, uid=%d\n", |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 212 | IPCThreadState::self()->getCallingPid(), |
| 213 | IPCThreadState::self()->getCallingUid()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 214 | } else { |
| 215 | Mutex::Autolock _l(mLock); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 216 | result.append("Sensor List:\n"); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 217 | for (size_t i=0 ; i<mSensorList.size() ; i++) { |
| 218 | const Sensor& s(mSensorList[i]); |
| 219 | const sensors_event_t& e(mLastEventSeen.valueFor(s.getHandle())); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 220 | result.appendFormat( |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 221 | "%-15s| %-10s| %-20s| 0x%08x | \"%s\" | type=%d |", |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 222 | s.getName().string(), |
| 223 | s.getVendor().string(), |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 224 | s.getStringType().string(), |
| 225 | s.getHandle(), |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 226 | s.getRequiredPermission().string(), |
| 227 | s.getType()); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 228 | |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 229 | const int reportingMode = s.getReportingMode(); |
| 230 | if (reportingMode == AREPORTING_MODE_CONTINUOUS) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 231 | result.append(" continuous | "); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 232 | } else if (reportingMode == AREPORTING_MODE_ON_CHANGE) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 233 | result.append(" on-change | "); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 234 | } else if (reportingMode == AREPORTING_MODE_ONE_SHOT) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 235 | result.append(" one-shot | "); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 236 | } else { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 237 | result.append(" special-trigger | "); |
| 238 | } |
| 239 | |
| 240 | if (s.getMaxDelay() > 0) { |
| 241 | result.appendFormat("minRate=%.2fHz | ", 1e6f / s.getMaxDelay()); |
| 242 | } else { |
| 243 | result.appendFormat("maxDelay=%dus |", s.getMaxDelay()); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 244 | } |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 245 | |
| 246 | if (s.getMinDelay() > 0) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 247 | result.appendFormat("maxRate=%.2fHz | ", 1e6f / s.getMinDelay()); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 248 | } else { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 249 | result.appendFormat("minDelay=%dus |", s.getMinDelay()); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 252 | if (s.getFifoMaxEventCount() > 0) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 253 | result.appendFormat("FifoMax=%d events | ", |
| 254 | s.getFifoMaxEventCount()); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 255 | } else { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 256 | result.append("no batching | "); |
| 257 | } |
| 258 | |
| 259 | if (s.isWakeUpSensor()) { |
| 260 | result.appendFormat("wakeUp | "); |
| 261 | } else { |
| 262 | result.appendFormat("non-wakeUp | "); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 263 | } |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 264 | |
| 265 | switch (s.getType()) { |
| 266 | case SENSOR_TYPE_ROTATION_VECTOR: |
| 267 | case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR: |
| 268 | result.appendFormat( |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 269 | "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f, %" PRId64 ">\n", |
| 270 | e.data[0], e.data[1], e.data[2], e.data[3], e.data[4], e.timestamp); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 271 | break; |
| 272 | case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED: |
| 273 | case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: |
| 274 | result.appendFormat( |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 275 | "last=<%5.1f,%5.1f,%5.1f,%5.1f,%5.1f,%5.1f, %" PRId64 ">\n", |
| 276 | e.data[0], e.data[1], e.data[2], e.data[3], e.data[4], e.data[5], |
| 277 | e.timestamp); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 278 | break; |
| 279 | case SENSOR_TYPE_GAME_ROTATION_VECTOR: |
| 280 | result.appendFormat( |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 281 | "last=<%5.1f,%5.1f,%5.1f,%5.1f, %" PRId64 ">\n", |
| 282 | e.data[0], e.data[1], e.data[2], e.data[3], e.timestamp); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 283 | break; |
| 284 | case SENSOR_TYPE_SIGNIFICANT_MOTION: |
| 285 | case SENSOR_TYPE_STEP_DETECTOR: |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 286 | result.appendFormat( "last=<%f %" PRId64 ">\n", e.data[0], e.timestamp); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 287 | break; |
| 288 | case SENSOR_TYPE_STEP_COUNTER: |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 289 | result.appendFormat( "last=<%" PRIu64 ", %" PRId64 ">\n", e.u64.step_counter, |
| 290 | e.timestamp); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 291 | break; |
| 292 | default: |
| 293 | // default to 3 values |
| 294 | result.appendFormat( |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 295 | "last=<%5.1f,%5.1f,%5.1f, %" PRId64 ">\n", |
| 296 | e.data[0], e.data[1], e.data[2], e.timestamp); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 297 | break; |
| 298 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 299 | result.append("\n"); |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 300 | } |
| 301 | SensorFusion::getInstance().dump(result); |
| 302 | SensorDevice::getInstance().dump(result); |
| 303 | |
Mathias Agopian | ba02cd2 | 2013-07-03 16:20:57 -0700 | [diff] [blame] | 304 | result.append("Active sensors:\n"); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 305 | for (size_t i=0 ; i<mActiveSensors.size() ; i++) { |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 306 | int handle = mActiveSensors.keyAt(i); |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 307 | result.appendFormat("%s (handle=0x%08x, connections=%zu)\n", |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 308 | getSensorName(handle).string(), |
| 309 | handle, |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 310 | mActiveSensors.valueAt(i)->getNumConnections()); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 311 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 312 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 313 | result.appendFormat("Max Socket Buffer size = %d events\n", |
| 314 | mSocketBufferSize/sizeof(sensors_event_t)); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 315 | result.appendFormat("WakeLock Status: %s \n", mWakeLockAcquired ? "acquired" : "not held"); |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 316 | result.appendFormat("%zd active connections\n", mActiveConnections.size()); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 317 | |
| 318 | for (size_t i=0 ; i < mActiveConnections.size() ; i++) { |
| 319 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
| 320 | if (connection != 0) { |
Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 321 | result.appendFormat("Connection Number: %zu \n", i); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 322 | connection->dump(result); |
| 323 | } |
| 324 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 325 | } |
| 326 | write(fd, result.string(), result.size()); |
| 327 | return NO_ERROR; |
| 328 | } |
| 329 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 330 | void SensorService::cleanupAutoDisabledSensorLocked(const sp<SensorEventConnection>& connection, |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 331 | sensors_event_t const* buffer, const int count) { |
| 332 | for (int i=0 ; i<count ; i++) { |
| 333 | int handle = buffer[i].sensor; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 334 | if (connection->hasSensor(handle)) { |
| 335 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 336 | // If this buffer has an event from a one_shot sensor and this connection is registered |
| 337 | // for this particular one_shot sensor, try cleaning up the connection. |
| 338 | if (sensor != NULL && |
| 339 | sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 340 | sensor->autoDisable(connection.get(), handle); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 341 | cleanupWithoutDisableLocked(connection, handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 347 | bool SensorService::threadLoop() |
| 348 | { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 349 | ALOGD("nuSensorService thread starting..."); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 350 | |
Mathias Agopian | 90ed3e8 | 2013-09-09 23:36:25 -0700 | [diff] [blame] | 351 | // each virtual sensor could generate an event per "real" event, that's why we need |
| 352 | // to size numEventMax much smaller than MAX_RECEIVE_BUFFER_EVENT_COUNT. |
| 353 | // in practice, this is too aggressive, but guaranteed to be enough. |
| 354 | const size_t minBufferSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT; |
| 355 | const size_t numEventMax = minBufferSize / (1 + mVirtualSensorList.size()); |
| 356 | |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 357 | sensors_event_t buffer[minBufferSize]; |
| 358 | sensors_event_t scratch[minBufferSize]; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 359 | SensorDevice& device(SensorDevice::getInstance()); |
| 360 | const size_t vcount = mVirtualSensorList.size(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 361 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 362 | SensorEventAckReceiver sender(this); |
| 363 | sender.run("SensorEventAckReceiver", PRIORITY_URGENT_DISPLAY); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 364 | ssize_t count; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 365 | const int halVersion = device.getHalDeviceVersion(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 366 | do { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 367 | count = device.poll(buffer, numEventMax); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 368 | if (count<0) { |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 369 | ALOGE("sensor poll failed (%s)", strerror(-count)); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 370 | break; |
| 371 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 372 | |
| 373 | // Reset sensors_event_t.flags to zero for all events in the buffer. |
| 374 | for (int i = 0; i < count; i++) { |
| 375 | buffer[i].flags = 0; |
| 376 | } |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 377 | Mutex::Autolock _l(mLock); |
| 378 | // Poll has returned. Hold a wakelock if one of the events is from a wake up sensor. The |
| 379 | // rest of this loop is under a critical section protected by mLock. Acquiring a wakeLock, |
| 380 | // sending events to clients (incrementing SensorEventConnection::mWakeLockRefCount) should |
| 381 | // not be interleaved with decrementing SensorEventConnection::mWakeLockRefCount and |
| 382 | // releasing the wakelock. |
| 383 | bool bufferHasWakeUpEvent = false; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 384 | for (int i = 0; i < count; i++) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 385 | if (isWakeUpSensorEvent(buffer[i])) { |
| 386 | bufferHasWakeUpEvent = true; |
| 387 | break; |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 391 | if (bufferHasWakeUpEvent && !mWakeLockAcquired) { |
| 392 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME); |
| 393 | mWakeLockAcquired = true; |
| 394 | ALOGD_IF(DEBUG_CONNECTIONS, "acquired wakelock %s", WAKE_LOCK_NAME); |
| 395 | } |
| 396 | recordLastValueLocked(buffer, count); |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 397 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 398 | // handle virtual sensors |
| 399 | if (count && vcount) { |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 400 | sensors_event_t const * const event = buffer; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 401 | const size_t activeVirtualSensorCount = mActiveVirtualSensors.size(); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 402 | if (activeVirtualSensorCount) { |
| 403 | size_t k = 0; |
Mathias Agopian | 984826c | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 404 | SensorFusion& fusion(SensorFusion::getInstance()); |
| 405 | if (fusion.isEnabled()) { |
| 406 | for (size_t i=0 ; i<size_t(count) ; i++) { |
| 407 | fusion.process(event[i]); |
| 408 | } |
| 409 | } |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 410 | for (size_t i=0 ; i<size_t(count) && k<minBufferSize ; i++) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 411 | for (size_t j=0 ; j<activeVirtualSensorCount ; j++) { |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 412 | if (count + k >= minBufferSize) { |
| 413 | ALOGE("buffer too small to hold all events: " |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 414 | "count=%zd, k=%zu, size=%zu", |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 415 | count, k, minBufferSize); |
| 416 | break; |
| 417 | } |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 418 | sensors_event_t out; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 419 | SensorInterface* si = mActiveVirtualSensors.valueAt(j); |
Mathias Agopian | d1920ff | 2012-05-29 19:46:14 -0700 | [diff] [blame] | 420 | if (si->process(&out, event[i])) { |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 421 | buffer[count + k] = out; |
| 422 | k++; |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | if (k) { |
| 427 | // record the last synthesized values |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 428 | recordLastValueLocked(&buffer[count], k); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 429 | count += k; |
| 430 | // sort the buffer by time-stamps |
| 431 | sortEventBuffer(buffer, count); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 436 | // handle backward compatibility for RotationVector sensor |
| 437 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_0) { |
| 438 | for (int i = 0; i < count; i++) { |
Mathias Agopian | 7438fd1 | 2013-07-08 12:50:39 -0700 | [diff] [blame] | 439 | if (buffer[i].type == SENSOR_TYPE_ROTATION_VECTOR) { |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 440 | // All the 4 components of the quaternion should be available |
| 441 | // No heading accuracy. Set it to -1 |
| 442 | buffer[i].data[4] = -1; |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 447 | // Send our events to clients. Check the state of wake lock for each client and release the |
| 448 | // lock if none of the clients need it. |
| 449 | bool needsWakeLock = false; |
| 450 | for (size_t i=0 ; i < mActiveConnections.size(); i++) { |
| 451 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 452 | if (connection != 0) { |
| 453 | connection->sendEvents(buffer, count, scratch); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 454 | needsWakeLock |= connection->needsWakeLock(); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 455 | // Some sensors need to be auto disabled after the trigger |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 456 | cleanupAutoDisabledSensorLocked(connection, buffer, count); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 457 | } |
| 458 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 459 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 460 | if (mWakeLockAcquired && !needsWakeLock) { |
| 461 | release_wake_lock(WAKE_LOCK_NAME); |
| 462 | mWakeLockAcquired = false; |
| 463 | ALOGD_IF(DEBUG_CONNECTIONS, "released wakelock %s", WAKE_LOCK_NAME); |
| 464 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 465 | } while (count >= 0 || Thread::exitPending()); |
| 466 | |
Steve Block | 3c20fbe | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 467 | ALOGW("Exiting SensorService::threadLoop => aborting..."); |
Mathias Agopian | 1a62301 | 2011-11-09 17:50:15 -0800 | [diff] [blame] | 468 | abort(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 469 | return false; |
| 470 | } |
| 471 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 472 | sp<Looper> SensorService::getLooper() const { |
| 473 | return mLooper; |
| 474 | } |
| 475 | |
| 476 | bool SensorService::SensorEventAckReceiver::threadLoop() { |
| 477 | ALOGD("new thread SensorEventAckReceiver"); |
| 478 | do { |
| 479 | sp<Looper> looper = mService->getLooper(); |
| 480 | looper->pollOnce(-1); |
| 481 | } while(!Thread::exitPending()); |
| 482 | return false; |
| 483 | } |
| 484 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 485 | void SensorService::recordLastValueLocked( |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 486 | const sensors_event_t* buffer, size_t count) { |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 487 | const sensors_event_t* last = NULL; |
| 488 | for (size_t i = 0; i < count; i++) { |
| 489 | const sensors_event_t* event = &buffer[i]; |
| 490 | if (event->type != SENSOR_TYPE_META_DATA) { |
| 491 | if (last && event->sensor != last->sensor) { |
| 492 | mLastEventSeen.editValueFor(last->sensor) = *last; |
| 493 | } |
| 494 | last = event; |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 495 | } |
| 496 | } |
Aravind Akella | 4b84704 | 2014-03-03 19:02:46 -0800 | [diff] [blame] | 497 | if (last) { |
| 498 | mLastEventSeen.editValueFor(last->sensor) = *last; |
| 499 | } |
Mathias Agopian | 94e8f68 | 2010-11-10 17:50:28 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 502 | void SensorService::sortEventBuffer(sensors_event_t* buffer, size_t count) |
| 503 | { |
| 504 | struct compar { |
| 505 | static int cmp(void const* lhs, void const* rhs) { |
| 506 | sensors_event_t const* l = static_cast<sensors_event_t const*>(lhs); |
| 507 | sensors_event_t const* r = static_cast<sensors_event_t const*>(rhs); |
Mathias Agopian | a5c106a | 2012-04-19 18:18:24 -0700 | [diff] [blame] | 508 | return l->timestamp - r->timestamp; |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 509 | } |
| 510 | }; |
| 511 | qsort(buffer, count, sizeof(sensors_event_t), compar::cmp); |
| 512 | } |
| 513 | |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 514 | String8 SensorService::getSensorName(int handle) const { |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 515 | size_t count = mUserSensorList.size(); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 516 | for (size_t i=0 ; i<count ; i++) { |
Mathias Agopian | 010e422 | 2011-06-08 20:06:50 -0700 | [diff] [blame] | 517 | const Sensor& sensor(mUserSensorList[i]); |
Mathias Agopian | 5d27072 | 2010-07-19 15:20:39 -0700 | [diff] [blame] | 518 | if (sensor.getHandle() == handle) { |
| 519 | return sensor.getName(); |
| 520 | } |
| 521 | } |
| 522 | String8 result("unknown"); |
| 523 | return result; |
| 524 | } |
| 525 | |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 526 | bool SensorService::isVirtualSensor(int handle) const { |
| 527 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 528 | return sensor->isVirtual(); |
| 529 | } |
| 530 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 531 | bool SensorService::isWakeUpSensorEvent(const sensors_event_t& event) const { |
Sean Wan | 7869e22 | 2014-07-14 17:07:33 -0700 | [diff] [blame] | 532 | int handle = event.sensor; |
| 533 | if (event.type == SENSOR_TYPE_META_DATA) { |
| 534 | handle = event.meta_data.sensor; |
| 535 | } |
| 536 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 537 | return sensor != NULL && sensor->getSensor().isWakeUpSensor(); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 540 | |
| 541 | SensorService::SensorRecord * SensorService::getSensorRecord(int handle) { |
| 542 | return mActiveSensors.valueFor(handle); |
| 543 | } |
| 544 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 545 | Vector<Sensor> SensorService::getSensorList() |
| 546 | { |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 547 | char value[PROPERTY_VALUE_MAX]; |
| 548 | property_get("debug.sensors", value, "0"); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 549 | const Vector<Sensor>& initialSensorList = (atoi(value)) ? |
| 550 | mUserSensorListDebug : mUserSensorList; |
| 551 | Vector<Sensor> accessibleSensorList; |
| 552 | for (size_t i = 0; i < initialSensorList.size(); i++) { |
| 553 | Sensor sensor = initialSensorList[i]; |
| 554 | if (canAccessSensor(sensor)) { |
| 555 | accessibleSensorList.add(sensor); |
| 556 | } else { |
| 557 | String8 infoMessage; |
| 558 | infoMessage.appendFormat( |
| 559 | "Skipped sensor %s because it requires permission %s", |
| 560 | sensor.getName().string(), |
| 561 | sensor.getRequiredPermission().string()); |
| 562 | ALOGI(infoMessage.string()); |
| 563 | } |
Mathias Agopian | 3326486 | 2012-06-28 19:46:54 -0700 | [diff] [blame] | 564 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 565 | return accessibleSensorList; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | sp<ISensorEventConnection> SensorService::createSensorEventConnection() |
| 569 | { |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 570 | uid_t uid = IPCThreadState::self()->getCallingUid(); |
| 571 | sp<SensorEventConnection> result(new SensorEventConnection(this, uid)); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 572 | return result; |
| 573 | } |
| 574 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 575 | void SensorService::cleanupConnection(SensorEventConnection* c) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 576 | { |
| 577 | Mutex::Autolock _l(mLock); |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 578 | const wp<SensorEventConnection> connection(c); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 579 | size_t size = mActiveSensors.size(); |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 580 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu active sensors", size); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 581 | for (size_t i=0 ; i<size ; ) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 582 | int handle = mActiveSensors.keyAt(i); |
| 583 | if (c->hasSensor(handle)) { |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 584 | ALOGD_IF(DEBUG_CONNECTIONS, "%zu: disabling handle=0x%08x", i, handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 585 | SensorInterface* sensor = mSensorMap.valueFor( handle ); |
Steve Block | f5a1230 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 586 | ALOGE_IF(!sensor, "mSensorMap[handle=0x%08x] is null!", handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 587 | if (sensor) { |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 588 | sensor->activate(c, false); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 589 | } |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 590 | } |
| 591 | SensorRecord* rec = mActiveSensors.valueAt(i); |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 592 | ALOGE_IF(!rec, "mActiveSensors[%zu] is null (handle=0x%08x)!", i, handle); |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 593 | ALOGD_IF(DEBUG_CONNECTIONS, |
Mark Salyzyn | db45861 | 2014-06-10 14:50:02 -0700 | [diff] [blame] | 594 | "removing connection %p for sensor[%zu].handle=0x%08x", |
Mathias Agopian | a1b7db9 | 2011-05-27 16:23:58 -0700 | [diff] [blame] | 595 | c, i, handle); |
| 596 | |
Mathias Agopian | db5b4bc | 2011-02-03 14:52:47 -0800 | [diff] [blame] | 597 | if (rec && rec->removeConnection(connection)) { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 598 | ALOGD_IF(DEBUG_CONNECTIONS, "... and it was the last connection"); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 599 | mActiveSensors.removeItemsAt(i, 1); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 600 | mActiveVirtualSensors.removeItem(handle); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 601 | delete rec; |
| 602 | size--; |
| 603 | } else { |
| 604 | i++; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 605 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 606 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 607 | mActiveConnections.remove(connection); |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 608 | BatteryService::cleanup(c->getUid()); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 609 | if (c->needsWakeLock()) { |
| 610 | checkWakeLockStateLocked(); |
| 611 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 612 | } |
| 613 | |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 614 | Sensor SensorService::getSensorFromHandle(int handle) const { |
| 615 | return mSensorMap.valueFor(handle)->getSensor(); |
| 616 | } |
| 617 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 618 | status_t SensorService::enable(const sp<SensorEventConnection>& connection, |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 619 | int handle, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, int reservedFlags) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 620 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 621 | if (mInitCheck != NO_ERROR) |
| 622 | return mInitCheck; |
| 623 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 624 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 625 | if (sensor == NULL) { |
| 626 | return BAD_VALUE; |
| 627 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 628 | |
| 629 | if (!verifyCanAccessSensor(sensor->getSensor(), "Tried enabling")) { |
| 630 | return BAD_VALUE; |
| 631 | } |
| 632 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 633 | Mutex::Autolock _l(mLock); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 634 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
| 635 | if (rec == 0) { |
| 636 | rec = new SensorRecord(connection); |
| 637 | mActiveSensors.add(handle, rec); |
| 638 | if (sensor->isVirtual()) { |
| 639 | mActiveVirtualSensors.add(handle, sensor); |
| 640 | } |
| 641 | } else { |
| 642 | if (rec->addConnection(connection)) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 643 | // this sensor is already activated, but we are adding a connection that uses it. |
| 644 | // 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] | 645 | // "continuous" sensor. |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 646 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ON_CHANGE) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 647 | // NOTE: The wake_up flag of this event may get set to |
| 648 | // WAKE_UP_SENSOR_EVENT_NEEDS_ACK if this is a wake_up event. |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 649 | sensors_event_t& event(mLastEventSeen.editValueFor(handle)); |
| 650 | if (event.version == sizeof(sensors_event_t)) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 651 | if (isWakeUpSensorEvent(event) && !mWakeLockAcquired) { |
| 652 | acquire_wake_lock(PARTIAL_WAKE_LOCK, WAKE_LOCK_NAME); |
| 653 | mWakeLockAcquired = true; |
| 654 | ALOGD_IF(DEBUG_CONNECTIONS, "acquired wakelock for on_change sensor %s", |
| 655 | WAKE_LOCK_NAME); |
| 656 | } |
| 657 | connection->sendEvents(&event, 1, NULL); |
| 658 | if (!connection->needsWakeLock() && mWakeLockAcquired) { |
| 659 | checkWakeLockStateLocked(); |
| 660 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | if (connection->addSensor(handle)) { |
| 667 | BatteryService::enableSensor(connection->getUid(), handle); |
| 668 | // the sensor was added (which means it wasn't already there) |
| 669 | // so, see if this connection becomes active |
| 670 | if (mActiveConnections.indexOf(connection) < 0) { |
| 671 | mActiveConnections.add(connection); |
| 672 | } |
| 673 | } else { |
| 674 | ALOGW("sensor %08x already enabled in connection %p (ignoring)", |
| 675 | handle, connection.get()); |
| 676 | } |
| 677 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 678 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 679 | if (samplingPeriodNs < minDelayNs) { |
| 680 | samplingPeriodNs = minDelayNs; |
| 681 | } |
| 682 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 683 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling batch handle==%d flags=%d" |
| 684 | "rate=%" PRId64 " timeout== %" PRId64"", |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 685 | handle, reservedFlags, samplingPeriodNs, maxBatchReportLatencyNs); |
| 686 | |
| 687 | status_t err = sensor->batch(connection.get(), handle, reservedFlags, samplingPeriodNs, |
| 688 | maxBatchReportLatencyNs); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 689 | |
| 690 | if (err == NO_ERROR && sensor->getSensor().getReportingMode() != AREPORTING_MODE_ONE_SHOT) { |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 691 | status_t err_flush = sensor->flush(connection.get(), handle); |
| 692 | // Flush may return error if the sensor is not activated or the underlying h/w sensor does |
| 693 | // not support flush. |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 694 | if (err_flush == NO_ERROR) { |
| 695 | connection->setFirstFlushPending(handle, true); |
| 696 | rec->addPendingFlushConnection(connection.get()); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 697 | } |
| 698 | } |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 699 | |
| 700 | if (err == NO_ERROR) { |
| 701 | ALOGD_IF(DEBUG_CONNECTIONS, "Calling activate on %d", handle); |
| 702 | err = sensor->activate(connection.get(), true); |
| 703 | } |
| 704 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 705 | if (err == NO_ERROR && sensor->getSensor().isWakeUpSensor()) { |
| 706 | // Add the file descriptor to the Looper for receiving acknowledgments; |
| 707 | int ret = mLooper->addFd(connection->getSensorChannel()->getSendFd(), 0, |
| 708 | ALOOPER_EVENT_INPUT, connection.get(), NULL); |
| 709 | } |
| 710 | |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 711 | if (err != NO_ERROR) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 712 | // batch/activate has failed, reset our state. |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 713 | cleanupWithoutDisableLocked(connection, handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 714 | } |
| 715 | return err; |
| 716 | } |
| 717 | |
| 718 | status_t SensorService::disable(const sp<SensorEventConnection>& connection, |
| 719 | int handle) |
| 720 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 721 | if (mInitCheck != NO_ERROR) |
| 722 | return mInitCheck; |
| 723 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 724 | Mutex::Autolock _l(mLock); |
| 725 | status_t err = cleanupWithoutDisableLocked(connection, handle); |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 726 | if (err == NO_ERROR) { |
| 727 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 728 | err = sensor ? sensor->activate(connection.get(), false) : status_t(BAD_VALUE); |
| 729 | } |
| 730 | return err; |
| 731 | } |
| 732 | |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 733 | status_t SensorService::cleanupWithoutDisable( |
| 734 | const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 735 | Mutex::Autolock _l(mLock); |
Mathias Agopian | ac9a96d | 2013-07-12 02:01:16 -0700 | [diff] [blame] | 736 | return cleanupWithoutDisableLocked(connection, handle); |
| 737 | } |
| 738 | |
| 739 | status_t SensorService::cleanupWithoutDisableLocked( |
| 740 | const sp<SensorEventConnection>& connection, int handle) { |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 741 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 742 | if (rec) { |
| 743 | // see if this connection becomes inactive |
Mathias Agopian | 787ac1b | 2012-09-18 18:49:18 -0700 | [diff] [blame] | 744 | if (connection->removeSensor(handle)) { |
| 745 | BatteryService::disableSensor(connection->getUid(), handle); |
| 746 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 747 | if (connection->hasAnySensor() == false) { |
| 748 | mActiveConnections.remove(connection); |
| 749 | } |
| 750 | // see if this sensor becomes inactive |
| 751 | if (rec->removeConnection(connection)) { |
| 752 | mActiveSensors.removeItem(handle); |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 753 | mActiveVirtualSensors.removeItem(handle); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 754 | delete rec; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 755 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 756 | return NO_ERROR; |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 757 | } |
Jaikumar Ganesh | 4342fdf | 2013-04-08 16:43:12 -0700 | [diff] [blame] | 758 | return BAD_VALUE; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 761 | status_t SensorService::setEventRate(const sp<SensorEventConnection>& connection, |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 762 | int handle, nsecs_t ns) |
| 763 | { |
Mathias Agopian | 50df295 | 2010-07-19 19:09:10 -0700 | [diff] [blame] | 764 | if (mInitCheck != NO_ERROR) |
| 765 | return mInitCheck; |
| 766 | |
Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 767 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 768 | if (!sensor) |
| 769 | return BAD_VALUE; |
| 770 | |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 771 | if (!verifyCanAccessSensor(sensor->getSensor(), "Tried configuring")) { |
| 772 | return BAD_VALUE; |
| 773 | } |
| 774 | |
Mathias Agopian | 1cd7000 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 775 | if (ns < 0) |
| 776 | return BAD_VALUE; |
| 777 | |
Mathias Agopian | 62569ec | 2011-11-07 21:21:47 -0800 | [diff] [blame] | 778 | nsecs_t minDelayNs = sensor->getSensor().getMinDelayNs(); |
| 779 | if (ns < minDelayNs) { |
| 780 | ns = minDelayNs; |
Mathias Agopian | ae09d65 | 2011-11-01 17:37:49 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Mathias Agopian | f001c92 | 2010-11-11 17:58:51 -0800 | [diff] [blame] | 783 | return sensor->setDelay(connection.get(), handle, ns); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 786 | status_t SensorService::flushSensor(const sp<SensorEventConnection>& connection, |
| 787 | int handle) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 788 | if (mInitCheck != NO_ERROR) return mInitCheck; |
| 789 | SensorInterface* sensor = mSensorMap.valueFor(handle); |
| 790 | if (sensor == NULL) { |
| 791 | return BAD_VALUE; |
| 792 | } |
| 793 | |
| 794 | if (!verifyCanAccessSensor(sensor->getSensor(), "Tried flushing")) { |
| 795 | return BAD_VALUE; |
| 796 | } |
| 797 | |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 798 | if (sensor->getSensor().getReportingMode() == AREPORTING_MODE_ONE_SHOT) { |
| 799 | ALOGE("flush called on a one-shot sensor"); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 800 | return INVALID_OPERATION; |
| 801 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 802 | |
| 803 | status_t ret = sensor->flush(connection.get(), handle); |
| 804 | if (ret == NO_ERROR) { |
| 805 | SensorRecord* rec = mActiveSensors.valueFor(handle); |
| 806 | if (rec != NULL) rec->addPendingFlushConnection(connection); |
| 807 | } |
| 808 | return ret; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 809 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 810 | |
| 811 | |
| 812 | bool SensorService::canAccessSensor(const Sensor& sensor) { |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 813 | return (sensor.getRequiredPermission().isEmpty()) || |
| 814 | PermissionCache::checkCallingPermission(String16(sensor.getRequiredPermission())); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | bool SensorService::verifyCanAccessSensor(const Sensor& sensor, const char* operation) { |
| 818 | if (canAccessSensor(sensor)) { |
| 819 | return true; |
| 820 | } else { |
| 821 | String8 errorMessage; |
| 822 | errorMessage.appendFormat( |
| 823 | "%s a sensor (%s) without holding its required permission: %s", |
| 824 | operation, |
| 825 | sensor.getName().string(), |
| 826 | sensor.getRequiredPermission().string()); |
| 827 | return false; |
| 828 | } |
| 829 | } |
| 830 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 831 | void SensorService::checkWakeLockState() { |
| 832 | Mutex::Autolock _l(mLock); |
| 833 | checkWakeLockStateLocked(); |
| 834 | } |
| 835 | |
| 836 | void SensorService::checkWakeLockStateLocked() { |
| 837 | if (!mWakeLockAcquired) { |
| 838 | return; |
| 839 | } |
| 840 | bool releaseLock = true; |
| 841 | for (size_t i=0 ; i<mActiveConnections.size() ; i++) { |
| 842 | sp<SensorEventConnection> connection(mActiveConnections[i].promote()); |
| 843 | if (connection != 0) { |
| 844 | if (connection->needsWakeLock()) { |
| 845 | releaseLock = false; |
| 846 | break; |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | if (releaseLock) { |
| 851 | ALOGD_IF(DEBUG_CONNECTIONS, "releasing wakelock %s", WAKE_LOCK_NAME); |
| 852 | release_wake_lock(WAKE_LOCK_NAME); |
| 853 | mWakeLockAcquired = false; |
| 854 | } |
| 855 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 856 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 857 | // --------------------------------------------------------------------------- |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 858 | SensorService::SensorRecord::SensorRecord( |
| 859 | const sp<SensorEventConnection>& connection) |
| 860 | { |
| 861 | mConnections.add(connection); |
| 862 | } |
| 863 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 864 | bool SensorService::SensorRecord::addConnection( |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 865 | const sp<SensorEventConnection>& connection) |
| 866 | { |
| 867 | if (mConnections.indexOf(connection) < 0) { |
| 868 | mConnections.add(connection); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 869 | return true; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 870 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 871 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | bool SensorService::SensorRecord::removeConnection( |
| 875 | const wp<SensorEventConnection>& connection) |
| 876 | { |
| 877 | ssize_t index = mConnections.indexOf(connection); |
| 878 | if (index >= 0) { |
| 879 | mConnections.removeItemsAt(index, 1); |
| 880 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 881 | // Remove this connections from the queue of flush() calls made on this sensor. |
| 882 | for (Vector< wp<SensorEventConnection> >::iterator it = |
| 883 | mPendingFlushConnections.begin(); it != mPendingFlushConnections.end();) { |
| 884 | if (it->unsafe_get() == connection.unsafe_get()) { |
| 885 | it = mPendingFlushConnections.erase(it); |
| 886 | } else { |
| 887 | ++it; |
| 888 | } |
| 889 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 890 | return mConnections.size() ? false : true; |
| 891 | } |
| 892 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 893 | void SensorService::SensorRecord::addPendingFlushConnection( |
| 894 | const sp<SensorEventConnection>& connection) { |
| 895 | mPendingFlushConnections.add(connection); |
| 896 | } |
| 897 | |
| 898 | void SensorService::SensorRecord::removeFirstPendingFlushConnection() { |
| 899 | if (mPendingFlushConnections.size() > 0) { |
| 900 | mPendingFlushConnections.removeAt(0); |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | SensorService::SensorEventConnection * |
| 905 | SensorService::SensorRecord::getFirstPendingFlushConnection() { |
| 906 | if (mPendingFlushConnections.size() > 0) { |
| 907 | return mPendingFlushConnections[0].unsafe_get(); |
| 908 | } |
| 909 | return NULL; |
| 910 | } |
| 911 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 912 | // --------------------------------------------------------------------------- |
| 913 | |
| 914 | SensorService::SensorEventConnection::SensorEventConnection( |
Mathias Agopian | 5307d17 | 2012-09-18 17:02:43 -0700 | [diff] [blame] | 915 | const sp<SensorService>& service, uid_t uid) |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 916 | : mService(service), mUid(uid), mWakeLockRefCount(0), mEventCache(NULL), mCacheSize(0), |
| 917 | mMaxCacheSize(0) { |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 918 | const SensorDevice& device(SensorDevice::getInstance()); |
| 919 | if (device.getHalDeviceVersion() >= SENSORS_DEVICE_API_VERSION_1_1) { |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 920 | // Increase socket buffer size to a max of 100 KB for batching capabilities. |
| 921 | mChannel = new BitTube(mService->mSocketBufferSize); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 922 | } else { |
| 923 | mChannel = new BitTube(SOCKET_BUFFER_SIZE_NON_BATCHED); |
| 924 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 925 | #if DEBUG_CONNECTIONS |
| 926 | mEventsReceived = mEventsSentFromCache = mEventsSent = 0; |
| 927 | #endif |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 930 | SensorService::SensorEventConnection::~SensorEventConnection() { |
Steve Block | a551237 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 931 | ALOGD_IF(DEBUG_CONNECTIONS, "~SensorEventConnection(%p)", this); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 932 | if (mEventCache != NULL) { |
| 933 | delete mEventCache; |
| 934 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 935 | mService->cleanupConnection(this); |
| 936 | } |
| 937 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 938 | void SensorService::SensorEventConnection::onFirstRef() { |
| 939 | LooperCallback::onFirstRef(); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 942 | bool SensorService::SensorEventConnection::needsWakeLock() { |
| 943 | Mutex::Autolock _l(mConnectionLock); |
| 944 | return mWakeLockRefCount > 0; |
| 945 | } |
| 946 | |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 947 | void SensorService::SensorEventConnection::dump(String8& result) { |
| 948 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 949 | result.appendFormat("\t %d WakeLockRefCount \n", mWakeLockRefCount); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 950 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 951 | const FlushInfo& flushInfo = mSensorInfo.valueAt(i); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 952 | result.appendFormat("\t %s 0x%08x | status: %s | pending flush events %d | uid %d|" |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 953 | "cache size: %d max cache size %d\n", |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 954 | mService->getSensorName(mSensorInfo.keyAt(i)).string(), |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 955 | mSensorInfo.keyAt(i), |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 956 | flushInfo.mFirstFlushPending ? "First flush pending" : |
| 957 | "active", |
Patrick Tjin | eefced1 | 2014-02-05 15:06:03 -0800 | [diff] [blame] | 958 | flushInfo.mPendingFlushEventsToSend, |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 959 | mUid, |
| 960 | mCacheSize, |
| 961 | mMaxCacheSize); |
| 962 | #if DEBUG_CONNECTIONS |
| 963 | result.appendFormat("\t events recvd: %d | sent %d | cache %d | dropped %d\n", |
| 964 | mEventsReceived, |
| 965 | mEventsSent, |
| 966 | mEventsSentFromCache, |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 967 | mEventsReceived - (mEventsSentFromCache |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 968 | mEventsSent + mCacheSize)); |
| 969 | #endif |
| 970 | |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 974 | bool SensorService::SensorEventConnection::addSensor(int32_t handle) { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 975 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 976 | if (!verifyCanAccessSensor(mService->getSensorFromHandle(handle), "Tried adding")) { |
| 977 | return false; |
| 978 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 979 | if (mSensorInfo.indexOfKey(handle) < 0) { |
| 980 | mSensorInfo.add(handle, FlushInfo()); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 981 | return true; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 982 | } |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 983 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 984 | } |
| 985 | |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 986 | bool SensorService::SensorEventConnection::removeSensor(int32_t handle) { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 987 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 988 | if (mSensorInfo.removeItem(handle) >= 0) { |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 989 | return true; |
| 990 | } |
| 991 | return false; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | bool SensorService::SensorEventConnection::hasSensor(int32_t handle) const { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 995 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 996 | return mSensorInfo.indexOfKey(handle) >= 0; |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | bool SensorService::SensorEventConnection::hasAnySensor() const { |
Mathias Agopian | 71d7a5c | 2010-11-14 20:55:25 -0800 | [diff] [blame] | 1000 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | 7c1c531 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 1001 | return mSensorInfo.size() ? true : false; |
| 1002 | } |
| 1003 | |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1004 | void SensorService::SensorEventConnection::setFirstFlushPending(int32_t handle, |
| 1005 | bool value) { |
| 1006 | Mutex::Autolock _l(mConnectionLock); |
| 1007 | ssize_t index = mSensorInfo.indexOfKey(handle); |
| 1008 | if (index >= 0) { |
| 1009 | FlushInfo& flushInfo = mSensorInfo.editValueAt(index); |
| 1010 | flushInfo.mFirstFlushPending = value; |
| 1011 | } |
| 1012 | } |
| 1013 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1014 | status_t SensorService::SensorEventConnection::sendEvents( |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 1015 | sensors_event_t const* buffer, size_t numEvents, |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1016 | sensors_event_t* scratch) { |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 1017 | // filter out events not for this connection |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 1018 | size_t count = 0; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1019 | Mutex::Autolock _l(mConnectionLock); |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 1020 | if (scratch) { |
| 1021 | size_t i=0; |
| 1022 | while (i<numEvents) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1023 | int32_t sensor_handle = buffer[i].sensor; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1024 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { |
| 1025 | ALOGD_IF(DEBUG_CONNECTIONS, "flush complete event sensor==%d ", |
| 1026 | buffer[i].meta_data.sensor); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1027 | // Setting sensor_handle to the correct sensor to ensure the sensor events per connection are |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1028 | // filtered correctly. buffer[i].sensor is zero for meta_data events. |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1029 | sensor_handle = buffer[i].meta_data.sensor; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1030 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1031 | ssize_t index = mSensorInfo.indexOfKey(sensor_handle); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1032 | // Check if this connection has registered for this sensor. If not continue to the |
| 1033 | // next sensor_event. |
| 1034 | if (index < 0) { |
| 1035 | ++i; |
| 1036 | continue; |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1037 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1038 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1039 | FlushInfo& flushInfo = mSensorInfo.editValueAt(index); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1040 | // Check if there is a pending flush_complete event for this sensor on this connection. |
| 1041 | if (buffer[i].type == SENSOR_TYPE_META_DATA && flushInfo.mFirstFlushPending == true) { |
| 1042 | SensorService::SensorRecord *rec = mService->getSensorRecord(sensor_handle); |
| 1043 | if (rec && rec->getFirstPendingFlushConnection() == this) { |
| 1044 | rec->removeFirstPendingFlushConnection(); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1045 | flushInfo.mFirstFlushPending = false; |
| 1046 | ++i; |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1047 | ALOGD_IF(DEBUG_CONNECTIONS, "First flush event for sensor==%d ", |
| 1048 | buffer[i].meta_data.sensor); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1049 | continue; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | // If there is a pending flush complete event for this sensor on this connection, |
| 1054 | // ignore the event and proceed to the next. |
| 1055 | if (flushInfo.mFirstFlushPending) { |
| 1056 | ++i; |
| 1057 | continue; |
| 1058 | } |
| 1059 | |
| 1060 | do { |
| 1061 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1062 | // Check if this connection has called flush() on this sensor. Only if |
| 1063 | // a flush() has been explicitly called, send a flush_complete_event. |
| 1064 | SensorService::SensorRecord *rec = mService->getSensorRecord(sensor_handle); |
| 1065 | if (rec && rec->getFirstPendingFlushConnection() == this) { |
| 1066 | rec->removeFirstPendingFlushConnection(); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1067 | scratch[count++] = buffer[i]; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1068 | } |
| 1069 | ++i; |
| 1070 | } else { |
| 1071 | // Regular sensor event, just copy it to the scratch buffer. |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1072 | scratch[count++] = buffer[i++]; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1073 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1074 | } while ((i<numEvents) && ((buffer[i].sensor == sensor_handle) || |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1075 | (buffer[i].type == SENSOR_TYPE_META_DATA && |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1076 | buffer[i].meta_data.sensor == sensor_handle))); |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 1077 | } |
Mathias Agopian | 3560fb2 | 2010-07-22 21:24:39 -0700 | [diff] [blame] | 1078 | } else { |
| 1079 | scratch = const_cast<sensors_event_t *>(buffer); |
| 1080 | count = numEvents; |
Mathias Agopian | cf51001 | 2010-07-22 16:18:10 -0700 | [diff] [blame] | 1081 | } |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1082 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1083 | sendPendingFlushEventsLocked(); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1084 | // Early return if there are no events for this connection. |
| 1085 | if (count == 0) { |
| 1086 | return status_t(NO_ERROR); |
| 1087 | } |
| 1088 | |
| 1089 | #if DEBUG_CONNECTIONS |
| 1090 | mEventsReceived += count; |
| 1091 | #endif |
| 1092 | if (mCacheSize != 0) { |
| 1093 | // There are some events in the cache which need to be sent first. Copy this buffer to |
| 1094 | // the end of cache. |
| 1095 | if (mCacheSize + count <= mMaxCacheSize) { |
| 1096 | memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t)); |
| 1097 | mCacheSize += count; |
| 1098 | } else { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1099 | // Check if any new sensors have registered on this connection which may have increased |
| 1100 | // the max cache size that is desired. |
| 1101 | if (mCacheSize + count < computeMaxCacheSizeLocked()) { |
| 1102 | reAllocateCacheLocked(scratch, count); |
| 1103 | return status_t(NO_ERROR); |
| 1104 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1105 | // Some events need to be dropped. |
| 1106 | int remaningCacheSize = mMaxCacheSize - mCacheSize; |
| 1107 | if (remaningCacheSize != 0) { |
| 1108 | memcpy(&mEventCache[mCacheSize], scratch, |
| 1109 | remaningCacheSize * sizeof(sensors_event_t)); |
| 1110 | } |
| 1111 | int numEventsDropped = count - remaningCacheSize; |
| 1112 | countFlushCompleteEventsLocked(mEventCache, numEventsDropped); |
| 1113 | // Drop the first "numEventsDropped" in the cache. |
| 1114 | memmove(mEventCache, &mEventCache[numEventsDropped], |
| 1115 | (mCacheSize - numEventsDropped) * sizeof(sensors_event_t)); |
| 1116 | |
| 1117 | // Copy the remainingEvents in scratch buffer to the end of cache. |
| 1118 | memcpy(&mEventCache[mCacheSize - numEventsDropped], scratch + remaningCacheSize, |
| 1119 | numEventsDropped * sizeof(sensors_event_t)); |
| 1120 | } |
| 1121 | return status_t(NO_ERROR); |
| 1122 | } |
| 1123 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1124 | int index_wake_up_event = findWakeUpSensorEventLocked(scratch, count); |
| 1125 | if (index_wake_up_event >= 0) { |
| 1126 | scratch[index_wake_up_event].flags |= WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 1127 | ++mWakeLockRefCount; |
| 1128 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1129 | |
| 1130 | // NOTE: ASensorEvent and sensors_event_t are the same type. |
| 1131 | ssize_t size = SensorEventQueue::write(mChannel, |
| 1132 | reinterpret_cast<ASensorEvent const*>(scratch), count); |
| 1133 | if (size < 0) { |
| 1134 | // Write error, copy events to local cache. |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1135 | if (index_wake_up_event >= 0) { |
| 1136 | // If there was a wake_up sensor_event, reset the flag. |
| 1137 | scratch[index_wake_up_event].flags &= ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 1138 | --mWakeLockRefCount; |
| 1139 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1140 | if (mEventCache == NULL) { |
| 1141 | mMaxCacheSize = computeMaxCacheSizeLocked(); |
| 1142 | mEventCache = new sensors_event_t[mMaxCacheSize]; |
| 1143 | mCacheSize = 0; |
| 1144 | } |
| 1145 | memcpy(&mEventCache[mCacheSize], scratch, count * sizeof(sensors_event_t)); |
| 1146 | mCacheSize += count; |
| 1147 | |
| 1148 | // Add this file descriptor to the looper to get a callback when this fd is available for |
| 1149 | // writing. |
| 1150 | mService->getLooper()->addFd(mChannel->getSendFd(), 0, |
| 1151 | ALOOPER_EVENT_OUTPUT | ALOOPER_EVENT_INPUT, this, NULL); |
| 1152 | return size; |
| 1153 | } |
| 1154 | |
| 1155 | #if DEBUG_CONNECTIONS |
| 1156 | if (size > 0) { |
| 1157 | mEventsSent += count; |
| 1158 | } |
| 1159 | #endif |
| 1160 | |
| 1161 | return size < 0 ? status_t(size) : status_t(NO_ERROR); |
| 1162 | } |
| 1163 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1164 | void SensorService::SensorEventConnection::reAllocateCacheLocked(sensors_event_t const* scratch, |
| 1165 | int count) { |
| 1166 | sensors_event_t *eventCache_new; |
| 1167 | const int new_cache_size = computeMaxCacheSizeLocked(); |
| 1168 | // Allocate new cache, copy over events from the old cache & scratch, free up memory. |
| 1169 | eventCache_new = new sensors_event_t[new_cache_size]; |
| 1170 | memcpy(eventCache_new, mEventCache, mCacheSize * sizeof(sensors_event_t)); |
| 1171 | memcpy(&eventCache_new[mCacheSize], scratch, count * sizeof(sensors_event_t)); |
| 1172 | |
| 1173 | ALOGD_IF(DEBUG_CONNECTIONS, "reAllocateCacheLocked maxCacheSize=%d %d", mMaxCacheSize, |
| 1174 | new_cache_size); |
| 1175 | |
| 1176 | delete mEventCache; |
| 1177 | mEventCache = eventCache_new; |
| 1178 | mCacheSize += count; |
| 1179 | mMaxCacheSize = new_cache_size; |
| 1180 | } |
| 1181 | |
| 1182 | void SensorService::SensorEventConnection::sendPendingFlushEventsLocked() { |
| 1183 | ASensorEvent flushCompleteEvent; |
| 1184 | flushCompleteEvent.type = SENSOR_TYPE_META_DATA; |
| 1185 | flushCompleteEvent.sensor = 0; |
| 1186 | // Loop through all the sensors for this connection and check if there are any pending |
| 1187 | // flush complete events to be sent. |
| 1188 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 1189 | FlushInfo& flushInfo = mSensorInfo.editValueAt(i); |
| 1190 | while (flushInfo.mPendingFlushEventsToSend > 0) { |
| 1191 | flushCompleteEvent.meta_data.sensor = mSensorInfo.keyAt(i); |
| 1192 | ssize_t size = SensorEventQueue::write(mChannel, &flushCompleteEvent, 1); |
| 1193 | if (size < 0) { |
| 1194 | return; |
| 1195 | } |
| 1196 | ALOGD_IF(DEBUG_CONNECTIONS, "sent dropped flush complete event==%d ", |
| 1197 | flushCompleteEvent.meta_data.sensor); |
| 1198 | flushInfo.mPendingFlushEventsToSend--; |
| 1199 | } |
| 1200 | } |
| 1201 | } |
| 1202 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1203 | void SensorService::SensorEventConnection::writeToSocketFromCacheLocked() { |
| 1204 | // At a time write at most half the size of the receiver buffer in SensorEventQueue. |
| 1205 | const int maxWriteSize = SensorEventQueue::MAX_RECEIVE_BUFFER_EVENT_COUNT/2; |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1206 | sendPendingFlushEventsLocked(); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1207 | // Write "count" events at a time. |
| 1208 | for (int numEventsSent = 0; numEventsSent < mCacheSize;) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1209 | const int numEventsToWrite = (mCacheSize - numEventsSent) < maxWriteSize ? |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1210 | mCacheSize - numEventsSent : maxWriteSize; |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1211 | int index_wake_up_event = |
| 1212 | findWakeUpSensorEventLocked(mEventCache + numEventsSent, numEventsToWrite); |
| 1213 | if (index_wake_up_event >= 0) { |
| 1214 | mEventCache[index_wake_up_event + numEventsSent].flags |= |
| 1215 | WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 1216 | ++mWakeLockRefCount; |
| 1217 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1218 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1219 | ssize_t size = SensorEventQueue::write(mChannel, |
| 1220 | reinterpret_cast<ASensorEvent const*>(mEventCache + numEventsSent), |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1221 | numEventsToWrite); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1222 | if (size < 0) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1223 | if (index_wake_up_event >= 0) { |
| 1224 | // If there was a wake_up sensor_event, reset the flag. |
| 1225 | mEventCache[index_wake_up_event + numEventsSent].flags &= |
| 1226 | ~WAKE_UP_SENSOR_EVENT_NEEDS_ACK; |
| 1227 | --mWakeLockRefCount; |
| 1228 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1229 | memmove(mEventCache, &mEventCache[numEventsSent], |
| 1230 | (mCacheSize - numEventsSent) * sizeof(sensors_event_t)); |
| 1231 | ALOGD_IF(DEBUG_CONNECTIONS, "wrote %d events from cache size==%d ", |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1232 | numEventsSent, mCacheSize); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1233 | mCacheSize -= numEventsSent; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1234 | return; |
| 1235 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1236 | numEventsSent += numEventsToWrite; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1237 | #if DEBUG_CONNECTIONS |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1238 | mEventsSentFromCache += numEventsToWrite; |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1239 | #endif |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 1240 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1241 | ALOGD_IF(DEBUG_CONNECTIONS, "wrote all events from cache size=%d ", mCacheSize); |
| 1242 | // All events from the cache have been sent. Reset cache size to zero. |
| 1243 | mCacheSize = 0; |
| 1244 | // Poll only for ALOOPER_EVENT_INPUT(read) on the file descriptor. |
| 1245 | mService->getLooper()->addFd(mChannel->getSendFd(), 0, ALOOPER_EVENT_INPUT, this, NULL); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1246 | } |
| 1247 | |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 1248 | void SensorService::SensorEventConnection::countFlushCompleteEventsLocked( |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1249 | sensors_event_t* scratch, const int numEventsDropped) { |
| 1250 | ALOGD_IF(DEBUG_CONNECTIONS, "dropping %d events ", numEventsDropped); |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1251 | // Count flushComplete events in the events that are about to the dropped. These will be sent |
| 1252 | // separately before the next batch of events. |
| 1253 | for (int j = 0; j < numEventsDropped; ++j) { |
| 1254 | if (scratch[j].type == SENSOR_TYPE_META_DATA) { |
| 1255 | FlushInfo& flushInfo = mSensorInfo.editValueFor(scratch[j].meta_data.sensor); |
| 1256 | flushInfo.mPendingFlushEventsToSend++; |
| 1257 | ALOGD_IF(DEBUG_CONNECTIONS, "increment pendingFlushCount %d", |
| 1258 | flushInfo.mPendingFlushEventsToSend); |
| 1259 | } |
| 1260 | } |
| 1261 | return; |
| 1262 | } |
| 1263 | |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1264 | int SensorService::SensorEventConnection::findWakeUpSensorEventLocked( |
| 1265 | sensors_event_t const* scratch, const int count) { |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1266 | for (int i = 0; i < count; ++i) { |
| 1267 | if (mService->isWakeUpSensorEvent(scratch[i])) { |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1268 | return i; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1269 | } |
| 1270 | } |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1271 | return -1; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1272 | } |
| 1273 | |
Mathias Agopian | b398927 | 2011-10-20 18:42:02 -0700 | [diff] [blame] | 1274 | sp<BitTube> SensorService::SensorEventConnection::getSensorChannel() const |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1275 | { |
| 1276 | return mChannel; |
| 1277 | } |
| 1278 | |
| 1279 | status_t SensorService::SensorEventConnection::enableDisable( |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1280 | int handle, bool enabled, nsecs_t samplingPeriodNs, nsecs_t maxBatchReportLatencyNs, |
| 1281 | int reservedFlags) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1282 | { |
| 1283 | status_t err; |
| 1284 | if (enabled) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1285 | err = mService->enable(this, handle, samplingPeriodNs, maxBatchReportLatencyNs, |
| 1286 | reservedFlags); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1287 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1288 | } else { |
| 1289 | err = mService->disable(this, handle); |
| 1290 | } |
| 1291 | return err; |
| 1292 | } |
| 1293 | |
| 1294 | status_t SensorService::SensorEventConnection::setEventRate( |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1295 | int handle, nsecs_t samplingPeriodNs) |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1296 | { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1297 | return mService->setEventRate(this, handle, samplingPeriodNs); |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 1300 | status_t SensorService::SensorEventConnection::flush() { |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 1301 | SensorDevice& dev(SensorDevice::getInstance()); |
| 1302 | const int halVersion = dev.getHalDeviceVersion(); |
Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 1303 | Mutex::Autolock _l(mConnectionLock); |
| 1304 | status_t err(NO_ERROR); |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 1305 | // Loop through all sensors for this connection and call flush on each of them. |
Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 1306 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 1307 | const int handle = mSensorInfo.keyAt(i); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1308 | FlushInfo& flushInfo = mSensorInfo.editValueFor(handle); |
Aravind Akella | b4099e7 | 2013-10-15 15:43:10 -0700 | [diff] [blame] | 1309 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_1 || mService->isVirtualSensor(handle)) { |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 1310 | // For older devices just increment pending flush count which will send a trivial |
| 1311 | // flush complete event. |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 1312 | flushInfo.mPendingFlushEventsToSend++; |
| 1313 | } else { |
| 1314 | status_t err_flush = mService->flushSensor(this, handle); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1315 | if (err_flush != NO_ERROR) { |
Aravind Akella | c551eac | 2013-10-14 17:04:42 -0700 | [diff] [blame] | 1316 | ALOGE("Flush error handle=%d %s", handle, strerror(-err_flush)); |
| 1317 | } |
| 1318 | err = (err_flush != NO_ERROR) ? err_flush : err; |
Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 1319 | } |
Aravind Akella | 701166d | 2013-10-08 14:59:26 -0700 | [diff] [blame] | 1320 | } |
| 1321 | return err; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 1322 | } |
Aravind Akella | 4c8b951 | 2013-09-05 17:03:38 -0700 | [diff] [blame] | 1323 | |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1324 | int SensorService::SensorEventConnection::handleEvent(int fd, int events, void* data) { |
| 1325 | if (events & ALOOPER_EVENT_HANGUP || events & ALOOPER_EVENT_ERROR) { |
| 1326 | return 0; |
| 1327 | } |
| 1328 | |
| 1329 | if (events & ALOOPER_EVENT_INPUT) { |
| 1330 | char buf; |
| 1331 | ssize_t ret = ::recv(fd, &buf, sizeof(buf), MSG_DONTWAIT); |
| 1332 | |
| 1333 | { |
| 1334 | Mutex::Autolock _l(mConnectionLock); |
| 1335 | --mWakeLockRefCount; |
| 1336 | } |
| 1337 | // Check if wakelock can be released by sensorservice. mConnectionLock needs to be released |
| 1338 | // here as checkWakeLockState() will need it. |
| 1339 | if (mWakeLockRefCount == 0) { |
| 1340 | mService->checkWakeLockState(); |
| 1341 | } |
| 1342 | // continue getting callbacks. |
| 1343 | return 1; |
| 1344 | } |
| 1345 | |
| 1346 | if (events & ALOOPER_EVENT_OUTPUT) { |
| 1347 | // send sensor data that is stored in mEventCache. |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1348 | Mutex::Autolock _l(mConnectionLock); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1349 | writeToSocketFromCacheLocked(); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1350 | } |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1351 | return 1; |
| 1352 | } |
| 1353 | |
| 1354 | int SensorService::SensorEventConnection::computeMaxCacheSizeLocked() const { |
| 1355 | int fifoWakeUpSensors = 0; |
| 1356 | int fifoNonWakeUpSensors = 0; |
| 1357 | for (size_t i = 0; i < mSensorInfo.size(); ++i) { |
| 1358 | const Sensor& sensor = mService->getSensorFromHandle(mSensorInfo.keyAt(i)); |
| 1359 | if (sensor.getFifoReservedEventCount() == sensor.getFifoMaxEventCount()) { |
| 1360 | // Each sensor has a reserved fifo. Sum up the fifo sizes for all wake up sensors and |
| 1361 | // non wake_up sensors. |
| 1362 | if (sensor.isWakeUpSensor()) { |
| 1363 | fifoWakeUpSensors += sensor.getFifoReservedEventCount(); |
| 1364 | } else { |
| 1365 | fifoNonWakeUpSensors += sensor.getFifoReservedEventCount(); |
| 1366 | } |
| 1367 | } else { |
| 1368 | // Shared fifo. Compute the max of the fifo sizes for wake_up and non_wake up sensors. |
| 1369 | if (sensor.isWakeUpSensor()) { |
| 1370 | fifoWakeUpSensors = fifoWakeUpSensors > sensor.getFifoMaxEventCount() ? |
| 1371 | fifoWakeUpSensors : sensor.getFifoMaxEventCount(); |
| 1372 | |
| 1373 | } else { |
| 1374 | fifoNonWakeUpSensors = fifoNonWakeUpSensors > sensor.getFifoMaxEventCount() ? |
| 1375 | fifoNonWakeUpSensors : sensor.getFifoMaxEventCount(); |
| 1376 | |
| 1377 | } |
| 1378 | } |
| 1379 | } |
| 1380 | if (fifoWakeUpSensors + fifoNonWakeUpSensors == 0) { |
| 1381 | // 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^] | 1382 | // size that is equal to that of the batch mode. |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1383 | ALOGI("Write failure in non-batch mode"); |
Aravind Akella | 6c2664a | 2014-08-13 12:24:50 -0700 | [diff] [blame^] | 1384 | return MAX_SOCKET_BUFFER_SIZE_BATCHED/sizeof(sensors_event_t); |
Aravind Akella | 56ae426 | 2014-07-10 16:01:10 -0700 | [diff] [blame] | 1385 | } |
| 1386 | return fifoWakeUpSensors + fifoNonWakeUpSensors; |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 1387 | } |
| 1388 | |
Mathias Agopian | fc32881 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 1389 | // --------------------------------------------------------------------------- |
| 1390 | }; // namespace android |
| 1391 | |