• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "GnssMeasCbJni"
18 
19 #include "GnssMeasurementCallback.h"
20 
21 namespace android::gnss {
22 
23 using binder::Status;
24 using hardware::gnss::CorrelationVector;
25 using hardware::gnss::ElapsedRealtime;
26 using hardware::gnss::GnssClock;
27 using hardware::gnss::GnssData;
28 using hardware::gnss::GnssMeasurement;
29 using hardware::gnss::SatellitePvt;
30 using GnssAgc = hardware::gnss::GnssData::GnssAgc;
31 
32 namespace {
33 jclass class_arrayList;
34 jclass class_clockInfo;
35 jclass class_correlationVectorBuilder;
36 jclass class_gnssAgc;
37 jclass class_gnssAgcBuilder;
38 jclass class_gnssMeasurementsEvent;
39 jclass class_gnssMeasurementsEventBuilder;
40 jclass class_gnssMeasurement;
41 jclass class_gnssClock;
42 jclass class_positionEcef;
43 jclass class_satellitePvtBuilder;
44 jclass class_velocityEcef;
45 
46 jmethodID method_arrayListAdd;
47 jmethodID method_arrayListCtor;
48 jmethodID method_correlationVectorBuilderBuild;
49 jmethodID method_correlationVectorBuilderCtor;
50 jmethodID method_correlationVectorBuilderSetFrequencyOffsetMetersPerSecond;
51 jmethodID method_correlationVectorBuilderSetMagnitude;
52 jmethodID method_correlationVectorBuilderSetSamplingStartMeters;
53 jmethodID method_correlationVectorBuilderSetSamplingWidthMeters;
54 jmethodID method_gnssAgcBuilderCtor;
55 jmethodID method_gnssAgcBuilderSetLevelDb;
56 jmethodID method_gnssAgcBuilderSetConstellationType;
57 jmethodID method_gnssAgcBuilderSetCarrierFrequencyHz;
58 jmethodID method_gnssAgcBuilderBuild;
59 jmethodID method_gnssMeasurementsEventBuilderCtor;
60 jmethodID method_gnssMeasurementsEventBuilderSetClock;
61 jmethodID method_gnssMeasurementsEventBuilderSetIsFullTracking;
62 jmethodID method_gnssMeasurementsEventBuilderSetMeasurements;
63 jmethodID method_gnssMeasurementsEventBuilderSetGnssAutomaticGainControls;
64 jmethodID method_gnssMeasurementsEventBuilderBuild;
65 jmethodID method_gnssMeasurementsSetCorrelationVectors;
66 jmethodID method_gnssMeasurementsSetSatellitePvt;
67 jmethodID method_gnssClockCtor;
68 jmethodID method_gnssMeasurementCtor;
69 jmethodID method_reportMeasurementData;
70 jmethodID method_satellitePvtBuilderBuild;
71 jmethodID method_satellitePvtBuilderCtor;
72 jmethodID method_satellitePvtBuilderSetPositionEcef;
73 jmethodID method_satellitePvtBuilderSetVelocityEcef;
74 jmethodID method_satellitePvtBuilderSetClockInfo;
75 jmethodID method_satellitePvtBuilderSetIonoDelayMeters;
76 jmethodID method_satellitePvtBuilderSetTropoDelayMeters;
77 jmethodID method_satellitePvtBuilderSetTimeOfClock;
78 jmethodID method_satellitePvtBuilderSetTimeOfEphemeris;
79 jmethodID method_satellitePvtBuilderSetIssueOfDataClock;
80 jmethodID method_satellitePvtBuilderSetIssueOfDataEphemeris;
81 jmethodID method_satellitePvtBuilderSetEphemerisSource;
82 jmethodID method_positionEcef;
83 jmethodID method_velocityEcef;
84 jmethodID method_clockInfo;
85 
86 } // anonymous namespace
87 
GnssMeasurement_class_init_once(JNIEnv * env,jclass & clazz)88 void GnssMeasurement_class_init_once(JNIEnv* env, jclass& clazz) {
89     method_reportMeasurementData = env->GetMethodID(clazz, "reportMeasurementData",
90                                                     "(Landroid/location/GnssMeasurementsEvent;)V");
91 
92     // Initialize GnssMeasurement related classes and methods
93     jclass gnssMeasurementsEventClass = env->FindClass("android/location/GnssMeasurementsEvent");
94     class_gnssMeasurementsEvent = (jclass)env->NewGlobalRef(gnssMeasurementsEventClass);
95     jclass gnssMeasurementsEventBuilderClass =
96             env->FindClass("android/location/GnssMeasurementsEvent$Builder");
97     class_gnssMeasurementsEventBuilder =
98             (jclass)env->NewGlobalRef(gnssMeasurementsEventBuilderClass);
99     method_gnssMeasurementsEventBuilderCtor =
100             env->GetMethodID(class_gnssMeasurementsEventBuilder, "<init>", "()V");
101     method_gnssMeasurementsEventBuilderSetClock =
102             env->GetMethodID(class_gnssMeasurementsEventBuilder, "setClock",
103                              "(Landroid/location/GnssClock;)"
104                              "Landroid/location/GnssMeasurementsEvent$Builder;");
105     method_gnssMeasurementsEventBuilderSetMeasurements =
106             env->GetMethodID(class_gnssMeasurementsEventBuilder, "setMeasurements",
107                              "([Landroid/location/GnssMeasurement;)"
108                              "Landroid/location/GnssMeasurementsEvent$Builder;");
109     method_gnssMeasurementsEventBuilderSetGnssAutomaticGainControls =
110             env->GetMethodID(class_gnssMeasurementsEventBuilder, "setGnssAutomaticGainControls",
111                              "([Landroid/location/GnssAutomaticGainControl;)"
112                              "Landroid/location/GnssMeasurementsEvent$Builder;");
113     method_gnssMeasurementsEventBuilderSetIsFullTracking =
114             env->GetMethodID(class_gnssMeasurementsEventBuilder, "setIsFullTracking",
115                              "(Z)"
116                              "Landroid/location/GnssMeasurementsEvent$Builder;");
117     method_gnssMeasurementsEventBuilderBuild =
118             env->GetMethodID(class_gnssMeasurementsEventBuilder, "build",
119                              "()Landroid/location/GnssMeasurementsEvent;");
120 
121     // Initialize GnssAgc related classes and methods
122     jclass gnssAgcClass = env->FindClass("android/location/GnssAutomaticGainControl");
123     class_gnssAgc = (jclass)env->NewGlobalRef(gnssAgcClass);
124     jclass gnssAgcBuilderClass =
125             env->FindClass("android/location/GnssAutomaticGainControl$Builder");
126     class_gnssAgcBuilder = (jclass)env->NewGlobalRef(gnssAgcBuilderClass);
127     method_gnssAgcBuilderCtor = env->GetMethodID(class_gnssAgcBuilder, "<init>", "()V");
128     method_gnssAgcBuilderSetLevelDb =
129             env->GetMethodID(class_gnssAgcBuilder, "setLevelDb",
130                              "(D)"
131                              "Landroid/location/GnssAutomaticGainControl$Builder;");
132     method_gnssAgcBuilderSetConstellationType =
133             env->GetMethodID(class_gnssAgcBuilder, "setConstellationType",
134                              "(I)"
135                              "Landroid/location/GnssAutomaticGainControl$Builder;");
136     method_gnssAgcBuilderSetCarrierFrequencyHz =
137             env->GetMethodID(class_gnssAgcBuilder, "setCarrierFrequencyHz",
138                              "(J)"
139                              "Landroid/location/GnssAutomaticGainControl$Builder;");
140     method_gnssAgcBuilderBuild = env->GetMethodID(class_gnssAgcBuilder, "build",
141                                                   "()Landroid/location/GnssAutomaticGainControl;");
142 
143     // Initialize GnssMeasurement related classes and methods
144     jclass gnssMeasurementClass = env->FindClass("android/location/GnssMeasurement");
145     class_gnssMeasurement = (jclass)env->NewGlobalRef(gnssMeasurementClass);
146     method_gnssMeasurementCtor = env->GetMethodID(class_gnssMeasurement, "<init>", "()V");
147     method_gnssMeasurementsSetSatellitePvt =
148             env->GetMethodID(class_gnssMeasurement, "setSatellitePvt",
149                              "(Landroid/location/SatellitePvt;)V");
150     method_gnssMeasurementsSetCorrelationVectors =
151             env->GetMethodID(class_gnssMeasurement, "setCorrelationVectors",
152                              "(Ljava/util/Collection;)V");
153 
154     jclass gnssClockClass = env->FindClass("android/location/GnssClock");
155     class_gnssClock = (jclass)env->NewGlobalRef(gnssClockClass);
156     method_gnssClockCtor = env->GetMethodID(class_gnssClock, "<init>", "()V");
157 
158     jclass satellitePvtBuilder = env->FindClass("android/location/SatellitePvt$Builder");
159     class_satellitePvtBuilder = (jclass)env->NewGlobalRef(satellitePvtBuilder);
160     method_satellitePvtBuilderCtor = env->GetMethodID(class_satellitePvtBuilder, "<init>", "()V");
161     method_satellitePvtBuilderSetPositionEcef =
162             env->GetMethodID(class_satellitePvtBuilder, "setPositionEcef",
163                              "(Landroid/location/SatellitePvt$PositionEcef;)"
164                              "Landroid/location/SatellitePvt$Builder;");
165     method_satellitePvtBuilderSetVelocityEcef =
166             env->GetMethodID(class_satellitePvtBuilder, "setVelocityEcef",
167                              "(Landroid/location/SatellitePvt$VelocityEcef;)"
168                              "Landroid/location/SatellitePvt$Builder;");
169     method_satellitePvtBuilderSetClockInfo =
170             env->GetMethodID(class_satellitePvtBuilder, "setClockInfo",
171                              "(Landroid/location/SatellitePvt$ClockInfo;)"
172                              "Landroid/location/SatellitePvt$Builder;");
173     method_satellitePvtBuilderSetIonoDelayMeters =
174             env->GetMethodID(class_satellitePvtBuilder, "setIonoDelayMeters",
175                              "(D)Landroid/location/SatellitePvt$Builder;");
176     method_satellitePvtBuilderSetTropoDelayMeters =
177             env->GetMethodID(class_satellitePvtBuilder, "setTropoDelayMeters",
178                              "(D)Landroid/location/SatellitePvt$Builder;");
179     method_satellitePvtBuilderSetTimeOfClock =
180             env->GetMethodID(class_satellitePvtBuilder, "setTimeOfClockSeconds",
181                              "(J)Landroid/location/SatellitePvt$Builder;");
182     method_satellitePvtBuilderSetTimeOfEphemeris =
183             env->GetMethodID(class_satellitePvtBuilder, "setTimeOfEphemerisSeconds",
184                              "(J)Landroid/location/SatellitePvt$Builder;");
185     method_satellitePvtBuilderSetIssueOfDataClock =
186             env->GetMethodID(class_satellitePvtBuilder, "setIssueOfDataClock",
187                              "(I)Landroid/location/SatellitePvt$Builder;");
188     method_satellitePvtBuilderSetIssueOfDataEphemeris =
189             env->GetMethodID(class_satellitePvtBuilder, "setIssueOfDataEphemeris",
190                              "(I)Landroid/location/SatellitePvt$Builder;");
191     method_satellitePvtBuilderSetEphemerisSource =
192             env->GetMethodID(class_satellitePvtBuilder, "setEphemerisSource",
193                              "(I)Landroid/location/SatellitePvt$Builder;");
194     method_satellitePvtBuilderBuild = env->GetMethodID(class_satellitePvtBuilder, "build",
195                                                        "()Landroid/location/SatellitePvt;");
196 
197     jclass positionEcefClass = env->FindClass("android/location/SatellitePvt$PositionEcef");
198     class_positionEcef = (jclass)env->NewGlobalRef(positionEcefClass);
199     method_positionEcef = env->GetMethodID(class_positionEcef, "<init>", "(DDDD)V");
200 
201     jclass velocityEcefClass = env->FindClass("android/location/SatellitePvt$VelocityEcef");
202     class_velocityEcef = (jclass)env->NewGlobalRef(velocityEcefClass);
203     method_velocityEcef = env->GetMethodID(class_velocityEcef, "<init>", "(DDDD)V");
204 
205     jclass clockInfoClass = env->FindClass("android/location/SatellitePvt$ClockInfo");
206     class_clockInfo = (jclass)env->NewGlobalRef(clockInfoClass);
207     method_clockInfo = env->GetMethodID(class_clockInfo, "<init>", "(DDD)V");
208 
209     jclass correlationVectorBuilder = env->FindClass("android/location/CorrelationVector$Builder");
210     class_correlationVectorBuilder = (jclass)env->NewGlobalRef(correlationVectorBuilder);
211     method_correlationVectorBuilderCtor =
212             env->GetMethodID(class_correlationVectorBuilder, "<init>", "()V");
213     method_correlationVectorBuilderSetMagnitude =
214             env->GetMethodID(class_correlationVectorBuilder, "setMagnitude",
215                              "([I)Landroid/location/CorrelationVector$Builder;");
216     method_correlationVectorBuilderSetFrequencyOffsetMetersPerSecond =
217             env->GetMethodID(class_correlationVectorBuilder, "setFrequencyOffsetMetersPerSecond",
218                              "(D)Landroid/location/CorrelationVector$Builder;");
219     method_correlationVectorBuilderSetSamplingStartMeters =
220             env->GetMethodID(class_correlationVectorBuilder, "setSamplingStartMeters",
221                              "(D)Landroid/location/CorrelationVector$Builder;");
222     method_correlationVectorBuilderSetSamplingWidthMeters =
223             env->GetMethodID(class_correlationVectorBuilder, "setSamplingWidthMeters",
224                              "(D)Landroid/location/CorrelationVector$Builder;");
225     method_correlationVectorBuilderBuild =
226             env->GetMethodID(class_correlationVectorBuilder, "build",
227                              "()Landroid/location/CorrelationVector;");
228 
229     jclass arrayListClass = env->FindClass("java/util/ArrayList");
230     class_arrayList = (jclass)env->NewGlobalRef(arrayListClass);
231     method_arrayListCtor = env->GetMethodID(class_arrayList, "<init>", "()V");
232     method_arrayListAdd = env->GetMethodID(class_arrayList, "add", "(Ljava/lang/Object;)Z");
233 }
234 
setMeasurementData(JNIEnv * env,jobject & callbacksObj,jobject clock,jobjectArray measurementArray,jobjectArray gnssAgcArray,bool hasIsFullTracking,jboolean isFullTracking)235 void setMeasurementData(JNIEnv* env, jobject& callbacksObj, jobject clock,
236                         jobjectArray measurementArray, jobjectArray gnssAgcArray,
237                         bool hasIsFullTracking, jboolean isFullTracking) {
238     jobject gnssMeasurementsEventBuilderObject =
239             env->NewObject(class_gnssMeasurementsEventBuilder,
240                            method_gnssMeasurementsEventBuilderCtor);
241     callObjectMethodIgnoringResult(env, gnssMeasurementsEventBuilderObject,
242                                    method_gnssMeasurementsEventBuilderSetClock, clock);
243     callObjectMethodIgnoringResult(env, gnssMeasurementsEventBuilderObject,
244                                    method_gnssMeasurementsEventBuilderSetMeasurements,
245                                    measurementArray);
246     callObjectMethodIgnoringResult(env, gnssMeasurementsEventBuilderObject,
247                                    method_gnssMeasurementsEventBuilderSetGnssAutomaticGainControls,
248                                    gnssAgcArray);
249     if (hasIsFullTracking) {
250         callObjectMethodIgnoringResult(env, gnssMeasurementsEventBuilderObject,
251                                        method_gnssMeasurementsEventBuilderSetIsFullTracking,
252                                        isFullTracking);
253     }
254     jobject gnssMeasurementsEventObject =
255             env->CallObjectMethod(gnssMeasurementsEventBuilderObject,
256                                   method_gnssMeasurementsEventBuilderBuild);
257 
258     env->CallVoidMethod(callbacksObj, method_reportMeasurementData, gnssMeasurementsEventObject);
259     checkAndClearExceptionFromCallback(env, __FUNCTION__);
260     env->DeleteLocalRef(gnssMeasurementsEventBuilderObject);
261     env->DeleteLocalRef(gnssMeasurementsEventObject);
262 }
263 
264 template <class T_Measurement, class T_Flags>
setMeasurementFields_V1_0(const T_Measurement & measurement,JavaObject & object)265 void setMeasurementFields_V1_0(const T_Measurement& measurement, JavaObject& object) {
266     uint32_t flags = static_cast<uint32_t>(measurement.flags);
267 
268     SET(Svid, static_cast<int32_t>(measurement.svid));
269     SET(TimeOffsetNanos, measurement.timeOffsetNs);
270     SET(State, static_cast<int32_t>(measurement.state));
271     SET(ReceivedSvTimeNanos, measurement.receivedSvTimeInNs);
272     SET(ReceivedSvTimeUncertaintyNanos, measurement.receivedSvTimeUncertaintyInNs);
273     SET(PseudorangeRateMetersPerSecond, measurement.pseudorangeRateMps);
274     SET(PseudorangeRateUncertaintyMetersPerSecond, measurement.pseudorangeRateUncertaintyMps);
275     SET(AccumulatedDeltaRangeState,
276         (static_cast<int32_t>(measurement.accumulatedDeltaRangeState) &
277          ~ADR_STATE_HALF_CYCLE_REPORTED)); // Half Cycle state not reported from Hardware in V1_0
278     SET(AccumulatedDeltaRangeMeters, measurement.accumulatedDeltaRangeM);
279     SET(AccumulatedDeltaRangeUncertaintyMeters, measurement.accumulatedDeltaRangeUncertaintyM);
280 
281     // Intentionally not copying deprecated fields of carrierCycles,
282     // carrierPhase, carrierPhaseUncertainty
283 
284     SET(MultipathIndicator, static_cast<int32_t>(measurement.multipathIndicator));
285 
286     if (flags & static_cast<uint32_t>(T_Flags::HAS_SNR)) {
287         SET(SnrInDb, measurement.snrDb);
288     }
289 
290     if (flags & static_cast<uint32_t>(T_Flags::HAS_AUTOMATIC_GAIN_CONTROL)) {
291         SET(AutomaticGainControlLevelInDb, measurement.agcLevelDb);
292     }
293 }
294 
295 template <class T_Measurement, class T_Flags>
setMeasurementFields_V2_1(const T_Measurement & measurement,JavaObject & object)296 void setMeasurementFields_V2_1(const T_Measurement& measurement, JavaObject& object) {
297     SET(BasebandCn0DbHz, measurement.basebandCN0DbHz);
298 
299     if (measurement.flags & T_Flags::HAS_FULL_ISB) {
300         SET(FullInterSignalBiasNanos, measurement.fullInterSignalBiasNs);
301     }
302 
303     if (measurement.flags & T_Flags::HAS_FULL_ISB_UNCERTAINTY) {
304         SET(FullInterSignalBiasUncertaintyNanos, measurement.fullInterSignalBiasUncertaintyNs);
305     }
306 
307     if (measurement.flags & T_Flags::HAS_SATELLITE_ISB) {
308         SET(SatelliteInterSignalBiasNanos, measurement.satelliteInterSignalBiasNs);
309     }
310 
311     if (measurement.flags & T_Flags::HAS_SATELLITE_ISB_UNCERTAINTY) {
312         SET(SatelliteInterSignalBiasUncertaintyNanos,
313             measurement.satelliteInterSignalBiasUncertaintyNs);
314     }
315 }
316 
317 template <class T_Clock, class T_Flags>
setClockFields_V1_0(const T_Clock & clock,JavaObject & object)318 void setClockFields_V1_0(const T_Clock& clock, JavaObject& object) {
319     uint32_t flags = static_cast<uint32_t>(clock.gnssClockFlags);
320     if (flags & static_cast<uint32_t>(T_Flags::HAS_LEAP_SECOND)) {
321         SET(LeapSecond, static_cast<int32_t>(clock.leapSecond));
322     }
323 
324     if (flags & static_cast<uint32_t>(T_Flags::HAS_TIME_UNCERTAINTY)) {
325         SET(TimeUncertaintyNanos, clock.timeUncertaintyNs);
326     }
327 
328     if (flags & static_cast<uint32_t>(T_Flags::HAS_FULL_BIAS)) {
329         SET(FullBiasNanos, clock.fullBiasNs);
330     }
331 
332     if (flags & static_cast<uint32_t>(T_Flags::HAS_BIAS)) {
333         SET(BiasNanos, clock.biasNs);
334     }
335 
336     if (flags & static_cast<uint32_t>(T_Flags::HAS_BIAS_UNCERTAINTY)) {
337         SET(BiasUncertaintyNanos, clock.biasUncertaintyNs);
338     }
339 
340     if (flags & static_cast<uint32_t>(T_Flags::HAS_DRIFT)) {
341         SET(DriftNanosPerSecond, clock.driftNsps);
342     }
343 
344     if (flags & static_cast<uint32_t>(T_Flags::HAS_DRIFT_UNCERTAINTY)) {
345         SET(DriftUncertaintyNanosPerSecond, clock.driftUncertaintyNsps);
346     }
347 
348     SET(TimeNanos, clock.timeNs);
349     SET(HardwareClockDiscontinuityCount, clock.hwClockDiscontinuityCount);
350 }
351 
352 template <class T_Clock, class T_Flags>
setClockFields_V2_1(const T_Clock & clock,JavaObject & object)353 void setClockFields_V2_1(const T_Clock& clock, JavaObject& object) {
354     JNIEnv* env = getJniEnv();
355     SET(ReferenceConstellationTypeForIsb,
356         static_cast<int32_t>(clock.referenceSignalTypeForIsb.constellation));
357     SET(ReferenceCarrierFrequencyHzForIsb, clock.referenceSignalTypeForIsb.carrierFrequencyHz);
358 
359     jstring referenceCodeTypeForIsb =
360             env->NewStringUTF(clock.referenceSignalTypeForIsb.codeType.c_str());
361     SET(ReferenceCodeTypeForIsb, referenceCodeTypeForIsb);
362     env->DeleteLocalRef(referenceCodeTypeForIsb);
363 }
364 
365 template <class T_ElapsedRealtime, class T_Flags>
setElapsedRealtimeFields(const T_ElapsedRealtime & elapsedRealtime,JavaObject & object)366 void setElapsedRealtimeFields(const T_ElapsedRealtime& elapsedRealtime, JavaObject& object) {
367     uint32_t flags = static_cast<uint32_t>(elapsedRealtime.flags);
368     if (flags & T_Flags::HAS_TIMESTAMP_NS) {
369         SET(ElapsedRealtimeNanos, static_cast<uint64_t>(elapsedRealtime.timestampNs));
370     }
371     if (flags & T_Flags::HAS_TIME_UNCERTAINTY_NS) {
372         SET(ElapsedRealtimeUncertaintyNanos,
373             static_cast<double>(elapsedRealtime.timeUncertaintyNs));
374     }
375 }
376 
377 // Implementation of GnssMeasurementCallbackAidl class.
378 
gnssMeasurementCb(const GnssData & data)379 Status GnssMeasurementCallbackAidl::gnssMeasurementCb(const GnssData& data) {
380     ALOGD("%s", __func__);
381     translateAndSetGnssData(data);
382     return Status::ok();
383 }
384 
translateAndSetGnssData(const GnssData & data)385 void GnssMeasurementCallbackAidl::translateAndSetGnssData(const GnssData& data) {
386     JNIEnv* env = getJniEnv();
387 
388     JavaObject gnssClockJavaObject(env, class_gnssClock, method_gnssClockCtor);
389     translateGnssClock(env, data, gnssClockJavaObject);
390     jobject clock = gnssClockJavaObject.get();
391     jobjectArray measurementArray = translateAllGnssMeasurements(env, data.measurements);
392 
393     jobjectArray gnssAgcArray = nullptr;
394     gnssAgcArray = translateAllGnssAgcs(env, data.gnssAgcs);
395     if (interfaceVersion >= 3) {
396         setMeasurementData(env, mCallbacksObj, clock, measurementArray, gnssAgcArray,
397                            /*hasIsFullTracking=*/true, data.isFullTracking);
398     } else {
399         setMeasurementData(env, mCallbacksObj, clock, measurementArray, gnssAgcArray,
400                            /*hasIsFullTracking=*/false,
401                            /*isFullTracking=*/JNI_FALSE);
402     }
403 
404     env->DeleteLocalRef(clock);
405     env->DeleteLocalRef(measurementArray);
406     env->DeleteLocalRef(gnssAgcArray);
407 }
408 
translateSingleGnssMeasurement(JNIEnv * env,const GnssMeasurement & measurement,JavaObject & object)409 void GnssMeasurementCallbackAidl::translateSingleGnssMeasurement(JNIEnv* env,
410                                                                  const GnssMeasurement& measurement,
411                                                                  JavaObject& object) {
412     setMeasurementFields_V1_0<GnssMeasurement, GnssMeasurement>(measurement, object);
413     setMeasurementFields_V2_1<GnssMeasurement, GnssMeasurement>(measurement, object);
414 
415     SET(Cn0DbHz, measurement.antennaCN0DbHz);
416     SET(ConstellationType, static_cast<int32_t>(measurement.signalType.constellation));
417     // Half cycle state is reported in the AIDL version of GnssMeasurement
418     SET(AccumulatedDeltaRangeState,
419         (static_cast<int32_t>(measurement.accumulatedDeltaRangeState) |
420          ADR_STATE_HALF_CYCLE_REPORTED));
421 
422     if (measurement.flags & static_cast<uint32_t>(GnssMeasurement::HAS_CARRIER_FREQUENCY)) {
423         SET(CarrierFrequencyHz, static_cast<float>(measurement.signalType.carrierFrequencyHz));
424     }
425 
426     if (measurement.flags & static_cast<uint32_t>(GnssMeasurement::HAS_SATELLITE_PVT)) {
427         const SatellitePvt& satellitePvt = measurement.satellitePvt;
428         uint16_t satFlags = static_cast<uint16_t>(satellitePvt.flags);
429         jobject positionEcef = nullptr;
430         jobject velocityEcef = nullptr;
431         jobject clockInfo = nullptr;
432         jobject satellitePvtBuilderObject =
433                 env->NewObject(class_satellitePvtBuilder, method_satellitePvtBuilderCtor);
434 
435         if (satFlags & SatellitePvt::HAS_POSITION_VELOCITY_CLOCK_INFO) {
436             positionEcef = env->NewObject(class_positionEcef, method_positionEcef,
437                                           satellitePvt.satPosEcef.posXMeters,
438                                           satellitePvt.satPosEcef.posYMeters,
439                                           satellitePvt.satPosEcef.posZMeters,
440                                           satellitePvt.satPosEcef.ureMeters);
441             velocityEcef =
442                     env->NewObject(class_velocityEcef, method_velocityEcef,
443                                    satellitePvt.satVelEcef.velXMps, satellitePvt.satVelEcef.velYMps,
444                                    satellitePvt.satVelEcef.velZMps,
445                                    satellitePvt.satVelEcef.ureRateMps);
446             clockInfo = env->NewObject(class_clockInfo, method_clockInfo,
447                                        satellitePvt.satClockInfo.satHardwareCodeBiasMeters,
448                                        satellitePvt.satClockInfo.satTimeCorrectionMeters,
449                                        satellitePvt.satClockInfo.satClkDriftMps);
450             callObjectMethodIgnoringResult(env, satellitePvtBuilderObject,
451                                            method_satellitePvtBuilderSetPositionEcef, positionEcef);
452             callObjectMethodIgnoringResult(env, satellitePvtBuilderObject,
453                                            method_satellitePvtBuilderSetVelocityEcef, velocityEcef);
454             callObjectMethodIgnoringResult(env, satellitePvtBuilderObject,
455                                            method_satellitePvtBuilderSetClockInfo, clockInfo);
456         }
457 
458         if (satFlags & SatellitePvt::HAS_IONO) {
459             callObjectMethodIgnoringResult(env, satellitePvtBuilderObject,
460                                            method_satellitePvtBuilderSetIonoDelayMeters,
461                                            satellitePvt.ionoDelayMeters);
462         }
463 
464         if (satFlags & SatellitePvt::HAS_TROPO) {
465             callObjectMethodIgnoringResult(env, satellitePvtBuilderObject,
466                                            method_satellitePvtBuilderSetTropoDelayMeters,
467                                            satellitePvt.tropoDelayMeters);
468         }
469 
470         if (interfaceVersion >= 2) {
471             callObjectMethodIgnoringResult(env, satellitePvtBuilderObject,
472                                            method_satellitePvtBuilderSetTimeOfClock,
473                                            satellitePvt.timeOfClockSeconds);
474             callObjectMethodIgnoringResult(env, satellitePvtBuilderObject,
475                                            method_satellitePvtBuilderSetTimeOfEphemeris,
476                                            satellitePvt.timeOfEphemerisSeconds);
477             callObjectMethodIgnoringResult(env, satellitePvtBuilderObject,
478                                            method_satellitePvtBuilderSetIssueOfDataClock,
479                                            satellitePvt.issueOfDataClock);
480             callObjectMethodIgnoringResult(env, satellitePvtBuilderObject,
481                                            method_satellitePvtBuilderSetIssueOfDataEphemeris,
482                                            satellitePvt.issueOfDataEphemeris);
483             callObjectMethodIgnoringResult(env, satellitePvtBuilderObject,
484                                            method_satellitePvtBuilderSetEphemerisSource,
485                                            static_cast<int>(satellitePvt.ephemerisSource));
486         }
487 
488         jobject satellitePvtObject =
489                 env->CallObjectMethod(satellitePvtBuilderObject, method_satellitePvtBuilderBuild);
490         env->CallVoidMethod(object.get(), method_gnssMeasurementsSetSatellitePvt,
491                             satellitePvtObject);
492         env->DeleteLocalRef(positionEcef);
493         env->DeleteLocalRef(velocityEcef);
494         env->DeleteLocalRef(clockInfo);
495         env->DeleteLocalRef(satellitePvtBuilderObject);
496         env->DeleteLocalRef(satellitePvtObject);
497     }
498 
499     if (measurement.flags & static_cast<uint32_t>(GnssMeasurement::HAS_CORRELATION_VECTOR)) {
500         jobject correlationVectorList = env->NewObject(class_arrayList, method_arrayListCtor);
501         for (uint16_t i = 0; i < measurement.correlationVectors.size(); ++i) {
502             const CorrelationVector& correlationVector = measurement.correlationVectors[i];
503             const std::vector<int32_t>& magnitudeVector = correlationVector.magnitude;
504 
505             jsize numMagnitude = magnitudeVector.size();
506             jintArray magnitudeArray = env->NewIntArray(numMagnitude);
507             env->SetIntArrayRegion(magnitudeArray, 0, numMagnitude,
508                                    reinterpret_cast<const jint*>(magnitudeVector.data()));
509 
510             jobject correlationVectorBuilderObject =
511                     env->NewObject(class_correlationVectorBuilder,
512                                    method_correlationVectorBuilderCtor);
513             callObjectMethodIgnoringResult(env, correlationVectorBuilderObject,
514                                            method_correlationVectorBuilderSetMagnitude,
515                                            magnitudeArray);
516             callObjectMethodIgnoringResult(
517                     env, correlationVectorBuilderObject,
518                     method_correlationVectorBuilderSetFrequencyOffsetMetersPerSecond,
519                     correlationVector.frequencyOffsetMps);
520             callObjectMethodIgnoringResult(env, correlationVectorBuilderObject,
521                                            method_correlationVectorBuilderSetSamplingStartMeters,
522                                            correlationVector.samplingStartM);
523             callObjectMethodIgnoringResult(env, correlationVectorBuilderObject,
524                                            method_correlationVectorBuilderSetSamplingWidthMeters,
525                                            correlationVector.samplingWidthM);
526             jobject correlationVectorObject =
527                     env->CallObjectMethod(correlationVectorBuilderObject,
528                                           method_correlationVectorBuilderBuild);
529 
530             env->CallBooleanMethod(correlationVectorList, method_arrayListAdd,
531                                    correlationVectorObject);
532 
533             env->DeleteLocalRef(magnitudeArray);
534             env->DeleteLocalRef(correlationVectorBuilderObject);
535             env->DeleteLocalRef(correlationVectorObject);
536         }
537         env->CallVoidMethod(object.get(), method_gnssMeasurementsSetCorrelationVectors,
538                             correlationVectorList);
539         env->DeleteLocalRef(correlationVectorList);
540     }
541 
542     jstring codeType = env->NewStringUTF(measurement.signalType.codeType.c_str());
543     SET(CodeType, codeType);
544     env->DeleteLocalRef(codeType);
545 }
546 
translateAllGnssMeasurements(JNIEnv * env,const std::vector<GnssMeasurement> & measurements)547 jobjectArray GnssMeasurementCallbackAidl::translateAllGnssMeasurements(
548         JNIEnv* env, const std::vector<GnssMeasurement>& measurements) {
549     if (measurements.size() == 0) {
550         return nullptr;
551     }
552 
553     jobjectArray gnssMeasurementArray =
554             env->NewObjectArray(measurements.size(), class_gnssMeasurement,
555                                 nullptr /* initialElement */);
556 
557     for (uint16_t i = 0; i < measurements.size(); ++i) {
558         JavaObject object(env, class_gnssMeasurement, method_gnssMeasurementCtor);
559         translateSingleGnssMeasurement(env, measurements[i], object);
560         jobject gnssMeasurement = object.get();
561         env->SetObjectArrayElement(gnssMeasurementArray, i, gnssMeasurement);
562         env->DeleteLocalRef(gnssMeasurement);
563     }
564 
565     return gnssMeasurementArray;
566 }
567 
translateAllGnssAgcs(JNIEnv * env,const std::vector<GnssAgc> & agcs)568 jobjectArray GnssMeasurementCallbackAidl::translateAllGnssAgcs(JNIEnv* env,
569                                                                const std::vector<GnssAgc>& agcs) {
570     if (agcs.size() == 0) {
571         return nullptr;
572     }
573 
574     jobjectArray gnssAgcArray =
575             env->NewObjectArray(agcs.size(), class_gnssAgc, nullptr /* initialElement */);
576 
577     for (uint16_t i = 0; i < agcs.size(); ++i) {
578         const GnssAgc& gnssAgc = agcs[i];
579 
580         jobject agcBuilderObject = env->NewObject(class_gnssAgcBuilder, method_gnssAgcBuilderCtor);
581         callObjectMethodIgnoringResult(env, agcBuilderObject, method_gnssAgcBuilderSetLevelDb,
582                                        gnssAgc.agcLevelDb);
583         callObjectMethodIgnoringResult(env, agcBuilderObject,
584                                        method_gnssAgcBuilderSetConstellationType,
585                                        (int)gnssAgc.constellation);
586         callObjectMethodIgnoringResult(env, agcBuilderObject,
587                                        method_gnssAgcBuilderSetCarrierFrequencyHz,
588                                        gnssAgc.carrierFrequencyHz);
589         jobject agcObject = env->CallObjectMethod(agcBuilderObject, method_gnssAgcBuilderBuild);
590 
591         env->SetObjectArrayElement(gnssAgcArray, i, agcObject);
592         env->DeleteLocalRef(agcBuilderObject);
593         env->DeleteLocalRef(agcObject);
594     }
595 
596     return gnssAgcArray;
597 }
598 
translateGnssClock(JNIEnv * env,const GnssData & data,JavaObject & object)599 void GnssMeasurementCallbackAidl::translateGnssClock(JNIEnv* env, const GnssData& data,
600                                                      JavaObject& object) {
601     setElapsedRealtimeFields<ElapsedRealtime, ElapsedRealtime>(data.elapsedRealtime, object);
602     setClockFields_V1_0<GnssClock, GnssClock>(data.clock, object);
603     setClockFields_V2_1<GnssClock, GnssClock>(data.clock, object);
604 }
605 
606 // Implementation of GnssMeasurementCallbackHidl class.
607 
gnssMeasurementCb_2_1(const hardware::gnss::V2_1::IGnssMeasurementCallback::GnssData & data)608 hardware::Return<void> GnssMeasurementCallbackHidl::gnssMeasurementCb_2_1(
609         const hardware::gnss::V2_1::IGnssMeasurementCallback::GnssData& data) {
610     translateAndSetGnssData(data);
611     return hardware::Void();
612 }
613 
gnssMeasurementCb_2_0(const hardware::gnss::V2_0::IGnssMeasurementCallback::GnssData & data)614 hardware::Return<void> GnssMeasurementCallbackHidl::gnssMeasurementCb_2_0(
615         const hardware::gnss::V2_0::IGnssMeasurementCallback::GnssData& data) {
616     translateAndSetGnssData(data);
617     return hardware::Void();
618 }
619 
gnssMeasurementCb(const hardware::gnss::V1_1::IGnssMeasurementCallback::GnssData & data)620 hardware::Return<void> GnssMeasurementCallbackHidl::gnssMeasurementCb(
621         const hardware::gnss::V1_1::IGnssMeasurementCallback::GnssData& data) {
622     translateAndSetGnssData(data);
623     return hardware::Void();
624 }
625 
GnssMeasurementCb(const hardware::gnss::V1_0::IGnssMeasurementCallback::GnssData & data)626 hardware::Return<void> GnssMeasurementCallbackHidl::GnssMeasurementCb(
627         const hardware::gnss::V1_0::IGnssMeasurementCallback::GnssData& data) {
628     translateAndSetGnssData(data);
629     return hardware::Void();
630 }
631 
632 template <>
getMeasurementCount(const hardware::gnss::V1_0::IGnssMeasurementCallback::GnssData & data)633 size_t GnssMeasurementCallbackHidl::getMeasurementCount<
634         hardware::gnss::V1_0::IGnssMeasurementCallback::GnssData>(
635         const hardware::gnss::V1_0::IGnssMeasurementCallback::GnssData& data) {
636     return data.measurementCount;
637 }
638 
639 // Preallocate object as: JavaObject object(env, "android/location/GnssMeasurement");
640 template <>
translateSingleGnssMeasurement(const hardware::gnss::V1_0::IGnssMeasurementCallback::GnssMeasurement & measurement,JavaObject & object)641 void GnssMeasurementCallbackHidl::translateSingleGnssMeasurement<
642         hardware::gnss::V1_0::IGnssMeasurementCallback::GnssMeasurement>(
643         const hardware::gnss::V1_0::IGnssMeasurementCallback::GnssMeasurement& measurement,
644         JavaObject& object) {
645     setMeasurementFields_V1_0<hardware::gnss::V1_0::IGnssMeasurementCallback::GnssMeasurement,
646                               GnssMeasurementFlags>(measurement, object);
647 
648     SET(ConstellationType, static_cast<int32_t>(measurement.constellation));
649     SET(Cn0DbHz, measurement.cN0DbHz);
650     if (measurement.flags & static_cast<uint32_t>(GnssMeasurementFlags::HAS_CARRIER_FREQUENCY)) {
651         SET(CarrierFrequencyHz, measurement.carrierFrequencyHz);
652     }
653 }
654 
655 // Preallocate object as: JavaObject object(env, "android/location/GnssMeasurement");
656 template <>
translateSingleGnssMeasurement(const hardware::gnss::V1_1::IGnssMeasurementCallback::GnssMeasurement & measurement_V1_1,JavaObject & object)657 void GnssMeasurementCallbackHidl::translateSingleGnssMeasurement<
658         hardware::gnss::V1_1::IGnssMeasurementCallback::GnssMeasurement>(
659         const hardware::gnss::V1_1::IGnssMeasurementCallback::GnssMeasurement& measurement_V1_1,
660         JavaObject& object) {
661     translateSingleGnssMeasurement(measurement_V1_1.v1_0, object);
662 
663     // Half cycle state is reported in HIDL v1.1 or newer.
664     SET(AccumulatedDeltaRangeState,
665         (static_cast<int32_t>(measurement_V1_1.accumulatedDeltaRangeState) |
666          ADR_STATE_HALF_CYCLE_REPORTED));
667 }
668 
669 // Preallocate object as: JavaObject object(env, "android/location/GnssMeasurement");
670 template <>
translateSingleGnssMeasurement(const hardware::gnss::V2_0::IGnssMeasurementCallback::GnssMeasurement & measurement_V2_0,JavaObject & object)671 void GnssMeasurementCallbackHidl::translateSingleGnssMeasurement<
672         hardware::gnss::V2_0::IGnssMeasurementCallback::GnssMeasurement>(
673         const hardware::gnss::V2_0::IGnssMeasurementCallback::GnssMeasurement& measurement_V2_0,
674         JavaObject& object) {
675     JNIEnv* env = getJniEnv();
676     translateSingleGnssMeasurement(measurement_V2_0.v1_1, object);
677 
678     jstring codeType = env->NewStringUTF(measurement_V2_0.codeType.c_str());
679     SET(CodeType, codeType);
680 
681     // Overwrite with v2_0.state since V2_0.v1_1.v1_0.state is deprecated.
682     SET(State, static_cast<int32_t>(measurement_V2_0.state));
683 
684     // Overwrite with v2_0.constellation since V2_0.v1_1.v1_0.constellation is deprecated.
685     SET(ConstellationType, static_cast<int32_t>(measurement_V2_0.constellation));
686 
687     if (codeType) {
688         env->DeleteLocalRef(codeType);
689     }
690 }
691 
692 // Preallocate object as: JavaObject object(env, "android/location/GnssMeasurement");
693 template <>
translateSingleGnssMeasurement(const hardware::gnss::V2_1::IGnssMeasurementCallback::GnssMeasurement & measurement_V2_1,JavaObject & object)694 void GnssMeasurementCallbackHidl::translateSingleGnssMeasurement<
695         hardware::gnss::V2_1::IGnssMeasurementCallback::GnssMeasurement>(
696         const hardware::gnss::V2_1::IGnssMeasurementCallback::GnssMeasurement& measurement_V2_1,
697         JavaObject& object) {
698     translateSingleGnssMeasurement(measurement_V2_1.v2_0, object);
699 
700     setMeasurementFields_V2_1<hardware::gnss::V2_1::IGnssMeasurementCallback::GnssMeasurement,
701                               GnssMeasurementFlags>(measurement_V2_1, object);
702 }
703 
704 template <>
translateGnssClock(const hardware::gnss::V1_0::IGnssMeasurementCallback::GnssClock & clock,JavaObject & object)705 void GnssMeasurementCallbackHidl::translateGnssClock(
706         const hardware::gnss::V1_0::IGnssMeasurementCallback::GnssClock& clock,
707         JavaObject& object) {
708     setClockFields_V1_0<hardware::gnss::V1_0::IGnssMeasurementCallback::GnssClock,
709                         GnssClockFlags>(clock, object);
710 }
711 
712 template <>
translateGnssClock(const hardware::gnss::V2_1::IGnssMeasurementCallback::GnssClock & clock,JavaObject & object)713 void GnssMeasurementCallbackHidl::translateGnssClock(
714         const hardware::gnss::V2_1::IGnssMeasurementCallback::GnssClock& clock,
715         JavaObject& object) {
716     setClockFields_V2_1<hardware::gnss::V2_1::IGnssMeasurementCallback::GnssClock,
717                         GnssClockFlags>(clock, object);
718     translateGnssClock(clock.v1_0, object);
719 }
720 
721 template <>
translateGnssClock(const hardware::gnss::V2_0::IGnssMeasurementCallback::GnssData & data,JavaObject & object)722 void GnssMeasurementCallbackHidl::translateGnssClock(
723         const hardware::gnss::V2_0::IGnssMeasurementCallback::GnssData& data, JavaObject& object) {
724     setElapsedRealtimeFields<hardware::gnss::V2_0::ElapsedRealtime,
725                              hardware::gnss::V2_0::ElapsedRealtimeFlags>(data.elapsedRealtime,
726                                                                          object);
727     translateGnssClock(data.clock, object);
728 }
729 
730 template <>
translateGnssClock(const hardware::gnss::V2_1::IGnssMeasurementCallback::GnssData & data,JavaObject & object)731 void GnssMeasurementCallbackHidl::translateGnssClock(
732         const hardware::gnss::V2_1::IGnssMeasurementCallback::GnssData& data, JavaObject& object) {
733     auto elapsedRealtime = data.elapsedRealtime;
734     uint16_t flags = static_cast<uint16_t>(elapsedRealtime.flags);
735     if (flags & hardware::gnss::V2_0::ElapsedRealtimeFlags::HAS_TIMESTAMP_NS) {
736         SET(ElapsedRealtimeNanos, static_cast<uint64_t>(elapsedRealtime.timestampNs));
737     }
738     if (flags & hardware::gnss::V2_0::ElapsedRealtimeFlags::HAS_TIME_UNCERTAINTY_NS) {
739         SET(ElapsedRealtimeUncertaintyNanos,
740             static_cast<double>(elapsedRealtime.timeUncertaintyNs));
741     }
742     translateGnssClock(data.clock, object);
743 }
744 
745 } // namespace android::gnss
746