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