Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef NETWORK_INTERFACE_NETWORK_UTILS_H
00009 #define NETWORK_INTERFACE_NETWORK_UTILS_H
00010
00011 #include <cstddef>
00012 #include <cstdint>
00013 #include <vector>
00014 #include <typeinfo>
00015 #include <cstring>
00016
00017 namespace AS
00018 {
00019 namespace Network
00020 {
00021 enum ByteOrder
00022 {
00023 BE = 0,
00024 LE
00025 };
00026
00027 inline bool system_is_big_endian()
00028 {
00029 union
00030 {
00031 uint32_t i;
00032 char c[4];
00033 } big_int = {0x12345678};
00034
00035 return big_int.c[0] == 1;
00036 }
00037
00038
00039 template<typename T>
00040 T read_le(uint8_t* bufArray,
00041 const uint32_t& size,
00042 const uint32_t& offset,
00043 const float& factor,
00044 const uint32_t& valueOffset)
00045 {
00046 uint64_t rcvData = 0;
00047
00048 for (uint32_t i = size; i > 0; i--)
00049 {
00050 rcvData <<= 8;
00051
00052
00053 rcvData |= bufArray[(offset - 1) + i];
00054 }
00055
00056 T retVal = 0;
00057 std::memcpy(&retVal, (system_is_big_endian()) ? &rcvData + sizeof(uint64_t) - sizeof(T) : &rcvData, sizeof(T));
00058 retVal *= (T) factor;
00059 retVal += valueOffset;
00060
00061 return retVal;
00062 };
00063
00064 template<typename T>
00065 T read_le(unsigned char* bufArray,
00066 const unsigned int& size,
00067 const unsigned int& offset)
00068 {
00069 return read_le<T>(bufArray, size, offset, 1.0, 0);
00070 };
00071
00072 template<typename T>
00073 std::vector<uint8_t> write_le(T *source)
00074 {
00075 std::vector<uint8_t> ret_val;
00076
00077 if (sizeof(source))
00078 return ret_val;
00079
00080 if (typeid(source) == typeid(float) || typeid(source) == typeid(double) || typeid(source) == typeid(long double))
00081 return ret_val;
00082
00083 T mask = 0xFF;
00084
00085 while ((*source & mask) > 0)
00086 {
00087 ret_val.push_back(uint8_t(*source & mask));
00088 mask <<= 8;
00089 }
00090
00091 return ret_val;
00092 };
00093
00094
00095 template<typename T>
00096 T read_be(unsigned char* bufArray,
00097 const unsigned int& size,
00098 const unsigned int& offset,
00099 const float& factor,
00100 const unsigned int& valueOffset)
00101 {
00102 uint64_t rcvData = 0;
00103
00104 for (unsigned int i = 0; i < size; i++)
00105 {
00106 rcvData <<= 8;
00107 rcvData |= bufArray[(offset) + i];
00108 }
00109
00110 T retVal;
00111 std::memcpy(&retVal, (system_is_big_endian()) ? &rcvData + sizeof(uint64_t) - sizeof(T) : &rcvData, sizeof(T));
00112 retVal *= (T) factor;
00113 retVal += valueOffset;
00114
00115 return retVal;
00116 };
00117
00118 template<typename T>
00119 T read_be(unsigned char* bufArray,
00120 const unsigned int& size,
00121 const unsigned int& offset)
00122 {
00123 return read_be<T>(bufArray, size, offset, 1.0, 0);
00124 }
00125
00126 template<typename T>
00127 std::vector<uint8_t> write_be(T *source)
00128 {
00129 std::vector<uint8_t> ret_val;
00130
00131 if (typeid(source) == typeid(float) || typeid(source) == typeid(double) || typeid(source) == typeid(long double))
00132 {
00133 return ret_val;
00134 }
00135
00136 T mask = 0xFF;
00137
00138 int shift = 8 * (sizeof(T) - 1);
00139 mask <<= shift;
00140
00141 while (mask > 0)
00142 {
00143
00144 ret_val.push_back(uint8_t(((*source) & mask) >> shift));
00145 shift -= 8;
00146 mask >>= 8;
00147 }
00148
00149 return ret_val;
00150 };
00151
00152 inline int32_t find_magic_word(uint8_t *in, uint32_t buf_size, size_t magic_word)
00153 {
00154 bool packet_found = false;
00155 uint32_t i = 0;
00156 uint32_t chunk;
00157 const uint32_t chunk_bytes = 4;
00158
00159 while (!packet_found && buf_size >= chunk_bytes)
00160 {
00161 chunk = read_be<uint32_t>(in, chunk_bytes, i);
00162
00163 if (chunk == magic_word)
00164 {
00165 packet_found = true;
00166 return i;
00167 }
00168
00169 i++;
00170 buf_size--;
00171 }
00172
00173
00174 return -1;
00175 };
00176 }
00177 }
00178
00179 #endif // NETWORK_INTERFACE_NETWORK_UTILS_H