@@ -185,6 +185,24 @@ int32_t cBufferPrepend(cBuffer_t *inst, uint8_t *data, size_t data_size) {
185185 return data_size ;
186186}
187187
188+ int32_t cBufferPrependUint16 (cBuffer_t * inst , uint16_t data ) {
189+ uint8_t tmp [2 ];
190+ tmp [0 ] = (uint8_t )(data >> 8 );
191+ tmp [1 ] = (uint8_t )(data );
192+
193+ return cBufferPrepend (inst , tmp , sizeof (tmp ));
194+ }
195+
196+ int32_t cBufferPrependUint32 (cBuffer_t * inst , uint32_t data ) {
197+ uint8_t tmp [4 ];
198+ tmp [0 ] = (uint8_t )(data >> 24 );
199+ tmp [1 ] = (uint8_t )(data >> 16 );
200+ tmp [2 ] = (uint8_t )(data >> 8 );
201+ tmp [3 ] = (uint8_t )(data );
202+
203+ return cBufferPrepend (inst , tmp , sizeof (tmp ));
204+ }
205+
188206int32_t cBufferPrependByte (cBuffer_t * inst , uint8_t data ) {
189207 if (inst == NULL ) {
190208 return C_BUFFER_NULL_ERROR ;
@@ -273,6 +291,30 @@ int32_t cBufferAppend(cBuffer_t *inst, uint8_t *data, size_t data_size) {
273291 return data_size ;
274292}
275293
294+ int32_t cBufferAppendByte (cBuffer_t * inst , uint8_t data ) {
295+ if (inst == NULL ) {
296+ return C_BUFFER_NULL_ERROR ;
297+ }
298+
299+ // This cast is safe as the inst null check is allready done
300+ if ((size_t )cBufferAvailableForWrite (inst ) < 1 ) {
301+ return C_BUFFER_INSUFFICIENT ;
302+ }
303+
304+ // Look for the special case were the buffer is empty
305+ if (inst -> head == inst -> tail ) {
306+ // For good reasons we want to reset the buffer when this happens.
307+ inst -> head = 0 ;
308+ inst -> tail = 0 ;
309+ }
310+
311+ // Check if we need to do a wrap copy
312+ inst -> data [inst -> head ] = data ;
313+ inst -> head = MODULO_INC (inst -> head , 1 , inst -> size );
314+
315+ return 1 ;
316+ }
317+
276318int32_t cBufferReadAll (cBuffer_t * inst , uint8_t * data , size_t max_read_size ) {
277319 if (inst == NULL || data == NULL ) {
278320 return C_BUFFER_NULL_ERROR ;
@@ -445,7 +487,7 @@ int32_t cBufferContiguate(cBuffer_t* inst)
445487 // Make sure that tail points to the start of the buffer
446488 inst -> head = 0 ;
447489 inst -> tail = 0 ;
448- } else if (inst -> head < inst -> tail ) {
490+ } else if (inst -> head < inst -> tail && inst -> head != 0 ) {
449491 int32_t num_of_bytes = cBufferAvailableForRead (inst );
450492
451493 uint8_t * last_element = & inst -> data [inst -> size - 1 ];
@@ -482,13 +524,26 @@ int32_t cBufferContiguate(cBuffer_t* inst)
482524 return C_BUFFER_SUCCESS ;
483525}
484526
527+ int32_t cBufferIsContigous (cBuffer_t * inst ) {
528+ if (inst == NULL ) {
529+ return C_BUFFER_NULL_ERROR ;
530+ }
531+
532+ // Check if there is a wrap in the buffer
533+ if (inst -> head < inst -> tail && inst -> head != 0 ) {
534+ return C_BUFFER_WRAPED ;
535+ }
536+
537+ return C_BUFFER_SUCCESS ;
538+ }
539+
485540uint8_t * cBufferGetReadPointer (cBuffer_t * inst ) {
486541 if (inst == NULL ) {
487542 return NULL ;
488543 }
489544
490545 // Protect from buffers with wraps
491- if (inst -> head < inst -> tail ) {
546+ if (inst -> head < inst -> tail && inst -> head != 0 ) {
492547 return NULL ;
493548 }
494549
0 commit comments