correct crc calculation and escaping
[huawei.git] / qcdm.pl
diff --git a/qcdm.pl b/qcdm.pl
index 472e4b1..21a1673 100755 (executable)
--- a/qcdm.pl
+++ b/qcdm.pl
@@ -46,7 +46,7 @@ my @crc_table = (
 sub crc {
        my $crc = 0xffff;
        foreach my $byte ( split(//, join('',@_)) ) {
 sub crc {
        my $crc = 0xffff;
        foreach my $byte ( split(//, join('',@_)) ) {
-               $crc = ($crc >> 8) ^ $crc_table[ ($crc ^ $byte) & 0xff ];
+               $crc = ($crc >> 8) ^ $crc_table[ ($crc ^ ord($byte)) & 0xff ];
        }
        return $crc ^ 0xffff;
 }
        }
        return $crc ^ 0xffff;
 }
@@ -121,8 +121,8 @@ sub diag {
 
        my $crc = pack('v', crc($data) );
 
 
        my $crc = pack('v', crc($data) );
 
-#      $data =~ s/\x7d/\x7d\x5d/;
-#      $data =~ s/\x7e/\x7d\x5e/;
+       $data =~ s/\x7d/\x7d\x5d/gs;
+       $data =~ s/\x7e/\x7d\x5e/gs;
 
        my $out = $data . $crc . "\x7e";
 
 
        my $out = $data . $crc . "\x7e";
 
@@ -139,20 +139,22 @@ sub block_read {
 
        my $ret = $port->read(1);
        while ( my $c = $port->input ) {
 
        my $ret = $port->read(1);
        while ( my $c = $port->input ) {
-               print STDERR "<<< ",dump($ret . $c), $/;
+               print STDERR "<<< ",as_hex($ret . $c), $/;
                $ret .= $c;
 #              last if $c =~ m/^OK\r/;
        }
                $ret .= $c;
 #              last if $c =~ m/^OK\r/;
        }
+       $ret =~ s/\x7d\x5d/\x7d/gs;
+       $ret =~ s/\x7d\x5e/\x7e/gs;
        return $ret;
 }
 
 diag 'GET_VERSION';
 
        return $ret;
 }
 
 diag 'GET_VERSION';
 
-$port->close || die "can't close port $!";
+diag 'GET_SERIAL_NUMBER';
 
 
-while ( block_read() ) {
-       warn "# more";
-}
+diag 'EXTENDED_BUILD_ID';
+
+$port->close || die "can't close port $!";
 
 warn "## $device closed\n";
 
 
 warn "## $device closed\n";