script/lsec
changeset 563 d113b63c55c4
child 576 158c5a3d0a2a
child 1744 7bc131b92039
equal deleted inserted replaced
562:dda32c8294ad 563:d113b63c55c4
       
     1 #!/usr/bin/perl
       
     2 
       
     3 #------------------------------------------------------------------------------
       
     4 #
       
     5 #  l s e c  -  List EtherCAT
       
     6 #
       
     7 #  Userspace tool for listing EtherCAT slaves.
       
     8 #
       
     9 #  $Id$
       
    10 #
       
    11 #  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
       
    12 #
       
    13 #  This file is part of the IgH EtherCAT Master.
       
    14 #
       
    15 #  The IgH EtherCAT Master is free software; you can redistribute it
       
    16 #  and/or modify it under the terms of the GNU General Public License
       
    17 #  as published by the Free Software Foundation; either version 2 of the
       
    18 #  License, or (at your option) any later version.
       
    19 #
       
    20 #  The IgH EtherCAT Master is distributed in the hope that it will be
       
    21 #  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    22 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    23 #  GNU General Public License for more details.
       
    24 #
       
    25 #  You should have received a copy of the GNU General Public License
       
    26 #  along with the IgH EtherCAT Master; if not, write to the Free Software
       
    27 #  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    28 #
       
    29 #  The right to use EtherCAT Technology is granted and comes free of
       
    30 #  charge under condition of compatibility of product made by
       
    31 #  Licensee. People intending to distribute/sell products based on the
       
    32 #  code, have to sign an agreement to guarantee that products using
       
    33 #  software based on IgH EtherCAT master stay compatible with the actual
       
    34 #  EtherCAT specification (which are released themselves as an open
       
    35 #  standard) as the (only) precondition to have the right to use EtherCAT
       
    36 #  Technology, IP and trade marks.
       
    37 #
       
    38 #------------------------------------------------------------------------------
       
    39 
       
    40 require 'sys/ioctl.ph';
       
    41 
       
    42 use strict;
       
    43 use Getopt::Std;
       
    44 
       
    45 my %opt;
       
    46 my $master_index;
       
    47 my $master_dir;
       
    48 my $term_width;
       
    49 
       
    50 #------------------------------------------------------------------------------
       
    51 
       
    52 $term_width = &get_terminal_width;
       
    53 &get_options;
       
    54 &query_master;
       
    55 exit 0;
       
    56 
       
    57 #------------------------------------------------------------------------------
       
    58 
       
    59 sub query_master
       
    60 {
       
    61     $master_dir = "/sys/ethercat" . $master_index;
       
    62     &query_slaves;
       
    63 }
       
    64 
       
    65 #------------------------------------------------------------------------------
       
    66 
       
    67 sub query_slaves
       
    68 {
       
    69     my $dirhandle;
       
    70     my $entry;
       
    71     my @slaves;
       
    72     my $slave;
       
    73     my $abs;
       
    74     my $line;
       
    75     my $ring_cols;
       
    76     my $adv_cols;
       
    77     my $fmt;
       
    78     my $cols;
       
    79 
       
    80     unless (opendir $dirhandle, $master_dir) {
       
    81 		print "Failed to open directory \"$master_dir\".\n";
       
    82 		exit 1;
       
    83     }
       
    84 
       
    85     while ($entry = readdir $dirhandle) {
       
    86         next unless $entry =~ /^slave(\d+)$/;
       
    87 
       
    88 		$slave = {};
       
    89 
       
    90 		open INFO, "$master_dir/$entry/info" or die
       
    91 			"ERROR: Failed to open $master_dir/$entry/info";
       
    92 
       
    93 		while ($line = <INFO>) {
       
    94 			if ($line =~ /^Name: (.*)$/) {
       
    95 				$slave->{'name'} = $1;
       
    96 			}
       
    97 			elsif ($line =~ /^Ring position: (\d+)$/) {
       
    98 				$slave->{'ring_position'} = $1;
       
    99 			}
       
   100 			elsif ($line =~ /^Advanced position: (\d+:\d+)$/) {
       
   101 				$slave->{'advanced_position'} = $1;
       
   102 			}
       
   103 			elsif ($line =~ /^State: (.+) /) {
       
   104 				$slave->{'state'} = $1;
       
   105 			}
       
   106 			elsif ($line =~ /^Coupler: ([a-z]+)$/) {
       
   107 				$slave->{'coupler'} = $1;
       
   108 			}
       
   109 			elsif ($line =~ /^Current consumption: (-?\d+) mA$/) {
       
   110 				$slave->{'current'} = $1;
       
   111 			}
       
   112 		}
       
   113 
       
   114 		close INFO;
       
   115 
       
   116 		push @slaves, $slave;
       
   117     }
       
   118     closedir $dirhandle;
       
   119 
       
   120     @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves;
       
   121 
       
   122     $ring_cols = 0;
       
   123     $adv_cols = 0;
       
   124     for $slave (@slaves) {
       
   125 	$cols = length $slave->{'ring_position'};
       
   126 	$ring_cols = $cols if ($cols > $ring_cols);
       
   127 	$cols = length $slave->{'advanced_position'};
       
   128 	$adv_cols = $cols if ($cols > $adv_cols);
       
   129     }
       
   130 
       
   131     if (defined $opt{'c'}) { # display power consumtion
       
   132 	$fmt = sprintf " %%%is  %%-%is  %%6i  %%6i  %%s\n",
       
   133 	    $ring_cols, $adv_cols;
       
   134 
       
   135 	my $current_sum = 0;
       
   136 	for $slave (@slaves) {
       
   137 	    if ($slave->{'coupler'} eq "yes") {
       
   138 		&print_line if !defined $opt{n};
       
   139 		$current_sum = 0; # reset current sum
       
   140 	    }
       
   141 	    $current_sum -= $slave->{'current'};
       
   142 	    printf($fmt, $slave->{'ring_position'},
       
   143 		   $slave->{'advanced_position'}, $slave->{'current'},
       
   144 		   $current_sum, $slave->{'name'});
       
   145 	}
       
   146     }
       
   147     else {
       
   148 	$fmt = sprintf " %%%is  %%-%is  %%-6s  %%s\n", $ring_cols, $adv_cols;
       
   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     }
       
   173 }
       
   174 
       
   175 #------------------------------------------------------------------------------
       
   176 
       
   177 sub print_usage
       
   178 {
       
   179     my $cmd = `basename $0`;
       
   180     chomp $cmd;
       
   181     print "Usage: $cmd [OPTIONS]\n";
       
   182     print "        -m <IDX>    Query master <IDX>.\n";
       
   183     print "        -c          Display current [mA] ";
       
   184     print "(3: consumption, 4: remaining).\n";
       
   185     print "        -n          Display no coupler lines.\n";
       
   186     print "        -h          Show this help.\n";
       
   187     exit 0;
       
   188 }
       
   189 
       
   190 #------------------------------------------------------------------------------
       
   191 
       
   192 sub get_terminal_width
       
   193 {
       
   194     my $winsize;
       
   195     die "no TIOCGWINSZ " unless defined &TIOCGWINSZ;
       
   196     open(TTY, "+</dev/tty") or die "No tty: $!";
       
   197     unless (ioctl(TTY, &TIOCGWINSZ, $winsize='')) {
       
   198 	die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ;
       
   199     }
       
   200     (my $row, my $col, my $xpixel, my $ypixel) = unpack('S4', $winsize);
       
   201     return $col;
       
   202 }
       
   203 #------------------------------------------------------------------------------
       
   204 
       
   205 sub print_line
       
   206 {
       
   207     for (my $i = 0; $i < $term_width; $i++) {print "-";}
       
   208     print "\n";
       
   209 }
       
   210 
       
   211 #------------------------------------------------------------------------------