30 |
}; |
}; |
31 |
|
|
32 |
GetOptions( |
GetOptions( |
33 |
'd|debug+' => \$debug, |
'd|debug+' => \$debug, |
34 |
'device=s' => \$device, |
'device=s' => \$device, |
35 |
'baudrate=i' => \$baudrate, |
'baudrate=i' => \$baudrate, |
36 |
'databits=i' => \$databits, |
'databits=i' => \$databits, |
39 |
'handshake=s' => \$handshake, |
'handshake=s' => \$handshake, |
40 |
) or die $!; |
) or die $!; |
41 |
|
|
42 |
|
my $verbose = $debug > 0 ? $debug-- : 0; |
43 |
|
|
44 |
=head1 NAME |
=head1 NAME |
45 |
|
|
46 |
3m-810 - support for 3M 810 RFID reader |
3m-810 - support for 3M 810 RFID reader |
80 |
$parity=$port->parity($parity); |
$parity=$port->parity($parity); |
81 |
$stopbits=$port->stopbits($stopbits); |
$stopbits=$port->stopbits($stopbits); |
82 |
|
|
83 |
print "## using $device $baudrate $databits $parity $stopbits\n"; |
print "## using $device $baudrate $databits $parity $stopbits debug: $debug verbose: $verbose\n"; |
84 |
|
|
85 |
# Just in case: reset our timing and buffers |
# Just in case: reset our timing and buffers |
86 |
$port->lookclear(); |
$port->lookclear(); |
93 |
|
|
94 |
# initial hand-shake with device |
# initial hand-shake with device |
95 |
|
|
96 |
cmd( 'D5 00 05 04 00 11 8C66', 'hw version?', |
cmd( 'D5 00 05 04 00 11 8C66', 'hw version', |
97 |
'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 { |
98 |
print "hardware version ", join('.', unpack('CCCC', skip_assert(3) )), "\n"; |
print "hardware version ", join('.', unpack('CCCC', skip_assert(3) )), "\n"; |
99 |
}); |
}); |
100 |
|
|
101 |
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?', |
102 |
'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() } ); |
103 |
|
|
104 |
# start scanning for tags |
# start scanning for tags |
105 |
|
|
106 |
cmd( 'D6 00 05 FE 00 05 FA40', "XXX scan $_", |
cmd( 'D6 00 05 FE 00 05 FA40', "scan for tags, retry $_", |
107 |
'D6 00 07 FE 00 00 05 00 C97B', 'no tag', sub { |
'D6 00 0F FE 00 00 05 ', sub { # 01 E00401003123AA26 941A # seen, serial length: 8 |
108 |
dispatch( |
my $rest = shift || die "no rest?"; |
109 |
'D6 00 0F FE 00 00 05 ',# 01 E00401003123AA26 941A # seen, serial length: 8 |
my $nr = ord( substr( $rest, 0, 1 ) ); |
110 |
sub { |
|
111 |
my $rest = shift || die "no rest?"; |
if ( ! $nr ) { |
112 |
my $nr = ord( substr( $rest, 0, 1 ) ); |
print "no tags in range\n"; |
113 |
|
update_visible_tags(); |
114 |
|
} else { |
115 |
|
|
116 |
my $tags = substr( $rest, 1 ); |
my $tags = substr( $rest, 1 ); |
117 |
|
|
118 |
my $tl = length( $tags ); |
my $tl = length( $tags ); |
121 |
my @tags; |
my @tags; |
122 |
push @tags, uc(unpack('H16', substr($tags, $_ * 8, 8))) foreach ( 0 .. $nr - 1 ); |
push @tags, uc(unpack('H16', substr($tags, $_ * 8, 8))) foreach ( 0 .. $nr - 1 ); |
123 |
warn "## tags ",as_hex($tags), " [$tl] = ",dump( $tags ) if $debug; |
warn "## tags ",as_hex($tags), " [$tl] = ",dump( $tags ) if $debug; |
124 |
print "seen $nr tags: ", join(',', @tags ) , "\n"; |
print "$nr tags in range: ", join(',', @tags ) , "\n"; |
125 |
|
|
126 |
# read data from tag |
update_visible_tags( @tags ); |
|
read_tag( $_ ) foreach @tags; |
|
127 |
|
|
128 |
} |
} |
129 |
) } |
} |
|
|
|
130 |
) foreach ( 1 .. 100 ); |
) foreach ( 1 .. 100 ); |
131 |
|
|
132 |
my $read_cached; |
|
133 |
|
|
134 |
|
my $tags_data; |
135 |
|
my $visible_tags; |
136 |
|
|
137 |
|
sub update_visible_tags { |
138 |
|
my @tags = @_; |
139 |
|
|
140 |
|
my $last_visible_tags = $visible_tags; |
141 |
|
$visible_tags = {}; |
142 |
|
|
143 |
|
foreach my $tag ( @tags ) { |
144 |
|
if ( ! defined $last_visible_tags->{$tag} ) { |
145 |
|
read_tag( $tag ); |
146 |
|
$visible_tags->{$tag}++; |
147 |
|
} else { |
148 |
|
warn "## using cached data for $tag" if $debug; |
149 |
|
} |
150 |
|
delete $last_visible_tags->{$tag}; # leave just missing tags |
151 |
|
} |
152 |
|
|
153 |
|
foreach my $tag ( keys %$last_visible_tags ) { |
154 |
|
print "removed tag $tag with data ",dump( delete $tags_data->{$tag} ),"\n"; |
155 |
|
} |
156 |
|
|
157 |
|
warn "## update_visible_tags(",dump( @tags ),") = ",dump( $visible_tags )," removed: ",dump( $last_visible_tags ), " data: ",dump( $tags_data ) if $debug; |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
sub read_tag { |
sub read_tag { |
162 |
my ( $tag ) = @_; |
my ( $tag ) = @_; |
163 |
|
|
164 |
|
confess "no tag?" unless $tag; |
165 |
|
|
166 |
|
return if defined $tags_data->{$tag}; |
167 |
|
|
168 |
print "read_tag $tag\n"; |
print "read_tag $tag\n"; |
|
return if $read_cached->{ $tag }++; |
|
169 |
|
|
170 |
cmd( "D6 00 0D 02 $tag 00 03 1CC4", 'read $tag offset: 0 blocks: 3', |
cmd( |
171 |
"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', |
172 |
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 { |
173 |
|
print "FIXME: tag $tag ready?\n"; |
174 |
|
}, |
175 |
|
"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"; |
176 |
my $rest = shift || die "no rest?"; |
my $rest = shift || die "no rest?"; |
177 |
warn "## DATA ", dump( $rest ) if $debug; |
warn "## DATA ", dump( $rest ) if $debug; |
178 |
my $blocks = ord(substr($rest,0,1)); |
my $tag = uc(unpack('H16',substr( $rest, 0, 8 ))); |
179 |
|
my $blocks = ord(substr($rest,8,1)); |
180 |
|
$rest = substr($rest,9); # leave just data blocks |
181 |
my @data; |
my @data; |
182 |
foreach my $nr ( 0 .. $blocks - 1 ) { |
foreach my $nr ( 0 .. $blocks - 1 ) { |
183 |
my $block = substr( $rest, 1 + $nr * 6, 6 ); |
my $block = substr( $rest, $nr * 6, 6 ); |
184 |
warn "## block ",as_hex( $block ) if $debug; |
warn "## block ",as_hex( $block ) if $debug; |
185 |
my $ord = unpack('v',substr( $block, 0, 2 )); |
my $ord = unpack('v',substr( $block, 0, 2 )); |
186 |
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; |
189 |
warn sprintf "## tag %08s %02d %s |%-4s|\n", $tag, $ord, as_hex( $data ), $data; |
warn sprintf "## tag %08s %02d %s |%-4s|\n", $tag, $ord, as_hex( $data ), $data; |
190 |
$data[ $ord ] = $data; |
$data[ $ord ] = $data; |
191 |
} |
} |
192 |
$read_cached->{ $tag } = join('', @data); |
$tags_data->{ $tag } = join('', @data); |
193 |
print "DATA $tag ",dump( $read_cached->{ $tag } ), "\n"; |
print "DATA $tag ",dump( $tags_data ), "\n"; |
194 |
}) |
} |
195 |
}); |
); |
196 |
|
|
197 |
# 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 |
198 |
if (0) { |
if (0) { |
286 |
return substr( $assert->{payload}, $to ); |
return substr( $assert->{payload}, $to ); |
287 |
} |
} |
288 |
|
|
|
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; |
|
|
} |
|
|
} |
|
|
|
|
289 |
use Digest::CRC; |
use Digest::CRC; |
290 |
|
|
291 |
sub crcccitt { |
sub crcccitt { |
321 |
return $bytes . $checksum; |
return $bytes . $checksum; |
322 |
} |
} |
323 |
|
|
324 |
sub readchunk { |
our $dispatch; |
|
my ( $parser ) = @_; |
|
325 |
|
|
326 |
|
sub readchunk { |
327 |
sleep 1; # FIXME remove |
sleep 1; # FIXME remove |
328 |
|
|
329 |
# read header of packet |
# read header of packet |
337 |
warn "## payload too short $payload_len != $len\n" if $payload_len != $len; |
warn "## payload too short $payload_len != $len\n" if $payload_len != $len; |
338 |
|
|
339 |
my $checksum = substr( $data, -2, 2 ); |
my $checksum = substr( $data, -2, 2 ); |
340 |
checksum( $header . $length . $payload, $checksum ); |
checksum( $header . $length . $payload , $checksum ); |
341 |
|
|
342 |
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" if $verbose; |
343 |
|
|
344 |
$assert->{len} = $len; |
$assert->{len} = $len; |
345 |
$assert->{payload} = $payload; |
$assert->{payload} = $payload; |
346 |
|
|
347 |
$parser->( $len, $payload ) if $parser && ref($parser) eq 'CODE'; |
my $full = $header . $length . $data; # full |
348 |
|
# find longest match for incomming data |
349 |
|
my ($to) = grep { |
350 |
|
my $match = substr($payload,0,length($_)); |
351 |
|
m/^\Q$match\E/ |
352 |
|
} sort { length($a) <=> length($b) } keys %$dispatch; |
353 |
|
warn "?? payload dispatch to ",dump( $payload, $dispatch, $to ) if $debug; |
354 |
|
|
355 |
|
if ( defined $to ) { |
356 |
|
my $rest = substr( $payload, length($to) ); |
357 |
|
warn "## DISPATCH payload to with rest", dump( $payload, $to, $rest ) if $debug; |
358 |
|
$dispatch->{ $to }->( $rest ); |
359 |
|
} else { |
360 |
|
print "NO DISPATCH for ",dump( $full ),"\n"; |
361 |
|
} |
362 |
|
|
363 |
return $data; |
return $data; |
364 |
} |
} |
376 |
} |
} |
377 |
|
|
378 |
sub cmd { |
sub cmd { |
379 |
my ( $cmd, $cmd_desc, $expect, $expect_desc, $coderef ) = @_; |
my $cmd = shift || confess "no cmd?"; |
380 |
|
my $cmd_desc = shift || confess "no description?"; |
381 |
|
my @expect = @_; |
382 |
|
|
383 |
my $bytes = str2bytes( $cmd ); |
my $bytes = str2bytes( $cmd ); |
384 |
|
|
385 |
# fix checksum if needed |
# fix checksum if needed |
386 |
$bytes = checksum( substr( $bytes, 0, -2 ), substr( $bytes, -2, 2 ) ); |
$bytes = checksum( substr( $bytes, 0, -2 ), substr( $bytes, -2, 2 ) ); |
387 |
|
|
388 |
warn ">> ", as_hex( $bytes ), "\t## $cmd_desc\n"; |
warn ">> ", as_hex( $bytes ), "\t## $cmd_desc\n" if $verbose; |
389 |
$assert->{send} = $cmd; |
$assert->{send} = $cmd; |
390 |
writechunk( $bytes ); |
writechunk( $bytes ); |
391 |
|
|
392 |
if ( $expect ) { |
while ( @expect ) { |
393 |
warn "?? $expect", $expect_desc ? "\t## $expect_desc" : '', "\n"; |
my $pattern = str2bytes( shift @expect ) || confess "no pattern?"; |
394 |
$assert->{expect} = substr(str2bytes($expect), 3, -2); # just expected payload |
my $coderef = shift @expect || confess "no coderef?"; |
395 |
readchunk( $coderef ); |
confess "not coderef" unless ref $coderef eq 'CODE'; |
396 |
|
|
397 |
|
next if defined $dispatch->{ $pattern }; |
398 |
|
|
399 |
|
$dispatch->{ substr($pattern,3) } = $coderef; |
400 |
|
warn "++ dispatch ", as_hex($pattern) ,dump( $dispatch ) if $debug; |
401 |
} |
} |
402 |
|
|
403 |
|
readchunk; |
404 |
} |
} |
405 |
|
|