/[RFID]/3m-810.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /3m-810.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 44 by dpavlin, Tue Jun 23 13:10:18 2009 UTC revision 56 by dpavlin, Fri Jun 26 11:46:45 2009 UTC
# Line 12  use JSON; Line 12  use JSON;
12    
13  use IO::Socket::INET;  use IO::Socket::INET;
14    
15  my $meteor_server = '192.168.1.13:4671';  my $debug = 0;
16    
17    my $tags_data;
18    my $tags_security;
19    my $visible_tags;
20    
21    my $meteor_server; # = '192.168.1.13:4671';
22  my $meteor_fh;  my $meteor_fh;
23    
24  sub meteor {  sub meteor {
# Line 57  sub http_server { Line 63  sub http_server {
63                  my ($client,$path) = @_;                  my ($client,$path) = @_;
64    
65                  $path = "www/$path";                  $path = "www/$path";
66                    $path .= 'rfid.html' if $path =~ m{/$};
67    
68                  return unless -e $path;                  return unless -e $path;
69    
# Line 78  sub http_server { Line 85  sub http_server {
85                  $client->autoflush(1);                  $client->autoflush(1);
86                  my $request = <$client>;                  my $request = <$client>;
87    
88                  warn "<< $request\n";                  warn "WEB << $request\n" if $debug;
89    
90                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {                  if ($request =~ m{^GET (/.*) HTTP/1.[01]}) {
91                          my $method = $1;                          my $method = $1;
92                            my $param;
93                            if ( $method =~ s{\?(.+)}{} ) {
94                                    foreach my $p ( split(/[&;]/, $1) ) {
95                                            my ($n,$v) = split(/=/, $p, 2);
96                                            $param->{$n} = $v;
97                                    }
98                                    warn "WEB << param: ",dump( $param ) if $debug;
99                            }
100                          if ( my $path = static( $client,$1 ) ) {                          if ( my $path = static( $client,$1 ) ) {
101                                  warn ">> $path";                                  warn "WEB >> $path" if $debug;
102                          } elsif ( $method =~ m{/scan} ) {                          } elsif ( $method =~ m{/scan} ) {
                                 my $callback = $1 if $method =~ m{\?callback=([^&;]+)};  
103                                  my $tags = scan_for_tags();                                  my $tags = scan_for_tags();
104                                  my $json;                                  my $json = { time => time() };
105                                  map {                                  map {
106                                          my $d = decode_tag($_);                                          my $d = decode_tag($_);
107                                          $d->{sid} = $_;                                          $d->{sid} = $_;
108                                            $d->{security} = $tags_security->{$_};
109                                          push @{ $json->{tags} },  $d;                                          push @{ $json->{tags} },  $d;
110                                  } keys %$tags;                                  } keys %$tags;
111                                  print $client "HTTP/1.0 200 OK\r\nContent-Type: application/x-javascript\r\n\r\n$callback(", to_json($json), ")\r\n";                                  print $client "HTTP/1.0 200 OK\r\nContent-Type: application/x-javascript\r\n\r\n",
112                                            $param->{callback}, "(", to_json($json), ")\r\n";
113                          } else {                          } else {
114                                  print $client "HTTP/1.0 404 Unkown method\r\n";                                  print $client "HTTP/1.0 404 Unkown method\r\n";
115                          }                          }
# Line 106  sub http_server { Line 122  sub http_server {
122          die "server died";          die "server died";
123  }  }
124    
125  my $debug = 0;  
126    my $last_message = {};
127    sub _message {
128            my $type = shift @_;
129            my $text = join(' ',@_);
130            my $last = $last_message->{$type};
131            if ( $text ne $last ) {
132                    warn $type eq 'diag' ? '# ' : '', $text, "\n";
133                    $last_message->{$type} = $text;
134            }
135    }
136    
137    sub _log { _message('log',@_) };
138    sub diag { _message('diag',@_) };
139    
140  my $device    = "/dev/ttyUSB0";  my $device    = "/dev/ttyUSB0";
141  my $baudrate  = "19200";  my $baudrate  = "19200";
# Line 146  GetOptions( Line 175  GetOptions(
175          'stopbits=i'  => \$stopbits,          'stopbits=i'  => \$stopbits,
176          'handshake=s' => \$handshake,          'handshake=s' => \$handshake,
177          'meteor=s'    => \$meteor_server,          'meteor=s'    => \$meteor_server,
178            'http-server!' => \$http_server,
179  ) or die $!;  ) or die $!;
180    
181  my $verbose = $debug > 0 ? $debug-- : 0;  my $verbose = $debug > 0 ? $debug-- : 0;
# Line 181  it under the same terms ans Perl itself. Line 211  it under the same terms ans Perl itself.
211    
212  =cut  =cut
213    
 my $tags_data;  
 my $visible_tags;  
   
