xref: /openbmc/phosphor-ipmi-flash/tools/bt.cpp (revision 9b7a3b4f)
1 #include "bt.hpp"
2 
3 #include <ipmiblob/blob_errors.hpp>
4 
5 #include <cstdint>
6 #include <vector>
7 
8 namespace host_tool
9 {
10 
sendContents(const std::string & input,std::uint16_t session)11 bool BtDataHandler::sendContents(const std::string& input,
12                                  std::uint16_t session)
13 {
14     int inputFd = sys->open(input.c_str(), 0);
15     if (inputFd < 0)
16     {
17         return false;
18     }
19 
20     std::int64_t fileSize = sys->getSize(input.c_str());
21     progress->start(fileSize);
22 
23     try
24     {
25         static constexpr int btBufferLen = 50;
26         std::uint8_t readBuffer[btBufferLen];
27         int bytesRead;
28         std::uint32_t offset = 0;
29 
30         do
31         {
32             bytesRead = sys->read(inputFd, readBuffer, sizeof(readBuffer));
33             if (bytesRead > 0)
34             {
35                 /* minorly awkward repackaging. */
36                 std::vector<std::uint8_t> buffer(&readBuffer[0],
37                                                  &readBuffer[bytesRead]);
38                 blob->writeBytes(session, offset, buffer);
39                 offset += bytesRead;
40                 progress->updateProgress(bytesRead);
41             }
42         } while (bytesRead > 0);
43     }
44     catch (const ipmiblob::BlobException& b)
45     {
46         progress->abort();
47         sys->close(inputFd);
48         return false;
49     }
50 
51     progress->finish();
52     sys->close(inputFd);
53     return true;
54 }
55 
56 } // namespace host_tool
57