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