Support the location injection in AIDL HAL

Bug: 213225295
Test: atest VtsHalGnssTargetTest
Change-Id: Iff9fca55722af9bad6cc50f0170e4e1a069d05d6
diff --git a/gnss/aidl/default/Gnss.cpp b/gnss/aidl/default/Gnss.cpp
index e296351..6578778 100644
--- a/gnss/aidl/default/Gnss.cpp
+++ b/gnss/aidl/default/Gnss.cpp
@@ -20,6 +20,7 @@
 #include <inttypes.h>
 #include <log/log.h>
 #include "AGnss.h"
+#include "DeviceFileReader.h"
 #include "GnssBatching.h"
 #include "GnssConfiguration.h"
 #include "GnssDebug.h"
@@ -28,10 +29,13 @@
 #include "GnssNavigationMessageInterface.h"
 #include "GnssPsds.h"
 #include "GnssVisibilityControl.h"
+#include "NmeaFixInfo.h"
 #include "Utils.h"
 
 namespace aidl::android::hardware::gnss {
+using ::android::hardware::gnss::common::NmeaFixInfo;
 using ::android::hardware::gnss::common::Utils;
+
 using ndk::ScopedAStatus;
 using GnssSvInfo = IGnssCallback::GnssSvInfo;
 
@@ -62,6 +66,12 @@
     return ScopedAStatus::ok();
 }
 
+std::unique_ptr<GnssLocation> Gnss::getLocationFromHW() {
+    std::string inputStr =
+            ::android::hardware::gnss::common::DeviceFileReader::Instance().getLocationData();
+    return ::android::hardware::gnss::common::NmeaFixInfo::getAidlLocationFromInputStr(inputStr);
+}
+
 ScopedAStatus Gnss::start() {
     ALOGD("start()");
     if (mIsActive) {
@@ -82,9 +92,14 @@
             auto svStatus = filterBlocklistedSatellites(Utils::getMockSvInfoList());
             this->reportSvStatus(svStatus);
 
+            auto currentLocation = getLocationFromHW();
             mGnssPowerIndication->notePowerConsumption();
-            const auto location = Utils::getMockLocation();
-            this->reportLocation(location);
+            if (currentLocation != nullptr) {
+                this->reportLocation(*currentLocation);
+            } else {
+                const auto location = Utils::getMockLocation();
+                this->reportLocation(location);
+            }
             std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMs));
         }
     });
diff --git a/gnss/aidl/default/Gnss.h b/gnss/aidl/default/Gnss.h
index 384c862..f21d756 100644
--- a/gnss/aidl/default/Gnss.h
+++ b/gnss/aidl/default/Gnss.h
@@ -79,6 +79,7 @@
     std::vector<IGnssCallback::GnssSvInfo> filterBlocklistedSatellites(
             std::vector<IGnssCallback::GnssSvInfo> gnssSvInfoList);
     void reportGnssStatusValue(const IGnssCallback::GnssStatusValue gnssStatusValue) const;
+    std::unique_ptr<GnssLocation> getLocationFromHW();
 
     static std::shared_ptr<IGnssCallback> sGnssCallback;
 
diff --git a/gnss/common/utils/default/GnssReplayUtils.cpp b/gnss/common/utils/default/GnssReplayUtils.cpp
index e3f4ff8..5356477 100644
--- a/gnss/common/utils/default/GnssReplayUtils.cpp
+++ b/gnss/common/utils/default/GnssReplayUtils.cpp
@@ -41,7 +41,7 @@
 
 bool ReplayUtils::isNMEA(const std::string& inputStr) {
     return !inputStr.empty() && (inputStr.find("$GPRMC,", 0) != std::string::npos ||
-                                 inputStr.find("$GPRMA,", 0) != std::string::npos);
+                                 inputStr.find("$GPGGA,", 0) != std::string::npos);
 }
 
 }  // namespace common
