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