psutils: Move utility functions to utils.*pp

The files named version.hpp and version.cpp implement the --get-version
option of the psutils tool.

However, the files also contain some utility functions that would be
helpful for implementing other command line options, such as --update or
the planned new option --get-model.

Move the utility functions into new files named utils.hpp and utils.cpp.
The functions will be defined within a namespace named 'utils'.

Tested:
* Verified automated testcases ran successfully
* Verified --get-version option worked correctly
  * When using a psu.json file
  * When using D-Bus information

Change-Id: If902ee4581fce000af37073ac2e7a7b0ade01f78
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
diff --git a/tools/power-utils/main.cpp b/tools/power-utils/main.cpp
index f240ece..8fae3a1 100644
--- a/tools/power-utils/main.cpp
+++ b/tools/power-utils/main.cpp
@@ -16,6 +16,7 @@
 #include "config.h"
 
 #include "updater.hpp"
+#include "utils.hpp"
 #include "version.hpp"
 
 #include <CLI/CLI.hpp>
@@ -51,7 +52,7 @@
 
     std::string ret;
 
-    bool useJsonFile = version::utils::checkFileExists(PSU_JSON_PATH);
+    bool useJsonFile = utils::checkFileExists(PSU_JSON_PATH);
     auto bus = sdbusplus::bus::new_default();
     if (!psuPath.empty())
     {
diff --git a/tools/power-utils/meson.build b/tools/power-utils/meson.build
index e329393..41149da 100644
--- a/tools/power-utils/meson.build
+++ b/tools/power-utils/meson.build
@@ -2,6 +2,7 @@
     'psutils',
     'version.cpp',
     'updater.cpp',
+    'utils.cpp',
     'main.cpp',
     dependencies: [
         cli11_dep,
diff --git a/tools/power-utils/test/meson.build b/tools/power-utils/test/meson.build
index e48f799..61c5f53 100644
--- a/tools/power-utils/test/meson.build
+++ b/tools/power-utils/test/meson.build
@@ -4,6 +4,7 @@
         'test_version',
         'test_version.cpp',
         '../version.cpp',
+        '../utils.cpp',
         dependencies: [
             gtest,
             nlohmann_json_dep,
@@ -26,7 +27,7 @@
         'test_updater',
         'test_updater.cpp',
         '../updater.cpp',
-        '../version.cpp',
+        '../utils.cpp',
         dependencies: [
             gtest,
             gmock,
diff --git a/tools/power-utils/updater.cpp b/tools/power-utils/updater.cpp
index 951b78c..dab0460 100644
--- a/tools/power-utils/updater.cpp
+++ b/tools/power-utils/updater.cpp
@@ -20,6 +20,7 @@
 #include "pmbus.hpp"
 #include "types.hpp"
 #include "utility.hpp"
+#include "utils.hpp"
 
 #include <sys/stat.h>
 
@@ -76,9 +77,8 @@
         }
         else
         {
-            using namespace version;
             const auto& [i2cbus, i2caddr] =
-                version::utils::getPsuI2c(bus, psuInventoryPath);
+                utils::getPsuI2c(bus, psuInventoryPath);
             const auto DevicePath = "/sys/bus/i2c/devices/";
             std::ostringstream ss;
             ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr;
@@ -253,7 +253,7 @@
 // Wrapper to check existence of PSU JSON file.
 bool usePsuJsonFile()
 {
-    return version::utils::checkFileExists(PSU_JSON_PATH);
+    return utils::checkFileExists(PSU_JSON_PATH);
 }
 } // namespace internal
 
diff --git a/tools/power-utils/updater.hpp b/tools/power-utils/updater.hpp
index 1732f86..251347e 100644
--- a/tools/power-utils/updater.hpp
+++ b/tools/power-utils/updater.hpp
@@ -16,7 +16,6 @@
 #pragma once
 
 #include "i2c_interface.hpp"
-#include "version.hpp"
 
 #include <sdbusplus/bus.hpp>
 
diff --git a/tools/power-utils/utils.cpp b/tools/power-utils/utils.cpp
new file mode 100644
index 0000000..0afc4a8
--- /dev/null
+++ b/tools/power-utils/utils.cpp
@@ -0,0 +1,168 @@
+/**
+ * Copyright © 2024 IBM Corporation
+ *
+ * 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 "utils.hpp"
+
+#include "utility.hpp"
+
+#include <phosphor-logging/log.hpp>
+#include <xyz/openbmc_project/Common/Device/error.hpp>
+
+#include <exception>
+#include <iostream>
+#include <regex>
+#include <stdexcept>
+
+using namespace phosphor::logging;
+using namespace phosphor::power::util;
+using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
+
+namespace utils
+{
+
+constexpr auto IBMCFFPSInterface =
+    "xyz.openbmc_project.Configuration.IBMCFFPSConnector";
+constexpr auto i2cBusProp = "I2CBus";
+constexpr auto i2cAddressProp = "I2CAddress";
+
+PsuI2cInfo getPsuI2c(sdbusplus::bus_t& bus, const std::string& psuInventoryPath)
+{
+    auto depth = 0;
+    auto objects = getSubTree(bus, "/", IBMCFFPSInterface, depth);
+    if (objects.empty())
+    {
+        throw std::runtime_error("Supported Configuration Not Found");
+    }
+
+    std::optional<std::uint64_t> i2cbus;
+    std::optional<std::uint64_t> i2caddr;
+
+    // GET a map of objects back.
+    // Each object will have a path, a service, and an interface.
+    for (const auto& [path, services] : objects)
+    {
+        auto service = services.begin()->first;
+
+        if (path.empty() || service.empty())
+        {
+            continue;
+        }
+
+        // Match the PSU identifier in the path with the passed PSU inventory
+        // path. Compare the last character of both paths to find the PSU bus
+        // and address. example: PSU path:
+        // /xyz/openbmc_project/inventory/system/board/Nisqually_Backplane/Power_Supply_Slot_0
+        // PSU inventory path:
+        // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0
+        if (path.back() == psuInventoryPath.back())
+        {
+            // Retrieve i2cBus and i2cAddress from array of properties.
+            auto properties =
+                getAllProperties(bus, path, IBMCFFPSInterface, service);
+            for (const auto& property : properties)
+            {
+                try
+                {
+                    if (property.first == i2cBusProp)
+                    {
+                        i2cbus = std::get<uint64_t>(properties.at(i2cBusProp));
+                    }
+                    else if (property.first == i2cAddressProp)
+                    {
+                        i2caddr =
+                            std::get<uint64_t>(properties.at(i2cAddressProp));
+                    }
+                }
+                catch (const std::exception& e)
+                {
+                    log<level::WARNING>(
+                        std::format("Error reading property {}: {}",
+                                    property.first, e.what())
+                            .c_str());
+                }
+            }
+
+            if (i2cbus.has_value() && i2caddr.has_value())
+            {
+                break;
+            }
+        }
+    }
+
+    if (!i2cbus.has_value() || !i2caddr.has_value())
+    {
+        throw std::runtime_error("Failed to get I2C bus or address");
+    }
+
+    return std::make_tuple(*i2cbus, *i2caddr);
+}
+
+std::unique_ptr<phosphor::pmbus::PMBusBase>
+    getPmbusIntf(std::uint64_t i2cBus, std::uint64_t i2cAddr)
+{
+    std::stringstream ss;
+    ss << std::hex << std::setw(4) << std::setfill('0') << i2cAddr;
+    return phosphor::pmbus::createPMBus(i2cBus, ss.str());
+}
+
+std::string readVPDValue(phosphor::pmbus::PMBusBase& pmbusIntf,
+                         const std::string& vpdName,
+                         const phosphor::pmbus::Type& type,
+                         const std::size_t& vpdSize)
+{
+    std::string vpdValue;
+    const std::regex illegalVPDRegex =
+        std::regex("[^[:alnum:]]", std::regex::basic);
+
+    try
+    {
+        vpdValue = pmbusIntf.readString(vpdName, type);
+    }
+    catch (const ReadFailure& e)
+    {
+        // Ignore the read failure, let pmbus code indicate failure.
+    }
+
+    if (vpdValue.size() != vpdSize)
+    {
+        log<level::INFO>(
+            std::format(" {} resize needed. size: {}", vpdName, vpdValue.size())
+                .c_str());
+        vpdValue.resize(vpdSize, ' ');
+    }
+
+    // Replace any illegal values with space(s).
+    std::regex_replace(vpdValue.begin(), vpdValue.begin(), vpdValue.end(),
+                       illegalVPDRegex, " ");
+
+    return vpdValue;
+}
+
+bool checkFileExists(const std::string& filePath)
+{
+    try
+    {
+        return std::filesystem::exists(filePath);
+    }
+    catch (const std::exception& e)
+    {
+        log<level::ERR>(std::format("Unable to check for existence of {}: {}",
+                                    filePath, e.what())
+                            .c_str());
+    }
+    return false;
+}
+
+} // namespace utils
diff --git a/tools/power-utils/utils.hpp b/tools/power-utils/utils.hpp
new file mode 100644
index 0000000..4c86b54
--- /dev/null
+++ b/tools/power-utils/utils.hpp
@@ -0,0 +1,88 @@
+/**
+ * Copyright © 2024 IBM Corporation
+ *
+ * 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 "pmbus.hpp"
+
+#include <sdbusplus/bus.hpp>
+
+#include <cstdint>
+#include <memory>
+#include <string>
+#include <tuple>
+
+/**
+ * @namespace utils
+ *
+ * Contains utility functions used within the psutils tool.
+ */
+namespace utils
+{
+
+// PsuI2cInfo contains the device i2c bus and i2c address
+using PsuI2cInfo = std::tuple<std::uint64_t, std::uint64_t>;
+
+/**
+ * @brief Get i2c bus and address
+ *
+ * @param[in] bus - Systemd bus connection
+ * @param[in] psuInventoryPath - The PSU inventory path.
+ *
+ * @return tuple - i2cBus and i2cAddr.
+ */
+PsuI2cInfo getPsuI2c(sdbusplus::bus_t& bus,
+                     const std::string& psuInventoryPath);
+
+/**
+ * @brief Get PMBus interface pointer
+ *
+ * @param[in] i2cBus - PSU i2c bus
+ * @param[in] i2cAddr - PSU i2c address
+ *
+ * @return Pointer to PSU PMBus interface
+ */
+std::unique_ptr<phosphor::pmbus::PMBusBase>
+    getPmbusIntf(std::uint64_t i2cBus, std::uint64_t i2cAddr);
+
+/**
+ * @brief Reads a VPD value from PMBus, corrects size, and contents.
+ *
+ * If the VPD data read is not the passed in size, resize and fill with
+ * spaces. If the data contains a non-alphanumeric value, replace any of
+ * those values with spaces.
+ *
+ * @param[in] pmbusIntf - PMBus Interface.
+ * @param[in] vpdName - The name of the sysfs "file" to read data from.
+ * @param[in] type - The HWMON file type to read from.
+ * @param[in] vpdSize - The expected size of the data for this VPD/property
+ *
+ * @return A string containing the VPD data read, resized if necessary
+ */
+std::string readVPDValue(phosphor::pmbus::PMBusBase& pmbusIntf,
+                         const std::string& vpdName,
+                         const phosphor::pmbus::Type& type,
+                         const std::size_t& vpdSize);
+
+/**
+ * @brief Check for file existence
+ *
+ * @param[in] filePath - File path
+ *
+ * @return bool
+ */
+bool checkFileExists(const std::string& filePath);
+
+} // namespace utils
diff --git a/tools/power-utils/version.cpp b/tools/power-utils/version.cpp
index fab0705..26507ee 100644
--- a/tools/power-utils/version.cpp
+++ b/tools/power-utils/version.cpp
@@ -19,35 +19,29 @@
 
 #include "pmbus.hpp"
 #include "utility.hpp"
+#include "utils.hpp"
 
+#include <nlohmann/json.hpp>
 #include <phosphor-logging/log.hpp>
-#include <xyz/openbmc_project/Common/Device/error.hpp>
 
 #include <exception>
-#include <iostream>
-#include <regex>
-#include <stdexcept>
 #include <tuple>
 
 using json = nlohmann::json;
 
+using namespace utils;
 using namespace phosphor::logging;
-
 using namespace phosphor::power::util;
-using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
 
 namespace version
 {
-namespace utils
+
+namespace internal
 {
-constexpr auto IBMCFFPSInterface =
-    "xyz.openbmc_project.Configuration.IBMCFFPSConnector";
-constexpr auto i2cBusProp = "I2CBus";
-constexpr auto i2cAddressProp = "I2CAddress";
 
 PsuVersionInfo getVersionInfo(const std::string& psuInventoryPath)
 {
-    auto data = phosphor::power::util::loadJSONFromFile(PSU_JSON_PATH);
+    auto data = loadJSONFromFile(PSU_JSON_PATH);
 
     if (data == nullptr)
     {
@@ -68,7 +62,7 @@
         return {};
     }
 
-    auto type = phosphor::power::util::getPMBusAccessType(data);
+    auto type = getPMBusAccessType(data);
 
     std::string versionStr;
     for (const auto& fru : data["fruConfigs"])
@@ -101,139 +95,12 @@
     return latest;
 }
 
-PsuI2cInfo getPsuI2c(sdbusplus::bus_t& bus, const std::string& psuInventoryPath)
-{
-    auto depth = 0;
-    auto objects = getSubTree(bus, "/", IBMCFFPSInterface, depth);
-    if (objects.empty())
-    {
-        throw std::runtime_error("Supported Configuration Not Found");
-    }
-
-    std::optional<std::uint64_t> i2cbus;
-    std::optional<std::uint64_t> i2caddr;
-
-    // GET a map of objects back.
-    // Each object will have a path, a service, and an interface.
-    for (const auto& [path, services] : objects)
-    {
-        auto service = services.begin()->first;
-
-        if (path.empty() || service.empty())
-        {
-            continue;
-        }
-
-        // Match the PSU identifier in the path with the passed PSU inventory
-        // path. Compare the last character of both paths to find the PSU bus
-        // and address. example: PSU path:
-        // /xyz/openbmc_project/inventory/system/board/Nisqually_Backplane/Power_Supply_Slot_0
-        // PSU inventory path:
-        // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0
-        if (path.back() == psuInventoryPath.back())
-        {
-            // Retrieve i2cBus and i2cAddress from array of properties.
-            auto properties =
-                getAllProperties(bus, path, IBMCFFPSInterface, service);
-            for (const auto& property : properties)
-            {
-                try
-                {
-                    if (property.first == i2cBusProp)
-                    {
-                        i2cbus = std::get<uint64_t>(properties.at(i2cBusProp));
-                    }
-                    else if (property.first == i2cAddressProp)
-                    {
-                        i2caddr =
-                            std::get<uint64_t>(properties.at(i2cAddressProp));
-                    }
-                }
-                catch (const std::exception& e)
-                {
-                    log<level::WARNING>(
-                        std::format("Error reading property {}: {}",
-                                    property.first, e.what())
-                            .c_str());
-                }
-            }
-
-            if (i2cbus.has_value() && i2caddr.has_value())
-            {
-                break;
-            }
-        }
-    }
-
-    if (!i2cbus.has_value() || !i2caddr.has_value())
-    {
-        throw std::runtime_error("Failed to get I2C bus or address");
-    }
-
-    return std::make_tuple(*i2cbus, *i2caddr);
-}
-
-std::unique_ptr<phosphor::pmbus::PMBusBase>
-    getPmbusIntf(std::uint64_t i2cBus, std::uint64_t i2cAddr)
-{
-    std::stringstream ss;
-    ss << std::hex << std::setw(4) << std::setfill('0') << i2cAddr;
-    return phosphor::pmbus::createPMBus(i2cBus, ss.str());
-}
-
-std::string readVPDValue(phosphor::pmbus::PMBusBase& pmbusIntf,
-                         const std::string& vpdName,
-                         const phosphor::pmbus::Type& type,
-                         const std::size_t& vpdSize)
-{
-    std::string vpdValue;
-    const std::regex illegalVPDRegex =
-        std::regex("[^[:alnum:]]", std::regex::basic);
-
-    try
-    {
-        vpdValue = pmbusIntf.readString(vpdName, type);
-    }
-    catch (const ReadFailure& e)
-    {
-        // Ignore the read failure, let pmbus code indicate failure.
-    }
-
-    if (vpdValue.size() != vpdSize)
-    {
-        log<level::INFO>(
-            std::format(" {} resize needed. size: {}", vpdName, vpdValue.size())
-                .c_str());
-        vpdValue.resize(vpdSize, ' ');
-    }
-
-    // Replace any illegal values with space(s).
-    std::regex_replace(vpdValue.begin(), vpdValue.begin(), vpdValue.end(),
-                       illegalVPDRegex, " ");
-
-    return vpdValue;
-}
-
-bool checkFileExists(const std::string& filePath)
-{
-    try
-    {
-        return std::filesystem::exists(filePath);
-    }
-    catch (const std::exception& e)
-    {
-        log<level::ERR>(std::format("Unable to check for existence of {}: {}",
-                                    filePath, e.what())
-                            .c_str());
-    }
-    return false;
-}
-} // namespace utils
+} // namespace internal
 
 std::string getVersion(const std::string& psuInventoryPath)
 {
     const auto& [devicePath, type, versionStr] =
-        utils::getVersionInfo(psuInventoryPath);
+        internal::getVersionInfo(psuInventoryPath);
     if (devicePath.empty() || versionStr.empty())
     {
         return "";
@@ -257,8 +124,8 @@
     std::string version;
     try
     {
-        const auto& [i2cbus, i2caddr] = utils::getPsuI2c(bus, psuInventoryPath);
-        auto pmbus = utils::getPmbusIntf(i2cbus, i2caddr);
+        const auto& [i2cbus, i2caddr] = getPsuI2c(bus, psuInventoryPath);
+        auto pmbus = getPmbusIntf(i2cbus, i2caddr);
         std::string name = "fw_version";
         auto type = phosphor::pmbus::Type::HwmonDeviceDebug;
         version = pmbus->readString(name, type);
@@ -288,6 +155,6 @@
     // means a newer version.
     //
     // So just compare by strings is OK for these cases
-    return utils::getLatestDefault(versions);
+    return internal::getLatestDefault(versions);
 }
 } // namespace version
diff --git a/tools/power-utils/version.hpp b/tools/power-utils/version.hpp
index 534d113..4770a71 100644
--- a/tools/power-utils/version.hpp
+++ b/tools/power-utils/version.hpp
@@ -19,22 +19,18 @@
 
 #include <sdbusplus/bus.hpp>
 
-#include <memory>
 #include <string>
 #include <tuple>
 #include <vector>
 
 namespace version
 {
-namespace utils
+namespace internal
 {
 // PsuInfo contains the device path, pmbus read type, and the version string
 using PsuVersionInfo =
     std::tuple<std::string, phosphor::pmbus::Type, std::string>;
 
-// PsuI2cInfo contains the device i2c bus and i2c address
-using PsuI2cInfo = std::tuple<std::uint64_t, std::uint64_t>;
-
 /**
  * @brief Get PSU version information
  *
@@ -53,57 +49,7 @@
  */
 std::string getLatestDefault(const std::vector<std::string>& versions);
 
-/**
- * @brief Get i2c bus and address
- *
- * @param[in] bus - Systemd bus connection
- * @param[in] psuInventoryPath - The PSU inventory path.
- *
- * @return tuple - i2cBus and i2cAddr.
- */
-PsuI2cInfo getPsuI2c(sdbusplus::bus_t& bus,
-                     const std::string& psuInventoryPath);
-
-/**
- * @brief Get PMBus interface pointer
- *
- * @param[in] i2cBus - PSU i2c bus
- * @param[in] i2cAddr - PSU i2c address
- *
- * @return Pointer to PSU PMBus interface
- */
-std::unique_ptr<phosphor::pmbus::PMBusBase>
-    getPmbusIntf(std::uint64_t i2cBus, std::uint64_t i2cAddr);
-
-/**
- * @brief Reads a VPD value from PMBus, corrects size, and contents.
- *
- * If the VPD data read is not the passed in size, resize and fill with
- * spaces. If the data contains a non-alphanumeric value, replace any of
- * those values with spaces.
- *
- * @param[in] pmbusIntf - PMBus Interface.
- * @param[in] vpdName - The name of the sysfs "file" to read data from.
- * @param[in] type - The HWMON file type to read from.
- * @param[in] vpdSize - The expected size of the data for this VPD/property
- *
- * @return A string containing the VPD data read, resized if necessary
- */
-std::string readVPDValue(phosphor::pmbus::PMBusBase& pmbusIntf,
-                         const std::string& vpdName,
-                         const phosphor::pmbus::Type& type,
-                         const std::size_t& vpdSize);
-
-/**
- * @brief Check for file existence
- *
- * @param[in] filePath - File path
- *
- * @return bool
- */
-bool checkFileExists(const std::string& filePath);
-
-} // namespace utils
+} // namespace internal
 
 /**
  * Get the software version of the PSU using sysfs