Merge Android 12

Bug: 202323961
Merged-In: Ic983f30d9643eb1d0af6fd3339c0a2ba38f043a0
Change-Id: I133ff9529758192196da2643cd82f5c2e0d62467
diff --git a/msm8998/android/Android.mk b/msm8998/android/Android.mk
index 142fdc8..4c6893a 100644
--- a/msm8998/android/Android.mk
+++ b/msm8998/android/Android.mk
@@ -22,8 +22,8 @@
     location_api/LocationUtil.cpp \
     location_api/GnssAPIClient.cpp \
     location_api/GeofenceAPIClient.cpp \
-    location_api/FlpAPIClient.cpp \
-    location_api/GnssMeasurementAPIClient.cpp \
+    location_api/BatchingAPIClient.cpp \
+    location_api/MeasurementAPIClient.cpp \
 
 LOCAL_C_INCLUDES:= \
     $(LOCAL_PATH)/location_api \
diff --git a/msm8998/android/GnssBatching.cpp b/msm8998/android/GnssBatching.cpp
index b79105c..6ffadc9 100644
--- a/msm8998/android/GnssBatching.cpp
+++ b/msm8998/android/GnssBatching.cpp
@@ -21,7 +21,7 @@
 #define LOG_TAG "LocSvc_GnssBatchingInterface"
 
 #include <log_util.h>
-#include <FlpAPIClient.h>
+#include <BatchingAPIClient.h>
 #include "GnssBatching.h"
 
 namespace android {
@@ -60,7 +60,7 @@
         mApi = nullptr;
     }
 
-    mApi = new FlpAPIClient(callback);
+    mApi = new BatchingAPIClient(callback);
     if (mApi == nullptr) {
         LOC_LOGE("%s]: failed to create mApi", __FUNCTION__);
         return false;
@@ -82,7 +82,7 @@
     if (mApi == nullptr) {
         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
     } else {
-        ret = mApi->flpGetBatchSize();
+        ret = mApi->getBatchSize();
     }
     return ret;
 }
@@ -92,7 +92,7 @@
     if (mApi == nullptr) {
         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
     } else {
-        ret = mApi->flpStartSession(options);
+        ret = mApi->startSession(options);
     }
     return ret;
 }
@@ -101,7 +101,7 @@
     if (mApi == nullptr) {
         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
     } else {
-        mApi->flpFlushBatchedLocations();
+        mApi->flushBatchedLocations();
     }
     return Void();
 }
@@ -111,7 +111,7 @@
     if (mApi == nullptr) {
         LOC_LOGE("%s]: mApi is nullptr", __FUNCTION__);
     } else {
-        ret = mApi->flpStopSession();
+        ret = mApi->stopSession();
     }
     return ret;
 }
diff --git a/msm8998/android/GnssBatching.h b/msm8998/android/GnssBatching.h
index b46fae8..de5d5c3 100644
--- a/msm8998/android/GnssBatching.h
+++ b/msm8998/android/GnssBatching.h
@@ -42,7 +42,7 @@
 using ::android::hardware::Void;
 using ::android::sp;
 
-class FlpAPIClient;
+class BatchingAPIClient;
 struct GnssBatching : public IGnssBatching {
     GnssBatching();
     ~GnssBatching();
@@ -68,7 +68,7 @@
  private:
     sp<GnssBatchingDeathRecipient> mGnssBatchingDeathRecipient = nullptr;
     sp<IGnssBatchingCallback> mGnssBatchingCbIface = nullptr;
-    FlpAPIClient* mApi = nullptr;
+    BatchingAPIClient* mApi = nullptr;
 };
 
 }  // namespace implementation
diff --git a/msm8998/android/GnssMeasurement.cpp b/msm8998/android/GnssMeasurement.cpp
index bc07265..8cbfabd 100644
--- a/msm8998/android/GnssMeasurement.cpp
+++ b/msm8998/android/GnssMeasurement.cpp
@@ -21,7 +21,7 @@
 #define LOG_TAG "LocSvc_GnssMeasurementInterface"
 
 #include <log_util.h>
