--- 3m-810.pl 2010/02/11 14:59:56 67 +++ 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,7 +84,7 @@ $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} ) { @@ -99,11 +99,14 @@ 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} ) { + } elsif ( $method =~ m{/secure(.js)} ) { + + my $json = $1; my $status = 501; # Not implementd @@ -117,13 +120,18 @@ secure_tag_with( $tag, $data ); } - print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n"; + 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; } @@ -160,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 = { @@ -384,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 = { @@ -438,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 + }, ); } @@ -454,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 )); @@ -494,16 +518,26 @@ 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(); + + rename $path, $to; + print ">> $to\n"; + + } forget_tag $tag; } @@ -514,6 +548,10 @@ 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; @@ -574,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; @@ -688,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;