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::StepDetect: 48 return "Step Detect"; 49 case SensorType::AccelerometerTemperature: 50 return "Accelerometer Temp"; 51 case SensorType::GyroscopeTemperature: 52 return "Gyroscope Temp"; 53 case SensorType::GeomagneticFieldTemperature: 54 return "Geomagnetic Field Temp"; 55 case SensorType::UncalibratedAccelerometer: 56 return "Uncal Accelerometer"; 57 case SensorType::UncalibratedGyroscope: 58 return "Uncal Gyroscope"; 59 case SensorType::UncalibratedGeomagneticField: 60 return "Uncal Geomagnetic Field"; 61 case SensorType::VendorType0: 62 return "Vendor Type 0"; 63 case SensorType::VendorType1: 64 return "Vendor Type 1"; 65 case SensorType::VendorType2: 66 return "Vendor Type 2"; 67 case SensorType::VendorType3: 68 return "Vendor Type 3"; 69 case SensorType::VendorType4: 70 return "Vendor Type 4"; 71 case SensorType::VendorType5: 72 return "Vendor Type 5"; 73 case SensorType::VendorType6: 74 return "Vendor Type 6"; 75 case SensorType::VendorType7: 76 return "Vendor Type 7"; 77 case SensorType::VendorType8: 78 return "Vendor Type 8"; 79 default: 80 CHRE_ASSERT(false); 81 return ""; 82 } 83 } 84 getSampleEventTypeForSensorType(SensorType sensorType)85uint16_t getSampleEventTypeForSensorType(SensorType sensorType) { 86 // The enum values of SensorType may not map to the defined values in the 87 // CHRE API. 88 uint8_t sensorTypeValue = getUnsignedIntFromSensorType(sensorType); 89 return CHRE_EVENT_SENSOR_DATA_EVENT_BASE + sensorTypeValue; 90 } 91 getSensorTypeForSampleEventType(uint16_t eventType)92SensorType getSensorTypeForSampleEventType(uint16_t eventType) { 93 return getSensorTypeFromUnsignedInt( 94 static_cast<uint8_t>(eventType - CHRE_EVENT_SENSOR_DATA_EVENT_BASE)); 95 } 96 getSensorTypeFromUnsignedInt(uint8_t sensorType)97SensorType getSensorTypeFromUnsignedInt(uint8_t sensorType) { 98 switch (sensorType) { 99 case CHRE_SENSOR_TYPE_ACCELEROMETER: 100 return SensorType::Accelerometer; 101 case CHRE_SENSOR_TYPE_INSTANT_MOTION_DETECT: 102 return SensorType::InstantMotion; 103 case CHRE_SENSOR_TYPE_STATIONARY_DETECT: 104 return SensorType::StationaryDetect; 105 case CHRE_SENSOR_TYPE_GYROSCOPE: 106 return SensorType::Gyroscope; 107 case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD: 108 return SensorType::GeomagneticField; 109 case CHRE_SENSOR_TYPE_PRESSURE: 110 return SensorType::Pressure; 111 case CHRE_SENSOR_TYPE_LIGHT: 112 return SensorType::Light; 113 case CHRE_SENSOR_TYPE_PROXIMITY: 114 return SensorType::Proximity; 115 case CHRE_SENSOR_TYPE_STEP_DETECT: 116 return SensorType::StepDetect; 117 case CHRE_SENSOR_TYPE_ACCELEROMETER_TEMPERATURE: 118 return SensorType::AccelerometerTemperature; 119 case CHRE_SENSOR_TYPE_GYROSCOPE_TEMPERATURE: 120 return SensorType::GyroscopeTemperature; 121 case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD_TEMPERATURE: 122 return SensorType::GeomagneticFieldTemperature; 123 case CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER: 124 return SensorType::UncalibratedAccelerometer; 125 case CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE: 126 return SensorType::UncalibratedGyroscope; 127 case CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD: 128 return SensorType::UncalibratedGeomagneticField; 129 case (CHRE_SENSOR_TYPE_VENDOR_START + 0): 130 return SensorType::VendorType0; 131 case (CHRE_SENSOR_TYPE_VENDOR_START + 1): 132 return SensorType::VendorType1; 133 case (CHRE_SENSOR_TYPE_VENDOR_START + 2): 134 return SensorType::VendorType2; 135 case (CHRE_SENSOR_TYPE_VENDOR_START + 3): 136 return SensorType::VendorType3; 137 case (CHRE_SENSOR_TYPE_VENDOR_START + 4): 138 return SensorType::VendorType4; 139 case (CHRE_SENSOR_TYPE_VENDOR_START + 5): 140 return SensorType::VendorType5; 141 case (CHRE_SENSOR_TYPE_VENDOR_START + 6): 142 return SensorType::VendorType6; 143 case (CHRE_SENSOR_TYPE_VENDOR_START + 7): 144 return SensorType::VendorType7; 145 case (CHRE_SENSOR_TYPE_VENDOR_START + 8): 146 return SensorType::VendorType8; 147 default: 148 return SensorType::Unknown; 149 } 150 } 151 getUnsignedIntFromSensorType(SensorType sensorType)152uint8_t getUnsignedIntFromSensorType(SensorType sensorType) { 153 switch (sensorType) { 154 case SensorType::Accelerometer: 155 return CHRE_SENSOR_TYPE_ACCELEROMETER; 156 case SensorType::InstantMotion: 157 return CHRE_SENSOR_TYPE_INSTANT_MOTION_DETECT; 158 case SensorType::StationaryDetect: 159 return CHRE_SENSOR_TYPE_STATIONARY_DETECT; 160 case SensorType::Gyroscope: 161 return CHRE_SENSOR_TYPE_GYROSCOPE; 162 case SensorType::GeomagneticField: 163 return CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD; 164 case SensorType::Pressure: 165 return CHRE_SENSOR_TYPE_PRESSURE; 166 case SensorType::Light: 167 return CHRE_SENSOR_TYPE_LIGHT; 168 case SensorType::Proximity: 169 return CHRE_SENSOR_TYPE_PROXIMITY; 170 case SensorType::StepDetect: 171 return CHRE_SENSOR_TYPE_STEP_DETECT; 172 case SensorType::AccelerometerTemperature: 173 return CHRE_SENSOR_TYPE_ACCELEROMETER_TEMPERATURE; 174 case SensorType::GyroscopeTemperature: 175 return CHRE_SENSOR_TYPE_GYROSCOPE_TEMPERATURE; 176 case SensorType::GeomagneticFieldTemperature: 177 return CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD_TEMPERATURE; 178 case SensorType::UncalibratedAccelerometer: 179 return CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER; 180 case SensorType::UncalibratedGyroscope: 181 return CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE; 182 case SensorType::UncalibratedGeomagneticField: 183 return CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD; 184 case SensorType::VendorType0: 185 return (CHRE_SENSOR_TYPE_VENDOR_START + 0); 186 case SensorType::VendorType1: 187 return (CHRE_SENSOR_TYPE_VENDOR_START + 1); 188 case SensorType::VendorType2: 189 return (CHRE_SENSOR_TYPE_VENDOR_START + 2); 190 case SensorType::VendorType3: 191 return (CHRE_SENSOR_TYPE_VENDOR_START + 3); 192 case SensorType::VendorType4: 193 return (CHRE_SENSOR_TYPE_VENDOR_START + 4); 194 case SensorType::VendorType5: 195 return (CHRE_SENSOR_TYPE_VENDOR_START + 5); 196 case SensorType::VendorType6: 197 return (CHRE_SENSOR_TYPE_VENDOR_START + 6); 198 case SensorType::VendorType7: 199 return (CHRE_SENSOR_TYPE_VENDOR_START + 7); 200 case SensorType::VendorType8: 201 return (CHRE_SENSOR_TYPE_VENDOR_START + 8); 202 default: 203 // Update implementation to prevent undefined or SensorType::Unknown from 204 // being used. 205 CHRE_ASSERT(false); 206 return 0; 207 } 208 } 209 getTempSensorType(SensorType sensorType)210SensorType getTempSensorType(SensorType sensorType) { 211 switch (sensorType) { 212 case SensorType::Accelerometer: 213 case SensorType::UncalibratedAccelerometer: 214 return SensorType::AccelerometerTemperature; 215 case SensorType::Gyroscope: 216 case SensorType::UncalibratedGyroscope: 217 return SensorType::GyroscopeTemperature; 218 case SensorType::GeomagneticField: 219 case SensorType::UncalibratedGeomagneticField: 220 return SensorType::GeomagneticFieldTemperature; 221 default: 222 return SensorType::Unknown; 223 } 224 } 225 getSensorSampleTypeFromSensorType(SensorType sensorType)226SensorSampleType getSensorSampleTypeFromSensorType(SensorType sensorType) { 227 switch (sensorType) { 228 case SensorType::Accelerometer: 229 case SensorType::Gyroscope: 230 case SensorType::GeomagneticField: 231 case SensorType::UncalibratedAccelerometer: 232 case SensorType::UncalibratedGyroscope: 233 case SensorType::UncalibratedGeomagneticField: 234 return SensorSampleType::ThreeAxis; 235 case SensorType::Pressure: 236 case SensorType::Light: 237 case SensorType::AccelerometerTemperature: 238 case SensorType::GyroscopeTemperature: 239 case SensorType::GeomagneticFieldTemperature: 240 return SensorSampleType::Float; 241 case SensorType::InstantMotion: 242 case SensorType::StationaryDetect: 243 case SensorType::StepDetect: 244 return SensorSampleType::Occurrence; 245 case SensorType::Proximity: 246 return SensorSampleType::Byte; 247 #ifdef CHREX_SENSOR_SUPPORT 248 case SensorType::VendorType0: 249 return SensorSampleType::Vendor0; 250 case SensorType::VendorType1: 251 return SensorSampleType::Vendor1; 252 case SensorType::VendorType2: 253 return SensorSampleType::Vendor2; 254 case SensorType::VendorType3: 255 return SensorSampleType::Vendor3; 256 case SensorType::VendorType4: 257 return SensorSampleType::Vendor4; 258 case SensorType::VendorType5: 259 return SensorSampleType::Vendor5; 260 case SensorType::VendorType6: 261 return SensorSampleType::Vendor6; 262 case SensorType::VendorType7: 263 return SensorSampleType::Vendor7; 264 case SensorType::VendorType8: 265 return SensorSampleType::Vendor8; 266 #endif // CHREX_SENSOR_SUPPORT 267 case SensorType::Unknown: 268 return SensorSampleType::Unknown; 269 default: 270 // Update implementation to prevent undefined from being used. 271 CHRE_ASSERT(false); 272 return SensorSampleType::Unknown; 273 } 274 } 275 getSensorModeFromEnum(enum chreSensorConfigureMode enumSensorMode)276SensorMode getSensorModeFromEnum(enum chreSensorConfigureMode enumSensorMode) { 277 switch (enumSensorMode) { 278 case CHRE_SENSOR_CONFIGURE_MODE_DONE: 279 return SensorMode::Off; 280 case CHRE_SENSOR_CONFIGURE_MODE_CONTINUOUS: 281 return SensorMode::ActiveContinuous; 282 case CHRE_SENSOR_CONFIGURE_MODE_ONE_SHOT: 283 return SensorMode::ActiveOneShot; 284 case CHRE_SENSOR_CONFIGURE_MODE_PASSIVE_CONTINUOUS: 285 return SensorMode::PassiveContinuous; 286 case CHRE_SENSOR_CONFIGURE_MODE_PASSIVE_ONE_SHOT: 287 return SensorMode::PassiveOneShot; 288 default: 289 // Default to off since it is the least harmful and has no power impact. 290 return SensorMode::Off; 291 } 292 } 293 sensorTypeIsOneShot(SensorType sensorType)294bool sensorTypeIsOneShot(SensorType sensorType) { 295 return (sensorType == SensorType::InstantMotion 296 || sensorType == SensorType::StationaryDetect 297 #ifdef CHREX_SENSOR_SUPPORT 298 || extension::vendorSensorTypeIsOneShot(sensorType) 299 #endif 300 ); 301 } 302 sensorTypeIsOnChange(SensorType sensorType)303bool sensorTypeIsOnChange(SensorType sensorType) { 304 return (sensorType == SensorType::Light 305 || sensorType == SensorType::Proximity 306 #ifdef CHREX_SENSOR_SUPPORT 307 || extension::vendorSensorTypeIsOnChange(sensorType) 308 #endif 309 ); 310 } 311 sensorTypeIsContinuous(SensorType sensorType)312bool sensorTypeIsContinuous(SensorType sensorType) { 313 return (!sensorTypeIsOneShot(sensorType) 314 && !sensorTypeIsOnChange(sensorType)); 315 } 316 sensorTypeReportsBias(SensorType sensorType)317bool sensorTypeReportsBias(SensorType sensorType) { 318 uint16_t eventType; 319 return getSensorBiasEventType(sensorType, &eventType); 320 } 321 getSensorBiasEventType(SensorType sensorType,uint16_t * eventType)322bool getSensorBiasEventType(SensorType sensorType, uint16_t *eventType) { 323 CHRE_ASSERT(eventType != nullptr); 324 bool success = false; 325 if (eventType != nullptr) { 326 success = true; 327 switch (sensorType) { 328 case SensorType::Accelerometer: 329 *eventType = CHRE_EVENT_SENSOR_ACCELEROMETER_BIAS_INFO; 330 break; 331 case SensorType::UncalibratedAccelerometer: 332 *eventType = CHRE_EVENT_SENSOR_UNCALIBRATED_ACCELEROMETER_BIAS_INFO; 333 break; 334 case SensorType::Gyroscope: 335 *eventType = CHRE_EVENT_SENSOR_GYROSCOPE_BIAS_INFO; 336 break; 337 case SensorType::UncalibratedGyroscope: 338 *eventType = CHRE_EVENT_SENSOR_UNCALIBRATED_GYROSCOPE_BIAS_INFO; 339 break; 340 case SensorType::GeomagneticField: 341 *eventType = CHRE_EVENT_SENSOR_GEOMAGNETIC_FIELD_BIAS_INFO; 342 break; 343 case SensorType::UncalibratedGeomagneticField: 344 *eventType = CHRE_EVENT_SENSOR_UNCALIBRATED_GEOMAGNETIC_FIELD_BIAS_INFO; 345 break; 346 default: 347 #ifdef CHREX_SENSOR_SUPPORT 348 success = 349 extension::vendorGetSensorBiasEventType(sensorType, eventType); 350 #else 351 success = false; 352 #endif 353 } 354 } 355 356 return success; 357 } 358 sensorTypeIsCalibrated(SensorType sensorType)359bool sensorTypeIsCalibrated(SensorType sensorType) { 360 return (sensorType == SensorType::Accelerometer 361 || sensorType == SensorType::Gyroscope 362 || sensorType == SensorType::GeomagneticField); 363 } 364 toCalibratedSensorType(SensorType sensorType)365SensorType toCalibratedSensorType(SensorType sensorType) { 366 switch (sensorType) { 367 case SensorType::UncalibratedAccelerometer: 368 return SensorType::Accelerometer; 369 case SensorType::UncalibratedGyroscope: 370 return SensorType::Gyroscope; 371 case SensorType::UncalibratedGeomagneticField: 372 return SensorType::GeomagneticField; 373 default: 374 /* empty */ 375 break; 376 } 377 378 return sensorType; 379 } 380 toUncalibratedSensorType(SensorType sensorType)381SensorType toUncalibratedSensorType(SensorType sensorType) { 382 switch (sensorType) { 383 case SensorType::Accelerometer: 384 return SensorType::UncalibratedAccelerometer; 385 case SensorType::Gyroscope: 386 return SensorType::UncalibratedGyroscope; 387 case SensorType::GeomagneticField: 388 return SensorType::UncalibratedGeomagneticField; 389 default: 390 /* empty */ 391 break; 392 } 393 394 return sensorType; 395 } 396 397 } // namespace chre 398