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 #ifndef _GTS_NANOAPPS_GENERAL_TEST_BASIC_SENSOR_TEST_BASE_H_
18 #define _GTS_NANOAPPS_GENERAL_TEST_BASIC_SENSOR_TEST_BASE_H_
19 
20 #include <general_test/test.h>
21 
22 #include "chre/util/optional.h"
23 
24 #include <chre.h>
25 
26 namespace general_test {
27 
28 /**
29  * Abstract base class for basic sensor tests.
30  *
31  * This repeats a similar test for several different sensor types.  Children
32  * classes must implement the abstract methods to define details about the
33  * sensor.
34  *
35  * This uses a Simple Protocol between the Host and Nanoapp.
36  */
37 class BasicSensorTestBase : public Test {
38  public:
39   BasicSensorTestBase();
40 
41  protected:
42   void handleEvent(uint32_t senderInstanceId, uint16_t eventType,
43                    const void *eventData) override;
44   void setUp(uint32_t messageSize, const void *message) override;
45 
46   /**
47    * Sends a message to itself to trigger startTest();
48    */
49   void sendStartTestMessage();
50 
51   /**
52    * Abstract method indicating which sensor type this is.
53    *
54    * @returns One of the CHRE_SENSOR_TYPE_* constants.
55    */
56   virtual uint8_t getSensorType() const = 0;
57 
58   /**
59    * Abstract method indicating if this is an on-change sensor.
60    *
61    * @returns true if this sensor is on-change; false otherwise.
62    */
63   virtual bool isOnChangeSensor() const = 0;
64 
65   /**
66    * Abstract method indicating if this is a one-shot sensor.
67    *
68    * @returns true if this sensor is one-shot; false otherwise.
69    */
70   virtual bool isOneShotSensor() const = 0;
71 
72   /**
73    * Abstract method which makes sure the given data is sane.
74    *
75    * This is a very loose test, and some sensors may provide no checking
76    * at all here.  But some sensor might be able to provide a basic check
77    * (for example, a barometer claiming 0 hPa is broken (assuming the tests
78    * aren't running in outer space)).
79    *
80    * @returns If the data is absurd, this function will not return (it
81    *     will trigger a fatal error report).  This function returning
82    *     is evidence the data is sane.
83    */
84   virtual void confirmDataIsSane(const void *eventData) = 0;
85 
86  private:
87   enum State {
88     kPreStart,
89     kPreConfigure,
90     kExpectingInitialDataEvent,
91     kExpectingLastDataEvent,
92     kFinished
93   };
94 
95   // Catch if CHRE performs reentrant calls for handleEvent()
96   bool mInMethod;
97   // If some external user changes the sampling status of our sensor,
98   // we shouldn't perform some of the checking, because it will be flaky.
99   bool mExternalSamplingStatusChange;
100   State mState;
101   uint32_t mInstanceId;
102   uint32_t mSensorHandle;
103   uint64_t mPreTimestamp;
104   uint64_t mFirstEventTimestamp;
105   uint64_t mLastEventTimestamp;
106   uint64_t mDoneTimestamp;
107   chreSensorSamplingStatus mOriginalStatus;
108   chreSensorSamplingStatus mNewStatus;
109 
110   bool mSupportsPassiveMode = true;
111 
112   // The current sensor index that we are testing for.
113   uint8_t mCurrentSensorIndex = 0;
114 
115   // The sensor handle for the previous sensor tested.
116   chre::Optional<uint32_t> mPrevSensorHandle;
117 
118   void startTest();
119   void finishTest();
120   void checkPassiveConfigure();
121   void handleBiasEvent(uint16_t eventType,
122                        const chreSensorThreeAxisData *eventData);
123   void handleSamplingChangeEvent(
124       const chreSensorSamplingStatusEvent *eventData);
125   void handleSensorDataEvent(uint16_t eventType, const void *eventData);
126   void verifyEventHeader(const chreSensorDataHeader *header, uint16_t eventType,
127                          uint64_t eventDuration);
128 };
129 
130 }  // namespace general_test
131 
132 #endif  // _GTS_NANOAPPS_GENERAL_TEST_BASIC_SENSOR_TEST_BASE_H_
133