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 * @param sensorType One of the CHRE_SENSOR_TYPE_* constants.
755 * @param sensorIndex The index of the desired sensor.
756 * @param handle If a sensor is found, then the memory will be filled with
757 * the value for the sensor's handle. This argument must be non-NULL.
758 * @return true if a sensor was found, false otherwise.
759 *
760 * @since v1.5
761 */
762 bool chreSensorFind(uint8_t sensorType, uint8_t sensorIndex, uint32_t *handle);
763
764 /**
765 * Get the chreSensorInfo struct for a given sensor.
766 *
767 * @param sensorHandle The sensor handle, as obtained from
768 * chreSensorFindDefault() or passed to nanoappHandleEvent().
769 * @param info If the sensor is valid, then this memory will be filled with
770 * the SensorInfo contents for this sensor. This argument must be
771 * non-NULL.
772 * @return true if the senor handle is valid and 'info' was filled in;
773 * false otherwise.
774 */
775 bool chreGetSensorInfo(uint32_t sensorHandle, struct chreSensorInfo *info);
776
777 /**
778 * Get the chreSensorSamplingStatus struct for a given sensor.
779 *
780 * Note that this may be different from what was requested in
781 * chreSensorConfigure(), for multiple reasons. It's possible that the sensor
782 * does not exactly support the interval requested in chreSensorConfigure(), so
783 * a faster one was chosen.
784 *
785 * It's also possible that there is another user of this sensor who has
786 * requested a faster interval and/or lower latency. This latter scenario
787 * should be noted, because it means the sensor rate can change due to no
788 * interaction from this nanoapp. Note that the
789 * CHRE_EVENT_SENSOR_SAMPLING_CHANGE event will trigger in this case, so it's
790 * not necessary to poll for such a change.
791 *
792 * @param sensorHandle The sensor handle, as obtained from
793 * chreSensorFindDefault() or passed to nanoappHandleEvent().
794 * @param status If the sensor is valid, then this memory will be filled with
795 * the sampling status contents for this sensor. This argument must be
796 * non-NULL.
797 * @return true if the senor handle is valid and 'status' was filled in;
798 * false otherwise.
799 */
800 bool chreGetSensorSamplingStatus(uint32_t sensorHandle,
801 struct chreSensorSamplingStatus *status);
802
803 /**
804 * Configures a given sensor at a specific interval and latency and mode.
805 *
806 * If this sensor's chreSensorInfo has isOneShot set to 1,
807 * then the mode must be one of the ONE_SHOT modes, or this method will fail.
808 *
809 * The CHRE wants to power as few sensors as possible, in keeping with its
810 * low power design. As such, it only turns on sensors when there are clients
811 * actively interested in that sensor data, and turns off sensors as soon as
812 * there are no clients interested in them. Calling this method generally
813 * indicates an interest, and using CHRE_SENSOR_CONFIGURE_MODE_DONE shows
814 * when we are no longer interested.
815 *
816 * Thus, each initial Configure of a sensor (per nanoapp) needs to eventually
817 * have a DONE call made, either directly or on its behalf. Subsequent calls
818 * to a Configure method within the same nanoapp, when there has been no DONE
819 * in between, still only require a single DONE call.
820 *
821 * For example, the following is valid usage:
822 * <code>
823 * chreSensorConfigure(myHandle, mode, interval0, latency0);
824 * [...]
825 * chreSensorConfigure(myHandle, mode, interval1, latency0);
826 * [...]
827 * chreSensorConfigure(myHandle, mode, interval1, latency1);
828 * [...]
829 * chreSensorConfigureModeOnly(myHandle, CHRE_SENSOR_CONFIGURE_MODE_DONE);
830 * </code>
831 *
832 * The first call to Configure is the one which creates the requirement
833 * to eventually call with DONE. The subsequent calls are just changing the
834 * interval/latency. They have not changed the fact that this nanoapp is
835 * still interested in output from the sensor 'myHandle'. Thus, only one
836 * single call for DONE is needed.
837 *
838 * There is a special case. One-shot sensors, sensors which
839 * just trigger a single event and never trigger again, implicitly go into
840 * DONE mode after that single event triggers. Thus, the
841 * following are legitimate usages:
842 * <code>
843 * chreSensorConfigure(myHandle, MODE_ONE_SHOT, interval, latency);
844 * [...]
845 * [myHandle triggers an event]
846 * [no need to configure to DONE].
847 * </code>
848 *
849 * And:
850 * <code>
851 * chreSensorConfigure(myHandle, MODE_ONE_SHOT, interval, latency);
852 * [...]
853 * chreSensorConfigureModeOnly(myHandle, MODE_DONE);
854 * [we cancelled myHandle before it ever triggered an event]
855 * </code>
856 *
857 * Note that while PASSIVE modes, by definition, don't express an interest in
858 * powering the sensor, DONE is still necessary to silence the event reporting.
859 * Starting with CHRE API v1.4, for sensors that do not support passive mode, a
860 * request with mode set to CHRE_SENSOR_CONFIGURE_MODE_PASSIVE_CONTINUOUS or
861 * CHRE_SENSOR_CONFIGURE_MODE_PASSIVE_ONE_SHOT will be rejected. CHRE API
862 * versions 1.3 and older implicitly assume that passive mode is supported
863 * across all sensors, however this is not necessarily the case. Clients can
864 * call chreSensorInfo to identify whether a sensor supports passive mode.
865 *
866 * When a calibrated sensor (e.g. CHRE_SENSOR_TYPE_ACCELEROMETER) is
867 * successfully enabled through this method and if bias delivery is supported,
868 * by default CHRE will start delivering bias events for the sensor
869 * (e.g. CHRE_EVENT_SENSOR_ACCELEROMETER_BIAS_INFO) to the nanoapp. If the
870 * nanoapp does not wish to receive these events, they can be disabled through
871 * chreSensorConfigureBiasEvents after enabling the sensor.
872 *
873 * @param sensorHandle The handle to the sensor, as obtained from
874 * chreSensorFindDefault().
875 * @param mode The mode to use. See descriptions within the
876 * chreSensorConfigureMode enum.
877 * @param interval The interval, in nanoseconds, at which we want events from
878 * the sensor. On success, the sensor will be set to 'interval', or a value
879 * less than 'interval'. There is a special value
880 * CHRE_SENSOR_INTERVAL_DEFAULT, in which we don't express a preference for
881 * the interval, and allow the sensor to choose what it wants. Note that
882 * due to batching, we may receive events less frequently than
883 * 'interval'.
884 * @param latency The maximum latency, in nanoseconds, allowed before the
885 * CHRE begins delivery of an event. This will control how many events
886 * can be queued by the sensor before requiring a delivery event.
887 * Latency is defined as the "timestamp when event is queued by the CHRE"
888 * minus "timestamp of oldest unsent data reading".
889 * There is a special value CHRE_SENSOR_LATENCY_DEFAULT, in which we don't
890 * express a preference for the latency, and allow the sensor to choose what
891 * it wants.
892 * Note that there is no assurance of how long it will take an event to
893 * get through a CHRE's queueing system, and thus there is no ability to
894 * request a minimum time from the occurrence of a phenomenon to when the
895 * nanoapp receives the information. The current CHRE API has no
896 * real-time elements, although future versions may introduce some to
897 * help with this issue.
898 * @return true if the configuration succeeded, false otherwise.
899 *
900 * @see chreSensorConfigureMode
901 * @see chreSensorFindDefault
902 * @see chreSensorInfo
903 * @see chreSensorConfigureBiasEvents
904 */
905 bool chreSensorConfigure(uint32_t sensorHandle,
906 enum chreSensorConfigureMode mode,
907 uint64_t interval, uint64_t latency);
908
909 /**
910 * Short cut for chreSensorConfigure where we only want to configure the mode
911 * and do not care about interval/latency.
912 *
913 * @see chreSensorConfigure
914 */
chreSensorConfigureModeOnly(uint32_t sensorHandle,enum chreSensorConfigureMode mode)915 static inline bool chreSensorConfigureModeOnly(
916 uint32_t sensorHandle, enum chreSensorConfigureMode mode) {
917 return chreSensorConfigure(sensorHandle,
918 mode,
919 CHRE_SENSOR_INTERVAL_DEFAULT,
920 CHRE_SENSOR_LATENCY_DEFAULT);
921 }
922
923 /**
924 * Convenience function that wraps chreSensorConfigure but enables batching to
925 * be controlled by specifying the desired maximum batch interval rather
926 * than maximum sample latency. Users may find the batch interval to be a more
927 * intuitive method of expressing the desired batching behavior.
928 *
929 * Batch interval is different from latency as the batch interval time is
930 * counted starting when the prior event containing a batch of sensor samples is
931 * delivered, while latency starts counting when the first sample is deferred to
932 * start collecting a batch. In other words, latency ignores the time between
933 * the last sample in a batch to the first sample of the next batch, while it's
934 * included in the batch interval, as illustrated below.
935 *
936 * Time 0 1 2 3 4 5 6 7 8
937 * Batch A B C
938 * Sample a1 a2 a3 b1 b2 b3 c1 c2 c3
939 * Latency [ ] [ ] [ ]
940 * BatchInt | | |
941 *
942 * In the diagram, the effective sample interval is 1 time unit, latency is 2
943 * time units, and batch interval is 3 time units.
944 *
945 * @param sensorHandle See chreSensorConfigure#sensorHandle
946 * @param mode See chreSensorConfigure#mode
947 * @param sampleInterval See chreSensorConfigure#interval, but note that
948 * CHRE_SENSOR_INTERVAL_DEFAULT is not a supported input to this method.
949 * @param batchInterval The desired maximum interval, in nanoseconds, between
950 * CHRE enqueuing each batch of sensor samples.
951 * @return Same as chreSensorConfigure
952 *
953 * @see chreSensorConfigure
954 *
955 * @since v1.1
956 */
chreSensorConfigureWithBatchInterval(uint32_t sensorHandle,enum chreSensorConfigureMode mode,uint64_t sampleInterval,uint64_t batchInterval)957 static inline bool chreSensorConfigureWithBatchInterval(
958 uint32_t sensorHandle, enum chreSensorConfigureMode mode,
959 uint64_t sampleInterval, uint64_t batchInterval) {
960 bool result = false;
961
962 if (sampleInterval != CHRE_SENSOR_INTERVAL_DEFAULT) {
963 uint64_t latency;
964 if (batchInterval == CHRE_SENSOR_BATCH_INTERVAL_DEFAULT) {
965 latency = CHRE_SENSOR_LATENCY_DEFAULT;
966 } else if (batchInterval > sampleInterval) {
967 latency = batchInterval - sampleInterval;
968 } else {
969 latency = CHRE_SENSOR_LATENCY_ASAP;
970 }
971 result = chreSensorConfigure(sensorHandle, mode, sampleInterval,
972 latency);
973 }
974
975 return result;
976 }
977
978 /**
979 * Configures the reception of bias events for a specific sensor.
980 *
981 * If bias event delivery is supported for a sensor, the sensor's chreSensorInfo
982 * has reportsBiasEvents set to 1. If supported, it must be supported for both
983 * calibrated and uncalibrated versions of the sensor. If supported, CHRE must
984 * provide bias events to the nanoapp by default when chreSensorConfigure is
985 * called to enable the calibrated version of the sensor (for backwards
986 * compatibility reasons, as this is the defined behavior for CHRE API v1.0).
987 * When configuring uncalibrated sensors, nanoapps must explicitly configure an
988 * enable request through this method to receive bias events. If bias event
989 * delivery is not supported for the sensor, this method will return false and
990 * no bias events will be generated.
991 *
992 * To enable bias event delivery (enable=true), the nanoapp must be registered
993 * to the sensor through chreSensorConfigure, and bias events will only be
994 * generated when the sensor is powered on. To disable the bias event delivery,
995 * this method can be invoked with enable=false.
996 *
997 * If an enable configuration is successful, the calling nanoapp will receive
998 * bias info events, e.g. CHRE_EVENT_SENSOR_ACCELEROMETER_BIAS_INFO, when the
999 * bias status changes (or first becomes available). Calibrated data
1000 * (e.g. CHRE_SENSOR_TYPE_ACCELEROMETER) is generated by subracting bias from
1001 * uncalibrated data (e.g. CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER).
1002 * Calibrated sensor events are generated by applying the most recent bias
1003 * available (i.e. timestamp of calibrated data are greater than or equal to the
1004 * timestamp of the bias data that has been applied to it). The configuration of
1005 * bias event delivery persists until the sensor is unregistered by the nanoapp
1006 * through chreSensorConfigure or modified through this method.
1007 *
1008 * To get an initial bias before new bias events, the nanoapp should get the
1009 * bias synchronously after this method is invoked, e.g.:
1010 *
1011 * if (chreSensorConfigure(handle, ...)) {
1012 * chreSensorConfigureBiasEvents(handle, true);
1013 * chreSensorGetThreeAxisBias(handle, &bias);
1014 * }
1015 *
1016 * Note that chreSensorGetThreeAxisBias() should be called after
1017 * chreSensorConfigureBiasEvents() to ensure that no bias events are lost.
1018 *
1019 * If called while running on a CHRE API version below v1.3, this function
1020 * returns false and has no effect. The default behavior regarding bias events
1021 * is unchanged, meaning that the implementation may still send bias events
1022 * when a calibrated sensor is registered (if supported), and will not send bias
1023 * events when an uncalibrated sensor is registered.
1024 *
1025 * @param sensorHandle The handle to the sensor, as obtained from
1026 * chreSensorFindDefault().
1027 * @param enable true to receive bias events, false otherwise
1028 *
1029 * @return true if the configuration succeeded, false otherwise
1030 *
1031 * @since v1.3
1032 */
1033 bool chreSensorConfigureBiasEvents(uint32_t sensorHandle, bool enable);
1034
1035 /**
1036 * Synchronously provides the most recent bias info available for a sensor. The
1037 * bias will only be provided for a sensor that supports bias event delivery
1038 * using the chreSensorThreeAxisData type. If the bias is not yet available
1039 * (but is supported), this method will store data with a bias of 0 and the
1040 * accuracy field in chreSensorDataHeader set to CHRE_SENSOR_ACCURACY_UNKNOWN.
1041 *
1042 * If called while running on a CHRE API version below v1.3, this function
1043 * returns false.
1044 *
1045 * @param sensorHandle The handle to the sensor, as obtained from
1046 * chreSensorFindDefault().
1047 * @param bias A pointer to where the bias will be stored.
1048 *
1049 * @return true if the bias was successfully stored, false if sensorHandle was
1050 * invalid or the sensor does not support three axis bias delivery
1051 *
1052 * @since v1.3
1053 *
1054 * @see chreSensorConfigureBiasEvents
1055 */
1056 bool chreSensorGetThreeAxisBias(uint32_t sensorHandle,
1057 struct chreSensorThreeAxisData *bias);
1058
1059 /**
1060 * Makes a request to flush all samples stored for batching. The nanoapp must be
1061 * registered to the sensor through chreSensorConfigure, and the sensor must be
1062 * powered on. If the request is accepted, all batched samples of the sensor
1063 * are sent to nanoapps registered to the sensor. During a flush, it is treated
1064 * as though the latency as given in chreSensorConfigure has expired. When all
1065 * batched samples have been flushed (or the flush fails), the nanoapp will
1066 * receive a unicast CHRE_EVENT_SENSOR_FLUSH_COMPLETE event. The time to deliver
1067 * this event must not exceed CHRE_SENSOR_FLUSH_COMPLETE_TIMEOUT_NS after this
1068 * method is invoked. If there are no samples in the batch buffer (either in
1069 * hardware FIFO or software), then this method will return true and a
1070 * CHRE_EVENT_SENSOR_FLUSH_COMPLETE event is delivered immediately.
1071 *
1072 * If a flush request is invalid (e.g. the sensor refers to a one-shot sensor,
1073 * or the sensor was not enabled), and this API will return false and no
1074 * CHRE_EVENT_SENSOR_FLUSH_COMPLETE event will be delivered.
1075 *
1076 * If multiple flush requests are made for a sensor prior to flush completion,
1077 * then the requesting nanoapp will receive all batched samples existing at the
1078 * time of the latest flush request. In this case, the number of
1079 * CHRE_EVENT_SENSOR_FLUSH_COMPLETE events received must equal the number of
1080 * flush requests made.
1081 *
1082 * If a sensor request is disabled after a flush request is made through this
1083 * method but before the flush operation is completed, the nanoapp will receive
1084 * a CHRE_EVENT_SENSOR_FLUSH_COMPLETE with the error code
1085 * CHRE_ERROR_FUNCTION_DISABLED for any pending flush requests.
1086 *
1087 * Starting with CHRE API v1.3, implementations must support this capability
1088 * across all exposed sensor types.
1089 *
1090 * @param sensorHandle The handle to the sensor, as obtained from
1091 * chreSensorFindDefault().
1092 * @param cookie An opaque value that will be included in the
1093 * chreSensorFlushCompleteEvent sent in relation to this request.
1094 *
1095 * @return true if the request was accepted for processing, false otherwise
1096 *
1097 * @since v1.3
1098 */
1099 bool chreSensorFlushAsync(uint32_t sensorHandle, const void *cookie);
1100
1101 #ifdef __cplusplus
1102 }
1103 #endif
1104
1105 #endif /* _CHRE_SENSOR_H_ */
1106