script/lsec
branchstable-1.3
changeset 1744 7bc131b92039
parent 1740 fa0af75bfbe9
child 1751 b6cfd85db58e
equal deleted inserted replaced
1743:1a7067207637 1744:7bc131b92039
    42 use strict;
    42 use strict;
    43 use Getopt::Std;
    43 use Getopt::Std;
    44 
    44 
    45 my %opt;
    45 my %opt;
    46 my $master_index;
    46 my $master_index;
    47 my $master_dir;
       
    48 my $term_width;
    47 my $term_width;
    49 
    48 
    50 #------------------------------------------------------------------------------
    49 #------------------------------------------------------------------------------
    51 
    50 
    52 $term_width = &get_terminal_width;
    51 $term_width = &get_terminal_width;
    53 &get_options;
    52 &get_options;
    54 &query_master;
    53 &query;
    55 exit 0;
    54 exit 0;
    56 
    55 
    57 #------------------------------------------------------------------------------
    56 #------------------------------------------------------------------------------
    58 
    57 
    59 sub query_master
    58 sub query
    60 {
    59 {
    61     $master_dir = "/sys/ethercat" . $master_index;
    60     my $master_dir;
    62     &query_slaves;
       
    63 }
       
    64 
       
    65 #------------------------------------------------------------------------------
       
    66 
       
    67 sub query_slaves
       
    68 {
       
    69     my $dirhandle;
    61     my $dirhandle;
    70     my $entry;
    62     my $entry;
       
    63     my $slave_info_file;
    71     my @slaves;
    64     my @slaves;
    72     my $slave;
    65     my $slave;
    73     my $abs;
    66     my $abs;
    74     my $line;
    67     my $line;
    75     my $ring_cols;
    68     my $ring_col_width;
    76     my $adv_cols;
    69     my $alias_col_width;
    77     my $fmt;
    70     my $fmt;
    78     my $cols;
    71     my $width;
       
    72     my $last_alias;
       
    73     my $alias_index;
       
    74 
       
    75     $master_dir = "/sys/ethercat/master" . $master_index;
    79 
    76 
    80     unless (opendir $dirhandle, $master_dir) {
    77     unless (opendir $dirhandle, $master_dir) {
    81 		print "Failed to open directory \"$master_dir\".\n";
    78         print "Failed to open directory \"$master_dir\".\n";
    82 		exit 1;
    79         exit 1;
    83     }
    80     }
    84 
    81 
    85     while ($entry = readdir $dirhandle) {
    82     while ($entry = readdir $dirhandle) {
    86         next unless $entry =~ /^slave(\d+)$/;
    83         next unless $entry =~ /^slave(\d+)$/;
    87 
    84 
    88 		$slave = {};
    85         $slave = {};
    89 
    86         $slave_info_file = "$master_dir/$entry/info"; 
    90 		open INFO, "$master_dir/$entry/info" or die
    87         open INFO, $slave_info_file or die
    91 			"ERROR: Failed to open $master_dir/$entry/info";
    88             "ERROR: Failed to open $slave_info_file.";
    92 
    89 
    93 		while ($line = <INFO>) {
    90         while ($line = <INFO>) {
    94 			if ($line =~ /^Name: (.*)$/) {
    91             if ($line =~ /^Name: (.*)$/) {
    95 				$slave->{'name'} = $1;
    92                 $slave->{'name'} = $1;
    96 			}
    93             }
    97 			elsif ($line =~ /^Ring position: (\d+)$/) {
    94             elsif ($line =~ /^Ring position: (\d+)$/) {
    98 				$slave->{'ring_position'} = $1;
    95                 $slave->{'ring_position'} = $1;
    99 			}
    96             }
   100 			elsif ($line =~ /^Advanced position: (\d+:\d+)$/) {
    97             elsif ($line =~ /Configured station alias: .* \((\d+)\)$/) {
   101 				$slave->{'advanced_position'} = $1;
    98                 $slave->{'alias'} = $1;
   102 			}
    99             }
   103 			elsif ($line =~ /^State: (.+) /) {
   100             elsif ($line =~ /^State: (.+) /) {
   104 				$slave->{'state'} = $1;
   101                 $slave->{'state'} = $1;
   105 			}
   102             }
   106 			elsif ($line =~ /^Coupler: ([a-z]+)$/) {
   103             elsif ($line =~ /^Current consumption: (-?\d+) mA$/) {
   107 				$slave->{'coupler'} = $1;
   104                 $slave->{'current'} = $1;
   108 			}
   105             }
   109 			elsif ($line =~ /^Current consumption: (-?\d+) mA$/) {
   106         }
   110 				$slave->{'current'} = $1;
   107 
   111 			}
   108         close INFO;
   112 		}
   109         push @slaves, $slave;
   113 
       
   114 		close INFO;
       
   115 
       
   116 		push @slaves, $slave;
       
   117     }
   110     }
   118     closedir $dirhandle;
   111     closedir $dirhandle;
   119 
   112 
   120     @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves;
   113     @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves;
   121 
   114 
   122     $ring_cols = 0;
   115     # create field addresses and calculate column widths
   123     $adv_cols = 0;
   116     $ring_col_width = 0;
       
   117     $alias_col_width = 0;
       
   118     $last_alias = "";
   124     for $slave (@slaves) {
   119     for $slave (@slaves) {
   125 	$cols = length $slave->{'ring_position'};
   120         if ($slave->{'alias'}) {
   126 	$ring_cols = $cols if ($cols > $ring_cols);
   121             $last_alias = $slave->{'alias'};
   127 	$cols = length $slave->{'advanced_position'};
   122             $alias_index = 0;
   128 	$adv_cols = $cols if ($cols > $adv_cols);
   123         }
       
   124         if ($last_alias) {
       
   125             $slave->{'field_address'} = "#" . $last_alias . ":" . $alias_index;
       
   126             $width = length $slave->{'field_address'};
       
   127             $alias_col_width = $width if ($width > $alias_col_width);
       
   128         }
       
   129         $width = length $slave->{'ring_position'};
       
   130         $ring_col_width = $width if ($width > $ring_col_width);
       
   131         $alias_index++;
   129     }
   132     }
   130 
   133 
   131     if (defined $opt{'c'}) { # display power consumtion
   134     if (defined $opt{'c'}) { # display power consumtion
   132 	$fmt = sprintf " %%%is  %%-%is  %%6i  %%6i  %%s\n",
   135         $fmt = sprintf " %%%is  %%%is  %%6i  %%6i  %%s\n",
   133 	    $ring_cols, $adv_cols;
   136             $ring_col_width, $alias_col_width;
   134 
   137 
   135 	my $current_sum = 0;
   138         my $current_sum = 0;
   136 	for $slave (@slaves) {
   139         for $slave (@slaves) {
   137 	    if ($slave->{'coupler'} eq "yes") {
   140             &print_line if $slave->{'alias'} and !defined $opt{n};
   138 		&print_line if !defined $opt{n};
   141             $current_sum = 0 if $slave->{'current'} < 0;
   139 		$current_sum = 0; # reset current sum
   142             $current_sum -= $slave->{'current'};
   140 	    }
   143             printf($fmt, $slave->{'ring_position'}, $slave->{'field_address'},
   141 	    $current_sum -= $slave->{'current'};
   144                     $slave->{'current'}, $current_sum, $slave->{'name'});
   142 	    printf($fmt, $slave->{'ring_position'},
   145         }
   143 		   $slave->{'advanced_position'}, $slave->{'current'},
   146     }
   144 		   $current_sum, $slave->{'name'});
   147     else { # normal display
   145 	}
   148         $fmt = sprintf " %%%is  %%%is  %%-6s  %%s\n",
       
   149             $ring_col_width, $alias_col_width;
       
   150 
       
   151         for $slave (@slaves) {
       
   152             &print_line if $slave->{'alias'} and !defined $opt{n};
       
   153             printf($fmt, $slave->{'ring_position'}, $slave->{'field_address'},
       
   154                     $slave->{'state'}, $slave->{'name'});
       
   155         }
       
   156     }
       
   157 }
       
   158 
       
   159 #------------------------------------------------------------------------------
       
   160 
       
   161 sub get_options
       
   162 {
       
   163     my $optret = getopts "m:cnh", \%opt;
       
   164 
       
   165     &print_usage if defined $opt{h} or $#ARGV > -1 or !$optret;
       
   166 
       
   167     if (defined $opt{m}) {
       
   168         $master_index = $opt{m};
   146     }
   169     }
   147     else {
   170     else {
   148 	$fmt = sprintf " %%%is  %%-%is  %%-6s  %%s\n", $ring_cols, $adv_cols;
   171         $master_index = 0;
   149 
       
   150 	for $slave (@slaves) {
       
   151 	    &print_line if $slave->{'coupler'} eq "yes" and !defined $opt{n};
       
   152 	    printf($fmt, $slave->{'ring_position'},
       
   153 		   $slave->{'advanced_position'}, $slave->{'state'},
       
   154 		   $slave->{'name'});
       
   155 	}
       
   156     }
       
   157 }
       
   158 
       
   159 #------------------------------------------------------------------------------
       
   160 
       
   161 sub get_options
       
   162 {
       
   163     my $optret = getopts "m:cnh", \%opt;
       
   164 
       
   165     &print_usage if defined $opt{h} or $#ARGV > -1 or !$optret;
       
   166 
       
   167     if (defined $opt{m}) {
       
   168 	$master_index = $opt{m};
       
   169     }
       
   170     else {
       
   171 	$master_index = 0;
       
   172     }
   172     }
   173 }
   173 }
   174 
   174 
   175 #------------------------------------------------------------------------------
   175 #------------------------------------------------------------------------------
   176 
   176 
   193 {
   193 {
   194     my $winsize;
   194     my $winsize;
   195     die "no TIOCGWINSZ " unless defined &TIOCGWINSZ;
   195     die "no TIOCGWINSZ " unless defined &TIOCGWINSZ;
   196     open(TTY, "+</dev/tty") or die "No tty: $!";
   196     open(TTY, "+</dev/tty") or die "No tty: $!";
   197     unless (ioctl(TTY, &TIOCGWINSZ, $winsize='')) {
   197     unless (ioctl(TTY, &TIOCGWINSZ, $winsize='')) {
   198 	die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ;
   198         die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ;
   199     }
   199     }
   200     (my $row, my $col, my $xpixel, my $ypixel) = unpack('S4', $winsize);
   200     (my $row, my $col, my $xpixel, my $ypixel) = unpack('S4', $winsize);
   201     return $col;
   201     return $col;
   202 }
   202 }
   203 #------------------------------------------------------------------------------
   203 #------------------------------------------------------------------------------
   204 
   204 
   205 sub print_line
   205 sub print_line
   206 {
   206 {
   207     for (my $i = 0; $i < $term_width; $i++) {print "-";}
   207     for (my $i = 0; $i < $term_width; $i++) {
       
   208         print "-";
       
   209     }
   208     print "\n";
   210     print "\n";
   209 }
   211 }
   210 
   212 
   211 #------------------------------------------------------------------------------
   213 #------------------------------------------------------------------------------