@@ -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
6060volatile uint8_t flags ; // Stream flags: SDS_STREAM_FLAG_ ..
61- uint8_t reserved ;
61+ uint8_t head_read ; // Data block header has been read
6262volatile uint32_t state ; // Stream state: SDS_STREAM_STATE_ ..
6363volatile 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