1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_SENSORS_INTERFACE_H
18 #define ANDROID_SENSORS_INTERFACE_H
19 
20 #include <stdint.h>
21 #include <sys/cdefs.h>
22 #include <sys/types.h>
23 
24 #include <hardware/hardware.h>
25 #include <cutils/native_handle.h>
26 
27 #include "sensors-base.h"
28 
29 __BEGIN_DECLS
30 
31 /*****************************************************************************/
32 
33 #define SENSORS_HEADER_VERSION          1
34 #define SENSORS_MODULE_API_VERSION_0_1  HARDWARE_MODULE_API_VERSION(0, 1)
35 #define SENSORS_DEVICE_API_VERSION_0_1  HARDWARE_DEVICE_API_VERSION_2(0, 1, SENSORS_HEADER_VERSION)
36 #define SENSORS_DEVICE_API_VERSION_1_0  HARDWARE_DEVICE_API_VERSION_2(1, 0, SENSORS_HEADER_VERSION)
37 #define SENSORS_DEVICE_API_VERSION_1_1  HARDWARE_DEVICE_API_VERSION_2(1, 1, SENSORS_HEADER_VERSION)
38 #define SENSORS_DEVICE_API_VERSION_1_2  HARDWARE_DEVICE_API_VERSION_2(1, 2, SENSORS_HEADER_VERSION)
39 #define SENSORS_DEVICE_API_VERSION_1_3  HARDWARE_DEVICE_API_VERSION_2(1, 3, SENSORS_HEADER_VERSION)
40 #define SENSORS_DEVICE_API_VERSION_1_4  HARDWARE_DEVICE_API_VERSION_2(1, 4, SENSORS_HEADER_VERSION)
41 
42 /**
43  * Please see the Sensors section of source.android.com for an
44  * introduction to and detailed descriptions of Android sensor types:
45  * http://source.android.com/devices/sensors/index.html
46  */
47 
48 /**
49  * The id of this module
50  */
51 #define SENSORS_HARDWARE_MODULE_ID "sensors"
52 
53 /**
54  * Name of the sensors device to open
55  */
56 #define SENSORS_HARDWARE_POLL       "poll"
57 
58 /**
59  * Sensor handle is greater than 0 and less than INT32_MAX.
60  *
61  * **** Deprecated ****
62  * Defined values below are kept for code compatibility. Note sensor handle can be as large as
63  * INT32_MAX.
64  */
65 #define SENSORS_HANDLE_BASE             0
66 #define SENSORS_HANDLE_BITS             31
67 #define SENSORS_HANDLE_COUNT            (1ull<<SENSORS_HANDLE_BITS)
68 
69 
70 /*
71  * **** Deprecated *****
72  * flags for (*batch)()
73  * Availability: SENSORS_DEVICE_API_VERSION_1_0
74  * see (*batch)() documentation for details.
75  * Deprecated as of  SENSORS_DEVICE_API_VERSION_1_3.
76  * WAKE_UP_* sensors replace WAKE_UPON_FIFO_FULL concept.
77  */
78 enum {
79     SENSORS_BATCH_DRY_RUN               = 0x00000001,
80     SENSORS_BATCH_WAKE_UPON_FIFO_FULL   = 0x00000002
81 };
82 
83 /*
84  * what field for meta_data_event_t
85  */
86 enum {
87     /* a previous flush operation has completed */
88     // META_DATA_FLUSH_COMPLETE = 1,
89     META_DATA_VERSION   /* always last, leave auto-assigned */
90 };
91 
92 /*
93  * The permission to use for body sensors (like heart rate monitors).
94  * See sensor types for more details on what sensors should require this
95  * permission.
96  */
97 #define SENSOR_PERMISSION_BODY_SENSORS "android.permission.BODY_SENSORS"
98 
99 /*
100  * sensor flags legacy names
101  *
102  * please use SENSOR_FLAG_* directly for new implementation.
103  * @see sensor_t
104  */
105 
106 #define SENSOR_FLAG_MASK(nbit, shift)   (((1<<(nbit))-1)<<(shift))
107 #define SENSOR_FLAG_MASK_1(shift)       SENSOR_FLAG_MASK(1, shift)
108 
109 /*
110  * Mask and shift for reporting mode sensor flags defined above.
111  */
112 #define REPORTING_MODE_SHIFT            SENSOR_FLAG_SHIFT_REPORTING_MODE
113 #define REPORTING_MODE_NBIT             (3)
114 #define REPORTING_MODE_MASK             SENSOR_FLAG_MASK_REPORTING_MODE
115 
116 /*
117  * Mask and shift for data_injection mode sensor flags defined above.
118  */
119 #define DATA_INJECTION_SHIFT            SENSOR_FLAG_SHIFT_DATA_INJECTION
120 #define DATA_INJECTION_MASK             SENSOR_FLAG_DATA_INJECTION
121 
122 /*
123  * Mask and shift for dynamic sensor flag.
124  */
125 #define DYNAMIC_SENSOR_SHIFT            SENSOR_FLAG_SHIFT_DYNAMIC_SENSOR
126 #define DYNAMIC_SENSOR_MASK             SENSOR_FLAG_DYNAMIC_SENSOR
127 
128 /*
129  * Mask and shift for sensor additional information support.
130  */
131 #define ADDITIONAL_INFO_SHIFT           SENSOR_FLAG_SHIFT_ADDITIONAL_INFO
132 #define ADDITIONAL_INFO_MASK            SENSOR_FLAG_ADDITIONAL_INFO
133 
134 /*
135  * Legacy alias of SENSOR_TYPE_MAGNETIC_FIELD.
136  *
137  * Previously, the type of a sensor measuring local magnetic field is named
138  * SENSOR_TYPE_GEOMAGNETIC_FIELD and SENSOR_TYPE_MAGNETIC_FIELD is its alias.
139  * SENSOR_TYPE_MAGNETIC_FIELD is redefined as primary name to avoid confusion.
140  * SENSOR_TYPE_GEOMAGNETIC_FIELD is the alias and is deprecating. New implementation must not use
141  * SENSOR_TYPE_GEOMAGNETIC_FIELD.
142  */
143 #define SENSOR_TYPE_GEOMAGNETIC_FIELD   SENSOR_TYPE_MAGNETIC_FIELD
144 
145 /*
146  * Sensor string types for Android defined sensor types.
147  *
148  * For Android defined sensor types, string type will be override in sensor service and thus no
149  * longer needed to be added to sensor_t data structure.
150  *
151  * These definitions are going to be removed soon.
152  */
153 #define SENSOR_STRING_TYPE_ACCELEROMETER                "android.sensor.accelerometer"
154 #define SENSOR_STRING_TYPE_MAGNETIC_FIELD               "android.sensor.magnetic_field"
155 #define SENSOR_STRING_TYPE_ORIENTATION                  "android.sensor.orientation"
156 #define SENSOR_STRING_TYPE_GYROSCOPE                    "android.sensor.gyroscope"
157 #define SENSOR_STRING_TYPE_LIGHT                        "android.sensor.light"
158 #define SENSOR_STRING_TYPE_PRESSURE                     "android.sensor.pressure"
159 #define SENSOR_STRING_TYPE_TEMPERATURE                  "android.sensor.temperature"
160 #define SENSOR_STRING_TYPE_PROXIMITY                    "android.sensor.proximity"
161 #define SENSOR_STRING_TYPE_GRAVITY                      "android.sensor.gravity"
162 #define SENSOR_STRING_TYPE_LINEAR_ACCELERATION          "android.sensor.linear_acceleration"
163 #define SENSOR_STRING_TYPE_ROTATION_VECTOR              "android.sensor.rotation_vector"
164 #define SENSOR_STRING_TYPE_RELATIVE_HUMIDITY            "android.sensor.relative_humidity"
165 #define SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE          "android.sensor.ambient_temperature"
166 #define SENSOR_STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED  "android.sensor.magnetic_field_uncalibrated"
167 #define SENSOR_STRING_TYPE_GAME_ROTATION_VECTOR         "android.sensor.game_rotation_vector"
168 #define SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED       "android.sensor.gyroscope_uncalibrated"
169 #define SENSOR_STRING_TYPE_SIGNIFICANT_MOTION           "android.sensor.significant_motion"
170 #define SENSOR_STRING_TYPE_STEP_DETECTOR                "android.sensor.step_detector"
171 #define SENSOR_STRING_TYPE_STEP_COUNTER                 "android.sensor.step_counter"
172 #define SENSOR_STRING_TYPE_GEOMAGNETIC_ROTATION_VECTOR  "android.sensor.geomagnetic_rotation_vector"
173 #define SENSOR_STRING_TYPE_HEART_RATE                   "android.sensor.heart_rate"
174 #define SENSOR_STRING_TYPE_TILT_DETECTOR                "android.sensor.tilt_detector"
175 #define SENSOR_STRING_TYPE_WAKE_GESTURE                 "android.sensor.wake_gesture"
176 #define SENSOR_STRING_TYPE_GLANCE_GESTURE               "android.sensor.glance_gesture"
177 #define SENSOR_STRING_TYPE_PICK_UP_GESTURE              "android.sensor.pick_up_gesture"
178 #define SENSOR_STRING_TYPE_WRIST_TILT_GESTURE           "android.sensor.wrist_tilt_gesture"
179 #define SENSOR_STRING_TYPE_DEVICE_ORIENTATION           "android.sensor.device_orientation"
180 #define SENSOR_STRING_TYPE_POSE_6DOF                    "android.sensor.pose_6dof"
181 #define SENSOR_STRING_TYPE_STATIONARY_DETECT            "android.sensor.stationary_detect"
182 #define SENSOR_STRING_TYPE_MOTION_DETECT                "android.sensor.motion_detect"
183 #define SENSOR_STRING_TYPE_HEART_BEAT                   "android.sensor.heart_beat"
184 #define SENSOR_STRING_TYPE_DYNAMIC_SENSOR_META          "android.sensor.dynamic_sensor_meta"
185 #define SENSOR_STRING_TYPE_ADDITIONAL_INFO              "android.sensor.additional_info"
186 #define SENSOR_STRING_TYPE_LOW_LATENCY_OFFBODY_DETECT   "android.sensor.low_latency_offbody_detect"
187 #define SENSOR_STRING_TYPE_ACCELEROMETER_UNCALIBRATED   "android.sensor.accelerometer_uncalibrated"
188 
189 /**
190  * Values returned by the accelerometer in various locations in the universe.
191  * all values are in SI units (m/s^2)
192  */
193 #define GRAVITY_SUN             (275.0f)
194 #define GRAVITY_EARTH           (9.80665f)
195 
196 /** Maximum magnetic field on Earth's surface */
197 #define MAGNETIC_FIELD_EARTH_MAX    (60.0f)
198 
199 /** Minimum magnetic field on Earth's surface */
200 #define MAGNETIC_FIELD_EARTH_MIN    (30.0f)
201 
202 struct sensor_t;
203 
204 /**
205  * sensor event data
206  */
207 typedef struct {
208     union {
209         float v[3];
210         struct {
211             float x;
212             float y;
213             float z;
214         };
215         struct {
216             float azimuth;
217             float pitch;
218             float roll;
219         };
220     };
221     int8_t status;
222     uint8_t reserved[3];
223 } sensors_vec_t;
224 
225 /**
226  * uncalibrated accelerometer, gyroscope and magnetometer event data
227  */
228 typedef struct {
229   union {
230     float uncalib[3];
231     struct {
232       float x_uncalib;
233       float y_uncalib;
234       float z_uncalib;
235     };
236   };
237   union {
238     float bias[3];
239     struct {
240       float x_bias;
241       float y_bias;
242       float z_bias;
243     };
244   };
245 } uncalibrated_event_t;
246 
247 /**
248  * Meta data event data
249  */
250 typedef struct meta_data_event {
251     int32_t what;
252     int32_t sensor;
253 } meta_data_event_t;
254 
255 /**
256  * Dynamic sensor meta event. See the description of SENSOR_TYPE_DYNAMIC_SENSOR_META type for
257  * details.
258  */
259 typedef struct dynamic_sensor_meta_event {
260     int32_t  connected;
261     int32_t  handle;
262     const struct sensor_t * sensor; // should be NULL if connected == false
263     uint8_t uuid[16];               // UUID of a dynamic sensor (using RFC 4122 byte order)
264                                     // For UUID 12345678-90AB-CDEF-1122-334455667788 the uuid field
265                                     // should be initialized as:
266                                     // {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x11, ...}
267 } dynamic_sensor_meta_event_t;
268 
269 /**
270  * Heart rate event data
271  */
272 typedef struct {
273   // Heart rate in beats per minute.
274   // Set to 0 when status is SENSOR_STATUS_UNRELIABLE or ..._NO_CONTACT
275   float bpm;
276   // Status of the sensor for this reading. Set to one SENSOR_STATUS_...
277   // Note that this value should only be set for sensors that explicitly define
278   // the meaning of this field. This field is not piped through the framework
279   // for other sensors.
280   int8_t status;
281 } heart_rate_event_t;
282 
283 typedef struct {
284     int32_t type;                           // type of payload data, see additional_info_type_t
285     int32_t serial;                         // sequence number of this frame for this type
286     union {
287         // for each frame, a single data type, either int32_t or float, should be used.
288         int32_t data_int32[14];
289         float   data_float[14];
290     };
291 } additional_info_event_t;
292 
293 /**
294  * Union of the various types of sensor data
295  * that can be returned.
296  */
297 typedef struct sensors_event_t {
298     /* must be sizeof(struct sensors_event_t) */
299     int32_t version;
300 
301     /* sensor identifier */
302     int32_t sensor;
303 
304     /* sensor type */
305     int32_t type;
306 
307     /* reserved */
308     int32_t reserved0;
309 
310     /* time is in nanosecond */
311     int64_t timestamp;
312 
313     union {
314         union {
315             float           data[16];
316 
317             /* acceleration values are in meter per second per second (m/s^2) */
318             sensors_vec_t   acceleration;
319 
320             /* magnetic vector values are in micro-Tesla (uT) */
321             sensors_vec_t   magnetic;
322 
323             /* orientation values are in degrees */
324             sensors_vec_t   orientation;
325 
326             /* gyroscope values are in rad/s */
327             sensors_vec_t   gyro;
328 
329             /* temperature is in degrees centigrade (Celsius) */
330             float           temperature;
331 
332             /* distance in centimeters */
333             float           distance;
334 
335             /* light in SI lux units */
336             float           light;
337 
338             /* pressure in hectopascal (hPa) */
339             float           pressure;
340 
341             /* relative humidity in percent */
342             float           relative_humidity;
343 
344             /* uncalibrated gyroscope values are in rad/s */
345             uncalibrated_event_t uncalibrated_gyro;
346 
347             /* uncalibrated magnetometer values are in micro-Teslas */
348             uncalibrated_event_t uncalibrated_magnetic;
349 
350             /* uncalibrated accelerometer values are in  meter per second per second (m/s^2) */
351             uncalibrated_event_t uncalibrated_accelerometer;
352 
353             /* heart rate data containing value in bpm and status */
354             heart_rate_event_t heart_rate;
355 
356             /* this is a special event. see SENSOR_TYPE_META_DATA above.
357              * sensors_meta_data_event_t events are all reported with a type of
358              * SENSOR_TYPE_META_DATA. The handle is ignored and must be zero.
359              */
360             meta_data_event_t meta_data;
361 
362             /* dynamic sensor meta event. See SENSOR_TYPE_DYNAMIC_SENSOR_META type for details */
363             dynamic_sensor_meta_event_t dynamic_sensor_meta;
364 
365             /*
366              * special additional sensor information frame, see
367              * SENSOR_TYPE_ADDITIONAL_INFO for details.
368              */
369             additional_info_event_t additional_info;
370         };
371 
372         union {
373             uint64_t        data[8];
374 
375             /* step-counter */
376             uint64_t        step_counter;
377         } u64;
378     };
379 
380     /* Reserved flags for internal use. Set to zero. */
381     uint32_t flags;
382 
383     uint32_t reserved1[3];
384 } sensors_event_t;
385 
386 
387 /* see SENSOR_TYPE_META_DATA */
388 typedef sensors_event_t sensors_meta_data_event_t;
389 
390 
391 /**
392  * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
393  * and the fields of this data structure must begin with hw_module_t
394  * followed by module specific information.
395  */
396 struct sensors_module_t {
397     struct hw_module_t common;
398 
399     /**
400      * Enumerate all available sensors. The list is returned in "list".
401      * return number of sensors in the list
402      */
403     int (*get_sensors_list)(struct sensors_module_t* module,
404             struct sensor_t const** list);
405 
406     /**
407      *  Place the module in a specific mode. The following modes are defined
408      *
409      *  0 - Normal operation. Default state of the module.
410      *  1 - Loopback mode. Data is injected for the supported
411      *      sensors by the sensor service in this mode.
412      * return 0 on success
413      *         -EINVAL if requested mode is not supported
414      *         -EPERM if operation is not allowed
415      */
416     int (*set_operation_mode)(unsigned int mode);
417 };
418 
419 struct sensor_t {
420 
421     /* Name of this sensor.
422      * All sensors of the same "type" must have a different "name".
423      */
424     const char*     name;
425 
426     /* vendor of the hardware part */
427     const char*     vendor;
428 
429     /* version of the hardware part + driver. The value of this field
430      * must increase when the driver is updated in a way that changes the
431      * output of this sensor. This is important for fused sensors when the
432      * fusion algorithm is updated.
433      */
434     int             version;
435 
436     /* handle that identifies this sensors. This handle is used to reference
437      * this sensor throughout the HAL API.
438      */
439     int             handle;
440 
441     /* this sensor's type. */
442     int             type;
443 
444     /* maximum range of this sensor's value in SI units */
445     float           maxRange;
446 
447     /* smallest difference between two values reported by this sensor */
448     float           resolution;
449 
450     /* rough estimate of this sensor's power consumption in mA */
451     float           power;
452 
453     /* this value depends on the reporting mode:
454      *
455      *   continuous: minimum sample period allowed in microseconds
456      *   on-change : 0
457      *   one-shot  :-1
458      *   special   : 0, unless otherwise noted
459      */
460     int32_t         minDelay;
461 
462     /* number of events reserved for this sensor in the batch mode FIFO.
463      * If there is a dedicated FIFO for this sensor, then this is the
464      * size of this FIFO. If the FIFO is shared with other sensors,
465      * this is the size reserved for that sensor and it can be zero.
466      */
467     uint32_t        fifoReservedEventCount;
468 
469     /* maximum number of events of this sensor that could be batched.
470      * This is especially relevant when the FIFO is shared between
471      * several sensors; this value is then set to the size of that FIFO.
472      */
473     uint32_t        fifoMaxEventCount;
474 
475     /* type of this sensor as a string.
476      *
477      * If type is OEM specific or sensor manufacturer specific type
478      * (>=SENSOR_TYPE_DEVICE_PRIVATE_BASE), this string must be defined with reserved domain of
479      * vendor/OEM as a prefix, e.g. com.google.glass.onheaddetector
480      *
481      * For sensors of Android defined types, Android framework will override this value. It is ok to
482      * leave it pointing to an empty string.
483      */
484     const char*    stringType;
485 
486     /* permission required to see this sensor, register to it and receive data.
487      * Set to "" if no permission is required. Some sensor types like the
488      * heart rate monitor have a mandatory require_permission.
489      * For sensors that always require a specific permission, like the heart
490      * rate monitor, the android framework might overwrite this string
491      * automatically.
492      */
493     const char*    requiredPermission;
494 
495     /* This value is defined only for continuous mode and on-change sensors. It is the delay between
496      * two sensor events corresponding to the lowest frequency that this sensor supports. When lower
497      * frequencies are requested through batch()/setDelay() the events will be generated at this
498      * frequency instead. It can be used by the framework or applications to estimate when the batch
499      * FIFO may be full.
500      *
501      * NOTE: 1) period_ns is in nanoseconds where as maxDelay/minDelay are in microseconds.
502      *              continuous, on-change: maximum sampling period allowed in microseconds.
503      *              one-shot, special : 0
504      *   2) maxDelay should always fit within a 32 bit signed integer. It is declared as 64 bit
505      *      on 64 bit architectures only for binary compatibility reasons.
506      * Availability: SENSORS_DEVICE_API_VERSION_1_3
507      */
508     #ifdef __LP64__
509        int64_t maxDelay;
510     #else
511        int32_t maxDelay;
512     #endif
513 
514     /* Flags for sensor. See SENSOR_FLAG_* above. Only the least significant 32 bits are used here.
515      * It is declared as 64 bit on 64 bit architectures only for binary compatibility reasons.
516      * Availability: SENSORS_DEVICE_API_VERSION_1_3
517      */
518     #ifdef __LP64__
519        uint64_t flags;
520     #else
521        uint32_t flags;
522     #endif
523 
524     /* reserved fields, must be zero */
525     void*           reserved[2];
526 };
527 
528 /**
529  * Shared memory information for a direct channel
530  */
531 struct sensors_direct_mem_t {
532     int type;                           // enum SENSOR_DIRECT_MEM_...
533     int format;                         // enum SENSOR_DIRECT_FMT_...
534     size_t size;                        // size of the memory region, in bytes
535     const struct native_handle *handle; // shared memory handle, which is interpreted differently
536                                         // depending on type
537 };
538 
539 /**
540  * Direct channel report configuration
541  */
542 struct sensors_direct_cfg_t {
543     int rate_level;             // enum SENSOR_DIRECT_RATE_...
544 };
545 
546 /*
547  * sensors_poll_device_t is used with SENSORS_DEVICE_API_VERSION_0_1
548  * and is present for backward binary and source compatibility.
549  * See the Sensors HAL interface section for complete descriptions of the
550  * following functions:
551  * http://source.android.com/devices/sensors/index.html#hal
552  */
553 struct sensors_poll_device_t {
554     struct hw_device_t common;
555     int (*activate)(struct sensors_poll_device_t *dev,
556             int sensor_handle, int enabled);
557     int (*setDelay)(struct sensors_poll_device_t *dev,
558             int sensor_handle, int64_t sampling_period_ns);
559     int (*poll)(struct sensors_poll_device_t *dev,
560             sensors_event_t* data, int count);
561 };
562 
563 /*
564  * struct sensors_poll_device_1 is used in HAL versions >= SENSORS_DEVICE_API_VERSION_1_0
565  */
566 typedef struct sensors_poll_device_1 {
567     union {
568         /* sensors_poll_device_1 is compatible with sensors_poll_device_t,
569          * and can be down-cast to it
570          */
571         struct sensors_poll_device_t v0;
572 
573         struct {
574             struct hw_device_t common;
575 
576             /* Activate/de-activate one sensor.
577              *
578              * sensor_handle is the handle of the sensor to change.
579              * enabled set to 1 to enable, or 0 to disable the sensor.
580              *
581              * After sensor de-activation, existing sensor events that have not
582              * been picked up by poll() should be abandoned immediately so that
583              * subsequent activation will not get stale sensor events (events
584              * that is generated prior to the latter activation).
585              *
586              * Return 0 on success, negative errno code otherwise.
587              */
588             int (*activate)(struct sensors_poll_device_t *dev,
589                     int sensor_handle, int enabled);
590 
591             /**
592              * Set the events's period in nanoseconds for a given sensor.
593              * If sampling_period_ns > max_delay it will be truncated to
594              * max_delay and if sampling_period_ns < min_delay it will be
595              * replaced by min_delay.
596              */
597             int (*setDelay)(struct sensors_poll_device_t *dev,
598                     int sensor_handle, int64_t sampling_period_ns);
599 
600             /**
601              * Write an array of sensor_event_t to data. The size of the
602              * available buffer is specified by count. Returns number of
603              * valid sensor_event_t.
604              *
605              * This function should block if there is no sensor event
606              * available when being called. Thus, return value should always be
607              * positive.
608              */
609             int (*poll)(struct sensors_poll_device_t *dev,
610                     sensors_event_t* data, int count);
611         };
612     };
613 
614 
615     /*
616      * Sets a sensor’s parameters, including sampling frequency and maximum
617      * report latency. This function can be called while the sensor is
618      * activated, in which case it must not cause any sensor measurements to
619      * be lost: transitioning from one sampling rate to the other cannot cause
620      * lost events, nor can transitioning from a high maximum report latency to
621      * a low maximum report latency.
622      * See the Batching sensor results page for details:
623      * http://source.android.com/devices/sensors/batching.html
624      */
625     int (*batch)(struct sensors_poll_device_1* dev,
626             int sensor_handle, int flags, int64_t sampling_period_ns,
627             int64_t max_report_latency_ns);
628 
629     /*
630      * Flush adds a META_DATA_FLUSH_COMPLETE event (sensors_event_meta_data_t)
631      * to the end of the "batch mode" FIFO for the specified sensor and flushes
632      * the FIFO.
633      * If the FIFO is empty or if the sensor doesn't support batching (FIFO size zero),
634      * it should return SUCCESS along with a trivial META_DATA_FLUSH_COMPLETE event added to the
635      * event stream. This applies to all sensors other than one-shot sensors.
636      * If the sensor is a one-shot sensor, flush must return -EINVAL and not generate
637      * any flush complete metadata.
638      * If the sensor is not active at the time flush() is called, flush() should return
639      * -EINVAL.
640      */
641     int (*flush)(struct sensors_poll_device_1* dev, int sensor_handle);
642 
643     /*
644      * Inject a single sensor sample to be to this device.
645      * data points to the sensor event to be injected
646      * return 0 on success
647      *         -EPERM if operation is not allowed
648      *         -EINVAL if sensor event cannot be injected
649      */
650     int (*inject_sensor_data)(struct sensors_poll_device_1 *dev, const sensors_event_t *data);
651 
652     /*
653      * Register/unregister direct report channel.
654      *
655      * A HAL declares support for direct report by setting non-NULL values for both
656      * register_direct_channel and config_direct_report.
657      *
658      * This function has two operation modes:
659      *
660      * Register: mem != NULL, register a channel using supplied shared memory information. By the
661      * time this function returns, sensors must finish initializing shared memory content
662      * (format dependent, see SENSOR_DIRECT_FMT_*).
663      *      Parameters:
664      *          mem             points to a valid struct sensors_direct_mem_t.
665      *          channel_handle  is ignored.
666      *      Return value:
667      *          A handle of channel (>0, <INT32_MAX) when success, which later can be referred in
668      *          unregister or config_direct_report call, or error code (<0) when failed
669      * Unregister: mem == NULL, unregister a previously registered channel.
670      *      Parameters:
671      *          mem             set to NULL
672      *          channel_handle  contains handle of channel to be unregistered
673      *      Return value:
674      *          0, even if the channel_handle is invalid, in which case it will be a no-op.
675      */
676     int (*register_direct_channel)(struct sensors_poll_device_1 *dev,
677             const struct sensors_direct_mem_t* mem, int channel_handle);
678 
679     /*
680      * Configure direct sensor event report in direct channel.
681      *
682      * Start, modify rate or stop direct report of a sensor in a certain direct channel. A special
683      * case is setting sensor handle -1 to stop means to stop all active sensor report on the
684      * channel specified.
685      *
686      * A HAL declares support for direct report by setting non-NULL values for both
687      * register_direct_channel and config_direct_report.
688      *
689      * Parameters:
690      *      sensor_handle   sensor to be configured. The sensor has to support direct report
691      *                      mode by setting flags of sensor_t. Also, direct report mode is only
692      *                      defined for continuous reporting mode sensors.
693      *      channel_handle  channel handle to be configured.
694      *      config          direct report parameters, see sensor_direct_cfg_t.
695      * Return value:
696      *      - when sensor is started or sensor rate level is changed: return positive identifier of
697      *        sensor in specified channel if successful, otherwise return negative error code.
698      *      - when sensor is stopped: return 0 for success or negative error code for failure.
699      */
700     int (*config_direct_report)(struct sensors_poll_device_1 *dev,
701             int sensor_handle, int channel_handle, const struct sensors_direct_cfg_t *config);
702 
703     /*
704      * Reserved for future use, must be zero.
705      */
706     void (*reserved_procs[5])(void);
707 
708 } sensors_poll_device_1_t;
709 
710 
711 /** convenience API for opening and closing a device */
712 
sensors_open(const struct hw_module_t * module,struct sensors_poll_device_t ** device)713 static inline int sensors_open(const struct hw_module_t* module,
714         struct sensors_poll_device_t** device) {
715     return module->methods->open(module,
716             SENSORS_HARDWARE_POLL, TO_HW_DEVICE_T_OPEN(device));
717 }
718 
sensors_close(struct sensors_poll_device_t * device)719 static inline int sensors_close(struct sensors_poll_device_t* device) {
720     return device->common.close(&device->common);
721 }
722 
sensors_open_1(const struct hw_module_t * module,sensors_poll_device_1_t ** device)723 static inline int sensors_open_1(const struct hw_module_t* module,
724         sensors_poll_device_1_t** device) {
725     return module->methods->open(module,
726             SENSORS_HARDWARE_POLL, TO_HW_DEVICE_T_OPEN(device));
727 }
728 
sensors_close_1(sensors_poll_device_1_t * device)729 static inline int sensors_close_1(sensors_poll_device_1_t* device) {
730     return device->common.close(&device->common);
731 }
732 
733 __END_DECLS
734 
735 #endif  // ANDROID_SENSORS_INTERFACE_H
736