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