1 /**
2 ****************************************************************************************
3 *
4 * @file co_endian.h
5 *
6 * @brief Common endianness conversion functions
7 *
8 * Copyright (C) RivieraWaves 2009-2015
9 *
10 *
11 ****************************************************************************************
12 */
13
14 #ifndef _CO_ENDIAN_H_
15 #define _CO_ENDIAN_H_
16
17 #include <stdint.h> // standard integer definitions
18 #include "rwip_config.h" // stack configuration
19 #include "ble_arch.h"
20
21 /**
22 ****************************************************************************************
23 * @defgroup CO_ENDIAN Endianness
24 * @ingroup COMMON
25 * @brief Endianness conversion functions.
26 *
27 * This set of functions converts values between the local system
28 * and a external one. It is inspired from the <tt>htonl</tt>-like functions
29 * from the standard C library.
30 *
31 * Example:
32 * @code
33 * struct eth_header *header = get_header(); // get pointer on Eth II packet header
34 * uint16_t eth_id; // will contain the type of the packet
35 * eth_id = co_ntohs(header->eth_id); // retrieve the type with correct endianness
36 * @endcode
37 *
38 * @{
39 * ****************************************************************************************
40 * */
41
42
43 /**
44 ****************************************************************************************
45 * @brief Swap bytes of a 32 bits value.
46 * The swap is done in every case. Should not be called directly.
47 * @param[in] val32 The 32 bits value to swap.
48 * @return The 32 bit swapped value.
49 ****************************************************************************************
50 */
co_bswap32(uint32_t val32)51 __INLINE uint32_t co_bswap32(uint32_t val32)
52 {
53 return (val32<<24) | ((val32<<8)&0xFF0000) | ((val32>>8)&0xFF00) | ((val32>>24)&0xFF);
54 }
55
56 /**
57 ****************************************************************************************
58 * @brief Swap bytes of a 16 bits value.
59 * The swap is done in every case. Should not be called directly.
60 * @param[in] val16 The 16 bit value to swap.
61 * @return The 16 bit swapped value.
62 ****************************************************************************************
63 */
co_bswap16(uint16_t val16)64 __INLINE uint16_t co_bswap16(uint16_t val16)
65 {
66 return ((val16<<8)&0xFF00) | ((val16>>8)&0xFF);
67 }
68 /// @} CO_ENDIAN
69
70
71
72
73 /**
74 * ****************************************************************************************
75 * @defgroup CO_ENDIAN_NET Endianness (Network)
76 * @ingroup CO_ENDIAN
77 * @brief Endianness conversion functions for Network data
78 *
79 * Converts values between the local system and big-endian network data
80 * (e.g. IP, Ethernet, but NOT WLAN).
81 *
82 * The \b host term in the descriptions of these functions refers
83 * to the local system, i.e. \b application or \b embedded system.
84 * Therefore, these functions will behave differently depending on which
85 * side they are used. The reason of this terminology is to keep the
86 * same name than the standard C function.
87 *
88 * Behavior will depends on the endianness of the host:
89 * - little endian: swap bytes;
90 * - big endian: identity function.
91 *
92 * @{
93 * ****************************************************************************************
94 * */
95
96 /**
97 ****************************************************************************************
98 * @brief Convert host to network long word.
99 *
100 * @param[in] hostlong Long word value to convert.
101 *
102 * @return The converted long word.
103 ****************************************************************************************
104 */
co_htonl(uint32_t hostlong)105 __INLINE uint32_t co_htonl(uint32_t hostlong)
106 {
107 #if (!CPU_LE)
108 return hostlong;
109 #else
110 return co_bswap32(hostlong);
111 #endif // CPU_LE
112 }
113
114 /**
115 ****************************************************************************************
116 * @brief Convert host to network short word.
117 *
118 * @param[in] hostshort Short word value to convert.
119 *
120 * @return The converted short word.
121 ****************************************************************************************
122 */
co_htons(uint16_t hostshort)123 __INLINE uint16_t co_htons(uint16_t hostshort)
124 {
125 #if (!CPU_LE)
126 return hostshort;
127 #else
128 return co_bswap16(hostshort);
129 #endif // CPU_LE
130 }
131
132 /**
133 ****************************************************************************************
134 * @brief Convert network to host long word.
135 *
136 * @param[in] netlong Long word value to convert.
137 *
138 * @return The converted long word.
139 ****************************************************************************************
140 */
co_ntohl(uint32_t netlong)141 __INLINE uint32_t co_ntohl(uint32_t netlong)
142 {
143 return co_htonl(netlong);
144 }
145
146 /**
147 ****************************************************************************************
148 * @brief Convert network to host short word.
149 *
150 * @param[in] netshort Short word value to convert.
151 *
152 * @return The converted short word.
153 ****************************************************************************************
154 */
co_ntohs(uint16_t netshort)155 __INLINE uint16_t co_ntohs(uint16_t netshort)
156 {
157 return co_htons(netshort);
158 }
159 /// @} CO_ENDIAN_NET
160
161 /**
162 * ****************************************************************************************
163 * @defgroup CO_ENDIAN_BT Endianness (BT)
164 * @ingroup CO_ENDIAN
165 * @brief Endianness conversion functions for Bluetooth data (HCI and protocol)
166 *
167 * Converts values between the local system and little-endian Bluetooth data.
168 *
169 * The \b host term in the descriptions of these functions refers
170 * to the local system (check \ref CO_ENDIAN_NET "this comment").
171 *
172 * Behavior will depends on the endianness of the host:
173 * - little endian: identity function;
174 * - big endian: swap bytes.
175 *
176 * @addtogroup CO_ENDIAN_BT
177 * @{
178 * ****************************************************************************************
179 * */
180
181 /**
182 ****************************************************************************************
183 * @brief Convert host to Bluetooth long word.
184 *
185 * @param[in] hostlong Long word value to convert.
186 *
187 * @return The converted long word.
188 ****************************************************************************************
189 */
co_htobl(uint32_t hostlong)190 __INLINE uint32_t co_htobl(uint32_t hostlong)
191 {
192 #if (CPU_LE)
193 return hostlong;
194 #else
195 return co_bswap32(hostlong);
196 #endif // CPU_LE
197 }
198
199 /**
200 ****************************************************************************************
201 * @brief Convert host to Bluetooth short word.
202 *
203 * @param[in] hostshort Short word value to convert.
204 *
205 * @return The converted short word.
206 ****************************************************************************************
207 */
co_htobs(uint16_t hostshort)208 __INLINE uint16_t co_htobs(uint16_t hostshort)
209 {
210 #if (CPU_LE)
211 return hostshort;
212 #else
213 return co_bswap16(hostshort);
214 #endif // CPU_LE
215 }
216
217
218 /**
219 ****************************************************************************************
220 * @brief Convert Bluetooth to host long word.
221 *
222 * @param[in] btlong Long word value to convert.
223 *
224 * @return The converted long word.
225 ****************************************************************************************
226 */
co_btohl(uint32_t btlong)227 __INLINE uint32_t co_btohl(uint32_t btlong)
228 {
229 return co_htobl(btlong);
230 }
231
232
233 /**
234 ****************************************************************************************
235 * @brief Convert Bluetooth to host short word.
236 *
237 * @param[in] btshort Short word value to convert.
238 *
239 * @return The converted short word.
240 ****************************************************************************************
241 */
co_btohs(uint16_t btshort)242 __INLINE uint16_t co_btohs(uint16_t btshort)
243 {
244 return co_htobs(btshort);
245 }
246 /// @} CO_ENDIAN
247
248 #endif // _CO_ENDIAN_H_
249