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