tools: implement bt sendcontents

Implement the block transfer (really not blocktransfer only)
sendcontents handler.  This handler sends the file contents within
the IPMI packets themselves.

Note: This is really used for kcs, etc, but it's called bt to avoid
confusion with general ipmi code.

Change-Id: I310034a6afdf0eb25894e658ccee42e6394aa4d2
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/tools/bt.cpp b/tools/bt.cpp
index da582d4..297bc30 100644
--- a/tools/bt.cpp
+++ b/tools/bt.cpp
@@ -1,12 +1,50 @@
 #include "bt.hpp"
 
+#include "blob_errors.hpp"
+
+#include <cstdint>
+#include <vector>
+
 namespace host_tool
 {
 
 bool BtDataHandler::sendContents(const std::string& input,
                                  std::uint16_t session)
 {
-    return false;
+    int inputFd = sys->open(input.c_str(), 0);
+    if (inputFd < 0)
+    {
+        return false;
+    }
+
+    static constexpr int btBufferLen = 50;
+    std::uint8_t readBuffer[btBufferLen];
+    int bytesRead;
+    std::uint32_t offset = 0;
+
+    try
+    {
+        do
+        {
+            bytesRead = sys->read(inputFd, readBuffer, sizeof(readBuffer));
+            if (bytesRead > 0)
+            {
+                /* minorly awkward repackaging. */
+                std::vector<std::uint8_t> buffer(&readBuffer[0],
+                                                 &readBuffer[bytesRead]);
+                blob->writeBytes(session, offset, buffer);
+                offset += bytesRead;
+            }
+        } while (bytesRead > 0);
+    }
+    catch (const BlobException& b)
+    {
+        sys->close(inputFd);
+        return false;
+    }
+
+    sys->close(inputFd);
+    return true;
 }
 
 } // namespace host_tool