blob: 7c338084d97ecefc4e613da9bdc3f6fcbdcbd6ae [file] [log] [blame]
Patrick Ventureaf696252018-12-11 10:22:14 -08001#include "bt.hpp"
2
Patrick Venture664c5bc2019-03-07 08:09:45 -08003#include <ipmiblob/blob_errors.hpp>
Patrick Venture9b37b092020-05-28 20:58:57 -07004
5#include <cstdint>
Patrick Venture907f3a72019-01-15 14:13:37 -08006#include <vector>
7
Patrick Venture9b534f02018-12-13 16:10:02 -08008namespace host_tool
9{
10
Patrick Ventureaf696252018-12-11 10:22:14 -080011bool BtDataHandler::sendContents(const std::string& input,
12 std::uint16_t session)
13{
Patrick Venture907f3a72019-01-15 14:13:37 -080014 int inputFd = sys->open(input.c_str(), 0);
15 if (inputFd < 0)
16 {
17 return false;
18 }
19
Patrick Venturecf9b2192019-06-27 12:09:52 -070020 std::int64_t fileSize = sys->getSize(input.c_str());
Patrick Venturecf9b2192019-06-27 12:09:52 -070021 progress->start(fileSize);
22
Patrick Venture907f3a72019-01-15 14:13:37 -080023 try
24 {
Patrick Venture63528042019-05-20 18:10:02 -070025 static constexpr int btBufferLen = 50;
26 std::uint8_t readBuffer[btBufferLen];
27 int bytesRead;
28 std::uint32_t offset = 0;
29
Patrick Venture907f3a72019-01-15 14:13:37 -080030 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;
Patrick Venturecf9b2192019-06-27 12:09:52 -070040 progress->updateProgress(bytesRead);
Patrick Venture907f3a72019-01-15 14:13:37 -080041 }
42 } while (bytesRead > 0);
43 }
Patrick Venture664c5bc2019-03-07 08:09:45 -080044 catch (const ipmiblob::BlobException& b)
Patrick Venture907f3a72019-01-15 14:13:37 -080045 {
William A. Kennington III0d5bb782021-01-19 15:50:42 -080046 progress->abort();
Patrick Venture907f3a72019-01-15 14:13:37 -080047 sys->close(inputFd);
48 return false;
49 }
50
William A. Kennington III0d5bb782021-01-19 15:50:42 -080051 progress->finish();
Patrick Venture907f3a72019-01-15 14:13:37 -080052 sys->close(inputFd);
53 return true;
Patrick Ventureaf696252018-12-11 10:22:14 -080054}
Patrick Venture9b534f02018-12-13 16:10:02 -080055
56} // namespace host_tool