Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ring_buffer.c « App « STM32_WPAN « BLE_LLD_Pressbutton « BLE_LLD « Applications « NUCLEO-WB15CC « Projects - github.com/Flipper-Zero/STM32CubeWB.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d04d5ca5b8e3a4f1b06b5952d0830ede28a4a297 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
 ******************************************************************************
  * File Name          : ring_buffer.c
  * Description        : utility to provide ring buffer component
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */

/* "SPECIFICATIONS"
 - support any type as an element (with any size): not for now, only char
 - no dynamic allocation
 - buffer size can be changed by user
 - support adding multiple elements (string)
 - support removing multiple elements (string?)

*/

/* Includes ------------------------------------------------------------------*/
#include "app_conf.h"
#include "app_common.h"
#include "dbg_trace.h"
#include "stm_logging.h"
#include "ring_buffer.h"

/* Private includes ----------------------------------------------------------*/

/* Private typedef -----------------------------------------------------------*/

/* Private defines -----------------------------------------------------------*/

/* Private macros ------------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Functions Definition ------------------------------------------------------*/

// WARNING: be careful to protect a buffer operation by critical section if it
// can be accessed in interruption too

// Private function to move an index one step forward
static inline void bufIncIndex(const Buffer *buf, uint32_t *index){
  *index = (*index + 1) % buf->mem_size;
}

// Initialize buffer structure, must be called first (see BUF_ALLOC)
void bufInit(Buffer *buf, uint32_t size){
  buf->idx_rd = 0;
  buf->idx_wr = 0;
  buf->mem_size = size;
  buf->data_size = 0;
}

// Test if buffer is empty
bool bufIsEmpty(const Buffer *buf){
  return (0 == buf->data_size);
}

// Test if buffer is full
bool bufIsFull(const Buffer *buf){
  return (buf->mem_size == buf->data_size);
}

// Store one byte in buffer
// Call ignored if buffer already full
void bufPutChar(Buffer *buf, char character){
  if(! bufIsFull(buf)){
    buf->memory[buf->idx_wr] = character;
    bufIncIndex(buf, &(buf->idx_wr));
    (buf->data_size)++;
  }
}

// Store a string in buffer (without '\0')
// When buffer is full, remaining bytes are dropped
void bufPutString(Buffer *buf, const char *string){
  uint32_t freeSize = buf->mem_size - buf->data_size;
  uint32_t addSize = MIN(strlen(string), freeSize);
  uint32_t toEndSize = buf->mem_size - buf->idx_wr;
  uint32_t cp1Size = addSize;
  uint32_t cp2Size = 0;
  if (addSize > toEndSize){
    cp1Size = toEndSize;
    cp2Size = addSize - toEndSize;
  }
  memcpy(&(buf->memory[buf->idx_wr]), string, cp1Size);
  if (cp2Size == 0){
    buf->idx_wr = (buf->idx_wr + cp1Size) % buf->mem_size;
  }else{
    memcpy(&(buf->memory[0]), &(string[cp1Size]), cp2Size);
    buf->idx_wr = cp2Size;
  }
  buf->data_size += addSize;
}

// Retrieve one byte from buffer
// Buffer must not be empty (check with bufIsEmpty())
char bufGetChar(Buffer *buf){
  char character;
  character = buf->memory[buf->idx_rd];
  bufIncIndex(buf, &(buf->idx_rd));
  (buf->data_size)--;
  return character;
}

// Retrieve at most size chars from buffer, return actual count copied
uint32_t bufGetMultiChar(Buffer *buf, char *dest, uint32_t size){
  uint32_t count = 0;
  while (! bufIsEmpty(buf) && (count < size)){
    *dest = bufGetChar(buf);
    dest++;
    count++;
  }
  return count;
}

#define TEST(EXP) \
  do {							    \
    if(!(EXP)) {					    \
      PRINT_MESG_DBG("Test failed on line %d: " #EXP, __LINE__);   \
      all_passed = false;				    \
    }							    \
  } while (0)

#define BUF_TEST_SIZE 128

// Test buffer code
void bufTest(void){
  uint32_t i;
  bool all_passed = true;
  char bufMem[sizeof(Buffer) + BUF_TEST_SIZE];
  Buffer *buf = (Buffer *)&bufMem;

  PRINT_MESG_DBG("entering %s()", __func__);
  bufInit(buf, BUF_TEST_SIZE);
  TEST(bufIsEmpty(buf));

  bufPutChar(buf, 'a');
  TEST(!bufIsEmpty(buf));
  TEST(!bufIsFull(buf));
  TEST('a' == bufGetChar(buf));
  TEST(bufIsEmpty(buf));

  TEST(bufIsEmpty(buf));
  const char testString[] = "ST Microelectronics";
  char checkString[64];
  i = 0;
  bufPutString(buf, testString);
  while(!bufIsEmpty(buf)){
    checkString[i] = bufGetChar(buf);
    i++;
  }
  checkString[i] = '\0';
  TEST(0 == strcmp(testString, checkString));

  for(uint32_t i = 0; i<buf->mem_size; i++){
    bufPutChar(buf, 'a');
  }
  TEST(!bufIsEmpty(buf));
  TEST(bufIsFull(buf));

  // Check that new entries are ignored when full
  bufPutChar(buf, 'b');

  i = 0;
  while(!bufIsEmpty(buf)){
    TEST('a' == bufGetChar(buf));
    i++;
  }
  TEST(buf->mem_size == i);

  // Check interleaved access
  TEST(bufIsEmpty(buf));
  bufPutChar(buf, 'a');
  bufPutChar(buf, 'b');
  TEST('a' == bufGetChar(buf));
  bufPutChar(buf, 'c');
  TEST('b' == bufGetChar(buf));
  TEST('c' == bufGetChar(buf));
  TEST(bufIsEmpty(buf));

  // Check bufPutString without rollover
  bufInit(buf, BUF_TEST_SIZE);
  const char testString2[] = "ring buffer test string";
  bufPutString(buf, testString2);
  i = 0;
  while(!bufIsEmpty(buf)){
    checkString[i] = bufGetChar(buf);
    i++;
  }
  checkString[i] = '\0';
  TEST(0 == strcmp(testString2, checkString));

// Check bufPutString with rollover
  bufInit(buf, BUF_TEST_SIZE);
  const char testString3[] = "ring buffer rollover test string";
  for (i=0; i < (BUF_TEST_SIZE - 10); i++){
    bufPutChar(buf, 'm');
  }
  for (i=0; i < 30; i++){
    bufGetChar(buf);
  }
  bufPutString(buf, testString3);
  for (i=0; i < (BUF_TEST_SIZE -10 -30); i++){
    bufGetChar(buf);
  }
  i = 0;
  while(!bufIsEmpty(buf)){
    checkString[i] = bufGetChar(buf);
    i++;
  }
  checkString[i] = '\0';
  PRINT_MESG_DBG(checkString);
  TEST(0 == strcmp(testString3, checkString));

  if(all_passed){
    PRINT_MESG_DBG("Ring buffer: all tests passed");
  }
}


/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/