• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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_PLATFORM_SLPI_SEE_SEE_CAL_HELPER_H_
18 #define CHRE_PLATFORM_SLPI_SEE_SEE_CAL_HELPER_H_
19 
20 extern "C" {
21 
22 #include "sns_client.h"
23 
24 }  // extern "C"
25 
26 #include "sns_suid.pb.h"
27 
28 #include "chre/core/sensor_type.h"
29 #include "chre/platform/mutex.h"
30 #include "chre/util/non_copyable.h"
31 #include "chre/util/optional.h"
32 
33 namespace chre {
34 
35 class SeeHelper;
36 
37 /**
38  * Helps manage and apply sensor calibration data provided through SEE.
39  */
40 class SeeCalHelper : public NonCopyable {
41  public:
42   /**
43    * Applies cached calibration (if any) to raw 3-axis sensor readings.
44    * Thread-safe.
45    *
46    * @param sensorType Type of sensor that generated the sample
47    * @param input 3-axis raw sample {x,y,z}
48    * @param output Location to store sample with calibration applied (can be
49    *               same as input)
50    */
51   void applyCalibration(SensorType sensorType, const float input[3],
52                         float output[3]) const;
53 
54   /**
55    * Returns the cached calibration data. If the calibration data is available,
56    * this method will store all fields in the provided chreSensorThreeAxisData
57    * pointer, where the sample count is one. Thread-safe.
58    *
59    * @param sensorType Type of sensor to retrieve calibration data from, should
60    *                   be the type of a runtime-calibrated sensor
61    * @param biasData A non-null pointer to store the calibration data, not used
62    *                 if the calibration data is not available
63    *
64    * @return true if calibration data is successfully stored, false otherwise
65    */
66   bool getBias(
67       SensorType sensorType, struct chreSensorThreeAxisData *biasData) const;
68 
69   /**
70    * Get the cached SUID of a calibration sensor that corresponds to the
71    * specified sensorType.
72    *
73    * @param sensorType The sensor type of the calibration sensor.
74    *
75    * @return A constant reference to the calibration sensor's SUID if present.
76    *         Otherwise, a reference to sns_suid_sensor_init_zero is returned.
77    */
78   const sns_std_suid& getCalSuidFromSensorType(SensorType sensorType) const;
79 
80   /**
81    * Uses the supplied SeeHelper instance to register for updates to all
82    * supported SEE calibration sensors. The SeeHelper instance should then pass
83    * decoded calibration data to updateCalibration() and use applyCalibration()
84    * as needed.
85    *
86    * @param seeHelper SeeHelper instance to use when looking up calibration
87    *                  sensor SUIDs and registering for their output
88    *
89    * @return true if all SEE calibration sensors were successfully registered
90    */
91   bool registerForCalibrationUpdates(SeeHelper& seeHelper);
92 
93   /**
94    * Updates the cached calibration data used in subsequent calls to
95    * applyCalibration. Thread-safe.
96    *
97    * @param suid Sensor UID associated with the incoming calibration data
98    * @param hasBias true if bias was decoded from the proto
99    * @param bias 3-axis bias; only valid if hasBias is true
100    * @param hasScale true if scale was decoded from the proto
101    * @param scale 3-axis scale factor; only valid if hasScale is true
102    * @param hasMatrix true if matrix was decoded from the proto
103    * @param matrix 3x3 compensation matrix; only valid if hasMatrix is true
104    * @param accuracy CHRE accuracy rating of the calibration quality (see
105    *     CHRE_SENSOR_ACCURACY)
106    * @param timestamp The timestamp of the calibration event
107    *
108    * @see CHRE_SENSOR_ACCURACY
109    */
110   void updateCalibration(const sns_std_suid& suid, bool hasBias, float bias[3],
111                          bool hasScale, float scale[3], bool hasMatrix,
112                          float matrix[9], uint8_t accuracy, uint64_t timestamp);
113 
114   /**
115    * @param suid SUID of the calibration sensor
116    *
117    * @return the SensorType corresponding to this physical sensor
118    */
119   SensorType getSensorTypeFromSuid(const sns_std_suid& suid) const;
120 
121  private:
122   //! A struct to store a sensor's calibration data
123   struct SeeCalData {
124     float bias[3];
125     float scale[3];
126     float matrix[9];
127     bool hasBias;
128     bool hasScale;
129     bool hasMatrix;
130     uint8_t accuracy;
131     uint64_t timestamp;
132   };
133 
134   //! A struct to store a cal sensor's UID and its cal data.
135   struct SeeCalInfo {
136     Optional<sns_std_suid> suid;
137     SeeCalData cal;
138   };
139 
140   //! The list of SEE cal sensors supported.
141   enum class SeeCalSensor : size_t {
142     AccelCal,
143     GyroCal,
144     MagCal,
145     NumCalSensors,
146   };
147 
148   //! A convenience constant.
149   static constexpr size_t kNumSeeCalSensors = static_cast<size_t>(
150       SeeCalSensor::NumCalSensors);
151 
152   //! Protects access to calibration data, which may be used in multiple threads
153   mutable Mutex mMutex;
154 
155   //! Cal info of all the cal sensors.
156   SeeCalInfo mCalInfo[kNumSeeCalSensors] = {};
157 
158   //! Map SensorType to associated index in mCalInfo
159   static size_t getCalIndexFromSensorType(SensorType sensorType);
160 
161   //! Map index in mCalInfo to SEE sensor data type string
162   static const char *getDataTypeForCalSensorIndex(size_t calSensorIndex);
163 
164   //! Map SUID to associated index in mCalInfo
165   size_t getCalIndexFromSuid(const sns_std_suid& suid) const;
166 };
167 
168 }  // namespace chre
169 
170 #endif  // CHRE_PLATFORM_SLPI_SEE_SEE_CAL_HELPER_H_
171