# HG changeset patch # User Florian Pose # Date 1171371719 0 # Node ID d113b63c55c4687ee5b07fb0a3a838e58927bf73 # Parent dda32c8294ad5f9e0bc76bc92848d962be683021 Changed structure of scripts subdirectory. diff -r dda32c8294ad -r d113b63c55c4 TODO --- a/TODO Tue Feb 13 13:00:19 2007 +0000 +++ b/TODO Tue Feb 13 13:01:59 2007 +0000 @@ -28,7 +28,6 @@ * Smaller changes: - Simplify FSMs with _enter() functions. - - Rename "sysconfig" file to "sysconfig/ethercat". - Dynamic creation of EoE handlers. - Output intermediate results during lsec. - State change FSM: Follow spontaneous state changes. diff -r dda32c8294ad -r d113b63c55c4 configure.ac --- a/configure.ac Tue Feb 13 13:00:19 2007 +0000 +++ b/configure.ac Tue Feb 13 13:01:59 2007 +0000 @@ -211,6 +211,8 @@ master/Makefile devices/Makefile script/Makefile + script/init.d/Makefile + script/sysconfig/Makefile include/Makefile examples/Makefile examples/mini/Makefile diff -r dda32c8294ad -r d113b63c55c4 script/Makefile.am --- a/script/Makefile.am Tue Feb 13 13:00:19 2007 +0000 +++ b/script/Makefile.am Tue Feb 13 13:01:59 2007 +0000 @@ -35,19 +35,10 @@ # #------------------------------------------------------------------------------ -initdir = $(sysconfdir)/init.d -sysdir = $(sysconfdir)/sysconfig +SUBDIRS = init.d sysconfig bin_SCRIPTS = lsec -init_SCRIPTS = ethercat -sys_DATA = sysconfig -EXTRA_DIST = lsec.pl ethercat.sh sysconfig - -lsec: lsec.pl - cp $(srcdir)/lsec.pl lsec - -ethercat: ethercat.sh - cp $(srcdir)/ethercat.sh ethercat +EXTRA_DIST = lsec #------------------------------------------------------------------------------ diff -r dda32c8294ad -r d113b63c55c4 script/ethercat.sh --- a/script/ethercat.sh Tue Feb 13 13:00:19 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,279 +0,0 @@ -#!/bin/sh - -#------------------------------------------------------------------------------ -# -# Init script for EtherCAT -# -# $Id$ -# -# Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH -# -# This file is part of the IgH EtherCAT Master. -# -# The IgH EtherCAT Master is free software; you can redistribute it -# and/or modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 of the -# License, or (at your option) any later version. -# -# The IgH EtherCAT Master is distributed in the hope that it will be -# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with the IgH EtherCAT Master; if not, write to the Free Software -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -# -# The right to use EtherCAT Technology is granted and comes free of -# charge under condition of compatibility of product made by -# Licensee. People intending to distribute/sell products based on the -# code, have to sign an agreement to guarantee that products using -# software based on IgH EtherCAT master stay compatible with the actual -# EtherCAT specification (which are released themselves as an open -# standard) as the (only) precondition to have the right to use EtherCAT -# Technology, IP and trade marks. -# -#------------------------------------------------------------------------------ - -### BEGIN INIT INFO -# Provides: ethercat -# Required-Start: $local_fs $syslog $network -# Should-Start: $time ntp -# Required-Stop: $local_fs $syslog $network -# Should-Stop: $time ntp -# Default-Start: 3 5 -# Default-Stop: 0 1 2 6 -# Short-Description: IgH EtherCAT master modules -# Description: -### END INIT INFO - -#------------------------------------------------------------------------------ - -device="ecxml" - -IFCONFIG=ifconfig -BRCTL=brctl -ROUTE=route - -#------------------------------------------------------------------------------ - -ETHERCAT_CONFIG=/etc/sysconfig/ethercat - -if [ ! -r $ETHERCAT_CONFIG ]; then - echo "$ETHERCAT_CONFIG not existing"; - if [ "$1" = "stop" ]; then - exit 0 - else - exit 6 - fi -fi - -. $ETHERCAT_CONFIG - -#------------------------------------------------------------------------------ - -# -# Function for setting up the EoE bridge -# -build_eoe_bridge() -{ - if [ -z "$EOE_BRIDGE" ]; then return; fi - - EOEIF=`$IFCONFIG -a | grep -o -E "^eoe[0-9]+ "` - - # add bridge, if it does not already exist - if ! $BRCTL show | grep -E -q "^$EOE_BRIDGE"; then - if ! $BRCTL addbr $EOE_BRIDGE; then - /bin/false - rc_status -v - rc_exit - fi - fi - - # check if specified interfaces are bridged - for interf in $EOEIF $EOE_EXTRA_INTERFACES; do - # interface is already part of the bridge (FIXME->show $EOE_BRIDGE) - if $BRCTL show | grep -E -q $interf - then continue - fi - # clear IP address and open interface - if ! $IFCONFIG $interf 0.0.0.0 up; then - /bin/false - rc_status -v - rc_exit - fi - # add interface to the bridge - if ! $BRCTL addif $EOE_BRIDGE $interf; then - /bin/false - rc_status -v - rc_exit - fi - done - - # configure IP on bridge - if [ -n "$EOE_IP_ADDRESS" -a -n "$EOE_IP_NETMASK" ]; then - if ! $IFCONFIG $EOE_BRIDGE $EOE_IP_ADDRESS \ - netmask $EOE_IP_NETMASK; then - /bin/false - rc_status -v - rc_exit - fi - fi - - # open bridge - if ! $IFCONFIG $EOE_BRIDGE up; then - /bin/false - rc_status -v - rc_exit - fi - - # install new default gateway - if [ -n "$EOE_GATEWAY" ]; then - while $ROUTE -n | grep -E -q "^0.0.0.0"; do - if ! $ROUTE del default; then - echo "Failed to remove route!" 1>&2 - /bin/false - rc_status -v - rc_exit - fi - done - if ! $ROUTE add default gw $EOE_GATEWAY; then - /bin/false - rc_status -v - rc_exit - fi - fi -} - -#------------------------------------------------------------------------------ - -. /etc/rc.status -rc_reset - -case "$1" in - - start) - echo -n "Starting EtherCAT master " - - if [ -z "$DEVICE_INDEX" ]; then - echo "ERROR: DEVICE_INDEX not set!" - /bin/false - rc_status -v - rc_exit - fi - - if [ -z "$EOE_INTERFACES" ]; then - # support legacy sysconfig files - if [ -n "$EOE_DEVICES" ]; then - EOE_INTERFACES=$EOE_DEVICES - else - EOE_INTERFACES=0 - fi - fi - - # unload conflicting modules at first - for mod in 8139too; do - if lsmod | grep "^$mod " > /dev/null; then - if ! rmmod $mod; then - /bin/false - rc_status -v - rc_exit - fi - fi - done - - # load master module - if ! modprobe ec_master ec_eoeif_count=$EOE_INTERFACES; then - modprobe 8139too - /bin/false - rc_status -v - rc_exit - fi - - # remove stale device node - rm -f /dev/${device}0 - - # get dynamic major number - major=$(awk "\$2==\"EtherCAT\" {print \$1}" /proc/devices) - - # create character device - mknod /dev/${device}0 c $major 0 - - # load device module - if ! modprobe ec_8139too ec_device_index=$DEVICE_INDEX; then - rmmod ec_master - modprobe 8139too - /bin/false - rc_status -v - rc_exit - fi - - # build EoE bridge - build_eoe_bridge - - rc_status -v - ;; - - stop) - echo -n "Shutting down EtherCAT master " - - # unload modules - for mod in ec_8139too ec_master; do - if lsmod | grep "^$mod " > /dev/null; then - if ! rmmod $mod; then - /bin/false - rc_status -v - rc_exit - fi; - fi; - done - - # remove device node - rm -f /dev/${device}0 - - sleep 1 - - # reload previous modules - if ! modprobe 8139too; then - echo "Warning: Failed to restore 8139too module." - fi - - rc_status -v - ;; - - restart) - $0 stop || exit 1 - sleep 1 - $0 start - rc_status - ;; - - status) - echo -n "Checking for EtherCAT " - - lsmod | grep "^ec_master " > /dev/null - master_running=$? - lsmod | grep "^ec_8139too " > /dev/null - device_running=$? - - # master module and device module loaded? - test $master_running -eq 0 -a $device_running -eq 0 - - rc_status -v - ;; - - bridge) - echo -n "Building EoE bridge " - build_eoe_bridge - rc_status -v - ;; - - *) - echo "USAGE: $0 {start|stop|restart|status|bridge}" - ;; - -esac - -rc_exit - -#------------------------------------------------------------------------------ diff -r dda32c8294ad -r d113b63c55c4 script/init.d/Makefile.am --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/script/init.d/Makefile.am Tue Feb 13 13:01:59 2007 +0000 @@ -0,0 +1,40 @@ +#------------------------------------------------------------------------------ +# +# $Id$ +# +# Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH +# +# This file is part of the IgH EtherCAT Master. +# +# The IgH EtherCAT Master is free software; you can redistribute it +# and/or modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# The IgH EtherCAT Master is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with the IgH EtherCAT Master; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# +# The right to use EtherCAT Technology is granted and comes free of +# charge under condition of compatibility of product made by +# Licensee. People intending to distribute/sell products based on the +# code, have to sign an agreement to guarantee that products using +# software based on IgH EtherCAT master stay compatible with the actual +# EtherCAT specification (which are released themselves as an open +# standard) as the (only) precondition to have the right to use EtherCAT +# Technology, IP and trade marks. +# +#------------------------------------------------------------------------------ + +initdir = $(sysconfdir)/init.d + +init_SCRIPTS = ethercat + +EXTRA_DIST = ethercat + +#------------------------------------------------------------------------------ diff -r dda32c8294ad -r d113b63c55c4 script/init.d/ethercat --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/script/init.d/ethercat Tue Feb 13 13:01:59 2007 +0000 @@ -0,0 +1,279 @@ +#!/bin/sh + +#------------------------------------------------------------------------------ +# +# Init script for EtherCAT +# +# $Id$ +# +# Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH +# +# This file is part of the IgH EtherCAT Master. +# +# The IgH EtherCAT Master is free software; you can redistribute it +# and/or modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# The IgH EtherCAT Master is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with the IgH EtherCAT Master; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# +# The right to use EtherCAT Technology is granted and comes free of +# charge under condition of compatibility of product made by +# Licensee. People intending to distribute/sell products based on the +# code, have to sign an agreement to guarantee that products using +# software based on IgH EtherCAT master stay compatible with the actual +# EtherCAT specification (which are released themselves as an open +# standard) as the (only) precondition to have the right to use EtherCAT +# Technology, IP and trade marks. +# +#------------------------------------------------------------------------------ + +### BEGIN INIT INFO +# Provides: ethercat +# Required-Start: $local_fs $syslog $network +# Should-Start: $time ntp +# Required-Stop: $local_fs $syslog $network +# Should-Stop: $time ntp +# Default-Start: 3 5 +# Default-Stop: 0 1 2 6 +# Short-Description: IgH EtherCAT master modules +# Description: +### END INIT INFO + +#------------------------------------------------------------------------------ + +device="ecxml" + +IFCONFIG=ifconfig +BRCTL=brctl +ROUTE=route + +#------------------------------------------------------------------------------ + +ETHERCAT_CONFIG=/etc/sysconfig/ethercat + +if [ ! -r $ETHERCAT_CONFIG ]; then + echo "$ETHERCAT_CONFIG not existing"; + if [ "$1" = "stop" ]; then + exit 0 + else + exit 6 + fi +fi + +. $ETHERCAT_CONFIG + +#------------------------------------------------------------------------------ + +# +# Function for setting up the EoE bridge +# +build_eoe_bridge() +{ + if [ -z "$EOE_BRIDGE" ]; then return; fi + + EOEIF=`$IFCONFIG -a | grep -o -E "^eoe[0-9]+ "` + + # add bridge, if it does not already exist + if ! $BRCTL show | grep -E -q "^$EOE_BRIDGE"; then + if ! $BRCTL addbr $EOE_BRIDGE; then + /bin/false + rc_status -v + rc_exit + fi + fi + + # check if specified interfaces are bridged + for interf in $EOEIF $EOE_EXTRA_INTERFACES; do + # interface is already part of the bridge (FIXME->show $EOE_BRIDGE) + if $BRCTL show | grep -E -q $interf + then continue + fi + # clear IP address and open interface + if ! $IFCONFIG $interf 0.0.0.0 up; then + /bin/false + rc_status -v + rc_exit + fi + # add interface to the bridge + if ! $BRCTL addif $EOE_BRIDGE $interf; then + /bin/false + rc_status -v + rc_exit + fi + done + + # configure IP on bridge + if [ -n "$EOE_IP_ADDRESS" -a -n "$EOE_IP_NETMASK" ]; then + if ! $IFCONFIG $EOE_BRIDGE $EOE_IP_ADDRESS \ + netmask $EOE_IP_NETMASK; then + /bin/false + rc_status -v + rc_exit + fi + fi + + # open bridge + if ! $IFCONFIG $EOE_BRIDGE up; then + /bin/false + rc_status -v + rc_exit + fi + + # install new default gateway + if [ -n "$EOE_GATEWAY" ]; then + while $ROUTE -n | grep -E -q "^0.0.0.0"; do + if ! $ROUTE del default; then + echo "Failed to remove route!" 1>&2 + /bin/false + rc_status -v + rc_exit + fi + done + if ! $ROUTE add default gw $EOE_GATEWAY; then + /bin/false + rc_status -v + rc_exit + fi + fi +} + +#------------------------------------------------------------------------------ + +. /etc/rc.status +rc_reset + +case "$1" in + + start) + echo -n "Starting EtherCAT master " + + if [ -z "$DEVICE_INDEX" ]; then + echo "ERROR: DEVICE_INDEX not set!" + /bin/false + rc_status -v + rc_exit + fi + + if [ -z "$EOE_INTERFACES" ]; then + # support legacy sysconfig files + if [ -n "$EOE_DEVICES" ]; then + EOE_INTERFACES=$EOE_DEVICES + else + EOE_INTERFACES=0 + fi + fi + + # unload conflicting modules at first + for mod in 8139too; do + if lsmod | grep "^$mod " > /dev/null; then + if ! rmmod $mod; then + /bin/false + rc_status -v + rc_exit + fi + fi + done + + # load master module + if ! modprobe ec_master ec_eoeif_count=$EOE_INTERFACES; then + modprobe 8139too + /bin/false + rc_status -v + rc_exit + fi + + # remove stale device node + rm -f /dev/${device}0 + + # get dynamic major number + major=$(awk "\$2==\"EtherCAT\" {print \$1}" /proc/devices) + + # create character device + mknod /dev/${device}0 c $major 0 + + # load device module + if ! modprobe ec_8139too ec_device_index=$DEVICE_INDEX; then + rmmod ec_master + modprobe 8139too + /bin/false + rc_status -v + rc_exit + fi + + # build EoE bridge + build_eoe_bridge + + rc_status -v + ;; + + stop) + echo -n "Shutting down EtherCAT master " + + # unload modules + for mod in ec_8139too ec_master; do + if lsmod | grep "^$mod " > /dev/null; then + if ! rmmod $mod; then + /bin/false + rc_status -v + rc_exit + fi; + fi; + done + + # remove device node + rm -f /dev/${device}0 + + sleep 1 + + # reload previous modules + if ! modprobe 8139too; then + echo "Warning: Failed to restore 8139too module." + fi + + rc_status -v + ;; + + restart) + $0 stop || exit 1 + sleep 1 + $0 start + rc_status + ;; + + status) + echo -n "Checking for EtherCAT " + + lsmod | grep "^ec_master " > /dev/null + master_running=$? + lsmod | grep "^ec_8139too " > /dev/null + device_running=$? + + # master module and device module loaded? + test $master_running -eq 0 -a $device_running -eq 0 + + rc_status -v + ;; + + bridge) + echo -n "Building EoE bridge " + build_eoe_bridge + rc_status -v + ;; + + *) + echo "USAGE: $0 {start|stop|restart|status|bridge}" + ;; + +esac + +rc_exit + +#------------------------------------------------------------------------------ diff -r dda32c8294ad -r d113b63c55c4 script/install.sh --- a/script/install.sh Tue Feb 13 13:00:19 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,101 +0,0 @@ -#!/bin/sh - -#------------------------------------------------------------------------------ -# -# EtherCAT install script -# -# $Id$ -# -# Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH -# -# This file is part of the IgH EtherCAT Master. -# -# The IgH EtherCAT Master is free software; you can redistribute it -# and/or modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 of the -# License, or (at your option) any later version. -# -# The IgH EtherCAT Master is distributed in the hope that it will be -# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with the IgH EtherCAT Master; if not, write to the Free Software -# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -# -# The right to use EtherCAT Technology is granted and comes free of -# charge under condition of compatibility of product made by -# Licensee. People intending to distribute/sell products based on the -# code, have to sign an agreement to guarantee that products using -# software based on IgH EtherCAT master stay compatible with the actual -# EtherCAT specification (which are released themselves as an open -# standard) as the (only) precondition to have the right to use EtherCAT -# Technology, IP and trade marks. -# -#------------------------------------------------------------------------------ - -# Fetch parameters - -if [ $# -ne 1 ]; then - echo "This script is called by \"make\". Run \"make install\" instead." - exit 1 -fi - -KERNEL=$1 - -if [ ! -d /lib/modules/$KERNEL ]; then - echo "Kernel \"$KERNEL\" does not exist in /lib/modules!" - exit 1 -fi - -echo "EtherCAT installer - Kernel: $KERNEL" - -#------------------------------------------------------------------------------ - -# Update module dependencies - -echo " Building module dependencies" -/sbin/depmod - -#------------------------------------------------------------------------------ - -# Create configuration file - -CONFIGFILE=/etc/sysconfig/ethercat - -if [ -s $CONFIGFILE ]; then - echo " Note: Using existing configuration file." -else - echo " Creating $CONFIGFILE" - cp script/sysconfig $CONFIGFILE || exit 1 - echo " Note: Please edit DEVICE_INDEX in $CONFIGFILE!" -fi - -#------------------------------------------------------------------------------ - -# Install rc script - -echo " Installing startup script" -cp script/ethercat.sh /etc/init.d/ethercat || exit 1 -chmod +x /etc/init.d/ethercat || exit 1 -if [ ! -L /usr/sbin/rcethercat ]; then - ln -s /etc/init.d/ethercat /usr/sbin/rcethercat || exit 1 -fi - -#------------------------------------------------------------------------------ - -# Install tools - -echo " Installing tools" -cp script/lsec.pl /usr/local/bin/lsec || exit 1 -chmod +x /usr/local/bin/lsec || exit 1 - -#------------------------------------------------------------------------------ - -# Finish - -echo "Done" -exit 0 - -#------------------------------------------------------------------------------ diff -r dda32c8294ad -r d113b63c55c4 script/lsec --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/script/lsec Tue Feb 13 13:01:59 2007 +0000 @@ -0,0 +1,211 @@ +#!/usr/bin/perl + +#------------------------------------------------------------------------------ +# +# l s e c - List EtherCAT +# +# Userspace tool for listing EtherCAT slaves. +# +# $Id$ +# +# Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH +# +# This file is part of the IgH EtherCAT Master. +# +# The IgH EtherCAT Master is free software; you can redistribute it +# and/or modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 of the +# License, or (at your option) any later version. +# +# The IgH EtherCAT Master is distributed in the hope that it will be +# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with the IgH EtherCAT Master; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +# +# The right to use EtherCAT Technology is granted and comes free of +# charge under condition of compatibility of product made by +# Licensee. People intending to distribute/sell products based on the +# code, have to sign an agreement to guarantee that products using +# software based on IgH EtherCAT master stay compatible with the actual +# EtherCAT specification (which are released themselves as an open +# standard) as the (only) precondition to have the right to use EtherCAT +# Technology, IP and trade marks. +# +#------------------------------------------------------------------------------ + +require 'sys/ioctl.ph'; + +use strict; +use Getopt::Std; + +my %opt; +my $master_index; +my $master_dir; +my $term_width; + +#------------------------------------------------------------------------------ + +$term_width = &get_terminal_width; +&get_options; +&query_master; +exit 0; + +#------------------------------------------------------------------------------ + +sub query_master +{ + $master_dir = "/sys/ethercat" . $master_index; + &query_slaves; +} + +#------------------------------------------------------------------------------ + +sub query_slaves +{ + my $dirhandle; + my $entry; + my @slaves; + my $slave; + my $abs; + my $line; + my $ring_cols; + my $adv_cols; + my $fmt; + my $cols; + + unless (opendir $dirhandle, $master_dir) { + print "Failed to open directory \"$master_dir\".\n"; + exit 1; + } + + while ($entry = readdir $dirhandle) { + next unless $entry =~ /^slave(\d+)$/; + + $slave = {}; + + open INFO, "$master_dir/$entry/info" or die + "ERROR: Failed to open $master_dir/$entry/info"; + + while ($line = ) { + if ($line =~ /^Name: (.*)$/) { + $slave->{'name'} = $1; + } + elsif ($line =~ /^Ring position: (\d+)$/) { + $slave->{'ring_position'} = $1; + } + elsif ($line =~ /^Advanced position: (\d+:\d+)$/) { + $slave->{'advanced_position'} = $1; + } + elsif ($line =~ /^State: (.+) /) { + $slave->{'state'} = $1; + } + elsif ($line =~ /^Coupler: ([a-z]+)$/) { + $slave->{'coupler'} = $1; + } + elsif ($line =~ /^Current consumption: (-?\d+) mA$/) { + $slave->{'current'} = $1; + } + } + + close INFO; + + push @slaves, $slave; + } + closedir $dirhandle; + + @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves; + + $ring_cols = 0; + $adv_cols = 0; + for $slave (@slaves) { + $cols = length $slave->{'ring_position'}; + $ring_cols = $cols if ($cols > $ring_cols); + $cols = length $slave->{'advanced_position'}; + $adv_cols = $cols if ($cols > $adv_cols); + } + + if (defined $opt{'c'}) { # display power consumtion + $fmt = sprintf " %%%is %%-%is %%6i %%6i %%s\n", + $ring_cols, $adv_cols; + + my $current_sum = 0; + for $slave (@slaves) { + if ($slave->{'coupler'} eq "yes") { + &print_line if !defined $opt{n}; + $current_sum = 0; # reset current sum + } + $current_sum -= $slave->{'current'}; + printf($fmt, $slave->{'ring_position'}, + $slave->{'advanced_position'}, $slave->{'current'}, + $current_sum, $slave->{'name'}); + } + } + else { + $fmt = sprintf " %%%is %%-%is %%-6s %%s\n", $ring_cols, $adv_cols; + + for $slave (@slaves) { + &print_line if $slave->{'coupler'} eq "yes" and !defined $opt{n}; + printf($fmt, $slave->{'ring_position'}, + $slave->{'advanced_position'}, $slave->{'state'}, + $slave->{'name'}); + } + } +} + +#------------------------------------------------------------------------------ + +sub get_options +{ + my $optret = getopts "m:cnh", \%opt; + + &print_usage if defined $opt{h} or $#ARGV > -1 or !$optret; + + if (defined $opt{m}) { + $master_index = $opt{m}; + } + else { + $master_index = 0; + } +} + +#------------------------------------------------------------------------------ + +sub print_usage +{ + my $cmd = `basename $0`; + chomp $cmd; + print "Usage: $cmd [OPTIONS]\n"; + print " -m Query master .\n"; + print " -c Display current [mA] "; + print "(3: consumption, 4: remaining).\n"; + print " -n Display no coupler lines.\n"; + print " -h Show this help.\n"; + exit 0; +} + +#------------------------------------------------------------------------------ + +sub get_terminal_width +{ + my $winsize; + die "no TIOCGWINSZ " unless defined &TIOCGWINSZ; + open(TTY, "+) { - if ($line =~ /^Name: (.*)$/) { - $slave->{'name'} = $1; - } - elsif ($line =~ /^Ring position: (\d+)$/) { - $slave->{'ring_position'} = $1; - } - elsif ($line =~ /^Advanced position: (\d+:\d+)$/) { - $slave->{'advanced_position'} = $1; - } - elsif ($line =~ /^State: (.+) /) { - $slave->{'state'} = $1; - } - elsif ($line =~ /^Coupler: ([a-z]+)$/) { - $slave->{'coupler'} = $1; - } - elsif ($line =~ /^Current consumption: (-?\d+) mA$/) { - $slave->{'current'} = $1; - } - } - - close INFO; - - push @slaves, $slave; - } - closedir $dirhandle; - - @slaves = sort { $a->{'ring_position'} <=> $b->{'ring_position'} } @slaves; - - $ring_cols = 0; - $adv_cols = 0; - for $slave (@slaves) { - $cols = length $slave->{'ring_position'}; - $ring_cols = $cols if ($cols > $ring_cols); - $cols = length $slave->{'advanced_position'}; - $adv_cols = $cols if ($cols > $adv_cols); - } - - if (defined $opt{'c'}) { # display power consumtion - $fmt = sprintf " %%%is %%-%is %%6i %%6i %%s\n", - $ring_cols, $adv_cols; - - my $current_sum = 0; - for $slave (@slaves) { - if ($slave->{'coupler'} eq "yes") { - &print_line if !defined $opt{n}; - $current_sum = 0; # reset current sum - } - $current_sum -= $slave->{'current'}; - printf($fmt, $slave->{'ring_position'}, - $slave->{'advanced_position'}, $slave->{'current'}, - $current_sum, $slave->{'name'}); - } - } - else { - $fmt = sprintf " %%%is %%-%is %%-6s %%s\n", $ring_cols, $adv_cols; - - for $slave (@slaves) { - &print_line if $slave->{'coupler'} eq "yes" and !defined $opt{n}; - printf($fmt, $slave->{'ring_position'}, - $slave->{'advanced_position'}, $slave->{'state'}, - $slave->{'name'}); - } - } -} - -#------------------------------------------------------------------------------ - -sub get_options -{ - my $optret = getopts "m:cnh", \%opt; - - &print_usage if defined $opt{h} or $#ARGV > -1 or !$optret; - - if (defined $opt{m}) { - $master_index = $opt{m}; - } - else { - $master_index = 0; - } -} - -#------------------------------------------------------------------------------ - -sub print_usage -{ - my $cmd = `basename $0`; - chomp $cmd; - print "Usage: $cmd [OPTIONS]\n"; - print " -m Query master .\n"; - print " -c Display current [mA] "; - print "(3: consumption, 4: remaining).\n"; - print " -n Display no coupler lines.\n"; - print " -h Show this help.\n"; - exit 0; -} - -#------------------------------------------------------------------------------ - -sub get_terminal_width -{ - my $winsize; - die "no TIOCGWINSZ " unless defined &TIOCGWINSZ; - open(TTY, "+