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();