-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestHarness.cpp
More file actions
1411 lines (1310 loc) · 44.3 KB
/
Copy pathTestHarness.cpp
File metadata and controls
1411 lines (1310 loc) · 44.3 KB
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 © 2019, 2020, 2021, 2022 HackEDA, Inc.
Licensed under the WiPhone Public License v.1.0 (the "License"); you
may not use this file except in compliance with the License. You may
obtain a copy of the License at
https://wiphone.io/WiPhone_Public_License_v1.0.txt.
*/
#include "TestHarness.h"
#ifdef WIPHONE_TEST_HARNESS
#include "GUI.h"
#include "Hardware.h"
#include "src/system_benchmark/SystemBenchmarkKernels.h"
#include "src/system_benchmark/SystemBenchmarkProfile.h"
#include "src/system_benchmark/SystemBenchmarkStore.h"
#include "esp_heap_caps.h"
#include "driver/uart.h"
#include <stdarg.h>
extern "C" unsigned D_DoomStage(void);
extern "C" void WiPhoneDoomAudioTelemetry(uint32_t* mixedBlocks,
uint32_t* submittedBlocks,
uint32_t* writeFailures,
uint32_t* activeChannels,
int* running);
extern "C" uint32_t WiPhoneDoomAudioReadPeak(uint32_t frames, uint32_t timeoutMs);
extern "C" void WiPhoneDoomMusicDiag(int *ready, int *songActive,
uint32_t *renderPeak);
namespace {
struct HarnessKey {
const char* name;
char code;
uint32_t mask;
};
const HarnessKey HARNESS_KEYS[] = {
{ "0", '0', WIPHONE_KEY_MASK_0 },
{ "1", '1', WIPHONE_KEY_MASK_1 },
{ "2", '2', WIPHONE_KEY_MASK_2 },
{ "3", '3', WIPHONE_KEY_MASK_3 },
{ "4", '4', WIPHONE_KEY_MASK_4 },
{ "5", '5', WIPHONE_KEY_MASK_5 },
{ "6", '6', WIPHONE_KEY_MASK_6 },
{ "7", '7', WIPHONE_KEY_MASK_7 },
{ "8", '8', WIPHONE_KEY_MASK_8 },
{ "9", '9', WIPHONE_KEY_MASK_9 },
{ "STAR", '*', WIPHONE_KEY_MASK_ASTERISK },
{ "HASH", '#', WIPHONE_KEY_MASK_HASH },
{ "UP", WIPHONE_KEY_UP, WIPHONE_KEY_MASK_UP },
{ "DOWN", WIPHONE_KEY_DOWN, WIPHONE_KEY_MASK_DOWN },
{ "LEFT", WIPHONE_KEY_LEFT, WIPHONE_KEY_MASK_LEFT },
{ "RIGHT", WIPHONE_KEY_RIGHT, WIPHONE_KEY_MASK_RIFHT },
{ "OK", WIPHONE_KEY_OK, WIPHONE_KEY_MASK_OK },
{ "BACK", WIPHONE_KEY_BACK, WIPHONE_KEY_MASK_BACK },
{ "SELECT", WIPHONE_KEY_SELECT, WIPHONE_KEY_MASK_SELECT },
{ "CALL", WIPHONE_KEY_CALL, WIPHONE_KEY_MASK_CALL },
{ "END", WIPHONE_KEY_END, WIPHONE_KEY_MASK_END },
{ "F1", WIPHONE_KEY_F1, WIPHONE_KEY_MASK_F1 },
{ "F2", WIPHONE_KEY_F2, WIPHONE_KEY_MASK_F2 },
{ "F3", WIPHONE_KEY_F3, WIPHONE_KEY_MASK_F3 },
{ "F4", WIPHONE_KEY_F4, WIPHONE_KEY_MASK_F4 },
};
bool appendFormat(char* output, size_t capacity, size_t& length,
const char* format, ...) {
if (length >= capacity) {
return false;
}
va_list arguments;
va_start(arguments, format);
int written = vsnprintf(output + length, capacity - length, format, arguments);
va_end(arguments);
if (written < 0 || static_cast<size_t>(written) >= capacity - length) {
return false;
}
length += static_cast<size_t>(written);
return true;
}
bool parseCanonicalMask(const char* text, uint32_t& mask) {
if (text == NULL || strlen(text) != 8u) {
return false;
}
uint32_t value = 0u;
for (const char* cursor = text; *cursor != '\0'; ++cursor) {
uint8_t digit;
if (*cursor >= '0' && *cursor <= '9') {
digit = static_cast<uint8_t>(*cursor - '0');
} else if (*cursor >= 'A' && *cursor <= 'F') {
digit = static_cast<uint8_t>(*cursor - 'A' + 10);
} else {
return false;
}
value = (value << 4) | digit;
}
mask = value;
return true;
}
uint8_t completedTestPrefix(const SystemBenchmarkRunResult& run) {
uint8_t count = 0u;
while (count < SYSTEM_BENCHMARK_TEST_COUNT &&
run.tests[count].medianUs != 0u) {
++count;
}
return count;
}
bool standardResultIsOfficial(const SystemBenchmarkRunResult& run) {
if (!systemBenchmarkProfileAllowsOfficialPass() || !run.safeCleanup ||
run.status != SYSTEM_BENCHMARK_PASS || run.globalScore == 0u ||
completedTestPrefix(run) != SYSTEM_BENCHMARK_TEST_COUNT) {
return false;
}
for (uint8_t index = 0u; index < SYSTEM_BENCHMARK_TEST_COUNT; ++index) {
if (run.tests[index].status != SYSTEM_BENCHMARK_PASS ||
run.tests[index].actualCrc != run.tests[index].expectedCrc ||
run.tests[index].score == 0u) {
return false;
}
}
for (uint8_t index = 0u; index < SYSTEM_BENCHMARK_DOMAIN_COUNT; ++index) {
if (run.domains[index].status != SYSTEM_BENCHMARK_PASS ||
run.domains[index].score == 0u) {
return false;
}
}
return true;
}
const char* systemBenchmarkRunStatus(
const SystemBenchmarkRunResult& run, bool extended) {
if (!run.safeCleanup || run.status == SYSTEM_BENCHMARK_UNSAFE) {
return "UNSAFE";
}
switch (run.status) {
case SYSTEM_BENCHMARK_PASS:
break;
case SYSTEM_BENCHMARK_CALIBRATION:
return "CALIBRATION";
case SYSTEM_BENCHMARK_INVALID:
return "INVALID";
case SYSTEM_BENCHMARK_BLOCKED:
return "BLOCKED";
case SYSTEM_BENCHMARK_CANCELLED:
return "CANCELLED";
case SYSTEM_BENCHMARK_FAIL:
return "FAIL";
default:
return "FAIL";
}
if (!extended) {
return "PASS";
}
bool anyPass = false;
bool anyFailure = false;
bool anyUnsafe = false;
for (uint8_t index = 0u;
index < run.extended.diagnosticCount; ++index) {
SystemBenchmarkDiagnosticStatus status = run.extended.diagnostics[index].status;
anyPass = anyPass || status == SYSTEM_BENCHMARK_DIAGNOSTIC_PASS;
anyFailure = anyFailure || status == SYSTEM_BENCHMARK_DIAGNOSTIC_FAIL;
anyUnsafe = anyUnsafe || status == SYSTEM_BENCHMARK_DIAGNOSTIC_UNSAFE;
}
if (anyUnsafe) {
return "UNSAFE";
}
if (anyFailure) {
return "DIAG_FAIL";
}
return anyPass ? "PASS" : "SKIP";
}
char diagnosticWireStatus(SystemBenchmarkDiagnosticStatus status) {
switch (status) {
case SYSTEM_BENCHMARK_DIAGNOSTIC_PASS: return 'P';
case SYSTEM_BENCHMARK_DIAGNOSTIC_FAIL: return 'F';
case SYSTEM_BENCHMARK_DIAGNOSTIC_UNSAFE: return 'U';
case SYSTEM_BENCHMARK_DIAGNOSTIC_NOT_RUN:
case SYSTEM_BENCHMARK_DIAGNOSTIC_SKIP:
default: return 'S';
}
}
} // namespace
WiPhoneTestHarness::WiPhoneTestHarness()
: lineLength(0), droppingLine(false), frame(NULL), frameWidth(0), frameHeight(0),
tilesAcross(0), tilesDown(0), nextTile(0), frameNumber(0), frameCrc(0),
responseSequence(0), heldMask(0), sendingFrame(false),
systemBenchmarkSequence(0), systemBenchmarkMask(0),
systemBenchmarkActive(false), systemBenchmarkExtended(false)
#ifdef BUILD_GAMES
, benchmarkSequence(0), gameFpsSequence(0), gameFpsActive(false)
#endif
{
line[0] = '\0';
}
WiPhoneTestHarness::~WiPhoneTestHarness() {
if (frame) {
heap_caps_free(frame);
}
}
void WiPhoneTestHarness::begin() {
int length = snprintf(tx, sizeof(tx), "@WP1,0,READY,%s,%u,%u\r\n",
FIRMWARE_VERSION, (unsigned)TFT_WIDTH, (unsigned)TFT_HEIGHT);
if (length > 0 && (size_t)length < sizeof(tx)) {
writeTx(length);
}
}
void WiPhoneTestHarness::poll(GUI& gui, RingBuffer<char>& keypadBuffer) {
if (systemBenchmarkActive) {
serviceSystemBenchmark(gui, keypadBuffer);
return;
}
pollInput(gui, keypadBuffer);
serviceGameFps(gui);
if (sendingFrame) {
sendNextTile();
}
}
void WiPhoneTestHarness::serviceGameFps(GUI& gui) {
#ifdef BUILD_GAMES
if (!gameFpsActive) {
return;
}
bool ready = false;
bool passed = false;
const char* error = "APP_ENDED";
uint32_t intervals = 0;
uint32_t frames = 0;
uint32_t wallUs = 0;
uint32_t renderCount = 0;
uint64_t renderSumUs = 0;
uint32_t renderMinUs = 0;
uint32_t renderMaxUs = 0;
uint32_t lcdCount = 0;
uint64_t lcdSumUs = 0;
uint32_t lcdMinUs = 0;
uint32_t lcdMaxUs = 0;
uint32_t lateCount = 0;
uint64_t lateSumUs = 0;
uint32_t lateMaxUs = 0;
uint32_t totalCompletions = 0;
bool available = gui.gravityPerformanceForTest(
ready, passed, error, intervals, frames, wallUs,
renderCount, renderSumUs, renderMinUs, renderMaxUs,
lcdCount, lcdSumUs, lcdMinUs, lcdMaxUs,
lateCount, lateSumUs, lateMaxUs, totalCompletions);
if (available && !ready) {
return;
}
if (!available) {
ready = true;
passed = false;
error = "APP_ENDED";
}
int length = snprintf(
tx, sizeof(tx),
"@WP1,%u,GAME_FPS,GRAVITY,V2,%s,%s,%u,%u,%u,"
"%u,%llu,%u,%u,%u,%llu,%u,%u,%u,%llu,%u,%u\r\n",
(unsigned)gameFpsSequence, passed ? "PASS" : "FAIL",
error != NULL ? error : "UNKNOWN", (unsigned)intervals,
(unsigned)frames, (unsigned)wallUs, (unsigned)renderCount,
(unsigned long long)renderSumUs, (unsigned)renderMinUs,
(unsigned)renderMaxUs, (unsigned)lcdCount,
(unsigned long long)lcdSumUs, (unsigned)lcdMinUs,
(unsigned)lcdMaxUs, (unsigned)lateCount,
(unsigned long long)lateSumUs, (unsigned)lateMaxUs,
(unsigned)totalCompletions);
if (length > 0 && (size_t)length < sizeof(tx)) {
writeTx(length);
} else {
writeResponse(gameFpsSequence, "ERR", "REPORT_OVERFLOW");
}
gui.clearGravityPerformanceForTest();
gameFpsActive = false;
#endif
}
void WiPhoneTestHarness::serviceSystemBenchmark(
GUI& gui, RingBuffer<char>& keypadBuffer) {
const SystemBenchmarkRunResult* run = NULL;
uint64_t totalUs = 0u;
uint32_t offset = 0u;
const char* error = NULL;
bool available = gui.systemBenchmarkResultForTest(
run, totalUs, offset, error);
(void)offset;
if (available && run == NULL) {
return;
}
resetQueuedInput(keypadBuffer);
if (!available) {
writeResponse(systemBenchmarkSequence, "ERR",
error != NULL ? error : "SYSTEM_BENCH_APP_ENDED");
systemBenchmarkActive = false;
return;
}
ReportFormatStatus format = formatSystemBenchmarkReport(gui, *run, totalUs);
if (format == REPORT_FORMAT_OK) {
writeTx(strlen(tx));
} else {
writeResponse(systemBenchmarkSequence, "ERR",
format == REPORT_FORMAT_OVERFLOW
? "REPORT_OVERFLOW" : "REPORT_INVALID");
}
systemBenchmarkActive = false;
}
WiPhoneTestHarness::ReportFormatStatus
WiPhoneTestHarness::formatSystemBenchmarkReport(
GUI& gui, const SystemBenchmarkRunResult& run, uint64_t totalUs) {
static_assert(SYSTEM_BENCHMARK_DIAGNOSTIC_COUNT == 6,
"SYSEXT240V1 report requires six diagnostics");
if (totalUs == 0u || run.profileId[0] == '\0' || run.buildId[0] == '\0') {
return REPORT_FORMAT_INVALID;
}
uint8_t completed = completedTestPrefix(run);
for (uint8_t index = completed;
index < SYSTEM_BENCHMARK_TEST_COUNT; ++index) {
if (run.tests[index].medianUs != 0u) {
return REPORT_FORMAT_INVALID;
}
}
const char* wireRun = systemBenchmarkRunStatus(run, systemBenchmarkExtended);
char extendedCrc[9] = "-";
if (systemBenchmarkExtended) {
snprintf(extendedCrc, sizeof(extendedCrc), "%08X",
static_cast<unsigned>(SYSTEM_BENCHMARK_EXTENDED_PROFILE.crc));
}
bool official = standardResultIsOfficial(run) &&
strcmp(wireRun, "UNSAFE") != 0 && strcmp(wireRun, "FAIL") != 0 &&
strcmp(wireRun, "INVALID") != 0 && strcmp(wireRun, "BLOCKED") != 0;
bool goldenMatch = systemBenchmarkProfileAllowsOfficialPass() &&
completed == SYSTEM_BENCHMARK_TEST_COUNT;
for (uint8_t index = 0u; goldenMatch &&
index < SYSTEM_BENCHMARK_TEST_COUNT; ++index) {
goldenMatch = run.tests[index].actualCrc == run.tests[index].expectedCrc;
}
uint32_t workCrc = completed != 0u
? systemBenchmarkWorkCrc(run.tests, completed) : 0u;
const SystemBenchmarkTestDescriptor* failureDescriptor = NULL;
bool goldenFailure = run.firstFailure.testId != 0;
if (goldenFailure) {
failureDescriptor = systemBenchmarkFindTest(run.firstFailure.testId);
if (failureDescriptor == NULL || run.firstFailure.actualCrc == 0u ||
run.firstFailure.actualCrc == run.firstFailure.expectedCrc ||
run.firstFailure.expectedCrc != failureDescriptor->expectedCrc ||
(run.status != SYSTEM_BENCHMARK_INVALID &&
strcmp(wireRun, "UNSAFE") != 0)) {
return REPORT_FORMAT_INVALID;
}
} else if (run.status == SYSTEM_BENCHMARK_INVALID) {
return REPORT_FORMAT_INVALID;
}
if (official && (!goldenMatch || workCrc != run.workCrc)) {
return REPORT_FORMAT_INVALID;
}
uint8_t baselineStatus = 0u;
uint32_t baselineGlobalScore = 0u;
int32_t baselineDeltaBasisPoints = 0;
if (!gui.systemBenchmarkBaselineForTest(
baselineStatus, baselineGlobalScore, baselineDeltaBasisPoints)) {
baselineStatus = static_cast<uint8_t>(
SystemBenchmarkStore::BASELINE_NONE);
}
bool compatibleBaseline = official &&
baselineStatus == static_cast<uint8_t>(
SystemBenchmarkStore::BASELINE_MATCH) &&
baselineGlobalScore != 0u;
const char* baselineWire = compatibleBaseline ? "COMPATIBLE" :
(baselineStatus == static_cast<uint8_t>(
SystemBenchmarkStore::BASELINE_NONE) ? "NONE" : "INCOMPATIBLE");
size_t length = 0u;
tx[0] = '\0';
if (!appendFormat(tx, sizeof(tx), length,
"@WP1,%u,SYSTEM_BENCH,V1,run=%s,off=%u,safe=%u,"
"p=%s,pid=%s,xid=%s,bid=%s,fw=%s,pc=%08X,xc=%s,"
"hw=%08X,gold=",
static_cast<unsigned>(systemBenchmarkSequence), wireRun,
official ? 1u : 0u, run.safeCleanup ? 1u : 0u,
systemBenchmarkExtended ? "EXTENDED" : "STANDARD",
run.profileId,
systemBenchmarkExtended
? SYSTEM_BENCHMARK_EXTENDED_PROFILE.id : "-",
run.buildId, FIRMWARE_VERSION,
static_cast<unsigned>(run.profileCrc),
extendedCrc,
static_cast<unsigned>(run.hardwareCrc))) {
return REPORT_FORMAT_OVERFLOW;
}
if (goldenFailure) {
if (!appendFormat(tx, sizeof(tx), length, "0")) {
return REPORT_FORMAT_OVERFLOW;
}
} else if (goldenMatch) {
if (!appendFormat(tx, sizeof(tx), length, "1")) {
return REPORT_FORMAT_OVERFLOW;
}
} else if (!appendFormat(tx, sizeof(tx), length, "-")) {
return REPORT_FORMAT_OVERFLOW;
}
if (!appendFormat(tx, sizeof(tx), length, ",wc=")) {
return REPORT_FORMAT_OVERFLOW;
}
if (completed != 0u) {
if (!appendFormat(tx, sizeof(tx), length, "%08X",
static_cast<unsigned>(workCrc))) {
return REPORT_FORMAT_OVERFLOW;
}
} else if (!appendFormat(tx, sizeof(tx), length, "-")) {
return REPORT_FORMAT_OVERFLOW;
}
if (!appendFormat(tx, sizeof(tx), length, ",ac=")) {
return REPORT_FORMAT_OVERFLOW;
}
for (uint8_t index = 0u; index < SYSTEM_BENCHMARK_TEST_COUNT; ++index) {
if (index != 0u && !appendFormat(tx, sizeof(tx), length, ";")) {
return REPORT_FORMAT_OVERFLOW;
}
if (index < completed) {
if (!appendFormat(tx, sizeof(tx), length, "%08X",
static_cast<unsigned>(run.tests[index].actualCrc))) {
return REPORT_FORMAT_OVERFLOW;
}
} else if (!appendFormat(tx, sizeof(tx), length, "-")) {
return REPORT_FORMAT_OVERFLOW;
}
}
if (!appendFormat(tx, sizeof(tx), length, ",fail=")) {
return REPORT_FORMAT_OVERFLOW;
}
if (goldenFailure) {
if (!appendFormat(tx, sizeof(tx), length, "%s:%08X:%08X",
failureDescriptor->name,
static_cast<unsigned>(run.firstFailure.expectedCrc),
static_cast<unsigned>(run.firstFailure.actualCrc))) {
return REPORT_FORMAT_OVERFLOW;
}
} else if (!appendFormat(tx, sizeof(tx), length, "-")) {
return REPORT_FORMAT_OVERFLOW;
}
if (!appendFormat(tx, sizeof(tx), length, ",n=")) {
return REPORT_FORMAT_OVERFLOW;
}
for (uint8_t index = 0u; index < SYSTEM_BENCHMARK_TEST_COUNT; ++index) {
if (!appendFormat(tx, sizeof(tx), length, "%s%u",
index == 0u ? "" : ";",
index < completed
? SYSTEM_BENCHMARK_REPETITION_COUNT : 0u)) {
return REPORT_FORMAT_OVERFLOW;
}
}
if (!appendFormat(tx, sizeof(tx), length, ",med=")) {
return REPORT_FORMAT_OVERFLOW;
}
for (uint8_t index = 0u; index < SYSTEM_BENCHMARK_TEST_COUNT; ++index) {
if (index != 0u && !appendFormat(tx, sizeof(tx), length, ";")) {
return REPORT_FORMAT_OVERFLOW;
}
if (index < completed) {
if (!appendFormat(tx, sizeof(tx), length, "%u",
static_cast<unsigned>(run.tests[index].medianUs))) {
return REPORT_FORMAT_OVERFLOW;
}
} else if (!appendFormat(tx, sizeof(tx), length, "-")) {
return REPORT_FORMAT_OVERFLOW;
}
}
if (!appendFormat(tx, sizeof(tx), length, ",ss=")) {
return REPORT_FORMAT_OVERFLOW;
}
for (uint8_t index = 0u; index < SYSTEM_BENCHMARK_TEST_COUNT; ++index) {
if (!appendFormat(tx, sizeof(tx), length, "%s",
index == 0u ? "" : ";")) {
return REPORT_FORMAT_OVERFLOW;
}
if (official) {
if (!appendFormat(tx, sizeof(tx), length, "%u",
static_cast<unsigned>(run.tests[index].score))) {
return REPORT_FORMAT_OVERFLOW;
}
} else if (!appendFormat(tx, sizeof(tx), length, "-")) {
return REPORT_FORMAT_OVERFLOW;
}
}
if (!appendFormat(tx, sizeof(tx), length, ",ds=")) {
return REPORT_FORMAT_OVERFLOW;
}
for (uint8_t index = 0u; index < SYSTEM_BENCHMARK_DOMAIN_COUNT; ++index) {
if (!appendFormat(tx, sizeof(tx), length, "%s",
index == 0u ? "" : ";")) {
return REPORT_FORMAT_OVERFLOW;
}
if (official) {
if (!appendFormat(tx, sizeof(tx), length, "%u",
static_cast<unsigned>(run.domains[index].score))) {
return REPORT_FORMAT_OVERFLOW;
}
} else if (!appendFormat(tx, sizeof(tx), length, "-")) {
return REPORT_FORMAT_OVERFLOW;
}
}
if (!appendFormat(tx, sizeof(tx), length, ",gs=")) {
return REPORT_FORMAT_OVERFLOW;
}
if (official) {
if (!appendFormat(tx, sizeof(tx), length, "%u",
static_cast<unsigned>(run.globalScore))) {
return REPORT_FORMAT_OVERFLOW;
}
} else if (!appendFormat(tx, sizeof(tx), length, "-")) {
return REPORT_FORMAT_OVERFLOW;
}
if (!appendFormat(tx, sizeof(tx), length, ",bl=%s,bg=",
baselineWire)) {
return REPORT_FORMAT_OVERFLOW;
}
if (compatibleBaseline) {
if (!appendFormat(tx, sizeof(tx), length, "%u",
static_cast<unsigned>(baselineGlobalScore))) {
return REPORT_FORMAT_OVERFLOW;
}
} else if (!appendFormat(tx, sizeof(tx), length, "-")) {
return REPORT_FORMAT_OVERFLOW;
}
if (!appendFormat(tx, sizeof(tx), length, ",bd=")) {
return REPORT_FORMAT_OVERFLOW;
}
if (compatibleBaseline) {
if (!appendFormat(tx, sizeof(tx), length, "%ld",
static_cast<long>(baselineDeltaBasisPoints))) {
return REPORT_FORMAT_OVERFLOW;
}
} else if (!appendFormat(tx, sizeof(tx), length, "-")) {
return REPORT_FORMAT_OVERFLOW;
}
if (!appendFormat(tx, sizeof(tx), length, ",xm=%08X,xd=",
static_cast<unsigned>(systemBenchmarkExtended
? systemBenchmarkMask : 0u))) {
return REPORT_FORMAT_OVERFLOW;
}
if (!systemBenchmarkExtended) {
if (!appendFormat(tx, sizeof(tx), length, "-")) {
return REPORT_FORMAT_OVERFLOW;
}
} else {
if (run.extended.diagnosticCount != SYSTEM_BENCHMARK_DIAGNOSTIC_COUNT) {
return REPORT_FORMAT_INVALID;
}
for (uint8_t index = 0u;
index < SYSTEM_BENCHMARK_DIAGNOSTIC_COUNT; ++index) {
const SystemBenchmarkDiagnosticResult& diagnostic =
run.extended.diagnostics[index];
const SystemBenchmarkExtendedDiagnosticDescriptor& descriptor =
SYSTEM_BENCHMARK_EXTENDED_PROFILE.diagnostics[index];
bool selected = descriptor.maskBit == 0u ||
(systemBenchmarkMask & descriptor.maskBit) != 0u;
bool skipped = diagnostic.status ==
SYSTEM_BENCHMARK_DIAGNOSTIC_NOT_RUN ||
diagnostic.status == SYSTEM_BENCHMARK_DIAGNOSTIC_SKIP;
if ((!selected && !skipped) ||
(skipped && (diagnostic.bytes != 0u ||
diagnostic.durationUs != 0u)) ||
(!skipped && diagnostic.durationUs == 0u) ||
(diagnostic.status == SYSTEM_BENCHMARK_DIAGNOSTIC_PASS &&
descriptor.reportsBytes && diagnostic.bytes == 0u)) {
return REPORT_FORMAT_INVALID;
}
if (!appendFormat(tx, sizeof(tx), length, "%s%c:%u:%u",
index == 0u ? "" : ";",
diagnosticWireStatus(diagnostic.status),
skipped ? 0u : static_cast<unsigned>(diagnostic.bytes),
skipped ? 0u :
static_cast<unsigned>(diagnostic.durationUs))) {
return REPORT_FORMAT_OVERFLOW;
}
}
}
if (!appendFormat(tx, sizeof(tx), length, ",tm=%llu\r\n",
static_cast<unsigned long long>(totalUs))) {
return REPORT_FORMAT_OVERFLOW;
}
return REPORT_FORMAT_OK;
}
void WiPhoneTestHarness::resetQueuedInput(
RingBuffer<char>& keypadBuffer) {
uart_flush_input(UART_NUM_0);
lineLength = 0u;
droppingLine = false;
heldMask = 0u;
while (!keypadBuffer.empty()) {
keypadBuffer.get();
}
}
bool WiPhoneTestHarness::isBenchmarkExclusive() const {
#ifdef BUILD_GAMES
return benchmark.isActive() || benchmark.hasResult();
#else
return false;
#endif
}
void WiPhoneTestHarness::serviceBenchmark(
GUI& gui, RingBuffer<char>& keypadBuffer) {
#ifdef BUILD_GAMES
benchmark.service(gui);
if (!benchmark.hasResult()) {
return;
}
uart_flush_input(UART_NUM_0);
lineLength = 0;
droppingLine = false;
heldMask = 0;
while (!keypadBuffer.empty()) {
keypadBuffer.get();
}
const GravityBenchmarkResult& result = benchmark.getResult();
int length = snprintf(
tx, sizeof(tx),
"@WP1,%u,BENCH,GRAVITY,V2,%s,%s,%s,%s,240,320,RGB565,%u,%u,%08X,"
"%u,%u,%u,%u,%u,%llu,%d,%u,%08X,%08X,"
"%u,%llu,%u,%u,%u,%llu,%u,%u,%u,%llu,%u,%u,%u,%llu,%u\r\n",
(unsigned)benchmarkSequence,
result.passed ? "PASS" : "FAIL", result.error,
GravityBenchmark::PROFILE, FIRMWARE_VERSION,
(unsigned)GRAVITY_DISPLAY_SPI_FREQUENCY, (unsigned)result.cpuMHz,
(unsigned)result.profileCrc,
(unsigned)result.steps, (unsigned)result.simulationMs,
(unsigned)result.gameMs, (unsigned)result.frames,
(unsigned)result.pushes, (unsigned long long)result.wallUs,
result.finishResult, (unsigned)result.finishStep,
(unsigned)result.workCrc, (unsigned)result.frameCrc,
(unsigned)result.physics.count,
(unsigned long long)result.physics.sumUs,
(unsigned)result.physics.reportedMin(),
(unsigned)result.physics.maxUs,
(unsigned)result.render.count,
(unsigned long long)result.render.sumUs,
(unsigned)result.render.reportedMin(),
(unsigned)result.render.maxUs,
(unsigned)result.lcd.count,
(unsigned long long)result.lcd.sumUs,
(unsigned)result.lcd.reportedMin(),
(unsigned)result.lcd.maxUs,
(unsigned)result.lateFrames,
(unsigned long long)result.lateSumUs,
(unsigned)result.lateMaxUs);
if (length > 0 && (size_t)length < sizeof(tx)) {
writeTx(length);
} else {
writeResponse(benchmarkSequence, "ERR", "REPORT_OVERFLOW");
}
benchmark.clearResult();
#endif
}
void WiPhoneTestHarness::pollInput(GUI& gui, RingBuffer<char>& keypadBuffer) {
uint8_t input;
while (uart_read_bytes(UART_NUM_0, &input, 1, 0) == 1) {
char ch = (char)input;
if (ch == '\r' || ch == '\n') {
if (droppingLine) {
droppingLine = false;
lineLength = 0;
writeResponse(0, "ERR", "LINE_TOO_LONG");
return;
}
if (lineLength) {
line[lineLength] = '\0';
processLine(gui, keypadBuffer);
lineLength = 0;
return;
}
continue;
}
if (droppingLine) {
continue;
}
if (lineLength + 1 >= sizeof(line)) {
droppingLine = true;
continue;
}
line[lineLength++] = ch;
}
}
void WiPhoneTestHarness::processLine(GUI& gui, RingBuffer<char>& keypadBuffer) {
static const size_t MAX_FIELDS = 6;
char* fields[MAX_FIELDS];
size_t fieldCount = 1;
fields[0] = line;
for (char* cursor = line; *cursor; ++cursor) {
if (*cursor == ',') {
*cursor = '\0';
if (fieldCount >= MAX_FIELDS) {
writeResponse(0, "ERR", "BAD_COMMAND");
return;
}
fields[fieldCount++] = cursor + 1;
}
}
uint32_t sequence;
if (fieldCount < 3 || strcmp(fields[0], "@WP1") ||
!parseSequence(fields[1], sequence) || !fields[2][0]) {
writeResponse(0, "ERR", "BAD_COMMAND");
return;
}
const char* command = fields[2];
if (!strcmp(command, "PING")) {
if (fieldCount != 3) {
writeResponse(sequence, "ERR", "BAD_PING_COMMAND");
return;
}
writeResponse(sequence, "OK", "PONG");
return;
}
if (!strcmp(command, "INFO")) {
if (fieldCount != 3) {
writeResponse(sequence, "ERR", "BAD_INFO_COMMAND");
return;
}
int length = snprintf(tx, sizeof(tx),
"@WP1,%u,INFO,%s,%u,%u,%u,%u,%08X\r\n",
(unsigned)sequence, FIRMWARE_VERSION,
(unsigned)TFT_WIDTH, (unsigned)TFT_HEIGHT,
(unsigned)ESP.getFreeHeap(),
(unsigned)ESP.getFreePsram(),
(unsigned)heldMask);
if (length > 0 && (size_t)length < sizeof(tx)) {
writeTx(length);
}
return;
}
if (gui.systemBenchmarkIsRunningForTest() && strcmp(command, "STATE")) {
writeResponse(sequence, "ERR", "SYSTEM_BENCH_BUSY");
return;
}
if (!strcmp(command, "SYSTEM_BENCH_START")) {
bool extended = false;
uint32_t diagnosticMask = 0u;
if (fieldCount == 4u && !strcmp(fields[3], "STANDARD")) {
extended = false;
} else if (fieldCount == 5u && !strcmp(fields[3], "EXTENDED") &&
parseCanonicalMask(fields[4], diagnosticMask)) {
extended = true;
} else {
writeResponse(sequence, "ERR", "BAD_SYSTEM_BENCH_START_COMMAND");
return;
}
uint32_t allowedMask = 0u;
for (uint8_t index = 0u;
index < SYSTEM_BENCHMARK_EXTENDED_PROFILE.diagnosticCount; ++index) {
allowedMask |= SYSTEM_BENCHMARK_EXTENDED_PROFILE.diagnostics[index].maskBit;
}
if ((!extended && diagnosticMask != 0u) ||
(diagnosticMask & ~allowedMask) != 0u) {
writeResponse(sequence, "ERR", "SYSTEM_BENCH_BAD_MASK");
return;
}
if (systemBenchmarkActive || gui.systemBenchmarkIsRunningForTest()) {
writeResponse(sequence, "ERR", "SYSTEM_BENCH_BUSY");
return;
}
#ifdef BUILD_GAMES
if (benchmark.isActive() || benchmark.hasResult()) {
writeResponse(sequence, "ERR", "BENCH_BUSY");
return;
}
if (gameFpsActive) {
writeResponse(sequence, "ERR", "GAME_FPS_BUSY");
return;
}
#endif
if (sendingFrame) {
writeResponse(sequence, "ERR", "FRAME_BUSY");
return;
}
if (gui.state.locked) {
writeResponse(sequence, "ERR", "PHONE_LOCKED");
return;
}
const char* error = NULL;
if (!gui.startSystemBenchmarkForTest(error) ||
!gui.startSystemBenchmarkRunForTest(
extended, diagnosticMask, error)) {
writeResponse(sequence, "ERR",
error != NULL ? error : "SYSTEM_BENCH_INIT_FAILED");
return;
}
if (frame != NULL) {
heap_caps_free(frame);
frame = NULL;
}
heldMask = 0u;
while (!keypadBuffer.empty()) {
keypadBuffer.get();
}
systemBenchmarkSequence = sequence;
systemBenchmarkMask = diagnosticMask;
systemBenchmarkExtended = extended;
systemBenchmarkActive = true;
return;
}
if (!strcmp(command, "BENCH_START")) {
if (fieldCount != 3) {
writeResponse(sequence, "ERR", "BAD_BENCH_START_COMMAND");
return;
}
if (systemBenchmarkActive || gui.systemBenchmarkIsRunningForTest()) {
writeResponse(sequence, "ERR", "SYSTEM_BENCH_BUSY");
return;
}
#ifdef BUILD_GAMES
if (benchmark.isActive() || benchmark.hasResult()) {
writeResponse(sequence, "ERR", "BENCH_BUSY");
return;
}
if (gameFpsActive) {
writeResponse(sequence, "ERR", "GAME_FPS_BUSY");
return;
}
if (sendingFrame) {
writeResponse(sequence, "ERR", "FRAME_BUSY");
return;
}
if (gui.state.locked) {
writeResponse(sequence, "ERR", "PHONE_LOCKED");
return;
}
if (frame != NULL) {
heap_caps_free(frame);
frame = NULL;
}
heldMask = 0;
while (!keypadBuffer.empty()) {
keypadBuffer.get();
}
const char* error = NULL;
if (!benchmark.start(gui, error)) {
writeResponse(sequence, "ERR", error != NULL ? error : "BENCH_INIT_FAILED");
return;
}
benchmarkSequence = sequence;
#else
writeResponse(sequence, "ERR", "BENCH_UNAVAILABLE");
#endif
return;
}
if (!strcmp(command, "GAME_FPS_START")) {
if (fieldCount != 3) {
writeResponse(sequence, "ERR", "BAD_GAME_FPS_START_COMMAND");
return;
}
if (systemBenchmarkActive || gui.systemBenchmarkIsRunningForTest()) {
writeResponse(sequence, "ERR", "SYSTEM_BENCH_BUSY");
return;
}
#ifdef BUILD_GAMES
if (gameFpsActive) {
writeResponse(sequence, "ERR", "GAME_FPS_BUSY");
return;
}
if (sendingFrame) {
writeResponse(sequence, "ERR", "FRAME_BUSY");
return;
}
if (benchmark.isActive() || benchmark.hasResult()) {
writeResponse(sequence, "ERR", "BENCH_BUSY");
return;
}
const char* error = NULL;
if (!gui.startGravityPerformanceForTest(error)) {
writeResponse(sequence, "ERR",
error != NULL ? error : "GAME_FPS_INIT_FAILED");
return;
}
gameFpsSequence = sequence;
gameFpsActive = true;
#else
writeResponse(sequence, "ERR", "GAME_FPS_UNAVAILABLE");
#endif
return;
}
if (!strcmp(command, "STATE")) {
if (fieldCount != 3) {
writeResponse(sequence, "ERR", "BAD_STATE_COMMAND");
return;
}
int length = snprintf(tx, sizeof(tx),
"@WP1,%u,STATE,%u,%u,%u\r\n",
(unsigned)sequence,
gui.state.locked ? 1u : 0u,
(unsigned)gui.currentAppForTest(),
(unsigned)gui.currentMenuForTest());
if (length > 0 && (size_t)length < sizeof(tx)) {
writeTx(length);
}
return;
}
if (!strcmp(command, "GAME")) {
if (fieldCount != 3) {
writeResponse(sequence, "ERR", "BAD_GAME_COMMAND");
return;
}
uint8_t appView;
uint8_t sessionState;
int8_t set;
int8_t track;
int8_t league;
int8_t openTrack;
int8_t openLeague;
uint32_t elapsedMs;
if (!gui.gravityStateForTest(appView, sessionState, set, track, league,
openTrack, openLeague, elapsedMs)) {
writeResponse(sequence, "ERR", "GRAVITY_NOT_RUNNING");
return;
}
int length = snprintf(tx, sizeof(tx),
"@WP1,%u,GAME,GRAVITY,%u,%u,%d,%d,%d,%d,%d,%u\r\n",
(unsigned)sequence, (unsigned)appView,
(unsigned)sessionState, (int)set, (int)track,
(int)league, (int)openTrack, (int)openLeague,
(unsigned)elapsedMs);
if (length > 0 && (size_t)length < sizeof(tx)) {
writeTx(length);
}
return;
}
#ifdef BUILD_GAMES
if (!strcmp(command, "DOOM")) {
if (fieldCount != 3) {
writeResponse(sequence, "ERR", "BAD_DOOM_COMMAND");
return;
}
bool active;
bool runtimeRunning;
bool runtimeFailed;
size_t stackFree;
uint32_t displayReadyCalls;
uint32_t submittedFrames;
uint32_t completedFrames;
uint64_t completionSpanUs;
uint32_t lastTransferDurationUs;
bool framebufferGuardsValid;
uint32_t frameCrc;
uint32_t paletteCrc;
uint16_t uniqueIndexes;
uint32_t dominantPixels;
const char* error;