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