Skip to content

Commit b2a9277

Browse files
authored
Add new functionality (#2)
* Add missing functionality Add cBufferIsContigous and cBufferAppendByte functions Add big endian copy for unsigned types Fix rotate condition
1 parent b0e873d commit b2a9277

2 files changed

Lines changed: 90 additions & 3 deletions

File tree

src/c_buffer.c

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
188206
int32_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+
276318
int32_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+
485540
uint8_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

src/c_buffer.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040
*/
4141

4242
typedef enum {
43-
C_BUFFER_SUCCESS,
43+
C_BUFFER_WRAPED = 1,
44+
C_BUFFER_SUCCESS = 0,
4445
C_BUFFER_NULL_ERROR = -301,
4546
C_BUFFER_INSUFFICIENT = -302,
4647
C_BUFFER_MISMATCH = -303,
@@ -100,6 +101,22 @@ int32_t cBufferAvailableForWrite(cBuffer_t* inst);
100101
*/
101102
int32_t cBufferPrepend(cBuffer_t *inst, uint8_t *data, size_t data_size);
102103

104+
/**
105+
* Write a uint32 at the start of the buffer in big endian format
106+
* Input: Pointer to buffer instance
107+
* Input: Data to write
108+
* Returns: cBufferErr_t or num bytes written
109+
*/
110+
int32_t cBufferPrependUint32(cBuffer_t *inst, uint32_t data);
111+
112+
/**
113+
* Write a uint16 at the start of the buffer in big endian format
114+
* Input: Pointer to buffer instance
115+
* Input: Data to write
116+
* Returns: cBufferErr_t or num bytes written
117+
*/
118+
int32_t cBufferPrependUint16(cBuffer_t *inst, uint16_t data);
119+
103120
/**
104121
* Write a single byte at the start of the buffer
105122
* Input: Pointer to buffer instance
@@ -117,6 +134,14 @@ int32_t cBufferPrependByte(cBuffer_t *inst, uint8_t data);
117134
*/
118135
int32_t cBufferAppend(cBuffer_t *inst, uint8_t *data, size_t data_size);
119136

137+
/**
138+
* Write the new data at the end of the buffer
139+
* Input: Pointer to buffer instance
140+
* Input: Byte to write
141+
* Returns: cBufferErr_t or 1 if one byte was written
142+
*/
143+
int32_t cBufferAppendByte(cBuffer_t *inst, uint8_t data);
144+
120145
/**
121146
* Read data from the buffer
122147
* Input: Pointer to buffer instance
@@ -158,6 +183,13 @@ int32_t cBufferClear(cBuffer_t *inst);
158183
*/
159184
int32_t cBufferContiguate(cBuffer_t* inst);
160185

186+
/**
187+
* Check if the data is available in continous memory
188+
* Input: Pointer to buffer instance
189+
* Returns: cBufferErr_t, SUCCESS indicates that the data is contigous
190+
*/
191+
int32_t cBufferIsContigous(cBuffer_t* inst);
192+
161193
/**
162194
* Get pointer to the current first element in the buffer
163195
* Input: Pointer to buffer instance

0 commit comments

Comments
 (0)