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),
}
$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",