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_CORE_SENSOR_TYPE_H_
18 #define CHRE_CORE_SENSOR_TYPE_H_
19 
20 #include <cstddef>
21 #include <cstdint>
22 
23 #include "chre_api/chre/sensor.h"
24 
25 /**
26  * @file
27  * This file contains miscellaneous types useful in the core framework and
28  * platform implementations for dealing with sensors.
29  *
30  * Additionally, it contains methods useful for converting between these types
31  * and types used by the CHRE APIs and other miscellaneous helper methods that
32  * help make the rest of the codebase more readable.
33  */
34 namespace chre {
35 
36 //! Indicates the reporting mode of the sensor
37 enum class ReportingMode : uint8_t {
38   OnChange,
39   OneShot,
40   Continuous,
41 };
42 
43 //! The union of possible CHRE sensor data event type with one sample.
44 union ChreSensorData {
45   struct chreSensorDataHeader header;
46   struct chreSensorThreeAxisData threeAxisData;
47   struct chreSensorOccurrenceData occurrenceData;
48   struct chreSensorFloatData floatData;
49   struct chreSensorByteData byteData;
50   struct chreSensorUint64Data uint64Data;
51 };
52 
53 // Validate that aliasing into the header is valid for all types of the union
54 static_assert(offsetof(ChreSensorData, threeAxisData.header) == 0,
55               "Three axis data header not at offset 0");
56 static_assert(offsetof(ChreSensorData, occurrenceData.header) == 0,
57               "Occurrence data header not at offset 0");
58 static_assert(offsetof(ChreSensorData, floatData.header) == 0,
59               "Float data header not at offset 0");
60 static_assert(offsetof(ChreSensorData, byteData.header) == 0,
61               "Byte data header not at offset 0");
62 static_assert(offsetof(ChreSensorData, uint64Data.header) == 0,
63               "Uint64 data header not at offset 0");
64 
65 /**
66  * Returns a sensor sample event type for a given sensor type. The sensor type
67  * must not be SensorType::Unknown. This is a fatal error.
68  *
69  * @param sensorType The type of the sensor.
70  * @return The event type for a sensor sample of the given sensor type.
71  */
getSampleEventTypeForSensorType(uint8_t sensorType)72 constexpr uint16_t getSampleEventTypeForSensorType(uint8_t sensorType) {
73   return CHRE_EVENT_SENSOR_DATA_EVENT_BASE + sensorType;
74 }
75 
76 /**
77  * Returns a sensor type for a given sensor sample event type.
78  *
79  * @param eventType The event type for a sensor sample.
80  * @return The type of the sensor.
81  */
getSensorTypeForSampleEventType(uint16_t eventType)82 constexpr uint8_t getSensorTypeForSampleEventType(uint16_t eventType) {
83   return static_cast<uint8_t>(eventType - CHRE_EVENT_SENSOR_DATA_EVENT_BASE);
84 }
85 
86 /**
87  * This SensorMode is designed to wrap constants provided by the CHRE API to
88  * imrpove type-safety. The details of these modes are left to the CHRE API mode
89  * definitions contained in the chreSensorConfigureMode enum.
90  */
91 enum class SensorMode {
92   Off,
93   ActiveContinuous,
94   ActiveOneShot,
95   PassiveContinuous,
96   PassiveOneShot,
97 };
98 
99 /**
100  * Returns a string representation of the given sensor mode.
101  *
102  * @param sensorMode The sensor mode to obtain a string for.
103  * @return A string representation of the sensor mode.
104  */
105 const char *getSensorModeName(SensorMode sensorMode);
106 
107 /**
108  * @return true if the sensor mode is considered to be active and would cause a
109  *         sensor to be powered on in order to get sensor data.
110  */
sensorModeIsActive(SensorMode sensorMode)111 constexpr bool sensorModeIsActive(SensorMode sensorMode) {
112   return (sensorMode == SensorMode::ActiveContinuous ||
113           sensorMode == SensorMode::ActiveOneShot);
114 }
115 
116 /**
117  * @return true if the sensor mode is considered to be passive and would not
118  *         cause a sensor to be powered on in order to get sensor data.
119  */
sensorModeIsPassive(SensorMode sensorMode)120 constexpr bool sensorModeIsPassive(SensorMode sensorMode) {
121   return (sensorMode == SensorMode::PassiveContinuous ||
122           sensorMode == SensorMode::PassiveOneShot);
123 }
124 
125 /**
126  * @return true if the sensor mode is considered to be contunuous.
127  */
sensorModeIsContinuous(SensorMode sensorMode)128 constexpr bool sensorModeIsContinuous(SensorMode sensorMode) {
129   return (sensorMode == SensorMode::ActiveContinuous ||
130           sensorMode == SensorMode::PassiveContinuous);
131 }
132 
133 /**
134  * @return true if the sensor mode is considered to be one-shot.
135  */
sensorModeIsOneShot(SensorMode sensorMode)136 constexpr bool sensorModeIsOneShot(SensorMode sensorMode) {
137   return (sensorMode == SensorMode::ActiveOneShot ||
138           sensorMode == SensorMode::PassiveOneShot);
139 }
140 
141 /**
142  * Translates a CHRE API enum sensor mode to a SensorMode. This function also
143  * performs input validation and will default to SensorMode::Off if the provided
144  * value is not a valid enum value.
145  *
146  * @param enumSensorMode A potentially unsafe CHRE API enum sensor mode.
147  * @return Returns a SensorMode given a CHRE API enum sensor mode.
148  */
149 SensorMode getSensorModeFromEnum(enum chreSensorConfigureMode enumSensorMode);
150 
151 /**
152  * Translates a SensorMode enum to the CHRE API enum sensor mode.
153  *
154  * @param enumSensorMode A valid SensorMode value
155  * @return A valid CHRE API enum sensor mode.
156  */
157 chreSensorConfigureMode getConfigureModeFromSensorMode(
158     enum SensorMode enumSensorMode);
159 
160 }  // namespace chre
161 
162 #endif  // CHRE_CORE_SENSOR_TYPE_H_
163