blob: 446e879ea4435ef9f2a44a746c3185c03c6a3ca3 [file] [log] [blame]
Tom Joseph15d88fb2016-08-05 03:30:04 -05001#pragma once
2
3#include <endian.h>
4#include <stdint.h>
5
6namespace endian
7{
8namespace details
9{
10template <typename T>
11struct 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 Mauery9e801a22018-10-12 13:20:49 -070019template <>
20struct convert<uint16_t>
Tom Joseph15d88fb2016-08-05 03:30:04 -050021{
Vernon Mauery9e801a22018-10-12 13:20:49 -070022 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 Joseph15d88fb2016-08-05 03:30:04 -050038};
39
Vernon Mauery9e801a22018-10-12 13:20:49 -070040template <>
41struct convert<uint32_t>
Tom Joseph15d88fb2016-08-05 03:30:04 -050042{
Vernon Mauery9e801a22018-10-12 13:20:49 -070043 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 Joseph15d88fb2016-08-05 03:30:04 -050059};
Vernon Mauery9e801a22018-10-12 13:20:49 -070060} // namespace details
Tom Joseph15d88fb2016-08-05 03:30:04 -050061
Vernon Mauery9e801a22018-10-12 13:20:49 -070062template <typename T>
63T to_ipmi(T i)
Tom Joseph15d88fb2016-08-05 03:30:04 -050064{
65 return details::convert<T>::to_ipmi(i);
66}
67
Vernon Mauery9e801a22018-10-12 13:20:49 -070068template <typename T>
69T from_ipmi(T i)
Tom Joseph15d88fb2016-08-05 03:30:04 -050070{
71 return details::convert<T>::from_ipmi(i);
72}
73
Vernon Mauery9e801a22018-10-12 13:20:49 -070074template <typename T>
75T to_network(T i)
Tom Joseph15d88fb2016-08-05 03:30:04 -050076{
77 return details::convert<T>::to_network(i);
78}
Vernon Mauery9e801a22018-10-12 13:20:49 -070079template <typename T>
80T from_network(T i)
Tom Joseph15d88fb2016-08-05 03:30:04 -050081{
82 return details::convert<T>::from_network(i);
83}
Vernon Mauery9e801a22018-10-12 13:20:49 -070084} // namespace endian