Yu-Han Yang | a509861 | 2019-02-08 16:22:07 -0800 | [diff] [blame] | 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> |
Edwin Tung | bbf7fb9 | 2021-12-27 12:13:02 +0800 | [diff] [blame] | 18 | #include <android/hardware/gnss/BnGnss.h> |
Yu-Han Yang | 1afbd5f | 2021-11-24 16:39:13 -0800 | [diff] [blame] | 19 | #include <android/hardware/gnss/IGnss.h> |
Yu-Han Yang | a509861 | 2019-02-08 16:22:07 -0800 | [diff] [blame] | 20 | #include "gtest/gtest.h" |
| 21 | |
Tyler Trephan | 0d9206a | 2020-09-28 22:03:18 -0700 | [diff] [blame] | 22 | #include <cutils/properties.h> |
Yu-Han Yang | 4165ed1 | 2022-02-09 14:47:50 -0800 | [diff] [blame^] | 23 | #include <utils/SystemClock.h> |
Tyler Trephan | 0d9206a | 2020-09-28 22:03:18 -0700 | [diff] [blame] | 24 | |
Yu-Han Yang | a509861 | 2019-02-08 16:22:07 -0800 | [diff] [blame] | 25 | namespace android { |
| 26 | namespace hardware { |
| 27 | namespace gnss { |
| 28 | namespace common { |
| 29 | |
Yu-Han Yang | 4165ed1 | 2022-02-09 14:47:50 -0800 | [diff] [blame^] | 30 | using android::hardware::gnss::ElapsedRealtime; |
| 31 | using android::hardware::gnss::GnssLocation; |
| 32 | |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 33 | using namespace measurement_corrections::V1_0; |
Yu-Han Yang | a509861 | 2019-02-08 16:22:07 -0800 | [diff] [blame] | 34 | using V1_0::GnssLocationFlags; |
| 35 | |
Edwin Tung | bbf7fb9 | 2021-12-27 12:13:02 +0800 | [diff] [blame] | 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 | |
Yu-Han Yang | 1afbd5f | 2021-11-24 16:39:13 -0800 | [diff] [blame] | 42 | template <> |
| 43 | int64_t Utils::getLocationTimestampMillis(const android::hardware::gnss::GnssLocation& location) { |
| 44 | return location.timestampMillis; |
| 45 | } |
Yu-Han Yang | a509861 | 2019-02-08 16:22:07 -0800 | [diff] [blame] | 46 | |
Yu-Han Yang | 1afbd5f | 2021-11-24 16:39:13 -0800 | [diff] [blame] | 47 | template <> |
| 48 | int64_t Utils::getLocationTimestampMillis(const V1_0::GnssLocation& location) { |
| 49 | return location.timestamp; |
Yu-Han Yang | a509861 | 2019-02-08 16:22:07 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Yu-Han Yang | 4165ed1 | 2022-02-09 14:47:50 -0800 | [diff] [blame^] | 52 | template <> |
| 53 | void Utils::checkLocationElapsedRealtime(const V1_0::GnssLocation&) {} |
| 54 | |
| 55 | template <> |
| 56 | void Utils::checkLocationElapsedRealtime(const android::hardware::gnss::GnssLocation& location) { |
| 57 | checkElapsedRealtime(location.elapsedRealtime); |
| 58 | } |
| 59 | |
| 60 | void Utils::checkElapsedRealtime(const ElapsedRealtime& elapsedRealtime) { |
| 61 | ASSERT_TRUE(elapsedRealtime.flags >= 0 && |
| 62 | elapsedRealtime.flags <= (ElapsedRealtime::HAS_TIMESTAMP_NS | |
| 63 | ElapsedRealtime::HAS_TIME_UNCERTAINTY_NS)); |
| 64 | if (elapsedRealtime.flags & ElapsedRealtime::HAS_TIMESTAMP_NS) { |
| 65 | ASSERT_TRUE(elapsedRealtime.timestampNs > 0); |
| 66 | } |
| 67 | if (elapsedRealtime.flags & ElapsedRealtime::HAS_TIME_UNCERTAINTY_NS) { |
| 68 | ASSERT_TRUE(elapsedRealtime.timeUncertaintyNs > 0); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | const GnssLocation Utils::getMockLocation(double latitudeDegrees, double longitudeDegrees, |
| 73 | double horizontalAccuracyMeters) { |
| 74 | ElapsedRealtime elapsedRealtime; |
| 75 | elapsedRealtime.flags = |
| 76 | ElapsedRealtime::HAS_TIMESTAMP_NS | ElapsedRealtime::HAS_TIME_UNCERTAINTY_NS; |
| 77 | elapsedRealtime.timestampNs = ::android::elapsedRealtimeNano(); |
| 78 | elapsedRealtime.timeUncertaintyNs = 1000; |
| 79 | GnssLocation location; |
| 80 | location.gnssLocationFlags = 0xFF; |
| 81 | location.latitudeDegrees = latitudeDegrees; |
| 82 | location.longitudeDegrees = longitudeDegrees; |
| 83 | location.altitudeMeters = 500.0; |
| 84 | location.speedMetersPerSec = 0.0; |
| 85 | location.bearingDegrees = 0.0; |
| 86 | location.horizontalAccuracyMeters = horizontalAccuracyMeters; |
| 87 | location.verticalAccuracyMeters = 1000.0; |
| 88 | location.speedAccuracyMetersPerSecond = 1000.0; |
| 89 | location.bearingAccuracyDegrees = 90.0; |
| 90 | location.timestampMillis = |
| 91 | static_cast<int64_t>(kMockTimestamp + ::android::elapsedRealtimeNano() * 1e-6); |
| 92 | location.elapsedRealtime = elapsedRealtime; |
| 93 | return location; |
| 94 | } |
| 95 | |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 96 | const MeasurementCorrections Utils::getMockMeasurementCorrections() { |
Yu-Han Yang | 08642f9 | 2019-03-02 14:22:14 -0800 | [diff] [blame] | 97 | ReflectingPlane reflectingPlane = { |
| 98 | .latitudeDegrees = 37.4220039, |
| 99 | .longitudeDegrees = -122.0840991, |
| 100 | .altitudeMeters = 250.35, |
| 101 | .azimuthDegrees = 203.0, |
| 102 | }; |
| 103 | |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 104 | SingleSatCorrection singleSatCorrection1 = { |
Yu-Han Yang | 08642f9 | 2019-03-02 14:22:14 -0800 | [diff] [blame] | 105 | .singleSatCorrectionFlags = GnssSingleSatCorrectionFlags::HAS_SAT_IS_LOS_PROBABILITY | |
| 106 | GnssSingleSatCorrectionFlags::HAS_EXCESS_PATH_LENGTH | |
| 107 | GnssSingleSatCorrectionFlags::HAS_EXCESS_PATH_LENGTH_UNC | |
| 108 | GnssSingleSatCorrectionFlags::HAS_REFLECTING_PLANE, |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 109 | .constellation = V1_0::GnssConstellationType::GPS, |
Yu-Han Yang | 08642f9 | 2019-03-02 14:22:14 -0800 | [diff] [blame] | 110 | .svid = 12, |
| 111 | .carrierFrequencyHz = 1.59975e+09, |
| 112 | .probSatIsLos = 0.50001, |
| 113 | .excessPathLengthMeters = 137.4802, |
| 114 | .excessPathLengthUncertaintyMeters = 25.5, |
| 115 | .reflectingPlane = reflectingPlane, |
| 116 | }; |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 117 | SingleSatCorrection singleSatCorrection2 = { |
Yu-Han Yang | 08642f9 | 2019-03-02 14:22:14 -0800 | [diff] [blame] | 118 | .singleSatCorrectionFlags = GnssSingleSatCorrectionFlags::HAS_SAT_IS_LOS_PROBABILITY | |
| 119 | GnssSingleSatCorrectionFlags::HAS_EXCESS_PATH_LENGTH | |
| 120 | GnssSingleSatCorrectionFlags::HAS_EXCESS_PATH_LENGTH_UNC, |
Edwin Tung | bbf7fb9 | 2021-12-27 12:13:02 +0800 | [diff] [blame] | 121 | |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 122 | .constellation = V1_0::GnssConstellationType::GPS, |
Yu-Han Yang | 08642f9 | 2019-03-02 14:22:14 -0800 | [diff] [blame] | 123 | .svid = 9, |
| 124 | .carrierFrequencyHz = 1.59975e+09, |
| 125 | .probSatIsLos = 0.873, |
| 126 | .excessPathLengthMeters = 26.294, |
| 127 | .excessPathLengthUncertaintyMeters = 10.0, |
| 128 | }; |
| 129 | |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 130 | hidl_vec<SingleSatCorrection> singleSatCorrections = {singleSatCorrection1, |
| 131 | singleSatCorrection2}; |
| 132 | MeasurementCorrections mockCorrections = { |
Yu-Han Yang | 08642f9 | 2019-03-02 14:22:14 -0800 | [diff] [blame] | 133 | .latitudeDegrees = 37.4219999, |
| 134 | .longitudeDegrees = -122.0840575, |
| 135 | .altitudeMeters = 30.60062531, |
| 136 | .horizontalPositionUncertaintyMeters = 9.23542, |
| 137 | .verticalPositionUncertaintyMeters = 15.02341, |
| 138 | .toaGpsNanosecondsOfWeek = 2935633453L, |
| 139 | .satCorrections = singleSatCorrections, |
| 140 | }; |
| 141 | return mockCorrections; |
| 142 | } |
| 143 | |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 144 | const measurement_corrections::V1_1::MeasurementCorrections |
| 145 | Utils::getMockMeasurementCorrections_1_1() { |
| 146 | MeasurementCorrections mockCorrections_1_0 = getMockMeasurementCorrections(); |
Sasha Kuznetsov | 31eea85 | 2020-01-03 13:06:38 -0800 | [diff] [blame] | 147 | |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 148 | measurement_corrections::V1_1::SingleSatCorrection singleSatCorrection1 = { |
Sasha Kuznetsov | 6678ffd | 2020-02-19 11:55:26 -0800 | [diff] [blame] | 149 | .v1_0 = mockCorrections_1_0.satCorrections[0], |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 150 | .constellation = V2_0::GnssConstellationType::IRNSS, |
Sasha Kuznetsov | 6678ffd | 2020-02-19 11:55:26 -0800 | [diff] [blame] | 151 | }; |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 152 | measurement_corrections::V1_1::SingleSatCorrection singleSatCorrection2 = { |
Sasha Kuznetsov | 6678ffd | 2020-02-19 11:55:26 -0800 | [diff] [blame] | 153 | .v1_0 = mockCorrections_1_0.satCorrections[1], |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 154 | .constellation = V2_0::GnssConstellationType::IRNSS, |
Sasha Kuznetsov | 6678ffd | 2020-02-19 11:55:26 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 157 | mockCorrections_1_0.satCorrections[0].constellation = V1_0::GnssConstellationType::UNKNOWN; |
| 158 | mockCorrections_1_0.satCorrections[1].constellation = V1_0::GnssConstellationType::UNKNOWN; |
Sasha Kuznetsov | 6678ffd | 2020-02-19 11:55:26 -0800 | [diff] [blame] | 159 | |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 160 | hidl_vec<measurement_corrections::V1_1::SingleSatCorrection> singleSatCorrections = { |
| 161 | singleSatCorrection1, singleSatCorrection2}; |
Sasha Kuznetsov | 6678ffd | 2020-02-19 11:55:26 -0800 | [diff] [blame] | 162 | |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 163 | measurement_corrections::V1_1::MeasurementCorrections mockCorrections_1_1 = { |
Sasha Kuznetsov | 31eea85 | 2020-01-03 13:06:38 -0800 | [diff] [blame] | 164 | .v1_0 = mockCorrections_1_0, |
| 165 | .hasEnvironmentBearing = true, |
| 166 | .environmentBearingDegrees = 45.0, |
| 167 | .environmentBearingUncertaintyDegrees = 4.0, |
Sasha Kuznetsov | 6678ffd | 2020-02-19 11:55:26 -0800 | [diff] [blame] | 168 | .satCorrections = singleSatCorrections, |
Sasha Kuznetsov | 31eea85 | 2020-01-03 13:06:38 -0800 | [diff] [blame] | 169 | }; |
| 170 | return mockCorrections_1_1; |
| 171 | } |
| 172 | |
Edwin Tung | bbf7fb9 | 2021-12-27 12:13:02 +0800 | [diff] [blame] | 173 | const MeasurementCorrectionsAidl Utils::getMockMeasurementCorrections_aidl() { |
| 174 | ReflectingPlaneAidl reflectingPlane; |
| 175 | reflectingPlane.latitudeDegrees = 37.4220039; |
| 176 | reflectingPlane.longitudeDegrees = -122.0840991; |
| 177 | reflectingPlane.altitudeMeters = 250.35; |
| 178 | reflectingPlane.azimuthDegrees = 203.0; |
| 179 | |
| 180 | SingleSatCorrectionAidl singleSatCorrection1; |
| 181 | singleSatCorrection1.singleSatCorrectionFlags = |
| 182 | SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_SAT_IS_LOS_PROBABILITY | |
| 183 | SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_EXCESS_PATH_LENGTH | |
| 184 | SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_EXCESS_PATH_LENGTH_UNC | |
| 185 | SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_REFLECTING_PLANE; |
| 186 | singleSatCorrection1.constellation = android::hardware::gnss::GnssConstellationType::GPS; |
| 187 | singleSatCorrection1.svid = 12; |
| 188 | singleSatCorrection1.carrierFrequencyHz = 1.59975e+09; |
| 189 | singleSatCorrection1.probSatIsLos = 0.50001; |
| 190 | singleSatCorrection1.excessPathLengthMeters = 137.4802; |
| 191 | singleSatCorrection1.excessPathLengthUncertaintyMeters = 25.5; |
| 192 | singleSatCorrection1.reflectingPlane = reflectingPlane; |
| 193 | |
| 194 | SingleSatCorrectionAidl singleSatCorrection2; |
| 195 | singleSatCorrection2.singleSatCorrectionFlags = |
| 196 | SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_SAT_IS_LOS_PROBABILITY | |
| 197 | SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_EXCESS_PATH_LENGTH | |
| 198 | SingleSatCorrectionAidl::SINGLE_SAT_CORRECTION_HAS_EXCESS_PATH_LENGTH_UNC; |
| 199 | singleSatCorrection2.constellation = GnssConstellationType::GPS; |
| 200 | singleSatCorrection2.svid = 9; |
| 201 | singleSatCorrection2.carrierFrequencyHz = 1.59975e+09; |
| 202 | singleSatCorrection2.probSatIsLos = 0.873; |
| 203 | singleSatCorrection2.excessPathLengthMeters = 26.294; |
| 204 | singleSatCorrection2.excessPathLengthUncertaintyMeters = 10.0; |
| 205 | |
| 206 | std::vector<SingleSatCorrectionAidl> singleSatCorrections = {singleSatCorrection1, |
| 207 | singleSatCorrection2}; |
| 208 | MeasurementCorrectionsAidl mockCorrections; |
| 209 | mockCorrections.latitudeDegrees = 37.4219999; |
| 210 | mockCorrections.longitudeDegrees = -122.0840575; |
| 211 | mockCorrections.altitudeMeters = 30.60062531; |
| 212 | mockCorrections.horizontalPositionUncertaintyMeters = 9.23542; |
| 213 | mockCorrections.verticalPositionUncertaintyMeters = 15.02341; |
| 214 | mockCorrections.toaGpsNanosecondsOfWeek = 2935633453L; |
| 215 | mockCorrections.hasEnvironmentBearing = true; |
| 216 | mockCorrections.environmentBearingDegrees = 45.0; |
| 217 | mockCorrections.environmentBearingUncertaintyDegrees = 4.0; |
| 218 | mockCorrections.satCorrections = singleSatCorrections; |
| 219 | |
| 220 | return mockCorrections; |
| 221 | } |
| 222 | |
Yu-Han Yang | 4e5ffc2 | 2020-04-17 15:44:59 -0700 | [diff] [blame] | 223 | /* |
| 224 | * MapConstellationType: |
| 225 | * Given a GnssConstellationType_2_0 type constellation, maps to its equivalent |
| 226 | * GnssConstellationType_1_0 type constellation. For constellations that do not have |
| 227 | * an equivalent value, maps to GnssConstellationType_1_0::UNKNOWN |
| 228 | */ |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 229 | V1_0::GnssConstellationType Utils::mapConstellationType(V2_0::GnssConstellationType constellation) { |
Yu-Han Yang | 4e5ffc2 | 2020-04-17 15:44:59 -0700 | [diff] [blame] | 230 | switch (constellation) { |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 231 | case V2_0::GnssConstellationType::GPS: |
| 232 | return V1_0::GnssConstellationType::GPS; |
| 233 | case V2_0::GnssConstellationType::SBAS: |
| 234 | return V1_0::GnssConstellationType::SBAS; |
| 235 | case V2_0::GnssConstellationType::GLONASS: |
| 236 | return V1_0::GnssConstellationType::GLONASS; |
| 237 | case V2_0::GnssConstellationType::QZSS: |
| 238 | return V1_0::GnssConstellationType::QZSS; |
| 239 | case V2_0::GnssConstellationType::BEIDOU: |
| 240 | return V1_0::GnssConstellationType::BEIDOU; |
| 241 | case V2_0::GnssConstellationType::GALILEO: |
| 242 | return V1_0::GnssConstellationType::GALILEO; |
Yu-Han Yang | 4e5ffc2 | 2020-04-17 15:44:59 -0700 | [diff] [blame] | 243 | default: |
Yu-Han Yang | 3a75dc0 | 2021-09-27 01:01:06 -0700 | [diff] [blame] | 244 | return V1_0::GnssConstellationType::UNKNOWN; |
Yu-Han Yang | 4e5ffc2 | 2020-04-17 15:44:59 -0700 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Tyler Trephan | 0d9206a | 2020-09-28 22:03:18 -0700 | [diff] [blame] | 248 | bool Utils::isAutomotiveDevice() { |
| 249 | char buffer[PROPERTY_VALUE_MAX] = {0}; |
| 250 | property_get("ro.hardware.type", buffer, ""); |
| 251 | return strncmp(buffer, "automotive", PROPERTY_VALUE_MAX) == 0; |
| 252 | } |
| 253 | |
Yu-Han Yang | a509861 | 2019-02-08 16:22:07 -0800 | [diff] [blame] | 254 | } // namespace common |
| 255 | } // namespace gnss |
| 256 | } // namespace hardware |
| 257 | } // namespace android |