--- 3m-810.pl 2010/02/11 15:10:39 68 +++ cpr-m02.pl 2010/07/16 13:05:24 87 @@ -10,6 +10,7 @@ use File::Slurp; use JSON; use POSIX qw(strftime); +use Time::HiRes; use IO::Socket::INET; @@ -31,7 +32,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 +85,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} ) { @@ -104,7 +105,9 @@ 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 @@ -118,13 +121,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; } @@ -148,9 +156,9 @@ sub diag { _message('diag',@_) }; my $device = "/dev/ttyUSB0"; -my $baudrate = "19200"; +my $baudrate = "38400"; my $databits = "8"; -my $parity = "none"; +my $parity = "even"; my $stopbits = "1"; my $handshake = "none"; @@ -161,7 +169,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 = { @@ -256,6 +265,111 @@ #$port->stty_inpck(1); #$port->stty_istrip(1); +sub cpr_m02_checksum { + my $data = shift; + + my $preset = 0xffff; + my $polynom = 0x8408; + + my $crc = $preset; + foreach my $i ( 0 .. length($data) - 1 ) { + $crc ^= ord(substr($data,$i,1)); + for my $j ( 0 .. 7 ) { + if ( $crc & 0x0001 ) { + $crc = ( $crc >> 1 ) ^ $polynom; + } else { + $crc = $crc >> 1; + } + } +# warn sprintf('%d %04x', $i, $crc & 0xffff); + } + + return pack('v', $crc); +} + +sub cpr_psst_wait { + # Protocol Start Synchronization Time (PSST): 5ms < data timeout 12 ms + Time::HiRes::sleep 0.005; +} + +sub cpr { + my ( $hex, $description, $coderef ) = @_; + my $bytes = str2bytes($hex); + my $len = pack( 'c', length( $bytes ) + 3 ); + my $send = $len . $bytes; + my $checksum = cpr_m02_checksum($send); + $send .= $checksum; + + warn ">> ", as_hex( $send ), "\t\t[$description]\n"; + $port->write( $send ); + + cpr_psst_wait; + + my $r_len = $port->read(1); + + while ( ! $r_len ) { + warn "# wait for response length 5ms\n"; + cpr_psst_wait; + $r_len = $port->read(1); + } + + my $data_len = ord($r_len) - 1; + my $data = $port->read( $data_len ); + warn "<< ", as_hex( $r_len . $data ),"\n"; + + cpr_psst_wait; + + $coderef->( $data ) if $coderef; + +} + +# FF = COM-ADDR any + +cpr( 'FF 52 00', 'Boud Rate Detection' ); + +cpr( 'FF 65', 'Get Software Version' ); + +cpr( 'FF 66 00', 'Get Reader Info - General hard and firware' ); + +cpr( 'FF 69', 'RF Reset' ); + + +sub cpr_read { + my $uid = shift; + my $hex_uid = as_hex($uid); + + cpr( "FF B0 23 01 $hex_uid 00 04", "Read Multiple Blocks $hex_uid" ); +# cpr( "FF B0 2B 01 $hex_uid", "Get System Information $hex_uid" ); +} + + +my $inventory; + +while(1) { + +cpr( 'FF B0 01 00', 'ISO - Inventory', sub { + my $data = shift; + my $data_sets = ord(substr($data,3,1)); + $data = substr($data,4); + foreach ( 1 .. $data_sets ) { + my $tr_type = substr($data,0,1); + die "FIXME only TR-TYPE=3 ISO 15693 supported" unless $tr_type eq "\x03"; + my $dsfid = substr($data,1,1); + my $uid = substr($data,2,8); + $inventory->{$uid}++; + $data = substr($data,10); + warn "# TAG $_ ",as_hex( $tr_type, $dsfid, $uid ),$/; + + cpr_read( $uid ); + } + warn "inventory: ",dump($inventory); +}); + +} + +#cpr( '', '?' ); + +exit; # initial hand-shake with device cmd( 'D5 00 05 04 00 11 8C66', 'hw version', @@ -385,7 +499,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 = { @@ -439,6 +557,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 + }, ); } @@ -455,7 +579,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 )); @@ -575,7 +704,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; @@ -589,7 +718,8 @@ 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"; + warn "## got $c bytes: ", as_hex($b), "\n"; + last if $c == 0; $data .= $b; } $desc ||= '?';