use Protocol.pm
[zc] / unpack.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Data::Dump qw(dump);
6
7 use lib '.';
8 use Protocol;
9
10 my $debug = $ENV{DEBUG};
11
12 warn "## protocol = ",dump( $protocol ) if $debug;
13
14 my $pkg_nr = 0;
15
16 sub protocol_decode {
17         my ( $up_down, $hex ) = @_;
18
19 warn "up/down: $up_down hex = ",dump($hex);
20
21 my $bin = join('', map { chr(hex($_)) } split(/\s+/,$hex));
22
23 warn "bin = ", dump($bin);
24
25 if ( $debug ) {
26         open(my $dump, '>', "/dev/shm/zc.$pkg_nr.$up_down");
27         print $dump $bin;
28         close($dump);
29         $pkg_nr++;
30 }
31
32 my $cksum = substr($bin, -2, 2);
33
34 if ( my $crc = modbus_crc16( substr($bin,0,-2) ) ) {
35         if ( $crc ne $cksum ) {
36                 warn "CRC error, got ",unpack('H*',$crc), " expected ", unpack('H*',$cksum), "\n";
37         } else {
38                 warn "CRC OK ", unpack('H*',$crc), "\n";
39         }
40 }
41
42 my ( $header, $ver, $function_code, $len ) = unpack("CCCv", $bin);
43
44 my $data = substr($bin,5,-2);
45
46 warn "header = $header ver = $ver function_code = $function_code len = $len == ",length($data), " data = ",dump($data), " cksum = ", unpack('H*',$cksum);
47
48 warn "header corrupt: $header" if $header != 0x5a;
49 warn "invalid length $len got ",length($data) if length($data) != $len;
50
51 my $function_code_upstream = {
52 0x07 => 'Upstream Heartbeat Frame',
53 0x08 => 'Upstream alarm frame',
54 0x03 => 'Upstream Read Parameter Frame',
55 0x06 => 'Upstream return write parameter frame',
56 };
57
58 my $function_code_downstream = {
59 0x03 => 'Downstream read parameter frame',
60 0x06 => 'Downstream write parameter frame',
61 };
62
63 print "Function code: $function_code ", 
64         $up_down eq 'up' ? $function_code_upstream->{ $function_code } : $function_code_downstream->{ $function_code }, "\n";
65
66
67
68
69 while ( $data ) {
70         my $hex = unpack('H*', $data);
71         $hex =~ s/(..)/$1 /g;
72         warn "XXX data = $hex\n";
73
74         my $data_id = unpack( 'C', substr($data,0,1) );
75         if ( ! exists( $protocol->{$data_id}->{description} ) ) {
76                 my $len = unpack('C', substr($data,1,1));
77                 printf "ERROR: no description for data_id %d 0x%2x len %d SKIPPING!\n", $data_id, $data_id, $len;
78                 $data = substr($data,2 + $len);
79                 next;
80         }
81         my $data_id_desc = $protocol->{$data_id}->{description};
82         my $pack_fmt = $protocol->{$data_id}->{pack_fmt} || die "can't find pack_fmt for data_id $data_id";
83
84         my $data_range;
85
86         if ( $data_id == 0x00 ) {
87                 # sequence number
88
89                 $data_range = substr($data,2,4);
90                 $data = substr($data,6);
91
92         } elsif ( $up_down eq 'down' && $function_code == 0x03 ) {
93                 # type A
94
95                 $data_range = substr($data,0,1);
96                 $data = substr($data,1);
97
98         } elsif ( $up_down eq 'up'   && ( $function_code == 0x07 || $function_code == 0x08 || $function_code == 0x03 ) ) {
99                 # type B
100
101                 my $data_len = unpack('C', substr($data,1,1));
102                 $data_range = substr($data,2, $data_len);
103
104                 $data = substr($data,2 + $data_len);
105
106         } elsif ( $up_down eq 'down' && $function_code == 0x06 ) {
107                 # type B
108
109                 my $data_len = unpack('C', substr($data,1,1));
110                 $data_range = substr($data,2, $data_len);
111
112                 $data = substr($data,2 + $data_len);
113
114         } elsif ( $up_down eq 'up' && $function_code == 0x06 ) {
115                 # type C
116
117                 # FIXME check first byte?
118                 my $data_range = substr($data,1,1);
119
120                 $data = substr($data,2);
121
122         } else {
123                 warn "ERROR unknown function_code = $function_code";
124         }
125
126         my $v = unpack($pack_fmt, $data_range);
127
128         if ( $data_id == 0x0c ) {
129                 $v = $v / 100;
130         } elsif ( $data_id == 0x0d ) {
131                 $v = $v / 100;
132         }
133
134         my $description = $protocol->{$data_id}->{description} || die "can't find description for data_id $data_id";
135         print "$up_down | $data_id | $description | v=$v | hex:", unpack('H*', $data_range), " fmt:$pack_fmt | range:",$protocol->{$data_id}->{range}, " | remark:", $protocol->{$data_id}->{remark},"\n";
136 }
137
138
139 } # protocol_decode
140
141 my $raw;
142 my ( $timestamp, $sensor, $imei, $up_down );
143 my $stat;
144 while(<>) {
145         chomp;
146
147         if ( m{^(\d\d\d\d-\d\d-\d\d\s\d\d:\d\d:\d\d).*Inclinometer/([^/]+)/([^/]+)/(\w+)} ) {
148                 if ( $raw ) {
149                         $raw =~ s{^\s+}{}; # strip leading spaces
150                         print "## $timestamp $sensor $imei $up_down $raw\n";
151                         protocol_decode( $up_down, $raw );
152                         $raw = '';
153                 }
154                 ( $timestamp, $sensor, $imei, $up_down ) = ( $1, $2, $3, $4 );
155                 $stat->{$sensor}->{$imei}->{$up_down}++;
156         } elsif ( m{^\s+} ) {
157                 s/\s\s\S\S\S+//g; # remove ascii
158                 s/^\s+/ /;
159                 $raw .= $_;
160                 warn "++ $_\n";
161         } else {
162                 warn "IGNORE: $_\n";
163         }
164
165 }
166
167 if ( $raw ) {
168         $raw =~ s{^\s+}{}; # strip leading spaces
169         print "## $timestamp $sensor $imei $up_down $raw\n";
170         protocol_decode( $up_down, $raw );
171 }
172
173 print "stat = ",dump($stat), "\n";
174