1 #!/usr/bin/perl |
|
2 |
|
3 #------------------------------------------------------------------------------ |
|
4 # |
|
5 # l s e c - List EtherCAT |
|
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 require 'sys/ioctl.ph'; |
|
41 |
|
42 use strict; |
|
43 use Getopt::Std; |
|
44 |
|
45 my %opt; |
|
46 my $master_index; |
|
47 my $term_width; |
|
48 |
|
49 #------------------------------------------------------------------------------ |
|
50 |
|
51 $term_width = &get_terminal_width; |
|
52 &get_options; |
|
53 &query; |
|
54 exit 0; |
|
55 |
|
56 #------------------------------------------------------------------------------ |
|
57 |
|
58 sub query |
|
59 { |
|
60 my $master_dir; |
|
61 my $dirhandle; |
|
62 my $entry; |
|
63 my $slave_info_file; |
|
64 my @slaves; |
|
65 my $slave; |
|
66 my $line; |
|
67 my $ring_col_width; |
|
68 my $alias_col_width; |
|
69 my $fmt; |
|
70 my $width; |
|
71 my $last_alias; |
|
72 my $alias_index; |
|
73 my $category; |
|
74 |
|
75 $master_dir = "/sys/ethercat/master" . $master_index; |
|
76 |
|
77 unless (opendir $dirhandle, $master_dir) { |
|
78 print "Failed to open directory \"$master_dir\".\n"; |
|
79 exit 1; |
|
80 } |
|
81 |
|
82 while ($entry = readdir $dirhandle) { |
|
83 next unless $entry =~ /^slave(\d+)$/; |
|
84 |
|
85 $slave = {}; |
|
86 $slave->{'current'} = 0; |
|
87 $slave_info_file = "$master_dir/$entry/info"; |
|
88 open INFO, $slave_info_file or die |
|
89 "ERROR: Failed to open $slave_info_file."; |
|
90 |
|
91 $category = ""; |
|
92 while ($line = <INFO>) { |
|
93 # remember category |
|
94 if ($line =~ /^([^\s][^:]*):$/) { |
|
95 $category = $1; |
|
96 } elsif ($line =~ /^\s*$/) { |
|
97 $category = ""; |
|
98 } |
|
99 |
|
100 if ($category eq "") { |
|
101 if ($line =~ /^Ring position: (\d+)$/) { |
|
102 $slave->{'ring_position'} = $1; |
|
103 } elsif ($line =~ /^State: (.+) /) { |
|
104 $slave->{'state'} = $1; |
|
105 } elsif ($line =~ /^Configured station alias: .* \((\d+)\)$/) { |
|
106 $slave->{'alias'} = $1; |
|
107 } |
|
108 } elsif ($category eq "Identity") { |
|
109 if ($line =~ /Vendor ID: .* \((\d+)\)$/) { |
|
110 $slave->{'vendor'} = $1; |
|
111 } elsif ($line =~ /Product code: .* \((\d+)\)$/) { |
|
112 $slave->{'product'} = $1; |
|
113 } |
|
114 } elsif ($category eq "General") { |
|
115 if ($line =~ /Name: (.*)$/) { |
|
116 $slave->{'name'} = $1; |
|
117 } elsif ($line =~ /Current consumption: (-?\d+) mA$/) { |
|
118 $slave->{'current'} = $1; |
|
119 } |
|
120 } |
|
121 } |
|
122 |
|
123 close INFO; |
|
124 push @slaves, $slave; |
|
125 } |
|
126 closedir $dirhandle; |
|
127 |
|
128 @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves; |
|
129 |
|
130 # create field addresses and calculate column widths |
|
131 $ring_col_width = 0; |
|
132 $alias_col_width = 0; |
|
133 $last_alias = ""; |
|
134 for $slave (@slaves) { |
|
135 if ($slave->{'alias'}) { |
|
136 $last_alias = $slave->{'alias'}; |
|
137 $alias_index = 0; |
|
138 } |
|
139 if ($last_alias) { |
|
140 $slave->{'field_address'} = "#" . $last_alias . ":" . $alias_index; |
|
141 $width = length $slave->{'field_address'}; |
|
142 $alias_col_width = $width if ($width > $alias_col_width); |
|
143 } |
|
144 $width = length $slave->{'ring_position'}; |
|
145 $ring_col_width = $width if ($width > $ring_col_width); |
|
146 $alias_index++; |
|
147 } |
|
148 |
|
149 # replace empty name with vendor id and product code |
|
150 for $slave (@slaves) { |
|
151 unless (defined $slave->{'name'}) { |
|
152 $slave->{'name'} = sprintf("0x%08X:0x%08X", $slave->{'vendor'}, |
|
153 $slave->{'product'}); |
|
154 } |
|
155 } |
|
156 |
|
157 if (defined $opt{'c'}) { # display power consumtion |
|
158 $fmt = sprintf " %%%is %%%is %%6i %%6i %%s\n", |
|
159 $ring_col_width, $alias_col_width; |
|
160 |
|
161 my $current_sum = 0; |
|
162 for $slave (@slaves) { |
|
163 &print_line if $slave->{'alias'} and !defined $opt{n}; |
|
164 $current_sum = 0 if $slave->{'current'} < 0; |
|
165 $current_sum -= $slave->{'current'}; |
|
166 printf($fmt, $slave->{'ring_position'}, $slave->{'field_address'}, |
|
167 $slave->{'current'}, $current_sum, $slave->{'name'}); |
|
168 } |
|
169 } |
|
170 else { # normal display |
|
171 $fmt = sprintf " %%%is %%%is %%-6s %%s\n", |
|
172 $ring_col_width, $alias_col_width; |
|
173 |
|
174 for $slave (@slaves) { |
|
175 &print_line if $slave->{'alias'} and !defined $opt{n}; |
|
176 printf($fmt, $slave->{'ring_position'}, $slave->{'field_address'}, |
|
177 $slave->{'state'}, $slave->{'name'}); |
|
178 } |
|
179 } |
|
180 } |
|
181 |
|
182 #------------------------------------------------------------------------------ |
|
183 |
|
184 sub get_options |
|
185 { |
|
186 my $optret = getopts "m:cnh", \%opt; |
|
187 |
|
188 &print_usage if defined $opt{h} or $#ARGV > -1 or !$optret; |
|
189 |
|
190 if (defined $opt{m}) { |
|
191 $master_index = $opt{m}; |
|
192 } |
|
193 else { |
|
194 $master_index = 0; |
|
195 } |
|
196 } |
|
197 |
|
198 #------------------------------------------------------------------------------ |
|
199 |
|
200 sub print_usage |
|
201 { |
|
202 my $cmd = `basename $0`; |
|
203 chomp $cmd; |
|
204 print "Usage: $cmd [OPTIONS]\n"; |
|
205 print " -m <IDX> Query master <IDX>.\n"; |
|
206 print " -c Display current [mA] "; |
|
207 print "(3: consumption, 4: remaining).\n"; |
|
208 print " -n Do not display lines before aliased slaves.\n"; |
|
209 print " -h Show this help.\n"; |
|
210 exit 0; |
|
211 } |
|
212 |
|
213 #------------------------------------------------------------------------------ |
|
214 |
|
215 sub get_terminal_width |
|
216 { |
|
217 my $winsize; |
|
218 die "no TIOCGWINSZ " unless defined &TIOCGWINSZ; |
|
219 open(TTY, "+</dev/tty") or die "No tty: $!"; |
|
220 unless (ioctl(TTY, &TIOCGWINSZ, $winsize='')) { |
|
221 die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ; |
|
222 } |
|
223 (my $row, my $col, my $xpixel, my $ypixel) = unpack('S4', $winsize); |
|
224 return $col; |
|
225 } |
|
226 #------------------------------------------------------------------------------ |
|
227 |
|
228 sub print_line |
|
229 { |
|
230 for (my $i = 0; $i < $term_width; $i++) { |
|
231 print "-"; |
|
232 } |
|
233 print "\n"; |
|
234 } |
|
235 |
|
236 #------------------------------------------------------------------------------ |
|