]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/FreeRTOS-Plus-UDP/include/FreeRTOS_IP_Private.h
3c8dda9a32653ef501a30240f318e54e6101ab15
[freertos] / FreeRTOS-Plus / Source / FreeRTOS-Plus-UDP / include / FreeRTOS_IP_Private.h
1 /*\r
2  * FreeRTOS+UDP V1.0.4\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://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 #ifndef FREERTOS_IP_PRIVATE_H\r
29 #define FREERTOS_IP_PRIVATE_H\r
30 \r
31 /* Application level configuration options. */\r
32 #include "FreeRTOSIPConfig.h"\r
33 #include "IPTraceMacroDefaults.h"\r
34 \r
35 typedef struct xNetworkAddressingParameters\r
36 {\r
37         uint32_t ulDefaultIPAddress;\r
38         uint32_t ulNetMask;\r
39         uint32_t ulGatewayAddress;\r
40         uint32_t ulDNSServerAddress;\r
41 } xNetworkAddressingParameters_t;\r
42 \r
43 \r
44 /*-----------------------------------------------------------*/\r
45 /* Protocol headers.                                         */\r
46 /*-----------------------------------------------------------*/\r
47 \r
48 #include "pack_struct_start.h"\r
49 struct xETH_HEADER\r
50 {\r
51         xMACAddress_t xDestinationAddress;\r
52         xMACAddress_t xSourceAddress;\r
53         uint16_t usFrameType;\r
54 }\r
55 #include "pack_struct_end.h"\r
56 typedef struct xETH_HEADER xEthernetHeader_t;\r
57 \r
58 #include "pack_struct_start.h"\r
59 struct xARP_HEADER\r
60 {\r
61         uint16_t usHardwareType;\r
62         uint16_t usProtocolType;\r
63         uint8_t ucHardwareAddressLength;\r
64         uint8_t ucProtocolAddressLength;\r
65         uint16_t usOperation;\r
66         xMACAddress_t xSenderHardwareAddress;\r
67         uint32_t ulSenderProtocolAddress;\r
68         xMACAddress_t xTargetHardwareAddress;\r
69         uint32_t ulTargetProtocolAddress;\r
70 }\r
71 #include "pack_struct_end.h"\r
72 typedef struct xARP_HEADER xARPHeader_t;\r
73 \r
74 #include "pack_struct_start.h"\r
75 struct xIP_HEADER\r
76 {\r
77         uint8_t ucVersionHeaderLength;\r
78         uint8_t ucDifferentiatedServicesCode;\r
79         uint16_t usLength;\r
80         uint16_t usIdentification;\r
81         uint16_t usFragmentOffset;\r
82         uint8_t ucTimeToLive;\r
83         uint8_t ucProtocol;\r
84         uint16_t usHeaderChecksum;\r
85         uint32_t ulSourceIPAddress;\r
86         uint32_t ulDestinationIPAddress;\r
87 }\r
88 #include "pack_struct_end.h"\r
89 typedef struct xIP_HEADER xIPHeader_t;\r
90 #define ipSIZE_OF_IP_HEADER 20\r
91 \r
92 #include "pack_struct_start.h"\r
93 struct xICMP_HEADER\r
94 {\r
95         uint8_t ucTypeOfMessage;\r
96         uint8_t ucTypeOfService;\r
97         uint16_t usChecksum;\r
98         uint16_t usIdentifier;\r
99         uint16_t usSequenceNumber;\r
100 }\r
101 #include "pack_struct_end.h"\r
102 typedef struct xICMP_HEADER xICMPHeader_t;\r
103 \r
104 #include "pack_struct_start.h"\r
105 struct xUDP_HEADER\r
106 {\r
107         uint16_t usSourcePort;\r
108         uint16_t usDestinationPort;\r
109         uint16_t usLength;\r
110         uint16_t usChecksum;\r
111 }\r
112 #include "pack_struct_end.h"\r
113 typedef struct xUDP_HEADER xUDPHeader_t;\r
114 #define ipSIZE_OF_UDP_HEADER 8\r
115 \r
116 #include "pack_struct_start.h"\r
117 struct xPSEUDO_HEADER\r
118 {\r
119         uint32_t ulSourceAddress;\r
120         uint32_t ulDestinationAddress;\r
121         uint8_t ucZeros;\r
122         uint8_t ucProtocol;\r
123         uint16_t usUDPLength;\r
124 }\r
125 #include "pack_struct_end.h"\r
126 typedef struct xPSEUDO_HEADER xPseudoHeader_t;\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 /* Nested protocol packets.                                  */\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 #include "pack_struct_start.h"\r
133 struct xARP_PACKET\r
134 {\r
135         xEthernetHeader_t xEthernetHeader;\r
136         xARPHeader_t xARPHeader;\r
137 }\r
138 #include "pack_struct_end.h"\r
139 typedef struct xARP_PACKET xARPPacket_t;\r
140 \r
141 #include "pack_struct_start.h"\r
142 struct xIP_PACKET\r
143 {\r
144         xEthernetHeader_t xEthernetHeader;\r
145         xIPHeader_t xIPHeader;\r
146 }\r
147 #include "pack_struct_end.h"\r
148 typedef struct xIP_PACKET xIPPacket_t;\r
149 \r
150 #include "pack_struct_start.h"\r
151 struct xICMP_PACKET\r
152 {\r
153         xEthernetHeader_t xEthernetHeader;\r
154         xIPHeader_t xIPHeader;\r
155         xICMPHeader_t xICMPHeader;\r
156 }\r
157 #include "pack_struct_end.h"\r
158 typedef struct xICMP_PACKET xICMPPacket_t;\r
159 \r
160 #include "pack_struct_start.h"\r
161 struct xUDP_PACKET\r
162 {\r
163         xEthernetHeader_t xEthernetHeader;\r
164         xIPHeader_t xIPHeader;\r
165         xUDPHeader_t xUDPHeader;\r
166 }\r
167 #include "pack_struct_end.h"\r
168 typedef struct xUDP_PACKET xUDPPacket_t;\r
169 \r
170 /* Dimensions the buffers that are filled by received Ethernet frames. */\r
171 #define ipETHERNET_CRC_BYTES                                    ( 4UL )\r
172 #define ipETHERNET_OPTIONAL_802_1Q_TAG_BYTES    ( 4UL )\r
173 #define ipTOTAL_ETHERNET_FRAME_SIZE                             ( ipconfigNETWORK_MTU + sizeof( xEthernetHeader_t ) + ipETHERNET_CRC_BYTES + ipETHERNET_OPTIONAL_802_1Q_TAG_BYTES )\r
174 \r
175 /* The maximum UDP payload length. */\r
176 #define ipMAX_UDP_PAYLOAD_LENGTH ( ( ipconfigNETWORK_MTU - ipSIZE_OF_IP_HEADER ) - ipSIZE_OF_UDP_HEADER )\r
177 \r
178 typedef enum\r
179 {\r
180         eReleaseBuffer = 0,             /* Processing the frame did not find anything to do - just release the buffer. */\r
181         eProcessBuffer,                 /* An Ethernet frame has a valid address - continue process its contents. */\r
182         eReturnEthernetFrame,   /* The Ethernet frame contains an ARP or ICMP packet that can be returned to its source. */\r
183         eFrameConsumed                  /* Processing the Ethernet packet contents resulted in the payload being sent to the stack. */\r
184 } eFrameProcessingResult_t;\r
185 \r
186 typedef enum\r
187 {\r
188         eNetworkDownEvent = 0,  /* The network interface has been lost and/or needs [re]connecting. */\r
189         eEthernetRxEvent,       /* The network interface has queued a received Ethernet frame. */\r
190         eARPTimerEvent,         /* The ARP timer expired. */\r
191         eStackTxEvent,          /* The software stack has queued a packet to transmit. */\r
192         eDHCPEvent                      /* Process the DHCP state machine. */\r
193 } eIPEvent_t;\r
194 \r
195 typedef struct IP_TASK_COMMANDS\r
196 {\r
197         eIPEvent_t eEventType;\r
198         void *pvData;\r
199 } xIPStackEvent_t;\r
200 \r
201 #define ipBROADCAST_IP_ADDRESS 0xffffffffUL\r
202 \r
203 /* Offset into the Ethernet frame that is used to temporarily store information\r
204 on the fragmentation status of the packet being sent.  The value is important,\r
205 as it is past the location into which the destination address will get placed. */\r
206 #define ipFRAGMENTATION_PARAMETERS_OFFSET               ( 6 )\r
207 #define ipSOCKET_OPTIONS_OFFSET                                 ( 6 )\r
208 \r
209 /* Only used when outgoing fragmentation is being used (FreeRTOSIPConfig.h\r
210 setting. */\r
211 #define ipGET_UDP_PAYLOAD_OFFSET_FOR_FRAGMENT( usFragmentOffset ) ( ( ( usFragmentOffset ) == 0 ) ? ipUDP_PAYLOAD_OFFSET : ipIP_PAYLOAD_OFFSET )\r
212 \r
213 /* The offset into a UDP packet at which the UDP data (payload) starts. */\r
214 #define ipUDP_PAYLOAD_OFFSET    ( sizeof( xUDPPacket_t ) )\r
215 \r
216 /* The offset into an IP packet into which the IP data (payload) starts. */\r
217 #define ipIP_PAYLOAD_OFFSET             ( sizeof( xIPPacket_t ) )\r
218 \r
219 /* Space left at the beginning of a network buffer storage area to store a\r
220 pointer back to the network buffer.  Should be a multiple of 8 to ensure\r
221 8 byte alignment is maintained on architectures that require it. */\r
222 #define ipBUFFER_PADDING                ( 8 )\r
223 \r
224 #include "pack_struct_start.h"\r
225 struct xUDP_IP_FRACMENT_PARAMETERS\r
226 {\r
227         uint8_t ucSocketOptions;\r
228         uint8_t ucPadFor16BitAlignment;\r
229         uint16_t usFragmentedPacketOffset;\r
230         uint16_t usFragmentLength;\r
231         uint16_t usPayloadChecksum;\r
232 }\r
233 #include "pack_struct_end.h"\r
234 typedef struct xUDP_IP_FRACMENT_PARAMETERS xIPFragmentParameters_t;\r
235 \r
236 #if( ipconfigBYTE_ORDER == FREERTOS_LITTLE_ENDIAN )\r
237 \r
238         /* Ethernet frame types. */\r
239         #define ipARP_TYPE      ( 0x0608U )\r
240         #define ipIP_TYPE       ( 0x0008U )\r
241 \r
242         /* ARP related definitions. */\r
243         #define ipARP_PROTOCOL_TYPE ( 0x0008U )\r
244         #define ipARP_HARDWARE_TYPE_ETHERNET ( 0x0100U )\r
245         #define ipARP_REQUEST ( 0x0100 )\r
246         #define ipARP_REPLY ( 0x0200 )\r
247 \r
248 #else\r
249 \r
250         /* Ethernet frame types. */\r
251         #define ipARP_TYPE      ( 0x0806U )\r
252         #define ipIP_TYPE       ( 0x0800U )\r
253 \r
254         /* ARP related definitions. */\r
255         #define ipARP_PROTOCOL_TYPE ( 0x0800U )\r
256         #define ipARP_HARDWARE_TYPE_ETHERNET ( 0x0001U )\r
257         #define ipARP_REQUEST ( 0x0001 )\r
258         #define ipARP_REPLY ( 0x0002 )\r
259 \r
260 #endif /* ipconfigBYTE_ORDER == FREERTOS_LITTLE_ENDIAN */\r
261 \r
262 /* The structure used to store buffers and pass them around the network stack.\r
263 Buffers can be in use by the stack, in use by the network interface hardware\r
264 driver, or free (not in use). */\r
265 typedef struct xNETWORK_BUFFER\r
266 {\r
267         xListItem xBufferListItem;              /* Used to reference the buffer form the free buffer list or a socket. */\r
268         uint32_t ulIPAddress;                   /* Source or destination IP address, depending on usage scenario. */\r
269         uint8_t *pucEthernetBuffer;     /* Pointer to the start of the Ethernet frame. */\r
270         size_t xDataLength;                     /* Starts by holding the total Ethernet frame length, then the UDP payload length. */\r
271         uint16_t usPort;                                /* Source or destination port, depending on usage scenario. */\r
272         uint16_t usBoundPort;                   /* The port to which a transmitting socket is bound. */\r
273 } xNetworkBufferDescriptor_t;\r
274 \r
275 void vNetworkBufferRelease( xNetworkBufferDescriptor_t * const pxNetworkBuffer );\r
276 \r
277 /*\r
278  * A version of FreeRTOS_GetReleaseNetworkBuffer() that can be called from an\r
279  * interrupt.  If a non zero value is returned, then the calling ISR should\r
280  * perform a context switch before exiting the ISR.\r
281  */\r
282 BaseType_t FreeRTOS_ReleaseFreeNetworkBufferFromISR( void );\r
283 \r
284 /*\r
285  * Create a message that contains a command to initialise the network interface.\r
286  * This is used during initialisation, and at any time the network interface\r
287  * goes down thereafter.  The network interface hardware driver is responsible\r
288  * for sending the message that contains the network interface down command/\r
289  * event.\r
290  *\r
291  * Only use the FreeRTOS_NetworkDownFromISR() version if the function is to be\r
292  * called from an interrupt service routine.  If FreeRTOS_NetworkDownFromISR()\r
293  * returns a non-zero value then a context switch should be performed ebfore\r
294  * the interrupt is exited.\r
295  */\r
296 void FreeRTOS_NetworkDown( void );\r
297 BaseType_t FreeRTOS_NetworkDownFromISR( void );\r
298 \r
299 /*\r
300  * Inspect an Ethernet frame to see if it contains data that the stack needs to\r
301  * process.  eProcessBuffer is returned if the frame should be processed by the\r
302  * stack.  eReleaseBuffer is returned if the frame should be discarded.\r
303  */\r
304 eFrameProcessingResult_t eConsiderFrameForProcessing( const uint8_t * const pucEthernetBuffer );\r
305 \r
306 #if( ipconfigINCLUDE_TEST_CODE == 1 )\r
307         UBaseType_t uxGetNumberOfFreeNetworkBuffers( void );\r
308 #endif /* ipconfigINCLUDE_TEST_CODE */\r
309 \r
310 /* Socket related private functions. */\r
311 BaseType_t xProcessReceivedUDPPacket( xNetworkBufferDescriptor_t *pxNetworkBuffer, uint16_t usPort );\r
312 void FreeRTOS_SocketsInit( void );\r
313 \r
314 /* If FreeRTOS+NABTO is included then include the prototype of the function that\r
315 creates the Nabto task. */\r
316 #if( ipconfigFREERTOS_PLUS_NABTO == 1 )\r
317         void vStartNabtoTask( void );\r
318 #endif\r
319 \r
320 \r
321 #endif /* FREERTOS_IP_PRIVATE_H */\r
322 \r
323 \r
324 \r
325 \r
326 \r
327 \r
328 \r
329 \r
330 \r
331 \r
332 \r
333 \r
334 \r