1 /*
2  * Copyright (C) 2018 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 #ifndef ANDROID_SENSOR_EVENTS_CHECKER_H
18 #define ANDROID_SENSOR_EVENTS_CHECKER_H
19 
20 #include <android/hardware/sensors/1.0/types.h>
21 
22 #include <cmath>
23 
24 class SensorEventsChecker {
25    public:
26     using Event = ::android::hardware::sensors::V1_0::Event;
27     virtual bool check(const std::vector<Event>& events, std::string* out) const = 0;
~SensorEventsChecker()28     virtual ~SensorEventsChecker() {}
29 };
30 
31 class NullChecker : public SensorEventsChecker {
32    public:
check(const std::vector<Event> &,std::string *)33     virtual bool check(const std::vector<Event>&, std::string*) const { return true; }
34 };
35 
36 class SensorEventPerEventChecker : public SensorEventsChecker {
37    public:
38     virtual bool checkEvent(const Event& event, std::string* out) const = 0;
check(const std::vector<Event> & events,std::string * out)39     virtual bool check(const std::vector<Event>& events, std::string* out) const {
40         for (const auto& e : events) {
41             if (!checkEvent(e, out)) {
42                 return false;
43             }
44         }
45         return true;
46     }
47 };
48 
49 class Vec3NormChecker : public SensorEventPerEventChecker {
50    public:
Vec3NormChecker(float min,float max)51     Vec3NormChecker(float min, float max) : mLowerLimit(min), mUpperLimit(max) {}
byNominal(float nominal,float allowedError)52     static Vec3NormChecker byNominal(float nominal, float allowedError) {
53         return Vec3NormChecker(nominal - allowedError, nominal + allowedError);
54     }
55 
checkEvent(const Event & event,std::string * out)56     virtual bool checkEvent(const Event& event, std::string* out) const {
57         android::hardware::sensors::V1_0::Vec3 v = event.u.vec3;
58         float norm = std::sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
59         if (norm < mLowerLimit || norm > mUpperLimit) {
60             if (out != nullptr) {
61                 std::ostringstream ss;
62                 ss << "Event @ " << event.timestamp << " (" << v.x << ", " << v.y << ", " << v.z
63                    << ")"
64                    << " has norm " << norm << ", which is beyond range"
65                    << " [" << mLowerLimit << ", " << mUpperLimit << "]";
66                 *out = ss.str();
67             }
68             return false;
69         }
70         return true;
71     }
72 
73    protected:
74     float mLowerLimit;
75     float mUpperLimit;
76 };
77 
78 #endif  // ANDROID_SENSOR_EVENTS_CHECKER_H