RM6785: Make nfc and power_profile detection treble compliant

Change-Id: I9db6244cf3396eee8c13a5a58941c35a33fca412
diff --git a/BoardConfig.mk b/BoardConfig.mk
index e0a2739..2536af9 100644
--- a/BoardConfig.mk
+++ b/BoardConfig.mk
@@ -124,6 +124,7 @@
 # SELinux
 include device/mediatek/sepolicy/sepolicy.mk
 SYSTEM_EXT_PRIVATE_SEPOLICY_DIRS += $(DEVICE_PATH)/sepolicy/private
+SYSTEM_EXT_PUBLIC_SEPOLICY_DIRS += $(DEVICE_PATH)/sepolicy/public
 BOARD_VENDOR_SEPOLICY_DIRS += $(DEVICE_PATH)/sepolicy/vendor
 
 # SPL
diff --git a/device.mk b/device.mk
index 531b548..f9426ea 100644
--- a/device.mk
+++ b/device.mk
@@ -121,6 +121,9 @@
     init.RM6785.rc \
     ueventd.mtk.rc
 
+PRODUCT_PACKAGES += \
+    init_RM6785_vendor
+
 # Kernel
 PRODUCT_SET_DEBUGFS_RESTRICTIONS := true
 
diff --git a/init/Android.bp b/init/Android.bp
index c6a2829..8d6f823 100644
--- a/init/Android.bp
+++ b/init/Android.bp
@@ -14,3 +14,11 @@
         "system/core/init"
     ]
 }
+
+cc_binary {
+    name: "init_RM6785_vendor",
+    init_rc: ["init_RM6785_vendor.rc"],
+    srcs: ["init_RM6785_vendor.cpp"],
+    shared_libs: ["libbase"],
+    vendor: true,
+}
diff --git a/init/init_RM6785.cpp b/init/init_RM6785.cpp
index f2f568c..b691fed 100644
--- a/init/init_RM6785.cpp
+++ b/init/init_RM6785.cpp
@@ -13,7 +13,6 @@
 
 #include "vendor_init.h"
 
-#define PROC_NFC "/proc/oppo_nfc/chipset"
 #define PROC_OPERATOR "/proc/oppoVersion/operatorName"
 
 void property_override(std::string prop, std::string value, bool add = true) {
@@ -53,18 +52,6 @@
     }
 }
 
-bool nfc_variant() {
-    std::ifstream nfc_file(PROC_NFC);
-    nfc_file.good();
-    std::string chipset;
-
-    getline(nfc_file, chipset);
-
-    LOG(INFO) << "oppo_nfc : chipset " << chipset;
-
-    return (chipset != "NULL");
-}
-
 std::tuple<std::string, std::string> get_device() {
     std::string device, model, line;
     std::ifstream operator_file(PROC_OPERATOR);
@@ -144,18 +131,5 @@
     set_ro_build_prop("model", model);
     set_ro_build_prop("name", model);
     set_ro_build_prop("product", model, false);
-
-    // for RRO
-    if (!device.find("RMX215")) {
-        property_override("ro.device", "RMX2151");
-    }
-    if (!device.find("RMX216")) {
-        property_override("ro.device", "RMX2161");
-    }
-
-    // NFC check
-    if (nfc_variant()) {
-        property_override("ro.boot.product.hardware.sku", "nfc");
-    }
 #endif
 }
