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 <android/hardware/gnss/BnGnss.h>
19 #include <android/hardware/gnss/IGnss.h>
20 #include "gtest/gtest.h"
21
22 #include <cutils/properties.h>
23 #include <utils/SystemClock.h>
24
25 namespace android {
26 namespace hardware {
27 namespace gnss {
28 namespace common {
29
30 using android::hardware::gnss::ElapsedRealtime;
31 using android::hardware::gnss::GnssLocation;
32
33 using namespace measurement_corrections::V1_0;
34 using V1_0::GnssLocationFlags;
35
36 using MeasurementCorrectionsAidl =
37 android::hardware::gnss::measurement_corrections::MeasurementCorrections;
38 using ReflectingPlaneAidl = android::hardware::gnss::measurement_corrections::ReflectingPlane;
39 using SingleSatCorrectionAidl =
40 android::hardware::gnss::measurement_corrections::SingleSatCorrection;
41 using ExcessPathInfo = SingleSatCorrectionAidl::ExcessPathInfo;
42
43 template <>
getLocationTimestampMillis(const android::hardware::gnss::GnssLocation & location)44 int64_t Utils::getLocationTimestampMillis(const android::hardware::gnss::GnssLocation& location) {
45 return location.timestampMillis;
46 }
47
48 template <>
getLocationTimestampMillis(const V1_0::GnssLocation & location)49 int64_t Utils::getLocationTimestampMillis(const V1_0::GnssLocation& location) {
50 return location.timestamp;
51 }
52
53 template <>
checkLocationElapsedRealtime(const V1_0::GnssLocation &)54 void Utils::checkLocationElapsedRealtime(const V1_0::GnssLocation&) {}
55
56 template <>
checkLocationElapsedRealtime(const android::hardware::gnss::GnssLocation & location)57 void Utils::checkLocationElapsedRealtime(const android::hardware::gnss::GnssLocation& location) {
58 checkElapsedRealtime(location.elapsedRealtime);
59 }
60
checkElapsedRealtime(const ElapsedRealtime & elapsedRealtime)61 void Utils::checkElapsedRealtime(const ElapsedRealtime& elapsedRealtime) {
62 ASSERT_TRUE(elapsedRealtime.flags >= 0 &&
63 elapsedRealtime.flags <= (ElapsedRealtime::HAS_TIMESTAMP_NS |
64 ElapsedRealtime::HAS_TIME_UNCERTAINTY_NS));
65 if (elapsedRealtime.flags & ElapsedRealtime::HAS_TIMESTAMP_NS) {
66 ASSERT_TRUE(elapsedRealtime.timestampNs > 0);
67 }
68 if (elapsedRealtime.flags & ElapsedRealtime::HAS_TIME_UNCERTAINTY_NS) {
69 ASSERT_TRUE(elapsedRealtime.timeUncertaintyNs > 0);
70 }
71 }
72
getMockLocation(double latitudeDegrees,double longitudeDegrees,double horizontalAccuracyMeters)73 const GnssLocation Utils::getMockLocation(double latitudeDegrees, double longitudeDegrees,
74 double horizontalAccuracyMeters) {
75 ElapsedRealtime elapsedRealtime;
76 elapsedRealtime.flags =
77 ElapsedRealtime::HAS_TIMESTAMP_NS | ElapsedRealtime::HAS_TIME_UNCERTAINTY_NS;
78 elapsedRealtime.timestampNs = ::android::elapsedRealtimeNano();
79 elapsedRealtime.timeUncertaintyNs = 1000;
80 GnssLocation location;
81 location.gnssLocationFlags = 0xFF;
82 location.latitudeDegrees = latitudeDegrees;
83 location.longitudeDegrees = longitudeDegrees;
84 location.altitudeMeters = 500.0;
85 location.speedMetersPerSec = 0.0;
86 location.bearingDegrees = 0.0;
87 location.horizontalAccuracyMeters = horizontalAccuracyMeters;
88 location.verticalAccuracyMeters = 1000.0;
89 location.speedAccuracyMetersPerSecond = 1000.0;
90 location.bearingAccuracyDegrees = 90.0;
91 location.timestampMillis =
92 static_cast<int64_t>(kMockTimestamp + ::android::elapsedRealtimeNano() * 1e-6);
93 location.elapsedRealtime = elapsedRealtime;
94 return location;
95 }
96
getMockMeasurementCorrections()97 const MeasurementCorrections Utils::getMockMeasurementCorrections() {
98 ReflectingPlane reflectingPlane = {
99 .latitudeDegrees = 37.4220039,
100 .longitudeDegrees = -122.0840991,
101 .altitudeMeters = 250.35,
102 .azimuthDegrees = 203.0,
103 };
104
105 SingleSatCorrection singleSatCorrection1 = {
106 .singleSatCorrectionFlags = GnssSingleSatCorrectionFlags::HAS_SAT_IS_LOS_PROBABILITY |
107 GnssSingleSatCorrectionFlags::HAS_EXCESS_PATH_LENGTH |
108 GnssSingleSatCorrectionFlags::HAS_EXCESS_PATH_LENGTH_UNC |
109 GnssSingleSatCorrectionFlags::HAS_REFLECTING_PLANE,
110 .constellation = V1_0::GnssConstellationType::GPS,
111 .svid = 12,
112 .carrierFrequencyHz = 1.59975e+09,
113 .probSatIsLos = 0.50001,
114 .excessPathLengthMeters = 137.4802,
115 .excessPathLengthUncertaintyMeters = 25.5,
116 .reflectingPlane = reflectingPlane,
117 };
118 SingleSatCorrection singleSatCorrection2 = {
119 .singleSatCorrectionFlags = GnssSingleSatCorrectionFlags::HAS_SAT_IS_LOS_PROBABILITY |
120 GnssSingleSatCorrectionFlags::HAS_EXCESS_PATH_LENGTH |
121 GnssSingleSatCorrectionFlags::HAS_EXCESS_PATH_LENGTH_UNC,
122
123 .constellation = V1_0::GnssConstellationType::GPS,
124 .svid = 9,
125 .carrierFrequencyHz = 1.59975e+09,
126 .probSatIsLos = 0.873,
127 .excessPathLengthMeters = 26.294,
128 .excessPathLengthUncertaintyMeters = 10.0,
129 };
130
131 hidl_vec<SingleSatCorrection> singleSatCorrections = {singleSatCorrection1,
132 singleSatCorrection2};
133 MeasurementCorrections mockCorrections = {
134 .latitudeDegrees = 37.4219999,
135 .longitudeDegrees = -122.0840575,
136 .altitudeMeters = 30.60062531,
137 .horizontalPositionUncertaintyMeters = 9.23542,
138 .verticalPositionUncertaintyMeters = 15.02341,
139 .toaGpsNanosecondsOfWeek = 2935633453L,
140 .satCorrections = singleSatCorrections,
141 };
142 return mockCorrections;
143 }
144
145 const measurement_corrections::V1_1::MeasurementCorrections
getMockMeasurementCorrections_1_1()146 Utils::getMockMeasurementCorrections_1_1() {
147 MeasurementCorrections mockCorrections_1_0 = getMockMeasurementCorrections();
148
149 measurement_corrections::V1_1::SingleSatCorrection singleSatCorrection1 = {
150 .v1_0 = mockCorrections_1_0.satCorrections[0],
151 .constellation = V2_0::GnssConstellationType::IRNSS,
152 };
153 measurement_corrections::V1_1::SingleSatCorrection singleSatCorrection2 = {
154 .v1_0 = mockCorrections_1_0.satCorrections[1],
155 .constellation = V2_0::GnssConstellationType::IRNSS,
156 };
157
158 mockCorrections_1_0.satCorrections[0].constellation = V1_0::GnssConstellationType::UNKNOWN;
159 mockCorrections_1_0.satCorrections[1].constellation = V1_0::GnssConstellationType::UNKNOWN;
160
161 hidl_vec<measurement_corrections::V1_1::SingleSatCorrection> singleSatCorrections = {
162 singleSatCorrection1, singleSatCorrection2};
163
164 measurement_corrections::V1_1::MeasurementCorrections 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 namespace {
createExcessPathInfo(float excessPathLengthMeters,float excessPathLengthUncertaintyMeters,const ReflectingPlaneAidl * reflectingPlane,float attenuationDb)175 const ExcessPathInfo createExcessPathInfo(float excessPathLengthMeters,
176 float excessPathLengthUncertaintyMeters,
177 const ReflectingPlaneAidl* reflectingPlane,
178 float attenuationDb) {
179 ExcessPathInfo excessPathInfo;
180 excessPathInfo.excessPathInfoFlags =
181 ExcessPathInfo::EXCESS_PATH_INFO_HAS_EXCESS_PATH_LENGTH |
182 ExcessPathInfo::EXCESS_PATH_INFO_HAS_EXCESS_PATH_LENGTH_UNC |
183 ExcessPathInfo::EXCESS_PATH_INFO_HAS_ATTENUATION |
184 (reflectingPlane == nullptr ? 0
185 : ExcessPathInfo::EXCESS_PATH_INFO_HAS_REFLECTING_PLANE);
186 excessPathInfo.excessPathLengthMeters = excessPathLengthMeters;
187 excessPathInfo.excessPathLengthUncertaintyMeters = excessPathLengthUncertaintyMeters;
188 if (reflectingPlane != nullptr) {
189 excessPathInfo.reflectingPlane = *reflectingPlane;
190 }
191 excessPathInfo.attenuationDb = attenuationDb;
192 return excessPathInfo;
193 }
194 } // anonymous namespace
195
getMockMeasurementCorrections_aidl()196 const MeasurementCorrectionsAidl Utils::getMockMeasurementCorrections_aidl() {
197 ReflectingPlaneAidl reflectingPlane;
198 reflectingPlane.latitudeDegrees = 37.4220039;
199 reflectingPlane.longitudeDegrees = -122.0840991;
200 reflectingPlane.altitudeMeters = 250.35;
201 reflectingPlane.reflectingPlaneAzimuthDegrees = 203.0;
202
203 SingleSatCorrectionAidl singleSatCorrection1;
204 singleSatCorrection1.singleSatCorrectionFlags =
205 SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_SAT_IS_LOS_PROBABILITY |
206 SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_COMBINED_EXCESS_PATH_LENGTH |
207 SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_COMBINED_EXCESS_PATH_LENGTH_UNC |
208 SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_COMBINED_ATTENUATION;
209 singleSatCorrection1.constellation = android::hardware::gnss::GnssConstellationType::GPS;
210 singleSatCorrection1.svid = 12;
211 singleSatCorrection1.carrierFrequencyHz = 1.59975e+09;
212 singleSatCorrection1.probSatIsLos = 0.50001;
213 singleSatCorrection1.combinedExcessPathLengthMeters = 203.5;
214 singleSatCorrection1.combinedExcessPathLengthUncertaintyMeters = 59.1;
215 singleSatCorrection1.combinedAttenuationDb = -4.3;
216 singleSatCorrection1.excessPathInfos.push_back(
217 createExcessPathInfo(137.4, 25.5, &reflectingPlane, -3.5));
218 singleSatCorrection1.excessPathInfos.push_back(
219 createExcessPathInfo(296.3, 87.2, &reflectingPlane, -5.1));
220
221 SingleSatCorrectionAidl singleSatCorrection2;
222 singleSatCorrection2.singleSatCorrectionFlags =
223 SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_SAT_IS_LOS_PROBABILITY |
224 SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_COMBINED_EXCESS_PATH_LENGTH |
225 SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_COMBINED_EXCESS_PATH_LENGTH_UNC |
226 SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_COMBINED_ATTENUATION;
227 singleSatCorrection2.constellation = GnssConstellationType::GPS;
228 singleSatCorrection2.svid = 9;
229 singleSatCorrection2.carrierFrequencyHz = 1.59975e+09;
230 singleSatCorrection2.probSatIsLos = 0.873;
231 singleSatCorrection2.combinedExcessPathLengthMeters = 26.294;
232 singleSatCorrection2.combinedExcessPathLengthUncertaintyMeters = 10.0;
233 singleSatCorrection2.combinedAttenuationDb = -0.5;
234 singleSatCorrection2.excessPathInfos.push_back(
235 createExcessPathInfo(26.294, 10.0, nullptr, -0.5));
236
237 std::vector<SingleSatCorrectionAidl> singleSatCorrections = {singleSatCorrection1,
238 singleSatCorrection2};
239 MeasurementCorrectionsAidl mockCorrections;
240 mockCorrections.latitudeDegrees = 37.4219999;
241 mockCorrections.longitudeDegrees = -122.0840575;
242 mockCorrections.altitudeMeters = 30.60062531;
243 mockCorrections.horizontalPositionUncertaintyMeters = 9.23542;
244 mockCorrections.verticalPositionUncertaintyMeters = 15.02341;
245 mockCorrections.toaGpsNanosecondsOfWeek = 2935633453L;
246 mockCorrections.hasEnvironmentBearing = true;
247 mockCorrections.environmentBearingDegrees = 45.0;
248 mockCorrections.environmentBearingUncertaintyDegrees = 4.0;
249 mockCorrections.satCorrections = singleSatCorrections;
250
251 return mockCorrections;
252 }
253
254 /*
255 * MapConstellationType:
256 * Given a GnssConstellationType_2_0 type constellation, maps to its equivalent
257 * GnssConstellationType_1_0 type constellation. For constellations that do not have
258 * an equivalent value, maps to GnssConstellationType_1_0::UNKNOWN
259 */
mapConstellationType(V2_0::GnssConstellationType constellation)260 V1_0::GnssConstellationType Utils::mapConstellationType(V2_0::GnssConstellationType constellation) {
261 switch (constellation) {
262 case V2_0::GnssConstellationType::GPS:
263 return V1_0::GnssConstellationType::GPS;
264 case V2_0::GnssConstellationType::SBAS:
265 return V1_0::GnssConstellationType::SBAS;
266 case V2_0::GnssConstellationType::GLONASS:
267 return V1_0::GnssConstellationType::GLONASS;
268 case V2_0::GnssConstellationType::QZSS:
269 return V1_0::GnssConstellationType::QZSS;
270 case V2_0::GnssConstellationType::BEIDOU:
271 return V1_0::GnssConstellationType::BEIDOU;
272 case V2_0::GnssConstellationType::GALILEO:
273 return V1_0::GnssConstellationType::GALILEO;
274 default:
275 return V1_0::GnssConstellationType::UNKNOWN;
276 }
277 }
278
isAutomotiveDevice()279 bool Utils::isAutomotiveDevice() {
280 char buffer[PROPERTY_VALUE_MAX] = {0};
281 property_get("ro.hardware.type", buffer, "");
282 return strncmp(buffer, "automotive", PROPERTY_VALUE_MAX) == 0;
283 }
284
285 } // namespace common
286 } // namespace gnss
287 } // namespace hardware
288 } // namespace android
289