fp@563: #!/usr/bin/perl
fp@563: 
fp@563: #------------------------------------------------------------------------------
fp@563: #
fp@563: #  l s e c  -  List EtherCAT
fp@563: #
fp@563: #  Userspace tool for listing EtherCAT slaves.
fp@563: #
fp@563: #  $Id$
fp@563: #
fp@563: #  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
fp@563: #
fp@563: #  This file is part of the IgH EtherCAT Master.
fp@563: #
fp@563: #  The IgH EtherCAT Master is free software; you can redistribute it
fp@563: #  and/or modify it under the terms of the GNU General Public License
fp@563: #  as published by the Free Software Foundation; either version 2 of the
fp@563: #  License, or (at your option) any later version.
fp@563: #
fp@563: #  The IgH EtherCAT Master is distributed in the hope that it will be
fp@563: #  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
fp@563: #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
fp@563: #  GNU General Public License for more details.
fp@563: #
fp@563: #  You should have received a copy of the GNU General Public License
fp@563: #  along with the IgH EtherCAT Master; if not, write to the Free Software
fp@563: #  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
fp@563: #
fp@563: #  The right to use EtherCAT Technology is granted and comes free of
fp@563: #  charge under condition of compatibility of product made by
fp@563: #  Licensee. People intending to distribute/sell products based on the
fp@563: #  code, have to sign an agreement to guarantee that products using
fp@563: #  software based on IgH EtherCAT master stay compatible with the actual
fp@563: #  EtherCAT specification (which are released themselves as an open
fp@563: #  standard) as the (only) precondition to have the right to use EtherCAT
fp@563: #  Technology, IP and trade marks.
fp@563: #
fp@563: #------------------------------------------------------------------------------
fp@563: 
fp@563: require 'sys/ioctl.ph';
fp@563: 
fp@563: use strict;
fp@563: use Getopt::Std;
fp@563: 
fp@563: my %opt;
fp@563: my $master_index;
fp@563: my $term_width;
fp@563: 
fp@563: #------------------------------------------------------------------------------
fp@563: 
fp@563: $term_width = &get_terminal_width;
fp@563: &get_options;
fp@576: &query;
fp@563: exit 0;
fp@563: 
fp@563: #------------------------------------------------------------------------------
fp@563: 
fp@576: sub query
fp@576: {
fp@576:     my $master_dir;
fp@563:     my $dirhandle;
fp@563:     my $entry;
fp@643:     my $slave_info_file;
fp@563:     my @slaves;
fp@563:     my $slave;
fp@563:     my $abs;
fp@563:     my $line;
fp@643:     my $ring_col_width;
fp@643:     my $alias_col_width;
fp@563:     my $fmt;
fp@643:     my $width;
fp@643:     my $last_alias;
fp@643:     my $alias_index;
fp@563: 
fp@576:     $master_dir = "/sys/ethercat/master" . $master_index;
fp@576: 
fp@563:     unless (opendir $dirhandle, $master_dir) {
fp@643:         print "Failed to open directory \"$master_dir\".\n";
fp@643:         exit 1;
fp@563:     }
fp@563: 
fp@563:     while ($entry = readdir $dirhandle) {
fp@563:         next unless $entry =~ /^slave(\d+)$/;
fp@563: 
fp@643:         $slave = {};
fp@643:         $slave_info_file = "$master_dir/$entry/info"; 
fp@643:         open INFO, $slave_info_file or die
fp@643:             "ERROR: Failed to open $slave_info_file.";
fp@643: 
fp@643:         while ($line = <INFO>) {
fp@643:             if ($line =~ /^Name: (.*)$/) {
fp@643:                 $slave->{'name'} = $1;
fp@643:             }
fp@643:             elsif ($line =~ /^Ring position: (\d+)$/) {
fp@643:                 $slave->{'ring_position'} = $1;
fp@643:             }
fp@643:             elsif ($line =~ /Configured station alias: .* \((\d+)\)$/) {
fp@643:                 $slave->{'alias'} = $1;
fp@643:             }
fp@643:             elsif ($line =~ /^State: (.+) /) {
fp@643:                 $slave->{'state'} = $1;
fp@643:             }
fp@643:             elsif ($line =~ /^Current consumption: (-?\d+) mA$/) {
fp@643:                 $slave->{'current'} = $1;
fp@643:             }
fp@643:         }
fp@643: 
fp@643:         close INFO;
fp@643:         push @slaves, $slave;
fp@563:     }
fp@563:     closedir $dirhandle;
fp@563: 
fp@563:     @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves;
fp@563: 
fp@643:     # create field addresses and calculate column widths
fp@643:     $ring_col_width = 0;
fp@643:     $alias_col_width = 0;
fp@643:     $last_alias = "";
fp@563:     for $slave (@slaves) {
fp@643:         if ($slave->{'alias'}) {
fp@643:             $last_alias = $slave->{'alias'};
fp@643:             $alias_index = 0;
fp@643:         }
fp@643:         if ($last_alias) {
fp@643:             $slave->{'field_address'} = "#" . $last_alias . ":" . $alias_index;
fp@643:             $width = length $slave->{'field_address'};
fp@643:             $alias_col_width = $width if ($width > $alias_col_width);
fp@643:         }
fp@643:         $width = length $slave->{'ring_position'};
fp@643:         $ring_col_width = $width if ($width > $ring_col_width);
fp@643:         $alias_index++;
fp@563:     }
fp@563: 
fp@563:     if (defined $opt{'c'}) { # display power consumtion
fp@643:         $fmt = sprintf " %%%is  %%%is  %%6i  %%6i  %%s\n",
fp@643:             $ring_col_width, $alias_col_width;
fp@643: 
fp@643:         my $current_sum = 0;
fp@643:         for $slave (@slaves) {
fp@643:             &print_line if $slave->{'alias'} and !defined $opt{n};
fp@643:             $current_sum = 0 if $slave->{'current'} < 0;
fp@643:             $current_sum -= $slave->{'current'};
fp@643:             printf($fmt, $slave->{'ring_position'}, $slave->{'field_address'},
fp@643:                     $slave->{'current'}, $current_sum, $slave->{'name'});
fp@643:         }
fp@643:     }
fp@643:     else { # normal display
fp@643:         $fmt = sprintf " %%%is  %%%is  %%-6s  %%s\n",
fp@643:             $ring_col_width, $alias_col_width;
fp@643: 
fp@643:         for $slave (@slaves) {
fp@643:             &print_line if $slave->{'alias'} and !defined $opt{n};
fp@643:             printf($fmt, $slave->{'ring_position'}, $slave->{'field_address'},
fp@643:                     $slave->{'state'}, $slave->{'name'});
fp@643:         }
fp@643:     }
fp@643: }
fp@643: 
fp@643: #------------------------------------------------------------------------------
fp@643: 
fp@643: sub get_options
fp@643: {
fp@643:     my $optret = getopts "m:cnh", \%opt;
fp@643: 
fp@643:     &print_usage if defined $opt{h} or $#ARGV > -1 or !$optret;
fp@643: 
fp@643:     if (defined $opt{m}) {
fp@643:         $master_index = $opt{m};
fp@563:     }
fp@563:     else {
fp@643:         $master_index = 0;
fp@563:     }
fp@563: }
fp@563: 
fp@563: #------------------------------------------------------------------------------
fp@563: 
fp@563: sub print_usage
fp@563: {
fp@563:     my $cmd = `basename $0`;
fp@563:     chomp $cmd;
fp@563:     print "Usage: $cmd [OPTIONS]\n";
fp@563:     print "        -m <IDX>    Query master <IDX>.\n";
fp@563:     print "        -c          Display current [mA] ";
fp@563:     print "(3: consumption, 4: remaining).\n";
fp@563:     print "        -n          Display no coupler lines.\n";
fp@563:     print "        -h          Show this help.\n";
fp@563:     exit 0;
fp@563: }
fp@563: 
fp@563: #------------------------------------------------------------------------------
fp@563: 
fp@563: sub get_terminal_width
fp@563: {
fp@563:     my $winsize;
fp@563:     die "no TIOCGWINSZ " unless defined &TIOCGWINSZ;
fp@563:     open(TTY, "+</dev/tty") or die "No tty: $!";
fp@563:     unless (ioctl(TTY, &TIOCGWINSZ, $winsize='')) {
fp@643:         die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ;
fp@563:     }
fp@563:     (my $row, my $col, my $xpixel, my $ypixel) = unpack('S4', $winsize);
fp@563:     return $col;
fp@563: }
fp@563: #------------------------------------------------------------------------------
fp@563: 
fp@563: sub print_line
fp@563: {
fp@643:     for (my $i = 0; $i < $term_width; $i++) {
fp@643:         print "-";
fp@643:     }
fp@563:     print "\n";
fp@563: }
fp@563: 
fp@563: #------------------------------------------------------------------------------