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