2 * FreeRTOS+TCP Labs Build 160919 (C) 2016 Real Time Engineers ltd.
\r
3 * Authors include Hein Tibosch and Richard Barry
\r
5 *******************************************************************************
\r
6 ***** NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ***
\r
9 *** FREERTOS+TCP IS STILL IN THE LAB (mainly because the FTP and HTTP ***
\r
10 *** demos have a dependency on FreeRTOS+FAT, which is only in the Labs ***
\r
13 *** FreeRTOS+TCP is functional and has been used in commercial products ***
\r
14 *** for some time. Be aware however that we are still refining its ***
\r
15 *** design, the source code does not yet quite conform to the strict ***
\r
16 *** coding and style standards mandated by Real Time Engineers ltd., and ***
\r
17 *** the documentation and testing is not necessarily complete. ***
\r
19 *** PLEASE REPORT EXPERIENCES USING THE SUPPORT RESOURCES FOUND ON THE ***
\r
20 *** URL: http://www.FreeRTOS.org/contact Active early adopters may, at ***
\r
21 *** the sole discretion of Real Time Engineers Ltd., be offered versions ***
\r
22 *** under a license other than that described below. ***
\r
25 ***** NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ***
\r
26 *******************************************************************************
\r
28 * FreeRTOS+TCP can be used under two different free open source licenses. The
\r
29 * license that applies is dependent on the processor on which FreeRTOS+TCP is
\r
30 * executed, as follows:
\r
32 * If FreeRTOS+TCP is executed on one of the processors listed under the Special
\r
33 * License Arrangements heading of the FreeRTOS+TCP license information web
\r
34 * page, then it can be used under the terms of the FreeRTOS Open Source
\r
35 * License. If FreeRTOS+TCP is used on any other processor, then it can be used
\r
36 * under the terms of the GNU General Public License V2. Links to the relevant
\r
39 * The FreeRTOS+TCP License Information Page: http://www.FreeRTOS.org/tcp_license
\r
40 * The FreeRTOS Open Source License: http://www.FreeRTOS.org/license
\r
41 * The GNU General Public License Version 2: http://www.FreeRTOS.org/gpl-2.0.txt
\r
43 * FreeRTOS+TCP is distributed in the hope that it will be useful. You cannot
\r
44 * use FreeRTOS+TCP unless you agree that you use the software 'as is'.
\r
45 * FreeRTOS+TCP is provided WITHOUT ANY WARRANTY; without even the implied
\r
46 * warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
47 * PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
\r
48 * implied, expressed, or statutory.
\r
50 * 1 tab == 4 spaces!
\r
52 * http://www.FreeRTOS.org
\r
53 * http://www.FreeRTOS.org/plus
\r
54 * http://www.FreeRTOS.org/labs
\r
58 /******************************************************************************
\r
60 * See the following web page for essential buffer allocation scheme usage and
\r
61 * configuration details:
\r
62 * http://www.FreeRTOS.org/FreeRTOS-Plus/FreeRTOS_Plus_TCP/Embedded_Ethernet_Buffer_Management.html
\r
64 ******************************************************************************/
\r
66 /* THIS FILE SHOULD NOT BE USED IF THE PROJECT INCLUDES A MEMORY ALLOCATOR
\r
67 * THAT WILL FRAGMENT THE HEAP MEMORY. For example, heap_2 must not be used,
\r
68 * heap_4 can be used. */
\r
70 /* Standard includes. */
\r
73 /* FreeRTOS includes. */
\r
74 #include "FreeRTOS.h"
\r
78 /* FreeRTOS+TCP includes. */
\r
79 #include "FreeRTOS_IP.h"
\r
80 #include "FreeRTOS_UDP_IP.h"
\r
81 #include "FreeRTOS_IP_Private.h"
\r
82 #include "NetworkInterface.h"
\r
83 #include "NetworkBufferManagement.h"
\r
85 #include "tcpip/tcpip.h"
\r
86 #include "tcpip/src/tcpip_private.h"
\r
88 #include "NetworkConfig.h"
\r
90 /* The obtained network buffer must be large enough to hold a packet that might
\r
91 * replace the packet that was requested to be sent. */
\r
92 #if ipconfigUSE_TCP == 1
\r
93 #define baMINIMAL_BUFFER_SIZE sizeof( TCPPacket_t )
\r
95 #define baMINIMAL_BUFFER_SIZE sizeof( ARPPacket_t )
\r
96 #endif /* ipconfigUSE_TCP == 1 */
\r
98 /*_RB_ This is too complex not to have an explanation. */
\r
99 #if defined( ipconfigETHERNET_MINIMUM_PACKET_BYTES )
\r
100 #define ASSERT_CONCAT_( a, b ) a ## b
\r
101 #define ASSERT_CONCAT( a, b ) ASSERT_CONCAT_( a, b )
\r
102 #define STATIC_ASSERT( e ) \
\r
103 ; enum { ASSERT_CONCAT( assert_line_, __LINE__ ) = 1 / ( !!( e ) ) }
\r
105 STATIC_ASSERT( ipconfigETHERNET_MINIMUM_PACKET_BYTES <= baMINIMAL_BUFFER_SIZE );
\r
108 /* A list of free (available) NetworkBufferDescriptor_t structures. */
\r
109 static List_t xFreeBuffersList;
\r
111 /* Some statistics about the use of buffers. */
\r
112 static size_t uxMinimumFreeNetworkBuffers;
\r
114 /* Declares the pool of NetworkBufferDescriptor_t structures that are available
\r
115 * to the system. All the network buffers referenced from xFreeBuffersList exist
\r
116 * in this array. The array is not accessed directly except during initialisation,
\r
117 * when the xFreeBuffersList is filled (as all the buffers are free when the system
\r
119 static NetworkBufferDescriptor_t xNetworkBufferDescriptors[ ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS ];
\r
121 /* This constant is defined as false to let FreeRTOS_TCP_IP.c know that the
\r
122 * network buffers have a variable size: resizing may be necessary */
\r
123 const BaseType_t xBufferAllocFixedSize = pdFALSE;
\r
125 /* The semaphore used to obtain network buffers. */
\r
126 static SemaphoreHandle_t xNetworkBufferSemaphore = NULL;
\r
128 /*-----------------------------------------------------------*/
\r
130 #ifdef PIC32_USE_ETHERNET
\r
132 /* PIC32 specific stuff */
\r
135 /* MAC packet acknowledgment, once MAC is done with it */
\r
136 static bool PIC32_MacPacketAcknowledge( TCPIP_MAC_PACKET * pPkt,
\r
137 const void * param );
\r
139 /* allocates a MAC packet that holds a data buffer that can be used by both: */
\r
140 /* - the FreeRTOSIP (NetworkBufferDescriptor_t->pucEthernetBuffer) */
\r
141 /* - the Harmony MAC driver: TCPIP_MAC_PACKET->pDSeg->segLoad */
\r
142 /* from the beginning of the buffer: */
\r
143 /* - 4 bytes pointer to the network descriptor (FreeRTOS) */
\r
144 /* - 4 bytes pointer to the MAC packet (pic32_NetworkInterface.c) */
\r
145 /* - 2 bytes offset from the MAC packet (Harmony MAC driver: segLoadOffset) */
\r
147 /* NOTE: segLoadLen should NOT include: */
\r
148 /* - the TCPIP_MAC_FRAME_OFFSET (== ipBUFFER_PADDING which should be == 10!) */
\r
149 /* - the sizeof(TCPIP_MAC_ETHERNET_HEADER) */
\r
150 /* These are added by the MAC packet allocation! */
\r
152 static uint8_t * PIC32_PktAlloc( uint16_t pktLen,
\r
153 uint16_t segLoadLen,
\r
154 TCPIP_MAC_PACKET_ACK_FUNC ackF,
\r
155 TCPIP_MAC_PACKET ** pPtrPkt )
\r
157 uint8_t * pBuff = 0;
\r
159 /* allocate standard packet */
\r
160 TCPIP_MAC_PACKET * pPkt = TCPIP_PKT_PacketAlloc( pktLen, segLoadLen, 0 );
\r
162 /* set the MAC packet pointer in the packet */
\r
165 pBuff = pPkt->pDSeg->segLoad;
\r
166 TCPIP_MAC_PACKET ** ppkt = ( TCPIP_MAC_PACKET ** ) ( pBuff - PIC32_BUFFER_PKT_PTR_OSSET );
\r
167 configASSERT( ( ( uint32_t ) ppkt & ( sizeof( uint32_t ) - 1 ) ) == 0 );
\r
168 *ppkt = pPkt; /* store the packet it comes from */
\r
169 pPkt->ackFunc = ackF;
\r
170 pPkt->ackParam = 0;
\r
183 /* standard PIC32 MAC allocation function for a MAC packet */
\r
184 /* this packet saves room for the FreeRTOS network descriptor */
\r
185 /* at the beginning of the data buffer */
\r
186 /* see NetworkBufferAllocate */
\r
187 /* Note: flags parameter is ignored since that's used in the Harmony stack only */
\r
188 TCPIP_MAC_PACKET * PIC32_MacPacketAllocate( uint16_t pktLen,
\r
189 uint16_t segLoadLen,
\r
190 TCPIP_MAC_PACKET_FLAGS flags )
\r
192 TCPIP_MAC_PACKET * pPkt;
\r
194 PIC32_PktAlloc( pktLen, segLoadLen, 0, &pPkt );
\r
199 /* standard PIC32 MAC packet acknowledgment */
\r
200 /* function called once MAC is done with it */
\r
201 static bool PIC32_MacPacketAcknowledge( TCPIP_MAC_PACKET * pPkt,
\r
202 const void * param )
\r
204 configASSERT( ( pPkt != 0 ) );
\r
206 TCPIP_PKT_PacketFree( pPkt );
\r
211 /* associates the current MAC packet with a network descriptor */
\r
212 /* mainly for RX packet */
\r
213 void PIC32_MacAssociate( TCPIP_MAC_PACKET * pRxPkt,
\r
214 NetworkBufferDescriptor_t * pxBufferDescriptor,
\r
217 uint8_t * pPktBuff = pRxPkt->pDSeg->segLoad;
\r
219 pxBufferDescriptor->pucEthernetBuffer = pPktBuff;
\r
220 pxBufferDescriptor->xDataLength = pktLength;
\r
222 /* make sure this is a properly allocated packet */
\r
223 TCPIP_MAC_PACKET ** ppkt = ( TCPIP_MAC_PACKET ** ) ( pPktBuff - PIC32_BUFFER_PKT_PTR_OSSET );
\r
224 configASSERT( ( ( uint32_t ) ppkt & ( sizeof( uint32_t ) - 1 ) ) == 0 );
\r
226 if( *ppkt != pRxPkt )
\r
228 configASSERT( false );
\r
231 /* set the proper descriptor info */
\r
232 NetworkBufferDescriptor_t ** ppDcpt = ( NetworkBufferDescriptor_t ** ) ( pPktBuff - ipBUFFER_PADDING );
\r
233 configASSERT( ( ( uint32_t ) ppDcpt & ( sizeof( uint32_t ) - 1 ) ) == 0 );
\r
234 *ppDcpt = pxBufferDescriptor;
\r
237 /* debug functionality */
\r
238 void PIC32_MacPacketOrphan( TCPIP_MAC_PACKET * pPkt )
\r
240 TCPIP_PKT_PacketFree( pPkt );
\r
241 configASSERT( false );
\r
244 /* FreeRTOS allocation functions */
\r
246 /* allocates a buffer that can be used by both: */
\r
247 /* - the FreeRTOSIP (NetworkBufferDescriptor_t->pucEthernetBuffer) */
\r
248 /* - the Harmony MAC driver: TCPIP_MAC_PACKET */
\r
249 /* See PIC32_PktAlloc for details */
\r
251 /* NOTE: reqLength should NOT include the ipBUFFER_PADDING (which should be == 10!) */
\r
252 /* or the sizeof(TCPIP_MAC_ETHERNET_HEADER) */
\r
253 /* These are added by the MAC packet allocation! */
\r
255 uint8_t * NetworkBufferAllocate( size_t reqLength )
\r
257 return PIC32_PktAlloc( sizeof( TCPIP_MAC_PACKET ), reqLength, PIC32_MacPacketAcknowledge, 0 );
\r
260 /* deallocates a network buffer previously allocated */
\r
261 /* with NetworkBufferAllocate */
\r
262 void NetworkBufferFree( uint8_t * pNetworkBuffer )
\r
264 if( pNetworkBuffer != 0 )
\r
266 TCPIP_MAC_PACKET ** ppkt = ( TCPIP_MAC_PACKET ** ) ( pNetworkBuffer - PIC32_BUFFER_PKT_PTR_OSSET );
\r
267 configASSERT( ( ( uint32_t ) ppkt & ( sizeof( uint32_t ) - 1 ) ) == 0 );
\r
268 TCPIP_MAC_PACKET * pPkt = *ppkt;
\r
269 configASSERT( ( pPkt != 0 ) );
\r
271 if( pPkt->ackFunc != 0 )
\r
273 ( *pPkt->ackFunc )( pPkt, pPkt->ackParam );
\r
277 PIC32_MacPacketOrphan( pPkt );
\r
282 #endif /* #ifdef PIC32_USE_ETHERNET */
\r
284 /*-----------------------------------------------------------*/
\r
286 BaseType_t xNetworkBuffersInitialise( void )
\r
288 BaseType_t xReturn, x;
\r
290 /* Only initialise the buffers and their associated kernel objects if they
\r
291 * have not been initialised before. */
\r
292 if( xNetworkBufferSemaphore == NULL )
\r
294 xNetworkBufferSemaphore = xSemaphoreCreateCounting( ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS, ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS );
\r
295 configASSERT( xNetworkBufferSemaphore );
\r
297 if( xNetworkBufferSemaphore != NULL )
\r
299 #if ( configQUEUE_REGISTRY_SIZE > 0 )
\r
301 vQueueAddToRegistry( xNetworkBufferSemaphore, "NetBufSem" );
\r
303 #endif /* configQUEUE_REGISTRY_SIZE */
\r
305 /* If the trace recorder code is included name the semaphore for viewing
\r
306 * in FreeRTOS+Trace. */
\r
307 #if ( ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 )
\r
309 extern QueueHandle_t xNetworkEventQueue;
\r
310 vTraceSetQueueName( xNetworkEventQueue, "IPStackEvent" );
\r
311 vTraceSetQueueName( xNetworkBufferSemaphore, "NetworkBufferCount" );
\r
313 #endif /* ipconfigINCLUDE_EXAMPLE_FREERTOS_PLUS_TRACE_CALLS == 1 */
\r
315 vListInitialise( &xFreeBuffersList );
\r
317 /* Initialise all the network buffers. No storage is allocated to
\r
318 * the buffers yet. */
\r
319 for( x = 0; x < ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS; x++ )
\r
321 /* Initialise and set the owner of the buffer list items. */
\r
322 xNetworkBufferDescriptors[ x ].pucEthernetBuffer = NULL;
\r
323 vListInitialiseItem( &( xNetworkBufferDescriptors[ x ].xBufferListItem ) );
\r
324 listSET_LIST_ITEM_OWNER( &( xNetworkBufferDescriptors[ x ].xBufferListItem ), &xNetworkBufferDescriptors[ x ] );
\r
326 /* Currently, all buffers are available for use. */
\r
327 vListInsert( &xFreeBuffersList, &( xNetworkBufferDescriptors[ x ].xBufferListItem ) );
\r
330 uxMinimumFreeNetworkBuffers = ipconfigNUM_NETWORK_BUFFER_DESCRIPTORS;
\r
334 if( xNetworkBufferSemaphore == NULL )
\r
345 /*-----------------------------------------------------------*/
\r
347 uint8_t * pucGetNetworkBuffer( size_t * pxRequestedSizeBytes )
\r
349 uint8_t * pucEthernetBuffer;
\r
350 size_t xSize = *pxRequestedSizeBytes;
\r
352 if( xSize < baMINIMAL_BUFFER_SIZE )
\r
354 /* Buffers must be at least large enough to hold a TCP-packet with
\r
355 * headers, or an ARP packet, in case TCP is not included. */
\r
356 xSize = baMINIMAL_BUFFER_SIZE;
\r
359 /* Round up xSize to the nearest multiple of N bytes,
\r
360 * where N equals 'sizeof( size_t )'. */
\r
361 if( ( xSize & ( sizeof( size_t ) - 1u ) ) != 0u )
\r
363 xSize = ( xSize | ( sizeof( size_t ) - 1u ) ) + 1u;
\r
366 *pxRequestedSizeBytes = xSize;
\r
368 /* Allocate a buffer large enough to store the requested Ethernet frame size
\r
369 * and a pointer to a network buffer structure (hence the addition of
\r
370 * ipBUFFER_PADDING bytes). */
\r
372 #ifdef PIC32_USE_ETHERNET
\r
373 pucEthernetBuffer = NetworkBufferAllocate( xSize - sizeof( TCPIP_MAC_ETHERNET_HEADER ) );
\r
375 pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xSize + ipBUFFER_PADDING );
\r
376 #endif /* #ifdef PIC32_USE_ETHERNET */
\r
378 configASSERT( pucEthernetBuffer );
\r
380 if( pucEthernetBuffer != NULL )
\r
382 /* Enough space is left at the start of the buffer to place a pointer to
\r
383 * the network buffer structure that references this Ethernet buffer.
\r
384 * Return a pointer to the start of the Ethernet buffer itself. */
\r
385 #ifndef PIC32_USE_ETHERNET
\r
386 pucEthernetBuffer += ipBUFFER_PADDING;
\r
387 #endif /* #ifndef PIC32_USE_ETHERNET */
\r
390 return pucEthernetBuffer;
\r
392 /*-----------------------------------------------------------*/
\r
394 void vReleaseNetworkBuffer( uint8_t * pucEthernetBuffer )
\r
396 /* There is space before the Ethernet buffer in which a pointer to the
\r
397 * network buffer that references this Ethernet buffer is stored. Remove the
\r
398 * space before freeing the buffer. */
\r
399 #ifdef PIC32_USE_ETHERNET
\r
400 NetworkBufferFree( pucEthernetBuffer );
\r
402 if( pucEthernetBuffer != NULL )
\r
404 pucEthernetBuffer -= ipBUFFER_PADDING;
\r
405 vPortFree( ( void * ) pucEthernetBuffer );
\r
407 #endif /* #ifdef PIC32_USE_ETHERNET */
\r
409 /*-----------------------------------------------------------*/
\r
411 NetworkBufferDescriptor_t * pxGetNetworkBufferWithDescriptor( size_t xRequestedSizeBytes,
\r
412 TickType_t xBlockTimeTicks )
\r
414 NetworkBufferDescriptor_t * pxReturn = NULL;
\r
417 if( ( xRequestedSizeBytes != 0u ) && ( xRequestedSizeBytes < ( size_t ) baMINIMAL_BUFFER_SIZE ) )
\r
419 /* ARP packets can replace application packets, so the storage must be
\r
420 * at least large enough to hold an ARP. */
\r
421 xRequestedSizeBytes = baMINIMAL_BUFFER_SIZE;
\r
424 #ifdef PIC32_USE_ETHERNET
\r
425 if( xRequestedSizeBytes != 0u )
\r
427 #endif /* #ifdef PIC32_USE_ETHERNET */
\r
428 xRequestedSizeBytes += 2u;
\r
430 if( ( xRequestedSizeBytes & ( sizeof( size_t ) - 1u ) ) != 0u )
\r
432 xRequestedSizeBytes = ( xRequestedSizeBytes | ( sizeof( size_t ) - 1u ) ) + 1u;
\r
434 #ifdef PIC32_USE_ETHERNET
\r
436 #endif /* #ifdef PIC32_USE_ETHERNET */
\r
438 /* If there is a semaphore available, there is a network buffer available. */
\r
439 if( xSemaphoreTake( xNetworkBufferSemaphore, xBlockTimeTicks ) == pdPASS )
\r
441 /* Protect the structure as it is accessed from tasks and interrupts. */
\r
442 taskENTER_CRITICAL();
\r
444 pxReturn = ( NetworkBufferDescriptor_t * ) listGET_OWNER_OF_HEAD_ENTRY( &xFreeBuffersList );
\r
445 uxListRemove( &( pxReturn->xBufferListItem ) );
\r
447 taskEXIT_CRITICAL();
\r
449 /* Reading UBaseType_t, no critical section needed. */
\r
450 uxCount = listCURRENT_LIST_LENGTH( &xFreeBuffersList );
\r
452 if( uxMinimumFreeNetworkBuffers > uxCount )
\r
454 uxMinimumFreeNetworkBuffers = uxCount;
\r
457 /* Allocate storage of exactly the requested size to the buffer. */
\r
458 configASSERT( pxReturn->pucEthernetBuffer == NULL );
\r
460 if( xRequestedSizeBytes > 0 )
\r
462 /* Extra space is obtained so a pointer to the network buffer can
\r
463 * be stored at the beginning of the buffer. */
\r
465 #ifdef PIC32_USE_ETHERNET
\r
466 pxReturn->pucEthernetBuffer = NetworkBufferAllocate( xRequestedSizeBytes - sizeof( TCPIP_MAC_ETHERNET_HEADER ) );
\r
468 pxReturn->pucEthernetBuffer = ( uint8_t * ) pvPortMalloc( xRequestedSizeBytes + ipBUFFER_PADDING );
\r
469 #endif /* #ifdef PIC32_USE_ETHERNET */
\r
471 if( pxReturn->pucEthernetBuffer == NULL )
\r
473 /* The attempt to allocate storage for the buffer payload failed,
\r
474 * so the network buffer structure cannot be used and must be
\r
476 vReleaseNetworkBufferAndDescriptor( pxReturn );
\r
481 /* Store a pointer to the network buffer structure in the
\r
482 * buffer storage area, then move the buffer pointer on past the
\r
483 * stored pointer so the pointer value is not overwritten by the
\r
484 * application when the buffer is used. */
\r
485 #ifdef PIC32_USE_ETHERNET
\r
486 *( ( NetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer - ipBUFFER_PADDING ) ) = pxReturn;
\r
488 *( ( NetworkBufferDescriptor_t ** ) ( pxReturn->pucEthernetBuffer ) ) = pxReturn;
\r
489 pxReturn->pucEthernetBuffer += ipBUFFER_PADDING;
\r
490 #endif /* #ifdef PIC32_USE_ETHERNET */
\r
492 /* Store the actual size of the allocated buffer, which may be
\r
493 * greater than the original requested size. */
\r
494 pxReturn->xDataLength = xRequestedSizeBytes;
\r
496 #if ( ipconfigUSE_LINKED_RX_MESSAGES != 0 )
\r
498 /* make sure the buffer is not linked */
\r
499 pxReturn->pxNextBuffer = NULL;
\r
501 #endif /* ipconfigUSE_LINKED_RX_MESSAGES */
\r
506 /* A descriptor is being returned without an associated buffer being
\r
511 if( pxReturn == NULL )
\r
513 iptraceFAILED_TO_OBTAIN_NETWORK_BUFFER();
\r
517 iptraceNETWORK_BUFFER_OBTAINED( pxReturn );
\r
522 /*-----------------------------------------------------------*/
\r
524 void vReleaseNetworkBufferAndDescriptor( NetworkBufferDescriptor_t * const pxNetworkBuffer )
\r
526 BaseType_t xListItemAlreadyInFreeList;
\r
528 /* Ensure the buffer is returned to the list of free buffers before the
\r
529 * counting semaphore is 'given' to say a buffer is available. Release the
\r
530 * storage allocated to the buffer payload. THIS FILE SHOULD NOT BE USED
\r
531 * IF THE PROJECT INCLUDES A MEMORY ALLOCATOR THAT WILL FRAGMENT THE HEAP
\r
532 * MEMORY. For example, heap_2 must not be used, heap_4 can be used. */
\r
533 vReleaseNetworkBuffer( pxNetworkBuffer->pucEthernetBuffer );
\r
534 pxNetworkBuffer->pucEthernetBuffer = NULL;
\r
536 taskENTER_CRITICAL();
\r
538 xListItemAlreadyInFreeList = listIS_CONTAINED_WITHIN( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
540 if( xListItemAlreadyInFreeList == pdFALSE )
\r
542 vListInsertEnd( &xFreeBuffersList, &( pxNetworkBuffer->xBufferListItem ) );
\r
545 taskEXIT_CRITICAL();
\r
548 * Update the network state machine, unless the program fails to release its 'xNetworkBufferSemaphore'.
\r
549 * The program should only try to release its semaphore if 'xListItemAlreadyInFreeList' is false.
\r
551 if( xListItemAlreadyInFreeList == pdFALSE )
\r
553 if ( xSemaphoreGive( xNetworkBufferSemaphore ) == pdTRUE )
\r
555 iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
\r
560 iptraceNETWORK_BUFFER_RELEASED( pxNetworkBuffer );
\r
563 /*-----------------------------------------------------------*/
\r
566 * Returns the number of free network buffers
\r
568 UBaseType_t uxGetNumberOfFreeNetworkBuffers( void )
\r
570 return listCURRENT_LIST_LENGTH( &xFreeBuffersList );
\r
572 /*-----------------------------------------------------------*/
\r
574 UBaseType_t uxGetMinimumFreeNetworkBuffers( void )
\r
576 return uxMinimumFreeNetworkBuffers;
\r
578 /*-----------------------------------------------------------*/
\r
580 NetworkBufferDescriptor_t * pxResizeNetworkBufferWithDescriptor( NetworkBufferDescriptor_t * pxNetworkBuffer,
\r
581 size_t xNewSizeBytes )
\r
583 size_t xOriginalLength;
\r
584 uint8_t * pucBuffer;
\r
586 #ifdef PIC32_USE_ETHERNET
\r
587 xOriginalLength = pxNetworkBuffer->xDataLength;
\r
589 xOriginalLength = pxNetworkBuffer->xDataLength + ipBUFFER_PADDING;
\r
590 xNewSizeBytes = xNewSizeBytes + ipBUFFER_PADDING;
\r
591 #endif /* #ifdef PIC32_USE_ETHERNET */
\r
593 pucBuffer = pucGetNetworkBuffer( &( xNewSizeBytes ) );
\r
595 if( pucBuffer == NULL )
\r
597 /* In case the allocation fails, return NULL. */
\r
598 pxNetworkBuffer = NULL;
\r
602 pxNetworkBuffer->xDataLength = xNewSizeBytes;
\r
603 if( xNewSizeBytes > xOriginalLength )
\r
605 xNewSizeBytes = xOriginalLength;
\r
608 #ifdef PIC32_USE_ETHERNET
\r
609 memcpy( pucBuffer, pxNetworkBuffer->pucEthernetBuffer, xNewSizeBytes );
\r
610 *( ( NetworkBufferDescriptor_t ** ) ( pucBuffer - ipBUFFER_PADDING ) ) = pxNetworkBuffer;
\r
612 memcpy( pucBuffer - ipBUFFER_PADDING, pxNetworkBuffer->pucEthernetBuffer - ipBUFFER_PADDING, xNewSizeBytes );
\r
613 #endif /* #ifdef PIC32_USE_ETHERNET */
\r
615 vReleaseNetworkBuffer( pxNetworkBuffer->pucEthernetBuffer );
\r
616 pxNetworkBuffer->pucEthernetBuffer = pucBuffer;
\r
619 return pxNetworkBuffer;
\r