Refactor: move get pmbus access type to utility
The function to get the pmbus access type is common, move it to utility.
Signed-off-by: Lei YU <mine260309@gmail.com>
Change-Id: I26e7da044c88c1efba141349ee4c18b24cdef81f
diff --git a/power-supply/power_supply.cpp b/power-supply/power_supply.cpp
index 0a5c9cc..62067ef 100644
--- a/power-supply/power_supply.cpp
+++ b/power-supply/power_supply.cpp
@@ -121,7 +121,6 @@
void PowerSupply::getAccessType()
{
- using namespace phosphor::pmbus;
using namespace phosphor::power::util;
fruJson = loadJSONFromFile(PSU_JSON_PATH);
if (fruJson == nullptr)
@@ -129,29 +128,7 @@
log<level::ERR>("InternalFailure when parsing the JSON file");
return;
}
-
- auto type = fruJson.at("inventoryPMBusAccessType");
-
- if (type == "Hwmon")
- {
- inventoryPMBusAccessType = Type::Hwmon;
- }
- else if (type == "DeviceDebug")
- {
- inventoryPMBusAccessType = Type::DeviceDebug;
- }
- else if (type == "Debug")
- {
- inventoryPMBusAccessType = Type::Debug;
- }
- else if (type == "HwmonDeviceDebug")
- {
- inventoryPMBusAccessType = Type::HwmonDeviceDebug;
- }
- else
- {
- inventoryPMBusAccessType = Type::Base;
- }
+ inventoryPMBusAccessType = getPMBusAccessType(fruJson);
}
void PowerSupply::captureCmd(util::NamesValues& nv, const std::string& cmd,
diff --git a/power-supply/power_supply.hpp b/power-supply/power_supply.hpp
index e292ca2..550373b 100644
--- a/power-supply/power_supply.hpp
+++ b/power-supply/power_supply.hpp
@@ -265,7 +265,8 @@
/**
* @brief The type of the power supply inventory pmbus access.
*/
- phosphor::pmbus::Type inventoryPMBusAccessType;
+ phosphor::pmbus::Type inventoryPMBusAccessType =
+ phosphor::pmbus::Type::Base;
/**
* @brief The JSON from the parsed power supply FRU JSON File.
diff --git a/utility.cpp b/utility.cpp
index 8fe02dc..bd84249 100644
--- a/utility.cpp
+++ b/utility.cpp
@@ -73,6 +73,36 @@
return data;
}
+phosphor::pmbus::Type getPMBusAccessType(const json& json)
+{
+ using namespace phosphor::pmbus;
+ Type type;
+
+ auto typeStr = json.at("inventoryPMBusAccessType");
+
+ if (typeStr == "Hwmon")
+ {
+ type = Type::Hwmon;
+ }
+ else if (typeStr == "DeviceDebug")
+ {
+ type = Type::DeviceDebug;
+ }
+ else if (typeStr == "Debug")
+ {
+ type = Type::Debug;
+ }
+ else if (typeStr == "HwmonDeviceDebug")
+ {
+ type = Type::HwmonDeviceDebug;
+ }
+ else
+ {
+ type = Type::Base;
+ }
+ return type;
+}
+
} // namespace util
} // namespace power
} // namespace phosphor
diff --git a/utility.hpp b/utility.hpp
index 78b9425..f6c40d8 100644
--- a/utility.hpp
+++ b/utility.hpp
@@ -1,5 +1,7 @@
#pragma once
+#include "pmbus.hpp"
+
#include <nlohmann/json.hpp>
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/log.hpp>
@@ -114,6 +116,15 @@
*/
nlohmann::json loadJSONFromFile(const char* path);
+/**
+ * Get PmBus access type from the json config
+ *
+ * @param[in] json - The json object
+ *
+ * @return The pmbus access type
+ */
+phosphor::pmbus::Type getPMBusAccessType(const nlohmann::json& json);
+
} // namespace util
} // namespace power
} // namespace phosphor