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