]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Source/FreeRTOS-Plus-TCP/include/FreeRTOS_IP.h
6bfb4ad423fab695f959f3a4bfa30251310a9a6d
[freertos] / FreeRTOS-Labs / Source / FreeRTOS-Plus-TCP / include / FreeRTOS_IP.h
1 /*\r
2  * FreeRTOS+TCP V2.2.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://aws.amazon.com/freertos\r
23  * http://www.FreeRTOS.org\r
24  */\r
25 \r
26 #ifndef FREERTOS_IP_H\r
27 #define FREERTOS_IP_H\r
28 \r
29 #ifdef __cplusplus\r
30 extern "C" {\r
31 #endif\r
32 \r
33 /* Application level configuration options. */\r
34 #include "FreeRTOSIPConfig.h"\r
35 #include "FreeRTOSIPConfigDefaults.h"\r
36 #include "IPTraceMacroDefaults.h"\r
37 \r
38 /* Some constants defining the sizes of several parts of a packet */\r
39 #define ipSIZE_OF_ETH_HEADER                    14u\r
40 #define ipSIZE_OF_IPv4_HEADER                   20u\r
41 #define ipSIZE_OF_IGMP_HEADER                   8u\r
42 #define ipSIZE_OF_ICMP_HEADER                   8u\r
43 #define ipSIZE_OF_UDP_HEADER                    8u\r
44 #define ipSIZE_OF_TCP_HEADER                    20u\r
45 \r
46 \r
47 /* The number of octets in the MAC and IP addresses respectively. */\r
48 #define ipMAC_ADDRESS_LENGTH_BYTES ( 6 )\r
49 #define ipIP_ADDRESS_LENGTH_BYTES ( 4 )\r
50 \r
51 /* IP protocol definitions. */\r
52 #define ipPROTOCOL_ICMP                 ( 1 )\r
53 #define ipPROTOCOL_IGMP         ( 2 )\r
54 #define ipPROTOCOL_TCP                  ( 6 )\r
55 #define ipPROTOCOL_UDP                  ( 17 )\r
56 \r
57 /* Dimensions the buffers that are filled by received Ethernet frames. */\r
58 #define ipSIZE_OF_ETH_CRC_BYTES                                 ( 4UL )\r
59 #define ipSIZE_OF_ETH_OPTIONAL_802_1Q_TAG_BYTES ( 4UL )\r
60 #define ipTOTAL_ETHERNET_FRAME_SIZE                             ( ( ( uint32_t ) ipconfigNETWORK_MTU ) + ( ( uint32_t ) ipSIZE_OF_ETH_HEADER ) + ipSIZE_OF_ETH_CRC_BYTES + ipSIZE_OF_ETH_OPTIONAL_802_1Q_TAG_BYTES )\r
61 \r
62 /*_RB_ Comment may need updating. */\r
63 /* Space left at the beginning of a network buffer storage area to store a\r
64 pointer back to the network buffer.  Should be a multiple of 8 to ensure 8 byte\r
65 alignment is maintained on architectures that require it.\r
66 \r
67 In order to get a 32-bit alignment of network packets, an offset of 2 bytes\r
68 would be desirable, as defined by ipconfigPACKET_FILLER_SIZE.  So the malloc'd\r
69 buffer will have the following contents:\r
70         uint32_t pointer;       // word-aligned\r
71         uchar_8 filler[6];\r
72         << ETH-header >>        // half-word-aligned\r
73         uchar_8 dest[6];    // start of pucEthernetBuffer\r
74         uchar_8 dest[6];\r
75         uchar16_t type;\r
76         << IP-header >>         // word-aligned\r
77         uint8_t ucVersionHeaderLength;\r
78         etc\r
79  */\r
80 #if( ipconfigBUFFER_PADDING != 0 )\r
81     #define ipBUFFER_PADDING    ipconfigBUFFER_PADDING\r
82 #else\r
83     #define ipBUFFER_PADDING    ( 8u + ipconfigPACKET_FILLER_SIZE )\r
84 #endif\r
85 \r
86 /* The structure used to store buffers and pass them around the network stack.\r
87 Buffers can be in use by the stack, in use by the network interface hardware\r
88 driver, or free (not in use). */\r
89 typedef struct xNETWORK_BUFFER\r
90 {\r
91         ListItem_t xBufferListItem;     /* Used to reference the buffer form the free buffer list or a socket. */\r
92         uint32_t ulIPAddress;                   /* Source or destination IP address, depending on usage scenario. */\r
93         uint8_t *pucEthernetBuffer;     /* Pointer to the start of the Ethernet frame. */\r
94         size_t xDataLength;                     /* Starts by holding the total Ethernet frame length, then the UDP/TCP payload length. */\r
95         uint16_t usPort;                                /* Source or destination port, depending on usage scenario. */\r
96         uint16_t usBoundPort;                   /* The port to which a transmitting socket is bound. */\r
97         #if( ipconfigUSE_LINKED_RX_MESSAGES != 0 )\r
98                 struct xNETWORK_BUFFER *pxNextBuffer; /* Possible optimisation for expert users - requires network driver support. */\r
99         #endif\r
100 } NetworkBufferDescriptor_t;\r
101 \r
102 #include "pack_struct_start.h"\r
103 struct xMAC_ADDRESS\r
104 {\r
105         uint8_t ucBytes[ ipMAC_ADDRESS_LENGTH_BYTES ];\r
106 }\r
107 #include "pack_struct_end.h"\r
108 \r
109 typedef struct xMAC_ADDRESS MACAddress_t;\r
110 \r
111 typedef enum eNETWORK_EVENTS\r
112 {\r
113         eNetworkUp,             /* The network is configured. */\r
114         eNetworkDown    /* The network connection has been lost. */\r
115 } eIPCallbackEvent_t;\r
116 \r
117 typedef enum ePING_REPLY_STATUS\r
118 {\r
119         eSuccess = 0,           /* A correct reply has been received for an outgoing ping. */\r
120         eInvalidChecksum,       /* A reply was received for an outgoing ping but the checksum of the reply was incorrect. */\r
121         eInvalidData            /* A reply was received to an outgoing ping but the payload of the reply was not correct. */\r
122 } ePingReplyStatus_t;\r
123 \r
124 typedef enum eNETWORK_ADDRESS_TYPE \r
125 {\r
126         eNetWorkAddressTypeIPV4,\r
127         eNetWorkAddressTypeIPV6,\r
128         eNetWorkAddressTypeHostName\r
129 } eNetWorkAddressType_t;\r
130 \r
131 /* Endian related definitions. */\r
132 #if( ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN )\r
133 \r
134         /* FreeRTOS_htons / FreeRTOS_htonl: some platforms might have built-in versions\r
135         using a single instruction so allow these versions to be overridden. */\r
136         #ifndef FreeRTOS_htons\r
137                 #define FreeRTOS_htons( usIn ) ( (uint16_t) ( ( ( usIn ) << 8U ) | ( ( usIn ) >> 8U ) ) )\r
138         #endif\r
139 \r
140         #ifndef FreeRTOS_htonl\r
141                 #define FreeRTOS_htonl( ulIn )                                                                                  \\r
142                         (                                                                                                                                       \\r
143                                 ( uint32_t )                                                                                                    \\r
144                                 (                                                                                                                               \\r
145                                         ( ( ( ( uint32_t ) ( ulIn ) )                ) << 24  ) |       \\r
146                                         ( ( ( ( uint32_t ) ( ulIn ) ) & 0x0000ff00UL ) <<  8  ) |       \\r
147                                         ( ( ( ( uint32_t ) ( ulIn ) ) & 0x00ff0000UL ) >>  8  ) |       \\r
148                                         ( ( ( ( uint32_t ) ( ulIn ) )                ) >> 24  )         \\r
149                                 )                                                                                                                               \\r
150                         )\r
151         #endif\r
152 \r
153 #else /* ipconfigBYTE_ORDER */\r
154 \r
155         #define FreeRTOS_htons( x ) ( ( uint16_t ) ( x ) )\r
156         #define FreeRTOS_htonl( x ) ( ( uint32_t ) ( x ) )\r
157 \r
158 #endif /* ipconfigBYTE_ORDER == pdFREERTOS_LITTLE_ENDIAN */\r
159 \r
160 #define FreeRTOS_ntohs( x ) FreeRTOS_htons( x )\r
161 #define FreeRTOS_ntohl( x ) FreeRTOS_htonl( x )\r
162 \r
163 #if( ipconfigHAS_INLINE_FUNCTIONS == 1 )\r
164 \r
165         static portINLINE int32_t  FreeRTOS_max_int32  (int32_t  a, int32_t  b);\r
166         static portINLINE uint32_t FreeRTOS_max_uint32 (uint32_t a, uint32_t b);\r
167         static portINLINE int32_t  FreeRTOS_min_int32  (int32_t  a, int32_t  b);\r
168         static portINLINE uint32_t FreeRTOS_min_uint32 (uint32_t a, uint32_t b);\r
169         static portINLINE uint32_t FreeRTOS_round_up   (uint32_t a, uint32_t d);\r
170         static portINLINE uint32_t FreeRTOS_round_down (uint32_t a, uint32_t d);\r
171         static portINLINE BaseType_t  FreeRTOS_min_BaseType  (BaseType_t  a, BaseType_t  b);\r
172         static portINLINE BaseType_t  FreeRTOS_max_BaseType  (BaseType_t  a, BaseType_t  b);\r
173         static portINLINE UBaseType_t FreeRTOS_max_UBaseType (UBaseType_t a, UBaseType_t b);\r
174         static portINLINE UBaseType_t   FreeRTOS_min_UBaseType (UBaseType_t  a, UBaseType_t  b);\r
175 \r
176 \r
177         static portINLINE int32_t  FreeRTOS_max_int32  (int32_t  a, int32_t  b) { return a >= b ? a : b; }\r
178         static portINLINE uint32_t FreeRTOS_max_uint32 (uint32_t a, uint32_t b) { return a >= b ? a : b; }\r
179         static portINLINE int32_t  FreeRTOS_min_int32  (int32_t  a, int32_t  b) { return a <= b ? a : b; }\r
180         static portINLINE uint32_t FreeRTOS_min_uint32 (uint32_t a, uint32_t b) { return a <= b ? a : b; }\r
181         static portINLINE uint32_t FreeRTOS_round_up   (uint32_t a, uint32_t d) { return d * ( ( a + d - 1u ) / d ); }\r
182         static portINLINE uint32_t FreeRTOS_round_down (uint32_t a, uint32_t d) { return d * ( a / d ); }\r
183 \r
184         static portINLINE BaseType_t  FreeRTOS_max_BaseType  (BaseType_t  a, BaseType_t  b) { return a >= b ? a : b; }\r
185         static portINLINE UBaseType_t FreeRTOS_max_UBaseType (UBaseType_t a, UBaseType_t b) { return a >= b ? a : b; }\r
186         static portINLINE BaseType_t  FreeRTOS_min_BaseType  (BaseType_t  a, BaseType_t  b) { return a <= b ? a : b; }\r
187         static portINLINE UBaseType_t FreeRTOS_min_UBaseType (UBaseType_t  a, UBaseType_t  b) { return a <= b ? a : b; }\r
188 \r
189 #else\r
190 \r
191         #define FreeRTOS_max_int32(a,b)  ( ( ( int32_t  ) ( a ) ) >= ( ( int32_t  ) ( b ) ) ? ( ( int32_t  ) ( a ) ) : ( ( int32_t  ) ( b ) ) )\r
192         #define FreeRTOS_max_uint32(a,b) ( ( ( uint32_t ) ( a ) ) >= ( ( uint32_t ) ( b ) ) ? ( ( uint32_t ) ( a ) ) : ( ( uint32_t ) ( b ) ) )\r
193 \r
194         #define FreeRTOS_min_int32(a,b)  ( ( ( int32_t  ) a ) <= ( ( int32_t  ) b ) ? ( ( int32_t  ) a ) : ( ( int32_t  ) b ) )\r
195         #define FreeRTOS_min_uint32(a,b) ( ( ( uint32_t ) a ) <= ( ( uint32_t ) b ) ? ( ( uint32_t ) a ) : ( ( uint32_t ) b ) )\r
196 \r
197         /*  Round-up: a = d * ( ( a + d - 1 ) / d ) */\r
198         #define FreeRTOS_round_up(a,d)   ( ( ( uint32_t ) ( d ) ) * ( ( ( ( uint32_t ) ( a ) ) + ( ( uint32_t ) ( d ) ) - 1UL ) / ( ( uint32_t ) ( d ) ) ) )\r
199         #define FreeRTOS_round_down(a,d) ( ( ( uint32_t ) ( d ) ) * ( ( ( uint32_t ) ( a ) ) / ( ( uint32_t ) ( d ) ) ) )\r
200 \r
201         #define FreeRTOS_ms_to_tick(ms)  ( ( ms * configTICK_RATE_HZ + 500 ) / 1000 )\r
202 \r
203         #define FreeRTOS_max_BaseType(a, b)  ( ( ( BaseType_t  ) ( a ) ) >= ( ( BaseType_t  ) ( b ) ) ? ( ( BaseType_t  ) ( a ) ) : ( ( BaseType_t  ) ( b ) ) )\r
204         #define FreeRTOS_max_UBaseType(a, b) ( ( ( UBaseType_t ) ( a ) ) >= ( ( UBaseType_t ) ( b ) ) ? ( ( UBaseType_t ) ( a ) ) : ( ( UBaseType_t ) ( b ) ) )\r
205         #define FreeRTOS_min_BaseType(a, b)  ( ( ( BaseType_t  ) ( a ) ) <= ( ( BaseType_t  ) ( b ) ) ? ( ( BaseType_t  ) ( a ) ) : ( ( BaseType_t  ) ( b ) ) )\r
206         #define FreeRTOS_min_UBaseType(a, b) ( ( ( UBaseType_t ) ( a ) ) <= ( ( UBaseType_t ) ( b ) ) ? ( ( UBaseType_t ) ( a ) ) : ( ( UBaseType_t ) ( b ) ) )\r
207 \r
208 #endif /* ipconfigHAS_INLINE_FUNCTIONS */\r
209 \r
210 #define pdMS_TO_MIN_TICKS( xTimeInMs ) ( pdMS_TO_TICKS( ( xTimeInMs ) ) < ( ( TickType_t ) 1 ) ? ( ( TickType_t ) 1 ) : pdMS_TO_TICKS( ( xTimeInMs ) ) )\r
211 \r
212 #ifndef pdTRUE_SIGNED\r
213         /* Temporary solution: eventually the defines below will appear in 'Source\include\projdefs.h' */\r
214         #define pdTRUE_SIGNED           pdTRUE\r
215         #define pdFALSE_SIGNED          pdFALSE\r
216         #define pdTRUE_UNSIGNED         ( ( UBaseType_t ) 1u )\r
217         #define pdFALSE_UNSIGNED        ( ( UBaseType_t ) 0u )\r
218 #endif\r
219 \r
220 /*\r
221  * FULL, UP-TO-DATE AND MAINTAINED REFERENCE DOCUMENTATION FOR ALL THESE\r
222  * FUNCTIONS IS AVAILABLE ON THE FOLLOWING URL:\r
223  * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/FreeRTOS_TCP_API_Functions.html\r
224  */\r
225 BaseType_t FreeRTOS_IPInit( const uint8_t ucIPAddress[ ipIP_ADDRESS_LENGTH_BYTES ],\r
226         const uint8_t ucNetMask[ ipIP_ADDRESS_LENGTH_BYTES ],\r
227         const uint8_t ucGatewayAddress[ ipIP_ADDRESS_LENGTH_BYTES ],\r
228         const uint8_t ucDNSServerAddress[ ipIP_ADDRESS_LENGTH_BYTES ],\r
229         const uint8_t ucMACAddress[ ipMAC_ADDRESS_LENGTH_BYTES ] );\r
230 \r
231 void * FreeRTOS_GetUDPPayloadBuffer( size_t xRequestedSizeBytes, TickType_t xBlockTimeTicks );\r
232 void FreeRTOS_GetAddressConfiguration( uint32_t *pulIPAddress, uint32_t *pulNetMask, uint32_t *pulGatewayAddress, uint32_t *pulDNSServerAddress );\r
233 void FreeRTOS_SetAddressConfiguration( const uint32_t *pulIPAddress, const uint32_t *pulNetMask, const uint32_t *pulGatewayAddress, const uint32_t *pulDNSServerAddress );\r
234 BaseType_t FreeRTOS_SendPingRequest( uint32_t ulIPAddress, size_t xNumberOfBytesToSend, TickType_t xBlockTimeTicks );\r
235 void FreeRTOS_ReleaseUDPPayloadBuffer( void *pvBuffer );\r
236 const uint8_t * FreeRTOS_GetMACAddress( void );\r
237 void FreeRTOS_UpdateMACAddress( const uint8_t ucMACAddress[ipMAC_ADDRESS_LENGTH_BYTES] );\r
238 void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent );\r
239 void vApplicationPingReplyHook( ePingReplyStatus_t eStatus, uint16_t usIdentifier );\r
240 uint32_t FreeRTOS_GetIPAddress( void );\r
241 void FreeRTOS_SetIPAddress( uint32_t ulIPAddress );\r
242 void FreeRTOS_SetNetmask( uint32_t ulNetmask );\r
243 void FreeRTOS_SetGatewayAddress( uint32_t ulGatewayAddress );\r
244 uint32_t FreeRTOS_GetGatewayAddress( void );\r
245 uint32_t FreeRTOS_GetDNSServerAddress( void );\r
246 uint32_t FreeRTOS_GetNetmask( void );\r
247 void FreeRTOS_OutputARPRequest( uint32_t ulIPAddress );\r
248 BaseType_t FreeRTOS_IsNetworkUp( void );\r
249 \r
250 #if( ipconfigCHECK_IP_QUEUE_SPACE != 0 )\r
251         UBaseType_t uxGetMinimumIPQueueSpace( void );\r
252 #endif\r
253 \r
254 /*\r
255  * Defined in FreeRTOS_Sockets.c\r
256  * //_RB_ Don't think this comment is correct.  If this is for internal use only it should appear after all the public API functions and not start with FreeRTOS_.\r
257  * Socket has had activity, reset the timer so it will not be closed\r
258  * because of inactivity\r
259  */\r
260 const char *FreeRTOS_GetTCPStateName( UBaseType_t ulState);\r
261 \r
262 /* _HT_ Temporary: show all valid ARP entries\r
263  */\r
264 void FreeRTOS_PrintARPCache( void );\r
265 void FreeRTOS_ClearARP( void );\r
266 \r
267 #if( ipconfigDHCP_REGISTER_HOSTNAME == 1 )\r
268 \r
269         /* DHCP has an option for clients to register their hostname.  It doesn't\r
270         have much use, except that a device can be found in a router along with its\r
271         name. If this option is used the callback below must be provided by the\r
272         application     writer to return a const string, denoting the device's name. */\r
273         const char *pcApplicationHostnameHook( void );\r
274 \r
275 #endif /* ipconfigDHCP_REGISTER_HOSTNAME */\r
276 \r
277 \r
278 /* This xApplicationGetRandomNumber() will set *pulNumber to a random number,\r
279 and return pdTRUE. When the random number generator is broken, it shall return\r
280 pdFALSE.\r
281 The function is defined in 'iot_secure_sockets.c'.\r
282 If that module is not included in the project, the application must provide an\r
283 implementation of it.\r
284 The macro's ipconfigRAND32() and configRAND32() are not in use anymore. */\r
285 BaseType_t xApplicationGetRandomNumber( uint32_t *pulNumber );\r
286 \r
287 /* For backward compatibility define old structure names to the newer equivalent\r
288 structure name. */\r
289 #ifndef ipconfigENABLE_BACKWARD_COMPATIBILITY\r
290         #define ipconfigENABLE_BACKWARD_COMPATIBILITY   1\r
291 #endif\r
292 \r
293 #if( ipconfigENABLE_BACKWARD_COMPATIBILITY == 1 )\r
294         #define xIPStackEvent_t                         IPStackEvent_t\r
295         #define xNetworkBufferDescriptor_t      NetworkBufferDescriptor_t\r
296         #define xMACAddress_t                           MACAddress_t\r
297         #define xWinProperties_t                        WinProperties_t\r
298         #define xSocket_t                                       Socket_t\r
299         #define xSocketSet_t                            SocketSet_t\r
300         #define ipSIZE_OF_IP_HEADER                     ipSIZE_OF_IPv4_HEADER\r
301 \r
302         /* Since August 2016, the public types and fields below have changed name:\r
303         abbreviations TCP/UDP are now written in capitals, and type names now end with "_t". */\r
304         #define FOnConnected                            FOnConnected_t\r
305         #define FOnTcpReceive                           FOnTCPReceive_t\r
306         #define FOnTcpSent                                      FOnTCPSent_t\r
307         #define FOnUdpReceive                           FOnUDPReceive_t\r
308         #define FOnUdpSent                                      FOnUDPSent_t\r
309 \r
310         #define pOnTcpConnected                         pxOnTCPConnected\r
311         #define pOnTcpReceive                           pxOnTCPReceive\r
312         #define pOnTcpSent                                      pxOnTCPSent\r
313         #define pOnUdpReceive                           pxOnUDPReceive\r
314         #define pOnUdpSent                                      pxOnUDPSent\r
315 \r
316         #define FOnUdpSent                                      FOnUDPSent_t\r
317         #define FOnTcpSent                                      FOnTCPSent_t\r
318 #endif /* ipconfigENABLE_BACKWARD_COMPATIBILITY */\r
319 \r
320 #ifdef __cplusplus\r
321 } /* extern "C" */\r
322 #endif\r
323 \r
324 #endif /* FREERTOS_IP_H */\r
325 \r
326 \r
327 \r
328 \r
329 \r
330 \r
331 \r
332 \r
333 \r
334 \r
335 \r
336 \r
337 \r