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 <Constants.h>
18 #include <MockLocation.h>
19 #include <Utils.h>
20 #include <aidl/android/hardware/gnss/BnGnss.h>
21 #include <utils/SystemClock.h>
22 
23 namespace android {
24 namespace hardware {
25 namespace gnss {
26 namespace common {
27 
28 using aidl::android::hardware::gnss::ElapsedRealtime;
29 using aidl::android::hardware::gnss::GnssClock;
30 using aidl::android::hardware::gnss::GnssData;
31 using aidl::android::hardware::gnss::GnssMeasurement;
32 using aidl::android::hardware::gnss::IGnss;
33 using aidl::android::hardware::gnss::IGnssMeasurementCallback;
34 using aidl::android::hardware::gnss::SatellitePvt;
35 
36 using GnssSvFlags = V1_0::IGnssCallback::GnssSvFlags;
37 using GnssMeasurementFlagsV1_0 = V1_0::IGnssMeasurementCallback::GnssMeasurementFlags;
38 using GnssMeasurementFlagsV2_1 = V2_1::IGnssMeasurementCallback::GnssMeasurementFlags;
39 using GnssMeasurementStateV2_0 = V2_0::IGnssMeasurementCallback::GnssMeasurementState;
40 using ElapsedRealtimeFlags = V2_0::ElapsedRealtimeFlags;
41 using GnssConstellationTypeV2_0 = V2_0::GnssConstellationType;
42 using IGnssMeasurementCallbackV2_0 = V2_0::IGnssMeasurementCallback;
43 using GnssSignalType = V2_1::GnssSignalType;
44 
45 using GnssDataV2_0 = V2_0::IGnssMeasurementCallback::GnssData;
46 using GnssDataV2_1 = V2_1::IGnssMeasurementCallback::GnssData;
47 using GnssSvInfoV1_0 = V1_0::IGnssCallback::GnssSvInfo;
48 using GnssSvInfoV2_0 = V2_0::IGnssCallback::GnssSvInfo;
49 using GnssSvInfoV2_1 = V2_1::IGnssCallback::GnssSvInfo;
50 using GnssAntennaInfo = ::android::hardware::gnss::V2_1::IGnssAntennaInfoCallback::GnssAntennaInfo;
51 using Row = V2_1::IGnssAntennaInfoCallback::Row;
52 using Coord = V2_1::IGnssAntennaInfoCallback::Coord;
53 
getMockMeasurementV2_1()54 GnssDataV2_1 Utils::getMockMeasurementV2_1() {
55     GnssDataV2_0 gnssDataV2_0 = Utils::getMockMeasurementV2_0();
56     V2_1::IGnssMeasurementCallback::GnssMeasurement gnssMeasurementV2_1 = {
57             .v2_0 = gnssDataV2_0.measurements[0],
58             .flags = (uint32_t)(GnssMeasurementFlagsV2_1::HAS_CARRIER_FREQUENCY |
59                                 GnssMeasurementFlagsV2_1::HAS_CARRIER_PHASE |
60                                 GnssMeasurementFlagsV2_1::HAS_FULL_ISB |
61                                 GnssMeasurementFlagsV2_1::HAS_FULL_ISB_UNCERTAINTY |
62                                 GnssMeasurementFlagsV2_1::HAS_SATELLITE_ISB |
63                                 GnssMeasurementFlagsV2_1::HAS_SATELLITE_ISB_UNCERTAINTY),
64             .fullInterSignalBiasNs = 30.0,
65             .fullInterSignalBiasUncertaintyNs = 250.0,
66             .satelliteInterSignalBiasNs = 20.0,
67             .satelliteInterSignalBiasUncertaintyNs = 150.0,
68             .basebandCN0DbHz = 25.0,
69     };
70     GnssSignalType referenceSignalTypeForIsb = {
71             .constellation = GnssConstellationTypeV2_0::GPS,
72             .carrierFrequencyHz = 1.59975e+09,
73             .codeType = "C",
74     };
75     V2_1::IGnssMeasurementCallback::GnssClock gnssClockV2_1 = {
76             .v1_0 = gnssDataV2_0.clock,
77             .referenceSignalTypeForIsb = referenceSignalTypeForIsb,
78     };
79     hidl_vec<V2_1::IGnssMeasurementCallback::GnssMeasurement> measurements(1);
80     measurements[0] = gnssMeasurementV2_1;
81     GnssDataV2_1 gnssDataV2_1 = {
82             .measurements = measurements,
83             .clock = gnssClockV2_1,
84             .elapsedRealtime = gnssDataV2_0.elapsedRealtime,
85     };
86     return gnssDataV2_1;
87 }
88 
getMockMeasurementV2_0()89 GnssDataV2_0 Utils::getMockMeasurementV2_0() {
90     V1_0::IGnssMeasurementCallback::GnssMeasurement measurement_1_0 = {
91             .flags = (uint32_t)GnssMeasurementFlagsV1_0::HAS_CARRIER_FREQUENCY,
92             .svid = (int16_t)6,
93             .constellation = V1_0::GnssConstellationType::UNKNOWN,
94             .timeOffsetNs = 0.0,
95             .receivedSvTimeInNs = 8195997131077,
96             .receivedSvTimeUncertaintyInNs = 15,
97             .cN0DbHz = 30.0,
98             .pseudorangeRateMps = -484.13739013671875,
99             .pseudorangeRateUncertaintyMps = 1.0379999876022339,
100             .accumulatedDeltaRangeState = (uint32_t)V1_0::IGnssMeasurementCallback::
101                     GnssAccumulatedDeltaRangeState::ADR_STATE_UNKNOWN,
102             .accumulatedDeltaRangeM = 0.0,
103             .accumulatedDeltaRangeUncertaintyM = 0.0,
104             .carrierFrequencyHz = 1.59975e+09,
105             .multipathIndicator =
106                     V1_0::IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_UNKNOWN};
107     V1_1::IGnssMeasurementCallback::GnssMeasurement measurement_1_1 = {.v1_0 = measurement_1_0};
108     V2_0::IGnssMeasurementCallback::GnssMeasurement measurement_2_0 = {
109             .v1_1 = measurement_1_1,
110             .codeType = "C",
111             .state = GnssMeasurementStateV2_0::STATE_CODE_LOCK |
112                      GnssMeasurementStateV2_0::STATE_BIT_SYNC |
113                      GnssMeasurementStateV2_0::STATE_SUBFRAME_SYNC |
114                      GnssMeasurementStateV2_0::STATE_TOW_DECODED |
115                      GnssMeasurementStateV2_0::STATE_GLO_STRING_SYNC |
116                      GnssMeasurementStateV2_0::STATE_GLO_TOD_DECODED,
117             .constellation = GnssConstellationTypeV2_0::GLONASS,
118     };
119 
120     hidl_vec<IGnssMeasurementCallbackV2_0::GnssMeasurement> measurements(1);
121     measurements[0] = measurement_2_0;
122     V1_0::IGnssMeasurementCallback::GnssClock clock = {.timeNs = 2713545000000,
123                                                        .fullBiasNs = -1226701900521857520,
124                                                        .biasNs = 0.59689998626708984,
125                                                        .biasUncertaintyNs = 47514.989972114563,
126                                                        .driftNsps = -51.757811607455452,
127                                                        .driftUncertaintyNsps = 310.64968328491528,
128                                                        .hwClockDiscontinuityCount = 1};
129 
130     V2_0::ElapsedRealtime timestamp = {
131             .flags = ElapsedRealtimeFlags::HAS_TIMESTAMP_NS |
132                      ElapsedRealtimeFlags::HAS_TIME_UNCERTAINTY_NS,
133             .timestampNs = static_cast<uint64_t>(::android::elapsedRealtimeNano()),
134             // This is an hardcoded value indicating a 1ms of uncertainty between the two clocks.
135             // In an actual implementation provide an estimate of the synchronization uncertainty
136             // or don't set the field.
137             .timeUncertaintyNs = 1000000};
138 
139     GnssDataV2_0 gnssData = {
140             .measurements = measurements, .clock = clock, .elapsedRealtime = timestamp};
141     return gnssData;
142 }
143 
getMockMeasurement(const bool enableCorrVecOutputs)144 GnssData Utils::getMockMeasurement(const bool enableCorrVecOutputs) {
145     aidl::android::hardware::gnss::GnssSignalType signalType = {
146             .constellation = aidl::android::hardware::gnss::GnssConstellationType::GLONASS,
147             .carrierFrequencyHz = 1.59975e+09,
148             .codeType = aidl::android::hardware::gnss::GnssSignalType::CODE_TYPE_C,
149     };
150     GnssMeasurement measurement = {
151             .flags = GnssMeasurement::HAS_AUTOMATIC_GAIN_CONTROL |
152                      GnssMeasurement::HAS_CARRIER_FREQUENCY | GnssMeasurement::HAS_CARRIER_PHASE |
153                      GnssMeasurement::HAS_CARRIER_PHASE_UNCERTAINTY |
154                      GnssMeasurement::HAS_FULL_ISB | GnssMeasurement::HAS_FULL_ISB_UNCERTAINTY |
155                      GnssMeasurement::HAS_SATELLITE_ISB |
156                      GnssMeasurement::HAS_SATELLITE_ISB_UNCERTAINTY |
157                      GnssMeasurement::HAS_SATELLITE_PVT,
158             .svid = 13,
159             .signalType = signalType,
160             .receivedSvTimeInNs = 8195997131077,
161             .receivedSvTimeUncertaintyInNs = 15,
162             .antennaCN0DbHz = 30.0,
163             .basebandCN0DbHz = 26.5,
164             .agcLevelDb = 2.3,
165             .pseudorangeRateMps = -484.13739013671875,
166             .pseudorangeRateUncertaintyMps = 1.0379999876022339,
167             .accumulatedDeltaRangeState = GnssMeasurement::ADR_STATE_UNKNOWN,
168             .accumulatedDeltaRangeM = 1.52,
169             .accumulatedDeltaRangeUncertaintyM = 2.43,
170             .multipathIndicator = aidl::android::hardware::gnss::GnssMultipathIndicator::UNKNOWN,
171             .state = GnssMeasurement::STATE_CODE_LOCK | GnssMeasurement::STATE_BIT_SYNC |
172                      GnssMeasurement::STATE_SUBFRAME_SYNC | GnssMeasurement::STATE_TOW_DECODED |
173                      GnssMeasurement::STATE_GLO_STRING_SYNC |
174                      GnssMeasurement::STATE_GLO_TOD_DECODED,
175             .fullInterSignalBiasNs = 21.5,
176             .fullInterSignalBiasUncertaintyNs = 792.0,
177             .satelliteInterSignalBiasNs = 233.9,
178             .satelliteInterSignalBiasUncertaintyNs = 921.2,
179             .satellitePvt = {.flags = SatellitePvt::HAS_POSITION_VELOCITY_CLOCK_INFO |
180                                       SatellitePvt::HAS_IONO | SatellitePvt::HAS_TROPO,
181                              .satPosEcef = {.posXMeters = 10442993.1153328,
182                                             .posYMeters = -19926932.8051666,
183                                             .posZMeters = -12034295.0216203,
184                                             .ureMeters = 1000.2345678},
185                              .satVelEcef = {.velXMps = -478.667183715732,
186                                             .velYMps = 1580.68371984114,
187                                             .velZMps = -3030.52994449997,
188                                             .ureRateMps = 10.2345678},
189                              .satClockInfo = {.satHardwareCodeBiasMeters = 1.396983861923e-09,
190                                               .satTimeCorrectionMeters = -7113.08964331,
191                                               .satClkDriftMps = 0},
192                              .ionoDelayMeters = 3.069949602639317e-08,
193                              .tropoDelayMeters = 3.882265204404031},
194             .correlationVectors = {}};
195 
196     GnssClock clock = {.gnssClockFlags = GnssClock::HAS_FULL_BIAS | GnssClock::HAS_BIAS |
197                                          GnssClock::HAS_BIAS_UNCERTAINTY | GnssClock::HAS_DRIFT |
198                                          GnssClock::HAS_DRIFT_UNCERTAINTY,
199                        .timeNs = 35854545000000,
200                        .fullBiasNs = -234621900521857520,
201                        .biasNs = 0.2352389998626708984,
202                        .biasUncertaintyNs = 274.989972114563,
203                        .driftNsps = -124.3742360,
204                        .driftUncertaintyNsps = 239.6234285828,
205                        .hwClockDiscontinuityCount = 999};
206 
207     ElapsedRealtime timestamp = {
208             .flags = ElapsedRealtime::HAS_TIMESTAMP_NS | ElapsedRealtime::HAS_TIME_UNCERTAINTY_NS,
209             .timestampNs = ::android::elapsedRealtimeNano(),
210             // This is an hardcoded value indicating a 1ms of uncertainty between the two clocks.
211             // In an actual implementation provide an estimate of the synchronization uncertainty
212             // or don't set the field.
213             .timeUncertaintyNs = 1020400};
214 
215     if (enableCorrVecOutputs) {
216         aidl::android::hardware::gnss::CorrelationVector correlationVector1 = {
217                 .frequencyOffsetMps = 10,
218                 .samplingWidthM = 30,
219                 .samplingStartM = 0,
220                 .magnitude = {0, 5000, 10000, 5000, 0, 0, 3000, 0}};
221         aidl::android::hardware::gnss::CorrelationVector correlationVector2 = {
222                 .frequencyOffsetMps = 20,
223                 .samplingWidthM = 30,
224                 .samplingStartM = -10,
225                 .magnitude = {0, 3000, 5000, 3000, 0, 0, 1000, 0}};
226         measurement.correlationVectors = {correlationVector1, correlationVector2};
227         measurement.flags |= GnssMeasurement::HAS_CORRELATION_VECTOR;
228     }
229 
230     GnssData gnssData = {
231             .measurements = {measurement}, .clock = clock, .elapsedRealtime = timestamp};
232     return gnssData;
233 }
234 
getMockLocationV2_0()235 V2_0::GnssLocation Utils::getMockLocationV2_0() {
236     const V2_0::ElapsedRealtime timestamp = {
237             .flags = V2_0::ElapsedRealtimeFlags::HAS_TIMESTAMP_NS |
238                      V2_0::ElapsedRealtimeFlags::HAS_TIME_UNCERTAINTY_NS,
239             .timestampNs = static_cast<uint64_t>(::android::elapsedRealtimeNano()),
240             // This is an hardcoded value indicating a 1ms of uncertainty between the two clocks.
241             // In an actual implementation provide an estimate of the synchronization uncertainty
242             // or don't set the field.
243             .timeUncertaintyNs = 1000000};
244 
245     V2_0::GnssLocation location = {.v1_0 = Utils::getMockLocationV1_0(),
246                                    .elapsedRealtime = timestamp};
247     return location;
248 }
249 
getMockLocationV1_0()250 V1_0::GnssLocation Utils::getMockLocationV1_0() {
251     V1_0::GnssLocation location = {
252             .gnssLocationFlags = 0xFF,
253             .latitudeDegrees = gMockLatitudeDegrees,
254             .longitudeDegrees = gMockLongitudeDegrees,
255             .altitudeMeters = gMockAltitudeMeters,
256             .speedMetersPerSec = gMockSpeedMetersPerSec,
257             .bearingDegrees = gMockBearingDegrees,
258             .horizontalAccuracyMeters = kMockHorizontalAccuracyMeters,
259             .verticalAccuracyMeters = kMockVerticalAccuracyMeters,
260             .speedAccuracyMetersPerSecond = kMockSpeedAccuracyMetersPerSecond,
261             .bearingAccuracyDegrees = kMockBearingAccuracyDegrees,
262             .timestamp =
263                     static_cast<int64_t>(kMockTimestamp + ::android::elapsedRealtimeNano() / 1e6)};
264     return location;
265 }
266 
getMockSvInfoListV2_1()267 hidl_vec<GnssSvInfoV2_1> Utils::getMockSvInfoListV2_1() {
268     GnssSvInfoV1_0 gnssSvInfoV1_0 = Utils::getMockSvInfoV1_0(3, V1_0::GnssConstellationType::GPS,
269                                                              32.5, 59.1, 166.5, kGpsL1FreqHz);
270     GnssSvInfoV2_0 gnssSvInfoV2_0 =
271             Utils::getMockSvInfoV2_0(gnssSvInfoV1_0, V2_0::GnssConstellationType::GPS);
272     hidl_vec<GnssSvInfoV2_1> gnssSvInfoList = {
273             Utils::getMockSvInfoV2_1(gnssSvInfoV2_0, 27.5),
274             getMockSvInfoV2_1(
275                     getMockSvInfoV2_0(getMockSvInfoV1_0(5, V1_0::GnssConstellationType::GPS, 27.0,
276                                                         29.0, 56.5, kGpsL1FreqHz),
277                                       V2_0::GnssConstellationType::GPS),
278                     22.0),
279             getMockSvInfoV2_1(
280                     getMockSvInfoV2_0(getMockSvInfoV1_0(17, V1_0::GnssConstellationType::GPS, 30.5,
281                                                         71.0, 77.0, kGpsL5FreqHz),
282                                       V2_0::GnssConstellationType::GPS),
283                     25.5),
284             getMockSvInfoV2_1(
285                     getMockSvInfoV2_0(getMockSvInfoV1_0(26, V1_0::GnssConstellationType::GPS, 24.1,
286                                                         28.0, 253.0, kGpsL5FreqHz),
287                                       V2_0::GnssConstellationType::GPS),
288                     19.1),
289             getMockSvInfoV2_1(
290                     getMockSvInfoV2_0(getMockSvInfoV1_0(5, V1_0::GnssConstellationType::GLONASS,
291                                                         20.5, 11.5, 116.0, kGloG1FreqHz),
292                                       V2_0::GnssConstellationType::GLONASS),
293                     15.5),
294             getMockSvInfoV2_1(
295                     getMockSvInfoV2_0(getMockSvInfoV1_0(17, V1_0::GnssConstellationType::GLONASS,
296                                                         21.5, 28.5, 186.0, kGloG1FreqHz),
297                                       V2_0::GnssConstellationType::GLONASS),
298                     16.5),
299             getMockSvInfoV2_1(
300                     getMockSvInfoV2_0(getMockSvInfoV1_0(18, V1_0::GnssConstellationType::GLONASS,
301                                                         28.3, 38.8, 69.0, kGloG1FreqHz),
302                                       V2_0::GnssConstellationType::GLONASS),
303                     25.3),
304             getMockSvInfoV2_1(
305                     getMockSvInfoV2_0(getMockSvInfoV1_0(10, V1_0::GnssConstellationType::GLONASS,
306                                                         25.0, 66.0, 247.0, kGloG1FreqHz),
307                                       V2_0::GnssConstellationType::GLONASS),
308                     20.0),
309             getMockSvInfoV2_1(
310                     getMockSvInfoV2_0(getMockSvInfoV1_0(3, V1_0::GnssConstellationType::UNKNOWN,
311                                                         22.0, 35.0, 112.0, kIrnssL5FreqHz),
312                                       V2_0::GnssConstellationType::IRNSS),
313                     19.7),
314     };
315     return gnssSvInfoList;
316 }
317 
getMockSvInfoV2_1(GnssSvInfoV2_0 gnssSvInfoV2_0,float basebandCN0DbHz)318 GnssSvInfoV2_1 Utils::getMockSvInfoV2_1(GnssSvInfoV2_0 gnssSvInfoV2_0, float basebandCN0DbHz) {
319     GnssSvInfoV2_1 gnssSvInfoV2_1 = {
320             .v2_0 = gnssSvInfoV2_0,
321             .basebandCN0DbHz = basebandCN0DbHz,
322     };
323     return gnssSvInfoV2_1;
324 }
325 
getMockSvInfoV2_0(GnssSvInfoV1_0 gnssSvInfoV1_0,V2_0::GnssConstellationType type)326 GnssSvInfoV2_0 Utils::getMockSvInfoV2_0(GnssSvInfoV1_0 gnssSvInfoV1_0,
327                                         V2_0::GnssConstellationType type) {
328     GnssSvInfoV2_0 gnssSvInfoV2_0 = {
329             .v1_0 = gnssSvInfoV1_0,
330             .constellation = type,
331     };
332     return gnssSvInfoV2_0;
333 }
334 
getMockSvInfoV1_0(int16_t svid,V1_0::GnssConstellationType type,float cN0DbHz,float elevationDegrees,float azimuthDegrees,float carrierFrequencyHz)335 GnssSvInfoV1_0 Utils::getMockSvInfoV1_0(int16_t svid, V1_0::GnssConstellationType type,
336                                         float cN0DbHz, float elevationDegrees, float azimuthDegrees,
337                                         float carrierFrequencyHz) {
338     GnssSvInfoV1_0 svInfo = {.svid = svid,
339                              .constellation = type,
340                              .cN0Dbhz = cN0DbHz,
341                              .elevationDegrees = elevationDegrees,
342                              .azimuthDegrees = azimuthDegrees,
343                              .carrierFrequencyHz = carrierFrequencyHz,
344                              .svFlag = GnssSvFlags::USED_IN_FIX | GnssSvFlags::HAS_EPHEMERIS_DATA |
345                                        GnssSvFlags::HAS_ALMANAC_DATA |
346                                        GnssSvFlags::HAS_CARRIER_FREQUENCY};
347     return svInfo;
348 }
349 
getMockAntennaInfos()350 hidl_vec<GnssAntennaInfo> Utils::getMockAntennaInfos() {
351     GnssAntennaInfo mockAntennaInfo_1 = {
352             .carrierFrequencyMHz = kGpsL1FreqHz * 1e-6,
353             .phaseCenterOffsetCoordinateMillimeters = Coord{.x = 1,
354                                                             .xUncertainty = 0.1,
355                                                             .y = 2,
356                                                             .yUncertainty = 0.1,
357                                                             .z = 3,
358                                                             .zUncertainty = 0.1},
359             .phaseCenterVariationCorrectionMillimeters =
360                     {
361                             Row{hidl_vec<double>{1, -1, 5, -2, 3, -1}},
362                             Row{hidl_vec<double>{-2, 3, 2, 0, 1, 2}},
363                             Row{hidl_vec<double>{1, 3, 2, -1, -3, 5}},
364                     },
365             .phaseCenterVariationCorrectionUncertaintyMillimeters =
366                     {
367                             Row{hidl_vec<double>{0.1, 0.2, 0.4, 0.1, 0.2, 0.3}},
368                             Row{hidl_vec<double>{0.3, 0.2, 0.3, 0.6, 0.1, 0.1}},
369                             Row{hidl_vec<double>{0.1, 0.1, 0.4, 0.2, 0.5, 0.3}},
370                     },
371             .signalGainCorrectionDbi =
372                     {
373                             Row{hidl_vec<double>{2, -3, 1, -3, 0, -4}},
374                             Row{hidl_vec<double>{1, 0, -4, 1, 3, -2}},
375                             Row{hidl_vec<double>{3, -2, 0, -2, 3, 0}},
376                     },
377             .signalGainCorrectionUncertaintyDbi =
378                     {
379                             Row{hidl_vec<double>{0.3, 0.1, 0.2, 0.6, 0.1, 0.3}},
380                             Row{hidl_vec<double>{0.1, 0.1, 0.5, 0.2, 0.3, 0.1}},
381                             Row{hidl_vec<double>{0.2, 0.4, 0.2, 0.1, 0.1, 0.2}},
382                     },
383     };
384 
385     GnssAntennaInfo mockAntennaInfo_2 = {
386             .carrierFrequencyMHz = kGpsL5FreqHz * 1e-6,
387             .phaseCenterOffsetCoordinateMillimeters = Coord{.x = 5,
388                                                             .xUncertainty = 0.1,
389                                                             .y = 6,
390                                                             .yUncertainty = 0.1,
391                                                             .z = 7,
392                                                             .zUncertainty = 0.1},
393     };
394 
395     hidl_vec<GnssAntennaInfo> mockAntennaInfos = {
396             mockAntennaInfo_1,
397             mockAntennaInfo_2,
398     };
399     return mockAntennaInfos;
400 }
401 
402 }  // namespace common
403 }  // namespace gnss
404 }  // namespace hardware
405 }  // namespace android
406