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 | |
| 244 | // For the newer HALs log errors if reporting mask flags are set incorrectly. |
| 245 | if (halVersion >= SENSORS_DEVICE_API_VERSION_1_3) { |
| 246 | // Wake-up flag is set here. |
| 247 | mFlags |= (hwSensor->flags & SENSOR_FLAG_WAKE_UP); |
| 248 | if (mFlags != hwSensor->flags) { |
| 249 | int actualReportingMode = |
| 250 | (hwSensor->flags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT; |
| 251 | int expectedReportingMode = (mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT; |
| 252 | if (actualReportingMode != expectedReportingMode) { |
| 253 | ALOGE("Reporting Mode incorrect: sensor %s handle=%d type=%d " |
| 254 | "actual=%d expected=%d", |
| 255 | mName.string(), mHandle, mType, actualReportingMode, expectedReportingMode); |
| 256 | } |
| 257 | |
| 258 | } |
| 259 | } |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 260 | |
| 261 | if (mRequiredPermission.length() > 0) { |
| 262 | // If the sensor is protected by a permission we need to know if it is |
| 263 | // a runtime one to determine whether we can use the permission cache. |
| 264 | sp<IBinder> binder = defaultServiceManager()->getService(String16("permission")); |
| 265 | if (binder != 0) { |
| 266 | sp<IPermissionController> permCtrl = interface_cast<IPermissionController>(binder); |
| 267 | mRequiredPermissionRuntime = permCtrl->isRuntimePermission( |
| 268 | String16(mRequiredPermission)); |
| 269 | } |
| 270 | } |
Mathias Agopian | a7352c9 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 273 | Sensor::~Sensor() |
| 274 | { |
| 275 | } |
| 276 | |
| 277 | const String8& Sensor::getName() const { |
| 278 | return mName; |
| 279 | } |
| 280 | |
| 281 | const String8& Sensor::getVendor() const { |
| 282 | return mVendor; |
| 283 | } |
| 284 | |
| 285 | int32_t Sensor::getHandle() const { |
| 286 | return mHandle; |
| 287 | } |
| 288 | |
| 289 | int32_t Sensor::getType() const { |
| 290 | return mType; |
| 291 | } |
| 292 | |
| 293 | float Sensor::getMinValue() const { |
| 294 | return mMinValue; |
| 295 | } |
| 296 | |
| 297 | float Sensor::getMaxValue() const { |
| 298 | return mMaxValue; |
| 299 | } |
| 300 | |
| 301 | float Sensor::getResolution() const { |
| 302 | return mResolution; |
| 303 | } |
| 304 | |
| 305 | float Sensor::getPowerUsage() const { |
| 306 | return mPower; |
| 307 | } |
| 308 | |
Mathias Agopian | a48bcf6 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 309 | int32_t Sensor::getMinDelay() const { |
| 310 | return mMinDelay; |
| 311 | } |
| 312 | |
Mathias Agopian | b62013f | 2011-05-17 22:54:42 -0700 | [diff] [blame] | 313 | nsecs_t Sensor::getMinDelayNs() const { |
| 314 | return getMinDelay() * 1000; |
| 315 | } |
| 316 | |
| 317 | int32_t Sensor::getVersion() const { |
| 318 | return mVersion; |
| 319 | } |
| 320 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 321 | uint32_t Sensor::getFifoReservedEventCount() const { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 322 | return mFifoReservedEventCount; |
| 323 | } |
| 324 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 325 | uint32_t Sensor::getFifoMaxEventCount() const { |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 326 | return mFifoMaxEventCount; |
| 327 | } |
| 328 | |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 329 | const String8& Sensor::getStringType() const { |
| 330 | return mStringType; |
| 331 | } |
| 332 | |
| 333 | const String8& Sensor::getRequiredPermission() const { |
| 334 | return mRequiredPermission; |
| 335 | } |
| 336 | |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 337 | bool Sensor::isRequiredPermissionRuntime() const { |
| 338 | return mRequiredPermissionRuntime; |
| 339 | } |
| 340 | |
| 341 | int32_t Sensor::getRequiredAppOp() const { |
| 342 | return mRequiredAppOp; |
| 343 | } |
| 344 | |
Aravind Akella | d9441e4 | 2014-05-13 18:20:30 -0700 | [diff] [blame] | 345 | int32_t Sensor::getMaxDelay() const { |
| 346 | return mMaxDelay; |
| 347 | } |
| 348 | |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 349 | uint32_t Sensor::getFlags() const { |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 350 | return mFlags; |
| 351 | } |
| 352 | |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 353 | bool Sensor::isWakeUpSensor() const { |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 354 | return mFlags & SENSOR_FLAG_WAKE_UP; |
| 355 | } |
| 356 | |
| 357 | int32_t Sensor::getReportingMode() const { |
| 358 | return ((mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT); |
Aravind Akella | 9a844cf | 2014-02-11 18:58:52 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 361 | size_t Sensor::getFlattenedSize() const |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 362 | { |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 363 | size_t fixedSize = |
Mathias Agopian | 2ebc4d6 | 2012-05-04 15:47:13 -0700 | [diff] [blame] | 364 | sizeof(int32_t) * 3 + |
Mathias Agopian | a48bcf6 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 365 | sizeof(float) * 4 + |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 366 | sizeof(int32_t) * 6 + |
| 367 | sizeof(bool); |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 368 | |
| 369 | size_t variableSize = |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 370 | sizeof(uint32_t) + FlattenableUtils::align<4>(mName.length()) + |
| 371 | sizeof(uint32_t) + FlattenableUtils::align<4>(mVendor.length()) + |
| 372 | sizeof(uint32_t) + FlattenableUtils::align<4>(mStringType.length()) + |
| 373 | sizeof(uint32_t) + FlattenableUtils::align<4>(mRequiredPermission.length()); |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 374 | |
| 375 | return fixedSize + variableSize; |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 378 | status_t Sensor::flatten(void* buffer, size_t size) const { |
| 379 | if (size < getFlattenedSize()) { |
| 380 | return NO_MEMORY; |
| 381 | } |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 382 | |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 383 | flattenString8(buffer, size, mName); |
| 384 | flattenString8(buffer, size, mVendor); |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 385 | FlattenableUtils::write(buffer, size, mVersion); |
| 386 | FlattenableUtils::write(buffer, size, mHandle); |
| 387 | FlattenableUtils::write(buffer, size, mType); |
| 388 | FlattenableUtils::write(buffer, size, mMinValue); |
| 389 | FlattenableUtils::write(buffer, size, mMaxValue); |
| 390 | FlattenableUtils::write(buffer, size, mResolution); |
| 391 | FlattenableUtils::write(buffer, size, mPower); |
| 392 | FlattenableUtils::write(buffer, size, mMinDelay); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 393 | FlattenableUtils::write(buffer, size, mFifoReservedEventCount); |
| 394 | FlattenableUtils::write(buffer, size, mFifoMaxEventCount); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 395 | flattenString8(buffer, size, mStringType); |
| 396 | flattenString8(buffer, size, mRequiredPermission); |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 397 | FlattenableUtils::write(buffer, size, mRequiredPermissionRuntime); |
| 398 | FlattenableUtils::write(buffer, size, mRequiredAppOp); |
Aravind Akella | d9441e4 | 2014-05-13 18:20:30 -0700 | [diff] [blame] | 399 | FlattenableUtils::write(buffer, size, mMaxDelay); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 400 | FlattenableUtils::write(buffer, size, mFlags); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 401 | return NO_ERROR; |
| 402 | } |
| 403 | |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 404 | status_t Sensor::unflatten(void const* buffer, size_t size) { |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 405 | if (!unflattenString8(buffer, size, mName)) { |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 406 | return NO_MEMORY; |
| 407 | } |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 408 | if (!unflattenString8(buffer, size, mVendor)) { |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 409 | return NO_MEMORY; |
| 410 | } |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 411 | |
| 412 | size_t fixedSize = |
| 413 | sizeof(int32_t) * 3 + |
| 414 | sizeof(float) * 4 + |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 415 | sizeof(int32_t) * 5; |
Mathias Agopian | e142428 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 416 | if (size < fixedSize) { |
| 417 | return NO_MEMORY; |
| 418 | } |
| 419 | |
| 420 | FlattenableUtils::read(buffer, size, mVersion); |
| 421 | FlattenableUtils::read(buffer, size, mHandle); |
| 422 | FlattenableUtils::read(buffer, size, mType); |
| 423 | FlattenableUtils::read(buffer, size, mMinValue); |
| 424 | FlattenableUtils::read(buffer, size, mMaxValue); |
| 425 | FlattenableUtils::read(buffer, size, mResolution); |
| 426 | FlattenableUtils::read(buffer, size, mPower); |
| 427 | FlattenableUtils::read(buffer, size, mMinDelay); |
Aravind Akella | 724d91d | 2013-06-27 12:04:23 -0700 | [diff] [blame] | 428 | FlattenableUtils::read(buffer, size, mFifoReservedEventCount); |
| 429 | FlattenableUtils::read(buffer, size, mFifoMaxEventCount); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 430 | |
| 431 | if (!unflattenString8(buffer, size, mStringType)) { |
| 432 | return NO_MEMORY; |
| 433 | } |
| 434 | if (!unflattenString8(buffer, size, mRequiredPermission)) { |
| 435 | return NO_MEMORY; |
| 436 | } |
Svetoslav | b412f6e | 2015-04-29 16:50:41 -0700 | [diff] [blame] | 437 | FlattenableUtils::read(buffer, size, mRequiredPermissionRuntime); |
| 438 | FlattenableUtils::read(buffer, size, mRequiredAppOp); |
Aravind Akella | d9441e4 | 2014-05-13 18:20:30 -0700 | [diff] [blame] | 439 | FlattenableUtils::read(buffer, size, mMaxDelay); |
Aravind Akella | 0e025c5 | 2014-06-03 19:19:57 -0700 | [diff] [blame] | 440 | FlattenableUtils::read(buffer, size, mFlags); |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 441 | return NO_ERROR; |
| 442 | } |
| 443 | |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 444 | void Sensor::flattenString8(void*& buffer, size_t& size, |
| 445 | const String8& string8) { |
Dan Stoza | d723bd7 | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 446 | uint32_t len = static_cast<uint32_t>(string8.length()); |
Aravind Akella | 7001804 | 2014-04-07 22:52:37 +0000 | [diff] [blame] | 447 | FlattenableUtils::write(buffer, size, len); |
| 448 | memcpy(static_cast<char*>(buffer), string8.string(), len); |
| 449 | FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len)); |
| 450 | } |
| 451 | |
| 452 | bool Sensor::unflattenString8(void const*& buffer, size_t& size, String8& outputString8) { |
| 453 | uint32_t len; |
| 454 | if (size < sizeof(len)) { |
| 455 | return false; |
| 456 | } |
| 457 | FlattenableUtils::read(buffer, size, len); |
| 458 | if (size < len) { |
| 459 | return false; |
| 460 | } |
| 461 | outputString8.setTo(static_cast<char const*>(buffer), len); |
| 462 | FlattenableUtils::advance(buffer, size, FlattenableUtils::align<4>(len)); |
| 463 | return true; |
| 464 | } |
| 465 | |
Mathias Agopian | 589ce85 | 2010-07-13 22:21:56 -0700 | [diff] [blame] | 466 | // ---------------------------------------------------------------------------- |
| 467 | }; // namespace android |