blob: 68e3632255127c75b3a841217410912c6099ccf0 [file] [log] [blame]
Clay Murphy4ea104f2013-10-28 17:44:33 -07001page.title=Batching sensor results
2@jd:body
3
4<!--
5 Copyright 2013 The Android Open Source Project
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18-->
19<div id="qv-wrapper">
20 <div id="qv">
21 <h2>In this document</h2>
22 <ol id="auto-toc">
23 </ol>
24 </div>
25</div>
26
27<h2 id="Why">Why batch?</h2>
Clay Murphy69cac8e2013-11-01 14:10:12 -070028<p>This page presents the specificities of Batch mode and the expected behaviors
29 of sensors while in batch mode. Batching can enable significant power savings by
30 preventing the application processor from waking up to receive each event. Instead, these
31 events can be grouped and processed together.</p>
32<h2 id="batch-function">batch(int handle, int flags, int64_t period_ns, int64_t
33 max_report_latency)</h2>
34<p>Enabling batch mode for a given sensor sets the delay between events.
35 max_report_latency sets the maximum time by which events can be delayed and
36 batched together before being reported to the applications. A value of zero
37 disables batch mode for the given sensor. The period_ns parameter is equivalent
38 to calling setDelay() -- this function both enables or disables the batch mode
39 AND sets the event's period in nanoseconds. See setDelay() for a detailed
40 explanation of the period_ns parameter.</p>
Clay Murphy4ea104f2013-10-28 17:44:33 -070041<p>In non-batch mode, all sensor events must be reported as soon as they are
42 detected. For example, an accelerometer activated at 50Hz will trigger
43 interrupts 50 times per second.<br/>
44 While in batch mode, sensor events do not need to be reported as soon as they
45 are detected. They can be temporarily stored and reported in batches, as long as
Clay Murphy69cac8e2013-11-01 14:10:12 -070046 no event is delayed by more than &quot;maxReportingLatency&quot; nanoseconds. That is, all events
Clay Murphy4ea104f2013-10-28 17:44:33 -070047 since the previous batch are recorded and returned at once. This reduces the
48 amount of interrupts sent to the SoC and allows the SoC to switch to a lower
49 power mode (idle) while the sensor is capturing and batching data.</p>
50<p>setDelay() is not affected and it behaves as usual. <br/>
51 <br/>
52 Each event has a timestamp associated with it. The timestamp must be accurate
Clay Murphy69cac8e2013-11-01 14:10:12 -070053 and correspond to the time at which the event physically happened.</p>
Clay Murphy4ea104f2013-10-28 17:44:33 -070054<p>Batching does not modify the behavior of poll(): batches from different sensors
55 can be interleaved and split. As usual, all events from the same sensor are
56 time-ordered.</p>
Clay Murphy69cac8e2013-11-01 14:10:12 -070057<h2 id="Suspend">Behavior outside of suspend mode</h2>
Clay Murphy4ea104f2013-10-28 17:44:33 -070058<p>These are the power modes of the application processor: on, idle, and suspend.
59 The sensors behave differently in each of these modes. As you would imagine, on
Clay Murphy69cac8e2013-11-01 14:10:12 -070060 mode is when the application processor is running. Idle mode is a medium power mode
61 where the application processor is powered but doesn't perform any tasks.
62 Suspend is a low-power mode where the application processor is not powered. The
63 power consumption of the device in this mode is usually 100 times less than in the On
64 mode.</p>
Clay Murphy4ea104f2013-10-28 17:44:33 -070065<p>When the SoC is awake (not in suspend mode), events must be reported in batches
Clay Murphy69cac8e2013-11-01 14:10:12 -070066 at least every maxReportingLatency. No event shall be dropped or lost. If internal
67 hardware FIFOs fill up before the maxReportingLatency, then events are reported at that
Clay Murphy4ea104f2013-10-28 17:44:33 -070068 point to ensure no event is lost.</p>
69<h2 id="Normal">Normal behavior in suspend mode</h2>
70<p>By default, batch mode doesn't significantly change the interaction with suspend
71 mode. That is, sensors must continue to allow the SoC to go into suspend mode
72 and sensors must stay active to fill their internal FIFO. In this mode, when the
73 FIFO fills up, it shall wrap around and behave like a circular buffer,
74 overwriting older events.<br/>
75 <br/>
76 As soon as the SoC comes out of suspend mode, a batch is produced with as much
77as the recent history as possible, and batch operation resumes as usual.</p>
78<p>The behavior described above allows applications to record the recent history of
79 a set of sensor types while keeping the SoC in suspend. It also allows the
80 hardware to not have to rely on a wake-up interrupt line.</p>
81<h2 id="WAKE_UPON_FIFO_FULL">WAKE_UPON_FIFO_FULL behavior in suspend mode</h2>
82<p>There are cases, however, where an application cannot afford to lose any events,
83 even when the device goes into suspend mode.</p>
84<p>For a given rate, if a sensor has the capability to store at least 10 seconds
85 worth of events in its FIFO and is able to wake up the SoC, it can implement an
86 optional secondary mode: the WAKE_UPON_FIFO_FULL mode.</p>
87<p>The caller will set the SENSORS_BATCH_WAKE_UPON_FIFO_FULL flag to activate this
88 mode. If the sensor does not support this mode, batch() will fail when the flag
89 is set.</p>
Clay Murphy69cac8e2013-11-01 14:10:12 -070090<p>In batch mode, and only when the flag SENSORS_BATCH_WAKE_UPON_FIFO_FULL is
91 set and supported, the specified sensor must be able to wake-up the SoC and be
92 able to buffer at least 10 seconds worth of the requested sensor events.</p>
Clay Murphy4ea104f2013-10-28 17:44:33 -070093<p>When running with the WAKE_UPON_FIFO_FULL flag set, no events can be lost. When
94 the FIFO is getting full, the sensor must wake up the SoC from suspend and
95 return a batch before the FIFO fills-up.</p>
96<p>Depending on the device, it might take a few milliseconds for the SoC to
97 entirely come out of suspend and start flushing the FIFO. Enough head room must
98 be allocated in the FIFO to allow the device to entirely come out of suspend
99 without the FIFO overflowing (no events shall be lost).</p>
100<p>Implementing the WAKE_UPON_FIFO_FULL mode is optional. If the hardware cannot
101 support this mode, or if the physical FIFO is so small that the device would
Clay Murphy69cac8e2013-11-01 14:10:12 -0700102 never be allowed to go into suspend for at least 10 seconds, then this function
103 <strong>must</strong> fail when the flag SENSORS_BATCH_WAKE_UPON_FIFO_FULL is set, regardless
104 of the value of the maxReportingLatency parameter.</p>
Clay Murphy4ea104f2013-10-28 17:44:33 -0700105<h2 id="Implementing">Implementing batching</h2>
106<p>Batch mode, if supported, should happen at the hardware level, typically using
107 hardware FIFOs. In particular, it SHALL NOT be implemented in the HAL, as this
108 would be counter productive. The goal here is to save significant amounts of
109 power. Batching should be implemented without the aid of the SoC, which should
110 be allowed to be in suspend mode during batching.</p>
111<p>In some implementations, events from several sensors can share the same physical
112 FIFO. In that case, all events in the FIFO can be sent and processed by the HAL
113 as soon as one batch must be reported.</p>
114<p>For example, if the following sensors are activated:</p>
115<ul>
Clay Murphy69cac8e2013-11-01 14:10:12 -0700116 <li>accelerometer batched with maxReportingLatency = 20s</li>
117 <li>gyroscope batched with maxReportingLatency = 5s</li>
Clay Murphy4ea104f2013-10-28 17:44:33 -0700118</ul>
119<p>Then the accelerometer batches can be reported at the same time the gyroscope
120 batches are reported (every 5 seconds).<br/>
121 <br/>
122 Batch mode can be enabled or disabled at any time, in particular while the
123 specified sensor is already enabled; and this shall not result in the loss of
124 events.</p>
Clay Murphy69cac8e2013-11-01 14:10:12 -0700125<h2 id="fifo-allocation">FiFo allocation priority</h2>
Clay Murphy4ea104f2013-10-28 17:44:33 -0700126<p>On platforms in which hardware FIFO size is limited, the system designers may
127 have to choose how much FIFO to reserve for each sensor. To help with this
128 choice, here is a list of applications made possible when batching is
129 implemented on the different sensors.</p>
130<p><strong>High value: Low power pedestrian dead reckoning</strong><br/>
131 Target batching time: 20 seconds to 1 minute<br/>
132 Sensors to batch:<br/>
133 - Step detector<br/>
134 - Rotation vector or game rotation vector at 5Hz<br/>
135 Gives us step and heading while letting the SoC go to Suspend.<br/>
136 <br/>
137 <strong>High value: Medium power activity/gesture recognition</strong><br/>
138 Target batching time: 3 seconds<br/>
139 Sensors to batch: accelerometer between 20Hz and 50Hz<br/>
140 Allows recognizing arbitrary activities and gestures without having<br/>
141 to keep the SoC fully awake while the data is collected.<br/>
142 <br/>
143 <strong>Medium-high value: Interrupt load reduction</strong><br/>
144 Target batching time: &lt; 1 second<br/>
145 Sensors to batch: any high frequency sensor.<br/>
146 If the gyroscope is set at 240Hz, even batching just 10 gyro events can<br/>
147 reduce the number of interrupts from 240/second to 24/second.<br/>
148 <br/>
149 <strong>Medium value: Continuous low frequency data collection</strong><br/>
150 Target batching time: &gt; 1 minute<br/>
151 Sensors to batch: barometer, humidity sensor, other low frequency<br/>
152 sensors.<br/>
153 Allows creating monitoring applications at low power.<br/>
154 <br/>
155 <strong>Medium value: Continuous full-sensors collection</strong><br/>
156 Target batching time: &gt; 1 minute<br/>
157 Sensors to batch: all, at high frequencies<br/>
158 Allows full collection of sensor data while leaving the SoC in<br/>
159 suspend mode. Only to consider if fifo space is not an issue.<br/>
160 <br/>
161 In each of the cases above, if WAKE_UPON_FIFO_FULL is implemented, the<br/>
162 applications might decide to let the SoC go to suspend, allowing for even<br/>
163 more power savings.</p>
164<h2 id="Dry-run">Dry run</h2>
165<p>If the flag SENSORS_BATCH_DRY_RUN is set, this function returns without
166 modifying the batch mode or the event period and has no side effects, but
167 returns errors as usual (as it would if this flag was not set). This flag is
168 used to check if batch mode is available for a given configuration, in
169 particular for a given sensor at a given rate.</p>
170<h2 id="Return-values">Return values</h2>
171<p>Because sensors must be independent, the return value must not depend on the
172 state of the system (whether another sensor is on or not), nor on whether the
173 flag SENSORS_BATCH_DRY_RUN is set (in other words, if a batch call with
174 SENSORS_BATCH_DRY_RUN is successful, the same call without SENSORS_BATCH_DRY_RUN
175 must succeed as well).</p>
176<p>If successful, 0 is returned.</p>
177<p>If the specified sensor doesn't support batch mode, -EINVAL is returned.<br/>
178 If the specified sensor's trigger-mode is one-shot, -EINVAL is returned.</p>
179<p>If WAKE UPON FIFO_FULL is specified and the specified sensor's internal FIFO is
180 too small to store at least 10 seconds worth of data at the given rate, -EINVAL
181 is returned. Note that as stated above, this has to be determined at compile
182 time and not based on the state of the system.</p>
183<p>If some other constraints above cannot be satisfied, -EINVAL is returned.<br/>
184 <br/>
Clay Murphy69cac8e2013-11-01 14:10:12 -0700185 Note: the maxReportingLatency parameter when &gt; 0 has no impact on whether this function
Clay Murphy4ea104f2013-10-28 17:44:33 -0700186 succeeds or fails.<br/>
187 <br/>
Clay Murphy69cac8e2013-11-01 14:10:12 -0700188 If maxReportingLatency is set to 0, this function must succeed.</p>
Clay Murphy4ea104f2013-10-28 17:44:33 -0700189<h2 id="Supporting-docs">Supporting documentation</h2>
190<p><a href="http://developer.android.com/guide/topics/sensors/index.html">Developer - Location and Sensors
191 APIs</a></p>
192<p><a href="http://developer.android.com/guide/topics/sensors/sensors_overview.html">Developer - Sensors
193 Overview</a></p>
194<p><a href="http://developer.android.com/reference/android/hardware/Sensor.html">Sensors SDK API
195 reference</a></p>
196<p><a href="{@docRoot}devices/reference/sensors_8h_source.html">Android
197 Hardware Abstraction Layer - sensors.h</a></p>
198<p><a href="http://developer.android.com/reference/android/hardware/SensorManager.html">SensorManager</a></p>