Add IGnssPowerIndication AIDL HAL (hardware/interfaces)

Bug: 168123084
Bug: 171821213
Bug: 172893736
Test: on Cuttlefish
Change-Id: Ia9fd1981a6f93b2cad168bd52dae8d7b8ede0282
diff --git a/gnss/aidl/vts/Android.bp b/gnss/aidl/vts/Android.bp
index 48a0e21..d102e7f 100644
--- a/gnss/aidl/vts/Android.bp
+++ b/gnss/aidl/vts/Android.bp
@@ -22,6 +22,7 @@
         "gnss_hal_test.cpp",
         "gnss_hal_test_cases.cpp",
         "GnssCallbackAidl.cpp",
+        "GnssPowerIndicationCallback.cpp",
         "VtsHalGnssTargetTest.cpp",
     ],
     shared_libs: [
diff --git a/gnss/aidl/vts/GnssPowerIndicationCallback.cpp b/gnss/aidl/vts/GnssPowerIndicationCallback.cpp
new file mode 100644
index 0000000..1cbfc20
--- /dev/null
+++ b/gnss/aidl/vts/GnssPowerIndicationCallback.cpp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "GnssPwrIndCallback"
+
+#include "GnssPowerIndicationCallback.h"
+#include <log/log.h>
+
+using android::hardware::gnss::GnssPowerStats;
+
+android::binder::Status GnssPowerIndicationCallback::setCapabilitiesCb(const int capabilities) {
+    ALOGI("Capabilities received %d", capabilities);
+    capabilities_cbq_.store(capabilities);
+    return android::binder::Status::ok();
+}
+
+android::binder::Status GnssPowerIndicationCallback::gnssPowerStatsCb(
+        const GnssPowerStats& gnssPowerStats) {
+    ALOGI("gnssPowerStatsCb");
+    ALOGI("elapsedRealtime: %ld, totalEnergyMilliJoule: %f",
+          (long)gnssPowerStats.elapsedRealtime.timestampNs, gnssPowerStats.totalEnergyMilliJoule);
+    ALOGI("singlebandTrackingModeEnergyMilliJoule: %f, multibandTrackingModeEnergyMilliJoule: %f",
+          gnssPowerStats.singlebandTrackingModeEnergyMilliJoule,
+          gnssPowerStats.multibandTrackingModeEnergyMilliJoule);
+    ALOGI("singlebandAcquisitionModeEnergyMilliJoule: %f, "
+          "multibandAcquisitionModeEnergyMilliJoule: %f",
+          gnssPowerStats.singlebandAcquisitionModeEnergyMilliJoule,
+          gnssPowerStats.multibandAcquisitionModeEnergyMilliJoule);
+    for (const auto& otherModeEnergyMilliJoule : gnssPowerStats.otherModesEnergyMilliJoule) {
+        ALOGI("otherModeEnergyMilliJoule: %f", otherModeEnergyMilliJoule);
+    }
+    gnss_power_stats_cbq_.store(gnssPowerStats);
+    return android::binder::Status::ok();
+}
diff --git a/gnss/aidl/vts/GnssPowerIndicationCallback.h b/gnss/aidl/vts/GnssPowerIndicationCallback.h
new file mode 100644
index 0000000..d4c4539
--- /dev/null
+++ b/gnss/aidl/vts/GnssPowerIndicationCallback.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <android/hardware/gnss/BnGnssPowerIndicationCallback.h>
+#include <android/hardware/gnss/GnssPowerStats.h>
+#include "GnssCallbackEventQueue.h"
+
+/** Implementation for IGnssPowerIndicationCallback. */
+class GnssPowerIndicationCallback : public android::hardware::gnss::BnGnssPowerIndicationCallback {
+  public:
+    GnssPowerIndicationCallback()
+        : capabilities_cbq_("capabilities"),
+          other_mode_names_cbq_("other_mode_names"),
+          gnss_power_stats_cbq_("gnss_power_stats") {}
+    ~GnssPowerIndicationCallback() {}
+
+    android::binder::Status setCapabilitiesCb(const int capabilities) override;
+    android::binder::Status gnssPowerStatsCb(
+            const android::hardware::gnss::GnssPowerStats& gnssPowerStats) override;
+
+    android::hardware::gnss::common::GnssCallbackEventQueue<int> capabilities_cbq_;
+    int last_capabilities_;
+    android::hardware::gnss::common::GnssCallbackEventQueue<std::vector<std::string>>
+            other_mode_names_cbq_;
+    std::vector<std::string> last_other_mode_names_;
+    android::hardware::gnss::common::GnssCallbackEventQueue<android::hardware::gnss::GnssPowerStats>
+            gnss_power_stats_cbq_;
+    android::hardware::gnss::GnssPowerStats last_gnss_power_stats_;
+};
diff --git a/gnss/aidl/vts/gnss_hal_test_cases.cpp b/gnss/aidl/vts/gnss_hal_test_cases.cpp
index d621053..4e8d0bd 100644
--- a/gnss/aidl/vts/gnss_hal_test_cases.cpp
+++ b/gnss/aidl/vts/gnss_hal_test_cases.cpp
@@ -16,13 +16,16 @@
 
 #define LOG_TAG "GnssHalTestCases"
 
+#include <android/hardware/gnss/IGnssPowerIndication.h>
 #include <android/hardware/gnss/IGnssPsds.h>
+#include "GnssPowerIndicationCallback.h"
 #include "gnss_hal_test.h"
 
 using android::sp;
 using android::hardware::gnss::BlocklistedSource;
 using GnssConstellationTypeAidl = android::hardware::gnss::GnssConstellationType;
 using android::hardware::gnss::IGnssConfiguration;
+using android::hardware::gnss::IGnssPowerIndication;
 using android::hardware::gnss::IGnssPsds;
 using android::hardware::gnss::PsdsType;
 
@@ -37,7 +40,7 @@
 /*
  * TestPsdsExtension:
  * 1. Gets the PsdsExtension and verifies that it returns a non-null extension.
- * 2. Injects empty PSDS data and verifies that it returns false.
+ * 2. Injects empty PSDS data and verifies that it returns an error.
  */
 TEST_P(GnssHalTest, TestPsdsExtension) {
     sp<IGnssPsds> iGnssPsds;
@@ -50,6 +53,34 @@
 }
 
 /*
+ * TestGnssPowerIndication
+ * 1. Gets the GnssPowerIndicationExtension.
+ * 2. Sets a GnssPowerIndicationCallback.
+ * 3.
+ */
+TEST_P(GnssHalTest, TestGnssPowerIndication) {
+    sp<IGnssPowerIndication> iGnssPowerIndication;
+    auto status = aidl_gnss_hal_->getExtensionGnssPowerIndication(&iGnssPowerIndication);
+    ASSERT_TRUE(status.isOk());
+    ASSERT_TRUE(iGnssPowerIndication != nullptr);
+
+    auto gnssPowerIndicationCallback = sp<GnssPowerIndicationCallback>::make();
+    status = iGnssPowerIndication->setCallback(gnssPowerIndicationCallback);
+    ASSERT_TRUE(status.isOk());
+
+    const int kTimeoutSec = 2;
+    EXPECT_TRUE(gnssPowerIndicationCallback->capabilities_cbq_.retrieve(
+            gnssPowerIndicationCallback->last_capabilities_, kTimeoutSec));
+
+    EXPECT_EQ(gnssPowerIndicationCallback->capabilities_cbq_.calledCount(), 1);
+
+    iGnssPowerIndication->requestGnssPowerStats();
+    EXPECT_TRUE(gnssPowerIndicationCallback->gnss_power_stats_cbq_.retrieve(
+            gnssPowerIndicationCallback->last_gnss_power_stats_, kTimeoutSec));
+    EXPECT_EQ(gnssPowerIndicationCallback->gnss_power_stats_cbq_.calledCount(), 1);
+}
+
+/*
  * FindStrongFrequentNonGpsSource:
  *
  * Search through a GnssSvStatus list for the strongest non-GPS satellite observed enough times