diff --git a/init/init_RM6785_vendor.cpp b/init/init_RM6785_vendor.cpp
new file mode 100644
index 0000000..cc56ccb
--- /dev/null
+++ b/init/init_RM6785_vendor.cpp
@@ -0,0 +1,57 @@
+//
+// Copyright (C) 2022 The LineageOS Project
+//
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <android-base/logging.h>
+#include <android-base/properties.h>
+#include <fstream>
+
+#define PROC_NFC "/proc/oppo_nfc/chipset"
+#define PROC_OPERATOR "/proc/oppoVersion/operatorName"
+
+void set_property(std::string prop, std::string value) {
+    LOG(INFO) << "Setting property: " << prop << " to " << value;
+    if (!android::base::SetProperty(prop.c_str(), value.c_str()))
+        LOG(ERROR) << "Unable to set: " << prop << " to " << value;
+}
+
+void nfc_detect() {
+    std::string chipset;
+    std::ifstream nfc_file(PROC_NFC);
+
+    getline(nfc_file, chipset);
+
+    LOG(INFO) << "oppo_nfc : chipset " << chipset;
+
+    if (chipset != "NULL")
+        set_property("ro.vendor.nfc_device", "1");
+}
+
+void power_profile() {
+    std::string op;
+    std::ifstream operator_file(PROC_OPERATOR);
+
+    getline(operator_file, op);
+
+    int operatorName = stoi(op);
+
+    switch (operatorName) {
+        case 94: case 140: case 141: case 142: case 146: case 148: case 149:
+            LOG(INFO) << "operatorName: " << operatorName;
+            set_property("ro.vendor.power_profile.device", "RMX2151");
+            break;
+        case 90: case 92: case 143: case 145: case 147:
+            LOG(INFO) << "operatorName: " << operatorName;
+            set_property("ro.vendor.power_profile.device", "RMX2161");
+            break;
+        default:
+            LOG(INFO) << "operatorName: " << operatorName;
+    }
+}
+
+int main() {
+    nfc_detect();
+    power_profile();
+}
diff --git a/init/init_RM6785_vendor.rc b/init/init_RM6785_vendor.rc
new file mode 100644
index 0000000..2b2a2e4
--- /dev/null
+++ b/init/init_RM6785_vendor.rc
@@ -0,0 +1,2 @@
+on init
+    exec - root root -- /vendor/bin/init_RM6785_vendor
diff --git a/rootdir/etc/init.RM6785.rc b/rootdir/etc/init.RM6785.rc
index 838ea63..0889c24 100644
--- a/rootdir/etc/init.RM6785.rc
+++ b/rootdir/etc/init.RM6785.rc
@@ -45,6 +45,9 @@
     ioprio rt 4
     writepid /dev/cpuset/foreground/tasks
 
+on property:ro.vendor.nfc_device=1
+    setprop ro.boot.product.hardware.sku nfc
+
 on property:ro.boot.product.hardware.sku=nfc
     start vendor.nfc_hal_service
 
diff --git a/rro_overlays/FrameworksOverlayRMX2151/AndroidManifest.xml b/rro_overlays/FrameworksOverlayRMX2151/AndroidManifest.xml
index 1fc8685..c69b482 100644
--- a/rro_overlays/FrameworksOverlayRMX2151/AndroidManifest.xml
+++ b/rro_overlays/FrameworksOverlayRMX2151/AndroidManifest.xml
@@ -4,7 +4,7 @@
     <overlay
         android:isStatic="true"
         android:priority="1"
-        android:requiredSystemPropertyName="ro.device"
+        android:requiredSystemPropertyName="ro.vendor.power_profile.device"
         android:requiredSystemPropertyValue="RMX2151"
         android:targetPackage="android" />
 </manifest>
diff --git a/rro_overlays/FrameworksOverlayRMX2161/AndroidManifest.xml b/rro_overlays/FrameworksOverlayRMX2161/AndroidManifest.xml
index 79e71e8..92b0fce 100644
--- a/rro_overlays/FrameworksOverlayRMX2161/AndroidManifest.xml
+++ b/rro_overlays/FrameworksOverlayRMX2161/AndroidManifest.xml
@@ -4,7 +4,7 @@
     <overlay
         android:isStatic="true"
         android:priority="1"
-        android:requiredSystemPropertyName="ro.device"
+        android:requiredSystemPropertyName="ro.vendor.power_profile.device"
         android:requiredSystemPropertyValue="RMX2161"
         android:targetPackage="android" />
 </manifest>
