Skip to content

Commit d1bd24b

Browse files
SDS: add support for zero size data blocks (#356)
1 parent 380f920 commit d1bd24b

4 files changed

Lines changed: 40 additions & 34 deletions

File tree

‎documentation/theory.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ sds:
276276
One detection record has a size of 24 bytes. For example, a block size of 240 bytes contains 10 object detections. The number of detections may vary between blocks and is calculated by dividing the block size by 24. `sample-frequency:` is omitted because all detections are results of the same inference and share the block timeslot.
277277

278278
!!! Note
279-
- The current `sdsWrite` implementation does not accept a zero-byte data block. An application must therefore omit the block or define an application-specific non-empty representation when an inference produces no detections.
280279
- A variable-size segmentation mask cannot be described as a fixed-size detection record. It may be stored in a separate image or mask stream and correlated with the detection stream using the same timeslot.
281280

282281
### Audio data stream

‎sds/include/sds.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ int32_t sdsClose (sdsId_t id);
102102
\brief Write entire data block along with its timeslot information to the SDS stream opened in write mode.
103103
\param[in] id \ref sdsId_t handle to SDS stream
104104
\param[in] timeslot timeslot
105-
\param[in] buf pointer to the data block buffer to be written
105+
\param[in] buf pointer to the data block buffer to be written; may be NULL when buf_size is 0
106106
\param[in] buf_size size of the data block buffer in bytes
107107
\return number of bytes successfully written or
108108
a negative value on error (see \ref SDS_Return_Codes)
@@ -114,7 +114,7 @@ int32_t sdsWrite (sdsId_t id, uint32_t timeslot, const void *buf, uint32_t buf_s
114114
\brief Read entire data block along with its timeslot information from the SDS stream opened in read mode.
115115
\param[in] id \ref sdsId_t handle to SDS stream
116116
\param[out] timeslot pointer to buffer for a timeslot value
117-
\param[out] buf pointer to the data block buffer to be read
117+
\param[out] buf pointer to the data block buffer to be read; may be NULL when buf_size is 0
118118
\param[in] buf_size size of the data block buffer in bytes
119119
\return number of bytes successfully read, or
120120
a negative value on error or SDS_EOS (see \ref SDS_Return_Codes)

‎sds/include/sds.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ This asynchronous design enables efficient, non-blocking data handling and optim
237237

238238
Before attempting to write, the function verifies that the entire header and the complete data block, provided via the buffer
239239
pointer `buf` and its size `buf_size`, can fit within the available space in the internal SDS circular buffer.
240+
A zero-length data block may be written by setting `buf_size` to 0; in this case `buf` may be NULL.
240241
If insufficient space is available, the operation is aborted and the function returns \ref SDS_NO_SPACE.
241242

242243
On success, the function writes the header and data block to the SDS circular buffer and returns the number of data bytes written,
@@ -264,6 +265,7 @@ available, the function aborts and returns \ref SDS_NO_DATA.
264265

265266
The function verifies that the user-provided buffer `buf`, with size `buf_size`, is large enough to accommodate the entire
266267
data block. If it is too small, the function aborts and returns \ref SDS_ERROR_PARAMETER.
268+
For a zero-length data block, `buf_size` may be 0 and `buf` may be NULL.
267269

268270
If the end of the stream has been reached and no further data is available, the function returns \ref SDS_EOS.
269271

‎sds/sds.c‎

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ typedef struct {
5858
uint8_t index; // Index of the SDS stream in psdsStreams array
5959
uint8_t mode; // Stream mode: SDS_STREAM_MODE_WRITE or SDS_STREAM_MODE_READ
6060
volatile uint8_t flags; // Stream flags: SDS_STREAM_FLAG_ ..
61-
uint8_t reserved;
61+
uint8_t head_read; // Data block header has been read
6262
volatile uint32_t state; // Stream state: SDS_STREAM_STATE_ ..
6363
volatile uint32_t lock; // Lock for atomic operations
6464
uint32_t buf_size; // Size of the buffer used for the stream
@@ -544,6 +544,7 @@ sdsId_t sdsOpen (const char *name, sdsMode_t mode, void *buf, uint32_t buf_size)
544544
stream->buf_size = buf_size;
545545
stream->head.timeslot = 0U;
546546
stream->head.data_size = 0U;
547+
stream->head_read = 0U;
547548

548549
// Set threshold value for the stream.
549550
if ((buf_size / 3) < SDS_IO_TRANSFER_SIZE) {
@@ -737,7 +738,7 @@ int32_t sdsWrite (sdsId_t id, uint32_t timeslot, const void *buf, uint32_t buf_s
737738
}
738739

739740
// Verify if parameters are valid.
740-
if ((buf != NULL) && (buf_size != 0U)) {
741+
if ((buf != NULL) || (buf_size == 0U)) {
741742

742743
// Check if header + data fits into the buffer.
743744
if ((buf_size + sizeof(dataBlockHead_t)) <= (stream->buf_size - sdsBufferGetCount(stream->sds_buffer))) {
@@ -747,7 +748,11 @@ int32_t sdsWrite (sdsId_t id, uint32_t timeslot, const void *buf, uint32_t buf_s
747748

748749
// Write header and data block: Buffer size has been validated, so write operations are expected to succeed.
749750
sdsBufferWrite(stream->sds_buffer, &head, sizeof(dataBlockHead_t));
750-
ret = sdsBufferWrite(stream->sds_buffer, buf, buf_size);
751+
if (buf_size != 0U) {
752+
ret = sdsBufferWrite(stream->sds_buffer, buf, buf_size);
753+
} else {
754+
ret = 0;
755+
}
751756

752757
// If amount of data in the SDS Stream Buffer is at or above the threshold,
753758
// notify the sdsThread by setting the corresponding thread flag to process the stream.
@@ -800,21 +805,22 @@ int32_t sdsRead (sdsId_t id, uint32_t *timeslot, void *buf, uint32_t buf_size) {
800805
}
801806

802807
// Verify if parameters are valid.
803-
if ((buf != NULL) && (buf_size != 0U)) {
808+
if ((buf != NULL) || (buf_size == 0U)) {
804809
// Save eos flag.
805810
eos = stream->flags & SDS_STREAM_EOS;
806811
// Get size of available data block.
807812
size = sdsBufferGetCount(stream->sds_buffer);
808813

809814
// Check if header was already read:
810-
// If not (head.data_size == 0) and there is enough data available, read new header.
811-
if ((stream->head.data_size == 0U) && (size >= (int32_t)HEAD_SIZE)) {
815+
// If header was not read yet and there is enough data available, read new header.
816+
if ((stream->head_read == 0U) && (size >= (int32_t)HEAD_SIZE)) {
812817
sdsBufferRead(stream->sds_buffer, &stream->head, HEAD_SIZE);
818+
stream->head_read = 1U;
813819
size -= HEAD_SIZE;
814820
}
815821

816-
// Check if Header is valid and data block size in header is valid.
817-
if (stream->head.data_size != 0U) {
822+
// Check if header is available and ready for processing.
823+
if (stream->head_read != 0U) {
818824
// Check if whole data block fits into the provided buffer.
819825
if (stream->head.data_size > buf_size) {
820826
// Provided buffer is too small to read the data block.
@@ -827,16 +833,20 @@ int32_t sdsRead (sdsId_t id, uint32_t *timeslot, void *buf, uint32_t buf_size) {
827833
} else {
828834
// Read data block from SDS Stream Buffer:
829835
// Buffer size has been validated, so read operation is expected to succeed.
830-
ret = sdsBufferRead(stream->sds_buffer, buf, stream->head.data_size);
836+
if (stream->head.data_size != 0U) {
837+
ret = sdsBufferRead(stream->sds_buffer, buf, stream->head.data_size);
838+
} else {
839+
ret = 0;
840+
}
831841

832842
// Get timeslot from the header.
833843
if (timeslot != NULL) {
834844
*timeslot = stream->head.timeslot;
835845
}
836846

837847
// Whole data block has been read from the SDS Stream Buffer.
838-
// Clear the header information to enable reading of the next data block.
839-
stream->head.data_size = 0U;
848+
// Clear the header read flag to enable reading of the next header.
849+
stream->head_read = 0U;
840850

841851
// If free space in the SDS Stream Buffer is at or above the threshold,
842852
// notify the sdsThread by setting the corresponding thread flag to process the stream.
@@ -845,15 +855,12 @@ int32_t sdsRead (sdsId_t id, uint32_t *timeslot, void *buf, uint32_t buf_size) {
845855
}
846856
}
847857
}
858+
} else if (eos != 0U) {
859+
// If header is not available and End Of Stream.
860+
ret = SDS_EOS;
848861
} else {
849-
// Header is not valid.
850-
// Check for End Of Stream.
851-
if ((size == 0) && (eos != 0U)) {
852-
ret = SDS_EOS;
853-
} else {
854-
// Whole header is not available in the SDS Stream Buffer.
855-
ret = SDS_NO_DATA;
856-
}
862+
// If whole header is not available in the SDS Stream Buffer.
863+
ret = SDS_NO_DATA;
857864
}
858865
} else {
859866
ret = SDS_ERROR_PARAMETER;
@@ -903,30 +910,28 @@ int32_t sdsGetSize (sdsId_t id) {
903910
size = sdsBufferGetCount(stream->sds_buffer);
904911

905912
// Check if header was already read:
906-
// If not (head.data_size == 0) and there is enough data available, read new header.
907-
if ((stream->head.data_size == 0U) && (size >= (int32_t)HEAD_SIZE)) {
913+
// If header was not read yet and there is enough data available, read new header.
914+
if ((stream->head_read == 0U) && (size >= (int32_t)HEAD_SIZE)) {
908915
sdsBufferRead(stream->sds_buffer, &stream->head, HEAD_SIZE);
916+
stream->head_read = 1U;
909917
size -= HEAD_SIZE;
910918
}
911919

912-
// Check if Header is valid and data block size in header is valid.
913-
if (stream->head.data_size != 0U) {
920+
// Check if header is available and ready for processing.
921+
if (stream->head_read != 0U) {
914922
if ((int32_t)stream->head.data_size > size) {
915923
// Whole data block is not available in the SDS Stream Buffer.
916924
ret = SDS_NO_DATA;
917925
} else {
918926
// Return size of the data block.
919927
ret = stream->head.data_size;
920928
}
929+
} else if (eos != 0U) {
930+
// If header is not available and End Of Stream.
931+
ret = SDS_EOS;
921932
} else {
922-
// Header is not valid.
923-
// Check for End Of Stream.
924-
if ((size == 0) && (eos != 0U)) {
925-
ret = SDS_EOS;
926-
} else {
927-
// Whole header is not available in the SDS Stream Buffer.
928-
ret = SDS_NO_DATA;
929-
}
933+
// If whole header is not available in the SDS Stream Buffer.
934+
ret = SDS_NO_DATA;
930935
}
931936

932937
// Release Lock.

0 commit comments

Comments
 (0)