1 /* 2 * Copyright (C) 2014 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 package android.hardware.cts.helpers; 18 19 import junit.framework.Assert; 20 21 import java.util.List; 22 import java.util.concurrent.TimeUnit; 23 24 /** 25 * A bundled sensor test operation/verification. 26 * 27 * It verifies the relationship between measurements from calibrated sensors and their corresponding 28 * uncalibrated sensors comply to the following equation: 29 * calibrated = uncalibrated - bias 30 */ 31 // TODO: refactor into proper test operation/verification classes: 32 // currently this is the only verification that requires input from multiple sensors, and the class 33 // is factored into: operation, verification, and listener as other sensor test operations 34 public class SensorCalibratedUncalibratedVerifier { 35 36 private final TestSensorManager mCalibratedSensorManager; 37 private final TestSensorManager mUncalibratedSensorManager; 38 private final TestSensorEventListener mCalibratedTestListener; 39 private final TestSensorEventListener mUncalibratedTestListener; 40 private final float mThreshold; 41 42 public SensorCalibratedUncalibratedVerifier( 43 TestSensorEnvironment calibratedEnvironment, 44 TestSensorEnvironment uncalibratedEnvironment, 45 float threshold) { 46 mCalibratedSensorManager = new TestSensorManager(calibratedEnvironment); 47 mUncalibratedSensorManager = new TestSensorManager(uncalibratedEnvironment); 48 mCalibratedTestListener = new TestSensorEventListener(calibratedEnvironment); 49 mUncalibratedTestListener = new TestSensorEventListener(uncalibratedEnvironment); 50 mThreshold = threshold; 51 } 52 53 /** 54 * Executes the operation: it collects the data and run verifications on it. 55 */ 56 public void execute() throws Throwable { 57 mCalibratedSensorManager.registerListener(mCalibratedTestListener); 58 mUncalibratedSensorManager.registerListener(mUncalibratedTestListener); 59 60 Thread.sleep(TimeUnit.SECONDS.toMillis(10)); 61 62 mCalibratedSensorManager.unregisterListener(); 63 mUncalibratedSensorManager.unregisterListener(); 64 65 verifyMeasurements( 66 mCalibratedTestListener.getCollectedEvents(), 67 mUncalibratedTestListener.getCollectedEvents(), 68 mThreshold); 69 } 70 71 private void verifyMeasurements( 72 List<TestSensorEvent> calibratedEvents, 73 List<TestSensorEvent> uncalibratedEvents, 74 float threshold) { 75 long measuredSamplingPeriodNs = SensorCtsHelper.getSamplingPeriodNs(calibratedEvents); 76 long synchronizationPeriodNs = measuredSamplingPeriodNs / 2; 77 int eventsValidated = 0; 78 79 // TODO: this makes the algorithm O(n^2) when we could have it O(n), but it has little 80 // impact on the overall test duration because the data collection is what takes the most 81 // time 82 for (TestSensorEvent calibratedEvent : calibratedEvents) { 83 long calibratedTimestampNs = calibratedEvent.timestamp; 84 long lowerTimestampThresholdNs = calibratedTimestampNs - synchronizationPeriodNs; 85 long upperTimestampThresholdNs = calibratedTimestampNs + synchronizationPeriodNs; 86 87 for (TestSensorEvent uncalibratedEvent : uncalibratedEvents) { 88 long uncalibratedTimestampNs = uncalibratedEvent.timestamp; 89 if (uncalibratedTimestampNs > lowerTimestampThresholdNs 90 && uncalibratedTimestampNs < upperTimestampThresholdNs) { 91 // perform validation 92 verifyCalibratedUncalibratedPair( 93 calibratedEvent, 94 uncalibratedEvent, 95 threshold); 96 ++eventsValidated; 97 } 98 } 99 } 100 101 String eventsValidatedMessage = String.format( 102 "Expected to find at least one Calibrated/Uncalibrated event pair for validation." 103 + " Found=%d", 104 eventsValidated); 105 Assert.assertTrue(eventsValidatedMessage, eventsValidated > 0); 106 } 107 108 private void verifyCalibratedUncalibratedPair( 109 TestSensorEvent calibratedEvent, 110 TestSensorEvent uncalibratedEvent, 111 float threshold) { 112 for (int i = 0; i < 3; ++i) { 113 float calibrated = calibratedEvent.values[i]; 114 float uncalibrated = uncalibratedEvent.values[i]; 115 float bias = uncalibratedEvent.values[i + 3]; 116 String message = String.format( 117 "Calibrated (%s) and Uncalibrated (%s) sensor readings are expected to satisfy:" 118 + " calibrated = uncalibrated - bias. Axis=%d, Calibrated=%s, " 119 + "Uncalibrated=%s, Bias=%s, Threshold=%s", 120 calibratedEvent.sensor.getName(), 121 uncalibratedEvent.sensor.getName(), 122 i, 123 calibrated, 124 uncalibrated, 125 bias, 126 threshold); 127 Assert.assertEquals(message, calibrated, uncalibrated - bias, threshold); 128 } 129 } 130 } 131