• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "chre/core/sensor_type_helpers.h"
18 
19 #include <cinttypes>
20 
21 #include "chre/platform/assert.h"
22 
23 namespace chre {
24 
getReportingMode(uint8_t sensorType)25 ReportingMode SensorTypeHelpers::getReportingMode(uint8_t sensorType) {
26   if (isVendorSensorType(sensorType)) {
27     return getVendorSensorReportingMode(sensorType);
28   }
29 
30   switch (sensorType) {
31     case CHRE_SENSOR_TYPE_INSTANT_MOTION_DETECT:
32     case CHRE_SENSOR_TYPE_STATIONARY_DETECT:
33       return ReportingMode::OneShot;
34     case CHRE_SENSOR_TYPE_LIGHT:
35     case CHRE_SENSOR_TYPE_PROXIMITY:
36     case CHRE_SENSOR_TYPE_STEP_COUNTER:
37     case CHRE_SENSOR_TYPE_HINGE_ANGLE:
38       return ReportingMode::OnChange;
39     default:
40       return ReportingMode::Continuous;
41   }
42 }
43 
isCalibrated(uint8_t sensorType)44 bool SensorTypeHelpers::isCalibrated(uint8_t sensorType) {
45   if (isVendorSensorType(sensorType)) {
46     return getVendorSensorIsCalibrated(sensorType);
47   }
48 
49   switch (sensorType) {
50     case CHRE_SENSOR_TYPE_ACCELEROMETER:
51     case CHRE_SENSOR_TYPE_GYROSCOPE:
52     case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD:
53       return true;
54     default:
55       return false;
56   }
57 }
58 
getBiasEventType(uint8_t sensorType,uint16_t * eventType)59 bool SensorTypeHelpers::getBiasEventType(uint8_t sensorType,
60                                          uint16_t *eventType) {
61   CHRE_ASSERT(eventType != nullptr);
62 
63   if (isVendorSensorType(sensorType)) {
64     return getVendorSensorBiasEventType(sensorType, eventType);
65   }
66 
67   bool success = true;
68   switch (sensorType) {
69     case CHRE_SENSOR_TYPE_ACCELEROMETER:
70       *eventType = CHRE_EVENT_SENSOR_ACCELEROMETER_BIAS_INFO;
71       break;
72     case CHRE_SENSOR_TYPE_GYROSCOPE:
73       *eventType = CHRE_EVENT_SENSOR_GYROSCOPE_BIAS_INFO;
74       break;
75     case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD:
76       *eventType = CHRE_EVENT_SENSOR_GEOMAGNETIC_FIELD_BIAS_INFO;
77       break;
78     case CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER:
79       *eventType = CHRE_EVENT_SENSOR_UNCALIBRATED_ACCELEROMETER_BIAS_INFO;
80       break;
81     case CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE:
82       *eventType = CHRE_EVENT_SENSOR_UNCALIBRATED_GYROSCOPE_BIAS_INFO;
83       break;
84     case CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD:
85       *eventType = CHRE_EVENT_SENSOR_UNCALIBRATED_GEOMAGNETIC_FIELD_BIAS_INFO;
86       break;
87     default:
88       success = false;
89   }
90 
91   return success;
92 }
93 
getLastEventSize(uint8_t sensorType)94 size_t SensorTypeHelpers::getLastEventSize(uint8_t sensorType) {
95   if (isOnChange(sensorType)) {
96     if (isVendorSensorType(sensorType)) {
97       return getVendorSensorLastEventSize(sensorType);
98     }
99 
100     switch (sensorType) {
101       case CHRE_SENSOR_TYPE_ACCELEROMETER:
102       case CHRE_SENSOR_TYPE_GYROSCOPE:
103       case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD:
104       case CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER:
105       case CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE:
106       case CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD:
107         return sizeof(chreSensorThreeAxisData);
108       case CHRE_SENSOR_TYPE_PRESSURE:
109       case CHRE_SENSOR_TYPE_LIGHT:
110       case CHRE_SENSOR_TYPE_ACCELEROMETER_TEMPERATURE:
111       case CHRE_SENSOR_TYPE_GYROSCOPE_TEMPERATURE:
112       case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD_TEMPERATURE:
113       case CHRE_SENSOR_TYPE_HINGE_ANGLE:
114         return sizeof(chreSensorFloatData);
115       case CHRE_SENSOR_TYPE_INSTANT_MOTION_DETECT:
116       case CHRE_SENSOR_TYPE_STATIONARY_DETECT:
117       case CHRE_SENSOR_TYPE_STEP_DETECT:
118         return sizeof(chreSensorOccurrenceData);
119       case CHRE_SENSOR_TYPE_PROXIMITY:
120         return sizeof(chreSensorByteData);
121       case CHRE_SENSOR_TYPE_STEP_COUNTER:
122         return sizeof(chreSensorUint64Data);
123       default:
124         // Update implementation to prevent undefined from being used.
125         CHRE_ASSERT(false);
126         return 0;
127     }
128   }
129   return 0;
130 }
131 
getSensorTypeName(uint8_t sensorType)132 const char *SensorTypeHelpers::getSensorTypeName(uint8_t sensorType) {
133   if (isVendorSensorType(sensorType)) {
134     return getVendorSensorTypeName(sensorType);
135   }
136 
137   switch (sensorType) {
138     case CHRE_SENSOR_TYPE_INVALID:
139       return "Unknown";
140     case CHRE_SENSOR_TYPE_ACCELEROMETER:
141       return "Accelerometer";
142     case CHRE_SENSOR_TYPE_INSTANT_MOTION_DETECT:
143       return "Instant Motion";
144     case CHRE_SENSOR_TYPE_STATIONARY_DETECT:
145       return "Stationary Detect";
146     case CHRE_SENSOR_TYPE_GYROSCOPE:
147       return "Gyroscope";
148     case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD:
149       return "Geomagnetic Field";
150     case CHRE_SENSOR_TYPE_PRESSURE:
151       return "Pressure";
152     case CHRE_SENSOR_TYPE_LIGHT:
153       return "Light";
154     case CHRE_SENSOR_TYPE_PROXIMITY:
155       return "Proximity";
156     case CHRE_SENSOR_TYPE_STEP_DETECT:
157       return "Step Detect";
158     case CHRE_SENSOR_TYPE_STEP_COUNTER:
159       return "Step Counter";
160     case CHRE_SENSOR_TYPE_HINGE_ANGLE:
161       return "Hinge Angle";
162     case CHRE_SENSOR_TYPE_ACCELEROMETER_TEMPERATURE:
163       return "Accelerometer Temp";
164     case CHRE_SENSOR_TYPE_GYROSCOPE_TEMPERATURE:
165       return "Gyroscope Temp";
166     case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD_TEMPERATURE:
167       return "Geomagnetic Field Temp";
168     case CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER:
169       return "Uncal Accelerometer";
170     case CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE:
171       return "Uncal Gyroscope";
172     case CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD:
173       return "Uncal Geomagnetic Field";
174     default:
175       CHRE_ASSERT(false);
176       return "";
177   }
178 }
179 
toUncalibratedSensorType(uint8_t sensorType)180 uint8_t SensorTypeHelpers::toUncalibratedSensorType(uint8_t sensorType) {
181   switch (sensorType) {
182     case CHRE_SENSOR_TYPE_ACCELEROMETER:
183       return CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER;
184     case CHRE_SENSOR_TYPE_GYROSCOPE:
185       return CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE;
186     case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD:
187       return CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD;
188     default:
189       /* empty */
190       break;
191   }
192 
193   return sensorType;
194 }
195 
getLastSample(uint8_t sensorType,const ChreSensorData * event,ChreSensorData * lastEvent)196 void SensorTypeHelpers::getLastSample(uint8_t sensorType,
197                                       const ChreSensorData *event,
198                                       ChreSensorData *lastEvent) {
199   if (!isOnChange(sensorType)) {
200     // no op
201     return;
202   }
203 
204   if (isVendorSensorType(sensorType)) {
205     getVendorLastSample(sensorType, event, lastEvent);
206   } else {
207     switch (sensorType) {
208       case CHRE_SENSOR_TYPE_LIGHT:
209       case CHRE_SENSOR_TYPE_HINGE_ANGLE:
210         copyLastSample<chreSensorFloatData>(&event->floatData,
211                                             &lastEvent->floatData);
212         break;
213       case CHRE_SENSOR_TYPE_PROXIMITY:
214         copyLastSample<chreSensorByteData>(&event->byteData,
215                                            &lastEvent->byteData);
216         break;
217       case CHRE_SENSOR_TYPE_STEP_COUNTER:
218         copyLastSample<chreSensorUint64Data>(&event->uint64Data,
219                                              &lastEvent->uint64Data);
220         break;
221       default:
222         LOGE("Unhandled sensor type %" PRIu8, sensorType);
223     }
224   }
225 }
226 
227 }  // namespace chre
228