Hridya Valsaraju | 29dc1e0 | 2016-10-21 14:41:12 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #define LOG_TAG "GnssHAL_GnssMeasurementInterface" |
| 18 | |
| 19 | #include "GnssMeasurement.h" |
| 20 | |
| 21 | namespace android { |
| 22 | namespace hardware { |
| 23 | namespace gnss { |
| 24 | namespace V1_0 { |
| 25 | namespace implementation { |
| 26 | |
| 27 | sp<IGnssMeasurementCallback> GnssMeasurement::sGnssMeasureCbIface = nullptr; |
| 28 | GpsMeasurementCallbacks GnssMeasurement::sGnssMeasurementCbs = { |
| 29 | .size = sizeof(GpsMeasurementCallbacks), |
| 30 | .measurement_callback = gpsMeasurementCb, |
| 31 | .gnss_measurement_callback = gnssMeasurementCb |
| 32 | }; |
| 33 | |
| 34 | GnssMeasurement::GnssMeasurement(const GpsMeasurementInterface* gpsMeasurementIface) |
| 35 | : mGnssMeasureIface(gpsMeasurementIface) {} |
| 36 | |
| 37 | void GnssMeasurement::gnssMeasurementCb(LegacyGnssData* legacyGnssData) { |
| 38 | if (sGnssMeasureCbIface == nullptr) { |
| 39 | ALOGE("%s: GNSSMeasurement Callback Interface configured incorrectly", __func__); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | if (legacyGnssData == nullptr) { |
| 44 | ALOGE("%s: Invalid GnssData from GNSS HAL", __func__); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | IGnssMeasurementCallback::GnssData gnssData; |
| 49 | gnssData.measurementCount = std::min(legacyGnssData->measurement_count, |
| 50 | static_cast<size_t>(GnssMax::SVS_COUNT)); |
| 51 | |
| 52 | for (size_t i = 0; i < gnssData.measurementCount; i++) { |
| 53 | auto entry = legacyGnssData->measurements[i]; |
| 54 | gnssData.measurements[i] = { |
| 55 | .flags = static_cast<IGnssMeasurementCallback::GnssMeasurementFlags>(entry.flags), |
| 56 | .svid = entry.svid, |
| 57 | .constellation = static_cast<GnssConstellationType>(entry.constellation), |
| 58 | .timeOffsetNs = entry.time_offset_ns, |
| 59 | .state = static_cast<IGnssMeasurementCallback::GnssMeasurementState>(entry.state), |
| 60 | .receivedSvTimeInNs = entry.received_sv_time_in_ns, |
| 61 | .receivedSvTimeUncertaintyInNs = entry.received_sv_time_uncertainty_in_ns, |
| 62 | .cN0DbHz = entry.c_n0_dbhz, |
| 63 | .pseudorangeRateMps = entry.pseudorange_rate_mps, |
| 64 | .pseudorangeRateUncertaintyMps = entry.pseudorange_rate_uncertainty_mps, |
| 65 | .accumulatedDeltaRangeState = static_cast<IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState>( |
| 66 | entry.accumulated_delta_range_state), |
| 67 | .accumulatedDeltaRangeM = entry.accumulated_delta_range_m, |
| 68 | .accumulatedDeltaRangeUncertaintyM = entry.accumulated_delta_range_uncertainty_m, |
| 69 | .carrierFrequencyHz = entry.carrier_frequency_hz, |
| 70 | .carrierCycles = entry.carrier_cycles, |
| 71 | .carrierPhase = entry.carrier_phase, |
| 72 | .carrierPhaseUncertainty = entry.carrier_phase_uncertainty, |
| 73 | .multipathIndicator = static_cast<IGnssMeasurementCallback::GnssMultipathIndicator>( |
| 74 | entry.multipath_indicator), |
| 75 | .snrDb = entry.snr_db |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | auto clockVal = legacyGnssData->clock; |
| 80 | gnssData.clock = { |
| 81 | .gnssClockFlags = static_cast<IGnssMeasurementCallback::GnssClockFlags>(clockVal.flags), |
| 82 | .leapSecond = clockVal.leap_second, |
| 83 | .timeNs = clockVal.time_ns, |
| 84 | .timeUncertaintyNs = clockVal.time_uncertainty_ns, |
| 85 | .fullBiasNs = clockVal.full_bias_ns, |
| 86 | .biasNs = clockVal.bias_ns, |
| 87 | .biasUncertaintyNs = clockVal.bias_uncertainty_ns, |
| 88 | .driftNsps = clockVal.drift_nsps, |
| 89 | .driftUncertaintyNsps = clockVal.drift_uncertainty_nsps, |
| 90 | .hwClockDiscontinuityCount = clockVal.hw_clock_discontinuity_count |
| 91 | }; |
| 92 | |
| 93 | sGnssMeasureCbIface->GnssMeasurementCb(gnssData); |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * The code in the following method has been moved here from GnssLocationProvider. |
| 98 | * It converts GpsData to GnssData. This code is no longer required in |
| 99 | * GnssLocationProvider since GpsData is deprecated and no longer part of the |
| 100 | * GNSS interface. |
| 101 | */ |
| 102 | void GnssMeasurement::gpsMeasurementCb(GpsData* gpsData) { |
| 103 | if (sGnssMeasureCbIface == nullptr) { |
| 104 | ALOGE("%s: GNSSMeasurement Callback Interface configured incorrectly", __func__); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | if (gpsData == nullptr) { |
| 109 | ALOGE("%s: Invalid GpsData from GNSS HAL", __func__); |
| 110 | return; |
| 111 | } |
| 112 | |
| 113 | IGnssMeasurementCallback::GnssData gnssData; |
| 114 | gnssData.measurementCount = std::min(gpsData->measurement_count, |
| 115 | static_cast<size_t>(GnssMax::SVS_COUNT)); |
| 116 | |
| 117 | |
| 118 | for (size_t i = 0; i < gnssData.measurementCount; i++) { |
| 119 | auto entry = gpsData->measurements[i]; |
| 120 | gnssData.measurements[i].flags = |
| 121 | static_cast<IGnssMeasurementCallback::GnssMeasurementFlags>( |
| 122 | entry.flags); |
| 123 | gnssData.measurements[i].svid = static_cast<int32_t>(entry.prn); |
| 124 | if (entry.prn >= 1 && entry.prn <= 32) { |
| 125 | gnssData.measurements[i].constellation = GnssConstellationType::GPS; |
| 126 | } else { |
| 127 | gnssData.measurements[i].constellation = |
| 128 | GnssConstellationType::UNKNOWN; |
| 129 | } |
| 130 | |
| 131 | gnssData.measurements[i].timeOffsetNs = entry.time_offset_ns; |
| 132 | gnssData.measurements[i].state = |
| 133 | static_cast<IGnssMeasurementCallback::GnssMeasurementState>( |
| 134 | entry.state); |
| 135 | gnssData.measurements[i].receivedSvTimeInNs = entry.received_gps_tow_ns; |
| 136 | gnssData.measurements[i].receivedSvTimeUncertaintyInNs = |
| 137 | entry.received_gps_tow_uncertainty_ns; |
| 138 | gnssData.measurements[i].cN0DbHz = entry.c_n0_dbhz; |
| 139 | gnssData.measurements[i].pseudorangeRateMps = entry.pseudorange_rate_mps; |
| 140 | gnssData.measurements[i].pseudorangeRateUncertaintyMps = |
| 141 | entry.pseudorange_rate_uncertainty_mps; |
| 142 | gnssData.measurements[i].accumulatedDeltaRangeState = |
| 143 | static_cast<IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState>( |
| 144 | entry.accumulated_delta_range_state); |
| 145 | gnssData.measurements[i].accumulatedDeltaRangeM = |
| 146 | entry.accumulated_delta_range_m; |
| 147 | gnssData.measurements[i].accumulatedDeltaRangeUncertaintyM = |
| 148 | entry.accumulated_delta_range_uncertainty_m; |
| 149 | |
| 150 | if (entry.flags & GNSS_MEASUREMENT_HAS_CARRIER_FREQUENCY) { |
| 151 | gnssData.measurements[i].carrierFrequencyHz = entry.carrier_frequency_hz; |
| 152 | } else { |
| 153 | gnssData.measurements[i].carrierFrequencyHz = 0; |
| 154 | } |
| 155 | |
| 156 | if (entry.flags & GNSS_MEASUREMENT_HAS_CARRIER_PHASE) { |
| 157 | gnssData.measurements[i].carrierPhase = entry.carrier_phase; |
| 158 | } else { |
| 159 | gnssData.measurements[i].carrierPhase = 0; |
| 160 | } |
| 161 | |
| 162 | if (entry.flags & GNSS_MEASUREMENT_HAS_CARRIER_PHASE_UNCERTAINTY) { |
| 163 | gnssData.measurements[i].carrierPhaseUncertainty = entry.carrier_phase_uncertainty; |
| 164 | } else { |
| 165 | gnssData.measurements[i].carrierPhaseUncertainty = 0; |
| 166 | } |
| 167 | |
| 168 | gnssData.measurements[i].multipathIndicator = |
| 169 | static_cast<IGnssMeasurementCallback::GnssMultipathIndicator>( |
| 170 | entry.multipath_indicator); |
| 171 | |
| 172 | if (entry.flags & GNSS_MEASUREMENT_HAS_SNR) { |
| 173 | gnssData.measurements[i].snrDb = entry.snr_db; |
| 174 | } else { |
| 175 | gnssData.measurements[i].snrDb = 0; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | auto clockVal = gpsData->clock; |
| 180 | static uint32_t discontinuity_count_to_handle_old_clock_type = 0; |
| 181 | auto flags = clockVal.flags; |
| 182 | |
| 183 | gnssData.clock.leapSecond = clockVal.leap_second; |
| 184 | /* |
| 185 | * GnssClock only supports the more effective HW_CLOCK type, so type |
| 186 | * handling and documentation complexity has been removed. To convert the |
| 187 | * old GPS_CLOCK types (active only in a limited number of older devices), |
| 188 | * the GPS time information is handled as an always discontinuous HW clock, |
| 189 | * with the GPS time information put into the full_bias_ns instead - so that |
| 190 | * time_ns - full_bias_ns = local estimate of GPS time. Additionally, the |
| 191 | * sign of full_bias_ns and bias_ns has flipped between GpsClock & |
| 192 | * GnssClock, so that is also handled below. |
| 193 | */ |
| 194 | switch (clockVal.type) { |
| 195 | case GPS_CLOCK_TYPE_UNKNOWN: |
| 196 | // Clock type unsupported. |
| 197 | ALOGE("Unknown clock type provided."); |
| 198 | break; |
| 199 | case GPS_CLOCK_TYPE_LOCAL_HW_TIME: |
| 200 | // Already local hardware time. No need to do anything. |
| 201 | break; |
| 202 | case GPS_CLOCK_TYPE_GPS_TIME: |
| 203 | // GPS time, need to convert. |
| 204 | flags |= GPS_CLOCK_HAS_FULL_BIAS; |
| 205 | clockVal.full_bias_ns = clockVal.time_ns; |
| 206 | clockVal.time_ns = 0; |
| 207 | gnssData.clock.hwClockDiscontinuityCount = |
| 208 | discontinuity_count_to_handle_old_clock_type++; |
| 209 | break; |
| 210 | } |
| 211 | |
| 212 | gnssData.clock.timeNs = clockVal.time_ns; |
| 213 | gnssData.clock.timeUncertaintyNs = clockVal.time_uncertainty_ns; |
| 214 | /* |
| 215 | * Definition of sign for full_bias_ns & bias_ns has been changed since N, |
| 216 | * so flip signs here. |
| 217 | */ |
| 218 | gnssData.clock.fullBiasNs = -(clockVal.full_bias_ns); |
| 219 | gnssData.clock.biasNs = -(clockVal.bias_ns); |
| 220 | gnssData.clock.biasUncertaintyNs = clockVal.bias_uncertainty_ns; |
| 221 | gnssData.clock.driftNsps = clockVal.drift_nsps; |
| 222 | gnssData.clock.driftUncertaintyNsps = clockVal.drift_uncertainty_nsps; |
| 223 | gnssData.clock.gnssClockFlags = |
| 224 | static_cast<IGnssMeasurementCallback::GnssClockFlags>(clockVal.flags); |
| 225 | |
| 226 | sGnssMeasureCbIface->GnssMeasurementCb(gnssData); |
| 227 | } |
| 228 | |
| 229 | // Methods from ::android::hardware::gnss::V1_0::IGnssMeasurement follow. |
| 230 | Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback( |
| 231 | const sp<IGnssMeasurementCallback>& callback) { |
| 232 | if (mGnssMeasureIface == nullptr) { |
| 233 | ALOGE("%s: GnssMeasure interface is unavailable", __func__); |
| 234 | return GnssMeasurementStatus::ERROR_GENERIC; |
| 235 | } |
| 236 | sGnssMeasureCbIface = callback; |
| 237 | |
| 238 | return static_cast<GnssMeasurement::GnssMeasurementStatus>( |
| 239 | mGnssMeasureIface->init(&sGnssMeasurementCbs)); |
| 240 | } |
| 241 | |
| 242 | Return<void> GnssMeasurement::close() { |
| 243 | if (mGnssMeasureIface == nullptr) { |
| 244 | ALOGE("%s: GnssMeasure interface is unavailable", __func__); |
| 245 | } else { |
| 246 | mGnssMeasureIface->close(); |
| 247 | } |
| 248 | return Void(); |
| 249 | } |
| 250 | |
| 251 | } // namespace implementation |
| 252 | } // namespace V1_0 |
| 253 | } // namespace gnss |
| 254 | } // namespace hardware |
| 255 | } // namespace android |