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 <Utils.h>
18 #include "gtest/gtest.h"
19 
20 namespace android {
21 namespace hardware {
22 namespace gnss {
23 namespace common {
24 
25 using V1_0::GnssConstellationType;
26 using V1_0::GnssLocationFlags;
27 
checkLocation(const GnssLocation & location,bool check_speed,bool check_more_accuracies)28 void Utils::checkLocation(const GnssLocation& location, bool check_speed,
29                           bool check_more_accuracies) {
30     EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_LAT_LONG);
31     EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_ALTITUDE);
32     if (check_speed) {
33         EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_SPEED);
34     }
35     EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_HORIZONTAL_ACCURACY);
36     // New uncertainties available in O must be provided,
37     // at least when paired with modern hardware (2017+)
38     if (check_more_accuracies) {
39         EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_VERTICAL_ACCURACY);
40         if (check_speed) {
41             EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_SPEED_ACCURACY);
42             if (location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING) {
43                 EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING_ACCURACY);
44             }
45         }
46     }
47     EXPECT_GE(location.latitudeDegrees, -90.0);
48     EXPECT_LE(location.latitudeDegrees, 90.0);
49     EXPECT_GE(location.longitudeDegrees, -180.0);
50     EXPECT_LE(location.longitudeDegrees, 180.0);
51     EXPECT_GE(location.altitudeMeters, -1000.0);
52     EXPECT_LE(location.altitudeMeters, 30000.0);
53     if (check_speed) {
54         EXPECT_GE(location.speedMetersPerSec, 0.0);
55         EXPECT_LE(location.speedMetersPerSec, 5.0);  // VTS tests are stationary.
56 
57         // Non-zero speeds must be reported with an associated bearing
58         if (location.speedMetersPerSec > 0.0) {
59             EXPECT_TRUE(location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING);
60         }
61     }
62 
63     /*
64      * Tolerating some especially high values for accuracy estimate, in case of
65      * first fix with especially poor geometry (happens occasionally)
66      */
67     EXPECT_GT(location.horizontalAccuracyMeters, 0.0);
68     EXPECT_LE(location.horizontalAccuracyMeters, 250.0);
69 
70     /*
71      * Some devices may define bearing as -180 to +180, others as 0 to 360.
72      * Both are okay & understandable.
73      */
74     if (location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING) {
75         EXPECT_GE(location.bearingDegrees, -180.0);
76         EXPECT_LE(location.bearingDegrees, 360.0);
77     }
78     if (location.gnssLocationFlags & GnssLocationFlags::HAS_VERTICAL_ACCURACY) {
79         EXPECT_GT(location.verticalAccuracyMeters, 0.0);
80         EXPECT_LE(location.verticalAccuracyMeters, 500.0);
81     }
82     if (location.gnssLocationFlags & GnssLocationFlags::HAS_SPEED_ACCURACY) {
83         EXPECT_GT(location.speedAccuracyMetersPerSecond, 0.0);
84         EXPECT_LE(location.speedAccuracyMetersPerSecond, 50.0);
85     }
86     if (location.gnssLocationFlags & GnssLocationFlags::HAS_BEARING_ACCURACY) {
87         EXPECT_GT(location.bearingAccuracyDegrees, 0.0);
88         EXPECT_LE(location.bearingAccuracyDegrees, 360.0);
89     }
90 
91     // Check timestamp > 1.48e12 (47 years in msec - 1970->2017+)
92     EXPECT_GT(location.timestamp, 1.48e12);
93 }
94 
getMockMeasurementCorrections()95 const MeasurementCorrections Utils::getMockMeasurementCorrections() {
96     ReflectingPlane reflectingPlane = {
97             .latitudeDegrees = 37.4220039,
98             .longitudeDegrees = -122.0840991,
99             .altitudeMeters = 250.35,
100             .azimuthDegrees = 203.0,
101     };
102 
103     SingleSatCorrection singleSatCorrection1 = {
104             .singleSatCorrectionFlags = GnssSingleSatCorrectionFlags::HAS_SAT_IS_LOS_PROBABILITY |
105                                         GnssSingleSatCorrectionFlags::HAS_EXCESS_PATH_LENGTH |
106                                         GnssSingleSatCorrectionFlags::HAS_EXCESS_PATH_LENGTH_UNC |
107                                         GnssSingleSatCorrectionFlags::HAS_REFLECTING_PLANE,
108             .constellation = GnssConstellationType::GPS,
109             .svid = 12,
110             .carrierFrequencyHz = 1.59975e+09,
111             .probSatIsLos = 0.50001,
112             .excessPathLengthMeters = 137.4802,
113             .excessPathLengthUncertaintyMeters = 25.5,
114             .reflectingPlane = reflectingPlane,
115     };
116     SingleSatCorrection singleSatCorrection2 = {
117             .singleSatCorrectionFlags = GnssSingleSatCorrectionFlags::HAS_SAT_IS_LOS_PROBABILITY |
118                                         GnssSingleSatCorrectionFlags::HAS_EXCESS_PATH_LENGTH |
119                                         GnssSingleSatCorrectionFlags::HAS_EXCESS_PATH_LENGTH_UNC,
120             .constellation = GnssConstellationType::GPS,
121             .svid = 9,
122             .carrierFrequencyHz = 1.59975e+09,
123             .probSatIsLos = 0.873,
124             .excessPathLengthMeters = 26.294,
125             .excessPathLengthUncertaintyMeters = 10.0,
126     };
127 
128     hidl_vec<SingleSatCorrection> singleSatCorrections = {singleSatCorrection1,
129                                                           singleSatCorrection2};
130     MeasurementCorrections mockCorrections = {
131             .latitudeDegrees = 37.4219999,
132             .longitudeDegrees = -122.0840575,
133             .altitudeMeters = 30.60062531,
134             .horizontalPositionUncertaintyMeters = 9.23542,
135             .verticalPositionUncertaintyMeters = 15.02341,
136             .toaGpsNanosecondsOfWeek = 2935633453L,
137             .satCorrections = singleSatCorrections,
138     };
139     return mockCorrections;
140 }
141 
142 /*
143  * MapConstellationType:
144  * Given a GnssConstellationType_2_0 type constellation, maps to its equivalent
145  * GnssConstellationType_1_0 type constellation. For constellations that do not have
146  * an equivalent value, maps to GnssConstellationType_1_0::UNKNOWN
147  */
mapConstellationType(GnssConstellationType_2_0 constellation)148 GnssConstellationType_1_0 Utils::mapConstellationType(GnssConstellationType_2_0 constellation) {
149     switch (constellation) {
150         case GnssConstellationType_2_0::GPS:
151             return GnssConstellationType_1_0::GPS;
152         case GnssConstellationType_2_0::SBAS:
153             return GnssConstellationType_1_0::SBAS;
154         case GnssConstellationType_2_0::GLONASS:
155             return GnssConstellationType_1_0::GLONASS;
156         case GnssConstellationType_2_0::QZSS:
157             return GnssConstellationType_1_0::QZSS;
158         case GnssConstellationType_2_0::BEIDOU:
159             return GnssConstellationType_1_0::BEIDOU;
160         case GnssConstellationType_2_0::GALILEO:
161             return GnssConstellationType_1_0::GALILEO;
162         default:
163             return GnssConstellationType_1_0::UNKNOWN;
164     }
165 }
166 
167 }  // namespace common
168 }  // namespace gnss
169 }  // namespace hardware
170 }  // namespace android
171