script/lsec
branchstable-1.3
changeset 1744 7bc131b92039
parent 1740 fa0af75bfbe9
child 1751 b6cfd85db58e
--- a/script/lsec	Fri Aug 10 15:08:44 2007 +0000
+++ b/script/lsec	Fri Aug 10 15:27:08 2007 +0000
@@ -44,131 +44,131 @@
 
 my %opt;
 my $master_index;
-my $master_dir;
 my $term_width;
 
 #------------------------------------------------------------------------------
 
 $term_width = &get_terminal_width;
 &get_options;
-&query_master;
+&query;
 exit 0;
 
 #------------------------------------------------------------------------------
 
-sub query_master
-{
-    $master_dir = "/sys/ethercat" . $master_index;
-    &query_slaves;
-}
-
-#------------------------------------------------------------------------------
-
-sub query_slaves
-{
+sub query
+{
+    my $master_dir;
     my $dirhandle;
     my $entry;
+    my $slave_info_file;
     my @slaves;
     my $slave;
     my $abs;
     my $line;
-    my $ring_cols;
-    my $adv_cols;
+    my $ring_col_width;
+    my $alias_col_width;
     my $fmt;
-    my $cols;
+    my $width;
+    my $last_alias;
+    my $alias_index;
+
+    $master_dir = "/sys/ethercat/master" . $master_index;
 
     unless (opendir $dirhandle, $master_dir) {
-		print "Failed to open directory \"$master_dir\".\n";
-		exit 1;
+        print "Failed to open directory \"$master_dir\".\n";
+        exit 1;
     }
 
     while ($entry = readdir $dirhandle) {
         next unless $entry =~ /^slave(\d+)$/;
 
-		$slave = {};
-
-		open INFO, "$master_dir/$entry/info" or die
-			"ERROR: Failed to open $master_dir/$entry/info";
-
-		while ($line = <INFO>) {
-			if ($line =~ /^Name: (.*)$/) {
-				$slave->{'name'} = $1;
-			}
-			elsif ($line =~ /^Ring position: (\d+)$/) {
-				$slave->{'ring_position'} = $1;
-			}
-			elsif ($line =~ /^Advanced position: (\d+:\d+)$/) {
-				$slave->{'advanced_position'} = $1;
-			}
-			elsif ($line =~ /^State: (.+) /) {
-				$slave->{'state'} = $1;
-			}
-			elsif ($line =~ /^Coupler: ([a-z]+)$/) {
-				$slave->{'coupler'} = $1;
-			}
-			elsif ($line =~ /^Current consumption: (-?\d+) mA$/) {
-				$slave->{'current'} = $1;
-			}
-		}
-
-		close INFO;
-
-		push @slaves, $slave;
+        $slave = {};
+        $slave_info_file = "$master_dir/$entry/info"; 
+        open INFO, $slave_info_file or die
+            "ERROR: Failed to open $slave_info_file.";
+
+        while ($line = <INFO>) {
+            if ($line =~ /^Name: (.*)$/) {
+                $slave->{'name'} = $1;
+            }
+            elsif ($line =~ /^Ring position: (\d+)$/) {
+                $slave->{'ring_position'} = $1;
+            }
+            elsif ($line =~ /Configured station alias: .* \((\d+)\)$/) {
+                $slave->{'alias'} = $1;
+            }
+            elsif ($line =~ /^State: (.+) /) {
+                $slave->{'state'} = $1;
+            }
+            elsif ($line =~ /^Current consumption: (-?\d+) mA$/) {
+                $slave->{'current'} = $1;
+            }
+        }
+
+        close INFO;
+        push @slaves, $slave;
     }
     closedir $dirhandle;
 
     @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves;
 
-    $ring_cols = 0;
-    $adv_cols = 0;
+    # create field addresses and calculate column widths
+    $ring_col_width = 0;
+    $alias_col_width = 0;
+    $last_alias = "";
     for $slave (@slaves) {
-	$cols = length $slave->{'ring_position'};
-	$ring_cols = $cols if ($cols > $ring_cols);
-	$cols = length $slave->{'advanced_position'};
-	$adv_cols = $cols if ($cols > $adv_cols);
+        if ($slave->{'alias'}) {
+            $last_alias = $slave->{'alias'};
+            $alias_index = 0;
+        }
+        if ($last_alias) {
+            $slave->{'field_address'} = "#" . $last_alias . ":" . $alias_index;
+            $width = length $slave->{'field_address'};
+            $alias_col_width = $width if ($width > $alias_col_width);
+        }
+        $width = length $slave->{'ring_position'};
+        $ring_col_width = $width if ($width > $ring_col_width);
+        $alias_index++;
     }
 
     if (defined $opt{'c'}) { # display power consumtion
-	$fmt = sprintf " %%%is  %%-%is  %%6i  %%6i  %%s\n",
-	    $ring_cols, $adv_cols;
-
-	my $current_sum = 0;
-	for $slave (@slaves) {
-	    if ($slave->{'coupler'} eq "yes") {
-		&print_line if !defined $opt{n};
-		$current_sum = 0; # reset current sum
-	    }
-	    $current_sum -= $slave->{'current'};
-	    printf($fmt, $slave->{'ring_position'},
-		   $slave->{'advanced_position'}, $slave->{'current'},
-		   $current_sum, $slave->{'name'});
-	}
+        $fmt = sprintf " %%%is  %%%is  %%6i  %%6i  %%s\n",
+            $ring_col_width, $alias_col_width;
+
+        my $current_sum = 0;
+        for $slave (@slaves) {
+            &print_line if $slave->{'alias'} and !defined $opt{n};
+            $current_sum = 0 if $slave->{'current'} < 0;
+            $current_sum -= $slave->{'current'};
+            printf($fmt, $slave->{'ring_position'}, $slave->{'field_address'},
+                    $slave->{'current'}, $current_sum, $slave->{'name'});
+        }
+    }
+    else { # normal display
+        $fmt = sprintf " %%%is  %%%is  %%-6s  %%s\n",
+            $ring_col_width, $alias_col_width;
+
+        for $slave (@slaves) {
+            &print_line if $slave->{'alias'} and !defined $opt{n};
+            printf($fmt, $slave->{'ring_position'}, $slave->{'field_address'},
+                    $slave->{'state'}, $slave->{'name'});
+        }
+    }
+}
+
+#------------------------------------------------------------------------------
+
+sub get_options
+{
+    my $optret = getopts "m:cnh", \%opt;
+
+    &print_usage if defined $opt{h} or $#ARGV > -1 or !$optret;
+
+    if (defined $opt{m}) {
+        $master_index = $opt{m};
     }
     else {
-	$fmt = sprintf " %%%is  %%-%is  %%-6s  %%s\n", $ring_cols, $adv_cols;
-
-	for $slave (@slaves) {
-	    &print_line if $slave->{'coupler'} eq "yes" and !defined $opt{n};
-	    printf($fmt, $slave->{'ring_position'},
-		   $slave->{'advanced_position'}, $slave->{'state'},
-		   $slave->{'name'});
-	}
-    }
-}
-
-#------------------------------------------------------------------------------
-
-sub get_options
-{
-    my $optret = getopts "m:cnh", \%opt;
-
-    &print_usage if defined $opt{h} or $#ARGV > -1 or !$optret;
-
-    if (defined $opt{m}) {
-	$master_index = $opt{m};
-    }
-    else {
-	$master_index = 0;
+        $master_index = 0;
     }
 }
 
@@ -195,7 +195,7 @@
     die "no TIOCGWINSZ " unless defined &TIOCGWINSZ;
     open(TTY, "+</dev/tty") or die "No tty: $!";
     unless (ioctl(TTY, &TIOCGWINSZ, $winsize='')) {
-	die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ;
+        die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ;
     }
     (my $row, my $col, my $xpixel, my $ypixel) = unpack('S4', $winsize);
     return $col;
@@ -204,7 +204,9 @@
 
 sub print_line
 {
-    for (my $i = 0; $i < $term_width; $i++) {print "-";}
+    for (my $i = 0; $i < $term_width; $i++) {
+        print "-";
+    }
     print "\n";
 }