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/platform/platform_sensor_type_helpers.h"
18 
19 #include "chre/platform/assert.h"
20 #include "chre/util/macros.h"
21 
22 #ifdef CHREX_SENSOR_SUPPORT
23 #include "chre/extensions/platform/vendor_sensor_types.h"
24 #endif  // CHREX_SENSOR_SUPPORT
25 
26 namespace chre {
27 
getVendorSensorReportingMode(uint8_t sensorType)28 ReportingMode PlatformSensorTypeHelpers::getVendorSensorReportingMode(
29     uint8_t sensorType) {
30 #ifdef CHREX_SENSOR_SUPPORT
31   if (extension::vendorSensorTypeIsOneShot(sensorType)) {
32     return ReportingMode::OneShot;
33   }
34   if (extension::vendorSensorTypeIsOnChange(sensorType)) {
35     return ReportingMode::OnChange;
36   }
37 #endif
38   return ReportingMode::Continuous;
39 }
40 
getVendorSensorIsCalibrated(uint8_t sensorType)41 bool PlatformSensorTypeHelpers::getVendorSensorIsCalibrated(
42     uint8_t sensorType) {
43 #ifdef CHREX_SENSOR_SUPPORT
44   return extension::vendorSensorTypeIsCalibrated(sensorType);
45 #else
46   UNUSED_VAR(sensorType);
47   return false;
48 #endif
49 }
50 
getVendorSensorBiasEventType(uint8_t sensorType,uint16_t * eventType)51 bool PlatformSensorTypeHelpers::getVendorSensorBiasEventType(
52     uint8_t sensorType, uint16_t *eventType) {
53 #ifdef CHREX_SENSOR_SUPPORT
54   return extension::vendorGetSensorBiasEventType(sensorType, eventType);
55 #else
56   return false;
57 #endif
58 }
59 
getVendorSensorLastEventSize(uint8_t sensorType)60 size_t PlatformSensorTypeHelpers::getVendorSensorLastEventSize(
61     uint8_t sensorType) {
62 #ifdef CHREX_SENSOR_SUPPORT
63   return extension::vendorGetLastEventSize(sensorType);
64 #else
65   return 0;
66 #endif
67 }
68 
getVendorSensorTypeName(uint8_t sensorType)69 const char *PlatformSensorTypeHelpers::getVendorSensorTypeName(
70     uint8_t sensorType) {
71 #ifdef CHREX_SENSOR_SUPPORT
72   return extension::vendorSensorTypeName(sensorType);
73 #else
74   switch (sensorType) {
75     case CHRE_VENDOR_SENSOR_TYPE(0):
76       return "Vendor Type 0";
77     case CHRE_VENDOR_SENSOR_TYPE(1):
78       return "Vendor Type 1";
79     case CHRE_VENDOR_SENSOR_TYPE(2):
80       return "Vendor Type 2";
81     case CHRE_VENDOR_SENSOR_TYPE(3):
82       return "Vendor Type 3";
83     case CHRE_VENDOR_SENSOR_TYPE(4):
84       return "Vendor Type 4";
85     case CHRE_VENDOR_SENSOR_TYPE(5):
86       return "Vendor Type 5";
87     case CHRE_VENDOR_SENSOR_TYPE(6):
88       return "Vendor Type 6";
89     case CHRE_VENDOR_SENSOR_TYPE(7):
90       return "Vendor Type 7";
91     case CHRE_VENDOR_SENSOR_TYPE(8):
92       return "Vendor Type 8";
93     case CHRE_VENDOR_SENSOR_TYPE(9):
94       return "Vendor Type 9";
95     default:
96       CHRE_ASSERT(false);
97       return "";
98   }
99 #endif
100 }
101 
getVendorLastSample(uint8_t sensorType,const ChreSensorData * event,ChreSensorData * lastEvent)102 void PlatformSensorTypeHelpers::getVendorLastSample(uint8_t sensorType,
103                                                     const ChreSensorData *event,
104                                                     ChreSensorData *lastEvent) {
105 #ifdef CHREX_SENSOR_SUPPORT
106   extension::vendorGetLastSample(sensorType, event, lastEvent);
107 #endif
108 }
109 
getTempSensorType(uint8_t sensorType)110 uint8_t PlatformSensorTypeHelpersBase::getTempSensorType(uint8_t sensorType) {
111   switch (sensorType) {
112     case CHRE_SENSOR_TYPE_ACCELEROMETER:
113     case CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER:
114       return CHRE_SENSOR_TYPE_ACCELEROMETER_TEMPERATURE;
115     case CHRE_SENSOR_TYPE_GYROSCOPE:
116     case CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE:
117       return CHRE_SENSOR_TYPE_GYROSCOPE_TEMPERATURE;
118     case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD:
119     case CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD:
120       return CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD_TEMPERATURE;
121     default:
122       return CHRE_SENSOR_TYPE_INVALID;
123   }
124 }
125 
126 SensorSampleType
getSensorSampleTypeFromSensorType(uint8_t sensorType)127 PlatformSensorTypeHelpersBase::getSensorSampleTypeFromSensorType(
128     uint8_t sensorType) {
129   switch (sensorType) {
130     case CHRE_SENSOR_TYPE_ACCELEROMETER:
131     case CHRE_SENSOR_TYPE_GYROSCOPE:
132     case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD:
133     case CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER:
134     case CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE:
135     case CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD:
136       return SensorSampleType::ThreeAxis;
137     case CHRE_SENSOR_TYPE_PRESSURE:
138     case CHRE_SENSOR_TYPE_LIGHT:
139     case CHRE_SENSOR_TYPE_ACCELEROMETER_TEMPERATURE:
140     case CHRE_SENSOR_TYPE_GYROSCOPE_TEMPERATURE:
141     case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD_TEMPERATURE:
142       return SensorSampleType::Float;
143     case CHRE_SENSOR_TYPE_INSTANT_MOTION_DETECT:
144     case CHRE_SENSOR_TYPE_STATIONARY_DETECT:
145     case CHRE_SENSOR_TYPE_STEP_DETECT:
146       return SensorSampleType::Occurrence;
147     case CHRE_SENSOR_TYPE_PROXIMITY:
148       return SensorSampleType::Byte;
149     default:
150 #ifdef CHREX_SENSOR_SUPPORT
151       return extension::vendorSensorSampleTypeFromSensorType(sensorType);
152 #else
153       // Update implementation to prevent undefined from being used.
154       CHRE_ASSERT(false);
155       return SensorSampleType::Unknown;
156 #endif
157   }
158 }
159 
toCalibratedSensorType(uint8_t sensorType)160 uint8_t PlatformSensorTypeHelpersBase::toCalibratedSensorType(
161     uint8_t sensorType) {
162   switch (sensorType) {
163     case CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER:
164       return CHRE_SENSOR_TYPE_ACCELEROMETER;
165     case CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE:
166       return CHRE_SENSOR_TYPE_GYROSCOPE;
167     case CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD:
168       return CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD;
169     default:
170       /* empty */
171       break;
172   }
173 
174   return sensorType;
175 }
176 
reportsBias(uint8_t sensorType)177 bool PlatformSensorTypeHelpersBase::reportsBias(uint8_t sensorType) {
178   switch (sensorType) {
179     case CHRE_SENSOR_TYPE_ACCELEROMETER:
180     case CHRE_SENSOR_TYPE_GYROSCOPE:
181     case CHRE_SENSOR_TYPE_GEOMAGNETIC_FIELD:
182     case CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER:
183     case CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE:
184     case CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD:
185       return true;
186     default:
187 #ifdef CHREX_SENSOR_SUPPORT
188       return extension::vendorSensorReportsBias(sensorType);
189 #else
190       return false;
191 #endif
192   }
193 }
194 
rewriteToChreSensorType(uint8_t * sensorType)195 void PlatformSensorTypeHelpersBase::rewriteToChreSensorType(
196     uint8_t *sensorType) {
197   CHRE_ASSERT(sensorType);
198 
199   if (*sensorType == CHRE_SLPI_SENSOR_TYPE_BIG_IMAGE_ACCEL) {
200     *sensorType = CHRE_SENSOR_TYPE_ACCELEROMETER;
201   } else if (*sensorType == CHRE_SLPI_SENSOR_TYPE_BIG_IMAGE_UNCAL_ACCEL) {
202     *sensorType = CHRE_SENSOR_TYPE_UNCALIBRATED_ACCELEROMETER;
203   } else if (*sensorType == CHRE_SLPI_SENSOR_TYPE_BIG_IMAGE_UNCAL_GYRO) {
204     *sensorType = CHRE_SENSOR_TYPE_UNCALIBRATED_GYROSCOPE;
205   } else if (*sensorType == CHRE_SLPI_SENSOR_TYPE_BIG_IMAGE_UNCAL_MAG) {
206     *sensorType = CHRE_SENSOR_TYPE_UNCALIBRATED_GEOMAGNETIC_FIELD;
207   } else if (*sensorType == CHRE_SLPI_SENSOR_TYPE_BIG_IMAGE_LIGHT) {
208     *sensorType = CHRE_SENSOR_TYPE_LIGHT;
209   }
210 }
211 
212 }  // namespace chre
213