Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 1 | #include "bt.hpp" |
| 2 | |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame^] | 3 | #include "blob_errors.hpp" |
| 4 | |
| 5 | #include <cstdint> |
| 6 | #include <vector> |
| 7 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 8 | namespace host_tool |
| 9 | { |
| 10 | |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 11 | bool BtDataHandler::sendContents(const std::string& input, |
| 12 | std::uint16_t session) |
| 13 | { |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame^] | 14 | 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 Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 48 | } |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 49 | |
| 50 | } // namespace host_tool |