Merge "WLC: Add the essential sysfs_wlc policy files"
diff --git a/dauntless/gsc.mk b/dauntless/gsc.mk
index 076ce4d..326fc6d 100644
--- a/dauntless/gsc.mk
+++ b/dauntless/gsc.mk
@@ -5,7 +5,6 @@
 PRODUCT_PACKAGES += \
     citadeld \
     citadel_updater \
-    android.hardware.weaver@1.0-service.citadel \
     android.hardware.weaver-service.citadel \
     android.hardware.authsecret-service.citadel \
     android.hardware.oemlock-service.citadel \
diff --git a/powerstats/CpupmStateResidencyDataProvider.cpp b/powerstats/CpupmStateResidencyDataProvider.cpp
new file mode 100644
index 0000000..bb0e61f
--- /dev/null
+++ b/powerstats/CpupmStateResidencyDataProvider.cpp
@@ -0,0 +1,146 @@
+/*
+ * Copyright (C) 2023 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.
+ */
+#include "CpupmStateResidencyDataProvider.h"
+
+#include <android-base/logging.h>
+#include <android-base/parseint.h>
+#include <android-base/strings.h>
+
+#include <string>
+#include <utility>
+
+using android::base::ParseUint;
+using android::base::Split;
+using android::base::StartsWith;
+using android::base::Trim;
+
+namespace aidl {
+namespace android {
+namespace hardware {
+namespace power {
+namespace stats {
+
+CpupmStateResidencyDataProvider::CpupmStateResidencyDataProvider(
+        const std::string &path, const Config &config)
+    : mPath(std::move(path)), mConfig(std::move(config)) {}
+
+int32_t CpupmStateResidencyDataProvider::matchState(char const *line) {
+    for (int32_t i = 0; i < mConfig.states.size(); i++) {
+        if (mConfig.states[i].second == Trim(std::string(line))) {
+            return i;
+        }
+    }
+    return -1;
+}
+
+int32_t CpupmStateResidencyDataProvider::matchEntity(char const *line) {
+    for (int32_t i = 0; i < mConfig.entities.size(); i++) {
+        if (StartsWith(Trim(std::string(line)), mConfig.entities[i].second)) {
+            return i;
+        }
+    }
+    return -1;
+}
+
+bool CpupmStateResidencyDataProvider::parseState(
+        char const *line, uint64_t *duration, uint64_t *count) {
+    std::vector<std::string> parts = Split(line, " ");
+    if (parts.size() != 5) {
+        return false;
+    }
+    if (!ParseUint(Trim(parts[1]), count)) {
+        return false;
+    }
+    if (!ParseUint(Trim(parts[3]), duration)) {
+        return false;
+    }
+    return true;
+}
+
+bool CpupmStateResidencyDataProvider::getStateResidencies(
+        std::unordered_map<std::string, std::vector<StateResidency>> *residencies) {
+    std::unique_ptr<FILE, decltype(&fclose)> fp(fopen(mPath.c_str(), "r"), fclose);
+    if (!fp) {
+        PLOG(ERROR) << __func__ << ":Failed to open file " << mPath;
+        return false;
+    }
+
+    for (int32_t i = 0; i < mConfig.entities.size(); i++) {
+        std::vector<StateResidency> stateResidencies(mConfig.states.size());
+        for (int32_t j = 0; j < stateResidencies.size(); j++) {
+            stateResidencies[j].id = j;
+        }
+        residencies->emplace(mConfig.entities[i].first, stateResidencies);
+    }
+
+    size_t len = 0;
+    char *line = nullptr;
+
+    int32_t temp, entityIndex, stateId = -1;
+    uint64_t duration, count;
+    auto it = residencies->end();
+
+    while (getline(&line, &len, fp.get()) != -1) {
+        temp = matchState(line);
+        // Assign new id only when a new valid state is encountered.
+        if (temp >= 0) {
+            stateId = temp;
+        }
+
+        if (stateId >= 0) {
+            entityIndex = matchEntity(line);
+            it = residencies->find(mConfig.entities[entityIndex].first);
+
+            if (it != residencies->end()) {
+                if (parseState(line, &duration, &count)) {
+                    it->second[stateId].totalTimeInStateMs = duration / US_TO_MS;
+                    it->second[stateId].totalStateEntryCount = count;
+                } else {
+                    LOG(ERROR) << "Failed to parse duration and count from [" << std::string(line)
+                               << "]";
+                    return false;
+                }
+            }
+        }
+    }
+
+    free(line);
+
+    return true;
+}
+
+std::unordered_map<std::string, std::vector<State>> CpupmStateResidencyDataProvider::getInfo() {
+    std::unordered_map<std::string, std::vector<State>> info;
+    for (auto const &entity : mConfig.entities) {
+        std::vector<State> stateInfo(mConfig.states.size());
+        int32_t stateId = 0;
+        for (auto const &state : mConfig.states) {
+            stateInfo[stateId] = State{
+                .id = stateId,
+                .name = state.first
+            };
+            stateId++;
+        }
+        info.emplace(entity.first, stateInfo);
+    }
+    return info;
+}
+
+}  // namespace stats
+}  // namespace power
+}  // namespace hardware
+}  // namespace android
+}  // namespace aidl
diff --git a/powerstats/OWNERS b/powerstats/OWNERS
new file mode 100644
index 0000000..e0d66d7
--- /dev/null
+++ b/powerstats/OWNERS
@@ -0,0 +1,4 @@
+darrenhsu@google.com
+joaodias@google.com
+krossmo@google.com
+vincentwang@google.com
diff --git a/powerstats/include/CpupmStateResidencyDataProvider.h b/powerstats/include/CpupmStateResidencyDataProvider.h
new file mode 100644
index 0000000..c04e11e
--- /dev/null
+++ b/powerstats/include/CpupmStateResidencyDataProvider.h
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2023 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 <PowerStatsAidl.h>
+
+namespace aidl {
+namespace android {
+namespace hardware {
+namespace power {
+namespace stats {
+
+class CpupmStateResidencyDataProvider : public PowerStats::IStateResidencyDataProvider {
+  public:
+    class Config {
+      public:
+        // List of power entity name pairs (name to display, name to parse)
+        std::vector<std::pair<std::string, std::string>> entities;
+
+        // List of state pairs (state to display, state to parse).
+        std::vector<std::pair<std::string, std::string>> states;
+    };
+
+    /*
+     * path - path to cpupm sysfs node.
+     */
+    CpupmStateResidencyDataProvider(const std::string &path, const Config &config);
+    ~CpupmStateResidencyDataProvider() = default;
+
+    /*
+     * See IStateResidencyDataProvider::getStateResidencies
+     */
+    bool getStateResidencies(
+        std::unordered_map<std::string, std::vector<StateResidency>> *residencies) override;
+
+    /*
+     * See IStateResidencyDataProvider::getInfo
+     */
+    std::unordered_map<std::string, std::vector<State>> getInfo() override;
+
+  private:
+    int32_t matchEntity(char const *line);
+    int32_t matchState(char const *line);
+    bool parseState(char const *line, uint64_t *duration, uint64_t *count);
+
+    // A constant to represent the number of microseconds in one millisecond.
+    const uint64_t US_TO_MS = 1000;
+
+    const std::string mPath;
+    const Config mConfig;
+};
+
+}  // namespace stats
+}  // namespace power
+}  // namespace hardware
+}  // namespace android
+}  // namespace aidl
diff --git a/touch/gti/Android.bp b/touch/gti/Android.bp
index b997aec..d21314a 100644
--- a/touch/gti/Android.bp
+++ b/touch/gti/Android.bp
@@ -5,6 +5,7 @@
 sh_binary {
     name: "dump_gti.sh",
     src: "dump_gti.sh",
+    init_rc: ["init.touch.gti.rc"],
     vendor: true,
     sub_dir: "dump",
 }
