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/send_message.h>
20
21 using nanoapp_testing::sendFatalFailureToHost;
22
23 namespace general_test {
24
checkFloat(float value,float extremeLow,float extremeHigh)25 static void checkFloat(float value, float extremeLow, float extremeHigh) {
26 if ((value < extremeLow) || (value > extremeHigh)) {
27 uint32_t i = static_cast<uint32_t>(value);
28 sendFatalFailureToHost("Value beyond extreme. As int:", &i);
29 }
30 }
31
checkTimestampDelta(uint32_t delta,size_t index)32 static void checkTimestampDelta(uint32_t delta, size_t index) {
33 if (index == 0) {
34 // This delta is allowed (and expected, but not required) to be 0.
35 return;
36 }
37 if (delta == 0) {
38 uint32_t indexInt = static_cast<uint32_t>(index);
39 sendFatalFailureToHost("timestampDelta was 0 for reading index ",
40 &indexInt);
41 }
42 }
43
sanityCheckThreeAxisData(const void * eventData,float extremeLow,float extremeHigh)44 static void sanityCheckThreeAxisData(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
sanityCheckFloatData(const void * eventData,float extremeLow,float extremeHigh)55 static void sanityCheckFloatData(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 sanityCheckThreeAxisData(eventData, -kExtreme, kExtreme);
67 }
68
confirmDataIsSane(const void * eventData)69 void BasicInstantMotionDetectTest::confirmDataIsSane(const void* eventData) {
70 // Nothing to sanity check.
71 }
72
confirmDataIsSane(const void * eventData)73 void BasicStationaryDetectTest::confirmDataIsSane(const void* eventData) {
74 // Nothing to sanity check.
75 }
76
confirmDataIsSane(const void * eventData)77 void BasicGyroscopeTest::confirmDataIsSane(const void* eventData) {
78 constexpr float kExtreme = 9420.0f; // Zippe centrifuge
79 sanityCheckThreeAxisData(eventData, -kExtreme, kExtreme);
80 }
81
confirmDataIsSane(const void * eventData)82 void BasicMagnetometerTest::confirmDataIsSane(const void* eventData) {
83 constexpr float kExtreme = 9400000.0f; // Strength of medical MRI
84 sanityCheckThreeAxisData(eventData, -kExtreme, kExtreme);
85 }
86
confirmDataIsSane(const void * eventData)87 void BasicBarometerTest::confirmDataIsSane(const void* eventData) {
88 constexpr float kExtremeLow = 337.0f; // Mount Everest summit
89 constexpr float kExtremeHigh = 1067.0f; // Dead Sea
90 sanityCheckFloatData(eventData, kExtremeLow, kExtremeHigh);
91 }
92
confirmDataIsSane(const void * eventData)93 void BasicLightSensorTest::confirmDataIsSane(const void* eventData) {
94 constexpr float kExtreme = 300000.0f; // 3x the Sun
95 sanityCheckFloatData(eventData, 0.0f, kExtreme);
96 }
97
confirmDataIsSane(const void * eventData)98 void BasicProximityTest::confirmDataIsSane(const void* eventData) {
99 auto data = static_cast<const chreSensorByteData*>(eventData);
100 for (size_t i = 0; i < data->header.readingCount; i++) {
101 checkTimestampDelta(data->readings[i].timestampDelta, i);
102 // 'invalid' is a sane reading for v1.1 or lower. But our padding should
103 // always be zero'd.
104 if (mApiVersion >= CHRE_API_VERSION_1_2 && data->readings[i].invalid) {
105 sendFatalFailureToHost("Invalid flag must not be set for proximity");
106 }
107 if (data->readings[i].padding0 != 0) {
108 uint32_t padding = data->readings[i].padding0;
109 sendFatalFailureToHost("padding0 is data is non-zero:", &padding);
110 }
111 }
112 }
113
114
115 } // namespace general_test
116