-#include <GnssMeasurementAPIClient.h>
+#include <MeasurementAPIClient.h>
 #include "GnssMeasurement.h"
 
 namespace android {
@@ -41,7 +41,7 @@
 
 GnssMeasurement::GnssMeasurement() {
     mGnssMeasurementDeathRecipient = new GnssMeasurementDeathRecipient(this);
-    mApi = new GnssMeasurementAPIClient();
+    mApi = new MeasurementAPIClient();
 }
 
 GnssMeasurement::~GnssMeasurement() {
@@ -74,7 +74,7 @@
     mGnssMeasurementCbIface = callback;
     mGnssMeasurementCbIface->linkToDeath(mGnssMeasurementDeathRecipient, 0 /*cookie*/);
 
-    return mApi->gnssMeasurementSetCallback(callback);
+    return mApi->measurementSetCallback(callback);
 }
 
 Return<void> GnssMeasurement::close()  {
@@ -87,7 +87,7 @@
         mGnssMeasurementCbIface->unlinkToDeath(mGnssMeasurementDeathRecipient);
         mGnssMeasurementCbIface = nullptr;
     }
-    mApi->gnssMeasurementClose();
+    mApi->measurementClose();
 
     return Void();
 }
diff --git a/msm8998/android/GnssMeasurement.h b/msm8998/android/GnssMeasurement.h
index cd364c3..5073169 100644
--- a/msm8998/android/GnssMeasurement.h
+++ b/msm8998/android/GnssMeasurement.h
@@ -38,7 +38,7 @@
 using ::android::hardware::hidl_string;
 using ::android::sp;
 
-class GnssMeasurementAPIClient;
+class MeasurementAPIClient;
 struct GnssMeasurement : public IGnssMeasurement {
     GnssMeasurement();
     ~GnssMeasurement();
@@ -64,7 +64,7 @@
  private:
     sp<GnssMeasurementDeathRecipient> mGnssMeasurementDeathRecipient = nullptr;
     sp<IGnssMeasurementCallback> mGnssMeasurementCbIface = nullptr;
-    GnssMeasurementAPIClient* mApi;
+    MeasurementAPIClient* mApi;
 };
 
 }  // namespace implementation
diff --git a/msm8998/android/location_api/FlpAPIClient.cpp b/msm8998/android/location_api/BatchingAPIClient.cpp
similarity index 89%
rename from msm8998/android/location_api/FlpAPIClient.cpp
rename to msm8998/android/location_api/BatchingAPIClient.cpp
index f3af773..7325592 100644
--- a/msm8998/android/location_api/FlpAPIClient.cpp
+++ b/msm8998/android/location_api/BatchingAPIClient.cpp
@@ -28,13 +28,13 @@
  */
 
 #define LOG_NDDEBUG 0
-#define LOG_TAG "LocSvc_FlpAPIClient"
+#define LOG_TAG "LocSvc_BatchingAPIClient"
 
 #include <log_util.h>
 #include <loc_cfg.h>
 
 #include "LocationUtil.h"
