summaryrefslogtreecommitdiff
path: root/FreeRTOS/Demo/Common/Minimal/MessageBufferDemo.c
blob: 05820c916897d48c1c6ca0b184fb3157def6661c (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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/*
 * FreeRTOS Kernel V10.4.1
 * 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://www.FreeRTOS.org
 * http://aws.amazon.com/freertos
 *
 * 1 tab == 4 spaces!
 */

/* Standard includes. */
#include "stdio.h"
#include "string.h"

/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "message_buffer.h"

/* Demo app includes. */
#include "MessageBufferDemo.h"

/* The number of bytes of storage in the message buffers used in this test. */
#define mbMESSAGE_BUFFER_LENGTH_BYTES	( ( size_t ) 50 )

/* The number of additional bytes used to store the length of each message. */
#define mbBYTES_TO_STORE_MESSAGE_LENGTH ( sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) )

/* Start and end ASCII characters used in messages sent to the buffers. */
#define mbASCII_SPACE					32
#define mbASCII_TILDA					126

/* Defines the number of tasks to create in this test and demo. */
#define mbNUMBER_OF_ECHO_CLIENTS	( 2 )
#define mbNUMBER_OF_SENDER_TASKS	( 2 )

/* Priority of the test tasks.  The send and receive go from low to high
priority tasks, and from high to low priority tasks. */
#define mbLOWER_PRIORITY			( tskIDLE_PRIORITY )
#define mbHIGHER_PRIORITY			( tskIDLE_PRIORITY + 1 )

/* Block times used when sending and receiving from the message buffers. */
#define mbRX_TX_BLOCK_TIME			pdMS_TO_TICKS( 175UL )

/* A block time of 0 means "don't block". */
#define mbDONT_BLOCK				( 0 )

/*-----------------------------------------------------------*/

/*
 * Performs various tests that do not require multiple tasks to interact.
 */
static void prvSingleTaskTests( MessageBufferHandle_t xMessageBuffer );

/*
 * Tests sending and receiving various lengths of messages via a message buffer.
 * The echo client sends the messages to the echo server, which then sends the
 * message back to the echo client which, checks it receives exactly what it
 * sent.
 */
static void prvEchoClient( void *pvParameters );
static void prvEchoServer( void *pvParameters );

/*
 * Tasks that send and receive to a message buffer at a low priority and without
 * blocking, so the send and receive functions interleave in time as the tasks
 * are switched in and out.
 */
static void prvNonBlockingReceiverTask( void *pvParameters );
static void prvNonBlockingSenderTask( void *pvParameters );

#if( configSUPPORT_STATIC_ALLOCATION == 1  )
	/* This file tests both statically and dynamically allocated message buffers.
	Allocate the structures and buffers to be used by the statically allocated
	objects, which get used in the echo tests. */
	static void prvReceiverTask( void *pvParameters );
	static void prvSenderTask( void *pvParameters );

	static StaticMessageBuffer_t xStaticMessageBuffers[ mbNUMBER_OF_ECHO_CLIENTS ];
	static uint8_t ucBufferStorage[ mbNUMBER_OF_SENDER_TASKS ][ mbMESSAGE_BUFFER_LENGTH_BYTES + 1 ];
	static uint32_t ulSenderLoopCounters[ mbNUMBER_OF_SENDER_TASKS ] = { 0 };
#endif /* configSUPPORT_STATIC_ALLOCATION */

/*-----------------------------------------------------------*/

/* The buffers used by the echo client and server tasks. */
typedef struct ECHO_MESSAGE_BUFFERS
{
	/* Handles to the data structures that describe the message buffers. */
	MessageBufferHandle_t xEchoClientBuffer;
	MessageBufferHandle_t xEchoServerBuffer;
} EchoMessageBuffers_t;
static uint32_t ulEchoLoopCounters[ mbNUMBER_OF_ECHO_CLIENTS ] = { 0 };

/* The non-blocking tasks monitor their operation, and if no errors have been
found, increment ulNonBlockingRxCounter.  xAreMessageBufferTasksStillRunning()
then checks ulNonBlockingRxCounter and only returns pdPASS if
ulNonBlockingRxCounter is still incrementing. */
static uint32_t ulNonBlockingRxCounter = 0;

/* A message that is longer than the buffer, parts of which are written to the
message buffer to test writing different lengths at different offsets. */
static const char *pc55ByteString = "One two three four five six seven eight nine ten eleve";

/* Remember the required stack size so tasks can be created at run time (after
initialisation time. */
static configSTACK_DEPTH_TYPE xBlockingStackSize = 0;

/*-----------------------------------------------------------*/

void vStartMessageBufferTasks( configSTACK_DEPTH_TYPE xStackSize  )
{
MessageBufferHandle_t xMessageBuffer;

#ifndef configMESSAGE_BUFFER_BLOCK_TASK_STACK_SIZE
	xBlockingStackSize = ( xStackSize + ( xStackSize >> 1U ) );
#else
	xBlockingStackSize = configMESSAGE_BUFFER_BLOCK_TASK_STACK_SIZE;
#endif

	/* The echo servers sets up the message buffers before creating the echo
	client tasks.  One set of tasks has the server as the higher priority, and
	the other has the client as the higher priority. */
	xTaskCreate( prvEchoServer, "1EchoServer", xBlockingStackSize, NULL, mbHIGHER_PRIORITY, NULL );
	xTaskCreate( prvEchoServer, "2EchoServer", xBlockingStackSize, NULL, mbLOWER_PRIORITY, NULL );

	/* The non blocking tasks run continuously and will interleave with each
	other, so must be created at the lowest priority.  The message buffer they
	use is created and passed in using the task's parameter. */
	xMessageBuffer = xMessageBufferCreate( mbMESSAGE_BUFFER_LENGTH_BYTES );
	xTaskCreate( prvNonBlockingReceiverTask, "NonBlkRx", xStackSize, ( void * ) xMessageBuffer, tskIDLE_PRIORITY, NULL );
	xTaskCreate( prvNonBlockingSenderTask, "NonBlkTx", xStackSize, ( void * ) xMessageBuffer, tskIDLE_PRIORITY, NULL );

	#if( configSUPPORT_STATIC_ALLOCATION == 1  )
	{
		/* The sender tasks set up the message buffers before creating the
		receiver tasks.  Priorities must be 0 and 1 as the priority is used to
		index into the xStaticMessageBuffers and ucBufferStorage arrays. */
		xTaskCreate( prvSenderTask, "1Sender", xBlockingStackSize, NULL, mbHIGHER_PRIORITY, NULL );
		xTaskCreate( prvSenderTask, "2Sender", xBlockingStackSize, NULL, mbLOWER_PRIORITY, NULL );
	}
	#endif /* configSUPPORT_STATIC_ALLOCATION */
}
/*-----------------------------------------------------------*/

static void prvSingleTaskTests( MessageBufferHandle_t xMessageBuffer )
{
size_t xReturned, xItem, xExpectedSpace, xNextLength;
const size_t xMax6ByteMessages = mbMESSAGE_BUFFER_LENGTH_BYTES / ( 6 + mbBYTES_TO_STORE_MESSAGE_LENGTH );
const size_t x6ByteLength = 6, x17ByteLength = 17;
uint8_t *pucFullBuffer, *pucData, *pucReadData;
TickType_t xTimeBeforeCall, xTimeAfterCall;
const TickType_t xBlockTime = pdMS_TO_TICKS( 25 ), xAllowableMargin = pdMS_TO_TICKS( 3 );
UBaseType_t uxOriginalPriority;

	/* Remove warning in case configASSERT() is not defined. */
	( void ) xAllowableMargin;

	/* To minimise stack and heap usage a full size buffer is allocated from
	the heap, then buffers which hold smaller amounts of data are overlayed
	with the larger buffer - just make sure not to use both at once!. */
	pucFullBuffer = pvPortMalloc( mbMESSAGE_BUFFER_LENGTH_BYTES );
	configASSERT( pucFullBuffer );

	pucData = pucFullBuffer;
	pucReadData = pucData + x17ByteLength;

	/* Nothing has been added or removed yet, so expect the free space to be
	exactly as created and the length of the next message to be 0. */
	xExpectedSpace = xMessageBufferSpaceAvailable( xMessageBuffer );
	configASSERT( xExpectedSpace == mbMESSAGE_BUFFER_LENGTH_BYTES );
	configASSERT( xMessageBufferIsEmpty( xMessageBuffer ) == pdTRUE );
	xNextLength = xMessageBufferNextLengthBytes( xMessageBuffer );
	configASSERT( xNextLength == 0 );
	/* In case configASSERT() is not define. */
	( void ) xExpectedSpace;
	( void ) xNextLength;

	/* The buffer is 50 bytes long.  When an item is added to the buffer an
	additional 4 bytes are added to hold the item's size.  That means adding
	6 bytes to the buffer will actually add 10 bytes to the buffer.  Therefore,
	with a 50 byte buffer, a maximum of 5 6 bytes items can be added before the
	buffer is completely full.  NOTE:  The numbers in this paragraph assume
	sizeof( configMESSAGE_BUFFER_LENGTH_TYPE ) == 4. */
	for( xItem = 0; xItem < xMax6ByteMessages; xItem++ )
	{
		configASSERT( xMessageBufferIsFull( xMessageBuffer ) == pdFALSE );

		/* Generate recognisable data to write to the buffer.  This is just
		ascii characters that shows which loop iteration the data was written
		in. The 'FromISR' version is used to give it some exercise as a block
		time is not used.  That requires the call to be in a critical section
		so this code can also run on FreeRTOS ports that do not support
		interrupt nesting (and so don't have interrupt safe critical
		sections).*/
		memset( ( void * ) pucData, ( ( int ) '0' ) + ( int ) xItem, x6ByteLength );
		taskENTER_CRITICAL();
		{
			xReturned = xMessageBufferSendFromISR( xMessageBuffer, ( void * ) pucData, x6ByteLength, NULL );
		}
		taskEXIT_CRITICAL();
		configASSERT( xReturned == x6ByteLength );
		( void ) xReturned; /* In case configASSERT() is not defined. */

		/* The space in the buffer will have reduced by the amount of user data
		written into the buffer and the amount of space used to store the length
		of the data written into the buffer. */
		xExpectedSpace -= ( x6ByteLength + mbBYTES_TO_STORE_MESSAGE_LENGTH );
		xReturned = xMessageBufferSpaceAvailable( xMessageBuffer );
		configASSERT( xReturned == xExpectedSpace );
		( void ) xReturned; /* In case configASSERT() is not defined. */

		/* Only 6 byte messages are written. */
		xNextLength = xMessageBufferNextLengthBytes( xMessageBuffer );
		configASSERT( xNextLength == x6ByteLength );
		( void ) xNextLength; /* In case configASSERT() is not defined. */
	}

	/* Now the buffer should be full, and attempting to add anything will should
	fail. */
	configASSERT( xMessageBufferIsFull( xMessageBuffer ) == pdTRUE );
	xReturned = xMessageBufferSend( xMessageBuffer, ( void * ) pucData, sizeof( pucData[ 0 ] ), mbDONT_BLOCK );
	configASSERT( xReturned == 0 );
	( void ) xReturned; /* In case configASSERT() is not defined. */

	/* Adding with a timeout should also fail after the appropriate time.  The
	priority is temporarily boosted in this part of the test to keep the
	allowable margin to a minimum. */
	uxOriginalPriority = uxTaskPriorityGet( NULL );
	vTaskPrioritySet( NULL, configMAX_PRIORITIES - 1 );
	xTimeBeforeCall = xTaskGetTickCount();
	xReturned = xMessageBufferSend( xMessageBuffer, ( void * ) pucData, sizeof( pucData[ 0 ] ), xBlockTime );
	xTimeAfterCall = xTaskGetTickCount();
	vTaskPrioritySet( NULL, uxOriginalPriority );
	configASSERT( ( ( TickType_t ) ( xTimeAfterCall - xTimeBeforeCall ) ) >= xBlockTime );
	configASSERT( ( ( TickType_t ) ( xTimeAfterCall - xTimeBeforeCall ) ) < ( xBlockTime + xAllowableMargin ) );
	configASSERT( xReturned == 0 );
	( void ) xReturned; /* In case configASSERT() is not defined. */
	( void ) xTimeBeforeCall;
	( void ) xTimeAfterCall;


	/* The buffer is now full of data in the form "000000", "111111", etc.  Make
	sure the data is read out as expected. */
	for( xItem = 0; xItem < xMax6ByteMessages; xItem++ )
	{
		/* Generate the data that is expected to be read out for this loop
		iteration. */
		memset( ( void * ) pucData, ( ( int ) '0' ) + ( int ) xItem, x6ByteLength );

		/* Try reading the message into a buffer that is too small.  The message
		should remain in the buffer. */
		xReturned = xMessageBufferReceive( xMessageBuffer, ( void * ) pucReadData, x6ByteLength - 1, mbDONT_BLOCK );
		configASSERT( xReturned == 0 );
		( void ) xReturned; /* In case configASSERT() is not defined. */

		/* Should still be at least one 6 byte message still available. */
		xNextLength = xMessageBufferNextLengthBytes( xMessageBuffer );
		configASSERT( xNextLength == x6ByteLength );
		( void ) xNextLength; /* In case configASSERT() is not defined. */

		/* Read the next 6 bytes out.  The 'FromISR' version is used to give it
		some exercise as a block time is not used.  THa requires the code to be
		in a critical section so this test can be run with FreeRTOS ports that
		do not support interrupt nesting (and therefore don't have interrupt
		safe critical sections). */
		taskENTER_CRITICAL();
		{
			xReturned = xMessageBufferReceiveFromISR( xMessageBuffer, ( void * ) pucReadData, x6ByteLength, NULL );
		}
		taskEXIT_CRITICAL();
		configASSERT( xReturned == x6ByteLength );
		( void ) xReturned; /* In case configASSERT() is not defined. */

		/* Does the data read out match that expected? */
		configASSERT( memcmp( ( void * ) pucData, ( void * ) pucReadData, x6ByteLength ) == 0 );

		/* The space in the buffer will have increased by the amount of user
		data read from into the buffer and the amount of space used to store the
		length of the data read into the buffer. */
		xExpectedSpace += ( x6ByteLength + mbBYTES_TO_STORE_MESSAGE_LENGTH );
		xReturned = xMessageBufferSpaceAvailable( xMessageBuffer );
		configASSERT( xReturned == xExpectedSpace );
		( void ) xReturned; /* In case configASSERT() is not defined. */
	}

	/* The buffer should be empty again. */
	configASSERT( xMessageBufferIsEmpty( xMessageBuffer ) == pdTRUE );
	xExpectedSpace = xMessageBufferSpaceAvailable( xMessageBuffer );
	configASSERT( xExpectedSpace == mbMESSAGE_BUFFER_LENGTH_BYTES );
	( void ) xExpectedSpace; /* In case configASSERT() is not defined. */
	xNextLength = xMessageBufferNextLengthBytes( xMessageBuffer );
	configASSERT( xNextLength == 0 );
	( void ) xNextLength; /* In case configASSERT() is not defined. */


	/* Reading with a timeout should also fail after the appropriate time.  The
	priority is temporarily boosted in this part of the test to keep the
	allowable margin to a minimum. */
	vTaskPrioritySet( NULL, configMAX_PRIORITIES - 1 );
	xTimeBeforeCall = xTaskGetTickCount();
	xReturned = xMessageBufferReceive( xMessageBuffer, ( void * ) pucReadData, x6ByteLength, xBlockTime );
	xTimeAfterCall = xTaskGetTickCount();
	vTaskPrioritySet( NULL, uxOriginalPriority );
	configASSERT( ( xTimeAfterCall - xTimeBeforeCall ) >= xBlockTime );
	configASSERT( ( xTimeAfterCall - xTimeBeforeCall ) < ( xBlockTime + xAllowableMargin ) );
	configASSERT( xReturned == 0 );
	( void ) xReturned; /* In case configASSERT() is not defined. */
	( void ) xTimeBeforeCall;
	( void ) xTimeAfterCall;


	/* In the next loop 17 bytes are written to then read out on each iteration.
	The expected length variable is always used after 17 bytes have been written
	into the buffer - the length of the message is also written, making a total
	of 21 bytes consumed for each 17 byte message. */
	xExpectedSpace = mbMESSAGE_BUFFER_LENGTH_BYTES - ( x17ByteLength + mbBYTES_TO_STORE_MESSAGE_LENGTH );

	/* Reading and writing 17 bytes at a time will result in 21 bytes being
	written into the buffer, and as 50 is not divisible by 21, writing multiple
	times will cause the data to wrap in the buffer.*/
	for( xItem = 0; xItem < 100; xItem++ )
	{
		/* Generate recognisable data to write to the queue.  This is just
		ascii characters that shows which loop iteration the data was written
		in. */
		memset( ( void * ) pucData, ( ( int ) '0' ) + ( int ) xItem, x17ByteLength );
		xReturned = xMessageBufferSend( xMessageBuffer, ( void * ) pucData, x17ByteLength, mbDONT_BLOCK );
		configASSERT( xReturned == x17ByteLength );
		( void ) xReturned; /* In case configASSERT() is not defined. */

		/* Only 17 byte messages are written. */
		xNextLength = xMessageBufferNextLengthBytes( xMessageBuffer );
		configASSERT( xNextLength == x17ByteLength );
		( void ) xNextLength; /* In case configASSERT() is not defined. */

		/* The space in the buffer will have reduced by the amount of user data
		written into the buffer and the amount of space used to store the length
		of the data written into the buffer. */
		xReturned = xMessageBufferSpaceAvailable( xMessageBuffer );
		configASSERT( xReturned == xExpectedSpace );
		( void ) xReturned; /* In case configASSERT() is not defined. */

		/* Read the 17 bytes out again. */
		xReturned = xMessageBufferReceive( xMessageBuffer, ( void * ) pucReadData, x17ByteLength, mbDONT_BLOCK );
		configASSERT( xReturned == x17ByteLength );
		( void ) xReturned; /* In case configASSERT() is not defined. */

		/* Does the data read out match that expected? */
		configASSERT( memcmp( ( void * ) pucData, ( void * ) pucReadData, x17ByteLength ) == 0 );

		/* Don't expect any messages to be available as the data was read out
		again. */
		xNextLength = xMessageBufferNextLengthBytes( xMessageBuffer );
		configASSERT( xNextLength == 0 );
		( void ) xNextLength; /* In case configASSERT() is not defined. */
	}

	/* The buffer should be empty again. */
	configASSERT( xMessageBufferIsEmpty( xMessageBuffer ) == pdTRUE );
	xExpectedSpace = xMessageBufferSpaceAvailable( xMessageBuffer );
	configASSERT( xExpectedSpace == mbMESSAGE_BUFFER_LENGTH_BYTES );

	/* Cannot write within sizeof( size_t ) (assumed to be 4 bytes in this test)
	bytes of the full 50 bytes, as that would not leave space for the four bytes
	taken by the data length. */
	xReturned = xMessageBufferSend( xMessageBuffer, ( const void * ) pc55ByteString, mbMESSAGE_BUFFER_LENGTH_BYTES, mbDONT_BLOCK );
	configASSERT( xReturned == 0 );
	( void ) xReturned; /* In case configASSERT() is not defined. */
	#ifndef configMESSAGE_BUFFER_LENGTH_TYPE
	{
		/* The following will fail if configMESSAGE_BUFFER_LENGTH_TYPE is set
		to a non 32-bit type. */
		xReturned = xMessageBufferSend( xMessageBuffer, ( const void * ) pc55ByteString, mbMESSAGE_BUFFER_LENGTH_BYTES - 1, mbDONT_BLOCK );
		configASSERT( xReturned == 0 );
		( void ) xReturned; /* In case configASSERT() is not defined. */
		xReturned = xMessageBufferSend( xMessageBuffer, ( const void * ) pc55ByteString, mbMESSAGE_BUFFER_LENGTH_BYTES - 2, mbDONT_BLOCK );
		configASSERT( xReturned == 0 );
		( void ) xReturned; /* In case configASSERT() is not defined. */
		xReturned = xMessageBufferSend( xMessageBuffer, ( const void * ) pc55ByteString, mbMESSAGE_BUFFER_LENGTH_BYTES - 3, mbDONT_BLOCK );
		configASSERT( xReturned == 0 );
		( void ) xReturned; /* In case configASSERT() is not defined. */
	}
	#endif

	/* Don't expect any messages to be available as the above were too large to
	get written. */
	xNextLength = xMessageBufferNextLengthBytes( xMessageBuffer );
	configASSERT( xNextLength == 0 );
	( void ) xNextLength; /* In case configASSERT() is not defined. */

	/* Can write mbMESSAGE_BUFFER_LENGTH_BYTES - sizeof( size_t ) bytes though. */
	xReturned = xMessageBufferSend( xMessageBuffer, ( const void * ) pc55ByteString, mbMESSAGE_BUFFER_LENGTH_BYTES - sizeof( size_t ), mbDONT_BLOCK );
	configASSERT( xReturned == mbMESSAGE_BUFFER_LENGTH_BYTES - sizeof( size_t ) );
	( void ) xReturned; /* In case configASSERT() is not defined. */
	xNextLength = xMessageBufferNextLengthBytes( xMessageBuffer );
	configASSERT( xNextLength == ( mbMESSAGE_BUFFER_LENGTH_BYTES - sizeof( size_t ) ) );
	( void ) xNextLength; /* In case configASSERT() is not defined. */
	xReturned = xMessageBufferReceive( xMessageBuffer, ( void * ) pucFullBuffer, mbMESSAGE_BUFFER_LENGTH_BYTES - sizeof( size_t ), mbDONT_BLOCK );
	configASSERT( xReturned == ( mbMESSAGE_BUFFER_LENGTH_BYTES - sizeof( size_t ) ) );
	( void ) xReturned; /* In case configASSERT() is not defined. */
	configASSERT( memcmp( ( const void * ) pucFullBuffer, pc55ByteString, mbMESSAGE_BUFFER_LENGTH_BYTES - sizeof( size_t ) ) == 0 );

	/* Clean up. */
	vPortFree( pucFullBuffer );
	xMessageBufferReset( xMessageBuffer );
}
/*-----------------------------------------------------------*/

static void prvNonBlockingSenderTask( void *pvParameters )
{
MessageBufferHandle_t xMessageBuffer;
int32_t iDataToSend = 0;
size_t xStringLength;
const int32_t iMaxValue = 1500;
char cTxString[ 12 ]; /* Large enough to hold a 32 number in ASCII. */

	/* In this case the message buffer has already been created and is passed
	into the task using the task's parameter. */
	xMessageBuffer = ( MessageBufferHandle_t ) pvParameters;

	/* Create a string from an incrementing number.  The length of the
	string will increase and decrease as the value of the number increases
	then overflows. */
	memset( cTxString, 0x00, sizeof( cTxString ) );
	sprintf( cTxString, "%d", ( int ) iDataToSend );
	xStringLength = strlen( cTxString );

	for( ;; )
	{
		/* Doesn't block so calls can interleave with the non-blocking
		receives performed by prvNonBlockingReceiverTask(). */
		if( xMessageBufferSend( xMessageBuffer, ( void * ) cTxString, strlen( cTxString ), mbDONT_BLOCK ) == xStringLength )
		{
			iDataToSend++;

			if( iDataToSend > iMaxValue )
			{
				/* The value sent is reset back to 0 to ensure the string being sent
				does not remain at the same length for too long. */
				iDataToSend = 0;
			}

			/* Create the next string. */
			memset( cTxString, 0x00, sizeof( cTxString ) );
			sprintf( cTxString, "%d", ( int ) iDataToSend );
			xStringLength = strlen( cTxString );
		}
	}
}
/*-----------------------------------------------------------*/

static void prvNonBlockingReceiverTask( void *pvParameters )
{
MessageBufferHandle_t xMessageBuffer;
BaseType_t xNonBlockingReceiveError = pdFALSE;
int32_t iDataToSend = 0;
size_t xStringLength, xReceiveLength;
const int32_t iMaxValue = 1500;
char cExpectedString[ 12 ]; /* Large enough to hold a 32 number in ASCII. */
char cRxString[ 12 ];

	/* In this case the message buffer has already been created and is passed
	into the task using the task's parameter. */
	xMessageBuffer = ( MessageBufferHandle_t ) pvParameters;

	/* Create a string from an incrementing number.  The length of the
	string will increase and decrease as the value of the number increases
	then overflows.  This should always match the string sent to the buffer by
	the non blocking sender task. */
	memset( cExpectedString, 0x00, sizeof( cExpectedString ) );
	memset( cRxString, 0x00, sizeof( cRxString ) );
	sprintf( cExpectedString, "%d", ( int ) iDataToSend );
	xStringLength = strlen( cExpectedString );

	for( ;; )
	{
		/* Doesn't block so calls can interleave with the non-blocking
		receives performed by prvNonBlockingReceiverTask(). */
		xReceiveLength = xMessageBufferReceive( xMessageBuffer, ( void * ) cRxString, sizeof( cRxString ), mbDONT_BLOCK );

		/* Should only ever receive no data is available, or the expected
		length of data is available. */
		if( ( xReceiveLength != 0 ) && ( xReceiveLength != xStringLength ) )
		{
			xNonBlockingReceiveError = pdTRUE;
		}

		if( xReceiveLength == xStringLength )
		{
			/* Ensure the received data was that expected, then generate the
			next expected string. */
			if( strcmp( cRxString, cExpectedString ) != 0 )
			{
				xNonBlockingReceiveError = pdTRUE;
			}

			iDataToSend++;

			if( iDataToSend > iMaxValue )
			{
				/* The value sent is reset back to 0 to ensure the string being sent
				does not remain at the same length for too long. */
				iDataToSend = 0;
			}

			memset( cExpectedString, 0x00, sizeof( cExpectedString ) );
			memset( cRxString, 0x00, sizeof( cRxString ) );
			sprintf( cExpectedString, "%d", ( int ) iDataToSend );
			xStringLength = strlen( cExpectedString );

			if( xNonBlockingReceiveError == pdFALSE )
			{
				/* No errors detected so increment the counter that lets the
				check task know this test is still functioning correctly. */
				ulNonBlockingRxCounter++;
			}
		}
	}
}
/*-----------------------------------------------------------*/

#if( configSUPPORT_STATIC_ALLOCATION == 1  )

	static void prvSenderTask( void *pvParameters )
	{
	MessageBufferHandle_t xMessageBuffer, xTempMessageBuffer;
	int32_t iDataToSend = 0;
	const int32_t iSendsBetweenIncrements = 100;
	char cTxString[ 12 ]; /* Large enough to hold a 32 number in ASCII. */
	const TickType_t xTicksToWait = mbRX_TX_BLOCK_TIME, xShortDelay = pdMS_TO_TICKS( 50 );
	StaticMessageBuffer_t xStaticMessageBuffer;
	size_t xBytesSent;


	/* The task's priority is used as an index into the loop counters used to
	indicate this task is still running. */
	UBaseType_t uxIndex = uxTaskPriorityGet( NULL );

		/* Make sure a change in priority does not inadvertently result in an
		invalid array index. */
		configASSERT( uxIndex < mbNUMBER_OF_ECHO_CLIENTS );

		/* Avoid compiler warnings about unused parameters. */
		( void ) pvParameters;

		xMessageBuffer = xMessageBufferCreateStatic( sizeof( ucBufferStorage ) / mbNUMBER_OF_SENDER_TASKS, /* The number of bytes in each buffer in the array. */
													 &( ucBufferStorage[ uxIndex ][ 0 ] ), /* The address of the buffer to use within the array. */
													 &( xStaticMessageBuffers[ uxIndex ] ) ); /* The static message buffer structure to use within the array. */

		/* Now the message buffer has been created the receiver task can be created.
		If this sender task has the higher priority then the receiver task is
		created at the lower priority - if this sender task has the lower priority
		then the receiver task is created at the higher priority. */
		if( uxTaskPriorityGet( NULL ) == mbLOWER_PRIORITY )
		{
			/* Here prvSingleTaskTests() performs various tests on a message buffer
			that was created statically. */
			prvSingleTaskTests( xMessageBuffer );
			xTaskCreate( prvReceiverTask, "MsgReceiver", xBlockingStackSize,  ( void * ) xMessageBuffer, mbHIGHER_PRIORITY, NULL );
		}
		else
		{
			xTaskCreate( prvReceiverTask, "MsgReceiver", xBlockingStackSize,  ( void * ) xMessageBuffer, mbLOWER_PRIORITY, NULL );
		}

		for( ;; )
		{
			/* Create a string from an incrementing number.  The length of the
			string will increase and decrease as the value of the number increases
			then overflows. */
			memset( cTxString, 0x00, sizeof( cTxString ) );
			sprintf( cTxString, "%d", ( int ) iDataToSend );

			do
			{
				xBytesSent = xMessageBufferSend( xMessageBuffer, ( void * ) cTxString, strlen( cTxString ), xTicksToWait );
			} while ( xBytesSent == 0 ); /* Buffer may become full when receiver is running at the idle priority. */

			iDataToSend++;

			if( ( iDataToSend % iSendsBetweenIncrements ) == 0 )
			{
				/* Increment a loop counter so a check task can tell this task is
				still running as expected. */
				ulSenderLoopCounters[ uxIndex ]++;

				if( uxTaskPriorityGet( NULL ) == mbHIGHER_PRIORITY )
				{
					/* Allow other tasks to run. */
					vTaskDelay( xShortDelay );
				}

				/* This message buffer is just created and deleted to ensure no
				issues when attempting to delete a message buffer that was
				created using statically allocated memory.  To save stack space
				the buffer is set to point to the cTxString array - this is
				ok because nothing is actually written to the memory. */
				xTempMessageBuffer = xMessageBufferCreateStatic( sizeof( cTxString ), ( uint8_t * ) cTxString, &xStaticMessageBuffer );
				vMessageBufferDelete( xTempMessageBuffer );
			}
		}
	}

#endif /* configSUPPORT_STATIC_ALLOCATION */
/*-----------------------------------------------------------*/

#if( configSUPPORT_STATIC_ALLOCATION == 1  )

	static void prvReceiverTask( void *pvParameters )
	{
	MessageBufferHandle_t * const pxMessageBuffer = ( MessageBufferHandle_t * ) pvParameters;
	char cExpectedString[ 12 ]; /* Large enough to hold a 32-bit number in ASCII. */
	char cReceivedString[ 12 ]; /* Large enough to hold a 32-bit number in ASCII. */
	int32_t iExpectedData = 0;
	const TickType_t xTicksToWait = pdMS_TO_TICKS( 5UL );
	size_t xReceivedBytes;

		for( ;; )
		{
			/* Generate the next expected string in the cExpectedString buffer. */
			memset( cExpectedString, 0x00, sizeof( cExpectedString ) );
			sprintf( cExpectedString, "%d", ( int ) iExpectedData );

			/* Receive the next string from the message buffer. */
			memset( cReceivedString, 0x00, sizeof( cReceivedString ) );

			do
			{
				xReceivedBytes = xMessageBufferReceive( pxMessageBuffer, ( void * ) cReceivedString, sizeof( cExpectedString ), xTicksToWait );

			} while( xReceivedBytes == 0 );

			/* Ensure the received string matches the expected string. */
			configASSERT( strcmp( cExpectedString, cReceivedString ) == 0 );

			iExpectedData++;
		}
	}

#endif /* configSUPPORT_STATIC_ALLOCATION */
/*-----------------------------------------------------------*/

static void prvEchoClient( void *pvParameters )
{
size_t xSendLength = 0, ux;
char *pcStringToSend, *pcStringReceived, cNextChar = mbASCII_SPACE;
const TickType_t xTicksToWait = pdMS_TO_TICKS( 50 );

/* The task's priority is used as an index into the loop counters used to
indicate this task is still running. */
UBaseType_t uxIndex = uxTaskPriorityGet( NULL );

/* Pointers to the client and server message buffers are passed into this task
using the task's parameter. */
EchoMessageBuffers_t *pxMessageBuffers = ( EchoMessageBuffers_t * ) pvParameters;

	/* Prevent compiler warnings. */
	( void ) pvParameters;

	/* Create the buffer into which strings to send to the server will be
	created, and the buffer into which strings echoed back from the server will
	be copied. */
	pcStringToSend = ( char * ) pvPortMalloc( mbMESSAGE_BUFFER_LENGTH_BYTES );
	pcStringReceived = ( char * ) pvPortMalloc( mbMESSAGE_BUFFER_LENGTH_BYTES );

	configASSERT( pcStringToSend );
	configASSERT( pcStringReceived );

	for( ;; )
	{
		/* Generate the length of the next string to send. */
		xSendLength++;

		/* The message buffer is being used to hold variable length data, so
		each data item requires sizeof( size_t ) bytes to hold the data's
		length, hence the sizeof() in the if() condition below. */
		if( xSendLength > ( mbMESSAGE_BUFFER_LENGTH_BYTES - sizeof( size_t ) ) )
		{
			/* Back to a string length of 1. */
			xSendLength = sizeof( char );

			/* Maintain a count of the number of times this code executes so a
			check task can determine if this task is still functioning as
			expected or not.  As there are two client tasks, and the priorities
			used are 0 and 1, the task's priority is used as an index into the
			loop count array. */
			ulEchoLoopCounters[ uxIndex ]++;
		}

		memset( pcStringToSend, 0x00, mbMESSAGE_BUFFER_LENGTH_BYTES );

		for( ux = 0; ux < xSendLength; ux++ )
		{
			pcStringToSend[ ux ] = cNextChar;

			cNextChar++;

			if( cNextChar > mbASCII_TILDA )
			{
				cNextChar = mbASCII_SPACE;
			}
		}

		/* Send the generated string to the buffer. */
		do
		{
			ux = xMessageBufferSend( pxMessageBuffers->xEchoClientBuffer, ( void * ) pcStringToSend, xSendLength, xTicksToWait );

			if( ux == 0 )
			{
				mtCOVERAGE_TEST_MARKER();
			}

		} while( ux == 0 );

		/* Wait for the string to be echoed back. */
		memset( pcStringReceived, 0x00, mbMESSAGE_BUFFER_LENGTH_BYTES );
		xMessageBufferReceive( pxMessageBuffers->xEchoServerBuffer, ( void * ) pcStringReceived, xSendLength, portMAX_DELAY );

		configASSERT( strcmp( pcStringToSend, pcStringReceived ) == 0 );
	}
}
/*-----------------------------------------------------------*/

static void prvEchoServer( void *pvParameters )
{
MessageBufferHandle_t xTempMessageBuffer;
size_t xReceivedLength;
char *pcReceivedString;
EchoMessageBuffers_t xMessageBuffers;
TickType_t xTimeOnEntering;
const TickType_t xTicksToBlock = pdMS_TO_TICKS( 250UL );

	/* Prevent compiler warnings about unused parameters. */
	( void ) pvParameters;

	/* Create the message buffer used to send data from the client to the server,
	and the message buffer used to echo the data from the server back to the
	client. */
	xMessageBuffers.xEchoClientBuffer = xMessageBufferCreate( mbMESSAGE_BUFFER_LENGTH_BYTES );
	xMessageBuffers.xEchoServerBuffer = xMessageBufferCreate( mbMESSAGE_BUFFER_LENGTH_BYTES );
	configASSERT( xMessageBuffers.xEchoClientBuffer );
	configASSERT( xMessageBuffers.xEchoServerBuffer );

	/* Create the buffer into which received strings will be copied. */
	pcReceivedString = ( char * ) pvPortMalloc( mbMESSAGE_BUFFER_LENGTH_BYTES );
	configASSERT( pcReceivedString );

	/* Don't expect to receive anything yet! */
	xTimeOnEntering = xTaskGetTickCount();
	xReceivedLength = xMessageBufferReceive( xMessageBuffers.xEchoClientBuffer, ( void * ) pcReceivedString, mbMESSAGE_BUFFER_LENGTH_BYTES, xTicksToBlock );
	configASSERT( ( ( TickType_t ) ( xTaskGetTickCount() - xTimeOnEntering ) ) >= xTicksToBlock );
	configASSERT( xReceivedLength == 0 );
	( void ) xTimeOnEntering; /* In case configASSERT() is not defined. */

	/* Now the message buffers have been created the echo client task can be
	created.  If this server task has the higher priority then the client task
	is created at the lower priority - if this server task has the lower
	priority then the client task is created at the higher priority. */
	if( uxTaskPriorityGet( NULL ) == mbLOWER_PRIORITY )
	{
		xTaskCreate( prvEchoClient, "EchoClient", configMINIMAL_STACK_SIZE,  ( void * ) &xMessageBuffers, mbHIGHER_PRIORITY, NULL );
	}
	else
	{
		/* Here prvSingleTaskTests() performs various tests on a message buffer
		that was created dynamically. */
		prvSingleTaskTests( xMessageBuffers.xEchoClientBuffer );
		xTaskCreate( prvEchoClient, "EchoClient", configMINIMAL_STACK_SIZE, ( void * ) &xMessageBuffers, mbLOWER_PRIORITY, NULL );
	}

	for( ;; )
	{
		memset( pcReceivedString, 0x00, mbMESSAGE_BUFFER_LENGTH_BYTES );

		/* Has any data been sent by the client? */
		xReceivedLength = xMessageBufferReceive( xMessageBuffers.xEchoClientBuffer, ( void * ) pcReceivedString, mbMESSAGE_BUFFER_LENGTH_BYTES, portMAX_DELAY );

		/* Should always receive data as max delay was used. */
		configASSERT( xReceivedLength > 0 );

		/* Echo the received data back to the client. */
		xMessageBufferSend( xMessageBuffers.xEchoServerBuffer, ( void * ) pcReceivedString, xReceivedLength, portMAX_DELAY );

		/* This message buffer is just created and deleted to ensure no memory
		leaks. */
		xTempMessageBuffer = xMessageBufferCreate( mbMESSAGE_BUFFER_LENGTH_BYTES );
		vMessageBufferDelete( xTempMessageBuffer );
	}
}
/*-----------------------------------------------------------*/

BaseType_t xAreMessageBufferTasksStillRunning( void )
{
static uint32_t ulLastEchoLoopCounters[ mbNUMBER_OF_ECHO_CLIENTS ] = { 0 };
static uint32_t ulLastNonBlockingRxCounter = 0;
BaseType_t xReturn = pdPASS, x;

	for( x = 0; x < mbNUMBER_OF_ECHO_CLIENTS; x++ )
	{
		if( ulLastEchoLoopCounters[ x ] == ulEchoLoopCounters[ x ] )
		{
			xReturn = pdFAIL;
		}
		else
		{
			ulLastEchoLoopCounters[ x ] = ulEchoLoopCounters[ x ];
		}
	}

	if( ulNonBlockingRxCounter == ulLastNonBlockingRxCounter )
	{
		xReturn = pdFAIL;
	}
	else
	{
		ulLastNonBlockingRxCounter = ulNonBlockingRxCounter;
	}

	#if( configSUPPORT_STATIC_ALLOCATION == 1 )
	{
		static uint32_t ulLastSenderLoopCounters[ mbNUMBER_OF_ECHO_CLIENTS ] = { 0 };

		for( x = 0; x < mbNUMBER_OF_SENDER_TASKS; x++ )
		{
			if( ulLastSenderLoopCounters[ x ] == ulSenderLoopCounters[ x ] )
			{
				xReturn = pdFAIL;
			}
			else
			{
				ulLastSenderLoopCounters[ x ] = ulSenderLoopCounters[ x ];
			}
		}
	}
	#endif /* configSUPPORT_STATIC_ALLOCATION */

	return xReturn;
}
/*-----------------------------------------------------------*/