1 /* Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above
9  *       copyright notice, this list of conditions and the following
10  *       disclaimer in the documentation and/or other materials provided
11  *       with the distribution.
12  *     * Neither the name of The Linux Foundation, nor the names of its
13  *       contributors may be used to endorse or promote products derived
14  *       from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #define LOG_NDEBUG 0
31 #define LOG_TAG "LocSvc_MeasurementAPIClient"
32 
33 #include <log_util.h>
34 #include <loc_cfg.h>
35 #include <inttypes.h>
36 
37 #include "LocationUtil.h"
38 #include "MeasurementAPIClient.h"
39 #include <loc_misc_utils.h>
40 
41 namespace android {
42 namespace hardware {
43 namespace gnss {
44 namespace V2_0 {
45 namespace implementation {
46 
47 using ::android::hardware::gnss::V1_0::IGnssMeasurement;
48 using ::android::hardware::gnss::V2_0::IGnssMeasurementCallback;
49 
50 static void convertGnssData(GnssMeasurementsNotification& in,
51         V1_0::IGnssMeasurementCallback::GnssData& out);
52 static void convertGnssData_1_1(GnssMeasurementsNotification& in,
53         V1_1::IGnssMeasurementCallback::GnssData& out);
54 static void convertGnssData_2_0(GnssMeasurementsNotification& in,
55         V2_0::IGnssMeasurementCallback::GnssData& out);
56 static void convertGnssMeasurement(GnssMeasurementsData& in,
57         V1_0::IGnssMeasurementCallback::GnssMeasurement& out);
58 static void convertGnssClock(GnssMeasurementsClock& in, IGnssMeasurementCallback::GnssClock& out);
59 static void convertGnssMeasurementsCodeType(GnssMeasurementsCodeType& in,
60         ::android::hardware::hidl_string& out);
61 static void convertElapsedRealtimeNanos(GnssMeasurementsNotification& in,
62         ::android::hardware::gnss::V2_0::ElapsedRealtime& elapsedRealtimeNanos);
63 
MeasurementAPIClient()64 MeasurementAPIClient::MeasurementAPIClient() :
65     mGnssMeasurementCbIface(nullptr),
66     mGnssMeasurementCbIface_1_1(nullptr),
67     mGnssMeasurementCbIface_2_0(nullptr),
68     mTracking(false)
69 {
70     LOC_LOGD("%s]: ()", __FUNCTION__);
71 }
72 
~MeasurementAPIClient()73 MeasurementAPIClient::~MeasurementAPIClient()
74 {
75     LOC_LOGD("%s]: ()", __FUNCTION__);
76 }
77 
clearInterfaces()78 void MeasurementAPIClient::clearInterfaces()
79 {
80     mGnssMeasurementCbIface = nullptr;
81     mGnssMeasurementCbIface_1_1 = nullptr;
82     mGnssMeasurementCbIface_2_0 = nullptr;
83 }
84 
85 // for GpsInterface
86 Return<IGnssMeasurement::GnssMeasurementStatus>
measurementSetCallback(const sp<V1_0::IGnssMeasurementCallback> & callback)87 MeasurementAPIClient::measurementSetCallback(const sp<V1_0::IGnssMeasurementCallback>& callback)
88 {
89     LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
90 
91     mMutex.lock();
92     clearInterfaces();
93     mGnssMeasurementCbIface = callback;
94     mMutex.unlock();
95 
96     return startTracking();
97 }
98 
99 Return<IGnssMeasurement::GnssMeasurementStatus>
measurementSetCallback_1_1(const sp<V1_1::IGnssMeasurementCallback> & callback,GnssPowerMode powerMode,uint32_t timeBetweenMeasurement)100 MeasurementAPIClient::measurementSetCallback_1_1(
101         const sp<V1_1::IGnssMeasurementCallback>& callback,
102         GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
103 {
104     LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
105             __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
106 
107     mMutex.lock();
108     clearInterfaces();
109     mGnssMeasurementCbIface_1_1 = callback;
110     mMutex.unlock();
111 
112     return startTracking(powerMode, timeBetweenMeasurement);
113 }
114 
115 Return<IGnssMeasurement::GnssMeasurementStatus>
measurementSetCallback_2_0(const sp<V2_0::IGnssMeasurementCallback> & callback,GnssPowerMode powerMode,uint32_t timeBetweenMeasurement)116 MeasurementAPIClient::measurementSetCallback_2_0(
117     const sp<V2_0::IGnssMeasurementCallback>& callback,
118     GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
119 {
120     LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
121         __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
122 
123     mMutex.lock();
124     clearInterfaces();
125     mGnssMeasurementCbIface_2_0 = callback;
126     mMutex.unlock();
127 
128     return startTracking(powerMode, timeBetweenMeasurement);
129 }
130 
131 Return<IGnssMeasurement::GnssMeasurementStatus>
startTracking(GnssPowerMode powerMode,uint32_t timeBetweenMeasurement)132 MeasurementAPIClient::startTracking(
133         GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
134 {
135     LocationCallbacks locationCallbacks;
136     memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
137     locationCallbacks.size = sizeof(LocationCallbacks);
138 
139     locationCallbacks.trackingCb = nullptr;
140     locationCallbacks.batchingCb = nullptr;
141     locationCallbacks.geofenceBreachCb = nullptr;
142     locationCallbacks.geofenceStatusCb = nullptr;
143     locationCallbacks.gnssLocationInfoCb = nullptr;
144     locationCallbacks.gnssNiCb = nullptr;
145     locationCallbacks.gnssSvCb = nullptr;
146     locationCallbacks.gnssNmeaCb = nullptr;
147 
148     locationCallbacks.gnssMeasurementsCb = nullptr;
149     if (mGnssMeasurementCbIface_2_0 != nullptr ||
150         mGnssMeasurementCbIface_1_1 != nullptr ||
151         mGnssMeasurementCbIface != nullptr) {
152         locationCallbacks.gnssMeasurementsCb =
153             [this](GnssMeasurementsNotification gnssMeasurementsNotification) {
154                 onGnssMeasurementsCb(gnssMeasurementsNotification);
155             };
156     }
157 
158     locAPISetCallbacks(locationCallbacks);
159 
160     TrackingOptions options = {};
161     memset(&options, 0, sizeof(TrackingOptions));
162     options.size = sizeof(TrackingOptions);
163     options.minInterval = 1000;
164     options.mode = GNSS_SUPL_MODE_STANDALONE;
165     if (GNSS_POWER_MODE_INVALID != powerMode) {
166         options.powerMode = powerMode;
167         options.tbm = timeBetweenMeasurement;
168     }
169 
170     mTracking = true;
171     LOC_LOGD("%s]: start tracking session", __FUNCTION__);
172     locAPIStartTracking(options);
173     return IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
174 }
175 
176 // for GpsMeasurementInterface
measurementClose()177 void MeasurementAPIClient::measurementClose() {
178     LOC_LOGD("%s]: ()", __FUNCTION__);
179     mTracking = false;
180     locAPIStopTracking();
181 }
182 
183 // callbacks
onGnssMeasurementsCb(GnssMeasurementsNotification gnssMeasurementsNotification)184 void MeasurementAPIClient::onGnssMeasurementsCb(
185         GnssMeasurementsNotification gnssMeasurementsNotification)
186 {
187     LOC_LOGD("%s]: (count: %u active: %d)",
188             __FUNCTION__, gnssMeasurementsNotification.count, mTracking);
189     if (mTracking) {
190         mMutex.lock();
191         sp<V1_0::IGnssMeasurementCallback> gnssMeasurementCbIface = nullptr;
192         sp<V1_1::IGnssMeasurementCallback> gnssMeasurementCbIface_1_1 = nullptr;
193         sp<V2_0::IGnssMeasurementCallback> gnssMeasurementCbIface_2_0 = nullptr;
194         if (mGnssMeasurementCbIface_2_0 != nullptr) {
195             gnssMeasurementCbIface_2_0 = mGnssMeasurementCbIface_2_0;
196         } else if (mGnssMeasurementCbIface_1_1 != nullptr) {
197             gnssMeasurementCbIface_1_1 = mGnssMeasurementCbIface_1_1;
198         } else if (mGnssMeasurementCbIface != nullptr) {
199             gnssMeasurementCbIface = mGnssMeasurementCbIface;
200         }
201         mMutex.unlock();
202 
203         if (gnssMeasurementCbIface_2_0 != nullptr) {
204             V2_0::IGnssMeasurementCallback::GnssData gnssData;
205             convertGnssData_2_0(gnssMeasurementsNotification, gnssData);
206             auto r = gnssMeasurementCbIface_2_0->gnssMeasurementCb_2_0(gnssData);
207             if (!r.isOk()) {
208                 LOC_LOGE("%s] Error from gnssMeasurementCb description=%s",
209                     __func__, r.description().c_str());
210             }
211         } else if (gnssMeasurementCbIface_1_1 != nullptr) {
212             V1_1::IGnssMeasurementCallback::GnssData gnssData;
213             convertGnssData_1_1(gnssMeasurementsNotification, gnssData);
214             auto r = gnssMeasurementCbIface_1_1->gnssMeasurementCb(gnssData);
215             if (!r.isOk()) {
216                 LOC_LOGE("%s] Error from gnssMeasurementCb description=%s",
217                     __func__, r.description().c_str());
218             }
219         } else if (gnssMeasurementCbIface != nullptr) {
220             V1_0::IGnssMeasurementCallback::GnssData gnssData;
221             convertGnssData(gnssMeasurementsNotification, gnssData);
222             auto r = gnssMeasurementCbIface->GnssMeasurementCb(gnssData);
223             if (!r.isOk()) {
224                 LOC_LOGE("%s] Error from GnssMeasurementCb description=%s",
225                     __func__, r.description().c_str());
226             }
227         }
228     }
229 }
230 
convertGnssMeasurement(GnssMeasurementsData & in,V1_0::IGnssMeasurementCallback::GnssMeasurement & out)231 static void convertGnssMeasurement(GnssMeasurementsData& in,
232         V1_0::IGnssMeasurementCallback::GnssMeasurement& out)
233 {
234     memset(&out, 0, sizeof(out));
235     if (in.flags & GNSS_MEASUREMENTS_DATA_SIGNAL_TO_NOISE_RATIO_BIT)
236         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_SNR;
237     if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_FREQUENCY_BIT)
238         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_FREQUENCY;
239     if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_CYCLES_BIT)
240         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_CYCLES;
241     if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_PHASE_BIT)
242         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_PHASE;
243     if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_PHASE_UNCERTAINTY_BIT)
244         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_PHASE_UNCERTAINTY;
245     if (in.flags & GNSS_MEASUREMENTS_DATA_AUTOMATIC_GAIN_CONTROL_BIT)
246         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_AUTOMATIC_GAIN_CONTROL;
247     convertGnssSvid(in, out.svid);
248     convertGnssConstellationType(in.svType, out.constellation);
249     out.timeOffsetNs = in.timeOffsetNs;
250     if (in.stateMask & GNSS_MEASUREMENTS_STATE_CODE_LOCK_BIT)
251         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_CODE_LOCK;
252     if (in.stateMask & GNSS_MEASUREMENTS_STATE_BIT_SYNC_BIT)
253         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BIT_SYNC;
254     if (in.stateMask & GNSS_MEASUREMENTS_STATE_SUBFRAME_SYNC_BIT)
255         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SUBFRAME_SYNC;
256     if (in.stateMask & GNSS_MEASUREMENTS_STATE_TOW_DECODED_BIT)
257         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_TOW_DECODED;
258     if (in.stateMask & GNSS_MEASUREMENTS_STATE_MSEC_AMBIGUOUS_BIT)
259         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_MSEC_AMBIGUOUS;
260     if (in.stateMask & GNSS_MEASUREMENTS_STATE_SYMBOL_SYNC_BIT)
261         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SYMBOL_SYNC;
262     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GLO_STRING_SYNC_BIT)
263         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_STRING_SYNC;
264     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GLO_TOD_DECODED_BIT)
265         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_TOD_DECODED;
266     if (in.stateMask & GNSS_MEASUREMENTS_STATE_BDS_D2_BIT_SYNC_BIT)
267         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_BIT_SYNC;
268     if (in.stateMask & GNSS_MEASUREMENTS_STATE_BDS_D2_SUBFRAME_SYNC_BIT)
269         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_SUBFRAME_SYNC;
270     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1BC_CODE_LOCK_BIT)
271         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1BC_CODE_LOCK;
272     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1C_2ND_CODE_LOCK_BIT)
273         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1C_2ND_CODE_LOCK;
274     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1B_PAGE_SYNC_BIT)
275         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1B_PAGE_SYNC;
276     if (in.stateMask &  GNSS_MEASUREMENTS_STATE_SBAS_SYNC_BIT)
277         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SBAS_SYNC;
278     out.receivedSvTimeInNs = in.receivedSvTimeNs;
279     out.receivedSvTimeUncertaintyInNs = in.receivedSvTimeUncertaintyNs;
280     out.cN0DbHz = in.carrierToNoiseDbHz;
281     out.pseudorangeRateMps = in.pseudorangeRateMps;
282     out.pseudorangeRateUncertaintyMps = in.pseudorangeRateUncertaintyMps;
283     if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_VALID_BIT)
284         out.accumulatedDeltaRangeState |=
285             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_VALID;
286     if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_RESET_BIT)
287         out.accumulatedDeltaRangeState |=
288             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_RESET;
289     if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_CYCLE_SLIP_BIT)
290         out.accumulatedDeltaRangeState |=
291             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_CYCLE_SLIP;
292     out.accumulatedDeltaRangeM = in.adrMeters;
293     out.accumulatedDeltaRangeUncertaintyM = in.adrUncertaintyMeters;
294     out.carrierFrequencyHz = in.carrierFrequencyHz;
295     out.carrierCycles = in.carrierCycles;
296     out.carrierPhase = in.carrierPhase;
297     out.carrierPhaseUncertainty = in.carrierPhaseUncertainty;
298     uint8_t indicator =
299         static_cast<uint8_t>(IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_UNKNOWN);
300     if (in.multipathIndicator & GNSS_MEASUREMENTS_MULTIPATH_INDICATOR_PRESENT)
301         indicator |= IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_PRESENT;
302     if (in.multipathIndicator & GNSS_MEASUREMENTS_MULTIPATH_INDICATOR_NOT_PRESENT)
303         indicator |= IGnssMeasurementCallback::GnssMultipathIndicator::INDICATIOR_NOT_PRESENT;
304     out.multipathIndicator =
305         static_cast<IGnssMeasurementCallback::GnssMultipathIndicator>(indicator);
306     out.snrDb = in.signalToNoiseRatioDb;
307     out.agcLevelDb = in.agcLevelDb;
308 }
309 
convertGnssClock(GnssMeasurementsClock & in,IGnssMeasurementCallback::GnssClock & out)310 static void convertGnssClock(GnssMeasurementsClock& in, IGnssMeasurementCallback::GnssClock& out)
311 {
312     memset(&out, 0, sizeof(out));
313     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_LEAP_SECOND_BIT)
314         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_LEAP_SECOND;
315     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_TIME_UNCERTAINTY_BIT)
316         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_TIME_UNCERTAINTY;
317     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_FULL_BIAS_BIT)
318         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_FULL_BIAS;
319     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_BIAS_BIT)
320         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_BIAS;
321     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_BIAS_UNCERTAINTY_BIT)
322         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_BIAS_UNCERTAINTY;
323     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_DRIFT_BIT)
324         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_DRIFT;
325     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_DRIFT_UNCERTAINTY_BIT)
326         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_DRIFT_UNCERTAINTY;
327     out.leapSecond = in.leapSecond;
328     out.timeNs = in.timeNs;
329     out.timeUncertaintyNs = in.timeUncertaintyNs;
330     out.fullBiasNs = in.fullBiasNs;
331     out.biasNs = in.biasNs;
332     out.biasUncertaintyNs = in.biasUncertaintyNs;
333     out.driftNsps = in.driftNsps;
334     out.driftUncertaintyNsps = in.driftUncertaintyNsps;
335     out.hwClockDiscontinuityCount = in.hwClockDiscontinuityCount;
336 }
337 
convertGnssData(GnssMeasurementsNotification & in,V1_0::IGnssMeasurementCallback::GnssData & out)338 static void convertGnssData(GnssMeasurementsNotification& in,
339         V1_0::IGnssMeasurementCallback::GnssData& out)
340 {
341     memset(&out, 0, sizeof(out));
342     out.measurementCount = in.count;
343     if (out.measurementCount > static_cast<uint32_t>(V1_0::GnssMax::SVS_COUNT)) {
344         LOC_LOGW("%s]: Too many measurement %u. Clamps to %d.",
345                 __FUNCTION__,  out.measurementCount, V1_0::GnssMax::SVS_COUNT);
346         out.measurementCount = static_cast<uint32_t>(V1_0::GnssMax::SVS_COUNT);
347     }
348     for (size_t i = 0; i < out.measurementCount; i++) {
349         convertGnssMeasurement(in.measurements[i], out.measurements[i]);
350     }
351     convertGnssClock(in.clock, out.clock);
352 }
353 
convertGnssData_1_1(GnssMeasurementsNotification & in,V1_1::IGnssMeasurementCallback::GnssData & out)354 static void convertGnssData_1_1(GnssMeasurementsNotification& in,
355         V1_1::IGnssMeasurementCallback::GnssData& out)
356 {
357     memset(&out, 0, sizeof(out));
358     out.measurements.resize(in.count);
359     for (size_t i = 0; i < in.count; i++) {
360         convertGnssMeasurement(in.measurements[i], out.measurements[i].v1_0);
361         if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_VALID_BIT)
362             out.measurements[i].accumulatedDeltaRangeState |=
363             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_VALID;
364         if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_RESET_BIT)
365             out.measurements[i].accumulatedDeltaRangeState |=
366             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_RESET;
367         if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_CYCLE_SLIP_BIT)
368             out.measurements[i].accumulatedDeltaRangeState |=
369             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_CYCLE_SLIP;
370         if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_HALF_CYCLE_RESOLVED_BIT)
371             out.measurements[i].accumulatedDeltaRangeState |=
372             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_HALF_CYCLE_RESOLVED;
373     }
374     convertGnssClock(in.clock, out.clock);
375 }
376 
convertGnssData_2_0(GnssMeasurementsNotification & in,V2_0::IGnssMeasurementCallback::GnssData & out)377 static void convertGnssData_2_0(GnssMeasurementsNotification& in,
378         V2_0::IGnssMeasurementCallback::GnssData& out)
379 {
380     memset(&out, 0, sizeof(out));
381     out.measurements.resize(in.count);
382     for (size_t i = 0; i < in.count; i++) {
383         convertGnssMeasurement(in.measurements[i], out.measurements[i].v1_1.v1_0);
384         convertGnssConstellationType(in.measurements[i].svType, out.measurements[i].constellation);
385         convertGnssMeasurementsCodeType(in.measurements[i].codeType, out.measurements[i].codeType);
386         if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_VALID_BIT)
387             out.measurements[i].v1_1.accumulatedDeltaRangeState |=
388             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_VALID;
389         if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_RESET_BIT)
390             out.measurements[i].v1_1.accumulatedDeltaRangeState |=
391             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_RESET;
392         if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_CYCLE_SLIP_BIT)
393             out.measurements[i].v1_1.accumulatedDeltaRangeState |=
394             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_CYCLE_SLIP;
395         if (in.measurements[i].adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_HALF_CYCLE_RESOLVED_BIT)
396             out.measurements[i].v1_1.accumulatedDeltaRangeState |=
397             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_HALF_CYCLE_RESOLVED;
398         if (in.measurements[i].stateMask & GNSS_MEASUREMENTS_STATE_CODE_LOCK_BIT)
399             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_CODE_LOCK;
400         if (in.measurements[i].stateMask & GNSS_MEASUREMENTS_STATE_BIT_SYNC_BIT)
401             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BIT_SYNC;
402         if (in.measurements[i].stateMask & GNSS_MEASUREMENTS_STATE_SUBFRAME_SYNC_BIT)
403             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SUBFRAME_SYNC;
404         if (in.measurements[i].stateMask & GNSS_MEASUREMENTS_STATE_TOW_DECODED_BIT)
405             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_TOW_DECODED;
406         if (in.measurements[i].stateMask & GNSS_MEASUREMENTS_STATE_MSEC_AMBIGUOUS_BIT)
407             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_MSEC_AMBIGUOUS;
408         if (in.measurements[i].stateMask & GNSS_MEASUREMENTS_STATE_SYMBOL_SYNC_BIT)
409             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SYMBOL_SYNC;
410         if (in.measurements[i].stateMask & GNSS_MEASUREMENTS_STATE_GLO_STRING_SYNC_BIT)
411             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_STRING_SYNC;
412         if (in.measurements[i].stateMask & GNSS_MEASUREMENTS_STATE_GLO_TOD_DECODED_BIT)
413             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_TOD_DECODED;
414         if (in.measurements[i].stateMask & GNSS_MEASUREMENTS_STATE_BDS_D2_BIT_SYNC_BIT)
415             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_BIT_SYNC;
416         if (in.measurements[i].stateMask & GNSS_MEASUREMENTS_STATE_BDS_D2_SUBFRAME_SYNC_BIT)
417             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_SUBFRAME_SYNC;
418         if (in.measurements[i].stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1BC_CODE_LOCK_BIT)
419             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1BC_CODE_LOCK;
420         if (in.measurements[i].stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1C_2ND_CODE_LOCK_BIT)
421             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1C_2ND_CODE_LOCK;
422         if (in.measurements[i].stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1B_PAGE_SYNC_BIT)
423             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1B_PAGE_SYNC;
424         if (in.measurements[i].stateMask &  GNSS_MEASUREMENTS_STATE_SBAS_SYNC_BIT)
425             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SBAS_SYNC;
426         if (in.measurements[i].stateMask &  GNSS_MEASUREMENTS_STATE_TOW_KNOWN_BIT)
427             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_TOW_KNOWN;
428         if (in.measurements[i].stateMask &  GNSS_MEASUREMENTS_STATE_GLO_TOD_KNOWN_BIT)
429             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_TOD_KNOWN;
430         if (in.measurements[i].stateMask &  GNSS_MEASUREMENTS_STATE_2ND_CODE_LOCK_BIT)
431             out.measurements[i].state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_2ND_CODE_LOCK;
432     }
433     convertGnssClock(in.clock, out.clock);
434     convertElapsedRealtimeNanos(in, out.elapsedRealtime);
435 }
436 
convertElapsedRealtimeNanos(GnssMeasurementsNotification & in,::android::hardware::gnss::V2_0::ElapsedRealtime & elapsedRealtime)437 static void convertElapsedRealtimeNanos(GnssMeasurementsNotification& in,
438         ::android::hardware::gnss::V2_0::ElapsedRealtime& elapsedRealtime)
439 {
440     if (in.clock.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_ELAPSED_REAL_TIME_BIT) {
441         elapsedRealtime.flags |= V2_0::ElapsedRealtimeFlags::HAS_TIMESTAMP_NS;
442         elapsedRealtime.timestampNs = in.clock.elapsedRealTime;
443         elapsedRealtime.flags |= V2_0::ElapsedRealtimeFlags::HAS_TIME_UNCERTAINTY_NS;
444         elapsedRealtime.timeUncertaintyNs = in.clock.elapsedRealTimeUnc;
445         LOC_LOGd("elapsedRealtime.timestampNs=%" PRIi64 ""
446                  " elapsedRealtime.timeUncertaintyNs=%" PRIi64 " elapsedRealtime.flags=0x%X",
447                  elapsedRealtime.timestampNs,
448                  elapsedRealtime.timeUncertaintyNs, elapsedRealtime.flags);
449     }
450 }
451 
convertGnssMeasurementsCodeType(GnssMeasurementsCodeType & in,::android::hardware::hidl_string & out)452 static void convertGnssMeasurementsCodeType(GnssMeasurementsCodeType& in,
453         ::android::hardware::hidl_string& out)
454 {
455     switch(in) {
456         case GNSS_MEASUREMENTS_CODE_TYPE_A:
457             out = "A";
458             break;
459         case GNSS_MEASUREMENTS_CODE_TYPE_B:
460             out = "B";
461             break;
462         case GNSS_MEASUREMENTS_CODE_TYPE_C:
463             out = "C";
464             break;
465         case GNSS_MEASUREMENTS_CODE_TYPE_I:
466             out = "I";
467             break;
468         case GNSS_MEASUREMENTS_CODE_TYPE_L:
469             out = "L";
470             break;
471         case GNSS_MEASUREMENTS_CODE_TYPE_M:
472             out = "M";
473             break;
474         case GNSS_MEASUREMENTS_CODE_TYPE_P:
475             out = "P";
476             break;
477         case GNSS_MEASUREMENTS_CODE_TYPE_Q:
478             out = "Q";
479             break;
480         case GNSS_MEASUREMENTS_CODE_TYPE_S:
481             out = "S";
482             break;
483         case GNSS_MEASUREMENTS_CODE_TYPE_W:
484             out = "W";
485             break;
486         case GNSS_MEASUREMENTS_CODE_TYPE_X:
487             out = "X";
488             break;
489         case GNSS_MEASUREMENTS_CODE_TYPE_Y:
490             out = "Y";
491             break;
492         case GNSS_MEASUREMENTS_CODE_TYPE_Z:
493             out = "Z";
494             break;
495         case GNSS_MEASUREMENTS_CODE_TYPE_N:
496             out = "N";
497             break;
498         default:
499             out = "UNKNOWN";
500     }
501 }
502 
503 }  // namespace implementation
504 }  // namespace V2_0
505 }  // namespace gnss
506 }  // namespace hardware
507 }  // namespace android
508