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_1 {
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 convertGnssData_2_1(GnssMeasurementsNotification& in,
57         V2_1::IGnssMeasurementCallback::GnssData& out);
58 static void convertGnssMeasurement(GnssMeasurementsData& in,
59         V1_0::IGnssMeasurementCallback::GnssMeasurement& out);
60 static void convertGnssClock(GnssMeasurementsClock& in, IGnssMeasurementCallback::GnssClock& out);
61 static void convertGnssClock_2_1(GnssMeasurementsClock& in,
62         V2_1::IGnssMeasurementCallback::GnssClock& out);
63 static void convertGnssMeasurementsCodeType(GnssMeasurementsCodeType& inCodeType,
64         char* inOtherCodeTypeName,
65         ::android::hardware::hidl_string& out);
66 static void convertGnssMeasurementsAccumulatedDeltaRangeState(GnssMeasurementsAdrStateMask& in,
67         ::android::hardware::hidl_bitfield
68                 <V1_1::IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState>& out);
69 static void convertGnssMeasurementsState(GnssMeasurementsStateMask& in,
70         ::android::hardware::hidl_bitfield
71                 <V2_0::IGnssMeasurementCallback::GnssMeasurementState>& out);
72 static void convertElapsedRealtimeNanos(GnssMeasurementsNotification& in,
73         ::android::hardware::gnss::V2_0::ElapsedRealtime& elapsedRealtimeNanos);
74 
MeasurementAPIClient()75 MeasurementAPIClient::MeasurementAPIClient() :
76     mGnssMeasurementCbIface(nullptr),
77     mGnssMeasurementCbIface_1_1(nullptr),
78     mGnssMeasurementCbIface_2_0(nullptr),
79     mGnssMeasurementCbIface_2_1(nullptr),
80     mTracking(false)
81 {
82     LOC_LOGD("%s]: ()", __FUNCTION__);
83 }
84 
~MeasurementAPIClient()85 MeasurementAPIClient::~MeasurementAPIClient()
86 {
87     LOC_LOGD("%s]: ()", __FUNCTION__);
88 }
89 
clearInterfaces()90 void MeasurementAPIClient::clearInterfaces()
91 {
92     mGnssMeasurementCbIface = nullptr;
93     mGnssMeasurementCbIface_1_1 = nullptr;
94     mGnssMeasurementCbIface_2_0 = nullptr;
95     mGnssMeasurementCbIface_2_1 = nullptr;
96 }
97 
98 // for GpsInterface
99 Return<IGnssMeasurement::GnssMeasurementStatus>
measurementSetCallback(const sp<V1_0::IGnssMeasurementCallback> & callback)100 MeasurementAPIClient::measurementSetCallback(const sp<V1_0::IGnssMeasurementCallback>& callback)
101 {
102     LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
103 
104     mMutex.lock();
105     clearInterfaces();
106     mGnssMeasurementCbIface = callback;
107     mMutex.unlock();
108 
109     return startTracking();
110 }
111 
112 Return<IGnssMeasurement::GnssMeasurementStatus>
measurementSetCallback_1_1(const sp<V1_1::IGnssMeasurementCallback> & callback,GnssPowerMode powerMode,uint32_t timeBetweenMeasurement)113 MeasurementAPIClient::measurementSetCallback_1_1(
114         const sp<V1_1::IGnssMeasurementCallback>& callback,
115         GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
116 {
117     LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
118             __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
119 
120     mMutex.lock();
121     clearInterfaces();
122     mGnssMeasurementCbIface_1_1 = callback;
123     mMutex.unlock();
124 
125     return startTracking(powerMode, timeBetweenMeasurement);
126 }
127 
128 Return<IGnssMeasurement::GnssMeasurementStatus>
measurementSetCallback_2_0(const sp<V2_0::IGnssMeasurementCallback> & callback,GnssPowerMode powerMode,uint32_t timeBetweenMeasurement)129 MeasurementAPIClient::measurementSetCallback_2_0(
130     const sp<V2_0::IGnssMeasurementCallback>& callback,
131     GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
132 {
133     LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
134         __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
135 
136     mMutex.lock();
137     clearInterfaces();
138     mGnssMeasurementCbIface_2_0 = callback;
139     mMutex.unlock();
140 
141     return startTracking(powerMode, timeBetweenMeasurement);
142 }
143 
measurementSetCallback_2_1(const sp<V2_1::IGnssMeasurementCallback> & callback,GnssPowerMode powerMode,uint32_t timeBetweenMeasurement)144 Return<IGnssMeasurement::GnssMeasurementStatus> MeasurementAPIClient::measurementSetCallback_2_1(
145         const sp<V2_1::IGnssMeasurementCallback>& callback,
146         GnssPowerMode powerMode, uint32_t timeBetweenMeasurement) {
147     LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
148         __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
149 
150     mMutex.lock();
151     clearInterfaces();
152     mGnssMeasurementCbIface_2_1 = callback;
153     mMutex.unlock();
154 
155     return startTracking(powerMode, timeBetweenMeasurement);
156 }
157 Return<IGnssMeasurement::GnssMeasurementStatus>
startTracking(GnssPowerMode powerMode,uint32_t timeBetweenMeasurement)158 MeasurementAPIClient::startTracking(
159         GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
160 {
161     LocationCallbacks locationCallbacks;
162     memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
163     locationCallbacks.size = sizeof(LocationCallbacks);
164 
165     locationCallbacks.trackingCb = nullptr;
166     locationCallbacks.batchingCb = nullptr;
167     locationCallbacks.geofenceBreachCb = nullptr;
168     locationCallbacks.geofenceStatusCb = nullptr;
169     locationCallbacks.gnssLocationInfoCb = nullptr;
170     locationCallbacks.gnssNiCb = nullptr;
171     locationCallbacks.gnssSvCb = nullptr;
172     locationCallbacks.gnssNmeaCb = nullptr;
173 
174     locationCallbacks.gnssMeasurementsCb = nullptr;
175     if (mGnssMeasurementCbIface_2_1 != nullptr ||
176         mGnssMeasurementCbIface_2_0 != nullptr ||
177         mGnssMeasurementCbIface_1_1 != nullptr ||
178         mGnssMeasurementCbIface != nullptr) {
179         locationCallbacks.gnssMeasurementsCb =
180             [this](GnssMeasurementsNotification gnssMeasurementsNotification) {
181                 onGnssMeasurementsCb(gnssMeasurementsNotification);
182             };
183     }
184 
185     locAPISetCallbacks(locationCallbacks);
186 
187     TrackingOptions options = {};
188     memset(&options, 0, sizeof(TrackingOptions));
189     options.size = sizeof(TrackingOptions);
190     options.minInterval = 1000;
191     options.mode = GNSS_SUPL_MODE_STANDALONE;
192     if (GNSS_POWER_MODE_INVALID != powerMode) {
193         options.powerMode = powerMode;
194         options.tbm = timeBetweenMeasurement;
195     }
196 
197     mTracking = true;
198     LOC_LOGD("%s]: start tracking session", __FUNCTION__);
199     locAPIStartTracking(options);
200     return IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
201 }
202 
203 // for GpsMeasurementInterface
measurementClose()204 void MeasurementAPIClient::measurementClose() {
205     LOC_LOGD("%s]: ()", __FUNCTION__);
206     mTracking = false;
207     locAPIStopTracking();
208 }
209 
210 // callbacks
onGnssMeasurementsCb(GnssMeasurementsNotification gnssMeasurementsNotification)211 void MeasurementAPIClient::onGnssMeasurementsCb(
212         GnssMeasurementsNotification gnssMeasurementsNotification)
213 {
214     LOC_LOGD("%s]: (count: %u active: %d)",
215             __FUNCTION__, gnssMeasurementsNotification.count, mTracking);
216     if (mTracking) {
217         mMutex.lock();
218         sp<V1_0::IGnssMeasurementCallback> gnssMeasurementCbIface = nullptr;
219         sp<V1_1::IGnssMeasurementCallback> gnssMeasurementCbIface_1_1 = nullptr;
220         sp<V2_0::IGnssMeasurementCallback> gnssMeasurementCbIface_2_0 = nullptr;
221         sp<V2_1::IGnssMeasurementCallback> gnssMeasurementCbIface_2_1 = nullptr;
222         if (mGnssMeasurementCbIface_2_1 != nullptr) {
223             gnssMeasurementCbIface_2_1 = mGnssMeasurementCbIface_2_1;
224         } else if (mGnssMeasurementCbIface_2_0 != nullptr) {
225             gnssMeasurementCbIface_2_0 = mGnssMeasurementCbIface_2_0;
226         } else if (mGnssMeasurementCbIface_1_1 != nullptr) {
227             gnssMeasurementCbIface_1_1 = mGnssMeasurementCbIface_1_1;
228         } else if (mGnssMeasurementCbIface != nullptr) {
229             gnssMeasurementCbIface = mGnssMeasurementCbIface;
230         }
231         mMutex.unlock();
232 
233         if (gnssMeasurementCbIface_2_1 != nullptr) {
234             V2_1::IGnssMeasurementCallback::GnssData gnssData;
235             convertGnssData_2_1(gnssMeasurementsNotification, gnssData);
236             auto r = gnssMeasurementCbIface_2_1->gnssMeasurementCb_2_1(gnssData);
237             if (!r.isOk()) {
238                 LOC_LOGE("%s] Error from gnssMeasurementCb description=%s",
239                     __func__, r.description().c_str());
240             }
241         } else if (gnssMeasurementCbIface_2_0 != nullptr) {
242             V2_0::IGnssMeasurementCallback::GnssData gnssData;
243             convertGnssData_2_0(gnssMeasurementsNotification, gnssData);
244             auto r = gnssMeasurementCbIface_2_0->gnssMeasurementCb_2_0(gnssData);
245             if (!r.isOk()) {
246                 LOC_LOGE("%s] Error from gnssMeasurementCb description=%s",
247                     __func__, r.description().c_str());
248             }
249         } else if (gnssMeasurementCbIface_1_1 != nullptr) {
250             V1_1::IGnssMeasurementCallback::GnssData gnssData;
251             convertGnssData_1_1(gnssMeasurementsNotification, gnssData);
252             auto r = gnssMeasurementCbIface_1_1->gnssMeasurementCb(gnssData);
253             if (!r.isOk()) {
254                 LOC_LOGE("%s] Error from gnssMeasurementCb description=%s",
255                     __func__, r.description().c_str());
256             }
257         } else if (gnssMeasurementCbIface != nullptr) {
258             V1_0::IGnssMeasurementCallback::GnssData gnssData;
259             convertGnssData(gnssMeasurementsNotification, gnssData);
260             auto r = gnssMeasurementCbIface->GnssMeasurementCb(gnssData);
261             if (!r.isOk()) {
262                 LOC_LOGE("%s] Error from GnssMeasurementCb description=%s",
263                     __func__, r.description().c_str());
264             }
265         }
266     }
267 }
268 
convertGnssMeasurement(GnssMeasurementsData & in,V1_0::IGnssMeasurementCallback::GnssMeasurement & out)269 static void convertGnssMeasurement(GnssMeasurementsData& in,
270         V1_0::IGnssMeasurementCallback::GnssMeasurement& out)
271 {
272     memset(&out, 0, sizeof(out));
273     if (in.flags & GNSS_MEASUREMENTS_DATA_SIGNAL_TO_NOISE_RATIO_BIT)
274         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_SNR;
275     if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_FREQUENCY_BIT)
276         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_FREQUENCY;
277     if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_CYCLES_BIT)
278         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_CYCLES;
279     if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_PHASE_BIT)
280         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_PHASE;
281     if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_PHASE_UNCERTAINTY_BIT)
282         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_PHASE_UNCERTAINTY;
283     if (in.flags & GNSS_MEASUREMENTS_DATA_AUTOMATIC_GAIN_CONTROL_BIT)
284         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_AUTOMATIC_GAIN_CONTROL;
285     convertGnssSvid(in, out.svid);
286     convertGnssConstellationType(in.svType, out.constellation);
287     out.timeOffsetNs = in.timeOffsetNs;
288     if (in.stateMask & GNSS_MEASUREMENTS_STATE_CODE_LOCK_BIT)
289         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_CODE_LOCK;
290     if (in.stateMask & GNSS_MEASUREMENTS_STATE_BIT_SYNC_BIT)
291         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BIT_SYNC;
292     if (in.stateMask & GNSS_MEASUREMENTS_STATE_SUBFRAME_SYNC_BIT)
293         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SUBFRAME_SYNC;
294     if (in.stateMask & GNSS_MEASUREMENTS_STATE_TOW_DECODED_BIT)
295         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_TOW_DECODED;
296     if (in.stateMask & GNSS_MEASUREMENTS_STATE_MSEC_AMBIGUOUS_BIT)
297         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_MSEC_AMBIGUOUS;
298     if (in.stateMask & GNSS_MEASUREMENTS_STATE_SYMBOL_SYNC_BIT)
299         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SYMBOL_SYNC;
300     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GLO_STRING_SYNC_BIT)
301         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_STRING_SYNC;
302     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GLO_TOD_DECODED_BIT)
303         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_TOD_DECODED;
304     if (in.stateMask & GNSS_MEASUREMENTS_STATE_BDS_D2_BIT_SYNC_BIT)
305         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_BIT_SYNC;
306     if (in.stateMask & GNSS_MEASUREMENTS_STATE_BDS_D2_SUBFRAME_SYNC_BIT)
307         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_SUBFRAME_SYNC;
308     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1BC_CODE_LOCK_BIT)
309         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1BC_CODE_LOCK;
310     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1C_2ND_CODE_LOCK_BIT)
311         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1C_2ND_CODE_LOCK;
312     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1B_PAGE_SYNC_BIT)
313         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1B_PAGE_SYNC;
314     if (in.stateMask &  GNSS_MEASUREMENTS_STATE_SBAS_SYNC_BIT)
315         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SBAS_SYNC;
316     out.receivedSvTimeInNs = in.receivedSvTimeNs;
317     out.receivedSvTimeUncertaintyInNs = in.receivedSvTimeUncertaintyNs;
318     out.cN0DbHz = in.carrierToNoiseDbHz;
319     out.pseudorangeRateMps = in.pseudorangeRateMps;
320     out.pseudorangeRateUncertaintyMps = in.pseudorangeRateUncertaintyMps;
321     if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_VALID_BIT)
322         out.accumulatedDeltaRangeState |=
323             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_VALID;
324     if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_RESET_BIT)
325         out.accumulatedDeltaRangeState |=
326             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_RESET;
327     if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_CYCLE_SLIP_BIT)
328         out.accumulatedDeltaRangeState |=
329             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_CYCLE_SLIP;
330     out.accumulatedDeltaRangeM = in.adrMeters;
331     out.accumulatedDeltaRangeUncertaintyM = in.adrUncertaintyMeters;
332     out.carrierFrequencyHz = in.carrierFrequencyHz;
333     out.carrierCycles = in.carrierCycles;
334     out.carrierPhase = in.carrierPhase;
335     out.carrierPhaseUncertainty = in.carrierPhaseUncertainty;
336     uint8_t indicator =
337         static_cast<uint8_t>(IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_UNKNOWN);
338     if (in.multipathIndicator & GNSS_MEASUREMENTS_MULTIPATH_INDICATOR_PRESENT)
339         indicator |= IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_PRESENT;
340     if (in.multipathIndicator & GNSS_MEASUREMENTS_MULTIPATH_INDICATOR_NOT_PRESENT)
341         indicator |= IGnssMeasurementCallback::GnssMultipathIndicator::INDICATIOR_NOT_PRESENT;
342     out.multipathIndicator =
343         static_cast<IGnssMeasurementCallback::GnssMultipathIndicator>(indicator);
344     out.snrDb = in.signalToNoiseRatioDb;
345     out.agcLevelDb = in.agcLevelDb;
346 }
347 
convertGnssClock(GnssMeasurementsClock & in,IGnssMeasurementCallback::GnssClock & out)348 static void convertGnssClock(GnssMeasurementsClock& in, IGnssMeasurementCallback::GnssClock& out)
349 {
350     memset(&out, 0, sizeof(out));
351     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_LEAP_SECOND_BIT)
352         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_LEAP_SECOND;
353     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_TIME_UNCERTAINTY_BIT)
354         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_TIME_UNCERTAINTY;
355     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_FULL_BIAS_BIT)
356         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_FULL_BIAS;
357     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_BIAS_BIT)
358         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_BIAS;
359     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_BIAS_UNCERTAINTY_BIT)
360         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_BIAS_UNCERTAINTY;
361     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_DRIFT_BIT)
362         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_DRIFT;
363     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_DRIFT_UNCERTAINTY_BIT)
364         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_DRIFT_UNCERTAINTY;
365     out.leapSecond = in.leapSecond;
366     out.timeNs = in.timeNs;
367     out.timeUncertaintyNs = in.timeUncertaintyNs;
368     out.fullBiasNs = in.fullBiasNs;
369     out.biasNs = in.biasNs;
370     out.biasUncertaintyNs = in.biasUncertaintyNs;
371     out.driftNsps = in.driftNsps;
372     out.driftUncertaintyNsps = in.driftUncertaintyNsps;
373     out.hwClockDiscontinuityCount = in.hwClockDiscontinuityCount;
374 }
375 
convertGnssClock_2_1(GnssMeasurementsClock & in,V2_1::IGnssMeasurementCallback::GnssClock & out)376 static void convertGnssClock_2_1(GnssMeasurementsClock& in,
377         V2_1::IGnssMeasurementCallback::GnssClock& out)
378 {
379     memset(&out, 0, sizeof(out));
380     convertGnssClock(in, out.v1_0);
381     convertGnssConstellationType(in.referenceSignalTypeForIsb.svType,
382             out.referenceSignalTypeForIsb.constellation);
383     out.referenceSignalTypeForIsb.carrierFrequencyHz =
384             in.referenceSignalTypeForIsb.carrierFrequencyHz;
385     convertGnssMeasurementsCodeType(in.referenceSignalTypeForIsb.codeType,
386             in.referenceSignalTypeForIsb.otherCodeTypeName,
387             out.referenceSignalTypeForIsb.codeType);
388 }
389 
convertGnssData(GnssMeasurementsNotification & in,V1_0::IGnssMeasurementCallback::GnssData & out)390 static void convertGnssData(GnssMeasurementsNotification& in,
391         V1_0::IGnssMeasurementCallback::GnssData& out)
392 {
393     memset(&out, 0, sizeof(out));
394     out.measurementCount = in.count;
395     if (out.measurementCount > static_cast<uint32_t>(V1_0::GnssMax::SVS_COUNT)) {
396         LOC_LOGW("%s]: Too many measurement %u. Clamps to %d.",
397                 __FUNCTION__,  out.measurementCount, V1_0::GnssMax::SVS_COUNT);
398         out.measurementCount = static_cast<uint32_t>(V1_0::GnssMax::SVS_COUNT);
399     }
400     for (size_t i = 0; i < out.measurementCount; i++) {
401         convertGnssMeasurement(in.measurements[i], out.measurements[i]);
402     }
403     convertGnssClock(in.clock, out.clock);
404 }
405 
convertGnssData_1_1(GnssMeasurementsNotification & in,V1_1::IGnssMeasurementCallback::GnssData & out)406 static void convertGnssData_1_1(GnssMeasurementsNotification& in,
407         V1_1::IGnssMeasurementCallback::GnssData& out)
408 {
409     memset(&out, 0, sizeof(out));
410     out.measurements.resize(in.count);
411     for (size_t i = 0; i < in.count; i++) {
412         convertGnssMeasurement(in.measurements[i], out.measurements[i].v1_0);
413         convertGnssMeasurementsAccumulatedDeltaRangeState(in.measurements[i].adrStateMask,
414                 out.measurements[i].accumulatedDeltaRangeState);
415     }
416     convertGnssClock(in.clock, out.clock);
417 }
418 
convertGnssData_2_0(GnssMeasurementsNotification & in,V2_0::IGnssMeasurementCallback::GnssData & out)419 static void convertGnssData_2_0(GnssMeasurementsNotification& in,
420         V2_0::IGnssMeasurementCallback::GnssData& out)
421 {
422     memset(&out, 0, sizeof(out));
423     out.measurements.resize(in.count);
424     for (size_t i = 0; i < in.count; i++) {
425         convertGnssMeasurement(in.measurements[i], out.measurements[i].v1_1.v1_0);
426         convertGnssConstellationType(in.measurements[i].svType, out.measurements[i].constellation);
427         convertGnssMeasurementsCodeType(in.measurements[i].codeType,
428             in.measurements[i].otherCodeTypeName,
429             out.measurements[i].codeType);
430         convertGnssMeasurementsAccumulatedDeltaRangeState(in.measurements[i].adrStateMask,
431                 out.measurements[i].v1_1.accumulatedDeltaRangeState);
432         convertGnssMeasurementsState(in.measurements[i].stateMask, out.measurements[i].state);
433     }
434     convertGnssClock(in.clock, out.clock);
435     convertElapsedRealtimeNanos(in, out.elapsedRealtime);
436 }
437 
convertGnssMeasurementsCodeType(GnssMeasurementsCodeType & inCodeType,char * inOtherCodeTypeName,::android::hardware::hidl_string & out)438 static void convertGnssMeasurementsCodeType(GnssMeasurementsCodeType& inCodeType,
439         char* inOtherCodeTypeName, ::android::hardware::hidl_string& out)
440 {
441     memset(&out, 0, sizeof(out));
442     switch(inCodeType) {
443         case GNSS_MEASUREMENTS_CODE_TYPE_A:
444             out = "A";
445             break;
446         case GNSS_MEASUREMENTS_CODE_TYPE_B:
447             out = "B";
448             break;
449         case GNSS_MEASUREMENTS_CODE_TYPE_C:
450             out = "C";
451             break;
452         case GNSS_MEASUREMENTS_CODE_TYPE_I:
453             out = "I";
454             break;
455         case GNSS_MEASUREMENTS_CODE_TYPE_L:
456             out = "L";
457             break;
458         case GNSS_MEASUREMENTS_CODE_TYPE_M:
459             out = "M";
460             break;
461         case GNSS_MEASUREMENTS_CODE_TYPE_P:
462             out = "P";
463             break;
464         case GNSS_MEASUREMENTS_CODE_TYPE_Q:
465             out = "Q";
466             break;
467         case GNSS_MEASUREMENTS_CODE_TYPE_S:
468             out = "S";
469             break;
470         case GNSS_MEASUREMENTS_CODE_TYPE_W:
471             out = "W";
472             break;
473         case GNSS_MEASUREMENTS_CODE_TYPE_X:
474             out = "X";
475             break;
476         case GNSS_MEASUREMENTS_CODE_TYPE_Y:
477             out = "Y";
478             break;
479         case GNSS_MEASUREMENTS_CODE_TYPE_Z:
480             out = "Z";
481             break;
482         case GNSS_MEASUREMENTS_CODE_TYPE_N:
483             out = "N";
484             break;
485         case GNSS_MEASUREMENTS_CODE_TYPE_OTHER:
486         default:
487             out = inOtherCodeTypeName;
488             break;
489     }
490 }
491 
convertGnssMeasurementsAccumulatedDeltaRangeState(GnssMeasurementsAdrStateMask & in,::android::hardware::hidl_bitfield<V1_1::IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState> & out)492 static void convertGnssMeasurementsAccumulatedDeltaRangeState(GnssMeasurementsAdrStateMask& in,
493         ::android::hardware::hidl_bitfield
494                 <V1_1::IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState>& out)
495 {
496     memset(&out, 0, sizeof(out));
497     if (in & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_VALID_BIT)
498         out |= IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_VALID;
499     if (in & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_RESET_BIT)
500         out |= IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_RESET;
501     if (in & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_CYCLE_SLIP_BIT)
502         out |= IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_CYCLE_SLIP;
503     if (in & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_HALF_CYCLE_RESOLVED_BIT)
504         out |= IGnssMeasurementCallback::
505                 GnssAccumulatedDeltaRangeState::ADR_STATE_HALF_CYCLE_RESOLVED;
506 }
507 
convertGnssMeasurementsState(GnssMeasurementsStateMask & in,::android::hardware::hidl_bitfield<V2_0::IGnssMeasurementCallback::GnssMeasurementState> & out)508 static void convertGnssMeasurementsState(GnssMeasurementsStateMask& in,
509         ::android::hardware::hidl_bitfield
510                 <V2_0::IGnssMeasurementCallback::GnssMeasurementState>& out)
511 {
512     memset(&out, 0, sizeof(out));
513     if (in & GNSS_MEASUREMENTS_STATE_CODE_LOCK_BIT)
514         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_CODE_LOCK;
515     if (in & GNSS_MEASUREMENTS_STATE_BIT_SYNC_BIT)
516         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BIT_SYNC;
517     if (in & GNSS_MEASUREMENTS_STATE_SUBFRAME_SYNC_BIT)
518         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SUBFRAME_SYNC;
519     if (in & GNSS_MEASUREMENTS_STATE_TOW_DECODED_BIT)
520         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_TOW_DECODED;
521     if (in & GNSS_MEASUREMENTS_STATE_MSEC_AMBIGUOUS_BIT)
522         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_MSEC_AMBIGUOUS;
523     if (in & GNSS_MEASUREMENTS_STATE_SYMBOL_SYNC_BIT)
524         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SYMBOL_SYNC;
525     if (in & GNSS_MEASUREMENTS_STATE_GLO_STRING_SYNC_BIT)
526         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_STRING_SYNC;
527     if (in & GNSS_MEASUREMENTS_STATE_GLO_TOD_DECODED_BIT)
528         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_TOD_DECODED;
529     if (in & GNSS_MEASUREMENTS_STATE_BDS_D2_BIT_SYNC_BIT)
530         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_BIT_SYNC;
531     if (in & GNSS_MEASUREMENTS_STATE_BDS_D2_SUBFRAME_SYNC_BIT)
532         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_SUBFRAME_SYNC;
533     if (in & GNSS_MEASUREMENTS_STATE_GAL_E1BC_CODE_LOCK_BIT)
534         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1BC_CODE_LOCK;
535     if (in & GNSS_MEASUREMENTS_STATE_GAL_E1C_2ND_CODE_LOCK_BIT)
536         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1C_2ND_CODE_LOCK;
537     if (in & GNSS_MEASUREMENTS_STATE_GAL_E1B_PAGE_SYNC_BIT)
538         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1B_PAGE_SYNC;
539     if (in & GNSS_MEASUREMENTS_STATE_SBAS_SYNC_BIT)
540         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SBAS_SYNC;
541     if (in & GNSS_MEASUREMENTS_STATE_TOW_KNOWN_BIT)
542         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_TOW_KNOWN;
543     if (in & GNSS_MEASUREMENTS_STATE_GLO_TOD_KNOWN_BIT)
544         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_TOD_KNOWN;
545     if (in & GNSS_MEASUREMENTS_STATE_2ND_CODE_LOCK_BIT)
546         out |= IGnssMeasurementCallback::GnssMeasurementState::STATE_2ND_CODE_LOCK;
547 }
548 
convertGnssData_2_1(GnssMeasurementsNotification & in,V2_1::IGnssMeasurementCallback::GnssData & out)549 static void convertGnssData_2_1(GnssMeasurementsNotification& in,
550         V2_1::IGnssMeasurementCallback::GnssData& out)
551 {
552     memset(&out, 0, sizeof(out));
553     out.measurements.resize(in.count);
554     for (size_t i = 0; i < in.count; i++) {
555         out.measurements[i].flags = 0;
556         convertGnssMeasurement(in.measurements[i], out.measurements[i].v2_0.v1_1.v1_0);
557         convertGnssConstellationType(in.measurements[i].svType,
558                 out.measurements[i].v2_0.constellation);
559         convertGnssMeasurementsCodeType(in.measurements[i].codeType,
560                 in.measurements[i].otherCodeTypeName,
561                 out.measurements[i].v2_0.codeType);
562         convertGnssMeasurementsAccumulatedDeltaRangeState(in.measurements[i].adrStateMask,
563                 out.measurements[i].v2_0.v1_1.accumulatedDeltaRangeState);
564         convertGnssMeasurementsState(in.measurements[i].stateMask,
565                 out.measurements[i].v2_0.state);
566         out.measurements[i].basebandCN0DbHz = in.measurements[i].basebandCarrierToNoiseDbHz;
567 
568         if (in.measurements[i].flags & GNSS_MEASUREMENTS_DATA_SIGNAL_TO_NOISE_RATIO_BIT) {
569             out.measurements[i].flags |=
570                 V2_1::IGnssMeasurementCallback::GnssMeasurementFlags::HAS_SNR;
571         }
572         if (in.measurements[i].flags & GNSS_MEASUREMENTS_DATA_CARRIER_FREQUENCY_BIT) {
573             out.measurements[i].flags |=
574                 V2_1::IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_FREQUENCY;
575         }
576         if (in.measurements[i].flags & GNSS_MEASUREMENTS_DATA_CARRIER_CYCLES_BIT) {
577             out.measurements[i].flags |=
578                 V2_1::IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_CYCLES;
579         }
580         if (in.measurements[i].flags & GNSS_MEASUREMENTS_DATA_CARRIER_PHASE_BIT) {
581             out.measurements[i].flags |=
582                 V2_1::IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_PHASE;
583         }
584         if (in.measurements[i].flags & GNSS_MEASUREMENTS_DATA_CARRIER_PHASE_UNCERTAINTY_BIT) {
585             out.measurements[i].flags |=
586                 V2_1::IGnssMeasurementCallback::
587                         GnssMeasurementFlags::HAS_CARRIER_PHASE_UNCERTAINTY;
588         }
589         if (in.measurements[i].flags & GNSS_MEASUREMENTS_DATA_AUTOMATIC_GAIN_CONTROL_BIT) {
590             out.measurements[i].flags |=
591                 V2_1::IGnssMeasurementCallback::GnssMeasurementFlags::HAS_AUTOMATIC_GAIN_CONTROL;
592         }
593         if (in.measurements[i].flags & GNSS_MEASUREMENTS_DATA_FULL_ISB_BIT) {
594             out.measurements[i].fullInterSignalBiasNs = in.measurements[i].fullInterSignalBiasNs;
595             out.measurements[i].flags |=
596                     V2_1::IGnssMeasurementCallback::GnssMeasurementFlags::HAS_FULL_ISB;
597         }
598         if (in.measurements[i].flags & GNSS_MEASUREMENTS_DATA_FULL_ISB_UNCERTAINTY_BIT) {
599             out.measurements[i].fullInterSignalBiasUncertaintyNs =
600                     in.measurements[i].fullInterSignalBiasUncertaintyNs;
601             out.measurements[i].flags |=
602                     V2_1::IGnssMeasurementCallback::
603                             GnssMeasurementFlags::HAS_FULL_ISB_UNCERTAINTY;
604         }
605         if (in.measurements[i].flags & GNSS_MEASUREMENTS_DATA_SATELLITE_ISB_BIT) {
606             out.measurements[i].satelliteInterSignalBiasNs =
607                     in.measurements[i].satelliteInterSignalBiasNs;
608             out.measurements[i].flags |=
609                     V2_1::IGnssMeasurementCallback::GnssMeasurementFlags::HAS_SATELLITE_ISB;
610         }
611         if (in.measurements[i].flags & GNSS_MEASUREMENTS_DATA_SATELLITE_ISB_UNCERTAINTY_BIT) {
612             out.measurements[i].satelliteInterSignalBiasUncertaintyNs =
613                     in.measurements[i].satelliteInterSignalBiasUncertaintyNs;
614             out.measurements[i].flags |=
615                     V2_1::IGnssMeasurementCallback::
616                             GnssMeasurementFlags::HAS_SATELLITE_ISB_UNCERTAINTY;
617         }
618     }
619     convertGnssClock_2_1(in.clock, out.clock);
620     convertElapsedRealtimeNanos(in, out.elapsedRealtime);
621 }
622 
convertElapsedRealtimeNanos(GnssMeasurementsNotification & in,::android::hardware::gnss::V2_0::ElapsedRealtime & elapsedRealtime)623 static void convertElapsedRealtimeNanos(GnssMeasurementsNotification& in,
624         ::android::hardware::gnss::V2_0::ElapsedRealtime& elapsedRealtime)
625 {
626     if (in.clock.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_ELAPSED_REAL_TIME_BIT) {
627         elapsedRealtime.flags |= V2_0::ElapsedRealtimeFlags::HAS_TIMESTAMP_NS;
628         elapsedRealtime.timestampNs = in.clock.elapsedRealTime;
629         elapsedRealtime.flags |= V2_0::ElapsedRealtimeFlags::HAS_TIME_UNCERTAINTY_NS;
630         elapsedRealtime.timeUncertaintyNs = in.clock.elapsedRealTimeUnc;
631         LOC_LOGd("elapsedRealtime.timestampNs=%" PRIi64 ""
632                  " elapsedRealtime.timeUncertaintyNs=%" PRIi64 " elapsedRealtime.flags=0x%X",
633                  elapsedRealtime.timestampNs,
634                  elapsedRealtime.timeUncertaintyNs, elapsedRealtime.flags);
635     }
636 }
637 
638 }  // namespace implementation
639 }  // namespace V2_1
640 }  // namespace gnss
641 }  // namespace hardware
642 }  // namespace android
643