-
Notifications
You must be signed in to change notification settings - Fork 1
/
enc4t_gp2_to_raw4t_gp2.pl
executable file
·137 lines (115 loc) · 5.08 KB
/
enc4t_gp2_to_raw4t_gp2.pl
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
#!/usr/bin/perl -w
# by Matija Nalis <mnalis-android@voyager.hr> GPLv3+, started 2014-06-22
# parse .gp2 container having new GSD4t binary data (double-CRC one) and run checks on it
#
# Usage: ./strace_to_gp2.pl data/strace4/strace.log.3491 | ./enc4t_gp2_to_raw4t_gp2.pl
#
use strict;
use autodie;
use Digest::CRC;
my $DEBUG = 3;
$|=1;
sub crc16($)
{
my ($input) = @_;
my $ctx = Digest::CRC->new(width=>16, init=>0xffff, refout=>1, poly=>0x8005, refin=>1, xorout=>0x0000, cont=>0); # params for crc16-modbus
$input =~ s/\s+//g;
my $bin = pack 'H*', $input; # convert ASCII HEX values to raw binary
$ctx->add($bin);
my $ret = uc($ctx->hexdigest);
print " calculating crc16_modbus($input) = 0x$ret\n" if $DEBUG > 8;
return hex($ret);
}
# format is like:
# 21/06/2014 00:02:23.287 (0) A0 A2 00 03 E4 00 9F 00 14 04 5B 8E 02 03 01 43 03 71 00 00 00 00 00 00 00 00 00 00 00 00 00 F7 C9 B0 B3
#
# A0 A2 -- lead-in
# 00 -- always zero?
# 03E4 -- sequence1
# 009F -- sequence2
# 0014 -- length of payload (length of full packet minus 0xf [full size of all headers]
# 045B -- CRC16 (modbus) of headers
# 8E 02 03 01 43 03 71 00 00 00 00 00 00 00 00 00 00 00 00 00 -- actual payload (FIXME - what is in it?)
# F7 C9 -- CRC16 (modbus) of payload
# B0 B3 -- lead-out
my $last_seq1 = undef;
my $last_seq2 = undef;
my $last_length = -1;
while (<>) {
next if /^\s*$/; # skip empty lines
next if /^\s*#/; # skip comment lines
if (m{^(\d{2}/\d{2}/\d{4}) (\d{2}:\d{2}:\d{2})(\.\d{3}) \(0\) A0 A2 ([A-F0-9 ]+) B0 B3\s*}) {
print "raw: $_" if $DEBUG > 8;
my $date = $1; my $time = $2; my $msec=$3;
my @data = split ' ', $4;
my $extra = '';
#### byte 0 (always 00) ####
my $p_lead_zero = shift @data;
if ($p_lead_zero ne '00') { warn "# WARNING: leading zero not 00 but $p_lead_zero in $_"; $extra .= " (WARNING: leading zero was not 00 but $p_lead_zero)" }
print " leading 00 -- OK\n" if $DEBUG > 7;
#### byte 1-2 (sequence1) ####
my $p_seq1 = (shift @data) . (shift @data);
#### byte 3-4 (sequence2) ####
my $p_seq2 = (shift @data) . (shift @data);
# FIXME see what is it for? grouping of commands/session?
my $seq2 = hex($p_seq2);
if (defined $last_seq2) {
if ( ($seq2 == $last_seq2 + 1) or # must be new=last+1 or...
($seq2 == $last_seq2) or # ... new=last
( ($seq2 == 0) and ($last_seq2 == 65535) ) # allow wraparound at 0xffff to 0x0000
) {} else {
warn "# WARNING: last seq2 was $last_seq2, didn't expect $seq2 in $_";
}
}
$last_seq2 ||= 'undef'; print " seq2 $last_seq2 + 0/1 = $seq2 (0x$p_seq2) -- OK\n" if $DEBUG > 7;
$last_seq2 = $seq2;
#### byte 5-6 (payload length) ####
# FIXME: sometimes wrong on bigger packets?? check
my $p_length = (shift @data) . (shift @data);
my $length = hex($p_length);
print " payload length = $length (0x$p_length)\n" if $DEBUG > 7;
#### sequence1 continue checks ####
my $seq1 = hex($p_seq1);
if (defined $last_seq1) {
if ( ($seq1 == $last_seq1 + 1) or # must be new=last+1
( ($seq1 == 0) and ($last_seq1 == 65535) ) or # allow wraparound at 0xffff to 0x0000
( ($last_length == 0) and ($seq1 == $last_seq1) ) # may be new=last if zero-lenght packet
) {} else {
warn "# WARNING: last seq1 was $last_seq1, didn't expect $seq1 in ($last_length) $_";
}
}
$last_seq1 ||= 'undef'; print " seq1 $last_seq1 + 1 = $seq1 (0x$p_seq1) -- OK\n" if $DEBUG > 7;
$last_seq1 = $seq1;
#### byte 7-8 (header CRC-16) ####
my $p_crc_head = (shift @data) . (shift @data);
my $crc_head = hex($p_crc_head);
print " header CRC16 = $crc_head (0x$p_crc_head)\n" if $DEBUG > 7;
my $crc_head_verify = crc16("$p_lead_zero $p_seq1 $p_seq2 $p_length");
if ($crc_head != $crc_head_verify) { warn "# WARNING: CRC header mismatch $crc_head != $crc_head_verify in $_"; next }
#### byte 9-xxx (actual payload) ####
my $p_payload = '';
my $cnt = $length;
while ($cnt--) {
my $byte = shift @data;
if (!defined $byte) { die "not enough payload: need $cnt more in $_" } # FIXME: dies sometimes (see gsd4t_logs/strace4/strace.log.3491.parsed.txt)
$p_payload .= "$byte ";
}
print " payload ==> $p_payload\n" if $DEBUG > 7;
#### byte xxx+1 and xxx+1 (payload CRC16) ####
my $p_crc_payload = (shift @data) . (shift @data);
my $crc_payload = hex($p_crc_payload);
print " payload CRC16 = $crc_payload (0x$p_crc_payload)\n" if $DEBUG > 7;
my $crc_payload_verify = crc16($p_payload);
if ($crc_payload != $crc_payload_verify) { die "CRC payload mismatch $crc_payload != $crc_payload_verify in $_" }
#### end of packet ####
if (@data) { die "done processing packet, but there is still data @data remaining in $_" }
print "--end packet--\n\n" if $DEBUG > 7;
$last_length = $length;
print "00/00/0006 $time$msec (0) ${p_payload}";
print "\t# seq1=$p_seq1 seq2=$p_seq2 len=$p_length$extra" if $DEBUG > 1;
print "\n";
} else {
die "unknown format for line: $_";
}
#die; # FIXME - test just first line
}