tools: specify common ipmi errors for humans

Add a check for common ipmi failure codes to the IPMI exception object
to make the messages more human readable.

Change-Id: If143a3a63748c258b2fdc4fd074cf5070c0b88d9
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index 0b01997..9be5ae4 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -30,7 +30,8 @@
 	tools_blob_unittest \
 	tools_bt_unittest \
 	tools_updater_unittest \
-	tools_ipmi_unittest
+	tools_ipmi_unittest \
+	tools_ipmi_error_unittest
 
 TESTS = $(check_PROGRAMS)
 
@@ -81,3 +82,6 @@
 
 tools_ipmi_unittest_SOURCES = tools_ipmi_unittest.cpp
 tools_ipmi_unittest_LDADD = $(top_builddir)/tools/ipmi_handler.o
+
+tools_ipmi_error_unittest_SOURCES = tools_ipmi_error_unittest.cpp
+tools_ipmi_error_unittest_LDADD =
diff --git a/test/tools_ipmi_error_unittest.cpp b/test/tools_ipmi_error_unittest.cpp
new file mode 100644
index 0000000..86264a1
--- /dev/null
+++ b/test/tools_ipmi_error_unittest.cpp
@@ -0,0 +1,28 @@
+#include "ipmi_errors.hpp"
+
+#include <gtest/gtest.h>
+
+namespace host_tool
+{
+
+TEST(IpmiExceptionTest, VerifyTimedOutIsString)
+{
+    /* Verify that throwing the exception with the cc code for timed out gets
+     * converted to the human readable string.
+     */
+    bool verified = false;
+
+    try
+    {
+        throw IpmiException(0xc3);
+    }
+    catch (const IpmiException& i)
+    {
+        EXPECT_STREQ("Received IPMI_CC: timeout", i.what());
+        verified = true;
+    }
+
+    EXPECT_TRUE(verified);
+}
+
+} // namespace host_tool
diff --git a/tools/ipmi_errors.hpp b/tools/ipmi_errors.hpp
index 210ef53..9f1a9f9 100644
--- a/tools/ipmi_errors.hpp
+++ b/tools/ipmi_errors.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include <exception>
+#include <map>
 #include <sstream>
 #include <string>
 
@@ -10,10 +11,26 @@
 class IpmiException : public std::exception
 {
   public:
+    const std::map<int, std::string> commonFailures = {
+        {0xc0, "busy"},
+        {0xc1, "invalid"},
+        {0xc3, "timeout"},
+    };
+
     explicit IpmiException(int cc)
     {
         std::ostringstream smessage;
-        smessage << "Received IPMI_CC: " << cc;
+
+        auto search = commonFailures.find(cc);
+        if (search != commonFailures.end())
+        {
+            smessage << "Received IPMI_CC: " << search->second;
+        }
+        else
+        {
+            smessage << "Received IPMI_CC: " << cc;
+        }
+
         message = smessage.str();
     }
     explicit IpmiException(const std::string& message) : message(message){};