script/lsec
author Florian Pose <fp@igh-essen.com>
Mon, 02 Jun 2008 11:08:49 +0000
changeset 936 30fddfce8db6
parent 835 23fd8b510803
permissions -rwxr-xr-x
Added multi-slave support.
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     1
#!/usr/bin/perl
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     2
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     3
#------------------------------------------------------------------------------
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     4
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     5
#  l s e c  -  List EtherCAT
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     6
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     7
#  Userspace tool for listing EtherCAT slaves.
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     8
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
     9
#  $Id$
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    10
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    11
#  Copyright (C) 2006  Florian Pose, Ingenieurgemeinschaft IgH
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    12
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    13
#  This file is part of the IgH EtherCAT Master.
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    14
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    15
#  The IgH EtherCAT Master is free software; you can redistribute it
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    16
#  and/or modify it under the terms of the GNU General Public License
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    17
#  as published by the Free Software Foundation; either version 2 of the
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    18
#  License, or (at your option) any later version.
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    19
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    20
#  The IgH EtherCAT Master is distributed in the hope that it will be
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    21
#  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    22
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    23
#  GNU General Public License for more details.
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    24
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    25
#  You should have received a copy of the GNU General Public License
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    26
#  along with the IgH EtherCAT Master; if not, write to the Free Software
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    27
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    28
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    29
#  The right to use EtherCAT Technology is granted and comes free of
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    30
#  charge under condition of compatibility of product made by
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    31
#  Licensee. People intending to distribute/sell products based on the
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    32
#  code, have to sign an agreement to guarantee that products using
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    33
#  software based on IgH EtherCAT master stay compatible with the actual
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    34
#  EtherCAT specification (which are released themselves as an open
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    35
#  standard) as the (only) precondition to have the right to use EtherCAT
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    36
#  Technology, IP and trade marks.
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    37
#
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    38
#------------------------------------------------------------------------------
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    39
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    40
require 'sys/ioctl.ph';
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    41
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    42
use strict;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    43
use Getopt::Std;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    44
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    45
my %opt;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    46
my $master_index;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    47
my $term_width;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    48
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    49
#------------------------------------------------------------------------------
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    50
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    51
$term_width = &get_terminal_width;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    52
&get_options;
576
158c5a3d0a2a New module file /sys/ethercat, masters moved to /sys/ethercat/masterX.
Florian Pose <fp@igh-essen.com>
parents: 563
diff changeset
    53
&query;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    54
exit 0;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    55
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    56
#------------------------------------------------------------------------------
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    57
576
158c5a3d0a2a New module file /sys/ethercat, masters moved to /sys/ethercat/masterX.
Florian Pose <fp@igh-essen.com>
parents: 563
diff changeset
    58
