blob: 449cdbdfe975b3b09b0fbb37c07038b69f08d4fb [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
Clay Murphy69cac8e2013-11-01 14:10:12 -0700127 (SoC) from going into suspend mode. It is the responsibility of applications to keep a
Clay Murphy4ea104f2013-10-28 17:44:33 -0700128 partial <a href="http://developer.android.com/reference/android/os/PowerManager.WakeLock.html">wake
129 lock</a> should they wish to receive sensor events while the screen is off. While in
130 suspend mode, and unless otherwise noted (<a
131href="{@docRoot}devices/sensors/batching.html">batch</a> mode
132 and sensor particularities), enabled sensors' events are lost.</p>
133<p>Note that conceptually, the sensor itself is not deactivated while in suspend
134 mode. Instead, the data it returns is missing. The oldest data is dropped to
135 accommodate the latest data. As soon as the SoC gets out of suspend mode,
136 operations resume as normal.</p>
137<p>Most applications should either hold a wake lock to ensure the system doesn't go
138 to suspend, or unregister from the sensors when they do not need them, unless
139 batch mode is active. When batching, sensors must continue to fill their
140 internal FIFO. (See the documentation of <a
141href="{@docRoot}devices/sensors/batching.html">batch</a> mode
142 to learn how suspend interacts with batch mode.)</p>
Clay Murphy69cac8e2013-11-01 14:10:12 -0700143<p>Wake-up sensors are a notable exception to the above. Wake-up sensors must
144wake up the SoC to deliver events. They must still let the SoC go into suspend
145mode, but must also wake it up when an event is triggered.</p>
Clay Murphy4ea104f2013-10-28 17:44:33 -0700146<h3 id="fusion">Sensor fusion and virtual sensors</h3>
147<p>Many composite sensor types are or can be implemented as virtual sensors from
148 underlying base sensors on the device. Examples of composite sensors types
149 include the rotation vector sensor, orientation sensor, step detector and step
150 counter.</p>
151<p>From the point of view of this API, these virtual sensors <strong>must</strong> appear as
152 real, individual sensors. It is the responsibility of the driver and HAL to make
153 sure this is the case.</p>
154<p>In particular, all sensors must be able to function concurrently. For example,
155 if defining both an accelerometer and a step counter, then both must be able to
156 work concurrently.</p>
157<h3 id="hal">HAL interface</h3>
158<p>These are the common sensor calls expected at the HAL level:</p>
159<ol>
160 <li><em>getSensorList()</em> - Gets the list of all sensors.</li>
Clay Murphy69cac8e2013-11-01 14:10:12 -0700161 <li><em>activate()</em> - Starts or stops the specified sensor.</li>
162 <li><em>batch()</em> - Sets parameters to group event data collection and optimize power use.</li>
Clay Murphy4ea104f2013-10-28 17:44:33 -0700163 <li><em>setDelay()</em> - Sets the event's period in
164 nanoseconds for a given sensor.</li>
165 <li><em>flush()</em> - Flush adds an event to the end of the
166 &quot;batch mode&quot; FIFO for the specified sensor and flushes the FIFO.</li>
167 <li><em>poll()</em> - Returns an array of sensor data. </li>
168</ol>
169<p>Please note, the implementation must be thread safe and allow these values to be
170 called from different threads.</p>
171<h4 id="getSensorList">getSensorList(sensor_type)</h4>
172<p>Provide the list of sensors implemented by the HAL for the given sensor type. </p>
173<p>Developers may then make multiple calls to get sensors of different types or use
174 Sensor.TYPE_ALL to get all the sensors. See getSensorList() defined on
175 developer.android.com for more details.</p>
176<h4 id="activate">activate(sensor, true/false)</h4>
177<pre>
178 int (*activate)(struct sensors_poll_device_t *dev,
179 int handle, int enabled);</pre>
180<p>Activates or deactivates the sensor with the specified handle. Handles must be
181 higher than SENSORS_HANDLE_BASE and must be unique. A handle identifies a given
182 sensor. The handle is used to activate and/or deactivate sensors. In this
183 version of the API, there can only be 256 handles.</p>
184<p>The handle is the handle of the sensor to change. The enabled argument is set to
185 1 to enable or 0 to disable the sensor.</p>
186<p>Unless otherwise noted in the individual sensor type descriptions, an activated
187 sensor never prevents the SoC from going into suspend mode; that is, the HAL
188 shall not hold a partial wake lock on behalf of applications.<br/>
189 <br/>
190 One-shot sensors deactivate themselves automatically upon receiving an event,
191 and they must still accept to be deactivated through a call to activate(...,
192 ..., 0).<br/>
193 <br/>
194 If &quot;enabled&quot; is 1 and the sensor is already activated, this function is a no-op
195 and succeeds. If &quot;enabled&quot; is 0 and the sensor is already deactivated, this
196 function is a no-op and succeeds. This returns 0 on success and a negative errno
197 code otherwise.</p>
198<h4 id="batch">batch(sensor, batching parameters)</h4>
199<pre>
200 int (*batch)(struct sensors_poll_device_1* dev,
201 int handle, int flags, int64_t period_ns, int64_t timeout);
202</pre>
203<p>Sets parameters to group event data collection and reduce power use. Batching
204 can enable significant power savings by allowing the application processor to
205 sleep rather than awake for each notification. Instead, these notifications can
206 be grouped and processed together. See the <a
207href="{@docRoot}devices/sensors/batching.html">Batching</a> section for details.</p>
208<h4 id="setDelay">setDelay(sensor, delay)</h4>
209<pre>
210 int (*setDelay)(struct sensors_poll_device_t *dev,
211 int handle, int64_t period_ns);
212</pre>
213<p>Sets the event's period in nanoseconds for a given sensor. What the period_ns
214 parameter means depends on the specified sensor's trigger mode:</p>
215<ul>
216 <li>Continuous: setDelay() sets the sampling rate.</li>
217 <li>On-change: setDelay() limits the delivery rate of events.</li>
218 <li>One-shot: setDelay() is ignored. It has no effect.</li>
219 <li>Special: See specific sensor type descriptions.</li>
220</ul>
221<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
222 sensor_t::minDelay is 0, in which case it is clamped to &gt;= 1ms:<br/>
223 @return 0 if successful, &lt; 0 on error</p>
224<h4 id="flush">flush()</h4>
225<pre>
226 int (*flush)(struct sensors_poll_device_1* dev, int handle);
227</pre>
228<p>Flush adds a META_DATA_FLUSH_COMPLETE event (sensors_event_meta_data_t) to the
229 end of the &quot;batch mode&quot; FIFO for the specified sensor and flushes the FIFO;
230 those events are delivered as usual (i.e.: as if the batch timeout had expired)
231 and removed from the FIFO.<br/>
232 <br/>
233 The flush happens asynchronously (i.e.: this function must return immediately).
234 If the implementation uses a single FIFO for several sensors, that FIFO is
235 flushed and the META_DATA_FLUSH_COMPLETE event is added only for the specified
236 sensor.<br/>
237 <br/>
238 If the specified sensor wasn't in batch mode, flush succeeds and promptly sends
239 a META_DATA_FLUSH_COMPLETE event for that sensor.</p>
240<p>If the FIFO was empty at the time of the call, flush returns 0 (success) and
241 promptly sends a META_DATA_FLUSH_COMPLETE event for that sensor.<br/>
242 <br/>
243 If the specified sensor wasn't enabled, flush returns -EINVAL. return 0 on
244 success, negative errno code otherwise.</p>
245<h4 id="poll">poll()</h4>
246<pre> int (*poll)(struct sensors_poll_device_t *dev,
247 sensors_event_t* data, int count);</pre>
248<p>Returns an array of sensor data. This function must block until events are
249 available. It will return the number of events read on success, or -errno in
250 case of an error.</p>
251<p>The number of events returned in data must be less or equal to the &quot;count&quot;
252 argument. This function shall never return 0 (no event).</p>