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
35
36
37
38
39
40
41
42
43
44
45
46
50
51
52
55
56
57
70
71
72
91
92
95
96
109
110
111
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
160
161
162
168
169
170
177
178
179
180
181
182
183
190
191
192
193
194
195
196
200
201
202
203
204
205
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
228
234
240
241
242
244
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
299
303
304
309
314
315
319
320
321
322
329
330
331
332
333
334
335
336
337
338
339
341
342
343
344
345
346
347
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
375
376
381
382
383
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
453
454
456
461
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
485
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
523
530
534
535
536
537
538
539
540
541
543
544
545
546
547
548
549
550
551
552
553
554
555
556
561
562
563
564
565
566
567
568
569
570
574
575
579
583
584
585
586
587
588
589
590
591
595
599
600
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
626
630
634
635
636
637
638
650
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
676
684
693
698
699
704
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
729
733
737
738
739
740
742
743
744
745
746
751
752
753
754
755
756
757
758
759
760
764
765
766
767
768
769
770
771
772
773
774
775
776
777
781
782
786
787
792
793
794
795
796
797
802
806
807
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
837
838
839
840
841
842
851
852
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
877
881
885
886
887
888
893
894
895
896
897
898
902
906
907
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
933
934
935
936
937
939
940
941
943
951
955
956
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
984
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1005
1009
1013
1014
1015
1019
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1099
1100
1101
1102
1103
1104
1105
1107
1113
1117
1118
1119
1123
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1148
1149
1150
1152
1153
1154
1155
1156
1158
1164
1168
1169
1171
1172
1173
1177
1178
1179
1180
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1200
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1218
1219
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1243
1244
1245
1246
1253
1254
1255
1256
1263
/* ... */
#include <stdint.h>
#include <string.h>
/* ... */
#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
#include "FreeRTOS.h"
#include "task.h"
#include "stream_buffer.h"
#if( configUSE_TASK_NOTIFICATIONS != 1 )
#error configUSE_TASK_NOTIFICATIONS must be set to 1 to build stream_buffer.c
#endif
/* ... */
#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
/* ... */
#ifndef sbRECEIVE_COMPLETED
#define sbRECEIVE_COMPLETED( pxStreamBuffer ) \
vTaskSuspendAll(); \
{ \
if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
{ \
( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToSend, \
( uint32_t ) 0, \
eNoAction ); \
( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
...} \
...} \
( void ) xTaskResumeAll();...
/* ... */#endif
#ifndef sbRECEIVE_COMPLETED_FROM_ISR
#define sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, \
pxHigherPriorityTaskWoken ) \
{ \
UBaseType_t uxSavedInterruptStatus; \
\
uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); \
{ \
if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL ) \
{ \
( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend, \
( uint32_t ) 0, \
eNoAction, \
pxHigherPriorityTaskWoken ); \
( pxStreamBuffer )->xTaskWaitingToSend = NULL; \
...} \
...} \
portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); \
...}...
/* ... */#endif
/* ... */
#ifndef sbSEND_COMPLETED
#define sbSEND_COMPLETED( pxStreamBuffer ) \
vTaskSuspendAll(); \
{ \
if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
{ \
( void ) xTaskNotify( ( pxStreamBuffer )->xTaskWaitingToReceive, \
( uint32_t ) 0, \
eNoAction ); \
( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
...} \
...} \
( void ) xTaskResumeAll();...
/* ... */#endif
#ifndef sbSEND_COMPLETE_FROM_ISR
#define sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken ) \
{ \
UBaseType_t uxSavedInterruptStatus; \
\
uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR(); \
{ \
if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL ) \
{ \
( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive, \
( uint32_t ) 0, \
eNoAction, \
pxHigherPriorityTaskWoken ); \
( pxStreamBuffer )->xTaskWaitingToReceive = NULL; \
...} \
...} \
portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); \
...}...
/* ... */#endif
#define sbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) )
#define sbFLAGS_IS_MESSAGE_BUFFER ( ( uint8_t ) 1 )
#define sbFLAGS_IS_STATICALLY_ALLOCATED ( ( uint8_t ) 2 )
typedef struct StreamBufferDef_t
{
volatile size_t xTail;
volatile size_t xHead;
size_t xLength;
size_t xTriggerLevelBytes;
volatile TaskHandle_t xTaskWaitingToReceive;
volatile TaskHandle_t xTaskWaitingToSend;
uint8_t *pucBuffer;
uint8_t ucFlags;
#if ( configUSE_TRACE_FACILITY == 1 )
UBaseType_t uxStreamBufferNumber;
#endif
...} StreamBuffer_t;
/* ... */
static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer ) PRIVILEGED_FUNCTION;
/* ... */
static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer, const uint8_t *pucData, size_t xCount ) PRIVILEGED_FUNCTION;
/* ... */
static size_t prvReadMessageFromBuffer( StreamBuffer_t *pxStreamBuffer,
void *pvRxData,
size_t xBufferLengthBytes,
size_t xBytesAvailable,
size_t xBytesToStoreMessageLength ) PRIVILEGED_FUNCTION;
/* ... */
static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
const void * pvTxData,
size_t xDataLengthBytes,
size_t xSpace,
size_t xRequiredSpace ) PRIVILEGED_FUNCTION;
/* ... */
static size_t prvReadBytesFromBuffer( StreamBuffer_t *pxStreamBuffer,
uint8_t *pucData,
size_t xMaxCount,
size_t xBytesAvailable ) PRIVILEGED_FUNCTION;
/* ... */
static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
uint8_t * const pucBuffer,
size_t xBufferSizeBytes,
size_t xTriggerLevelBytes,
uint8_t ucFlags ) PRIVILEGED_FUNCTION;
#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
StreamBufferHandle_t xStreamBufferGenericCreate( size_t xBufferSizeBytes, size_t xTriggerLevelBytes, BaseType_t xIsMessageBuffer )
{
uint8_t *pucAllocatedMemory;
uint8_t ucFlags;
/* ... */
if( xIsMessageBuffer == pdTRUE )
{
ucFlags = sbFLAGS_IS_MESSAGE_BUFFER;
configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
}if (xIsMessageBuffer == pdTRUE) { ... }
else
{
ucFlags = 0;
configASSERT( xBufferSizeBytes > 0 );
}else { ... }
configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
/* ... */
if( xTriggerLevelBytes == ( size_t ) 0 )
{
xTriggerLevelBytes = ( size_t ) 1;
}if (xTriggerLevelBytes == ( size_t ) 0) { ... }
/* ... */
xBufferSizeBytes++;
pucAllocatedMemory = ( uint8_t * ) pvPortMalloc( xBufferSizeBytes + sizeof( StreamBuffer_t ) );
if( pucAllocatedMemory != NULL )
{
prvInitialiseNewStreamBuffer( ( StreamBuffer_t * ) pucAllocatedMemory,
pucAllocatedMemory + sizeof( StreamBuffer_t ),
xBufferSizeBytes,
xTriggerLevelBytes,
ucFlags );
traceSTREAM_BUFFER_CREATE( ( ( StreamBuffer_t * ) pucAllocatedMemory ), xIsMessageBuffer );
}if (pucAllocatedMemory != NULL) { ... }
else
{
traceSTREAM_BUFFER_CREATE_FAILED( xIsMessageBuffer );
}else { ... }
return ( StreamBufferHandle_t ) pucAllocatedMemory;
}{ ... }
/* ... */#endif
#if( configSUPPORT_STATIC_ALLOCATION == 1 )
StreamBufferHandle_t xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes,
size_t xTriggerLevelBytes,
BaseType_t xIsMessageBuffer,
uint8_t * const pucStreamBufferStorageArea,
StaticStreamBuffer_t * const pxStaticStreamBuffer )
{
StreamBuffer_t * const pxStreamBuffer = ( StreamBuffer_t * ) pxStaticStreamBuffer;
StreamBufferHandle_t xReturn;
uint8_t ucFlags;
configASSERT( pucStreamBufferStorageArea );
configASSERT( pxStaticStreamBuffer );
configASSERT( xTriggerLevelBytes <= xBufferSizeBytes );
/* ... */
if( xTriggerLevelBytes == ( size_t ) 0 )
{
xTriggerLevelBytes = ( size_t ) 1;
}if (xTriggerLevelBytes == ( size_t ) 0) { ... }
if( xIsMessageBuffer != pdFALSE )
{
ucFlags = sbFLAGS_IS_MESSAGE_BUFFER | sbFLAGS_IS_STATICALLY_ALLOCATED;
}if (xIsMessageBuffer != pdFALSE) { ... }
else
{
ucFlags = sbFLAGS_IS_STATICALLY_ALLOCATED;
}else { ... }
/* ... */
configASSERT( xBufferSizeBytes > sbBYTES_TO_STORE_MESSAGE_LENGTH );
#if( configASSERT_DEFINED == 1 )
{
/* ... */
volatile size_t xSize = sizeof( StaticStreamBuffer_t );
configASSERT( xSize == sizeof( StreamBuffer_t ) );
...} /* ... */
#endif
if( ( pucStreamBufferStorageArea != NULL ) && ( pxStaticStreamBuffer != NULL ) )
{
prvInitialiseNewStreamBuffer( pxStreamBuffer,
pucStreamBufferStorageArea,
xBufferSizeBytes,
xTriggerLevelBytes,
ucFlags );
/* ... */
pxStreamBuffer->ucFlags |= sbFLAGS_IS_STATICALLY_ALLOCATED;
traceSTREAM_BUFFER_CREATE( pxStreamBuffer, xIsMessageBuffer );
xReturn = ( StreamBufferHandle_t ) pxStaticStreamBuffer;
}if (( pucStreamBufferStorageArea != NULL ) && ( pxStaticStreamBuffer != NULL )) { ... }
else
{
xReturn = NULL;
traceSTREAM_BUFFER_CREATE_STATIC_FAILED( xReturn, xIsMessageBuffer );
}else { ... }
return xReturn;
}xStreamBufferGenericCreateStatic (size_t xBufferSizeBytes, size_t xTriggerLevelBytes, BaseType_t xIsMessageBuffer, uint8_t * const pucStreamBufferStorageArea, StaticStreamBuffer_t * const pxStaticStreamBuffer) { ... }
/* ... */
#endif
void vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer )
{
StreamBuffer_t * pxStreamBuffer = xStreamBuffer;
configASSERT( pxStreamBuffer );
traceSTREAM_BUFFER_DELETE( xStreamBuffer );
if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) pdFALSE )
{
#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
{
/* ... */
vPortFree( ( void * ) pxStreamBuffer );
...}/* ... */
#else
{
/* ... */
configASSERT( xStreamBuffer == ( StreamBufferHandle_t ) ~0 );
...}/* ... */
#endif
}if (( pxStreamBuffer->ucFlags & sbFLAGS_IS_STATICALLY_ALLOCATED ) == ( uint8_t ) pdFALSE) { ... }
else
{
/* ... */
( void ) memset( pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) );
}else { ... }
}{ ... }
BaseType_t xStreamBufferReset( StreamBufferHandle_t xStreamBuffer )
{
StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
BaseType_t xReturn = pdFAIL;
#if( configUSE_TRACE_FACILITY == 1 )
UBaseType_t uxStreamBufferNumber;
#endif
configASSERT( pxStreamBuffer );
#if( configUSE_TRACE_FACILITY == 1 )
{
/* ... */
uxStreamBufferNumber = pxStreamBuffer->uxStreamBufferNumber;
...}/* ... */
#endif
taskENTER_CRITICAL();
{
if( pxStreamBuffer->xTaskWaitingToReceive == NULL )
{
if( pxStreamBuffer->xTaskWaitingToSend == NULL )
{
prvInitialiseNewStreamBuffer( pxStreamBuffer,
pxStreamBuffer->pucBuffer,
pxStreamBuffer->xLength,
pxStreamBuffer->xTriggerLevelBytes,
pxStreamBuffer->ucFlags );
xReturn = pdPASS;
#if( configUSE_TRACE_FACILITY == 1 )
{
pxStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
...}/* ... */
#endif
traceSTREAM_BUFFER_RESET( xStreamBuffer );
}if (pxStreamBuffer->xTaskWaitingToSend == NULL) { ... }
}if (pxStreamBuffer->xTaskWaitingToReceive == NULL) { ... }
...}
taskEXIT_CRITICAL();
return xReturn;
}{ ... }
BaseType_t xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer, size_t xTriggerLevel )
{
StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
BaseType_t xReturn;
configASSERT( pxStreamBuffer );
if( xTriggerLevel == ( size_t ) 0 )
{
xTriggerLevel = ( size_t ) 1;
}if (xTriggerLevel == ( size_t ) 0) { ... }
/* ... */
if( xTriggerLevel <= pxStreamBuffer->xLength )
{
pxStreamBuffer->xTriggerLevelBytes = xTriggerLevel;
xReturn = pdPASS;
}if (xTriggerLevel <= pxStreamBuffer->xLength) { ... }
else
{
xReturn = pdFALSE;
}else { ... }
return xReturn;
}{ ... }
size_t xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer )
{
const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
size_t xSpace;
configASSERT( pxStreamBuffer );
xSpace = pxStreamBuffer->xLength + pxStreamBuffer->xTail;
xSpace -= pxStreamBuffer->xHead;
xSpace -= ( size_t ) 1;
if( xSpace >= pxStreamBuffer->xLength )
{
xSpace -= pxStreamBuffer->xLength;
}if (xSpace >= pxStreamBuffer->xLength) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
return xSpace;
}{ ... }
size_t xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer )
{
const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
size_t xReturn;
configASSERT( pxStreamBuffer );
xReturn = prvBytesInBuffer( pxStreamBuffer );
return xReturn;
}{ ... }
size_t xStreamBufferSend( StreamBufferHandle_t xStreamBuffer,
const void *pvTxData,
size_t xDataLengthBytes,
TickType_t xTicksToWait )
{
StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
size_t xReturn, xSpace = 0;
size_t xRequiredSpace = xDataLengthBytes;
TimeOut_t xTimeOut;
configASSERT( pvTxData );
configASSERT( pxStreamBuffer );
/* ... */
if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
{
xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
configASSERT( xRequiredSpace > xDataLengthBytes );
}if (( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
if( xTicksToWait != ( TickType_t ) 0 )
{
vTaskSetTimeOutState( &xTimeOut );
do
{
/* ... */
taskENTER_CRITICAL();
{
xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
if( xSpace < xRequiredSpace )
{
( void ) xTaskNotifyStateClear( NULL );
configASSERT( pxStreamBuffer->xTaskWaitingToSend == NULL );
pxStreamBuffer->xTaskWaitingToSend = xTaskGetCurrentTaskHandle();
}if (xSpace < xRequiredSpace) { ... }
else
{
taskEXIT_CRITICAL();
break;
}else { ... }
...}
taskEXIT_CRITICAL();
traceBLOCKING_ON_STREAM_BUFFER_SEND( xStreamBuffer );
( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
pxStreamBuffer->xTaskWaitingToSend = NULL;
...} while( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE );
}if (xTicksToWait != ( TickType_t ) 0) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
if( xSpace == ( size_t ) 0 )
{
xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
}if (xSpace == ( size_t ) 0) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
if( xReturn > ( size_t ) 0 )
{
traceSTREAM_BUFFER_SEND( xStreamBuffer, xReturn );
if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
{
sbSEND_COMPLETED( pxStreamBuffer );
}if (prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
}if (xReturn > ( size_t ) 0) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
traceSTREAM_BUFFER_SEND_FAILED( xStreamBuffer );
}else { ... }
return xReturn;
}{ ... }
size_t xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer,
const void *pvTxData,
size_t xDataLengthBytes,
BaseType_t * const pxHigherPriorityTaskWoken )
{
StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
size_t xReturn, xSpace;
size_t xRequiredSpace = xDataLengthBytes;
configASSERT( pvTxData );
configASSERT( pxStreamBuffer );
/* ... */
if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
{
xRequiredSpace += sbBYTES_TO_STORE_MESSAGE_LENGTH;
}if (( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
xSpace = xStreamBufferSpacesAvailable( pxStreamBuffer );
xReturn = prvWriteMessageToBuffer( pxStreamBuffer, pvTxData, xDataLengthBytes, xSpace, xRequiredSpace );
if( xReturn > ( size_t ) 0 )
{
if( prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes )
{
sbSEND_COMPLETE_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
}if (prvBytesInBuffer( pxStreamBuffer ) >= pxStreamBuffer->xTriggerLevelBytes) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
}if (xReturn > ( size_t ) 0) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
traceSTREAM_BUFFER_SEND_FROM_ISR( xStreamBuffer, xReturn );
return xReturn;
}{ ... }
static size_t prvWriteMessageToBuffer( StreamBuffer_t * const pxStreamBuffer,
const void * pvTxData,
size_t xDataLengthBytes,
size_t xSpace,
size_t xRequiredSpace )
{
BaseType_t xShouldWrite;
size_t xReturn;
if( xSpace == ( size_t ) 0 )
{
/* ... */
xShouldWrite = pdFALSE;
}if (xSpace == ( size_t ) 0) { ... }
else if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) == ( uint8_t ) 0 )
{
/* ... */
xShouldWrite = pdTRUE;
xDataLengthBytes = configMIN( xDataLengthBytes, xSpace );
}else if (( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) == ( uint8_t ) 0) { ... }
else if( xSpace >= xRequiredSpace )
{
/* ... */
xShouldWrite = pdTRUE;
( void ) prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) &( xDataLengthBytes ), sbBYTES_TO_STORE_MESSAGE_LENGTH );
}else if (xSpace >= xRequiredSpace) { ... }
else
{
xShouldWrite = pdFALSE;
}else { ... }
if( xShouldWrite != pdFALSE )
{
xReturn = prvWriteBytesToBuffer( pxStreamBuffer, ( const uint8_t * ) pvTxData, xDataLengthBytes );
}if (xShouldWrite != pdFALSE) { ... }
else
{
xReturn = 0;
}else { ... }
return xReturn;
}{ ... }
size_t xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer,
void *pvRxData,
size_t xBufferLengthBytes,
TickType_t xTicksToWait )
{
StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
configASSERT( pvRxData );
configASSERT( pxStreamBuffer );
/* ... */
if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
{
xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
}if (( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0) { ... }
else
{
xBytesToStoreMessageLength = 0;
}else { ... }
if( xTicksToWait != ( TickType_t ) 0 )
{
/* ... */
taskENTER_CRITICAL();
{
xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
/* ... */
if( xBytesAvailable <= xBytesToStoreMessageLength )
{
( void ) xTaskNotifyStateClear( NULL );
configASSERT( pxStreamBuffer->xTaskWaitingToReceive == NULL );
pxStreamBuffer->xTaskWaitingToReceive = xTaskGetCurrentTaskHandle();
}if (xBytesAvailable <= xBytesToStoreMessageLength) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
...}
taskEXIT_CRITICAL();
if( xBytesAvailable <= xBytesToStoreMessageLength )
{
traceBLOCKING_ON_STREAM_BUFFER_RECEIVE( xStreamBuffer );
( void ) xTaskNotifyWait( ( uint32_t ) 0, ( uint32_t ) 0, NULL, xTicksToWait );
pxStreamBuffer->xTaskWaitingToReceive = NULL;
xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
}if (xBytesAvailable <= xBytesToStoreMessageLength) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
}if (xTicksToWait != ( TickType_t ) 0) { ... }
else
{
xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
}else { ... }
/* ... */
if( xBytesAvailable > xBytesToStoreMessageLength )
{
xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable, xBytesToStoreMessageLength );
if( xReceivedLength != ( size_t ) 0 )
{
traceSTREAM_BUFFER_RECEIVE( xStreamBuffer, xReceivedLength );
sbRECEIVE_COMPLETED( pxStreamBuffer );
}if (xReceivedLength != ( size_t ) 0) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
}if (xBytesAvailable > xBytesToStoreMessageLength) { ... }
else
{
traceSTREAM_BUFFER_RECEIVE_FAILED( xStreamBuffer );
mtCOVERAGE_TEST_MARKER();
}else { ... }
return xReceivedLength;
}{ ... }
size_t xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer )
{
StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
size_t xReturn, xBytesAvailable, xOriginalTail;
configMESSAGE_BUFFER_LENGTH_TYPE xTempReturn;
configASSERT( pxStreamBuffer );
if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
{
xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
if( xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH )
{
/* ... */
xOriginalTail = pxStreamBuffer->xTail;
( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempReturn, sbBYTES_TO_STORE_MESSAGE_LENGTH, xBytesAvailable );
xReturn = ( size_t ) xTempReturn;
pxStreamBuffer->xTail = xOriginalTail;
}if (xBytesAvailable > sbBYTES_TO_STORE_MESSAGE_LENGTH) { ... }
else
{
/* ... */
configASSERT( xBytesAvailable == 0 );
xReturn = 0;
}else { ... }
}if (( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0) { ... }
else
{
xReturn = 0;
}else { ... }
return xReturn;
}{ ... }
size_t xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer,
void *pvRxData,
size_t xBufferLengthBytes,
BaseType_t * const pxHigherPriorityTaskWoken )
{
StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
size_t xReceivedLength = 0, xBytesAvailable, xBytesToStoreMessageLength;
configASSERT( pvRxData );
configASSERT( pxStreamBuffer );
/* ... */
if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
{
xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
}if (( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0) { ... }
else
{
xBytesToStoreMessageLength = 0;
}else { ... }
xBytesAvailable = prvBytesInBuffer( pxStreamBuffer );
/* ... */
if( xBytesAvailable > xBytesToStoreMessageLength )
{
xReceivedLength = prvReadMessageFromBuffer( pxStreamBuffer, pvRxData, xBufferLengthBytes, xBytesAvailable, xBytesToStoreMessageLength );
if( xReceivedLength != ( size_t ) 0 )
{
sbRECEIVE_COMPLETED_FROM_ISR( pxStreamBuffer, pxHigherPriorityTaskWoken );
}if (xReceivedLength != ( size_t ) 0) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
}if (xBytesAvailable > xBytesToStoreMessageLength) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
traceSTREAM_BUFFER_RECEIVE_FROM_ISR( xStreamBuffer, xReceivedLength );
return xReceivedLength;
}{ ... }
static size_t prvReadMessageFromBuffer( StreamBuffer_t *pxStreamBuffer,
void *pvRxData,
size_t xBufferLengthBytes,
size_t xBytesAvailable,
size_t xBytesToStoreMessageLength )
{
size_t xOriginalTail, xReceivedLength, xNextMessageLength;
configMESSAGE_BUFFER_LENGTH_TYPE xTempNextMessageLength;
if( xBytesToStoreMessageLength != ( size_t ) 0 )
{
/* ... */
xOriginalTail = pxStreamBuffer->xTail;
( void ) prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) &xTempNextMessageLength, xBytesToStoreMessageLength, xBytesAvailable );
xNextMessageLength = ( size_t ) xTempNextMessageLength;
/* ... */
xBytesAvailable -= xBytesToStoreMessageLength;
/* ... */
if( xNextMessageLength > xBufferLengthBytes )
{
/* ... */
pxStreamBuffer->xTail = xOriginalTail;
xNextMessageLength = 0;
}if (xNextMessageLength > xBufferLengthBytes) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
}if (xBytesToStoreMessageLength != ( size_t ) 0) { ... }
else
{
/* ... */
xNextMessageLength = xBufferLengthBytes;
}else { ... }
xReceivedLength = prvReadBytesFromBuffer( pxStreamBuffer, ( uint8_t * ) pvRxData, xNextMessageLength, xBytesAvailable );
return xReceivedLength;
}{ ... }
BaseType_t xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer )
{
const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
BaseType_t xReturn;
size_t xTail;
configASSERT( pxStreamBuffer );
xTail = pxStreamBuffer->xTail;
if( pxStreamBuffer->xHead == xTail )
{
xReturn = pdTRUE;
}if (pxStreamBuffer->xHead == xTail) { ... }
else
{
xReturn = pdFALSE;
}else { ... }
return xReturn;
}{ ... }
BaseType_t xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer )
{
BaseType_t xReturn;
size_t xBytesToStoreMessageLength;
const StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
configASSERT( pxStreamBuffer );
/* ... */
if( ( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0 )
{
xBytesToStoreMessageLength = sbBYTES_TO_STORE_MESSAGE_LENGTH;
}if (( pxStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER ) != ( uint8_t ) 0) { ... }
else
{
xBytesToStoreMessageLength = 0;
}else { ... }
if( xStreamBufferSpacesAvailable( xStreamBuffer ) <= xBytesToStoreMessageLength )
{
xReturn = pdTRUE;
}if (xStreamBufferSpacesAvailable( xStreamBuffer ) <= xBytesToStoreMessageLength) { ... }
else
{
xReturn = pdFALSE;
}else { ... }
return xReturn;
}{ ... }
BaseType_t xStreamBufferSendCompletedFromISR( StreamBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken )
{
StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
BaseType_t xReturn;
UBaseType_t uxSavedInterruptStatus;
configASSERT( pxStreamBuffer );
uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR();
{
if( ( pxStreamBuffer )->xTaskWaitingToReceive != NULL )
{
( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToReceive,
( uint32_t ) 0,
eNoAction,
pxHigherPriorityTaskWoken );
( pxStreamBuffer )->xTaskWaitingToReceive = NULL;
xReturn = pdTRUE;
}if (( pxStreamBuffer )->xTaskWaitingToReceive != NULL) { ... }
else
{
xReturn = pdFALSE;
}else { ... }
...}
portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
return xReturn;
}{ ... }
BaseType_t xStreamBufferReceiveCompletedFromISR( StreamBufferHandle_t xStreamBuffer, BaseType_t *pxHigherPriorityTaskWoken )
{
StreamBuffer_t * const pxStreamBuffer = xStreamBuffer;
BaseType_t xReturn;
UBaseType_t uxSavedInterruptStatus;
configASSERT( pxStreamBuffer );
uxSavedInterruptStatus = ( UBaseType_t ) portSET_INTERRUPT_MASK_FROM_ISR();
{
if( ( pxStreamBuffer )->xTaskWaitingToSend != NULL )
{
( void ) xTaskNotifyFromISR( ( pxStreamBuffer )->xTaskWaitingToSend,
( uint32_t ) 0,
eNoAction,
pxHigherPriorityTaskWoken );
( pxStreamBuffer )->xTaskWaitingToSend = NULL;
xReturn = pdTRUE;
}if (( pxStreamBuffer )->xTaskWaitingToSend != NULL) { ... }
else
{
xReturn = pdFALSE;
}else { ... }
...}
portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
return xReturn;
}{ ... }
static size_t prvWriteBytesToBuffer( StreamBuffer_t * const pxStreamBuffer, const uint8_t *pucData, size_t xCount )
{
size_t xNextHead, xFirstLength;
configASSERT( xCount > ( size_t ) 0 );
xNextHead = pxStreamBuffer->xHead;
/* ... */
xFirstLength = configMIN( pxStreamBuffer->xLength - xNextHead, xCount );
configASSERT( ( xNextHead + xFirstLength ) <= pxStreamBuffer->xLength );
( void ) memcpy( ( void* ) ( &( pxStreamBuffer->pucBuffer[ xNextHead ] ) ), ( const void * ) pucData, xFirstLength );
/* ... */
if( xCount > xFirstLength )
{
configASSERT( ( xCount - xFirstLength ) <= pxStreamBuffer->xLength );
( void ) memcpy( ( void * ) pxStreamBuffer->pucBuffer, ( const void * ) &( pucData[ xFirstLength ] ), xCount - xFirstLength );
}if (xCount > xFirstLength) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
xNextHead += xCount;
if( xNextHead >= pxStreamBuffer->xLength )
{
xNextHead -= pxStreamBuffer->xLength;
}if (xNextHead >= pxStreamBuffer->xLength) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
pxStreamBuffer->xHead = xNextHead;
return xCount;
}{ ... }
static size_t prvReadBytesFromBuffer( StreamBuffer_t *pxStreamBuffer, uint8_t *pucData, size_t xMaxCount, size_t xBytesAvailable )
{
size_t xCount, xFirstLength, xNextTail;
xCount = configMIN( xBytesAvailable, xMaxCount );
if( xCount > ( size_t ) 0 )
{
xNextTail = pxStreamBuffer->xTail;
/* ... */
xFirstLength = configMIN( pxStreamBuffer->xLength - xNextTail, xCount );
/* ... */
configASSERT( xFirstLength <= xMaxCount );
configASSERT( ( xNextTail + xFirstLength ) <= pxStreamBuffer->xLength );
( void ) memcpy( ( void * ) pucData, ( const void * ) &( pxStreamBuffer->pucBuffer[ xNextTail ] ), xFirstLength );
/* ... */
if( xCount > xFirstLength )
{
configASSERT( xCount <= xMaxCount );
( void ) memcpy( ( void * ) &( pucData[ xFirstLength ] ), ( void * ) ( pxStreamBuffer->pucBuffer ), xCount - xFirstLength );
}if (xCount > xFirstLength) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
/* ... */
xNextTail += xCount;
if( xNextTail >= pxStreamBuffer->xLength )
{
xNextTail -= pxStreamBuffer->xLength;
}if (xNextTail >= pxStreamBuffer->xLength) { ... }
pxStreamBuffer->xTail = xNextTail;
}if (xCount > ( size_t ) 0) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
return xCount;
}{ ... }
static size_t prvBytesInBuffer( const StreamBuffer_t * const pxStreamBuffer )
{
size_t xCount;
xCount = pxStreamBuffer->xLength + pxStreamBuffer->xHead;
xCount -= pxStreamBuffer->xTail;
if ( xCount >= pxStreamBuffer->xLength )
{
xCount -= pxStreamBuffer->xLength;
}if (xCount >= pxStreamBuffer->xLength) { ... }
else
{
mtCOVERAGE_TEST_MARKER();
}else { ... }
return xCount;
}{ ... }
static void prvInitialiseNewStreamBuffer( StreamBuffer_t * const pxStreamBuffer,
uint8_t * const pucBuffer,
size_t xBufferSizeBytes,
size_t xTriggerLevelBytes,
uint8_t ucFlags )
{
/* ... */
#if( configASSERT_DEFINED == 1 )
{
/* ... */
const BaseType_t xWriteValue = 0x55;
configASSERT( memset( pucBuffer, ( int ) xWriteValue, xBufferSizeBytes ) == pucBuffer );
...} /* ... */
#endif
( void ) memset( ( void * ) pxStreamBuffer, 0x00, sizeof( StreamBuffer_t ) );
pxStreamBuffer->pucBuffer = pucBuffer;
pxStreamBuffer->xLength = xBufferSizeBytes;
pxStreamBuffer->xTriggerLevelBytes = xTriggerLevelBytes;
pxStreamBuffer->ucFlags = ucFlags;
}{ ... }
#if ( configUSE_TRACE_FACILITY == 1 )
UBaseType_t uxStreamBufferGetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer )
{
return xStreamBuffer->uxStreamBufferNumber;
}{ ... }
/* ... */#endif
#if ( configUSE_TRACE_FACILITY == 1 )
void vStreamBufferSetStreamBufferNumber( StreamBufferHandle_t xStreamBuffer, UBaseType_t uxStreamBufferNumber )
{
xStreamBuffer->uxStreamBufferNumber = uxStreamBufferNumber;
}{ ... }
/* ... */#endif
#if ( configUSE_TRACE_FACILITY == 1 )
uint8_t ucStreamBufferGetStreamBufferType( StreamBufferHandle_t xStreamBuffer )
{
return ( xStreamBuffer->ucFlags & sbFLAGS_IS_MESSAGE_BUFFER );
}{ ... }
/* ... */#endif