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