]> www.fi.muni.cz Git - slotcarman.git/blobdiff - SCX/Reader.pm
Proof-of-concept GUI
[slotcarman.git] / SCX / Reader.pm
index 1b7c44f40203c49f2833b1816feda3ee46d2f4d0..5a98a6d281ca7c22e97c444cddab0885003b1caf 100644 (file)
@@ -82,13 +82,23 @@ sub read {
 
        my @bad_bytes;
 
-       while (@bytes >= $PACKET_SIZE) {
-               if ($bytes[0] != 0x55 || $bytes[9] != 0x05
-                       || SCX::CRC::digest(@bytes[0..7]) != $bytes[8]) {
+       while (@bytes >= 2) {
+               if ($bytes[0] != 0x55) {
                        push @bad_bytes, shift @bytes;
                        next;
                }
+               my $cmd = $bytes[1];
 
+               my $packet_size = $cmd >= 0x40 && $cmd <= 0x46 ? 4 : 9;
+               last if @bytes <= $packet_size;
+
+               if ($bytes[$packet_size] != 0x05
+                       || SCX::CRC::digest(@bytes[0..$packet_size-2])
+                               != $bytes[$packet_size-1]) {
+                       push @bad_bytes, shift @bytes;
+                       next;
+               }
+               
                if (@bad_bytes) {
                        $self->{logfh}->print("Cannot parse bytes",
                                (map { sprintf(' %02x', $_) } @bad_bytes),
@@ -97,8 +107,8 @@ sub read {
                }
 
                $self->{logfh}->print("Callback\n");
-               &{ $self->{callback} }(@bytes[1..7]);
-               splice @bytes, 0, 10;
+               &{ $self->{callback} }(@bytes[1..$packet_size]);
+               splice @bytes, 0, $packet_size+1;
        }
        if (@bad_bytes) {
                $self->{logfh}->print("Cannot parse bytes",