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