Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

[*.{c,h,cpp,hpp}]
indent_style = space
indent_size = 4

[*.{yml,yaml}]
indent_style = space
indent_size = 2

[*.md]
trim_trailing_whitespace = false

[Makefile]
indent_style = tab
130 changes: 130 additions & 0 deletions include/stream_buffer_ext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* FreeRTOS Kernel V11.1.0
* Copyright (C) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* SPDX-License-Identifier: MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* https://www.FreeRTOS.org
* https://github.com/FreeRTOS
*/

/**
* @file stream_buffer_ext.h
* @brief Extended stream buffer operations: peek, snapshot, and inspection
* without consuming data from the buffer.
*
* These functions are designed to work alongside the core stream_buffer API
* and provide read-only inspection capabilities.
*/

#ifndef STREAM_BUFFER_EXT_H
#define STREAM_BUFFER_EXT_H

#include "FreeRTOS.h"
#include "stream_buffer.h"

#if defined( __cplusplus )
extern "C" {
#endif

/**
* @brief Opaque snapshot of stream buffer state at a point in time.
*/
typedef struct StreamBufferSnapshot
{
size_t xReadableBytes; /**< Number of bytes available to read. */
size_t xWritableBytes; /**< Number of bytes available to write. */
size_t xBufferCapacity; /**< Total capacity of the buffer. */
size_t xHead; /**< Write index at snapshot time. */
size_t xTail; /**< Read index at snapshot time. */
uint8_t ucIsFull; /**< 1 if buffer was full at snapshot time. */
uint8_t ucIsEmpty; /**< 1 if buffer was empty at snapshot time. */
} StreamBufferSnapshot_t;

/**
* @brief Peek at data in the stream buffer without consuming it.
*
* Copies up to xBufferLengthBytes from the stream buffer into pvRxData
* without advancing the read pointer. The data remains in the buffer
* for subsequent peek or receive operations.
*
* @param[in] xStreamBuffer The handle of the stream buffer.
* @param[out] pvRxData Pointer to the buffer into which data is copied.
* @param[in] xBufferLengthBytes Maximum number of bytes to peek.
*
* @return The number of bytes actually copied into pvRxData.
*/
size_t xStreamBufferPeek( StreamBufferHandle_t xStreamBuffer,
void * pvRxData,
size_t xBufferLengthBytes );

/**
* @brief Peek at data starting from a specific offset within the buffer.
*
* Similar to xStreamBufferPeek(), but starts reading from xOffset bytes
* past the current read pointer. Useful for inspecting data deeper in
* the buffer without reading preceding bytes.
*
* @param[in] xStreamBuffer The handle of the stream buffer.
* @param[in] xOffset Byte offset from the current read position.
* @param[out] pvRxData Buffer to copy data into.
* @param[in] xBufferLengthBytes Maximum bytes to copy.
*
* @return The number of bytes actually copied. Returns 0 if xOffset
* exceeds the available data.
*/
size_t xStreamBufferPeekAt( StreamBufferHandle_t xStreamBuffer,
size_t xOffset,
void * pvRxData,
size_t xBufferLengthBytes );

/**
* @brief Take a consistent snapshot of the stream buffer's current state.
*
* Captures readable/writable byte counts, head/tail positions, and
* full/empty status atomically (with respect to single-reader/writer).
*
* @param[in] xStreamBuffer The handle of the stream buffer.
* @param[out] pxSnapshot Pointer to the snapshot structure to fill.
*
* @return pdTRUE if the snapshot was taken successfully, pdFALSE if
* any parameter is NULL.
*/
BaseType_t xStreamBufferSnapshot( StreamBufferHandle_t xStreamBuffer,
StreamBufferSnapshot_t * pxSnapshot );

/**
* @brief Get the number of bytes available to read without consuming them.
*
* This is equivalent to xStreamBufferBytesAvailable() but implemented
* in the extension module for consistency.
*
* @param[in] xStreamBuffer The handle of the stream buffer.
*
* @return The number of readable bytes in the buffer.
*/
size_t xStreamBufferGetReadableLength( StreamBufferHandle_t xStreamBuffer );

#if defined( __cplusplus )
}
#endif

#endif /* STREAM_BUFFER_EXT_H */
29 changes: 12 additions & 17 deletions stream_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -767,21 +767,19 @@ size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer )
do
{
xOriginalTail = pxStreamBuffer->xTail;
xSpace = pxStreamBuffer->xLength + pxStreamBuffer->xTail;
xSpace -= pxStreamBuffer->xHead;

if( pxStreamBuffer->xTail >= pxStreamBuffer->xHead )
{
xSpace = pxStreamBuffer->xTail - pxStreamBuffer->xHead;
}
else
{
xSpace = ( pxStreamBuffer->xLength - pxStreamBuffer->xHead ) + pxStreamBuffer->xTail;
}
} while( xOriginalTail != pxStreamBuffer->xTail );

xSpace -= ( size_t ) 1;

if( xSpace >= pxStreamBuffer->xLength )
{
xSpace -= pxStreamBuffer->xLength;
}
else
{
mtCOVERAGE_TEST_MARKER();
}

traceRETURN_xStreamBufferSpacesAvailable( xSpace );

return xSpace;
Expand Down Expand Up @@ -1571,16 +1569,13 @@ static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer )
/* Returns the distance between xTail and xHead. */
size_t xCount;

xCount = pxStreamBuffer->xLength + pxStreamBuffer->xHead;
xCount -= pxStreamBuffer->xTail;

if( xCount >= pxStreamBuffer->xLength )
if( pxStreamBuffer->xHead >= pxStreamBuffer->xTail )
{
xCount -= pxStreamBuffer->xLength;
xCount = pxStreamBuffer->xHead - pxStreamBuffer->xTail;
}
else
{
mtCOVERAGE_TEST_MARKER();
xCount = ( pxStreamBuffer->xLength - pxStreamBuffer->xTail ) + pxStreamBuffer->xHead;
}

return xCount;
Expand Down
Loading