1 /*
2  * Copyright (C) 2019 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_POWER_TEST_REQUEST_MANAGER_H_
18 #define CHRE_POWER_TEST_REQUEST_MANAGER_H_
19 
20 #include <chre.h>
21 #include <cinttypes>
22 
23 #include "chre/util/singleton.h"
24 #include "chre/util/time.h"
25 #include "common.h"
26 
27 namespace chre {
28 
29 //! Handles requests coming in from the power test host app, enabling /
30 //! disabling various sensors as necessary.
31 class RequestManager {
32  public:
33   /**
34    * Processes a message from the host, performing the requested action(s).
35    *
36    * @param hostMessage the message data received from the host AP
37    * @return whether the message was processed successfully
38    */
39   bool handleMessageFromHost(const chreMessageFromHostData &hostMessage);
40 
41   /**
42    * Handles a timer event using the cookie to determine what action should be
43    * performed.
44    *
45    * @param cookie if non-null, contains an enum value corresponding to whatever
46    *     action should be performed when the timer fires
47    */
48   void handleTimerEvent(const void *cookie) const;
49 
50  private:
51   //! Indicates the source that initially set up the timer.
52   enum TimerType {
53     WAKEUP,
54     WIFI,
55     CELL,
56     NUM_TYPES,
57   };
58 
59   //! Holds the timer ID for each of the timer types.
60   uint32_t mTimerIds[TimerType::NUM_TYPES] = {CHRE_TIMER_INVALID};
61 
62   //! WiFi scan request parameters from host.
63   uint8_t mWifiScanType = CHRE_WIFI_SCAN_TYPE_NO_PREFERENCE;
64   uint8_t mWifiRadioChain = CHRE_WIFI_RADIO_CHAIN_PREF_DEFAULT;
65   uint8_t mWifiChannelSet = CHRE_WIFI_CHANNEL_SET_NON_DFS;
66 
67   /**
68    * Enables or disables break-it mode. When enabled, requests WiFi / GNSS /
69    * Cell data at one second intervals, buffers audio at the fastest possible
70    * rate and enables all sensors at their fastest sampling rates.
71    *
72    * @param enable whether to enable the break-it mode
73    * @return whether the request was successful
74    */
75   bool requestBreakIt(bool enable);
76 
77   /**
78    * Enables / disables audio sampling. If enabled, identifies the primary audio
79    * source's minimum buffer duration and requests audio at that rate.
80    *
81    * @param enable whether to enable audio sampling
82    * @return whether the request was successful
83    */
84   bool requestAudioAtFastestRate(bool enable) const;
85 
86   /**
87    * Enables / disables a repeating wakeup timer set to fire at the given rate.
88    *
89    * @param enable whether to enable the wakeup timer
90    * @param type the source of the timer request. Used as the cookie that is
91    *     given to the nanoapp when the timer fires
92    * @param delay amount of time, in nanoseconds, between each timer event
93    */
94   bool requestTimer(bool enable, TimerType type, Nanoseconds delay);
95 
96   /**
97    * Performs a Wifi scan. Should be invoked when a timer of TimerType::WIFI
98    * fires.
99    */
100   void wifiTimerCallback() const;
101 
102   /**
103    * Enables / disables GNSS location sampling.
104    *
105    * @param enable whether to enable GNSS location sampling
106    * @param scanIntervalMillis amount of time, in milliseconds, between each
107    *     GNSS location sample
108    * @param minTimeToNextFixMillis amount of time, in milliseconds, to wait
109    *     before generating the first location fix
110    * @return whether the request was successful
111    */
112   bool requestGnssLocation(bool enable, uint32_t scanIntervalMillis,
113                            uint32_t minTimeToNextFixMillis) const;
114 
115   /**
116    * Enables / disables GNSS measurement sampling.
117    *
118    * @param enable whether to enable GNSS measurement sampling
119    * @param intervalMillis amount of time, in milliseconds, between each
120    *     GNSS measurement sample
121    * @return whether the request was successful
122    */
123   bool requestGnssMeasurement(bool enable, uint32_t intervalMillis) const;
124 
125   /**
126    * Requests cell info. Should be invoked when a timer of TimerType::CELL
127    * fires.
128    */
129   void cellTimerCallback() const;
130 
131   /**
132    * Enables / disables sampling of audio.
133    *
134    * @param enable whether to enable audio sampling
135    * @param bufferDurationNs amount of time, in nanoseconds, to buffer audio
136    *     data before delivering to the nanoapp
137    * @return whether the request was successful
138    */
139   bool requestAudio(bool enable, uint64_t bufferDurationNs) const;
140 
141   /**
142    * Enables / disables sampling of a particular sensor.
143    *
144    * @param enable whether to enable sensor sampling
145    * @param sensorType the type of the sensor to configure
146    * @param samplingIntervalNs The sampling rate, in nanoseconds, to configure
147    *    the sensor to sample at
148    * @param latencyNs The latency, in nanoseconds, between batches of sensor
149    *    data. This controls how much data is batched before requiring a delivery
150    *    to the nanoapp
151    * @return whether the request was successful
152    */
153   bool requestSensor(bool enable, uint8_t sensorType,
154                      uint64_t samplingIntervalNs, uint64_t latencyNs) const;
155 
156   /**
157    * Enables / disables sampling of all sensors. If enabled, samples all
158    * available sensors at their fastest rate.
159    *
160    * @param enable whether to enable sensor sampling
161    * @return whether the request was successful
162    */
163   bool requestAllSensors(bool enable) const;
164 };
165 
166 }  // namespace chre
167 
168 typedef chre::Singleton<chre::RequestManager> RequestManagerSingleton;
169 
170 #endif  // CHRE_POWER_TEST_REQUEST_MANAGER_H_
171