summaryrefslogtreecommitdiff
path: root/zaptel.init
blob: 92c431d9fd08c7653d031ca68e9882805c13925e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/sh
#
# zaptel        This shell script takes care of loading and unloading \
#               Zapata Telephony interfaces
# chkconfig: 2345 9 92
# description: The zapata telephony drivers allow you to use your linux \
# computer to accept incoming data and voice interfaces
#
# config: /etc/sysconfig/zaptel

initdir=/etc/init.d

ZTCFG=/sbin/ztcfg
ZTCFG_CMD="$ZTCFG" # e.g: for a custom zaptel.conf location

XPP_SYNC=auto

#
# Determine which kind of configuration we're using
#
system=redhat  # assume redhat
if [ -f /etc/debian_version ]; then
    system=debian
fi

# Source function library.
if [ $system = redhat ]; then
    . $initdir/functions || exit 0
fi

# Source zaptel configuration.
if [ $system = debian ]; then
    [ -f /etc/default/zaptel ] && . /etc/default/zaptel
	LOCKFILE=/var/lock/zaptel
elif [ $system = redhat ]; then
    [ -f /etc/sysconfig/zaptel ] && . /etc/sysconfig/zaptel
	LOCKFILE=/var/lock/subsys/zaptel
fi

if [ -z "${MODULES}" ]; then 
	# Populate defaults if not present
	MODULES="tor2 wct4xxp wct1xxp wcte11xp wcfxo wctdm wctdm24xxp" 
fi

# recursively unload a module and its dependencies, if possible.
# where's modprobe -r when you need it?
# inputs: module to unload.
# returns: the result from 
unload_module() {
	module="$1"
	line=`lsmod 2>/dev/null | grep "^$1 "`
	if [ "$line" = '' ]; then return; fi # module was not loaded

	set -- $line
	# $1: the original module, $2: size, $3: refcount, $4: deps list
	mods=`echo $4 | tr , ' '`
	# xpp_usb keeps the xpds below busy if an xpp hardware is
	# connected. Hence must be removed before them:
	case "$module" in xpd_*) mods="xpp_usb $mods";; esac
	for mod in $mods; do
		# run in a subshell, so it won't step over our vars:
		(unload_module $mod) 
		# TODO: the following is probably the error handling we want:
		# if [ $? != 0 ]; then return 1; fi
	done
	rmmod $module
}

# Initialize the Xorcom Astribank (xpp/) using perl utiliites:
# intended to replace all the the three functions below if user has 
# installed the zaptel-perl utilities.
xpp_startup() {
	# do nothing if there are no astribank devices:
	if ! grep -q connected /proc/xpp/xbuses 2>/dev/null; then return 0; fi

	echo "Waiting for Astribank devices to initialize:"
	cat /proc/xpp/XBUS-[0-9]*/waitfor_xpds 2>/dev/null || true
	
	# overriding locales for the above two, as perl can be noisy
	# when locales are missing.
	# No register all the devices if they didn't auto-register:
	LC_ALL=C zt_registration on

	# this one could actually be run after ztcfg:
	LC_ALL=C xpp_sync "$XPP_SYNC"
}

hpec_start() {
	# check for HPEC licenses
	for f in /var/lib/digium/licenses/HPEC*.lic; do
		if [ -f ${f} ]; then
			haveHPEC=1
		fi
	done
	if [ -z "${haveHPEC}" ]; then return; fi

	# If we got here, HPEC license was found

	# zaphpec_enable not installed in /usr/sbin
	if [ ! -f /usr/sbin/zaphpec_enable ]; then
		echo -n "Running zaphpec_enable: Failed"
		echo -n "."
		echo "  The zaphpec_enable binary is not installed in /usr/sbin."
		return
	fi

	# zaphpec_enable not set executable
	if [ ! -x /usr/sbin/zaphpec_enable ]; then
		echo -n "Running zaphpec_enable: Failed"
		echo -n "."
		echo "  /usr/sbin/zaphpec_enable is not set as executable."
		return
	fi

	# zaphpec_enable properly installed
	if [ $system = debian ]; then
		echo -n "Running zaphpec_enable: "
		/usr/sbin/zaphpec_enable 2> /dev/null
	elif [ $system = redhat ]; then
		action "Running zaphpec_enable: " /usr/sbin/zaphpec_enable
	fi
	if [ $? = 0 ]; then
		echo -n "done"
		echo "."
	else
		echo -n "Failed"
		echo -n "."
		echo "  This can be caused if you had already run zaphpec_enable, or if your HPEC license is no longer valid."
	fi
}

shutdown_dynamic() {
	if ! grep -q ' ZTD/' /proc/* 2>/dev/null; then return; fi

	# we should only get here if we have dynamic spans. Right?
	$ZTCFG -s
}

# Check that telephony is up.
[ "${TELEPHONY}" = "yes" ] || exit 0

[ -x "$ZTCFG" ] || exit 0

[ -f /etc/zaptel.conf ] || exit 0

if [ "${DEBUG}" = "yes" ]; then
	ARGS="debug=1"
fi

run_ztcfg() {
	if [ $system = debian ]; then
	 	echo -n "Running ztcfg: " 
	 	$ZTCFG_CMD && echo -n "done"
		RETVAL=$?
		echo "."
	elif [ $system = redhat ]; then
		action "Running ztcfg: " $ZTCFG_CMD
		RETVAL=$?
	fi
}

RETVAL=0

# See how we were called.
case "$1" in
  start)
	#if [ $system = debian ]; then
	#    echo -n "Loading zaptel framework: " 
	#    modprobe zaptel ${ARGS} 2> /dev/null && echo -n "done"
	#    echo "."
	#elif [ $system = redhat ]; then
	#    action "Loading zaptel framework: " modprobe zaptel ${ARGS}
	#fi
	echo -n "Loading zaptel hardware modules:"
	for x in $MODULES; do 
		eval localARGS="\$${x}_ARGS"
		if modprobe ${x} ${ARGS} ${localARGS} 2> /dev/null; then
			echo -n " $x"
		fi
	done
	xpp_startup
	echo -n "Waiting for zap to come online..."
	TMOUT=10 # max secs to wait
	while [ ! -d /dev/zap ] ; do
 		sleep 1
		TMOUT=`expr $TMOUT - 1`
		if [ $TMOUT -eq 0 ] ; then
			echo "Error: missing /dev/zap!"
			exit 1
		fi
	done
	echo "OK"
	#sleep 3 # TODO: remove it
	
	if head -c 0 /dev/zap/pseudo; then 
		echo "No Zaptel timing source sound. loading ztdummy"
		modprobe ztdummy 
	fi

	run_ztcfg

	RETVAL=$?

	[ $RETVAL -eq 0 ] && touch $LOCKFILE
	
	hpec_start
	;;
  stop)
	# Unload drivers
	shutdown_dynamic # FIXME: needs test from someone with dynamic spans

	echo -n "Unloading zaptel hardware drivers:"
  	unload_module zaptel
	RETVAL=$?
	echo "."

	[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
	;;
  unload)
	# We don't have zaptel helper, so let's not replicate too much code:
	# allow others to use the unload command.
  	unload_module zaptel
	;;
  restart)
	$0 stop
	$0 start
	;;
  reload)
	run_ztcfg
	
	;;
  *)
	echo "Usage: zaptel {start|stop|restart|reload}"
	exit 1
esac

exit $RETVAL