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. |
Zane Shelley | 5a78fa8 | 2022-09-16 16:49:58 -0500 | [diff] [blame] | 74 | memcpy(o_buf, static_cast<const char*>(iv_buffer) + iv_currentIndex, |
| 75 | i_size); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 76 | |
| 77 | // Increment the curent index for the next piece of data. |
| 78 | iv_currentIndex += i_size; |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 79 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 80 | }; |
| 81 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 82 | /** @brief Template specialization for uint16_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 83 | template <> |
| 84 | inline ChipDataStream& ChipDataStream::operator>>(uint16_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 85 | { |
| 86 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 87 | o_right = be16toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 88 | return *this; |
| 89 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 90 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 91 | /** @brief Template specialization for int16_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 92 | template <> |
| 93 | inline ChipDataStream& ChipDataStream::operator>>(int16_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 94 | { |
| 95 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 96 | o_right = be16toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 97 | return *this; |
| 98 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 99 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 100 | /** @brief Template specialization for uint32_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 101 | template <> |
| 102 | inline ChipDataStream& ChipDataStream::operator>>(uint32_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 103 | { |
| 104 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 105 | o_right = be32toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 106 | return *this; |
| 107 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 108 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 109 | /** @brief Template specialization for int32_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 110 | template <> |
| 111 | inline ChipDataStream& ChipDataStream::operator>>(int32_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 112 | { |
| 113 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 114 | o_right = be32toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 115 | return *this; |
| 116 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 117 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 118 | /** @brief Template specialization for uint64_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 119 | template <> |
| 120 | inline ChipDataStream& ChipDataStream::operator>>(uint64_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 121 | { |
| 122 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 123 | o_right = be64toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 124 | return *this; |
| 125 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 126 | |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 127 | /** @brief Template specialization for int64_t. */ |
Zane Shelley | 7f7a42d | 2019-10-28 13:28:31 -0500 | [diff] [blame] | 128 | template <> |
| 129 | inline ChipDataStream& ChipDataStream::operator>>(int64_t& o_right) |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 130 | { |
| 131 | read(&o_right, sizeof(o_right)); |
Zane Shelley | b8b3cdf | 2020-05-04 13:28:04 -0500 | [diff] [blame] | 132 | o_right = be64toh(o_right); // Chip Data is big-endian |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 133 | return *this; |
| 134 | } |
Paul Greenwood | 31a5488 | 2019-08-01 17:05:09 -0500 | [diff] [blame] | 135 | |
Zane Shelley | b9a8e76 | 2020-05-11 21:41:32 -0500 | [diff] [blame] | 136 | /** @brief Template specialization for RegisterId_t. */ |
| 137 | template <> |
| 138 | inline ChipDataStream& ChipDataStream::operator>>(RegisterId_t& o_right) |
| 139 | { |
| 140 | // A register ID is only 3 bytes, but there isn't a 24-bit integer type. So |
| 141 | // extract 3 bytes to a uint32_t and drop the unused byte. |
| 142 | uint32_t tmp = 0; |
| 143 | read(&tmp, 3); |
| 144 | o_right = static_cast<RegisterId_t>(be32toh(tmp) >> 8); |
| 145 | return *this; |
| 146 | } |
| 147 | |
Zane Shelley | 83da245 | 2019-10-25 15:45:34 -0500 | [diff] [blame] | 148 | } // namespace libhei |