correctly parse Upstream return write parameter frame
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 4 Oct 2020 08:21:58 +0000 (10:21 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sun, 4 Oct 2020 08:21:58 +0000 (10:21 +0200)
Protocol.pm

index 829b7f0..3ec521d 100644 (file)
@@ -210,12 +210,16 @@ sub protocol_decode {
 
                } elsif ( $up_down eq 'down' && $function_code == 0x03 ) {
                        # type A
+                       # 0x03 => 'Downstream read parameter frame',
 
                        $data_range = substr($data,0,1);
                        $data = substr($data,1);
 
                } elsif ( $up_down eq 'up'   && ( $function_code == 0x07 || $function_code == 0x08 || $function_code == 0x03 ) ) {
                        # type B
+                       # 0x07 => 'Upstream Heartbeat Frame',
+                       # 0x08 => 'Upstream alarm frame',
+                       # 0x03 => 'Upstream Read Parameter Frame',
 
                        $data_len = unpack('C', substr($data,1,1));
                        $data_range = substr($data,2, $data_len);
@@ -224,6 +228,7 @@ sub protocol_decode {
 
                } elsif ( $up_down eq 'down' && $function_code == 0x06 ) {
                        # type B
+                       # 0x06 => 'Downstream write parameter frame',
 
                        $data_len = unpack('C', substr($data,1,1));
                        $data_range = substr($data,2, $data_len);
@@ -232,15 +237,21 @@ sub protocol_decode {
 
                } elsif ( $up_down eq 'up' && $function_code == 0x06 ) {
                        # type C
+                       # 0x06 => 'Upstream return write parameter frame',
 
-                       # FIXME check first byte?
-                       my $data_range = substr($data,1,1);
+                       $data_len = unpack('C', substr($data,1,1));
+                       $data_range = substr($data,2, $data_len);
+                       if ( $data_len == 1 ) {
+                               # XXX return is OK/not OK
+                               $pack_fmt = 'C';
+                       }
 
-                       $data = substr($data,2);
+                       $data = substr($data,2 + $data_len);
 
                } else {
                        $hash->{error}->{function_code_unknown} = $function_code;
-                       warn "ERROR unknown function_code = $function_code";
+                       print STDERR "ERROR unknown function_code = $function_code\n";
+                       return $hash;
                }
 
                my @v = unpack($pack_fmt, $data_range);