blob: 32f3ee1eeec95268c55f6c783bfbceca1bb04881 [file] [log] [blame]
Clay Murphy4ea104f2013-10-28 17:44:33 -07001page.title=Sensors HAL overview
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="intro">Introduction</h2>
28<p><a href="http://developer.android.com/guide/topics/sensors/sensors_overview.html">Android
29 sensors</a> give applications access to a mobile device's underlying base sensor(s):
30 accelerometer, gyroscope, and magnetometer. Manufacturers develop the drivers
31 that define additional composite sensor types from those base sensors. For
32 instance, Android offers both calibrated and uncalibrated gyroscopes, a
33 geomagnetic rotation vector and a game rotation vector. This variety gives
34 developers some flexibility in tuning applications for battery life optimization
35 and accuracy.</p>
36<p>The <a href="{@docRoot}devices/reference/sensors_8h_source.html">Sensors
37Hardware Abstraction Layer (HAL) API</a> is the interface between the hardware drivers
38and the Android framework; the <a href="http://developer.android.com/reference/android/hardware/Sensor.html">Sensors Software Development Kit (SDK)
39 API</a> is the interface between the Android framework and the Java applications. Please note,
40 the Sensors HAL API described in this documentation is not identical to the
41 Sensors SDK API described on <a href="http://developer.android.com/reference/android/hardware/Sensor.html">developer.android.com</a>.
42 For example, some sensors that are deprecated in the SDK may still exist in the
43 HAL, and vice versa.</p>
44<p>Similarly, audio recorders, Global Positioning System (GPS) devices, and
45 accessory (pluggable) sensors are not supported by the Android Sensors HAL API
46 described here. This API covers sensors that are physically part of the device
47 only. Please see the <a href="{@docRoot}devices/audio.html">Audio</a>, <a href="{@docRoot}devices/reference/gps_8h.html">Location </a><a href="{@docRoot}devices/reference/gps_8h.html">Strategies</a>,
48 and the <a href="{@docRoot}accessories/index.html">Accessories</a> section
49 for information on those devices.</p>
50<p>Application framework<br/>
51 At the application framework level is the app code, which utilizes the <a href="http://developer.android.com/reference/android/hardware/package-summary.html">android.hardware</a> APIs to interact with the sensors hardware. Internally, this code calls
52 corresponding JNI glue classes to access the native code that interacts with the
53 sensors hardware.</p>
54<p>JNI<br/>
55 The JNI code associated with <a href="http://developer.android.com/reference/android/hardware/package-summary.html">android.hardware</a> is located in the frameworks/base/core/jni/ directory. This code calls the lower
56 level native code to obtain access to the sensor hardware.</p>
57<p>Native framework<br/>
58 The native framework is defined in frameworks/native/ and provides a native
59 equivalent to the <a href="http://developer.android.com/reference/android/hardware/package-summary.html">android.hardware</a> package. The native framework calls the Binder IPC proxies to obtain access to
60 sensor-specific services.</p>
61<p>Binder IPC<br/>
62 The Binder IPC proxies facilitate communication over process boundaries.</p>
63<p>HAL<br/>
64 The Hardware Abstraction Layer (HAL) defines the standard interface that sensor
65 services call into and that you must implement to have your sensor hardware
66 function correctly. The sensor HAL interfaces are located in
67 hardware/libhardware/include/hardware.</p>
68<p>Kernel Driver<br/>
69 The sensors driver interacts with the hardware and your implementation of the
70 HAL. The HAL is driver-agnostic.</p>
71<h3 id="axis-def">Sensor axis definition</h3>
72<p>The sensor event values are expressed in a specific frame that is static
73 relative to the phone. This API is relative only to the NATURAL orientation of
74 the screen. In other words:</p>
75<ul>
76 <li>the axes are not swapped when the device's screen orientation changes.</li>
77 <li>higher level services <em>may</em> perform this transformation.</li>
78</ul>
79<div class="figure" style="width:269px"> <img src="http://developer.android.com/images/axis_device.png" alt="Coordinate system relative to device for Sensor
80 API" height="225" />
81 <p class="img-caption"> <strong>Figure 1.</strong> Coordinate system (relative to a device) that's used by the Sensor
82 API. </p>
83</div>
84<h3 id="accuracy">Accuracy</h3>
85<p>The sensors included by the manufacturer must be accurate and precise to meet
86the expectations of application developers. The sensors included in Android devices are
87 tested for sensor interaction and accuracy as part of the <a href="{@docRoot}compatibility/index.html">Android Compatibility
88 program</a> starting in the
89 Android 4.4 release. Testing will continue to be improved in future releases.
90 See the <em>Sensors</em> section of the Android Compatibility Definition Document (CDD)
91 for the exact requirements.</p>
92<h3 id="power">Power consumption</h3>
93<p>Some defined sensor are higher power than others. Others are lower power by
94 design and should be implemented as such with their processing done in the
95 hardware. This means they should not require the application processor to be
96 running. Here are the low-power sensors:</p>
97<ul>
98 <li><a href="{@docRoot}devices/sensors/composite_sensors.html#Magnetometer">Geomagnetic rotation vector</a></li>
99 <li><a href="{@docRoot}devices/sensors/composite_sensors.html#Significant">Significant motion</a></li>
100 <li><a href="{@docRoot}devices/sensors/composite_sensors.html#counter">Step counter</a></li>
101 <li><a href="{@docRoot}devices/sensors/composite_sensors.html#detector">Step detector</a></li>
102</ul>
103<p>They are accompanied by a low-power <img src="images/battery_icon.png"
104alt="low-power sensors"/>
105 icon in the <a href="{@docRoot}devices/sensors/composite_sensors.html#summary">Sensor summary</a> table. </p>
106<p>These sensor types cannot be implemented at high power as their primary benefit
107 is low battery use. It is better to not implement a low-power sensor at all
108 rather than implement it as high power.</p>
109<p>Composite low-power sensor types, such as the step detector, must have their
110 processing conducted in the hardware; power use is much lower than if done in
111 the software. Power use is low on small microprocessors and even lower still on
112 application-specific integrated circuits (ASICs). A hardware implementation of
113 composite sensor types can also make use of more raw sensor data and a better
114 synchronization between sensors.</p>
115<h3 id="release">HAL release cycle</h3>
116<p>Functionality is tied to versions of the API. Android maintains two versions of
117 the Sensors HAL API per release. For instance, if version 1 was the latest and
118 version 1.1 is released, the version prior to 1 will no longer be supported upon
119 that release. Only the two latest versions of the Sensors HAL API are supported.</p>
120<h2 id="interaction">Interaction</h2>
121<h3 id="concurrent">Concurrent running</h3>
122<p>Android sensors must work independently of one another. Activating one sensor
123 shall not deactivate another sensor. Activating one shall not reduce the rate of
124 another. This is a key element of compatibility testing.</p>
125<h3 id="suspend">Interaction with suspend mode</h3>
126<p>Unless otherwise noted, an enabled sensor shall not prevent the system on a chip
127 (SoC) from going into suspend mode. See the <a
128href="{@docRoot}devices/sensors/batching.html#Suspend">Suspend
129 mode</a> section for a full description of the expected
130 behavior in this mode. It is the responsibility of applications to keep a
131 partial <a href="http://developer.android.com/reference/android/os/PowerManager.WakeLock.html">wake
132 lock</a> should they wish to receive sensor events while the screen is off. While in
133 suspend mode, and unless otherwise noted (<a
134href="{@docRoot}devices/sensors/batching.html">batch</a> mode
135 and sensor particularities), enabled sensors' events are lost.</p>
136<p>Note that conceptually, the sensor itself is not deactivated while in suspend
137 mode. Instead, the data it returns is missing. The oldest data is dropped to
138 accommodate the latest data. As soon as the SoC gets out of suspend mode,
139 operations resume as normal.</p>
140<p>Most applications should either hold a wake lock to ensure the system doesn't go
141 to suspend, or unregister from the sensors when they do not need them, unless
142 batch mode is active. When batching, sensors must continue to fill their
143 internal FIFO. (See the documentation of <a
144href="{@docRoot}devices/sensors/batching.html">batch</a> mode
145 to learn how suspend interacts with batch mode.)</p>
146<p>In batch mode, and only when the flag SENSORS_BATCH_WAKE_UPON_FIFO_FULL is set
147 and supported, the specified sensor must be able to wake-up the SoC and be able
148 to buffer at least 10 seconds worth of the requested sensor events.</p>
149<p>There are notable exceptions to this behavior, which are sensor-dependent. (See
150 the sensor types descriptions below.)</p>
151<p>The individual sensor references specify the wake-up behavior of each sensor:</p>
152<ul>
153 <li>wake-up: yes - this sensor must wake-up the SoC to deliver events</li>
154 <li>wake-up: no - this sensor shall not wake-up the SoC; events are dropped</li>
155</ul>
156<h3 id="fusion">Sensor fusion and virtual sensors</h3>
157<p>Many composite sensor types are or can be implemented as virtual sensors from
158 underlying base sensors on the device. Examples of composite sensors types
159 include the rotation vector sensor, orientation sensor, step detector and step
160 counter.</p>
161<p>From the point of view of this API, these virtual sensors <strong>must</strong> appear as
162 real, individual sensors. It is the responsibility of the driver and HAL to make
163 sure this is the case.</p>
164<p>In particular, all sensors must be able to function concurrently. For example,
165 if defining both an accelerometer and a step counter, then both must be able to
166 work concurrently.</p>
167<h3 id="hal">HAL interface</h3>
168<p>These are the common sensor calls expected at the HAL level:</p>
169<ol>
170 <li><em>getSensorList()</em> - Gets the list of all sensors.</li>
171 <li><em>activate()</em> - Starts the specified sensor.</li>
172 <li><em>batch</em> - Sets parameters to group event data collection and optimize power use.</li>
173 <li><em>setDelay()</em> - Sets the event's period in
174 nanoseconds for a given sensor.</li>
175 <li><em>flush()</em> - Flush adds an event to the end of the
176 &quot;batch mode&quot; FIFO for the specified sensor and flushes the FIFO.</li>
177 <li><em>poll()</em> - Returns an array of sensor data. </li>
178</ol>
179<p>Please note, the implementation must be thread safe and allow these values to be
180 called from different threads.</p>
181<h4 id="getSensorList">getSensorList(sensor_type)</h4>
182<p>Provide the list of sensors implemented by the HAL for the given sensor type. </p>
183<p>Developers may then make multiple calls to get sensors of different types or use
184 Sensor.TYPE_ALL to get all the sensors. See getSensorList() defined on
185 developer.android.com for more details.</p>
186<h4 id="activate">activate(sensor, true/false)</h4>
187<pre>
188 int (*activate)(struct sensors_poll_device_t *dev,
189 int handle, int enabled);</pre>
190<p>Activates or deactivates the sensor with the specified handle. Handles must be
191 higher than SENSORS_HANDLE_BASE and must be unique. A handle identifies a given
192 sensor. The handle is used to activate and/or deactivate sensors. In this
193 version of the API, there can only be 256 handles.</p>
194<p>The handle is the handle of the sensor to change. The enabled argument is set to
195 1 to enable or 0 to disable the sensor.</p>
196<p>Unless otherwise noted in the individual sensor type descriptions, an activated
197 sensor never prevents the SoC from going into suspend mode; that is, the HAL
198 shall not hold a partial wake lock on behalf of applications.<br/>
199 <br/>
200 One-shot sensors deactivate themselves automatically upon receiving an event,
201 and they must still accept to be deactivated through a call to activate(...,
202 ..., 0).<br/>
203 <br/>
204 If &quot;enabled&quot; is 1 and the sensor is already activated, this function is a no-op
205 and succeeds. If &quot;enabled&quot; is 0 and the sensor is already deactivated, this
206 function is a no-op and succeeds. This returns 0 on success and a negative errno
207 code otherwise.</p>
208<h4 id="batch">batch(sensor, batching parameters)</h4>
209<pre>
210 int (*batch)(struct sensors_poll_device_1* dev,
211 int handle, int flags, int64_t period_ns, int64_t timeout);
212</pre>
213<p>Sets parameters to group event data collection and reduce power use. Batching
214 can enable significant power savings by allowing the application processor to
215 sleep rather than awake for each notification. Instead, these notifications can
216 be grouped and processed together. See the <a
217href="{@docRoot}devices/sensors/batching.html">Batching</a> section for details.</p>
218<h4 id="setDelay">setDelay(sensor, delay)</h4>
219<pre>
220 int (*setDelay)(struct sensors_poll_device_t *dev,
221 int handle, int64_t period_ns);
222</pre>
223<p>Sets the event's period in nanoseconds for a given sensor. What the period_ns
224 parameter means depends on the specified sensor's trigger mode:</p>
225<ul>
226 <li>Continuous: setDelay() sets the sampling rate.</li>
227 <li>On-change: setDelay() limits the delivery rate of events.</li>
228 <li>One-shot: setDelay() is ignored. It has no effect.</li>
229 <li>Special: See specific sensor type descriptions.</li>
230</ul>
231<p>For continuous and on-change sensors, if the requested value is less than sensor_t::minDelay, then it's silently clamped to sensor_t::minDelay unless
232 sensor_t::minDelay is 0, in which case it is clamped to &gt;= 1ms:<br/>
233 @return 0 if successful, &lt; 0 on error</p>
234<h4 id="flush">flush()</h4>
235<pre>
236 int (*flush)(struct sensors_poll_device_1* dev, int handle);
237</pre>
238<p>Flush adds a META_DATA_FLUSH_COMPLETE event (sensors_event_meta_data_t) to the
239 end of the &quot;batch mode&quot; FIFO for the specified sensor and flushes the FIFO;
240 those events are delivered as usual (i.e.: as if the batch timeout had expired)
241 and removed from the FIFO.<br/>
242 <br/>
243 The flush happens asynchronously (i.e.: this function must return immediately).
244 If the implementation uses a single FIFO for several sensors, that FIFO is
245 flushed and the META_DATA_FLUSH_COMPLETE event is added only for the specified
246 sensor.<br/>
247 <br/>
248 If the specified sensor wasn't in batch mode, flush succeeds and promptly sends
249 a META_DATA_FLUSH_COMPLETE event for that sensor.</p>
250<p>If the FIFO was empty at the time of the call, flush returns 0 (success) and
251 promptly sends a META_DATA_FLUSH_COMPLETE event for that sensor.<br/>
252 <br/>
253 If the specified sensor wasn't enabled, flush returns -EINVAL. return 0 on
254 success, negative errno code otherwise.</p>
255<h4 id="poll">poll()</h4>
256<pre> int (*poll)(struct sensors_poll_device_t *dev,
257 sensors_event_t* data, int count);</pre>
258<p>Returns an array of sensor data. This function must block until events are
259 available. It will return the number of events read on success, or -errno in
260 case of an error.</p>
261<p>The number of events returned in data must be less or equal to the &quot;count&quot;
262 argument. This function shall never return 0 (no event).</p>