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