Upgrade IPower to @1.3

This change add @1.3 and addresses some
techical debt.

Bug: 151980167
Test: boot
Test: ...power@1.3-service.ranchu: IPower@1.3 service is initialized
Signed-off-by: Roman Kiryanov <rkir@google.com>
Merged-In: I6871af9ca056368d80704ebd22633519c2bbf09e
Change-Id: I9706840b64f43e1f5c74bc3a59a3a4aa797a34e0
diff --git a/manifest.xml b/manifest.xml
index 6450113..ce4b2bd 100644
--- a/manifest.xml
+++ b/manifest.xml
@@ -96,15 +96,6 @@
         </interface>
     </hal>
     <hal format="hidl">
-        <name>android.hardware.power</name>
-        <transport>hwbinder</transport>
-        <version>1.1</version>
-        <interface>
-            <name>IPower</name>
-            <instance>default</instance>
-        </interface>
-    </hal>
-    <hal format="hidl">
         <name>android.hardware.media.omx</name>
         <transport>hwbinder</transport>
         <version>1.0</version>
diff --git a/power/Android.bp b/power/Android.bp
new file mode 100644
index 0000000..85b860d
--- /dev/null
+++ b/power/Android.bp
@@ -0,0 +1,18 @@
+cc_binary {
+    name: "android.hardware.power@1.3-service.ranchu",
+    defaults: ["hidl_defaults"],
+    vendor: true,
+    relative_install_path: "hw",
+    srcs: ["main.cpp"],
+    shared_libs: [
+        "libbase",
+        "libcutils",
+        "libhidlbase",
+        "liblog",
+        "libutils",
+        "android.hardware.power@1.3",
+    ],
+    vintf_fragments: ["android.hardware.power@1.3-service.ranchu.xml"],
+    init_rc: ["android.hardware.power@1.3-service.ranchu.rc"],
+}
+
diff --git a/power/Android.mk b/power/Android.mk
deleted file mode 100644
index 4a0b4d9..0000000
--- a/power/Android.mk
+++ /dev/null
@@ -1,37 +0,0 @@
-# Copyright (C) 2017 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.
-
-LOCAL_PATH := $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_VENDOR_MODULE := true
-LOCAL_MODULE_RELATIVE_PATH := hw
-
-LOCAL_MODULE := android.hardware.power@1.1-service.ranchu
-LOCAL_INIT_RC := android.hardware.power@1.1-service.ranchu.rc
-LOCAL_SRC_FILES := service.cpp Power.cpp
-
-LOCAL_HEADER_LIBRARIES := libhardware_headers
-
-LOCAL_SHARED_LIBRARIES := \
-    libbase \
-    libcutils \
-    libhidlbase \
-    liblog \
-    libutils \
-    android.hardware.power@1.0 \
-    android.hardware.power@1.1 \
-
-include $(BUILD_EXECUTABLE)
diff --git a/power/Power.cpp b/power/Power.cpp
deleted file mode 100644
index 5ba33c8..0000000
--- a/power/Power.cpp
+++ /dev/null
@@ -1,146 +0,0 @@
-/*
- * Copyright (C) 2017 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 "android.hardware.power@1.1-service.marlin"
-
-#include <android/log.h>
-#include <utils/Log.h>
-
-#include <android-base/properties.h>
-
-#include "Power.h"
-
-enum subsystem_type {
-    SUBSYSTEM_WLAN,
-
-    //Don't add any lines after that line
-    SUBSYSTEM_COUNT
-};
-
-enum wlan_param_id {
-    CUMULATIVE_SLEEP_TIME_MS,
-    CUMULATIVE_TOTAL_ON_TIME_MS,
-    DEEP_SLEEP_ENTER_COUNTER,
-    LAST_DEEP_SLEEP_ENTER_TSTAMP_MS,
-
-    //Don't add any lines after that line
-    WLAN_PARAM_COUNT
-};
-
-enum wlan_state_id {
-    WLAN_STATE_ACTIVE = 0,
-    WLAN_STATE_DEEP_SLEEP,
-
-    //Don't add any lines after that line
-    WLAN_STATE_COUNT
-};
-
-namespace android {
-namespace hardware {
-namespace power {
-namespace V1_1 {
-namespace implementation {
-
-using ::android::hardware::power::V1_0::Feature;
-using ::android::hardware::power::V1_0::PowerHint;
-using ::android::hardware::power::V1_0::PowerStatePlatformSleepState;
-using ::android::hardware::power::V1_0::Status;
-using ::android::hardware::power::V1_1::PowerStateSubsystem;
-using ::android::hardware::hidl_vec;
-using ::android::hardware::Return;
-using ::android::hardware::Void;
-
-Power::Power() {
-}
-
-// Methods from ::android::hardware::power::V1_0::IPower follow.
-Return<void> Power::setInteractive(bool __attribute__((__unused__)) interactive)  {
-    return Void();
-}
-
-Return<void> Power::powerHint(PowerHint __attribute__((__unused__)) hint, int32_t __attribute__((__unused__)) data) {
-    return Void();
-}
-
-Return<void> Power::setFeature(Feature /*feature*/, bool /*activate*/)  {
-    return Void();
-}
-
-Return<void> Power::getPlatformLowPowerStats(getPlatformLowPowerStats_cb _hidl_cb) {
-
-    hidl_vec<PowerStatePlatformSleepState> states;
-
-    states.resize(0);
-    _hidl_cb(states, Status::SUCCESS);
-    return Void();
-}
-
-static int get_wlan_low_power_stats(struct PowerStateSubsystem &subsystem) {
-
-    struct PowerStateSubsystemSleepState *state;
-
-    subsystem.name = "wlan";
-    subsystem.states.resize(WLAN_STATE_COUNT);
-
-    /* Update statistics for Active State */
-    state = &subsystem.states[WLAN_STATE_ACTIVE];
-    state->name = "Active";
-    state->residencyInMsecSinceBoot = 1000;
-    state->totalTransitions = 1;
-    state->lastEntryTimestampMs = 0;
-    state->supportedOnlyInSuspend = false;
-
-    /* Update statistics for Deep-Sleep state */
-    state = &subsystem.states[WLAN_STATE_DEEP_SLEEP];
-    state->name = "Deep-Sleep";
-    state->residencyInMsecSinceBoot = 0;
-    state->totalTransitions = 0;
-    state->lastEntryTimestampMs = 0;
-    state->supportedOnlyInSuspend = false;
-
-    return 0;
-}
-
-Return<void> Power::getSubsystemLowPowerStats(getSubsystemLowPowerStats_cb _hidl_cb) {
-
-    hidl_vec<PowerStateSubsystem> subsystems;
-    int ret;
-
-    subsystems.resize(subsystem_type::SUBSYSTEM_COUNT);
-
-    //We currently have only one Subsystem for WLAN
-    ret = get_wlan_low_power_stats(subsystems[subsystem_type::SUBSYSTEM_WLAN]);
-    if (ret != 0) {
-        goto done;
-    }
-
-    //Add query for other subsystems here
-
-done:
-    _hidl_cb(subsystems, Status::SUCCESS);
-    return Void();
-}
-
-Return<void> Power::powerHintAsync(PowerHint hint, int32_t data) {
-    // just call the normal power hint in this oneway function
-    return powerHint(hint, data);
-}
-
-}  // namespace implementation
-}  // namespace V1_1
-}  // namespace power
-}  // namespace hardware
-}  // namespace android
diff --git a/power/Power.h b/power/Power.h
deleted file mode 100644
index 2676b7d..0000000
--- a/power/Power.h
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Copyright (C) 2017 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.
- */
-
-#ifndef ANDROID_HARDWARE_POWER_V1_1_POWER_H
-#define ANDROID_HARDWARE_POWER_V1_1_POWER_H
-
-#include <android/hardware/power/1.1/IPower.h>
-#include <hidl/MQDescriptor.h>
-#include <hidl/Status.h>
-#include <hardware/power.h>
-
-namespace android {
-namespace hardware {
-namespace power {
-namespace V1_1 {
-namespace implementation {
-
-using ::android::hardware::power::V1_0::Feature;
-using ::android::hardware::power::V1_0::PowerHint;
-using ::android::hardware::power::V1_1::IPower;
-using ::android::hardware::Return;
-using ::android::hardware::Void;
-
-struct Power : public IPower {
-    // Methods from ::android::hardware::power::V1_0::IPower follow.
-
-    Power();
-
-    Return<void> setInteractive(bool interactive) override;
-    Return<void> powerHint(PowerHint hint, int32_t data) override;
-    Return<void> setFeature(Feature feature, bool activate) override;
-    Return<void> getPlatformLowPowerStats(getPlatformLowPowerStats_cb _hidl_cb) override;
-
-    // Methods from ::android::hardware::power::V1_1::IPower follow.
-    Return<void> getSubsystemLowPowerStats(getSubsystemLowPowerStats_cb _hidl_cb) override;
-    Return<void> powerHintAsync(PowerHint hint, int32_t data) override;
-
-    // Methods from ::android::hidl::base::V1_0::IBase follow.
-
-};
-
-}  // namespace implementation
-}  // namespace V1_1
-}  // namespace power
-}  // namespace hardware
-}  // namespace android
-
-#endif  // ANDROID_HARDWARE_POWER_V1_1_POWER_H
diff --git a/power/android.hardware.power@1.1-service.ranchu.rc b/power/android.hardware.power@1.1-service.ranchu.rc
deleted file mode 100644
index f5a05d6..0000000
--- a/power/android.hardware.power@1.1-service.ranchu.rc
+++ /dev/null
@@ -1,4 +0,0 @@
-service vendor.power-hal-1-1 /vendor/bin/hw/android.hardware.power@1.1-service.ranchu
-    class hal
-    user system
-    group system
diff --git a/power/android.hardware.power@1.3-service.ranchu.rc b/power/android.hardware.power@1.3-service.ranchu.rc
new file mode 100644
index 0000000..2e9b546
--- /dev/null
+++ b/power/android.hardware.power@1.3-service.ranchu.rc
@@ -0,0 +1,4 @@
+service vendor.power-hal-1-3 /vendor/bin/hw/android.hardware.power@1.3-service.ranchu
+    class hal
+    user system
+    group system
diff --git a/power/android.hardware.power@1.3-service.ranchu.xml b/power/android.hardware.power@1.3-service.ranchu.xml
new file mode 100644
index 0000000..e659341
--- /dev/null
+++ b/power/android.hardware.power@1.3-service.ranchu.xml
@@ -0,0 +1,11 @@
+<manifest version="1.0" type="device" target-level="3">
+    <hal format="hidl">
+        <name>android.hardware.power</name>
+        <transport>hwbinder</transport>
+        <version>1.3</version>
+        <interface>
+            <name>IPower</name>
+            <instance>default</instance>
+        </interface>
+    </hal>
+</manifest>
diff --git a/power/main.cpp b/power/main.cpp
new file mode 100644
index 0000000..c90ecbc
--- /dev/null
+++ b/power/main.cpp
@@ -0,0 +1,151 @@
+/*
+ * Copyright (C) 2017 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 "android.hardware.power@1.3-service.ranchu"
+#include <android/log.h>
+#include <android/hardware/power/1.3/IPower.h>
+#include <hidl/LegacySupport.h>
+
+namespace {
+namespace ahp = ::android::hardware::power;
+typedef ahp::V1_0::Status Status0;
+typedef ahp::V1_0::PowerStatePlatformSleepState PowerStatePlatformSleepState0;
+typedef ahp::V1_1::PowerStateSubsystem PowerStateSubsystem1;
+typedef ahp::V1_1::PowerStateSubsystemSleepState PowerStateSubsystemSleepState1;
+
+using ::android::hardware::hidl_vec;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+
+enum class SubsystemType {
+    Wlan,
+    //Don't add any lines after that line
+    Count
+};
+enum class WlanParamId {
+    CumulativeSleepTimeMs,
+    CumulativeTotalTimeOnMs,
+    DeepSleepEnterCounter,
+    LastDeepSleepEnterTstampMs,
+    //Don't add any lines after that line
+    ParamCount
+};
+enum WlanStateId {
+    Active = 0,
+    DeepSleep,
+    //Don't add any lines after that line
+    Count
+};
+
+struct Power3 : public ahp::V1_3::IPower {
+    Power3() {
+    }
+
+    // v1.3
+    Return<void> powerHintAsync_1_3(ahp::V1_3::PowerHint hint, int32_t data) override {
+        (void)hint;
+        (void)data;
+        return {};
+    }
+
+    // v1.2
+    Return<void> powerHintAsync_1_2(ahp::V1_2::PowerHint hint, int32_t data) override {
+        (void)hint;
+        (void)data;
+        return {};
+    }
+
+    // v1.1
+    Return<void> getSubsystemLowPowerStats(getSubsystemLowPowerStats_cb _hidl_cb) override {
+        hidl_vec<PowerStateSubsystem1> subsystems;
+        subsystems.resize(static_cast<size_t>(SubsystemType::Count));
+
+        getWlanLowPowerStats(&subsystems[static_cast<size_t>(SubsystemType::Wlan)]);
+
+        _hidl_cb(subsystems, Status0::SUCCESS);
+        return {};
+    }
+
+    Return<void> powerHintAsync(ahp::V1_0::PowerHint hint, int32_t data) override {
+        return powerHint(hint, data);
+    }
+
+    // v1.0
+    Return<void> setInteractive(bool interactive) override {
+        (void)interactive;
+        return {};
+    }
+
+    Return<void> powerHint(ahp::V1_0::PowerHint hint, int32_t data) override {
+        (void)hint;
+        (void)data;
+        return {};
+    }
+
+    Return<void> setFeature(ahp::V1_0::Feature feature, bool activate) override {
+        (void)feature;
+        (void)activate;
+        return {};
+    }
+
+    Return<void> getPlatformLowPowerStats(getPlatformLowPowerStats_cb _hidl_cb) override {
+        hidl_vec<PowerStatePlatformSleepState0> states;
+        states.resize(0);
+        _hidl_cb(states, Status0::SUCCESS);
+        return {};
+    }
+
+    static void getWlanLowPowerStats(PowerStateSubsystem1* subsystem) {
+        subsystem->name = "wlan";
+        subsystem->states.resize(static_cast<size_t>(WlanStateId::Count));
+
+        PowerStateSubsystemSleepState1* state;
+
+        /* Update statistics for Active State */
+        state = &subsystem->states[static_cast<size_t>(WlanStateId::Active)];
+        state->name = "Active";
+        state->residencyInMsecSinceBoot = 1000;
+        state->totalTransitions = 1;
+        state->lastEntryTimestampMs = 0;
+        state->supportedOnlyInSuspend = false;
+
+        /* Update statistics for Deep-Sleep state */
+        state = &subsystem->states[static_cast<size_t>(WlanStateId::DeepSleep)];
+        state->name = "Deep-Sleep";
+        state->residencyInMsecSinceBoot = 0;
+        state->totalTransitions = 0;
+        state->lastEntryTimestampMs = 0;
+        state->supportedOnlyInSuspend = false;
+    }
+};
+}  // namespace
+
+int main(int, char**) {
+    using ::android::sp;
+    ::android::hardware::configureRpcThreadpool(1, true /* callerWillJoin */);
+
+    sp<ahp::V1_3::IPower> power(new Power3());
+    if (power->registerAsService() != ::android::NO_ERROR) {
+        ALOGE("failed to register the IPower@1.3 service");
+        return -EINVAL;
+    }
+
+    ALOGI("IPower@1.3 service is initialized");
+
+    ::android::hardware::joinRpcThreadpool();
+    ALOGI("IPower@1.3 service is terminating");
+    return 0;
+}
diff --git a/power/service.cpp b/power/service.cpp
deleted file mode 100644
index b0fb829..0000000
--- a/power/service.cpp
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Copyright (C) 2017 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 "android.hardware.power@1.1-service.ranchu"
-
-#include <android/log.h>
-#include <hidl/HidlTransportSupport.h>
-#include <hardware/power.h>
-#include "Power.h"
-
-using android::sp;
-using android::status_t;
-using android::OK;
-
-// libhwbinder:
-using android::hardware::configureRpcThreadpool;
-using android::hardware::joinRpcThreadpool;
-
-// Generated HIDL files
-using android::hardware::power::V1_1::IPower;
-using android::hardware::power::V1_1::implementation::Power;
-
-int main() {
-
-    status_t status;
-    android::sp<IPower> service = nullptr;
-
-    ALOGI("Power HAL Service 1.1 for Ranchu is starting.");
-
-    service = new Power();
-    if (service == nullptr) {
-        ALOGE("Can not create an instance of Power HAL Iface, exiting.");
-
-        goto shutdown;
-    }
-
-    configureRpcThreadpool(1, true /*callerWillJoin*/);
-
-    status = service->registerAsService();
-    if (status != OK) {
-        ALOGE("Could not register service for Power HAL Iface (%d).", status);
-        goto shutdown;
-    }
-
-    ALOGI("Power Service is ready");
-    joinRpcThreadpool();
-    //Should not pass this line
-
-shutdown:
-    // In normal operation, we don't expect the thread pool to exit
-
-    ALOGE("Power Service is shutting down");
-    return 1;
-}
diff --git a/sepolicy/common/file_contexts b/sepolicy/common/file_contexts
index de27621..fd3bf31 100644
--- a/sepolicy/common/file_contexts
+++ b/sepolicy/common/file_contexts
@@ -38,7 +38,7 @@
 /vendor/bin/hw/android\.hardware\.drm@[0-9]+\.[0-9]+-service\.clearkey          u:object_r:hal_drm_clearkey_exec:s0
 /vendor/bin/hw/android\.hardware\.gatekeeper@1\.0-service.software    u:object_r:hal_gatekeeper_default_exec:s0
 /vendor/bin/hw/android\.hardware\.keymaster@4\.0-strongbox-service u:object_r:hal_keymaster_default_exec:s0
