1 /*
2  * Copyright (C) 2008 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 and LOG_NDEBUG before <log/log.h> to overwrite the default values.
18 #define LOG_TAG "GnssLocationProviderJni"
19 #define LOG_NDEBUG 0
20 
21 #include <android/hardware/gnss/1.0/IGnss.h>
22 #include <android/hardware/gnss/1.0/IGnssMeasurement.h>
23 #include <android/hardware/gnss/1.1/IGnss.h>
24 #include <android/hardware/gnss/1.1/IGnssMeasurement.h>
25 #include <android/hardware/gnss/2.0/IGnss.h>
26 #include <android/hardware/gnss/2.0/IGnssMeasurement.h>
27 #include <android/hardware/gnss/2.1/IGnss.h>
28 #include <android/hardware/gnss/2.1/IGnssAntennaInfo.h>
29 #include <android/hardware/gnss/2.1/IGnssMeasurement.h>
30 #include <android/hardware/gnss/BnGnss.h>
31 #include <android/hardware/gnss/BnGnssAntennaInfo.h>
32 #include <android/hardware/gnss/BnGnssCallback.h>
33 #include <android/hardware/gnss/BnGnssDebug.h>
34 #include <android/hardware/gnss/BnGnssGeofence.h>
35 #include <android/hardware/gnss/BnGnssGeofenceCallback.h>
36 #include <android/hardware/gnss/BnGnssMeasurementCallback.h>
37 #include <android/hardware/gnss/BnGnssPowerIndicationCallback.h>
38 #include <android/hardware/gnss/BnGnssPsdsCallback.h>
39 #include <binder/IServiceManager.h>
40 #include <nativehelper/JNIHelp.h>
41 #include <pthread.h>
42 #include <string.h>
43 #include <utils/SystemClock.h>
44 
45 #include <cinttypes>
46 #include <iomanip>
47 #include <limits>
48 
49 #include "android_runtime/AndroidRuntime.h"
50 #include "android_runtime/Log.h"
51 #include "gnss/AGnss.h"
52 #include "gnss/AGnssRil.h"
53 #include "gnss/Gnss.h"
54 #include "gnss/GnssAntennaInfo.h"
55 #include "gnss/GnssAntennaInfoCallback.h"
56 #include "gnss/GnssBatching.h"
57 #include "gnss/GnssConfiguration.h"
58 #include "gnss/GnssDebug.h"
59 #include "gnss/GnssGeofence.h"
60 #include "gnss/GnssMeasurement.h"
61 #include "gnss/GnssNavigationMessage.h"
62 #include "gnss/GnssVisibilityControl.h"
63 #include "gnss/MeasurementCorrections.h"
64 #include "gnss/Utils.h"
65 #include "hardware_legacy/power.h"
66 #include "jni.h"
67 #include "utils/Log.h"
68 #include "utils/misc.h"
69 
70 static jclass class_gnssPowerStats;
71 
72 static jmethodID method_reportGnssPowerStats;
73 static jmethodID method_reportNfwNotification;
74 static jmethodID method_isInEmergencySession;
75 static jmethodID method_gnssPowerStatsCtor;
76 static jmethodID method_setSubHalPowerIndicationCapabilities;
77 
78 using android::OK;
79 using android::sp;
80 using android::status_t;
81 using android::String16;
82 using android::wp;
83 using android::binder::Status;
84 
85 using android::hardware::Return;
86 using android::hardware::Void;
87 using android::hardware::hidl_vec;
88 using android::hardware::hidl_string;
89 using android::hardware::hidl_death_recipient;
90 
91 using android::hardware::gnss::V1_0::GnssLocationFlags;
92 using android::hardware::gnss::V1_0::IGnssNavigationMessage;
93 using android::hardware::gnss::V1_0::IGnssNavigationMessageCallback;
94 using android::hardware::gnss::V1_0::IGnssXtra;
95 using android::hardware::gnss::V1_0::IGnssXtraCallback;
96 using android::hardware::gnss::V2_0::ElapsedRealtimeFlags;
97 
98 using android::hidl::base::V1_0::IBase;
99 
100 using GnssConstellationType_V1_0 = android::hardware::gnss::V1_0::GnssConstellationType;
101 using GnssConstellationType_V2_0 = android::hardware::gnss::V2_0::GnssConstellationType;
102 using GnssLocation_V1_0 = android::hardware::gnss::V1_0::GnssLocation;
103 using GnssLocation_V2_0 = android::hardware::gnss::V2_0::GnssLocation;
104 using IGnss_V1_0 = android::hardware::gnss::V1_0::IGnss;
105 using IGnss_V1_1 = android::hardware::gnss::V1_1::IGnss;
106 using IGnss_V2_0 = android::hardware::gnss::V2_0::IGnss;
107 using IGnss_V2_1 = android::hardware::gnss::V2_1::IGnss;
108 using IGnssCallback_V1_0 = android::hardware::gnss::V1_0::IGnssCallback;
109 using IGnssCallback_V2_0 = android::hardware::gnss::V2_0::IGnssCallback;
110 using IGnssCallback_V2_1 = android::hardware::gnss::V2_1::IGnssCallback;
111 
112 using android::hardware::gnss::BlocklistedSource;
113 using android::hardware::gnss::GnssConstellationType;
114 using android::hardware::gnss::GnssPowerStats;
115 using android::hardware::gnss::IGnssPowerIndication;
116 using android::hardware::gnss::IGnssPowerIndicationCallback;
117 
118 using IGnssAidl = android::hardware::gnss::IGnss;
119 using IGnssBatchingAidl = android::hardware::gnss::IGnssBatching;
120 using IGnssDebugAidl = android::hardware::gnss::IGnssDebug;
121 using IGnssPsdsAidl = android::hardware::gnss::IGnssPsds;
122 using IGnssPsdsCallbackAidl = android::hardware::gnss::IGnssPsdsCallback;
123 using IGnssConfigurationAidl = android::hardware::gnss::IGnssConfiguration;
124 using GnssLocationAidl = android::hardware::gnss::GnssLocation;
125 using IGnssAntennaInfoAidl = android::hardware::gnss::IGnssAntennaInfo;
126 
127 sp<IGnssPowerIndication> gnssPowerIndicationIface = nullptr;
128 
129 std::unique_ptr<android::gnss::GnssHal> gnssHal = nullptr;
130 std::unique_ptr<android::gnss::AGnssInterface> agnssIface = nullptr;
131 std::unique_ptr<android::gnss::AGnssRilInterface> agnssRilIface = nullptr;
132 std::unique_ptr<android::gnss::GnssAntennaInfoInterface> gnssAntennaInfoIface = nullptr;
133 std::unique_ptr<android::gnss::GnssConfigurationInterface> gnssConfigurationIface = nullptr;
134 std::unique_ptr<android::gnss::GnssMeasurementInterface> gnssMeasurementIface = nullptr;
135 std::unique_ptr<android::gnss::GnssNavigationMessageInterface> gnssNavigationMessageIface = nullptr;
136 std::unique_ptr<android::gnss::GnssBatchingInterface> gnssBatchingIface = nullptr;
137 std::unique_ptr<android::gnss::GnssDebugInterface> gnssDebugIface = nullptr;
138 std::unique_ptr<android::gnss::GnssGeofenceInterface> gnssGeofencingIface = nullptr;
139 std::unique_ptr<android::gnss::GnssPsdsInterface> gnssPsdsIface = nullptr;
140 std::unique_ptr<android::gnss::GnssVisibilityControlInterface> gnssVisibilityControlIface = nullptr;
141 std::unique_ptr<android::gnss::MeasurementCorrectionsInterface> gnssMeasurementCorrectionsIface =
142         nullptr;
143 
144 namespace android {
145 
146 namespace {
147 
148 }  // namespace
149 
150 /*
151  * GnssPowerIndicationCallback class implements the callback methods for the IGnssPowerIndication
152  * interface.
153  */
154 struct GnssPowerIndicationCallback : public android::hardware::gnss::BnGnssPowerIndicationCallback {
155 public:
156     Status setCapabilitiesCb(const int capabilities) override;
157     Status gnssPowerStatsCb(const GnssPowerStats& data) override;
158 };
159 
setCapabilitiesCb(const int capabilities)160 Status GnssPowerIndicationCallback::setCapabilitiesCb(const int capabilities) {
161     ALOGD("GnssPowerIndicationCallback::%s: %du\n", __func__, capabilities);
162     JNIEnv* env = getJniEnv();
163     env->CallVoidMethod(mCallbacksObj, method_setSubHalPowerIndicationCapabilities, capabilities);
164     checkAndClearExceptionFromCallback(env, __FUNCTION__);
165     return Status::ok();
166 }
167 
gnssPowerStatsCb(const GnssPowerStats & data)168 Status GnssPowerIndicationCallback::gnssPowerStatsCb(const GnssPowerStats& data) {
169     JNIEnv* env = getJniEnv();
170 
171     int size = data.otherModesEnergyMilliJoule.size();
172     jdoubleArray otherModesEnergy = env->NewDoubleArray(size);
173     if (size > 0) {
174         env->SetDoubleArrayRegion(otherModesEnergy, (jsize)0, size,
175                                   &(data.otherModesEnergyMilliJoule[0]));
176     }
177     jobject gnssPowerStats =
178             env->NewObject(class_gnssPowerStats, method_gnssPowerStatsCtor,
179                            data.elapsedRealtime.flags, data.elapsedRealtime.timestampNs,
180                            data.elapsedRealtime.timeUncertaintyNs, data.totalEnergyMilliJoule,
181                            data.singlebandTrackingModeEnergyMilliJoule,
182                            data.multibandTrackingModeEnergyMilliJoule,
183                            data.singlebandAcquisitionModeEnergyMilliJoule,
184                            data.multibandAcquisitionModeEnergyMilliJoule, otherModesEnergy);
185 
186     env->CallVoidMethod(mCallbacksObj, method_reportGnssPowerStats, gnssPowerStats);
187 
188     checkAndClearExceptionFromCallback(env, __FUNCTION__);
189     env->DeleteLocalRef(gnssPowerStats);
190     env->DeleteLocalRef(otherModesEnergy);
191     return Status::ok();
192 }
193 
194 /* Initializes the GNSS service handle. */
android_location_gnss_hal_GnssNative_set_gps_service_handle()195 static void android_location_gnss_hal_GnssNative_set_gps_service_handle() {
196     gnssHal = std::make_unique<gnss::GnssHal>();
197 }
198 
199 /* One time initialization at system boot */
android_location_gnss_hal_GnssNative_class_init_once(JNIEnv * env,jclass clazz)200 static void android_location_gnss_hal_GnssNative_class_init_once(JNIEnv* env, jclass clazz) {
201     // Initialize the top level gnss HAL handle.
202     android_location_gnss_hal_GnssNative_set_gps_service_handle();
203 
204     // Cache methodIDs and class IDs.
205     method_reportNfwNotification = env->GetMethodID(clazz, "reportNfwNotification",
206             "(Ljava/lang/String;BLjava/lang/String;BLjava/lang/String;BZZ)V");
207     method_reportGnssPowerStats =
208             env->GetMethodID(clazz, "reportGnssPowerStats",
209                              "(Lcom/android/server/location/gnss/GnssPowerStats;)V");
210     method_isInEmergencySession = env->GetMethodID(clazz, "isInEmergencySession", "()Z");
211 
212     method_setSubHalPowerIndicationCapabilities =
213             env->GetMethodID(clazz, "setSubHalPowerIndicationCapabilities", "(I)V");
214 
215     jclass gnssPowerStatsClass = env->FindClass("com/android/server/location/gnss/GnssPowerStats");
216     class_gnssPowerStats = (jclass)env->NewGlobalRef(gnssPowerStatsClass);
217     method_gnssPowerStatsCtor = env->GetMethodID(class_gnssPowerStats, "<init>", "(IJDDDDDD[D)V");
218 
219     gnss::AGnss_class_init_once(env, clazz);
220     gnss::AGnssRil_class_init_once(env, clazz);
221     gnss::Gnss_class_init_once(env, clazz);
222     gnss::GnssAntennaInfo_class_init_once(env, clazz);
223     gnss::GnssBatching_class_init_once(env, clazz);
224     gnss::GnssConfiguration_class_init_once(env);
225     gnss::GnssGeofence_class_init_once(env, clazz);
226     gnss::GnssMeasurement_class_init_once(env, clazz);
227     gnss::GnssNavigationMessage_class_init_once(env, clazz);
228     gnss::GnssPsds_class_init_once(env, clazz);
229     gnss::GnssVisibilityControl_class_init_once(env, clazz);
230     gnss::MeasurementCorrections_class_init_once(env, clazz);
231     gnss::MeasurementCorrectionsCallback_class_init_once(env, clazz);
232     gnss::Utils_class_init_once(env);
233 }
234 
235 /* Initialization needed at system boot and whenever GNSS service dies. */
android_location_gnss_hal_GnssNative_init_once(JNIEnv * env,jobject obj,jboolean reinitializeGnssServiceHandle)236 static void android_location_gnss_hal_GnssNative_init_once(JNIEnv* env, jobject obj,
237                                                            jboolean reinitializeGnssServiceHandle) {
238     /*
239      * Save a pointer to JVM.
240      */
241     jint jvmStatus = env->GetJavaVM(&android::ScopedJniThreadAttach::sJvm);
242     if (jvmStatus != JNI_OK) {
243         LOG_ALWAYS_FATAL("Unable to get Java VM. Error: %d", jvmStatus);
244     }
245 
246     if (reinitializeGnssServiceHandle) {
247         android_location_gnss_hal_GnssNative_set_gps_service_handle();
248     }
249 
250     if (gnssHal == nullptr || !gnssHal->isSupported()) {
251         ALOGE("Unable to get GPS service\n");
252         return;
253     }
254 
255     gnssHal->linkToDeath();
256     gnssPsdsIface = gnssHal->getGnssPsdsInterface();
257     agnssRilIface = gnssHal->getAGnssRilInterface();
258     agnssIface = gnssHal->getAGnssInterface();
259     gnssNavigationMessageIface = gnssHal->getGnssNavigationMessageInterface();
260     gnssMeasurementIface = gnssHal->getGnssMeasurementInterface();
261     gnssAntennaInfoIface = gnssHal->getGnssAntennaInfoInterface();
262     gnssMeasurementCorrectionsIface = gnssHal->getMeasurementCorrectionsInterface();
263     gnssDebugIface = gnssHal->getGnssDebugInterface();
264     gnssConfigurationIface = gnssHal->getGnssConfigurationInterface();
265     gnssGeofencingIface = gnssHal->getGnssGeofenceInterface();
266     gnssBatchingIface = gnssHal->getGnssBatchingInterface();
267     gnssVisibilityControlIface = gnssHal->getGnssVisibilityControlInterface();
268     gnssPowerIndicationIface = gnssHal->getGnssPowerIndicationInterface();
269 
270     if (mCallbacksObj) {
271         ALOGE("Callbacks already initialized");
272     } else {
273         mCallbacksObj = env->NewGlobalRef(obj);
274     }
275 }
276 
android_location_gnss_hal_GnssNative_is_supported(JNIEnv *,jclass)277 static jboolean android_location_gnss_hal_GnssNative_is_supported(JNIEnv* /* env */, jclass) {
278     return (gnssHal != nullptr && gnssHal->isSupported()) ? JNI_TRUE : JNI_FALSE;
279 }
280 
android_location_GnssNetworkConnectivityHandler_is_agps_ril_supported(JNIEnv *,jclass)281 static jboolean android_location_GnssNetworkConnectivityHandler_is_agps_ril_supported(
282         JNIEnv* /* env */, jclass /* clazz */) {
283     return (agnssRilIface != nullptr) ? JNI_TRUE : JNI_FALSE;
284 }
285 
android_location_GnssConfiguration_get_gnss_configuration_version(JNIEnv * env,jclass)286 static jobject android_location_GnssConfiguration_get_gnss_configuration_version(
287         JNIEnv* env, jclass /* jclazz */) {
288     if (gnssConfigurationIface == nullptr) {
289         return nullptr;
290     }
291     return gnssConfigurationIface->getVersion(env);
292 }
293 
294 /* Initialization needed each time the GPS service is shutdown. */
android_location_gnss_hal_GnssNative_init(JNIEnv *,jclass)295 static jboolean android_location_gnss_hal_GnssNative_init(JNIEnv* /* env */, jclass) {
296     /*
297      * This must be set before calling into the HAL library.
298      */
299     if (!mCallbacksObj) {
300         ALOGE("No callbacks set during GNSS HAL initialization.");
301         return JNI_FALSE;
302     }
303 
304     /*
305      * Fail if the main interface fails to initialize
306      */
307     if (!gnssHal->isSupported()) {
308         ALOGE("Unable to initialize GNSS HAL.");
309         return JNI_FALSE;
310     }
311 
312     // Set top level IGnss HAL callback.
313     gnssHal->setCallback();
314 
315     // Set IGnssPsds callback.
316     if (gnssPsdsIface == nullptr ||
317         !gnssPsdsIface->setCallback(std::make_unique<gnss::GnssPsdsCallback>())) {
318         ALOGI("Unable to initialize IGnssPsds interface.");
319     }
320 
321     // Set IAGnss callback.
322     if (agnssIface == nullptr ||
323         !agnssIface->setCallback(std::make_unique<gnss::AGnssCallback>())) {
324         ALOGI("Unable to initialize IAGnss interface.");
325     }
326 
327     // Set GnssGeofence callback.
328     if (gnssGeofencingIface != nullptr) {
329         gnssGeofencingIface->setCallback(std::make_unique<gnss::GnssGeofenceCallback>());
330     } else {
331         ALOGI("Unable to initialize IGnssGeofencing interface.");
332     }
333 
334     // Set IAGnssRil callback.
335     if (agnssRilIface == nullptr ||
336         !agnssRilIface->setCallback(std::make_unique<gnss::AGnssRilCallback>())) {
337         ALOGI("Unable to initialize IAGnssRil interface.");
338     }
339 
340     // Set IGnssVisibilityControl callback.
341     if (gnssVisibilityControlIface != nullptr) {
342         gnssVisibilityControlIface->setCallback(
343                 std::make_unique<gnss::GnssVisibilityControlCallback>());
344     } else {
345         ALOGI("Unable to initialize IGnssVisibilityControl interface.");
346     }
347 
348     // Set IMeasurementCorrection callback.
349     if (gnssMeasurementCorrectionsIface == nullptr ||
350         !gnssMeasurementCorrectionsIface->setCallback(
351                 std::make_unique<gnss::MeasurementCorrectionsCallback>())) {
352         ALOGI("Unable to initialize IGnssMeasurementCorrections interface.");
353     }
354 
355     // Set IGnssPowerIndication.hal callback.
356     if (gnssPowerIndicationIface != nullptr) {
357         sp<IGnssPowerIndicationCallback> gnssPowerIndicationCallback =
358                 new GnssPowerIndicationCallback();
359         auto status = gnssPowerIndicationIface->setCallback(gnssPowerIndicationCallback);
360         if (!checkAidlStatus(status, "IGnssPowerIndication setCallback() failed.")) {
361             gnssPowerIndicationIface = nullptr;
362         }
363     }
364 
365     return JNI_TRUE;
366 }
367 
android_location_gnss_hal_GnssNative_cleanup(JNIEnv *,jclass)368 static void android_location_gnss_hal_GnssNative_cleanup(JNIEnv* /* env */, jclass) {
369     gnssHal->close();
370 }
371 
android_location_gnss_hal_GnssNative_set_position_mode(JNIEnv *,jclass,jint mode,jint recurrence,jint min_interval,jint preferred_accuracy,jint preferred_time,jboolean low_power_mode)372 static jboolean android_location_gnss_hal_GnssNative_set_position_mode(
373         JNIEnv* /* env */, jclass, jint mode, jint recurrence, jint min_interval,
374         jint preferred_accuracy, jint preferred_time, jboolean low_power_mode) {
375     return gnssHal->setPositionMode(mode, recurrence, min_interval, preferred_accuracy,
376                                     preferred_time, low_power_mode);
377 }
378 
android_location_gnss_hal_GnssNative_start(JNIEnv *,jclass)379 static jboolean android_location_gnss_hal_GnssNative_start(JNIEnv* /* env */, jclass) {
380     return gnssHal->start();
381 }
382 
android_location_gnss_hal_GnssNative_stop(JNIEnv *,jclass)383 static jboolean android_location_gnss_hal_GnssNative_stop(JNIEnv* /* env */, jclass) {
384     return gnssHal->stop();
385 }
386 
android_location_gnss_hal_GnssNative_start_sv_status_collection(JNIEnv *,jclass)387 static jboolean android_location_gnss_hal_GnssNative_start_sv_status_collection(JNIEnv* /* env */,
388                                                                                 jclass) {
389     return gnssHal->startSvStatus();
390 }
391 
android_location_gnss_hal_GnssNative_stop_sv_status_collection(JNIEnv *,jclass)392 static jboolean android_location_gnss_hal_GnssNative_stop_sv_status_collection(JNIEnv* /* env */,
393                                                                                jclass) {
394     return gnssHal->stopSvStatus();
395 }
396 
android_location_gnss_hal_GnssNative_start_nmea_message_collection(JNIEnv *,jclass)397 static jboolean android_location_gnss_hal_GnssNative_start_nmea_message_collection(
398         JNIEnv* /* env */, jclass) {
399     return gnssHal->startNmea();
400 }
401 
android_location_gnss_hal_GnssNative_stop_nmea_message_collection(JNIEnv *,jclass)402 static jboolean android_location_gnss_hal_GnssNative_stop_nmea_message_collection(JNIEnv* /* env */,
403                                                                                   jclass) {
404     return gnssHal->stopNmea();
405 }
406 
android_location_gnss_hal_GnssNative_delete_aiding_data(JNIEnv *,jclass,jint flags)407 static void android_location_gnss_hal_GnssNative_delete_aiding_data(JNIEnv* /* env */, jclass,
408                                                                     jint flags) {
409     gnssHal->deleteAidingData(flags);
410 }
411 
android_location_gnss_hal_GnssNative_agps_set_reference_location_cellid(JNIEnv *,jclass,jint type,jint mcc,jint mnc,jint lac,jlong cid,jint tac,jint pcid,jint arfcn)412 static void android_location_gnss_hal_GnssNative_agps_set_reference_location_cellid(
413         JNIEnv* /* env */, jclass, jint type, jint mcc, jint mnc, jint lac, jlong cid, jint tac,
414         jint pcid, jint arfcn) {
415     if (agnssRilIface == nullptr) {
416         ALOGE("%s: IAGnssRil interface not available.", __func__);
417         return;
418     }
419     agnssRilIface->setRefLocation(type, mcc, mnc, lac, cid, tac, pcid, arfcn);
420 }
421 
android_location_gnss_hal_GnssNative_agps_set_id(JNIEnv * env,jclass,jint type,jstring setid_string)422 static void android_location_gnss_hal_GnssNative_agps_set_id(JNIEnv* env, jclass, jint type,
423                                                              jstring setid_string) {
424     if (agnssRilIface == nullptr) {
425         ALOGE("%s: IAGnssRil interface not available.", __func__);
426         return;
427     }
428     agnssRilIface->setSetId(type, setid_string);
429 }
430 
android_location_gnss_hal_GnssNative_inject_ni_supl_message_data(JNIEnv * env,jclass,jbyteArray data,jint length,jint slotIndex)431 static void android_location_gnss_hal_GnssNative_inject_ni_supl_message_data(JNIEnv* env, jclass,
432                                                                              jbyteArray data,
433                                                                              jint length,
434                                                                              jint slotIndex) {
435     if (agnssRilIface == nullptr) {
436         ALOGE("%s: IAGnssRil interface not available.", __func__);
437         return;
438     }
439     agnssRilIface->injectNiSuplMessageData(data, length, slotIndex);
440 }
441 
android_location_gnss_hal_GnssNative_read_nmea(JNIEnv * env,jclass,jbyteArray nmeaArray,jint buffer_size)442 static jint android_location_gnss_hal_GnssNative_read_nmea(JNIEnv* env, jclass,
443                                                            jbyteArray nmeaArray, jint buffer_size) {
444     return gnssHal->readNmea(nmeaArray, buffer_size);
445 }
446 
android_location_gnss_hal_GnssNative_inject_time(JNIEnv *,jclass,jlong time,jlong timeReference,jint uncertainty)447 static void android_location_gnss_hal_GnssNative_inject_time(JNIEnv* /* env */, jclass, jlong time,
448                                                              jlong timeReference,
449                                                              jint uncertainty) {
450     gnssHal->injectTime(time, timeReference, uncertainty);
451 }
452 
android_location_gnss_hal_GnssNative_inject_best_location(JNIEnv *,jclass,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)453 static void android_location_gnss_hal_GnssNative_inject_best_location(
454         JNIEnv* /* env */, jclass, jint gnssLocationFlags, jdouble latitudeDegrees,
455         jdouble longitudeDegrees, jdouble altitudeMeters, jfloat speedMetersPerSec,
456         jfloat bearingDegrees, jfloat horizontalAccuracyMeters, jfloat verticalAccuracyMeters,
457         jfloat speedAccuracyMetersPerSecond, jfloat bearingAccuracyDegrees, jlong timestamp,
458         jint elapsedRealtimeFlags, jlong elapsedRealtimeNanos,
459         jdouble elapsedRealtimeUncertaintyNanos) {
460     gnssHal->injectBestLocation(gnssLocationFlags, latitudeDegrees, longitudeDegrees,
461                                 altitudeMeters, speedMetersPerSec, bearingDegrees,
462                                 horizontalAccuracyMeters, verticalAccuracyMeters,
463                                 speedAccuracyMetersPerSecond, bearingAccuracyDegrees, timestamp,
464                                 elapsedRealtimeFlags, elapsedRealtimeNanos,
465                                 elapsedRealtimeUncertaintyNanos);
466 }
467 
android_location_gnss_hal_GnssNative_inject_location(JNIEnv *,jclass,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)468 static void android_location_gnss_hal_GnssNative_inject_location(
469         JNIEnv* /* env */, jclass, jint gnssLocationFlags, jdouble latitudeDegrees,
470         jdouble longitudeDegrees, jdouble altitudeMeters, jfloat speedMetersPerSec,
471         jfloat bearingDegrees, jfloat horizontalAccuracyMeters, jfloat verticalAccuracyMeters,
472         jfloat speedAccuracyMetersPerSecond, jfloat bearingAccuracyDegrees, jlong timestamp,
473         jint elapsedRealtimeFlags, jlong elapsedRealtimeNanos,
474         jdouble elapsedRealtimeUncertaintyNanos) {
475     gnssHal->injectLocation(gnssLocationFlags, latitudeDegrees, longitudeDegrees, altitudeMeters,
476                             speedMetersPerSec, bearingDegrees, horizontalAccuracyMeters,
477                             verticalAccuracyMeters, speedAccuracyMetersPerSecond,
478                             bearingAccuracyDegrees, timestamp, elapsedRealtimeFlags,
479                             elapsedRealtimeNanos, elapsedRealtimeUncertaintyNanos);
480 }
481 
android_location_gnss_hal_GnssNative_supports_psds(JNIEnv *,jclass)482 static jboolean android_location_gnss_hal_GnssNative_supports_psds(JNIEnv* /* env */, jclass) {
483     return (gnssPsdsIface != nullptr) ? JNI_TRUE : JNI_FALSE;
484 }
485 
android_location_gnss_hal_GnssNative_inject_psds_data(JNIEnv * env,jclass,jbyteArray data,jint length,jint psdsType)486 static void android_location_gnss_hal_GnssNative_inject_psds_data(JNIEnv* env, jclass,
487                                                                   jbyteArray data, jint length,
488                                                                   jint psdsType) {
489     if (gnssPsdsIface == nullptr) {
490         ALOGE("%s: IGnssPsds or IGnssXtra interface not available.", __func__);
491         return;
492     }
493     gnssPsdsIface->injectPsdsData(data, length, psdsType);
494 }
495 
android_location_GnssNetworkConnectivityHandler_agps_data_conn_open(JNIEnv * env,jobject,jlong networkHandle,jstring apn,jint apnIpType)496 static void android_location_GnssNetworkConnectivityHandler_agps_data_conn_open(
497         JNIEnv* env, jobject /* obj */, jlong networkHandle, jstring apn, jint apnIpType) {
498     if (apn == nullptr) {
499         jniThrowException(env, "java/lang/IllegalArgumentException", nullptr);
500         return;
501     }
502 
503     if (agnssIface != nullptr) {
504         agnssIface->dataConnOpen(env, networkHandle, apn, apnIpType);
505     } else {
506         ALOGE("%s: IAGnss interface not available.", __func__);
507         return;
508     }
509 }
510 
android_location_GnssNetworkConnectivityHandler_agps_data_conn_closed(JNIEnv *,jobject)511 static void android_location_GnssNetworkConnectivityHandler_agps_data_conn_closed(JNIEnv* /* env */,
512                                                                        jobject /* obj */) {
513     if (agnssIface != nullptr) {
514         agnssIface->dataConnClosed();
515     } else {
516         ALOGE("%s: IAGnss interface not available.", __func__);
517         return;
518     }
519 }
520 
android_location_GnssNetworkConnectivityHandler_agps_data_conn_failed(JNIEnv *,jobject)521 static void android_location_GnssNetworkConnectivityHandler_agps_data_conn_failed(JNIEnv* /* env */,
522                                                                        jobject /* obj */) {
523     if (agnssIface != nullptr) {
524         agnssIface->dataConnFailed();
525     } else {
526         ALOGE("%s: IAGnss interface not available.", __func__);
527         return;
528     }
529 }
530 
android_location_gnss_hal_GnssNative_set_agps_server(JNIEnv * env,jclass,jint type,jstring hostname,jint port)531 static void android_location_gnss_hal_GnssNative_set_agps_server(JNIEnv* env, jclass, jint type,
532                                                                  jstring hostname, jint port) {
533     if (agnssIface != nullptr) {
534         agnssIface->setServer(env, type, hostname, port);
535     } else {
536         ALOGE("%s: IAGnss interface not available.", __func__);
537         return;
538     }
539 }
540 
android_location_gnss_hal_GnssNative_get_internal_state(JNIEnv * env,jclass)541 static jstring android_location_gnss_hal_GnssNative_get_internal_state(JNIEnv* env, jclass) {
542     /*
543      * TODO: Create a jobject to represent GnssDebug.
544      */
545 
546     if (gnssDebugIface == nullptr) {
547         ALOGE("%s: IGnssDebug interface not available.", __func__);
548         return nullptr;
549     }
550     return gnssDebugIface->getDebugData(env);
551 }
552 
android_location_gnss_hal_GnssNative_request_power_stats(JNIEnv * env)553 static void android_location_gnss_hal_GnssNative_request_power_stats(JNIEnv* env) {
554     if (gnssPowerIndicationIface == nullptr) {
555         return;
556     }
557     auto status = gnssPowerIndicationIface->requestGnssPowerStats();
558     checkAidlStatus(status, "IGnssPowerIndication requestGnssPowerStats() failed.");
559 }
560 
android_location_gnss_hal_GnssNative_is_gnss_visibility_control_supported(JNIEnv *,jclass)561 static jboolean android_location_gnss_hal_GnssNative_is_gnss_visibility_control_supported(
562         JNIEnv* /* env */, jclass) {
563     return (gnssVisibilityControlIface != nullptr) ?  JNI_TRUE : JNI_FALSE;
564 }
565 
android_location_GnssNetworkConnectivityHandler_update_network_state(JNIEnv * env,jobject,jboolean connected,jint type,jboolean roaming,jboolean available,jstring apn,jlong networkHandle,jshort capabilities)566 static void android_location_GnssNetworkConnectivityHandler_update_network_state(JNIEnv* env,
567                                                                        jobject /* obj */,
568                                                                        jboolean connected,
569                                                                        jint type,
570                                                                        jboolean roaming,
571                                                                        jboolean available,
572                                                                        jstring apn,
573                                                                        jlong networkHandle,
574                                                                        jshort capabilities) {
575     if (agnssRilIface == nullptr) {
576         ALOGE("%s: IAGnssRil interface not available.", __func__);
577         return;
578     }
579     agnssRilIface->updateNetworkState(connected, type, roaming, available, apn, networkHandle,
580                                       capabilities);
581 }
582 
android_location_gnss_hal_GnssNative_is_geofence_supported(JNIEnv *,jclass)583 static jboolean android_location_gnss_hal_GnssNative_is_geofence_supported(JNIEnv* /* env */,
584                                                                            jclass) {
585     if (gnssGeofencingIface == nullptr) {
586         return JNI_FALSE;
587     }
588     return JNI_TRUE;
589 }
590 
android_location_gnss_hal_GnssNative_add_geofence(JNIEnv *,jclass,jint geofenceId,jdouble latitude,jdouble longitude,jdouble radius,jint last_transition,jint monitor_transition,jint notification_responsiveness,jint unknown_timer)591 static jboolean android_location_gnss_hal_GnssNative_add_geofence(
592         JNIEnv* /* env */, jclass, jint geofenceId, jdouble latitude, jdouble longitude,
593         jdouble radius, jint last_transition, jint monitor_transition,
594         jint notification_responsiveness, jint unknown_timer) {
595     if (gnssGeofencingIface == nullptr) {
596         ALOGE("%s: IGnssGeofencing interface not available.", __func__);
597         return JNI_FALSE;
598     }
599     return gnssGeofencingIface->addGeofence(geofenceId, latitude, longitude, radius,
600                                             last_transition, monitor_transition,
601                                             notification_responsiveness, unknown_timer);
602 }
603 
android_location_gnss_hal_GnssNative_remove_geofence(JNIEnv *,jclass,jint geofenceId)604 static jboolean android_location_gnss_hal_GnssNative_remove_geofence(JNIEnv* /* env */, jclass,
605                                                                      jint geofenceId) {
606     if (gnssGeofencingIface == nullptr) {
607         ALOGE("%s: IGnssGeofencing interface not available.", __func__);
608         return JNI_FALSE;
609     }
610     return gnssGeofencingIface->removeGeofence(geofenceId);
611 }
612 
android_location_gnss_hal_GnssNative_pause_geofence(JNIEnv *,jclass,jint geofenceId)613 static jboolean android_location_gnss_hal_GnssNative_pause_geofence(JNIEnv* /* env */, jclass,
614                                                                     jint geofenceId) {
615     if (gnssGeofencingIface == nullptr) {
616         ALOGE("%s: IGnssGeofencing interface not available.", __func__);
617         return JNI_FALSE;
618     }
619     return gnssGeofencingIface->pauseGeofence(geofenceId);
620 }
621 
android_location_gnss_hal_GnssNative_resume_geofence(JNIEnv *,jclass,jint geofenceId,jint monitor_transition)622 static jboolean android_location_gnss_hal_GnssNative_resume_geofence(JNIEnv* /* env */, jclass,
623                                                                      jint geofenceId,
624                                                                      jint monitor_transition) {
625     if (gnssGeofencingIface == nullptr) {
626         ALOGE("%s: IGnssGeofencing interface not available.", __func__);
627         return JNI_FALSE;
628     }
629     return gnssGeofencingIface->resumeGeofence(geofenceId, monitor_transition);
630 }
631 
android_location_gnss_hal_GnssNative_is_antenna_info_supported(JNIEnv * env,jclass)632 static jboolean android_location_gnss_hal_GnssNative_is_antenna_info_supported(JNIEnv* env,
633                                                                                jclass) {
634     if (gnssAntennaInfoIface != nullptr) {
635         return JNI_TRUE;
636     }
637     return JNI_FALSE;
638 }
639 
android_location_gnss_hal_GnssNative_start_antenna_info_listening(JNIEnv *,jclass)640 static jboolean android_location_gnss_hal_GnssNative_start_antenna_info_listening(JNIEnv* /* env */,
641                                                                                   jclass) {
642     if (gnssAntennaInfoIface == nullptr) {
643         ALOGE("%s: IGnssAntennaInfo interface not available.", __func__);
644         return JNI_FALSE;
645     }
646     return gnssAntennaInfoIface->setCallback(std::make_unique<gnss::GnssAntennaInfoCallback>());
647 }
648 
android_location_gnss_hal_GnssNative_stop_antenna_info_listening(JNIEnv *,jclass)649 static jboolean android_location_gnss_hal_GnssNative_stop_antenna_info_listening(JNIEnv* /* env */,
650                                                                                  jclass) {
651     if (gnssAntennaInfoIface == nullptr) {
652         ALOGE("%s: IGnssAntennaInfo interface not available.", __func__);
653         return JNI_FALSE;
654     }
655     return gnssAntennaInfoIface->close();
656 }
657 
android_location_gnss_hal_GnssNative_is_measurement_supported(JNIEnv * env,jclass)658 static jboolean android_location_gnss_hal_GnssNative_is_measurement_supported(JNIEnv* env, jclass) {
659     if (gnssMeasurementIface != nullptr) {
660         return JNI_TRUE;
661     }
662 
663     return JNI_FALSE;
664 }
665 
android_location_gnss_hal_GnssNative_start_measurement_collection(JNIEnv *,jclass,jboolean enableFullTracking,jboolean enableCorrVecOutputs,jint intervalMs)666 static jboolean android_location_gnss_hal_GnssNative_start_measurement_collection(
667         JNIEnv* /* env */, jclass, jboolean enableFullTracking, jboolean enableCorrVecOutputs,
668         jint intervalMs) {
669     if (gnssMeasurementIface == nullptr) {
670         ALOGE("%s: IGnssMeasurement interface not available.", __func__);
671         return JNI_FALSE;
672     }
673     hardware::gnss::IGnssMeasurementInterface::Options options;
674     options.enableFullTracking = enableFullTracking;
675     options.enableCorrVecOutputs = enableCorrVecOutputs;
676     options.intervalMs = intervalMs;
677 
678     return gnssMeasurementIface->setCallback(std::make_unique<gnss::GnssMeasurementCallback>(
679                                                      gnssMeasurementIface->getInterfaceVersion()),
680                                              options);
681 }
682 
android_location_gnss_hal_GnssNative_stop_measurement_collection(JNIEnv * env,jclass)683 static jboolean android_location_gnss_hal_GnssNative_stop_measurement_collection(JNIEnv* env,
684                                                                                  jclass) {
685     if (gnssMeasurementIface == nullptr) {
686         ALOGE("%s: IGnssMeasurement interface not available.", __func__);
687         return JNI_FALSE;
688     }
689 
690     return gnssMeasurementIface->close();
691 }
692 
android_location_gnss_hal_GnssNative_is_measurement_corrections_supported(JNIEnv * env,jclass)693 static jboolean android_location_gnss_hal_GnssNative_is_measurement_corrections_supported(
694         JNIEnv* env, jclass) {
695     if (gnssMeasurementCorrectionsIface != nullptr) {
696         return JNI_TRUE;
697     }
698 
699     return JNI_FALSE;
700 }
701 
android_location_gnss_hal_GnssNative_inject_measurement_corrections(JNIEnv * env,jclass,jobject correctionsObj)702 static jboolean android_location_gnss_hal_GnssNative_inject_measurement_corrections(
703         JNIEnv* env, jclass, jobject correctionsObj) {
704     if (gnssMeasurementCorrectionsIface == nullptr) {
705         ALOGW("Trying to inject GNSS measurement corrections on a chipset that does not"
706             " support them.");
707         return JNI_FALSE;
708     }
709     return gnssMeasurementCorrectionsIface->setCorrections(env, correctionsObj);
710 }
711 
android_location_gnss_hal_GnssNative_is_navigation_message_supported(JNIEnv * env,jclass)712 static jboolean android_location_gnss_hal_GnssNative_is_navigation_message_supported(JNIEnv* env,
713                                                                                      jclass) {
714     if (gnssNavigationMessageIface != nullptr) {
715         return JNI_TRUE;
716     }
717     return JNI_FALSE;
718 }
719 
android_location_gnss_hal_GnssNative_start_navigation_message_collection(JNIEnv * env,jclass)720 static jboolean android_location_gnss_hal_GnssNative_start_navigation_message_collection(
721         JNIEnv* env, jclass) {
722     if (gnssNavigationMessageIface == nullptr) {
723         ALOGE("%s: IGnssNavigationMessage interface not available.", __func__);
724         return JNI_FALSE;
725     }
726 
727     return gnssNavigationMessageIface->setCallback(
728             std::make_unique<gnss::GnssNavigationMessageCallback>());
729 }
730 
android_location_gnss_hal_GnssNative_stop_navigation_message_collection(JNIEnv * env,jclass)731 static jboolean android_location_gnss_hal_GnssNative_stop_navigation_message_collection(JNIEnv* env,
732                                                                                         jclass) {
733     if (gnssNavigationMessageIface == nullptr) {
734         ALOGE("%s: IGnssNavigationMessage interface not available.", __func__);
735         return JNI_FALSE;
736     }
737     return gnssNavigationMessageIface->close();
738 }
739 
android_location_GnssConfiguration_set_emergency_supl_pdn(JNIEnv *,jobject,jint emergencySuplPdn)740 static jboolean android_location_GnssConfiguration_set_emergency_supl_pdn(JNIEnv*,
741                                                                           jobject,
742                                                                           jint emergencySuplPdn) {
743     if (gnssConfigurationIface == nullptr) {
744         ALOGE("%s: IGnssConfiguration interface not available.", __func__);
745         return JNI_FALSE;
746     }
747     return gnssConfigurationIface->setEmergencySuplPdn(emergencySuplPdn);
748 }
749 
android_location_GnssConfiguration_set_supl_version(JNIEnv *,jobject,jint version)750 static jboolean android_location_GnssConfiguration_set_supl_version(JNIEnv*,
751                                                                     jobject,
752                                                                     jint version) {
753     if (gnssConfigurationIface == nullptr) {
754         ALOGE("%s: IGnssConfiguration interface not available.", __func__);
755         return JNI_FALSE;
756     }
757     return gnssConfigurationIface->setSuplVersion(version);
758 }
759 
android_location_GnssConfiguration_set_supl_es(JNIEnv *,jobject,jint suplEs)760 static jboolean android_location_GnssConfiguration_set_supl_es(JNIEnv*,
761                                                                jobject,
762                                                                jint suplEs) {
763     if (gnssConfigurationIface == nullptr) {
764         ALOGE("%s: IGnssConfiguration interface not available.", __func__);
765         return JNI_FALSE;
766     }
767     return gnssConfigurationIface->setSuplEs(suplEs);
768 }
769 
android_location_GnssConfiguration_set_supl_mode(JNIEnv *,jobject,jint mode)770 static jboolean android_location_GnssConfiguration_set_supl_mode(JNIEnv*,
771                                                                  jobject,
772                                                                  jint mode) {
773     if (gnssConfigurationIface == nullptr) {
774         ALOGE("%s: IGnssConfiguration interface not available.", __func__);
775         return JNI_FALSE;
776     }
777     return gnssConfigurationIface->setSuplMode(mode);
778 }
779 
android_location_GnssConfiguration_set_gps_lock(JNIEnv *,jobject,jint gpsLock)780 static jboolean android_location_GnssConfiguration_set_gps_lock(JNIEnv*,
781                                                                 jobject,
782                                                                 jint gpsLock) {
783     if (gnssConfigurationIface == nullptr) {
784         ALOGE("%s: IGnssConfiguration interface not available.", __func__);
785         return JNI_FALSE;
786     }
787     return gnssConfigurationIface->setGpsLock(gpsLock);
788 }
789 
android_location_GnssConfiguration_set_lpp_profile(JNIEnv *,jobject,jint lppProfile)790 static jboolean android_location_GnssConfiguration_set_lpp_profile(JNIEnv*,
791                                                                    jobject,
792                                                                    jint lppProfile) {
793     if (gnssConfigurationIface == nullptr) {
794         ALOGE("%s: IGnssConfiguration interface not available.", __func__);
795         return JNI_FALSE;
796     }
797     return gnssConfigurationIface->setLppProfile(lppProfile);
798 }
799 
android_location_GnssConfiguration_set_gnss_pos_protocol_select(JNIEnv *,jobject,jint gnssPosProtocol)800 static jboolean android_location_GnssConfiguration_set_gnss_pos_protocol_select(JNIEnv*,
801                                                                             jobject,
802                                                                             jint gnssPosProtocol) {
803     if (gnssConfigurationIface == nullptr) {
804         ALOGE("%s: IGnssConfiguration interface not available.", __func__);
805         return JNI_FALSE;
806     }
807     return gnssConfigurationIface->setGlonassPositioningProtocol(gnssPosProtocol);
808 }
809 
android_location_GnssConfiguration_set_satellite_blocklist(JNIEnv * env,jobject,jintArray constellations,jintArray sv_ids)810 static jboolean android_location_GnssConfiguration_set_satellite_blocklist(JNIEnv* env, jobject,
811                                                                            jintArray constellations,
812                                                                            jintArray sv_ids) {
813     if (gnssConfigurationIface == nullptr) {
814         ALOGI("IGnssConfiguration interface does not support satellite blocklist.");
815         return JNI_FALSE;
816     }
817     return gnssConfigurationIface->setBlocklist(env, constellations, sv_ids);
818 }
819 
android_location_GnssConfiguration_set_es_extension_sec(JNIEnv *,jobject,jint emergencyExtensionSeconds)820 static jboolean android_location_GnssConfiguration_set_es_extension_sec(
821         JNIEnv*, jobject, jint emergencyExtensionSeconds) {
822     if (gnssConfigurationIface == nullptr) {
823         ALOGE("%s: IGnssConfiguration interface not available.", __func__);
824         return JNI_FALSE;
825     }
826     return gnssConfigurationIface->setEsExtensionSec(emergencyExtensionSeconds);
827 }
828 
android_location_gnss_hal_GnssNative_get_batch_size(JNIEnv *)829 static jint android_location_gnss_hal_GnssNative_get_batch_size(JNIEnv*) {
830     if (gnssBatchingIface == nullptr) {
831         return 0; // batching not supported, size = 0
832     }
833     return gnssBatchingIface->getBatchSize();
834 }
835 
android_location_gnss_hal_GnssNative_init_batching(JNIEnv *,jclass)836 static jboolean android_location_gnss_hal_GnssNative_init_batching(JNIEnv*, jclass) {
837     if (gnssBatchingIface == nullptr) {
838         return JNI_FALSE; // batching not supported
839     }
840     return gnssBatchingIface->init(std::make_unique<gnss::GnssBatchingCallback>());
841 }
842 
android_location_gnss_hal_GnssNative_cleanup_batching(JNIEnv *,jclass)843 static void android_location_gnss_hal_GnssNative_cleanup_batching(JNIEnv*, jclass) {
844     if (gnssBatchingIface == nullptr) {
845         return; // batching not supported
846     }
847     gnssBatchingIface->cleanup();
848 }
849 
android_location_gnss_hal_GnssNative_start_batch(JNIEnv *,jclass,jlong periodNanos,jfloat minUpdateDistanceMeters,jboolean wakeOnFifoFull)850 static jboolean android_location_gnss_hal_GnssNative_start_batch(JNIEnv*, jclass, jlong periodNanos,
851                                                                  jfloat minUpdateDistanceMeters,
852                                                                  jboolean wakeOnFifoFull) {
853     if (gnssBatchingIface == nullptr) {
854         return JNI_FALSE; // batching not supported
855     }
856     return gnssBatchingIface->start(periodNanos, minUpdateDistanceMeters, wakeOnFifoFull);
857 }
858 
android_location_gnss_hal_GnssNative_flush_batch(JNIEnv *,jclass)859 static void android_location_gnss_hal_GnssNative_flush_batch(JNIEnv*, jclass) {
860     if (gnssBatchingIface == nullptr) {
861         return; // batching not supported
862     }
863     gnssBatchingIface->flush();
864 }
865 
android_location_gnss_hal_GnssNative_stop_batch(JNIEnv *,jclass)866 static jboolean android_location_gnss_hal_GnssNative_stop_batch(JNIEnv*, jclass) {
867     if (gnssBatchingIface == nullptr) {
868         return JNI_FALSE; // batching not supported
869     }
870     return gnssBatchingIface->stop();
871 }
872 
android_location_GnssVisibilityControl_enable_nfw_location_access(JNIEnv * env,jobject,jobjectArray proxyApps)873 static jboolean android_location_GnssVisibilityControl_enable_nfw_location_access(
874         JNIEnv* env, jobject, jobjectArray proxyApps) {
875     if (gnssVisibilityControlIface == nullptr) {
876         ALOGI("IGnssVisibilityControl interface not available.");
877         return JNI_FALSE;
878     }
879     return gnssVisibilityControlIface->enableNfwLocationAccess(env, proxyApps);
880 }
881 
882 static const JNINativeMethod sCoreMethods[] = {
883         /* name, signature, funcPtr */
884         {"native_class_init_once", "()V",
885          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_class_init_once)},
886         {"native_is_supported", "()Z",
887          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_is_supported)},
888         {"native_init_once", "(Z)V",
889          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_init_once)},
890 };
891 
892 static const JNINativeMethod sLocationProviderMethods[] = {
893         /* name, signature, funcPtr */
894         {"native_init", "()Z", reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_init)},
895         {"native_cleanup", "()V",
896          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_cleanup)},
897         {"native_set_position_mode", "(IIIIIZ)Z",
898          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_set_position_mode)},
899         {"native_start", "()Z",
900          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_start)},
901         {"native_stop", "()Z", reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_stop)},
902         {"native_delete_aiding_data", "(I)V",
903          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_delete_aiding_data)},
904         {"native_read_nmea", "([BI)I",
905          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_read_nmea)},
906         {"native_inject_time", "(JJI)V",
907          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_inject_time)},
908         {"native_inject_best_location", "(IDDDFFFFFFJIJD)V",
909          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_inject_best_location)},
910         {"native_inject_location", "(IDDDFFFFFFJIJD)V",
911          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_inject_location)},
912         {"native_supports_psds", "()Z",
913          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_supports_psds)},
914         {"native_inject_psds_data", "([BII)V",
915          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_inject_psds_data)},
916         {"native_agps_set_id", "(ILjava/lang/String;)V",
917          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_agps_set_id)},
918         {"native_agps_set_ref_location_cellid", "(IIIIJIII)V",
919          reinterpret_cast<void*>(
920                  android_location_gnss_hal_GnssNative_agps_set_reference_location_cellid)},
921         {"native_set_agps_server", "(ILjava/lang/String;I)V",
922          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_set_agps_server)},
923         {"native_inject_ni_supl_message_data", "([BII)V",
924          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_inject_ni_supl_message_data)},
925         {"native_get_internal_state", "()Ljava/lang/String;",
926          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_get_internal_state)},
927         {"native_is_gnss_visibility_control_supported", "()Z",
928          reinterpret_cast<void*>(
929                  android_location_gnss_hal_GnssNative_is_gnss_visibility_control_supported)},
930         {"native_start_sv_status_collection", "()Z",
931          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_start_sv_status_collection)},
932         {"native_stop_sv_status_collection", "()Z",
933          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_stop_sv_status_collection)},
934         {"native_start_nmea_message_collection", "()Z",
935          reinterpret_cast<void*>(
936                  android_location_gnss_hal_GnssNative_start_nmea_message_collection)},
937         {"native_stop_nmea_message_collection", "()Z",
938          reinterpret_cast<void*>(
939                  android_location_gnss_hal_GnssNative_stop_nmea_message_collection)},
940 };
941 
942 static const JNINativeMethod sBatchingMethods[] = {
943         /* name, signature, funcPtr */
944         {"native_get_batch_size", "()I",
945          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_get_batch_size)},
946         {"native_start_batch", "(JFZ)Z",
947          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_start_batch)},
948         {"native_flush_batch", "()V",
949          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_flush_batch)},
950         {"native_stop_batch", "()Z",
951          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_stop_batch)},
952         {"native_init_batching", "()Z",
953          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_init_batching)},
954         {"native_cleanup_batching", "()V",
955          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_cleanup_batching)},
956 };
957 
958 static const JNINativeMethod sAntennaInfoMethods[] = {
959         /* name, signature, funcPtr */
960         {"native_is_antenna_info_supported", "()Z",
961          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_is_antenna_info_supported)},
962         {"native_start_antenna_info_listening", "()Z",
963          reinterpret_cast<void*>(
964                  android_location_gnss_hal_GnssNative_start_antenna_info_listening)},
965         {"native_stop_antenna_info_listening", "()Z",
966          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_stop_antenna_info_listening)},
967 };
968 
969 static const JNINativeMethod sGeofenceMethods[] = {
970         /* name, signature, funcPtr */
971         {"native_is_geofence_supported", "()Z",
972          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_is_geofence_supported)},
973         {"native_add_geofence", "(IDDDIIII)Z",
974          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_add_geofence)},
975         {"native_remove_geofence", "(I)Z",
976          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_remove_geofence)},
977         {"native_pause_geofence", "(I)Z",
978          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_pause_geofence)},
979         {"native_resume_geofence", "(II)Z",
980          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_resume_geofence)},
981 };
982 
983 static const JNINativeMethod sMeasurementMethods[] = {
984         /* name, signature, funcPtr */
985         {"native_is_measurement_supported", "()Z",
986          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_is_measurement_supported)},
987         {"native_start_measurement_collection", "(ZZI)Z",
988          reinterpret_cast<void*>(
989                  android_location_gnss_hal_GnssNative_start_measurement_collection)},
990         {"native_stop_measurement_collection", "()Z",
991          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_stop_measurement_collection)},
992 };
993 
994 static const JNINativeMethod sMeasurementCorrectionsMethods[] = {
995         /* name, signature, funcPtr */
996         {"native_is_measurement_corrections_supported", "()Z",
997          reinterpret_cast<void*>(
998                  android_location_gnss_hal_GnssNative_is_measurement_corrections_supported)},
999         {"native_inject_measurement_corrections",
1000          "(Landroid/location/GnssMeasurementCorrections;)Z",
1001          reinterpret_cast<void*>(
1002                  android_location_gnss_hal_GnssNative_inject_measurement_corrections)},
1003 };
1004 
1005 static const JNINativeMethod sNavigationMessageMethods[] = {
1006         /* name, signature, funcPtr */
1007         {"native_is_navigation_message_supported", "()Z",
1008          reinterpret_cast<void*>(
1009                  android_location_gnss_hal_GnssNative_is_navigation_message_supported)},
1010         {"native_start_navigation_message_collection", "()Z",
1011          reinterpret_cast<void*>(
1012                  android_location_gnss_hal_GnssNative_start_navigation_message_collection)},
1013         {"native_stop_navigation_message_collection", "()Z",
1014          reinterpret_cast<void*>(
1015                  android_location_gnss_hal_GnssNative_stop_navigation_message_collection)},
1016 };
1017 
1018 static const JNINativeMethod sNetworkConnectivityMethods[] = {
1019         /* name, signature, funcPtr */
1020         {"native_is_agps_ril_supported", "()Z",
1021          reinterpret_cast<void*>(
1022                  android_location_GnssNetworkConnectivityHandler_is_agps_ril_supported)},
1023         {"native_update_network_state", "(ZIZZLjava/lang/String;JS)V",
1024          reinterpret_cast<void*>(
1025                  android_location_GnssNetworkConnectivityHandler_update_network_state)},
1026         {"native_agps_data_conn_open", "(JLjava/lang/String;I)V",
1027          reinterpret_cast<void*>(
1028                  android_location_GnssNetworkConnectivityHandler_agps_data_conn_open)},
1029         {"native_agps_data_conn_closed", "()V",
1030          reinterpret_cast<void*>(
1031                  android_location_GnssNetworkConnectivityHandler_agps_data_conn_closed)},
1032         {"native_agps_data_conn_failed", "()V",
1033          reinterpret_cast<void*>(
1034                  android_location_GnssNetworkConnectivityHandler_agps_data_conn_failed)},
1035 };
1036 
1037 static const JNINativeMethod sConfigurationMethods[] = {
1038         /* name, signature, funcPtr */
1039         {"native_get_gnss_configuration_version",
1040          "()Lcom/android/server/location/gnss/GnssConfiguration$HalInterfaceVersion;",
1041          reinterpret_cast<void*>(
1042                  android_location_GnssConfiguration_get_gnss_configuration_version)},
1043         {"native_set_supl_es", "(I)Z",
1044          reinterpret_cast<void*>(android_location_GnssConfiguration_set_supl_es)},
1045         {"native_set_supl_version", "(I)Z",
1046          reinterpret_cast<void*>(android_location_GnssConfiguration_set_supl_version)},
1047         {"native_set_supl_mode", "(I)Z",
1048          reinterpret_cast<void*>(android_location_GnssConfiguration_set_supl_mode)},
1049         {"native_set_lpp_profile", "(I)Z",
1050          reinterpret_cast<void*>(android_location_GnssConfiguration_set_lpp_profile)},
1051         {"native_set_gnss_pos_protocol_select", "(I)Z",
1052          reinterpret_cast<void*>(android_location_GnssConfiguration_set_gnss_pos_protocol_select)},
1053         {"native_set_gps_lock", "(I)Z",
1054          reinterpret_cast<void*>(android_location_GnssConfiguration_set_gps_lock)},
1055         {"native_set_emergency_supl_pdn", "(I)Z",
1056          reinterpret_cast<void*>(android_location_GnssConfiguration_set_emergency_supl_pdn)},
1057         {"native_set_satellite_blocklist", "([I[I)Z",
1058          reinterpret_cast<void*>(android_location_GnssConfiguration_set_satellite_blocklist)},
1059         {"native_set_es_extension_sec", "(I)Z",
1060          reinterpret_cast<void*>(android_location_GnssConfiguration_set_es_extension_sec)},
1061 };
1062 
1063 static const JNINativeMethod sVisibilityControlMethods[] = {
1064         /* name, signature, funcPtr */
1065         {"native_enable_nfw_location_access", "([Ljava/lang/String;)Z",
1066          reinterpret_cast<void*>(
1067                  android_location_GnssVisibilityControl_enable_nfw_location_access)},
1068 };
1069 
1070 static const JNINativeMethod sPowerIndicationMethods[] = {
1071         /* name, signature, funcPtr */
1072         {"native_request_power_stats", "()V",
1073          reinterpret_cast<void*>(android_location_gnss_hal_GnssNative_request_power_stats)},
1074 };
1075 
register_android_server_location_GnssLocationProvider(JNIEnv * env)1076 int register_android_server_location_GnssLocationProvider(JNIEnv* env) {
1077     int res;
1078 
1079     res = jniRegisterNativeMethods(env, "com/android/server/location/gnss/hal/GnssNative",
1080                                    sAntennaInfoMethods, NELEM(sAntennaInfoMethods));
1081     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1082 
1083     res = jniRegisterNativeMethods(env, "com/android/server/location/gnss/hal/GnssNative",
1084                                    sBatchingMethods, NELEM(sBatchingMethods));
1085     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1086 
1087     res = jniRegisterNativeMethods(env, "com/android/server/location/gnss/hal/GnssNative",
1088                                    sGeofenceMethods, NELEM(sGeofenceMethods));
1089     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1090 
1091     res = jniRegisterNativeMethods(env, "com/android/server/location/gnss/hal/GnssNative",
1092                                    sMeasurementMethods, NELEM(sMeasurementMethods));
1093     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1094 
1095     res = jniRegisterNativeMethods(env, "com/android/server/location/gnss/hal/GnssNative",
1096                                    sMeasurementCorrectionsMethods,
1097                                    NELEM(sMeasurementCorrectionsMethods));
1098     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1099 
1100     res = jniRegisterNativeMethods(env, "com/android/server/location/gnss/hal/GnssNative",
1101                                    sNavigationMessageMethods, NELEM(sNavigationMessageMethods));
1102     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1103 
1104     res = jniRegisterNativeMethods(env,
1105                                    "com/android/server/location/gnss/"
1106                                    "GnssNetworkConnectivityHandler",
1107                                    sNetworkConnectivityMethods, NELEM(sNetworkConnectivityMethods));
1108     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1109 
1110     res = jniRegisterNativeMethods(env, "com/android/server/location/gnss/GnssConfiguration",
1111                                    sConfigurationMethods, NELEM(sConfigurationMethods));
1112     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1113 
1114     res = jniRegisterNativeMethods(env, "com/android/server/location/gnss/GnssVisibilityControl",
1115                                    sVisibilityControlMethods, NELEM(sVisibilityControlMethods));
1116     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1117 
1118     res = jniRegisterNativeMethods(env, "com/android/server/location/gnss/hal/GnssNative",
1119                                    sPowerIndicationMethods, NELEM(sPowerIndicationMethods));
1120     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1121 
1122     res = jniRegisterNativeMethods(env, "com/android/server/location/gnss/hal/GnssNative",
1123                                    sLocationProviderMethods, NELEM(sLocationProviderMethods));
1124     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1125 
1126     res = jniRegisterNativeMethods(env, "com/android/server/location/gnss/hal/GnssNative",
1127                                    sCoreMethods, NELEM(sCoreMethods));
1128     LOG_FATAL_IF(res < 0, "Unable to register native methods.");
1129 
1130     return 0;
1131 }
1132 
1133 } /* namespace android */
1134