1 /*
2 * Copyright (C) 2016 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 _CHRE_SENSOR_H_
18 #define _CHRE_SENSOR_H_
19
20 /**
21 * @file
22 * API dealing with sensor interaction in the Context Hub Runtime
23 * Environment.
24 *
25 * This includes the definition of our sensor types and the ability to
26 * configure them for receiving events.
27 */
28
29 #include <stdbool.h>
30 #include <stdint.h>
31
32 #include <chre/common.h>
33 #include <chre/event.h>
34 #include <chre/sensor_types.h>
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40
41 /**
42 * Base value for all of the data events for sensors.
43 *
44 * The value for a data event FOO is
45 * CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_FOO
46 *
47 * This allows for easy mapping, and also explains why there are gaps
48 * in our values since we don't have all possible sensor types assigned.
49 */
50 #define CHRE_EVENT_SENSOR_DATA_EVENT_BASE CHRE_EVENT_SENSOR_FIRST_EVENT
51
52 /**
53 * nanoappHandleEvent argument: struct chreSensorThreeAxisData
54 *
55 * The data can be interpreted using the 'x', 'y', and 'z' fields within
56 * 'readings', or by the 3D array 'v' (v[0] == x; v[1] == y; v[2] == z).
57 *
58 * All values are in SI units (m/s^2) and measure the acceleration applied to
59 * the device.
60 */
61 #define CHRE_EVENT_SENSOR_ACCELEROMETER_DATA \
62 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_ACCELEROMETER)
63
64 /**
65 * nanoappHandleEvent argument: struct chreSensorOccurrenceData
66 *
67 * Since this is a one-shot sensor, after this event is delivered to the
68 * nanoapp, the sensor automatically goes into DONE mode. Sensors of this
69 * type must be configured with a ONE_SHOT mode.
70 */
71 #define CHRE_EVENT_SENSOR_INSTANT_MOTION_DETECT_DATA \
72 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_INSTANT_MOTION_DETECT)
73
74 /**
75 * nanoappHandleEvent argument: struct chreSensorOccurrenceData
76 *
77 * Since this is a one-shot sensor, after this event is delivered to the
78 * nanoapp, the sensor automatically goes into DONE mode. Sensors of this
79 * type must be configured with a ONE_SHOT mode.
80 */
81 #define CHRE_EVENT_SENSOR_STATIONARY_DETECT_DATA \
82 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_STATIONARY_DETECT)
83
84 /**
85 * nanoappHandleEvent argument: struct chreSensorThreeAxisData
86 *
87 * The data can be interpreted using the 'x', 'y', and 'z' fields within
88 * 'readings', or by the 3D array 'v' (v[0] == x; v[1] == y; v[2] == z).
89 *
90 * All values are in radians/second and measure the rate of rotation
91 * around the X, Y and Z axis.
92 */
93 #define CHRE_EVENT_SENSOR_GYROSCOPE_DATA \
94 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_GYROSCOPE)
95
96 /**
97 * nanoappHandleEvent argument: struct chreSensorThreeAxisData
98 *
99 * The data can be interpreted using the 'x', 'y', and 'z' fields within
100 * 'readings', or by the 3D array 'v' (v[0] == x; v[1] == y; v[2] == z).
101 *
102 * All values are in micro-Tesla (uT) and measure the geomagnetic
103 * field in the X, Y and Z axis.
104 */
105 #define CHRE_EVENT_SENSOR_GEOMAGNETIC_FIELD_DATA \
106 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD)
107
108 /**
109 * nanoappHandleEvent argument: struct chreSensorFloatData
110 *
111 * The data can be interpreted using the 'pressure' field within 'readings'.
112 * This value is in hectopascals (hPa).
113 */
114 #define CHRE_EVENT_SENSOR_PRESSURE_DATA \
115 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_PRESSURE)
116
117 /**
118 * nanoappHandleEvent argument: struct chreSensorFloatData
119 *
120 * The data can be interpreted using the 'light' field within 'readings'.
121 * This value is in SI lux units.
122 */
123 #define CHRE_EVENT_SENSOR_LIGHT_DATA \
124 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_LIGHT)
125
126 /**
127 * nanoappHandleEvent argument: struct chreSensorByteData
128 *
129 * The data is interpreted from the following fields in 'readings':
130 * o 'isNear': If set to 1, we are nearby (on the order of centimeters);
131 * if set to 0, we are far. The meaning of near/far in this field must be
132 * consistent with the Android definition.
133 * o 'invalid': If set to 1, this is not a valid reading of this data.
134 * As of CHRE API v1.2, this field is deprecated and must always be set to
135 * 0. If an invalid reading is generated by the sensor hardware, it must
136 * be dropped and not delivered to any nanoapp.
137 *
138 * In prior versions of the CHRE API, there can be an invalid event generated
139 * upon configuring this sensor. Thus, the 'invalid' field must be checked on
140 * the first event before interpreting 'isNear'.
141 */
142 #define CHRE_EVENT_SENSOR_PROXIMITY_DATA \
143 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_PROXIMITY)
144
145 /**
146 * nanoappHandleEvent argument: struct chreSensorOccurrenceData
147 *
148 * This data is generated every time a step is taken by the user.
149 *
150 * This is backed by the same algorithm that feeds Android's
151 * SENSOR_TYPE_STEP_DETECTOR, and therefore sacrifices some accuracy to target
152 * an update latency of under 2 seconds.
153 *
154 * @since v1.3
155 */
156 #define CHRE_EVENT_SENSOR_STEP_DETECT_DATA \
157 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_STEP_DETECT)
158
159 /**
160 * nanoappHandleEvent argument: struct chreSensorUint64Data
161 *
162 * The value of the data is the cumulative number of steps taken by the user
163 * since the last reboot while the sensor is active. This data is generated
164 * every time a step is taken by the user.
165 *
166 * This is backed by the same algorithm that feeds Android's
167 * SENSOR_TYPE_STEP_COUNTER, and therefore targets high accuracy with under
168 * 10 seconds of update latency.
169 *
170 * @since v1.5
171 */
172 #define CHRE_EVENT_SENSOR_STEP_COUNTER_DATA \
173 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_STEP_COUNTER)
174
175 /**
176 * nanoappHandleEvent argument: struct chreSensorFloatData
177 *
178 * The value of the data is the measured hinge angle between 0 and 360 degrees
179 * inclusive.
180 *
181 * This is backed by the same algorithm that feeds Android's
182 * SENSOR_TYPE_HINGE_ANGLE.
183 *
184 * @since v1.5
185 */
186 #define CHRE_EVENT_SENSOR_HINGE_ANGLE_DATA \
187 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_HINGE_ANGLE)
188
189 /**
190 * nanoappHandleEvent argument: struct chreSensorThreeAxisData
191 *
192 * The data can be interpreted using the 'x', 'y', and 'z' fields within
193 * 'readings', or by the 3D array 'v' (v[0] == x; v[1] == y; v[2] == z).
194 *
195 * All values are in SI units (m/s^2) and measure the acceleration applied to
196 * the device.
197 */
198 #define CHRE_EVENT_SENSOR_UNCALIBRATED_ACCELEROMETER_DATA \
199 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER)
200
201 /**
202 * nanoappHandleEvent argument: struct chreSensorThreeAxisData
203 *
204 * The data can be interpreted using the 'x', 'y', and 'z' fields within
205 * 'readings', or by the 3D array 'v' (v[0] == x; v[1] == y; v[2] == z).
206 *
207 * All values are in radians/second and measure the rate of rotation
208 * around the X, Y and Z axis.
209 */
210 #define CHRE_EVENT_SENSOR_UNCALIBRATED_GYROSCOPE_DATA \
211 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE)
212
213 /**
214 * nanoappHandleEvent argument: struct chreSensorThreeAxisData
215 *
216 * The data can be interpreted using the 'x', 'y', and 'z' fields within
217 * 'readings', or by the 3D array 'v' (v[0] == x; v[1] == y; v[2] == z).
218 *
219 * All values are in micro-Tesla (uT) and measure the geomagnetic
220 * field in the X, Y and Z axis.
221 */
222 #define CHRE_EVENT_SENSOR_UNCALIBRATED_GEOMAGNETIC_FIELD_DATA \
223 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD)
224
225 /**
226 * nanoappHandleEvent argument: struct chreSensorFloatData
227 *
228 * The data can be interpreted using the 'temperature' field within 'readings'.
229 * This value is in degrees Celsius.
230 */
231 #define CHRE_EVENT_SENSOR_ACCELEROMETER_TEMPERATURE_DATA \
232 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_ACCELEROMETER_TEMPERATURE)
233
234 /**
235 * nanoappHandleEvent argument: struct chreSensorFloatData
236 *
237 * The data can be interpreted using the 'temperature' field within 'readings'.
238 * This value is in degrees Celsius.
239 */
240 #define CHRE_EVENT_SENSOR_GYROSCOPE_TEMPERATURE_DATA \
241 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_GYROSCOPE_TEMPERATURE)
242
243 /**
244 * nanoappHandleEvent argument: struct chreSensorFloatData
245 *
246 * The data can be interpreted using the 'temperature' field within 'readings'.
247 * This value is in degrees Celsius.
248 */
249 #define CHRE_EVENT_SENSOR_GEOMAGNETIC_FIELD_TEMPERATURE_DATA \
250 (CHRE_EVENT_SENSOR_DATA_EVENT_BASE + CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD_TEMPERATURE)
251
252 /**
253 * First value for sensor events which are not data from the sensor.
254 *
255 * Unlike the data event values, these other event values don't have any
256 * mapping to sensor types.
257 */
258 #define CHRE_EVENT_SENSOR_OTHER_EVENTS_BASE \
259 (CHRE_EVENT_SENSOR_FIRST_EVENT + 0x0100)
260
261 /**
262 * nanoappHandleEvent argument: struct chreSensorSamplingStatusEvent
263 *
264 * Indicates that the interval and/or the latency which this sensor is
265 * sampling at has changed.
266 */
267 #define CHRE_EVENT_SENSOR_SAMPLING_CHANGE \
268 (CHRE_EVENT_SENSOR_OTHER_EVENTS_BASE + 0)
269
270 /**
271 * nanoappHandleEvent argument: struct chreSensorThreeAxisData
272 *
273 * The data can be interpreted using the 'x_bias', 'y_bias', and 'z_bias'
274 * field within 'readings', or by the 3D array 'bias' (bias[0] == x_bias;
275 * bias[1] == y_bias; bias[2] == z_bias). Bias is subtracted from uncalibrated
276 * data to generate calibrated data.
277 *
278 * All values are in radians/second and measure the rate of rotation
279 * around the X, Y and Z axis.
280 *
281 * If bias delivery is supported, this event is generated by default when
282 * chreSensorConfigure is called to enable for the sensor of type
283 * CHRE_SENSOR_TYPE_GYROSCOPE, or if bias delivery is explicitly enabled
284 * through chreSensorConfigureBiasEvents() for the sensor.
285 */
286 #define CHRE_EVENT_SENSOR_GYROSCOPE_BIAS_INFO \
287 (CHRE_EVENT_SENSOR_OTHER_EVENTS_BASE + 1)
288
289 /**
290 * nanoappHandleEvent argument: struct chreSensorThreeAxisData
291 *
292 * The data can be interpreted using the 'x_bias', 'y_bias', and 'z_bias'
293 * field within 'readings', or by the 3D array 'bias' (bias[0] == x_bias;
294 * bias[1] == y_bias; bias[2] == z_bias). Bias is subtracted from uncalibrated
295 * data to generate calibrated data.
296 *
297 * All values are in micro-Tesla (uT) and measure the geomagnetic
298 * field in the X, Y and Z axis.
299 *
300 * If bias delivery is supported, this event is generated by default when
301 * chreSensorConfigure is called to enable for the sensor of type
302 * CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD, or if bias delivery is explicitly enabled
303 * through chreSensorConfigureBiasEvents() for the sensor.
304 */
305 #define CHRE_EVENT_SENSOR_GEOMAGNETIC_FIELD_BIAS_INFO \
306 (CHRE_EVENT_SENSOR_OTHER_EVENTS_BASE + 2)
307
308 /**
309 * nanoappHandleEvent argument: struct chreSensorThreeAxisData
310 *
311 * The data can be interpreted using the 'x_bias', 'y_bias', and 'z_bias'
312 * field within 'readings', or by the 3D array 'bias' (bias[0] == x_bias;
313 * bias[1] == y_bias; bias[2] == z_bias). Bias is subtracted from uncalibrated
314 * data to generate calibrated data.
315 *
316 * All values are in SI units (m/s^2) and measure the acceleration applied to
317 * the device.
318 *
319 * If bias delivery is supported, this event is generated by default when
320 * chreSensorConfigure is called to enable for the sensor of type
321 * CHRE_SENSOR_TYPE_ACCELEROMETER, or if bias delivery is explicitly enabled
322 * through chreSensorConfigureBiasEvents() for the sensor.
323 *
324 * @since v1.3
325 */
326 #define CHRE_EVENT_SENSOR_ACCELEROMETER_BIAS_INFO \
327 (CHRE_EVENT_SENSOR_OTHER_EVENTS_BASE + 3)
328
329 /**
330 * nanoappHandleEvent argument: struct chreSensorFlushCompleteEvent
331 *
332 * An event indicating that a flush request made by chreSensorFlushAsync has
333 * completed.
334 *
335 * @see chreSensorFlushAsync
336 *
337 * @since v1.3
338 */
339 #define CHRE_EVENT_SENSOR_FLUSH_COMPLETE \
340 (CHRE_EVENT_SENSOR_OTHER_EVENTS_BASE + 4)
341
342 /**
343 * nanoappHandleEvent argument: struct chreSensorThreeAxisData
344 *
345 * The data of this event is the same as that of
346 * CHRE_EVENT_SENSOR_GYROSCOPE_BIAS_INFO, except the sensorHandle field of
347 * chreSensorDataHeader contains the handle of the sensor of type
348 * CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE.
349 *
350 * This event is only generated if the bias reporting is explicitly enabled
351 * for a nanoapp through chreSensorConfigureBiasEvents() for the sensor of type
352 * CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE.
353 *
354 * @see CHRE_EVENT_SENSOR_GYROSCOPE_BIAS_INFO
355 *
356 * @since v1.3
357 */
358 #define CHRE_EVENT_SENSOR_UNCALIBRATED_GYROSCOPE_BIAS_INFO \
359 (CHRE_EVENT_SENSOR_OTHER_EVENTS_BASE + 5)
360
361 /**
362 * nanoappHandleEvent argument: struct chreSensorThreeAxisData
363 *
364 * The data of this event is the same as that of
365 * CHRE_EVENT_SENSOR_GEOMAGNETIC_FIELD_BIAS_INFO, except the sensorHandle field
366 * of chreSensorDataHeader contains the handle of the sensor of type
367 * CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD.
368 *
369 * This event is only generated if the bias reporting is explicitly enabled
370 * for a nanoapp through chreSensorConfigureBiasEvents() for the sensor of type
371 * CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD.
372 *
373 * @see CHRE_EVENT_SENSOR_GEOMAGNETIC_FIELD_BIAS_INFO
374 *
375 * @since v1.3
376 */
377 #define CHRE_EVENT_SENSOR_UNCALIBRATED_GEOMAGNETIC_FIELD_BIAS_INFO \
378 (CHRE_EVENT_SENSOR_OTHER_EVENTS_BASE + 6)
379
380 /**
381 * nanoappHandleEvent argument: struct chreSensorThreeAxisData
382 *
383 * The data of this event is the same as that of
384 * CHRE_EVENT_SENSOR_ACCELEROMETER_BIAS_INFO, except the sensorHandle field
385 * of chreSensorDataHeader contains the handle of the sensor of type
386 * CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER.
387 *
388 * This event is only generated if the bias reporting is explicitly enabled
389 * for a nanoapp through chreSensorConfigureBiasEvents for the sensor of type
390 * CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER.
391 *
392 * @see CHRE_EVENT_SENSOR_ACCELEROMETER_BIAS_INFO
393 *
394 * @since v1.3
395 */
396 #define CHRE_EVENT_SENSOR_UNCALIBRATED_ACCELEROMETER_BIAS_INFO \
397 (CHRE_EVENT_SENSOR_OTHER_EVENTS_BASE + 7)
398
399 #if CHRE_EVENT_SENSOR_UNCALIBRATED_ACCELEROMETER_BIAS_INFO > \
400 CHRE_EVENT_SENSOR_LAST_EVENT
401 #error Too many sensor events.
402 #endif
403
404 /**
405 * Value indicating we want the smallest possible latency for a sensor.
406 *
407 * This literally translates to 0 nanoseconds for the chreSensorConfigure()
408 * argument. While we won't get exactly 0 nanoseconds, the CHRE will
409 * queue up this event As Soon As Possible.
410 */
411 #define CHRE_SENSOR_LATENCY_ASAP UINT64_C(0)
412
413 /**
414 * Special value indicating non-importance, or non-applicability of the sampling
415 * interval.
416 *
417 * @see chreSensorConfigure
418 * @see chreSensorSamplingStatus
419 */
420 #define CHRE_SENSOR_INTERVAL_DEFAULT UINT64_C(-1)
421
422 /**
423 * Special value indicating non-importance of the latency.
424 *
425 * @see chreSensorConfigure
426 * @see chreSensorSamplingStatus
427 */
428 #define CHRE_SENSOR_LATENCY_DEFAULT UINT64_C(-1)
429
430 /**
431 * A sensor index value indicating that it is the default sensor.
432 *
433 * @see chreSensorFind
434 */
435 #define CHRE_SENSOR_INDEX_DEFAULT UINT8_C(0)
436
437 /**
438 * Special value indicating non-importance of the batch interval.
439 *
440 * @see chreSensorConfigureWithBatchInterval
441 */
442 #define CHRE_SENSOR_BATCH_INTERVAL_DEFAULT UINT64_C(-1)
443
444 // This is used to define elements of enum chreSensorConfigureMode.
445 #define CHRE_SENSOR_CONFIGURE_RAW_POWER_ON (1 << 0)
446
447 // This is used to define elements of enum chreSensorConfigureMode.
448 #define CHRE_SENSOR_CONFIGURE_RAW_REPORT_CONTINUOUS (1 << 1)
449
450 // This is used to define elements of enum chreSensorConfigureMode.
451 #define CHRE_SENSOR_CONFIGURE_RAW_REPORT_ONE_SHOT (2 << 1)
452
453 /**
454 * The maximum amount of time allowed to elapse between the call to
455 * chreSensorFlushAsync() and when CHRE_EVENT_SENSOR_FLUSH_COMPLETE is delivered
456 * to the nanoapp on a successful flush.
457 */
458 #define CHRE_SENSOR_FLUSH_COMPLETE_TIMEOUT_NS (5 * CHRE_NSEC_PER_SEC)
459
460 /**
461 * Modes we can configure a sensor to use.
462 *
463 * Our mode will affect not only how/if we receive events, but
464 * also whether or not the sensor will be powered on our behalf.
465 *
466 * @see chreSensorConfigure
467 */
468 enum chreSensorConfigureMode {
469 /**
470 * Get events from the sensor.
471 *
472 * Power: Turn on if not already on.
473 * Reporting: Continuous. Send each new event as it comes (subject to
474 * batching and latency).
475 */
476 CHRE_SENSOR_CONFIGURE_MODE_CONTINUOUS =
477 (CHRE_SENSOR_CONFIGURE_RAW_POWER_ON |
478 CHRE_SENSOR_CONFIGURE_RAW_REPORT_CONTINUOUS),
479
480 /**
481 * Get a single event from the sensor and then become DONE.
482 *
483 * Once the event is sent, the sensor automatically
484 * changes to CHRE_SENSOR_CONFIGURE_MODE_DONE mode.
485 *
486 * Power: Turn on if not already on.
487 * Reporting: One shot. Send the next event and then be DONE.
488 */
489 CHRE_SENSOR_CONFIGURE_MODE_ONE_SHOT =
490 (CHRE_SENSOR_CONFIGURE_RAW_POWER_ON |
491 CHRE_SENSOR_CONFIGURE_RAW_REPORT_ONE_SHOT),
492
493 /**
494 * Get events from a sensor that are generated for any client in the system.
495 *
496 * This is considered passive because the sensor will not be powered on for
497 * the sake of our nanoapp. If and only if another client in the system has
498 * requested this sensor power on will we get events.
499 *
500 * This can be useful for something which is interested in seeing data, but
501 * not interested enough to be responsible for powering on the sensor.
502 *
503 * Power: Do not power the sensor on our behalf.
504 * Reporting: Continuous. Send each event as it comes.
505 */
506 CHRE_SENSOR_CONFIGURE_MODE_PASSIVE_CONTINUOUS =
507 CHRE_SENSOR_CONFIGURE_RAW_REPORT_CONTINUOUS,
508
509 /**
510 * Get a single event from a sensor that is generated for any client in the
511 * system.
512 *
513 * See CHRE_SENSOR_CONFIGURE_MODE_PASSIVE_CONTINUOUS for more details on
514 * what the "passive" means.
515 *
516 * Power: Do not power the sensor on our behalf.
517 * Reporting: One shot. Send only the next event and then be DONE.
518 */
519 CHRE_SENSOR_CONFIGURE_MODE_PASSIVE_ONE_SHOT =
520 CHRE_SENSOR_CONFIGURE_RAW_REPORT_ONE_SHOT,
521
522 /**
523 * Indicate we are done using this sensor and no longer interested in it.
524 *
525 * See chreSensorConfigure for more details on expressing interest or
526 * lack of interest in a sensor.
527 *
528 * Power: Do not power the sensor on our behalf.
529 * Reporting: None.
530 */
531 CHRE_SENSOR_CONFIGURE_MODE_DONE = 0,
532 };
533
534 /**
535 * A structure containing information about a Sensor.
536 *
537 * See documentation of individual fields below.
538 */
539 struct chreSensorInfo {
540 /**
541 * The name of the sensor.
542 *
543 * A text name, useful for logging/debugging, describing the Sensor. This
544 * is not assured to be unique (i.e. there could be multiple sensors with
545 * the name "Temperature").
546 *
547 * CHRE implementations may not set this as NULL. An empty
548 * string, while discouraged, is legal.
549 */
550 const char *sensorName;
551
552 /**
553 * One of the CHRE_SENSOR_TYPE_* defines above.
554 */
555 uint8_t sensorType;
556
557 /**
558 * Flag indicating if this sensor is on-change.
559 *
560 * An on-change sensor only generates events when underlying state
561 * changes. This has the same meaning as on-change does in the Android
562 * Sensors HAL. See sensors.h for much more details.
563 *
564 * A value of 1 indicates this is on-change. 0 indicates this is not
565 * on-change.
566 */
567 uint8_t isOnChange : 1;
568
569 /**
570 * Flag indicating if this sensor is one-shot.
571 *
572 * A one-shot sensor only triggers a single event, and then automatically
573 * disables itself.
574 *
575 * A value of 1 indicates this is one-shot. 0 indicates this is not
576 * on-change.
577 */
578 uint8_t isOneShot : 1;
579
580 /**
581 * Flag indicating if this sensor supports reporting bias info events.
582 *
583 * This field will be set to 0 when running on CHRE API versions prior to
584 * v1.3, but must be ignored (i.e. does not mean bias info event is not
585 * supported).
586 *
587 * @see chreSensorConfigureBiasEvents
588 *
589 * @since v1.3
590 */
591 uint8_t reportsBiasEvents : 1;
592
593 /**
594 * Flag indicating if this sensor supports passive mode requests.
595 *
596 * This field will be set to 0 when running on CHRE API versions prior to
597 * v1.4, and must be ignored (i.e. does not mean passive mode requests are
598 * not supported).
599 *
600 * @see chreSensorConfigure
601 *
602 * @since v1.4
603 */
604 uint8_t supportsPassiveMode : 1;
605
606 uint8_t unusedFlags : 4;
607
608 /**
609 * The minimum sampling interval supported by this sensor, in nanoseconds.
610 *
611 * Requests to chreSensorConfigure with a lower interval than this will
612 * fail. If the sampling interval is not applicable to this sensor, this
613 * will be set to CHRE_SENSOR_INTERVAL_DEFAULT.
614 *
615 * This field will be set to 0 when running on CHRE API versions prior to
616 * v1.1, indicating that the minimum interval is not known.
617 *
618 * @since v1.1
619 */
620 uint64_t minInterval;
621
622 /**
623 * Uniquely identifies the sensor for a given type. A value of 0 indicates
624 * that this is the "default" sensor, which is returned by
625 * chreSensorFindDefault().
626 *
627 * The sensor index of a given type must be stable across boots (i.e. must
628 * not change), and a different sensor of the same type must have different
629 * sensor index values, and the set of sensorIndex values for a given sensor
630 * type must be continuguous.
631 *
632 * @since v1.5
633 */
634 uint8_t sensorIndex;
635 };
636
637 /**
638 * The status of a sensor's sampling configuration.
639 */
640 struct chreSensorSamplingStatus {
641 /**
642 * The interval, in nanoseconds, at which the sensor is now sampling.
643 *
644 * If this is CHRE_SENSOR_INTERVAL_DEFAULT, then a sampling interval
645 * isn't meaningful for this sensor.
646 *
647 * Note that if 'enabled' is false, this value is not meaningful.
648 */
649 uint64_t interval;
650
651 /**
652 * The latency, in nanoseconds, at which the senor is now reporting.
653 *
654 * If this is CHRE_SENSOR_LATENCY_DEFAULT, then a latency
655 * isn't meaningful for this sensor.
656 *
657 * The effective batch interval can be derived from this value by
658 * adding the current sampling interval.
659 *
660 * Note that if 'enabled' is false, this value is not meaningful.
661 */
662 uint64_t latency;
663
664 /**
665 * True if the sensor is actively powered and sampling; false otherwise.
666 */
667 bool enabled;
668 };
669
670 /**
671 * The nanoappHandleEvent argument for CHRE_EVENT_SENSOR_SAMPLING_CHANGE.
672 *
673 * Note that only at least one of 'interval' or 'latency' must be
674 * different than it was prior to this event. Thus, one of these
675 * fields may be (but doesn't need to be) the same as before.
676 */
677 struct chreSensorSamplingStatusEvent {
678 /**
679 * The handle of the sensor which has experienced a change in sampling.
680 */
681 uint32_t sensorHandle;
682
683 /**
684 * The new sampling status.
685 *
686 * At least one of the field in this struct will be different from
687 * the previous sampling status event.
688 */
689 struct chreSensorSamplingStatus status;
690 };
691
692 /**
693 * The nanoappHandleEvent argument for CHRE_EVENT_SENSOR_FLUSH_COMPLETE.
694 *
695 * @see chreSensorFlushAsync
696 *
697 * @since v1.3
698 */
699 struct chreSensorFlushCompleteEvent {
700 /**
701 * The handle of the sensor which a flush was completed.
702 */
703 uint32_t sensorHandle;
704
705 /**
706 * Populated with a value from enum {@link #chreError}, indicating whether
707 * the flush failed, and if so, provides the cause of the failure.
708 */
709 uint8_t errorCode;
710
711 /**
712 * Reserved for future use. Set to 0.
713 */
714 uint8_t reserved[3];
715
716 /**
717 * Set to the cookie parameter given to chreSensorFlushAsync.
718 */
719 const void *cookie;
720 };
721
722 /**
723 * Find the default sensor for a given sensor type.
724 *
725 * @param sensorType One of the CHRE_SENSOR_TYPE_* constants.
726 * @param handle If a sensor is found, then the memory will be filled with
727 * the value for the sensor's handle. This argument must be non-NULL.
728 * @return true if a sensor was found, false otherwise.
729 */
730 bool chreSensorFindDefault(uint8_t sensorType, uint32_t *handle);
731
732 /**
733 * Finds a sensor of a given index and sensor type.
734 *
735 * For CHRE implementations that support multiple sensors of the same sensor
736 * type, this method can be used to get the non-default sensor(s). The default
737 * sensor, as defined in the chreSensorFindDefault(), will be returned if
738 * a sensor index of zero is specified.
739 *
740 * A simple example of iterating all available sensors of a given type is
741 * provided here:
742 *
743 * uint32_t handle;
744 * for (uint8_t i = 0; chreSensorFind(sensorType, i, &handle); i++) {
745 * chreLog(CHRE_LOG_INFO,
746 * "Found sensor index %" PRIu8 ", which has handle %" PRIu32,
747 * i, handle);
748 * }
749 *
750 * If this method is invoked for CHRE versions prior to v1.5, invocations with
751 * sensorIndex value of 0 will be equivalent to using chreSensorFindDefault, and
752 * if sensorIndex is non-zero will return false.
753 *
754 * In cases where multiple sensors are supported in both the Android sensors
755 * framework and CHRE, the sensorName of the chreSensorInfo struct for a given
756 * sensor instance must match exactly with that of the
757 * android.hardware.Sensor#getName() return value. This can be used to match a
758 * sensor instance between the Android and CHRE sensors APIs.
759 *
760 * @param sensorType One of the CHRE_SENSOR_TYPE_* constants.
761 * @param sensorIndex The index of the desired sensor.
762 * @param handle If a sensor is found, then the memory will be filled with
763 * the value for the sensor's handle. This argument must be non-NULL.
764 * @return true if a sensor was found, false otherwise.
765 *
766 * @since v1.5
767 */
768 bool chreSensorFind(uint8_t sensorType, uint8_t sensorIndex, uint32_t *handle);
769
770 /**
771 * Get the chreSensorInfo struct for a given sensor.
772 *
773 * @param sensorHandle The sensor handle, as obtained from
774 * chreSensorFindDefault() or passed to nanoappHandleEvent().
775 * @param info If the sensor is valid, then this memory will be filled with
776 * the SensorInfo contents for this sensor. This argument must be
777 * non-NULL.
778 * @return true if the senor handle is valid and 'info' was filled in;
779 * false otherwise.
780 */
781 bool chreGetSensorInfo(uint32_t sensorHandle, struct chreSensorInfo *info);
782
783 /**
784 * Get the chreSensorSamplingStatus struct for a given sensor.
785 *
786 * Note that this may be different from what was requested in
787 * chreSensorConfigure(), for multiple reasons. It's possible that the sensor
788 * does not exactly support the interval requested in chreSensorConfigure(), so
789 * a faster one was chosen.
790 *
791 * It's also possible that there is another user of this sensor who has
792 * requested a faster interval and/or lower latency. This latter scenario
793 * should be noted, because it means the sensor rate can change due to no
794 * interaction from this nanoapp. Note that the
795 * CHRE_EVENT_SENSOR_SAMPLING_CHANGE event will trigger in this case, so it's
796 * not necessary to poll for such a change.
797 *
798 * @param sensorHandle The sensor handle, as obtained from
799 * chreSensorFindDefault() or passed to nanoappHandleEvent().
800 * @param status If the sensor is valid, then this memory will be filled with
801 * the sampling status contents for this sensor. This argument must be
802 * non-NULL.
803 * @return true if the senor handle is valid and 'status' was filled in;
804 * false otherwise.
805 */
806 bool chreGetSensorSamplingStatus(uint32_t sensorHandle,
807 struct chreSensorSamplingStatus *status);
808
809 /**
810 * Configures a given sensor at a specific interval and latency and mode.
811 *
812 * If this sensor's chreSensorInfo has isOneShot set to 1,
813 * then the mode must be one of the ONE_SHOT modes, or this method will fail.
814 *
815 * The CHRE wants to power as few sensors as possible, in keeping with its
816 * low power design. As such, it only turns on sensors when there are clients
817 * actively interested in that sensor data, and turns off sensors as soon as
818 * there are no clients interested in them. Calling this method generally
819 * indicates an interest, and using CHRE_SENSOR_CONFIGURE_MODE_DONE shows
820 * when we are no longer interested.
821 *
822 * Thus, each initial Configure of a sensor (per nanoapp) needs to eventually
823 * have a DONE call made, either directly or on its behalf. Subsequent calls
824 * to a Configure method within the same nanoapp, when there has been no DONE
825 * in between, still only require a single DONE call.
826 *
827 * For example, the following is valid usage:
828 * <code>
829 * chreSensorConfigure(myHandle, mode, interval0, latency0);
830 * [...]
831 * chreSensorConfigure(myHandle, mode, interval1, latency0);
832 * [...]
833 * chreSensorConfigure(myHandle, mode, interval1, latency1);
834 * [...]
835 * chreSensorConfigureModeOnly(myHandle, CHRE_SENSOR_CONFIGURE_MODE_DONE);
836 * </code>
837 *
838 * The first call to Configure is the one which creates the requirement
839 * to eventually call with DONE. The subsequent calls are just changing the
840 * interval/latency. They have not changed the fact that this nanoapp is
841 * still interested in output from the sensor 'myHandle'. Thus, only one
842 * single call for DONE is needed.
843 *
844 * There is a special case. One-shot sensors, sensors which
845 * just trigger a single event and never trigger again, implicitly go into
846 * DONE mode after that single event triggers. Thus, the
847 * following are legitimate usages:
848 * <code>
849 * chreSensorConfigure(myHandle, MODE_ONE_SHOT, interval, latency);
850 * [...]
851 * [myHandle triggers an event]
852 * [no need to configure to DONE].
853 * </code>
854 *
855 * And:
856 * <code>
857 * chreSensorConfigure(myHandle, MODE_ONE_SHOT, interval, latency);
858 * [...]
859 * chreSensorConfigureModeOnly(myHandle, MODE_DONE);
860 * [we cancelled myHandle before it ever triggered an event]
861 * </code>
862 *
863 * Note that while PASSIVE modes, by definition, don't express an interest in
864 * powering the sensor, DONE is still necessary to silence the event reporting.
865 * Starting with CHRE API v1.4, for sensors that do not support passive mode, a
866 * request with mode set to CHRE_SENSOR_CONFIGURE_MODE_PASSIVE_CONTINUOUS or
867 * CHRE_SENSOR_CONFIGURE_MODE_PASSIVE_ONE_SHOT will be rejected. CHRE API
868 * versions 1.3 and older implicitly assume that passive mode is supported
869 * across all sensors, however this is not necessarily the case. Clients can
870 * call chreSensorInfo to identify whether a sensor supports passive mode.
871 *
872 * When a calibrated sensor (e.g. CHRE_SENSOR_TYPE_ACCELEROMETER) is
873 * successfully enabled through this method and if bias delivery is supported,
874 * by default CHRE will start delivering bias events for the sensor
875 * (e.g. CHRE_EVENT_SENSOR_ACCELEROMETER_BIAS_INFO) to the nanoapp. If the
876 * nanoapp does not wish to receive these events, they can be disabled through
877 * chreSensorConfigureBiasEvents after enabling the sensor.
878 *
879 * @param sensorHandle The handle to the sensor, as obtained from
880 * chreSensorFindDefault().
881 * @param mode The mode to use. See descriptions within the
882 * chreSensorConfigureMode enum.
883 * @param interval The interval, in nanoseconds, at which we want events from
884 * the sensor. On success, the sensor will be set to 'interval', or a value
885 * less than 'interval'. There is a special value
886 * CHRE_SENSOR_INTERVAL_DEFAULT, in which we don't express a preference for
887 * the interval, and allow the sensor to choose what it wants. Note that
888 * due to batching, we may receive events less frequently than
889 * 'interval'.
890 * @param latency The maximum latency, in nanoseconds, allowed before the
891 * CHRE begins delivery of an event. This will control how many events
892 * can be queued by the sensor before requiring a delivery event.
893 * Latency is defined as the "timestamp when event is queued by the CHRE"
894 * minus "timestamp of oldest unsent data reading".
895 * There is a special value CHRE_SENSOR_LATENCY_DEFAULT, in which we don't
896 * express a preference for the latency, and allow the sensor to choose what
897 * it wants.
898 * Note that there is no assurance of how long it will take an event to
899 * get through a CHRE's queueing system, and thus there is no ability to
900 * request a minimum time from the occurrence of a phenomenon to when the
901 * nanoapp receives the information. The current CHRE API has no
902 * real-time elements, although future versions may introduce some to
903 * help with this issue.
904 * @return true if the configuration succeeded, false otherwise.
905 *
906 * @see chreSensorConfigureMode
907 * @see chreSensorFindDefault
908 * @see chreSensorInfo
909 * @see chreSensorConfigureBiasEvents
910 */
911 bool chreSensorConfigure(uint32_t sensorHandle,
912 enum chreSensorConfigureMode mode,
913 uint64_t interval, uint64_t latency);
914
915 /**
916 * Short cut for chreSensorConfigure where we only want to configure the mode
917 * and do not care about interval/latency.
918 *
919 * @see chreSensorConfigure
920 */
chreSensorConfigureModeOnly(uint32_t sensorHandle,enum chreSensorConfigureMode mode)921 static inline bool chreSensorConfigureModeOnly(
922 uint32_t sensorHandle, enum chreSensorConfigureMode mode) {
923 return chreSensorConfigure(sensorHandle,
924 mode,
925 CHRE_SENSOR_INTERVAL_DEFAULT,
926 CHRE_SENSOR_LATENCY_DEFAULT);
927 }
928
929 /**
930 * Convenience function that wraps chreSensorConfigure but enables batching to
931 * be controlled by specifying the desired maximum batch interval rather
932 * than maximum sample latency. Users may find the batch interval to be a more
933 * intuitive method of expressing the desired batching behavior.
934 *
935 * Batch interval is different from latency as the batch interval time is
936 * counted starting when the prior event containing a batch of sensor samples is
937 * delivered, while latency starts counting when the first sample is deferred to
938 * start collecting a batch. In other words, latency ignores the time between
939 * the last sample in a batch to the first sample of the next batch, while it's
940 * included in the batch interval, as illustrated below.
941 *
942 * Time 0 1 2 3 4 5 6 7 8
943 * Batch A B C
944 * Sample a1 a2 a3 b1 b2 b3 c1 c2 c3
945 * Latency [ ] [ ] [ ]
946 * BatchInt | | |
947 *
948 * In the diagram, the effective sample interval is 1 time unit, latency is 2
949 * time units, and batch interval is 3 time units.
950 *
951 * @param sensorHandle See chreSensorConfigure#sensorHandle
952 * @param mode See chreSensorConfigure#mode
953 * @param sampleInterval See chreSensorConfigure#interval, but note that
954 * CHRE_SENSOR_INTERVAL_DEFAULT is not a supported input to this method.
955 * @param batchInterval The desired maximum interval, in nanoseconds, between
956 * CHRE enqueuing each batch of sensor samples.
957 * @return Same as chreSensorConfigure
958 *
959 * @see chreSensorConfigure
960 *
961 * @since v1.1
962 */
chreSensorConfigureWithBatchInterval(uint32_t sensorHandle,enum chreSensorConfigureMode mode,uint64_t sampleInterval,uint64_t batchInterval)963 static inline bool chreSensorConfigureWithBatchInterval(
964 uint32_t sensorHandle, enum chreSensorConfigureMode mode,
965 uint64_t sampleInterval, uint64_t batchInterval) {
966 bool result = false;
967
968 if (sampleInterval != CHRE_SENSOR_INTERVAL_DEFAULT) {
969 uint64_t latency;
970 if (batchInterval == CHRE_SENSOR_BATCH_INTERVAL_DEFAULT) {
971 latency = CHRE_SENSOR_LATENCY_DEFAULT;
972 } else if (batchInterval > sampleInterval) {
973 latency = batchInterval - sampleInterval;
974 } else {
975 latency = CHRE_SENSOR_LATENCY_ASAP;
976 }
977 result = chreSensorConfigure(sensorHandle, mode, sampleInterval,
978 latency);
979 }
980
981 return result;
982 }
983
984 /**
985 * Configures the reception of bias events for a specific sensor.
986 *
987 * If bias event delivery is supported for a sensor, the sensor's chreSensorInfo
988 * has reportsBiasEvents set to 1. If supported, it must be supported for both
989 * calibrated and uncalibrated versions of the sensor. If supported, CHRE must
990 * provide bias events to the nanoapp by default when chreSensorConfigure is
991 * called to enable the calibrated version of the sensor (for backwards
992 * compatibility reasons, as this is the defined behavior for CHRE API v1.0).
993 * When configuring uncalibrated sensors, nanoapps must explicitly configure an
994 * enable request through this method to receive bias events. If bias event
995 * delivery is not supported for the sensor, this method will return false and
996 * no bias events will be generated.
997 *
998 * To enable bias event delivery (enable=true), the nanoapp must be registered
999 * to the sensor through chreSensorConfigure, and bias events will only be
1000 * generated when the sensor is powered on. To disable the bias event delivery,
1001 * this method can be invoked with enable=false.
1002 *
1003 * If an enable configuration is successful, the calling nanoapp will receive
1004 * bias info events, e.g. CHRE_EVENT_SENSOR_ACCELEROMETER_BIAS_INFO, when the
1005 * bias status changes (or first becomes available). Calibrated data
1006 * (e.g. CHRE_SENSOR_TYPE_ACCELEROMETER) is generated by subracting bias from
1007 * uncalibrated data (e.g. CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER).
1008 * Calibrated sensor events are generated by applying the most recent bias
1009 * available (i.e. timestamp of calibrated data are greater than or equal to the
1010 * timestamp of the bias data that has been applied to it). The configuration of
1011 * bias event delivery persists until the sensor is unregistered by the nanoapp
1012 * through chreSensorConfigure or modified through this method.
1013 *
1014 * To get an initial bias before new bias events, the nanoapp should get the
1015 * bias synchronously after this method is invoked, e.g.:
1016 *
1017 * if (chreSensorConfigure(handle, ...)) {
1018 * chreSensorConfigureBiasEvents(handle, true);
1019 * chreSensorGetThreeAxisBias(handle, &bias);
1020 * }
1021 *
1022 * Note that chreSensorGetThreeAxisBias() should be called after
1023 * chreSensorConfigureBiasEvents() to ensure that no bias events are lost.
1024 *
1025 * If called while running on a CHRE API version below v1.3, this function
1026 * returns false and has no effect. The default behavior regarding bias events
1027 * is unchanged, meaning that the implementation may still send bias events
1028 * when a calibrated sensor is registered (if supported), and will not send bias
1029 * events when an uncalibrated sensor is registered.
1030 *
1031 * @param sensorHandle The handle to the sensor, as obtained from
1032 * chreSensorFindDefault().
1033 * @param enable true to receive bias events, false otherwise
1034 *
1035 * @return true if the configuration succeeded, false otherwise
1036 *
1037 * @since v1.3
1038 */
1039 bool chreSensorConfigureBiasEvents(uint32_t sensorHandle, bool enable);
1040
1041 /**
1042 * Synchronously provides the most recent bias info available for a sensor. The
1043 * bias will only be provided for a sensor that supports bias event delivery
1044 * using the chreSensorThreeAxisData type. If the bias is not yet available
1045 * (but is supported), this method will store data with a bias of 0 and the
1046 * accuracy field in chreSensorDataHeader set to CHRE_SENSOR_ACCURACY_UNKNOWN.
1047 *
1048 * If called while running on a CHRE API version below v1.3, this function
1049 * returns false.
1050 *
1051 * @param sensorHandle The handle to the sensor, as obtained from
1052 * chreSensorFindDefault().
1053 * @param bias A pointer to where the bias will be stored.
1054 *
1055 * @return true if the bias was successfully stored, false if sensorHandle was
1056 * invalid or the sensor does not support three axis bias delivery
1057 *
1058 * @since v1.3
1059 *
1060 * @see chreSensorConfigureBiasEvents
1061 */
1062 bool chreSensorGetThreeAxisBias(uint32_t sensorHandle,
1063 struct chreSensorThreeAxisData *bias);
1064
1065 /**
1066 * Makes a request to flush all samples stored for batching. The nanoapp must be
1067 * registered to the sensor through chreSensorConfigure, and the sensor must be
1068 * powered on. If the request is accepted, all batched samples of the sensor
1069 * are sent to nanoapps registered to the sensor. During a flush, it is treated
1070 * as though the latency as given in chreSensorConfigure has expired. When all
1071 * batched samples have been flushed (or the flush fails), the nanoapp will
1072 * receive a unicast CHRE_EVENT_SENSOR_FLUSH_COMPLETE event. The time to deliver
1073 * this event must not exceed CHRE_SENSOR_FLUSH_COMPLETE_TIMEOUT_NS after this
1074 * method is invoked. If there are no samples in the batch buffer (either in
1075 * hardware FIFO or software), then this method will return true and a
1076 * CHRE_EVENT_SENSOR_FLUSH_COMPLETE event is delivered immediately.
1077 *
1078 * If a flush request is invalid (e.g. the sensor refers to a one-shot sensor,
1079 * or the sensor was not enabled), and this API will return false and no
1080 * CHRE_EVENT_SENSOR_FLUSH_COMPLETE event will be delivered.
1081 *
1082 * If multiple flush requests are made for a sensor prior to flush completion,
1083 * then the requesting nanoapp will receive all batched samples existing at the
1084 * time of the latest flush request. In this case, the number of
1085 * CHRE_EVENT_SENSOR_FLUSH_COMPLETE events received must equal the number of
1086 * flush requests made.
1087 *
1088 * If a sensor request is disabled after a flush request is made through this
1089 * method but before the flush operation is completed, the nanoapp will receive
1090 * a CHRE_EVENT_SENSOR_FLUSH_COMPLETE with the error code
1091 * CHRE_ERROR_FUNCTION_DISABLED for any pending flush requests.
1092 *
1093 * Starting with CHRE API v1.3, implementations must support this capability
1094 * across all exposed sensor types.
1095 *
1096 * @param sensorHandle The handle to the sensor, as obtained from
1097 * chreSensorFindDefault().
1098 * @param cookie An opaque value that will be included in the
1099 * chreSensorFlushCompleteEvent sent in relation to this request.
1100 *
1101 * @return true if the request was accepted for processing, false otherwise
1102 *
1103 * @since v1.3
1104 */
1105 bool chreSensorFlushAsync(uint32_t sensorHandle, const void *cookie);
1106
1107 #ifdef __cplusplus
1108 }
1109 #endif
1110
1111 #endif /* _CHRE_SENSOR_H_ */
1112