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_REQUEST_MANAGER_H_
18 #define CHRE_CORE_SENSOR_REQUEST_MANAGER_H_
19 
20 #include "chre/core/request_multiplexer.h"
21 #include "chre/core/sensor.h"
22 #include "chre/core/sensor_request.h"
23 #include "chre/util/fixed_size_vector.h"
24 #include "chre/util/non_copyable.h"
25 #include "chre/util/optional.h"
26 
27 namespace chre {
28 
29 class SensorRequestManager : public NonCopyable {
30  public:
31   /**
32    * Performs initialization of the SensorRequestManager and populates the
33    * sensor list with platform sensors.
34    */
35   SensorRequestManager();
36 
37   /**
38    * Destructs the sensor request manager and releases platform sensor resources
39    * if requested.
40    */
41   ~SensorRequestManager();
42 
43   /**
44    * Determines whether the runtime is aware of a given sensor type. The
45    * supplied sensorHandle is only populated if the sensor type is known.
46    *
47    * @param sensorType The type of the sensor.
48    * @param sensorHandle A non-null pointer to a uint32_t to use as a sensor
49    *                     handle for nanoapps.
50    * @return true if the supplied sensor type is available for use.
51    */
52   bool getSensorHandle(SensorType sensorType, uint32_t *sensorHandle) const;
53 
54   /**
55    * Sets a sensor request for the given nanoapp for the provided sensor handle.
56    * If the nanoapp has made a previous request, it is replaced by this request.
57    * If the request changes the mode to SensorMode::Off the request is removed.
58    *
59    * @param nanoapp A non-null pointer to the nanoapp requesting this change.
60    * @param sensorHandle The sensor handle for which this sensor request is
61    *        directed at.
62    * @param request The new sensor request for this nanoapp.
63    * @return true if the request was set successfully. If the sensorHandle is
64    *         out of range or the platform sensor fails to update to the new
65    *         request false will be returned.
66    */
67   bool setSensorRequest(Nanoapp *nanoapp, uint32_t sensorHandle,
68                         const SensorRequest& sensorRequest);
69 
70   /**
71    * Populates the supplied info struct if the sensor handle exists.
72    *
73    * @param sensorHandle The handle of the sensor.
74    * @param nanoapp A non-null pointer to the nanoapp requesting this change.
75    * @param info A non-null pointer to a chreSensorInfo struct.
76    * @return true if the supplied sensor handle exists.
77    */
78   bool getSensorInfo(uint32_t sensorHandle, const Nanoapp *nanoapp,
79                      struct chreSensorInfo *info) const;
80   /*
81    * Removes all requests of a sensorType and unregisters all nanoapps for its
82    * events.
83    *
84    * @param sensorType The sensor type whose requests are to be removed.
85    * @return true if all requests of the sensor type have been successfully
86    *         removed.
87    */
88   bool removeAllRequests(SensorType sensorType);
89 
90   /**
91    * Obtains a pointer to the Sensor of the specified sensorType.
92    *
93    * @param sensorType The SensorType of the sensor.
94    * @return A pointer to the Sensor of sensorType. It returns a nullptr if
95    *         sensorType is SensorType::Unknown.
96    */
97   Sensor *getSensor(SensorType sensorType);
98 
99  private:
100   /**
101    * This allows tracking the state of a sensor with the various requests for it
102    * and can trigger a change in mode/rate/latency when required.
103    */
104   struct SensorRequests {
105     //! The sensor associated with this request multiplexer.
106     Sensor sensor;
107 
108     //! The request multiplexer for this sensor.
109     RequestMultiplexer<SensorRequest> multiplexer;
110 
111     /**
112      * Searches through the list of sensor requests for a request owned by the
113      * given nanoapp. The provided non-null index pointer is populated with the
114      * index of the request if it is found.
115      *
116      * @param nanoapp The nanoapp whose request is being searched for.
117      * @param index A non-null pointer to an index that is populated if a request
118      *        for this nanoapp is found.
119      * @return A pointer to a SensorRequest that is owned by the provided nanoapp
120      *         if one is found otherwise nullptr.
121      */
122     const SensorRequest *find(const Nanoapp *nanoapp, size_t *index) const;
123 
124     /**
125      * Adds a new sensor request to the request multiplexer for this sensor.
126      *
127      * @param request The request to add to the multiplexer.
128      * @param requestChanged A non-null pointer to a bool to indicate that the
129      *        net request made to the sensor has changed. This boolean is always
130      *        assigned to the status of the request changing (true or false).
131      * @return true if the add operation was successful.
132      */
133     bool add(const SensorRequest& request, bool *requestChanged);
134 
135     /**
136      * Removes a sensor request from the request multiplexer for this sensor.
137      * The provided index must fall in the range of the sensor requests managed
138      * by the multiplexer.
139      *
140      * @param removeIndex The index to remove the request from.
141      * @param requestChanged A non-null pointer to a bool to indicate that the
142      *        net request made to the sensor has changed. This boolean is always
143      *        assigned to the status of the request changing (true or false).
144      * @return true if the remove operation was successful.
145      */
146     bool remove(size_t removeIndex, bool *requestChanged);
147 
148     /**
149      * Updates a sensor request in the request multiplexer for this sensor. The
150      * provided index must fall in range of the sensor requests managed by the
151      * multiplexer.
152      *
153      * @param updateIndex The index to update the request at.
154      * @param request The new sensor request to replace the existing request
155      *        with.
156      * @return true if the update operation was successful.
157      */
158     bool update(size_t updateIndex, const SensorRequest& request,
159                 bool *requestChanged);
160 
161     /**
162      * Removes all requests and consolidates all the maximal request changes
163      * into one sensor configuration update.
164      *
165      * @return true if all the requests have been removed and sensor
166      *         configuration successfully updated.
167      */
168     bool removeAll();
169   };
170 
171   //! The list of sensor requests
172   FixedSizeVector<SensorRequests, getSensorTypeCount()> mSensorRequests;
173 };
174 
175 }  // namespace chre
176 
177 #endif  // CHRE_CORE_SENSOR_REQUEST_MANAGER_H_
178