91 |
|
|
92 |
# initial hand-shake with device |
# initial hand-shake with device |
93 |
|
|
94 |
cmd( 'D5 00 05 04 00 11 8C66', 'hw version?', |
cmd( 'D5 00 05 04 00 11 8C66', 'hw version', |
95 |
'D5 00 09 04 00 11 0A 05 00 02 7250', 'hw 10.5.0.2', sub { |
'D5 00 09 04 00 11 0A 05 00 02 7250', sub { |
96 |
print "hardware version ", join('.', unpack('CCCC', skip_assert(3) )), "\n"; |
print "hardware version ", join('.', unpack('CCCC', skip_assert(3) )), "\n"; |
97 |
}); |
}); |
98 |
|
|
99 |
cmd( 'D6 00 0C 13 04 01 00 02 00 03 00 04 00 AAF2','stats?', |
cmd( 'D6 00 0C 13 04 01 00 02 00 03 00 04 00 AAF2','FIXME: stats?', |
100 |
'D6 00 0C 13 00 02 01 01 03 02 02 03 00 E778','FIXME: unimplemented', sub { assert() } ); |
'D6 00 0C 13 00 02 01 01 03 02 02 03 00 E778', sub { assert() } ); |
101 |
|
|
102 |
# start scanning for tags |
# start scanning for tags |
103 |
|
|
104 |
cmd( 'D6 00 05 FE 00 05 FA40', "XXX scan $_", |
cmd( 'D6 00 05 FE 00 05 FA40', "scan for tags, retry $_", |
105 |
'D6 00 07 FE 00 00 05 00 C97B', 'no tag', sub { |
'D6 00 07 FE 00 00 05 00 C97B', sub { |
106 |
dispatch( |
assert(); |
107 |
'D6 00 0F FE 00 00 05 ',# 01 E00401003123AA26 941A # seen, serial length: 8 |
print "no tag in range\n"; |
108 |
sub { |
|
109 |
my $rest = shift || die "no rest?"; |
}, |
110 |
my $nr = ord( substr( $rest, 0, 1 ) ); |
'D6 00 0F FE 00 00 05 ', sub { # 01 E00401003123AA26 941A # seen, serial length: 8 |
111 |
|
my $rest = shift || die "no rest?"; |
112 |
|
my $nr = ord( substr( $rest, 0, 1 ) ); |
113 |
|
|
114 |
|
if ( ! $nr ) { |
115 |
|
print "no tags in range\n"; |
116 |
|
} else { |
117 |
|
|
118 |
my $tags = substr( $rest, 1 ); |
my $tags = substr( $rest, 1 ); |
119 |
|
|
120 |
my $tl = length( $tags ); |
my $tl = length( $tags ); |
129 |
read_tag( $_ ) foreach @tags; |
read_tag( $_ ) foreach @tags; |
130 |
|
|
131 |
} |
} |
132 |
) } |
} |
|
|
|
133 |
) foreach ( 1 .. 100 ); |
) foreach ( 1 .. 100 ); |
134 |
|
|
135 |
my $read_cached; |
my $read_cached; |
137 |
sub read_tag { |
sub read_tag { |
138 |
my ( $tag ) = @_; |
my ( $tag ) = @_; |
139 |
|
|
|
print "read_tag $tag\n"; |
|
140 |
return if $read_cached->{ $tag }++; |
return if $read_cached->{ $tag }++; |
141 |
|
|
142 |
|
print "read_tag $tag\n"; |
143 |
|
|
144 |
cmd( "D6 00 0D 02 $tag 00 03 1CC4", 'read $tag offset: 0 blocks: 3', |
cmd( |
145 |
"D6 00 0F FE 00 00 05 01 $tag 941A", "$tag ready?", sub { |
"D6 00 0D 02 $tag 00 03 1CC4", 'read $tag offset: 0 blocks: 3', |
146 |
dispatch( "D6 00 1F 02 00 $tag ", sub { # 03 00 00 04 11 00 01 01 00 31 32 33 34 02 00 35 36 37 38 531F\n"; |
"D6 00 0F FE 00 00 05 01 $tag 941A", sub { |
147 |
|
print "FIXME: tag $tag ready?\n"; |
148 |
|
}, |
149 |
|
"D6 00 1F 02 00", sub { # $tag 03 00 00 04 11 00 01 01 00 31 32 33 34 02 00 35 36 37 38 531F\n"; |
150 |
my $rest = shift || die "no rest?"; |
my $rest = shift || die "no rest?"; |
151 |
warn "## DATA ", dump( $rest ) if $debug; |
warn "## DATA ", dump( $rest ) if $debug; |
152 |
my $blocks = ord(substr($rest,0,1)); |
my $tag = uc(unpack('H16',substr( $rest, 0, 8 ))); |
153 |
|
my $blocks = ord(substr($rest,8,1)); |
154 |
|
$rest = substr($rest,9); # leave just data blocks |
155 |
my @data; |
my @data; |
156 |
foreach my $nr ( 0 .. $blocks - 1 ) { |
foreach my $nr ( 0 .. $blocks - 1 ) { |
157 |
my $block = substr( $rest, 1 + $nr * 6, 6 ); |
my $block = substr( $rest, $nr * 6, 6 ); |
158 |
warn "## block ",as_hex( $block ) if $debug; |
warn "## block ",as_hex( $block ) if $debug; |
159 |
my $ord = unpack('v',substr( $block, 0, 2 )); |
my $ord = unpack('v',substr( $block, 0, 2 )); |
160 |
die "got block $ord, expected block $nr from ",dump( $block ) if $ord != $nr; |
die "got block $ord, expected block $nr from ",dump( $block ) if $ord != $nr; |
164 |
$data[ $ord ] = $data; |
$data[ $ord ] = $data; |
165 |
} |
} |
166 |
$read_cached->{ $tag } = join('', @data); |
$read_cached->{ $tag } = join('', @data); |
167 |
print "DATA $tag ",dump( $read_cached->{ $tag } ), "\n"; |
print "DATA $tag ",dump( $read_cached ), "\n"; |
168 |
}) |
} |
169 |
}); |
); |
170 |
|
|
171 |
# D6 00 1F 02 00 $tag 03 00 00 04 11 00 01 01 00 30 30 30 30 02 00 30 30 30 30 E5F4 |
# D6 00 1F 02 00 $tag 03 00 00 04 11 00 01 01 00 30 30 30 30 02 00 30 30 30 30 E5F4 |
172 |
if (0) { |
if (0) { |
260 |
return substr( $assert->{payload}, $to ); |
return substr( $assert->{payload}, $to ); |
261 |
} |
} |
262 |
|
|
|
our $dispatch; |
|
|
sub dispatch { |
|
|
my ( $pattern, $coderef ) = @_; |
|
|
|
|
|
$dispatch->{ $pattern } = $coderef; |
|
|
|
|
|
my $patt = substr( str2bytes($pattern), 3 ); # just payload |
|
|
my $l = length($patt); |
|
|
my $p = substr( $assert->{payload}, 0, $l ); |
|
|
warn "## dispatch pattern $pattern [$l] ",dump( $patt, $p ) if $debug; |
|
|
|
|
|
if ( $assert->{payload} eq $assert->{expect} ) { |
|
|
warn "## no dispatch, payload expected" if $debug; |
|
|
} elsif ( $p eq $patt ) { |
|
|
# if matched call with rest of payload |
|
|
$coderef->( substr( $assert->{payload}, $l ) ); |
|
|
} else { |
|
|
warn "## dispatch ignored" if $debug; |
|
|
} |
|
|
} |
|
|
|
|
263 |
use Digest::CRC; |
use Digest::CRC; |
264 |
|
|
265 |
sub crcccitt { |
sub crcccitt { |
295 |
return $bytes . $checksum; |
return $bytes . $checksum; |
296 |
} |
} |
297 |
|
|
298 |
sub readchunk { |
our $dispatch; |
|
my ( $parser ) = @_; |
|
299 |
|
|
300 |
|
sub readchunk { |
301 |
sleep 1; # FIXME remove |
sleep 1; # FIXME remove |
302 |
|
|
303 |
# read header of packet |
# read header of packet |
311 |
warn "## payload too short $payload_len != $len\n" if $payload_len != $len; |
warn "## payload too short $payload_len != $len\n" if $payload_len != $len; |
312 |
|
|
313 |
my $checksum = substr( $data, -2, 2 ); |
my $checksum = substr( $data, -2, 2 ); |
314 |
checksum( $header . $length . $payload, $checksum ); |
checksum( $header . $length . $payload , $checksum ); |
315 |
|
|
316 |
print "<< ",as_hex( $header ), " [$len] ", as_hex( $payload ), " | sum: ",as_hex($checksum),"\n"; |
print "<< ",as_hex( $header ), " [$len] ", as_hex( $payload ), " | sum: ",as_hex($checksum),"\n"; |
317 |
|
|
318 |
$assert->{len} = $len; |
$assert->{len} = $len; |
319 |
$assert->{payload} = $payload; |
$assert->{payload} = $payload; |
320 |
|
|
321 |
$parser->( $len, $payload ) if $parser && ref($parser) eq 'CODE'; |
my $full = $header . $length . $data; # full |
322 |
|
# find longest match for incomming data |
323 |
|
my ($to) = grep { |
324 |
|
my $match = substr($payload,0,length($_)); |
325 |
|
m/^\Q$match\E/ |
326 |
|
} sort { length($a) <=> length($b) } keys %$dispatch; |
327 |
|
warn "?? payload dispatch to ",dump( $payload, $dispatch, $to ) if $debug; |
328 |
|
|
329 |
|
if ( defined $to ) { |
330 |
|
my $rest = substr( $payload, length($to) ); |
331 |
|
warn "## DISPATCH payload to with rest", dump( $payload, $to, $rest ) if $debug; |
332 |
|
$dispatch->{ $to }->( $rest ); |
333 |
|
} else { |
334 |
|
print "NO DISPATCH for ",dump( $full ),"\n"; |
335 |
|
} |
336 |
|
|
337 |
return $data; |
return $data; |
338 |
} |
} |
350 |
} |
} |
351 |
|
|
352 |
sub cmd { |
sub cmd { |
353 |
my ( $cmd, $cmd_desc, $expect, $expect_desc, $coderef ) = @_; |
my $cmd = shift || confess "no cmd?"; |
354 |
|
my $cmd_desc = shift || confess "no description?"; |
355 |
|
my @expect = @_; |
356 |
|
|
357 |
my $bytes = str2bytes( $cmd ); |
my $bytes = str2bytes( $cmd ); |
358 |
|
|
359 |
# fix checksum if needed |
# fix checksum if needed |
363 |
$assert->{send} = $cmd; |
$assert->{send} = $cmd; |
364 |
writechunk( $bytes ); |
writechunk( $bytes ); |
365 |
|
|
366 |
if ( $expect ) { |
while ( @expect ) { |
367 |
warn "?? $expect", $expect_desc ? "\t## $expect_desc" : '', "\n"; |
my $pattern = str2bytes( shift @expect ) || confess "no pattern?"; |
368 |
$assert->{expect} = substr(str2bytes($expect), 3, -2); # just expected payload |
my $coderef = shift @expect || confess "no coderef?"; |
369 |
readchunk( $coderef ); |
confess "not coderef" unless ref $coderef eq 'CODE'; |
370 |
|
|
371 |
|
next if defined $dispatch->{ $pattern }; |
372 |
|
|
373 |
|
$dispatch->{ substr($pattern,3) } = $coderef; |
374 |
|
warn "++ dispatch ", as_hex($pattern) ,dump( $dispatch ) if $debug; |
375 |
} |
} |
376 |
|
|
377 |
|
readchunk; |
378 |
} |
} |
379 |
|
|