Add read() function to PMBus

Adding a read() function to PMBus to read one or more bytes of data. A
number of errors will require data from the 2 byte STATUS_WORD command,
1 byte from STATUS_INPUT and various other commands, along with later
functions that will require even more bytes of data.

Change-Id: I0433b36254565a124229a5bb884fbebd6ec536fc
Signed-off-by: Brandon Wyman <bjwyman@gmail.com>
diff --git a/pmbus.cpp b/pmbus.cpp
index b525bd9..95e3bbb 100644
--- a/pmbus.cpp
+++ b/pmbus.cpp
@@ -53,7 +53,7 @@
     switch (type)
     {
         default:
-            /* fall through */
+        /* fall through */
         case Type::Base:
             return basePath;
             break;
@@ -111,7 +111,7 @@
         auto rc = errno;
 
         log<level::ERR>("Failed to read sysfs file",
-                entry("FILENAME=%s", path.c_str()));
+                        entry("FILENAME=%s", path.c_str()));
 
         elog<ReadFailure>(xyz::openbmc_project::Sensor::Device::
                           ReadFailure::CALLOUT_ERRNO(rc),
@@ -123,6 +123,38 @@
     return value != 0;
 }
 
+uint64_t PMBus::read(const std::string& name, Type type)
+{
+    uint64_t data = 0;
+    std::ifstream file;
+    auto path = getPath(type);
+    path /= name;
+
+    file.exceptions(std::ifstream::failbit |
+                    std::ifstream::badbit |
+                    std::ifstream::eofbit);
+
+    try
+    {
+        file.open(path);
+        file >> std::hex >> data;
+    }
+    catch (std::exception& e)
+    {
+        auto rc = errno;
+        log<level::ERR>("Failed to read sysfs file",
+                        entry("FILENAME=%s", path.c_str()));
+
+        using metadata = xyz::openbmc_project::Sensor::Device::ReadFailure;
+
+        elog<ReadFailure>(metadata::CALLOUT_ERRNO(rc),
+                          metadata::CALLOUT_DEVICE_PATH(
+                                  fs::canonical(basePath).c_str()));
+    }
+
+    return data;
+}
+
 void PMBus::write(const std::string& name, int value, Type type)
 {
     std::ofstream file;
@@ -144,7 +176,7 @@
         auto rc = errno;
 
         log<level::ERR>("Failed to write sysfs file",
-                entry("FILENAME=%s", path.c_str()));
+                        entry("FILENAME=%s", path.c_str()));
 
         elog<WriteFailure>(xyz::openbmc_project::Control::Device::
                            WriteFailure::CALLOUT_ERRNO(rc),
@@ -163,7 +195,7 @@
     for (auto& f : fs::directory_iterator(path))
     {
         if ((f.path().filename().string().find("hwmon") !=
-            std::string::npos) &&
+             std::string::npos) &&
             (fs::is_directory(f.path())))
         {
             hwmonDir = f.path().filename();
diff --git a/pmbus.hpp b/pmbus.hpp
index 4946829..82e2ffc 100644
--- a/pmbus.hpp
+++ b/pmbus.hpp
@@ -61,7 +61,7 @@
          *
          * @param[in] name - path concatenated to
          *                   basePath to read
-         * @param[in] type - one of Base, Hwmon, or Debug
+         * @param[in] type - one of Base, Hwmon, or Debug (path type)
          *
          * @return bool - false if result was 0, else true
          */
@@ -75,13 +75,22 @@
          * @param[in] name - path concatenated to
          *                   basePath to read
          * @param[in] page - page number
-         * @param[in] type - one of Base, Hwmon, or Debug
+         * @param[in] type - one of Base, Hwmon, or Debug (path type)
          *
          * @return bool - false if result was 0, else true
          */
         bool readBitInPage(const std::string& name,
                            size_t page,
                            Type type);
+        /**
+         * Read byte(s) from file in sysfs.
+         *
+         * @param[in] name   - path concatenated to basePath to read
+         * @param[in] type   - one of Base, Hwmon, or Debug (path type)
+         *
+         * @return uint64_t - Up to 8 bytes of data read from file.
+         */
+        uint64_t read(const std::string& name, Type type);
 
         /**
          * Writes an integer value to the file, therefore doing
@@ -90,7 +99,7 @@
          * @param[in] name - path concatenated to
          *                   basePath to write
          * @param[in] value - the value to write
-         * @param[in] type - one of Base, Hwmon, or Debug
+         * @param[in] type - one of Base, Hwmon, or Debug (path type)
          */
         void write(const std::string& name, int value, Type type);
 
@@ -127,7 +136,7 @@
         /**
          * Returns the path to use for the passed in type.
          *
-         * @param[in] type - one of Base, Hwmon, or Debug
+         * @param[in] type - one of Base, Hwmon, or Debug (path type)
          *
          * @return fs::path - the full path to Base, Hwmon, or Debug path
          */