--- 3m-810.pl 2009/03/28 14:20:27 24
+++ 3m-810.pl 2009/04/06 13:10:40 29
@@ -7,15 +7,28 @@
use Data::Dump qw/dump/;
use Carp qw/confess/;
use Getopt::Long;
+use File::Slurp;
use IO::Socket::INET;
-my $meteor = IO::Socket::INET->new( '192.168.1.13:4671' ) || die "can't connect to meteor: $!";
+my $meteor_server = '192.168.1.13:4671';
+my $meteor_fh;
sub meteor {
- my ( $item, $html ) = @_;
- warn ">> meteor $item $html\n";
- print $meteor "ADDMESSAGE test $item|" . localtime() . "
$html\n";
+ my @a = @_;
+ push @a, scalar localtime() if $a[0] =~ m{^info};
+
+ if ( ! defined $meteor_fh ) {
+ warn "# open connection to $meteor_server";
+ $meteor_fh = IO::Socket::INET->new(
+ PeerAddr => $meteor_server,
+ Timeout => 1,
+ ) || warn "can't connect to meteor $meteor_server: $!"; # FIXME warn => die for production
+ $meteor_fh = 0; # don't try again
+ }
+
+ warn ">> meteor ",dump( @a );
+ print $meteor_fh "ADDMESSAGE test ",join('|',@a),"\n" if $meteor_fh;
}
my $debug = 0;
@@ -27,6 +40,8 @@
my $stopbits = "1";
my $handshake = "none";
+my $program_path = './program/';
+
my $response = {
'd500090400110a0500027250' => 'version?',
'd60007fe00000500c97b' => 'no tag in range',
@@ -47,6 +62,7 @@
'parity=s' => \$parity,
'stopbits=i' => \$stopbits,
'handshake=s' => \$handshake,
+ 'meteor=s' => \$meteor_server,
) or die $!;
my $verbose = $debug > 0 ? $debug-- : 0;
@@ -110,7 +126,7 @@
'D5 00 09 04 00 11 0A 05 00 02 7250', sub {
my $hw_ver = join('.', unpack('CCCC', skip_assert(3) ));
print "hardware version $hw_ver\n";
- meteor( -1, "Found reader $hw_ver" );
+ meteor( 'info', "Found reader hardware $hw_ver" );
});
cmd( 'D6 00 0C 13 04 01 00 02 00 03 00 04 00 AAF2','FIXME: stats?',
@@ -126,7 +142,7 @@
if ( ! $nr ) {
print "no tags in range\n";
update_visible_tags();
- meteor( -1, "No tags in range" );
+ meteor( 'info-none-in-range' );
$tags_data = {};
} else {
@@ -140,10 +156,9 @@
warn "## tags ",as_hex($tags), " [$tl] = ",dump( $tags ) if $debug;
print "$nr tags in range: ", join(',', @tags ) , "\n";
- update_visible_tags( @tags );
+ meteor( 'info-in-range', join(' ',@tags));
- my $html = join('', map { "
$_" } @tags);
- meteor( 0, "Tags:" );
+ update_visible_tags( @tags );
}
}
) while(1);
@@ -159,71 +174,102 @@
foreach my $tag ( @tags ) {
if ( ! defined $last_visible_tags->{$tag} ) {
- read_tag( $tag );
+ if ( defined $tags_data->{$tag} ) {
+# meteor( 'in-range', $tag );
+ } else {
+ meteor( 'read', $tag );
+ read_tag( $tag );
+ }
$visible_tags->{$tag}++;
} else {
warn "## using cached data for $tag" if $debug;
}
delete $last_visible_tags->{$tag}; # leave just missing tags
+
+ if ( -e "$program_path/$tag" ) {
+ meteor( 'write', $tag );
+ write_tag( $tag );
+ }
}
foreach my $tag ( keys %$last_visible_tags ) {
my $data = delete $tags_data->{$tag};
print "removed tag $tag with data ",dump( $data ),"\n";
+ meteor( 'removed', $tag );
}
warn "## update_visible_tags(",dump( @tags ),") = ",dump( $visible_tags )," removed: ",dump( $last_visible_tags ), " data: ",dump( $tags_data ) if $debug;
}
+my $tag_data_block;
+
+sub read_tag_data {
+ my ($start_block,$rest) = @_;
+ die "no rest?" unless $rest;
+ warn "## DATA [$start_block] ", dump( $rest ) if $debug;
+ my $tag = uc(unpack('H16',substr( $rest, 0, 8 )));
+ my $blocks = ord(substr($rest,8,1));
+ $rest = substr($rest,9); # leave just data blocks
+ foreach my $nr ( 0 .. $blocks - 1 ) {
+ my $block = substr( $rest, $nr * 6, 6 );
+ warn "## block ",as_hex( $block ) if $debug;
+ my $ord = unpack('v',substr( $block, 0, 2 ));
+ my $expected_ord = $nr + $start_block;
+ die "got block $ord, expected block $expected_ord from ",dump( $block ) if $ord != $expected_ord;
+ my $data = substr( $block, 2 );
+ die "data payload should be 4 bytes" if length($data) != 4;
+ warn sprintf "## tag %9s %02d %s |%-4s|\n", $tag, $ord, as_hex( $data ), $data;
+ $tag_data_block->{$tag}->[ $ord ] = $data;
+ }
+ $tags_data->{ $tag } = join('', @{ $tag_data_block->{$tag} });
+ print "DATA $tag ",dump( $tags_data ), "\n";
+}
sub read_tag {
my ( $tag ) = @_;
confess "no tag?" unless $tag;
- return if defined $tags_data->{$tag};
-
print "read_tag $tag\n";
cmd(
- "D6 00 0D 02 $tag 00 03 1CC4", 'read $tag offset: 0 blocks: 3',
+ "D6 00 0D 02 $tag 00 03 1CC4", "read $tag offset: 0 blocks: 3",
"D6 00 0F FE 00 00 05 01 $tag 941A", sub {
print "FIXME: tag $tag ready?\n";
},
"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";
- my $rest = shift || die "no rest?";
- warn "## DATA ", dump( $rest ) if $debug;
- my $tag = uc(unpack('H16',substr( $rest, 0, 8 )));
- my $blocks = ord(substr($rest,8,1));
- $rest = substr($rest,9); # leave just data blocks
- my @data;
- foreach my $nr ( 0 .. $blocks - 1 ) {
- my $block = substr( $rest, $nr * 6, 6 );
- warn "## block ",as_hex( $block ) if $debug;
- my $ord = unpack('v',substr( $block, 0, 2 ));
- die "got block $ord, expected block $nr from ",dump( $block ) if $ord != $nr;
- my $data = substr( $block, 2 );
- die "data payload should be 4 bytes" if length($data) != 4;
- warn sprintf "## tag %08s %02d %s |%-4s|\n", $tag, $ord, as_hex( $data ), $data;
- $data[ $ord ] = $data;
- }
- $tags_data->{ $tag } = join('', @data);
- print "DATA $tag ",dump( $tags_data ), "\n";
+ read_tag_data( 0, @_ );
+ },
+ );
+
+ cmd(
+ "D6 00 0D 02 $tag 03 04 3970", "read $tag offset: 3 blocks: 4",
+ "D6 00 25 02 00", sub { # $tag 04 03 00 30 30 00 00 04 00 00 00 00 00
+ read_tag_data( 3, @_ );
}
);
- # 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
-if (0) {
- cmd( "D6 00 0D 02 $tag 03 04 3970", 'read offset: 3 blocks: 4' );
-
- # D6 00 25 02 00 $tag 04 03 00 30 30 00 00 04 00 00 00 00 00
- # $tag 05 00 00 00 00 00 06 00 00 00 00 00 B9BA
- warn "?? D6 00 25 02 00 $tag 04 03 00 39 30 31 32 04 00 ....\n";
}
- warn "?? D6 00 0F FE 00 00 05 01 $tag 941A ##### ready?\n";
- my $item = unpack('H*', substr($tag,-8) ) % 100000;
- meteor( $item, "Loading $item" );
+sub write_tag {
+ my ($tag) = @_;
+
+ my $path = "$program_path/$tag";
+
+ my $data = read_file( $path );
+
+ print "write_tag $tag = $data\n";
+
+ cmd(
+ "D6 00 26 04 $tag 00 06 00 04 11 00 01 61 61 61 61 62 62 62 62 63 63 63 63 64 64 64 64 00 00 00 00 FD3B", "write $tag",
+ "D6 00 0D 04 00 $tag 06 AFB1", sub { assert() },
+ ) foreach ( 1 .. 3 ); # XXX 3M software does this three times!
+
+ my $to = $path;
+ $to .= '.' . time();
+
+ rename $path, $to;
+ print ">> $to\n";
}
@@ -278,6 +324,7 @@
my $data = '';
while ( length( $data ) < $len ) {
my ( $c, $b ) = $port->read(1);
+ die "no bytes on port: $!" unless defined $b;
#warn "## got $c bytes: ", as_hex($b), "\n";
$data .= $b;
}