10 |
use File::Slurp; |
use File::Slurp; |
11 |
use JSON; |
use JSON; |
12 |
use POSIX qw(strftime); |
use POSIX qw(strftime); |
13 |
|
use Time::HiRes; |
14 |
|
|
15 |
use IO::Socket::INET; |
use IO::Socket::INET; |
16 |
|
|
17 |
my $debug = 0; |
my $debug = 2; |
18 |
|
|
19 |
my $tags_data; |
my $tags_data; |
20 |
my $tags_security; |
my $tags_security; |
32 |
Reuse => 1 |
Reuse => 1 |
33 |
); |
); |
34 |
|
|
35 |
die "can't setup server" unless $server; |
die "can't setup server: $!" unless $server; |
36 |
|
|
37 |
print "Server $0 ready at $server_url\n"; |
print "Server $0 ready at $server_url\n"; |
38 |
|
|
85 |
$d->{security} = $tags_security->{$_}; |
$d->{security} = $tags_security->{$_}; |
86 |
push @{ $json->{tags} }, $d; |
push @{ $json->{tags} }, $d; |
87 |
} keys %$tags; |
} keys %$tags; |
88 |
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", |
89 |
$param->{callback}, "(", to_json($json), ")\r\n"; |
$param->{callback}, "(", to_json($json), ")\r\n"; |
90 |
} elsif ( $method =~ m{/program} ) { |
} elsif ( $method =~ m{/program} ) { |
91 |
|
|
92 |
my $status = 501; # Not implementd |
my $status = 501; # Not implementd |
93 |
|
|
94 |
foreach my $p ( keys %$param ) { |
foreach my $p ( keys %$param ) { |
95 |
next unless $p =~ m/^tag_(\S+)/; |
next unless $p =~ m/^(E[0-9A-F]{15})$/; |
96 |
my $tag = $1; |
my $tag = $1; |
97 |
my $content = "\x04\x11\x00\x01" . $param->{$p}; |
my $content = "\x04\x11\x00\x01" . $param->{$p}; |
98 |
$content = "\x00" if $param->{$p} eq 'blank'; |
$content = "\x00" if $param->{$p} eq 'blank'; |
100 |
|
|
101 |
warn "PROGRAM $tag $content\n"; |
warn "PROGRAM $tag $content\n"; |
102 |
write_tag( $tag, $content ); |
write_tag( $tag, $content ); |
103 |
|
secure_tag_with( $tag, $param->{$p} =~ /^130/ ? 'DA' : 'D7' ); |
104 |
} |
} |
105 |
|
|
106 |
print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n"; |
print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n"; |
107 |
|
|
108 |
|
} elsif ( $method =~ m{/secure(.js)} ) { |
109 |
|
|
110 |
|
my $json = $1; |
111 |
|
|
112 |
|
my $status = 501; # Not implementd |
113 |
|
|
114 |
|
foreach my $p ( keys %$param ) { |
115 |
|
next unless $p =~ m/^(E[0-9A-F]{15})$/; |
116 |
|
my $tag = $1; |
117 |
|
my $data = $param->{$p}; |
118 |
|
$status = 302; |
119 |
|
|
120 |
|
warn "SECURE $tag $data\n"; |
121 |
|
secure_tag_with( $tag, $data ); |
122 |
|
} |
123 |
|
|
124 |
|
if ( $json ) { |
125 |
|
print $client "HTTP/1.0 200 OK\r\nContent-Type: application/json\r\n\r\n", |
126 |
|
$param->{callback}, "({ ok: 1 })\r\n"; |
127 |
|
} else { |
128 |
|
print $client "HTTP/1.0 $status $method\r\nLocation: $server_url\r\n\r\n"; |
129 |
|
} |
130 |
|
|
131 |
} else { |
} else { |
132 |
print $client "HTTP/1.0 404 Unkown method\r\n"; |
print $client "HTTP/1.0 404 Unkown method\r\n\r\n"; |
133 |
} |
} |
134 |
} else { |
} else { |
135 |
print $client "HTTP/1.0 500 No method\r\n"; |
print $client "HTTP/1.0 500 No method\r\n\r\n"; |
136 |
} |
} |
137 |
close $client; |
close $client; |
138 |
} |
} |
156 |
sub diag { _message('diag',@_) }; |
sub diag { _message('diag',@_) }; |
157 |
|
|
158 |
my $device = "/dev/ttyUSB0"; |
my $device = "/dev/ttyUSB0"; |
159 |
my $baudrate = "19200"; |
my $baudrate = "38400"; |
160 |
my $databits = "8"; |
my $databits = "8"; |
161 |
my $parity = "none"; |
my $parity = "even"; |
162 |
my $stopbits = "1"; |
my $stopbits = "1"; |
163 |
my $handshake = "none"; |
my $handshake = "none"; |
164 |
|
|
169 |
my $http_server = 1; |
my $http_server = 1; |
170 |
|
|
171 |
# 3M defaults: 8,4 |
# 3M defaults: 8,4 |
172 |
my $max_rfid_block = 16; |
# cards 16, stickers: 8 |
173 |
|
my $max_rfid_block = 8; |
174 |
my $read_blocks = 8; |
my $read_blocks = 8; |
175 |
|
|
176 |
my $response = { |
my $response = { |
265 |
#$port->stty_inpck(1); |
#$port->stty_inpck(1); |
266 |
#$port->stty_istrip(1); |
#$port->stty_istrip(1); |
267 |
|
|
268 |
|
sub cpr_m02_checksum { |
269 |
|
my $data = shift; |
270 |
|
|
271 |
|
my $preset = 0xffff; |
272 |
|
my $polynom = 0x8408; |
273 |
|
|
274 |
|
my $crc = $preset; |
275 |
|
foreach my $i ( 0 .. length($data) - 1 ) { |
276 |
|
$crc ^= ord(substr($data,$i,1)); |
277 |
|
for my $j ( 0 .. 7 ) { |
278 |
|
if ( $crc & 0x0001 ) { |
279 |
|
$crc = ( $crc >> 1 ) ^ $polynom; |
280 |
|
} else { |
281 |
|
$crc = $crc >> 1; |
282 |
|
} |
283 |
|
} |
284 |
|
# warn sprintf('%d %04x', $i, $crc & 0xffff); |
285 |
|
} |
286 |
|
|
287 |
|
return pack('v', $crc); |
288 |
|
} |
289 |
|
|
290 |
|
sub cpr { |
291 |
|
my ( $hex, $description ) = shift; |
292 |
|
my $bytes = str2bytes($hex); |
293 |
|
my $len = pack( 'c', length( $bytes ) + 3 ); |
294 |
|
my $send = $len . $bytes; |
295 |
|
my $checksum = cpr_m02_checksum($send); |
296 |
|
$send .= $checksum; |
297 |
|
|
298 |
|
warn ">> ", as_hex( $send ), "[$description]\n"; |
299 |
|
$port->write( $send ); |
300 |
|
|
301 |
|
my $r_len = $port->read(1); |
302 |
|
|
303 |
|
while ( ! $r_len ) { |
304 |
|
warn "# wait for response length 0.050\n"; |
305 |
|
Time::HiRes::sleep 0.050; |
306 |
|
$r_len = $port->read(1); |
307 |
|
} |
308 |
|
|
309 |
|
warn "<< response len: ", as_hex($r_len), "\n"; |
310 |
|
$r_len = ord($r_len) - 1; |
311 |
|
my $data = $port->read( $r_len ); |
312 |
|
warn "<< ", as_hex( $data ); |
313 |
|
|
314 |
|
Time::HiRes::sleep 0.050; |
315 |
|
} |
316 |
|
|
317 |
|
#cpr( 'FF 52 00', 'detect boud rate' ); |
318 |
|
|
319 |
|
#cpr( '00 65', 'software version' ); |
320 |
|
|
321 |
|
cpr( 'FF 65', 'get ? info' ); |
322 |
|
|
323 |
|
cpr( 'FF 69 00', 'get reader info' ); |
324 |
|
|
325 |
|
cpr( 'FF B0 01 00', '?' ); |
326 |
|
|
327 |
|
cpr( 'FF 69', '?' ); |
328 |
|
|
329 |
|
#cpr( '', '?' ); |
330 |
|
|
331 |
|
exit; |
332 |
# initial hand-shake with device |
# initial hand-shake with device |
333 |
|
|
334 |
cmd( 'D5 00 05 04 00 11 8C66', 'hw version', |
cmd( 'D5 00 05 04 00 11 8C66', 'hw version', |
458 |
sub decode_tag { |
sub decode_tag { |
459 |
my $tag = shift; |
my $tag = shift; |
460 |
|
|
461 |
my $data = $tags_data->{$tag} || die "no data for $tag"; |
my $data = $tags_data->{$tag}; |
462 |
|
if ( ! $data ) { |
463 |
|
warn "no data for $tag\n"; |
464 |
|
return; |
465 |
|
} |
466 |
|
|
467 |
my ( $u1, $set_item, $u2, $type, $content, $br_lib, $custom ) = unpack('C4Z16Nl>',$data); |
my ( $u1, $set_item, $u2, $type, $content, $br_lib, $custom ) = unpack('C4Z16Nl>',$data); |
468 |
my $hash = { |
my $hash = { |
489 |
return $hash; |
return $hash; |
490 |
} |
} |
491 |
|
|
492 |
|
sub forget_tag { |
493 |
|
my $tag = shift; |
494 |
|
delete $tags_data->{$tag}; |
495 |
|
delete $visible_tags->{$tag}; |
496 |
|
} |
497 |
|
|
498 |
sub read_tag { |
sub read_tag { |
499 |
my ( $tag ) = @_; |
my ( $tag ) = @_; |
500 |
|
|
507 |
while ( $start_block < $max_rfid_block ) { |
while ( $start_block < $max_rfid_block ) { |
508 |
|
|
509 |
cmd( |
cmd( |
510 |
sprintf( "D6 00 0D 02 $tag %02x %02x ffff", $start_block, $read_blocks ), |
sprintf( "D6 00 0D 02 $tag %02x %02x BEEF", $start_block, $read_blocks ), |
511 |
"read $tag offset: $start_block blocks: $read_blocks", |
"read $tag offset: $start_block blocks: $read_blocks", |
512 |
"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"; |
"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"; |
513 |
$start_block = read_tag_data( $start_block, @_ ); |
$start_block = read_tag_data( $start_block, @_ ); |
514 |
warn "# read tag upto $start_block\n"; |
warn "# read tag upto $start_block\n"; |
515 |
}, |
}, |
516 |
"D6 00 0F FE 00 00 05 01 $tag 941A", sub { |
"D6 00 0F FE 00 00 05 01 $tag BEEF", sub { |
517 |
print "FIXME: tag $tag ready? (expected block read instead)\n"; |
print "FIXME: tag $tag ready? (expected block read instead)\n"; |
518 |
}, |
}, |
519 |
|
"D6 00 0D 02 06 $tag", sub { |
520 |
|
my $rest = shift; |
521 |
|
print "ERROR reading $tag ", as_hex($rest), $/; |
522 |
|
forget_tag $tag; |
523 |
|
$start_block = $max_rfid_block; # XXX break out of while |
524 |
|
}, |
525 |
); |
); |
526 |
|
|
527 |
} |
} |
529 |
my $security; |
my $security; |
530 |
|
|
531 |
cmd( |
cmd( |
532 |
"D6 00 0B 0A $tag 1234", "check security $tag", |
"D6 00 0B 0A $tag BEEF", "check security $tag", |
533 |
"D6 00 0D 0A 00", sub { |
"D6 00 0D 0A 00", sub { |
534 |
my $rest = shift; |
my $rest = shift; |
535 |
my $from_tag; |
my $from_tag; |
538 |
$security = as_hex( $security ); |
$security = as_hex( $security ); |
539 |
$tags_security->{$tag} = $security; |
$tags_security->{$tag} = $security; |
540 |
warn "# SECURITY $tag = $security\n"; |
warn "# SECURITY $tag = $security\n"; |
541 |
} |
}, |
542 |
|
"D6 00 0C 0A 06", sub { |
543 |
|
my $rest = shift; |
544 |
|
warn "ERROR reading security from $rest\n"; |
545 |
|
forget_tag $tag; |
546 |
|
}, |
547 |
); |
); |
548 |
|
|
549 |
print "TAG $tag ", dump(decode_tag( $tag )); |
print "TAG $tag ", dump(decode_tag( $tag )); |
584 |
print "write_tag $tag = ",dump( $data ), " [$len/$blocks] == $hex_data\n"; |
print "write_tag $tag = ",dump( $data ), " [$len/$blocks] == $hex_data\n"; |
585 |
|
|
586 |
cmd( |
cmd( |
587 |
"d6 00 ff 04 $tag 00 $blocks 00 $hex_data ffff", "write $tag", |
"d6 00 ff 04 $tag 00 $blocks 00 $hex_data BEEF", "write $tag", |
588 |
"d6 00 0d 04 00 $tag $blocks afb1", sub { assert() }, |
"d6 00 0d 04 00 $tag $blocks BEEF", sub { assert() }, |
589 |
); # foreach ( 1 .. 3 ); # XXX 3m software does this three times! |
); # foreach ( 1 .. 3 ); # XXX 3m software does this three times! |
590 |
|
|
591 |
my $to = $path; |
my $to = $path; |
594 |
rename $path, $to; |
rename $path, $to; |
595 |
print ">> $to\n"; |
print ">> $to\n"; |
596 |
|
|
597 |
# force re-read of tag |
forget_tag $tag; |
598 |
delete $tags_data->{$tag}; |
} |
599 |
delete $visible_tags->{$tag}; |
|
600 |
|
sub secure_tag_with { |
601 |
|
my ( $tag, $data ) = @_; |
602 |
|
|
603 |
|
cmd( |
604 |
|
"d6 00 0c 09 $tag $data BEEF", "secure $tag -> $data", |
605 |
|
"d6 00 0c 09 00 $tag BEEF", sub { assert() }, |
606 |
|
); |
607 |
|
|
608 |
|
forget_tag $tag; |
609 |
} |
} |
610 |
|
|
611 |
sub secure_tag { |
sub secure_tag { |
614 |
my $path = "$secure_path/$tag"; |
my $path = "$secure_path/$tag"; |
615 |
my $data = substr(read_file( $path ),0,2); |
my $data = substr(read_file( $path ),0,2); |
616 |
|
|
617 |
cmd( |
secure_tag_with( $tag, $data ); |
|
"d6 00 0c 09 $tag $data 1234", "secure $tag -> $data", |
|
|
"d6 00 0c 09 00 $tag 1234", sub { assert() }, |
|
|
); |
|
618 |
|
|
619 |
my $to = $path; |
my $to = $path; |
620 |
$to .= '.' . time(); |
$to .= '.' . time(); |
663 |
sub as_hex { |
sub as_hex { |
664 |
my @out; |
my @out; |
665 |
foreach my $str ( @_ ) { |
foreach my $str ( @_ ) { |
666 |
my $hex = unpack( 'H*', $str ); |
my $hex = uc unpack( 'H*', $str ); |
667 |
$hex =~ s/(..)/$1 /g if length( $str ) > 2; |
$hex =~ s/(..)/$1 /g if length( $str ) > 2; |
668 |
$hex =~ s/\s+$//; |
$hex =~ s/\s+$//; |
669 |
push @out, $hex; |
push @out, $hex; |
677 |
while ( length( $data ) < $len ) { |
while ( length( $data ) < $len ) { |
678 |
my ( $c, $b ) = $port->read(1); |
my ( $c, $b ) = $port->read(1); |
679 |
die "no bytes on port: $!" unless defined $b; |
die "no bytes on port: $!" unless defined $b; |
680 |
#warn "## got $c bytes: ", as_hex($b), "\n"; |
warn "## got $c bytes: ", as_hex($b), "\n"; |
681 |
|
last if $c == 0; |
682 |
$data .= $b; |
$data .= $b; |
683 |
} |
} |
684 |
$desc ||= '?'; |
$desc ||= '?'; |
736 |
warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug; |
warn "## checksum ",dump( $bytes, $xor, $checksum ) if $debug; |
737 |
|
|
738 |
if ( defined $checksum && $xor ne $checksum ) { |
if ( defined $checksum && $xor ne $checksum ) { |
739 |
print "checksum doesn't match: ", as_hex($xor), " != ", as_hex($checksum), " data: ", as_hex($bytes), "\n"; |
warn "checksum error: ", as_hex($xor), " != ", as_hex($checksum), " data: ", as_hex($bytes), "\n" if $checksum ne "\xBE\xEF"; |
740 |
return $bytes . $xor; |
return $bytes . $xor; |
741 |
} |
} |
742 |
return $bytes . $checksum; |
return $bytes . $checksum; |