diff --git a/gnss/common/utils/default/NmeaFixInfo.cpp b/gnss/common/utils/default/NmeaFixInfo.cpp
index c7ee134..22aef90 100644
--- a/gnss/common/utils/default/NmeaFixInfo.cpp
+++ b/gnss/common/utils/default/NmeaFixInfo.cpp
@@ -34,6 +34,9 @@
 namespace gnss {
 namespace common {
 
+using aidl::android::hardware::gnss::ElapsedRealtime;
+using aidl::android::hardware::gnss::GnssLocation;
+
 NmeaFixInfo::NmeaFixInfo() : hasGMCRecord(false), hasGGARecord(false) {}
 
 float NmeaFixInfo::getAltitudeMeters() const {
@@ -237,6 +240,40 @@
 }
 
 /**
+ * Convert V2_0::GnssLocation to aidl::GnssLocation.
+ */
+std::unique_ptr<GnssLocation> NmeaFixInfo::getAidlLocationFromInputStr(
+        const std::string& inputStr) {
+    std::unique_ptr<V2_0::GnssLocation> locationV2 = getLocationFromInputStr(inputStr);
+    if (locationV2 == nullptr) {
+        return nullptr;
+    }
+
+    ElapsedRealtime elapsedRealtime = {
+            .flags = ElapsedRealtime::HAS_TIMESTAMP_NS | ElapsedRealtime::HAS_TIME_UNCERTAINTY_NS,
+            .timestampNs = ::android::elapsedRealtimeNano(),
+            // This is an hardcoded value indicating a 1ms of uncertainty between the two clocks.
+            // In an actual implementation provide an estimate of the synchronization uncertainty
+            // or don't set the field.
+            .timeUncertaintyNs = 1020400};
+
+    GnssLocation location = {
+            .gnssLocationFlags = locationV2->v1_0.gnssLocationFlags,
+            .latitudeDegrees = locationV2->v1_0.latitudeDegrees,
+            .longitudeDegrees = locationV2->v1_0.longitudeDegrees,
+            .altitudeMeters = locationV2->v1_0.altitudeMeters,
+            .speedMetersPerSec = locationV2->v1_0.speedMetersPerSec,
+            .bearingDegrees = locationV2->v1_0.bearingDegrees,
+            .horizontalAccuracyMeters = locationV2->v1_0.horizontalAccuracyMeters,
+            .verticalAccuracyMeters = locationV2->v1_0.verticalAccuracyMeters,
+            .speedAccuracyMetersPerSecond = locationV2->v1_0.speedAccuracyMetersPerSecond,
+            .bearingAccuracyDegrees = locationV2->v1_0.bearingAccuracyDegrees,
+            .timestampMillis = locationV2->v1_0.timestamp,
+            .elapsedRealtime = elapsedRealtime};
+    return std::make_unique<GnssLocation>(location);
+}
+
+/**
  * Parses the input string in NMEA format and convert to GnssLocation.
  */
 std::unique_ptr<V2_0::GnssLocation> NmeaFixInfo::toGnssLocation() const {
diff --git a/gnss/common/utils/default/include/NmeaFixInfo.h b/gnss/common/utils/default/include/NmeaFixInfo.h
index 5c27045..4073361 100644
--- a/gnss/common/utils/default/include/NmeaFixInfo.h
+++ b/gnss/common/utils/default/include/NmeaFixInfo.h
@@ -22,6 +22,7 @@
 #include <hidl/Status.h>
 #include <ctime>
 #include <string>
+#include "aidl/android/hardware/gnss/IGnss.h"
 namespace android {
 namespace hardware {
 namespace gnss {
@@ -45,6 +46,8 @@
 
   public:
     static std::unique_ptr<V2_0::GnssLocation> getLocationFromInputStr(const std::string& inputStr);
+    static std::unique_ptr<aidl::android::hardware::gnss::GnssLocation> getAidlLocationFromInputStr(
+            const std::string& inputStr);
 
   private:
     static void splitStr(const std::string& line, const char& delimiter,