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