--- 3m-810.pl 2010/02/11 13:23:26 65 +++ 3m-810.pl 2010/07/23 13:16:51 92 @@ -31,7 +31,7 @@ Reuse => 1 ); - die "can't setup server" unless $server; + die "can't setup server: $!" unless $server; print "Server $0 ready at $server_url\n"; @@ -84,14 +84,14 @@ $d->{security} = $tags_security->{$_}; push @{ $json->{tags} }, $d; } keys %$tags; - print $client "HTTP/1.0 200 OK\r\nContent-Type: application/x-javascript\r\n\r\n", + print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n", $param->{callback}, "(", to_json($json), ")\r\n"; } elsif ( $method =~ m{/program} ) { my $status = 501; # Not implementd foreach my $p ( keys %$param ) { - next unless $p =~ m/^tag_(\S+)/; + next unless $p =~ m/^(E[0-9A-F]{15})$/; my $tag = $1; my $content = "\x04\x11\x00\x01" . $param->{$p}; $content = "\x00" if $param->{$p} eq 'blank'; @@ -99,15 +99,39 @@ warn "PROGRAM $tag $content\n"; write_tag( $tag, $content ); + secure_tag_with( $tag, $param->{$p} =~ /^130/ ? 'DA' : 'D7' ); } print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n"; + } elsif ( $method =~ m{/secure(.js)} ) { + + my $json = $1; + + my $status = 501; # Not implementd + + foreach my $p ( keys %$param ) { + next unless $p =~ m/^(E[0-9A-F]{15})$/; + my $tag = $1; + my $data = $param->{$p}; + $status = 302; + + warn "SECURE $tag $data\n"; + secure_tag_with( $tag, $data ); + } + + if ( $json ) { + print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n", + $param->{callback}, "({ ok: 1 })\r\n"; + } else { + print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n"; + } + } else { - print $client "HTTP/1.0 404 Unkown method\r\n"; + print $client "HTTP/1.0 404 Unkown method\r\n\r\n"; } } else { - print $client "HTTP/1.0 500 No method\r\n"; + print $client "HTTP/1.0 500 No method\r\n\r\n"; } close $client; } @@ -144,7 +168,8 @@ my $http_server = 1; # 3M defaults: 8,4 -my $max_rfid_block = 16; +# cards 16, stickers: 8 +my $max_rfid_block = 8; my $read_blocks = 8; my $response = { @@ -368,7 +393,11 @@ sub decode_tag { my $tag = shift; - my $data = $tags_data->{$tag} || die "no data for $tag"; + my $data = $tags_data->{$tag}; + if ( ! $data ) { + warn "no data for $tag\n"; + return; + } my ( $u1, $set_item, $u2, $type, $content, $br_lib, $custom ) = unpack('C4Z16Nl>',$data); my $hash = { @@ -395,6 +424,12 @@ return $hash; } +sub forget_tag { + my $tag = shift; + delete $tags_data->{$tag}; + delete $visible_tags->{$tag}; +} + sub read_tag { my ( $tag ) = @_; @@ -416,6 +451,12 @@ "D6 00 0F FE 00 00 05 01 $tag BEEF", sub { print "FIXME: tag $tag ready? (expected block read instead)\n"; }, + "D6 00 0D 02 06 $tag", sub { + my $rest = shift; + print "ERROR reading $tag ", as_hex($rest), $/; + forget_tag $tag; + $start_block = $max_rfid_block; # XXX break out of while + }, ); } @@ -432,7 +473,12 @@ $security = as_hex( $security ); $tags_security->{$tag} = $security; warn "# SECURITY $tag = $security\n"; - } + }, + "D6 00 0C 0A 06", sub { + my $rest = shift; + warn "ERROR reading security from $rest\n"; + forget_tag $tag; + }, ); print "TAG $tag ", dump(decode_tag( $tag )); @@ -472,20 +518,43 @@ print "write_tag $tag = ",dump( $data ), " [$len/$blocks] == $hex_data\n"; + my $ok = 0; + cmd( "d6 00 ff 04 $tag 00 $blocks 00 $hex_data BEEF", "write $tag", - "d6 00 0d 04 00 $tag $blocks BEEF", sub { assert() }, + "d6 00 0d 06 00 $tag $blocks BEEF", sub { assert(); $ok++ }, + "d6 00 0d 04 06 ", sub { + my $data = shift; + warn "no tag ",as_hex( substr($data,0,8) ), " in range for write\n"; + }, ); # foreach ( 1 .. 3 ); # XXX 3m software does this three times! - my $to = $path; - $to .= '.' . time(); + if ( $ok ) { - rename $path, $to; - print ">> $to\n"; + my $to = $path; + $to .= '.' . time(); - # force re-read of tag - delete $tags_data->{$tag}; - delete $visible_tags->{$tag}; + rename $path, $to; + print ">> $to\n"; + + } + + forget_tag $tag; +} + +sub secure_tag_with { + my ( $tag, $data ) = @_; + + cmd( + "d6 00 0c 09 $tag $data BEEF", "secure $tag -> $data", + "d6 00 0c 09 00 $tag BEEF", sub { assert() }, + "d6 00 0c 09 06 ", sub { + my $data = shift; + warn "no tag ",as_hex( substr($data,0,8) ), " in range for secure\n"; + }, + ); + + forget_tag $tag; } sub secure_tag { @@ -494,10 +563,7 @@ my $path = "$secure_path/$tag"; my $data = substr(read_file( $path ),0,2); - cmd( - "d6 00 0c 09 $tag $data BEEF", "secure $tag -> $data", - "d6 00 0c 09 00 $tag BEEF", sub { assert() }, - ); + secure_tag_with( $tag, $data ); my $to = $path; $to .= '.' . time(); @@ -546,7 +612,7 @@ sub as_hex { my @out; foreach my $str ( @_ ) { - my $hex = unpack( 'H*', $str ); + my $hex = uc unpack( 'H*', $str ); $hex =~ s/(..)/$1 /g if length( $str ) > 2; $hex =~ s/\s+$//; push @out, $hex; @@ -660,7 +726,7 @@ warn "## DISPATCH payload to with rest", dump( $payload, $to, $rest ) if $debug; $dispatch->{ $to }->( $rest ); } else { - die "NO DISPATCH for ",as_hex( $full ),"\n"; + die "NO DISPATCH for ",as_hex( $full ), " in ", dump( $dispatch ); } return $data;