blob: 0d1163637cef8861d073d3b92a45632bf38f322a [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];
54 gnssData.measurements[i] = {
Yifan Hong7037fdb2016-12-05 17:16:09 -080055 .flags = entry.flags,
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -070056 .svid = entry.svid,
57 .constellation = static_cast<GnssConstellationType>(entry.constellation),
58 .timeOffsetNs = entry.time_offset_ns,
Yifan Hong7037fdb2016-12-05 17:16:09 -080059 .state = entry.state,
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -070060 .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,
Yifan Hong7037fdb2016-12-05 17:16:09 -080065 .accumulatedDeltaRangeState = entry.accumulated_delta_range_state,
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -070066 .accumulatedDeltaRangeM = entry.accumulated_delta_range_m,
67 .accumulatedDeltaRangeUncertaintyM = entry.accumulated_delta_range_uncertainty_m,
68 .carrierFrequencyHz = entry.carrier_frequency_hz,
69 .carrierCycles = entry.carrier_cycles,
70 .carrierPhase = entry.carrier_phase,
71 .carrierPhaseUncertainty = entry.carrier_phase_uncertainty,
72 .multipathIndicator = static_cast<IGnssMeasurementCallback::GnssMultipathIndicator>(
73 entry.multipath_indicator),
74 .snrDb = entry.snr_db
75 };
76 }
77
78 auto clockVal = legacyGnssData->clock;
79 gnssData.clock = {
Yifan Hong7037fdb2016-12-05 17:16:09 -080080 .gnssClockFlags = clockVal.flags,
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -070081 .leapSecond = clockVal.leap_second,
82 .timeNs = clockVal.time_ns,
83 .timeUncertaintyNs = clockVal.time_uncertainty_ns,
84 .fullBiasNs = clockVal.full_bias_ns,
85 .biasNs = clockVal.bias_ns,
86 .biasUncertaintyNs = clockVal.bias_uncertainty_ns,
87 .driftNsps = clockVal.drift_nsps,
88 .driftUncertaintyNsps = clockVal.drift_uncertainty_nsps,
89 .hwClockDiscontinuityCount = clockVal.hw_clock_discontinuity_count
90 };
91
92 sGnssMeasureCbIface->GnssMeasurementCb(gnssData);
93}
94
95/*
96 * The code in the following method has been moved here from GnssLocationProvider.
97 * It converts GpsData to GnssData. This code is no longer required in
98 * GnssLocationProvider since GpsData is deprecated and no longer part of the
99 * GNSS interface.
100 */
101void GnssMeasurement::gpsMeasurementCb(GpsData* gpsData) {
102 if (sGnssMeasureCbIface == nullptr) {
103 ALOGE("%s: GNSSMeasurement Callback Interface configured incorrectly", __func__);
104 return;
105 }
106
107 if (gpsData == nullptr) {
108 ALOGE("%s: Invalid GpsData from GNSS HAL", __func__);
109 return;
110 }
111
112 IGnssMeasurementCallback::GnssData gnssData;
113 gnssData.measurementCount = std::min(gpsData->measurement_count,
114 static_cast<size_t>(GnssMax::SVS_COUNT));
115
116
117 for (size_t i = 0; i < gnssData.measurementCount; i++) {
118 auto entry = gpsData->measurements[i];
Yifan Hong7037fdb2016-12-05 17:16:09 -0800119 gnssData.measurements[i].flags = entry.flags;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700120 gnssData.measurements[i].svid = static_cast<int32_t>(entry.prn);
121 if (entry.prn >= 1 && entry.prn <= 32) {
122 gnssData.measurements[i].constellation = GnssConstellationType::GPS;
123 } else {
124 gnssData.measurements[i].constellation =
125 GnssConstellationType::UNKNOWN;
126 }
127
128 gnssData.measurements[i].timeOffsetNs = entry.time_offset_ns;
Yifan Hong7037fdb2016-12-05 17:16:09 -0800129 gnssData.measurements[i].state = entry.state;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700130 gnssData.measurements[i].receivedSvTimeInNs = entry.received_gps_tow_ns;
131 gnssData.measurements[i].receivedSvTimeUncertaintyInNs =
132 entry.received_gps_tow_uncertainty_ns;
133 gnssData.measurements[i].cN0DbHz = entry.c_n0_dbhz;
134 gnssData.measurements[i].pseudorangeRateMps = entry.pseudorange_rate_mps;
135 gnssData.measurements[i].pseudorangeRateUncertaintyMps =
136 entry.pseudorange_rate_uncertainty_mps;
137 gnssData.measurements[i].accumulatedDeltaRangeState =
Yifan Hong7037fdb2016-12-05 17:16:09 -0800138 entry.accumulated_delta_range_state;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700139 gnssData.measurements[i].accumulatedDeltaRangeM =
140 entry.accumulated_delta_range_m;
141 gnssData.measurements[i].accumulatedDeltaRangeUncertaintyM =
142 entry.accumulated_delta_range_uncertainty_m;
143
144 if (entry.flags & GNSS_MEASUREMENT_HAS_CARRIER_FREQUENCY) {
145 gnssData.measurements[i].carrierFrequencyHz = entry.carrier_frequency_hz;
146 } else {
147 gnssData.measurements[i].carrierFrequencyHz = 0;
148 }
149
150 if (entry.flags & GNSS_MEASUREMENT_HAS_CARRIER_PHASE) {
151 gnssData.measurements[i].carrierPhase = entry.carrier_phase;
152 } else {
153 gnssData.measurements[i].carrierPhase = 0;
154 }
155
156 if (entry.flags & GNSS_MEASUREMENT_HAS_CARRIER_PHASE_UNCERTAINTY) {
157 gnssData.measurements[i].carrierPhaseUncertainty = entry.carrier_phase_uncertainty;
158 } else {
159 gnssData.measurements[i].carrierPhaseUncertainty = 0;
160 }
161
162 gnssData.measurements[i].multipathIndicator =
163 static_cast<IGnssMeasurementCallback::GnssMultipathIndicator>(
164 entry.multipath_indicator);
165
166 if (entry.flags & GNSS_MEASUREMENT_HAS_SNR) {
167 gnssData.measurements[i].snrDb = entry.snr_db;
168 } else {
169 gnssData.measurements[i].snrDb = 0;
170 }
171 }
172
173 auto clockVal = gpsData->clock;
174 static uint32_t discontinuity_count_to_handle_old_clock_type = 0;
175 auto flags = clockVal.flags;
176
177 gnssData.clock.leapSecond = clockVal.leap_second;
178 /*
179 * GnssClock only supports the more effective HW_CLOCK type, so type
180 * handling and documentation complexity has been removed. To convert the
181 * old GPS_CLOCK types (active only in a limited number of older devices),
182 * the GPS time information is handled as an always discontinuous HW clock,
183 * with the GPS time information put into the full_bias_ns instead - so that
184 * time_ns - full_bias_ns = local estimate of GPS time. Additionally, the
185 * sign of full_bias_ns and bias_ns has flipped between GpsClock &
186 * GnssClock, so that is also handled below.
187 */
188 switch (clockVal.type) {
189 case GPS_CLOCK_TYPE_UNKNOWN:
190 // Clock type unsupported.
191 ALOGE("Unknown clock type provided.");
192 break;
193 case GPS_CLOCK_TYPE_LOCAL_HW_TIME:
194 // Already local hardware time. No need to do anything.
195 break;
196 case GPS_CLOCK_TYPE_GPS_TIME:
197 // GPS time, need to convert.
198 flags |= GPS_CLOCK_HAS_FULL_BIAS;
199 clockVal.full_bias_ns = clockVal.time_ns;
200 clockVal.time_ns = 0;
201 gnssData.clock.hwClockDiscontinuityCount =
202 discontinuity_count_to_handle_old_clock_type++;
203 break;
204 }
205
206 gnssData.clock.timeNs = clockVal.time_ns;
207 gnssData.clock.timeUncertaintyNs = clockVal.time_uncertainty_ns;
208 /*
209 * Definition of sign for full_bias_ns & bias_ns has been changed since N,
210 * so flip signs here.
211 */
212 gnssData.clock.fullBiasNs = -(clockVal.full_bias_ns);
213 gnssData.clock.biasNs = -(clockVal.bias_ns);
214 gnssData.clock.biasUncertaintyNs = clockVal.bias_uncertainty_ns;
215 gnssData.clock.driftNsps = clockVal.drift_nsps;
216 gnssData.clock.driftUncertaintyNsps = clockVal.drift_uncertainty_nsps;
Yifan Hong7037fdb2016-12-05 17:16:09 -0800217 gnssData.clock.gnssClockFlags = clockVal.flags;
Hridya Valsaraju29dc1e02016-10-21 14:41:12 -0700218
219 sGnssMeasureCbIface->GnssMeasurementCb(gnssData);
220}
221
222// Methods from ::android::hardware::gnss::V1_0::IGnssMeasurement follow.
223Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
224 const sp<IGnssMeasurementCallback>& callback) {
225 if (mGnssMeasureIface == nullptr) {
226 ALOGE("%s: GnssMeasure interface is unavailable", __func__);
227 return GnssMeasurementStatus::ERROR_GENERIC;
228 }
229 sGnssMeasureCbIface = callback;
230
231 return static_cast<GnssMeasurement::GnssMeasurementStatus>(
232 mGnssMeasureIface->init(&sGnssMeasurementCbs));
233}
234
235Return<void> GnssMeasurement::close() {
236 if (mGnssMeasureIface == nullptr) {
237 ALOGE("%s: GnssMeasure interface is unavailable", __func__);
238 } else {
239 mGnssMeasureIface->close();
240 }
241 return Void();
242}
243
244} // namespace implementation
245} // namespace V1_0
246} // namespace gnss
247} // namespace hardware
248} // namespace android