Add power-utils

The power-utils is added to support psu code manager as vendor-specifc
tool.
In this commit, the util returns the PSU version based on the PSU
inventory path, where the inventory path are mapped to the PSU sysfs
device directory based on a json config.

Tested: Verify the version is returned correctly on Witherspoon:
           $ ./psutils --getversion \
           /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0
           01100110
        And it returns non-zero when it fails to get the version without
        throwing exception.

Signed-off-by: Lei YU <mine260309@gmail.com>
Change-Id: Ib60f3aa50ce581d55fe4cd62642f30398e25be83
diff --git a/tools/power-utils/version.cpp b/tools/power-utils/version.cpp
new file mode 100644
index 0000000..1acbbf2
--- /dev/null
+++ b/tools/power-utils/version.cpp
@@ -0,0 +1,103 @@
+/**
+ * Copyright © 2019 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 "config.h"
+
+#include "version.hpp"
+
+#include "pmbus.hpp"
+#include "utility.hpp"
+
+#include <phosphor-logging/log.hpp>
+#include <tuple>
+
+using json = nlohmann::json;
+
+using namespace phosphor::logging;
+
+// PsuInfo contains the device path, pmbus read type, and the version string
+using PsuVersionInfo =
+    std::tuple<std::string, phosphor::pmbus::Type, std::string>;
+
+namespace utils
+{
+PsuVersionInfo getVersionInfo(const std::string& psuInventoryPath)
+{
+    auto data = phosphor::power::util::loadJSONFromFile(PSU_JSON_PATH);
+
+    if (data == nullptr)
+    {
+        return {};
+    }
+
+    auto devices = data.find("psuDevices");
+    if (devices == data.end())
+    {
+        log<level::WARNING>("Unable to find psuDevices");
+        return {};
+    }
+    auto devicePath = devices->find(psuInventoryPath);
+    if (devicePath == devices->end())
+    {
+        log<level::WARNING>("Unable to find path for PSU",
+                            entry("PATH=%s", psuInventoryPath.c_str()));
+        return {};
+    }
+
+    auto type = phosphor::power::util::getPMBusAccessType(data);
+
+    std::string versionStr;
+    for (const auto& fru : data["fruConfigs"])
+    {
+        if (fru["propertyName"] == "Version")
+        {
+            versionStr = fru["fileName"];
+            break;
+        }
+    }
+    if (versionStr.empty())
+    {
+        log<level::WARNING>("Unable to find Version file");
+        return {};
+    }
+    return std::make_tuple(*devicePath, type, versionStr);
+}
+} // namespace utils
+
+namespace version
+{
+
+std::string getVersion(const std::string& psuInventoryPath)
+{
+    const auto& [devicePath, type, versionStr] =
+        utils::getVersionInfo(psuInventoryPath);
+    if (devicePath.empty() || versionStr.empty())
+    {
+        return {};
+    }
+    std::string version;
+    try
+    {
+        phosphor::pmbus::PMBus pmbus(devicePath);
+        version = pmbus.readString(versionStr, type);
+    }
+    catch (const std::exception& ex)
+    {
+        log<level::ERR>(ex.what());
+    }
+    return version;
+}
+
+} // namespace version