Tom Joseph | 15d88fb | 2016-08-05 03:30:04 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <endian.h> |
| 4 | #include <stdint.h> |
| 5 | |
| 6 | namespace endian |
| 7 | { |
| 8 | namespace details |
| 9 | { |
| 10 | template <typename T> |
| 11 | struct convert |
| 12 | { |
| 13 | static T to_ipmi(T) = delete; |
| 14 | static T from_ipmi(T) = delete; |
| 15 | static T to_network(T) = delete; |
| 16 | static T from_network(T) = delete; |
| 17 | }; |
| 18 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 19 | template <> |
| 20 | struct convert<uint16_t> |
Tom Joseph | 15d88fb | 2016-08-05 03:30:04 -0500 | [diff] [blame] | 21 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 22 | static uint16_t to_ipmi(uint16_t i) |
| 23 | { |
| 24 | return htole16(i); |
| 25 | }; |
| 26 | static uint16_t from_ipmi(uint16_t i) |
| 27 | { |
| 28 | return le16toh(i); |
| 29 | }; |
| 30 | static uint16_t to_network(uint16_t i) |
| 31 | { |
| 32 | return htobe16(i); |
| 33 | }; |
| 34 | static uint16_t from_network(uint16_t i) |
| 35 | { |
| 36 | return be16toh(i); |
| 37 | }; |
Tom Joseph | 15d88fb | 2016-08-05 03:30:04 -0500 | [diff] [blame] | 38 | }; |
| 39 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 40 | template <> |
| 41 | struct convert<uint32_t> |
Tom Joseph | 15d88fb | 2016-08-05 03:30:04 -0500 | [diff] [blame] | 42 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 43 | static uint32_t to_ipmi(uint32_t i) |
| 44 | { |
| 45 | return htole32(i); |
| 46 | }; |
| 47 | static uint32_t from_ipmi(uint32_t i) |
| 48 | { |
| 49 | return le32toh(i); |
| 50 | }; |
| 51 | static uint32_t to_network(uint32_t i) |
| 52 | { |
| 53 | return htobe32(i); |
| 54 | }; |
| 55 | static uint32_t from_network(uint32_t i) |
| 56 | { |
| 57 | return be32toh(i); |
| 58 | }; |
Tom Joseph | 15d88fb | 2016-08-05 03:30:04 -0500 | [diff] [blame] | 59 | }; |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 60 | } // namespace details |
Tom Joseph | 15d88fb | 2016-08-05 03:30:04 -0500 | [diff] [blame] | 61 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 62 | template <typename T> |
| 63 | T to_ipmi(T i) |
Tom Joseph | 15d88fb | 2016-08-05 03:30:04 -0500 | [diff] [blame] | 64 | { |
| 65 | return details::convert<T>::to_ipmi(i); |
| 66 | } |
| 67 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 68 | template <typename T> |
| 69 | T from_ipmi(T i) |
Tom Joseph | 15d88fb | 2016-08-05 03:30:04 -0500 | [diff] [blame] | 70 | { |
| 71 | return details::convert<T>::from_ipmi(i); |
| 72 | } |
| 73 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 74 | template <typename T> |
| 75 | T to_network(T i) |
Tom Joseph | 15d88fb | 2016-08-05 03:30:04 -0500 | [diff] [blame] | 76 | { |
| 77 | return details::convert<T>::to_network(i); |
| 78 | } |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 79 | template <typename T> |
| 80 | T from_network(T i) |
Tom Joseph | 15d88fb | 2016-08-05 03:30:04 -0500 | [diff] [blame] | 81 | { |
| 82 | return details::convert<T>::from_network(i); |
| 83 | } |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 84 | } // namespace endian |