• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 before <log/log.h> to overwrite the default value.
18 #define LOG_TAG "GnssJni"
19 
20 #include "Gnss.h"
21 
22 #include <binder/IServiceManager.h>
23 
24 #include "Utils.h"
25 
26 namespace android::gnss {
27 
28 using hardware::Return;
29 
30 using GnssLocationAidl = hardware::gnss::GnssLocation;
31 using GnssLocation_V1_0 = hardware::gnss::V1_0::GnssLocation;
32 using GnssLocation_V2_0 = hardware::gnss::V2_0::GnssLocation;
33 using IAGnssAidl = hardware::gnss::IAGnss;
34 using IAGnssRilAidl = hardware::gnss::IAGnssRil;
35 using IGnssAidl = hardware::gnss::IGnss;
36 using IGnss_V1_0 = hardware::gnss::V1_0::IGnss;
37 using IGnss_V1_1 = hardware::gnss::V1_1::IGnss;
38 using IGnss_V2_0 = hardware::gnss::V2_0::IGnss;
39 using IGnss_V2_1 = hardware::gnss::V2_1::IGnss;
40 using IGnssAntennaInfoAidl = hardware::gnss::IGnssAntennaInfo;
41 using IGnssCallbackAidl = hardware::gnss::IGnssCallback;
42 using IGnssCallback_V1_0 = hardware::gnss::V1_0::IGnssCallback;
43 using IGnssCallback_V2_0 = hardware::gnss::V2_0::IGnssCallback;
44 using IGnssCallback_V2_1 = hardware::gnss::V2_1::IGnssCallback;
45 using IGnssConfigurationAidl = android::hardware::gnss::IGnssConfiguration;
46 using IGnssDebugAidl = hardware::gnss::IGnssDebug;
47 using android::hardware::gnss::IGnssPsds;
48 
49 namespace {
50 
createGnssLocation(jint gnssLocationFlags,jdouble latitudeDegrees,jdouble longitudeDegrees,jdouble altitudeMeters,jfloat speedMetersPerSec,jfloat bearingDegrees,jfloat horizontalAccuracyMeters,jfloat verticalAccuracyMeters,jfloat speedAccuracyMetersPerSecond,jfloat bearingAccuracyDegrees,jlong timestamp,jint elapsedRealtimeFlags,jlong elapsedRealtimeNanos,jdouble elapsedRealtimeUncertaintyNanos)51 GnssLocationAidl createGnssLocation(jint gnssLocationFlags, jdouble latitudeDegrees,
52                                     jdouble longitudeDegrees, jdouble altitudeMeters,
53                                     jfloat speedMetersPerSec, jfloat bearingDegrees,
54                                     jfloat horizontalAccuracyMeters, jfloat verticalAccuracyMeters,
55                                     jfloat speedAccuracyMetersPerSecond,
56                                     jfloat bearingAccuracyDegrees, jlong timestamp,
57                                     jint elapsedRealtimeFlags, jlong elapsedRealtimeNanos,
58                                     jdouble elapsedRealtimeUncertaintyNanos) {
59     GnssLocationAidl location;
60     location.gnssLocationFlags = static_cast<int>(gnssLocationFlags);
61     location.latitudeDegrees = static_cast<double>(latitudeDegrees);
62     location.longitudeDegrees = static_cast<double>(longitudeDegrees);
63     location.altitudeMeters = static_cast<double>(altitudeMeters);
64     location.speedMetersPerSec = static_cast<double>(speedMetersPerSec);
65     location.bearingDegrees = static_cast<double>(bearingDegrees);
66     location.horizontalAccuracyMeters = static_cast<double>(horizontalAccuracyMeters);
67     location.verticalAccuracyMeters = static_cast<double>(verticalAccuracyMeters);
68     location.speedAccuracyMetersPerSecond = static_cast<double>(speedAccuracyMetersPerSecond);
69     location.bearingAccuracyDegrees = static_cast<double>(bearingAccuracyDegrees);
70     location.timestampMillis = static_cast<uint64_t>(timestamp);
71 
72     location.elapsedRealtime.flags = static_cast<int>(elapsedRealtimeFlags);
73     location.elapsedRealtime.timestampNs = static_cast<uint64_t>(elapsedRealtimeNanos);
74     location.elapsedRealtime.timeUncertaintyNs =
75             static_cast<double>(elapsedRealtimeUncertaintyNanos);
76 
77     return location;
78 }
79 
createGnssLocation_V1_0(jint gnssLocationFlags,jdouble latitudeDegrees,jdouble longitudeDegrees,jdouble altitudeMeters,jfloat speedMetersPerSec,jfloat bearingDegrees,jfloat horizontalAccuracyMeters,jfloat verticalAccuracyMeters,jfloat speedAccuracyMetersPerSecond,jfloat bearingAccuracyDegrees,jlong timestamp)80 GnssLocation_V1_0 createGnssLocation_V1_0(jint gnssLocationFlags, jdouble latitudeDegrees,
81                                           jdouble longitudeDegrees, jdouble altitudeMeters,
82                                           jfloat speedMetersPerSec, jfloat bearingDegrees,
83                                           jfloat horizontalAccuracyMeters,
84                                           jfloat verticalAccuracyMeters,
85                                           jfloat speedAccuracyMetersPerSecond,
86                                           jfloat bearingAccuracyDegrees, jlong timestamp) {
87     GnssLocation_V1_0 location;
88     location.gnssLocationFlags = static_cast<uint16_t>(gnssLocationFlags);
89     location.latitudeDegrees = static_cast<double>(latitudeDegrees);
90     location.longitudeDegrees = static_cast<double>(longitudeDegrees);
91     location.altitudeMeters = static_cast<double>(altitudeMeters);
92     location.speedMetersPerSec = static_cast<float>(speedMetersPerSec);
93     location.bearingDegrees = static_cast<float>(bearingDegrees);
94     location.horizontalAccuracyMeters = static_cast<float>(horizontalAccuracyMeters);
95     location.verticalAccuracyMeters = static_cast<float>(verticalAccuracyMeters);
96     location.speedAccuracyMetersPerSecond = static_cast<float>(speedAccuracyMetersPerSecond);
97     location.bearingAccuracyDegrees = static_cast<float>(bearingAccuracyDegrees);
98     location.timestamp = static_cast<uint64_t>(timestamp);
99 
100     return location;
101 }
102 
createGnssLocation_V2_0(jint gnssLocationFlags,jdouble latitudeDegrees,jdouble longitudeDegrees,jdouble altitudeMeters,jfloat speedMetersPerSec,jfloat bearingDegrees,jfloat horizontalAccuracyMeters,jfloat verticalAccuracyMeters,jfloat speedAccuracyMetersPerSecond,jfloat bearingAccuracyDegrees,jlong timestamp,jint elapsedRealtimeFlags,jlong elapsedRealtimeNanos,jdouble elapsedRealtimeUncertaintyNanos)103 GnssLocation_V2_0 createGnssLocation_V2_0(jint gnssLocationFlags, jdouble latitudeDegrees,
104                                           jdouble longitudeDegrees, jdouble altitudeMeters,
105                                           jfloat speedMetersPerSec, jfloat bearingDegrees,
106                                           jfloat horizontalAccuracyMeters,
107                                           jfloat verticalAccuracyMeters,
108                                           jfloat speedAccuracyMetersPerSecond,
109                                           jfloat bearingAccuracyDegrees, jlong timestamp,
110                                           jint elapsedRealtimeFlags, jlong elapsedRealtimeNanos,
111                                           jdouble elapsedRealtimeUncertaintyNanos) {
112     GnssLocation_V2_0 location;
113     location.v1_0 = createGnssLocation_V1_0(gnssLocationFlags, latitudeDegrees, longitudeDegrees,
114                                             altitudeMeters, speedMetersPerSec, bearingDegrees,
115                                             horizontalAccuracyMeters, verticalAccuracyMeters,
116                                             speedAccuracyMetersPerSecond, bearingAccuracyDegrees,
117                                             timestamp);
118 
119     location.elapsedRealtime.flags = static_cast<uint16_t>(elapsedRealtimeFlags);
120     location.elapsedRealtime.timestampNs = static_cast<uint64_t>(elapsedRealtimeNanos);
121     location.elapsedRealtime.timeUncertaintyNs =
122             static_cast<uint64_t>(elapsedRealtimeUncertaintyNanos);
123 
124     return location;
125 }
126 
127 } // anonymous namespace
128 
129 // Implementation of GnssHal, which unifies all versions of GNSS HALs
130 
GnssHal()131 GnssHal::GnssHal() {
132     gnssHalAidl = waitForVintfService<IGnssAidl>();
133     if (gnssHalAidl != nullptr) {
134         ALOGD("Successfully got GNSS AIDL handle. Version=%d.", gnssHalAidl->getInterfaceVersion());
135         if (gnssHalAidl->getInterfaceVersion() >= 2) {
136             return;
137         }
138     }
139 
140     ALOGD("Trying IGnss_V2_1::getService()");
141     gnssHal_V2_1 = IGnss_V2_1::getService();
142     if (gnssHal_V2_1 != nullptr) {
143         gnssHal_V2_0 = gnssHal_V2_1;
144         gnssHal_V1_1 = gnssHal_V2_1;
145         gnssHal = gnssHal_V2_1;
146         return;
147     }
148 
149     ALOGD("gnssHal 2.1 was null, trying 2.0");
150     gnssHal_V2_0 = IGnss_V2_0::getService();
151     if (gnssHal_V2_0 != nullptr) {
152         gnssHal_V1_1 = gnssHal_V2_0;
153         gnssHal = gnssHal_V2_0;
154         return;
155     }
156 
157     ALOGD("gnssHal 2.0 was null, trying 1.1");
158     gnssHal_V1_1 = IGnss_V1_1::getService();
159     if (gnssHal_V1_1 != nullptr) {
160         gnssHal = gnssHal_V1_1;
161         return;
162     }
163 
164     ALOGD("gnssHal 1.1 was null, trying 1.0");
165     gnssHal = IGnss_V1_0::getService();
166 }
167 
isSupported()168 jboolean GnssHal::isSupported() {
169     return (gnssHalAidl != nullptr || gnssHal != nullptr) ? JNI_TRUE : JNI_FALSE;
170 }
171 
linkToDeath()172 void GnssHal::linkToDeath() {
173     if (gnssHalAidl != nullptr) {
174         gnssHalDeathRecipientAidl = new GnssDeathRecipientAidl();
175         status_t linked = IInterface::asBinder(gnssHalAidl)->linkToDeath(gnssHalDeathRecipientAidl);
176         if (linked != OK) {
177             ALOGE("Unable to link to GNSS AIDL HAL death notification");
178         } else {
179             ALOGD("Successfully linked to GNSS AIDL HAl death notification");
180         }
181     }
182 
183     if (gnssHal != nullptr) {
184         gnssHalDeathRecipient = new GnssDeathRecipient();
185         hardware::Return<bool> linked = gnssHal->linkToDeath(gnssHalDeathRecipient, /*cookie*/ 0);
186         if (!linked.isOk()) {
187             ALOGE("Transaction error in linking to GnssHAL death: %s",
188                   linked.description().c_str());
189         } else if (!linked) {
190             ALOGW("Unable to link to GnssHal death notifications");
191         } else {
192             ALOGD("Link to death notification successful");
193         }
194     }
195 }
196 
setCallback()197 jboolean GnssHal::setCallback() {
198     if (gnssHalAidl != nullptr) {
199         sp<IGnssCallbackAidl> gnssCbIfaceAidl =
200                 new GnssCallbackAidl(gnssHalAidl->getInterfaceVersion());
201         auto status = gnssHalAidl->setCallback(gnssCbIfaceAidl);
202         if (!checkAidlStatus(status, "IGnssAidl setCallback() failed.")) {
203             return JNI_FALSE;
204         }
205     }
206     if (gnssHal != nullptr) {
207         Return<bool> result = false;
208         sp<IGnssCallback_V2_1> gnssCbIface = new GnssCallbackHidl();
209         if (gnssHal_V2_1 != nullptr) {
210             result = gnssHal_V2_1->setCallback_2_1(gnssCbIface);
211         } else if (gnssHal_V2_0 != nullptr) {
212             result = gnssHal_V2_0->setCallback_2_0(gnssCbIface);
213         } else if (gnssHal_V1_1 != nullptr) {
214             result = gnssHal_V1_1->setCallback_1_1(gnssCbIface);
215         } else {
216             result = gnssHal->setCallback(gnssCbIface);
217         }
218         if (!checkHidlReturn(result, "IGnss setCallback() failed.")) {
219             return JNI_FALSE;
220         }
221     }
222     return JNI_TRUE;
223 }
224 
close()225 void GnssHal::close() {
226     if (gnssHalAidl != nullptr) {
227         auto status = gnssHalAidl->close();
228         checkAidlStatus(status, "IGnssAidl close() failed.");
229     }
230 
231     if (gnssHal != nullptr) {
232         auto result = gnssHal->cleanup();
233         checkHidlReturn(result, "IGnss cleanup() failed.");
234     }
235 }
236 
start()237 jboolean GnssHal::start() {
238     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
239         auto status = gnssHalAidl->start();
240         return checkAidlStatus(status, "IGnssAidl start() failed.");
241     }
242 
243     if (gnssHal == nullptr) {
244         return JNI_FALSE;
245     }
246 
247     auto result = gnssHal->start();
248     return checkHidlReturn(result, "IGnss start() failed.");
249 }
250 
stop()251 jboolean GnssHal::stop() {
252     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
253         auto status = gnssHalAidl->stop();
254         return checkAidlStatus(status, "IGnssAidl stop() failed.");
255     }
256 
257     if (gnssHal == nullptr) {
258         return JNI_FALSE;
259     }
260 
261     auto result = gnssHal->stop();
262     return checkHidlReturn(result, "IGnss stop() failed.");
263 }
264 
startSvStatus()265 jboolean GnssHal::startSvStatus() {
266     isSvStatusRegistered = true;
267     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
268         auto status = gnssHalAidl->startSvStatus();
269         return checkAidlStatus(status, "IGnssAidl startSvStatus() failed.");
270     }
271     if (gnssHal == nullptr) {
272         return JNI_FALSE;
273     }
274     return JNI_TRUE;
275 }
276 
stopSvStatus()277 jboolean GnssHal::stopSvStatus() {
278     isSvStatusRegistered = false;
279     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
280         auto status = gnssHalAidl->stopSvStatus();
281         return checkAidlStatus(status, "IGnssAidl stopSvStatus() failed.");
282     }
283     if (gnssHal == nullptr) {
284         return JNI_FALSE;
285     }
286     return JNI_TRUE;
287 }
288 
startNmea()289 jboolean GnssHal::startNmea() {
290     isNmeaRegistered = true;
291     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
292         auto status = gnssHalAidl->startNmea();
293         return checkAidlStatus(status, "IGnssAidl startNmea() failed.");
294     }
295     if (gnssHal == nullptr) {
296         return JNI_FALSE;
297     }
298     return JNI_TRUE;
299 }
300 
stopNmea()301 jboolean GnssHal::stopNmea() {
302     isNmeaRegistered = false;
303     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
304         auto status = gnssHalAidl->stopNmea();
305         return checkAidlStatus(status, "IGnssAidl stopNmea() failed.");
306     }
307     if (gnssHal == nullptr) {
308         return JNI_FALSE;
309     }
310     return JNI_TRUE;
311 }
312 
readNmea(jbyteArray & nmeaArray,jint & buffer_size)313 jint GnssHal::readNmea(jbyteArray& nmeaArray, jint& buffer_size) {
314     // this should only be called from within a call to reportNmea
315     JNIEnv* env = getJniEnv();
316     jbyte* nmea = reinterpret_cast<jbyte*>(env->GetPrimitiveArrayCritical(nmeaArray, 0));
317     int length = GnssCallbackHidl::sNmeaStringLength;
318     if (length > buffer_size) {
319         length = buffer_size;
320     }
321     memcpy(nmea, GnssCallbackHidl::sNmeaString, length);
322     env->ReleasePrimitiveArrayCritical(nmeaArray, nmea, JNI_ABORT);
323     return (jint)length;
324 }
325 
setPositionMode(jint mode,jint recurrence,jint min_interval,jint preferred_accuracy,jint preferred_time,jboolean low_power_mode)326 jboolean GnssHal::setPositionMode(jint mode, jint recurrence, jint min_interval,
327                                   jint preferred_accuracy, jint preferred_time,
328                                   jboolean low_power_mode) {
329     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
330         IGnssAidl::PositionModeOptions options;
331         options.mode = static_cast<IGnssAidl::GnssPositionMode>(mode);
332         options.recurrence = static_cast<IGnssAidl::GnssPositionRecurrence>(recurrence);
333         options.minIntervalMs = min_interval;
334         options.preferredAccuracyMeters = preferred_accuracy;
335         options.preferredTimeMs = preferred_time;
336         options.lowPowerMode = low_power_mode;
337         auto status = gnssHalAidl->setPositionMode(options);
338         return checkAidlStatus(status, "IGnssAidl setPositionMode() failed.");
339     }
340 
341     Return<bool> result = false;
342     if (gnssHal_V1_1 != nullptr) {
343         result = gnssHal_V1_1->setPositionMode_1_1(static_cast<IGnss_V1_0::GnssPositionMode>(mode),
344                                                    static_cast<IGnss_V1_0::GnssPositionRecurrence>(
345                                                            recurrence),
346                                                    min_interval, preferred_accuracy, preferred_time,
347                                                    low_power_mode);
348     } else if (gnssHal != nullptr) {
349         result = gnssHal->setPositionMode(static_cast<IGnss_V1_0::GnssPositionMode>(mode),
350                                           static_cast<IGnss_V1_0::GnssPositionRecurrence>(
351                                                   recurrence),
352                                           min_interval, preferred_accuracy, preferred_time);
353     }
354     return checkHidlReturn(result, "IGnss setPositionMode() failed.");
355 }
356 
deleteAidingData(jint flags)357 void GnssHal::deleteAidingData(jint flags) {
358     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
359         auto status = gnssHalAidl->deleteAidingData(static_cast<IGnssAidl::GnssAidingData>(flags));
360         checkAidlStatus(status, "IGnssAidl deleteAidingData() failed.");
361         return;
362     }
363 
364     if (gnssHal == nullptr) {
365         return;
366     }
367 
368     auto result = gnssHal->deleteAidingData(static_cast<IGnss_V1_0::GnssAidingData>(flags));
369     checkHidlReturn(result, "IGnss deleteAidingData() failed.");
370 }
371 
injectTime(jlong time,jlong timeReference,jint uncertainty)372 void GnssHal::injectTime(jlong time, jlong timeReference, jint uncertainty) {
373     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
374         auto status = gnssHalAidl->injectTime(time, timeReference, uncertainty);
375         checkAidlStatus(status, "IGnssAidl injectTime() failed.");
376         return;
377     }
378 
379     if (gnssHal == nullptr) {
380         return;
381     }
382     auto result = gnssHal->injectTime(time, timeReference, uncertainty);
383     checkHidlReturn(result, "IGnss injectTime() failed.");
384 }
385 
injectLocation(jint gnssLocationFlags,jdouble latitudeDegrees,jdouble longitudeDegrees,jdouble altitudeMeters,jfloat speedMetersPerSec,jfloat bearingDegrees,jfloat horizontalAccuracyMeters,jfloat verticalAccuracyMeters,jfloat speedAccuracyMetersPerSecond,jfloat bearingAccuracyDegrees,jlong timestamp,jint elapsedRealtimeFlags,jlong elapsedRealtimeNanos,jdouble elapsedRealtimeUncertaintyNanos)386 void GnssHal::injectLocation(jint gnssLocationFlags, jdouble latitudeDegrees,
387                              jdouble longitudeDegrees, jdouble altitudeMeters,
388                              jfloat speedMetersPerSec, jfloat bearingDegrees,
389                              jfloat horizontalAccuracyMeters, jfloat verticalAccuracyMeters,
390                              jfloat speedAccuracyMetersPerSecond, jfloat bearingAccuracyDegrees,
391                              jlong timestamp, jint elapsedRealtimeFlags, jlong elapsedRealtimeNanos,
392                              jdouble elapsedRealtimeUncertaintyNanos) {
393     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
394         GnssLocationAidl location =
395                 createGnssLocation(gnssLocationFlags, latitudeDegrees, longitudeDegrees,
396                                    altitudeMeters, speedMetersPerSec, bearingDegrees,
397                                    horizontalAccuracyMeters, verticalAccuracyMeters,
398                                    speedAccuracyMetersPerSecond, bearingAccuracyDegrees, timestamp,
399                                    elapsedRealtimeFlags, elapsedRealtimeNanos,
400                                    elapsedRealtimeUncertaintyNanos);
401         auto status = gnssHalAidl->injectLocation(location);
402         checkAidlStatus(status, "IGnssAidl injectLocation() failed.");
403         return;
404     }
405 
406     if (gnssHal == nullptr) {
407         return;
408     }
409     auto result =
410             gnssHal->injectLocation(latitudeDegrees, longitudeDegrees, horizontalAccuracyMeters);
411     checkHidlReturn(result, "IGnss injectLocation() failed.");
412 }
413 
injectBestLocation(jint gnssLocationFlags,jdouble latitudeDegrees,jdouble longitudeDegrees,jdouble altitudeMeters,jfloat speedMetersPerSec,jfloat bearingDegrees,jfloat horizontalAccuracyMeters,jfloat verticalAccuracyMeters,jfloat speedAccuracyMetersPerSecond,jfloat bearingAccuracyDegrees,jlong timestamp,jint elapsedRealtimeFlags,jlong elapsedRealtimeNanos,jdouble elapsedRealtimeUncertaintyNanos)414 void GnssHal::injectBestLocation(jint gnssLocationFlags, jdouble latitudeDegrees,
415                                  jdouble longitudeDegrees, jdouble altitudeMeters,
416                                  jfloat speedMetersPerSec, jfloat bearingDegrees,
417                                  jfloat horizontalAccuracyMeters, jfloat verticalAccuracyMeters,
418                                  jfloat speedAccuracyMetersPerSecond, jfloat bearingAccuracyDegrees,
419                                  jlong timestamp, jint elapsedRealtimeFlags,
420                                  jlong elapsedRealtimeNanos,
421                                  jdouble elapsedRealtimeUncertaintyNanos) {
422     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
423         GnssLocationAidl location =
424                 createGnssLocation(gnssLocationFlags, latitudeDegrees, longitudeDegrees,
425                                    altitudeMeters, speedMetersPerSec, bearingDegrees,
426                                    horizontalAccuracyMeters, verticalAccuracyMeters,
427                                    speedAccuracyMetersPerSecond, bearingAccuracyDegrees, timestamp,
428                                    elapsedRealtimeFlags, elapsedRealtimeNanos,
429                                    elapsedRealtimeUncertaintyNanos);
430         auto status = gnssHalAidl->injectBestLocation(location);
431         checkAidlStatus(status, "IGnssAidl injectBestLocation() failed.");
432         return;
433     }
434 
435     if (gnssHal_V2_0 != nullptr) {
436         GnssLocation_V2_0 location =
437                 createGnssLocation_V2_0(gnssLocationFlags, latitudeDegrees, longitudeDegrees,
438                                         altitudeMeters, speedMetersPerSec, bearingDegrees,
439                                         horizontalAccuracyMeters, verticalAccuracyMeters,
440                                         speedAccuracyMetersPerSecond, bearingAccuracyDegrees,
441                                         timestamp, elapsedRealtimeFlags, elapsedRealtimeNanos,
442                                         elapsedRealtimeUncertaintyNanos);
443         auto result = gnssHal_V2_0->injectBestLocation_2_0(location);
444         checkHidlReturn(result, "IGnss injectBestLocation_2_0() failed.");
445         return;
446     }
447 
448     if (gnssHal_V1_1 != nullptr) {
449         GnssLocation_V1_0 location =
450                 createGnssLocation_V1_0(gnssLocationFlags, latitudeDegrees, longitudeDegrees,
451                                         altitudeMeters, speedMetersPerSec, bearingDegrees,
452                                         horizontalAccuracyMeters, verticalAccuracyMeters,
453                                         speedAccuracyMetersPerSecond, bearingAccuracyDegrees,
454                                         timestamp);
455         auto result = gnssHal_V1_1->injectBestLocation(location);
456         checkHidlReturn(result, "IGnss injectBestLocation() failed.");
457         return;
458     }
459 
460     ALOGE("IGnss injectBestLocation() is called but gnssHal_V1_1 is not available.");
461 }
462 
getAGnssInterface()463 std::unique_ptr<AGnssInterface> GnssHal::getAGnssInterface() {
464     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
465         sp<IAGnssAidl> agnssAidl;
466         auto status = gnssHalAidl->getExtensionAGnss(&agnssAidl);
467         if (checkAidlStatus(status, "Unable to get a handle to AGnss interface.")) {
468             return std::make_unique<gnss::AGnss>(agnssAidl);
469         }
470     } else if (gnssHal_V2_0 != nullptr) {
471         auto agnss_V2_0 = gnssHal_V2_0->getExtensionAGnss_2_0();
472         if (checkHidlReturn(agnss_V2_0, "Unable to get a handle to AGnss_V2_0")) {
473             return std::make_unique<gnss::AGnss_V2_0>(agnss_V2_0);
474         }
475     } else if (gnssHal != nullptr) {
476         auto agnss_V1_0 = gnssHal->getExtensionAGnss();
477         if (checkHidlReturn(agnss_V1_0, "Unable to get a handle to AGnss_V1_0")) {
478             return std::make_unique<gnss::AGnss_V1_0>(agnss_V1_0);
479         }
480     }
481     return nullptr;
482 }
483 
getAGnssRilInterface()484 std::unique_ptr<AGnssRilInterface> GnssHal::getAGnssRilInterface() {
485     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
486         sp<IAGnssRilAidl> agnssRilAidl;
487         auto status = gnssHalAidl->getExtensionAGnssRil(&agnssRilAidl);
488         if (checkAidlStatus(status, "Unable to get a handle to AGnssRil interface.")) {
489             return std::make_unique<gnss::AGnssRil>(agnssRilAidl);
490         }
491     } else if (gnssHal_V2_0 != nullptr) {
492         auto agnssRil_V2_0 = gnssHal_V2_0->getExtensionAGnssRil_2_0();
493         if (checkHidlReturn(agnssRil_V2_0, "Unable to get a handle to AGnssRil_V2_0")) {
494             return std::make_unique<gnss::AGnssRil_V2_0>(agnssRil_V2_0);
495         }
496     } else if (gnssHal != nullptr) {
497         auto agnssRil_V1_0 = gnssHal->getExtensionAGnssRil();
498         if (checkHidlReturn(agnssRil_V1_0, "Unable to get a handle to AGnssRil_V1_0")) {
499             return std::make_unique<gnss::AGnssRil_V1_0>(agnssRil_V1_0);
500         }
501     }
502     return nullptr;
503 }
504 
getGnssNavigationMessageInterface()505 std::unique_ptr<GnssNavigationMessageInterface> GnssHal::getGnssNavigationMessageInterface() {
506     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
507         sp<hardware::gnss::IGnssNavigationMessageInterface> gnssNavigationMessage;
508         auto status = gnssHalAidl->getExtensionGnssNavigationMessage(&gnssNavigationMessage);
509         if (checkAidlStatus(status,
510                             "Unable to get a handle to GnssNavigationMessage AIDL interface.")) {
511             return std::make_unique<gnss::GnssNavigationMessageAidl>(gnssNavigationMessage);
512         }
513     } else if (gnssHal != nullptr) {
514         auto gnssNavigationMessage = gnssHal->getExtensionGnssNavigationMessage();
515         if (checkHidlReturn(gnssNavigationMessage,
516                             "Unable to get a handle to GnssNavigationMessage interface.")) {
517             return std::make_unique<gnss::GnssNavigationMessageHidl>(gnssNavigationMessage);
518         }
519     }
520     return nullptr;
521 }
522 
getGnssMeasurementInterface()523 std::unique_ptr<GnssMeasurementInterface> GnssHal::getGnssMeasurementInterface() {
524     // Allow all causal combinations between IGnss.hal and IGnssMeasurement.hal. That means,
525     // 2.1@IGnss can be paired with {1.0, 1,1, 2.0, 2.1}@IGnssMeasurement
526     // 2.0@IGnss can be paired with {1.0, 1,1, 2.0}@IGnssMeasurement
527     // 1.1@IGnss can be paired {1.0, 1.1}@IGnssMeasurement
528     // 1.0@IGnss is paired with 1.0@IGnssMeasurement
529     if (gnssHalAidl != nullptr) {
530         sp<hardware::gnss::IGnssMeasurementInterface> gnssMeasurement;
531         auto status = gnssHalAidl->getExtensionGnssMeasurement(&gnssMeasurement);
532         if (checkAidlStatus(status, "Unable to get a handle to GnssMeasurement AIDL interface.")) {
533             return std::make_unique<android::gnss::GnssMeasurement>(gnssMeasurement);
534         }
535     }
536     if (gnssHal_V2_1 != nullptr) {
537         auto gnssMeasurement = gnssHal_V2_1->getExtensionGnssMeasurement_2_1();
538         if (checkHidlReturn(gnssMeasurement, "Unable to get a handle to GnssMeasurement_V2_1")) {
539             return std::make_unique<android::gnss::GnssMeasurement_V2_1>(gnssMeasurement);
540         }
541     }
542     if (gnssHal_V2_0 != nullptr) {
543         auto gnssMeasurement = gnssHal_V2_0->getExtensionGnssMeasurement_2_0();
544         if (checkHidlReturn(gnssMeasurement, "Unable to get a handle to GnssMeasurement_V2_0")) {
545             return std::make_unique<android::gnss::GnssMeasurement_V2_0>(gnssMeasurement);
546         }
547     }
548     if (gnssHal_V1_1 != nullptr) {
549         auto gnssMeasurement = gnssHal_V1_1->getExtensionGnssMeasurement_1_1();
550         if (checkHidlReturn(gnssMeasurement, "Unable to get a handle to GnssMeasurement_V1_1")) {
551             return std::make_unique<android::gnss::GnssMeasurement_V1_1>(gnssMeasurement);
552         }
553     }
554     if (gnssHal != nullptr) {
555         auto gnssMeasurement = gnssHal->getExtensionGnssMeasurement();
556         if (checkHidlReturn(gnssMeasurement, "Unable to get a handle to GnssMeasurement_V1_0")) {
557             return std::make_unique<android::gnss::GnssMeasurement_V1_0>(gnssMeasurement);
558         }
559     }
560     return nullptr;
561 }
562 
getGnssDebugInterface()563 std::unique_ptr<GnssDebugInterface> GnssHal::getGnssDebugInterface() {
564     // Allow all causal combinations between IGnss.hal and IGnssDebug.hal. That means,
565     // 2.0@IGnss can be paired with {1.0, 2.0}@IGnssDebug
566     // 1.0@IGnss is paired with 1.0@IGnssDebug
567     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
568         sp<IGnssDebugAidl> gnssDebugAidl;
569         auto status = gnssHalAidl->getExtensionGnssDebug(&gnssDebugAidl);
570         if (checkAidlStatus(status, "Unable to get a handle to GnssDebug interface.")) {
571             return std::make_unique<gnss::GnssDebug>(gnssDebugAidl);
572         }
573     }
574     if (gnssHal_V2_0 != nullptr) {
575         auto gnssDebug_V2_0 = gnssHal_V2_0->getExtensionGnssDebug_2_0();
576         if (checkHidlReturn(gnssDebug_V2_0, "Unable to get a handle to GnssDebug_V2_0.")) {
577             return std::make_unique<gnss::GnssDebug_V2_0>(gnssDebug_V2_0);
578         }
579     }
580     if (gnssHal != nullptr) {
581         auto gnssDebug_V1_0 = gnssHal->getExtensionGnssDebug();
582         if (checkHidlReturn(gnssDebug_V1_0, "Unable to get a handle to GnssDebug_V1_0.")) {
583             return std::make_unique<gnss::GnssDebug_V1_0>(gnssDebug_V1_0);
584         }
585     }
586     return nullptr;
587 }
588 
getGnssConfigurationInterface()589 std::unique_ptr<GnssConfigurationInterface> GnssHal::getGnssConfigurationInterface() {
590     if (gnssHalAidl != nullptr) {
591         sp<IGnssConfigurationAidl> gnssConfigurationAidl;
592         auto status = gnssHalAidl->getExtensionGnssConfiguration(&gnssConfigurationAidl);
593         if (checkAidlStatus(status,
594                             "Unable to get a handle to GnssConfiguration AIDL interface.")) {
595             return std::make_unique<android::gnss::GnssConfiguration>(gnssConfigurationAidl);
596         }
597     } else if (gnssHal_V2_1 != nullptr) {
598         auto gnssConfiguration = gnssHal_V2_1->getExtensionGnssConfiguration_2_1();
599         if (checkHidlReturn(gnssConfiguration,
600                             "Unable to get a handle to GnssConfiguration_V2_1")) {
601             return std::make_unique<android::gnss::GnssConfiguration_V2_1>(gnssConfiguration);
602         }
603     } else if (gnssHal_V2_0 != nullptr) {
604         auto gnssConfiguration = gnssHal_V2_0->getExtensionGnssConfiguration_2_0();
605         if (checkHidlReturn(gnssConfiguration,
606                             "Unable to get a handle to GnssConfiguration_V2_0")) {
607             return std::make_unique<android::gnss::GnssConfiguration_V2_0>(gnssConfiguration);
608         }
609     } else if (gnssHal_V1_1 != nullptr) {
610         auto gnssConfiguration = gnssHal_V1_1->getExtensionGnssConfiguration_1_1();
611         if (checkHidlReturn(gnssConfiguration,
612                             "Unable to get a handle to GnssConfiguration_V1_1")) {
613             return std::make_unique<gnss::GnssConfiguration_V1_1>(gnssConfiguration);
614         }
615     } else if (gnssHal != nullptr) {
616         auto gnssConfiguration = gnssHal->getExtensionGnssConfiguration();
617         if (checkHidlReturn(gnssConfiguration,
618                             "Unable to get a handle to GnssConfiguration_V1_0")) {
619             return std::make_unique<gnss::GnssConfiguration_V1_0>(gnssConfiguration);
620         }
621     }
622     return nullptr;
623 }
624 
getGnssGeofenceInterface()625 std::unique_ptr<GnssGeofenceInterface> GnssHal::getGnssGeofenceInterface() {
626     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
627         sp<hardware::gnss::IGnssGeofence> gnssGeofence;
628         auto status = gnssHalAidl->getExtensionGnssGeofence(&gnssGeofence);
629         if (checkAidlStatus(status, "Unable to get a handle to GnssGeofence AIDL interface.")) {
630             return std::make_unique<gnss::GnssGeofenceAidl>(gnssGeofence);
631         }
632     } else if (gnssHal != nullptr) {
633         auto gnssGeofencing = gnssHal->getExtensionGnssGeofencing();
634         if (checkHidlReturn(gnssGeofencing, "Unable to get a handle to GnssGeofencing")) {
635             return std::make_unique<gnss::GnssGeofenceHidl>(gnssGeofencing);
636         }
637     }
638     return nullptr;
639 }
640 
getGnssBatchingInterface()641 std::unique_ptr<GnssBatchingInterface> GnssHal::getGnssBatchingInterface() {
642     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
643         sp<android::hardware::gnss::IGnssBatching> gnssBatchingAidl;
644         auto status = gnssHalAidl->getExtensionGnssBatching(&gnssBatchingAidl);
645         if (checkAidlStatus(status, "Unable to get a handle to GnssBatching interface.")) {
646             return std::make_unique<gnss::GnssBatching>(gnssBatchingAidl);
647         }
648     }
649     if (gnssHal_V2_0 != nullptr) {
650         auto gnssBatching_V2_0 = gnssHal_V2_0->getExtensionGnssBatching_2_0();
651         if (checkHidlReturn(gnssBatching_V2_0, "Unable to get a handle to GnssBatching_V2_0")) {
652             return std::make_unique<gnss::GnssBatching_V2_0>(gnssBatching_V2_0);
653         }
654     }
655     if (gnssHal != nullptr) {
656         auto gnssBatching_V1_0 = gnssHal->getExtensionGnssBatching();
657         if (checkHidlReturn(gnssBatching_V1_0, "Unable to get a handle to GnssBatching")) {
658             return std::make_unique<gnss::GnssBatching_V1_0>(gnssBatching_V1_0);
659         }
660     }
661     return nullptr;
662 }
663 
getMeasurementCorrectionsInterface()664 std::unique_ptr<MeasurementCorrectionsInterface> GnssHal::getMeasurementCorrectionsInterface() {
665     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
666         sp<android::hardware::gnss::measurement_corrections::IMeasurementCorrectionsInterface>
667                 gnssMeasurementCorrectionsAidl;
668         auto status =
669                 gnssHalAidl->getExtensionMeasurementCorrections(&gnssMeasurementCorrectionsAidl);
670         if (checkAidlStatus(status,
671                             "Unable to get a handle to GnssVisibilityControl AIDL interface.")) {
672             return std::make_unique<gnss::MeasurementCorrectionsIface_Aidl>(
673                     gnssMeasurementCorrectionsAidl);
674         }
675     }
676     if (gnssHal_V2_1 != nullptr) {
677         auto gnssCorrections = gnssHal_V2_1->getExtensionMeasurementCorrections_1_1();
678         if (checkHidlReturn(gnssCorrections,
679                             "Unable to get a handle to GnssMeasurementCorrections HIDL "
680                             "interface")) {
681             return std::make_unique<gnss::MeasurementCorrectionsIface_V1_1>(gnssCorrections);
682         }
683     }
684     if (gnssHal_V2_0 != nullptr) {
685         auto gnssCorrections = gnssHal_V2_0->getExtensionMeasurementCorrections();
686         if (checkHidlReturn(gnssCorrections,
687                             "Unable to get a handle to GnssMeasurementCorrections HIDL "
688                             "interface")) {
689             return std::make_unique<gnss::MeasurementCorrectionsIface_V1_0>(gnssCorrections);
690         }
691     }
692     return nullptr;
693 }
694 
getGnssVisibilityControlInterface()695 std::unique_ptr<GnssVisibilityControlInterface> GnssHal::getGnssVisibilityControlInterface() {
696     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
697         sp<android::hardware::gnss::visibility_control::IGnssVisibilityControl>
698                 gnssVisibilityControlAidl;
699         auto status = gnssHalAidl->getExtensionGnssVisibilityControl(&gnssVisibilityControlAidl);
700         if (checkAidlStatus(status,
701                             "Unable to get a handle to GnssVisibilityControl AIDL interface.")) {
702             return std::make_unique<gnss::GnssVisibilityControlAidl>(gnssVisibilityControlAidl);
703         }
704     } else if (gnssHal_V2_0 != nullptr) {
705         auto gnssVisibilityControlHidl = gnssHal_V2_0->getExtensionVisibilityControl();
706         if (checkHidlReturn(gnssVisibilityControlHidl,
707                             "Unable to get a handle to GnssVisibilityControl HIDL interface")) {
708             return std::make_unique<gnss::GnssVisibilityControlHidl>(gnssVisibilityControlHidl);
709         }
710     }
711     return nullptr;
712 }
713 
getGnssAntennaInfoInterface()714 std::unique_ptr<GnssAntennaInfoInterface> GnssHal::getGnssAntennaInfoInterface() {
715     if (gnssHalAidl != nullptr && gnssHalAidl->getInterfaceVersion() >= 2) {
716         sp<IGnssAntennaInfoAidl> gnssAntennaInfoAidl;
717         auto status = gnssHalAidl->getExtensionGnssAntennaInfo(&gnssAntennaInfoAidl);
718         if (checkAidlStatus(status, "Unable to get a handle to GnssAntennaInfo interface.")) {
719             return std::make_unique<gnss::GnssAntennaInfoAidl>(gnssAntennaInfoAidl);
720         }
721     } else if (gnssHal_V2_1 != nullptr) {
722         auto gnssAntennaInfo_V2_1 = gnssHal_V2_1->getExtensionGnssAntennaInfo();
723         if (checkHidlReturn(gnssAntennaInfo_V2_1,
724                             "Unable to get a handle to GnssAntennaInfo_V2_1")) {
725             return std::make_unique<gnss::GnssAntennaInfo_V2_1>(gnssAntennaInfo_V2_1);
726         }
727     }
728     return nullptr;
729 }
730 
getGnssPsdsInterface()731 std::unique_ptr<GnssPsdsInterface> GnssHal::getGnssPsdsInterface() {
732     if (gnssHalAidl != nullptr) {
733         sp<IGnssPsds> gnssPsdsAidl;
734         auto status = gnssHalAidl->getExtensionPsds(&gnssPsdsAidl);
735         if (checkAidlStatus(status, "Unable to get a handle to PSDS interface.")) {
736             return std::make_unique<gnss::GnssPsdsAidl>(gnssPsdsAidl);
737         }
738     } else if (gnssHal != nullptr) {
739         auto gnssXtra = gnssHal->getExtensionXtra();
740         if (checkHidlReturn(gnssXtra, "Unable to get a handle to XTRA interface.")) {
741             return std::make_unique<gnss::GnssPsdsHidl>(gnssXtra);
742         }
743     }
744     return nullptr;
745 }
746 
getGnssPowerIndicationInterface()747 sp<hardware::gnss::IGnssPowerIndication> GnssHal::getGnssPowerIndicationInterface() {
748     if (gnssHalAidl != nullptr) {
749         sp<hardware::gnss::IGnssPowerIndication> gnssPowerIndication;
750         auto status = gnssHalAidl->getExtensionGnssPowerIndication(&gnssPowerIndication);
751         if (checkAidlStatus(status, "Unable to get a handle to GnssPowerIndication")) {
752             return gnssPowerIndication;
753         }
754     }
755     return nullptr;
756 }
757 
getGnssNiInterface()758 sp<hardware::gnss::V1_0::IGnssNi> GnssHal::getGnssNiInterface() {
759     if (gnssHal != nullptr) {
760         auto gnssNi = gnssHal->getExtensionGnssNi();
761         if (checkHidlReturn(gnssNi, "Unable to get a handle to GnssNi")) {
762             return gnssNi;
763         }
764     }
765     return nullptr;
766 }
767 
768 } // namespace android::gnss
769