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