blob: e5fa438ce10ccb9fa4d267b06231e1bb4b2b853d [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/>
Clay Murphy584e1652013-12-10 10:53:20 -080058 The native framework is defined in <code>frameworks/native/</code> and provides a native
Clay Murphy4ea104f2013-10-28 17:44:33 -070059 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
Clay Murphy584e1652013-12-10 10:53:20 -080067 <code>hardware/libhardware/include/hardware</code>. See <a
68href="http://source.android.com/devices/reference/sensors_8h.html">sensors.h</a> for
69additional details.</p>
Clay Murphy4ea104f2013-10-28 17:44:33 -070070<p>Kernel Driver<br/>
71 The sensors driver interacts with the hardware and your implementation of the
72 HAL. The HAL is driver-agnostic.</p>
73<h3 id="axis-def">Sensor axis definition</h3>
74<p>The sensor event values are expressed in a specific frame that is static
75 relative to the phone. This API is relative only to the NATURAL orientation of
76 the screen. In other words:</p>
77<ul>
78 <li>the axes are not swapped when the device's screen orientation changes.</li>
79 <li>higher level services <em>may</em> perform this transformation.</li>
80</ul>
81<div class="figure" style="width:269px"> <img src="http://developer.android.com/images/axis_device.png" alt="Coordinate system relative to device for Sensor
82 API" height="225" />
83 <p class="img-caption"> <strong>Figure 1.</strong> Coordinate system (relative to a device) that's used by the Sensor
84 API. </p>
85</div>
86<h3 id="accuracy">Accuracy</h3>
87<p>The sensors included by the manufacturer must be accurate and precise to meet
88the expectations of application developers. The sensors included in Android devices are
89 tested for sensor interaction and accuracy as part of the <a href="{@docRoot}compatibility/index.html">Android Compatibility
90 program</a> starting in the
91 Android 4.4 release. Testing will continue to be improved in future releases.
92 See the <em>Sensors</em> section of the Android Compatibility Definition Document (CDD)
93 for the exact requirements.</p>
94<h3 id="power">Power consumption</h3>
95<p>Some defined sensor are higher power than others. Others are lower power by
96 design and should be implemented as such with their processing done in the
97 hardware. This means they should not require the application processor to be
98 running. Here are the low-power sensors:</p>
99<ul>
100 <li><a href="{@docRoot}devices/sensors/composite_sensors.html#Magnetometer">Geomagnetic rotation vector</a></li>
101 <li><a href="{@docRoot}devices/sensors/composite_sensors.html#Significant">Significant motion</a></li>
102 <li><a href="{@docRoot}devices/sensors/composite_sensors.html#counter">Step counter</a></li>
103 <li><a href="{@docRoot}devices/sensors/composite_sensors.html#detector">Step detector</a></li>
104</ul>
105<p>They are accompanied by a low-power <img src="images/battery_icon.png"
106alt="low-power sensors"/>
107 icon in the <a href="{@docRoot}devices/sensors/composite_sensors.html#summary">Sensor summary</a> table. </p>
108<p>These sensor types cannot be implemented at high power as their primary benefit
109 is low battery use. It is better to not implement a low-power sensor at all
110 rather than implement it as high power.</p>
111<p>Composite low-power sensor types, such as the step detector, must have their
112 processing conducted in the hardware; power use is much lower than if done in
113 the software. Power use is low on small microprocessors and even lower still on
114 application-specific integrated circuits (ASICs). A hardware implementation of
115 composite sensor types can also make use of more raw sensor data and a better
116 synchronization between sensors.</p>
117<h3 id="release">HAL release cycle</h3>
118<p>Functionality is tied to versions of the API. Android maintains two versions of
119 the Sensors HAL API per release. For instance, if version 1 was the latest and
120 version 1.1 is released, the version prior to 1 will no longer be supported upon
121 that release. Only the two latest versions of the Sensors HAL API are supported.</p>
122<h2 id="interaction">Interaction</h2>
123<h3 id="concurrent">Concurrent running</h3>
124<p>Android sensors must work independently of one another. Activating one sensor
125 shall not deactivate another sensor. Activating one shall not reduce the rate of
126 another. This is a key element of compatibility testing.</p>
127<h3 id="suspend">Interaction with suspend mode</h3>
128<p>Unless otherwise noted, an enabled sensor shall not prevent the system on a chip
Clay Murphy69cac8e2013-11-01 14:10:12 -0700129 (SoC) from going into suspend mode. It is the responsibility of applications to keep a
Clay Murphy4ea104f2013-10-28 17:44:33 -0700130 partial <a href="http://developer.android.com/reference/android/os/PowerManager.WakeLock.html">wake
131 lock</a> should they wish to receive sensor events while the screen is off. While in
132 suspend mode, and unless otherwise noted (<a
133href="{@docRoot}devices/sensors/batching.html">batch</a> mode
134 and sensor particularities), enabled sensors' events are lost.</p>
135<p>Note that conceptually, the sensor itself is not deactivated while in suspend
136 mode. Instead, the data it returns is missing. The oldest data is dropped to
137 accommodate the latest data. As soon as the SoC gets out of suspend mode,
138 operations resume as normal.</p>
139<p>Most applications should either hold a wake lock to ensure the system doesn't go
140 to suspend, or unregister from the sensors when they do not need them, unless
141 batch mode is active. When batching, sensors must continue to fill their
142 internal FIFO. (See the documentation of <a
143href="{@docRoot}devices/sensors/batching.html">batch</a> mode
144 to learn how suspend interacts with batch mode.)</p>
Clay Murphy69cac8e2013-11-01 14:10:12 -0700145<p>Wake-up sensors are a notable exception to the above. Wake-up sensors must
146wake up the SoC to deliver events. They must still let the SoC go into suspend
147mode, but must also wake it up when an event is triggered.</p>
Clay Murphy4ea104f2013-10-28 17:44:33 -0700148<h3 id="fusion">Sensor fusion and virtual sensors</h3>
149<p>Many composite sensor types are or can be implemented as virtual sensors from
150 underlying base sensors on the device. Examples of composite sensors types
151 include the rotation vector sensor, orientation sensor, step detector and step
152 counter.</p>
153<p>From the point of view of this API, these virtual sensors <strong>must</strong> appear as
154 real, individual sensors. It is the responsibility of the driver and HAL to make
155 sure this is the case.</p>
156<p>In particular, all sensors must be able to function concurrently. For example,
157 if defining both an accelerometer and a step counter, then both must be able to
158 work concurrently.</p>
159<h3 id="hal">HAL interface</h3>
160<p>These are the common sensor calls expected at the HAL level:</p>
161<ol>
162 <li><em>getSensorList()</em> - Gets the list of all sensors.</li>
Clay Murphy69cac8e2013-11-01 14:10:12 -0700163 <li><em>activate()</em> - Starts or stops the specified sensor.</li>
164 <li><em>batch()</em> - Sets parameters to group event data collection and optimize power use.</li>
Clay Murphy4ea104f2013-10-28 17:44:33 -0700165 <li><em>setDelay()</em> - Sets the event's period in
166 nanoseconds for a given sensor.</li>
167 <li><em>flush()</em> - Flush adds an event to the end of the
168 &quot;batch mode&quot; FIFO for the specified sensor and flushes the FIFO.</li>
169 <li><em>poll()</em> - Returns an array of sensor data. </li>
170</ol>
171<p>Please note, the implementation must be thread safe and allow these values to be
172 called from different threads.</p>
173<h4 id="getSensorList">getSensorList(sensor_type)</h4>
174<p>Provide the list of sensors implemented by the HAL for the given sensor type. </p>
175<p>Developers may then make multiple calls to get sensors of different types or use
Clay Murphy584e1652013-12-10 10:53:20 -0800176 <code>Sensor.TYPE_ALL</code> to get all the sensors. See getSensorList() defined on
Clay Murphy4ea104f2013-10-28 17:44:33 -0700177 developer.android.com for more details.</p>
178<h4 id="activate">activate(sensor, true/false)</h4>
179<pre>
180 int (*activate)(struct sensors_poll_device_t *dev,
181 int handle, int enabled);</pre>
182<p>Activates or deactivates the sensor with the specified handle. Handles must be
Clay Murphy584e1652013-12-10 10:53:20 -0800183 higher than <code>SENSORS_HANDLE_BASE</code> and must be unique. A handle identifies a given
Clay Murphy4ea104f2013-10-28 17:44:33 -0700184 sensor. The handle is used to activate and/or deactivate sensors. In this
185 version of the API, there can only be 256 handles.</p>
186<p>The handle is the handle of the sensor to change. The enabled argument is set to
187 1 to enable or 0 to disable the sensor.</p>
188<p>Unless otherwise noted in the individual sensor type descriptions, an activated
189 sensor never prevents the SoC from going into suspend mode; that is, the HAL
190 shall not hold a partial wake lock on behalf of applications.<br/>
191 <br/>
192 One-shot sensors deactivate themselves automatically upon receiving an event,
193 and they must still accept to be deactivated through a call to activate(...,
194 ..., 0).<br/>
195 <br/>
196 If &quot;enabled&quot; is 1 and the sensor is already activated, this function is a no-op
197 and succeeds. If &quot;enabled&quot; is 0 and the sensor is already deactivated, this
198 function is a no-op and succeeds. This returns 0 on success and a negative errno
199 code otherwise.</p>
200<h4 id="batch">batch(sensor, batching parameters)</h4>
201<pre>
202 int (*batch)(struct sensors_poll_device_1* dev,
203 int handle, int flags, int64_t period_ns, int64_t timeout);
204</pre>
205<p>Sets parameters to group event data collection and reduce power use. Batching
206 can enable significant power savings by allowing the application processor to
207 sleep rather than awake for each notification. Instead, these notifications can
208 be grouped and processed together. See the <a
209href="{@docRoot}devices/sensors/batching.html">Batching</a> section for details.</p>
210<h4 id="setDelay">setDelay(sensor, delay)</h4>
211<pre>
212 int (*setDelay)(struct sensors_poll_device_t *dev,
213 int handle, int64_t period_ns);
214</pre>
Clay Murphy584e1652013-12-10 10:53:20 -0800215<p>Sets the event's period in nanoseconds for a given sensor. What the
216<code>period_ns</code> parameter means depends on the specified sensor's trigger mode:</p>
Clay Murphy4ea104f2013-10-28 17:44:33 -0700217<ul>
218 <li>Continuous: setDelay() sets the sampling rate.</li>
219 <li>On-change: setDelay() limits the delivery rate of events.</li>
220 <li>One-shot: setDelay() is ignored. It has no effect.</li>
221 <li>Special: See specific sensor type descriptions.</li>
222</ul>
Clay Murphy584e1652013-12-10 10:53:20 -0800223<p>For continuous and on-change sensors, if the requested value is less than
224<code>sensor_t::minDelay</code>, then it's silently clamped to
225<code>sensor_t::minDelay</code> unless <code>sensor_t::minDelay</code> is 0,
Clay Murphyeeb15ca2013-12-09 17:07:32 -0800226in which case it is clamped to &gt;= 1ms. setDelay will not be called when the sensor is
227in batching mode. In this case, batch() will be called with the new period. Return 0 if successful,
228&lt; 0 on error.</p>
229<p>When calculating the sampling period T in setDelay (or batch), the actual period
230should be smaller than T and no smaller than T/2. Finer granularity is not
231necessary.</p>
Clay Murphy4ea104f2013-10-28 17:44:33 -0700232<h4 id="flush">flush()</h4>
233<pre>
234 int (*flush)(struct sensors_poll_device_1* dev, int handle);
235</pre>
Clay Murphy584e1652013-12-10 10:53:20 -0800236<p>Flush adds a <code>META_DATA_FLUSH_COMPLETE</code> event
237(<code>sensors_event_meta_data_t</code>) to the
Clay Murphy4ea104f2013-10-28 17:44:33 -0700238 end of the &quot;batch mode&quot; FIFO for the specified sensor and flushes the FIFO;
239 those events are delivered as usual (i.e.: as if the batch timeout had expired)
240 and removed from the FIFO.<br/>
241 <br/>
242 The flush happens asynchronously (i.e.: this function must return immediately).
243 If the implementation uses a single FIFO for several sensors, that FIFO is
Clay Murphy584e1652013-12-10 10:53:20 -0800244 flushed and the <code>META_DATA_FLUSH_COMPLETE</code> event is added only for the specified
Clay Murphy4ea104f2013-10-28 17:44:33 -0700245 sensor.<br/>
246 <br/>
247 If the specified sensor wasn't in batch mode, flush succeeds and promptly sends
Clay Murphy584e1652013-12-10 10:53:20 -0800248 a <code>META_DATA_FLUSH_COMPLETE</code> event for that sensor.</p>
Clay Murphy4ea104f2013-10-28 17:44:33 -0700249<p>If the FIFO was empty at the time of the call, flush returns 0 (success) and
Clay Murphy584e1652013-12-10 10:53:20 -0800250 promptly sends a <code>META_DATA_FLUSH_COMPLETE</code> event for that sensor.<br/>
Clay Murphy4ea104f2013-10-28 17:44:33 -0700251 <br/>
252 If the specified sensor wasn't enabled, flush returns -EINVAL. return 0 on
253 success, negative errno code otherwise.</p>
254<h4 id="poll">poll()</h4>
255<pre> int (*poll)(struct sensors_poll_device_t *dev,
256 sensors_event_t* data, int count);</pre>
257<p>Returns an array of sensor data. This function must block until events are
258 available. It will return the number of events read on success, or -errno in
259 case of an error.</p>
260<p>The number of events returned in data must be less or equal to the &quot;count&quot;
261 argument. This function shall never return 0 (no event).</p>