author | Edouard Tisserant <edouard.tisserant@gmail.com> |
Mon, 08 Oct 2018 23:16:34 +0200 | |
branch | stable-1.5 |
changeset 2726 | ca80d6dac4c8 |
parent 2710 | 05359dee3578 |
permissions | -rwxr-xr-x |
2689 | 1 |
#!/bin/bash |
2192 | 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 |
||
2543
0d978e56a635
Use absolute path fpr ldmod; thanks to S. Blanchet.
Florian Pose <fp@igh-essen.com>
parents:
2193
diff
changeset
|
36 |
LSMOD=/sbin/lsmod |
2192 | 37 |
MODPROBE=/sbin/modprobe |
38 |
RMMOD=/sbin/rmmod |
|
39 |
MODINFO=/sbin/modinfo |
|
40 |
||
41 |
ETHERCAT=@prefix@/bin/ethercat |
|
42 |
||
43 |
#------------------------------------------------------------------------------ |
|
44 |
||
45 |
ETHERCAT_CONFIG=/etc/ethercat.conf |
|
46 |
||
47 |
if [ ! -r ${ETHERCAT_CONFIG} ]; then |
|
48 |
echo ${ETHERCAT_CONFIG} not existing; |
|
49 |
exit 6 |
|
50 |
fi |
|
51 |
||
52 |
. ${ETHERCAT_CONFIG} |
|
53 |
||
54 |
#------------------------------------------------------------------------------ |
|
55 |
||
56 |
parse_mac_address() { |
|
57 |
if [ -z "${1}" ]; then |
|
58 |
MAC="" |
|
59 |
elif echo ${1} | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then |
|
60 |
MAC=${1} |
|
61 |
else |
|
62 |
echo Invalid MAC address \"${1}\" in ${ETHERCAT_CONFIG} |
|
63 |
exit 1 |
|
64 |
fi |
|
65 |
} |
|
66 |
||
67 |
#------------------------------------------------------------------------------ |
|
68 |
||
69 |
case "${1}" in |
|
70 |
||
71 |
start) |
|
72 |
# construct DEVICES and BACKUPS from configuration variables |
|
73 |
DEVICES="" |
|
74 |
BACKUPS="" |
|
75 |
MASTER_INDEX=0 |
|
76 |
||
77 |
while true; do |
|
78 |
DEVICE=$(eval echo "\${MASTER${MASTER_INDEX}_DEVICE}") |
|
79 |
BACKUP=$(eval echo "\${MASTER${MASTER_INDEX}_BACKUP}") |
|
80 |
if [ -z "${DEVICE}" ]; then break; fi |
|
81 |
||
82 |
if [ ${MASTER_INDEX} -gt 0 ]; then |
|
83 |
DEVICES=${DEVICES}, |
|
84 |
BACKUPS=${BACKUPS}, |
|
85 |
fi |
|
86 |
||
87 |
parse_mac_address ${DEVICE} |
|
88 |
DEVICES=${DEVICES}${MAC} |
|
89 |
||
90 |
parse_mac_address ${BACKUP} |
|
91 |
BACKUPS=${BACKUPS}${MAC} |
|
92 |
||
93 |
MASTER_INDEX=$(expr ${MASTER_INDEX} + 1) |
|
94 |
done |
|
95 |
||
96 |
# load master module |
|
97 |
if ! ${MODPROBE} ${MODPROBE_FLAGS} ec_master \ |
|
98 |
main_devices=${DEVICES} backup_devices=${BACKUPS}; then |
|
99 |
exit 1 |
|
100 |
fi |
|
101 |
||
102 |
LOADED_MODULES=ec_master |
|
103 |
||
104 |
# check for modules to replace |
|
105 |
for MODULE in ${DEVICE_MODULES}; do |
|
106 |
ECMODULE=ec_${MODULE} |
|
107 |
if ! ${MODINFO} ${ECMODULE} > /dev/null; then |
|
108 |
continue # ec_* module not found |
|
109 |
fi |
|
110 |
||
2710
05359dee3578
rtdmnet module should follow same (un)loading rules as generic module
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2689
diff
changeset
|
111 |
if [ ${MODULE} != "generic" -a ${MODULE} != "rtdmnet" -a ${MODULE} != "ccat" ]; then |
2192 | 112 |
# try to unload standard module |
2543
0d978e56a635
Use absolute path fpr ldmod; thanks to S. Blanchet.
Florian Pose <fp@igh-essen.com>
parents:
2193
diff
changeset
|
113 |
if ${LSMOD} | grep "^${MODULE} " > /dev/null; then |
2192 | 114 |
if ! ${RMMOD} ${MODULE}; then |
115 |
${RMMOD} ${LOADED_MODULES} |
|
116 |
exit 1 |
|
117 |
fi |
|
118 |
fi |
|
119 |
fi |
|
120 |
||
121 |
if ! ${MODPROBE} ${MODPROBE_FLAGS} ${ECMODULE}; then |
|
2710
05359dee3578
rtdmnet module should follow same (un)loading rules as generic module
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2689
diff
changeset
|
122 |
if [ ${MODULE} != "generic" -a ${MODULE} != "rtdmnet" -a ${MODULE} != "ccat" ]; then |
2192 | 123 |
${MODPROBE} ${MODPROBE_FLAGS} ${MODULE} # try to restore |
124 |
fi |
|
125 |
${RMMOD} ${LOADED_MODULES} |
|
126 |
exit 1 |
|
127 |
fi |
|
128 |
||
2193
334f4530c1df
Fixed appending to LOADED_MODULES variable.
Florian Pose <fp@igh-essen.com>
parents:
2192
diff
changeset
|
129 |
LOADED_MODULES="${ECMODULE} ${LOADED_MODULES}" |
2192 | 130 |
done |
131 |
||
132 |
exit 0 |
|
133 |
;; |
|
134 |
||
135 |
#------------------------------------------------------------------------------ |
|
136 |
||
137 |
stop) |
|
138 |
# unload EtherCAT device modules |
|
139 |
for MODULE in ${DEVICE_MODULES} master; do |
|
140 |
ECMODULE=ec_${MODULE} |
|
2543
0d978e56a635
Use absolute path fpr ldmod; thanks to S. Blanchet.
Florian Pose <fp@igh-essen.com>
parents:
2193
diff
changeset
|
141 |
if ! ${LSMOD} | grep -q "^${ECMODULE} "; then |
2192 | 142 |
continue # ec_* module not loaded |
143 |
fi |
|
144 |
if ! ${RMMOD} ${ECMODULE}; then |
|
145 |
exit 1 |
|
146 |
fi; |
|
147 |
done |
|
148 |
||
149 |
sleep 1 |
|
150 |
||
151 |
# load standard modules again |
|
152 |
for MODULE in ${DEVICE_MODULES}; do |
|
2710
05359dee3578
rtdmnet module should follow same (un)loading rules as generic module
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
2689
diff
changeset
|
153 |
if [ ${MODULE} == "generic" -o ${MODULE} == "rtdmnet" -o ${MODULE} == "ccat" ]; then |
2192 | 154 |
continue |
155 |
fi |
|
156 |
${MODPROBE} ${MODPROBE_FLAGS} ${MODULE} |
|
157 |
done |
|
158 |
||
159 |
exit 0 |
|
160 |
;; |
|
161 |
||
162 |
#------------------------------------------------------------------------------ |
|
163 |
||
164 |
restart) |
|
165 |
$0 stop || exit 1 |
|
166 |
sleep 1 |
|
167 |
$0 start |
|
168 |
;; |
|
169 |
||
170 |
#------------------------------------------------------------------------------ |
|
171 |
||
172 |
status) |
|
173 |
echo "Checking for EtherCAT master @VERSION@ " |
|
174 |
||
175 |
# count masters in configuration file |
|
176 |
MASTER_COUNT=0 |
|
177 |
while true; do |
|
178 |
DEVICE=$(eval echo "\${MASTER${MASTER_COUNT}_DEVICE}") |
|
179 |
if [ -z "${DEVICE}" ]; then break; fi |
|
180 |
MASTER_COUNT=$(expr ${MASTER_COUNT} + 1) |
|
181 |
done |
|
182 |
||
183 |
RESULT=0 |
|
184 |
||
185 |
for i in `seq 0 $(expr ${MASTER_COUNT} - 1)`; do |
|
186 |
echo -n "Master${i} " |
|
187 |
||
188 |
# Check if the master is in idle or operation phase |
|
189 |
${ETHERCAT} master --master ${i} 2>/dev/null | \ |
|
190 |
grep -qE 'Phase:[[:space:]]*Idle|Phase:[[:space:]]*Operation' |
|
191 |
EXITCODE=$? |
|
192 |
||
193 |
if [ ${EXITCODE} -eq 0 ]; then |
|
194 |
echo " running" |
|
195 |
else |
|
196 |
echo " dead" |
|
197 |
RESULT=1 |
|
198 |
fi |
|
199 |
done |
|
200 |
||
201 |
exit ${RESULT} |
|
202 |
;; |
|
203 |
||
204 |
#------------------------------------------------------------------------------ |
|
205 |
||
206 |
*) |
|
207 |
echo "USAGE: $0 {start|stop|restart|status}" |
|
208 |
exit 1 |
|
209 |
;; |
|
210 |
esac |
|
211 |
||
212 |
#------------------------------------------------------------------------------ |