fp@1624: #!/usr/bin/perl fp@1624: fp@1624: #------------------------------------------------------------------------------ fp@1624: # fp@1624: # l s e c - List EtherCAT fp@1624: # fp@1624: # Userspace tool for listing EtherCAT slaves. fp@1624: # fp@1624: # $Id$ fp@1624: # fp@1624: # Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@1624: # fp@1624: # This file is part of the IgH EtherCAT Master. fp@1624: # fp@1624: # The IgH EtherCAT Master is free software; you can redistribute it fp@1624: # and/or modify it under the terms of the GNU General Public License fp@1624: # as published by the Free Software Foundation; either version 2 of the fp@1624: # License, or (at your option) any later version. fp@1624: # fp@1624: # The IgH EtherCAT Master is distributed in the hope that it will be fp@1624: # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1624: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@1624: # GNU General Public License for more details. fp@1624: # fp@1624: # You should have received a copy of the GNU General Public License fp@1624: # along with the IgH EtherCAT Master; if not, write to the Free Software fp@1624: # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1624: # fp@1624: # The right to use EtherCAT Technology is granted and comes free of fp@1624: # charge under condition of compatibility of product made by fp@1624: # Licensee. People intending to distribute/sell products based on the fp@1624: # code, have to sign an agreement to guarantee that products using fp@1624: # software based on IgH EtherCAT master stay compatible with the actual fp@1624: # EtherCAT specification (which are released themselves as an open fp@1624: # standard) as the (only) precondition to have the right to use EtherCAT fp@1624: # Technology, IP and trade marks. fp@1624: # fp@1624: #------------------------------------------------------------------------------ fp@1624: fp@1624: use strict; fp@1624: use Getopt::Std; fp@1624: fp@1624: my $master_index; fp@1624: my $master_dir; fp@1624: my $show_sii_naming; fp@1624: fp@1624: #------------------------------------------------------------------------------ fp@1624: fp@1624: &get_options; fp@1624: &query_master; fp@1624: exit 0; fp@1624: fp@1624: #------------------------------------------------------------------------------ fp@1624: fp@1624: sub query_master fp@1624: { fp@1624: $master_dir = "/sys/ethercat" . $master_index; fp@1624: &query_slaves; fp@1624: } fp@1624: fp@1624: #------------------------------------------------------------------------------ fp@1624: fp@1624: sub query_slaves fp@1624: { fp@1624: my $dirhandle; fp@1624: my $slave_dir; fp@1624: my $entry; fp@1624: my $slave_index; fp@1624: my $file_name; fp@1624: my $vendor_name; fp@1624: my @slaves; fp@1624: my $slave; fp@1624: my $abs; fp@1624: fp@1624: unless (opendir $dirhandle, $master_dir) { fp@1624: print "Failed to open directory \"$master_dir\".\n"; fp@1624: exit 1; fp@1624: } fp@1624: fp@1624: while ($entry = readdir $dirhandle) { fp@1624: next unless $entry =~ /^slave(\d+)$/; fp@1624: $slave_dir = $master_dir . "/" . $entry; fp@1624: fp@1624: $slave = {}; fp@1624: $slave->{'ring_position'} = fp@1624: &read_integer("$slave_dir/ring_position"); fp@1624: $slave->{'advanced_position'} = fp@1624: &read_string("$slave_dir/advanced_position"); fp@1624: unless ($show_sii_naming) { fp@1624: $slave->{'vendor_name'} = fp@1624: &read_string("$slave_dir/vendor_name"); fp@1624: $slave->{'product_name'} = fp@1624: &read_string("$slave_dir/product_name"); fp@1624: $slave->{'product_desc'} = fp@1624: &read_string("$slave_dir/product_desc"); fp@1624: } fp@1624: else { fp@1624: $slave->{'name'} = fp@1624: &read_string("$slave_dir/name"); fp@1624: } fp@1624: $slave->{'type'} = fp@1624: &read_string("$slave_dir/type"); fp@1624: fp@1624: push @slaves, $slave; fp@1624: } fp@1624: closedir $dirhandle; fp@1624: fp@1624: @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves; fp@1624: fp@1624: print "EtherCAT bus listing for master $master_index:\n"; fp@1624: for $slave (@slaves) { fp@1624: if ($slave->{'type'} eq "coupler") { fp@1624: print "--------------------------------------------------------\n"; fp@1624: } fp@1624: fp@1624: $abs = sprintf "%i", $slave->{'ring_position'}; fp@1624: printf(" %3s %8s ", $abs, $slave->{'advanced_position'}); fp@1624: unless ($show_sii_naming) { fp@1624: printf("%-12s %-10s %s\n", $slave->{'vendor_name'}, fp@1624: $slave->{'product_name'}, $slave->{'product_desc'}); fp@1624: } fp@1624: else { fp@1624: printf("%s\n", $slave->{'name'}); fp@1624: } fp@1624: } fp@1624: } fp@1624: fp@1624: #------------------------------------------------------------------------------ fp@1624: fp@1624: sub read_string fp@1624: { fp@1624: (my $file_name) = @_; fp@1624: my $data; fp@1624: fp@1624: $data = `cat $file_name 2>/dev/null`; fp@1624: if ($?) { fp@1624: print "ERROR: Unable to read string $file_name!\n"; fp@1624: exit 1; fp@1624: } fp@1624: fp@1624: chomp $data; fp@1624: return $data; fp@1624: } fp@1624: fp@1624: #------------------------------------------------------------------------------ fp@1624: fp@1624: sub read_integer fp@1624: { fp@1624: (my $file_name) = @_; fp@1624: fp@1624: if (`cat $file_name 2>/dev/null` !~ /^(\d+)$/) { fp@1624: print "ERROR: Unable to read integer $file_name!\n"; fp@1624: exit 1; fp@1624: } fp@1624: fp@1624: return int $1; fp@1624: } fp@1624: fp@1624: #------------------------------------------------------------------------------ fp@1624: fp@1624: sub get_options fp@1624: { fp@1624: my %opt; fp@1624: my $optret; fp@1624: fp@1624: $optret = getopts "m:sh", \%opt; fp@1624: fp@1624: &print_usage if defined $opt{'h'} or $#ARGV > -1 or !$optret; fp@1624: fp@1624: if (defined $opt{'m'}) { fp@1624: $master_index = $opt{'m'}; fp@1624: } fp@1624: else { fp@1624: $master_index = 0; fp@1624: } fp@1624: fp@1624: $show_sii_naming = defined $opt{'s'}; fp@1624: } fp@1624: fp@1624: #------------------------------------------------------------------------------ fp@1624: fp@1624: sub print_usage fp@1624: { fp@1624: print "Usage: $0 [OPTIONS]\n"; fp@1624: print " -m Query master .\n"; fp@1624: print " -s Show EEPROM name instead of"; fp@1624: print " vendor/product/description.\n"; fp@1624: print " -h Show this help.\n"; fp@1624: exit 0; fp@1624: } fp@1624: fp@1624: #------------------------------------------------------------------------------