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/argument.cpp b/tools/power-utils/argument.cpp
new file mode 100644
index 0000000..570c97f
--- /dev/null
+++ b/tools/power-utils/argument.cpp
@@ -0,0 +1,84 @@
+/**
+ * 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 "argument.hpp"
+
+#include <algorithm>
+#include <iostream>
+#include <iterator>
+
+namespace phosphor
+{
+namespace power
+{
+
+void ArgumentParser::usage(char** argv)
+{
+    std::cerr << "Usage: " << argv[0] << " [options] <psu-inventory-path>\n";
+    std::cerr << "Options:\n";
+    std::cerr << "    --help                Print this menu\n";
+    std::cerr << "    --get-version         Get PSU version\n";
+    std::cerr << std::flush;
+}
+
+const option ArgumentParser::options[] = {
+    {"get-version", required_argument, NULL, 'g'},
+    {"help", no_argument, NULL, 'h'},
+    {0, 0, 0, 0},
+};
+
+const char* ArgumentParser::optionStr = "g:h?";
+ArgumentParser::ArgumentParser(int argc, char** argv)
+{
+    int option = 0;
+    while (-1 != (option = getopt_long(argc, argv, optionStr, options, NULL)))
+    {
+        if ((option == '?') || (option == 'h'))
+        {
+            usage(argv);
+            exit(-1);
+        }
+
+        auto i = &options[0];
+        while ((i->val != option) && (i->val != 0))
+        {
+            ++i;
+        }
+
+        if (i->val)
+        {
+            arguments[i->name] = (i->has_arg ? optarg : trueString);
+        }
+    }
+}
+
+const std::string& ArgumentParser::operator[](const std::string& opt)
+{
+    auto i = arguments.find(opt);
+    if (i == arguments.end())
+    {
+        return emptyString;
+    }
+    else
+    {
+        return i->second;
+    }
+}
+
+const std::string ArgumentParser::trueString = "true";
+const std::string ArgumentParser::emptyString = "";
+
+} // namespace power
+} // namespace phosphor
diff --git a/tools/power-utils/main.cpp b/tools/power-utils/main.cpp
new file mode 100644
index 0000000..e043821
--- /dev/null
+++ b/tools/power-utils/main.cpp
@@ -0,0 +1,39 @@
+/**
+ * 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 "argument.hpp"
+#include "version.hpp"
+
+#include <phosphor-logging/log.hpp>
+
+using namespace phosphor::power;
+using namespace phosphor::logging;
+
+int main(int argc, char** argv)
+{
+    ArgumentParser args{argc, argv};
+    auto psuPath = args["get-version"];
+    if (psuPath.empty())
+    {
+        log<level::ERR>("PSU Inventory path argument required");
+        args.usage(argv);
+        exit(1);
+    }
+
+    // For now only get-version is supported
+    auto version = version::getVersion(psuPath);
+    printf("%s", version.c_str());
+    return version.empty() ? 1 : 0;
+}
diff --git a/tools/power-utils/meson.build b/tools/power-utils/meson.build
new file mode 100644
index 0000000..fa193be
--- /dev/null
+++ b/tools/power-utils/meson.build
@@ -0,0 +1,15 @@
+psutils = executable(
+    'psutils',
+    'argument.cpp',
+    'version.cpp',
+    'main.cpp',
+    dependencies: [
+        phosphor_dbus_interfaces,
+        phosphor_logging,
+    ],
+    include_directories: '../..',
+    install: true,
+    link_with: [
+        libpower,
+    ]
+)
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
diff --git a/tools/power-utils/version.hpp b/tools/power-utils/version.hpp
new file mode 100644
index 0000000..26f0ce6
--- /dev/null
+++ b/tools/power-utils/version.hpp
@@ -0,0 +1,30 @@
+/**
+ * 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.
+ */
+#pragma once
+
+#include <string>
+
+namespace version
+{
+
+/**
+ * Get the software version of the PSU
+ *
+ * @param[in] psuInventoryPath - The inventory path of the PSU
+ */
+std::string getVersion(const std::string& psuInventoryPath);
+
+} // namespace version