[Tinyos-commits] CVS: tinyos-1.x/tools/src/motelist motelist-linux-2.6, NONE, 1.1

Cory Sharp cssharp at users.sourceforge.net
Wed Jul 27 13:46:32 PDT 2005


Update of /cvsroot/tinyos/tinyos-1.x/tools/src/motelist
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2432

Added Files:
	motelist-linux-2.6 
Log Message:
Motelist for Linux kernels 2.6.x that requires only the /sys filesystem.
This means superuser access is no longer necessary to read special files.


--- NEW FILE: motelist-linux-2.6 ---
#!/usr/bin/perl -w
use strict;
# $Id: motelist-linux-2.6,v 1.1 2005/07/27 20:46:29 cssharp Exp $
# @author Cory Sharp <cory at moteiv.com>

my $help = <<'EOF';
usage: motelist [-l] [-c] [device_list]

  $Revision: 1.1 $

options:
  -h  display this help
  -c  compact format, not pretty but easier for parsing
  -dev_prefix  force the device prefix for the serial device
  -usb  display extra usb information
EOF

my %Opt = (
  compact => 0,
  usb => 0,
  dev_prefix => [ "/dev/usb/tts/", "/dev/ttyUSB", "/dev/tts/USB" ],
);

while (@ARGV) {
  last unless $ARGV[0] =~ /^-/;
  my $opt = shift @ARGV;
  if( $opt eq "-h" ) { print "$help\n"; exit 0; }
  elsif( $opt eq "-c" ) { $Opt{compact} = 1; }
  elsif( $opt eq "-dev_prefix" ) { $Opt{dev_prefix} = shift @ARGV; }
  elsif( $opt eq "-usb" ) { $Opt{usb} = 1; }
  else { print STDERR "$help\nerror, unknown command line option $opt\n"; exit 1; }
}

#  Scan /sys/bus/usb/drivers/usb for FTDI devices
my @ftdidevs =
  grep { ($_->{idVendor}||"") eq "0403" && ($_->{idProduct}||"") eq "6001" }
  map { {
    sysusb => $_,
    idVendor => snarf("$_/idVendor",1),
    idProduct => snarf("$_/idProduct",1),
  } }
  glob("/sys/bus/usb/drivers/usb/*");

#  If none were found, quit
if( @ftdidevs == 0 ) {
  print "No devices found.\n";
  exit 0;
}

#  Populate each FTDI device with more complete information
for my $f (@ftdidevs) {
  my $sysusb = $f->{sysusb};

  $f->{manufacturer} = snarf("$sysusb/manufacturer",1);
  $f->{product} = snarf("$sysusb/product",1);
  $f->{serial} = snarf("$sysusb/serial",1);
  $f->{ndev} = snarf("$sysusb/devnum",1);

  my $devstr = readlink($sysusb);
  if( $devstr =~ m{([^/]+)/usb(\d+)/.*-([^/]+)$} ) {
    $f->{usbpath} = "usb-$1-$3";
    $f->{nbus} = $2;
  }
  ($f->{devnum} = $sysusb) =~ s{^.*/}{};

  my $port = "$f->{sysusb}/$f->{devnum}:1.0";
  ($f->{driver} = readlink("$port/driver")) =~ s{^.*/}{} if -l "$port/driver";
  ($f->{serialname} = (glob("$port/tty*"),undef)[0]) =~ s{^.*/}{};
  $f->{serialnum} = $1 if $f->{serialname} =~ /(\d+)/;

  #  For each device, force to use dev_prefix if it's set and not "auto".
  #  Otherwise, check a set of standard prefixes, and commit to the first one
  #  that actually exists.
  my $serialdev = " (unknown)";
  if( exists $f->{serialnum} ) {
    if( ref($Opt{dev_prefix}) eq "ARRAY" ) {
      for my $prefix (@{$Opt{dev_prefix}}) {
	my $file = $prefix . $f->{serialnum};
	if( -e $file ) { $serialdev = $file; last; }
      }
    } else {
      $serialdev = $Opt{dev_prefix} . $f->{serialnum};
    }
  }
  $f->{serialdev} = $serialdev;
}

#  Print a header
if( !$Opt{compact} ) {
  if( $Opt{usb} ) {
    print << "EOF" unless $Opt{compact};
Bus Dev USB Path                 Reference  Device           Description
--- --- ------------------------ ---------- ---------------- -------------------------------------
EOF
  } else {
    print << "EOF" unless $Opt{compact};
Reference  Device           Description
---------- ---------------- ---------------------------------------------
EOF
  }
}

#  Print the usb information
for my $dev (sort { cmp_usbdev($a,$b) } @ftdidevs) {
  my $desc = join( " ", $dev->{manufacturer}||"", $dev->{product}||"" ) || " (none)";
  my @output = ( $dev->{serial}||" (none)", $dev->{serialdev}, $desc );
  @output = ( $dev->{nbus}, $dev->{ndev}, $dev->{usbpath}, @output ) if $Opt{usb};
  if( $Opt{compact} ) {
    print join(",", at output) . "\n";
  } else {
    printf( ($Opt{usb}?"%3d %3d %-24s ":"")."%-10s %-16s %s\n", @output );
  }
}

sub cmp_usbdev {
  my ($a,$b) = @_;
  if( defined $a->{serialnum} ) {
    if( defined $b->{serialnum} ) {
      return $a->{serialnum} <=> $b->{serialnum};
    }
    return -1;
  }
  return 1 if defined $b->{serialnum};
  return ($a->{serial}||"") cmp ($b->{serial}||"");
}

sub snarf {
  open my $fh, $_[0] or return undef;
  my $text = do{local $/;<$fh>};
  close $fh;
  $text =~ s/\s+$// if $_[1];
  return $text;
}




More information about the Tinyos-commits mailing list