blob: 3444f9816af1a5224a45004eb141ebbca2ae8f4a [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#ifndef AOS_COMMON_BYTEORDER_H_
2#define AOS_COMMON_BYTEORDER_H_
3
4#ifndef __VXWORKS__
5#include <endian.h> // endian(3)
6#endif
7
8// Contains functions for converting between host and network byte order for
9// things other than 16/32 bit integers (those are provided by byteorder(3)).
10// Also gives a nice templated interface to these functions.
11
12namespace aos {
13
14#ifndef __VXWORKS__
15namespace {
16
17template<typename int_type, typename T, int_type (*function)(int_type)> static inline void copier(const void *in, void *out) {
18 static_assert(sizeof(T) == sizeof(int_type), "bad template args");
19 // Have confirmed by looking at the assembly code that this generates the same
20 // code as the (undefined by the c++ standard) way of using a union to do it.
21 // (which is pretty efficient)
22 int_type temp;
23 memcpy(&temp, in, sizeof(T));
24 temp = function(temp);
25 memcpy(out, &temp, sizeof(T));
26}
27template<typename int_type, typename T, int_type (*function)(int_type)> static inline T memcpier(T in) {
28 copier<int_type, T, function>(&in, &in);
29 return in;
30}
31
32// Can't make them static because they have to have external linkage to be used as a
33// template parameter.
34// Needed because be64toh etc are macros. (not that the manpage says anything...)
35// These are used instead of ntohs etc because gcc didn't inline those.
36inline uint64_t _be64toh(uint64_t in) { return be64toh(in); }
37inline uint64_t _htobe64(uint64_t in) { return htobe64(in); }
38inline uint32_t _be32toh(uint32_t in) { return be32toh(in); }
39inline uint32_t _htobe32(uint32_t in) { return htobe32(in); }
40inline uint16_t _be16toh(uint16_t in) { return be16toh(in); }
41inline uint16_t _htobe16(uint16_t in) { return htobe16(in); }
42
43template<int bytes, typename T> class do_ntoh {
44 public:
45 static inline T apply(T net);
46 static inline void copy(const char *in, T &net);
47};
48template<typename T> class do_ntoh<1, T> {
49 public:
50 static inline T apply(T net) { return net; }
51 static inline void copy(const char *in, T *host) { host[0] = in[0]; }
52};
53template<typename T> class do_ntoh<2, T> {
54 public:
55 static inline T apply(T net) { return memcpier<uint16_t, T, _be16toh>(net); }
56 static inline void copy(const char *in, T *host) { copier<uint16_t, T, _be16toh>(in, host); }
57};
58template<typename T> class do_ntoh<4, T> {
59 public:
60 static inline T apply(T net) { return memcpier<uint32_t, T, _be32toh>(net); }
61 static inline void copy(const char *in, T *host) { copier<uint32_t, T, _be32toh>(in, host); }
62};
63template<typename T> class do_ntoh<8, T> {
64 public:
65 static inline T apply(T net) { return memcpier<uint64_t, T, _be64toh>(net); }
66 static inline void copy(const char *in, T *host) { copier<uint64_t, T, _be64toh>(in, host); }
67};
68template<int bytes, typename T> class do_hton {
69 public:
70 static inline T apply(T host);
71 static inline void copy(const T *host, char *out);
72};
73template<typename T> class do_hton<1, T> {
74 public:
75 static inline T apply(T host) { return host; }
76 static inline void copy(const T *host, char *out) { out[0] = host[0]; }
77};
78template<typename T> class do_hton<2, T> {
79 public:
80 static inline T apply(T host) { return memcpier<uint16_t, T, _htobe16>(host); }
81 static inline void copy(const T *host, char *out) { copier<uint16_t, T, _htobe16>(host, out); }
82};
83template<typename T> class do_hton<4, T> {
84 public:
85 static inline T apply(T host) { return memcpier<uint32_t, T, _htobe32>(host); }
86 static inline void copy(const T *host, char *out) { copier<uint32_t, T, _htobe32>(host, out); }
87};
88template<typename T> class do_hton<8, T> {
89 public:
90 static inline T apply(T host) { return memcpier<uint64_t, T, _htobe64>(host); }
91 static inline void copy(const T *host, char *out) { copier<uint64_t, T, _htobe64>(host, out); }
92};
93
94} // namespace
95#endif // ifndef __VXWORKS__
96
97// Converts T from network to host byte order.
98template<typename T> static inline T ntoh(T net) {
99#ifndef __VXWORKS__
100 return do_ntoh<sizeof(net), T>::apply(net);
101#else
102 return net;
103#endif
104}
105// Converts T from host to network byte order.
106template<typename T> static inline T hton(T host) {
107#ifndef __VXWORKS__
108 return do_hton<sizeof(host), T>::apply(host);
109#else
110 return host;
111#endif
112}
113
114template<typename T> static inline void to_host(const char *input, T *host) {
115#ifndef __VXWORKS__
116 do_ntoh<sizeof(*host), T>::copy(input, host);
117#else
118 memcpy(host, input, sizeof(*host));
119#endif
120}
121template<typename T> static inline void to_network(const T *host, char *output) {
122#ifndef __VXWORKS__
123 do_hton<sizeof(*host), T>::copy(host, output);
124#else
125 memcpy(output, host, sizeof(*host));
126#endif
127}
128
129} // namespace aos
130
131#endif
132