1 /* 2 * Copyright (C) 2017 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 17 #include "chre/core/sensor_type.h" 18 19 #include "chre/platform/assert.h" 20 21 #ifdef CHREX_SENSOR_SUPPORT 22 #include "chre/extensions/platform/vendor_sensor_types.h" 23 #endif // CHREX_SENSOR_SUPPORT 24 25 namespace chre { 26 getSensorTypeName(SensorType sensorType)27const char *getSensorTypeName(SensorType sensorType) { 28 switch (sensorType) { 29 case SensorType::Unknown: 30 return "Unknown"; 31 case SensorType::Accelerometer: 32 return "Accelerometer"; 33 case SensorType::InstantMotion: 34 return "Instant Motion"; 35 case SensorType::StationaryDetect: 36 return "Stationary Detect"; 37 case SensorType::Gyroscope: 38 return "Gyroscope"; 39 case SensorType::GeomagneticField: 40 return "Geomagnetic Field"; 41 case SensorType::Pressure: 42 return "Pressure"; 43 case SensorType::Light: 44 return "Light"; 45 case SensorType::Proximity: 46 return "Proximity"; 47 case SensorType::AccelerometerTemperature: 48 return "Accelerometer Temp"; 49 case SensorType::GyroscopeTemperature: 50 return "Gyroscope Temp"; 51 case SensorType::GeomagneticFieldTemperature: 52 return "Geomagnetic Field Temp"; 53 case SensorType::UncalibratedAccelerometer: 54 return "Uncal Accelerometer"; 55 case SensorType::UncalibratedGyroscope: 56 return "Uncal Gyroscope"; 57 case SensorType::UncalibratedGeomagneticField: 58 return "Uncal Geomagnetic Field"; 59 case SensorType::VendorType0: 60 return "Vendor Type 0"; 61 case SensorType::VendorType1: 62 return "Vendor Type 1"; 63 case SensorType::VendorType2: 64 return "Vendor Type 2"; 65 default: 66 CHRE_ASSERT(false); 67 return ""; 68 } 69 } 70 getSampleEventTypeForSensorType(SensorType sensorType)71uint16_t getSampleEventTypeForSensorType(SensorType sensorType) { 72 // The enum values of SensorType may not map to the defined values in the 73 // CHRE API. 74 uint8_t sensorTypeValue = getUnsignedIntFromSensorType(sensorType); 75 return CHRE_EVENT_SENSOR_DATA_EVENT_BASE + sensorTypeValue; 76 } 77 getSensorTypeForSampleEventType(uint16_t eventType)78SensorType getSensorTypeForSampleEventType(uint16_t eventType) { 79 return getSensorTypeFromUnsignedInt( 80 static_cast<uint8_t>(eventType - CHRE_EVENT_SENSOR_DATA_EVENT_BASE)); 81 } 82 getSensorTypeFromUnsignedInt(uint8_t sensorType)83SensorType getSensorTypeFromUnsignedInt(uint8_t sensorType) { 84 switch (sensorType) { 85 case CHRE_SENSOR_TYPE_ACCELEROMETER: 86 return SensorType::Accelerometer; 87 case CHRE_SENSOR_TYPE_INSTANT_MOTION_DETECT: 88 return SensorType::InstantMotion; 89 case CHRE_SENSOR_TYPE_STATIONARY_DETECT: 90 return SensorType::StationaryDetect; 91 case CHRE_SENSOR_TYPE_GYROSCOPE: 92 return SensorType::Gyroscope; 93 case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD: 94 return SensorType::GeomagneticField; 95 case CHRE_SENSOR_TYPE_PRESSURE: 96 return SensorType::Pressure; 97 case CHRE_SENSOR_TYPE_LIGHT: 98 return SensorType::Light; 99 case CHRE_SENSOR_TYPE_PROXIMITY: 100 return SensorType::Proximity; 101 case CHRE_SENSOR_TYPE_ACCELEROMETER_TEMPERATURE: 102 return SensorType::AccelerometerTemperature; 103 case CHRE_SENSOR_TYPE_GYROSCOPE_TEMPERATURE: 104 return SensorType::GyroscopeTemperature; 105 case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD_TEMPERATURE: 106 return SensorType::GeomagneticFieldTemperature; 107 case CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER: 108 return SensorType::UncalibratedAccelerometer; 109 case CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE: 110 return SensorType::UncalibratedGyroscope; 111 case CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD: 112 return SensorType::UncalibratedGeomagneticField; 113 case (CHRE_SENSOR_TYPE_VENDOR_START + 0): 114 return SensorType::VendorType0; 115 case (CHRE_SENSOR_TYPE_VENDOR_START + 1): 116 return SensorType::VendorType1; 117 case (CHRE_SENSOR_TYPE_VENDOR_START + 2): 118 return SensorType::VendorType2; 119 default: 120 return SensorType::Unknown; 121 } 122 } 123 getUnsignedIntFromSensorType(SensorType sensorType)124uint8_t getUnsignedIntFromSensorType(SensorType sensorType) { 125 switch (sensorType) { 126 case SensorType::Accelerometer: 127 return CHRE_SENSOR_TYPE_ACCELEROMETER; 128 case SensorType::InstantMotion: 129 return CHRE_SENSOR_TYPE_INSTANT_MOTION_DETECT; 130 case SensorType::StationaryDetect: 131 return CHRE_SENSOR_TYPE_STATIONARY_DETECT; 132 case SensorType::Gyroscope: 133 return CHRE_SENSOR_TYPE_GYROSCOPE; 134 case SensorType::GeomagneticField: 135 return CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD; 136 case SensorType::Pressure: 137 return CHRE_SENSOR_TYPE_PRESSURE; 138 case SensorType::Light: 139 return CHRE_SENSOR_TYPE_LIGHT; 140 case SensorType::Proximity: 141 return CHRE_SENSOR_TYPE_PROXIMITY; 142 case SensorType::AccelerometerTemperature: 143 return CHRE_SENSOR_TYPE_ACCELEROMETER_TEMPERATURE; 144 case SensorType::GyroscopeTemperature: 145 return CHRE_SENSOR_TYPE_GYROSCOPE_TEMPERATURE; 146 case SensorType::GeomagneticFieldTemperature: 147 return CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD_TEMPERATURE; 148 case SensorType::UncalibratedAccelerometer: 149 return CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER; 150 case SensorType::UncalibratedGyroscope: 151 return CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE; 152 case SensorType::UncalibratedGeomagneticField: 153 return CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD; 154 case SensorType::VendorType0: 155 return (CHRE_SENSOR_TYPE_VENDOR_START + 0); 156 case SensorType::VendorType1: 157 return (CHRE_SENSOR_TYPE_VENDOR_START + 1); 158 case SensorType::VendorType2: 159 return (CHRE_SENSOR_TYPE_VENDOR_START + 2); 160 default: 161 // Update implementation to prevent undefined or SensorType::Unknown from 162 // being used. 163 CHRE_ASSERT(false); 164 return 0; 165 } 166 } 167 getTempSensorType(SensorType sensorType)168SensorType getTempSensorType(SensorType sensorType) { 169 switch (sensorType) { 170 case SensorType::Accelerometer: 171 case SensorType::UncalibratedAccelerometer: 172 return SensorType::AccelerometerTemperature; 173 case SensorType::Gyroscope: 174 case SensorType::UncalibratedGyroscope: 175 return SensorType::GyroscopeTemperature; 176 case SensorType::GeomagneticField: 177 case SensorType::UncalibratedGeomagneticField: 178 return SensorType::GeomagneticFieldTemperature; 179 default: 180 return SensorType::Unknown; 181 } 182 } 183 getSensorSampleTypeFromSensorType(SensorType sensorType)184SensorSampleType getSensorSampleTypeFromSensorType(SensorType sensorType) { 185 switch (sensorType) { 186 case SensorType::Accelerometer: 187 case SensorType::Gyroscope: 188 case SensorType::GeomagneticField: 189 case SensorType::UncalibratedAccelerometer: 190 case SensorType::UncalibratedGyroscope: 191 case SensorType::UncalibratedGeomagneticField: 192 return SensorSampleType::ThreeAxis; 193 case SensorType::Pressure: 194 case SensorType::Light: 195 case SensorType::AccelerometerTemperature: 196 case SensorType::GyroscopeTemperature: 197 case SensorType::GeomagneticFieldTemperature: 198 return SensorSampleType::Float; 199 case SensorType::InstantMotion: 200 case SensorType::StationaryDetect: 201 return SensorSampleType::Occurrence; 202 case SensorType::Proximity: 203 return SensorSampleType::Byte; 204 case SensorType::VendorType0: 205 return SensorSampleType::Vendor0; 206 case SensorType::VendorType1: 207 return SensorSampleType::Vendor1; 208 case SensorType::VendorType2: 209 return SensorSampleType::Vendor2; 210 case SensorType::Unknown: 211 return SensorSampleType::Unknown; 212 default: 213 // Update implementation to prevent undefined from being used. 214 CHRE_ASSERT(false); 215 return SensorSampleType::Unknown; 216 } 217 } 218 getSensorModeFromEnum(enum chreSensorConfigureMode enumSensorMode)219SensorMode getSensorModeFromEnum(enum chreSensorConfigureMode enumSensorMode) { 220 switch (enumSensorMode) { 221 case CHRE_SENSOR_CONFIGURE_MODE_DONE: 222 return SensorMode::Off; 223 case CHRE_SENSOR_CONFIGURE_MODE_CONTINUOUS: 224 return SensorMode::ActiveContinuous; 225 case CHRE_SENSOR_CONFIGURE_MODE_ONE_SHOT: 226 return SensorMode::ActiveOneShot; 227 case CHRE_SENSOR_CONFIGURE_MODE_PASSIVE_CONTINUOUS: 228 return SensorMode::PassiveContinuous; 229 case CHRE_SENSOR_CONFIGURE_MODE_PASSIVE_ONE_SHOT: 230 return SensorMode::PassiveOneShot; 231 default: 232 // Default to off since it is the least harmful and has no power impact. 233 return SensorMode::Off; 234 } 235 } 236 sensorTypeIsOneShot(SensorType sensorType)237bool sensorTypeIsOneShot(SensorType sensorType) { 238 return (sensorType == SensorType::InstantMotion 239 || sensorType == SensorType::StationaryDetect 240 #ifdef CHREX_SENSOR_SUPPORT 241 || extension::vendorSensorTypeIsOneShot(sensorType) 242 #endif 243 ); 244 } 245 sensorTypeIsOnChange(SensorType sensorType)246bool sensorTypeIsOnChange(SensorType sensorType) { 247 return (sensorType == SensorType::Light 248 || sensorType == SensorType::Proximity 249 #ifdef CHREX_SENSOR_SUPPORT 250 || extension::vendorSensorTypeIsOnChange(sensorType) 251 #endif 252 ); 253 } 254 sensorTypeIsContinuous(SensorType sensorType)255bool sensorTypeIsContinuous(SensorType sensorType) { 256 return (!sensorTypeIsOneShot(sensorType) 257 && !sensorTypeIsOnChange(sensorType)); 258 } 259 260 } // namespace chre 261