diff --git a/touch/gti/dump_gti.sh b/touch/gti/dump_gti.sh
index 573962d..a7d3a61 100644
--- a/touch/gti/dump_gti.sh
+++ b/touch/gti/dump_gti.sh
@@ -1,5 +1,12 @@
 #!/vendor/bin/sh
 path="/sys/devices/virtual/goog_touch_interface/gti.0"
+procfs_path="/proc/goog_touch_interface/gti.0"
+
+if [[ -d "$procfs_path" ]]; then
+heatmap_path=$procfs_path
+else
+heatmap_path=$path
+fi
 
 echo "------ Force Touch Active ------"
 echo 1 > $path/force_active
@@ -8,22 +15,22 @@
 cat $path/fw_ver
 
 echo "------ Get Mutual Sensing Data - Baseline ------"
-cat $path/ms_base
+cat $heatmap_path/ms_base
 
 echo "------ Get Mutual Sensing Data - Delta ------"
-cat $path/ms_diff
+cat $heatmap_path/ms_diff
 
 echo "------ Get Mutual Sensing Data - Raw ------"
-cat $path/ms_raw
+cat $heatmap_path/ms_raw
 
 echo "------ Get Self Sensing Data - Baseline ------"
-cat $path/ss_base
+cat $heatmap_path/ss_base
 
 echo "------ Get Self Sensing Data - Delta ------"
