forked from u-blox/ubxlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu_port_test.c
2624 lines (2302 loc) · 104 KB
/
u_port_test.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
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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2020 u-blox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* Only #includes of u_* and the C standard library are allowed here,
* no platform stuff and no OS stuff. Anything required from
* the platform/OS must be brought in through u_port* to maintain
* portability.
*/
/** @file
* @brief Test for the port API: these should pass on all platforms.
* IMPORTANT: see notes in u_cfg_test_platform_specific.h for the
* naming rules that must be followed when using the U_PORT_TEST_FUNCTION()
* macro.
*/
#ifdef U_CFG_OVERRIDE
# include "u_cfg_override.h" // For a customer's configuration override
#endif
#include "stddef.h" // NULL, size_t etc.
#include "stdint.h" // int32_t etc.
#include "stdbool.h"
#include "stdlib.h" // rand()
#include "string.h" // strlen() and strcmp()
#include "stdio.h" // snprintf()
#include "time.h" // time_t and struct tm
#include "u_cfg_sw.h"
#include "u_cfg_os_platform_specific.h" // For #define U_CFG_OS_CLIB_LEAKS
#include "u_cfg_app_platform_specific.h"
#include "u_cfg_test_platform_specific.h"
#include "u_port_clib_platform_specific.h" /* strtok_r() and mktime() and
integer stdio, must be included
before the other port files if
any print or scan function
is used. */
#include "u_port_clib_mktime64.h"
#include "u_port.h"
#include "u_port_debug.h"
#include "u_port_os.h"
#include "u_port_gpio.h"
//lint -esym(766, u_port_uart.h) Suppress not referenced, which will be the case if U_PORT_TEST_CHECK_TIME_TAKEN is defined
#include "u_port_uart.h"
#include "u_port_crypto.h"
#include "u_port_event_queue.h"
#include "u_error_common.h"
#ifdef CONFIG_IRQ_OFFLOAD
#include <irq_offload.h> // To test semaphore from ISR in zephyr
#endif
/* ----------------------------------------------------------------
* COMPILE-TIME MACROS
* -------------------------------------------------------------- */
#ifndef _WIN32
/** Check time delays on all platforms except _WIN32: on _WIN32
* the tests are run on the same machine as all of the compilation
* processes etc. and hence any attempt to check real-timeness
* is futile.
*/
# define U_PORT_TEST_CHECK_TIME_TAKEN
#endif
#ifdef _WIN32
/** On _WIN32 it is possible to delete a task from another task,
* so we can check that.
*/
#define U_PORT_TEST_DELETE_OTHER_TASK
#endif
/** The queue length to create during testing.
*/
#define U_PORT_TEST_QUEUE_LENGTH 20
/** The size of each item on the queue during testing.
*/
#define U_PORT_TEST_QUEUE_ITEM_SIZE sizeof(int32_t)
/** The task block duration to use in testing the
* time for which a block lasts. This needs to
* be quite long as any error must be visible
* in the test duration as measured by the
* test system which is logging the test output.
*/
#define U_PORT_TEST_OS_BLOCK_TIME_MS 5000
#ifdef U_PORT_TEST_CHECK_TIME_TAKEN
/** The guard time for the OS test.
*/
# define U_PORT_TEST_OS_GUARD_DURATION_MS 7000
/** Tolerance on block time. Note that this needs
* to be large enough to account for the tick coarseness
* on all platforms. For instance, on ESP32 the default
* tick is 10 ms.
*/
# define U_PORT_TEST_OS_BLOCK_TIME_TOLERANCE_MS 150
#endif // #ifdef U_PORT_TEST_CHECK_TIME_TAKEN
#if (U_CFG_TEST_UART_A >= 0) && (U_CFG_TEST_UART_B < 0)
# ifdef U_PORT_TEST_CHECK_TIME_TAKEN
/** The amount of time to wait for the UART-loopbacked
* data to arrive back normally.
*/
# define U_PORT_TEST_UART_TIME_TO_ARRIVE_MS 1000
# else
/** The amount of time to wait for the UART-loopbacked
* data to arrive back when allowing laziness (e.g. on
* a heavily loaded Windoze machine).
*/
# define U_PORT_TEST_UART_TIME_TO_ARRIVE_MS 5000
# endif
#endif
/** The number of re-entrancy test tasks to run.
*/
#define U_PORT_TEST_OS_NUM_REENT_TASKS 3
/** Fill value for the heap.
*/
#define U_PORT_TEST_OS_MALLOC_FILL ((int32_t) 0xdeadbeef)
/** The amount of memory to malloc()ate during re-entrancy
* testing.
*/
#define U_PORT_TEST_OS_MALLOC_SIZE_INTS ((int32_t) (1024 / sizeof(int32_t)))
/** Number of interations for the event queue test.
* Must be less than 256.
*/
#define U_PORT_TEST_OS_EVENT_QUEUE_ITERATIONS 100
/** The minimum item size for the event queue test: we used
* to fix this at 1 however there are some OS's which, internally,
* allocate space in words, hence it is 4 for greater compatibility.
*/
#define U_PORT_TEST_OS_EVENT_QUEUE_PARAM_MIN_SIZE_BYTES 4
/** How long to wait to receive a message on a queue in osTestTask.
*/
#define U_PORT_OS_TEST_TASK_TRY_RECEIVE_MS 10
#ifndef U_PORT_TEST_CRITICAL_SECTION_TEST_TASK_START_TIME_SECONDS
/** How long to wait for the critical section test task to start,
* leaving plenty of time for Windows.
*/
# define U_PORT_TEST_CRITICAL_SECTION_TEST_TASK_START_TIME_SECONDS 10
#endif
#ifndef U_PORT_TEST_CRITICAL_SECTION_TEST_WAIT_TIME_MS
/** How long to wait to check that the critical section is no longer
* in effect: needs to be large to allow for Windows slop and small
* enough not to cause any platform-specific watchdog to fire on an
* embedded target.
*/
# ifdef _WIN32
# define U_PORT_TEST_CRITICAL_SECTION_TEST_WAIT_TIME_MS 5000
# else
# define U_PORT_TEST_CRITICAL_SECTION_TEST_WAIT_TIME_MS 20
# endif
#endif
#ifndef U_PORT_TEST_CRITICAL_SECTION_TEST_WAIT_TIME_MS
/** How long to wait to check the critical section: needs to be
* large to allow for Windows slop and small enough not to cause
* any platform-specific watchdog to fire on an embedded target.
*/
# ifdef _WIN32
# define U_PORT_TEST_CRITICAL_SECTION_TEST_WAIT_TIME_MS 5000
# else
# define U_PORT_TEST_CRITICAL_SECTION_TEST_WAIT_TIME_MS U_CFG_OS_YIELD_MS
# endif
#endif
#ifndef U_PORT_TEST_CRITICAL_SECTION_TEST_WAIT_LOOPS
/** If time does not pass during a critical section (e.g. on
* our STM32F4 port it does not) then we can't use
* U_PORT_TEST_CRITICAL_SECTION_TEST_WAIT_TIME_MS so in those
* cases we just have to busy-wait for this number of loops.
* Question is, what should the value be? It is obviously a
* compromise between platforms/CPU-clock-rates, needs to be big
* enough for at least one RTOS tick to have passed and not so
* large as to trip-up any interrupt watchdog (ESP-IDF has one
* of those).
*/
//lint -esym(750, U_PORT_TEST_CRITICAL_SECTION_TEST_WAIT_LOOPS) Suppress
// not referenced, which might be the case if we're on Windows
# define U_PORT_TEST_CRITICAL_SECTION_TEST_WAIT_LOOPS 1000000
#endif
/* ----------------------------------------------------------------
* TYPES
* -------------------------------------------------------------- */
#if (U_CFG_TEST_UART_A >= 0) && (U_CFG_TEST_UART_B < 0)
/** Type to hold the stuff that the UART test task needs to know
* about.
*/
typedef struct {
size_t callCount;
int32_t blockNumber;
size_t indexInBlock;
char *pReceive;
size_t bytesReceived;
int32_t errorCode;
} uartEventCallbackData_t;
#endif
/** Struct for mktime64() testing.
*/
typedef struct {
struct tm timeStruct;
int64_t time;
} mktime64TestData_t;
/* ----------------------------------------------------------------
* VARIABLES
* -------------------------------------------------------------- */
// OS test mutex handle.
static uPortMutexHandle_t gMutexHandle = NULL;
// OS test semaphore handle.
static uPortSemaphoreHandle_t gSemaphoreHandle = NULL;
// OS test queue handle for data.
static uPortQueueHandle_t gQueueHandleData = NULL;
// OS test queue handle for control.
static uPortQueueHandle_t gQueueHandleControl = NULL;
// OS test task handle.
static uPortTaskHandle_t gTaskHandle = NULL;
// OS task parameter.
static char gTaskParameter[6];
// Stuff to send to the OS test task, must all be positive numbers.
static const int32_t gStuffToSend[] = {0, 100, 25, 3};
// Flag for re-entrancy testing, wait for start.
static bool gWaitForGo;
// Flag for re-entrancy testing, wait for delete.
static bool gWaitForStop;
// Handle for event queue callback max length
static int32_t gEventQueueMaxHandle;
// Error flag for event queue callback max length
static int32_t gEventQueueMaxErrorFlag;
// Counter for event queue callback max length
static int32_t gEventQueueMaxCounter;
// Handle for event queue callback min length
static int32_t gEventQueueMinHandle;
// Error flag for event queue callback min length
static int32_t gEventQueueMinErrorFlag;
// Counter for event queue callback min length
static int32_t gEventQueueMinCounter;
#if (U_CFG_TEST_UART_A >= 0) && (U_CFG_TEST_UART_B < 0)
// The data to send during UART testing.
static const char gUartTestData[] = "_____0000:0123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"_____0100:0123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"_____0200:0123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"_____0300:0123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"_____0400:0123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"_____0500:0123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"_____0600:0123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"_____0700:0123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"_____0800:0123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789"
"_____0900:0123456789012345678901234567890123456789"
"01234567890123456789012345678901234567890123456789";
// A buffer to receive UART data into:
// deliberately a non-integer divisor of
// U_CFG_TEST_UART_BUFFER_LENGTH_BYTES
// so that the buffers go "around the corner"
static char gUartBuffer[(U_CFG_TEST_UART_BUFFER_LENGTH_BYTES / 2) +
(U_CFG_TEST_UART_BUFFER_LENGTH_BYTES / 4)];
#endif // (U_CFG_TEST_UART_A >= 0) && (U_CFG_TEST_UART_B < 0)
/** Data for mktime64() testing.
*/
static mktime64TestData_t gMktime64TestData[] = {
{{0, 0, 0, 1, 0, 70, 0, 0, 0}, 0},
{{1, 0, 0, 1, 0, 70, 0, 0, 0}, 1},
{{1, 1, 0, 1, 0, 70, 0, 0, 0}, 61},
{{1, 1, 1, 1, 0, 70, 0, 0, 0}, 3661},
{{1, 1, 1, 1, 1, 70, 0, 0, 0}, 2682061},
{{1, 1, 1, 1, 1, 70, 0, 0, 0}, 2682061},
{{1, 1, 1, 1, 1, 70, 1, 0, 0}, 2682061},
{{1, 1, 1, 1, 1, 70, 1, 1, 0}, 2682061},
{{1, 1, 1, 1, 1, 70, 1, 1, 1}, 2682061},
{{61, 0, 0, 1, 0, 70, 0, 0, 0}, 61},
{{0, 59, 0, 1, 0, 70, 0, 0, 0}, 3540},
{{0, 0, 23, 1, 0, 70, 0, 0, 0}, 82800},
{{0, 0, 0, 31, 0, 70, 0, 0, 0}, 2592000},
{{0, 0, 0, 1, 12, 70, 0, 0, 0}, 31536000},
{{0, 0, 0, 1, 0, 137, 0, 0, 0}, 2114380800LL},
{{0, 0, 0, 1, 0, 150, 0, 0, 0}, 2524608000LL}
};
/** SHA256 test vector, input, RC4.55 from:
* https://www.dlitz.net/crypto/shad256-test-vectors/
*/
static char const gSha256Input[] =
"\xde\x18\x89\x41\xa3\x37\x5d\x3a\x8a\x06\x1e\x67\x57\x6e\x92\x6d"
"\xc7\x1a\x7f\xa3\xf0\xcc\xeb\x97\x45\x2b\x4d\x32\x27\x96\x5f\x9e"
"\xa8\xcc\x75\x07\x6d\x9f\xb9\xc5\x41\x7a\xa5\xcb\x30\xfc\x22\x19"
"\x8b\x34\x98\x2d\xbb\x62\x9e";
/** SHA256 test vector, output, RC4.55 from:
* https://www.dlitz.net/crypto/shad256-test-vectors/
*/
static const char gSha256Output[] =
"\x03\x80\x51\xe9\xc3\x24\x39\x3b\xd1\xca\x19\x78\xdd\x09\x52\xc2"
"\xaa\x37\x42\xca\x4f\x1b\xd5\xcd\x46\x11\xce\xa8\x38\x92\xd3\x82";
/** HMAC SHA256 test vector, key, test 1 from:
* https://tools.ietf.org/html/rfc4231#page-3
*/
static const char gHmacSha256Key[] =
"\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
"\x0b\x0b\x0b\x0b";
/** HMAC SHA256 test vector, input data, test 1 from:
* https://tools.ietf.org/html/rfc4231#page-3
*/
static const char gHmacSha256Input[] = "\x48\x69\x20\x54\x68\x65\x72\x65";
/** HMAC SHA256 test vector, output data, test 1 from:
* https://tools.ietf.org/html/rfc4231#page-3
*/
static const char gHmacSha256Output[] =
"\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1\x2b"
"\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32\xcf\xf7";
/** AES CBC 128 test vector, key, test 1 from:
* https://tools.ietf.org/html/rfc3602#page-6
*/
static const char gAes128CbcKey[] =
"\x06\xa9\x21\x40\x36\xb8\xa1\x5b\x51\x2e\x03\xd5\x34\x12\x00\x06";
/** AES CBC 128 test vector, initial vector, test 1 from:
* https://tools.ietf.org/html/rfc3602#page-6
*/
static const char gAes128CbcIV[] =
"\x3d\xaf\xba\x42\x9d\x9e\xb4\x30\xb4\x22\xda\x80\x2c\x9f\xac\x41";
/** AES CBC 128 test vector, clear text, test 1 from:
* https://tools.ietf.org/html/rfc3602#page-6
*/
static const char gAes128CbcClear[] = "Single block msg";
/** AES CBC 128 test vector, encrypted text, test 1 from:
* https://tools.ietf.org/html/rfc3602#page-6
*/
static const char gAes128CbcEncrypted[] =
"\xe3\x53\x77\x9c\x10\x79\xae\xb8\x27\x08\x94\x2d\xbe\x77\x18\x1a";
/** For tracking heap lost to memory lost by the C library.
*/
static size_t gSystemHeapLost = 0;
/** Timer parameter value array; must have the same number of
* entries as gTimerHandle.
*/
static int32_t gTimerParameterValue[4] = {0};
/** Index into the gTimerParameterValue array.
*/
static size_t gTimerParameterIndex = 0;
/** Timer handle array; must have the same number of
* entries as gTimerParameterValue.
*/
static uPortTimerHandle_t gTimerHandle[4] = {0};
/** A variable to use during critical section testing.
*/
static uint32_t gVariable = 0;
/* ----------------------------------------------------------------
* STATIC FUNCTIONS
* -------------------------------------------------------------- */
// The test task for re-entrancy.
// The parameter is a pointer to an integer which, on arrival,
// is a unique non-zero index that the task can identify itself
// by and, on return, should be set to zero for success, negative
// for error.
static void osReentTask(void *pParameter)
{
int32_t index = *((int32_t *) pParameter) & 0xFF;
int32_t *pMem;
int32_t *pTmp;
int32_t returnCode = 0;
char *pStr;
char *pSaved;
char str[6];
char buffer[32];
int32_t checkInt = ((U_PORT_TEST_OS_MALLOC_FILL) & ~0xFF) | index;
int32_t mallocSizeInts = 1 + (rand() % (U_PORT_TEST_OS_MALLOC_SIZE_INTS - 1));
// Wait for it...
while (gWaitForGo) {
uPortTaskBlock(U_CFG_OS_YIELD_MS);
}
// Malloc a random amount of memory and fill it with
// a known value unique to this task, yielding while
// doing it so that others can get in and mess it up
pMem = (int32_t *) malloc(mallocSizeInts * sizeof(int32_t));
uPortTaskBlock(U_CFG_OS_YIELD_MS);
if (pMem != NULL) {
pTmp = pMem;
for (int32_t x = 0; x < mallocSizeInts; x++) {
*pTmp = checkInt;
pTmp++;
uPortTaskBlock(U_CFG_OS_YIELD_MS);
}
// Copy the string into RAM so that strtok can
// fiddle with it
strncpy(str, "a,b,c", sizeof(str));
// Do a strtok_r()
strtok_r(str, ",", &pSaved);
uPortTaskBlock(U_CFG_OS_YIELD_MS);
strtok_r(NULL, ",", &pSaved);
uPortTaskBlock(U_CFG_OS_YIELD_MS);
pStr = strtok_r(NULL, ",", &pSaved);
uPortTaskBlock(U_CFG_OS_YIELD_MS);
// Do an snprintf() with parameters, which should
// allocate memory.
snprintf(buffer, sizeof(buffer), "%u %d %s", 4294967295U, (int) index, pStr);
uPortTaskBlock(U_CFG_OS_YIELD_MS);
// Do a logging call with parameters, which will ultimately
// call printf(). Note that this may not necessarily produce
// "nice" output when logging from multiple tasks but it should
// not corrupt memory
uPortLog("U_PORT_TEST_OS_REENT_TASK_%d: %d \"%s\".\n", index, index, buffer);
uPortTaskBlock(U_CFG_OS_YIELD_MS);
// Check what ended up in the buffer we wrote earlier
pStr = strtok_r(buffer, " ", &pSaved);
if (pStr != NULL) {
// First should be "4294967295"
if (strcmp(pStr, "4294967295") == 0) {
// Next should be the index
pStr = strtok_r(NULL, " ", &pSaved);
if (pStr != NULL) {
if (strtol(pStr, NULL, 10) == index) {
// And finally, the single character 'c'
pStr = strtok_r(NULL, " ", &pSaved);
if (pStr != NULL) {
if (strlen(pStr) == 1) {
if (*pStr != 'c') {
returnCode = -8;
}
} else {
returnCode = -7;
}
} else {
returnCode = -6;
}
} else {
returnCode = -5;
}
} else {
returnCode = -4;
}
} else {
returnCode = -3;
}
} else {
returnCode = -2;
}
// Check that the memory we malloc()ed still contains
// what we put there
pTmp = pMem;
for (int32_t x = 0; (returnCode == 0) &&
(x < mallocSizeInts); x++) {
if (*pTmp != checkInt) {
returnCode = -9;
}
pTmp++;
}
// Free the memory again
free(pMem);
// Run around doing more malloc/compare/free with random
// amounts of memory and yielding just to mix things up
uPortLog("U_PORT_TEST_OS_REENT_TASK_%d: please wait while malloc()"
" is thrashed...\n", index);
for (size_t x = 0; (returnCode == 0) && (x < 100); x++) {
mallocSizeInts = 1 + (rand() % (U_PORT_TEST_OS_MALLOC_SIZE_INTS - 1));
pMem = (int32_t *) malloc(mallocSizeInts * sizeof(int32_t));
uPortTaskBlock(U_CFG_OS_YIELD_MS);
if (pMem != NULL) {
pTmp = pMem;
for (int32_t y = 0; y < mallocSizeInts; y++) {
*pTmp = checkInt;
pTmp++;
}
uPortTaskBlock(U_CFG_OS_YIELD_MS);
pTmp = pMem;
for (int32_t y = 0; (returnCode == 0) &&
(y < mallocSizeInts); y++) {
if (*pTmp != checkInt) {
returnCode = -10;
}
pTmp++;
}
} else {
returnCode = -11;
}
free(pMem);
}
} else {
returnCode = -1;
}
uPortLog("U_PORT_TEST_OS_REENT_TASK: instance %d done, returning %d.\n",
index, returnCode);
// Finally, set the parameter to the return code to indicate done
*((int32_t *) pParameter) = returnCode;
// Wait for it...
while (gWaitForStop) {
uPortTaskBlock(U_CFG_OS_YIELD_MS);
}
#ifndef U_PORT_TEST_DELETE_OTHER_TASK
// And delete ourselves
uPortTaskDelete(NULL);
#endif
}
// The test task for OS stuff.
//lint -esym(818, pParameters) Suppress "could be const"
// since this has to match the function signature
// exactly to avoid a compiler warning
static void osTestTask(void *pParameters)
{
int32_t queueItem = 0;
int32_t index = 0;
int32_t x = 0;
int32_t y;
uPortTaskHandle_t taskHandle = NULL;
#if U_CFG_OS_CLIB_LEAKS
int32_t heapClibLoss;
// Calling C library functions from a new task
// allocates additional memory which, depending
// on the OS/system, may not be recovered;
// take account of that here.
heapClibLoss = uPortGetHeapFree();
#endif
// Fill in the parameter
strncpy(gTaskParameter, "Boo!", sizeof(gTaskParameter));
// Pause here to let the task that spawned this one
// run otherwise gTaskHandle won't have been populated.
uPortTaskBlock(U_CFG_OS_YIELD_MS);
uPortLog("U_PORT_TEST_OS_TASK: task with handle 0x%08x started,"
" received parameter pointer 0x%08x containing string"
" \"%s\".\n", (int) gTaskHandle, pParameters,
(const char *) pParameters);
U_PORT_TEST_ASSERT(strcmp((const char *) pParameters, gTaskParameter) == 0);
#if U_CFG_OS_CLIB_LEAKS
// Take account of any heap lost through the first printf()
gSystemHeapLost += (size_t) (unsigned) (heapClibLoss - uPortGetHeapFree());
#endif
U_PORT_TEST_ASSERT(uPortTaskIsThis(gTaskHandle));
U_PORT_TEST_ASSERT(uPortTaskGetHandle(NULL) < 0);
U_PORT_TEST_ASSERT(uPortTaskGetHandle(&taskHandle) == 0);
uPortLog("U_PORT_TEST: uPortTaskGetHandle() returned 0x%08x\n", taskHandle);
U_PORT_TEST_ASSERT(gTaskHandle == taskHandle);
uPortLog("U_PORT_TEST_OS_TASK: task trying to lock the mutex.\n");
U_PORT_TEST_ASSERT(gMutexHandle != NULL);
U_PORT_TEST_ASSERT(uPortMutexTryLock(gMutexHandle, 500) == 0);
uPortLog("U_PORT_TEST_OS_TASK: task trying to lock the mutex again, should fail!.\n");
U_PORT_TEST_ASSERT(uPortMutexTryLock(gMutexHandle, 10) != 0);
uPortLog("U_PORT_TEST_OS_TASK: unlocking it again.\n");
U_PORT_TEST_ASSERT(uPortMutexUnlock(gMutexHandle) == 0);
uPortLog("U_PORT_TEST_OS_TASK: locking it again (non-try version).\n");
U_PORT_MUTEX_LOCK(gMutexHandle);
U_PORT_TEST_ASSERT(gQueueHandleControl != NULL);
U_PORT_TEST_ASSERT(gQueueHandleData != NULL);
uPortLog("U_PORT_TEST_OS_TASK: task waiting on queue for data.\n");
while (queueItem >= 0) {
U_PORT_TEST_ASSERT(uPortQueueReceive(gQueueHandleData,
&queueItem) == 0);
uPortLog("U_PORT_TEST_OS_TASK: task received %d.\n", queueItem);
if ((queueItem >= 0) &&
(index < (int32_t) (sizeof(gStuffToSend) / sizeof(gStuffToSend[0])))) {
uPortLog(" item %d, expecting %d.\n",
index + 1, gStuffToSend[index]);
U_PORT_TEST_ASSERT(gStuffToSend[index] == queueItem);
index++;
}
x = 0;
y = uPortQueuePeek(gQueueHandleControl, &x);
U_PORT_TEST_ASSERT((y == 0) ||
(y == (int32_t) U_ERROR_COMMON_NOT_IMPLEMENTED) ||
(y == (int32_t) U_ERROR_COMMON_TIMEOUT));
if (uPortQueueTryReceive(gQueueHandleControl,
U_PORT_OS_TEST_TASK_TRY_RECEIVE_MS,
&queueItem) == 0) {
uPortLog("U_PORT_TEST_OS_TASK: task received %d on control"
" queue.\n", queueItem);
U_PORT_TEST_ASSERT(queueItem == -1);
U_PORT_TEST_ASSERT((y < 0) || (x == queueItem));
}
uPortLog("U_PORT_TEST_OS_TASK: queueItem %d.\n", queueItem);
}
uPortLog("U_PORT_TEST_OS_TASK: task exiting, unlocking mutex.\n");
U_PORT_MUTEX_UNLOCK(gMutexHandle);
uPortLog("U_PORT_TEST_OS_TASK: task deleting itself.\n");
U_PORT_TEST_ASSERT(uPortTaskDelete(NULL) == 0);
}
// Function to send stuff to a queue.
static int32_t sendToQueue(uPortQueueHandle_t queueHandle,
int32_t thing)
{
return uPortQueueSend(queueHandle, &thing);
}
// Function to send stuff to a queue using the IRQ version.
static int32_t sendToQueueIrq(uPortQueueHandle_t queueHandle,
int32_t thing)
{
return uPortQueueSendIrq(queueHandle, &thing);
}
// An event queue function for max length parameter.
//lint -esym(818, pParam) Suppress "could be const"
// since this has to match the function signature
// exactly to avoid a compiler warning
static void eventQueueMaxFunction(void *pParam,
size_t paramLength)
{
uint8_t fill = 0xFF;
if (gEventQueueMaxCounter <
U_PORT_TEST_OS_EVENT_QUEUE_ITERATIONS) {
// For U_PORT_TEST_OS_EVENT_QUEUE_ITERATIONS
// we expect to receive paramLength of
// U_PORT_EVENT_QUEUE_MAX_PARAM_LENGTH_BYTES
// containing the pattern 0xFF to 0 repeated
// but with the last byte containing a counter
// which increments from zero.
if (paramLength != U_PORT_EVENT_QUEUE_MAX_PARAM_LENGTH_BYTES) {
gEventQueueMaxErrorFlag = 1;
}
for (size_t x = 0; (gEventQueueMaxErrorFlag == 0) &&
(x < paramLength - 1); x++) {
if (*((uint8_t *) pParam + x) != fill) {
gEventQueueMaxErrorFlag = 2;
}
fill--;
}
if (gEventQueueMaxErrorFlag == 0) {
if (*((uint8_t *) pParam + paramLength - 1) !=
(uint8_t) gEventQueueMaxCounter) {
gEventQueueMaxErrorFlag = 3;
}
}
} else {
if (gEventQueueMaxCounter ==
U_PORT_TEST_OS_EVENT_QUEUE_ITERATIONS) {
// For one final bonus iteration we expect
// pParam to be NULL and paramLength 0
if (pParam != NULL) {
gEventQueueMaxErrorFlag = 4;
}
if (paramLength != 0) {
gEventQueueMaxErrorFlag = 5;
}
} else {
// Anything else shouldn't happen
gEventQueueMaxErrorFlag = 6;
}
}
if (gEventQueueMaxErrorFlag == 0) {
if (!uPortEventQueueIsTask(gEventQueueMaxHandle)) {
// Not detecting that this is an event task
gEventQueueMaxErrorFlag = 7;
} else {
if (uPortEventQueueIsTask(gEventQueueMinHandle)) {
// Detecting that this is the wrong event task
gEventQueueMaxErrorFlag = 8;
}
}
}
gEventQueueMaxCounter++;
}
// Event queue function for minimum length parameter.
//lint -esym(818, pParam) Suppress "could be const"
// since this has to match the function signature
// exactly to avoid a compiler warning
static void eventQueueMinFunction(void *pParam,
size_t paramLength)
{
if (gEventQueueMinCounter <
U_PORT_TEST_OS_EVENT_QUEUE_ITERATIONS) {
// For U_PORT_TEST_OS_EVENT_QUEUE_ITERATIONS
// we expect to receive paramLength of
// U_PORT_TEST_OS_EVENT_QUEUE_PARAM_MIN_SIZE_BYTES where
// *pParam is a count of the number of times we've
// been called.
if (paramLength != U_PORT_TEST_OS_EVENT_QUEUE_PARAM_MIN_SIZE_BYTES) {
gEventQueueMinErrorFlag = U_PORT_TEST_OS_EVENT_QUEUE_PARAM_MIN_SIZE_BYTES;
}
if (gEventQueueMinErrorFlag == 0) {
if (*((uint8_t *) pParam) != (uint8_t) gEventQueueMinCounter) {
gEventQueueMinErrorFlag = 2;
}
}
} else {
if (gEventQueueMinCounter ==
U_PORT_TEST_OS_EVENT_QUEUE_ITERATIONS) {
// For one final bonus iteration we expect
// pParam to be NULL and paramLength 0
if (pParam != NULL) {
gEventQueueMinErrorFlag = 4;
}
if (paramLength != 0) {
gEventQueueMinErrorFlag = 5;
}
} else {
// Anything else shouldn't happen
gEventQueueMinErrorFlag = 6;
}
}
if (gEventQueueMinErrorFlag == 0) {
if (!uPortEventQueueIsTask(gEventQueueMinHandle)) {
// Not detecting that this is an event task
gEventQueueMinErrorFlag = 7;
} else {
if (uPortEventQueueIsTask(gEventQueueMaxHandle)) {
// Detecting that this is the wrong event task
gEventQueueMinErrorFlag = 8;
}
}
}
gEventQueueMinCounter++;
}
#if (U_CFG_TEST_UART_A >= 0) && (U_CFG_TEST_UART_B < 0)
// Callback that is called when data arrives at the UART
static void uartReceivedDataCallback(int32_t uartHandle,
uint32_t filter,
void *pParameters)
{
int32_t receiveSizeOrError;
int32_t actualSizeOrError;
uartEventCallbackData_t *pEventCallbackData = (uartEventCallbackData_t *) pParameters;
pEventCallbackData->callCount++;
if (filter != U_PORT_UART_EVENT_BITMASK_DATA_RECEIVED) {
pEventCallbackData->errorCode = -1;
} else {
// Run until we spot an error or run out of data
do {
receiveSizeOrError = uPortUartGetReceiveSize(uartHandle);
// Since the initial part of the test is to send a manual
// "there some data" message, even though there isn't any
// in the buffer yet, we shouldn't go on to read the
// data unless we know there really is some; otherwise
// uPortUartRead() could get stuck, holding a mutex out
// down in the porting layer and preventing us from doing
// the uPortUartWrite() part of the test. This sending
// data and receiving it ourselves isn't a normal case,
// it only occurs during testing.
actualSizeOrError = 0;
if (receiveSizeOrError > 0) {
actualSizeOrError = uPortUartRead(uartHandle,
pEventCallbackData->pReceive,
gUartBuffer + sizeof(gUartBuffer) -
pEventCallbackData->pReceive);
if (actualSizeOrError < 0) {
pEventCallbackData->errorCode = -2;
}
// actualSizeOrError will be smaller than receiveSizeOrError
// if our data buffer is smaller than the UART receive
// buffer but something might also have been received
// between the two calls, making it larger. Just
// can't easily check uPortUartGetReceiveSize()
// for accuracy, so instead do a range check here
if (receiveSizeOrError < 0) {
pEventCallbackData->errorCode = -3;
}
if (receiveSizeOrError > U_CFG_TEST_UART_BUFFER_LENGTH_BYTES) {
pEventCallbackData->errorCode = -4;
}
if (actualSizeOrError > U_CFG_TEST_UART_BUFFER_LENGTH_BYTES) {
pEventCallbackData->errorCode = -5;
}
// Compare the data with the expected data
for (int32_t x = 0; (pEventCallbackData->errorCode == 0) &&
(x < actualSizeOrError); x++) {
if (gUartTestData[pEventCallbackData->indexInBlock] ==
*(pEventCallbackData->pReceive)) {
pEventCallbackData->bytesReceived++;
pEventCallbackData->indexInBlock++;
// -1 below to omit gUartTestData string terminator
if (pEventCallbackData->indexInBlock >= sizeof(gUartTestData) - 1) {
pEventCallbackData->indexInBlock = 0;
pEventCallbackData->blockNumber++;
}
pEventCallbackData->pReceive++;
if (pEventCallbackData->pReceive >= gUartBuffer + sizeof(gUartBuffer)) {
pEventCallbackData->pReceive = gUartBuffer;
}
} else {
pEventCallbackData->errorCode = -6;
}
}
}
} while ((actualSizeOrError > 0) && (pEventCallbackData->errorCode == 0));
}
}
// Run a UART test at the given baud rate and with/without flow control.
static void runUartTest(int32_t size, int32_t speed, bool flowControlOn)
{
int32_t uartHandle;
uartEventCallbackData_t eventCallbackData = {0};
int32_t bytesToSend;
int32_t bytesSent = 0;
int32_t pinCts;
int32_t pinRts;
uPortGpioConfig_t gpioConfig = U_PORT_GPIO_CONFIG_DEFAULT;
int32_t stackMinFreeBytes;
int32_t x;
eventCallbackData.callCount = 0;
eventCallbackData.pReceive = gUartBuffer;
// Grab here the pins that would be passed to
// uPortUartOpen(), not the _GET versions. On
// a platform where the pins are set at compile
// time these values will be -1, ignored.
pinCts = U_CFG_TEST_PIN_UART_A_CTS;
pinRts = U_CFG_TEST_PIN_UART_A_RTS;
// Print where the pins are actually connected, that's what
// the user needs to know. On a platform which can set
// the pins at run-time the values will be the same
// as the pinCts and pinRts values.
uPortLog("U_PORT_TEST: UART CTS is on pin %d and RTS on"
" pin %d", U_CFG_TEST_PIN_UART_A_CTS_GET,
U_CFG_TEST_PIN_UART_A_RTS_GET);
if (!flowControlOn) {
uPortLog(" but we're going to ignore them for this"
" test.\n");
// If we want to test with flow control off
// but the flow control pins are actually
// connected then they need to be set
// to "get on with it"
if (pinCts >= 0) {
// Make CTS an output pin and low
x = uPortGpioSet(pinCts, 0);
// On Windows GPIOs aren't supported but
// pinCts is still used as a flow control
// on/off indicator
U_PORT_TEST_ASSERT((x == 0) ||
(x == (int32_t) U_ERROR_COMMON_NOT_SUPPORTED));
gpioConfig.pin = pinCts;
gpioConfig.direction = U_PORT_GPIO_DIRECTION_OUTPUT;
x = uPortGpioConfig(&gpioConfig);
U_PORT_TEST_ASSERT((x == 0) ||
(x == (int32_t) U_ERROR_COMMON_NOT_SUPPORTED));
uPortTaskBlock(U_CFG_OS_YIELD_MS);
}
if (pinRts >= 0) {
// Make RTS an output pin and low
x = uPortGpioSet(pinRts, 0);
// On Windows GPIOs aren't supported but
// pinRts is still used as a flow control
// on/off indicator
U_PORT_TEST_ASSERT((x == 0) ||
(x == (int32_t) U_ERROR_COMMON_NOT_SUPPORTED));
gpioConfig.pin = pinRts;
gpioConfig.direction = U_PORT_GPIO_DIRECTION_OUTPUT;
x = uPortGpioConfig(&gpioConfig);
U_PORT_TEST_ASSERT((x == 0) ||
(x == (int32_t) U_ERROR_COMMON_NOT_SUPPORTED));
uPortTaskBlock(U_CFG_OS_YIELD_MS);
}
pinCts = -1;
pinRts = -1;
} else {
uPortLog(".\n");
}
uPortLog("U_PORT_TEST: testing UART loop-back, %d byte(s) at %d"
" bits/s with flow control %s.\n", size, speed,
flowControlOn ? "on" : "off");
uPortLog("U_PORT_TEST: add a UART instance...\n");
uartHandle = uPortUartOpen(U_CFG_TEST_UART_A,
speed, NULL,
U_CFG_TEST_UART_BUFFER_LENGTH_BYTES,
U_CFG_TEST_PIN_UART_A_TXD,
U_CFG_TEST_PIN_UART_A_RXD,
pinCts, pinRts);
U_PORT_TEST_ASSERT(uartHandle >= 0);
uPortLog("U_PORT_TEST: add a UART event callback which"
" will receive the data...\n");
U_PORT_TEST_ASSERT(uPortUartEventCallbackSet(uartHandle,
(uint32_t) U_PORT_UART_EVENT_BITMASK_DATA_RECEIVED,
uartReceivedDataCallback,
(void *) &eventCallbackData,
U_PORT_EVENT_QUEUE_MIN_TASK_STACK_SIZE_BYTES,
U_CFG_OS_APP_TASK_PRIORITY + 1) == 0);
// Check that the callback is there
U_PORT_TEST_ASSERT(uPortUartEventCallbackFilterGet(uartHandle) ==
U_PORT_UART_EVENT_BITMASK_DATA_RECEIVED);
// Set the filter (there's only one so this isn't doing much,
// but what can you do)
U_PORT_TEST_ASSERT(uPortUartEventCallbackFilterSet(uartHandle,
(uint32_t) U_PORT_UART_EVENT_BITMASK_DATA_RECEIVED) ==
0);
// Can't easily check that the CTS suspend/resume functions work
// and, in any case, they may not be supported so simply call them
// both here, before the main body of the test, to check that they
// don't crash anything and that the test works afterwards
x = uPortUartCtsSuspend(uartHandle);
U_PORT_TEST_ASSERT((x == 0) ||
(x == (int32_t) U_ERROR_COMMON_NOT_SUPPORTED));
uPortUartCtsResume(uartHandle);
// Manually send an Rx event and check that it caused
// the callback to be called