1 /*
2 * Copyright (C) 2019 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_HELPERS_H_
18 #define CHRE_CORE_SENSOR_TYPE_HELPERS_H_
19
20 #include <cstring>
21
22 #include "chre/core/sensor_type.h"
23 #include "chre/platform/platform_sensor_type_helpers.h"
24
25 namespace chre {
26
27 /**
28 * Exposes several static methods to assist in determining sensor information
29 * from the sensor type.
30 */
31 class SensorTypeHelpers : public PlatformSensorTypeHelpers {
32 public:
33 /**
34 * @param sensorType The type of sensor to check
35 * @return Whether this sensor is a one-shot sensor.
36 */
isOneShot(uint8_t sensorType)37 static bool isOneShot(uint8_t sensorType) {
38 return getReportingMode(sensorType) == ReportingMode::OneShot;
39 }
40
41 /**
42 * @param sensorType The type of sensor to check
43 * @return Whether this sensor is an on-change sensor.
44 */
isOnChange(uint8_t sensorType)45 static bool isOnChange(uint8_t sensorType) {
46 return getReportingMode(sensorType) == ReportingMode::OnChange;
47 }
48
49 /**
50 * @param sensorType The type of sensor to check
51 * @return Whether this sensor is a continuous sensor.
52 */
isContinuous(uint8_t sensorType)53 static bool isContinuous(uint8_t sensorType) {
54 return getReportingMode(sensorType) == ReportingMode::Continuous;
55 }
56
57 /**
58 * @param sensorType The type of sensor to check
59 * @return true if this sensor is a vendor sensor.
60 */
isVendorSensorType(uint8_t sensorType)61 static bool isVendorSensorType(uint8_t sensorType) {
62 return sensorType >= CHRE_SENSOR_TYPE_VENDOR_START;
63 }
64
65 /**
66 * @param sensorType The type of sensor to get the reporting mode for.
67 * @return the reporting mode for this sensor.
68 */
69 static ReportingMode getReportingMode(uint8_t sensorType);
70
71 /**
72 * @param sensorType The type of sensor to check
73 * @return Whether this sensor is calibrated.
74 */
75 static bool isCalibrated(uint8_t sensorType);
76
77 /**
78 * @param sensorType The type of sensor to get the bias event type for.
79 * @param eventType A non-null pointer to the event type to populate
80 * @return true if this sensor has a bias event type.
81 */
82 static bool getBiasEventType(uint8_t sensorType, uint16_t *eventType);
83
84 /**
85 * Determines the size needed to store the latest event from a sensor. Since
86 * only on-change sensors have their latest events retained, only those
87 * sensors will receive a non-zero value from this method.
88 *
89 * @param sensorType The sensorType of this sensor.
90 * @return the memory size for an on-change sensor to store its last data
91 * event.
92 */
93 static size_t getLastEventSize(uint8_t sensorType);
94
95 /**
96 * @param sensorType The sensor type to obtain a string for.
97 * @return A string representation of the sensor type.
98 */
99 static const char *getSensorTypeName(uint8_t sensorType);
100
101 /**
102 * @param sensorType The sensor type.
103 * @return The corresponding uncalibrated sensor type. If the sensor does not
104 * have one or is already uncalibrated, then the input sensorType is
105 * returned.
106 */
107 static uint8_t toUncalibratedSensorType(uint8_t sensorType);
108
109 /**
110 * Extracts the last sample from the suppled event and updates it to the
111 * supplied last event memory as a single-sample event. No-op if not an
112 * on-change sensor.
113 *
114 * @param sensorType The type of this sensor.
115 * @param event A non-null data event of the specified sensorType, must
116 * contain one more more samples.
117 * @param lastEvent The sensor's last event memory to store the last event
118 * of an on-change sensor.
119 */
120 static void getLastSample(uint8_t sensorType, const ChreSensorData *event,
121 ChreSensorData *lastEvent);
122
123 /**
124 * Copies the last data sample from newEvent to lastEvent memory and modifies
125 * its header accordingly.
126 *
127 * @param newEvent Sensor data event of type SensorDataType.
128 * @param lastEvent Single-sample data event of type SensorDataType.
129 */
130 template <typename SensorDataType>
131 static void copyLastSample(const SensorDataType *newEvent,
132 SensorDataType *lastEvent);
133 };
134
135 template <typename SensorDataType>
copyLastSample(const SensorDataType * newEvent,SensorDataType * lastEvent)136 void SensorTypeHelpers::copyLastSample(const SensorDataType *newEvent,
137 SensorDataType *lastEvent) {
138 // Copy header and one sample over to last event memory.
139 memcpy(lastEvent, newEvent, sizeof(SensorDataType));
140
141 // Modify last event if there are more than one samples in the supplied event.
142 if (newEvent->header.readingCount > 1) {
143 // Identify last sample's timestamp.
144 uint64_t sampleTimestampNs = newEvent->header.baseTimestamp;
145 for (size_t i = 0; i < newEvent->header.readingCount; ++i) {
146 sampleTimestampNs += newEvent->readings[i].timestampDelta;
147 }
148
149 // Update last event to match the last data sample.
150 lastEvent->header.baseTimestamp = sampleTimestampNs;
151 lastEvent->header.readingCount = 1;
152 lastEvent->readings[0] =
153 newEvent->readings[newEvent->header.readingCount - 1];
154 lastEvent->readings[0].timestampDelta = 0;
155 }
156 }
157
158 } // namespace chre
159
160 #endif // CHRE_CORE_SENSOR_TYPE_HELPERS_H_
161