-#include "FlpAPIClient.h"
+#include "BatchingAPIClient.h"
 
 namespace android {
 namespace hardware {
@@ -45,7 +45,7 @@
 static void convertBatchOption(const IGnssBatching::Options& in, LocationOptions& out,
         LocationCapabilitiesMask mask);
 
-FlpAPIClient::FlpAPIClient(const sp<IGnssBatchingCallback>& callback) :
+BatchingAPIClient::BatchingAPIClient(const sp<IGnssBatchingCallback>& callback) :
     LocationAPIClientBase(),
     mGnssBatchingCbIface(callback),
     mDefaultId(42),
@@ -74,18 +74,18 @@
     locAPISetCallbacks(locationCallbacks);
 }
 
-FlpAPIClient::~FlpAPIClient()
+BatchingAPIClient::~BatchingAPIClient()
 {
     LOC_LOGD("%s]: ()", __FUNCTION__);
 }
 
-int FlpAPIClient::flpGetBatchSize()
+int BatchingAPIClient::getBatchSize()
 {
     LOC_LOGD("%s]: ()", __FUNCTION__);
     return locAPIGetBatchSize();
 }
 
-int FlpAPIClient::flpStartSession(const IGnssBatching::Options& opts)
+int BatchingAPIClient::startSession(const IGnssBatching::Options& opts)
 {
     LOC_LOGD("%s]: (%lld %d)", __FUNCTION__,
             static_cast<long long>(opts.periodNanos), static_cast<uint8_t>(opts.flags));
@@ -102,7 +102,7 @@
     return retVal;
 }
 
-int FlpAPIClient::flpUpdateSessionOptions(const IGnssBatching::Options& opts)
+int BatchingAPIClient::updateSessionOptions(const IGnssBatching::Options& opts)
 {
     LOC_LOGD("%s]: (%lld %d)", __FUNCTION__,
             static_cast<long long>(opts.periodNanos), static_cast<uint8_t>(opts.flags));
@@ -120,7 +120,7 @@
     return retVal;
 }
 
-int FlpAPIClient::flpStopSession()
+int BatchingAPIClient::stopSession()
 {
     LOC_LOGD("%s]: ", __FUNCTION__);
     int retVal = -1;
@@ -130,25 +130,25 @@
     return retVal;
 }
 
-void FlpAPIClient::flpGetBatchedLocation(int last_n_locations)
+void BatchingAPIClient::getBatchedLocation(int last_n_locations)
 {
     LOC_LOGD("%s]: (%d)", __FUNCTION__, last_n_locations);
     locAPIGetBatchedLocations(last_n_locations);
 }
 
-void FlpAPIClient::flpFlushBatchedLocations()
+void BatchingAPIClient::flushBatchedLocations()
 {
     LOC_LOGD("%s]: ()", __FUNCTION__);
     locAPIGetBatchedLocations(SIZE_MAX);
 }
 
-void FlpAPIClient::onCapabilitiesCb(LocationCapabilitiesMask capabilitiesMask)
+void BatchingAPIClient::onCapabilitiesCb(LocationCapabilitiesMask capabilitiesMask)
 {
     LOC_LOGD("%s]: (%02x)", __FUNCTION__, capabilitiesMask);
     mLocationCapabilitiesMask = capabilitiesMask;
 }
 
-void FlpAPIClient::onBatchingCb(size_t count, Location* location)
+void BatchingAPIClient::onBatchingCb(size_t count, Location* location)
 {
     LOC_LOGD("%s]: (count: %zu)", __FUNCTION__, count);
     if (mGnssBatchingCbIface != nullptr && count > 0) {
diff --git a/msm8998/android/location_api/FlpAPIClient.h b/msm8998/android/location_api/BatchingAPIClient.h
similarity index 78%
rename from msm8998/android/location_api/FlpAPIClient.h
rename to msm8998/android/location_api/BatchingAPIClient.h
index 9fc7e19..da62a51 100644
--- a/msm8998/android/location_api/FlpAPIClient.h
+++ b/msm8998/android/location_api/BatchingAPIClient.h
@@ -27,8 +27,8 @@
  *
  */
 
-#ifndef FLP_API_CLINET_H
-#define FLP_API_CLINET_H
+#ifndef BATCHING_API_CLINET_H
+#define BATCHING_API_CLINET_H
 
 #include <android/hardware/gnss/1.0/IGnssBatching.h>
 #include <android/hardware/gnss/1.0/IGnssBatchingCallback.h>
@@ -36,7 +36,6 @@
 
 #include <LocationAPIClientBase.h>
 
-#define FLP_CONF_FILE "/vendor/etc/flp.conf"
 
 namespace android {
 namespace hardware {
@@ -44,19 +43,19 @@
 namespace V1_0 {
 namespace implementation {
 
-class FlpAPIClient : public LocationAPIClientBase
+class BatchingAPIClient : public LocationAPIClientBase
 {
 public:
-    FlpAPIClient(const sp<IGnssBatchingCallback>& callback);
-    ~FlpAPIClient();
-    int flpGetBatchSize();
-    int flpStartSession(const IGnssBatching::Options& options);
-    int flpUpdateSessionOptions(const IGnssBatching::Options& options);
-    int flpStopSession();
-    void flpGetBatchedLocation(int last_n_locations);
-    void flpFlushBatchedLocations();
+    BatchingAPIClient(const sp<IGnssBatchingCallback>& callback);
+    ~BatchingAPIClient();
+    int getBatchSize();
+    int startSession(const IGnssBatching::Options& options);
+    int updateSessionOptions(const IGnssBatching::Options& options);
+    int stopSession();
+    void getBatchedLocation(int last_n_locations);
+    void flushBatchedLocations();
 
-    inline LocationCapabilitiesMask flpGetCapabilities() { return mLocationCapabilitiesMask; }
+    inline LocationCapabilitiesMask getCapabilities() { return mLocationCapabilitiesMask; }
 
     // callbacks
     void onCapabilitiesCb(LocationCapabilitiesMask capabilitiesMask) final;
@@ -74,4 +73,4 @@
 }  // namespace gnss
 }  // namespace hardware
 }  // namespace android
-#endif // FLP_API_CLINET_H
+#endif // BATCHING_API_CLINET_H
diff --git a/msm8998/android/location_api/GnssMeasurementAPIClient.cpp b/msm8998/android/location_api/MeasurementAPIClient.cpp
similarity index 85%
rename from msm8998/android/location_api/GnssMeasurementAPIClient.cpp
rename to msm8998/android/location_api/MeasurementAPIClient.cpp
index 6e915b0..075ef69 100644
--- a/msm8998/android/location_api/GnssMeasurementAPIClient.cpp
+++ b/msm8998/android/location_api/MeasurementAPIClient.cpp
@@ -28,13 +28,13 @@
  */
 
 #define LOG_NDDEBUG 0
-#define LOG_TAG "LocSvc_GnssMeasurementAPIClient"
+#define LOG_TAG "LocSvc_MeasurementAPIClient"
 
 #include <log_util.h>
 #include <loc_cfg.h>
 
 #include "LocationUtil.h"
-#include "GnssMeasurementAPIClient.h"
+#include "MeasurementAPIClient.h"
 
 namespace android {
 namespace hardware {
@@ -48,32 +48,21 @@
         IGnssMeasurementCallback::GnssMeasurement& out);
 static void convertGnssClock(GnssMeasurementsClock& in, IGnssMeasurementCallback::GnssClock& out);
 
-GnssMeasurementAPIClient::GnssMeasurementAPIClient() :
+MeasurementAPIClient::MeasurementAPIClient() :
     mGnssMeasurementCbIface(nullptr),
-    mLocationCapabilitiesMask(0)
+    mTracking(false)
 {
     LOC_LOGD("%s]: ()", __FUNCTION__);
-    pthread_mutex_init(&mLock, nullptr);
-    pthread_cond_init (&mCond, nullptr);
-
-    // set default LocationOptions.
-    memset(&mLocationOptions, 0, sizeof(LocationOptions));
-    mLocationOptions.size = sizeof(LocationOptions);
-    mLocationOptions.minInterval = 1000;
-    mLocationOptions.minDistance = 0;
-    mLocationOptions.mode = GNSS_SUPL_MODE_STANDALONE;
 }
 
-GnssMeasurementAPIClient::~GnssMeasurementAPIClient()
+MeasurementAPIClient::~MeasurementAPIClient()
 {
     LOC_LOGD("%s]: ()", __FUNCTION__);
-    pthread_cond_destroy(&mCond);
-    pthread_mutex_destroy(&mLock);
 }
 
 // for GpsInterface
 Return<IGnssMeasurement::GnssMeasurementStatus>
-GnssMeasurementAPIClient::gnssMeasurementSetCallback(const sp<IGnssMeasurementCallback>& callback)
+MeasurementAPIClient::measurementSetCallback(const sp<IGnssMeasurementCallback>& callback)
 {
     LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
 
@@ -100,50 +89,32 @@
     }
 
     locAPISetCallbacks(locationCallbacks);
-
-    while (!mLocationCapabilitiesMask) {
-        LOC_LOGD("%s]: wait for capabilities...", __FUNCTION__);
-        pthread_mutex_lock(&mLock);
-        pthread_cond_wait(&mCond, &mLock);
-        pthread_mutex_unlock(&mLock);
-    }
-    if (mLocationCapabilitiesMask & LOCATION_CAPABILITIES_GNSS_MSB_BIT)
-        mLocationOptions.mode = GNSS_SUPL_MODE_MSB;
-    else
-        mLocationOptions.mode = GNSS_SUPL_MODE_STANDALONE;
+    LocationOptions options;
+    memset(&options, 0, sizeof(LocationOptions));
+    options.size = sizeof(LocationOptions);
+    options.minInterval = 1000;
+    options.mode = GNSS_SUPL_MODE_STANDALONE;
+    mTracking = true;
     LOC_LOGD("%s]: start tracking session", __FUNCTION__);
-    locAPIStartTracking(mLocationOptions);
+    locAPIStartTracking(options);
 
     return IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
 }
 
 // for GpsMeasurementInterface
-void GnssMeasurementAPIClient::gnssMeasurementClose() {
+void MeasurementAPIClient::measurementClose() {
     LOC_LOGD("%s]: ()", __FUNCTION__);
-    pthread_mutex_lock(&mLock);
-    mGnssMeasurementCbIface = nullptr;
-    pthread_mutex_unlock(&mLock);
+    mTracking = false;
     locAPIStopTracking();
 }
 
 // callbacks
-void GnssMeasurementAPIClient::onCapabilitiesCb(LocationCapabilitiesMask capabilitiesMask)
-{
-    LOC_LOGD("%s]: (%02x)", __FUNCTION__, capabilitiesMask);
-    mLocationCapabilitiesMask = capabilitiesMask;
-    pthread_mutex_lock(&mLock);
-    pthread_cond_signal(&mCond);
-    pthread_mutex_unlock(&mLock);
-}
-
-void GnssMeasurementAPIClient::onGnssMeasurementsCb(
+void MeasurementAPIClient::onGnssMeasurementsCb(
         GnssMeasurementsNotification gnssMeasurementsNotification)
 {
-    LOC_LOGD("%s]: (count: %zu)", __FUNCTION__, gnssMeasurementsNotification.count);
-    // we don't need to lock the mutext
-    // if mGnssMeasurementCbIface is set to nullptr
-    if (mGnssMeasurementCbIface != nullptr) {
-        pthread_mutex_lock(&mLock);
+    LOC_LOGD("%s]: (count: %zu active: %zu)",
+         __FUNCTION__, gnssMeasurementsNotification.count, mTracking);
+    if (mTracking) {
         if (mGnssMeasurementCbIface != nullptr) {
             IGnssMeasurementCallback::GnssData gnssData;
             convertGnssData(gnssMeasurementsNotification, gnssData);
@@ -153,7 +124,6 @@
                     __func__, r.description().c_str());
             }
         }
-        pthread_mutex_unlock(&mLock);
     }
 }
 
diff --git a/msm8998/android/location_api/GnssMeasurementAPIClient.h b/msm8998/android/location_api/MeasurementAPIClient.h
similarity index 76%
rename from msm8998/android/location_api/GnssMeasurementAPIClient.h
rename to msm8998/android/location_api/MeasurementAPIClient.h
index 02fe89a..422564d 100644
--- a/msm8998/android/location_api/GnssMeasurementAPIClient.h
+++ b/msm8998/android/location_api/MeasurementAPIClient.h
@@ -27,8 +27,8 @@
  *
  */
 
-#ifndef GNSS_MEASUREMENT_API_CLINET_H
-#define GNSS_MEASUREMENT_API_CLINET_H
+#ifndef MEASUREMENT_API_CLINET_H
+#define MEASUREMENT_API_CLINET_H
 
 
 #include <android/hardware/gnss/1.0/IGnssMeasurement.h>
@@ -45,32 +45,26 @@
 using ::android::hardware::gnss::V1_0::IGnssMeasurement;
 using ::android::sp;
 
-class GnssMeasurementAPIClient : public LocationAPIClientBase
+class MeasurementAPIClient : public LocationAPIClientBase
 {
 public:
-    GnssMeasurementAPIClient();
-    virtual ~GnssMeasurementAPIClient();
-    GnssMeasurementAPIClient(const GnssMeasurementAPIClient&) = delete;
-    GnssMeasurementAPIClient& operator=(const GnssMeasurementAPIClient&) = delete;
+    MeasurementAPIClient();
+    virtual ~MeasurementAPIClient();
+    MeasurementAPIClient(const MeasurementAPIClient&) = delete;
+    MeasurementAPIClient& operator=(const MeasurementAPIClient&) = delete;
 
     // for GpsMeasurementInterface
-    Return<IGnssMeasurement::GnssMeasurementStatus> gnssMeasurementSetCallback(
+    Return<IGnssMeasurement::GnssMeasurementStatus> measurementSetCallback(
             const sp<IGnssMeasurementCallback>& callback);
-    void gnssMeasurementClose();
+    void measurementClose();
 
     // callbacks we are interested in
-    void onCapabilitiesCb(LocationCapabilitiesMask capabilitiesMask) final;
     void onGnssMeasurementsCb(GnssMeasurementsNotification gnssMeasurementsNotification) final;
 
 private:
-    pthread_mutex_t mLock;
-    pthread_cond_t mCond;
-
     sp<IGnssMeasurementCallback> mGnssMeasurementCbIface;
 
-    LocationCapabilitiesMask mLocationCapabilitiesMask;
-
-    LocationOptions mLocationOptions;
+    bool mTracking;
 };
 
 }  // namespace implementation
@@ -78,4 +72,4 @@
 }  // namespace gnss
 }  // namespace hardware
 }  // namespace android
-#endif // GNSS_MEASUREMENT_API_CLINET_H
+#endif // MEASUREMENT_API_CLINET_H
diff --git a/msm8998/location/LocationAPIClientBase.cpp b/msm8998/location/LocationAPIClientBase.cpp
index 606a7b7..ea7f5a5 100644
--- a/msm8998/location/LocationAPIClientBase.cpp
+++ b/msm8998/location/LocationAPIClientBase.cpp
@@ -33,7 +33,7 @@
 #include <loc_cfg.h>
 #include "LocationAPIClientBase.h"
 
-#define FLP_CONF_FILE "/vendor/etc/flp.conf"
+#define BATCHING_CONF_FILE "/vendor/etc/flp.conf"
 #define GEOFENCE_SESSION_ID -1
 
 LocationAPIClientBase::LocationAPIClientBase() :
@@ -201,11 +201,11 @@
 int32_t LocationAPIClientBase::locAPIGetBatchSize()
 {
     if (mBatchSize == -1) {
-        const loc_param_s_type flp_conf_param_table[] =
+        const loc_param_s_type batching_conf_param_table[] =
         {
             {"BATCH_SIZE", &mBatchSize, nullptr, 'n'},
         };
-        UTIL_READ_CONF(FLP_CONF_FILE, flp_conf_param_table);
+        UTIL_READ_CONF(BATCHING_CONF_FILE, batching_conf_param_table);
         if (mBatchSize < 0) {
             // set mBatchSize to 0 if we got an illegal value from config file
             mBatchSize = 0;
diff --git a/sdm845/android/location_api/BatchingAPIClient.cpp b/sdm845/android/location_api/BatchingAPIClient.cpp
index ccb78b9..8ecfa55 100644
--- a/sdm845/android/location_api/BatchingAPIClient.cpp
+++ b/sdm845/android/location_api/BatchingAPIClient.cpp
@@ -32,6 +32,7 @@
 
 #include <log_util.h>
 #include <loc_cfg.h>
+#include <thread>
 
 #include "LocationUtil.h"
 #include "BatchingAPIClient.h"
@@ -90,12 +91,16 @@
 
 int BatchingAPIClient::getBatchSize()
 {
-    LOC_LOGD("%s]: ()", __FUNCTION__);
-    return locAPIGetBatchSize();
+    int batchSize = locAPIGetBatchSize();
+    LOC_LOGd("batchSize: %d", batchSize);
+    return batchSize;
 }
 
 int BatchingAPIClient::startSession(const IGnssBatching::Options& opts)
 {
+    mMutex.lock();
+    mState = STARTED;
+    mMutex.unlock();
     LOC_LOGD("%s]: (%lld %d)", __FUNCTION__,
             static_cast<long long>(opts.periodNanos), static_cast<uint8_t>(opts.flags));
     int retVal = -1;
@@ -131,8 +136,12 @@
 
 int BatchingAPIClient::stopSession()
 {
+    mMutex.lock();
+    mState = STOPPING;
+    mMutex.unlock();
     LOC_LOGD("%s]: ", __FUNCTION__);
     int retVal = -1;
+    locAPIGetBatchedLocations(mDefaultId, SIZE_MAX);
     if (locAPIStopSession(mDefaultId) == LOCATION_ERROR_SUCCESS) {
         retVal = 1;
     }
@@ -148,7 +157,13 @@
 void BatchingAPIClient::flushBatchedLocations()
 {
     LOC_LOGD("%s]: ()", __FUNCTION__);
-    locAPIGetBatchedLocations(mDefaultId, SIZE_MAX);
+    uint32_t retVal = locAPIGetBatchedLocations(mDefaultId, SIZE_MAX);
+    // when flush a stopped session or one doesn't exist, just report an empty batch.
+    if (LOCATION_ERROR_ID_UNKNOWN == retVal) {
+        BatchingOptions opt = {};
+        ::std::thread thd(&BatchingAPIClient::onBatchingCb, this, 0, nullptr, opt);
+        thd.detach();
+    }
 }
 
 void BatchingAPIClient::onCapabilitiesCb(LocationCapabilitiesMask capabilitiesMask)
@@ -159,19 +174,50 @@
 
 void BatchingAPIClient::onBatchingCb(size_t count, Location* location, BatchingOptions /*batchOptions*/)
 {
-    LOC_LOGD("%s]: (count: %zu)", __FUNCTION__, count);
-    if (mGnssBatchingCbIface != nullptr && count > 0) {
-        hidl_vec<GnssLocation> locationVec;
-        locationVec.resize(count);
-        for (size_t i = 0; i < count; i++) {
-            convertGnssLocation(location[i], locationVec[i]);
-        }
-        auto r = mGnssBatchingCbIface->gnssLocationBatchCb(locationVec);
-        if (!r.isOk()) {
-            LOC_LOGE("%s] Error from gnssLocationBatchCb description=%s",
-                __func__, r.description().c_str());
-        }
+    bool processReport = false;
+    LOC_LOGd("(count: %zu)", count);
+    mMutex.lock();
+    // back to back stop() and flush() could bring twice onBatchingCb(). Each one might come first.
+    // Combine them both (the first goes to cache, the second in location*) before report to FW
+    switch (mState) {
+        case STOPPING:
+            mState = STOPPED;
+            for (size_t i = 0; i < count; i++) {
+                mBatchedLocationInCache.push_back(location[i]);
+            }
+            break;
+        case STARTED:
+        case STOPPED: // flush() always trigger report, even on a stopped session
+            processReport = true;
+            break;
+        default:
+            break;
     }
+    // report location batch when in STARTED state or flush(), combined with cache in last stop()
+    if (processReport) {
+        auto gnssBatchingCbIface(mGnssBatchingCbIface);
+        size_t batchCacheCnt = mBatchedLocationInCache.size();
+        LOC_LOGd("(batchCacheCnt: %zu)", batchCacheCnt);
+        if (gnssBatchingCbIface != nullptr) {
+            hidl_vec<V1_0::GnssLocation> locationVec;
+            if (count+batchCacheCnt > 0) {
+                locationVec.resize(count+batchCacheCnt);
+                for (size_t i = 0; i < batchCacheCnt; ++i) {
+                    convertGnssLocation(mBatchedLocationInCache[i], locationVec[i]);
+                }
+                for (size_t i = 0; i < count; i++) {
+                    convertGnssLocation(location[i], locationVec[i+batchCacheCnt]);
+                }
+            }
+            auto r = gnssBatchingCbIface->gnssLocationBatchCb(locationVec);
+            if (!r.isOk()) {
+                LOC_LOGE("%s] Error from gnssLocationBatchCb 1.0 description=%s",
+                        __func__, r.description().c_str());
+            }
+        }
+        mBatchedLocationInCache.clear();
+    }
+    mMutex.unlock();
 }
 
 static void convertBatchOption(const IGnssBatching::Options& in, LocationOptions& out,
diff --git a/sdm845/android/location_api/BatchingAPIClient.h b/sdm845/android/location_api/BatchingAPIClient.h
index 64d47a0..93ccd16 100644
--- a/sdm845/android/location_api/BatchingAPIClient.h
+++ b/sdm845/android/location_api/BatchingAPIClient.h
@@ -33,7 +33,7 @@
 #include <android/hardware/gnss/1.0/IGnssBatching.h>
 #include <android/hardware/gnss/1.0/IGnssBatchingCallback.h>
 #include <pthread.h>
-
+#include <mutex>
 #include <LocationAPIClientBase.h>
 
 namespace android {
@@ -42,6 +42,8 @@
 namespace V1_1 {
 namespace implementation {
 
+enum BATCHING_STATE { STARTED, STOPPING, STOPPED };
+
 class BatchingAPIClient : public LocationAPIClientBase
 {
 public:
@@ -62,8 +64,12 @@
 
 private:
     sp<V1_0::IGnssBatchingCallback> mGnssBatchingCbIface;
+    std::mutex mMutex;
     uint32_t mDefaultId;
     LocationCapabilitiesMask mLocationCapabilitiesMask;
+    volatile BATCHING_STATE mState = STOPPED;
+
+    std::vector<Location> mBatchedLocationInCache;
 };
 
 }  // namespace implementation
diff --git a/sdm845/location/LocationAPIClientBase.cpp b/sdm845/location/LocationAPIClientBase.cpp
index 67e559b..f181798 100644
--- a/sdm845/location/LocationAPIClientBase.cpp
+++ b/sdm845/location/LocationAPIClientBase.cpp
@@ -613,7 +613,7 @@
             }
         }  else {
             retVal = LOCATION_ERROR_ID_UNKNOWN;
-            LOC_LOGE("%s:%d] invalid session: %d.", __FUNCTION__, __LINE__, id);
+            LOC_LOGd("unknown session id: %d, might flush() a stopped session",  id);
         }
     }
     pthread_mutex_unlock(&mMutex);