1 /*
2 * Copyright (C) 2016 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 <general_test/basic_sensor_tests.h>
18
19 #include <shared/macros.h>
20 #include <shared/send_message.h>
21
22 #include "chre/util/macros.h"
23
24 namespace general_test {
25
checkFloat(float value,float extremeLow,float extremeHigh)26 static void checkFloat(float value, float extremeLow, float extremeHigh) {
27 if ((value < extremeLow) || (value > extremeHigh)) {
28 uint32_t i = static_cast<uint32_t>(value);
29 EXPECT_FAIL_RETURN("Value beyond extreme. As int:", &i);
30 }
31 }
32
checkTimestampDelta(uint32_t delta,size_t index)33 static void checkTimestampDelta(uint32_t delta, size_t index) {
34 if (index == 0) {
35 // This delta is allowed (and expected, but not required) to be 0.
36 return;
37 }
38 if (delta == 0) {
39 uint32_t indexInt = static_cast<uint32_t>(index);
40 EXPECT_FAIL_RETURN("timestampDelta was 0 for reading index ", &indexInt);
41 }
42 }
43
verifyThreeAxisData(const void * eventData,float extremeLow,float extremeHigh)44 static void verifyThreeAxisData(const void *eventData, float extremeLow,
45 float extremeHigh) {
46 auto data = static_cast<const chreSensorThreeAxisData *>(eventData);
47 for (size_t i = 0; i < data->header.readingCount; i++) {
48 checkTimestampDelta(data->readings[i].timestampDelta, i);
49 for (size_t j = 0; j < 3; j++) {
50 checkFloat(data->readings[i].values[j], extremeLow, extremeHigh);
51 }
52 }
53 }
54
verifyFloatData(const void * eventData,float extremeLow,float extremeHigh)55 static void verifyFloatData(const void *eventData, float extremeLow,
56 float extremeHigh) {
57 auto data = static_cast<const chreSensorFloatData *>(eventData);
58 for (size_t i = 0; i < data->header.readingCount; i++) {
59 checkTimestampDelta(data->readings[i].timestampDelta, i);
60 checkFloat(data->readings[i].value, extremeLow, extremeHigh);
61 }
62 }
63
confirmDataIsSane(const void * eventData)64 void BasicAccelerometerTest::confirmDataIsSane(const void *eventData) {
65 constexpr float kExtreme = 70.5f; // Apollo 16 on reentry (7.19g)
66 verifyThreeAxisData(eventData, -kExtreme, kExtreme);
67 }
68
confirmDataIsSane(const void * eventData)69 void BasicInstantMotionDetectTest::confirmDataIsSane(const void *eventData) {
70 UNUSED_VAR(eventData);
71 // Nothing to check.
72 }
73
confirmDataIsSane(const void * eventData)74 void BasicStationaryDetectTest::confirmDataIsSane(const void *eventData) {
75 UNUSED_VAR(eventData);
76 // Nothing to check.
77 }
78
confirmDataIsSane(const void * eventData)79 void BasicGyroscopeTest::confirmDataIsSane(const void *eventData) {
80 constexpr float kExtreme = 9420.0f; // Zippe centrifuge
81 verifyThreeAxisData(eventData, -kExtreme, kExtreme);
82 }
83
confirmDataIsSane(const void * eventData)84 void BasicMagnetometerTest::confirmDataIsSane(const void *eventData) {
85 constexpr float kExtreme = 9400000.0f; // Strength of medical MRI
86 verifyThreeAxisData(eventData, -kExtreme, kExtreme);
87 }
88
confirmDataIsSane(const void * eventData)89 void BasicBarometerTest::confirmDataIsSane(const void *eventData) {
90 constexpr float kExtremeLow = 337.0f; // Mount Everest summit
91 constexpr float kExtremeHigh = 1067.0f; // Dead Sea
92 verifyFloatData(eventData, kExtremeLow, kExtremeHigh);
93 }
94
confirmDataIsSane(const void * eventData)95 void BasicLightSensorTest::confirmDataIsSane(const void *eventData) {
96 constexpr float kExtreme = 300000.0f; // 3x the Sun
97 verifyFloatData(eventData, 0.0f, kExtreme);
98 }
99
confirmDataIsSane(const void * eventData)100 void BasicProximityTest::confirmDataIsSane(const void *eventData) {
101 auto data = static_cast<const chreSensorByteData *>(eventData);
102 for (size_t i = 0; i < data->header.readingCount; i++) {
103 checkTimestampDelta(data->readings[i].timestampDelta, i);
104 // 'invalid' is a sane reading for v1.1 or lower. But our padding should
105 // always be zero'd.
106 if (mApiVersion >= CHRE_API_VERSION_1_2 && data->readings[i].invalid) {
107 EXPECT_FAIL_RETURN("Invalid flag must not be set for proximity");
108 }
109 if (data->readings[i].padding0 != 0) {
110 uint32_t padding = data->readings[i].padding0;
111 EXPECT_FAIL_RETURN("padding0 is data is non-zero:", &padding);
112 }
113 }
114 }
115
116 } // namespace general_test
117