Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -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 | 8f515ce | 2014-06-09 14:32:04 -0700 | [diff] [blame] | 17 | #include <inttypes.h> |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 18 | #include <stdint.h> |
| 19 | #include <sys/types.h> |
Aravind Akella | d9441e4 | 2014-05-13 18:20:30 -0700 | [diff] [blame] | 20 | #include <sys/limits.h> |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 21 | |
| 22 | #include <utils/Errors.h> |
| 23 | #include <utils/String8.h> |
| 24 | #include <utils/Flattenable.h> |
| 25 | |
| 26 | #include <hardware/sensors.h> |
| 27 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 28 | #include <binder/AppOpsManager.h> |
| 29 | #include <binder/IServiceManager.h> |
| 30 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 31 | #include <gui/Sensor.h> |
Aravind Akella | d9441e4 | 2014-05-13 18:20:30 -0700 | [diff] [blame] | 32 | #include <log/log.h> |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 33 | |
| 34 | // ---------------------------------------------------------------------------- |
| 35 | namespace android { |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | |
| 38 | Sensor::Sensor() |
| 39 | : mHandle(0), mType(0), |
| 40 | mMinValue(0), mMaxValue(0), mResolution(0), |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 41 | mPower(0), mMinDelay(0), mFifoReservedEventCount(0), mFifoMaxEventCount(0), |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 42 | mMaxDelay(0), mFlags(0) |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 46 | Sensor::Sensor(struct sensor_t const* hwSensor, int halVersion) |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 47 | { |
| 48 | mName = hwSensor->name; |
| 49 | mVendor = hwSensor->vendor; |
Mathias Agopian | 2ebc4d6 | 2012-05-04 15:47:13 -0700 | [diff] [blame] | 50 | mVersion = hwSensor->version; |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 51 | mHandle = hwSensor->handle; |
| 52 | mType = hwSensor->type; |
| 53 | mMinValue = 0; // FIXME: minValue |
| 54 | mMaxValue = hwSensor->maxRange; // FIXME: maxValue |
| 55 | mResolution = hwSensor->resolution; |
| 56 | mPower = hwSensor->power; |
Mathias Agopian | a48bcf6 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 57 | mMinDelay = hwSensor->minDelay; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 58 | mFlags = 0; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 59 | |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 60 | // Set fifo event count zero for older devices which do not support batching. Fused |
| 61 | // sensors also have their fifo counts set to zero. |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 62 | if (halVersion > SENSORS_DEVICE_API_VERSION_1_0) { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 63 | mFifoReservedEventCount = hwSensor->fifoReservedEventCount; |
| 64 | mFifoMaxEventCount = hwSensor->fifoMaxEventCount; |
Aravind Akella | d35e3af | 2014-05-12 17:14:56 -0700 | [diff] [blame] | 65 | } else { |
| 66 | mFifoReservedEventCount = 0; |
| 67 | mFifoMaxEventCount = 0; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Aravind Akella | d9441e4 | 2014-05-13 18:20:30 -0700 | [diff] [blame] | 70 | if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) { |
| 71 | if (hwSensor->maxDelay > INT_MAX) { |
| 72 | // Max delay is declared as a 64 bit integer for 64 bit architectures. But it should |
| 73 | // always fit in a 32 bit integer, log error and cap it to INT_MAX. |
Mark Salyzyn | 8f515ce | 2014-06-09 14:32:04 -0700 | [diff] [blame] | 74 | ALOGE("Sensor maxDelay overflow error %s %" PRId64, mName.string(), |
| 75 | static_cast<int64_t>(hwSensor->maxDelay)); |
Aravind Akella | d9441e4 | 2014-05-13 18:20:30 -0700 | [diff] [blame] | 76 | mMaxDelay = INT_MAX; |
| 77 | } else { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 78 | mMaxDelay = static_cast<int32_t>(hwSensor->maxDelay); |
Aravind Akella | d9441e4 | 2014-05-13 18:20:30 -0700 | [diff] [blame] | 79 | } |
| 80 | } else { |
| 81 | // For older hals set maxDelay to 0. |
| 82 | mMaxDelay = 0; |
| 83 | } |
| 84 | |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 85 | // Ensure existing sensors have correct string type, required permissions and reporting mode. |
Aravind Akella | 64ffcb0 | 2014-07-29 12:27:41 -0700 | [diff] [blame] | 86 | // Set reportingMode for all android defined sensor types, set wake-up flag only for proximity |
| 87 | // sensor, significant motion, tilt, pick_up gesture, wake gesture and glance gesture on older |
| 88 | // HALs. Newer HALs can define both wake-up and non wake-up proximity sensors. |
| 89 | // All the OEM defined defined sensors have flags set to whatever is provided by the HAL. |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 90 | switch (mType) { |
| 91 | case SENSOR_TYPE_ACCELEROMETER: |
| 92 | mStringType = SENSOR_STRING_TYPE_ACCELEROMETER; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 93 | mFlags |= SENSOR_FLAG_CONTINUOUS_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 94 | break; |
| 95 | case SENSOR_TYPE_AMBIENT_TEMPERATURE: |
| 96 | mStringType = SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 97 | mFlags |= SENSOR_FLAG_ON_CHANGE_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 98 | break; |
| 99 | case SENSOR_TYPE_GAME_ROTATION_VECTOR: |
| 100 | mStringType = SENSOR_STRING_TYPE_GAME_ROTATION_VECTOR; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 101 | mFlags |= SENSOR_FLAG_CONTINUOUS_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 102 | break; |
| 103 | case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR: |
| 104 | mStringType = SENSOR_STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 105 | mFlags |= SENSOR_FLAG_CONTINUOUS_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 106 | break; |
| 107 | case SENSOR_TYPE_GRAVITY: |
| 108 | mStringType = SENSOR_STRING_TYPE_GRAVITY; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 109 | mFlags |= SENSOR_FLAG_CONTINUOUS_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 110 | break; |
| 111 | case SENSOR_TYPE_GYROSCOPE: |
| 112 | mStringType = SENSOR_STRING_TYPE_GYROSCOPE; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 113 | mFlags |= SENSOR_FLAG_CONTINUOUS_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 114 | break; |
| 115 | case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: |
| 116 | mStringType = SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 117 | mFlags |= SENSOR_FLAG_CONTINUOUS_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 118 | break; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 119 | case SENSOR_TYPE_HEART_RATE: { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 120 | mStringType = SENSOR_STRING_TYPE_HEART_RATE; |
| 121 | mRequiredPermission = SENSOR_PERMISSION_BODY_SENSORS; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 122 | AppOpsManager appOps; |
| 123 | mRequiredAppOp = appOps.permissionToOpCode(String16(SENSOR_PERMISSION_BODY_SENSORS)); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 124 | mFlags |= SENSOR_FLAG_ON_CHANGE_MODE; |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 125 | } break; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 126 | case SENSOR_TYPE_LIGHT: |
| 127 | mStringType = SENSOR_STRING_TYPE_LIGHT; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 128 | mFlags |= SENSOR_FLAG_ON_CHANGE_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 129 | break; |
| 130 | case SENSOR_TYPE_LINEAR_ACCELERATION: |
| 131 | mStringType = SENSOR_STRING_TYPE_LINEAR_ACCELERATION; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 132 | mFlags |= SENSOR_FLAG_CONTINUOUS_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 133 | break; |
| 134 | case SENSOR_TYPE_MAGNETIC_FIELD: |
| 135 | mStringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 136 | mFlags |= SENSOR_FLAG_CONTINUOUS_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 137 | break; |
| 138 | case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED: |
| 139 | mStringType = SENSOR_STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 140 | mFlags |= SENSOR_FLAG_CONTINUOUS_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 141 | break; |
| 142 | case SENSOR_TYPE_ORIENTATION: |
| 143 | mStringType = SENSOR_STRING_TYPE_ORIENTATION; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 144 | mFlags |= SENSOR_FLAG_CONTINUOUS_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 145 | break; |
| 146 | case SENSOR_TYPE_PRESSURE: |
| 147 | mStringType = SENSOR_STRING_TYPE_PRESSURE; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 148 | mFlags |= SENSOR_FLAG_CONTINUOUS_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 149 | break; |
| 150 | case SENSOR_TYPE_PROXIMITY: |
| 151 | mStringType = SENSOR_STRING_TYPE_PROXIMITY; |
Aravind Akella | 64ffcb0 | 2014-07-29 12:27:41 -0700 | [diff] [blame] | 152 | mFlags |= SENSOR_FLAG_ON_CHANGE_MODE; |
| 153 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) { |
| 154 | mFlags |= SENSOR_FLAG_WAKE_UP; |
| 155 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 156 | break; |
| 157 | case SENSOR_TYPE_RELATIVE_HUMIDITY: |
| 158 | mStringType = SENSOR_STRING_TYPE_RELATIVE_HUMIDITY; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 159 | mFlags |= SENSOR_FLAG_ON_CHANGE_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 160 | break; |
| 161 | case SENSOR_TYPE_ROTATION_VECTOR: |
| 162 | mStringType = SENSOR_STRING_TYPE_ROTATION_VECTOR; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 163 | mFlags |= SENSOR_FLAG_CONTINUOUS_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 164 | break; |
| 165 | case SENSOR_TYPE_SIGNIFICANT_MOTION: |
| 166 | mStringType = SENSOR_STRING_TYPE_SIGNIFICANT_MOTION; |
Aravind Akella | 64ffcb0 | 2014-07-29 12:27:41 -0700 | [diff] [blame] | 167 | mFlags |= SENSOR_FLAG_ONE_SHOT_MODE; |
| 168 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) { |
| 169 | mFlags |= SENSOR_FLAG_WAKE_UP; |
| 170 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 171 | break; |
| 172 | case SENSOR_TYPE_STEP_COUNTER: |
| 173 | mStringType = SENSOR_STRING_TYPE_STEP_COUNTER; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 174 | mFlags |= SENSOR_FLAG_ON_CHANGE_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 175 | break; |
| 176 | case SENSOR_TYPE_STEP_DETECTOR: |
| 177 | mStringType = SENSOR_STRING_TYPE_STEP_DETECTOR; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 178 | mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 179 | break; |
| 180 | case SENSOR_TYPE_TEMPERATURE: |
| 181 | mStringType = SENSOR_STRING_TYPE_TEMPERATURE; |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 182 | mFlags |= SENSOR_FLAG_ON_CHANGE_MODE; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 183 | break; |
Aravind Akella | 64ffcb0 | 2014-07-29 12:27:41 -0700 | [diff] [blame] | 184 | case SENSOR_TYPE_TILT_DETECTOR: |
| 185 | mStringType = SENSOR_STRING_TYPE_TILT_DETECTOR; |
| 186 | mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE; |
| 187 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) { |
| 188 | mFlags |= SENSOR_FLAG_WAKE_UP; |
| 189 | } |
| 190 | break; |
Etienne Le Grand | e284a90 | 2014-05-07 19:49:05 -0700 | [diff] [blame] | 191 | case SENSOR_TYPE_WAKE_GESTURE: |
| 192 | mStringType = SENSOR_STRING_TYPE_WAKE_GESTURE; |
Aravind Akella | 64ffcb0 | 2014-07-29 12:27:41 -0700 | [diff] [blame] | 193 | mFlags |= SENSOR_FLAG_ONE_SHOT_MODE; |
| 194 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) { |
| 195 | mFlags |= SENSOR_FLAG_WAKE_UP; |
| 196 | } |
Etienne Le Grand | e284a90 | 2014-05-07 19:49:05 -0700 | [diff] [blame] | 197 | break; |
Jeff Brown | 31d825d | 2014-07-17 15:13:55 -0700 | [diff] [blame] | 198 | case SENSOR_TYPE_GLANCE_GESTURE: |
| 199 | mStringType = SENSOR_STRING_TYPE_GLANCE_GESTURE; |
Aravind Akella | 64ffcb0 | 2014-07-29 12:27:41 -0700 | [diff] [blame] | 200 | mFlags |= SENSOR_FLAG_ONE_SHOT_MODE; |
| 201 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) { |
| 202 | mFlags |= SENSOR_FLAG_WAKE_UP; |
| 203 | } |
Jeff Brown | 31d825d | 2014-07-17 15:13:55 -0700 | [diff] [blame] | 204 | break; |
Aravind Akella | fd8498c | 2014-07-28 18:01:11 -0700 | [diff] [blame] | 205 | case SENSOR_TYPE_PICK_UP_GESTURE: |
| 206 | mStringType = SENSOR_STRING_TYPE_PICK_UP_GESTURE; |
Aravind Akella | 64ffcb0 | 2014-07-29 12:27:41 -0700 | [diff] [blame] | 207 | mFlags |= SENSOR_FLAG_ONE_SHOT_MODE; |
| 208 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) { |
| 209 | mFlags |= SENSOR_FLAG_WAKE_UP; |
| 210 | } |
Aravind Akella | fd8498c | 2014-07-28 18:01:11 -0700 | [diff] [blame] | 211 | break; |
Nick Vaccaro | 9bb99b1 | 2015-01-21 15:27:23 -0800 | [diff] [blame] | 212 | case SENSOR_TYPE_WRIST_TILT_GESTURE: |
| 213 | mStringType = SENSOR_STRING_TYPE_WRIST_TILT_GESTURE; |
| 214 | mFlags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE; |
| 215 | if (halVersion < SENSORS_DEVICE_API_VERSION_1_3) { |
| 216 | mFlags |= SENSOR_FLAG_WAKE_UP; |
| 217 | } |
| 218 | break; |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 219 | default: |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 220 | // Only pipe the stringType, requiredPermission and flags for custom sensors. |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 221 | if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor->stringType) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 222 | mStringType = hwSensor->stringType; |
| 223 | } |
Aravind Akella | 8493b79 | 2014-09-08 15:45:47 -0700 | [diff] [blame] | 224 | if (halVersion > SENSORS_DEVICE_API_VERSION_1_0 && hwSensor->requiredPermission) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 225 | mRequiredPermission = hwSensor->requiredPermission; |
| 226 | } |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 227 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 228 | if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 229 | mFlags = static_cast<uint32_t>(hwSensor->flags); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 230 | } else { |
| 231 | // This is an OEM defined sensor on an older HAL. Use minDelay to determine the |
| 232 | // reporting mode of the sensor. |
| 233 | if (mMinDelay > 0) { |
| 234 | mFlags |= SENSOR_FLAG_CONTINUOUS_MODE; |
| 235 | } else if (mMinDelay == 0) { |
| 236 | mFlags |= SENSOR_FLAG_ON_CHANGE_MODE; |
| 237 | } else if (mMinDelay < 0) { |
| 238 | mFlags |= SENSOR_FLAG_ONE_SHOT_MODE; |
| 239 | } |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 240 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 241 | break; |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 242 | } |
Aravind Akella | 64ffcb0 | 2014-07-29 12:27:41 -0700 | [diff] [blame] | 243 | |
Aravind Akella | f9b7f85 | 2015-09-10 14:52:31 -0700 | [diff] [blame^] | 244 | // Set DATA_INJECTION flag here. Defined in HAL 1_4. |
| 245 | if (halVersion >= SENSORS_DEVICE_API_VERSION_1_4) { |
| 246 | mFlags |= (hwSensor->flags & DATA_INJECTION_MASK); |
| 247 | } |
| 248 | |
Aravind Akella | 64ffcb0 | 2014-07-29 12:27:41 -0700 | [diff] [blame] | 249 | // For the newer HALs log errors if reporting mask flags are set incorrectly. |
| 250 | if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) { |
| 251 | // Wake-up flag is set here. |
| 252 | mFlags |= (hwSensor->flags & SENSOR_FLAG_WAKE_UP); |
| 253 | if (mFlags != hwSensor->flags) { |
| 254 | int actualReportingMode = |
| 255 | (hwSensor->flags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT; |
| 256 | int expectedReportingMode = (mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT; |
| 257 | if (actualReportingMode != expectedReportingMode) { |
| 258 | ALOGE("Reporting Mode incorrect: sensor %s handle=%d type=%d " |
| 259 | "actual=%d expected=%d", |
| 260 | mName.string(), mHandle, mType, actualReportingMode, expectedReportingMode); |
| 261 | } |
| 262 | |
| 263 | } |
| 264 | } |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 265 | |
| 266 | if (mRequiredPermission.length() > 0) { |
| 267 | // If the sensor is protected by a permission we need to know if it is |
| 268 | // a runtime one to determine whether we can use the permission cache. |
| 269 | sp<IBinder> binder = defaultServiceManager()->getService(String16("permission")); |
| 270 | if (binder != 0) { |
| 271 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); |
| 272 | mRequiredPermissionRuntime = permCtrl->isRuntimePermission( |
| 273 | String16(mRequiredPermission)); |
| 274 | } |
| 275 | } |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 278 | Sensor::~Sensor() |
| 279 | { |
| 280 | } |
| 281 | |
| 282 | const String8& Sensor::getName() const { |
| 283 | return mName; |
| 284 | } |
| 285 | |
| 286 | const String8& Sensor::getVendor() const { |
| 287 | return mVendor; |
| 288 | } |
| 289 | |
| 290 | int32_t Sensor::getHandle() const { |
| 291 | return mHandle; |
| 292 | } |
| 293 | |
| 294 | int32_t Sensor::getType() const { |
| 295 | return mType; |
| 296 | } |
| 297 | |
| 298 | float Sensor::getMinValue() const { |
| 299 | return mMinValue; |
| 300 | } |
| 301 | |
| 302 | float Sensor::getMaxValue() const { |
| 303 | return mMaxValue; |
| 304 | } |
| 305 | |
| 306 | float Sensor::getResolution() const { |
| 307 | return mResolution; |
| 308 | } |
| 309 | |
| 310 | float Sensor::getPowerUsage() const { |
| 311 | return mPower; |
| 312 | } |
| 313 | |
Mathias Agopian | a48bcf6 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 314 | int32_t Sensor::getMinDelay() const { |
| 315 | return mMinDelay; |
| 316 | } |
| 317 | |
Mathias Agopian | b62013f | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 318 | nsecs_t Sensor::getMinDelayNs() const { |
| 319 | return getMinDelay() * 1000; |
| 320 | } |
| 321 | |
| 322 | int32_t Sensor::getVersion() const { |
| 323 | return mVersion; |
| 324 | } |
| 325 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 326 | uint32_t Sensor::getFifoReservedEventCount() const { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 327 | return mFifoReservedEventCount; |
| 328 | } |
| 329 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 330 | uint32_t Sensor::getFifoMaxEventCount() const { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 331 | return mFifoMaxEventCount; |
| 332 | } |
| 333 | |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 334 | const String8& Sensor::getStringType() const { |
| 335 | return mStringType; |
| 336 | } |
| 337 | |
| 338 | const String8& Sensor::getRequiredPermission() const { |
| 339 | return mRequiredPermission; |
| 340 | } |
| 341 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 342 | bool Sensor::isRequiredPermissionRuntime() const { |
| 343 | return mRequiredPermissionRuntime; |
| 344 | } |
| 345 | |
| 346 | int32_t Sensor::getRequiredAppOp() const { |
| 347 | return mRequiredAppOp; |
| 348 | } |
| 349 | |
Aravind Akella | d9441e4 | 2014-05-13 18:20:30 -0700 | [diff] [blame] | 350 | int32_t Sensor::getMaxDelay() const { |
| 351 | return mMaxDelay; |
| 352 | } |
| 353 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 354 | uint32_t Sensor::getFlags() const { |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 355 | return mFlags; |
| 356 | } |
| 357 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 358 | bool Sensor::isWakeUpSensor() const { |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 359 | return mFlags & SENSOR_FLAG_WAKE_UP; |
| 360 | } |
| 361 | |
| 362 | int32_t Sensor::getReportingMode() const { |
| 363 | return ((mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 364 | } |
| 365 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 366 | size_t Sensor::getFlattenedSize() const |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 367 | { |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 368 | size_t fixedSize = |
Mathias Agopian | 2ebc4d6 | 2012-05-04 15:47:13 -0700 | [diff] [blame] | 369 | sizeof(int32_t) * 3 + |
Mathias Agopian | a48bcf6 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 370 | sizeof(float) * 4 + |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 371 | sizeof(int32_t) * 6 + |
| 372 | sizeof(bool); |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 373 | |
| 374 | size_t variableSize = |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 375 | sizeof(uint32_t) + FlattenableUtils::align<4>(mName.length()) + |
| 376 | sizeof(uint32_t) + FlattenableUtils::align<4>(mVendor.length()) + |
| 377 | sizeof(uint32_t) + FlattenableUtils::align<4>(mStringType.length()) + |
| 378 | sizeof(uint32_t) + FlattenableUtils::align<4>(mRequiredPermission.length()); |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 379 | |
| 380 | return fixedSize + variableSize; |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 383 | status_t Sensor::flatten(void* buffer, size_t size) const { |
| 384 | if (size < getFlattenedSize()) { |
| 385 | return NO_MEMORY; |
| 386 | } |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 387 | |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 388 | flattenString8(buffer, size, mName); |
| 389 | flattenString8(buffer, size, mVendor); |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 390 | FlattenableUtils::write(buffer, size, mVersion); |
| 391 | FlattenableUtils::write(buffer, size, mHandle); |
| 392 | FlattenableUtils::write(buffer, size, mType); |
| 393 | FlattenableUtils::write(buffer, size, mMinValue); |
| 394 | FlattenableUtils::write(buffer, size, mMaxValue); |
| 395 | FlattenableUtils::write(buffer, size, mResolution); |
| 396 | FlattenableUtils::write(buffer, size, mPower); |
| 397 | FlattenableUtils::write(buffer, size, mMinDelay); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 398 | FlattenableUtils::write(buffer, size, mFifoReservedEventCount); |
| 399 | FlattenableUtils::write(buffer, size, mFifoMaxEventCount); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 400 | flattenString8(buffer, size, mStringType); |
| 401 | flattenString8(buffer, size, mRequiredPermission); |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 402 | FlattenableUtils::write(buffer, size, mRequiredPermissionRuntime); |
| 403 | FlattenableUtils::write(buffer, size, mRequiredAppOp); |
Aravind Akella | d9441e4 | 2014-05-13 18:20:30 -0700 | [diff] [blame] | 404 | FlattenableUtils::write(buffer, size, mMaxDelay); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 405 | FlattenableUtils::write(buffer, size, mFlags); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 406 | return NO_ERROR; |
| 407 | } |
| 408 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 409 | status_t Sensor::unflatten(void const* buffer, size_t size) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 410 | if (!unflattenString8(buffer, size, mName)) { |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 411 | return NO_MEMORY; |
| 412 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 413 | if (!unflattenString8(buffer, size, mVendor)) { |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 414 | return NO_MEMORY; |
| 415 | } |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 416 | |
| 417 | size_t fixedSize = |
| 418 | sizeof(int32_t) * 3 + |
| 419 | sizeof(float) * 4 + |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 420 | sizeof(int32_t) * 5; |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 421 | if (size < fixedSize) { |
| 422 | return NO_MEMORY; |
| 423 | } |
| 424 | |
| 425 | FlattenableUtils::read(buffer, size, mVersion); |
| 426 | FlattenableUtils::read(buffer, size, mHandle); |
| 427 | FlattenableUtils::read(buffer, size, mType); |
| 428 | FlattenableUtils::read(buffer, size, mMinValue); |
| 429 | FlattenableUtils::read(buffer, size, mMaxValue); |
| 430 | FlattenableUtils::read(buffer, size, mResolution); |
| 431 | FlattenableUtils::read(buffer, size, mPower); |
| 432 | FlattenableUtils::read(buffer, size, mMinDelay); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 433 | FlattenableUtils::read(buffer, size, mFifoReservedEventCount); |
| 434 | FlattenableUtils::read(buffer, size, mFifoMaxEventCount); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 435 | |
| 436 | if (!unflattenString8(buffer, size, mStringType)) { |
| 437 | return NO_MEMORY; |
| 438 | } |
| 439 | if (!unflattenString8(buffer, size, mRequiredPermission)) { |
| 440 | return NO_MEMORY; |
| 441 | } |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 442 | FlattenableUtils::read(buffer, size, mRequiredPermissionRuntime); |
| 443 | FlattenableUtils::read(buffer, size, mRequiredAppOp); |
Aravind Akella | d9441e4 | 2014-05-13 18:20:30 -0700 | [diff] [blame] | 444 | FlattenableUtils::read(buffer, size, mMaxDelay); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 445 | FlattenableUtils::read(buffer, size, mFlags); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 446 | return NO_ERROR; |
| 447 | } |
| 448 | |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 449 | void Sensor::flattenString8(void*& buffer, size_t& size, |
| 450 | const String8& string8) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 451 | uint32_t len = static_cast<uint32_t>(string8.length()); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 452 | FlattenableUtils::write(buffer, size, len); |
| 453 | memcpy(static_cast<char*>(buffer), string8.string(), len); |
| 454 | FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len)); |
| 455 | } |
| 456 | |
| 457 | bool Sensor::unflattenString8(void const*& buffer, size_t& size, String8& outputString8) { |
| 458 | uint32_t len; |
| 459 | if (size < sizeof(len)) { |
| 460 | return false; |
| 461 | } |
| 462 | FlattenableUtils::read(buffer, size, len); |
| 463 | if (size < len) { |
| 464 | return false; |
| 465 | } |
| 466 | outputString8.setTo(static_cast<char const*>(buffer), len); |
| 467 | FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len)); |
| 468 | return true; |
| 469 | } |
| 470 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 471 | // ---------------------------------------------------------------------------- |
| 472 | }; // namespace android |