sbgECom Library  4.0.1987-stable
C library to interface SBG Systems IMU/AHRS/INS
sbgNetwork.h
Go to the documentation of this file.
1 
47 #ifndef SBG_NETWORK_H
48 #define SBG_NETWORK_H
49 
50 // sbgCommonLib headers
51 #include <sbgCommon.h>
52 
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 //----------------------------------------------------------------------//
59 //- Common IPv4 definitions -//
60 //----------------------------------------------------------------------//
61 #define SBG_IPV4_UNSPECIFIED_ADDR sbgIpAddr(0, 0, 0, 0)
62 #define SBG_IPV4_BROADCAST_ADDR sbgIpAddr(255, 255, 255, 255)
64 #define SBG_NETWORK_IPV4_STRING_SIZE (16)
66 //----------------------------------------------------------------------//
67 //- IP setters / getters -//
68 //----------------------------------------------------------------------//
69 
78 SBG_INLINE sbgIpAddress sbgIpAddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
79 {
80 #if SBG_CONFIG_BIG_ENDIAN == 1
81  return (a << 24) | (b << 16) | (c << 8) | d;
82 #else
83  return a | (b << 8) | (c << 16) | (d << 24);
84 #endif
85 }
86 
93 {
94 #if SBG_CONFIG_BIG_ENDIAN == 1
95  return (uint8_t)((ipAddr & 0xFF000000) >> 24);
96 #else
97  return (uint8_t)((ipAddr & 0x000000FF));
98 #endif
99 }
100 
107 {
108 #if SBG_CONFIG_BIG_ENDIAN == 1
109  return (uint8_t)((ipAddr & 0x00FF0000) >> 16);
110 #else
111  return (uint8_t)((ipAddr & 0x0000FF00) >> 8);
112 #endif
113 }
114 
121 {
122 #if SBG_CONFIG_BIG_ENDIAN == 1
123  return (uint8_t)((ipAddr & 0x0000FF00) >> 8);
124 #else
125  return (uint8_t)((ipAddr & 0x00FF0000) >> 16);
126 #endif
127 }
128 
135 {
136 #if SBG_CONFIG_BIG_ENDIAN == 1
137  return (uint8_t)((ipAddr & 0x000000FF));
138 #else
139  return (uint8_t)((ipAddr & 0xFF000000) >> 24);
140 #endif
141 }
142 
143 //----------------------------------------------------------------------//
144 //- IP manipulation methods -//
145 //----------------------------------------------------------------------//
146 
153 SBG_COMMON_LIB_API void sbgNetworkIpToString(sbgIpAddress ipAddr, char *pBuffer, size_t maxSize);
154 
161 
162 //----------------------------------------------------------------------//
163 //- IP operations -//
164 //----------------------------------------------------------------------//
165 
173 {
174  return (ipAddress & netmask);
175 }
176 
184 {
185  return (ipAddress & ~netmask);
186 }
187 
188 //----------------------------------------------------------------------//
189 //- IP validation methods -//
190 //----------------------------------------------------------------------//
191 
198 {
199  if (ipAddress == 0)
200  {
201  return true;
202  }
203  else
204  {
205  return false;
206  }
207 }
208 
215 {
216  //
217  // Check the if A part of the ip address is within 1 and 223
218  //
219  if ((sbgIpAddrGetA(ipAddress) > 0) && (sbgIpAddrGetA(ipAddress) < 224))
220  {
221  //
222  // The ip address is valid
223  //
224  return true;
225  }
226  else
227  {
228  //
229  // The ip address is not valid
230  //
231  return false;
232  }
233 }
234 
242 {
243  //
244  // Just check if the host part is equals to zero
245  //
246  if (sbgIpGetHostAddr(ipAddress, netmask) == 0)
247  {
248  return false;
249  }
250  else
251  {
252  return true;
253  }
254 }
255 
264 {
265  if ((firstIpAddr & netmask) == (secondIpAddr & netmask))
266  {
267  return true;
268  }
269  else
270  {
271  return false;
272  }
273 }
274 
281 
282 
283 #ifdef __cplusplus
284 }
285 #endif
286 
287 #endif // SBG_NETWORK_H
Main header for the SBG Systems common C library.
#define SBG_INLINE
Definition: sbgDefines.h:227
#define SBG_COMMON_LIB_API
Definition: sbgDefines.h:79
SBG_INLINE bool sbgIpAddressValid(sbgIpAddress ipAddress)
Definition: sbgNetwork.h:214
SBG_INLINE sbgIpAddress sbgIpGetNetworkAddr(sbgIpAddress ipAddress, sbgIpAddress netmask)
Definition: sbgNetwork.h:172
SBG_INLINE uint8_t sbgIpAddrGetA(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:92
SBG_COMMON_LIB_API sbgIpAddress sbgNetworkIpFromString(const char *pBuffer)
SBG_INLINE bool sbgIpAddrIsSameNetwork(sbgIpAddress firstIpAddr, sbgIpAddress secondIpAddr, sbgIpAddress netmask)
Definition: sbgNetwork.h:263
SBG_COMMON_LIB_API bool sbgIpNetMaskValid(sbgIpAddress netmask)
SBG_COMMON_LIB_API void sbgNetworkIpToString(sbgIpAddress ipAddr, char *pBuffer, size_t maxSize)
SBG_INLINE sbgIpAddress sbgIpAddrWithinSubnet(sbgIpAddress ipAddress, sbgIpAddress netmask)
Definition: sbgNetwork.h:241
SBG_INLINE uint8_t sbgIpAddrGetB(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:106
SBG_INLINE uint8_t sbgIpAddrGetC(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:120
SBG_INLINE bool sbgIpAddressIsUnspecified(sbgIpAddress ipAddress)
Definition: sbgNetwork.h:197
SBG_INLINE sbgIpAddress sbgIpAddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
Definition: sbgNetwork.h:78
SBG_INLINE uint8_t sbgIpAddrGetD(sbgIpAddress ipAddr)
Definition: sbgNetwork.h:134
SBG_INLINE sbgIpAddress sbgIpGetHostAddr(sbgIpAddress ipAddress, sbgIpAddress netmask)
Definition: sbgNetwork.h:183
uint32_t sbgIpAddress
Definition: sbgTypes.h:66