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 CHRE_CORE_SENSOR_H_
18 #define CHRE_CORE_SENSOR_H_
19 
20 #include "chre/platform/platform_sensor.h"
21 #include "chre/util/non_copyable.h"
22 #include "chre/util/optional.h"
23 
24 namespace chre {
25 
26 class Sensor : public NonCopyable {
27  public:
28   /**
29    * Default constructs a Sensor with an unknown sensor type.
30    */
31   Sensor();
32 
33   /**
34    * Constructs a Sensor by moving a PlatformSensor.
35    *
36    * @param platformSensor The platform implementation of this Sensor.
37    */
38   Sensor(PlatformSensor&& platformSensor);
39 
40   /**
41    * @return The type of this sensor.
42    */
43   SensorType getSensorType() const;
44 
45   /**
46    * @return true if this Sensor instance has an instance of the underlying
47    * PlatformSensor. This is useful to determine if this sensor is supplied by
48    * the platform.
49    */
50   bool isValid() const;
51 
52   /**
53    * Sets the current request of this sensor. If this request is a change from
54    * the previous request, it is sent to the underlying platform. If isValid()
55    * returns false this function will also return false and do nothing.
56    *
57    * @param request The new request for this sensor.
58    * @return true if there was no change required or the platform has set the
59    *         request successfully.
60    */
61   bool setRequest(const SensorRequest& request);
62 
63   /**
64    * Performs a move-assignment of a Sensor.
65    *
66    * @param other The other sensor to move.
67    * @return a reference to this object.
68    */
69   Sensor& operator=(Sensor&& other);
70 
71   /**
72    * @return The minimal interval in nanoseconds of this sensor.
73    */
74   uint64_t getMinInterval() const;
75 
76   /**
77    * @return The name (type and model) of this sensor.
78    */
79   const char *getSensorName() const;
80 
81   /**
82    * @return Pointer to this sensor's last event. It returns a nullptr if the
83    * the platform doesn't provide it or the last data event is invalid.
84    */
85   ChreSensorData *getLastEvent() const;
86 
87   /**
88    * Copies the supplied event to the sensor's last event.
89    *
90    * @param event The pointer to the event to copy from.
91    */
92   void setLastEvent(const ChreSensorData *event);
93 
94  private:
95   //! The most recent sensor request sent to this sensor.
96   SensorRequest mSensorRequest;
97 
98   //! The validity of this sensor's last event.
99   bool mLastEventValid = false;
100 
101   //! The underlying platform sensor that is managed by this common interface.
102   Optional<PlatformSensor> mPlatformSensor;
103 };
104 
105 }  // namespace chre
106 
107 #endif  // CHRE_CORE_SENSOR_H_
108