-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathater
More file actions
executable file
·1642 lines (1379 loc) · 40.4 KB
/
Copy pathater
File metadata and controls
executable file
·1642 lines (1379 loc) · 40.4 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
#!/usr/bin/perl
# g0, 2018
# License: GPLx and Artistic
# Lots of negative logic, Ron, my late CS teacher, would be pissed at me.
# Light a cigar and relax Ron, it's just a silly Perl script.
use strict;
my $epoch = time();
use Time::HiRes qw(usleep nanosleep time gettimeofday tv_interval);
my $t0 = [gettimeofday];
my ($sec0, $micro_sec0) = gettimeofday;
use File::Basename;
use Term::ANSIColor;
use Getopt::Long;
use Pod::Usage;
use File::Basename;
use File::Glob ':globally';
use v5.10;
require 'aterlib.pl';
=head1 Description
Ater, explore baseband AT command interfaces
=cut
=head1 Requirements
adb
an android system that exposes an AT command interface at some serial device
AT_serial set in your configuration file, ater.conf
=cut
=head1 Synopsis
that is; ater -h
=cut
=head1 Options, Examples
--help|h|?
list implemented options
list configuration files
--at=s
send an AT+COM command
s:<+COM>
e.g. ater -at +CGMI
special characters in commands should be escaped
e.g. for AT$QCPWRDN ater -at \$QCPWRDN
e.g. for AT+CLCK="SC",2 ater -at +CLCK=\"SC\",2
--serial=s
select Android device
s:adb_android_device_serial_number
--at-serial|as=s
select AT char device
s:<AT CLI serial device>
not implemented, change it in config
--stat|statistics
print per run statistics for
executed AT commands
received OKs,
errors, trap_triggers, ...
--learn-verbs|lv
try to learn CLI verbs
log effort to files
it learns through AT commands used to list AT commands
the list of commands that list commands could be set at LEARN_VERBS_SEED in the configuration file
--learn-set-objects|lso
implies, input from learn-verbs|lv
try to learn AT CLI set objects
log effort to files
--learn-all-objects|lao
implies, input from learn-verbs|lv
try to learn all listed AT CLI objects
log effort to files
--laocff|learn-all-objects-commands-from-file=s
try to learn all listed AT CLI objects
log effort to files
--verbose
--rate|throttle-factor=i
set in milliseconds the wait time in between AT commands,
and set in milliseconds the wait time in between an AT command and an AT response read.
The default wait time in between commands is 25 milliseconds,
the default wait time in between commands and responses is 25 milliseconds,
therefore, the default throttle-factor is 50 milliseconds.
Throttle factor less than 1ms will be set 1ms..
--documentation
print Ater POD
--printMD
print Ater Readme.MD
--wait-for-device|w=s
wait for android device, s:android_device_serial_number
not implemented, for now you can wait using the wait-for-device.sh wrapper
--grol|get-rid-of-lock
remove soft lock and kill adb fetcher
that or ah_get_rid_of_lock
--who
who android_device,
who baseband_processor
who rild
--debug
print Ater debug information
--time
time Ater run
--chk|check-trap
exits with 0 if the trap value is not changed
configure TRAP_SET_COMMAND, TRAP_QUERY_COMMAND, TRAP_VALUE in the config file
--ping
exits with 0 if PONG_RESPONSE is seen after a PING_COMMAND command
configure PONG_RESPONSE, PING_COMMAND in the config file
--tc|test-commands-from-file-or-pipe=s
test (ATCOM, ATCOM?, ATCOM=?) a list of commands
e.g. ater -tc=./at-commands/totry --throttle-factor 300
blacklisted commands set in the configuration file are excluded
--rc|run-commands-from-file-or-pipe=s
run a list of commands
or drive fuzzing campaigns from files against the AT CLI itself
e.g. ater -rc=./at-commands/torun
blacklisted commands set in the configuration file are not excluded,
just comment out with '#' the commands you don't want to run
--rwp|run-rc-with-ping
send the ping command set at your config
in between commands
--rwt|run-rc-with-chk-trap
check the trap set at your config
in between commands
exit on trigger unless --findresets
--findresets|rc-find-commands-that-reset-trap
find at commands that reset the trap
it does not exit on trap trigger
Examples
run an AT command
./ater -at \$CCLK?
scan for cellular networks
./ater -at +COPS=? --throttle-factor=20000
run a list of commands
./ater -rc=./at-commands/read-messages
run the same command and print AT command statistics
./ater -rc=./at-commands/read-messages -stats
run a list of AT commands with a ping in between commands, exit badly, 3, if the AT device does not pong
./ater -rc=./at-commands/test -rwp -stats
however, if you don't add a large enough throttle-factor (default throttle-factor is 25ms) on
./ater -rc=./at-commands/tofail -rwp -stats
$ echo $?
0
the ping command will pong before the device shutdown ($QCPWRDN)
so, add a large throttle-factor ie wait time in between commands
./ater -rc=./at-commands/tofail -rwp -stats -throttle-factor=5000
Ater:main::atadb shell "echo 'AT+CGMI\r' > /dev/smd8" 256
Ater: main::test_commands: __dev__smd8 did not pong after $QCPWRDN
$ echo $?
3
if you know of a good trap, you could try
./ater -rc=./at-commands/verbs.MSM8917C00B191.ater -rwt -stats
find AT commands that trigger the trap ie change the trap value
./ater -rc=./at-commands/verbs.MSM8917C00B191.ater -rwt -findresets -stats
test a list of commands from a file, that is run AT_COMMAND, AT_COMMAND?, AT_COMMMAND=? for each AT_COMMAND
./ater -tc=./at-commands/verbs.MSM8917C00B191.ater -stats
test a list of commands from a pipe, that is run AT_COMMAND, AT_COMMAND?, AT_COMMMAND=? for each AT_COMMAND
./ater -tc './helpers.sh ah_look_for_HiSi_AT_commands|' -stats
robust AT device ping
source ./helpers.sh
ah_wait -at -ping
attempt to learn set objects/values for commands learned with LEARN_VERBS_SEED
./ater -lso -stats
run AT_COMMAND, AT_COMMAND?, AT_COMMMAND=? for ~ commands learnded with LEARN_VERBS_SEED
./ater -lao -stats
run AT_COMMAND, AT_COMMAND?, AT_COMMMAND=? for all commands from file
./ater -laocff=./at-commands/verbs.MSM8917C00B191.ater -stats
run AT_COMMAND, AT_COMMAND?, AT_COMMMAND=? for all commands from pipe
./ater -laocff './helpers ah_look_for_HiSi_AT_commands|' -stats
see the MS-SIM IMSI number
/ater -at=+CIMI
look up an mccmnc number
./ater -mccmnc 20201
look up mobile operators in China
./ater -mccgrep china
explore android system with serial device KVXBB17C01210608 ($ adb devices)
./ater -serial KVXBB17C01210608 -wa
=cut
=head1 Origin
g0, 2018
=cut
my ($name, $path, $suffix) = fileparse( $0, qr/\.[^.]*$/ );
my $DEBUG = 0;
our $ME = basename($0);
$ME =~ tr/\.pl$//d;
my $CONFIG = "${path}$ME.conf";
my $ME_plain = $ME;
our %CONF = ();
sub say_config {
while(my ($key,$val) = each(%CONF)){
say "$key -> \'$val\'";
}
}
sub read_config
{
open CONFIG, '<' ,$CONFIG or die "$ME: " . (caller(0))[3] . " $!";
while(<CONFIG>){
chomp;
s/#.*//;
s/^\s+//;
s/\s+$//;
next unless length;
my ($var,$val) = split(/\s*=\s*/, $_ , 2);
$CONF{$var} = $val;
}
close CONFIG;
say_config if($DEBUG);
}
read_config;
my @AT_Blist = ();
if(exists $CONF{'AT_Blist'}){
@AT_Blist = split(',', $CONF{'AT_Blist'});
for(@AT_Blist){
s/^\s+//g;
s/\s+$//g;
}
}
sub blacklisted
{
my $command = shift;
say "$ME" . (caller(0))[3] . " checking $command" if $DEBUG;
return 1 if(grep(/^${command}$/, @AT_Blist));
return 0;
}
my @LEARN_VERBS_SEED = ();
if(exists $CONF{'LEARN_VERBS_SEED'}){
@LEARN_VERBS_SEED = split(',', $CONF{'LEARN_VERBS_SEED'});
for(@LEARN_VERBS_SEED){
s/^\s+//g;
s/\s+$//g;
}
}
my $AT_RESP_LOCK = $CONF{'AT_RESP_LOCK'} || "/tmp/ater.lock";
my $AT_serial = $CONF{'AT_serial'} || '/dev/smd11';
my $cATout = '"cat ' . $AT_serial . '"';
my $DEFAULT_cATout = '"cat ' . $AT_serial . '"';
my $DLM = $CONF{'DLM'} || '|';
my $RATE = $CONF{'RATE'} || 25;
my $PING_COMMAND = $CONF{'PING_COMMAND'};
my $PONG_RESPONSE = $CONF{'PONG_RESPONSE'};
my $TRAP_SET_COMMAND = $CONF{'TRAP_SET_COMMAND'};
my $TRAP_QUERY_COMMAND = $CONF{'TRAP_QUERY_COMMAND'};
my $TRAP_DEFAULT_VALUE = $CONF{'TRAP_DEFAULT_VALUE'};
my $TRAP_VALUE = $CONF{'TRAP_VALUE'};
my $TRAP_QUERY_COMMAND_SENT_COUNT = 0;
my $TRAP_TRIGGER_COUNT = 0;
my $PING_COMMAND_SENT_COUNT = 0;
my $MCC_in = '';
my $MCCMNC_in = '';
my $MCCGREP_in = '';
my $ADB_SHELL_COMMAND='';
my $ANDROID_PID_BY_NAME='';
my $ANDROID_PID='';
my $ANDROID_SRV='';
my %ops = ();
my @ops = (
'at|at-command=s',
's|adb-shell-command=s',
'serial|adb-serial-device=s',
'stats|statistics',
'atdev|at-device=s',
'help|?',
'verbose',
'lv|learn-verbs',
'lso|learn-set-objects',
'lao|learn-all-objects|set-objects-and-options',
'laocff|learn-all-objects-commands-from-file=s',
'who',
'was|who-android-system',
'wsr|who-android-service=s',
'wpn|who-android-pid-by-name=s',
'wpi|who-android-pid=i',
'debug|debug-ater',
'time',
'doc|documentation',
'rate|throttle-factor=i',
'grol|get-rid-of-lock',
'printmd|print-md',
'tc|test-commands-from-file-or-pipe=s',
'rc|run-commands-from-file-or-pipe=s',
'rwt|run-rc-with-chk-trap',
'rwp|run-rc-with-ping',
# 'll|list-locks',
# ./helpers wait -at +CGMI
# source helpers; ah_wait -at +CGMI
# 'wait|w|adb-wait-for-device',
'findresets|rc-find-commands-that-reset-trap',
# 'lt|list-trap-triggers',
'testping|test-ping-command',
'ping',
'chk|check-trap',
'settrap',
'su|su-instead-of-adb-root',
'mcc=s',
'mccmnc=s',
'mccgrep=s',
'ops',
);
GetOptions(
\%ops,
'at|at-command=s',
's|adb-shell-command=s' => \$ADB_SHELL_COMMAND,
'serial|adb-serial-device=s',
'stats|statistics',
'atdev|at-device=s' =>\$AT_serial,
'help|?',
'verbose',
'lv|learn-verbs',
'lso|learn-set-objects',
'lao|learn-all-objects|set-objects-and-options',
'laocff|learn-all-objects-commands-from-file=s',
'who',
'was|who-android-system',
'wsr=s' => \$ANDROID_SRV,
'wpn=s' => \$ANDROID_PID_BY_NAME,
#'wpi|who-android-pid=i' => \$ANDROID_PID,
'wpi=i' => \$ANDROID_PID,
'debug|debug-ater' => \$DEBUG,
'time',
'doc|documentation',
'rate|throttle-factor=i' => \$RATE,
'grol|get-rid-of-lock',
'printmd|print-md',
'tc|test-commands-from-file-or-pipe=s',
'rc|run-commands-from-file-or-pipe=s',
'rwt|run-rc-with-chk-trap',
'rwp|run-rc-with-ping',
# 'll|list-locks',
# 'wait|w|adb-wait-for-device',
'findresets|rc-find-commands-that-reset-trap',
# 'lt|list-trap-triggers',
'testping|test-ping-command',
'ping',
'chk|check-trap',
'settrap',
'su|su-instead-of-adb-root',
'mcc=s' => \$MCC_in,
'mccmnc=s' => \$MCCMNC_in,
'mccgrep=s'=> \$MCCGREP_in,
'ops',
)or die("\n$ME: I could not make sense of your options.\n");
Getopt::Long::Configure("bundling");
my $wpn = $ops{'wpn'};
my $wpi = $ops{'wpi'};
my $wsr = $ops{'wsr'};
my $was= $ops{'was'};
my $sh_adb = $ops{'s'};
my $ops = $ops{'ops'};
my $mcc = $ops{'mcc'};
my $su = $ops{'su'};
my $settrap = $ops{'settrap'};
my $laocff = $ops{'laocff'};
my $chk = $ops{'chk'};
my $ping = $ops{'ping'};
my $testping = $ops{'testping'};
my $tc = $ops{'tc'};
my $rc = $ops{'rc'};
my $rwt = $ops{'rwt'};
my $rwp = $ops{'rwp'};
my $findresets= $ops{'findresets'};
my $atcmd = $ops{'at'};
my $statistics = $ops{'stats'};
# my $serial = $ops{'serial'};
our $serial = $ops{'serial'};
my $help = $ops{'help'};
my $chk_trap = $ops{'chk'};
my $verbose = $ops{'verbose'};
my $atdev = $ops{'atdev'};
my $learn_verbs = $ops{'lv'};
my $learn_set_objects = $ops{'lso'};
my $learn_all_objects = $ops{'lao'};
my $who = $ops{'who'};
my $time = $ops{'time'};
my $wait_for_device = 0;
#my $wait_for_device = $ops{'wait'};
my $doc = $ops{'doc'};
my $get_rid_of_lock = $ops{'grol'};
my $print_README_md = $ops{'printmd'};
if($serial){
$serial = '-s ' . $serial . ' ';
}else{
$serial = '';
}
# $serial = "wait-for-device $serial" if(exists $ops{'wait'});
# not as easy as it sounds or I am tripping
# perldoc -f alarm
# or from adb, wait-for[-TRANSPORT]-STATE
# wait for device to be in the given state
# easier and better with a wrapper though e.g. ./wait-for-device.sh -at +CGMI
sub adb_device_serial
{
my @devices_list = `adb devices -l`;
my @serial = ();
for(@devices_list){
next if(/^List\s+.*/); #but
if(/device/){
my @fields = split /\s+/, $_;
push @serial, $fields[0];
}
}
if($DEBUG){ say '' . (caller(0))[3] . "->" . $_ for(@serial); }
return @serial;
}
my $dev = $AT_serial;
my $delimeter = '__';
$dev =~ s/\//$delimeter/g;
my @serials = adb_device_serial;
my $androidserial = $serials[0] unless(length $serial);
my $DATA_DIR = $CONF{'DATA_DIR'} || "${path}data";
my $DATA_MCCMNC = $CONF{'DATA_MCCMNC'} || "${DATA_DIR}/MCCMNCv2.csv";
my $LOG_DIR = $CONF{'LOG_DIR'} || "${path}logs";
my $TEST_COMMANDS_LOG= "$LOG_DIR/${androidserial}_${dev}_test_commands.ater";
my $LOG_VERBS = $CONF{'LOG_VERBS'} || "$LOG_DIR/${androidserial}_${dev}_verbs.ater";
my $LOG_SET_OK_OBJECTS = $CONF{'LOG_SET_OK_OBJECTS'} || "$LOG_DIR/${androidserial}_${dev}_set_objects_ok.ater";
my $LOG_SET_ERROR_OBJECTS = $CONF{'LOG_SET_ERROR_OBJECTS'} || "$LOG_DIR/${androidserial}_${dev}_set_objects_error.ater";
my $LOG_SET_REST_OBJECTS = $CONF{'LOG_SET_REST_OBJECTS'} || "$LOG_DIR/${androidserial}_${dev}_set_object_other.ater";
my $LOG_ALL_OK_OBJECTS = $CONF{'LOG_ALL_OK_OBJECTS'} || "$LOG_DIR/${androidserial}_${dev}_objects_all_ok.ater";
my $LOG_ALL_ERROR_OBJECTS = $CONF{'LOG_ALL_ERROR_OBJECTS'} || "$LOG_DIR/${androidserial}_${dev}_objects_all_error.ater";
my $DEFAULT_LOG_ALL_REST_OBJECTS = "$LOG_DIR/${androidserial}_${dev}_all_rest.ater";
my $DEFAULT_LOG_OPTIONS = "$LOG_DIR/${androidserial}_${dev}_objects_options.ater";
my $DEFAULT_LOG_OPTIONS_E = "$LOG_DIR/${androidserial}_${dev}_objects_options_e.ater";
my $DEFAULT_LOG_PIDS = "$LOG_DIR/PIDS_${androidserial}_${dev}.ater";
my $LOG_ALL_REST_OBJECTS = $CONF{'LOG_ALL_REST_OBJECTS'} || $DEFAULT_LOG_ALL_REST_OBJECTS;
my $LOG_OPTIONS = $CONF{'LOG_OPTIONS'} || $DEFAULT_LOG_OPTIONS;
my $LOG_OPTIONS_E = $CONF{'LOG_OPTIONS_E'} || $DEFAULT_LOG_OPTIONS_E;
my $LOG_PIDS = "$CONF{'LOG_PIDS'}" || $DEFAULT_LOG_PIDS;
my $R_CME_ERROR = "$CONF{'R_CME_ERROR'}" || 'red';
my $R_ERROR = "$CONF{'R_ERROR'}" || 'red';
my $R_OK = "$CONF{'R_OK'}" || 'green';
my $R_NO_CARRIER = "$CONF{'R_NO CARRIER'}" || 'magenta';
my $ATER_COLOR = "$CONF{'ATER_COLOR'}" || 'bold green';
$ME = ''. color($ATER_COLOR) . ucfirst $ME . color('reset');
if($ops){
for(@ops){
print "-";
if(/\|/){
my @fs = split(/\|/, $_);
print $fs[0];
}else{
print ;
}
print ' ';
}
exit 0;
}
if($help){
say;
print "$ME ";
say ' options';
say "\t --$_" for(@ops);
say;
print "$ME ";
say 'configuration';
say "\t $ME_plain.conf";
say;
exit 0;
}
if($print_README_md){
for(`pod2text $0`){
if(/^Synopsis.*/){
print '# ';
print;
say "\t\t --$_" for(@ops);
print "\n";
}else{
print '# ' if(/^\w.*/);
print;
}
}
exit 0;
}
if($get_rid_of_lock){
unlink $AT_RESP_LOCK or warn "$ME: " . (caller(0))[3] . "I could not delete $AT_RESP_LOCK. $!";
my @pinfs = <$LOG_DIR/PIDS*ater>;
for my $file (@pinfs){
say "file $file";
open FILE, '<' , $file or die "$ME: " . (caller(0))[3] . " $!";
while(<FILE>){
next unless length;
chomp;
s/^\s+//;
s/\s+$//;
next unless(/^\d+$/);
say;
system("kill -9 $_ " . ++$_ ." ". ++$_ );
}
close FILE;
say $file;
unlink $file or warn "$ME: " . (caller(0))[3] . "I could not delete $file. $!";
}
exit 0;
}
if($doc){
pod2usage({-exitval => 0, -verbose => 2, -output => \*STDOUT});
}
if($wait_for_device){ #nop
my $PID;
my @args = ();
say "wait for device" if $DEBUG;
say "@ARGV" if $DEBUG;
for my $arg(@ARGV){
unless(/-w/){
push @args, $arg;
}
}
system("${path}wait-for-device.sh " . @args);
if($PID = fork){
say "$ME: wait ". ++$PID . "." if($DEBUG);
say "PID: $PID" if $DEBUG;
}else{
system("${path}wait-for-device.sh ".@args) or print STDERR "$ME: $!";
exit 0;
}
exit 0;
}
my $line_p = 0;
my @gsieved = ();
my %OPTIONS = ();
my $OLDPID = 0;
my $PID = 255;
my $ATC = 0;
my $ERC = 0;
my $OKC = 0;
my $OTHER_C = 0;
my $CMEERROR_C = 0;
my $NO_CARRIER_C = 0;
my $ATout = '';
sub data_mcc
{
my ($mcc, $mccmnc, $any) = @_;
my $DEL = ',,';
my $MCC = 1;
my $MCCMNC = 0;
say "0 - Test Networks" if($mcc eq "0");
say "2 - Europe" if($mcc eq "2");
say "3 - North America and the Carribean" if($mcc eq "3");
say "5 - Oceania" if($mcc eq "5");
say "6 - Africa" if($mcc eq "6");
say "7 - South and Central America" if($mcc eq "7");
say "9 - World Wide" if($mcc eq "9");
open(my $FH, "<", $DATA_MCCMNC) or die ("I could not open $DATA_MCCMNC: $!");
foreach (<$FH>){
chomp;
if(length $any){
say if(/$any/i);
next;
}
my @line = split($DEL, $_);
if(defined $mccmnc){
say if($line[0] eq $mccmnc);
}else{
say if($line[1] eq $mcc);
}
}
close($FH);
}
sub at
{
my $com = $_[0];
my $silent = 0;
$silent = 1 if defined $_[1] and $_[1] =~ /.*silent.*/;
my $print = $_[2];
$print = 1 if($print eq 'print');
my $status = 0;
unless($silent){
print color('blue');
print "$ME" . (caller(0))[3] if($print);
$DEBUG ? print "( $com," . quotemeta($com) . ")->\n" : print "( $com )->\n";
print color('reset');
}
my $sh = '';
if(defined $su){
$com =~ s/\$/\\\\\\\$/g;
$sh = 'adb '.$serial.'shell "su -c \'echo \\"AT' . $com . '\r\\" > ' . $AT_serial."'\"";
}else{
$com =~ s/"/\\"/g;
$com =~ s/'/\\'/g;
$com =~ s/\$/\\\$/g;
$sh = 'adb '.$serial.'shell "echo \'AT'.$com.'\r\' > '.$AT_serial.'"';
}
say "sh: ".$sh if $DEBUG;
system($sh);
if($? == 0){
$status = 1;
}else{
say "$ME:" . (caller(0))[3] . $sh . " $?";
}
$ATC++;
return $status;
}
sub init_line_p
{
$line_p = 1;
open ATOUT, "<$ATout" or die "$ME:" . (caller(0))[3] . ": Can't open < $ATout : $!";
$line_p++ while(<ATOUT>);
close ATOUT;
}
sub rr
{
my $me = (caller(0))[3];
my $line_c = 0;
my @response = ();
my $silent = 0;
$silent = 1 if(defined($_[1]) and $_[1] =~ /.*silent.*/);
say "<- $_[0]" if $DEBUG;
usleep(1000 * $RATE);
#chk_lock
open ATOUT, "<$ATout" or die "$ME:$me: can't open < $ATout : $!\n";
while(<ATOUT>){
if($. >= $line_p){
print "r: $.: $_" if $DEBUG;
chomp;
tr/\cM//d;
push @response, $_;
$line_c++;
}
};
# seek ATOUT, $fpos, 1;
# $fpos = tell ATOUT;
close ATOUT;
$line_p += $line_c;
say "<-." if $DEBUG;
unless($silent){
for(@response){
if(/^\+CME\s?ERROR/){
print color($R_CME_ERROR);
$CMEERROR_C++;
last;
}
elsif(/^ERROR/){
$ERC++;
print color($R_ERROR);
last;
}elsif(/^OK/){
$OKC++;
print color($R_OK);
last;
}elsif(/^NO\s?CARRIER/){
$NO_CARRIER_C++;
print color($R_NO_CARRIER);
last;
}
}
say "<- $ARGV[0]";
say $_ for(@response);
say "<-.";
print color('reset');
}
return @response;
}
#sub who_android_system
#{
# say_command("setenforce 0;getenforce");
# say_command("getprop |egrep 'adb|secure|kernel|debug'");
# say_command("echo 1 > /dev/hwlog_switch");
# say_command("cat /dev/hwlog_switch");
# say_command("cat /sys/kernel/debug/debug_enabled");
# say_command("ls /sys/kernel/debug");
# # /data/local.prop
# say_command("getprop ro.debuggable");
# say_command("setprop kmleak.debug 1");
# say_command("setprop debug.aps.enable 1");
# say_command("setprop persist.sys.huawei.debug 1");
# say_command("setprop persist.sys.kmemleak.debug 1");
# say_command("setprop debug.atrace.tags.enableflags 1");
# say_command("getprop |grep kmleak");
# say_command("toybox ps -AlwZ |grep -i debug");
# say_command("getprop |grep -i debug");
# say_command("grep Proc /proc/cpuinfo");
# say_command("cat /proc/cpuinfo |grep CPU |sort |uniq");
# say_command("free -h");
# say_command("cat /vendor/build.prop");
# say_command("ls -l /etc/init");
# say_command("grep mount_all /*rc");
# # say_command("getprop |grep ro");
# # say_command("ls -l /dev/block/platform/*/by-name 2>/dev/null");
# say_command("find /{system,vendor}/bin -perm -4000 -o -perm -2000 -exec ls -lZ {} \\;");
# #don't try to or with above
# say_command("find /{system,vendor}/bin -perm -2 -exec ls -lZ {} \\;");
# say_command("find /dev/socket -perm -4000 -o -perm 2000 -o -perm -2 -exec ls -lZ {} \\;");
# say_command("find /dev -perm -2 -exec ls -lZ {} \\;");
# say_command("find /dev/ -type c -a -user root -o -user radio -type c -exec ls -lZ {} \\;");
# say_command("cat /proc/net/{ptype,protocols}");
# say_command("toybox netstat -paneW |grep LISTEN");
# say_command("toybox netstat -punta");
#}
sub who
{
my $ADB = "adb $serial";
my $state = `$ADB get-state`; chomp $state;
my $devpath = `$ADB get-devpath`; chomp $devpath;
my $serialno = `adb get-serialno`; chomp $serialno;
my $dev_arch = `$ADB shell getprop ro.product.cpu.abi`; chomp $dev_arch;
my $dev_sdk_v = `$ADB shell getprop ro.build.version.sdk`; chomp $dev_sdk_v;
say "$ME:" . (caller(0))[3] . "\t adb:$state:$devpath:$serialno:$dev_arch:$dev_sdk_v";
at('I', 'silent');
my @resp = rr('I', 'silent');
my $OK = pop @resp;
say "$ME:" . (caller(0))[3] . " @resp " . color('bold green') . $OK . color('reset') if( defined($_[0]) and $_[0] =~ /.*print.*/);
say;
# who_android_system;
who_android_service('ril','radio','modem');
# explore_pid('ril');
for(@resp){
return 1 if(/^OK\s.*/);
}
return 1 if($OK =~ /^OK\s.*/);
return 0;
}
sub at_rr
{
my $com = shift;
init_line_p;
at($com);
usleep(1000 * $RATE);
my @resp = rr($com);
return @resp;
}
sub chk_trap
{
my @response = ();
my $at_status = 0;
return 0 unless(defined $TRAP_QUERY_COMMAND && defined $TRAP_VALUE);
my $at_status = at($TRAP_QUERY_COMMAND, 'silent');
my @response = rr($TRAP_QUERY_COMMAND, 'silent');
$TRAP_QUERY_COMMAND_SENT_COUNT++;
for(@response){
return 1 if(/$TRAP_VALUE/);
}
++$TRAP_TRIGGER_COUNT;
return 0;
}
sub learn_set_objects
{
my @gsieved = @_;
say "--\n@gsieved\n--\n" if $DEBUG;
truncate $LOG_SET_OK_OBJECTS, 0;
truncate $LOG_SET_ERROR_OBJECTS, 0;
truncate $LOG_SET_REST_OBJECTS, 0;
my $com = '';
my @response = ();
say "learning set objects ...";
if(scalar @gsieved < 12){
say "gsieved contents: { @gsieved }" if $DEBUG;
}
for(@gsieved){
my $com = $_;
next if(grep(/^$com$/, @AT_Blist));
$com =~ s/"/\\"/g;
$com =~ s/'/\\'/g;
$com =~ s/\$/\\\$/g;
$com =~ s/\^/\\\^/g;
my $aOK = 0;
my $aERROR = 0;
$com = "$_" . '?';
at($com);
@response = rr("$_".'?');
for(@response){
if(/.*ERROR.*/){
$aERROR = 1;
}elsif(/^OK/){
$aOK = 1;
}
}
if($aOK){
log_response(\@response, $LOG_SET_OK_OBJECTS, $com);
}elsif($aERROR){
log_response(\@response, $LOG_SET_ERROR_OBJECTS, $com);
}else{
log_response(\@response, $LOG_SET_REST_OBJECTS, $com);
}
}
say " $LOG_SET_OK_OBJECTS";
say " $LOG_SET_ERROR_OBJECTS";
say " $LOG_SET_REST_OBJECTS";
}
sub learn_all_objects
{
my @gsieved = @_;
say "--\n@gsieved\n--\n" if $DEBUG;
truncate $LOG_ALL_OK_OBJECTS, 0;
truncate $LOG_ALL_ERROR_OBJECTS, 0;
truncate $LOG_ALL_REST_OBJECTS, 0;
truncate $LOG_OPTIONS, 0;
truncate $LOG_OPTIONS_E, 0;
my $como = '';
my @response = ();
say "$ME:" . (caller(0))[3] . " ..." if $DEBUG;
for(@gsieved){
my $quoted_command = quotemeta($_);
say "$ME:" . (caller(0))[3] . " quoted_command: $quoted_command" if $DEBUG;
next if(grep(/^$quoted_command$/, @AT_Blist));
my $sieved_com = $_;
at_log_response($LOG_OPTIONS_E, "$sieved_com");
at_log_response($LOG_OPTIONS_E, "${sieved_com}?");
at_log_response($LOG_OPTIONS_E, "$sieved_com=?", 1);
open my $fh, '>>', $LOG_OPTIONS_E or die "$ME:" . (caller(0))[3] . " can't append to $LOG_OPTIONS_E :\n $!\n";
print $fh "\n";
close $fh;
my $aOK = 0;
my $aERROR = 0;
my $sieved_com = $_;
say "\t \$_:$_" if $DEBUG;
$como = "$_" . '=?';
at($como);
@response = rr($como);
for(@response){
if(/^ERROR/){
$aERROR = 1;
}elsif(/^OK/){
$aOK = 1;
}
if(/^\Q$sieved_com\E:+\s+\(/){
my @fields = split /:/, $_;
say '-' x 6 . "\t $_" if $DEBUG;
open my $fh, '>>', $LOG_OPTIONS or die "$ME:" . (caller(0))[3] . " can't append to $LOG_OPTIONS :\n $!\n";
print $fh "$fields[0] ".$DLM." ";
print $fh "$fields[1]\n";
close $fh;
open my $fh, '>>', $LOG_OPTIONS_E or die "$ME:" . (caller(0))[3] . " can't append to $LOG_OPTIONS_E :\n $!\n";
print $fh "\n$fields[0] ".$DLM." ";
print $fh "$fields[1]\n";
close $fh;
}else{
print "\t else: $_ \n" if $DEBUG;
}
}
if($aOK){
log_response(\@response, $LOG_ALL_OK_OBJECTS, $como);
}elsif($aERROR){
log_response(\@response, $LOG_ALL_ERROR_OBJECTS, "$sieved_com , $como");
}else{
log_response(\@response, $LOG_ALL_REST_OBJECTS, $como);
}
}
say "$ME:" . (caller(0))[3] . " relevant log files";
say "$LOG_ALL_OK_OBJECTS";
say "$LOG_ALL_ERROR_OBJECTS";
say "$LOG_ALL_REST_OBJECTS";
say "$LOG_OPTIONS";
say "$LOG_OPTIONS_E";
}
sub print_sieved
{
our @gsieved;
print '-' x 12 . "\n";
say $_ for(@gsieved);
print '-' x 12 . "\n";
}