script/ethercatctl.in
branchstable-1.5
changeset 2192 1c7c546ac08c
child 2190 3fe304fea395
equal deleted inserted replaced
2191:54cda823743a 2192:1c7c546ac08c
       
     1 #!/bin/sh
       
     2 
       
     3 #------------------------------------------------------------------------------
       
     4 #
       
     5 #  Start script for EtherCAT to use with systemd
       
     6 #
       
     7 #  $Id$
       
     8 #
       
     9 #  Copyright (C) 2006-2012  Florian Pose, Ingenieurgemeinschaft IgH
       
    10 #
       
    11 #  This file is part of the IgH EtherCAT Master.
       
    12 #
       
    13 #  The IgH EtherCAT Master is free software; you can redistribute it and/or
       
    14 #  modify it under the terms of the GNU General Public License version 2, as
       
    15 #  published by the Free Software Foundation.
       
    16 #
       
    17 #  The IgH EtherCAT Master is distributed in the hope that it will be useful,
       
    18 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    19 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
       
    20 #  Public License for more details.
       
    21 #
       
    22 #  You should have received a copy of the GNU General Public License along
       
    23 #  with the IgH EtherCAT Master; if not, write to the Free Software
       
    24 #  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    25 #
       
    26 #  ---
       
    27 #
       
    28 #  The license mentioned above concerns the source code only. Using the
       
    29 #  EtherCAT technology and brand is only permitted in compliance with the
       
    30 #  industrial property and similar rights of Beckhoff Automation GmbH.
       
    31 #
       
    32 #  vim: expandtab sw=4 tw=78
       
    33 #
       
    34 #------------------------------------------------------------------------------
       
    35 
       
    36 MODPROBE=/sbin/modprobe
       
    37 RMMOD=/sbin/rmmod
       
    38 MODINFO=/sbin/modinfo
       
    39 
       
    40 ETHERCAT=@prefix@/bin/ethercat
       
    41 
       
    42 #------------------------------------------------------------------------------
       
    43 
       
    44 ETHERCAT_CONFIG=/etc/ethercat.conf
       
    45 
       
    46 if [ ! -r ${ETHERCAT_CONFIG} ]; then
       
    47     echo ${ETHERCAT_CONFIG} not existing;
       
    48     exit 6
       
    49 fi
       
    50 
       
    51 . ${ETHERCAT_CONFIG}
       
    52 
       
    53 #------------------------------------------------------------------------------
       
    54 
       
    55 parse_mac_address() {
       
    56     if [ -z "${1}" ]; then
       
    57         MAC=""
       
    58     elif echo ${1} | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then
       
    59         MAC=${1}
       
    60     else
       
    61         echo Invalid MAC address \"${1}\" in ${ETHERCAT_CONFIG}
       
    62         exit 1
       
    63     fi
       
    64 }
       
    65 
       
    66 #------------------------------------------------------------------------------
       
    67 
       
    68 case "${1}" in
       
    69 
       
    70 start)
       
    71     # construct DEVICES and BACKUPS from configuration variables
       
    72     DEVICES=""
       
    73     BACKUPS=""
       
    74     MASTER_INDEX=0
       
    75 
       
    76     while true; do
       
    77         DEVICE=$(eval echo "\${MASTER${MASTER_INDEX}_DEVICE}")
       
    78         BACKUP=$(eval echo "\${MASTER${MASTER_INDEX}_BACKUP}")
       
    79         if [ -z "${DEVICE}" ]; then break; fi
       
    80 
       
    81         if [ ${MASTER_INDEX} -gt 0 ]; then
       
    82             DEVICES=${DEVICES},
       
    83             BACKUPS=${BACKUPS},
       
    84         fi
       
    85 
       
    86         parse_mac_address ${DEVICE}
       
    87         DEVICES=${DEVICES}${MAC}
       
    88 
       
    89         parse_mac_address ${BACKUP}
       
    90         BACKUPS=${BACKUPS}${MAC}
       
    91 
       
    92         MASTER_INDEX=$(expr ${MASTER_INDEX} + 1)
       
    93     done
       
    94 
       
    95     # load master module
       
    96     if ! ${MODPROBE} ${MODPROBE_FLAGS} ec_master \
       
    97             main_devices=${DEVICES} backup_devices=${BACKUPS}; then
       
    98         exit 1
       
    99     fi
       
   100 
       
   101     LOADED_MODULES=ec_master
       
   102 
       
   103     # check for modules to replace
       
   104     for MODULE in ${DEVICE_MODULES}; do
       
   105         ECMODULE=ec_${MODULE}
       
   106         if ! ${MODINFO} ${ECMODULE} > /dev/null; then
       
   107             continue # ec_* module not found
       
   108         fi
       
   109 
       
   110         if [ ${MODULE} != "generic" ]; then
       
   111             # try to unload standard module
       
   112             if lsmod | grep "^${MODULE} " > /dev/null; then
       
   113                 if ! ${RMMOD} ${MODULE}; then
       
   114                     ${RMMOD} ${LOADED_MODULES}
       
   115                     exit 1
       
   116                 fi
       
   117             fi
       
   118         fi
       
   119 
       
   120         if ! ${MODPROBE} ${MODPROBE_FLAGS} ${ECMODULE}; then
       
   121             if [ ${MODULE} != "generic" ]; then
       
   122                 ${MODPROBE} ${MODPROBE_FLAGS} ${MODULE} # try to restore
       
   123             fi
       
   124             ${RMMOD} ${LOADED_MODULES}
       
   125             exit 1
       
   126         fi
       
   127 
       
   128         LOADED_MODULES=${ECMODULE} ${LOADED_MODULES}
       
   129     done
       
   130 
       
   131     exit 0
       
   132     ;;
       
   133 
       
   134 #------------------------------------------------------------------------------
       
   135 
       
   136 stop)
       
   137     # unload EtherCAT device modules
       
   138     for MODULE in ${DEVICE_MODULES} master; do
       
   139         ECMODULE=ec_${MODULE}
       
   140         if ! lsmod | grep -q "^${ECMODULE} "; then
       
   141             continue # ec_* module not loaded
       
   142         fi
       
   143         if ! ${RMMOD} ${ECMODULE}; then
       
   144             exit 1
       
   145         fi;
       
   146     done
       
   147 
       
   148     sleep 1
       
   149 
       
   150     # load standard modules again
       
   151     for MODULE in ${DEVICE_MODULES}; do
       
   152         if [ ${MODULE} == "generic" ]; then
       
   153             continue
       
   154         fi
       
   155         ${MODPROBE} ${MODPROBE_FLAGS} ${MODULE}
       
   156     done
       
   157 
       
   158     exit 0
       
   159     ;;
       
   160 
       
   161 #------------------------------------------------------------------------------
       
   162 
       
   163 restart)
       
   164     $0 stop || exit 1
       
   165     sleep 1
       
   166     $0 start
       
   167     ;;
       
   168 
       
   169 #------------------------------------------------------------------------------
       
   170 
       
   171 status)
       
   172     echo "Checking for EtherCAT master @VERSION@ "
       
   173 
       
   174     # count masters in configuration file
       
   175     MASTER_COUNT=0
       
   176     while true; do
       
   177         DEVICE=$(eval echo "\${MASTER${MASTER_COUNT}_DEVICE}")
       
   178         if [ -z "${DEVICE}" ]; then break; fi
       
   179         MASTER_COUNT=$(expr ${MASTER_COUNT} + 1)
       
   180     done
       
   181 
       
   182     RESULT=0
       
   183 
       
   184     for i in `seq 0 $(expr ${MASTER_COUNT} - 1)`; do
       
   185         echo -n "Master${i} "
       
   186 
       
   187         # Check if the master is in idle or operation phase
       
   188         ${ETHERCAT} master --master ${i} 2>/dev/null | \
       
   189             grep -qE 'Phase:[[:space:]]*Idle|Phase:[[:space:]]*Operation'
       
   190         EXITCODE=$?
       
   191 
       
   192         if [ ${EXITCODE} -eq 0 ]; then
       
   193             echo " running"
       
   194         else
       
   195             echo " dead"
       
   196             RESULT=1
       
   197         fi
       
   198     done
       
   199 
       
   200     exit ${RESULT}
       
   201     ;;
       
   202 
       
   203 #------------------------------------------------------------------------------
       
   204 
       
   205 *)
       
   206     echo "USAGE: $0 {start|stop|restart|status}"
       
   207     exit 1
       
   208     ;;
       
   209 esac
       
   210 
       
   211 #------------------------------------------------------------------------------