forked from FreeRTOS/FreeRTOS-Plus-TCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FreeRTOS_Stream_Buffer.c
446 lines (383 loc) · 13.9 KB
/
FreeRTOS_Stream_Buffer.c
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
* FreeRTOS+TCP V2.3.3
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/**
* @file FreeRTOS_Stream_Buffer.c
* @brief Provides the API for managing/creating the stream buffers in the FreeRTOS+TCP network stack.
*/
/* Standard includes. */
#include <stdint.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_UDP_IP.h"
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_IP_Private.h"
/**
* @brief Get the space between lower and upper value provided to the function.
* @param[in] pxBuffer: The circular stream buffer.
* @param[in] uxLower: The lower value.
* @param[in] uxUpper: The upper value.
* @return The space between uxLower and uxUpper, which equals to the distance
* minus 1.
*/
size_t uxStreamBufferSpace( const StreamBuffer_t * pxBuffer,
const size_t uxLower,
const size_t uxUpper )
{
size_t uxCount;
uxCount = pxBuffer->LENGTH + uxUpper - uxLower - 1U;
if( uxCount >= pxBuffer->LENGTH )
{
uxCount -= pxBuffer->LENGTH;
}
return uxCount;
}
/**
* @brief Get the distance between lower and upper value provided to the function.
* @param[in] pxBuffer: The circular stream buffer.
* @param[in] uxLower: The lower value.
* @param[in] uxUpper: The upper value.
* @return The distance between uxLower and uxUpper.
*/
size_t uxStreamBufferDistance( const StreamBuffer_t * pxBuffer,
const size_t uxLower,
const size_t uxUpper )
{
size_t uxCount;
uxCount = pxBuffer->LENGTH + uxUpper - uxLower;
if( uxCount >= pxBuffer->LENGTH )
{
uxCount -= pxBuffer->LENGTH;
}
return uxCount;
}
/**
* @brief Get the number of items which can be added to the buffer at
* the head before reaching the tail.
* @param[in] pxBuffer: The circular stream buffer.
* @return The number of items which can still be added to uxHead
* before hitting on uxTail
*/
size_t uxStreamBufferGetSpace( const StreamBuffer_t * pxBuffer )
{
size_t uxHead = pxBuffer->uxHead;
size_t uxTail = pxBuffer->uxTail;
return uxStreamBufferSpace( pxBuffer, uxHead, uxTail );
}
/*-----------------------------------------------------------*/
/**
* @brief Get the distance between the pointer in free space and the tail.
* @param[in] pxBuffer: The circular stream buffer.
* @return Distance between uxFront and uxTail or the number of items
* which can still be added to uxFront, before hitting on uxTail.
*/
size_t uxStreamBufferFrontSpace( const StreamBuffer_t * pxBuffer )
{
size_t uxFront = pxBuffer->uxFront;
size_t uxTail = pxBuffer->uxTail;
return uxStreamBufferSpace( pxBuffer, uxFront, uxTail );
}
/*-----------------------------------------------------------*/
/**
* @brief Get the number of items which can be read from the tail before
* reaching the head.
* @param[in] pxBuffer: The circular stream buffer.
* @return The number of items which can be read from the tail before
* reaching the head.
*/
size_t uxStreamBufferGetSize( const StreamBuffer_t * pxBuffer )
{
size_t uxHead = pxBuffer->uxHead;
size_t uxTail = pxBuffer->uxTail;
return uxStreamBufferDistance( pxBuffer, uxTail, uxHead );
}
/*-----------------------------------------------------------*/
/**
* @brief Get the space between the mid pointer and the head in the stream
* buffer.
* @param[in] pxBuffer: The circular stream buffer.
* @return The space between the mid pointer and the head.
*/
size_t uxStreamBufferMidSpace( const StreamBuffer_t * pxBuffer )
{
size_t uxHead = pxBuffer->uxHead;
size_t uxMid = pxBuffer->uxMid;
return uxStreamBufferDistance( pxBuffer, uxMid, uxHead );
}
/*-----------------------------------------------------------*/
/**
* @brief Move Clear the stream buffer.
* @param[in] pxBuffer: The circular stream buffer.
*/
void vStreamBufferClear( StreamBuffer_t * pxBuffer )
{
/* Make the circular buffer empty */
pxBuffer->uxHead = 0U;
pxBuffer->uxTail = 0U;
pxBuffer->uxFront = 0U;
pxBuffer->uxMid = 0U;
}
/*-----------------------------------------------------------*/
/**
* @brief Move the mid pointer forward by given byte count
* @param[in] pxBuffer: The circular stream buffer.
* @param[in] uxCount: The byte count by which the mid pointer is to be moved.
*/
void vStreamBufferMoveMid( StreamBuffer_t * pxBuffer,
size_t uxCount )
{
/* Increment uxMid, but no further than uxHead */
size_t uxSize = uxStreamBufferMidSpace( pxBuffer );
size_t uxMid = pxBuffer->uxMid;
size_t uxMoveCount = uxCount;
if( uxMoveCount > uxSize )
{
uxMoveCount = uxSize;
}
uxMid += uxMoveCount;
if( uxMid >= pxBuffer->LENGTH )
{
uxMid -= pxBuffer->LENGTH;
}
pxBuffer->uxMid = uxMid;
}
/*-----------------------------------------------------------*/
/**
* @brief Check whether the value in left is less than or equal to the
* value in right from the perspective of the circular stream
* buffer.
* @param[in] pxBuffer: The circular stream buffer.
* @param[in] uxLeft: The left pointer in the stream buffer.
* @param[in] uxRight: The right value pointer in the stream buffer.
* @return pdTRUE if uxLeft <= uxRight, else pdFALSE.
*/
BaseType_t xStreamBufferLessThenEqual( const StreamBuffer_t * pxBuffer,
const size_t uxLeft,
const size_t uxRight )
{
BaseType_t xReturn;
size_t uxTail = pxBuffer->uxTail;
/* Returns true if ( uxLeft < uxRight ) */
if( ( ( ( uxLeft < uxTail ) ? 1U : 0U ) ^ ( ( uxRight < uxTail ) ? 1U : 0U ) ) != 0U )
{
if( uxRight < uxTail )
{
xReturn = pdTRUE;
}
else
{
xReturn = pdFALSE;
}
}
else
{
if( uxLeft <= uxRight )
{
xReturn = pdTRUE;
}
else
{
xReturn = pdFALSE;
}
}
return xReturn;
}
/*-----------------------------------------------------------*/
/**
* @brief Get the pointer to data and the amount of data which can be read in one go.
*
* @param[in] pxBuffer: The circular stream buffer.
* @param[out] ppucData: Pointer to the data pointer which will point to the
* data which can be read.
*
* @return The number of bytes which can be read in one go (which might be less than
* actual number of available bytes since this is a circular buffer and tail
* can loop back to the start of the buffer).
*/
size_t uxStreamBufferGetPtr( StreamBuffer_t * pxBuffer,
uint8_t ** ppucData )
{
size_t uxNextTail = pxBuffer->uxTail;
size_t uxSize = uxStreamBufferGetSize( pxBuffer );
*ppucData = pxBuffer->ucArray + uxNextTail;
return FreeRTOS_min_uint32( uxSize, pxBuffer->LENGTH - uxNextTail );
}
/*-----------------------------------------------------------*/
/**
* @brief Adds data to a stream buffer.
*
* @param[in,out] pxBuffer: The buffer to which the bytes will be added.
* @param[in] uxOffset: If uxOffset > 0, data will be written at an offset from uxHead
* while uxHead will not be moved yet.
* @param[in,out] pucData: A pointer to the data to be added. If 'pucData' equals NULL,
* the function is called to advance the 'Head' only.
* @param[in] uxByteCount: The number of bytes to add.
*
* @return The number of bytes added to the buffer.
*/
size_t uxStreamBufferAdd( StreamBuffer_t * pxBuffer,
size_t uxOffset,
const uint8_t * pucData,
size_t uxByteCount )
{
size_t uxSpace, uxNextHead, uxFirst;
size_t uxCount = uxByteCount;
uxSpace = uxStreamBufferGetSpace( pxBuffer );
/* If uxOffset > 0, items can be placed in front of uxHead */
if( uxSpace > uxOffset )
{
uxSpace -= uxOffset;
}
else
{
uxSpace = 0U;
}
/* The number of bytes that can be written is the minimum of the number of
* bytes requested and the number available. */
uxCount = FreeRTOS_min_uint32( uxSpace, uxCount );
if( uxCount != 0U )
{
uxNextHead = pxBuffer->uxHead;
if( uxOffset != 0U )
{
/* ( uxOffset > 0 ) means: write in front if the uxHead marker */
uxNextHead += uxOffset;
if( uxNextHead >= pxBuffer->LENGTH )
{
uxNextHead -= pxBuffer->LENGTH;
}
}
if( pucData != NULL )
{
/* Calculate the number of bytes that can be added in the first
* write - which may be less than the total number of bytes that need
* to be added if the buffer will wrap back to the beginning. */
uxFirst = FreeRTOS_min_uint32( pxBuffer->LENGTH - uxNextHead, uxCount );
/* Write as many bytes as can be written in the first write. */
( void ) memcpy( &( pxBuffer->ucArray[ uxNextHead ] ), pucData, uxFirst );
/* If the number of bytes written was less than the number that
* could be written in the first write... */
if( uxCount > uxFirst )
{
/* ...then write the remaining bytes to the start of the
* buffer. */
( void ) memcpy( pxBuffer->ucArray, &( pucData[ uxFirst ] ), uxCount - uxFirst );
}
}
if( uxOffset == 0U )
{
/* ( uxOffset == 0 ) means: write at uxHead position */
uxNextHead += uxCount;
if( uxNextHead >= pxBuffer->LENGTH )
{
uxNextHead -= pxBuffer->LENGTH;
}
pxBuffer->uxHead = uxNextHead;
}
if( xStreamBufferLessThenEqual( pxBuffer, pxBuffer->uxFront, uxNextHead ) != pdFALSE )
{
/* Advance the front pointer */
pxBuffer->uxFront = uxNextHead;
}
}
return uxCount;
}
/*-----------------------------------------------------------*/
/**
* @brief Read bytes from stream buffer.
*
* @param[in] pxBuffer: The buffer from which the bytes will be read.
* @param[in] uxOffset: can be used to read data located at a certain offset from 'lTail'.
* @param[in,out] pucData: If 'pucData' equals NULL, the function is called to advance 'lTail' only.
* @param[in] uxMaxCount: The number of bytes to read.
* @param[in] xPeek: if 'xPeek' is pdTRUE, or if 'uxOffset' is non-zero, the 'lTail' pointer will
* not be advanced.
*
* @return The count of the bytes read.
*/
size_t uxStreamBufferGet( StreamBuffer_t * pxBuffer,
size_t uxOffset,
uint8_t * pucData,
size_t uxMaxCount,
BaseType_t xPeek )
{
size_t uxSize, uxCount, uxFirst, uxNextTail;
/* How much data is available? */
uxSize = uxStreamBufferGetSize( pxBuffer );
if( uxSize > uxOffset )
{
uxSize -= uxOffset;
}
else
{
uxSize = 0U;
}
/* Use the minimum of the wanted bytes and the available bytes. */
uxCount = FreeRTOS_min_uint32( uxSize, uxMaxCount );
if( uxCount > 0U )
{
uxNextTail = pxBuffer->uxTail;
if( uxOffset != 0U )
{
uxNextTail += uxOffset;
if( uxNextTail >= pxBuffer->LENGTH )
{
uxNextTail -= pxBuffer->LENGTH;
}
}
if( pucData != NULL )
{
/* Calculate the number of bytes that can be read - which may be
* less than the number wanted if the data wraps around to the start of
* the buffer. */
uxFirst = FreeRTOS_min_uint32( pxBuffer->LENGTH - uxNextTail, uxCount );
/* Obtain the number of bytes it is possible to obtain in the first
* read. */
( void ) memcpy( pucData, &( pxBuffer->ucArray[ uxNextTail ] ), uxFirst );
/* If the total number of wanted bytes is greater than the number
* that could be read in the first read... */
if( uxCount > uxFirst )
{
/*...then read the remaining bytes from the start of the buffer. */
( void ) memcpy( &( pucData[ uxFirst ] ), pxBuffer->ucArray, uxCount - uxFirst );
}
}
if( ( xPeek == pdFALSE ) && ( uxOffset == 0UL ) )
{
/* Move the tail pointer to effectively remove the data read from
* the buffer. */
uxNextTail += uxCount;
if( uxNextTail >= pxBuffer->LENGTH )
{
uxNextTail -= pxBuffer->LENGTH;
}
pxBuffer->uxTail = uxNextTail;
}
}
return uxCount;
}