Skip to content
Merged
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
9 changes: 9 additions & 0 deletions include/zephyr/storage/stream_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ int stream_flash_init(struct stream_flash_ctx *ctx, const struct device *fdev,
*/
size_t stream_flash_bytes_written(const struct stream_flash_ctx *ctx);

/**
* @brief Read number of bytes buffered for the next flash write.
*
* @param ctx context
*
* @return Number of payload bytes buffered for the next flash write.
*/
size_t stream_flash_bytes_buffered(const struct stream_flash_ctx *ctx);

/**
* @brief Process input buffers to be written to flash device in single blocks.
* Will store remainder between calls.
Expand Down
5 changes: 5 additions & 0 deletions subsys/storage/stream/stream_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ size_t stream_flash_bytes_written(const struct stream_flash_ctx *ctx)
return ctx->bytes_written;
}

size_t stream_flash_bytes_buffered(const struct stream_flash_ctx *ctx)
{
return ctx->buf_bytes;
}

#ifdef CONFIG_STREAM_FLASH_INSPECT
struct _inspect_flash {
size_t buf_len;
Expand Down
36 changes: 36 additions & 0 deletions tests/subsys/storage/stream/stream_flash/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,42 @@ ZTEST(lib_stream_flash, test_stream_flash_bytes_written)
VERIFY_WRITTEN(BUF_LEN, BUF_LEN);
}

ZTEST(lib_stream_flash, test_stream_flash_bytes_buffered)
{
int rc;
size_t buffered;

init_target();

/* Initially no bytes should be buffered */
buffered = stream_flash_bytes_buffered(&ctx);
zassert_equal(buffered, 0, "expected no buffered bytes");

/* Write partial buffer */
rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN - 128, false);
zassert_equal(rc, 0, "expected success");

/* Verify buffered bytes */
buffered = stream_flash_bytes_buffered(&ctx);
zassert_equal(buffered, BUF_LEN - 128, "expected buffered bytes");

/* Write remaining buffer */
rc = stream_flash_buffered_write(&ctx, write_buf, 128, false);
zassert_equal(rc, 0, "expected success");

/* After auto-flush, no bytes should be buffered */
buffered = stream_flash_bytes_buffered(&ctx);
zassert_equal(buffered, 0, "expected no buffered bytes");

/* Write more than buffer size to trigger auto-flush */
rc = stream_flash_buffered_write(&ctx, write_buf, BUF_LEN + 128, false);
zassert_equal(rc, 0, "expected success");

/* Verify buffered bytes */
buffered = stream_flash_bytes_buffered(&ctx);
zassert_equal(buffered, 128, "expected remaining buffered bytes after auto-flush");
}

ZTEST(lib_stream_flash, test_stream_flash_buf_size_greater_than_page_size)
{
int rc;
Expand Down