blob: 297bc3018796a217194663d5eae387b0195af11e [file] [log] [blame]
Patrick Ventureaf696252018-12-11 10:22:14 -08001#include "bt.hpp"
2
Patrick Venture907f3a72019-01-15 14:13:37 -08003#include "blob_errors.hpp"
4
5#include <cstdint>
6#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
20 static constexpr int btBufferLen = 50;
21 std::uint8_t readBuffer[btBufferLen];
22 int bytesRead;
23 std::uint32_t offset = 0;
24
25 try
26 {
27 do
28 {
29 bytesRead = sys->read(inputFd, readBuffer, sizeof(readBuffer));
30 if (bytesRead > 0)
31 {
32 /* minorly awkward repackaging. */
33 std::vector<std::uint8_t> buffer(&readBuffer[0],
34 &readBuffer[bytesRead]);
35 blob->writeBytes(session, offset, buffer);
36 offset += bytesRead;
37 }
38 } while (bytesRead > 0);
39 }
40 catch (const BlobException& b)
41 {
42 sys->close(inputFd);
43 return false;
44 }
45
46 sys->close(inputFd);
47 return true;
Patrick Ventureaf696252018-12-11 10:22:14 -080048}
Patrick Venture9b534f02018-12-13 16:10:02 -080049
50} // namespace host_tool