forked from os-autoinst/os-autoinst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmwqemu.pm
1011 lines (858 loc) · 24.4 KB
/
bmwqemu.pm
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
$|=1;
package bmwqemu;
use strict;
use warnings;
use Time::HiRes qw(sleep gettimeofday);
use Digest::MD5;
use IO::Socket;
use File::Basename;
eval {require Algorithm::Line::Bresenham;};
use Exporter;
use ocr;
use cv;
use needle;
use threads;
use threads::shared;
use Thread::Queue;
use POSIX;
use Term::ANSIColor;
use Data::Dump "dump";
our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
@ISA = qw(Exporter);
@EXPORT = qw($realname $username $password $scriptdir $testresults $serialdev $testedversion %cmd
&diag &modstart &fileContent &qemusend_nolog &qemusend &backend_send_nolog &backend_send &sendkey
&sendkeyw &sendautotype &sendpassword &mouse_move &mouse_set &mouse_click &mouse_hide &clickimage &result_dir
&timeout_screenshot &waitidle &waitserial &waitimage &waitforneedle &waitstillimage &waitcolor
&checkneedle &goandclick
&init_backend &start_vm &stop_vm &set_ocr_rect &get_ocr
&script_run &script_sudo &script_sudo_logout &x11_start_program &ensure_installed &clear_console
&getcurrentscreenshot &power &mydie &checkEnv &waitinststage);
# shared vars
my $goodimageseen :shared = 0;
my $screenshotQueue = Thread::Queue->new();
my $prestandstillwarning :shared = 0;
my $timeoutcounter :shared = 0;
share($ENV{SCREENSHOTINTERVAL}); # to adjust at runtime
my @lastavgcolor = (0,0,0); share(@lastavgcolor);
my @ocrrect; share(@ocrrect);
my @extrahashrects; share(@extrahashrects);
# shared vars end
# global vars
our $logfd;
our $clock_ticks = POSIX::sysconf( &POSIX::_SC_CLK_TCK );
our $debug=1;
our $idlethreshold=($ENV{IDLETHRESHOLD}||$ENV{IDLETHESHOLD}||18)*$clock_ticks/100; # % load max for being considered idle
our $timesidleneeded=2;
our $standstillthreshold=530;
our $realname="Bernhard M. Wiedemann";
our $username="bernhard";
our $password="nots3cr3t";
our $testresults="testresults";
our $serialdev="ttyS0"; #FIXME: also backend
our $serialfile="serial0";
our $gocrbin="/usr/bin/gocr";
our $scriptdir=$0; $scriptdir=~s{/[^/]+$}{};
our $testedversion=$ENV{ISO}||""; $testedversion=~s{.*/}{};$testedversion=~s/\.iso$//; $testedversion=~s{-Media1?$}{};
if(!$ENV{DISTRI}) {
if($testedversion=~m/^(debian|openSUSE|Fedora|SLE[SD]-1\d|oi|FreeBSD|archlinux)-/) {$ENV{DISTRI}=lc($1)}
}
$ENV{CASEDIR}||="$scriptdir/distri/$ENV{DISTRI}" if $ENV{DISTRI};
foreach my $part (split("-", $testedversion)) {$ENV{uc($part)}=1}
## env vars
$ENV{QEMUPORT}||=15222;
$ENV{INSTLANG}||="en_US";
$ENV{CASEDIR}||="$scriptdir/distri/$ENV{DISTRI}" if $ENV{DISTRI};
if(defined($ENV{DISTRI}) && $ENV{DISTRI} eq 'archlinux') {$ENV{HDDMODEL}="ide";}
## env vars end
## keyboard cmd vars
our @keyhistory;
our %cmd=qw(
next alt-n
xnext alt-n
install alt-i
finish alt-f
accept alt-a
createpartsetup alt-c
custompart alt-c
addpart alt-d
donotformat alt-d
addraid alt-i
add alt-a
raid0 alt-0
raid1 alt-1
raid5 alt-5
raid6 alt-6
raid10 alt-i
mountpoint alt-m
filesystem alt-s
acceptlicense alt-a
instdetails alt-d
rebootnow alt-n
otherrootpw alt-s
noautologin alt-a
change alt-c
software s
);
if($ENV{INSTLANG} eq "de_DE") {
$cmd{"next"}="alt-w";
$cmd{"createpartsetup"}="alt-e";
$cmd{"custompart"}="alt-b";
$cmd{"addpart"}="alt-h";
$cmd{"finish"}="alt-b";
$cmd{"accept"}="alt-r";
$cmd{"donotformat"}="alt-n";
$cmd{"add"}="alt-h";
# $cmd{"raid6"}="alt-d"; 11.2 only
$cmd{"raid10"}="alt-r";
$cmd{"mountpoint"}="alt-e";
$cmd{"rebootnow"}="alt-j";
$cmd{"otherrootpw"}="alt-e";
$cmd{"change"}="alt-n";
$cmd{"software"}="w";
}
if($ENV{INSTLANG} eq "fr_FR") {
$cmd{"next"}="alt-s";
}
## keyboard cmd vars end
needle::init("$scriptdir/distri/$ENV{DISTRI}/needles") if ($scriptdir && $ENV{DISTRI});
## some var checks
if(!-x $gocrbin) {$gocrbin=undef}
if($ENV{SUSEMIRROR} && $ENV{SUSEMIRROR}=~s{^(\w+)://}{}) { # strip & check proto
if($1 ne "http") {die "only http mirror URLs are currently supported but found '$1'."}
}
## some var checks end
# global vars end
# local vars
our $backend; #FIXME: make local after adding frontend-api to bmwqemu
my $framecounter = 0; # screenshot counter
## sudo stuff
my $sudotimeout=298; # 5 mins
my $lastsudotime;
my $sudos=0;
## sudo stuff end
## charmap (like L => shift+l)
my %charmap=(
","=>"comma", "."=>"dot", "/"=>"slash", "="=>"equal", "-"=>"minus", "*"=>"asterisk",
"["=>"bracket_left", "]"=>"bracket_right",
"{"=>"shift-bracket_left", "}"=>"shift-bracket_right",
"\\"=>"backslash", "|"=>"shift-backslash",
";"=>"semicolon", ":"=>"shift-semicolon",
"'"=>"apostrophe", '"'=>"shift-apostrophe",
"`"=>"grave_accent", "~"=>"shift-grave_accent",
"<"=>"shift-comma", ">"=>"shift-dot",
"+"=>"shift-equal", "_"=>"shift-minus", '?'=>"shift-slash",
"\t"=>"tab", "\n"=>"ret", " "=>"spc", "\b"=>"backspace", "\e"=>"esc"
);
for my $c ("A".."Z") {$charmap{$c}="shift-\L$c"}
{
my $n=0;
for my $c (')','!','@','#','$','%','^','&','*','(') {$charmap{$c}="shift-".($n++)}
}
## charmap end
# local vars end
# global/shared var set functions
sub set_ocr_rect {@ocrrect=@_;}
# global/shared var set functions end
# util and helper functions
sub diag($) {
$logfd && print $logfd "@_\n";
return unless $debug;
print STDERR "@_\n";
}
sub fctlog {
my $fname = shift;
my @fparams = @_;
$logfd && print $logfd '<<< '.$fname.'('.join(', ', @fparams).")\n";
return unless $debug;
print STDERR colored('<<< '.$fname.'('.join(', ', @fparams).')', 'blue')."\n";
}
sub fctres {
my $fname = shift;
my @fparams = @_;
$logfd && print $logfd ">>> $fname: @fparams\n";
return unless $debug;
print STDERR colored(">>> $fname: @fparams", 'green')."\n";
}
sub fctinfo {
my $fname = shift;
my @fparams = @_;
$logfd && print $logfd "::: $fname: @fparams\n";
return unless $debug;
print STDERR colored("::: $fname: @fparams", 'yellow')."\n";
}
sub modstart {
my @text = @_;
$logfd && print $logfd "||| @text\n";
return unless $debug;
print STDERR colored("||| @text", 'bold')."\n";
}
sub checkEnv($$) {
my $var = shift;
my $val = shift;
return 1 if (defined $ENV{$var} && $ENV{$var} eq $val);
return 0;
}
sub fileContent($) {
my($fn)=@_;
open(my $fd, $fn) or return undef;
local $/;
my $result=<$fd>;
close($fd);
return $result;
}
sub result_dir() {
unless (-e "$testresults/$testedversion") {
mkdir $testresults;
mkdir "$testresults/$testedversion" or die "mkdir $testresults/$testedversion: $!\n";
}
return "$testresults/$testedversion"
}
our $lastscreenshot;
our $lastscreenshotName;
our $lastscreenshotCount;
sub getcurrentscreenshot() {
my $filename;
# using a queue to get the latest is most likely the least efficient solution,
# but we need to check the current screenshot not to miss things
while ($screenshotQueue->pending()) {
# unfortunately passing objects between threads is almost impossible
$filename = $screenshotQueue->dequeue();
}
if ($filename) {
$lastscreenshot = tinycv::read($filename);
$lastscreenshotName = $filename;
$lastscreenshotCount = 0;
}
return $lastscreenshot;
}
sub check_color($$) {
my $color = shift;
my $range = shift;
my $n=0;
foreach my $r (@$range) {
my $c=$color->[$n++];
next unless defined $r;
return 0 unless $r->[0]<=$c && $c<=$r->[1];
}
return 1;
}
# TODO: move to a separate tests file:
sub test_check_color()
{
my $c=[0.1, 0.6, 0.2];
die 1 unless check_color($c, []); # all zero ranges match
die 2 unless check_color($c, [undef, [0.2,0.7], undef]); # just match green
die 3 unless check_color($c, [[0,0.4], [0.4,0.7], [0,0.4]]); # all three must match
die 4 if check_color($c, [[0.3,0.4], [0.2,0.7], [0,0.4]]); # red too low
die 5 if check_color($c, [undef, [0.7,0.9], [0,0.4]]); # green too low
die 6 if check_color($c, [undef, [0.4,0.9], [0,0.1]]); # blue too high
}
# util and helper functions end
# backend management
sub init_backend($) {
my $name=shift;
require "backend/$name.pm";
$backend="backend::$name"->new();
open($logfd, ">>", "currentautoinst-log.txt");
# set unbuffered so that sendkey lines from main thread will be written
my $oldfh=select($logfd); $|=1; select($oldfh);
}
sub start_vm() {
$backend->start_vm();
}
sub stop_vm() {
$backend->stop_vm();
}
sub mydie {
fctlog('mydie', "@_");
$backend->stop_vm();
close $logfd;
eval 'croak "mydie"; ';
exit 1;
}
sub backend_send_nolog($) {
# should not be used if possible
if($backend) {
$backend->send(@_);
}
else {
warn "no backend"
}
}
sub backend_send($) {
# should not be used if possible
fctlog('backend_send', join(',', @_));
&backend_send_nolog;
}
sub qemusend_nolog($) {&backend_send_nolog;} # deprecated
sub qemusend($) {&backend_send;} # deprecated
# backend management end
# runtime keyboard/mouse io functions
## keyboard
=head2 sendkey
sendkey($qemu_key_name)
=cut
sub sendkey($) {
my $key=shift;
#fctlog('sendkey', "key=$key");
$backend->sendkey($key);
my @t=gettimeofday();
push(@keyhistory, [$t[0]*1000000+$t[1], $key]);
sleep(0.15);
}
=head2 sendkeyw
sendkeyw($qemu_key_name)
L</sendkey> then L</waitidle>
=cut
sub sendkeyw($) {
sendkey(shift);
waitidle();
}
=head2 sendautotype
sendautotype($string)
send a string of characters, mapping them to appropriate key names as necessary
=cut
sub sendautotype($) {
my $string=shift;
fctlog('sendautotype', "string='$string'");
foreach my $letter (split("", $string)) {
if($charmap{$letter}) { $letter=$charmap{$letter} }
sendkey $letter;
}
}
sub sendpassword() {
sendautotype($password);
}
## keyboard end
## mouse
sub mouse_move_nosleep($$) {
my ($mdx, $mdy) = @_;
fctlog('mouse_move', "delta_x=$mdx", "delta_y=$mdy");
$backend->mouse_move($mdx, $mdy);
}
sub mouse_set_nosleep($$) {
my ($mx, $my) = @_;
fctlog('mouse_set', "x=$mx", "y=$my");
$backend->mouse_set($mx, $my);
}
sub mouse_move($$) {
# relative
# FIXME: backend value abstraction
my ($mdx, $mdy) = @_;
mouse_move_nosleep($mdx, $mdy);
sleep 0.5;
}
sub mouse_set($$) {
# absolute
my ($mx, $my) = @_;
mouse_set_nosleep($mx, $my);
sleep 0.5;
}
sub mouse_click(;$$) {
my $button = shift || 'left';
my $time = shift || 0.15;
fctlog('mouse_click', "button=$button", "cursor_down=$time");
$backend->mouse_button($button, 1);
sleep $time;
$backend->mouse_button($button, 0);
}
sub mouse_hide(;$) {
my $border_offset = shift || 0;
fctlog('mouse_hide', "border_offset=$border_offset");
$backend->mouse_hide($border_offset);
}
## mouse end
## helpers
sub x11_start_program($;$) {
my $program=shift;
my $options=shift||{};
sendkey "alt-f2"; sleep 4;
sendautotype $program; sleep 1;
if($options->{terminal}) {sendkey "alt-t";sleep 3;}
sendkey "ret";
waitidle();
sleep 1;
}
=head2 script_run
script_run($program, [$wait_seconds])
Run $program (by assuming the console prompt and typing it).
Wait for idle before and after.
=cut
sub script_run($;$) {
# start console application
my $name=shift;
my $wait=shift || 9;
waitidle();
sendautotype("$name\n");
waitidle($wait);
sleep 3;
}
=head2 script_sudo
script_sudo($program, [$wait_seconds])
Run $program. Handle the sudo timeout and send password when appropriate.
$wait_seconds
=cut
sub script_sudo($;$) {
my ($prog,$wait)=@_;
sendautotype("sudo $prog\n");
if(!$lastsudotime||$lastsudotime+$sudotimeout<time()) {$sudos=0}
if($password && !$sudos++) {
waitidle();
sendpassword;
sendkey "ret";
}
$lastsudotime=time();
waitidle($wait);
}
=head2 script_sudo_logout
Reset so that the next sudo will send password
=cut
sub script_sudo_logout() {
$sudos=0
}
sub ensure_installed {
my @pkglist=@_;
#pkcon refresh # once
#pkcon install @pkglist
if($ENV{OPENSUSE}) {
x11_start_program("xdg-su -c 'zypper -n in @pkglist'"); # SUSE-specific
} elsif($ENV{DEBIAN}) {
x11_start_program("su -c 'aptitude -y install @pkglist'", {terminal=>1});
} elsif($ENV{FEDORA}) {
x11_start_program("su -c 'yum -y install @pkglist'", {terminal=>1});
} else {
mydie "TODO: implement package install for your distri $ENV{DISTRI}";
}
if($password) { sendpassword; sendkeyw "ret"; }
waitstillimage(6,90); # wait for install
}
sub clear_console() {
sendkey "ctrl-c";
sleep 1;
sendkey "ctrl-c";
sendautotype "reset\n";
sleep 2;
}
## helpers end
#TODO: convert to new bmwqemu
#sub clickimage($;$$$$) {
# my ($reflist,$button,$bstatus,$flags,$timeout) = @_;
# $flags||="h";
# $timeout||=60;
# my $waitres = waitimage("click/$reflist",$timeout);
# if(defined $waitres) {
# diag "Got absolute refimg coordinates: $waitres->[0]x$waitres->[1]";
# $waitres->[2]=~m/-(-?\d+)-(-?\d+)\.ppm$/;
# my @relcoor = ($1,$2);
# #my @relcoor = ($waitres[2],$waitres[2]);
# #$relcoor[0]=~s/^.*\d-(-?\d+)--?\d+.ppm/$1/;
# #$relcoor[1]=~s/^.*\d--?\d+-(-?\d+).ppm/$1/;
# diag "Got relative action coordinates: $relcoor[0]x$relcoor[1]";
# my @abscoor;
# for my $i (0..1) { $abscoor[$i] = $waitres->[$i] + $relcoor[$i]; }
# diag "Got absolute action coordinates: $abscoor[0]x$abscoor[1]";
# # slide
# if($flags=~m/s/) {
# diag "Sliding mouse to $abscoor[0]x$abscoor[1]";
# for my $pos (Algorithm::Line::Bresenham::line($mouse_position[1],$mouse_position[0] => $abscoor[1],$abscoor[0])) {
# mousemove($pos->[1],$pos->[0],0.005);
# }
# }
# else {
# diag "Set mouse position: $abscoor[0]x$abscoor[1]";
# mousemove($abscoor[0],$abscoor[1]);
# }
# sleep(0.25);
# mousebuttonaction($button, $bstatus);
# sleep(0.25);
# # cursor in ninja mode
# if($flags=~m/h/) {
# mousemove(800,600);
# }
# return @abscoor;
# }
# else {
# diag "Skipping click action!";
# return undef;
# }
#}
sub power($) {
# params: (on), off, acpi, reset
my $action = shift;
fctlog('power', "action=$action");
$backend->power($action);
}
# runtime keyboard/mouse io functions end
# runtime information gathering functions
sub do_take_screenshot() {
my $ret = $backend->screendump();
if ($ret->xres() > 800) {
return $ret->scale(800, 600);
} else {
return $ret;
}
}
sub timeout_screenshot() {
my $n = ++$timeoutcounter;
my $dir=result_dir;
my $n2=sprintf("%02i",$n);
getcurrentscreenshot()->write_optimized("$dir/timeout-$n2.png");
}
sub take_screenshot(;$) {
my $flags = shift || '';
my $path="qemuscreenshot/";
mkdir $path;
my $t=[gettimeofday()];
my $img = do_take_screenshot();
# strip first 10 screenshots, if they are too small (was that related to some ffmpeg issues?)
if(($framecounter++ < 10) && $img->xres()<800) { return; }
# TODO detect bad needles
my $filename=$path.sprintf("%i.%06i.png", $t->[0], $t->[1]);
unless($flags=~m/q/) {
fctlog('screendump', "filename=$filename");
}
#print STDERR $filename,"\n";
my($statuser, $statsystem) = $backend->cpu_stat();
my $statstr = '';
if ($statuser) {
for($statuser,$statsystem) {$_/=$clock_ticks}
$statstr .= "statuser=$statuser ";
$statstr .= "statsystem=$statsystem ";
}
if ($img->xres() > 0) {
@lastavgcolor = $img->avgcolor();
}
#my $filevar = "file=".basename($lastname)." ";
#my $laststgvar = ($ENV{HW})?"laststage=$lastinststage ":'';
#my $md5var = ($ENV{HW})?'':"md5=$md5 ";
#my $avgvar = "avgcolor=".join(',', map(sprintf("%.3f", $_), @lastavgcolor));
#diag($md5var.$filevar.$laststgvar.$statstr.$avgvar);
# hardlinking identical files saves space
# 48 is about the similarity of two screenshots with blinking cursor
if($lastscreenshot && $lastscreenshot->similarity($img) > 48) {
symlink(basename($lastscreenshotName), $filename);
$lastscreenshotCount++;
$prestandstillwarning=($lastscreenshotCount>$standstillthreshold/2);
if($lastscreenshotCount>$standstillthreshold) {
timeout_screenshot(); sleep 1;
my $dir=result_dir;
sendkey "alt-sysrq-w";
sendkey "alt-sysrq-l";
sendkey "alt-sysrq-d"; # only available with CONFIG_LOCKDEP
do_take_screenshot()->write_optimized("$dir/standstill-1.png");sleep 1;
mydie "standstill detected. test ended. see $filename\n"; # above 120s of autoreboot
}
}
else { # new
$img->write($filename) || die "write $filename";
$screenshotQueue->enqueue($filename);
$lastscreenshot = $img;
$lastscreenshotName = $filename;
$lastscreenshotCount = 0;
my $ocr=get_ocr($img);
#if($ocr) { diag "ocr: $ocr" }
}
}
sub do_start_audiocapture($) {
my $filename = shift;
fctlog('start_audiocapture', $filename);
$backend->start_audiocapture($filename);
}
sub do_stop_audiocapture($) {
my $index = shift;
fctlog('stop_audiocapture', $index);
$backend->stop_audiocapture($index);
}
sub alive() {
if(defined $backend) {
# backend will kill me when
# backend.run has been deleted
return $backend->alive();
}
return 0;
}
# runtime information gathering functions end
# check functions (runtime and result-checks)
sub checkrefimgs($$$) {
my ($screenimg, $refimg, $flags) = @_;
my $screenppm = tinycv::read($screenimg);
my $refppm = tinycv::read($refimg);
if (!$screenppm || !$refppm) {
return undef;
}
if ($flags=~m/t/) {
# black/white => drop most background
$screenppm->threshold(0x80);
$refppm->threshold(0x80);
}
if ($flags=~m/f/) {
# perform vector-based fuzzy matching using opencv
return $screenppm->search_fuzzy($refppm);
}
elsif ($flags=~m/d/) {
# allow difference of 40 per byte
return $screenppm->search($refppm, 40);
}
else {
return $screenppm->search($refppm, 0);
}
}
sub get_ocr($) {
# input: tinycv object
my $img=shift;
my $ocr=ocr::get_ocr($img, "-m 2 -s 6", \@ocrrect);
if(!$ocr) {return ""}
$ocr=~s/^[_ \t\n]+//;
$ocr=~s/\n/ --- /g;
# correct common mis-readings:
$ocr=~s/nstaII/nstall/g;
$ocr=~s/l(install|Remaining)/($1/g;
return " ocr='$ocr'";
}
sub decodewav($) {
# FIXME: move to multimonNG (multimon fork)
my $wavfile = shift;
my $dtmf = '';
my $mm = "multimon -a DTMF -t wav $wavfile";
open M, "$mm |" || return 1;
while (<M>) {
next unless /^DTMF: .$/;
my ($a, $b) = split ':';
$b =~ tr/0-9*#ABCD//csd; # Allow 0-9 * # A B C D
$dtmf .= $b;
}
return $dtmf;
}
# check functions end
# wait functions
=head2 waitstillimage
waitstillimage([$stilltime_sec [, $timeout_sec [, $similarity_level]]])
Wait until the screen stops changing
=cut
sub waitstillimage(;$$$) {
my $stilltime=shift||7;
my $timeout=shift||30;
my $similarity_level=shift||48;
my $starttime=time;
my @recentimages; # fifo
fctlog('waitstillimage', "stilltime=$stilltime", "timeout=$timeout", "simlvl=$similarity_level");
while(time-$starttime<$timeout) {
my $img=getcurrentscreenshot();
next unless $img; # this must stay to get only valid imgs to fifo
push(@recentimages, $img);
if(@recentimages > $stilltime) {
my $e = shift @recentimages;
if ($img->similarity($e) > $similarity_level) {
fctres('waitstillimage', "detected same image for $stilltime seconds");
return 1;
}
}
}
timeout_screenshot();
fctres('waitstillimage', "waitstillimage timed out after $timeout");
return 0;
}
sub waitimage($;$$) {
my $reflist = shift;
my $timeout = shift || 60;
my $flags = shift || 'd';
my $wact = ($flags=~m/s/)?'disappear':'appear';
fctlog('waitimage', "reflist=$reflist", "timeout=$timeout", "flags=$flags");
diag "WARNING: waitimage is no longer supported\n";
for(my $i=0;$i<=$timeout;$i+=2) {
getcurrentscreenshot();
sleep 1;
}
timeout_screenshot();
fctres('waitimage', "Waiting for images $reflist ($wact) timed out!");
return undef;
}
=head2 waitcolor
waitcolor($rgb_minmax [, $timeout_sec])
$rgb_minmax is [[red_min,red_max], [green_min,green_max], [blue_min,blue_max]]
eg: [undef, [0.2, 0.7], [0,0.1]]
=cut
sub waitcolor($;$) {
my $rgb_minmax = shift;
my $timeout = shift || 30;
my $starttime = time;
fctlog('waitcolor', "rgb=".dump(@$rgb_minmax), "timeout=$timeout");
while(time-$starttime<$timeout) {
if (check_color(\@lastavgcolor, $rgb_minmax)) {
fctres('waitcolor', "detected ".dump(@lastavgcolor));
return 1;
}
sleep 1;
}
timeout_screenshot();
fctres('waitcolor', "rgb ".dump(@$rgb_minmax)." timed out after $timeout");
return 0;
}
=head2 waitserial
waitserial($regex [, $timeout_sec])
Wait for a message to appear on serial output.
You could have sent it there earlier with
C<script_run("echo Hello World E<gt> /dev/$serialdev");>
=cut
sub waitserial($;$) {
# wait for a message to appear on serial output
my $regexp=shift;
my $timeout=shift||90; # seconds
fctlog('waitserial', "regex=$regexp", "timeout=$timeout");
for my $n (1..$timeout) {
my $str=`tail $serialfile`;
if($str=~m/$regexp/) {fctres('waitserial', "found $regexp"); return 1;}
if($prestandstillwarning) {return 2}
sleep 1;
}
fctres('waitserial', "$regexp timed out after $timeout");
return 0;
}
=head2 waitidle
waitidle([$timeout_sec])
Wait until the system becomes idle (as configured by IDLETHESHOLD in env.sh)
=cut
sub waitidle(;$) {
my $timeout=shift||19;
my $prev;
fctlog('waitidle', "timeout=$timeout");
return 0;
my $timesidle=0;
for my $n (1..$timeout) {
my($stat, $systemstat) = $backend->cpu_stat();
sleep 1; # sleep before skip to timeout when having no data (hw)
next unless $stat;
$stat += $systemstat;
if($prev) {
my $diff = $stat - $prev;
if($diff<$idlethreshold) {
if(++$timesidle > $timesidleneeded) { # idle for $x sec
#if($diff<2000000) # idle for one sec
fctres('waitidle', "idle detected");
return 1;
}
}
else {$timesidle=0}
}
$prev = $stat;
}
fctres('waitidle', "timed out after $timeout");
return 0;
}
sub waitinststage($;$$) {
my $stage = shift;
my $timeout = shift||30;
my $extra = shift;
return waitforneedle($stage, $timeout, $extra);
}
sub _waitforneedle {
my %args = @_;
my $mustmatch = $args{'mustmatch'};
my $timeout = $args{'timeout'} || 30;
# get the array reference to all matching needles
my $needles;
if (ref($mustmatch) eq "ARRAY") {
$needles = $mustmatch;
$mustmatch = '';
for my $n (@{$needles}) {
$mustmatch .= $n->{name} . " ";
}
} elsif ($mustmatch) {
$needles = needle::tags($mustmatch) || [];
}
fctlog('waitforneedle', "'$mustmatch'", "timeout=$timeout");
if (!@$needles) {
printf "NO goods for $mustmatch\n";
# give it some time to settle but not too much
$timeout = 3;
}
my $img = getcurrentscreenshot();
my $oldimg;
for my $n (1..$timeout) {
if ($oldimg) {
sleep 1;
$img = getcurrentscreenshot();
if ($oldimg == $img) { # no change, no need to search
print "no change $n\n";
next;
}
}
my $foundneedle = $img->search($needles);
if ($foundneedle) {
my $t = time();
$img->write(result_dir() . "/match-$mustmatch-$t.png");
fctres(sprintf("found %s, similarity %.2f @ %d/%d",
$foundneedle->{'needle'}->{'name'},
$foundneedle->{'similarity'},
$foundneedle->{'x'}, $foundneedle->{'y'}));
if ($args{'click'}) {
my $rx = 1; # $origx / $img->xres();
my $ry = 1; # $origy / $img->yres();
my $x = ($foundneedle->{'x'} + $foundneedle->{'needle'}->{'width'}/2)*$rx;
my $y = ($foundneedle->{'y'} + $foundneedle->{'needle'}->{'height'}/2)*$ry;
diag ("clicking at $x/$y");
mouse_set($x, $y);
mouse_click($args{'click'}, $args{'clicktime'});
}
return $foundneedle;
}
$oldimg = $img;
}
fctres('waitforneedle', "match=$mustmatch timed out after $timeout");
for (@{$needles||[]}) {
diag $_->{'file'};
}
my $t = time();
$img->write_optimized(result_dir() . "/$mustmatch-$t.png");
my $fn = result_dir() . "/$mustmatch-$t.json";
open(J, ">", $fn) or die "$fn: $!\n";
my $json = { xpos => 0, ypos => 0, width => $img->xres() , height => $img->yres() };
my @tags = ( $mustmatch );
# write out some known env variables
for my $key (qw(VIDEOMODE DESKTOP DISTRI INSTLANG LIVECD)) {
push(@tags, "ENV-$key-" . $ENV{$key}) if $ENV{$key};
}
$json->{"tags"} = \@tags;
print J JSON->new->pretty->encode( $json );
close(J);
diag("wrote $fn");
$args{'retried'} ||= 0;
if (!$args{'check'} && $ENV{'interactive_crop'} && $args{'retried'} < 3) {
my $newname = $mustmatch.($ENV{'interactive_crop'} || '');
system("$scriptdir/crop.py", '--new', $newname, $fn) == 0 || mydie;
# FIXME: kill needle with same file name
$fn = sprintf("%s/needles/%s.json", $ENV{'CASEDIR'}, $newname)
if (-e $fn);
{
diag("reading new needle $fn");
needle->new($fn) || mydie "$!\n";
# XXX: recursion!
return waitforneedle($mustmatch, 3, $args{'check'}, $args{'retried'}+1);
}
}
mydie unless $args{'check'};
return undef;
}
sub waitforneedle($;$) {
return _waitforneedle(mustmatch => $_[0], timeout => $_[1]);
}
sub checkneedle($;$) {
return _waitforneedle(mustmatch => $_[0], timeout => $_[1], check => 1);
}
# warning: will not work due to https://bugs.launchpad.net/qemu/+bug/752476
sub goandclick($;$$$) {
return _waitforneedle(mustmatch => $_[0],
click => ($_[1] || 'left'),
timeout => $_[2],
clicktime => $_[3]);
}
#FIXME: new wait functions
# waitscreenactive - ($backend->screenactive())
# wait-time - like sleep but prints info to log