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