214  my $item_type = {  my $item_type = {
215          1 => 'Book',          1 => 'Book',
216          6 => 'CD/CD ROM',          6 => 'CD/CD ROM',
# Line 209  $databits=$port->databits($databits); Line 236  $databits=$port->databits($databits);
236  $parity=$port->parity($parity);  $parity=$port->parity($parity);
237  $stopbits=$port->stopbits($stopbits);  $stopbits=$port->stopbits($stopbits);
238    
239  print "## using $device $baudrate $databits $parity $stopbits debug: $debug verbose: $verbose\n";  warn "## using $device $baudrate $databits $parity $stopbits debug: $debug verbose: $verbose\n";
240    
241  # Just in case: reset our timing and buffers  # Just in case: reset our timing and buffers
242  $port->lookclear();  $port->lookclear();
# Line 236  sub scan_for_tags { Line 263  sub scan_for_tags {
263    
264          my @tags;          my @tags;
265    
266          cmd( 'D6 00  05   FE     00  05         FA40', "scan for tags, retry $_",          cmd( 'D6 00  05   FE     00  05         FA40', "scan for tags",
267                   'D6 00  0F   FE  00 00  05 ', sub { # 01 E00401003123AA26  941A         # seen, serial length: 8                   'D6 00  0F   FE  00 00  05 ', sub { # 01 E00401003123AA26  941A         # seen, serial length: 8
268                          my $rest = shift || die "no rest?";                          my $rest = shift || die "no rest?";
269                          my $nr = ord( substr( $rest, 0, 1 ) );                          my $nr = ord( substr( $rest, 0, 1 ) );
270    
271                          if ( ! $nr ) {                          if ( ! $nr ) {
272                                  print "no tags in range\n";                                  _log "no tags in range\n";
273                                  update_visible_tags();                                  update_visible_tags();
274                                  meteor( 'info-none-in-range' );                                  meteor( 'info-none-in-range' );
275                                  $tags_data = {};                                  $tags_data = {};
276                          } else {                          } else {
277    
278                                  my $tags = substr( $rest, 1 );                                  my $tags = substr( $rest, 1 );
   
279                                  my $tl = length( $tags );                                  my $tl = length( $tags );
280                                  die "wrong length $tl for $nr tags: ",dump( $tags ) if $tl =! $nr * 8;                                  die "wrong length $tl for $nr tags: ",dump( $tags ) if $tl =! $nr * 8;
281    
282                                  push @tags, uc(unpack('H16', substr($tags, $_ * 8, 8))) foreach ( 0 .. $nr - 1 );                                  push @tags, uc(unpack('H16', substr($tags, $_ * 8, 8))) foreach ( 0 .. $nr - 1 );
283                                  warn "## tags ",as_hex($tags), " [$tl] = ",dump( $tags ) if $debug;                                  warn "## tags ",as_hex($tags), " [$tl] = ",dump( $tags ) if $debug;
284                                  print "$nr tags in range: ", join(',', @tags ) , "\n";                                  _log "$nr tags in range: ", join(',', @tags ) , "\n";
285    
286                                  meteor( 'info-in-range', join(' ',@tags));                                  meteor( 'info-in-range', join(' ',@tags));
287    
# Line 264  sub scan_for_tags { Line 290  sub scan_for_tags {
290                  }                  }
291          );          );
292    
293          warn "## tags: ",dump( @tags );          diag "tags: ",dump( @tags );
294          return $tags_data;          return $tags_data;
295    
296  }  }
# Line 286  sub update_visible_tags { Line 312  sub update_visible_tags {
312          $visible_tags = {};          $visible_tags = {};
313    
314          foreach my $tag ( @tags ) {          foreach my $tag ( @tags ) {
315                    $visible_tags->{$tag}++;
316                  if ( ! defined $last_visible_tags->{$tag} ) {                  if ( ! defined $last_visible_tags->{$tag} ) {
317                          if ( defined $tags_data->{$tag} ) {                          if ( defined $tags_data->{$tag} ) {
318  #                               meteor( 'in-range', $tag );  #                               meteor( 'in-range', $tag );
# Line 293  sub update_visible_tags { Line 320  sub update_visible_tags {
320                                  meteor( 'read', $tag );                                  meteor( 'read', $tag );
321                                  read_tag( $tag );                                  read_tag( $tag );
322                          }                          }
                         $visible_tags->{$tag}++;  
323                  } else {                  } else {
324                          warn "## using cached data for $tag" if $debug;                          warn "## using cached data for $tag" if $debug;
325                  }                  }
# Line 409  sub read_tag { Line 435  sub read_tag {
435                          ( $from_tag, $security ) = ( substr($rest,0,8), substr($rest,8,1) );                          ( $from_tag, $security ) = ( substr($rest,0,8), substr($rest,8,1) );
436                          die "security from other tag: ",as_hex( $from_tag ) if $from_tag ne str2bytes( $tag );                          die "security from other tag: ",as_hex( $from_tag ) if $from_tag ne str2bytes( $tag );
437                          $security = as_hex( $security );                          $security = as_hex( $security );
438                            $tags_security->{$tag} = $security;
439                          warn "# SECURITY $tag = $security\n";                          warn "# SECURITY $tag = $security\n";
440                  }                  }
441          );          );
# Line 634  sub readchunk { Line 661  sub readchunk {
661                  warn "## DISPATCH payload to with rest", dump( $payload, $to, $rest ) if $debug;                  warn "## DISPATCH payload to with rest", dump( $payload, $to, $rest ) if $debug;
662                  $dispatch->{ $to }->( $rest );                  $dispatch->{ $to }->( $rest );
663          } else {          } else {
664                  print "NO DISPATCH for ",dump( $full ),"\n";                  print "NO DISPATCH for ",as_hex( $full ),"\n";
665          }          }
666    
667          return $data;          return $data;

Legend:
Removed from v.44  
changed lines
  Added in v.56

  ViewVC Help
Powered by ViewVC 1.1.26