blob: 67f6d8d9425973a4748c905864465c9d88409ac9 [file] [log] [blame]
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -07001/*
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
21namespace android {
22namespace hardware {
23namespace gnss {
24namespace V1_0 {
25namespace implementation {
26
27sp<IGnssMeasurementCallback> GnssMeasurement::sGnssMeasureCbIface = nullptr;
28GpsMeasurementCallbacks GnssMeasurement::sGnssMeasurementCbs = {
29 .size = sizeof(GpsMeasurementCallbacks),
30 .measurement_callback = gpsMeasurementCb,
31 .gnss_measurement_callback = gnssMeasurementCb
32};
33
34GnssMeasurement::GnssMeasurement(const GpsMeasurementInterface* gpsMeasurementIface)
35 : mGnssMeasureIface(gpsMeasurementIface) {}
36
37void 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];
gomoc3d92782017-01-11 14:04:21 -080054 auto state = static_cast<GnssMeasurementState>(entry.state);
55 if (state & IGnssMeasurementCallback::GnssMeasurementState::STATE_TOW_DECODED) {
56 state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_TOW_KNOWN;
57 }
58 if (state & IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_TOD_DECODED) {
59 state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_TOD_KNOWN;
60 }
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -070061 gnssData.measurements[i] = {
Yifan Hong7037fdb2016-12-05 17:16:09 -080062 .flags = entry.flags,
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -070063 .svid = entry.svid,
64 .constellation = static_cast<GnssConstellationType>(entry.constellation),
65 .timeOffsetNs = entry.time_offset_ns,
gomoc3d92782017-01-11 14:04:21 -080066 .state = state,
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -070067 .receivedSvTimeInNs = entry.received_sv_time_in_ns,
68 .receivedSvTimeUncertaintyInNs = entry.received_sv_time_uncertainty_in_ns,
69 .cN0DbHz = entry.c_n0_dbhz,
70 .pseudorangeRateMps = entry.pseudorange_rate_mps,
71 .pseudorangeRateUncertaintyMps = entry.pseudorange_rate_uncertainty_mps,
Yifan Hong7037fdb2016-12-05 17:16:09 -080072 .accumulatedDeltaRangeState = entry.accumulated_delta_range_state,
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -070073 .accumulatedDeltaRangeM = entry.accumulated_delta_range_m,
74 .accumulatedDeltaRangeUncertaintyM = entry.accumulated_delta_range_uncertainty_m,
75 .carrierFrequencyHz = entry.carrier_frequency_hz,
76 .carrierCycles = entry.carrier_cycles,
77 .carrierPhase = entry.carrier_phase,
78 .carrierPhaseUncertainty = entry.carrier_phase_uncertainty,
79 .multipathIndicator = static_cast<IGnssMeasurementCallback::GnssMultipathIndicator>(
80 entry.multipath_indicator),
81 .snrDb = entry.snr_db
82 };
83 }
84
85 auto clockVal = legacyGnssData->clock;
86 gnssData.clock = {
Yifan Hong7037fdb2016-12-05 17:16:09 -080087 .gnssClockFlags = clockVal.flags,
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -070088 .leapSecond = clockVal.leap_second,
89 .timeNs = clockVal.time_ns,
90 .timeUncertaintyNs = clockVal.time_uncertainty_ns,
91 .fullBiasNs = clockVal.full_bias_ns,
92 .biasNs = clockVal.bias_ns,
93 .biasUncertaintyNs = clockVal.bias_uncertainty_ns,
94 .driftNsps = clockVal.drift_nsps,
95 .driftUncertaintyNsps = clockVal.drift_uncertainty_nsps,
96 .hwClockDiscontinuityCount = clockVal.hw_clock_discontinuity_count
97 };
98
99 sGnssMeasureCbIface->GnssMeasurementCb(gnssData);
100}
101
102/*
103 * The code in the following method has been moved here from GnssLocationProvider.
104 * It converts GpsData to GnssData. This code is no longer required in
105 * GnssLocationProvider since GpsData is deprecated and no longer part of the
106 * GNSS interface.
107 */
108void GnssMeasurement::gpsMeasurementCb(GpsData* gpsData) {
109 if (sGnssMeasureCbIface == nullptr) {
110 ALOGE("%s: GNSSMeasurement Callback Interface configured incorrectly", __func__);
111 return;
112 }
113
114 if (gpsData == nullptr) {
115 ALOGE("%s: Invalid GpsData from GNSS HAL", __func__);
116 return;
117 }
118
119 IGnssMeasurementCallback::GnssData gnssData;
120 gnssData.measurementCount = std::min(gpsData->measurement_count,
121 static_cast<size_t>(GnssMax::SVS_COUNT));
122
123
124 for (size_t i = 0; i < gnssData.measurementCount; i++) {
125 auto entry = gpsData->measurements[i];
Yifan Hong7037fdb2016-12-05 17:16:09 -0800126 gnssData.measurements[i].flags = entry.flags;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700127 gnssData.measurements[i].svid = static_cast<int32_t>(entry.prn);
128 if (entry.prn >= 1 && entry.prn <= 32) {
129 gnssData.measurements[i].constellation = GnssConstellationType::GPS;
130 } else {
131 gnssData.measurements[i].constellation =
132 GnssConstellationType::UNKNOWN;
133 }
134
135 gnssData.measurements[i].timeOffsetNs = entry.time_offset_ns;
Yifan Hong7037fdb2016-12-05 17:16:09 -0800136 gnssData.measurements[i].state = entry.state;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700137 gnssData.measurements[i].receivedSvTimeInNs = entry.received_gps_tow_ns;
138 gnssData.measurements[i].receivedSvTimeUncertaintyInNs =
139 entry.received_gps_tow_uncertainty_ns;
140 gnssData.measurements[i].cN0DbHz = entry.c_n0_dbhz;
141 gnssData.measurements[i].pseudorangeRateMps = entry.pseudorange_rate_mps;
142 gnssData.measurements[i].pseudorangeRateUncertaintyMps =
143 entry.pseudorange_rate_uncertainty_mps;
144 gnssData.measurements[i].accumulatedDeltaRangeState =
Yifan Hong7037fdb2016-12-05 17:16:09 -0800145 entry.accumulated_delta_range_state;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700146 gnssData.measurements[i].accumulatedDeltaRangeM =
147 entry.accumulated_delta_range_m;
148 gnssData.measurements[i].accumulatedDeltaRangeUncertaintyM =
149 entry.accumulated_delta_range_uncertainty_m;
150
151 if (entry.flags & GNSS_MEASUREMENT_HAS_CARRIER_FREQUENCY) {
152 gnssData.measurements[i].carrierFrequencyHz = entry.carrier_frequency_hz;
153 } else {
154 gnssData.measurements[i].carrierFrequencyHz = 0;
155 }
156
157 if (entry.flags & GNSS_MEASUREMENT_HAS_CARRIER_PHASE) {
158 gnssData.measurements[i].carrierPhase = entry.carrier_phase;
159 } else {
160 gnssData.measurements[i].carrierPhase = 0;
161 }
162
163 if (entry.flags & GNSS_MEASUREMENT_HAS_CARRIER_PHASE_UNCERTAINTY) {
164 gnssData.measurements[i].carrierPhaseUncertainty = entry.carrier_phase_uncertainty;
165 } else {
166 gnssData.measurements[i].carrierPhaseUncertainty = 0;
167 }
168
169 gnssData.measurements[i].multipathIndicator =
170 static_cast<IGnssMeasurementCallback::GnssMultipathIndicator>(
171 entry.multipath_indicator);
172
173 if (entry.flags & GNSS_MEASUREMENT_HAS_SNR) {
174 gnssData.measurements[i].snrDb = entry.snr_db;
175 } else {
176 gnssData.measurements[i].snrDb = 0;
177 }
178 }
179
180 auto clockVal = gpsData->clock;
181 static uint32_t discontinuity_count_to_handle_old_clock_type = 0;
182 auto flags = clockVal.flags;
183
184 gnssData.clock.leapSecond = clockVal.leap_second;
185 /*
186 * GnssClock only supports the more effective HW_CLOCK type, so type
187 * handling and documentation complexity has been removed. To convert the
188 * old GPS_CLOCK types (active only in a limited number of older devices),
189 * the GPS time information is handled as an always discontinuous HW clock,
190 * with the GPS time information put into the full_bias_ns instead - so that
191 * time_ns - full_bias_ns = local estimate of GPS time. Additionally, the
192 * sign of full_bias_ns and bias_ns has flipped between GpsClock &
193 * GnssClock, so that is also handled below.
194 */
195 switch (clockVal.type) {
196 case GPS_CLOCK_TYPE_UNKNOWN:
197 // Clock type unsupported.
198 ALOGE("Unknown clock type provided.");
199 break;
200 case GPS_CLOCK_TYPE_LOCAL_HW_TIME:
201 // Already local hardware time. No need to do anything.
202 break;
203 case GPS_CLOCK_TYPE_GPS_TIME:
204 // GPS time, need to convert.
205 flags |= GPS_CLOCK_HAS_FULL_BIAS;
206 clockVal.full_bias_ns = clockVal.time_ns;
207 clockVal.time_ns = 0;
208 gnssData.clock.hwClockDiscontinuityCount =
209 discontinuity_count_to_handle_old_clock_type++;
210 break;
211 }
212
213 gnssData.clock.timeNs = clockVal.time_ns;
214 gnssData.clock.timeUncertaintyNs = clockVal.time_uncertainty_ns;
215 /*
216 * Definition of sign for full_bias_ns & bias_ns has been changed since N,
217 * so flip signs here.
218 */
219 gnssData.clock.fullBiasNs = -(clockVal.full_bias_ns);
220 gnssData.clock.biasNs = -(clockVal.bias_ns);
221 gnssData.clock.biasUncertaintyNs = clockVal.bias_uncertainty_ns;
222 gnssData.clock.driftNsps = clockVal.drift_nsps;
223 gnssData.clock.driftUncertaintyNsps = clockVal.drift_uncertainty_nsps;
Yifan Hong7037fdb2016-12-05 17:16:09 -0800224 gnssData.clock.gnssClockFlags = clockVal.flags;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700225
226 sGnssMeasureCbIface->GnssMeasurementCb(gnssData);
227}
228
229// Methods from ::android::hardware::gnss::V1_0::IGnssMeasurement follow.
230Return<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
242Return<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