X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=SCX%2FReader.pm;h=5a98a6d281ca7c22e97c444cddab0885003b1caf;hb=68ff3ebd0073097de84fc55b22047cb5b9d98db7;hp=1b7c44f40203c49f2833b1816feda3ee46d2f4d0;hpb=09f6bee9b34515e38dcf40a5e56b3bd327f7ba7e;p=slotcarman.git diff --git a/SCX/Reader.pm b/SCX/Reader.pm index 1b7c44f..5a98a6d 100644 --- a/SCX/Reader.pm +++ b/SCX/Reader.pm @@ -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",