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@1715: use strict; fp@1715: use Getopt::Std; fp@1715: fp@1715: my $master_index; fp@1715: my $master_dir; fp@1715: fp@1715: #------------------------------------------------------------------------------ fp@1715: 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@1715: my $line; 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@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@1715: print "EtherCAT bus listing for master $master_index:\n"; fp@1715: for $slave (@slaves) { fp@1715: $abs = sprintf "%i", $slave->{'ring_position'}; fp@1715: printf(" %3s %8s %-6s %s\n", fp@1715: $abs, $slave->{'advanced_position'}, fp@1715: $slave->{'state'}, $slave->{'name'}); fp@1715: } fp@1715: } fp@1715: fp@1715: #------------------------------------------------------------------------------ fp@1715: fp@1715: sub get_options fp@1715: { fp@1715: my %opt; fp@1715: my $optret; fp@1715: fp@1715: $optret = getopts "m:h", \%opt; fp@1715: fp@1715: &print_usage if defined $opt{'h'} or $#ARGV > -1 or !$optret; fp@1715: fp@1715: if (defined $opt{'m'}) { fp@1715: $master_index = $opt{'m'}; fp@1715: } fp@1715: else { fp@1715: $master_index = 0; fp@1715: } fp@1715: } fp@1715: fp@1715: #------------------------------------------------------------------------------ fp@1715: fp@1715: sub print_usage fp@1715: { fp@1715: my $cmd = `basename $0`; fp@1715: chomp $cmd; fp@1715: print "Usage: $cmd [OPTIONS]\n"; fp@1715: print " -m Query master .\n"; fp@1715: print " -h Show this help.\n"; fp@1715: exit 0; fp@1715: } fp@1715: fp@1715: #------------------------------------------------------------------------------