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