Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 1 | page.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 Murphy | 69cac8e | 2013-11-01 14:10:12 -0700 | [diff] [blame] | 28 | <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. |
Clay Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 35 | <code>max_report_latency</code> sets the maximum time by which events can be delayed and |
Clay Murphy | 69cac8e | 2013-11-01 14:10:12 -0700 | [diff] [blame] | 36 | batched together before being reported to the applications. A value of zero |
Clay Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 37 | disables batch mode for the given sensor. The <code>period_ns</code> parameter is equivalent |
Clay Murphy | 69cac8e | 2013-11-01 14:10:12 -0700 | [diff] [blame] | 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 |
Clay Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 40 | explanation of the <code>period_ns</code> parameter.</p> |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 41 | <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 Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 46 | no event is delayed by more than <code>maxReportingLatency</code> nanoseconds. That is, all events |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 47 | 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 Murphy | 69cac8e | 2013-11-01 14:10:12 -0700 | [diff] [blame] | 53 | and correspond to the time at which the event physically happened.</p> |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 54 | <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 Murphy | 69cac8e | 2013-11-01 14:10:12 -0700 | [diff] [blame] | 57 | <h2 id="Suspend">Behavior outside of suspend mode</h2> |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 58 | <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 Murphy | 69cac8e | 2013-11-01 14:10:12 -0700 | [diff] [blame] | 60 | 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 Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 65 | <p>When the SoC is awake (not in suspend mode), events must be reported in batches |
Clay Murphy | 69cac8e | 2013-11-01 14:10:12 -0700 | [diff] [blame] | 66 | 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 Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 68 | 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 |
| 77 | as 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 |
Clay Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 86 | optional secondary mode: the <code>WAKE_UPON_FIFO_FULL</code> mode.</p> |
| 87 | <p>The caller will set the <code>SENSORS_BATCH_WAKE_UPON_FIFO_FULL</code> flag to activate this |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 88 | mode. If the sensor does not support this mode, batch() will fail when the flag |
| 89 | is set.</p> |
Clay Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 90 | <p>In batch mode, and only when the flag |
| 91 | <code>SENSORS_BATCH_WAKE_UPON_FIFO_FULL</code> is |
Clay Murphy | 69cac8e | 2013-11-01 14:10:12 -0700 | [diff] [blame] | 92 | set and supported, the specified sensor must be able to wake-up the SoC and be |
| 93 | able to buffer at least 10 seconds worth of the requested sensor events.</p> |
Clay Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 94 | <p>When running with the <code>WAKE_UPON_FIFO_FULL</code> flag set, no events can be lost. When |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 95 | the FIFO is getting full, the sensor must wake up the SoC from suspend and |
| 96 | return a batch before the FIFO fills-up.</p> |
| 97 | <p>Depending on the device, it might take a few milliseconds for the SoC to |
| 98 | entirely come out of suspend and start flushing the FIFO. Enough head room must |
| 99 | be allocated in the FIFO to allow the device to entirely come out of suspend |
| 100 | without the FIFO overflowing (no events shall be lost).</p> |
Clay Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 101 | <p>Implementing the <code>WAKE_UPON_FIFO_FULL</code> mode is optional. If the hardware cannot |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 102 | support this mode, or if the physical FIFO is so small that the device would |
Clay Murphy | 69cac8e | 2013-11-01 14:10:12 -0700 | [diff] [blame] | 103 | never be allowed to go into suspend for at least 10 seconds, then this function |
Clay Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 104 | <strong>must</strong> fail when the flag |
| 105 | <code>SENSORS_BATCH_WAKE_UPON_FIFO_FULL</code> is set, regardless |
Clay Murphy | 69cac8e | 2013-11-01 14:10:12 -0700 | [diff] [blame] | 106 | of the value of the maxReportingLatency parameter.</p> |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 107 | <h2 id="Implementing">Implementing batching</h2> |
| 108 | <p>Batch mode, if supported, should happen at the hardware level, typically using |
| 109 | hardware FIFOs. In particular, it SHALL NOT be implemented in the HAL, as this |
| 110 | would be counter productive. The goal here is to save significant amounts of |
| 111 | power. Batching should be implemented without the aid of the SoC, which should |
| 112 | be allowed to be in suspend mode during batching.</p> |
| 113 | <p>In some implementations, events from several sensors can share the same physical |
| 114 | FIFO. In that case, all events in the FIFO can be sent and processed by the HAL |
| 115 | as soon as one batch must be reported.</p> |
| 116 | <p>For example, if the following sensors are activated:</p> |
| 117 | <ul> |
Clay Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 118 | <li>accelerometer batched with <code>maxReportingLatency</code> = 20s</li> |
| 119 | <li>gyroscope batched with <code>maxReportingLatency</code> = 5s</li> |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 120 | </ul> |
| 121 | <p>Then the accelerometer batches can be reported at the same time the gyroscope |
| 122 | batches are reported (every 5 seconds).<br/> |
| 123 | <br/> |
| 124 | Batch mode can be enabled or disabled at any time, in particular while the |
| 125 | specified sensor is already enabled; and this shall not result in the loss of |
| 126 | events.</p> |
Clay Murphy | 69cac8e | 2013-11-01 14:10:12 -0700 | [diff] [blame] | 127 | <h2 id="fifo-allocation">FiFo allocation priority</h2> |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 128 | <p>On platforms in which hardware FIFO size is limited, the system designers may |
| 129 | have to choose how much FIFO to reserve for each sensor. To help with this |
| 130 | choice, here is a list of applications made possible when batching is |
| 131 | implemented on the different sensors.</p> |
| 132 | <p><strong>High value: Low power pedestrian dead reckoning</strong><br/> |
| 133 | Target batching time: 20 seconds to 1 minute<br/> |
| 134 | Sensors to batch:<br/> |
| 135 | - Step detector<br/> |
| 136 | - Rotation vector or game rotation vector at 5Hz<br/> |
| 137 | Gives us step and heading while letting the SoC go to Suspend.<br/> |
| 138 | <br/> |
| 139 | <strong>High value: Medium power activity/gesture recognition</strong><br/> |
| 140 | Target batching time: 3 seconds<br/> |
| 141 | Sensors to batch: accelerometer between 20Hz and 50Hz<br/> |
| 142 | Allows recognizing arbitrary activities and gestures without having<br/> |
| 143 | to keep the SoC fully awake while the data is collected.<br/> |
| 144 | <br/> |
| 145 | <strong>Medium-high value: Interrupt load reduction</strong><br/> |
| 146 | Target batching time: < 1 second<br/> |
| 147 | Sensors to batch: any high frequency sensor.<br/> |
| 148 | If the gyroscope is set at 240Hz, even batching just 10 gyro events can<br/> |
| 149 | reduce the number of interrupts from 240/second to 24/second.<br/> |
| 150 | <br/> |
| 151 | <strong>Medium value: Continuous low frequency data collection</strong><br/> |
| 152 | Target batching time: > 1 minute<br/> |
| 153 | Sensors to batch: barometer, humidity sensor, other low frequency<br/> |
| 154 | sensors.<br/> |
| 155 | Allows creating monitoring applications at low power.<br/> |
| 156 | <br/> |
| 157 | <strong>Medium value: Continuous full-sensors collection</strong><br/> |
| 158 | Target batching time: > 1 minute<br/> |
| 159 | Sensors to batch: all, at high frequencies<br/> |
| 160 | Allows full collection of sensor data while leaving the SoC in<br/> |
| 161 | suspend mode. Only to consider if fifo space is not an issue.<br/> |
| 162 | <br/> |
Clay Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 163 | In each of the cases above, if <code>WAKE_UPON_FIFO_FULL</code> is implemented, the<br/> |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 164 | applications might decide to let the SoC go to suspend, allowing for even<br/> |
| 165 | more power savings.</p> |
| 166 | <h2 id="Dry-run">Dry run</h2> |
Clay Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 167 | <p>If the flag <code>SENSORS_BATCH_DRY_RUN</code> is set, this function returns without |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 168 | modifying the batch mode or the event period and has no side effects, but |
| 169 | returns errors as usual (as it would if this flag was not set). This flag is |
| 170 | used to check if batch mode is available for a given configuration, in |
| 171 | particular for a given sensor at a given rate.</p> |
| 172 | <h2 id="Return-values">Return values</h2> |
| 173 | <p>Because sensors must be independent, the return value must not depend on the |
| 174 | state of the system (whether another sensor is on or not), nor on whether the |
Clay Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 175 | flag <code>SENSORS_BATCH_DRY_RUN</code> is set (in other words, if a batch call with |
| 176 | <code>SENSORS_BATCH_DRY_RUN</code> is successful, the same call without |
| 177 | <code>SENSORS_BATCH_DRY_RUN</code> |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 178 | must succeed as well).</p> |
| 179 | <p>If successful, 0 is returned.</p> |
| 180 | <p>If the specified sensor doesn't support batch mode, -EINVAL is returned.<br/> |
| 181 | If the specified sensor's trigger-mode is one-shot, -EINVAL is returned.</p> |
| 182 | <p>If WAKE UPON FIFO_FULL is specified and the specified sensor's internal FIFO is |
| 183 | too small to store at least 10 seconds worth of data at the given rate, -EINVAL |
| 184 | is returned. Note that as stated above, this has to be determined at compile |
| 185 | time and not based on the state of the system.</p> |
| 186 | <p>If some other constraints above cannot be satisfied, -EINVAL is returned.<br/> |
| 187 | <br/> |
Clay Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 188 | Note: The <code>maxReportingLatency</code> parameter when > 0 has no impact on |
| 189 | whether this function succeeds or fails.<br/> |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 190 | <br/> |
Clay Murphy | 584e165 | 2013-12-10 10:53:20 -0800 | [diff] [blame^] | 191 | If <code>maxReportingLatency</code> is set to 0, this function must succeed.</p> |
Clay Murphy | 4ea104f | 2013-10-28 17:44:33 -0700 | [diff] [blame] | 192 | <h2 id="Supporting-docs">Supporting documentation</h2> |
| 193 | <p><a href="http://developer.android.com/guide/topics/sensors/index.html">Developer - Location and Sensors |
| 194 | APIs</a></p> |
| 195 | <p><a href="http://developer.android.com/guide/topics/sensors/sensors_overview.html">Developer - Sensors |
| 196 | Overview</a></p> |
| 197 | <p><a href="http://developer.android.com/reference/android/hardware/Sensor.html">Sensors SDK API |
| 198 | reference</a></p> |
| 199 | <p><a href="{@docRoot}devices/reference/sensors_8h_source.html">Android |
| 200 | Hardware Abstraction Layer - sensors.h</a></p> |
| 201 | <p><a href="http://developer.android.com/reference/android/hardware/SensorManager.html">SensorManager</a></p> |