Add IGnssPowerIndication AIDL HAL (hardware/interfaces)

Bug: 168123084
Bug: 171821213
Bug: 172893736
Test: on Cuttlefish
Change-Id: Ia9fd1981a6f93b2cad168bd52dae8d7b8ede0282
diff --git a/gnss/aidl/default/Android.bp b/gnss/aidl/default/Android.bp
index 1fe43c3..23677ed 100644
--- a/gnss/aidl/default/Android.bp
+++ b/gnss/aidl/default/Android.bp
@@ -47,6 +47,7 @@
     srcs: [
         "Gnss.cpp",
         "GnssHidlHal.cpp",
+        "GnssPowerIndication.cpp",
         "GnssPsds.cpp",
         "GnssConfiguration.cpp",
         "service.cpp",
diff --git a/gnss/aidl/default/Gnss.cpp b/gnss/aidl/default/Gnss.cpp
index b4d92fd..669372b 100644
--- a/gnss/aidl/default/Gnss.cpp
+++ b/gnss/aidl/default/Gnss.cpp
@@ -19,6 +19,7 @@
 #include "Gnss.h"
 #include <log/log.h>
 #include "GnssConfiguration.h"
+#include "GnssPowerIndication.h"
 #include "GnssPsds.h"
 
 namespace aidl::android::hardware::gnss {
@@ -65,4 +66,12 @@
     return ndk::ScopedAStatus::ok();
 }
 
+ndk::ScopedAStatus Gnss::getExtensionGnssPowerIndication(
+        std::shared_ptr<IGnssPowerIndication>* iGnssPowerIndication) {
+    ALOGD("Gnss::getExtensionGnssPowerIndication");
+
+    *iGnssPowerIndication = SharedRefBase::make<GnssPowerIndication>();
+    return ndk::ScopedAStatus::ok();
+}
+
 }  // namespace aidl::android::hardware::gnss
diff --git a/gnss/aidl/default/Gnss.h b/gnss/aidl/default/Gnss.h
index 61d7cf7..9f6ef0e 100644
--- a/gnss/aidl/default/Gnss.h
+++ b/gnss/aidl/default/Gnss.h
@@ -18,6 +18,7 @@
 
 #include <aidl/android/hardware/gnss/BnGnss.h>
 #include <aidl/android/hardware/gnss/BnGnssConfiguration.h>
+#include <aidl/android/hardware/gnss/BnGnssPowerIndication.h>
 #include <aidl/android/hardware/gnss/BnGnssPsds.h>
 #include "GnssConfiguration.h"
 
@@ -30,6 +31,8 @@
     ndk::ScopedAStatus getExtensionPsds(std::shared_ptr<IGnssPsds>* iGnssPsds) override;
     ndk::ScopedAStatus getExtensionGnssConfiguration(
             std::shared_ptr<IGnssConfiguration>* iGnssConfiguration) override;
+    ndk::ScopedAStatus getExtensionGnssPowerIndication(
+            std::shared_ptr<IGnssPowerIndication>* iGnssPowerIndication) override;
 
     std::shared_ptr<GnssConfiguration> mGnssConfiguration;
 
diff --git a/gnss/aidl/default/GnssPowerIndication.cpp b/gnss/aidl/default/GnssPowerIndication.cpp
new file mode 100644
index 0000000..8ea60f3
--- /dev/null
+++ b/gnss/aidl/default/GnssPowerIndication.cpp
@@ -0,0 +1,62 @@
+/*
+ * 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 "GnssPowerIndicationAidl"
+
+#include "GnssPowerIndication.h"
+#include <aidl/android/hardware/gnss/BnGnss.h>
+#include <log/log.h>
+
+namespace aidl::android::hardware::gnss {
+
+std::shared_ptr<IGnssPowerIndicationCallback> GnssPowerIndication::sCallback = nullptr;
+
+ndk::ScopedAStatus GnssPowerIndication::setCallback(
+        const std::shared_ptr<IGnssPowerIndicationCallback>& callback) {
+    ALOGD("setCallback");
+    std::unique_lock<std::mutex> lock(mMutex);
+    sCallback = callback;
+    sCallback->setCapabilitiesCb(IGnssPowerIndicationCallback::CAPABILITY_TOTAL |
+                                 IGnssPowerIndicationCallback::CAPABILITY_SINGLEBAND_TRACKING |
+                                 IGnssPowerIndicationCallback::CAPABILITY_MULTIBAND_TRACKING |
+                                 IGnssPowerIndicationCallback::CAPABILITY_SINGLEBAND_ACQUISITION |
+                                 IGnssPowerIndicationCallback::CAPABILITY_MULTIBAND_ACQUISITION |
+                                 IGnssPowerIndicationCallback::CAPABILITY_OTHER_MODES);
+    return ndk::ScopedAStatus::ok();
+}
+
+ndk::ScopedAStatus GnssPowerIndication::requestGnssPowerStats() {
+    ALOGD("requestGnssPowerStats");
+    std::unique_lock<std::mutex> lock(mMutex);
+
+    ElapsedRealtime elapsedRealtime = {
+            .flags = IGnss::ELAPSED_REALTIME_HAS_TIMESTAMP_NS |
+                     IGnss::ELAPSED_REALTIME_HAS_TIME_UNCERTAINTY_NS,
+            .timestampNs = (long)1323542,
+            .timeUncertaintyNs = (long)1000,
+    };
+    GnssPowerStats gnssPowerStats = {
+            .elapsedRealtime = elapsedRealtime,
+            .totalEnergyMilliJoule = 1.59975e+3,
+            .singlebandTrackingModeEnergyMilliJoule = 1.2342e+3,
+            .multibandTrackingModeEnergyMilliJoule = 3.653e+2,
+            .otherModesEnergyMilliJoule = {1.232e+2, 3.234e+3},
+    };
+    sCallback->gnssPowerStatsCb(gnssPowerStats);
+    return ndk::ScopedAStatus::ok();
+}
+
+}  // namespace aidl::android::hardware::gnss
diff --git a/gnss/aidl/default/GnssPowerIndication.h b/gnss/aidl/default/GnssPowerIndication.h
new file mode 100644
index 0000000..e32fd72
--- /dev/null
+++ b/gnss/aidl/default/GnssPowerIndication.h
@@ -0,0 +1,37 @@
+/*
+ * 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 <aidl/android/hardware/gnss/BnGnssPowerIndication.h>
+
+namespace aidl::android::hardware::gnss {
+
+struct GnssPowerIndication : public BnGnssPowerIndication {
+  public:
+    ndk::ScopedAStatus setCallback(
+            const std::shared_ptr<IGnssPowerIndicationCallback>& callback) override;
+    ndk::ScopedAStatus requestGnssPowerStats() override;
+
+  private:
+    // Guarded by mMutex
+    static std::shared_ptr<IGnssPowerIndicationCallback> sCallback;
+
+    // Synchronization lock for sCallback
+    mutable std::mutex mMutex;
+};
+
+}  // namespace aidl::android::hardware::gnss