-/vendor/bin/hw/android\.hardware\.power@1\.1-service.ranchu           u:object_r:hal_power_default_exec:s0
+/vendor/bin/hw/android\.hardware\.power@1\.3-service.ranchu           u:object_r:hal_power_default_exec:s0
 /vendor/bin/hw/android\.hardware\.thermal@2\.0-service.mock           u:object_r:hal_thermal_default_exec:s0
 /vendor/bin/hw/android\.hardware\.camera\.provider@2\.6-service-google u:object_r:hal_camera_default_exec:s0
 /vendor/bin/hw/android\.hardware\.authsecret@1\.0-service  u:object_r:hal_authsecret_default_exec:s0
diff --git a/vendor.mk b/vendor.mk
index 0ee1771..c8a46f1 100644
--- a/vendor.mk
+++ b/vendor.mk
@@ -59,9 +59,7 @@
     fingerprint.goldfish \
     audio.primary.goldfish \
     audio.primary.goldfish_legacy \
-    power.goldfish \
     stagefright \
-    power.ranchu \
     fingerprint.ranchu \
     android.hardware.graphics.composer@2.1-impl \
     android.hardware.graphics.composer@2.1-service \
@@ -132,7 +130,7 @@
     android.hardware.drm@1.3-service.widevine
 
 PRODUCT_PACKAGES += \
-    android.hardware.power@1.1-service.ranchu \
+    android.hardware.power@1.3-service.ranchu \
 
 PRODUCT_PROPERTY_OVERRIDES += ro.control_privapp_permissions=enforce
 PRODUCT_PROPERTY_OVERRIDES += ro.hardware.power=ranchu