Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 3 | #include <endian.h> |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 4 | #include <string.h> |
| 5 | |
Zane Shelley | d507351 | 2021-01-14 12:51:18 -0600 | [diff] [blame^] | 6 | #include <util/hei_includes.hpp> |
Zane Shelley | ca9f625 | 2019-10-25 21:17:30 -0500 | [diff] [blame] | 7 | |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 8 | namespace libhei |
| 9 | { |
| 10 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 11 | /** @brief A streaming utility to read a Chip Data File buffer. */ |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 12 | class ChipDataStream |
| 13 | { |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 14 | public: |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 15 | /** |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 16 | * @brief Constructor. |
| 17 | * @param i_buffer A pointer to the buffer. |
| 18 | * @param i_bufferSize The buffer size. |
| 19 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 20 | ChipDataStream(void* i_buffer, size_t i_bufferSize) : |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 21 | iv_buffer(i_buffer), iv_bufferSize(i_bufferSize) |
| 22 | { |
| 23 | HEI_ASSERT(nullptr != i_buffer); |
| 24 | HEI_ASSERT(0 < i_bufferSize); |
| 25 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 26 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 27 | /** @brief Destructor. */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 28 | ~ChipDataStream() = default; |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 29 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 30 | /** @brief Copy constructor. */ |
| 31 | ChipDataStream(const ChipDataStream&) = delete; |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 32 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 33 | /** @brief Assignment operator. */ |
| 34 | ChipDataStream& operator=(const ChipDataStream&) = delete; |
| 35 | |
| 36 | private: |
| 37 | /** Pointer to the first address of the Chip Data File buffer. */ |
| 38 | const void* const iv_buffer; |
| 39 | |
| 40 | /** The Chip Data File buffer size. */ |
| 41 | const size_t iv_bufferSize; |
| 42 | |
| 43 | /** Current byte index within the buffer. */ |
| 44 | size_t iv_currentIndex = 0; |
| 45 | |
| 46 | public: |
| 47 | /** @brief Output stream operator. */ |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 48 | template <class D> |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 49 | ChipDataStream& operator>>(D& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 50 | { |
| 51 | read(&o_right, sizeof(D)); |
| 52 | return *this; |
| 53 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 54 | |
Zane Shelley | 0165edc | 2020-05-11 22:28:29 -0500 | [diff] [blame] | 55 | /** @return True, if the stream is currently at the end of the file. */ |
| 56 | bool eof() |
| 57 | { |
| 58 | return iv_currentIndex == iv_bufferSize; |
| 59 | } |
| 60 | |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 61 | private: |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 62 | /** |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 63 | * @brief Copies the given number of data bytes into the buffer from the |
| 64 | * current index and then increments the index. |
| 65 | * @param o_buf The output buffer. |
| 66 | * @param i_size The number of bytes to copy. |
| 67 | */ |
Zane Shelley | fe27b65 | 2019-10-28 11:33:07 -0500 | [diff] [blame] | 68 | void read(void* o_buf, size_t i_size) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 69 | { |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 70 | // Check for buffer overflow. |
| 71 | HEI_ASSERT((iv_currentIndex + i_size) <= iv_bufferSize); |
| 72 | |
| 73 | // Copy the buffer. |
| 74 | memcpy(o_buf, (char*)iv_buffer + iv_currentIndex, i_size); |
| 75 | |
| 76 | // Increment the curent index for the next piece of data. |
| 77 | iv_currentIndex += i_size; |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 78 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 79 | }; |
| 80 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 81 | /** @brief Template specialization for uint16_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 82 | template <> |
| 83 | inline ChipDataStream& ChipDataStream::operator>>(uint16_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 84 | { |
| 85 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 86 | o_right = be16toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 87 | return *this; |
| 88 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 89 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 90 | /** @brief Template specialization for int16_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 91 | template <> |
| 92 | inline ChipDataStream& ChipDataStream::operator>>(int16_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 93 | { |
| 94 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 95 | o_right = be16toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 96 | return *this; |
| 97 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 98 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 99 | /** @brief Template specialization for uint32_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 100 | template <> |
| 101 | inline ChipDataStream& ChipDataStream::operator>>(uint32_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 102 | { |
| 103 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 104 | o_right = be32toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 105 | return *this; |
| 106 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 107 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 108 | /** @brief Template specialization for int32_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 109 | template <> |
| 110 | inline ChipDataStream& ChipDataStream::operator>>(int32_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 111 | { |
| 112 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 113 | o_right = be32toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 114 | return *this; |
| 115 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 116 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 117 | /** @brief Template specialization for uint64_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 118 | template <> |
| 119 | inline ChipDataStream& ChipDataStream::operator>>(uint64_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 120 | { |
| 121 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 122 | o_right = be64toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 123 | return *this; |
| 124 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 125 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 126 | /** @brief Template specialization for int64_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 127 | template <> |
| 128 | inline ChipDataStream& ChipDataStream::operator>>(int64_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 129 | { |
| 130 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 131 | o_right = be64toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 132 | return *this; |
| 133 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 134 | |
Zane Shelley | b9a8e76 | 2020-05-11 21:41:32 -0500 | [diff] [blame] | 135 | /** @brief Template specialization for RegisterId_t. */ |
| 136 | template <> |
| 137 | inline ChipDataStream& ChipDataStream::operator>>(RegisterId_t& o_right) |
| 138 | { |
| 139 | // A register ID is only 3 bytes, but there isn't a 24-bit integer type. So |
| 140 | // extract 3 bytes to a uint32_t and drop the unused byte. |
| 141 | uint32_t tmp = 0; |
| 142 | read(&tmp, 3); |
| 143 | o_right = static_cast<RegisterId_t>(be32toh(tmp) >> 8); |
| 144 | return *this; |
| 145 | } |
| 146 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 147 | } // namespace libhei |