RM6785: Refactor libinit

Change-Id: I49c33bf86e981b3156a901beaa19870ef4fd666f
diff --git a/init/init_RM6785.cpp b/init/init_RM6785.cpp
index b691fed..2ce8964 100644
--- a/init/init_RM6785.cpp
+++ b/init/init_RM6785.cpp
@@ -1,135 +1,132 @@
 //
-// Copyright (C) 2022 The LineageOS Project
+// Copyright (C) 2022-2023 The LineageOS Project
 //
 // SPDX-License-Identifier: Apache-2.0
 //
 
-#include <fstream>
-#include <tuple>
+#include <android-base/file.h>
 #include <android-base/logging.h>
+#include <vector>
 
 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
 #include <sys/_system_properties.h>
 
 #include "vendor_init.h"
 
-#define PROC_OPERATOR "/proc/oppoVersion/operatorName"
+constexpr const char* kProcOperatorPath = "/proc/oppoVersion/operatorName";
 
-void property_override(std::string prop, std::string value, bool add = true) {
-    auto pi = (prop_info *)__system_property_find(prop.c_str());
+struct DeviceProps {
+    std::string device;
+    std::string model;
+};
+
+void property_override(const char* prop, const char* value, bool add = true) {
+    prop_info* pi = (prop_info*)__system_property_find(prop);
 
     if (pi != nullptr) {
-        __system_property_update(pi, value.c_str(), value.size());
+        __system_property_update(pi, value, strlen(value));
     } else if (add) {
-        __system_property_add(prop.c_str(), prop.size(), value.c_str(),
-                              value.size());
+        __system_property_add(prop, strlen(prop), value, strlen(value));
     }
 }
 
-void set_ro_build_prop(const std::string &prop, const std::string &value,
+void set_ro_build_prop(const std::string& prop, const std::string& value,
                        bool product = true) {
     std::string prop_name;
-    std::string prop_types[] = {
-        "",
-        "bootimage.",
-        "odm.",
-        "odm_dlkm.",
-        "product.",
-        "system.",
-        "system_ext.",
-        "vendor.",
-        "vendor_dlkm.",
-    };
+    std::vector<std::string> prop_types = {
+            "",        "bootimage.",  "odm.",    "odm_dlkm.",   "product.",
+            "system.", "system_ext.", "vendor.", "vendor_dlkm."};
 
-    for (const std::string &source : prop_types) {
+    for (const auto& source : prop_types) {
         if (product) {
             prop_name = "ro.product." + source + prop;
         } else {
             prop_name = "ro." + source + "build." + prop;
         }
 
-        property_override(prop_name, value, false);
+        property_override(prop_name.c_str(), value.c_str(), false);
     }
 }
 
-std::tuple<std::string, std::string> get_device() {
-    std::string device, model, line;
-    std::ifstream operator_file(PROC_OPERATOR);
-    operator_file.good();
-    getline(operator_file, line);
+DeviceProps get_device() {
+    std::string device, model, operator_content;
+    if (!android::base::ReadFileToString(kProcOperatorPath,
+                                         &operator_content)) {
+        LOG(ERROR) << "Failed to read file: " << kProcOperatorPath;
+        return {"", ""};
+    }
 
-    int operatorName = stoi(line);
+    int operator_code = stoi(operator_content);
 
-    switch (operatorName) {
+    DeviceProps device_props;
+    switch (operator_code) {
         // realme 6
         case 101:
         case 102:
         case 104:
         case 105:
-            device = "RMX2001L1";
-            model = "RMX2001";
+            device_props = {"RMX2001", "RMX2001L1"};
             break;
         case 106:
-            device = "RMX2003L1";
-            model = "RMX2003";
+            device_props = {"RMX2003", "RMX2003L1"};
             break;
         case 113:
-            device = "RMX2005L1";
-            model = "RMX2005";
+            device_props = {"RMX2005", "RMX2005L1"};
             break;
         // realme 6i/6s/Narzo
         case 111:
         case 112:
         case 114:
-            device = "RMX2002L1";
-            model = "RMX2002";
+            device_props = {"RMX2002", "RMX2002L1"};
             break;
         // realme 7
         case 140:
         case 141:
         case 146:
         case 149:
-            device = "RMX2151L1";
-            model = "RMX2151";
+            device_props = {"RMX2151", "RMX2151L1"};
             break;
         case 142:
-            device = "RMX2153L1";
-            model = "RMX2153";
+            device_props = {"RMX2153", "RMX2153L1"};
             break;
         case 94:
         case 148:
-            device = "RMX2155L1";
-            model = "RMX2155";
+            device_props = {"RMX2155", "RMX2155L1"};
             break;
         // realme Narzo 30 4G
         case 90:
         case 92:
-            device = "RMX2156L1";
-            model = "RMX2156";
+            device_props = {"RMX2156", "RMX2156L1"};
             break;
         // realme Narzo 20 Pro
         case 143:
-            device = "RMX2161L1";
-            model = "RMX2161";
+            device_props = {"RMX2161", "RMX2161L1"};
             break;
         case 145:
         case 147:
-            device = "RMX2163L1";
-            model = "RMX2163";
+            device_props = {"RMX2163", "RMX2163L1"};
             break;
         default:
-            LOG(ERROR) << "Unable to read operator from " << PROC_OPERATOR;
+            LOG(ERROR) << "Unknown operator found: " << operator_code;
+            device_props = {"", ""};
     }
-    return {device, model};
+
+    return device_props;
+}
+
+void set_device() {
+    const DeviceProps device_props = get_device();
+
+    if (!device_props.device.empty() && !device_props.model.empty()) {
+        set_ro_build_prop("device", device_props.device);
+        set_ro_build_prop("model", device_props.model);
+        set_ro_build_prop("name", device_props.model);
+        set_ro_build_prop("product", device_props.model, false);
+    }
 }
 
 void vendor_load_properties() {
 #ifndef __ANDROID_RECOVERY__
-    auto [device, model] = get_device();
-
-    set_ro_build_prop("device", device);
-    set_ro_build_prop("model", model);
-    set_ro_build_prop("name", model);
-    set_ro_build_prop("product", model, false);
+    set_device();
 #endif
 }
diff --git a/init/init_RM6785_vendor.cpp b/init/init_RM6785_vendor.cpp
index a6be8e0..85733d7 100644
--- a/init/init_RM6785_vendor.cpp
+++ b/init/init_RM6785_vendor.cpp
@@ -1,57 +1,76 @@
 //
-// Copyright (C) 2022 The LineageOS Project
+// Copyright (C) 2022-2023 The LineageOS Project
 //
 // SPDX-License-Identifier: Apache-2.0
 //
 
+#include <android-base/file.h>
 #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"
+constexpr const char* kProcNfcPath = "/proc/oppo_nfc/chipset";
+constexpr const char* kProcOperatorPath = "/proc/oppoVersion/operatorName";
+constexpr const char* kPropPowerProfile = "ro.vendor.power_profile.device";
+constexpr const char* kPropNfcDevice = "ro.vendor.nfc_device";
 
-void set_property(std::string prop, std::string value) {
+void set_property(const std::string& prop, const std::string& value) {
     LOG(INFO) << "Setting property: " << prop << " to " << value;
-    if (!android::base::SetProperty(prop.c_str(), value.c_str()))
+    if (!android::base::SetProperty(prop, value)) {
         LOG(ERROR) << "Unable to set: " << prop << " to " << value;
+    }
 }
 
-void nfc_detect() {
-    std::string chipset;
-    std::ifstream nfc_file(PROC_NFC);
+void detect_nfc() {
+    std::string nfc_chipset;
+    if (!android::base::ReadFileToString(kProcNfcPath, &nfc_chipset)) {
+        LOG(ERROR) << "Failed to read file: " << kProcNfcPath;
+        return;
+    }
 
-    getline(nfc_file, chipset);
-
-    LOG(INFO) << "oppo_nfc : chipset " << chipset;
-
-    if (chipset != "NULL")
-        set_property("ro.vendor.nfc_device", "1");
+    if (nfc_chipset != "NULL") {
+        set_property(kPropNfcDevice, "1");
+    }
 }
 
-void power_profile() {
-    std::string op;
-    std::ifstream operator_file(PROC_OPERATOR);
+void set_power_profile() {
+    std::string device_name, operator_content;
+    if (!android::base::ReadFileToString(kProcOperatorPath,
+                                         &operator_content)) {
+        LOG(ERROR) << "Failed to read file: " << kProcOperatorPath;
+        return;
+    }
 
-    getline(operator_file, op);
+    int operator_code = stoi(operator_content);
 
-    int operatorName = stoi(op);
-
-    switch (operatorName) {
-        case 90: case 92: 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");
+    switch (operator_code) {
+        case 90:
+        case 92:
+        case 94:
+        case 140:
+        case 141:
+        case 142:
+        case 146:
+        case 148:
+        case 149:
+            device_name = "RMX2151";
             break;
-        case 143: case 145: case 147:
-            LOG(INFO) << "operatorName: " << operatorName;
-            set_property("ro.vendor.power_profile.device", "RMX2161");
+        case 143:
+        case 145:
+        case 147:
+            device_name = "RMX2161";
             break;
         default:
-            LOG(INFO) << "operatorName: " << operatorName;
+            LOG(ERROR) << "Unknown operator found: " << operator_code;
+    }
+
+    if (!device_name.empty()) {
+        set_property(kPropPowerProfile, device_name);
     }
 }
 
 int main() {
-    nfc_detect();
-    power_profile();
+    detect_nfc();
+    set_power_profile();
+
+    return 0;
 }