pixel: Implement PowerShare HAL

Change-Id: I3c0321b844acb5f97ac398acc842501410a44b6c
diff --git a/powershare/Android.bp b/powershare/Android.bp
new file mode 100644
index 0000000..21c1dd1
--- /dev/null
+++ b/powershare/Android.bp
@@ -0,0 +1,33 @@
+// Copyright (C) 2021 The LineageOS 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.
+
+cc_binary {
+    name: "vendor.lineage.powershare@1.0-service.pixel",
+    vintf_fragments: ["vendor.lineage.powershare@1.0-service.pixel.xml"],
+    init_rc: ["vendor.lineage.powershare@1.0-service.pixel.rc"],
+    relative_install_path: "hw",
+
+    vendor: true,
+    srcs: [
+        "PowerShare.cpp",
+        "service.cpp",
+    ],
+    shared_libs: [
+        "libbase",
+        "libbinder",
+        "libhidlbase",
+        "libutils",
+        "vendor.lineage.powershare@1.0",
+    ],
+}
diff --git a/powershare/PowerShare.cpp b/powershare/PowerShare.cpp
new file mode 100644
index 0000000..4aa56ee
--- /dev/null
+++ b/powershare/PowerShare.cpp
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2021 The LineageOS 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.
+ */
+
+#include "PowerShare.h"
+#include <fstream>
+
+#define WLC_DEV_DIR "/sys/class/power_supply/wireless/device"
+#define RTX_ENABLE_PATH WLC_DEV_DIR "/rtx"
+
+/*
+ * Write value to path and close file.
+ */
+template <typename T>
+static void set(const std::string& path, const T& value) {
+    std::ofstream file(path);
+    file << value;
+}
+
+template <typename T>
+static T get(const std::string& path, const T& def) {
+    std::ifstream file(path);
+    T result;
+
+    file >> result;
+    return file.fail() ? def : result;
+}
+
+namespace vendor::lineage::powershare::pixel {
+
+Return<bool> PowerShare::isEnabled() {
+    return get(RTX_ENABLE_PATH, 0) == 1;
+}
+
+Return<bool> PowerShare::setEnabled(bool enable) {
+    set(RTX_ENABLE_PATH, enable ? 1 : 0);
+    return isEnabled();
+}
+
+Return<uint32_t> PowerShare::getMinBattery() {
+    return 0;
+}
+
+Return<uint32_t> PowerShare::setMinBattery(uint32_t) {
+    return 0;
+}
+
+}  // namespace vendor::lineage::powershare::pixel
diff --git a/powershare/PowerShare.h b/powershare/PowerShare.h
new file mode 100644
index 0000000..e7bc4ec
--- /dev/null
+++ b/powershare/PowerShare.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2021 The LineageOS 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 <vendor/lineage/powershare/1.0/IPowerShare.h>
+#include <hidl/MQDescriptor.h>
+#include <hidl/Status.h>
+
+namespace vendor::lineage::powershare::pixel {
+
+using ::android::hardware::hidl_array;
+using ::android::hardware::hidl_memory;
+using ::android::hardware::hidl_string;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::Return;
+using ::android::hardware::Void;
+using ::android::sp;
+
+struct PowerShare : public V1_0::IPowerShare {
+    // Methods from ::vendor::lineage::powershare::V1_0::IPowerShare follow.
+    Return<bool> isEnabled() override;
+    Return<bool> setEnabled(bool enable) override;
+    Return<uint32_t> getMinBattery() override;
+    Return<uint32_t> setMinBattery(uint32_t minBattery) override;
+};
+
+}  // namespace vendor::lineage::powershare::pixel
diff --git a/powershare/device.mk b/powershare/device.mk
new file mode 100644
index 0000000..527e752
--- /dev/null
+++ b/powershare/device.mk
@@ -0,0 +1,5 @@
+# PowerShare
+PRODUCT_PACKAGES += \
+    vendor.lineage.powershare@1.0-service.pixel
+
+BOARD_SEPOLICY_DIRS += hardware/google/pixel-sepolicy/powershare
diff --git a/powershare/service.cpp b/powershare/service.cpp
new file mode 100644
index 0000000..b5862e8
--- /dev/null
+++ b/powershare/service.cpp
@@ -0,0 +1,61 @@
+/*
+ * Copyright (C) 2021 The LineageOS 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 "vendor.lineage.powershare@1.0-service.pixel"
+
+#include <android-base/logging.h>
+#include <binder/ProcessState.h>
+#include <hidl/HidlTransportSupport.h>
+
+#include "PowerShare.h"
+
+using android::hardware::configureRpcThreadpool;
+using android::hardware::joinRpcThreadpool;
+using android::sp;
+using android::status_t;
+using android::OK;
+
+using ::vendor::lineage::powershare::pixel::PowerShare;
+
+int main() {
+    sp<PowerShare> powerShare;
+    status_t status;
+
+    LOG(INFO) << "PowerShare HAL service is starting.";
+
+    powerShare = new PowerShare();
+    if (powerShare == nullptr) {
+        LOG(ERROR) << "Can not create an instance of PowerShare HAL, exiting.";
+        goto shutdown;
+    }
+
+    configureRpcThreadpool(1, true /*callerWillJoin*/);
+
+    status = powerShare->registerAsService();
+    if (status != OK) {
+        LOG(ERROR) << "Could not register service for PowerShare HAL (" << status << ")";
+        goto shutdown;
+    }
+
+    LOG(INFO) << "PowerShare HAL service is ready.";
+    joinRpcThreadpool();
+    // Should not pass this line
+
+shutdown:
+    // In normal operation, we don't expect the thread pool to shutdown
+    LOG(ERROR) << "PowerShare HAL service is shutting down.";
+    return 1;
+}
diff --git a/powershare/vendor.lineage.powershare@1.0-service.pixel.rc b/powershare/vendor.lineage.powershare@1.0-service.pixel.rc
new file mode 100644
index 0000000..8bf1822
--- /dev/null
+++ b/powershare/vendor.lineage.powershare@1.0-service.pixel.rc
@@ -0,0 +1,4 @@
+service vendor.powershare-hal-1-0-pixel /vendor/bin/hw/vendor.lineage.powershare@1.0-service.pixel
+    class hal
+    user system
+    group system
diff --git a/powershare/vendor.lineage.powershare@1.0-service.pixel.xml b/powershare/vendor.lineage.powershare@1.0-service.pixel.xml
new file mode 100644
index 0000000..b4af2cd
--- /dev/null
+++ b/powershare/vendor.lineage.powershare@1.0-service.pixel.xml
@@ -0,0 +1,11 @@
+<manifest version="1.0" type="device">
+    <hal format="hidl">
+        <name>vendor.lineage.powershare</name>
+        <transport>hwbinder</transport>
+        <version>1.0</version>
+        <interface>
+            <name>IPowerShare</name>
+            <instance>default</instance>
+        </interface>
+    </hal>
+</manifest>