Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2018 Intel Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
| 18 | #include <array> |
| 19 | #include <ipmid/message/types.hpp> |
| 20 | #include <memory> |
Vernon Mauery | bae9135 | 2019-04-03 12:11:08 -0700 | [diff] [blame] | 21 | #include <optional> |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 22 | #include <phosphor-logging/log.hpp> |
| 23 | #include <tuple> |
| 24 | #include <utility> |
Alexander Amelkin | 00b096d | 2019-04-15 15:11:03 +0300 | [diff] [blame] | 25 | #include <variant> |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 26 | #include <vector> |
| 27 | |
| 28 | namespace ipmi |
| 29 | { |
| 30 | |
| 31 | namespace message |
| 32 | { |
| 33 | |
| 34 | namespace details |
| 35 | { |
| 36 | |
| 37 | /************************************** |
| 38 | * ipmi return type helpers |
| 39 | **************************************/ |
| 40 | |
| 41 | template <typename NumericType, size_t byteIndex = 0> |
| 42 | void PackBytes(uint8_t* pointer, const NumericType& i) |
| 43 | { |
| 44 | if constexpr (byteIndex < sizeof(NumericType)) |
| 45 | { |
| 46 | *pointer = static_cast<uint8_t>(i >> (8 * byteIndex)); |
| 47 | PackBytes<NumericType, byteIndex + 1>(pointer + 1, i); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | template <typename NumericType, size_t byteIndex = 0> |
| 52 | void PackBytesUnaligned(Payload& p, const NumericType& i) |
| 53 | { |
| 54 | if constexpr (byteIndex < sizeof(NumericType)) |
| 55 | { |
| 56 | p.appendBits(CHAR_BIT, static_cast<uint8_t>(i >> (8 * byteIndex))); |
| 57 | PackBytesUnaligned<NumericType, byteIndex + 1>(p, i); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** @struct PackSingle |
| 62 | * @brief Utility to pack a single C++ element into a Payload |
| 63 | * |
| 64 | * User-defined types are expected to specialize this template in order to |
| 65 | * get their functionality. |
| 66 | * |
| 67 | * @tparam S - Type of element to pack. |
| 68 | */ |
| 69 | template <typename T> |
| 70 | struct PackSingle |
| 71 | { |
| 72 | /** @brief Do the operation to pack element. |
| 73 | * |
| 74 | * @param[in] p - Payload to pack into. |
| 75 | * @param[out] t - The reference to pack item into. |
| 76 | */ |
James Feist | f299807 | 2019-04-03 11:54:52 -0700 | [diff] [blame] | 77 | static int op(Payload& p, const T& t) |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 78 | { |
Vernon Mauery | 336405b | 2019-04-08 09:59:01 -0700 | [diff] [blame] | 79 | static_assert(std::is_integral_v<T>, |
| 80 | "Attempt to pack a type that has no IPMI pack operation"); |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 81 | // if not on a byte boundary, must pack values LSbit/LSByte first |
| 82 | if (p.bitCount) |
| 83 | { |
| 84 | PackBytesUnaligned<T>(p, t); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | // copy in bits to vector.... |
| 89 | p.raw.resize(p.raw.size() + sizeof(T)); |
| 90 | uint8_t* out = p.raw.data() + p.raw.size() - sizeof(T); |
| 91 | PackBytes<T>(out, t); |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | }; |
| 96 | |
Vernon Mauery | 508c457 | 2019-04-08 10:01:33 -0700 | [diff] [blame] | 97 | /** @brief Specialization of PackSingle for std::tuple<T> */ |
| 98 | template <typename... T> |
| 99 | struct PackSingle<std::tuple<T...>> |
| 100 | { |
| 101 | static int op(Payload& p, const std::tuple<T...>& v) |
| 102 | { |
| 103 | return std::apply([&p](const T&... args) { return p.pack(args...); }, |
| 104 | v); |
| 105 | } |
| 106 | }; |
| 107 | |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 108 | /** @brief Specialization of PackSingle for std::string |
| 109 | * represented as a UCSD-Pascal style string |
| 110 | */ |
| 111 | template <> |
| 112 | struct PackSingle<std::string> |
| 113 | { |
James Feist | f299807 | 2019-04-03 11:54:52 -0700 | [diff] [blame] | 114 | static int op(Payload& p, const std::string& t) |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 115 | { |
| 116 | // check length first |
| 117 | uint8_t len; |
| 118 | if (t.length() > std::numeric_limits<decltype(len)>::max()) |
| 119 | { |
| 120 | using namespace phosphor::logging; |
| 121 | log<level::ERR>("long string truncated on IPMI message pack"); |
| 122 | return 1; |
| 123 | } |
| 124 | len = static_cast<uint8_t>(t.length()); |
| 125 | PackSingle<uint8_t>::op(p, len); |
| 126 | p.append(t.c_str(), t.c_str() + t.length()); |
| 127 | return 0; |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | /** @brief Specialization of PackSingle for fixed_uint_t types |
| 132 | */ |
| 133 | template <unsigned N> |
| 134 | struct PackSingle<fixed_uint_t<N>> |
| 135 | { |
James Feist | f299807 | 2019-04-03 11:54:52 -0700 | [diff] [blame] | 136 | static int op(Payload& p, const fixed_uint_t<N>& t) |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 137 | { |
| 138 | size_t count = N; |
| 139 | static_assert(N <= (details::bitStreamSize - CHAR_BIT)); |
| 140 | uint64_t bits = t; |
| 141 | while (count > 0) |
| 142 | { |
| 143 | size_t appendCount = std::min(count, static_cast<size_t>(CHAR_BIT)); |
| 144 | p.appendBits(appendCount, static_cast<uint8_t>(bits)); |
| 145 | bits >>= CHAR_BIT; |
| 146 | count -= appendCount; |
| 147 | } |
| 148 | return 0; |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | /** @brief Specialization of PackSingle for bool. */ |
| 153 | template <> |
| 154 | struct PackSingle<bool> |
| 155 | { |
James Feist | f299807 | 2019-04-03 11:54:52 -0700 | [diff] [blame] | 156 | static int op(Payload& p, const bool& b) |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 157 | { |
| 158 | p.appendBits(1, b); |
| 159 | return 0; |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | /** @brief Specialization of PackSingle for std::bitset<N> */ |
| 164 | template <size_t N> |
| 165 | struct PackSingle<std::bitset<N>> |
| 166 | { |
James Feist | f299807 | 2019-04-03 11:54:52 -0700 | [diff] [blame] | 167 | static int op(Payload& p, const std::bitset<N>& t) |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 168 | { |
| 169 | size_t count = N; |
| 170 | static_assert(N <= (details::bitStreamSize - CHAR_BIT)); |
| 171 | unsigned long long bits = t.to_ullong(); |
| 172 | while (count > 0) |
| 173 | { |
| 174 | size_t appendCount = std::min(count, size_t(CHAR_BIT)); |
| 175 | p.appendBits(appendCount, static_cast<uint8_t>(bits)); |
| 176 | bits >>= CHAR_BIT; |
| 177 | count -= appendCount; |
| 178 | } |
| 179 | return 0; |
| 180 | } |
| 181 | }; |
| 182 | |
Vernon Mauery | bae9135 | 2019-04-03 12:11:08 -0700 | [diff] [blame] | 183 | /** @brief Specialization of PackSingle for std::optional<T> */ |
| 184 | template <typename T> |
| 185 | struct PackSingle<std::optional<T>> |
| 186 | { |
| 187 | static int op(Payload& p, const std::optional<T>& t) |
| 188 | { |
| 189 | int ret = 0; |
| 190 | if (t) |
| 191 | { |
| 192 | ret = PackSingle<T>::op(p, *t); |
| 193 | } |
| 194 | return ret; |
| 195 | } |
| 196 | }; |
| 197 | |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 198 | /** @brief Specialization of PackSingle for std::array<T, N> */ |
| 199 | template <typename T, size_t N> |
| 200 | struct PackSingle<std::array<T, N>> |
| 201 | { |
James Feist | f299807 | 2019-04-03 11:54:52 -0700 | [diff] [blame] | 202 | static int op(Payload& p, const std::array<T, N>& t) |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 203 | { |
| 204 | int ret = 0; |
James Feist | f299807 | 2019-04-03 11:54:52 -0700 | [diff] [blame] | 205 | for (const auto& v : t) |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 206 | { |
| 207 | int ret = PackSingle<T>::op(p, v); |
| 208 | if (ret) |
| 209 | { |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | return ret; |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | /** @brief Specialization of PackSingle for std::vector<T> */ |
| 218 | template <typename T> |
| 219 | struct PackSingle<std::vector<T>> |
| 220 | { |
James Feist | f299807 | 2019-04-03 11:54:52 -0700 | [diff] [blame] | 221 | static int op(Payload& p, const std::vector<T>& t) |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 222 | { |
| 223 | int ret = 0; |
James Feist | f299807 | 2019-04-03 11:54:52 -0700 | [diff] [blame] | 224 | for (const auto& v : t) |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 225 | { |
| 226 | int ret = PackSingle<T>::op(p, v); |
| 227 | if (ret) |
| 228 | { |
| 229 | break; |
| 230 | } |
| 231 | } |
| 232 | return ret; |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | /** @brief Specialization of PackSingle for std::vector<uint8_t> */ |
| 237 | template <> |
| 238 | struct PackSingle<std::vector<uint8_t>> |
| 239 | { |
James Feist | f299807 | 2019-04-03 11:54:52 -0700 | [diff] [blame] | 240 | static int op(Payload& p, const std::vector<uint8_t>& t) |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 241 | { |
William A. Kennington III | 906e0f8 | 2019-04-24 14:44:03 -0700 | [diff] [blame^] | 242 | if (p.bitCount != 0) |
| 243 | { |
| 244 | return 1; |
| 245 | } |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 246 | p.raw.reserve(p.raw.size() + t.size()); |
| 247 | p.raw.insert(p.raw.end(), t.begin(), t.end()); |
| 248 | return 0; |
| 249 | } |
| 250 | }; |
| 251 | |
James Feist | f299807 | 2019-04-03 11:54:52 -0700 | [diff] [blame] | 252 | /** @brief Specialization of PackSingle for std::variant<T, N> */ |
| 253 | template <typename... T> |
| 254 | struct PackSingle<std::variant<T...>> |
| 255 | { |
| 256 | static int op(Payload& p, const std::variant<T...>& v) |
| 257 | { |
| 258 | return std::visit( |
| 259 | [&p](const auto& arg) { |
| 260 | return PackSingle<std::decay_t<decltype(arg)>>::op(p, arg); |
| 261 | }, |
| 262 | v); |
| 263 | } |
| 264 | }; |
| 265 | |
Vernon Mauery | e7329c7 | 2018-10-08 12:05:16 -0700 | [diff] [blame] | 266 | } // namespace details |
| 267 | |
| 268 | } // namespace message |
| 269 | |
| 270 | } // namespace ipmi |