/usr/bin/motelist is in tinyos-tools 1.4.2-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | #!/usr/bin/perl -w
use strict;
# $Id: motelist-linux,v 1.4 2006-12-12 18:23:01 vlahan Exp $
# @author Cory Sharp <cory@moteiv.com>
# @author Joe Polastre
my $help = <<'EOF';
usage: motelist [options]
$Revision: 1.4 $
options:
-h display this help
-c compact format, not pretty but easier for parsing
-f specify the usb-serial file (for smote.cs)
-k kernel version: 2.4, 2.6, auto (default)
-m method to scan usb: procfs, sysfs, auto (default)
-dev_prefix force the device prefix for the serial device
-usb display extra usb information
EOF
my %Opt = (
compact => 0,
usb => 0,
method => "auto",
kernel => "auto",
dev_prefix => [ "/dev/usb/tts/", "/dev/ttyUSB", "/dev/tts/USB" ],
usbserial => "sudo cat /proc/tty/driver/usbserial |",
);
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 "-f" ) { $Opt{usbserial} = shift @ARGV; }
elsif( $opt eq "-k" ) { $Opt{kernel} = shift @ARGV; }
elsif( $opt eq "-m" ) { $Opt{method} = shift @ARGV; }
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; }
}
if( $Opt{kernel} eq "auto" ) {
$Opt{kernel} = "unknown";
$Opt{kernel} = $1 if snarf("/proc/version") =~ /\bLinux version (\d+\.\d+)/;
}
if( $Opt{method} eq "auto" ) {
$Opt{method} = ($Opt{kernel} eq "2.4") ? "procfs" : "sysfs";
}
my @devs = $Opt{method} eq "procfs" ? scan_procfs() : scan_sysfs();
print_motelist( sort { cmp_usbdev($a,$b) } @devs );
#
# SysFS
#
sub scan_sysfs {
# Scan /sys/bus/usb/drivers/usb for FTDI or CP210X devices
my @ftdidevs =
grep { (($_->{UsbVendor}||"") eq "0403" && ($_->{UsbProduct}||"") eq "6001")
|| (($_->{UsbVendor}||"") eq "10c4" && ($_->{UsbProduct}||"") eq "ea60")}
map { {
SysPath => $_,
UsbVendor => snarf("$_/idVendor",1),
UsbProduct => snarf("$_/idProduct",1),
} }
glob("/sys/bus/usb/drivers/usb/*");
# Gather information about each FTDI device
for my $f (@ftdidevs) {
my $syspath = $f->{SysPath};
$f->{InfoManufacturer} = snarf("$syspath/manufacturer",1);
$f->{InfoProduct} = snarf("$syspath/product",1);
$f->{InfoSerial} = snarf("$syspath/serial",1);
$f->{UsbDevNum} = snarf("$syspath/devnum",1);
my $devstr = readlink($syspath);
if( $devstr =~ m{([^/]+)/usb(\d+)/.*-([^/]+)$} ) {
$f->{UsbPath} = "usb-$1-$3";
$f->{UsbBusNum} = $2;
}
($f->{SysDev} = $syspath) =~ s{^.*/}{};
my $port = "$syspath/$f->{SysDev}:1.0";
($f->{DriverName} = readlink("$port/driver")) =~ s{^.*/}{} if -l "$port/driver";
($f->{SerialDevName} = (glob("$port/tty*"),undef)[0]) =~ s{^.*/}{};
$f->{SerialDevNum} = $1 if $f->{SerialDevName} =~ /(\d+)/;
$f->{SerialDevName} = getSerialDevName( $f->{SerialDevNum} ) || " (none)";
}
return @ftdidevs;
}
#
# Scan Procfs
#
sub scan_procfs {
my $text_devs = snarf("< /proc/bus/usb/devices");
my $text_serial = snarf($Opt{usbserial});
my @usbdevs = map { {parse_usb_devices_text($_)} }
grep { !/^\s*$/ } split /\n+(?=T:)/, $text_devs;
my %usbtree = build_usb_tree( @usbdevs );
my %usbserialtree = build_usbserial_tree( $text_serial );
for my $tts ( values %usbserialtree ) {
$usbtree{usbkey($tts->{path})}{usbserial} = $tts if defined $tts->{path};
}
my @ftdidevs = map { {
UsbVendor => $_->{Vendor},
UsbProduct => $_->{ProdID},
InfoManufacturer => $_->{Manufacturer},
InfoProduct => $_->{Product},
InfoSerial => $_->{SerialNumber},
UsbBusNum => $_->{nbus},
UsbDevNum => $_->{ndev},
UsbPath => (($Opt{kernel} eq "2.4") ? $_->{usbserial}{path} : $_->{usbpath}),
DriverName => $_->{driver},
SerialDevNum => $_->{usbserial}{tts},
SerialDevName => getSerialDevName($_->{usbserial}{tts}) || " (none)",
} }
grep { ($_->{Vendor}||"") eq "0403" && ($_->{ProdID}||"") eq "6001" }
values %usbtree;
return @ftdidevs;
}
sub build_usb_tree {
my @devs = @_;
my %tree = ();
for my $dev (sort { $a->{Lev} <=> $b->{Lev} } @devs) {
my ($bus,$lev,$prnt) = ( $dev->{Bus}+0, $dev->{Lev}+0, $dev->{Prnt}+0 );
my $devnum = $dev->{"Dev#"}+0;
$dev->{nbus} = $bus;
$dev->{ndev} = $devnum;
$tree{"bus$bus"} = {} unless exists $tree{"bus$bus"};
$tree{"bus$bus"}{"dev$devnum"} = $dev;
if( $lev == 0 ) {
$dev->{usbpath} = "usb-$dev->{SerialNumber}";
} else {
my $sep = ($lev==1) ? "-" : ".";
$dev->{parent} = $tree{"bus$bus"}{"dev$prnt"};
$dev->{usbpath} = $dev->{parent}{usbpath} . $sep . ($dev->{Port}+1);
}
$tree{usbkey($dev->{usbpath})} = $dev;
}
return %tree;
}
sub parse_usb_devices_text {
my $text = shift;
$text =~ s/^\S+\s*//gm;
return ($text =~ m/([^\s=]+)=\s*(.*?\S)\s*(?=[^\s=]+=|$)/mg);
}
sub build_usbserial_tree {
my $text = shift;
my %tree = ();
while( $text =~ /^([^:]+):(.*)/mg ) {
my ($tts,$params) = ($1,$2);
$tree{$tts} = { tts => $tts };
while ($params =~ m/\s+([^:]+):(?:"([^"]*)"|(\S+))/g) {
$tree{$tts}{$1} = $2||$3;
}
}
return %tree;
}
sub usbkey {
if( $Opt{kernel} eq "2.4" ) {
(my $key = $_[0]) =~ s/^.*-//;
return $key;
}
return $_[0];
}
#
# getSerialDevName
#
# For each device, force to use dev_prefix if it's not an array. Otherwise,
# assume it's a list of candidate prefixes. Check them and commit to the
# first one that actually exists.
#
sub getSerialDevName {
my $devnum = shift;
my $devname = undef;
if( defined $devnum ) {
if( ref($Opt{dev_prefix}) eq "ARRAY" ) {
$devname = $devnum;
for my $prefix (@{$Opt{dev_prefix}}) {
my $file = $prefix . $devnum;
if( -e $file ) { $devname = $file; last; }
}
} else {
$devname = $Opt{dev_prefix} . $devnum;
}
}
return $devname;
}
#
# Print motelist
#
sub print_motelist {
my @devs = @_;
# If none were found, quit
if( @devs == 0 ) {
print "No devices found.\n";
return;
}
# 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) } @devs) {
my $desc = join( " ", $dev->{InfoManufacturer}||"", $dev->{InfoProduct}||"" ) || " (none)";
my @output = ( $dev->{InfoSerial}||" (none)", $dev->{SerialDevName}, $desc );
@output = ( $dev->{UsbBusNum}, $dev->{UsbDevNum}, $dev->{UsbPath}, @output ) if $Opt{usb};
if( $Opt{compact} ) {
print join(",",@output) . "\n";
} else {
printf( ($Opt{usb}?"%3d %3d %-24s ":"")."%-10s %-16s %s\n", @output );
}
}
}
#
# Cmp Usbdev's
#
sub cmp_usbdev {
my ($a,$b) = @_;
if( defined $a->{SerialDevNum} ) {
if( defined $b->{SerialDevNum} ) {
return $a->{SerialDevNum} <=> $b->{SerialDevNum};
}
return -1;
}
return 1 if defined $b->{SerialDevNum};
return ($a->{InfoSerial}||"") cmp ($b->{InfoSerial}||"");
}
#
# Read a file in
#
sub snarf {
open my $fh, $_[0] or return undef;
my $text = do{local $/;<$fh>};
close $fh;
$text =~ s/\s+$// if $_[1];
return $text;
}
|