sub query
158c5a3d0a2a New module file /sys/ethercat, masters moved to /sys/ethercat/masterX.
Florian Pose <fp@igh-essen.com>
parents: 563
diff changeset
    59
{
158c5a3d0a2a New module file /sys/ethercat, masters moved to /sys/ethercat/masterX.
Florian Pose <fp@igh-essen.com>
parents: 563
diff changeset
    60
    my $master_dir;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    61
    my $dirhandle;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    62
    my $entry;
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    63
    my $slave_info_file;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    64
    my @slaves;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    65
    my $slave;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    66
    my $line;
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    67
    my $ring_col_width;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    68
    my $alias_col_width;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    69
    my $fmt;
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    70
    my $width;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    71
    my $last_alias;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    72
    my $alias_index;
766
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
    73
    my $category;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    74
576
158c5a3d0a2a New module file /sys/ethercat, masters moved to /sys/ethercat/masterX.
Florian Pose <fp@igh-essen.com>
parents: 563
diff changeset
    75
    $master_dir = "/sys/ethercat/master" . $master_index;
158c5a3d0a2a New module file /sys/ethercat, masters moved to /sys/ethercat/masterX.
Florian Pose <fp@igh-essen.com>
parents: 563
diff changeset
    76
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    77
    unless (opendir $dirhandle, $master_dir) {
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    78
        print "Failed to open directory \"$master_dir\".\n";
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    79
        exit 1;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    80
    }
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    81
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    82
    while ($entry = readdir $dirhandle) {
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    83
        next unless $entry =~ /^slave(\d+)$/;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
    84
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    85
        $slave = {};
835
23fd8b510803 Evaluate CoE details from general category; moved current consumption
Florian Pose <fp@igh-essen.com>
parents: 766
diff changeset
    86
        $slave->{'current'} = 0;
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    87
        $slave_info_file = "$master_dir/$entry/info"; 
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    88
        open INFO, $slave_info_file or die
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    89
            "ERROR: Failed to open $slave_info_file.";
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    90
766
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
    91
        $category = "";
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    92
        while ($line = <INFO>) {
766
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
    93
            # remember category
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
    94
            if ($line =~ /^([^\s][^:]*):$/) {
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
    95
                $category = $1;
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
    96
            } elsif ($line =~ /^\s*$/) {
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
    97
                $category = "";
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
    98
            }
766
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
    99
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   100
            if ($category eq "") {
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   101
                if ($line =~ /^Ring position: (\d+)$/) {
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   102
                    $slave->{'ring_position'} = $1;
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   103
                } elsif ($line =~ /^State: (.+) /) {
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   104
                    $slave->{'state'} = $1;
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   105
                } elsif ($line =~ /^Configured station alias: .* \((\d+)\)$/) {
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   106
                    $slave->{'alias'} = $1;
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   107
                }
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   108
            } elsif ($category eq "Identity") {
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   109
                if ($line =~ /Vendor ID: .* \((\d+)\)$/) {
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   110
                    $slave->{'vendor'} = $1;
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   111
                } elsif ($line =~ /Product code: .* \((\d+)\)$/) {
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   112
                    $slave->{'product'} = $1;
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   113
                }
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   114
            } elsif ($category eq "General") {
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   115
                if ($line =~ /Name: (.*)$/) {
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   116
                    $slave->{'name'} = $1;
835
23fd8b510803 Evaluate CoE details from general category; moved current consumption
Florian Pose <fp@igh-essen.com>
parents: 766
diff changeset
   117
                } elsif ($line =~ /Current consumption: (-?\d+) mA$/) {
23fd8b510803 Evaluate CoE details from general category; moved current consumption
Florian Pose <fp@igh-essen.com>
parents: 766
diff changeset
   118
                    $slave->{'current'} = $1;
766
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   119
                }
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   120
            }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   121
        }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   122
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   123
        close INFO;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   124
        push @slaves, $slave;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   125
    }
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   126
    closedir $dirhandle;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   127
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   128
    @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   129
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   130
    # create field addresses and calculate column widths
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   131
    $ring_col_width = 0;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   132
    $alias_col_width = 0;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   133
    $last_alias = "";
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   134
    for $slave (@slaves) {
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   135
        if ($slave->{'alias'}) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   136
            $last_alias = $slave->{'alias'};
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   137
            $alias_index = 0;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   138
        }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   139
        if ($last_alias) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   140
            $slave->{'field_address'} = "#" . $last_alias . ":" . $alias_index;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   141
            $width = length $slave->{'field_address'};
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   142
            $alias_col_width = $width if ($width > $alias_col_width);
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   143
        }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   144
        $width = length $slave->{'ring_position'};
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   145
        $ring_col_width = $width if ($width > $ring_col_width);
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   146
        $alias_index++;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   147
    }
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   148
766
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   149
    # replace empty name with vendor id and product code
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   150
    for $slave (@slaves) {
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   151
        unless (defined $slave->{'name'}) {
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   152
            $slave->{'name'} = sprintf("0x%08X:0x%08X", $slave->{'vendor'},
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   153
                    $slave->{'product'});
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   154
        }
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   155
    }
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   156
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   157
    if (defined $opt{'c'}) { # display power consumtion
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   158
        $fmt = sprintf " %%%is  %%%is  %%6i  %%6i  %%s\n",
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   159
            $ring_col_width, $alias_col_width;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   160
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   161
        my $current_sum = 0;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   162
        for $slave (@slaves) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   163
            &print_line if $slave->{'alias'} and !defined $opt{n};
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   164
            $current_sum = 0 if $slave->{'current'} < 0;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   165
            $current_sum -= $slave->{'current'};
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   166
            printf($fmt, $slave->{'ring_position'}, $slave->{'field_address'},
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   167
                    $slave->{'current'}, $current_sum, $slave->{'name'});
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   168
        }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   169
    }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   170
    else { # normal display
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   171
        $fmt = sprintf " %%%is  %%%is  %%-6s  %%s\n",
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   172
            $ring_col_width, $alias_col_width;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   173
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   174
        for $slave (@slaves) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   175
            &print_line if $slave->{'alias'} and !defined $opt{n};
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   176
            printf($fmt, $slave->{'ring_position'}, $slave->{'field_address'},
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   177
                    $slave->{'state'}, $slave->{'name'});
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   178
        }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   179
    }
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   180
}
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   181
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   182
#------------------------------------------------------------------------------
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   183
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   184
sub get_options
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   185
{
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   186
    my $optret = getopts "m:cnh", \%opt;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   187
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   188
    &print_usage if defined $opt{h} or $#ARGV > -1 or !$optret;
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   189
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   190
    if (defined $opt{m}) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   191
        $master_index = $opt{m};
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   192
    }
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   193
    else {
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   194
        $master_index = 0;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   195
    }
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   196
}
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   197
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   198
#------------------------------------------------------------------------------
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   199
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   200
sub print_usage
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   201
{
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   202
    my $cmd = `basename $0`;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   203
    chomp $cmd;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   204
    print "Usage: $cmd [OPTIONS]\n";
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   205
    print "        -m <IDX>    Query master <IDX>.\n";
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   206
    print "        -c          Display current [mA] ";
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   207
    print "(3: consumption, 4: remaining).\n";
766
97e98cbd0cd2 Fixed lsec script.
Florian Pose <fp@igh-essen.com>
parents: 643
diff changeset
   208
    print "        -n          Do not display lines before aliased slaves.\n";
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   209
    print "        -h          Show this help.\n";
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   210
    exit 0;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   211
}
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   212
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   213
#------------------------------------------------------------------------------
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   214
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   215
sub get_terminal_width
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   216
{
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   217
    my $winsize;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   218
    die "no TIOCGWINSZ " unless defined &TIOCGWINSZ;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   219
    open(TTY, "+</dev/tty") or die "No tty: $!";
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   220
    unless (ioctl(TTY, &TIOCGWINSZ, $winsize='')) {
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   221
        die sprintf "$0: ioctl TIOCGWINSZ (%08x: $!)\n", &TIOCGWINSZ;
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   222
    }
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   223
    (my $row, my $col, my $xpixel, my $ypixel) = unpack('S4', $winsize);
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   224
    return $col;
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   225
}
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   226
#------------------------------------------------------------------------------
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   227
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   228
sub print_line
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   229
{
643
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   230
    for (my $i = 0; $i < $term_width; $i++) {
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   231
        print "-";
78929d878b2c Removed X:Y addressing scheme, ec_slave_is_coupler(),
Florian Pose <fp@igh-essen.com>
parents: 576
diff changeset
   232
    }
563
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   233
    print "\n";
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   234
}
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   235
d113b63c55c4 Changed structure of scripts subdirectory.
Florian Pose <fp@igh-essen.com>
parents:
diff changeset
   236
#------------------------------------------------------------------------------