diff --git a/sepolicy/private/init.te b/sepolicy/private/init.te
new file mode 100644
index 0000000..f825e98
--- /dev/null
+++ b/sepolicy/private/init.te
@@ -0,0 +1,2 @@
+# libinit
+allow init proc_oppo_operator:file r_file_perms;
diff --git a/sepolicy/public/file.te b/sepolicy/public/file.te
new file mode 100644
index 0000000..46ba0bf
--- /dev/null
+++ b/sepolicy/public/file.te
@@ -0,0 +1 @@
+type proc_oppo_operator, fs_type, proc_type;
diff --git a/sepolicy/public/genfs_contexts b/sepolicy/public/genfs_contexts
new file mode 100644
index 0000000..8cfb02f
--- /dev/null
+++ b/sepolicy/public/genfs_contexts
@@ -0,0 +1,2 @@
+# Proc
+genfscon proc /oppoVersion/operatorName                                   u:object_r:proc_oppo_operator:s0
diff --git a/sepolicy/vendor/file.te b/sepolicy/vendor/file.te
index ae3641d..712145d 100644
--- a/sepolicy/vendor/file.te
+++ b/sepolicy/vendor/file.te
@@ -1,5 +1,6 @@
 type proc_fastchg_fw, fs_type, proc_type;
 type proc_fingerprint, fs_type, proc_type;
+type proc_nfc_chipset, fs_type, proc_type;
 type proc_sensor_feature, fs_type, proc_type;
 type proc_tp, fs_type, proc_type;
 type sysfs_fingerprint, fs_type, sysfs_type;
diff --git a/sepolicy/vendor/file_contexts b/sepolicy/vendor/file_contexts
index 3eda654..c381d06 100644
--- a/sepolicy/vendor/file_contexts
+++ b/sepolicy/vendor/file_contexts
@@ -19,3 +19,6 @@
 
 # Goodix Fingerprint data
 /data/vendor/fingerprint/gf_data(/.*)?     u:object_r:fingerprint_vendor_data_file:s0
+
+# Init services
+/(vendor|system/vendor)/bin/init_RM6785_vendor   u:object_r:init_RM6785_exec:s0
diff --git a/sepolicy/vendor/genfs_contexts b/sepolicy/vendor/genfs_contexts
index 44248f3..e7b82d1 100644
--- a/sepolicy/vendor/genfs_contexts
+++ b/sepolicy/vendor/genfs_contexts
@@ -2,6 +2,7 @@
 genfscon proc /fastchg_fw_update                                          u:object_r:proc_fastchg_fw:s0
 genfscon proc /fp_id                                                      u:object_r:proc_fingerprint:s0
 genfscon proc /oplusSensorFeature/is_support_mtk_cali                     u:object_r:proc_sensor_feature:s0
+genfscon proc /oppo_nfc/chipset                                           u:object_r:proc_nfc_chipset:s0
 genfscon proc /touchpanel                                                 u:object_r:proc_tp:s0
 
 # Sysfs
diff --git a/sepolicy/vendor/init_RM6785.te b/sepolicy/vendor/init_RM6785.te
new file mode 100644
index 0000000..ce37c1d
--- /dev/null
+++ b/sepolicy/vendor/init_RM6785.te
@@ -0,0 +1,10 @@
+type init_RM6785, domain;
+type init_RM6785_exec, vendor_file_type, exec_type, file_type;
+
+init_daemon_domain(init_RM6785)
+
+set_prop(init_RM6785, vendor_nfc_prop)
+set_prop(init_RM6785, vendor_power_profile_prop)
+
+allow init_RM6785 proc_nfc_chipset:file r_file_perms;
+allow init_RM6785 proc_oppo_operator:file r_file_perms;
diff --git a/sepolicy/vendor/property.te b/sepolicy/vendor/property.te
index 43f9191..cfd6cf7 100644
--- a/sepolicy/vendor/property.te
+++ b/sepolicy/vendor/property.te
@@ -1,2 +1,4 @@
 vendor_internal_prop(vendor_audio_tunning_prop)
 vendor_internal_prop(vendor_fingerprint_prop)
+vendor_internal_prop(vendor_nfc_prop)
+vendor_internal_prop(vendor_power_profile_prop)
diff --git a/sepolicy/vendor/property_contexts b/sepolicy/vendor/property_contexts
index dbed547..160f5fa 100644
--- a/sepolicy/vendor/property_contexts
+++ b/sepolicy/vendor/property_contexts
@@ -5,3 +5,9 @@
 # Fingerprint
 persist.vendor.fingerprint.                 u:object_r:vendor_fingerprint_prop:s0
 vendor.fingerprint.                         u:object_r:vendor_fingerprint_prop:s0
+
+# NFC device
+ro.vendor.nfc_device                        u:object_r:vendor_nfc_prop:s0
+
+# Power profile
+ro.vendor.power_profile.device              u:object_r:vendor_power_profile_prop:s0