script/ec_list.pl
branchstable-1.0
changeset 1624 9dc190591c0f
parent 1623 05622513f627
child 1625 49d577ddcb08
equal deleted inserted replaced
1623:05622513f627 1624:9dc190591c0f
     1 #!/usr/bin/perl
       
     2 
       
     3 #------------------------------------------------------------------------------
       
     4 #
       
     5 #  e c _ l i s t . p l
       
     6 #
       
     7 #  Userspace tool for listing EtherCAT slaves.
       
     8 #
       
     9 #  $Id$
       
    10 #
       
    11 #  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
       
    12 #
       
    13 #  This file is part of the IgH EtherCAT Master.
       
    14 #
       
    15 #  The IgH EtherCAT Master is free software; you can redistribute it
       
    16 #  and/or modify it under the terms of the GNU General Public License
       
    17 #  as published by the Free Software Foundation; either version 2 of the
       
    18 #  License, or (at your option) any later version.
       
    19 #
       
    20 #  The IgH EtherCAT Master is distributed in the hope that it will be
       
    21 #  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    22 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    23 #  GNU General Public License for more details.
       
    24 #
       
    25 #  You should have received a copy of the GNU General Public License
       
    26 #  along with the IgH EtherCAT Master; if not, write to the Free Software
       
    27 #  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    28 #
       
    29 #  The right to use EtherCAT Technology is granted and comes free of
       
    30 #  charge under condition of compatibility of product made by
       
    31 #  Licensee. People intending to distribute/sell products based on the
       
    32 #  code, have to sign an agreement to guarantee that products using
       
    33 #  software based on IgH EtherCAT master stay compatible with the actual
       
    34 #  EtherCAT specification (which are released themselves as an open
       
    35 #  standard) as the (only) precondition to have the right to use EtherCAT
       
    36 #  Technology, IP and trade marks.
       
    37 #
       
    38 #------------------------------------------------------------------------------
       
    39 
       
    40 use strict;
       
    41 use Getopt::Std;
       
    42 
       
    43 my $master_index;
       
    44 my $master_dir;
       
    45 my $show_sii_naming;
       
    46 
       
    47 #------------------------------------------------------------------------------
       
    48 
       
    49 &get_options;
       
    50 &query_master;
       
    51 exit 0;
       
    52 
       
    53 #------------------------------------------------------------------------------
       
    54 
       
    55 sub query_master
       
    56 {
       
    57     $master_dir = "/sys/ethercat" . $master_index;
       
    58     &query_slaves;
       
    59 }
       
    60 
       
    61 #------------------------------------------------------------------------------
       
    62 
       
    63 sub query_slaves
       
    64 {
       
    65     my $dirhandle;
       
    66     my $slave_dir;
       
    67     my $entry;
       
    68     my $slave_index;
       
    69     my $file_name;
       
    70     my $vendor_name;
       
    71     my @slaves;
       
    72     my $slave;
       
    73     my $abs;
       
    74 
       
    75     unless (opendir $dirhandle, $master_dir) {
       
    76 	print "Failed to open directory \"$master_dir\".\n";
       
    77 	exit 1;
       
    78     }
       
    79 
       
    80     while ($entry = readdir $dirhandle) {
       
    81         next unless $entry =~ /^slave(\d+)$/;
       
    82 	$slave_dir = $master_dir . "/" . $entry;
       
    83 
       
    84 	$slave = {};
       
    85 	$slave->{'ring_position'} =
       
    86 	    &read_integer("$slave_dir/ring_position");
       
    87 	$slave->{'coupler_address'} =
       
    88 	    &read_string("$slave_dir/coupler_address");
       
    89 	unless ($show_sii_naming) {
       
    90 	    $slave->{'vendor_name'} =
       
    91 		&read_string("$slave_dir/vendor_name");
       
    92 	    $slave->{'product_name'} =
       
    93 		&read_string("$slave_dir/product_name");
       
    94 	    $slave->{'product_desc'} =
       
    95 		&read_string("$slave_dir/product_desc");
       
    96 	}
       
    97 	else {
       
    98 	    $slave->{'sii_name'} =
       
    99 		&read_string("$slave_dir/sii_name");
       
   100 	}
       
   101 	$slave->{'type'} =
       
   102 	    &read_string("$slave_dir/type");
       
   103 
       
   104 	push @slaves, $slave;
       
   105     }
       
   106     closedir $dirhandle;
       
   107 
       
   108     @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves;
       
   109 
       
   110     print "EtherCAT bus listing for master $master_index:\n";
       
   111     for $slave (@slaves) {
       
   112 	if ($slave->{'type'} eq "coupler") {
       
   113 	    print "--------------------------------------------------------\n";
       
   114 	}
       
   115 
       
   116 	$abs = sprintf "%i", $slave->{'ring_position'};
       
   117 	printf(" %3s %8s   ", $abs, $slave->{'coupler_address'});
       
   118 	unless ($show_sii_naming) {
       
   119 	    printf("%-12s %-10s %s\n", $slave->{'vendor_name'},
       
   120 		   $slave->{'product_name'}, $slave->{'product_desc'});
       
   121 	}
       
   122 	else {
       
   123 	    printf("%s\n", $slave->{'sii_name'});
       
   124 	}
       
   125     }
       
   126 }
       
   127 
       
   128 #------------------------------------------------------------------------------
       
   129 
       
   130 sub read_string
       
   131 {
       
   132     (my $file_name) = @_;
       
   133     my $data;
       
   134 
       
   135     $data = `cat $file_name 2>/dev/null`;
       
   136     if ($?) {
       
   137 	print "ERROR: Unable to read string $file_name!\n";
       
   138 	exit 1;
       
   139     }
       
   140 
       
   141     chomp $data;
       
   142     return $data;
       
   143 }
       
   144 
       
   145 #------------------------------------------------------------------------------
       
   146 
       
   147 sub read_integer
       
   148 {
       
   149     (my $file_name) = @_;
       
   150 
       
   151     if (`cat $file_name 2>/dev/null` !~ /^(\d+)$/) {
       
   152 	print "ERROR: Unable to read integer $file_name!\n";
       
   153 	exit 1;
       
   154     }
       
   155 
       
   156     return int $1;
       
   157 }
       
   158 
       
   159 #------------------------------------------------------------------------------
       
   160 
       
   161 sub get_options
       
   162 {
       
   163     my %opt;
       
   164     my $optret;
       
   165 
       
   166     $optret = getopts "m:sh", \%opt;
       
   167 
       
   168     &print_usage if defined $opt{'h'} or $#ARGV > -1 or !$optret;
       
   169 
       
   170     if (defined $opt{'m'}) {
       
   171 	$master_index = $opt{'m'};
       
   172     }
       
   173     else {
       
   174 	$master_index = 0;
       
   175     }
       
   176 
       
   177     $show_sii_naming = defined $opt{'s'};
       
   178 }
       
   179 
       
   180 #------------------------------------------------------------------------------
       
   181 
       
   182 sub print_usage
       
   183 {
       
   184     print "Usage: ec_list [OPTIONS]\n";
       
   185     print "        -m <IDX>    Query master <IDX>.\n";
       
   186     print "        -s          Show SII naming instead of";
       
   187     print " vendor/product/description.\n";
       
   188     print "        -h          Show this help.\n";
       
   189     exit 0;
       
   190 }
       
   191 
       
   192 #------------------------------------------------------------------------------