-cat $path/ss_diff
+cat $heatmap_path/ss_diff
 
 echo "------ Get Self Sensing Data - Raw ------"
-cat $path/ss_raw
+cat $heatmap_path/ss_raw
 
 echo "------ Self Test ------"
 cat $path/self_test
diff --git a/touch/gti/init.touch.gti.rc b/touch/gti/init.touch.gti.rc
new file mode 100644
index 0000000..d714a97
--- /dev/null
+++ b/touch/gti/init.touch.gti.rc
@@ -0,0 +1,9 @@
+on property:vendor.device.modules.ready=1
+    chown system system /proc/goog_touch_interface
+    chown system system /proc/goog_touch_interface/gti.0
+    chown system system /proc/goog_touch_interface/gti.0/ms_base
+    chown system system /proc/goog_touch_interface/gti.0/ms_diff
+    chown system system /proc/goog_touch_interface/gti.0/ms_raw
+    chown system system /proc/goog_touch_interface/gti.0/ss_base
+    chown system system /proc/goog_touch_interface/gti.0/ss_diff
+    chown system system /proc/goog_touch_interface/gti.0/ss_raw
diff --git a/touch/gti/sepolicy/dump_gti.te b/touch/gti/sepolicy/dump_gti.te
index 7482246..060fc29 100644
--- a/touch/gti/sepolicy/dump_gti.te
+++ b/touch/gti/sepolicy/dump_gti.te
@@ -1,5 +1,7 @@
 pixel_bugreport(dump_gti)
 
+allow dump_gti proc_touch_gti:dir r_dir_perms;
+allow dump_gti proc_touch_gti:file rw_file_perms;
 allow dump_gti sysfs_touch_gti:dir r_dir_perms;
 allow dump_gti sysfs_touch_gti:file rw_file_perms;
 allow dump_gti vendor_toolbox_exec:file execute_no_trans;
diff --git a/touch/gti/sepolicy/file.te b/touch/gti/sepolicy/file.te
index 9776707..c3900f0 100644
--- a/touch/gti/sepolicy/file.te
+++ b/touch/gti/sepolicy/file.te
@@ -1,2 +1,3 @@
+type proc_touch_gti, proc_type, fs_type;
 type sysfs_touch_gti, sysfs_type, fs_type;
 
diff --git a/touch/gti/sepolicy/genfs_contexts b/touch/gti/sepolicy/genfs_contexts
index a64e39b..45d3b53 100644
--- a/touch/gti/sepolicy/genfs_contexts
+++ b/touch/gti/sepolicy/genfs_contexts
@@ -1,2 +1,4 @@
 # Touch
 genfscon sysfs /devices/virtual/goog_touch_interface                            u:object_r:sysfs_touch_gti:s0
+genfscon proc  /goog_touch_interface                                            u:object_r:proc_touch_gti:s0
+