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> |
| 28 | <p>Batching can enable significant power savings by preventing the application |
| 29 | processor from waking up to receive each notification. Instead, these |
| 30 | notifications can be grouped and processed together.</p> |
| 31 | <p>Enabling batch mode for a given sensor sets the delay between events. A timeout |
| 32 | value of zero disables batch mode for the given sensor. The period_ns parameter is equivalent to calling setDelay() -- this function both enables or disables the batch mode AND sets the event's period in nanoseconds. See setDelay() for a detailed explanation of the period_ns parameter.</p> |
| 33 | <p>In non-batch mode, all sensor events must be reported as soon as they are |
| 34 | detected. For example, an accelerometer activated at 50Hz will trigger |
| 35 | interrupts 50 times per second.<br/> |
| 36 | While in batch mode, sensor events do not need to be reported as soon as they |
| 37 | are detected. They can be temporarily stored and reported in batches, as long as |
| 38 | no event is delayed by more than "timeout" nanoseconds. That is, all events |
| 39 | since the previous batch are recorded and returned at once. This reduces the |
| 40 | amount of interrupts sent to the SoC and allows the SoC to switch to a lower |
| 41 | power mode (idle) while the sensor is capturing and batching data.</p> |
| 42 | <p>setDelay() is not affected and it behaves as usual. <br/> |
| 43 | <br/> |
| 44 | Each event has a timestamp associated with it. The timestamp must be accurate |
| 45 | and correspond to the time in which the event physically happened.</p> |
| 46 | <p>Batching does not modify the behavior of poll(): batches from different sensors |
| 47 | can be interleaved and split. As usual, all events from the same sensor are |
| 48 | time-ordered.</p> |
| 49 | <h2 id="Suspend">Suspend mode</h2> |
| 50 | <p>These are the power modes of the application processor: on, idle, and suspend. |
| 51 | The sensors behave differently in each of these modes. As you would imagine, on |
| 52 | mode is when the application processor is running. Idle mode is low-power use |
| 53 | waiting for activity before going into suspension. Suspend mode is when the |
| 54 | application processor is not running.</p> |
| 55 | <p>When the SoC is awake (not in suspend mode), events must be reported in batches |
| 56 | at least every "timeout." No event shall be dropped or lost. If internal |
| 57 | hardware FIFOs fill up before the timeout, then events are reported at that |
| 58 | point to ensure no event is lost.</p> |
| 59 | <h2 id="Normal">Normal behavior in suspend mode</h2> |
| 60 | <p>By default, batch mode doesn't significantly change the interaction with suspend |
| 61 | mode. That is, sensors must continue to allow the SoC to go into suspend mode |
| 62 | and sensors must stay active to fill their internal FIFO. In this mode, when the |
| 63 | FIFO fills up, it shall wrap around and behave like a circular buffer, |
| 64 | overwriting older events.<br/> |
| 65 | <br/> |
| 66 | As soon as the SoC comes out of suspend mode, a batch is produced with as much |
| 67 | as the recent history as possible, and batch operation resumes as usual.</p> |
| 68 | <p>The behavior described above allows applications to record the recent history of |
| 69 | a set of sensor types while keeping the SoC in suspend. It also allows the |
| 70 | hardware to not have to rely on a wake-up interrupt line.</p> |
| 71 | <h2 id="WAKE_UPON_FIFO_FULL">WAKE_UPON_FIFO_FULL behavior in suspend mode</h2> |
| 72 | <p>There are cases, however, where an application cannot afford to lose any events, |
| 73 | even when the device goes into suspend mode.</p> |
| 74 | <p>For a given rate, if a sensor has the capability to store at least 10 seconds |
| 75 | worth of events in its FIFO and is able to wake up the SoC, it can implement an |
| 76 | optional secondary mode: the WAKE_UPON_FIFO_FULL mode.</p> |
| 77 | <p>The caller will set the SENSORS_BATCH_WAKE_UPON_FIFO_FULL flag to activate this |
| 78 | mode. If the sensor does not support this mode, batch() will fail when the flag |
| 79 | is set.</p> |
| 80 | <p>When running with the WAKE_UPON_FIFO_FULL flag set, no events can be lost. When |
| 81 | the FIFO is getting full, the sensor must wake up the SoC from suspend and |
| 82 | return a batch before the FIFO fills-up.</p> |
| 83 | <p>Depending on the device, it might take a few milliseconds for the SoC to |
| 84 | entirely come out of suspend and start flushing the FIFO. Enough head room must |
| 85 | be allocated in the FIFO to allow the device to entirely come out of suspend |
| 86 | without the FIFO overflowing (no events shall be lost).</p> |
| 87 | <p>Implementing the WAKE_UPON_FIFO_FULL mode is optional. If the hardware cannot |
| 88 | support this mode, or if the physical FIFO is so small that the device would |
| 89 | never be allowed to go into suspend for at least 10 seconds, then this function <strong>must</strong> fail when the flag SENSORS_BATCH_WAKE_UPON_FIFO_FULL is set, regardless |
| 90 | of the value of the timeout parameter.</p> |
| 91 | <h2 id="Implementing">Implementing batching</h2> |
| 92 | <p>Batch mode, if supported, should happen at the hardware level, typically using |
| 93 | hardware FIFOs. In particular, it SHALL NOT be implemented in the HAL, as this |
| 94 | would be counter productive. The goal here is to save significant amounts of |
| 95 | power. Batching should be implemented without the aid of the SoC, which should |
| 96 | be allowed to be in suspend mode during batching.</p> |
| 97 | <p>In some implementations, events from several sensors can share the same physical |
| 98 | FIFO. In that case, all events in the FIFO can be sent and processed by the HAL |
| 99 | as soon as one batch must be reported.</p> |
| 100 | <p>For example, if the following sensors are activated:</p> |
| 101 | <ul> |
| 102 | <li>accelerometer batched with timeout = 20s</li> |
| 103 | <li>gyroscope batched with timeout = 5s</li> |
| 104 | </ul> |
| 105 | <p>Then the accelerometer batches can be reported at the same time the gyroscope |
| 106 | batches are reported (every 5 seconds).<br/> |
| 107 | <br/> |
| 108 | Batch mode can be enabled or disabled at any time, in particular while the |
| 109 | specified sensor is already enabled; and this shall not result in the loss of |
| 110 | events.</p> |
| 111 | <h2 id="different-sensors">Batching different sensors</h2> |
| 112 | <p>On platforms in which hardware FIFO size is limited, the system designers may |
| 113 | have to choose how much FIFO to reserve for each sensor. To help with this |
| 114 | choice, here is a list of applications made possible when batching is |
| 115 | implemented on the different sensors.</p> |
| 116 | <p><strong>High value: Low power pedestrian dead reckoning</strong><br/> |
| 117 | Target batching time: 20 seconds to 1 minute<br/> |
| 118 | Sensors to batch:<br/> |
| 119 | - Step detector<br/> |
| 120 | - Rotation vector or game rotation vector at 5Hz<br/> |
| 121 | Gives us step and heading while letting the SoC go to Suspend.<br/> |
| 122 | <br/> |
| 123 | <strong>High value: Medium power activity/gesture recognition</strong><br/> |
| 124 | Target batching time: 3 seconds<br/> |
| 125 | Sensors to batch: accelerometer between 20Hz and 50Hz<br/> |
| 126 | Allows recognizing arbitrary activities and gestures without having<br/> |
| 127 | to keep the SoC fully awake while the data is collected.<br/> |
| 128 | <br/> |
| 129 | <strong>Medium-high value: Interrupt load reduction</strong><br/> |
| 130 | Target batching time: < 1 second<br/> |
| 131 | Sensors to batch: any high frequency sensor.<br/> |
| 132 | If the gyroscope is set at 240Hz, even batching just 10 gyro events can<br/> |
| 133 | reduce the number of interrupts from 240/second to 24/second.<br/> |
| 134 | <br/> |
| 135 | <strong>Medium value: Continuous low frequency data collection</strong><br/> |
| 136 | Target batching time: > 1 minute<br/> |
| 137 | Sensors to batch: barometer, humidity sensor, other low frequency<br/> |
| 138 | sensors.<br/> |
| 139 | Allows creating monitoring applications at low power.<br/> |
| 140 | <br/> |
| 141 | <strong>Medium value: Continuous full-sensors collection</strong><br/> |
| 142 | Target batching time: > 1 minute<br/> |
| 143 | Sensors to batch: all, at high frequencies<br/> |
| 144 | Allows full collection of sensor data while leaving the SoC in<br/> |
| 145 | suspend mode. Only to consider if fifo space is not an issue.<br/> |
| 146 | <br/> |
| 147 | In each of the cases above, if WAKE_UPON_FIFO_FULL is implemented, the<br/> |
| 148 | applications might decide to let the SoC go to suspend, allowing for even<br/> |
| 149 | more power savings.</p> |
| 150 | <h2 id="Dry-run">Dry run</h2> |
| 151 | <p>If the flag SENSORS_BATCH_DRY_RUN is set, this function returns without |
| 152 | modifying the batch mode or the event period and has no side effects, but |
| 153 | returns errors as usual (as it would if this flag was not set). This flag is |
| 154 | used to check if batch mode is available for a given configuration, in |
| 155 | particular for a given sensor at a given rate.</p> |
| 156 | <h2 id="Return-values">Return values</h2> |
| 157 | <p>Because sensors must be independent, the return value must not depend on the |
| 158 | state of the system (whether another sensor is on or not), nor on whether the |
| 159 | flag SENSORS_BATCH_DRY_RUN is set (in other words, if a batch call with |
| 160 | SENSORS_BATCH_DRY_RUN is successful, the same call without SENSORS_BATCH_DRY_RUN |
| 161 | must succeed as well).</p> |
| 162 | <p>If successful, 0 is returned.</p> |
| 163 | <p>If the specified sensor doesn't support batch mode, -EINVAL is returned.<br/> |
| 164 | If the specified sensor's trigger-mode is one-shot, -EINVAL is returned.</p> |
| 165 | <p>If WAKE UPON FIFO_FULL is specified and the specified sensor's internal FIFO is |
| 166 | too small to store at least 10 seconds worth of data at the given rate, -EINVAL |
| 167 | is returned. Note that as stated above, this has to be determined at compile |
| 168 | time and not based on the state of the system.</p> |
| 169 | <p>If some other constraints above cannot be satisfied, -EINVAL is returned.<br/> |
| 170 | <br/> |
| 171 | Note: the timeout parameter when > 0 has no impact on whether this function |
| 172 | succeeds or fails.<br/> |
| 173 | <br/> |
| 174 | If timeout is set to 0, this function must succeed.</p> |
| 175 | <h2 id="Supporting-docs">Supporting documentation</h2> |
| 176 | <p><a href="http://developer.android.com/guide/topics/sensors/index.html">Developer - Location and Sensors |
| 177 | APIs</a></p> |
| 178 | <p><a href="http://developer.android.com/guide/topics/sensors/sensors_overview.html">Developer - Sensors |
| 179 | Overview</a></p> |
| 180 | <p><a href="http://developer.android.com/reference/android/hardware/Sensor.html">Sensors SDK API |
| 181 | reference</a></p> |
| 182 | <p><a href="{@docRoot}devices/reference/sensors_8h_source.html">Android |
| 183 | Hardware Abstraction Layer - sensors.h</a></p> |
| 184 | <p><a href="http://developer.android.com/reference/android/hardware/SensorManager.html">SensorManager</a></p> |