FP5: gps/location: Update to LA.UM.9.14.1.r1-11900-QCM6490.QSSI14.0

Change-Id: I40672365d0e0d64a6081d427c417d9101af0748f
diff --git a/gps/android/2.1/AGnss.cpp b/gps/android/2.1/AGnss.cpp
index ce7b3aa..33a8fc3 100644
--- a/gps/android/2.1/AGnss.cpp
+++ b/gps/android/2.1/AGnss.cpp
@@ -51,9 +51,6 @@
     V2_0::IAGnssCallback::AGnssType  aType;
     IAGnssCallback::AGnssStatusValue aStatus;
 
-    // cache the AGps Type
-    mType = type;
-
     switch (type) {
     case LOC_AGPS_TYPE_SUPL:
         aType = IAGnssCallback::AGnssType::SUPL;
@@ -87,8 +84,13 @@
         return;
     }
 
-    if (mAGnssCbIface != nullptr) {
-        auto r = mAGnssCbIface->agnssStatusCb(aType, aStatus);
+    mMutex.lock();
+    // cache the AGps Type
+    mType = type;
+    auto aGnssCbIface = mAGnssCbIface;
+    mMutex.unlock();
+    if (aGnssCbIface != nullptr) {
+        auto r = aGnssCbIface->agnssStatusCb(aType, aStatus);
         if (!r.isOk()) {
             LOC_LOGw("Error invoking AGNSS status cb %s", r.description().c_str());
         }
@@ -106,7 +108,9 @@
     }
 
     // Save the interface
+    mMutex.lock();
     mAGnssCbIface = callback;
+    mMutex.unlock();
 
     AgpsCbInfo cbInfo = {};
     cbInfo.statusV4Cb = (void*)agnssStatusIpV4Cb;
diff --git a/gps/android/2.1/AGnss.h b/gps/android/2.1/AGnss.h
index cf9c8a7..3d2cc7e 100644
--- a/gps/android/2.1/AGnss.h
+++ b/gps/android/2.1/AGnss.h
@@ -21,6 +21,7 @@
 #ifndef ANDROID_HARDWARE_GNSS_V2_0_AGNSS_H
 #define ANDROID_HARDWARE_GNSS_V2_0_AGNSS_H
 
+#include <mutex>
 #include <android/hardware/gnss/2.0/IAGnss.h>
 #include <hidl/Status.h>
 #include <gps_extended_c.h>
@@ -66,6 +67,7 @@
 
  private:
     Gnss* mGnss = nullptr;
+    std::mutex mMutex;
     sp<V2_0::IAGnssCallback> mAGnssCbIface = nullptr;
 
     AGpsExtType mType;
diff --git a/gps/android/2.1/AGnssRil.cpp b/gps/android/2.1/AGnssRil.cpp
index f413e93..49b98f6 100644
--- a/gps/android/2.1/AGnssRil.cpp
+++ b/gps/android/2.1/AGnssRil.cpp
@@ -29,7 +29,7 @@
 #include <string>
 #include "Gnss.h"
 #include "AGnssRil.h"
-#include <DataItemConcreteTypesBase.h>
+#include <DataItemConcreteTypes.h>
 
 typedef void* (getLocationInterface)();
 
diff --git a/gps/android/2.1/Android.mk b/gps/android/2.1/Android.mk
index 4be97a9..0d1af7a 100644
--- a/gps/android/2.1/Android.mk
+++ b/gps/android/2.1/Android.mk
@@ -2,8 +2,6 @@
 
 include $(CLEAR_VARS)
 LOCAL_MODULE := android.hardware.gnss@2.1-impl-qti
-# activate the following line for debug purposes only, comment out for production
-#LOCAL_SANITIZE_DIAG += $(GNSS_SANITIZE_DIAG)
 LOCAL_VENDOR_MODULE := true
 LOCAL_MODULE_RELATIVE_PATH := hw
 LOCAL_SRC_FILES := \
@@ -74,8 +72,6 @@
 include $(CLEAR_VARS)
 LOCAL_MODULE := android.hardware.gnss@2.1-service-qti
 
-# activate the following line for debug purposes only, comment out for production
-#LOCAL_SANITIZE_DIAG += $(GNSS_SANITIZE_DIAG)
 LOCAL_VINTF_FRAGMENTS := android.hardware.gnss@2.1-service-qti.xml
 LOCAL_VENDOR_MODULE := true
 LOCAL_MODULE_RELATIVE_PATH := hw
diff --git a/gps/android/2.1/GnssMeasurement.cpp b/gps/android/2.1/GnssMeasurement.cpp
index af75802..de2ccd0 100644
--- a/gps/android/2.1/GnssMeasurement.cpp
+++ b/gps/android/2.1/GnssMeasurement.cpp
@@ -18,11 +18,15 @@
  * limitations under the License.
  */
 
+/* ​​​​​Changes from Qualcomm Innovation Center are provided under the following license:
+ * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ */
+
 #define LOG_TAG "LocSvc_GnssMeasurementInterface"
 
 #include <log_util.h>
 #include "GnssMeasurement.h"
-#include "MeasurementAPIClient.h"
 
 namespace android {
 namespace hardware {
@@ -54,32 +58,44 @@
 // Methods from ::android::hardware::gnss::V1_0::IGnssMeasurement follow.
 Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
         const sp<V1_0::IGnssMeasurementCallback>& callback)  {
+    return setCallback(callback, mGnssMeasurementCbIface, GNSS_POWER_MODE_INVALID);
+}
 
+template <typename T>
+Return<IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback(
+        const sp<T>& callback, sp<T>& myCallback, GnssPowerMode powerMode) {
     Return<GnssMeasurement::GnssMeasurementStatus> ret =
-        IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
-    if (mGnssMeasurementCbIface != nullptr) {
-        LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
-        return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
-    }
-
-    if (callback == nullptr) {
-        LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
-        return ret;
-    }
+            IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
     if (mApi == nullptr) {
         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
         return ret;
     }
 
-    clearInterfaces();
+    if (myCallback != callback) {
+        if (nullptr == callback) {
+            LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
+            mApi->measurementSetCallback(callback);
+            close();
+        } else {
+            if (nullptr != myCallback) {
+                myCallback->unlinkToDeath(mGnssMeasurementDeathRecipient);
+            }
+            myCallback = callback;
+            myCallback->linkToDeath(mGnssMeasurementDeathRecipient, 0);
+            ret = mApi->measurementSetCallback(callback, powerMode);
+        }
+    }
 
-    mGnssMeasurementCbIface = callback;
-    mGnssMeasurementCbIface->linkToDeath(mGnssMeasurementDeathRecipient, 0);
-
-    return mApi->measurementSetCallback(callback);
+    return ret;
 }
 
 void GnssMeasurement::clearInterfaces() {
+    if (mApi != nullptr) {
+        mApi->measurementSetCallback<V1_0::IGnssMeasurementCallback>(nullptr);
+        mApi->measurementSetCallback<V1_1::IGnssMeasurementCallback>(nullptr);
+        mApi->measurementSetCallback<V2_0::IGnssMeasurementCallback>(nullptr);
+        mApi->measurementSetCallback<V2_1::IGnssMeasurementCallback>(nullptr);
+    }
     if (mGnssMeasurementCbIface != nullptr) {
         mGnssMeasurementCbIface->unlinkToDeath(mGnssMeasurementDeathRecipient);
         mGnssMeasurementCbIface = nullptr;
@@ -113,95 +129,24 @@
 // Methods from ::android::hardware::gnss::V1_1::IGnssMeasurement follow.
 Return<GnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1(
         const sp<V1_1::IGnssMeasurementCallback>& callback, bool enableFullTracking) {
-
-    Return<GnssMeasurement::GnssMeasurementStatus> ret =
-        IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
-    if (mGnssMeasurementCbIface_1_1 != nullptr) {
-        LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
-        return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
-    }
-
-    if (callback == nullptr) {
-        LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
-        return ret;
-    }
-    if (nullptr == mApi) {
-        LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
-        return ret;
-    }
-
-    clearInterfaces();
-
-    mGnssMeasurementCbIface_1_1 = callback;
-    mGnssMeasurementCbIface_1_1->linkToDeath(mGnssMeasurementDeathRecipient, 0);
-
-    GnssPowerMode powerMode = enableFullTracking?
-            GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
-
-    return mApi->measurementSetCallback_1_1(callback, powerMode);
+    return setCallback(callback, mGnssMeasurementCbIface_1_1,
+                       enableFullTracking ? GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2);
 }
+
 // Methods from ::android::hardware::gnss::V2_0::IGnssMeasurement follow.
 Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0(
         const sp<V2_0::IGnssMeasurementCallback>& callback,
         bool enableFullTracking) {
-
-    Return<GnssMeasurement::GnssMeasurementStatus> ret =
-        IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
-    if (mGnssMeasurementCbIface_2_0 != nullptr) {
-        LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
-        return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
-    }
-
-    if (callback == nullptr) {
-        LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
-        return ret;
-    }
-    if (nullptr == mApi) {
-        LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
-        return ret;
-    }
-
-    clearInterfaces();
-
-    mGnssMeasurementCbIface_2_0 = callback;
-    mGnssMeasurementCbIface_2_0->linkToDeath(mGnssMeasurementDeathRecipient, 0);
-
-    GnssPowerMode powerMode = enableFullTracking ?
-        GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
-
-    return mApi->measurementSetCallback_2_0(callback, powerMode);
+    return setCallback(callback, mGnssMeasurementCbIface_2_0,
+                       enableFullTracking ? GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2);
 }
 
 // Methods from ::android::hardware::gnss::V2_1::IGnssMeasurement follow.
 Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_1(
         const sp<::android::hardware::gnss::V2_1::IGnssMeasurementCallback>& callback,
         bool enableFullTracking) {
-    Return<GnssMeasurement::GnssMeasurementStatus> ret =
-        IGnssMeasurement::GnssMeasurementStatus::ERROR_GENERIC;
-    if (mGnssMeasurementCbIface_2_1 != nullptr) {
-        LOC_LOGE("%s]: GnssMeasurementCallback is already set", __FUNCTION__);
-        return IGnssMeasurement::GnssMeasurementStatus::ERROR_ALREADY_INIT;
-    }
-
-    if (callback == nullptr) {
-        LOC_LOGE("%s]: callback is nullptr", __FUNCTION__);
-        return ret;
-    }
-    if (nullptr == mApi) {
-        LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
-        return ret;
-    }
-
-    clearInterfaces();
-
-    mGnssMeasurementCbIface_2_1 = callback;
-    mGnssMeasurementCbIface_2_1->linkToDeath(mGnssMeasurementDeathRecipient, 0);
-
-    GnssPowerMode powerMode = enableFullTracking ?
-            GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2;
-
-    return mApi->measurementSetCallback_2_1(callback, powerMode);
-
+    return setCallback(callback, mGnssMeasurementCbIface_2_1,
+                       enableFullTracking ? GNSS_POWER_MODE_M1 : GNSS_POWER_MODE_M2);
 }
 
 }  // namespace implementation
diff --git a/gps/android/2.1/GnssMeasurement.h b/gps/android/2.1/GnssMeasurement.h
index 2ac45c6..96969e7 100644
--- a/gps/android/2.1/GnssMeasurement.h
+++ b/gps/android/2.1/GnssMeasurement.h
@@ -18,12 +18,18 @@
  * limitations under the License.
  */
 
+/* ​​​​​Changes from Qualcomm Innovation Center are provided under the following license:
+ * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All rights reserved.
+ * SPDX-License-Identifier: BSD-3-Clause-Clear
+ */
+
 #ifndef ANDROID_HARDWARE_GNSS_V2_1_GNSSMEASUREMENT_H
 #define ANDROID_HARDWARE_GNSS_V2_1_GNSSMEASUREMENT_H
 
 #include <android/hardware/gnss/2.1/IGnssMeasurement.h>
 #include <hidl/MQDescriptor.h>
 #include <hidl/Status.h>
+#include "MeasurementAPIClient.h"
 
 namespace android {
 namespace hardware {
@@ -74,7 +80,9 @@
         sp<GnssMeasurement> mGnssMeasurement;
     };
 
- private:
+    template <typename T>
+    Return<IGnssMeasurement::GnssMeasurementStatus> setCallback(
+            const sp<T>& callback, sp<T>& myCallback, GnssPowerMode powerMode);
     sp<GnssMeasurementDeathRecipient> mGnssMeasurementDeathRecipient = nullptr;
     sp<V1_0::IGnssMeasurementCallback> mGnssMeasurementCbIface = nullptr;
     sp<V1_1::IGnssMeasurementCallback> mGnssMeasurementCbIface_1_1 = nullptr;
diff --git a/gps/android/2.1/location_api/MeasurementAPIClient.cpp b/gps/android/2.1/location_api/MeasurementAPIClient.cpp
index 0028074..96a7a94 100644
--- a/gps/android/2.1/location_api/MeasurementAPIClient.cpp
+++ b/gps/android/2.1/location_api/MeasurementAPIClient.cpp
@@ -95,65 +95,6 @@
     mGnssMeasurementCbIface_2_1 = nullptr;
 }
 
-// for GpsInterface
-Return<IGnssMeasurement::GnssMeasurementStatus>
-MeasurementAPIClient::measurementSetCallback(const sp<V1_0::IGnssMeasurementCallback>& callback)
-{
-    LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
-
-    mMutex.lock();
-    clearInterfaces();
-    mGnssMeasurementCbIface = callback;
-    mMutex.unlock();
-
-    return startTracking();
-}
-
-Return<IGnssMeasurement::GnssMeasurementStatus>
-MeasurementAPIClient::measurementSetCallback_1_1(
-        const sp<V1_1::IGnssMeasurementCallback>& callback,
-        GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
-{
-    LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
-            __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
-
-    mMutex.lock();
-    clearInterfaces();
-    mGnssMeasurementCbIface_1_1 = callback;
-    mMutex.unlock();
-
-    return startTracking(powerMode, timeBetweenMeasurement);
-}
-
-Return<IGnssMeasurement::GnssMeasurementStatus>
-MeasurementAPIClient::measurementSetCallback_2_0(
-    const sp<V2_0::IGnssMeasurementCallback>& callback,
-    GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
-{
-    LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
-        __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
-
-    mMutex.lock();
-    clearInterfaces();
-    mGnssMeasurementCbIface_2_0 = callback;
-    mMutex.unlock();
-
-    return startTracking(powerMode, timeBetweenMeasurement);
-}
-
-Return<IGnssMeasurement::GnssMeasurementStatus> MeasurementAPIClient::measurementSetCallback_2_1(
-        const sp<V2_1::IGnssMeasurementCallback>& callback,
-        GnssPowerMode powerMode, uint32_t timeBetweenMeasurement) {
-    LOC_LOGD("%s]: (%p) (powermode: %d) (tbm: %d)",
-        __FUNCTION__, &callback, (int)powerMode, timeBetweenMeasurement);
-
-    mMutex.lock();
-    clearInterfaces();
-    mGnssMeasurementCbIface_2_1 = callback;
-    mMutex.unlock();
-
-    return startTracking(powerMode, timeBetweenMeasurement);
-}
 Return<IGnssMeasurement::GnssMeasurementStatus>
 MeasurementAPIClient::startTracking(
         GnssPowerMode powerMode, uint32_t timeBetweenMeasurement)
@@ -162,16 +103,6 @@
     memset(&locationCallbacks, 0, sizeof(LocationCallbacks));
     locationCallbacks.size = sizeof(LocationCallbacks);
 
-    locationCallbacks.trackingCb = nullptr;
-    locationCallbacks.batchingCb = nullptr;
-    locationCallbacks.geofenceBreachCb = nullptr;
-    locationCallbacks.geofenceStatusCb = nullptr;
-    locationCallbacks.gnssLocationInfoCb = nullptr;
-    locationCallbacks.gnssNiCb = nullptr;
-    locationCallbacks.gnssSvCb = nullptr;
-    locationCallbacks.gnssNmeaCb = nullptr;
-
-    locationCallbacks.gnssMeasurementsCb = nullptr;
     if (mGnssMeasurementCbIface_2_1 != nullptr ||
         mGnssMeasurementCbIface_2_0 != nullptr ||
         mGnssMeasurementCbIface_1_1 != nullptr ||
@@ -195,7 +126,8 @@
     }
 
     mTracking = true;
-    LOC_LOGD("%s]: start tracking session", __FUNCTION__);
+    LOC_LOGd("(powermode: %d) (tbm %d)", (int)powerMode, timeBetweenMeasurement);
+
     locAPIStartTracking(options);
     return IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
 }
@@ -204,6 +136,7 @@
 void MeasurementAPIClient::measurementClose() {
     LOC_LOGD("%s]: ()", __FUNCTION__);
     mTracking = false;
+    clearInterfaces();
     locAPIStopTracking();
 }
 
diff --git a/gps/android/2.1/location_api/MeasurementAPIClient.h b/gps/android/2.1/location_api/MeasurementAPIClient.h
index 3e8805b..1a3051f 100644
--- a/gps/android/2.1/location_api/MeasurementAPIClient.h
+++ b/gps/android/2.1/location_api/MeasurementAPIClient.h
@@ -32,7 +32,6 @@
 
 #include <mutex>
 #include <android/hardware/gnss/2.1/IGnssMeasurement.h>
-//#include <android/hardware/gnss/1.1/IGnssMeasurementCallback.h>
 #include <android/hardware/gnss/2.1/IGnssMeasurementCallback.h>
 #include <LocationAPIClientBase.h>
 #include <hidl/Status.h>
@@ -54,20 +53,16 @@
     MeasurementAPIClient& operator=(const MeasurementAPIClient&) = delete;
 
     // for GpsMeasurementInterface
-    Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback(
-            const sp<V1_0::IGnssMeasurementCallback>& callback);
-    Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback_1_1(
-            const sp<V1_1::IGnssMeasurementCallback>& callback,
-            GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID,
-            uint32_t timeBetweenMeasurement = GPS_DEFAULT_FIX_INTERVAL_MS);
-    Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback_2_0(
-            const sp<V2_0::IGnssMeasurementCallback>& callback,
-            GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID,
-            uint32_t timeBetweenMeasurement = GPS_DEFAULT_FIX_INTERVAL_MS);
-    Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback_2_1(
-            const sp<V2_1::IGnssMeasurementCallback>& callback,
-            GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID,
-            uint32_t timeBetweenMeasurement = GPS_DEFAULT_FIX_INTERVAL_MS);
+    template <typename T>
+    Return<IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback(
+            const sp<T>& callback, GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID) {
+        mMutex.lock();
+        setCallbackLocked(callback);
+        mMutex.unlock();
+
+        return startTracking(powerMode);
+    }
+
     void measurementClose();
     Return<IGnssMeasurement::GnssMeasurementStatus> startTracking(
             GnssPowerMode powerMode = GNSS_POWER_MODE_INVALID,
@@ -77,6 +72,18 @@
     void onGnssMeasurementsCb(GnssMeasurementsNotification gnssMeasurementsNotification) final;
 
 private:
+    inline void setCallbackLocked(const sp<V1_0::IGnssMeasurementCallback>& callback) {
+        mGnssMeasurementCbIface = callback;
+    }
+    inline void setCallbackLocked(const sp<V1_1::IGnssMeasurementCallback>& callback) {
+        mGnssMeasurementCbIface_1_1 = callback;
+    }
+    inline void setCallbackLocked(const sp<V2_0::IGnssMeasurementCallback>& callback) {
+        mGnssMeasurementCbIface_2_0 = callback;
+    }
+    inline void setCallbackLocked(const sp<V2_1::IGnssMeasurementCallback>& callback) {
+        mGnssMeasurementCbIface_2_1 = callback;
+    }
     virtual ~MeasurementAPIClient();
 
     std::mutex mMutex;