310 |
my $path = "$program_path/$tag"; |
my $path = "$program_path/$tag"; |
311 |
|
|
312 |
my $data = read_file( $path ); |
my $data = read_file( $path ); |
313 |
|
my $hex_data; |
314 |
|
|
315 |
$data = substr($data,0,16); |
if ( $data =~ s{^hex\s+}{} ) { |
316 |
|
$hex_data = $data; |
317 |
|
$hex_data =~ s{\s+}{}g; |
318 |
|
} else { |
319 |
|
|
320 |
my $hex_data = unpack('H*', $data) . ' 00' x ( 16 - length($data) ); |
# pad to block size |
321 |
|
$data .= "\0" x ( 4 - ( length($data) % 4 ) ); |
322 |
|
|
323 |
print "write_tag $tag = ",dump( $data ), " == $hex_data\n"; |
my $max_len = 7 * 4; |
324 |
|
|
325 |
|
if ( length($data) > $max_len ) { |
326 |
|
$data = substr($data,0,$max_len); |
327 |
|
warn "strip content to $max_len bytes\n"; |
328 |
|
} |
329 |
|
|
330 |
|
$hex_data = unpack('H*', $data); |
331 |
|
} |
332 |
|
|
333 |
|
my $len = length($hex_data) / 2; |
334 |
|
my $blocks = sprintf('%02x', $len / 4); |
335 |
|
|
336 |
|
print "write_tag $tag = ",dump( $data ), " [$len/$blocks] == $hex_data\n"; |
337 |
|
|
338 |
cmd( |
cmd( |
339 |
"d6 00 26 04 $tag 00 06 00 04 11 00 01 $hex_data 00 00 00 00 fd3b", "write $tag", |
"d6 00 ff 04 $tag 00 $blocks 00 $hex_data ffff", "write $tag", |
340 |
"d6 00 0d 04 00 $tag 06 afb1", sub { assert() }, |
"d6 00 0d 04 00 $tag $blocks afb1", sub { assert() }, |
341 |
) foreach ( 1 .. 3 ); # xxx 3m software does this three times! |
) foreach ( 1 .. 3 ); # xxx 3m software does this three times! |
342 |
|
|
343 |
my $to = $path; |
my $to = $path; |
398 |
sub writechunk |
sub writechunk |
399 |
{ |
{ |
400 |
my $str=shift; |
my $str=shift; |
401 |
|
warn "DEBUG: ", as_hex($str); |
402 |
my $count = $port->write($str); |
my $count = $port->write($str); |
403 |
|
my $len = length($str); |
404 |
|
die "wrong write length $count != $len in ",as_hex( $str ) if $count != $len; |
405 |
print "#> ", as_hex( $str ), "\t[$count]\n" if $debug; |
print "#> ", as_hex( $str ), "\t[$count]\n" if $debug; |
406 |
} |
} |
407 |
|
|
468 |
sub checksum { |
sub checksum { |
469 |
my ( $bytes, $checksum ) = @_; |
my ( $bytes, $checksum ) = @_; |
470 |
|
|
|
my $xor = crcccitt( substr($bytes,1) ); # skip D6 |
|
|
warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug; |
|
|
|
|
471 |
my $len = ord(substr($bytes,2,1)); |
my $len = ord(substr($bytes,2,1)); |
472 |
my $len_real = length($bytes) - 1; |
my $len_real = length($bytes) - 1; |
473 |
|
|
474 |
if ( $len_real != $len ) { |
if ( $len_real != $len ) { |
475 |
print "length wrong: $len_real != $len\n"; |
print "length wrong: $len_real != $len\n"; |
476 |
$bytes = substr($bytes,0,2) . chr($len_real) . substr($bytes,4); |
$bytes = substr($bytes,0,2) . chr($len_real) . substr($bytes,3); |
477 |
} |
} |
478 |
|
|
479 |
|
my $xor = crcccitt( substr($bytes,1) ); # skip D6 |
480 |
|
warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug; |
481 |
|
|
482 |
if ( defined $checksum && $xor ne $checksum ) { |
if ( defined $checksum && $xor ne $checksum ) { |
483 |
print "checksum doesn't match: ", as_hex($xor), " != ", as_hex($checksum), " data: ", as_hex($bytes), "\n"; |
print "checksum doesn't match: ", as_hex($xor), " != ", as_hex($checksum), " data: ", as_hex($bytes), "\n"; |
484 |
return $bytes . $xor; |
return $bytes . $xor; |