From 280dd142b16f7accf99c62829b95607ac7af8856 Mon Sep 17 00:00:00 2001 From: Holden <68555040+HTRamsey@users.noreply.github.com> Date: Tue, 9 Jan 2024 01:09:23 -0500 Subject: [PATCH 1/3] Fix declaration of socket functions without TCP (#1068) * Fix declaration of socket ID functions without TCP * Don't require TCP for GetIPType --------- Co-authored-by: Holden Co-authored-by: Holden Co-authored-by: Tony Josi --- source/FreeRTOS_Sockets.c | 70 +++++++++++++++---------------- source/include/FreeRTOS_Sockets.h | 24 +++++------ 2 files changed, 47 insertions(+), 47 deletions(-) diff --git a/source/FreeRTOS_Sockets.c b/source/FreeRTOS_Sockets.c index ca170e0503..36c216e001 100644 --- a/source/FreeRTOS_Sockets.c +++ b/source/FreeRTOS_Sockets.c @@ -5401,41 +5401,6 @@ void vSocketWakeUpUser( FreeRTOS_Socket_t * pxSocket ) #if ( ipconfigUSE_TCP == 1 ) - -/** - * @brief Get the version of IP: either 'ipTYPE_IPv4' or 'ipTYPE_IPv6'. - * - * @param[in] xSocket The socket to be checked. - * - * @return Either ipTYPE_IPv4 or ipTYPE_IPv6. - */ - BaseType_t FreeRTOS_GetIPType( ConstSocket_t xSocket ) - { - const FreeRTOS_Socket_t * pxSocket = ( const FreeRTOS_Socket_t * ) xSocket; - BaseType_t xResult = ( BaseType_t ) ipTYPE_IPv4; - - switch( pxSocket->bits.bIsIPv6 ) /* LCOV_EXCL_BR_LINE Exclude this line because default case is not counted. */ - { - #if ( ipconfigUSE_IPv4 != 0 ) - case pdFALSE_UNSIGNED: - xResult = ( BaseType_t ) ipTYPE_IPv4; - break; - #endif /* ( ipconfigUSE_IPv4 != 0 ) */ - - #if ( ipconfigUSE_IPv6 != 0 ) - case pdTRUE_UNSIGNED: - xResult = ( BaseType_t ) ipTYPE_IPv6; - break; - #endif /* ( ipconfigUSE_IPv6 != 0 ) */ - - default: - /* MISRA 16.4 Compliance */ - break; - } - - return xResult; - } - /** * @brief Check the number of bytes that may be added to txStream. * @@ -5812,6 +5777,41 @@ void * pvSocketGetSocketID( const ConstSocket_t xSocket ) } /*-----------------------------------------------------------*/ +/** + * @brief Get the version of IP: either 'ipTYPE_IPv4' or 'ipTYPE_IPv6'. + * + * @param[in] xSocket The socket to be checked. + * + * @return Either ipTYPE_IPv4 or ipTYPE_IPv6. + */ +BaseType_t FreeRTOS_GetIPType( ConstSocket_t xSocket ) +{ + const FreeRTOS_Socket_t * pxSocket = ( const FreeRTOS_Socket_t * ) xSocket; + BaseType_t xResult = ( BaseType_t ) ipTYPE_IPv4; + + switch( pxSocket->bits.bIsIPv6 ) /* LCOV_EXCL_BR_LINE Exclude this line because default case is not counted. */ + { + #if ( ipconfigUSE_IPv4 != 0 ) + case pdFALSE_UNSIGNED: + xResult = ( BaseType_t ) ipTYPE_IPv4; + break; + #endif /* ( ipconfigUSE_IPv4 != 0 ) */ + + #if ( ipconfigUSE_IPv6 != 0 ) + case pdTRUE_UNSIGNED: + xResult = ( BaseType_t ) ipTYPE_IPv6; + break; + #endif /* ( ipconfigUSE_IPv6 != 0 ) */ + + default: + /* MISRA 16.4 Compliance */ + break; + } + + return xResult; +} +/*-----------------------------------------------------------*/ + #if ( ( ipconfigHAS_PRINTF != 0 ) && ( ipconfigUSE_TCP == 1 ) ) /** diff --git a/source/include/FreeRTOS_Sockets.h b/source/include/FreeRTOS_Sockets.h index 60b127e3ee..0b1a0a1832 100644 --- a/source/include/FreeRTOS_Sockets.h +++ b/source/include/FreeRTOS_Sockets.h @@ -233,6 +233,18 @@ BaseType_t * pxHigherPriorityTaskWoken ); #endif +/* This option adds the possibility to have a user-ID attached to a socket. + * The type of this ID is a void *. Both UDP and TCP sockets have + * this ID. It has a default value of NULL. + */ + BaseType_t xSocketSetSocketID( const Socket_t xSocket, + void * pvSocketID ); + + void * pvSocketGetSocketID( const ConstSocket_t xSocket ); + +/* Get the type of IP: either 'ipTYPE_IPv4' or 'ipTYPE_IPv6'. */ + BaseType_t FreeRTOS_GetIPType( ConstSocket_t xSocket ); + /* End Common Socket Attributes */ @@ -352,9 +364,6 @@ BaseType_t FreeRTOS_GetRemoteAddress( ConstSocket_t xSocket, struct freertos_sockaddr * pxAddress ); -/* Get the type of IP: either 'ipTYPE_IPv4' or 'ipTYPE_IPv6'. */ - BaseType_t FreeRTOS_GetIPType( ConstSocket_t xSocket ); - /* Returns the number of bytes that may be added to txStream. */ BaseType_t FreeRTOS_maywrite( ConstSocket_t xSocket ); @@ -383,15 +392,6 @@ void FreeRTOS_netstat( void ); -/* This option adds the possibility to have a user-ID attached to a socket. - * The type of this ID is a void *. Both UDP and TCP sockets have - * this ID. It has a default value of NULL. - */ - BaseType_t xSocketSetSocketID( const Socket_t xSocket, - void * pvSocketID ); - - void * pvSocketGetSocketID( const ConstSocket_t xSocket ); - /* End TCP Socket Attributes. */ #endif /* ( ipconfigUSE_TCP == 1 ) */ From 15b2f81bd486f639869668daea9e1c918de10b7f Mon Sep 17 00:00:00 2001 From: Emil Popov Date: Tue, 9 Jan 2024 08:02:08 -0500 Subject: [PATCH 2/3] No searching for end-points when responding to mDNS/LLMNR/NBNS (#1064) * When responding to a mDNS/LLMNR/NBNS query, uses the endpoint selected during the reception of the query instead of searching for one. * Removes prvFindEndPointOnNetMask( NetworkBufferDescriptor_t * pxNetworkBuffer ) which is no longer needed. * Updates the DNS parser unit test. Thanks @htibosch * Fix CBMC proof for prepareReplyDNSMessage * Updates the DNS parser unit test. Thanks @htibosch * Uncrustify: triggered by comment --------- Co-authored-by: Emil Popov Co-authored-by: Tony Josi Co-authored-by: GitHub Action --- source/FreeRTOS_DNS_Parser.c | 46 ++----------------- .../prepareReplyDNSMessage_harness.c | 1 + .../FreeRTOS_DNS_Parser_utest.c | 46 +++++++++++++------ 3 files changed, 37 insertions(+), 56 deletions(-) diff --git a/source/FreeRTOS_DNS_Parser.c b/source/FreeRTOS_DNS_Parser.c index dbe426f50d..b13ba06124 100644 --- a/source/FreeRTOS_DNS_Parser.c +++ b/source/FreeRTOS_DNS_Parser.c @@ -47,47 +47,6 @@ #if ( ipconfigUSE_DNS != 0 ) -/** @brief The list of all callback structures. */ - - - #if ( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) || ( ipconfigUSE_MDNS == 1 ) ) - -/** - * @brief Find the best matching end-point given a reply that was received. - * @param[in] pxNetworkBuffer The Ethernet packet that was received. - * @return An end-point. - */ - static NetworkEndPoint_t * prvFindEndPointOnNetMask( NetworkBufferDescriptor_t * pxNetworkBuffer ) - { - NetworkEndPoint_t * pxEndPoint = NULL; - - #if ( ipconfigUSE_IPv6 != 0 ) - IPPacket_IPv6_t * xIPPacket_IPv6 = ( ( IPPacket_IPv6_t * ) pxNetworkBuffer->pucEthernetBuffer ); - - if( xIPPacket_IPv6->xEthernetHeader.usFrameType == ipIPv6_FRAME_TYPE ) - { - pxEndPoint = FreeRTOS_FindEndPointOnNetMask_IPv6( &xIPPacket_IPv6->xIPHeader.xSourceAddress ); - } - else - #endif /* ( ipconfigUSE_IPv6 != 0 ) */ - - #if ( ipconfigUSE_IPv4 != 0 ) - { - IPPacket_t * xIPPacket = ( ( IPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer ); - - pxEndPoint = FreeRTOS_FindEndPointOnNetMask( xIPPacket->xIPHeader.ulSourceIPAddress, 6 ); - } - #endif /* ( ipconfigUSE_IPv4 != 0 ) */ - - if( pxEndPoint != NULL ) - { - pxNetworkBuffer->pxEndPoint = pxEndPoint; - } - - return pxEndPoint; - } - #endif /* ( ( ipconfigUSE_NBNS == 1 ) || ( ipconfigUSE_LLMNR == 1 ) || ( ipconfigUSE_MDNS == 1 ) ) */ -/*-----------------------------------------------------------*/ /** * @brief Read the Name field out of a DNS response packet. @@ -911,9 +870,10 @@ IPHeader_t * pxIPHeader; UDPHeader_t * pxUDPHeader; size_t uxDataLength; - NetworkEndPoint_t * pxEndPoint = prvFindEndPointOnNetMask( pxNetworkBuffer ); + NetworkEndPoint_t * pxEndPoint = pxNetworkBuffer->pxEndPoint; const size_t uxIPHeaderLength = uxIPHeaderSizePacket( pxNetworkBuffer ); + configASSERT( pxEndPoint != NULL ); pxUDPPacket = ( ( UDPPacket_t * ) pxNetworkBuffer->pucEthernetBuffer ); pxIPHeader = &pxUDPPacket->xIPHeader; @@ -959,7 +919,7 @@ pxIPHeader->ucTimeToLive = ipconfigUDP_TIME_TO_LIVE; } - pxIPHeader->ulSourceIPAddress = ( pxEndPoint != NULL ) ? pxEndPoint->ipv4_settings.ulIPAddress : 0U; + pxIPHeader->ulSourceIPAddress = pxEndPoint->ipv4_settings.ulIPAddress; pxIPHeader->usIdentification = FreeRTOS_htons( usPacketIdentifier ); /* The stack doesn't support fragments, so the fragment offset field must always be zero. diff --git a/test/cbmc/proofs/DNS/prepareReplyDNSMessage/prepareReplyDNSMessage_harness.c b/test/cbmc/proofs/DNS/prepareReplyDNSMessage/prepareReplyDNSMessage_harness.c index 6ca377862b..851bd44744 100644 --- a/test/cbmc/proofs/DNS/prepareReplyDNSMessage/prepareReplyDNSMessage_harness.c +++ b/test/cbmc/proofs/DNS/prepareReplyDNSMessage/prepareReplyDNSMessage_harness.c @@ -106,6 +106,7 @@ void harness() __CPROVER_assume( ( xDataSize > ( sizeof( UDPPacket_t ) + sizeof( NBNSRequest_t ) + sizeof( NBNSAnswer_t ) - 2 * sizeof( uint16_t ) ) ) && ( xDataSize < ipconfigNETWORK_MTU ) ); xNetworkBuffer.pucEthernetBuffer = safeMalloc( xDataSize ); + xNetworkBuffer.pxEndPoint = pxNetworkEndPoint_Temp; /* xNetworkBuffer.pucEthernetBuffer is checked if its valid before the call to * prepareReplyDNSMessage() */ diff --git a/test/unit-test/FreeRTOS_DNS_Parser/FreeRTOS_DNS_Parser_utest.c b/test/unit-test/FreeRTOS_DNS_Parser/FreeRTOS_DNS_Parser_utest.c index 0037524f25..0eab41c755 100644 --- a/test/unit-test/FreeRTOS_DNS_Parser/FreeRTOS_DNS_Parser_utest.c +++ b/test/unit-test/FreeRTOS_DNS_Parser/FreeRTOS_DNS_Parser_utest.c @@ -505,6 +505,7 @@ void test_prepareReplyDNSMessage_success( void ) pxNetworkBuffer.pucEthernetBuffer = ether_buffer; pxNetworkBuffer.xDataLength = 300; + pxNetworkBuffer.pxEndPoint = &xEndPoint; BaseType_t lNetLength = 50; @@ -522,7 +523,7 @@ void test_prepareReplyDNSMessage_success( void ) pxIPHeader->ulSourceIPAddress = 1234; uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv4_HEADER ); - FreeRTOS_FindEndPointOnNetMask_ExpectAndReturn( xIPPacket->xIPHeader.ulSourceIPAddress, 6, &xEndPoint ); + usGenerateChecksum_ExpectAnyArgsAndReturn( 555 ); usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 444 ); @@ -548,7 +549,7 @@ void test_prepareReplyDNSMessage_success_MDNS( void ) pxNetworkBuffer.pucEthernetBuffer = ether_buffer; pxNetworkBuffer.xDataLength = 300; - + pxNetworkBuffer.pxEndPoint = &xEndPoint; BaseType_t lNetLength = 50; UDPPacket_t * pxUDPPacket; @@ -566,7 +567,7 @@ void test_prepareReplyDNSMessage_success_MDNS( void ) pxIPHeader->ulDestinationIPAddress = ipMDNS_IP_ADDRESS; uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv4_HEADER ); - FreeRTOS_FindEndPointOnNetMask_ExpectAndReturn( xIPPacket->xIPHeader.ulSourceIPAddress, 6, &xEndPoint ); + usGenerateChecksum_ExpectAnyArgsAndReturn( 555 ); usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 444 ); @@ -592,6 +593,7 @@ void test_prepareReplyDNSMessage_NullEndPoint( void ) pxNetworkBuffer.pucEthernetBuffer = ether_buffer; pxNetworkBuffer.xDataLength = 300; + pxNetworkBuffer.pxEndPoint = &xEndPoint; BaseType_t lNetLength = 50; @@ -609,7 +611,7 @@ void test_prepareReplyDNSMessage_NullEndPoint( void ) pxIPHeader->ulSourceIPAddress = 1234; uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv4_HEADER ); - FreeRTOS_FindEndPointOnNetMask_ExpectAndReturn( xIPPacket->xIPHeader.ulSourceIPAddress, 6, NULL ); + usGenerateChecksum_ExpectAnyArgsAndReturn( 555 ); usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 444 ); @@ -634,6 +636,7 @@ void test_prepareReplyDNSMessage_IPv6success( void ) pxNetworkBuffer.pucEthernetBuffer = ether_buffer; pxNetworkBuffer.xDataLength = 300; + pxNetworkBuffer.pxEndPoint = &xEndPoint; BaseType_t lNetLength = 50; @@ -654,7 +657,7 @@ void test_prepareReplyDNSMessage_IPv6success( void ) pxIPHeader->ucVersionHeaderLength = 0x60U; uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv6_HEADER ); - FreeRTOS_FindEndPointOnNetMask_IPv6_ExpectAndReturn( xIPPacket_IPv6->xIPHeader.xSourceAddress.ucBytes, &xEndPoint ); + usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 444 ); prepareReplyDNSMessage( &pxNetworkBuffer, @@ -678,7 +681,7 @@ void test_prepareReplyDNSMessage_IPv6Fail( void ) pxNetworkBuffer.pucEthernetBuffer = ether_buffer; pxNetworkBuffer.xDataLength = 300; - + pxNetworkBuffer.pxEndPoint = &xEndPoint; BaseType_t lNetLength = 50; UDPPacket_t * pxUDPPacket; @@ -696,7 +699,7 @@ void test_prepareReplyDNSMessage_IPv6Fail( void ) pxIPHeader->ucVersionHeaderLength = 0x0U; uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv6_HEADER ); - FreeRTOS_FindEndPointOnNetMask_ExpectAndReturn( xIPPacket->xIPHeader.ulSourceIPAddress, 6, &xEndPoint ); + usGenerateChecksum_ExpectAnyArgsAndReturn( 555 ); usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 444 ); @@ -964,7 +967,7 @@ void test_DNS_TreatNBNS_success_nbns_non_fixed_size_buffer( void ) usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS ); /* usType */ usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_QUERY ); pxResizeNetworkBufferWithDescriptor_ExpectAnyArgsAndReturn( &pxNewBuffer ); - FreeRTOS_FindEndPointOnNetMask_ExpectAnyArgsAndReturn( &xEndPoint ); + usGenerateChecksum_ExpectAnyArgsAndReturn( 4 ); usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 4 ); vReturnEthernetFrame_Expect( &pxNetworkBuffer, pdFALSE ); /* goal */ @@ -1031,7 +1034,7 @@ void test_DNS_TreatNBNS_success_BufferAllocation1( void ) usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_QUERY ); /* usFlags */ usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS ); /* usType */ usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_QUERY ); - FreeRTOS_FindEndPointOnNetMask_ExpectAnyArgsAndReturn( &xEndPoint ); + usGenerateChecksum_ExpectAnyArgsAndReturn( 4 ); usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 4 ); vReturnEthernetFrame_Expect( &pxNetworkBuffer, pdFALSE ); /* goal */ @@ -1126,7 +1129,7 @@ void test_DNS_TreatNBNS_success_nbns_non_fixed_size_buffer2( void ) usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_TYPE_NET_BIOS ); /* usType */ usChar2u16_ExpectAnyArgsAndReturn( dnsNBNS_FLAGS_OPCODE_QUERY ); pxResizeNetworkBufferWithDescriptor_ExpectAnyArgsAndReturn( &pxNetworkBuffer ); - FreeRTOS_FindEndPointOnNetMask_ExpectAnyArgsAndReturn( &xEndPoint ); + usGenerateChecksum_ExpectAnyArgsAndReturn( 4 ); usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 4 ); vReturnEthernetFrame_Expect( &pxNetworkBuffer, pdFALSE ); /* goal */ @@ -2382,7 +2385,7 @@ void test_DNS_ParseDNSReply_answer_lmmnr_reply_valid_new_netbuffer( void ) hook_return = pdTRUE; pxUDPPayloadBuffer_to_NetworkBuffer_ExpectAnyArgsAndReturn( &pxNetworkBuffer ); pxDuplicateNetworkBufferWithDescriptor_ExpectAnyArgsAndReturn( &pxNetworkBuffer ); - FreeRTOS_FindEndPointOnNetMask_ExpectAndReturn( xIPPacket->xIPHeader.ulSourceIPAddress, 6, &xEndPoint ); + usGenerateChecksum_ExpectAnyArgsAndReturn( 555 ); usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 444 ); vReturnEthernetFrame_Expect( &pxNetworkBuffer, pdFALSE ); @@ -2473,7 +2476,7 @@ void test_DNS_ParseDNSReply_answer_lmmnr_reply_valid_new_netbuffer2( void ) hook_return = pdTRUE; pxUDPPayloadBuffer_to_NetworkBuffer_ExpectAnyArgsAndReturn( &pxNetworkBuffer ); pxDuplicateNetworkBufferWithDescriptor_ExpectAnyArgsAndReturn( &pxNetworkBuffer ); - FreeRTOS_FindEndPointOnNetMask_ExpectAndReturn( xIPPacket->xIPHeader.ulSourceIPAddress, 6, &xEndPoint ); + usGenerateChecksum_ExpectAnyArgsAndReturn( 555 ); usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 444 ); vReturnEthernetFrame_Expect( &pxNetworkBuffer, pdFALSE ); @@ -2564,7 +2567,7 @@ void test_DNS_ParseDNSReply_answer_lmmnr_reply_valid_new_netbuffer3( void ) hook_return = pdTRUE; xBufferAllocFixedSize = pdTRUE; pxUDPPayloadBuffer_to_NetworkBuffer_ExpectAnyArgsAndReturn( &pxNetworkBuffer ); - FreeRTOS_FindEndPointOnNetMask_ExpectAndReturn( xIPPacket->xIPHeader.ulSourceIPAddress, 6, &xEndPoint ); + usGenerateChecksum_ExpectAnyArgsAndReturn( 555 ); usGenerateProtocolChecksum_ExpectAnyArgsAndReturn( 444 ); vReturnEthernetFrame_Expect( &pxNetworkBuffer, pdFALSE ); @@ -3416,3 +3419,20 @@ BaseType_t xApplicationDNSQueryHook_Multi( struct xNetworkEndPoint * pxEndPoint, hook_called = pdTRUE; return hook_return; } + +void test_prepareReplyDNSMessage_null_pointer( void ) +{ + NetworkBufferDescriptor_t pxNetworkBuffer = { 0 }; + uint8_t ether_buffer[ 300 ] = { 0 }; + size_t uxDataLength; + BaseType_t lNetLength = 54; + NetworkEndPoint_t xEndPoint = { 0 }; + + pxNetworkBuffer.pucEthernetBuffer = ether_buffer; + pxNetworkBuffer.xDataLength = 300; + /* This will cause an assert(). */ + pxNetworkBuffer.pxEndPoint = NULL; + + uxIPHeaderSizePacket_IgnoreAndReturn( ipSIZE_OF_IPv4_HEADER ); + catch_assert( prepareReplyDNSMessage( &pxNetworkBuffer, lNetLength ) ); +} From 7af0bfe578d82f070b783c0a39e620ce4725cd1c Mon Sep 17 00:00:00 2001 From: Holden <68555040+HTRamsey@users.noreply.github.com> Date: Tue, 9 Jan 2024 23:36:44 -0500 Subject: [PATCH 3/3] Stream Buffer Const Correctness (#1067) * Stream Buffer Const Correctness * cleanup & simplify * Uncrustify: triggered by comment. * fix test * add const for pass by value parameters * don't change parameters * remove pass by value parameter consts --------- Co-authored-by: Holden Co-authored-by: Holden <68555040+holden-zenith@users.noreply.github.com> Co-authored-by: GitHub Action Co-authored-by: Holden Co-authored-by: Tony Josi --- source/FreeRTOS_Stream_Buffer.c | 146 +++++++++++------------- source/include/FreeRTOS_Stream_Buffer.h | 80 ++++--------- 2 files changed, 91 insertions(+), 135 deletions(-) diff --git a/source/FreeRTOS_Stream_Buffer.c b/source/FreeRTOS_Stream_Buffer.c index 0552d6b1e3..21dd205e47 100644 --- a/source/FreeRTOS_Stream_Buffer.c +++ b/source/FreeRTOS_Stream_Buffer.c @@ -32,17 +32,14 @@ /* Standard includes. */ #include +#include /* FreeRTOS includes. */ #include "FreeRTOS.h" #include "task.h" -#include "semphr.h" /* FreeRTOS+TCP includes. */ #include "FreeRTOS_IP.h" -#include "FreeRTOS_UDP_IP.h" -#include "FreeRTOS_Sockets.h" -#include "FreeRTOS_IP_Private.h" /** @@ -53,21 +50,21 @@ * @return The space between uxLower and uxUpper, which equals to the distance * minus 1. */ -size_t uxStreamBufferSpace( const StreamBuffer_t * pxBuffer, - const size_t uxLower, - const size_t uxUpper ) +size_t uxStreamBufferSpace( const StreamBuffer_t * const pxBuffer, + size_t uxLower, + size_t uxUpper ) { - size_t uxCount; - - uxCount = pxBuffer->LENGTH + uxUpper - uxLower - 1U; + const size_t uxLength = pxBuffer->LENGTH; + size_t uxCount = uxLength + uxUpper - uxLower - 1U; - if( uxCount >= pxBuffer->LENGTH ) + if( uxCount >= uxLength ) { - uxCount -= pxBuffer->LENGTH; + uxCount -= uxLength; } return uxCount; } +/*-----------------------------------------------------------*/ /** * @brief Get the distance between lower and upper value provided to the function. @@ -76,21 +73,21 @@ size_t uxStreamBufferSpace( const StreamBuffer_t * pxBuffer, * @param[in] uxUpper The upper value. * @return The distance between uxLower and uxUpper. */ -size_t uxStreamBufferDistance( const StreamBuffer_t * pxBuffer, - const size_t uxLower, - const size_t uxUpper ) +size_t uxStreamBufferDistance( const StreamBuffer_t * const pxBuffer, + size_t uxLower, + size_t uxUpper ) { - size_t uxCount; - - uxCount = pxBuffer->LENGTH + uxUpper - uxLower; + const size_t uxLength = pxBuffer->LENGTH; + size_t uxCount = uxLength + uxUpper - uxLower; - if( uxCount >= pxBuffer->LENGTH ) + if( uxCount >= uxLength ) { - uxCount -= pxBuffer->LENGTH; + uxCount -= uxLength; } return uxCount; } +/*-----------------------------------------------------------*/ /** * @brief Get the number of items which can be added to the buffer at @@ -99,12 +96,9 @@ size_t uxStreamBufferDistance( const StreamBuffer_t * pxBuffer, * @return The number of items which can still be added to uxHead * before hitting on uxTail */ -size_t uxStreamBufferGetSpace( const StreamBuffer_t * pxBuffer ) +size_t uxStreamBufferGetSpace( const StreamBuffer_t * const pxBuffer ) { - size_t uxHead = pxBuffer->uxHead; - size_t uxTail = pxBuffer->uxTail; - - return uxStreamBufferSpace( pxBuffer, uxHead, uxTail ); + return uxStreamBufferSpace( pxBuffer, pxBuffer->uxHead, pxBuffer->uxTail ); } /*-----------------------------------------------------------*/ @@ -114,12 +108,9 @@ size_t uxStreamBufferGetSpace( const StreamBuffer_t * pxBuffer ) * @return Distance between uxFront and uxTail or the number of items * which can still be added to uxFront, before hitting on uxTail. */ -size_t uxStreamBufferFrontSpace( const StreamBuffer_t * pxBuffer ) +size_t uxStreamBufferFrontSpace( const StreamBuffer_t * const pxBuffer ) { - size_t uxFront = pxBuffer->uxFront; - size_t uxTail = pxBuffer->uxTail; - - return uxStreamBufferSpace( pxBuffer, uxFront, uxTail ); + return uxStreamBufferSpace( pxBuffer, pxBuffer->uxFront, pxBuffer->uxTail ); } /*-----------------------------------------------------------*/ @@ -130,12 +121,9 @@ size_t uxStreamBufferFrontSpace( const StreamBuffer_t * pxBuffer ) * @return The number of items which can be read from the tail before * reaching the head. */ -size_t uxStreamBufferGetSize( const StreamBuffer_t * pxBuffer ) +size_t uxStreamBufferGetSize( const StreamBuffer_t * const pxBuffer ) { - size_t uxHead = pxBuffer->uxHead; - size_t uxTail = pxBuffer->uxTail; - - return uxStreamBufferDistance( pxBuffer, uxTail, uxHead ); + return uxStreamBufferDistance( pxBuffer, pxBuffer->uxTail, pxBuffer->uxHead ); } /*-----------------------------------------------------------*/ @@ -145,20 +133,17 @@ size_t uxStreamBufferGetSize( const StreamBuffer_t * pxBuffer ) * @param[in] pxBuffer The circular stream buffer. * @return The space between the mid pointer and the head. */ -size_t uxStreamBufferMidSpace( const StreamBuffer_t * pxBuffer ) +size_t uxStreamBufferMidSpace( const StreamBuffer_t * const pxBuffer ) { - size_t uxHead = pxBuffer->uxHead; - size_t uxMid = pxBuffer->uxMid; - - return uxStreamBufferDistance( pxBuffer, uxMid, uxHead ); + return uxStreamBufferDistance( pxBuffer, pxBuffer->uxMid, pxBuffer->uxHead ); } /*-----------------------------------------------------------*/ /** - * @brief Move Clear the stream buffer. + * @brief Clear the stream buffer. * @param[in] pxBuffer The circular stream buffer. */ -void vStreamBufferClear( StreamBuffer_t * pxBuffer ) +void vStreamBufferClear( StreamBuffer_t * const pxBuffer ) { /* Make the circular buffer empty */ pxBuffer->uxHead = 0U; @@ -174,11 +159,12 @@ void vStreamBufferClear( StreamBuffer_t * pxBuffer ) * @param[in] pxBuffer The circular stream buffer. * @param[in] uxCount The byte count by which the mid pointer is to be moved. */ -void vStreamBufferMoveMid( StreamBuffer_t * pxBuffer, +void vStreamBufferMoveMid( StreamBuffer_t * const pxBuffer, size_t uxCount ) { /* Increment uxMid, but no further than uxHead */ - size_t uxSize = uxStreamBufferMidSpace( pxBuffer ); + const size_t uxLength = pxBuffer->LENGTH; + const size_t uxSize = uxStreamBufferMidSpace( pxBuffer ); size_t uxMid = pxBuffer->uxMid; size_t uxMoveCount = uxCount; @@ -189,9 +175,9 @@ void vStreamBufferMoveMid( StreamBuffer_t * pxBuffer, uxMid += uxMoveCount; - if( uxMid >= pxBuffer->LENGTH ) + if( uxMid >= uxLength ) { - uxMid -= pxBuffer->LENGTH; + uxMid -= uxLength; } pxBuffer->uxMid = uxMid; @@ -207,14 +193,13 @@ void vStreamBufferMoveMid( StreamBuffer_t * pxBuffer, * @param[in] uxRight The right value pointer in the stream buffer. * @return pdTRUE if uxLeft <= uxRight, else pdFALSE. */ -BaseType_t xStreamBufferLessThenEqual( const StreamBuffer_t * pxBuffer, - const size_t uxLeft, - const size_t uxRight ) +BaseType_t xStreamBufferLessThenEqual( const StreamBuffer_t * const pxBuffer, + size_t uxLeft, + size_t uxRight ) { BaseType_t xReturn = pdFALSE; - size_t uxTail = pxBuffer->uxTail; + const size_t uxTail = pxBuffer->uxTail; - /* Returns true if ( uxLeft <= uxRight ) */ if( ( uxLeft - uxTail ) <= ( uxRight - uxTail ) ) { xReturn = pdTRUE; @@ -235,11 +220,11 @@ BaseType_t xStreamBufferLessThenEqual( const StreamBuffer_t * pxBuffer, * actual number of available bytes since this is a circular buffer and tail * can loop back to the start of the buffer). */ -size_t uxStreamBufferGetPtr( StreamBuffer_t * pxBuffer, - uint8_t ** ppucData ) +size_t uxStreamBufferGetPtr( StreamBuffer_t * const pxBuffer, + uint8_t ** const ppucData ) { - size_t uxNextTail = pxBuffer->uxTail; - size_t uxSize = uxStreamBufferGetSize( pxBuffer ); + const size_t uxNextTail = pxBuffer->uxTail; + const size_t uxSize = uxStreamBufferGetSize( pxBuffer ); *ppucData = pxBuffer->ucArray + uxNextTail; @@ -259,15 +244,13 @@ size_t uxStreamBufferGetPtr( StreamBuffer_t * pxBuffer, * * @return The number of bytes added to the buffer. */ -size_t uxStreamBufferAdd( StreamBuffer_t * pxBuffer, +size_t uxStreamBufferAdd( StreamBuffer_t * const pxBuffer, size_t uxOffset, - const uint8_t * pucData, + const uint8_t * const pucData, size_t uxByteCount ) { - size_t uxSpace, uxNextHead, uxFirst; - size_t uxCount = uxByteCount; - - uxSpace = uxStreamBufferGetSpace( pxBuffer ); + size_t uxCount; + size_t uxSpace = uxStreamBufferGetSpace( pxBuffer ); /* If uxOffset > 0, items can be placed in front of uxHead */ if( uxSpace > uxOffset ) @@ -281,20 +264,21 @@ size_t uxStreamBufferAdd( StreamBuffer_t * pxBuffer, /* The number of bytes that can be written is the minimum of the number of * bytes requested and the number available. */ - uxCount = FreeRTOS_min_size_t( uxSpace, uxCount ); + uxCount = FreeRTOS_min_size_t( uxSpace, uxByteCount ); if( uxCount != 0U ) { - uxNextHead = pxBuffer->uxHead; + const size_t uxLength = pxBuffer->LENGTH; + size_t uxNextHead = pxBuffer->uxHead; if( uxOffset != 0U ) { /* ( uxOffset > 0 ) means: write in front if the uxHead marker */ uxNextHead += uxOffset; - if( uxNextHead >= pxBuffer->LENGTH ) + if( uxNextHead >= uxLength ) { - uxNextHead -= pxBuffer->LENGTH; + uxNextHead -= uxLength; } } @@ -303,7 +287,7 @@ size_t uxStreamBufferAdd( StreamBuffer_t * pxBuffer, /* Calculate the number of bytes that can be added in the first * write - which may be less than the total number of bytes that need * to be added if the buffer will wrap back to the beginning. */ - uxFirst = FreeRTOS_min_size_t( pxBuffer->LENGTH - uxNextHead, uxCount ); + const size_t uxFirst = FreeRTOS_min_size_t( uxLength - uxNextHead, uxCount ); /* Write as many bytes as can be written in the first write. */ ( void ) memcpy( &( pxBuffer->ucArray[ uxNextHead ] ), pucData, uxFirst ); @@ -327,9 +311,9 @@ size_t uxStreamBufferAdd( StreamBuffer_t * pxBuffer, /* ( uxOffset == 0 ) means: write at uxHead position */ uxNextHead += uxCount; - if( uxNextHead >= pxBuffer->LENGTH ) + if( uxNextHead >= uxLength ) { - uxNextHead -= pxBuffer->LENGTH; + uxNextHead -= uxLength; } pxBuffer->uxHead = uxNextHead; @@ -360,16 +344,16 @@ size_t uxStreamBufferAdd( StreamBuffer_t * pxBuffer, * * @return The count of the bytes read. */ -size_t uxStreamBufferGet( StreamBuffer_t * pxBuffer, +size_t uxStreamBufferGet( StreamBuffer_t * const pxBuffer, size_t uxOffset, - uint8_t * pucData, + uint8_t * const pucData, size_t uxMaxCount, BaseType_t xPeek ) { - size_t uxSize, uxCount, uxFirst, uxNextTail; + size_t uxCount; /* How much data is available? */ - uxSize = uxStreamBufferGetSize( pxBuffer ); + size_t uxSize = uxStreamBufferGetSize( pxBuffer ); if( uxSize > uxOffset ) { @@ -383,17 +367,18 @@ size_t uxStreamBufferGet( StreamBuffer_t * pxBuffer, /* Use the minimum of the wanted bytes and the available bytes. */ uxCount = FreeRTOS_min_size_t( uxSize, uxMaxCount ); - if( uxCount > 0U ) + if( uxCount != 0U ) { - uxNextTail = pxBuffer->uxTail; + const size_t uxLength = pxBuffer->LENGTH; + size_t uxNextTail = pxBuffer->uxTail; if( uxOffset != 0U ) { uxNextTail += uxOffset; - if( uxNextTail >= pxBuffer->LENGTH ) + if( uxNextTail >= uxLength ) { - uxNextTail -= pxBuffer->LENGTH; + uxNextTail -= uxLength; } } @@ -402,7 +387,7 @@ size_t uxStreamBufferGet( StreamBuffer_t * pxBuffer, /* Calculate the number of bytes that can be read - which may be * less than the number wanted if the data wraps around to the start of * the buffer. */ - uxFirst = FreeRTOS_min_size_t( pxBuffer->LENGTH - uxNextTail, uxCount ); + const size_t uxFirst = FreeRTOS_min_size_t( uxLength - uxNextTail, uxCount ); /* Obtain the number of bytes it is possible to obtain in the first * read. */ @@ -412,7 +397,7 @@ size_t uxStreamBufferGet( StreamBuffer_t * pxBuffer, * that could be read in the first read... */ if( uxCount > uxFirst ) { - /*...then read the remaining bytes from the start of the buffer. */ + /* ...then read the remaining bytes from the start of the buffer. */ ( void ) memcpy( &( pucData[ uxFirst ] ), pxBuffer->ucArray, uxCount - uxFirst ); } } @@ -423,9 +408,9 @@ size_t uxStreamBufferGet( StreamBuffer_t * pxBuffer, * the buffer. */ uxNextTail += uxCount; - if( uxNextTail >= pxBuffer->LENGTH ) + if( uxNextTail >= uxLength ) { - uxNextTail -= pxBuffer->LENGTH; + uxNextTail -= uxLength; } pxBuffer->uxTail = uxNextTail; @@ -434,3 +419,4 @@ size_t uxStreamBufferGet( StreamBuffer_t * pxBuffer, return uxCount; } +/*-----------------------------------------------------------*/ diff --git a/source/include/FreeRTOS_Stream_Buffer.h b/source/include/FreeRTOS_Stream_Buffer.h index 70182e5bc7..c9dcc8e613 100644 --- a/source/include/FreeRTOS_Stream_Buffer.h +++ b/source/include/FreeRTOS_Stream_Buffer.h @@ -28,8 +28,7 @@ /* * FreeRTOS_Stream_Buffer.h * - * A circular character buffer - * An implementation of a circular buffer without a length field + * An implementation of a circular character buffer without a length field * If LENGTH defines the size of the buffer, a maximum of (LENGTH-1) bytes can be stored * In order to add or read data from the buffer, memcpy() will be called at most 2 times */ @@ -43,9 +42,7 @@ #endif /* *INDENT-ON* */ -/** - * structure to store all the details of a stream buffer. - */ +/* Struct to store all the details of a stream buffer. */ typedef struct xSTREAM_BUFFER { volatile size_t uxTail; /**< next item to read */ @@ -56,69 +53,42 @@ typedef struct xSTREAM_BUFFER uint8_t ucArray[ sizeof( size_t ) ]; /**< array big enough to store any pointer address */ } StreamBuffer_t; -void vStreamBufferClear( StreamBuffer_t * pxBuffer ); -/*-----------------------------------------------------------*/ - -size_t uxStreamBufferSpace( const StreamBuffer_t * pxBuffer, - const size_t uxLower, - const size_t uxUpper ); -/*-----------------------------------------------------------*/ +size_t uxStreamBufferSpace( const StreamBuffer_t * const pxBuffer, + size_t uxLower, + size_t uxUpper ); -size_t uxStreamBufferDistance( const StreamBuffer_t * pxBuffer, - const size_t uxLower, - const size_t uxUpper ); -/*-----------------------------------------------------------*/ +size_t uxStreamBufferDistance( const StreamBuffer_t * const pxBuffer, + size_t uxLower, + size_t uxUpper ); -size_t uxStreamBufferGetSpace( const StreamBuffer_t * pxBuffer ); -/*-----------------------------------------------------------*/ +size_t uxStreamBufferGetSpace( const StreamBuffer_t * const pxBuffer ); -size_t uxStreamBufferFrontSpace( const StreamBuffer_t * pxBuffer ); -/*-----------------------------------------------------------*/ +size_t uxStreamBufferFrontSpace( const StreamBuffer_t * const pxBuffer ); -size_t uxStreamBufferGetSize( const StreamBuffer_t * pxBuffer ); -/*-----------------------------------------------------------*/ +size_t uxStreamBufferGetSize( const StreamBuffer_t * const pxBuffer ); size_t uxStreamBufferMidSpace( const StreamBuffer_t * pxBuffer ); -/*-----------------------------------------------------------*/ -void vStreamBufferMoveMid( StreamBuffer_t * pxBuffer, - size_t uxCount ); -/*-----------------------------------------------------------*/ +void vStreamBufferClear( StreamBuffer_t * const pxBuffer ); -BaseType_t xStreamBufferLessThenEqual( const StreamBuffer_t * pxBuffer, - const size_t uxLeft, - const size_t uxRight ); -/*-----------------------------------------------------------*/ +void vStreamBufferMoveMid( StreamBuffer_t * const pxBuffer, + const size_t uxCount ); -size_t uxStreamBufferGetPtr( StreamBuffer_t * pxBuffer, - uint8_t ** ppucData ); +BaseType_t xStreamBufferLessThenEqual( const StreamBuffer_t * const pxBuffer, + size_t uxLeft, + size_t uxRight ); -/* - * Add bytes to a stream buffer. - * - * pxBuffer - The buffer to which the bytes will be added. - * uxOffset - If uxOffset > 0, data will be written at an offset from uxHead - * while uxHead will not be moved yet. - * pucData - A pointer to the data to be added. - * uxCount - The number of bytes to add. - */ -size_t uxStreamBufferAdd( StreamBuffer_t * pxBuffer, +size_t uxStreamBufferGetPtr( StreamBuffer_t * const pxBuffer, + uint8_t ** const ppucData ); + +size_t uxStreamBufferAdd( StreamBuffer_t * const pxBuffer, size_t uxOffset, - const uint8_t * pucData, + const uint8_t * const pucData, size_t uxByteCount ); -/* - * Read bytes from a stream buffer. - * - * pxBuffer - The buffer from which the bytes will be read. - * uxOffset - Can be used to read data located at a certain offset from 'uxTail'. - * pucData - A pointer to the buffer into which data will be read. - * uxMaxCount - The number of bytes to read. - * xPeek - If set to pdTRUE the data will remain in the buffer. - */ -size_t uxStreamBufferGet( StreamBuffer_t * pxBuffer, +size_t uxStreamBufferGet( StreamBuffer_t * const pxBuffer, size_t uxOffset, - uint8_t * pucData, + uint8_t * const pucData, size_t uxMaxCount, BaseType_t xPeek ); @@ -128,4 +98,4 @@ size_t uxStreamBufferGet( StreamBuffer_t * pxBuffer, #endif /* *INDENT-ON* */ -#endif /* !defined( FREERTOS_STREAM_BUFFER_H ) */ +#endif /* FREERTOS_STREAM_BUFFER_H */