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 <cstdint> |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 4 | #include <ipmiblob/blob_errors.hpp> |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 5 | #include <vector> |
| 6 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 7 | namespace host_tool |
| 8 | { |
| 9 | |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 10 | bool BtDataHandler::sendContents(const std::string& input, |
| 11 | std::uint16_t session) |
| 12 | { |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 13 | int inputFd = sys->open(input.c_str(), 0); |
| 14 | if (inputFd < 0) |
| 15 | { |
| 16 | return false; |
| 17 | } |
| 18 | |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame^] | 19 | std::int64_t fileSize = sys->getSize(input.c_str()); |
| 20 | if (fileSize == 0) |
| 21 | { |
| 22 | std::fprintf(stderr, "Zero-length file, or other file access error\n"); |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | progress->start(fileSize); |
| 27 | |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 28 | try |
| 29 | { |
Patrick Venture | 6352804 | 2019-05-20 18:10:02 -0700 | [diff] [blame] | 30 | static constexpr int btBufferLen = 50; |
| 31 | std::uint8_t readBuffer[btBufferLen]; |
| 32 | int bytesRead; |
| 33 | std::uint32_t offset = 0; |
| 34 | |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 35 | do |
| 36 | { |
| 37 | bytesRead = sys->read(inputFd, readBuffer, sizeof(readBuffer)); |
| 38 | if (bytesRead > 0) |
| 39 | { |
| 40 | /* minorly awkward repackaging. */ |
| 41 | std::vector<std::uint8_t> buffer(&readBuffer[0], |
| 42 | &readBuffer[bytesRead]); |
| 43 | blob->writeBytes(session, offset, buffer); |
| 44 | offset += bytesRead; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame^] | 45 | progress->updateProgress(bytesRead); |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 46 | } |
| 47 | } while (bytesRead > 0); |
| 48 | } |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 49 | catch (const ipmiblob::BlobException& b) |
Patrick Venture | 907f3a7 | 2019-01-15 14:13:37 -0800 | [diff] [blame] | 50 | { |
| 51 | sys->close(inputFd); |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | sys->close(inputFd); |
| 56 | return true; |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 57 | } |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 58 | |
| 59 | } // namespace host_tool |