xref: /aosp_15_r20/system/chre/apps/power_test/common/include/request_manager.h (revision 84e339476a462649f82315436d70fd732297a399)
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 <cinttypes>
21 
22 #include "chre/util/singleton.h"
23 #include "chre/util/time.h"
24 #include "chre_api/chre.h"
25 #include "common.h"
26 #include "generated/chre_power_test_generated.h"
27 
28 namespace chre {
29 
30 //! Handles requests coming in from the power test host app, enabling /
31 //! disabling various sensors as necessary.
32 class RequestManager {
33  public:
34   /**
35    * Processes a message from the host, performing the requested action(s).
36    *
37    * @param hostMessage the message data received from the host AP
38    */
39   void 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   /**
51    * Handles an event indicating the result of starting a NAN subscription.
52    */
53   void handleNanIdResult(const struct chreWifiNanIdentifierEvent *event);
54 
55   /**
56    * Requests NAN ranging using the provided discovery event to populate a NAN
57    * RTT config.
58    */
59   void requestNanRanging(const struct chreWifiNanDiscoveryEvent *event);
60 
61   /**
62    * Responds to a host request indicating whether the request was successfully
63    * executed.
64    *
65    * @param success whether the nanoapp successfully fulfilled a request
66    */
67   void sendResponseMessageToHost(bool success);
68 
69  private:
70   //! Indicates the source that initially set up the timer.
71   enum TimerType {
72     WAKEUP,
73     WIFI,
74     CELL,
75     NUM_TYPES,
76   };
77 
78   //! Holds the timer ID for each of the timer types.
79   uint32_t mTimerIds[TimerType::NUM_TYPES] = {CHRE_TIMER_INVALID};
80 
81   //! WiFi scan request parameters from host.
82   uint8_t mWifiScanType = CHRE_WIFI_SCAN_TYPE_NO_PREFERENCE;
83   uint8_t mWifiRadioChain = CHRE_WIFI_RADIO_CHAIN_PREF_DEFAULT;
84   uint8_t mWifiChannelSet = CHRE_WIFI_CHANNEL_SET_NON_DFS;
85 
86   //! The most recent host endpoint ID that communicated with the nanoapp.
87   uint16_t mLastHostEndpointId = CHRE_HOST_ENDPOINT_UNSPECIFIED;
88 
89   /**
90    * Enables or disables break-it mode. When enabled, requests WiFi / GNSS /
91    * Cell data at one second intervals, buffers audio at the fastest possible
92    * rate and enables all sensors at their fastest sampling rates.
93    *
94    * @param enable whether to enable the break-it mode
95    * @return whether the request was successful
96    */
97   bool requestBreakIt(bool enable);
98 
99   /**
100    * Enables / disables audio sampling. If enabled, identifies the primary audio
101    * source's minimum buffer duration and requests audio at that rate.
102    *
103    * @param enable whether to enable audio sampling
104    * @return whether the request was successful
105    */
106   bool requestAudioAtFastestRate(bool enable) const;
107 
108   /**
109    * Enables / disables a repeating wakeup timer set to fire at the given rate.
110    *
111    * @param enable whether to enable the wakeup timer
112    * @param type the source of the timer request. Used as the cookie that is
113    *     given to the nanoapp when the timer fires
114    * @param delay amount of time, in nanoseconds, between each timer event
115    */
116   bool requestTimer(bool enable, TimerType type, Nanoseconds delay);
117 
118   /**
119    * Performs a Wifi scan. Should be invoked when a timer of TimerType::WIFI
120    * fires.
121    */
122   void wifiTimerCallback() const;
123 
124   /**
125    * Enables / disables GNSS location sampling.
126    *
127    * @param enable whether to enable GNSS location sampling
128    * @param scanIntervalMillis amount of time, in milliseconds, between each
129    *     GNSS location sample
130    * @param minTimeToNextFixMillis amount of time, in milliseconds, to wait
131    *     before generating the first location fix
132    * @return whether the request was successful
133    */
134   bool requestGnssLocation(bool enable, uint32_t scanIntervalMillis,
135                            uint32_t minTimeToNextFixMillis) const;
136 
137   /**
138    * Enables / disables GNSS measurement sampling.
139    *
140    * @param enable whether to enable GNSS measurement sampling
141    * @param intervalMillis amount of time, in milliseconds, between each
142    *     GNSS measurement sample
143    * @return whether the request was successful
144    */
145   bool requestGnssMeasurement(bool enable, uint32_t intervalMillis) const;
146 
147   /**
148    * Requests cell info. Should be invoked when a timer of TimerType::CELL
149    * fires.
150    */
151   void cellTimerCallback() const;
152 
153   /**
154    * Enables / disables sampling of audio.
155    *
156    * @param enable whether to enable audio sampling
157    * @param bufferDurationNs amount of time, in nanoseconds, to buffer audio
158    *     data before delivering to the nanoapp
159    * @return whether the request was successful
160    */
161   bool requestAudio(bool enable, uint64_t bufferDurationNs) const;
162 
163   /**
164    * Enables / disables sampling of a particular sensor.
165    *
166    * @param enable whether to enable sensor sampling
167    * @param sensorType the type of the sensor to configure
168    * @param samplingIntervalNs The sampling rate, in nanoseconds, to configure
169    *    the sensor to sample at
170    * @param latencyNs The latency, in nanoseconds, between batches of sensor
171    *    data. This controls how much data is batched before requiring a delivery
172    *    to the nanoapp
173    * @return whether the request was successful
174    */
175   bool requestSensor(bool enable, uint8_t sensorType,
176                      uint64_t samplingIntervalNs, uint64_t latencyNs) const;
177 
178   /**
179    * Enables / disables sampling of all sensors. If enabled, samples all
180    * available sensors at their fastest rate.
181    *
182    * @param enable whether to enable sensor sampling
183    * @return whether the request was successful
184    */
185   bool requestAllSensors(bool enable) const;
186 
187   /**
188    * Requests a WiFi NAN subscription using the provided params.
189    *
190    * @param msg Message from the host containing the appropriate WiFi NAN params
191    * @return whether the request is successful.
192    */
193   bool requestWifiNanSub(const power_test::WifiNanSubMessage *msg);
194 
195   /**
196    * Cancels an existing WiFi NAN subscription.
197    *
198    * @param subscriptionId Previous ID provided from a started subscription
199    * @return whether the request is successful.
200    */
201   bool cancelWifiNanSub(uint32_t subscriptionId);
202 };
203 
204 }  // namespace chre
205 
206 typedef chre::Singleton<chre::RequestManager> RequestManagerSingleton;
207 
208 #endif  // CHRE_POWER_TEST_REQUEST_MANAGER_H_
209