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_PLATFORM_PLATFORM_SENSOR_H_
18 #define CHRE_PLATFORM_PLATFORM_SENSOR_H_
19 
20 #include "chre/core/sensor_request.h"
21 #include "chre/target_platform/platform_sensor_base.h"
22 #include "chre/util/dynamic_vector.h"
23 #include "chre/util/non_copyable.h"
24 
25 namespace chre {
26 
27 /**
28  * Provides an interface to obtain a platform-independent description of a
29  * sensor. The PlatformSensorBase is subclassed here to allow platforms to
30  * inject their own storage for their implementation.
31  */
32 class PlatformSensor : public PlatformSensorBase,
33                        public NonCopyable {
34  public:
35   /**
36    * Default constructs a PlatformSensor.
37    */
38   PlatformSensor();
39 
40   /**
41    * Constructs a PlatformSensor by moving another.
42    *
43    * @param other The PlatformSensor to move.
44    */
45   PlatformSensor(PlatformSensor&& other);
46 
47   /**
48    * Destructs the PlatformSensor object.
49    */
50   ~PlatformSensor();
51 
52   /**
53    * Initializes the platform sensors subsystem. This must be called as part of
54    * the initialization of the runtime.
55    */
56   static void init();
57 
58   /**
59    * Obtains a list of the sensors that the platform provides. The supplied
60    * DynamicVector should be empty when passed in. If this method returns false
61    * the vector may be partially filled.
62    *
63    * @param sensors A non-null pointer to a DynamicVector to populate with the
64    *                list of sensors.
65    * @return Returns true if the query was successful.
66    */
67   static bool getSensors(DynamicVector<PlatformSensor> *sensors);
68 
69   /*
70    * Deinitializes the platform sensors subsystem. This must be called as part
71    * of the deinitialization of the runtime.
72    */
73   static void deinit();
74 
75   /**
76    * Sends the sensor request to the platform sensor. The implementation
77    * of this method is supplied by the platform. If the request is
78    * invalid/unsupported by this sensor, for example because it requests an
79    * interval that is too short, then this function must return false. If
80    * setting this new request fails due to a transient failure (example:
81    * inability to communicate with the sensor) false must also be returned.
82    *
83    * @param request The new request to set this sensor to.
84    * @return true if the platform sensor was successfully configured with the
85    *         supplied request.
86    */
87   bool setRequest(const SensorRequest& request);
88 
89   /**
90    * Obtains the SensorType of this platform sensor. The implementation of this
91    * method is supplied by the platform as the mechanism for determining the
92    * type may vary across platforms.
93    *
94    * @return The type of this sensor.
95    */
96   SensorType getSensorType() const;
97 
98   /**
99    * @return The minimum interval in nanoseconds of this sensor.
100    */
101   uint64_t getMinInterval() const;
102 
103   /**
104    * Returns the name (type and model) of this sensor.
105    *
106    * @return A pointer to a static string.
107    */
108   const char *getSensorName() const;
109 
110   /**
111    * @return Pointer to this sensor's last data event. It returns a nullptr if
112    *         the the platform doesn't provide it.
113    */
114   ChreSensorData *getLastEvent() const;
115 
116   /**
117    * Copies the supplied event to the sensor's last event.
118    *
119    * @param event The pointer to the event to copy from.
120    */
121   void setLastEvent(const ChreSensorData *event);
122 
123   /**
124    * Performs a move-assignment of a PlatformSensor.
125    *
126    * @param other The other PlatformSensor to move.
127    * @return a reference to this object.
128    */
129   PlatformSensor& operator=(PlatformSensor&& other);
130 };
131 
132 }  // namespace chre
133 
134 #endif  // CHRE_PLATFORM_PLATFORM_SENSOR_H_
135