android_sensor_sys/
lib.rs

1// Copyright 2016 The android_sensor_sys Developers
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7#![allow(non_camel_case_types)]
8
9extern crate android_looper_sys;
10extern crate libc;
11
12use android_looper_sys::*;
13use libc::{c_int, c_float, c_void, c_char};
14use std::mem;
15
16/// Earth's gravity in m/s^2
17pub const ASENSOR_STANDARD_GRAVITY: f32 = 9.80665;
18/// Maximum magnetic field on Earth's surface in uT
19pub const ASENSOR_MAGNETIC_FIELD_EARTH_MAX: f32 = 60.0;
20/// Minimum magnetic field on Earth's surface in uT
21pub const ASENSOR_MAGNETIC_FIELD_EARTH_MIN: f32 = 30.0;
22
23#[repr(C)]
24#[derive(Copy)]
25pub struct AHeartRateEvent {
26    pub bpm: c_float,
27    pub status: i8,
28}
29impl Clone for AHeartRateEvent {
30    fn clone(&self) -> Self {
31        *self
32    }
33}
34impl Default for AHeartRateEvent {
35    fn default() -> Self {
36        unsafe { mem::zeroed() }
37    }
38}
39
40#[repr(C)]
41#[derive(Copy)]
42pub struct AMetaDataEvent {
43    pub what: i32,
44    pub sensor: i32,
45}
46impl Clone for AMetaDataEvent {
47    fn clone(&self) -> Self {
48        *self
49    }
50}
51impl Default for AMetaDataEvent {
52    fn default() -> Self {
53        unsafe { mem::zeroed() }
54    }
55}
56
57/// ASensor is an opaque type that provides information about an hardware sensors.
58///
59/// A ASensor pointer can be obtained using `ASensorManager_getDefaultSensor()`, `ASensorManager_getDefaultSensorEx()` or from a `ASensorList`.
60pub enum ASensor { }
61
62#[repr(C)]
63#[derive(Copy)]
64pub struct ASensorEvent {
65    pub version: i32,
66    pub sensor: i32,
67    pub event_type: i32,
68    reserved0: i32,
69    pub timestamp: i64,
70    _bindgen_data_1_: [u64; 8usize],
71    pub flags: i32,
72    reserved1: [i32; 3usize],
73}
74impl ASensorEvent {
75    unsafe fn _data_f32(&mut self) -> *mut [c_float; 16usize] {
76        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
77        mem::transmute(raw.offset(0))
78    }
79    pub unsafe fn mut_vector(&mut self) -> *mut ASensorVector {
80        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
81        mem::transmute(raw.offset(0))
82    }
83    pub unsafe fn vector(&self) -> *const ASensorVector {
84        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
85        mem::transmute(raw.offset(0))
86    }
87    pub unsafe fn mut_acceleration(&mut self) -> *mut ASensorVector {
88        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
89        mem::transmute(raw.offset(0))
90    }
91    pub unsafe fn acceleration(&self) -> *const ASensorVector {
92        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
93        mem::transmute(raw.offset(0))
94    }
95    pub unsafe fn mut_magnetic(&mut self) -> *mut ASensorVector {
96        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
97        mem::transmute(raw.offset(0))
98    }
99    pub unsafe fn magnetic(&self) -> *const ASensorVector {
100        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
101        mem::transmute(raw.offset(0))
102    }
103    pub unsafe fn mut_temperature(&mut self) -> *mut c_float {
104        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
105        mem::transmute(raw.offset(0))
106    }
107    pub unsafe fn temperature(&self) -> *const c_float {
108        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
109        mem::transmute(raw.offset(0))
110    }
111    pub unsafe fn mut_distance(&mut self) -> *mut c_float {
112        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
113        mem::transmute(raw.offset(0))
114    }
115    pub unsafe fn distance(&self) -> *const c_float {
116        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
117        mem::transmute(raw.offset(0))
118    }
119    pub unsafe fn mut_light(&mut self) -> *mut c_float {
120        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
121        mem::transmute(raw.offset(0))
122    }
123    pub unsafe fn light(&self) -> *const c_float {
124        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
125        mem::transmute(raw.offset(0))
126    }
127    pub unsafe fn mut_pressure(&mut self) -> *mut c_float {
128        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
129        mem::transmute(raw.offset(0))
130    }
131    pub unsafe fn pressure(&self) -> *const c_float {
132        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
133        mem::transmute(raw.offset(0))
134    }
135    pub unsafe fn mut_relative_humidity(&mut self) -> *mut c_float {
136        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
137        mem::transmute(raw.offset(0))
138    }
139    pub unsafe fn relative_humidity(&self) -> *const c_float {
140        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
141        mem::transmute(raw.offset(0))
142    }
143    pub unsafe fn mut_uncalibrated_gyro(&mut self) -> *mut AUncalibratedEvent {
144        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
145        mem::transmute(raw.offset(0))
146    }
147    pub unsafe fn uncalibrated_gyro(&self) -> *const AUncalibratedEvent {
148        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
149        mem::transmute(raw.offset(0))
150    }
151    pub unsafe fn mut_uncalibrated_magnetic(&mut self) -> *mut AUncalibratedEvent {
152        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
153        mem::transmute(raw.offset(0))
154    }
155    pub unsafe fn uncalibrated_magnetic(&self) -> *const AUncalibratedEvent {
156        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
157        mem::transmute(raw.offset(0))
158    }
159    pub unsafe fn mut_meta_data(&mut self) -> *mut AMetaDataEvent {
160        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
161        mem::transmute(raw.offset(0))
162    }
163    pub unsafe fn meta_data(&self) -> *const AMetaDataEvent {
164        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
165        mem::transmute(raw.offset(0))
166    }
167    pub unsafe fn mut_heart_rate(&mut self) -> *mut AHeartRateEvent {
168        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
169        mem::transmute(raw.offset(0))
170    }
171    pub unsafe fn heart_rate(&self) -> *const AHeartRateEvent {
172        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
173        mem::transmute(raw.offset(0))
174    }
175
176    unsafe fn mut_data_u64(&mut self) -> *mut ASensorEvent_u64 {
177        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
178        mem::transmute(raw.offset(0))
179    }
180    unsafe fn data_u64(&self) -> *const ASensorEvent_u64 {
181        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
182        mem::transmute(raw.offset(0))
183    }
184    pub unsafe fn mut_step_counter(&mut self) -> *mut u64 {
185        (*self.mut_data_u64()).mut_step_counter()
186    }
187    pub unsafe fn step_counter(&self) -> *const u64 {
188        (*self.data_u64()).step_counter()
189    }
190}
191impl Clone for ASensorEvent {
192    fn clone(&self) -> Self {
193        *self
194    }
195}
196impl Default for ASensorEvent {
197    fn default() -> Self {
198        unsafe { mem::zeroed() }
199    }
200}
201
202#[repr(C)]
203#[derive(Copy)]
204struct ASensorEvent_u64 {
205    pub _bindgen_data_: [u64; 8usize],
206}
207impl ASensorEvent_u64 {
208    unsafe fn _data(&mut self) -> *mut [u64; 8usize] {
209        let raw: *mut u8 = mem::transmute(&self._bindgen_data_);
210        mem::transmute(raw.offset(0))
211    }
212    pub unsafe fn mut_step_counter(&mut self) -> *mut u64 {
213        let raw: *mut u8 = mem::transmute(&self._bindgen_data_);
214        mem::transmute(raw.offset(0))
215    }
216    pub unsafe fn step_counter(&self) -> *const u64 {
217        let raw: *const u8 = mem::transmute(&self._bindgen_data_);
218        mem::transmute(raw.offset(0))
219    }
220}
221impl Clone for ASensorEvent_u64 {
222    fn clone(&self) -> Self {
223        *self
224    }
225}
226impl Default for ASensorEvent_u64 {
227    fn default() -> Self {
228        unsafe { mem::zeroed() }
229    }
230}
231
232/// ASensorEventQueue is an opaque type that provides access to `ASensorEvent` from hardware sensors.
233///
234/// A new ASensorEventQueue can be obtained using `ASensorManager_createEventQueue()`.
235pub enum ASensorEventQueue { }
236
237/// ASensorList is an array of reference to `ASensor`.
238///
239/// A ASensorList can be initialized using `ASensorManager_getSensorList()`.
240pub type ASensorList = *const ASensorRef;
241
242/// ASensorRef is a type for constant pointers to `ASensor`.
243///
244/// This is used to define entry in `ASensorList` arrays.
245pub type ASensorRef = *const ASensor;
246
247/// ASensorManager is an opaque type to manage sensors and events queues.
248///
249/// ASensorManager is a singleton that can be obtained using `ASensorManager_getInstance()`.
250pub enum ASensorManager { }
251
252/// A sensor event.
253#[repr(C)]
254#[derive(Copy)]
255pub struct ASensorVector {
256    pub _bindgen_data_1_: [u32; 3usize],
257    pub status: u8,
258    pub reserved: [u8; 3usize],
259}
260
261impl ASensorVector {
262    pub unsafe fn v(&self) -> *const [c_float; 3usize] {
263        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
264        mem::transmute(raw.offset(0))
265    }
266    pub unsafe fn x(&self) -> *const c_float {
267        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
268        mem::transmute(raw.offset(0))
269    }
270    pub unsafe fn y(&self) -> *const c_float {
271        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
272        mem::transmute(raw.offset(4))
273    }
274    pub unsafe fn z(&self) -> *const c_float {
275        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
276        mem::transmute(raw.offset(8))
277    }
278    pub unsafe fn azimuth(&self) -> *const c_float {
279        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
280        mem::transmute(raw.offset(0))
281    }
282    pub unsafe fn pitch(&self) -> *const c_float {
283        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
284        mem::transmute(raw.offset(4))
285    }
286    pub unsafe fn roll(&self) -> *const c_float {
287        let raw: *const u8 = mem::transmute(&self._bindgen_data_1_);
288        mem::transmute(raw.offset(8))
289    }
290
291    pub unsafe fn mut_v(&mut self) -> *mut [c_float; 3usize] {
292        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
293        mem::transmute(raw.offset(0))
294    }
295    pub unsafe fn mut_x(&mut self) -> *mut c_float {
296        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
297        mem::transmute(raw.offset(0))
298    }
299    pub unsafe fn mut_y(&mut self) -> *mut c_float {
300        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
301        mem::transmute(raw.offset(4))
302    }
303    pub unsafe fn mut_z(&mut self) -> *mut c_float {
304        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
305        mem::transmute(raw.offset(8))
306    }
307    pub unsafe fn mut_azimuth(&mut self) -> *mut c_float {
308        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
309        mem::transmute(raw.offset(0))
310    }
311    pub unsafe fn mut_pitch(&mut self) -> *mut c_float {
312        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
313        mem::transmute(raw.offset(4))
314    }
315    pub unsafe fn mut_roll(&mut self) -> *mut c_float {
316        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
317        mem::transmute(raw.offset(8))
318    }
319}
320impl Clone for ASensorVector {
321    fn clone(&self) -> Self {
322        *self
323    }
324}
325impl Default for ASensorVector {
326    fn default() -> Self {
327        unsafe { mem::zeroed() }
328    }
329}
330
331#[repr(C)]
332#[derive(Copy)]
333pub struct AUncalibratedEvent {
334    pub _bindgen_data_1_: [u32; 3usize],
335    pub _bindgen_data_2_: [u32; 3usize],
336}
337impl AUncalibratedEvent {
338    pub unsafe fn uncalib(&mut self) -> *mut [c_float; 3usize] {
339        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
340        mem::transmute(raw.offset(0))
341    }
342    pub unsafe fn x_uncalib(&mut self) -> *mut c_float {
343        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
344        mem::transmute(raw.offset(0))
345    }
346    pub unsafe fn y_uncalib(&mut self) -> *mut c_float {
347        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
348        mem::transmute(raw.offset(4))
349    }
350    pub unsafe fn z_uncalib(&mut self) -> *mut c_float {
351        let raw: *mut u8 = mem::transmute(&self._bindgen_data_1_);
352        mem::transmute(raw.offset(8))
353    }
354    pub unsafe fn bias(&mut self) -> *mut [c_float; 3usize] {
355        let raw: *mut u8 = mem::transmute(&self._bindgen_data_2_);
356        mem::transmute(raw.offset(0))
357    }
358    pub unsafe fn x_bias(&mut self) -> *mut c_float {
359        let raw: *mut u8 = mem::transmute(&self._bindgen_data_2_);
360        mem::transmute(raw.offset(0))
361    }
362    pub unsafe fn y_bias(&mut self) -> *mut c_float {
363        let raw: *mut u8 = mem::transmute(&self._bindgen_data_2_);
364        mem::transmute(raw.offset(4))
365    }
366    pub unsafe fn z_bias(&mut self) -> *mut c_float {
367        let raw: *mut u8 = mem::transmute(&self._bindgen_data_2_);
368        mem::transmute(raw.offset(8))
369    }
370}
371impl Clone for AUncalibratedEvent {
372    fn clone(&self) -> Self {
373        *self
374    }
375}
376impl Default for AUncalibratedEvent {
377    fn default() -> Self {
378        unsafe { mem::zeroed() }
379    }
380}
381
382#[derive(Clone, Copy, Debug)]
383#[repr(u32)]
384pub enum SensorType {
385    /// `ASENSOR_TYPE_ACCELEROMETER` reporting-mode: continuous
386    ///
387    /// All values are in SI units (m/s^2) and measure the acceleration of the device minus the force of gravity.
388    Accelerometer = 1,
389    /// `ASENSOR_TYPE_MAGNETIC_FIELD` reporting-mode: continuous
390    ///
391    /// All values are in micro-Tesla (uT) and measure the geomagnetic field in the X, Y and Z axis.
392    MagneticField = 2,
393    /// `ASENSOR_TYPE_GYROSCOPE` reporting-mode: continuous
394    ///
395    /// All values are in radians/second and measure the rate of rotation around the X, Y and Z axis.
396    Gyroscope = 4,
397    /// `ASENSOR_TYPE_LIGHT` reporting-mode: on-change
398    ///
399    /// The light sensor value is returned in SI lux units.
400    Light = 5,
401    /// `ASENSOR_TYPE_PROXIMITY` reporting-mode: on-change
402    ///
403    /// The proximity sensor which turns the screen off and back on during calls is the wake-up proximity sensor. Implement wake-up proximity sensor before implementing a non wake-up proximity sensor. For the wake-up proximity sensor set the flag SENSOR_FLAG_WAKE_UP. The value corresponds to the distance to the nearest object in centimeters.
404    Proximity = 8,
405}
406#[derive(Clone, Copy, Debug)]
407#[repr(i32)]
408pub enum SensorStatus {
409    /// `ASENSOR_STATUS_NO_CONTACT` no contact
410    NoContact = -1,
411    /// `ASENSOR_STATUS_UNRELIABLE` unreliable
412    Unreliable = 0,
413    /// `ASENSOR_STATUS_ACCURACY_LOW` low accuracy
414    AccuracyLow = 1,
415    /// `ASENSOR_STATUS_ACCURACY_MEDIUM` medium accuracy
416    AccuracyMedium = 2,
417    /// `ASENSOR_STATUS_ACCURACY_HIGH` high accuracy
418    AccuracyHigh = 3,
419}
420#[derive(Clone, Copy, Debug)]
421#[repr(u32)]
422pub enum ReportingMode {
423    /// `AREPORTING_MODE_CONTINUOUS` continuous reporting
424    Continuous = 0,
425    /// `AREPORTING_MODE_ON_CHANGE` reporting on change
426    OnChange = 1,
427    /// `AREPORTING_MODE_ONE_SHOT` one shot reporting
428    OneShot = 2,
429    /// `AREPORTING_MODE_SPECIAL_TRIGGER` special trigger reporting
430    SpecialTrigger = 3,
431}
432
433extern "C" {
434    pub fn ASensorManager_getInstance() -> *mut ASensorManager;
435    pub fn ASensorManager_getSensorList(manager: *mut ASensorManager,
436                                        list: *mut ASensorList)
437                                        -> c_int;
438    pub fn ASensorManager_getDefaultSensor(manager: *mut ASensorManager,
439                                           seonsor_type: c_int)
440                                           -> *const ASensor;
441    pub fn ASensorManager_getDefaultSensorEx(manager: *mut ASensorManager,
442                                             sensor_type: c_int,
443                                             wakeUp: u8)
444                                             -> *const ASensor;
445    pub fn ASensorManager_createEventQueue(manager: *mut ASensorManager,
446                                           looper: *mut ALooper,
447                                           ident: i32,
448                                           callback: ALooper_callbackFunc,
449                                           data: *mut c_void)
450                                           -> *mut ASensorEventQueue;
451    pub fn ASensorManager_destroyEventQueue(manager: *mut ASensorManager,
452                                            queue: *mut ASensorEventQueue)
453                                            -> c_int;
454    pub fn ASensorEventQueue_enableSensor(queue: *mut ASensorEventQueue,
455                                          sensor: *const ASensor)
456                                          -> c_int;
457    pub fn ASensorEventQueue_disableSensor(queue: *mut ASensorEventQueue,
458                                           sensor: *const ASensor)
459                                           -> c_int;
460    pub fn ASensorEventQueue_setEventRate(queue: *mut ASensorEventQueue,
461                                          sensor: *const ASensor,
462                                          usec: i32)
463                                          -> c_int;
464    pub fn ASensorEventQueue_hasEvents(queue: *mut ASensorEventQueue) -> c_int;
465    pub fn ASensorEventQueue_getEvents(queue: *mut ASensorEventQueue,
466                                       events: *mut ASensorEvent,
467                                       count: isize)
468                                       -> c_int;
469    pub fn ASensor_getName(sensor: *const ASensor) -> *const c_char;
470    pub fn ASensor_getVendor(sensor: *const ASensor) -> *const c_char;
471    pub fn ASensor_getType(sensor: *const ASensor) -> c_int;
472    pub fn ASensor_getResolution(sensor: *const ASensor) -> c_float;
473    pub fn ASensor_getMinDelay(sensor: *const ASensor) -> c_int;
474    pub fn ASensor_getFifoMaxEventCount(sensor: *const ASensor) -> c_int;
475    pub fn ASensor_getFifoReservedEventCount(sensor: *const ASensor) -> c_int;
476    pub fn ASensor_getStringType(sensor: *const ASensor) -> *const c_char;
477    pub fn ASensor_getReportingMode(sensor: *const ASensor) -> c_int;
478    pub fn ASensor_isWakeUpSensor(sensor: *const ASensor) -> u8;
479}